Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: test_xsk: Integrate test_xsk.c to test_progs framework

test_xsk.c isn't part of the test_progs framework.

Integrate the tests defined by test_xsk.c into the test_progs framework
through a new file : prog_tests/xsk.c. ZeroCopy mode isn't tested in it
as veth peers don't support it.

Move test_xsk{.c/.h} to prog_tests/.

Add the find_bit library to test_progs sources in the Makefile as it is
is used by test_xsk.c

Reviewed-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
Link: https://lore.kernel.org/r/20251031-xsk-v7-15-39fe486593a3@bootlin.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Bastien Curutchet (eBPF Foundation) and committed by
Alexei Starovoitov
d1aec26f 75fc6308

+163 -3
+11 -2
tools/testing/selftests/bpf/Makefile
··· 544 544 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) 545 545 TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 546 546 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) 547 + TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ 548 + $$(filter %.c,$(TRUNNER_LIB_SOURCES))) 547 549 TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) 548 550 TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h 549 551 TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) ··· 689 687 $$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@) 690 688 $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 691 689 690 + $(TRUNNER_LIB_OBJS): $(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c 691 + $$(call msg,LIB-OBJ,$(TRUNNER_BINARY),$$@) 692 + $(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ 693 + 692 694 # non-flavored in-srctree builds receive special treatment, in particular, we 693 695 # do not need to copy extra resources (see e.g. test_btf_dump_case()) 694 696 $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) ··· 706 700 707 701 $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ 708 702 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ 703 + $(TRUNNER_LIB_OBJS) \ 709 704 $(RESOLVE_BTFIDS) \ 710 705 $(TRUNNER_BPFTOOL) \ 711 706 $(OUTPUT)/veristat \ ··· 753 746 $(VERIFY_SIG_HDR) \ 754 747 flow_dissector_load.h \ 755 748 ip_check_defrag_frags.h 749 + TRUNNER_LIB_SOURCES := find_bit.c 756 750 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ 757 751 $(OUTPUT)/liburandom_read.so \ 758 752 $(OUTPUT)/xdp_synproxy \ ··· 791 783 TRUNNER_TESTS_DIR := map_tests 792 784 TRUNNER_BPF_PROGS_DIR := progs 793 785 TRUNNER_EXTRA_SOURCES := test_maps.c 786 + TRUNNER_LIB_SOURCES := 794 787 TRUNNER_EXTRA_FILES := 795 788 TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) 796 789 TRUNNER_BPF_CFLAGS := ··· 813 804 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 814 805 815 806 # Include find_bit.c to compile xskxceiver. 816 - EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c 817 - $(OUTPUT)/xskxceiver: $(EXTRA_SRC) test_xsk.c test_xsk.h xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT) 807 + EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c prog_tests/test_xsk.c prog_tests/test_xsk.h 808 + $(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT) 818 809 $(call msg,BINARY,,$@) 819 810 $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ 820 811
+151
tools/testing/selftests/bpf/prog_tests/xsk.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <net/if.h> 3 + #include <stdarg.h> 4 + 5 + #include "network_helpers.h" 6 + #include "test_progs.h" 7 + #include "test_xsk.h" 8 + #include "xsk_xdp_progs.skel.h" 9 + 10 + #define VETH_RX "veth0" 11 + #define VETH_TX "veth1" 12 + #define MTU 1500 13 + 14 + int setup_veth(bool busy_poll) 15 + { 16 + SYS(fail, 17 + "ip link add %s numtxqueues 4 numrxqueues 4 type veth peer name %s numtxqueues 4 numrxqueues 4", 18 + VETH_RX, VETH_TX); 19 + SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_RX); 20 + SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_TX); 21 + 22 + if (busy_poll) { 23 + SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_RX); 24 + SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_RX); 25 + SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_TX); 26 + SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_TX); 27 + } 28 + 29 + SYS(fail, "ip link set %s mtu %d", VETH_RX, MTU); 30 + SYS(fail, "ip link set %s mtu %d", VETH_TX, MTU); 31 + SYS(fail, "ip link set %s up", VETH_RX); 32 + SYS(fail, "ip link set %s up", VETH_TX); 33 + 34 + return 0; 35 + 36 + fail: 37 + return -1; 38 + } 39 + 40 + void delete_veth(void) 41 + { 42 + SYS_NOFAIL("ip link del %s", VETH_RX); 43 + SYS_NOFAIL("ip link del %s", VETH_TX); 44 + } 45 + 46 + int configure_ifobj(struct ifobject *tx, struct ifobject *rx) 47 + { 48 + rx->ifindex = if_nametoindex(VETH_RX); 49 + if (!ASSERT_OK_FD(rx->ifindex, "get RX ifindex")) 50 + return -1; 51 + 52 + tx->ifindex = if_nametoindex(VETH_TX); 53 + if (!ASSERT_OK_FD(tx->ifindex, "get TX ifindex")) 54 + return -1; 55 + 56 + tx->shared_umem = false; 57 + rx->shared_umem = false; 58 + 59 + 60 + return 0; 61 + } 62 + 63 + static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode) 64 + { 65 + struct ifobject *ifobj_tx, *ifobj_rx; 66 + struct test_spec test; 67 + int ret; 68 + 69 + ifobj_tx = ifobject_create(); 70 + if (!ASSERT_OK_PTR(ifobj_tx, "create ifobj_tx")) 71 + return; 72 + 73 + ifobj_rx = ifobject_create(); 74 + if (!ASSERT_OK_PTR(ifobj_rx, "create ifobj_rx")) 75 + goto delete_tx; 76 + 77 + if (!ASSERT_OK(configure_ifobj(ifobj_tx, ifobj_rx), "conigure ifobj")) 78 + goto delete_rx; 79 + 80 + ret = get_hw_ring_size(ifobj_tx->ifname, &ifobj_tx->ring); 81 + if (!ret) { 82 + ifobj_tx->hw_ring_size_supp = true; 83 + ifobj_tx->set_ring.default_tx = ifobj_tx->ring.tx_pending; 84 + ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending; 85 + } 86 + 87 + if (!ASSERT_OK(init_iface(ifobj_rx, worker_testapp_validate_rx), "init RX")) 88 + goto delete_rx; 89 + if (!ASSERT_OK(init_iface(ifobj_tx, worker_testapp_validate_tx), "init TX")) 90 + goto delete_rx; 91 + 92 + test_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]); 93 + 94 + test.tx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE); 95 + if (!ASSERT_OK_PTR(test.tx_pkt_stream_default, "TX pkt generation")) 96 + goto delete_rx; 97 + test.rx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE); 98 + if (!ASSERT_OK_PTR(test.rx_pkt_stream_default, "RX pkt generation")) 99 + goto delete_rx; 100 + 101 + 102 + test_init(&test, ifobj_tx, ifobj_rx, mode, test_to_run); 103 + ret = test.test_func(&test); 104 + if (ret != TEST_SKIP) 105 + ASSERT_OK(ret, "Run test"); 106 + pkt_stream_restore_default(&test); 107 + 108 + if (ifobj_tx->hw_ring_size_supp) 109 + hw_ring_size_reset(ifobj_tx); 110 + 111 + pkt_stream_delete(test.tx_pkt_stream_default); 112 + pkt_stream_delete(test.rx_pkt_stream_default); 113 + xsk_xdp_progs__destroy(ifobj_tx->xdp_progs); 114 + xsk_xdp_progs__destroy(ifobj_rx->xdp_progs); 115 + 116 + delete_rx: 117 + ifobject_delete(ifobj_rx); 118 + delete_tx: 119 + ifobject_delete(ifobj_tx); 120 + } 121 + 122 + void test_ns_xsk_skb(void) 123 + { 124 + int i; 125 + 126 + if (!ASSERT_OK(setup_veth(false), "setup veth")) 127 + return; 128 + 129 + for (i = 0; i < ARRAY_SIZE(tests); i++) { 130 + if (test__start_subtest(tests[i].name)) 131 + test_xsk(&tests[i], TEST_MODE_SKB); 132 + } 133 + 134 + delete_veth(); 135 + } 136 + 137 + void test_ns_xsk_drv(void) 138 + { 139 + int i; 140 + 141 + if (!ASSERT_OK(setup_veth(false), "setup veth")) 142 + return; 143 + 144 + for (i = 0; i < ARRAY_SIZE(tests); i++) { 145 + if (test__start_subtest(tests[i].name)) 146 + test_xsk(&tests[i], TEST_MODE_DRV); 147 + } 148 + 149 + delete_veth(); 150 + } 151 +
tools/testing/selftests/bpf/test_xsk.c tools/testing/selftests/bpf/prog_tests/test_xsk.c
tools/testing/selftests/bpf/test_xsk.h tools/testing/selftests/bpf/prog_tests/test_xsk.h
+1 -1
tools/testing/selftests/bpf/xskxceiver.c
··· 90 90 #include <sys/mman.h> 91 91 #include <sys/types.h> 92 92 93 - #include "test_xsk.h" 93 + #include "prog_tests/test_xsk.h" 94 94 #include "xsk_xdp_progs.skel.h" 95 95 #include "xsk.h" 96 96 #include "xskxceiver.h"