Skip to content

Commit d1aec26

Browse files
bastien-curutchetAlexei Starovoitov
authored andcommitted
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 <[email protected]> Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 75fc630 commit d1aec26

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
544544
$$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
545545
TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
546546
$$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
547+
TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
548+
$$(filter %.c,$(TRUNNER_LIB_SOURCES)))
547549
TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
548550
TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
549551
TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
@@ -687,6 +689,10 @@ $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
687689
$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
688690
$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
689691

692+
$(TRUNNER_LIB_OBJS): $(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c
693+
$$(call msg,LIB-OBJ,$(TRUNNER_BINARY),$$@)
694+
$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
695+
690696
# non-flavored in-srctree builds receive special treatment, in particular, we
691697
# do not need to copy extra resources (see e.g. test_btf_dump_case())
692698
$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
@@ -700,6 +706,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
700706

701707
$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
702708
$(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
709+
$(TRUNNER_LIB_OBJS) \
703710
$(RESOLVE_BTFIDS) \
704711
$(TRUNNER_BPFTOOL) \
705712
$(OUTPUT)/veristat \
@@ -746,6 +753,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
746753
$(VERIFY_SIG_HDR) \
747754
flow_dissector_load.h \
748755
ip_check_defrag_frags.h
756+
TRUNNER_LIB_SOURCES := find_bit.c
749757
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
750758
$(OUTPUT)/liburandom_read.so \
751759
$(OUTPUT)/xdp_synproxy \
@@ -783,6 +791,7 @@ endif
783791
TRUNNER_TESTS_DIR := map_tests
784792
TRUNNER_BPF_PROGS_DIR := progs
785793
TRUNNER_EXTRA_SOURCES := test_maps.c
794+
TRUNNER_LIB_SOURCES :=
786795
TRUNNER_EXTRA_FILES :=
787796
TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
788797
TRUNNER_BPF_CFLAGS :=
@@ -804,8 +813,8 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
804813
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
805814

806815
# Include find_bit.c to compile xskxceiver.
807-
EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c
808-
$(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)
816+
EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c prog_tests/test_xsk.c prog_tests/test_xsk.h
817+
$(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
809818
$(call msg,BINARY,,$@)
810819
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
811820

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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/xskxceiver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
#include <sys/mman.h>
9191
#include <sys/types.h>
9292

93-
#include "test_xsk.h"
93+
#include "prog_tests/test_xsk.h"
9494
#include "xsk_xdp_progs.skel.h"
9595
#include "xsk.h"
9696
#include "xskxceiver.h"

0 commit comments

Comments
 (0)