Skip to content

Commit a94df60

Browse files
bastien-curutchetMartin KaFai Lau
authored andcommitted
selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
test_xdp_redirect.sh can't be used by the BPF CI. Migrate test_xdp_redirect.sh into a new test case in xdp_do_redirect.c. It uses the same network topology and the same BPF programs located in progs/test_xdp_redirect.c and progs/xdp_dummy.c. Remove test_xdp_redirect.sh and its Makefile entry. Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 2c6c5c7 commit a94df60

File tree

3 files changed

+165
-80
lines changed

3 files changed

+165
-80
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
100100

101101
# Order correspond to 'make run_tests' order
102102
TEST_PROGS := test_kmod.sh \
103-
test_xdp_redirect.sh \
104103
test_xdp_redirect_multi.sh \
105104
test_xdp_meta.sh \
106105
test_tunnel.sh \

tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <bpf/bpf_endian.h>
1212
#include <uapi/linux/netdev.h>
1313
#include "test_xdp_do_redirect.skel.h"
14+
#include "test_xdp_redirect.skel.h"
15+
#include "xdp_dummy.skel.h"
1416

1517
struct udp_packet {
1618
struct ethhdr eth;
@@ -246,3 +248,166 @@ void test_xdp_do_redirect(void)
246248
SYS_NOFAIL("ip netns del testns");
247249
test_xdp_do_redirect__destroy(skel);
248250
}
251+
252+
#define NS_NB 3
253+
#define NS0 "NS0"
254+
#define NS1 "NS1"
255+
#define NS2 "NS2"
256+
#define IPV4_NETWORK "10.1.1"
257+
#define VETH1_INDEX 111
258+
#define VETH2_INDEX 222
259+
260+
struct test_data {
261+
struct netns_obj *ns[NS_NB];
262+
u32 xdp_flags;
263+
};
264+
265+
static void cleanup(struct test_data *data)
266+
{
267+
int i;
268+
269+
for (i = 0; i < NS_NB; i++)
270+
netns_free(data->ns[i]);
271+
}
272+
273+
/**
274+
* ping_setup -
275+
* Create two veth peers and forward packets in-between using XDP
276+
*
277+
* ------------ ------------
278+
* | NS1 | | NS2 |
279+
* | veth0 | | veth0 |
280+
* | 10.1.1.1 | | 10.1.1.2 |
281+
* -----|------ ------|-----
282+
* | |
283+
* | |
284+
* -----|-----------------------|-------
285+
* | veth1 veth2 |
286+
* | (id:111) (id:222) |
287+
* | | | |
288+
* | ----- xdp forwarding ----- |
289+
* | |
290+
* | NS0 |
291+
* -------------------------------------
292+
*/
293+
static int ping_setup(struct test_data *data)
294+
{
295+
int i;
296+
297+
data->ns[0] = netns_new(NS0, false);
298+
if (!ASSERT_OK_PTR(data->ns[0], "create ns"))
299+
return -1;
300+
301+
for (i = 1; i < NS_NB; i++) {
302+
char ns_name[4] = {};
303+
304+
snprintf(ns_name, 4, "NS%d", i);
305+
data->ns[i] = netns_new(ns_name, false);
306+
if (!ASSERT_OK_PTR(data->ns[i], "create ns"))
307+
goto fail;
308+
309+
SYS(fail,
310+
"ip -n %s link add veth%d index %d%d%d type veth peer name veth0 netns %s",
311+
NS0, i, i, i, i, ns_name);
312+
SYS(fail, "ip -n %s link set veth%d up", NS0, i);
313+
314+
SYS(fail, "ip -n %s addr add %s.%d/24 dev veth0", ns_name, IPV4_NETWORK, i);
315+
SYS(fail, "ip -n %s link set veth0 up", ns_name);
316+
}
317+
318+
return 0;
319+
320+
fail:
321+
cleanup(data);
322+
return -1;
323+
}
324+
325+
static void ping_test(struct test_data *data)
326+
{
327+
struct test_xdp_redirect *skel = NULL;
328+
struct xdp_dummy *skel_dummy = NULL;
329+
struct nstoken *nstoken = NULL;
330+
int i, ret;
331+
332+
skel_dummy = xdp_dummy__open_and_load();
333+
if (!ASSERT_OK_PTR(skel_dummy, "open and load xdp_dummy skeleton"))
334+
goto close;
335+
336+
for (i = 1; i < NS_NB; i++) {
337+
char ns_name[4] = {};
338+
339+
snprintf(ns_name, 4, "NS%d", i);
340+
nstoken = open_netns(ns_name);
341+
if (!ASSERT_OK_PTR(nstoken, "open ns"))
342+
goto close;
343+
344+
ret = bpf_xdp_attach(if_nametoindex("veth0"),
345+
bpf_program__fd(skel_dummy->progs.xdp_dummy_prog),
346+
data->xdp_flags, NULL);
347+
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach dummy_prog"))
348+
goto close;
349+
350+
close_netns(nstoken);
351+
nstoken = NULL;
352+
}
353+
354+
skel = test_xdp_redirect__open_and_load();
355+
if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
356+
goto close;
357+
358+
nstoken = open_netns(NS0);
359+
if (!ASSERT_OK_PTR(nstoken, "open NS0"))
360+
goto close;
361+
362+
ret = bpf_xdp_attach(VETH2_INDEX,
363+
bpf_program__fd(skel->progs.xdp_redirect_to_111),
364+
data->xdp_flags, NULL);
365+
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
366+
goto close;
367+
368+
ret = bpf_xdp_attach(VETH1_INDEX,
369+
bpf_program__fd(skel->progs.xdp_redirect_to_222),
370+
data->xdp_flags, NULL);
371+
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
372+
goto close;
373+
374+
close_netns(nstoken);
375+
nstoken = NULL;
376+
377+
nstoken = open_netns(NS1);
378+
if (!ASSERT_OK_PTR(nstoken, "open NS1"))
379+
goto close;
380+
381+
SYS(close, "ping -c 1 %s.2 > /dev/null", IPV4_NETWORK);
382+
383+
close:
384+
close_netns(nstoken);
385+
xdp_dummy__destroy(skel_dummy);
386+
test_xdp_redirect__destroy(skel);
387+
}
388+
389+
390+
static void xdp_redirect_ping(u32 xdp_flags)
391+
{
392+
struct test_data data = {};
393+
394+
if (ping_setup(&data) < 0)
395+
return;
396+
397+
data.xdp_flags = xdp_flags;
398+
ping_test(&data);
399+
cleanup(&data);
400+
}
401+
402+
void test_xdp_index_redirect(void)
403+
{
404+
if (test__start_subtest("noflag"))
405+
xdp_redirect_ping(0);
406+
407+
if (test__start_subtest("drvflag"))
408+
xdp_redirect_ping(XDP_FLAGS_DRV_MODE);
409+
410+
if (test__start_subtest("skbflag"))
411+
xdp_redirect_ping(XDP_FLAGS_SKB_MODE);
412+
}
413+

tools/testing/selftests/bpf/test_xdp_redirect.sh

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)