Add YNL test framework and library improvements
Subsystems (4)
v4 cover Add YNL test framework and library improvements
This series enhances YNL tools with some functionalities and adds YNL test framework.
Changes include:
- Add MAC address parsing support in YNL library
- Support ipv4-or-v6 display hint for dual-stack fields
- Add tests covering CLI and ethtool functionality
The tests provide usage examples and regression testing for YNL tools.
# make run_tests TAP version 13 1..9 ok 1 YNL CLI list families ok 2 YNL CLI netdev operations ok 3 YNL CLI ethtool operations ok 4 YNL CLI rt-route operations ok 5 YNL CLI rt-addr operations ok 6 YNL CLI rt-link operations ok 7 YNL CLI rt-neigh operations ok 8 YNL CLI rt-rule operations ok 9 YNL CLI nlctrl getfamily # Totals: pass:9 fail:0 xfail:0 xpass:0 skip:0 error:0 TAP version 13 1..8 ok 1 YNL ethtool device info ok 2 YNL ethtool statistics ok 3 YNL ethtool ring parameters (show/set) ok 4 YNL ethtool coalesce parameters (show/set) ok 5 YNL ethtool pause parameters (show/set) ok 6 YNL ethtool features info (show/set) ok 7 YNL ethtool channels info (show/set) ok 8 YNL ethtool time stamping # Totals: pass:8 fail:0 xfail:0 xpass:0 skip:0 error:0
v4: Use KTAP helper to report the test result (Matthieu Baerts)
iterate through $(TESTS) instead of being hard coded (Donald Hunter)
v3: add make run_tests to run all the tests at a time (Jakub Kicinski)
use ipv4-or-v6 display hint for dual-stack fields (Jakub Kicinski) check sysfs in case of netdevsim buildin (Sabrina Dubroca)
v2: move test from selftest to ynl folder (Jakub Kicinski)
Link: https://lore.kernel.org/netdev/[email protected]
v1: Link: https://lore.kernel.org/netdev/[email protected]
Hangbin Liu (3):
tools: ynl: Add MAC address parsing support
netlink: specs: support ipv4-or-v6 for dual-stack fields
tools: ynl: add YNL test framework
Documentation/netlink/genetlink-c.yaml | 2 +- Documentation/netlink/genetlink.yaml | 2 +- Documentation/netlink/netlink-raw.yaml | 2 +- Documentation/netlink/specs/rt-addr.yaml | 6 +- Documentation/netlink/specs/rt-link.yaml | 16 +- Documentation/netlink/specs/rt-neigh.yaml | 2 +- Documentation/netlink/specs/rt-route.yaml | 8 +- Documentation/netlink/specs/rt-rule.yaml | 6 +- tools/net/ynl/Makefile | 8 +- tools/net/ynl/pyynl/lib/ynl.py | 9 + tools/net/ynl/tests/Makefile | 32 +++ tools/net/ynl/tests/config | 6 + tools/net/ynl/tests/test_ynl_cli.sh | 309 ++++++++++++++++++++++ tools/net/ynl/tests/test_ynl_ethtool.sh | 206 +++++++++++++++ 14 files changed, 591 insertions(+), 23 deletions(-) create mode 100644 tools/net/ynl/tests/Makefile create mode 100644 tools/net/ynl/tests/config create mode 100755 tools/net/ynl/tests/test_ynl_cli.sh create mode 100755 tools/net/ynl/tests/test_ynl_ethtool.sh
Signature · 1 line
2.50.1
PATCH v4 1/3 tools: ynl: Add MAC address parsing support
Add missing support for parsing MAC addresses when display_hint is 'mac' in the YNL library. This enables YNL CLI to accept MAC address strings for attributes like lladdr in rt-neigh operations.
--- tools/net/ynl/pyynl/lib/ynl.py | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/net/ynl/pyynl/lib/ynl.py +9 -0
@@ -985,6 +985,15 @@ class YnlFamily(SpecFamily): raw = bytes.fromhex(string) else: raw = int(string, 16) + elif attr_spec.display_hint == 'mac': + # Parse MAC address in format "00:11:22:33:44:55" or "001122334455" + if ':' in string: + mac_bytes = [int(x, 16) for x in string.split(':')] + else: + if len(string) % 2 != 0: + raise Exception(f"Invalid MAC address format: {string}") + mac_bytes = [int(string[i:i+2], 16) for i in range(0, len(string), 2)] + raw = bytes(mac_bytes) else: raise Exception(f"Display hint '{attr_spec.display_hint}' not implemented" f" when parsing '{attr_spec['name']}'")
Signature · 1 line
2.50.1
PATCH v4 2/3 netlink: specs: support ipv4-or-v6 for dual-stack fields
In reply to Hangbin LiuSince commit 1b255e1beabf ("tools: ynl: add ipv4-or-v6 display hint"), we can display either IPv4 or IPv6 addresses for a single field based on the address family. However, most dual-stack fields still use the ipv4 display hint. This update changes them to use the new ipv4-or-v6 display hint and converts IPv4-only fields to use the u32 type.
Field changes:
- v4-or-v6
- IFA_ADDRESS, IFA_LOCAL
- IFLA_GRE_LOCAL, IFLA_GRE_REMOTE
- IFLA_VTI_LOCAL, IFLA_VTI_REMOTE
- IFLA_IPTUN_LOCAL, IFLA_IPTUN_REMOTE
- NDA_DST
- RTA_DST, RTA_SRC, RTA_GATEWAY, RTA_PREFSRC
- FRA_SRC, FRA_DST
- ipv4
- IFA_BROADCAST
- IFLA_GENEVE_REMOTE
- IFLA_IPTUN_6RD_RELAY_PREFIX
--- Documentation/netlink/genetlink-c.yaml | 2 +- Documentation/netlink/genetlink.yaml | 2 +- Documentation/netlink/netlink-raw.yaml | 2 +- Documentation/netlink/specs/rt-addr.yaml | 6 +++--- Documentation/netlink/specs/rt-link.yaml | 16 ++++++++-------- Documentation/netlink/specs/rt-neigh.yaml | 2 +- Documentation/netlink/specs/rt-route.yaml | 8 ++++---- Documentation/netlink/specs/rt-rule.yaml | 6 ++++-- 8 files changed, 23 insertions(+), 21 deletions(-)
Documentation/netlink/genetlink-c.yaml +1 -1
@@ -227,7 +227,7 @@ properties: Optional format indicator that is intended only for choosing the right formatting mechanism when displaying values of this type. - enum: [ hex, mac, fddi, ipv4, ipv6, uuid ] + enum: [ hex, mac, fddi, ipv4, ipv6, ipv4-or-v6, uuid ] # Start genetlink-c name-prefix: type: string
Documentation/netlink/genetlink.yaml +1 -1
@@ -185,7 +185,7 @@ properties: Optional format indicator that is intended only for choosing the right formatting mechanism when displaying values of this type. - enum: [ hex, mac, fddi, ipv4, ipv6, uuid ] + enum: [ hex, mac, fddi, ipv4, ipv6, ipv4-or-v6, uuid ] # Make sure name-prefix does not appear in subsets (subsets inherit naming) dependencies:
Documentation/netlink/netlink-raw.yaml +1 -1
@@ -157,7 +157,7 @@ properties: Optional format indicator that is intended only for choosing the right formatting mechanism when displaying values of this type. - enum: [ hex, mac, fddi, ipv4, ipv6, uuid ] + enum: [ hex, mac, fddi, ipv4, ipv6, ipv4-or-v6, uuid ] struct: description: Name of the nested struct type. type: string
Documentation/netlink/specs/rt-addr.yaml +3 -3
@@ -86,17 +86,17 @@ attribute-sets: - name: address type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: local type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: label type: string - name: broadcast - type: binary + type: u32 display-hint: ipv4 - name: anycast
Documentation/netlink/specs/rt-link.yaml +8 -8
@@ -1707,11 +1707,11 @@ attribute-sets: - name: local type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: remote type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: ttl type: u8
@@ -1833,11 +1833,11 @@ attribute-sets: - name: local type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: remote type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: fwmark type: u32
@@ -1868,7 +1868,7 @@ attribute-sets: type: u32 - name: remote - type: binary + type: u32 display-hint: ipv4 - name: ttl
@@ -1952,11 +1952,11 @@ attribute-sets: - name: local type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: remote type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: ttl type: u8
@@ -1986,7 +1986,7 @@ attribute-sets: display-hint: ipv6 - name: 6rd-relay-prefix - type: binary + type: u32 display-hint: ipv4 - name: 6rd-prefixlen
Documentation/netlink/specs/rt-neigh.yaml +1 -1
@@ -194,7 +194,7 @@ attribute-sets: - name: dst type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: lladdr type: binary
Documentation/netlink/specs/rt-route.yaml +4 -4
@@ -87,11 +87,11 @@ attribute-sets: - name: dst type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: src type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: iif type: u32
@@ -101,14 +101,14 @@ attribute-sets: - name: gateway type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: priority type: u32 - name: prefsrc type: binary - display-hint: ipv4 + display-hint: ipv4-or-v6 - name: metrics type: nest
Documentation/netlink/specs/rt-rule.yaml +4 -2
@@ -96,10 +96,12 @@ attribute-sets: attributes: - name: dst - type: u32 + type: binary + display-hint: ipv4-or-v6 - name: src - type: u32 + type: binary + display-hint: ipv4-or-v6 - name: iifname type: string
Signature · 1 line
2.50.1
PATCH v4 3/3 tools: ynl: add YNL test framework
In reply to Hangbin LiuAdd a test framework for YAML Netlink (YNL) tools, covering both CLI and ethtool functionality. The framework includes:
1) cli: family listing, netdev, ethtool, rt-* families, and nlctrl operations
2) ethtool: device info, statistics, ring/coalesce/pause parameters, and feature gettings
The current YNL syntax is a bit obscure, and end users may not always know how to use it. This test framework provides usage examples and also serves as a regression test to catch potential breakages caused by future changes.
--- tools/net/ynl/Makefile | 8 +- tools/net/ynl/tests/Makefile | 32 +++ tools/net/ynl/tests/config | 6 + tools/net/ynl/tests/test_ynl_cli.sh | 309 ++++++++++++++++++++++++ tools/net/ynl/tests/test_ynl_ethtool.sh | 206 ++++++++++++++++ 5 files changed, 559 insertions(+), 2 deletions(-) create mode 100644 tools/net/ynl/tests/Makefile create mode 100644 tools/net/ynl/tests/config create mode 100755 tools/net/ynl/tests/test_ynl_cli.sh create mode 100755 tools/net/ynl/tests/test_ynl_ethtool.sh
tools/net/ynl/Makefile +6 -2
@@ -12,7 +12,7 @@ endif libdir ?= $(prefix)/$(libdir_relative) includedir ?= $(prefix)/include -SUBDIRS = lib generated samples ynltool +SUBDIRS = lib generated samples ynltool tests all: $(SUBDIRS) libynl.a
@@ -49,5 +49,9 @@ install: libynl.a lib/*.h @echo -e "\tINSTALL pyynl" @pip install --prefix=$(DESTDIR)$(prefix) . @make -C generated install + @make -C tests install -.PHONY: all clean distclean install $(SUBDIRS) +run_tests: + @$(MAKE) -C tests run_tests + +.PHONY: all clean distclean install run_tests $(SUBDIRS)
tools/net/ynl/tests/Makefile new file +32 -0
@@ -0,0 +1,32 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for YNL tests + +TESTS := \ + test_ynl_cli.sh \ + test_ynl_ethtool.sh \ +# end of TESTS + +all: $(TESTS) + +run_tests: + @for test in $(TESTS); do \ + ./$$test; \ + done + +install: $(TESTS) + @mkdir -p $(DESTDIR)/usr/bin + @mkdir -p $(DESTDIR)/usr/share/kselftest + @cp ../../../testing/selftests/kselftest/ktap_helpers.sh $(DESTDIR)/usr/share/kselftest/ + @for test in $(TESTS); do \ + name=$$(basename $$test .sh); \ + sed -e 's|^ynl=.*|ynl="ynl"|' \ + -e 's|^ynl_ethtool=.*|ynl_ethtool="ynl-ethtool"|' \ + -e 's|KSELFTEST_KTAP_HELPERS=.*|KSELFTEST_KTAP_HELPERS="/usr/share/kselftest/ktap_helpers.sh"|' \ + $$test > $(DESTDIR)/usr/bin/$$name; \ + chmod +x $(DESTDIR)/usr/bin/$$name; \ + done + +clean: + @# Nothing to clean + +.PHONY: all install clean run_tests
tools/net/ynl/tests/config new file +6 -0
@@ -0,0 +1,6 @@ +CONFIG_DUMMY=m +CONFIG_INET_DIAG=y +CONFIG_IPV6=y +CONFIG_NET_NS=y +CONFIG_NETDEVSIM=m +CONFIG_VETH=m
tools/net/ynl/tests/test_ynl_cli.sh new file +309 -0
309 lines not on this page · raw message
tools/net/ynl/tests/test_ynl_ethtool.sh new file +206 -0
206 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [PATCHv4 net-next 3/3] tools: ynl: add YNL test framework
Quoted 22 lines · Hangbin Liu
tools/net/ynl/tests/test_ynl_cli.sh new file +21
-0+cleanup() { + if [[ -n "$testns" ]]; then + ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true + ip netns del "$testns" 2>/dev/null || true + fi +} + +# Check if ynl command is available +if ! command -v $ynl &>/dev/null && [[ ! -x $ynl ]]; then + ktap_skip_all "ynl command not found: $ynl" + exit "$KSFT_SKIP" +fi + +trap cleanup EXIT + +ktap_print_header +ktap_set_plan 9 +setup + +# Run all tests +cli_list_families
tools/net/ynl/tests/test_ynl_cli.sh line 309
Quoted 10 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +10
-0+cli_netdev_ops +cli_ethtool_ops +cli_rt_route_ops +cli_rt_addr_ops +cli_rt_link_ops +cli_rt_neigh_ops +cli_rt_rule_ops +cli_nlctrl_ops + +ktap_finished
minor nit: ktap_finished should probably be in the 'cleanup' trap handler
Quoted 20 lines
tools/net/ynl/tests/test_ynl_ethtool.sh new file +20
-0+cleanup() { + if [[ -n "$testns" ]]; then + ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true + ip netns del "$testns" 2>/dev/null || true + fi +} + +# Check if ynl-ethtool command is available +if ! command -v $ynl_ethtool &>/dev/null && [[ ! -x $ynl_ethtool ]]; then + ktap_skip_all "ynl-ethtool command not found: $ynl_ethtool" + exit "$KSFT_SKIP" +fi + +trap cleanup EXIT + +ktap_print_header +ktap_set_plan 8 +setup + +# Run all tests
tools/net/ynl/tests/test_ynl_ethtool.sh line 206
Quoted 10 lines
tools/net/ynl/tests/test_ynl_ethtool.sh new file +10
-0+ethtool_device_info +ethtool_statistics +ethtool_ring_params +ethtool_coalesce_params +ethtool_pause_params +ethtool_features_info +ethtool_channels_info +ethtool_time_stamping + +ktap_finished
And here.
Otherwise LGTM.
Re: [PATCHv4 net-next 3/3] tools: ynl: add YNL test framework
Hi Donald,
Quoted 26 lines · Donald Hunter
Quoted 25 lines · Hangbin Liu
tools/net/ynl/tests/test_ynl_cli.sh new file +24
-0+cleanup() { + if [[ -n "$testns" ]]; then + ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true + ip netns del "$testns" 2>/dev/null || true + fi +} + +# Check if ynl command is available +if ! command -v $ynl &>/dev/null && [[ ! -x $ynl ]]; then + ktap_skip_all "ynl command not found: $ynl" + exit "$KSFT_SKIP" +fi + +trap cleanup EXIT + +ktap_print_header +ktap_set_plan 9>> +setup + +# Run all tests +cli_list_families +cli_netdev_ops +cli_ethtool_ops +cli_rt_route_ops +cli_rt_addr_ops
Quoted 8 lines
tools/net/ynl/tests/test_ynl_cli.sh line 309
Quoted 6 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +6
-0+cli_rt_link_ops +cli_rt_neigh_ops +cli_rt_rule_ops +cli_nlctrl_ops + +ktap_finishedminor nit:
ktap_finishedshould probably be in the 'cleanup' trap handler
@Donald: I don't think 'ktap_finished' should be called there: in case of errors with an early exit during the setup phase, the two scripts will call 'ktap_skip_all', then 'exit "$KSFT_SKIP"'. If 'ktap_finished' is called in the 'cleanup' trap, it will print a total with everything set to 0 and call 'exit' again with other values (and no effects). So I think it is not supposed to be called from the exit trap.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Donald Hunter
maintainerRe: [PATCHv4 net-next 3/3] tools: ynl: add YNL test framework
Quoted 38 lines · Matthieu Baerts
Hi Donald,
Quoted 26 lines · Donald Hunter
Quoted 25 lines · Hangbin Liu
tools/net/ynl/tests/test_ynl_cli.sh new file +24
-0+cleanup() { + if [[ -n "$testns" ]]; then + ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true + ip netns del "$testns" 2>/dev/null || true + fi +} + +# Check if ynl command is available +if ! command -v $ynl &>/dev/null && [[ ! -x $ynl ]]; then + ktap_skip_all "ynl command not found: $ynl" + exit "$KSFT_SKIP" +fi + +trap cleanup EXIT + +ktap_print_header +ktap_set_plan 9>> +setup + +# Run all tests +cli_list_families +cli_netdev_ops +cli_ethtool_ops +cli_rt_route_ops +cli_rt_addr_opsQuoted 8 lines
tools/net/ynl/tests/test_ynl_cli.sh line 309
Quoted 6 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +6
-0+cli_rt_link_ops +cli_rt_neigh_ops +cli_rt_rule_ops +cli_nlctrl_ops + +ktap_finishedminor nit:
ktap_finishedshould probably be in the 'cleanup' trap handler
Quoted 6 lines
@Donald: I don't think '
ktap_finished' should be called there: in case of errors with an early exit during the setup phase, the two scripts will call 'ktap_skip_all', then 'exit "$KSFT_SKIP"'. If 'ktap_finished' is called in the 'cleanup' trap, it will print a total with everything set to 0 and call 'exit' again with other values (and no effects). So I think it is not supposed to be called from the exit trap.
Okay, fair. I thought the goal was to always output totals. Looking at ktap_helpers.sh I see that it can't output a meaningful skip count for the skip_call case.
Quoted 3 lines
Cheers,
Matt
Re: [PATCHv4 net-next 3/3] tools: ynl: add YNL test framework
Quoted 47 lines · Donald Hunter
Quoted 38 lines · Matthieu Baerts
Hi Donald,
Quoted 26 lines · Donald Hunter
Quoted 25 lines · Hangbin Liu
tools/net/ynl/tests/test_ynl_cli.sh new file +24
-0+cleanup() { + if [[ -n "$testns" ]]; then + ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true + ip netns del "$testns" 2>/dev/null || true + fi +} + +# Check if ynl command is available +if ! command -v $ynl &>/dev/null && [[ ! -x $ynl ]]; then + ktap_skip_all "ynl command not found: $ynl" + exit "$KSFT_SKIP" +fi + +trap cleanup EXIT + +ktap_print_header +ktap_set_plan 9>> +setup + +# Run all tests +cli_list_families +cli_netdev_ops +cli_ethtool_ops +cli_rt_route_ops +cli_rt_addr_opsQuoted 8 lines
tools/net/ynl/tests/test_ynl_cli.sh line 309
Quoted 6 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +6
-0+cli_rt_link_ops +cli_rt_neigh_ops +cli_rt_rule_ops +cli_nlctrl_ops + +ktap_finishedminor nit:
ktap_finishedshould probably be in the 'cleanup' trap handlerQuoted 6 lines
@Donald: I don't think '
ktap_finished' should be called there: in case of errors with an early exit during the setup phase, the two scripts will call 'ktap_skip_all', then 'exit "$KSFT_SKIP"'. If 'ktap_finished' is called in the 'cleanup' trap, it will print a total with everything set to 0 and call 'exit' again with other values (and no effects). So I think it is not supposed to be called from the exit trap.
Quoted 3 lines
Okay, fair. I thought the goal was to always output totals. Looking at
ktap_helpers.shI see that it can't output a meaningful skip count for theskip_callcase.
Definitely, I will add this to my todo list and see if I could fix the helper.
Thanks
Hangbin
Re: [PATCHv4 net-next 3/3] tools: ynl: add YNL test framework
In reply to Hangbin LiuHi Hangbin,
Quoted 11 lines · Hangbin Liu
Add a test framework for YAML Netlink (YNL) tools, covering both CLI and ethtool functionality. The framework includes:
1) cli: family listing, netdev, ethtool, rt-* families, and nlctrl operations
2) ethtool: device info, statistics, ring/coalesce/pause parameters, and feature gettingsThe current YNL syntax is a bit obscure, and end users may not always know how to use it. This test framework provides usage examples and also serves as a regression test to catch potential breakages caused by future changes.
Thank you for the new version!
I have two minor comments, up to you to fix that or not.
(...)
tools/net/ynl/tests/test_ynl_cli.sh line 7
Quoted 13 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +7
-0@@ -0,0 +1,309 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Test YNL CLI functionality + +# Load KTAP test helpers +KSELFTEST_KTAP_HELPERS="$(dirname "$(realpath "$0")")/../../../testing/selftests/kselftest/ktap_helpers.sh" +# shellcheck disable=SC1090
nit: maybe you can use this instead?
# shellcheck source=../../../testing/selftests/kselftest/ktap_helpers.sh
(If not, it might be good to always document why something is disabled by adding a comment on the same line or before.)
(Same for the other test.)
(...)
tools/net/ynl/tests/test_ynl_cli.sh line 295
Quoted 2 lines
tools/net/ynl/tests/test_ynl_cli.sh new file +2
-0+ktap_print_header +ktap_set_plan 9
I think you should print the plan after the setup, to avoid this in case of errors during the setup:
TAP version 13
1..9
1..0 # SKIP (...)
<exit 4>
Note that it might be interesting to add a dedicated variable for the tests counter that you increment after having declared each test, just not to forget modifying the plan when a new test is added, e.g.
TESTS_NO=0
(...)
cli_list_families() {
(...)
}
TESTS_NO=$((TESTS_NO + 1))(...)
# Run all tests
ktap_set_plan "${TESTS_NO}"
cli_list_families
(...)(Same for the other test.)
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.