Skip to contentSkip to conversation

tools: ynl: convert samples into selftests

PATCH Applied Jakub Kicinski 3 participants 13 messages Mar 7 – Mar 10

Subsystems (3)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

v2 cover tools: ynl: convert samples into selftests

The "samples" were always poor man's tests, used to manually confirm that C YNL works as expected. Since a proper tests/ directory now exists move the samples and use the kselftest harness to turn them into selftests outputting KTAP.

v2:
- add missing ASSERTs on allocation failures
- fix the exec in the bash wrappers preventing cleanup
v1: https://lore.kernel.org/[email protected]

Jakub Kicinski (10):
tools: ynl: move samples to tests
tools: ynl: convert netdev sample to selftest
tools: ynl: convert ovs sample to selftest
tools: ynl: convert rt-link sample to selftest
tools: ynl: convert tc and tc-filter-add samples to selftest
tools: ynl: add netdevsim wrapper library for YNL tests
tools: ynl: convert devlink sample to selftest
tools: ynl: convert ethtool sample to selftest
tools: ynl: convert rt-addr sample to selftest
tools: ynl: convert rt-route sample to selftest

 tools/net/ynl/Makefile                      |   4 +-
 tools/net/ynl/samples/Makefile              |  36 --
 tools/net/ynl/tests/Makefile                |  70 +++-
 tools/net/ynl/samples/devlink.c             |  61 ---
 tools/net/ynl/samples/ethtool.c             |  65 ----
 tools/net/ynl/samples/netdev.c              | 128 ------
 tools/net/ynl/samples/ovs.c                 |  60 ---
 tools/net/ynl/samples/rt-addr.c             |  80 ----
 tools/net/ynl/samples/rt-link.c             | 184 ---------
 tools/net/ynl/samples/rt-route.c            |  80 ----
 tools/net/ynl/samples/tc-filter-add.c       | 335 ----------------
 tools/net/ynl/samples/tc.c                  |  80 ----
 tools/net/ynl/tests/devlink.c               | 101 +++++
 tools/net/ynl/tests/ethtool.c               |  92 +++++
 tools/net/ynl/tests/netdev.c                | 231 +++++++++++
 tools/net/ynl/tests/ovs.c                   | 108 ++++++
 tools/net/ynl/tests/rt-addr.c               | 111 ++++++
 tools/net/ynl/tests/rt-link.c               | 206 ++++++++++
 tools/net/ynl/tests/rt-route.c              | 113 ++++++
 tools/net/ynl/tests/tc.c                    | 409 ++++++++++++++++++++
 tools/net/ynl/{samples => tests}/.gitignore |   3 +-
 tools/net/ynl/tests/config                  |   8 +
 tools/net/ynl/tests/devlink.sh              |   5 +
 tools/net/ynl/tests/ethtool.sh              |   5 +
 tools/net/ynl/tests/rt-addr.sh              |   5 +
 tools/net/ynl/tests/rt-route.sh             |   5 +
 tools/net/ynl/tests/ynl_nsim_lib.sh         |  35 ++
 27 files changed, 1501 insertions(+), 1119 deletions(-)
 delete mode 100644 tools/net/ynl/samples/Makefile
 delete mode 100644 tools/net/ynl/samples/devlink.c
 delete mode 100644 tools/net/ynl/samples/ethtool.c
 delete mode 100644 tools/net/ynl/samples/netdev.c
 delete mode 100644 tools/net/ynl/samples/ovs.c
 delete mode 100644 tools/net/ynl/samples/rt-addr.c
 delete mode 100644 tools/net/ynl/samples/rt-link.c
 delete mode 100644 tools/net/ynl/samples/rt-route.c
 delete mode 100644 tools/net/ynl/samples/tc-filter-add.c
 delete mode 100644 tools/net/ynl/samples/tc.c
 create mode 100644 tools/net/ynl/tests/devlink.c
 create mode 100644 tools/net/ynl/tests/ethtool.c
 create mode 100644 tools/net/ynl/tests/netdev.c
 create mode 100644 tools/net/ynl/tests/ovs.c
 create mode 100644 tools/net/ynl/tests/rt-addr.c
 create mode 100644 tools/net/ynl/tests/rt-link.c
 create mode 100644 tools/net/ynl/tests/rt-route.c
 create mode 100644 tools/net/ynl/tests/tc.c
 rename tools/net/ynl/{samples => tests}/.gitignore (87%)
 create mode 100755 tools/net/ynl/tests/devlink.sh
 create mode 100755 tools/net/ynl/tests/ethtool.sh
 create mode 100755 tools/net/ynl/tests/rt-addr.sh
 create mode 100755 tools/net/ynl/tests/rt-route.sh
 create mode 100644 tools/net/ynl/tests/ynl_nsim_lib.sh
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/10 tools: ynl: move samples to tests

The "samples" were always poor man's tests (used to manually confirm that C YNL works).

Move all C sample programs from tools/net/ynl/samples/ to tools/net/ynl/tests/, "merge" the Makefiles. The subsequent changes will convert each sample into a proper KTAP selftests.

Since these are now tests rather than samples - default to enabling asan. After all we're testing user space code here.

Sort the gitignore while at it, the page-pool entry was a leftover so delete it.

---
 tools/net/ynl/Makefile                        |  4 +-
 tools/net/ynl/samples/Makefile                | 36 --------------
 tools/net/ynl/tests/Makefile                  | 47 +++++++++++++++++--
 tools/net/ynl/{samples => tests}/devlink.c    |  0
 tools/net/ynl/{samples => tests}/ethtool.c    |  0
 tools/net/ynl/{samples => tests}/netdev.c     |  0
 tools/net/ynl/{samples => tests}/ovs.c        |  0
 tools/net/ynl/{samples => tests}/rt-addr.c    |  0
 tools/net/ynl/{samples => tests}/rt-link.c    |  0
 tools/net/ynl/{samples => tests}/rt-route.c   |  0
 .../ynl/{samples => tests}/tc-filter-add.c    |  0
 tools/net/ynl/{samples => tests}/tc.c         |  0
 tools/net/ynl/{samples => tests}/.gitignore   |  3 +-
 13 files changed, 45 insertions(+), 45 deletions(-)
 delete mode 100644 tools/net/ynl/samples/Makefile
 rename tools/net/ynl/{samples => tests}/devlink.c (100%)
 rename tools/net/ynl/{samples => tests}/ethtool.c (100%)
 rename tools/net/ynl/{samples => tests}/netdev.c (100%)
 rename tools/net/ynl/{samples => tests}/ovs.c (100%)
 rename tools/net/ynl/{samples => tests}/rt-addr.c (100%)
 rename tools/net/ynl/{samples => tests}/rt-link.c (100%)
 rename tools/net/ynl/{samples => tests}/rt-route.c (100%)
 rename tools/net/ynl/{samples => tests}/tc-filter-add.c (100%)
 rename tools/net/ynl/{samples => tests}/tc.c (100%)
 rename tools/net/ynl/{samples => tests}/.gitignore (87%)
tools/net/ynl/Makefile +2 -2
index 9b692f368be7..d514a48dae27 100644
@@ -14,12 +14,12 @@ includedir ?= $(prefix)/include
 
 SPECDIR=../../../Documentation/netlink/specs
 
-SUBDIRS = lib generated samples ynltool tests
+SUBDIRS = lib generated ynltool tests
 
 all: $(SUBDIRS) libynl.a
 
+tests: | lib generated libynl.a
 ynltool: | lib generated libynl.a
-samples: | lib generated
 libynl.a: | lib generated
 	@echo -e "\tAR $@"
 	@ar rcs $@ lib/ynl.o generated/*-user.o
tools/net/ynl/samples/Makefile deleted +0 -36
deleted file mode 100644
index d76cbd41cbb1..000000000000
@@ -1,36 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-
-include ../Makefile.deps
-
-CC=gcc
-CFLAGS += -std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \
-	-I../lib/ -I../generated/ -idirafter $(UAPI_PATH)
-ifeq ("$(DEBUG)","1")
-  CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
-endif
-
-LDLIBS=../lib/ynl.a ../generated/protos.a
-
-SRCS=$(wildcard *.c)
-BINS=$(patsubst %.c,%,${SRCS})
-
-include $(wildcard *.d)
-
-all: $(BINS)
-
-CFLAGS_page-pool=$(CFLAGS_netdev)
-CFLAGS_tc-filter-add:=$(CFLAGS_tc)
-
-$(BINS): ../lib/ynl.a ../generated/protos.a $(SRCS)
-	@echo -e '\tCC sample $@'
-	@$(COMPILE.c) $(CFLAGS_$@) [email protected] -o [email protected]
-	@$(LINK.c) [email protected] -o $@  $(LDLIBS)
-
-clean:
-	rm -f *.o *.d *~
-
-distclean: clean
-	rm -f $(BINS)
-
-.PHONY: all clean distclean
-.DEFAULT_GOAL=all
tools/net/ynl/tests/Makefile +42 -5

74 lines not on this page · raw message

tools/net/ynl/tests/devlink.c renamed from tools/net/ynl/samples/devlink.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/devlink.c
rename to tools/net/ynl/tests/devlink.c
tools/net/ynl/tests/ethtool.c renamed from tools/net/ynl/samples/ethtool.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/ethtool.c
rename to tools/net/ynl/tests/ethtool.c
tools/net/ynl/tests/netdev.c renamed from tools/net/ynl/samples/netdev.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/netdev.c
rename to tools/net/ynl/tests/netdev.c
tools/net/ynl/tests/ovs.c renamed from tools/net/ynl/samples/ovs.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/ovs.c
rename to tools/net/ynl/tests/ovs.c
tools/net/ynl/tests/rt-addr.c renamed from tools/net/ynl/samples/rt-addr.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/rt-addr.c
rename to tools/net/ynl/tests/rt-addr.c
tools/net/ynl/tests/rt-link.c renamed from tools/net/ynl/samples/rt-link.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/rt-link.c
rename to tools/net/ynl/tests/rt-link.c
tools/net/ynl/tests/rt-route.c renamed from tools/net/ynl/samples/rt-route.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/rt-route.c
rename to tools/net/ynl/tests/rt-route.c
tools/net/ynl/tests/tc-filter-add.c renamed from tools/net/ynl/samples/tc-filter-add.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/tc-filter-add.c
rename to tools/net/ynl/tests/tc-filter-add.c
tools/net/ynl/tests/tc.c renamed from tools/net/ynl/samples/tc.c +0 -0
similarity index 100%
rename from tools/net/ynl/samples/tc.c
rename to tools/net/ynl/tests/tc.c
tools/net/ynl/tests/.gitignore renamed from tools/net/ynl/samples/.gitignore +1 -2
similarity index 87%
rename from tools/net/ynl/samples/.gitignore
rename to tools/net/ynl/tests/.gitignore
index 05087ee323ba..045385df42a4 100644
@@ -1,8 +1,7 @@
-ethtool
 devlink
+ethtool
 netdev
 ovs
-page-pool
 rt-addr
 rt-link
 rt-route
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH v2 2/10 tools: ynl: convert netdev sample to selftest

In reply to Jakub Kicinski

Convert netdev.c to produce KTAP output with 3 tests:
- dev_dump: dump all netdev devices, skip if empty
- dev_get: query first device from dump by ifindex
- ntf_check: subscribe to "mgmt", create a veth via rt-link, verify netdev notification is received, then delete the veth

Remove stdin/scanf-based UI. Add rt-link dependency for the veth notification test.

TAP version 13
1..3
# Starting 3 tests from 1 test cases.
# RUN netdev.dump ...
# lo[1] xdp-features (0): xdp-rx-metadata-features (0): xsk-fea...
# sit0[2] xdp-features (0): xdp-rx-metadata-features (0): xsk-fea...
# OK netdev.dump
ok 1 netdev.dump
# RUN netdev.get ...
# lo[1] xdp-features (0): xdp-rx-metadata-features (0): xsk-fea...
# OK netdev.get
ok 2 netdev.get
# RUN netdev.ntf_check ...
# veth0[7] xdp-features (0): xdp-rx-metadata-features (7): timesta...
# OK netdev.ntf_check
ok 3 netdev.ntf_check
# PASSED: 3 / 3 tests passed.
# Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile |  29 +++--
 tools/net/ynl/tests/netdev.c | 229 +++++++++++++++++++++++++----------
 2 files changed, 187 insertions(+), 71 deletions(-)
tools/net/ynl/tests/Makefile +21 -8
index 5fa36c877235..1d77d3662f46 100644
@@ -18,16 +18,29 @@ TEST_PROGS := \
 	test_ynl_ethtool.sh \
 # end of TEST_PROGS
 
-SRCS=$(wildcard *.c)
-BINS=$(patsubst %.c,%,${SRCS})
+TEST_GEN_PROGS := \
+	netdev \
+# end of TEST_GEN_PROGS
 
+BINS := \
+	devlink \
+	ethtool \
+	ovs \
+	rt-addr \
+	rt-link \
+	rt-route \
+	tc \
+	tc-filter-add \
+# end of BINS
+
+CFLAGS_netdev:=$(CFLAGS_netdev) $(CFLAGS_rt-link)
 CFLAGS_tc-filter-add:=$(CFLAGS_tc)
 
 include $(wildcard *.d)
 
 INSTALL_PATH ?= $(DESTDIR)/usr/share/kselftest
 
-all: $(BINS) $(TEST_PROGS)
+all: $(TEST_GEN_PROGS) $(BINS)
 
 ../lib/ynl.a:
 	@$(MAKE) -C ../lib
@@ -35,7 +48,7 @@ all: $(BINS) $(TEST_PROGS)
  ../generated/protos.a:
 	@$(MAKE) -C ../generated
 
-$(BINS): ../lib/ynl.a ../generated/protos.a
+$(TEST_GEN_PROGS) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
 	@echo -e '\tCC test $@'
 	@$(COMPILE.c) $(CFLAGS_$@) [email protected] -o [email protected]
 	@$(LINK.c) [email protected] -o $@  $(LDLIBS)
@@ -45,7 +58,7 @@ $(BINS): ../lib/ynl.a ../generated/protos.a
 		./$$test; \
 	done
 
-install: $(TEST_PROGS) $(BINS)
+install: $(TEST_GEN_PROGS) $(BINS)
 	@mkdir -p $(INSTALL_PATH)/ynl
 	@cp ../../../testing/selftests/kselftest/ktap_helpers.sh $(INSTALL_PATH)/
 	@for test in $(TEST_PROGS); do \
@@ -56,10 +69,10 @@ install: $(TEST_PROGS) $(BINS)
 		    $$test > $(INSTALL_PATH)/ynl/$$name; \
 		chmod +x $(INSTALL_PATH)/ynl/$$name; \
 	done
-	@for bin in $(BINS); do \
+	@for bin in $(TEST_GEN_PROGS) $(BINS); do \
 		cp $$bin $(INSTALL_PATH)/ynl/$$bin; \
 	done
-	@for test in $(TEST_PROGS); do \
+	@for test in $(TEST_PROGS) $(TEST_GEN_PROGS); do \
 		echo "ynl:$$test"; \
 	done > $(INSTALL_PATH)/kselftest-list.txt
 
@@ -67,7 +80,7 @@ install: $(TEST_PROGS) $(BINS)
 	rm -f *.o *.d *~
 
 distclean: clean
-	rm -f $(BINS)
+	rm -f $(TEST_GEN_PROGS) $(BINS)
 
 .PHONY: all install clean distclean run_tests
 .DEFAULT_GOAL=all
tools/net/ynl/tests/netdev.c +166 -63

276 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH v2 3/10 tools: ynl: convert ovs sample to selftest

In reply to Jakub Kicinski

Convert ovs.c to produce KTAP output with kselftest_harness. The single "crud" test creates a new OVS datapath, fetches it back by name, then dumps all datapaths verifying the new one appears.

IIRC I added this test because ovs is a genetlink family but has a family-specific fixed header.

TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN ovs.crud ...
# get:
# ynl-test(3): pid:0 cache:256
# dump:
# ynl-test(3): pid:0 cache:256
# OK ovs.crud
ok 1 ovs.crud
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile |   3 +-
 tools/net/ynl/tests/ovs.c    | 128 ++++++++++++++++++++++++-----------
 tools/net/ynl/tests/config   |   1 +
 3 files changed, 91 insertions(+), 41 deletions(-)
tools/net/ynl/tests/Makefile +2 -1
index 1d77d3662f46..df9d37c8b2a4 100644
@@ -20,12 +20,12 @@ TEST_PROGS := \
 
 TEST_GEN_PROGS := \
 	netdev \
+	ovs \
 # end of TEST_GEN_PROGS
 
 BINS := \
 	devlink \
 	ethtool \
-	ovs \
 	rt-addr \
 	rt-link \
 	rt-route \
@@ -34,6 +34,7 @@ BINS := \
 # end of BINS
 
 CFLAGS_netdev:=$(CFLAGS_netdev) $(CFLAGS_rt-link)
+CFLAGS_ovs:=$(CFLAGS_ovs_datapath)
 CFLAGS_tc-filter-add:=$(CFLAGS_tc)
 
 include $(wildcard *.d)
tools/net/ynl/tests/ovs.c +88 -40

145 lines not on this page · raw message

tools/net/ynl/tests/config +1 -0
index 339f1309c03f..357b34611da4 100644
@@ -3,4 +3,5 @@ CONFIG_INET_DIAG=y
 CONFIG_IPV6=y
 CONFIG_NET_NS=y
 CONFIG_NETDEVSIM=m
+CONFIG_OPENVSWITCH=m
 CONFIG_VETH=m
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH v2 4/10 tools: ynl: convert rt-link sample to selftest

In reply to Jakub Kicinski

Convert rt-link.c to use kselftest_harness.h with FIXTURE/TEST_F. Move rt-link from BINS to TEST_GEN_PROGS.

Output:

TAP version 13
1..3
# Starting 3 tests from 1 test cases.
# RUN rt_link.dump ...

  #   1:          lo: mtu 65536
  #   2:          sit0: mtu  1480  kind sit

# OK rt_link.dump
ok 1 rt_link.dump
# RUN rt_link.netkit ...
# 4: nk1: mtu 1500 kind netkit primary 1 policy blackhole
# OK rt_link.netkit
ok 2 rt_link.netkit
# RUN rt_link.netkit_err_msg ...
# OK rt_link.netkit_err_msg
ok 3 rt_link.netkit_err_msg
# PASSED: 3 / 3 tests passed.
# Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile  |   2 +-
 tools/net/ynl/tests/rt-link.c | 196 +++++++++++++++++++---------------
 tools/net/ynl/tests/config    |   1 +
 3 files changed, 111 insertions(+), 88 deletions(-)
tools/net/ynl/tests/Makefile +1 -1
index df9d37c8b2a4..08d1146d91ce 100644
@@ -21,13 +21,13 @@ TEST_PROGS := \
 TEST_GEN_PROGS := \
 	netdev \
 	ovs \
+	rt-link \
 # end of TEST_GEN_PROGS
 
 BINS := \
 	devlink \
 	ethtool \
 	rt-addr \
-	rt-link \
 	rt-route \
 	tc \
 	tc-filter-add \
tools/net/ynl/tests/rt-link.c +109 -87

256 lines not on this page · raw message

tools/net/ynl/tests/config +1 -0
index 357b34611da4..b4c58d86a6c2 100644
@@ -3,5 +3,6 @@ CONFIG_INET_DIAG=y
 CONFIG_IPV6=y
 CONFIG_NET_NS=y
 CONFIG_NETDEVSIM=m
+CONFIG_NETKIT=y
 CONFIG_OPENVSWITCH=m
 CONFIG_VETH=m
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH v2 5/10 tools: ynl: convert tc and tc-filter-add samples to selftest

In reply to Jakub Kicinski

Convert tc.c and tc-filter-add.c to produce KTAP output with kselftest_harness. Merge the two tests together. They both test TC one is testing qdisc and the other classifiers but they can easily live in a single selftest.

Make the test spawn a new netns, and run the operations on lo to avoid onerous setup and cleanup.

TAP version 13
1..2
# Starting 2 tests from 1 test cases.
# RUN tc.qdisc ...
# lo: fq_codel limit: 10240p target: 5ms new_flow_cnt: 0
# OK tc.qdisc
ok 1 tc.qdisc
# RUN tc.flower ...

  # flower pref 1 proto: 0x8100
  # flower:
  #   vlan_id: 100
  #   vlan_prio: 5
  #   num_of_vlans: 3
  # action order: 1 vlan push id 200 protocol 0x8100 priority 0
  # action order: 2 vlan push id 300 protocol 0x8100 priority 0
  #            OK  tc.flower
  ok 2 tc.flower
  # PASSED: 2 / 2 tests passed.
  # Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/net/ynl/tests/Makefile        |   4 +-
 tools/net/ynl/tests/tc-filter-add.c | 335 -----------------------
 tools/net/ynl/tests/tc.c            | 405 +++++++++++++++++++++++++---
 tools/net/ynl/tests/config          |   6 +
 4 files changed, 374 insertions(+), 376 deletions(-)
 delete mode 100644 tools/net/ynl/tests/tc-filter-add.c
tools/net/ynl/tests/Makefile +1 -3
index 08d1146d91ce..524092a8de7e 100644
@@ -22,6 +22,7 @@ TEST_GEN_PROGS := \
 	netdev \
 	ovs \
 	rt-link \
+	tc \
 # end of TEST_GEN_PROGS
 
 BINS := \
@@ -29,13 +30,10 @@ BINS := \
 	ethtool \
 	rt-addr \
 	rt-route \
-	tc \
-	tc-filter-add \
 # end of BINS
 
 CFLAGS_netdev:=$(CFLAGS_netdev) $(CFLAGS_rt-link)
 CFLAGS_ovs:=$(CFLAGS_ovs_datapath)
-CFLAGS_tc-filter-add:=$(CFLAGS_tc)
 
 include $(wildcard *.d)
 
tools/net/ynl/tests/tc-filter-add.c deleted +0 -335

335 lines not on this page · raw message

tools/net/ynl/tests/tc.c +367 -38

440 lines not on this page · raw message

tools/net/ynl/tests/config +6 -0
index b4c58d86a6c2..75c0fe72391f 100644
@@ -1,7 +1,13 @@
 CONFIG_DUMMY=m
 CONFIG_INET_DIAG=y
 CONFIG_IPV6=y
+CONFIG_NET_ACT_VLAN=m
+CONFIG_NET_CLS_ACT=y
+CONFIG_NET_CLS_FLOWER=m
+CONFIG_NET_SCH_FQ_CODEL=m
+CONFIG_NET_SCH_INGRESS=m
 CONFIG_NET_NS=y
+CONFIG_NET_SCHED=y
 CONFIG_NETDEVSIM=m
 CONFIG_NETKIT=y
 CONFIG_OPENVSWITCH=m
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH v2 6/10 tools: ynl: add netdevsim wrapper library for YNL tests

In reply to Jakub Kicinski

Some tests need netdevsim setup which is painful to do from C.

Add ynl_nsim_lib.sh, a shared library providing nsim_setup and nsim_cleanup functions for tests that need a netdevsim device.

---
 tools/net/ynl/tests/Makefile        |  5 +++++
 tools/net/ynl/tests/ynl_nsim_lib.sh | 35 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 tools/net/ynl/tests/ynl_nsim_lib.sh
tools/net/ynl/tests/Makefile +5 -0
index 524092a8de7e..a329de031add 100644
@@ -32,6 +32,8 @@ BINS := \
 	rt-route \
 # end of BINS
 
+TEST_FILES := ynl_nsim_lib.sh
+
 CFLAGS_netdev:=$(CFLAGS_netdev) $(CFLAGS_rt-link)
 CFLAGS_ovs:=$(CFLAGS_ovs_datapath)
 
@@ -68,6 +70,9 @@ install: $(TEST_GEN_PROGS) $(BINS)
 		    $$test > $(INSTALL_PATH)/ynl/$$name; \
 		chmod +x $(INSTALL_PATH)/ynl/$$name; \
 	done
+	@for file in $(TEST_FILES); do \
+		cp $$file $(INSTALL_PATH)/ynl/$$file; \
+	done
 	@for bin in $(TEST_GEN_PROGS) $(BINS); do \
 		cp $$bin $(INSTALL_PATH)/ynl/$$bin; \
 	done
tools/net/ynl/tests/ynl_nsim_lib.sh new file +35 -0
new file mode 100644
index 000000000000..98cdce44a69c
@@ -0,0 +1,35 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Shared netdevsim setup/cleanup for YNL C test wrappers
+
+NSIM_ID="1337"
+NSIM_DEV=""
+KSFT_SKIP=4
+
+nsim_cleanup() {
+	echo "$NSIM_ID" > /sys/bus/netdevsim/del_device 2>/dev/null || true
+}
+
+nsim_setup() {
+	modprobe netdevsim 2>/dev/null
+	if ! [ -f /sys/bus/netdevsim/new_device ]; then
+		echo "netdevsim module not available, skipping" >&2
+		exit "$KSFT_SKIP"
+	fi
+
+	trap nsim_cleanup EXIT
+
+	echo "$NSIM_ID 1" > /sys/bus/netdevsim/new_device
+	udevadm settle
+
+	NSIM_DEV=$(ls /sys/bus/netdevsim/devices/netdevsim${NSIM_ID}/net 2>/dev/null | head -1)
+	if [ -z "$NSIM_DEV" ]; then
+		echo "failed to find netdevsim device" >&2
+		exit 1
+	fi
+
+	ip link set dev "$NSIM_DEV" name nsim0
+	ip link set dev nsim0 up
+	ip addr add 192.168.1.1/24 dev nsim0
+	ip addr add 2001:db8::1/64 dev nsim0 nodad
+}
Signature · 1 line
2.53.0
author maintainer
#7 lore raw

PATCH v2 7/10 tools: ynl: convert devlink sample to selftest

In reply to Jakub Kicinski

Convert devlink.c to use kselftest_harness.h with FIXTURE/TEST_F. Move devlink from BINS to TEST_GEN_FILES in the Makefile since it's invoked via the devlink.sh wrapper which sets up netdevsim.

Output:

TAP version 13
1..2
# Starting 2 tests from 1 test cases.
# RUN devlink.dump ...

  # netdevsim/netdevsim1337
  #            OK  devlink.dump
  ok 1 devlink.dump
  #  RUN           devlink.info ...
  # netdevsim/netdevsim1337:
  #   driver: netdevsim
  #   running fw:
  #     fw.mgmt: 10.20.30
  #            OK  devlink.info
  ok 2 devlink.info
  # PASSED: 2 / 2 tests passed.
  # Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/net/ynl/tests/Makefile   |  16 ++++--
 tools/net/ynl/tests/devlink.c  | 100 +++++++++++++++++++++++----------
 tools/net/ynl/tests/devlink.sh |   5 ++
 3 files changed, 85 insertions(+), 36 deletions(-)
 create mode 100755 tools/net/ynl/tests/devlink.sh
tools/net/ynl/tests/Makefile +10 -6
index a329de031add..14d399a70f10 100644
@@ -14,6 +14,7 @@ endif
 LDLIBS=../lib/ynl.a ../generated/protos.a
 
 TEST_PROGS := \
+	devlink.sh \
 	test_ynl_cli.sh \
 	test_ynl_ethtool.sh \
 # end of TEST_PROGS
@@ -25,8 +26,11 @@ TEST_GEN_PROGS := \
 	tc \
 # end of TEST_GEN_PROGS
 
-BINS := \
+TEST_GEN_FILES := \
 	devlink \
+# end of TEST_GEN_FILES
+
+BINS := \
 	ethtool \
 	rt-addr \
 	rt-route \
@@ -41,7 +45,7 @@ include $(wildcard *.d)
 
 INSTALL_PATH ?= $(DESTDIR)/usr/share/kselftest
 
-all: $(TEST_GEN_PROGS) $(BINS)
+all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
 
 ../lib/ynl.a:
 	@$(MAKE) -C ../lib
@@ -49,7 +53,7 @@ all: $(TEST_GEN_PROGS) $(BINS)
  ../generated/protos.a:
 	@$(MAKE) -C ../generated
 
-$(TEST_GEN_PROGS) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
+$(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
 	@echo -e '\tCC test $@'
 	@$(COMPILE.c) $(CFLAGS_$@) [email protected] -o [email protected]
 	@$(LINK.c) [email protected] -o $@  $(LDLIBS)
@@ -59,7 +63,7 @@ $(TEST_GEN_PROGS) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
 		./$$test; \
 	done
 
-install: $(TEST_GEN_PROGS) $(BINS)
+install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
 	@mkdir -p $(INSTALL_PATH)/ynl
 	@cp ../../../testing/selftests/kselftest/ktap_helpers.sh $(INSTALL_PATH)/
 	@for test in $(TEST_PROGS); do \
@@ -73,7 +77,7 @@ install: $(TEST_GEN_PROGS) $(BINS)
 	@for file in $(TEST_FILES); do \
 		cp $$file $(INSTALL_PATH)/ynl/$$file; \
 	done
-	@for bin in $(TEST_GEN_PROGS) $(BINS); do \
+	@for bin in $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS); do \
 		cp $$bin $(INSTALL_PATH)/ynl/$$bin; \
 	done
 	@for test in $(TEST_PROGS) $(TEST_GEN_PROGS); do \
@@ -84,7 +88,7 @@ install: $(TEST_GEN_PROGS) $(BINS)
 	rm -f *.o *.d *~
 
 distclean: clean
-	rm -f $(TEST_GEN_PROGS) $(BINS)
+	rm -f $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
 
 .PHONY: all install clean distclean run_tests
 .DEFAULT_GOAL=all
tools/net/ynl/tests/devlink.c +70 -30

128 lines not on this page · raw message

tools/net/ynl/tests/devlink.sh new file +5 -0
new file mode 100755
index 000000000000..a684c749aa5e
@@ -0,0 +1,5 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+source "$(dirname "$(realpath "$0")")/ynl_nsim_lib.sh"
+nsim_setup
+"$(dirname "$(realpath "$0")")/devlink"
Signature · 1 line
2.53.0
author maintainer
#8 lore raw

PATCH v2 8/10 tools: ynl: convert ethtool sample to selftest

In reply to Jakub Kicinski

Convert ethtool.c to use kselftest_harness.h with FIXTURE/TEST_F. Move ethtool from BINS to TEST_GEN_FILES and add ethtool.sh wrapper which sets up a netdevsim device before running the test binary.

Output:

TAP version 13
1..2
# Starting 2 tests from 1 test cases.
# RUN ethtool.channels ...
# nsim0: combined 1
# OK ethtool.channels
ok 1 ethtool.channels
# RUN ethtool.rings ...
# nsim0: rx 512 tx 512
# OK ethtool.rings
ok 2 ethtool.rings
# PASSED: 2 / 2 tests passed.
# Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile   |  3 +-
 tools/net/ynl/tests/ethtool.c  | 83 ++++++++++++++++++++++------------
 tools/net/ynl/tests/ethtool.sh |  5 ++
 3 files changed, 62 insertions(+), 29 deletions(-)
 create mode 100755 tools/net/ynl/tests/ethtool.sh
tools/net/ynl/tests/Makefile +2 -1
index 14d399a70f10..c380e9f331a3 100644
@@ -15,6 +15,7 @@ LDLIBS=../lib/ynl.a ../generated/protos.a
 
 TEST_PROGS := \
 	devlink.sh \
+	ethtool.sh \
 	test_ynl_cli.sh \
 	test_ynl_ethtool.sh \
 # end of TEST_PROGS
@@ -28,10 +29,10 @@ TEST_GEN_PROGS := \
 
 TEST_GEN_FILES := \
 	devlink \
+	ethtool \
 # end of TEST_GEN_FILES
 
 BINS := \
-	ethtool \
 	rt-addr \
 	rt-route \
 # end of BINS
tools/net/ynl/tests/ethtool.c +55 -28

111 lines not on this page · raw message

tools/net/ynl/tests/ethtool.sh new file +5 -0
new file mode 100755
index 000000000000..0859ddd697e8
@@ -0,0 +1,5 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+source "$(dirname "$(realpath "$0")")/ynl_nsim_lib.sh"
+nsim_setup
+"$(dirname "$(realpath "$0")")/ethtool"
Signature · 1 line
2.53.0
author maintainer
#9 lore raw

PATCH v2 9/10 tools: ynl: convert rt-addr sample to selftest

In reply to Jakub Kicinski

Convert rt-addr.c to use kselftest_harness.h with FIXTURE/TEST_F.

Validate that the addresses configured by the wrapper (192.168.1.1 and 2001:db8::1) appear in the dump.

Output:

TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN rt_addr.dump ...
# lo: 127.0.0.1
# nsim0: 192.168.1.1
# lo: ::1

  #            nsim0: 2001:db8::1
  #            nsim0: fe80::7c66:c9ff:fe5f:bf01
  #            OK  rt_addr.dump

ok 1 rt_addr.dump
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile   |  3 +-
 tools/net/ynl/tests/rt-addr.c  | 85 +++++++++++++++++++++++-----------
 tools/net/ynl/tests/rt-addr.sh |  5 ++
 3 files changed, 65 insertions(+), 28 deletions(-)
 create mode 100755 tools/net/ynl/tests/rt-addr.sh
tools/net/ynl/tests/Makefile +2 -1
index c380e9f331a3..52b54ea6c90f 100644
@@ -16,6 +16,7 @@ LDLIBS=../lib/ynl.a ../generated/protos.a
 TEST_PROGS := \
 	devlink.sh \
 	ethtool.sh \
+	rt-addr.sh \
 	test_ynl_cli.sh \
 	test_ynl_ethtool.sh \
 # end of TEST_PROGS
@@ -30,10 +31,10 @@ TEST_GEN_PROGS := \
 TEST_GEN_FILES := \
 	devlink \
 	ethtool \
+	rt-addr \
 # end of TEST_GEN_FILES
 
 BINS := \
-	rt-addr \
 	rt-route \
 # end of BINS
 
tools/net/ynl/tests/rt-addr.c +58 -27

116 lines not on this page · raw message

tools/net/ynl/tests/rt-addr.sh new file +5 -0
new file mode 100755
index 000000000000..87661236d126
@@ -0,0 +1,5 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+source "$(dirname "$(realpath "$0")")/ynl_nsim_lib.sh"
+nsim_setup
+"$(dirname "$(realpath "$0")")/rt-addr"
Signature · 1 line
2.53.0
author maintainer
#10 lore raw

PATCH v2 10/10 tools: ynl: convert rt-route sample to selftest

In reply to Jakub Kicinski

Convert rt-route.c to use kselftest_harness.h with FIXTURE/TEST_F. This is the last test to convert so clean up the Makefile.

Validate that the connected routes for 192.168.1.0/24 and 2001:db8::/64 appear in the dump.

Output:

TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN rt_route.dump ...

  # oif: nsim0            dst: 192.168.1.0/24
  # oif: lo               dst: ::1/128
  # oif: nsim0            dst: 2001:db8::1/128
  # oif: nsim0            dst: 2001:db8::/64
  # oif: nsim0            dst: fe80::/64
  # oif: nsim0            dst: ff00::/8

# OK rt_route.dump
ok 1 rt_route.dump
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/net/ynl/tests/Makefile    | 16 +++---
 tools/net/ynl/tests/rt-route.c  | 87 +++++++++++++++++++++++----------
 tools/net/ynl/tests/rt-route.sh |  5 ++
 3 files changed, 72 insertions(+), 36 deletions(-)
 create mode 100755 tools/net/ynl/tests/rt-route.sh
tools/net/ynl/tests/Makefile +7 -9
index 52b54ea6c90f..2a02958c7039 100644
@@ -17,6 +17,7 @@ TEST_PROGS := \
 	devlink.sh \
 	ethtool.sh \
 	rt-addr.sh \
+	rt-route.sh \
 	test_ynl_cli.sh \
 	test_ynl_ethtool.sh \
 # end of TEST_PROGS
@@ -32,11 +33,8 @@ TEST_GEN_FILES := \
 	devlink \
 	ethtool \
 	rt-addr \
-# end of TEST_GEN_FILES
-
-BINS := \
 	rt-route \
-# end of BINS
+# end of TEST_GEN_FILES
 
 TEST_FILES := ynl_nsim_lib.sh
 
@@ -47,7 +45,7 @@ include $(wildcard *.d)
 
 INSTALL_PATH ?= $(DESTDIR)/usr/share/kselftest
 
-all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
+all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
 
 ../lib/ynl.a:
 	@$(MAKE) -C ../lib
@@ -55,7 +53,7 @@ all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
  ../generated/protos.a:
 	@$(MAKE) -C ../generated
 
-$(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
+$(TEST_GEN_PROGS) $(TEST_GEN_FILES): %: %.c ../lib/ynl.a ../generated/protos.a
 	@echo -e '\tCC test $@'
 	@$(COMPILE.c) $(CFLAGS_$@) [email protected] -o [email protected]
 	@$(LINK.c) [email protected] -o $@  $(LDLIBS)
@@ -65,7 +63,7 @@ $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS): %: %.c ../lib/ynl.a ../generated/pr
 		./$$test; \
 	done
 
-install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
+install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
 	@mkdir -p $(INSTALL_PATH)/ynl
 	@cp ../../../testing/selftests/kselftest/ktap_helpers.sh $(INSTALL_PATH)/
 	@for test in $(TEST_PROGS); do \
@@ -79,7 +77,7 @@ install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
 	@for file in $(TEST_FILES); do \
 		cp $$file $(INSTALL_PATH)/ynl/$$file; \
 	done
-	@for bin in $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS); do \
+	@for bin in $(TEST_GEN_PROGS) $(TEST_GEN_FILES); do \
 		cp $$bin $(INSTALL_PATH)/ynl/$$bin; \
 	done
 	@for test in $(TEST_PROGS) $(TEST_GEN_PROGS); do \
@@ -90,7 +88,7 @@ install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
 	rm -f *.o *.d *~
 
 distclean: clean
-	rm -f $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
+	rm -f $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
 
 .PHONY: all install clean distclean run_tests
 .DEFAULT_GOAL=all
tools/net/ynl/tests/rt-route.c +60 -27

117 lines not on this page · raw message

tools/net/ynl/tests/rt-route.sh new file +5 -0
new file mode 100755
index 000000000000..020338f0a238
@@ -0,0 +1,5 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+source "$(dirname "$(realpath "$0")")/ynl_nsim_lib.sh"
+nsim_setup
+"$(dirname "$(realpath "$0")")/rt-route"
Signature · 1 line
2.53.0
maintainer Reviewed-by Tested-by on the v2 series
#11 lore raw
In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski

The "samples" were always poor man's tests, used to manually confirm that C YNL works as expected. Since a proper tests/ directory now exists move the samples and use the kselftest harness to turn them into selftests outputting KTAP.

v2:
- add missing ASSERTs on allocation failures
- fix the exec in the bash wrappers preventing cleanup
v1: https://lore.kernel.org/[email protected]

Quoted 11 lines

Jakub Kicinski (10):
tools: ynl: move samples to tests
tools: ynl: convert netdev sample to selftest
tools: ynl: convert ovs sample to selftest
tools: ynl: convert rt-link sample to selftest
tools: ynl: convert tc and tc-filter-add samples to selftest
tools: ynl: add netdevsim wrapper library for YNL tests
tools: ynl: convert devlink sample to selftest
tools: ynl: convert ethtool sample to selftest
tools: ynl: convert rt-addr sample to selftest
tools: ynl: convert rt-route sample to selftest

Series LGTM

#12 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Fri, 6 Mar 2026 19:36:20 -0800 you wrote:

The "samples" were always poor man's tests, used to manually confirm that C YNL works as expected. Since a proper tests/ directory now exists move the samples and use the kselftest harness to turn them into selftests outputting KTAP.

v2:
- add missing ASSERTs on allocation failures
- fix the exec in the bash wrappers preventing cleanup
v1: https://lore.kernel.org/[email protected]

[...]

Here is the summary with links:
- [net-next,v2,01/10] tools: ynl: move samples to tests

    https://git.kernel.org/netdev/net-next/c/e0aa0c61758f
  - [net-next,v2,02/10] tools: ynl: convert netdev sample to selftest
    https://git.kernel.org/netdev/net-next/c/285804d63f35
  - [net-next,v2,03/10] tools: ynl: convert ovs sample to selftest
    https://git.kernel.org/netdev/net-next/c/7e3effbc7627
  - [net-next,v2,04/10] tools: ynl: convert rt-link sample to selftest
    https://git.kernel.org/netdev/net-next/c/5c3206786c2d
  - [net-next,v2,05/10] tools: ynl: convert tc and tc-filter-add samples to selftest
    https://git.kernel.org/netdev/net-next/c/6cf8fb4722c3
  - [net-next,v2,06/10] tools: ynl: add netdevsim wrapper library for YNL tests
    https://git.kernel.org/netdev/net-next/c/7a95e5256293
  - [net-next,v2,07/10] tools: ynl: convert devlink sample to selftest
    https://git.kernel.org/netdev/net-next/c/db20b374e7f7
  - [net-next,v2,08/10] tools: ynl: convert ethtool sample to selftest
    https://git.kernel.org/netdev/net-next/c/1419fbf5a817
  - [net-next,v2,09/10] tools: ynl: convert rt-addr sample to selftest
    https://git.kernel.org/netdev/net-next/c/e7a39b8f5fcf
  - [net-next,v2,10/10] tools: ynl: convert rt-route sample to selftest
    https://git.kernel.org/netdev/net-next/c/aa234faa5a4d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html