Commit 03f3aa4a authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov
Browse files

Merge branch 'selftests-bpf-migrate-test_xdp_redirect_multi-sh-to-test_progs'

Bastien Curutchet says:

====================
This patch series continues the work to migrate the *.sh tests into
prog_tests framework.

test_xdp_redirect_multi.sh tests the XDP redirections done through
bpf_redirect_map().

This is already partly covered by test_xdp_veth.c that already tests
map redirections at XDP level. What isn't covered yet by test_xdp_veth is
the use of the broadcast flags (BPF_F_BROADCAST or BPF_F_EXCLUDE_INGRESS)
and XDP egress programs.

This series is the prep work that will be followed up adding test cases
to eventually cover the tests done in test_xdp_redirect_multi.sh:
 - PATCH 1 Add an helper to generate unique names
 - PATCH 2 to 9 rework test_xdp_veth to make it more generic and allow to
   configure different test cases
 - PATCH 10 adds test cases for 'classic' bpf_redirect_map()
====================

Link: https://patch.msgid.link/20250131-redirect-multi-v4-0-970b33678512@bootlin.com


Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 12fdd29d 0c4ea7e3
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -446,6 +446,23 @@ char *ping_command(int family)
	return "ping";
}

int append_tid(char *str, size_t sz)
{
	size_t end;

	if (!str)
		return -1;

	end = strlen(str);
	if (end + 8 > sz)
		return -1;

	sprintf(&str[end], "%07d", gettid());
	str[end + 7] = '\0';

	return 0;
}

int remove_netns(const char *name)
{
	char *cmd;
+12 −0
Original line number Diff line number Diff line
@@ -98,6 +98,18 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes);
int make_netns(const char *name);
int remove_netns(const char *name);

/**
 * append_tid() - Append thread ID to the given string.
 *
 * @str: string to extend
 * @sz: string's size
 *
 * 8 characters are used to append the thread ID (7 digits + '\0')
 *
 * Returns -1 on errors, 0 otherwise
 */
int append_tid(char *str, size_t sz);

static __u16 csum_fold(__u32 csum)
{
	csum = (csum & 0xffff) + (csum >> 16);
+197 −124
Original line number Diff line number Diff line
@@ -3,17 +3,27 @@
/* Create 3 namespaces with 3 veth peers, and forward packets in-between using
 * native XDP
 *
 *                      XDP_TX
 * NS1(veth11)        NS2(veth22)        NS3(veth33)
 * Network topology:
 *  ----------        ----------       ----------
 *  |  NS1   |        |  NS2   |       |  NS3   |
 *  | veth11 |        | veth22 |       | veth33 |
 *  ----|-----        -----|----       -----|----
 *      |                  |                |
 *    veth1              veth2            veth3
 *
 * Test cases:
 *  - [test_xdp_veth_redirect] : ping veth33 from veth11
 *
 *    veth11             veth22              veth33
 *  (XDP_PASS)          (XDP_TX)           (XDP_PASS)
 *       |                  |                  |
 *       |                  |                  |
 *   (veth1,            (veth2,            (veth3,
 *   id:111)            id:122)            id:133)
 *     veth1             veth2              veth3
 * (XDP_REDIRECT)     (XDP_REDIRECT)     (XDP_REDIRECT)
 *      ^ |                ^ |                ^ |
 *     | |  XDP_REDIRECT  | |  XDP_REDIRECT  | |
 *      | |                | |                | |
 *      | ------------------ ------------------ |
 *      -----------------------------------------
 *                    XDP_REDIRECT
 */

#define _GNU_SOURCE
@@ -23,137 +33,134 @@
#include "xdp_dummy.skel.h"
#include "xdp_redirect_map.skel.h"
#include "xdp_tx.skel.h"
#include <uapi/linux/if_link.h>

#define VETH_PAIRS_COUNT	3
#define NS_SUFFIX_LEN		6
#define VETH_NAME_MAX_LEN	16
#define VETH_NAME_MAX_LEN	32
#define IP_MAX_LEN		16
#define IP_SRC				"10.1.1.11"
#define IP_DST				"10.1.1.33"
#define IP_CMD_MAX_LEN		128

struct skeletons {
	struct xdp_dummy *xdp_dummy;
	struct xdp_tx *xdp_tx;
	struct xdp_redirect_map *xdp_redirect_maps;
};
#define PROG_NAME_MAX_LEN	128
#define NS_NAME_MAX_LEN		32

struct veth_configuration {
	char local_veth[VETH_NAME_MAX_LEN]; /* Interface in main namespace */
	char remote_veth[VETH_NAME_MAX_LEN]; /* Peer interface in dedicated namespace*/
	const char *namespace; /* Namespace for the remote veth */
	char next_veth[VETH_NAME_MAX_LEN]; /* Local interface to redirect traffic to */
	char *remote_addr; /* IP address of the remote veth */
	char namespace[NS_NAME_MAX_LEN]; /* Namespace for the remote veth */
	int next_veth; /* Local interface to redirect traffic to */
	char remote_addr[IP_MAX_LEN]; /* IP address of the remote veth */
};

static struct veth_configuration config[VETH_PAIRS_COUNT] = {
static const struct veth_configuration default_config[VETH_PAIRS_COUNT] = {
	{
		.local_veth = "veth1",
		.local_veth = "veth1-",
		.remote_veth = "veth11",
		.next_veth = "veth2",
		.next_veth = 1,
		.remote_addr = IP_SRC,
		.namespace = "ns-veth11"
		.namespace = "ns-veth11-"
	},
	{
		.local_veth = "veth2",
		.local_veth = "veth2-",
		.remote_veth = "veth22",
		.next_veth = "veth3",
		.remote_addr = NULL,
		.namespace = "ns-veth22"
		.next_veth = 2,
		.remote_addr = "",
		.namespace = "ns-veth22-"
	},
	{
		.local_veth = "veth3",
		.local_veth = "veth3-",
		.remote_veth = "veth33",
		.next_veth = "veth1",
		.next_veth = 0,
		.remote_addr = IP_DST,
		.namespace = "ns-veth33"
		.namespace = "ns-veth33-"
	}
};

static int attach_programs_to_veth_pair(struct skeletons *skeletons, int index)
struct prog_configuration {
	char local_name[PROG_NAME_MAX_LEN]; /* BPF prog to attach to local_veth */
	char remote_name[PROG_NAME_MAX_LEN]; /* BPF prog to attach to remote_veth */
	u32 local_flags; /* XDP flags to use on local_veth */
	u32 remote_flags; /* XDP flags to use on remote_veth */
};

static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
					struct veth_configuration *net_config,
					struct prog_configuration *prog, int index)
{
	struct bpf_program *local_prog, *remote_prog;
	struct bpf_link **local_link, **remote_link;
	struct nstoken *nstoken;
	struct bpf_link *link;
	int interface;

	switch (index) {
	case 0:
		local_prog = skeletons->xdp_redirect_maps->progs.xdp_redirect_map_0;
		local_link = &skeletons->xdp_redirect_maps->links.xdp_redirect_map_0;
		remote_prog = skeletons->xdp_dummy->progs.xdp_dummy_prog;
		remote_link = &skeletons->xdp_dummy->links.xdp_dummy_prog;
		break;
	case 1:
		local_prog = skeletons->xdp_redirect_maps->progs.xdp_redirect_map_1;
		local_link = &skeletons->xdp_redirect_maps->links.xdp_redirect_map_1;
		remote_prog = skeletons->xdp_tx->progs.xdp_tx;
		remote_link = &skeletons->xdp_tx->links.xdp_tx;
	int interface, ret, i;

	for (i = 0; i < nb_obj; i++) {
		local_prog = bpf_object__find_program_by_name(objs[i], prog[index].local_name);
		if (local_prog)
			break;
	case 2:
		local_prog = skeletons->xdp_redirect_maps->progs.xdp_redirect_map_2;
		local_link = &skeletons->xdp_redirect_maps->links.xdp_redirect_map_2;
		remote_prog = skeletons->xdp_dummy->progs.xdp_dummy_prog;
		remote_link = &skeletons->xdp_dummy->links.xdp_dummy_prog;
	}
	if (!ASSERT_OK_PTR(local_prog, "find local program"))
		return -1;

	for (i = 0; i < nb_obj; i++) {
		remote_prog = bpf_object__find_program_by_name(objs[i], prog[index].remote_name);
		if (remote_prog)
			break;
	}
	interface = if_nametoindex(config[index].local_veth);
	if (!ASSERT_OK_PTR(remote_prog, "find remote program"))
		return -1;

	interface = if_nametoindex(net_config[index].local_veth);
	if (!ASSERT_NEQ(interface, 0, "non zero interface index"))
		return -1;
	link = bpf_program__attach_xdp(local_prog, interface);
	if (!ASSERT_OK_PTR(link, "attach xdp program to local veth"))

	ret = bpf_xdp_attach(interface, bpf_program__fd(local_prog),
			     prog[index].local_flags, NULL);
	if (!ASSERT_OK(ret, "attach xdp program to local veth"))
		return -1;
	*local_link = link;
	nstoken = open_netns(config[index].namespace);

	nstoken = open_netns(net_config[index].namespace);
	if (!ASSERT_OK_PTR(nstoken, "switch to remote veth namespace"))
		return -1;
	interface = if_nametoindex(config[index].remote_veth);

	interface = if_nametoindex(net_config[index].remote_veth);
	if (!ASSERT_NEQ(interface, 0, "non zero interface index")) {
		close_netns(nstoken);
		return -1;
	}
	link = bpf_program__attach_xdp(remote_prog, interface);
	*remote_link = link;

	ret = bpf_xdp_attach(interface, bpf_program__fd(remote_prog),
			     prog[index].remote_flags, NULL);
	if (!ASSERT_OK(ret, "attach xdp program to remote veth")) {
		close_netns(nstoken);
	if (!ASSERT_OK_PTR(link, "attach xdp program to remote veth"))
		return -1;
	}

	close_netns(nstoken);
	return 0;
}

static int configure_network(struct skeletons *skeletons)
static int create_network(struct veth_configuration *net_config)
{
	int interface_id;
	int map_fd;
	int err;
	int i = 0;
	int i, err;

	memcpy(net_config, default_config, VETH_PAIRS_COUNT * sizeof(struct veth_configuration));

	/* First create and configure all interfaces */
	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		SYS(fail, "ip netns add %s", config[i].namespace);
		SYS(fail, "ip link add %s type veth peer name %s netns %s",
		    config[i].local_veth, config[i].remote_veth, config[i].namespace);
		SYS(fail, "ip link set dev %s up", config[i].local_veth);
		if (config[i].remote_addr)
			SYS(fail, "ip -n %s addr add %s/24 dev %s",	config[i].namespace,
			    config[i].remote_addr, config[i].remote_veth);
		SYS(fail, "ip -n %s link set dev %s up", config[i].namespace,
		    config[i].remote_veth);
	}
		err = append_tid(net_config[i].namespace, NS_NAME_MAX_LEN);
		if (!ASSERT_OK(err, "append TID to ns name"))
			return -1;

	/* Then configure the redirect map and attach programs to interfaces */
	map_fd = bpf_map__fd(skeletons->xdp_redirect_maps->maps.tx_port);
	if (!ASSERT_GE(map_fd, 0, "open redirect map"))
		goto fail;
	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		interface_id = if_nametoindex(config[i].next_veth);
		if (!ASSERT_NEQ(interface_id, 0, "non zero interface index"))
			goto fail;
		err = bpf_map_update_elem(map_fd, &i, &interface_id, BPF_ANY);
		if (!ASSERT_OK(err, "configure interface redirection through map"))
			goto fail;
		if (attach_programs_to_veth_pair(skeletons, i))
			goto fail;
		err = append_tid(net_config[i].local_veth, VETH_NAME_MAX_LEN);
		if (!ASSERT_OK(err, "append TID to local veth name"))
			return -1;

		SYS(fail, "ip netns add %s", net_config[i].namespace);
		SYS(fail, "ip link add %s type veth peer name %s netns %s",
		    net_config[i].local_veth, net_config[i].remote_veth, net_config[i].namespace);
		SYS(fail, "ip link set dev %s up", net_config[i].local_veth);
		if (net_config[i].remote_addr[0])
			SYS(fail, "ip -n %s addr add %s/24 dev %s",	net_config[i].namespace,
			    net_config[i].remote_addr, net_config[i].remote_veth);
		SYS(fail, "ip -n %s link set dev %s up", net_config[i].namespace,
		    net_config[i].remote_veth);
	}

	return 0;
@@ -162,52 +169,118 @@ static int configure_network(struct skeletons *skeletons)
	return -1;
}

static void cleanup_network(void)
static void cleanup_network(struct veth_configuration *net_config)
{
	struct nstoken *nstoken;
	int i;

	/* Deleting namespaces is enough to automatically remove veth pairs as well
	 */
	for (i = 0; i < VETH_PAIRS_COUNT; i++)
		SYS_NOFAIL("ip netns del %s", config[i].namespace);
	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		bpf_xdp_detach(if_nametoindex(net_config[i].local_veth), 0, NULL);
		nstoken = open_netns(net_config[i].namespace);
		if (nstoken) {
			bpf_xdp_detach(if_nametoindex(net_config[i].remote_veth), 0, NULL);
			close_netns(nstoken);
		}
		/* in case the detach failed */
		SYS_NOFAIL("ip link del %s", net_config[i].local_veth);
		SYS_NOFAIL("ip netns del %s", net_config[i].namespace);
	}

static int check_ping(struct skeletons *skeletons)
{
	/* Test: if all interfaces are properly configured, we must be able to ping
	 * veth33 from veth11
	 */
	return SYS_NOFAIL("ip netns exec %s ping -c 1 -W 1 %s > /dev/null",
					  config[0].namespace, IP_DST);
}

void test_xdp_veth_redirect(void)
#define VETH_REDIRECT_SKEL_NB	3
static void xdp_veth_redirect(u32 flags)
{
	struct prog_configuration ping_config[VETH_PAIRS_COUNT] = {
		{
			.local_name = "xdp_redirect_map_0",
			.remote_name = "xdp_dummy_prog",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
			.local_name = "xdp_redirect_map_1",
			.remote_name = "xdp_tx",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
	struct skeletons skeletons = {};
			.local_name = "xdp_redirect_map_2",
			.remote_name = "xdp_dummy_prog",
			.local_flags = flags,
			.remote_flags = flags,
		}
	};
	struct veth_configuration net_config[VETH_PAIRS_COUNT];
	struct bpf_object *bpf_objs[VETH_REDIRECT_SKEL_NB];
	struct xdp_redirect_map *xdp_redirect_map;
	struct xdp_dummy *xdp_dummy;
	struct xdp_tx *xdp_tx;
	int map_fd;
	int i;

	skeletons.xdp_dummy = xdp_dummy__open_and_load();
	if (!ASSERT_OK_PTR(skeletons.xdp_dummy, "xdp_dummy__open_and_load"))
	xdp_dummy = xdp_dummy__open_and_load();
	if (!ASSERT_OK_PTR(xdp_dummy, "xdp_dummy__open_and_load"))
		return;

	skeletons.xdp_tx = xdp_tx__open_and_load();
	if (!ASSERT_OK_PTR(skeletons.xdp_tx, "xdp_tx__open_and_load"))
	xdp_tx = xdp_tx__open_and_load();
	if (!ASSERT_OK_PTR(xdp_tx, "xdp_tx__open_and_load"))
		goto destroy_xdp_dummy;

	skeletons.xdp_redirect_maps = xdp_redirect_map__open_and_load();
	if (!ASSERT_OK_PTR(skeletons.xdp_redirect_maps, "xdp_redirect_map__open_and_load"))
	xdp_redirect_map = xdp_redirect_map__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_map, "xdp_redirect_map__open_and_load"))
		goto destroy_xdp_tx;

	if (configure_network(&skeletons))
	if (!ASSERT_OK(create_network(net_config), "create network"))
		goto destroy_xdp_redirect_map;

	ASSERT_OK(check_ping(&skeletons), "ping");
	/* Then configure the redirect map and attach programs to interfaces */
	map_fd = bpf_map__fd(xdp_redirect_map->maps.tx_port);
	if (!ASSERT_OK_FD(map_fd, "open redirect map"))
		goto destroy_xdp_redirect_map;

	bpf_objs[0] = xdp_dummy->obj;
	bpf_objs[1] = xdp_tx->obj;
	bpf_objs[2] = xdp_redirect_map->obj;
	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		int next_veth = net_config[i].next_veth;
		int interface_id;
		int err;

		interface_id = if_nametoindex(net_config[next_veth].local_veth);
		if (!ASSERT_NEQ(interface_id, 0, "non zero interface index"))
			goto destroy_xdp_redirect_map;
		err = bpf_map_update_elem(map_fd, &i, &interface_id, BPF_ANY);
		if (!ASSERT_OK(err, "configure interface redirection through map"))
			goto destroy_xdp_redirect_map;
		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB,
						 net_config, ping_config, i))
			goto destroy_xdp_redirect_map;
	}

	/* Test: if all interfaces are properly configured, we must be able to ping
	 * veth33 from veth11
	 */
	ASSERT_OK(SYS_NOFAIL("ip netns exec %s ping -c 1 -W 1 %s > /dev/null",
			     net_config[0].namespace, IP_DST), "ping");

destroy_xdp_redirect_map:
	xdp_redirect_map__destroy(skeletons.xdp_redirect_maps);
	xdp_redirect_map__destroy(xdp_redirect_map);
destroy_xdp_tx:
	xdp_tx__destroy(skeletons.xdp_tx);
	xdp_tx__destroy(xdp_tx);
destroy_xdp_dummy:
	xdp_dummy__destroy(skeletons.xdp_dummy);
	xdp_dummy__destroy(xdp_dummy);

	cleanup_network(net_config);
}

void test_xdp_veth_redirect(void)
{
	if (test__start_subtest("0"))
		xdp_veth_redirect(0);

	if (test__start_subtest("DRV_MODE"))
		xdp_veth_redirect(XDP_FLAGS_DRV_MODE);

	cleanup_network();
	if (test__start_subtest("SKB_MODE"))
		xdp_veth_redirect(XDP_FLAGS_SKB_MODE);
}