Skip to content

Commit

Permalink
selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bastien-curutchet committed Jan 10, 2025
1 parent 7149048 commit f99393d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 80 deletions.
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)

# Order correspond to 'make run_tests' order
TEST_PROGS := test_kmod.sh \
test_xdp_redirect.sh \
test_xdp_redirect_multi.sh \
test_xdp_meta.sh \
test_tunnel.sh \
Expand Down
165 changes: 165 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <bpf/bpf_endian.h>
#include <uapi/linux/netdev.h>
#include "test_xdp_do_redirect.skel.h"
#include "test_xdp_redirect.skel.h"
#include "xdp_dummy.skel.h"

struct udp_packet {
struct ethhdr eth;
Expand Down Expand Up @@ -246,3 +248,166 @@ void test_xdp_do_redirect(void)
SYS_NOFAIL("ip netns del testns");
test_xdp_do_redirect__destroy(skel);
}

#define NS_NB 3
#define NS0 "NS0"
#define NS1 "NS1"
#define NS2 "NS2"
#define IPV4_NETWORK "10.1.1"
#define VETH1_INDEX 111
#define VETH2_INDEX 222

struct test_data {
struct netns_obj *ns[NS_NB];
u32 xdp_flags;
};

static void cleanup(struct test_data *data)
{
int i;

for (i = 0; i < NS_NB; i++)
netns_free(data->ns[i]);
}

/**
* ping_setup -
* Create two veth peers and forward packets in-between using XDP
*
* ------------ ------------
* | NS1 | | NS2 |
* | veth0 | | veth0 |
* | 10.1.1.1 | | 10.1.1.2 |
* -----|------ ------|-----
* | |
* | |
* -----|-----------------------|-------
* | veth1 veth2 |
* | (id:111) (id:222) |
* | | | |
* | ----- xdp forwarding ----- |
* | |
* | NS0 |
* -------------------------------------
*/
static int ping_setup(struct test_data *data)
{
int i;

data->ns[0] = netns_new(NS0, false);
if (!ASSERT_OK_PTR(data->ns[0], "create ns"))
return -1;

for (i = 1; i < NS_NB; i++) {
char ns_name[4] = {};

snprintf(ns_name, 4, "NS%d", i);
data->ns[i] = netns_new(ns_name, false);
if (!ASSERT_OK_PTR(data->ns[i], "create ns"))
goto fail;

SYS(fail,
"ip -n %s link add veth%d index %d%d%d type veth peer name veth0 netns %s",
NS0, i, i, i, i, ns_name);
SYS(fail, "ip -n %s link set veth%d up", NS0, i);

SYS(fail, "ip -n %s addr add %s.%d/24 dev veth0", ns_name, IPV4_NETWORK, i);
SYS(fail, "ip -n %s link set veth0 up", ns_name);
}

return 0;

fail:
cleanup(data);
return -1;
}

static void ping_test(struct test_data *data)
{
struct test_xdp_redirect *skel = NULL;
struct xdp_dummy *skel_dummy = NULL;
struct nstoken *nstoken = NULL;
int i, ret;

skel_dummy = xdp_dummy__open_and_load();
if (!ASSERT_OK_PTR(skel_dummy, "open and load xdp_dummy skeleton"))
goto close;

for (i = 1; i < NS_NB; i++) {
char ns_name[4] = {};

snprintf(ns_name, 4, "NS%d", i);
nstoken = open_netns(ns_name);
if (!ASSERT_OK_PTR(nstoken, "open ns"))
goto close;

ret = bpf_xdp_attach(if_nametoindex("veth0"),
bpf_program__fd(skel_dummy->progs.xdp_dummy_prog),
data->xdp_flags, NULL);
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach dummy_prog"))
goto close;

close_netns(nstoken);
nstoken = NULL;
}

skel = test_xdp_redirect__open_and_load();
if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
goto close;

nstoken = open_netns(NS0);
if (!ASSERT_OK_PTR(nstoken, "open NS0"))
goto close;

ret = bpf_xdp_attach(VETH2_INDEX,
bpf_program__fd(skel->progs.xdp_redirect_to_111),
data->xdp_flags, NULL);
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
goto close;

ret = bpf_xdp_attach(VETH1_INDEX,
bpf_program__fd(skel->progs.xdp_redirect_to_222),
data->xdp_flags, NULL);
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
goto close;

close_netns(nstoken);
nstoken = NULL;

nstoken = open_netns(NS1);
if (!ASSERT_OK_PTR(nstoken, "open NS1"))
goto close;

SYS(close, "ping -c 1 %s.2", IPV4_NETWORK);

close:
close_netns(nstoken);
xdp_dummy__destroy(skel_dummy);
test_xdp_redirect__destroy(skel);
}


static void xdp_redirect_ping(u32 xdp_flags)
{
struct test_data data = {};

if (ping_setup(&data) < 0)
return;

data.xdp_flags = xdp_flags;
ping_test(&data);
cleanup(&data);
}

void test_xdp_index_redirect(void)
{
if (test__start_subtest("noflag"))
xdp_redirect_ping(0);

if (test__start_subtest("drvflag"))
xdp_redirect_ping(XDP_FLAGS_DRV_MODE);

if (test__start_subtest("skbflag"))
xdp_redirect_ping(XDP_FLAGS_SKB_MODE);
}

79 changes: 0 additions & 79 deletions tools/testing/selftests/bpf/test_xdp_redirect.sh

This file was deleted.

0 comments on commit f99393d

Please sign in to comment.