Skip to content

Commit 002ca61

Browse files
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 Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]>
1 parent ec5a92b commit 002ca61

File tree

5 files changed

+199
-3
lines changed

5 files changed

+199
-3
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
541541
$$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
542542
TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
543543
$$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
544+
TRUNNER_LIB_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \
545+
$$(filter %.c,$(TRUNNER_LIB_SOURCES)))
544546
TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
545547
TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
546548
TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
@@ -672,6 +674,10 @@ $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
672674
$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
673675
$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
674676

677+
$(TRUNNER_LIB_OBJS): $(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c
678+
$$(call msg,LIB-OBJ,$(TRUNNER_BINARY),$$@)
679+
$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
680+
675681
# non-flavored in-srctree builds receive special treatment, in particular, we
676682
# do not need to copy extra resources (see e.g. test_btf_dump_case())
677683
$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
@@ -685,6 +691,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
685691

686692
$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
687693
$(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
694+
$(TRUNNER_LIB_OBJS) \
688695
$(RESOLVE_BTFIDS) \
689696
$(TRUNNER_BPFTOOL) \
690697
$(OUTPUT)/veristat \
@@ -718,6 +725,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
718725
json_writer.c \
719726
flow_dissector_load.h \
720727
ip_check_defrag_frags.h
728+
TRUNNER_LIB_SOURCES := find_bit.c
721729
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
722730
$(OUTPUT)/liburandom_read.so \
723731
$(OUTPUT)/xdp_synproxy \
@@ -755,6 +763,7 @@ endif
755763
TRUNNER_TESTS_DIR := map_tests
756764
TRUNNER_BPF_PROGS_DIR := progs
757765
TRUNNER_EXTRA_SOURCES := test_maps.c
766+
TRUNNER_LIB_SOURCES :=
758767
TRUNNER_EXTRA_FILES :=
759768
TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
760769
TRUNNER_BPF_CFLAGS :=
@@ -776,8 +785,8 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
776785
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
777786

778787
# Include find_bit.c to compile xskxceiver.
779-
EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c
780-
$(OUTPUT)/xskxceiver: $(EXTRA_SRC) test_xsk.c xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
788+
EXTRA_SRC := $(TOOLSDIR)/lib/find_bit.c prog_tests/test_xsk.c
789+
$(OUTPUT)/xskxceiver: $(EXTRA_SRC) xskxceiver.c xskxceiver.h $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
781790
$(call msg,BINARY,,$@)
782791
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
783792

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <net/if.h>
3+
4+
#include "network_helpers.h"
5+
#include "test_progs.h"
6+
#include "test_xsk.h"
7+
#include "xsk_xdp_progs.skel.h"
8+
9+
#define VETH_RX "veth0"
10+
#define VETH_TX "veth1"
11+
#define MTU 1500
12+
13+
void __printf(1, 2) xsk_log(const char *msg, ...)
14+
{
15+
int saved_errno = errno;
16+
va_list args;
17+
18+
va_start(args, msg);
19+
printf("# ");
20+
errno = saved_errno;
21+
vprintf(msg, args);
22+
va_end(args);
23+
}
24+
25+
void xsk_verbose(const char *msg, ...)
26+
{
27+
if (env_verbosity > VERBOSE_NORMAL) {
28+
va_list args;
29+
30+
va_start(args, msg);
31+
xsk_log(msg, args);
32+
va_end(args);
33+
}
34+
}
35+
36+
void xsk_skip(const char *msg, ...)
37+
{
38+
va_list args;
39+
40+
fprintf(stderr, "TEST SKIPPED :");
41+
va_start(args, msg);
42+
xsk_log(msg, args);
43+
va_end(args);
44+
}
45+
46+
void xsk_fail(const char *msg, ...)
47+
{
48+
va_list args;
49+
50+
va_start(args, msg);
51+
xsk_log(msg, args);
52+
va_end(args);
53+
}
54+
55+
int setup_veth(bool busy_poll)
56+
{
57+
SYS(fail,
58+
"ip link add %s numtxqueues 4 numrxqueues 4 type veth peer name %s numtxqueues 4 numrxqueues 4",
59+
VETH_RX, VETH_TX);
60+
SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_RX);
61+
SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_TX);
62+
63+
if (busy_poll) {
64+
SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_RX);
65+
SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_RX);
66+
SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_TX);
67+
SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_TX);
68+
}
69+
70+
SYS(fail, "ip link set %s mtu %d", VETH_RX, MTU);
71+
SYS(fail, "ip link set %s mtu %d", VETH_TX, MTU);
72+
SYS(fail, "ip link set %s up", VETH_RX);
73+
SYS(fail, "ip link set %s up", VETH_TX);
74+
75+
return 0;
76+
77+
fail:
78+
return -1;
79+
}
80+
81+
void delete_veth(void)
82+
{
83+
SYS_NOFAIL("ip link del %s", VETH_RX);
84+
SYS_NOFAIL("ip link del %s", VETH_TX);
85+
}
86+
87+
int configure_ifobj(struct ifobject *tx, struct ifobject *rx)
88+
{
89+
rx->ifindex = if_nametoindex(VETH_RX);
90+
if (!ASSERT_OK_FD(rx->ifindex, "get RX ifindex"))
91+
return -1;
92+
93+
tx->ifindex = if_nametoindex(VETH_TX);
94+
if (!ASSERT_OK_FD(tx->ifindex, "get TX ifindex"))
95+
return -1;
96+
97+
tx->shared_umem = false;
98+
rx->shared_umem = false;
99+
100+
101+
return 0;
102+
}
103+
104+
static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode)
105+
{
106+
struct ifobject *ifobj_tx, *ifobj_rx;
107+
struct test_spec test;
108+
int ret;
109+
110+
ifobj_tx = ifobject_create();
111+
if (!ASSERT_OK_PTR(ifobj_tx, "create ifobj_tx"))
112+
return;
113+
114+
ifobj_rx = ifobject_create();
115+
if (!ASSERT_OK_PTR(ifobj_rx, "create ifobj_rx"))
116+
goto delete_tx;
117+
118+
if (!ASSERT_OK(setup_veth(false), "setup veth"))
119+
goto delete_rx;
120+
121+
if (!ASSERT_OK(configure_ifobj(ifobj_tx, ifobj_rx), "conigure ifobj"))
122+
goto delete_veth;
123+
124+
ret = get_hw_ring_size(ifobj_tx->ifname, &ifobj_tx->ring);
125+
if (!ret) {
126+
ifobj_tx->hw_ring_size_supp = true;
127+
ifobj_tx->set_ring.default_tx = ifobj_tx->ring.tx_pending;
128+
ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending;
129+
}
130+
131+
if (!ASSERT_OK(init_iface(ifobj_rx, worker_testapp_validate_rx), "init RX"))
132+
goto delete_veth;
133+
if (!ASSERT_OK(init_iface(ifobj_tx, worker_testapp_validate_tx), "init TX"))
134+
goto delete_veth;
135+
136+
test_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]);
137+
138+
test.tx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
139+
if (!ASSERT_OK_PTR(test.tx_pkt_stream_default, "TX pkt generation"))
140+
goto delete_veth;
141+
test.rx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
142+
if (!ASSERT_OK_PTR(test.rx_pkt_stream_default, "RX pkt generation"))
143+
goto delete_veth;
144+
145+
146+
test_init(&test, ifobj_tx, ifobj_rx, mode, test_to_run);
147+
ret = test.test_func(&test);
148+
if (ret != TEST_SKIP)
149+
ASSERT_OK(ret, "Run test");
150+
pkt_stream_restore_default(&test);
151+
152+
if (ifobj_tx->hw_ring_size_supp)
153+
hw_ring_size_reset(ifobj_tx);
154+
155+
pkt_stream_delete(test.tx_pkt_stream_default);
156+
pkt_stream_delete(test.rx_pkt_stream_default);
157+
xsk_xdp_progs__destroy(ifobj_tx->xdp_progs);
158+
xsk_xdp_progs__destroy(ifobj_rx->xdp_progs);
159+
160+
delete_veth:
161+
delete_veth();
162+
delete_rx:
163+
ifobject_delete(ifobj_rx);
164+
delete_tx:
165+
ifobject_delete(ifobj_tx);
166+
}
167+
168+
void test_ns_xsk_skb(void)
169+
{
170+
int i;
171+
172+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
173+
if (test__start_subtest(tests[i].name))
174+
test_xsk(&tests[i], TEST_MODE_SKB);
175+
}
176+
}
177+
178+
void test_ns_xsk_drv(void)
179+
{
180+
int i;
181+
182+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
183+
if (test__start_subtest(tests[i].name))
184+
test_xsk(&tests[i], TEST_MODE_DRV);
185+
}
186+
}
187+

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)