Skip to contentSkip to conversation

selftests/bpf: Migrate test_xdp_redirect_multi.sh to test_progs

PATCH Superseded Bastien Curutchet (eBPF Foundation) 2 participants 20 messages Jan 28, 2025 – Jan 29, 2025

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover selftests/bpf: Migrate test_xdp_redirect_multi.sh to test_progs

Hi all,

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.

Hence, this patch series add test cases to test_xdp_veth.c to get rid of the 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()
- PATCH 11 and 12 cover the broadcast flags
- PATCH 13 covers the XDP egress programs
- PATCH 14 removes test_xdp_redirect_multi.sh

---
Changes in v3:
- Add append_tid() helper and use unique names to allow parallel testing
- Check create_network()'s return value through ASSERT_OK()
- Remove check_ping() and unused defines
- Change next_veth type (from string to int)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use serial_test_* to avoid conflict between tests
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Bastien Curutchet (eBPF Foundation) (14):
selftests/bpf: helpers: Add append_tid() selftests/bpf: test_xdp_veth: Remove unused defines selftests/bpf: test_xdp_veth: Remove unecessarry check_ping() selftests/bpf: test_xdp_veth: Use int to describe next veth selftests/bpf: test_xdp_veth: Split network configuration selftests/bpf: test_xdp_veth: Rename config[] selftests/bpf: test_xdp_veth: Add prog_config[] table selftests/bpf: test_xdp_veth: Add XDP flags to prog_configuration selftests/bpf: test_xdp_veth: Use unique names selftests/bpf: test_xdp_veth: Add new test cases for XDP flags selftests/bpf: Optionally select broadcasting flags selftests/bpf: test_xdp_veth: Add XDP broadcast redirection tests selftests/bpf: test_xdp_veth: Add XDP program on egress test selftests/bpf: Remove test_xdp_redirect_multi.sh

 tools/testing/selftests/bpf/Makefile               |   2 -
 tools/testing/selftests/bpf/network_helpers.c      |  11 +
 tools/testing/selftests/bpf/network_helpers.h      |  10 +
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 589 ++++++++++++++++-----
 .../testing/selftests/bpf/progs/xdp_redirect_map.c |  89 ++++
 .../selftests/bpf/progs/xdp_redirect_multi_kern.c  |  41 +-
 .../selftests/bpf/test_xdp_redirect_multi.sh       | 214 --------
 tools/testing/selftests/bpf/xdp_redirect_multi.c   | 226 --------
 8 files changed, 608 insertions(+), 574 deletions(-)

---
base-commit: e65d1a6e096af0648ffdbc9d7e8bd7a44dcc700b
change-id: 20250103-redirect-multi-245d6eafb5d1

Best regards,

Signature · 1 line
Bastien Curutchet (eBPF Foundation) <[email protected]>
#1 lore raw

PATCH v3 1/14 selftests/bpf: helpers: Add append_tid()

Some tests can't be run in parallel because they use same namespace names or veth names.

Create an helper that appends the thread ID to a given string. 8 characters are used for it (7 digits + '\0')

---
 tools/testing/selftests/bpf/network_helpers.c | 11 +++++++++++
 tools/testing/selftests/bpf/network_helpers.h | 10 ++++++++++
 2 files changed, 21 insertions(+)
tools/testing/selftests/bpf/network_helpers.c +11 -0
index 80844a5fb1feef2ff73c2f0293e52495803ab769..d2ff7521aaa696ed04d8f1308394b4c01c1c038b 100644
@@ -446,6 +446,17 @@ char *ping_command(int family)
 	return "ping";
 }
 
+int append_tid(char *str, size_t offset)
+{
+	if (!str)
+		return -1;
+
+	sprintf(&str[offset], "%07d", gettid());
+	str[offset + 7] = '\0';
+
+	return 0;
+}
+
 int remove_netns(const char *name)
 {
 	char *cmd;
tools/testing/selftests/bpf/network_helpers.h +10 -0
index ebec8a8d6f81e9d079a3b087127a37885c656856..b2451dd00594190e1dcb58498d70dd80c0e7c51c 100644
@@ -98,6 +98,16 @@ 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.
+ *
+ * /!\ the appended thread ID is 8 characters long
+ *     so the input string must be allocated accordingly
+ *
+ * Returns -1 if input is NULL, 0 otherwise
+ */
+int append_tid(char *str, size_t offset);
+
 static __u16 csum_fold(__u32 csum)
 {
 	csum = (csum & 0xffff) + (csum >> 16);
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH bpf-next v3 01/14] selftests/bpf: helpers: Add append_tid()

Quoted 17 lines · Bastien Curutchet (eBPF Foundation)

Some tests can't be run in parallel because they use same namespace names or veth names.

Create an helper that appends the thread ID to a given string. 8 characters are used for it (7 digits + '\0')

---
  tools/testing/selftests/bpf/network_helpers.c | 11 +++++++++++
  tools/testing/selftests/bpf/network_helpers.h | 10 ++++++++++
  2 files changed, 21 insertions(+)
tools/testing/selftests/bpf/network_helpers.c +0 -0
index 80844a5fb1feef2ff73c2f0293e52495803ab769..d2ff7521aaa696ed04d8f1308394b4c01c1c038b 100644

tools/testing/selftests/bpf/network_helpers.c char *ping_command(int family) · line 449

Quoted 4 lines
tools/testing/selftests/bpf/network_helpers.c +1 -0
@@ -446,6 +446,17 @@ char *ping_command(int family)
  	return "ping";
  }
 
+int append_tid(char *str, size_t offset)

nit. offset should always be strlen(str) now. The append_tid will be easier to use if the append_tid always does the strlen() itself to figure out the end of the str.

It will be useful to replace the "size_t offset" arg with "size_t sz" which tells the max size of the "char *str" and the append_tid does a check to ensure there is enough space to append the "%07d" tid.

Quoted 35 lines
tools/testing/selftests/bpf/network_helpers.c +10 -0
+{
+	if (!str)
+		return -1;
+
+	sprintf(&str[offset], "%07d", gettid());
+	str[offset + 7] = '\0';
+
+	return 0;
+}
+
  int remove_netns(const char *name)
  {
  	char *cmd;
tools/testing/selftests/bpf/network_helpers.h +10 -0
index ebec8a8d6f81e9d079a3b087127a37885c656856..b2451dd00594190e1dcb58498d70dd80c0e7c51c 100644
@@ -98,6 +98,16 @@ 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.
+ *
+ * /!\ the appended thread ID is 8 characters long
+ *     so the input string must be allocated accordingly
+ *
+ * Returns -1 if input is NULL, 0 otherwise
+ */
+int append_tid(char *str, size_t offset);
+
  static __u16 csum_fold(__u32 csum)
  {
  	csum = (csum & 0xffff) + (csum >> 16);
#3 lore raw

Re: [PATCH bpf-next v3 01/14] selftests/bpf: helpers: Add append_tid()

Hi Martin,

Quoted 23 lines · Martin KaFai Lau
Quoted 17 lines · Bastien Curutchet (eBPF Foundation)

Some tests can't be run in parallel because they use same namespace names or veth names.

Create an helper that appends the thread ID to a given string. 8 characters are used for it (7 digits + '\0')

---
  tools/testing/selftests/bpf/network_helpers.c | 11 +++++++++++ tools/testing/selftests/bpf/network_helpers.h | 10 ++++++++++

  2 files changed, 21 insertions(+)
tools/testing/selftests/bpf/network_helpers.c +0 -0
index 80844a5fb1feef2ff73c2f0293e52495803ab769..d2ff7521aaa696ed04d8f1308394b4c01c1c038b 100644

tools/testing/selftests/bpf/network_helpers.c char *ping_command(int family) · line 448

Quoted 3 lines
tools/testing/selftests/bpf/network_helpers.c +1 -0
@@ -446,6 +446,17 @@ char *ping_command(int family)
       return "ping";
   }
+int append_tid(char *str, size_t offset)

nit. offset should always be strlen(str) now. The append_tid will be easier to use if the append_tid always does the strlen() itself to figure out the end of the str.

Quoted 2 lines

It will be useful to replace the "size_t offset" arg with "size_t sz" which tells the max size of the "char *str" and the append_tid does a check to ensure there is enough space to append the "%07d" tid.

Ok, I'll do that.

Best regards,
Bastien

#4 lore raw

PATCH v3 2/14 selftests/bpf: test_xdp_veth: Remove unused defines

In reply to Bastien Curutchet (eBPF Foundation)

IP_CMD_MAX_LEN and NS_SUFFIX_LEN aren't used anywhere.

Remove these unused defines

---
 tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c | 2 --
 1 file changed, 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +0 -2
index 8d75424fe6bc8b2d4eeabe3ec49b883284c834e9..95e1791ea7e0f950609607b30d35f78da82e058b 100644
@@ -25,11 +25,9 @@
 #include "xdp_tx.skel.h"
 
 #define VETH_PAIRS_COUNT	3
-#define NS_SUFFIX_LEN		6
 #define VETH_NAME_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;
Signature · 1 line
2.47.1
#5 lore raw

PATCH v3 3/14 selftests/bpf: test_xdp_veth: Remove unecessarry check_ping()

In reply to Bastien Curutchet (eBPF Foundation)

check_ping() directly returns a SYS_NOFAIL without any previous treatment. It's called only once in the file and hardcodes the used namespace and ip address.

Replace check_ping() with a direct call of SYS_NOFAIL in the test.

---
 tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +5 -10
index 95e1791ea7e0f950609607b30d35f78da82e058b..d41884fdc430f1ceed53f16b0dddbdbb34f83301 100644
@@ -170,15 +170,6 @@ static void cleanup_network(void)
 		SYS_NOFAIL("ip netns del %s", 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)
 {
 	struct skeletons skeletons = {};
@@ -198,7 +189,11 @@ void test_xdp_veth_redirect(void)
 	if (configure_network(&skeletons))
 		goto destroy_xdp_redirect_map;
 
-	ASSERT_OK(check_ping(&skeletons), "ping");
+	/* 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",
+			     config[0].namespace, IP_DST), "ping");
 
 destroy_xdp_redirect_map:
 	xdp_redirect_map__destroy(skeletons.xdp_redirect_maps);
Signature · 1 line
2.47.1
#6 lore raw

PATCH v3 4/14 selftests/bpf: test_xdp_veth: Use int to describe next veth

In reply to Bastien Curutchet (eBPF Foundation)

In the struct veth_configuration, the next_veth string is used to tell the next virtual interface to which packets must be redirected to. So it has to match the local_veth string of an other veth_configuration.

Change next_veth type to int to avoid handling two identical strings. This integer is used as an offset in the network configuration table.

---
 tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +7 -5
index d41884fdc430f1ceed53f16b0dddbdbb34f83301..942c6e99e15ef69003c033e7bc1bfc9bc9777557 100644
@@ -39,7 +39,7 @@ 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 */
+	int next_veth; /* Local interface to redirect traffic to */
 	char *remote_addr; /* IP address of the remote veth */
 };
 
@@ -47,21 +47,21 @@ static struct veth_configuration config[VETH_PAIRS_COUNT] = {
 	{
 		.local_veth = "veth1",
 		.remote_veth = "veth11",
-		.next_veth = "veth2",
+		.next_veth = 1,
 		.remote_addr = IP_SRC,
 		.namespace = "ns-veth11"
 	},
 	{
 		.local_veth = "veth2",
 		.remote_veth = "veth22",
-		.next_veth = "veth3",
+		.next_veth = 2,
 		.remote_addr = NULL,
 		.namespace = "ns-veth22"
 	},
 	{
 		.local_veth = "veth3",
 		.remote_veth = "veth33",
-		.next_veth = "veth1",
+		.next_veth = 0,
 		.remote_addr = IP_DST,
 		.namespace = "ns-veth33"
 	}
@@ -144,7 +144,9 @@ static int configure_network(struct skeletons *skeletons)
 	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);
+		int next_veth = config[i].next_veth;
+
+		interface_id = if_nametoindex(config[next_veth].local_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);
Signature · 1 line
2.47.1
#7 lore raw

PATCH v3 5/14 selftests/bpf: test_xdp_veth: Split network configuration

In reply to Bastien Curutchet (eBPF Foundation)

configure_network() does two things : it first creates the network topology and then configures the BPF maps to fit the test needs. This isn't convenient if we want to re-use the same network topology for different test cases.

Rename configure_network() create_network().
Move the BPF configuration to the test itself.
Split the test description in two parts, first the description of the network topology, then the description of the test case. Remove the veth indexes from the ASCII art as dynamic ones are used

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 81 +++++++++++++---------
 1 file changed, 47 insertions(+), 34 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +47 -34
index 942c6e99e15ef69003c033e7bc1bfc9bc9777557..710136861bda7607dcaca6186b5acfe0082a870c 100644
@@ -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)
- *      |                  |                  |
- *      |                  |                  |
- *   (veth1,            (veth2,            (veth3,
- *   id:111)            id:122)            id:133)
- *     ^ |                ^ |                ^ |
- *     | |  XDP_REDIRECT  | |  XDP_REDIRECT  | |
- *     | ------------------ ------------------ |
- *     -----------------------------------------
- *                    XDP_REDIRECT
+ * 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
+ * (XDP_REDIRECT)     (XDP_REDIRECT)     (XDP_REDIRECT)
+ *      ^ |                ^ |                ^ |
+ *      | |                | |                | |
+ *      | ------------------ ------------------ |
+ *      -----------------------------------------
  */
 
 #define _GNU_SOURCE
@@ -119,12 +129,9 @@ static int attach_programs_to_veth_pair(struct skeletons *skeletons, int index)
 	return 0;
 }
 
-static int configure_network(struct skeletons *skeletons)
+static int create_network(void)
 {
-	int interface_id;
-	int map_fd;
-	int err;
-	int i = 0;
+	int i;
 
 	/* First create and configure all interfaces */
 	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
@@ -139,27 +146,11 @@ static int configure_network(struct skeletons *skeletons)
 		    config[i].remote_veth);
 	}
 
-	/* 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++) {
-		int next_veth = config[i].next_veth;
-
-		interface_id = if_nametoindex(config[next_veth].local_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;
-	}
-
 	return 0;
 
 fail:
 	return -1;
+
 }
 
 static void cleanup_network(void)
@@ -175,6 +166,8 @@ static void cleanup_network(void)
 void test_xdp_veth_redirect(void)
 {
 	struct skeletons skeletons = {};
+	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"))
@@ -188,9 +181,29 @@ void test_xdp_veth_redirect(void)
 	if (!ASSERT_OK_PTR(skeletons.xdp_redirect_maps, "xdp_redirect_map__open_and_load"))
 		goto destroy_xdp_tx;
 
-	if (configure_network(&skeletons))
+	if (!ASSERT_OK(create_network(), "create_network"))
 		goto destroy_xdp_redirect_map;
 
+	/* Then configure the redirect map and attach programs to interfaces */
+	map_fd = bpf_map__fd(skeletons.xdp_redirect_maps->maps.tx_port);
+	if (!ASSERT_OK_FD(map_fd, "open redirect map"))
+		goto destroy_xdp_redirect_map;
+
+	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
+		int next_veth = config[i].next_veth;
+		int interface_id;
+		int err;
+
+		interface_id = if_nametoindex(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(&skeletons, i))
+			goto destroy_xdp_redirect_map;
+	}
+
 	/* Test: if all interfaces are properly configured, we must be able to ping
 	 * veth33 from veth11
 	 */
Signature · 1 line
2.47.1
#8 lore raw

PATCH v3 6/14 selftests/bpf: test_xdp_veth: Rename config[]

In reply to Bastien Curutchet (eBPF Foundation)

The network topology is held by the config[] table. This 'config' name is a bit too generic if we want to add other configuration variables.

Rename config[] to net_config[].

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 32 +++++++++++-----------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +16 -16
index 710136861bda7607dcaca6186b5acfe0082a870c..a214d5b479bea3008ebf71e0ba37bc50b9561f14 100644
@@ -53,7 +53,7 @@ struct veth_configuration {
 	char *remote_addr; /* IP address of the remote veth */
 };
 
-static struct veth_configuration config[VETH_PAIRS_COUNT] = {
+static struct veth_configuration net_config[VETH_PAIRS_COUNT] = {
 	{
 		.local_veth = "veth1",
 		.remote_veth = "veth11",
@@ -105,17 +105,17 @@ static int attach_programs_to_veth_pair(struct skeletons *skeletons, int index)
 		remote_link = &skeletons->xdp_dummy->links.xdp_dummy_prog;
 		break;
 	}
-	interface = if_nametoindex(config[index].local_veth);
+	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"))
 		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;
@@ -135,15 +135,15 @@ static int create_network(void)
 
 	/* 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 netns add %s", net_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);
+		    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)
+			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;
@@ -160,7 +160,7 @@ static void cleanup_network(void)
 	/* 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);
+		SYS_NOFAIL("ip netns del %s", net_config[i].namespace);
 }
 
 void test_xdp_veth_redirect(void)
@@ -190,11 +190,11 @@ void test_xdp_veth_redirect(void)
 		goto destroy_xdp_redirect_map;
 
 	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
-		int next_veth = config[i].next_veth;
+		int next_veth = net_config[i].next_veth;
 		int interface_id;
 		int err;
 
-		interface_id = if_nametoindex(config[next_veth].local_veth);
+		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);
@@ -208,7 +208,7 @@ void test_xdp_veth_redirect(void)
 	 * veth33 from veth11
 	 */
 	ASSERT_OK(SYS_NOFAIL("ip netns exec %s ping -c 1 -W 1 %s > /dev/null",
-			     config[0].namespace, IP_DST), "ping");
+			     net_config[0].namespace, IP_DST), "ping");
 
 destroy_xdp_redirect_map:
 	xdp_redirect_map__destroy(skeletons.xdp_redirect_maps);
Signature · 1 line
2.47.1
#9 lore raw

PATCH v3 7/14 selftests/bpf: test_xdp_veth: Add prog_config[] table

In reply to Bastien Curutchet (eBPF Foundation)

The BPF program attached to each veth is hardcoded through the use of the struct skeletons. It prevents from re-using the initialization code in new test cases.

Replace the struct skeletons by a bpf_object table.
Add a struct prog_configuration that holds the name of BPF program to load on a given veth pair.
Use bpf_object__find_program_by_name() / bpf_xdp_attach() API instead of bpf_program__attach_xdp() to retrieve the BPF programs from their names. Detach BPF progs in the cleanup() as it's not automatically done by this API.

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 133 +++++++++++++--------
 1 file changed, 80 insertions(+), 53 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +80 -53
index a214d5b479bea3008ebf71e0ba37bc50b9561f14..d1435490b96701a33236adc10dd1b120b95fda58 100644
@@ -38,12 +38,7 @@
 #define VETH_NAME_MAX_LEN	16
 #define IP_SRC				"10.1.1.11"
 #define IP_DST				"10.1.1.33"
-
-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
 
 struct veth_configuration {
 	char local_veth[VETH_NAME_MAX_LEN]; /* Interface in main namespace */
@@ -77,55 +72,59 @@ static struct veth_configuration net_config[VETH_PAIRS_COUNT] = {
 	}
 };
 
-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 */
+};
+
+static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
+					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;
-		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;
-		break;
+	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;
 	}
+	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;
+	}
+	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), 0, NULL);
+	if (!ASSERT_OK(ret, "attach xdp program to local veth"))
 		return -1;
-	*local_link = link;
+
 	nstoken = open_netns(net_config[index].namespace);
 	if (!ASSERT_OK_PTR(nstoken, "switch to remote veth namespace"))
 		return -1;
+
 	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;
-	close_netns(nstoken);
-	if (!ASSERT_OK_PTR(link, "attach xdp program to remote veth"))
+
+	ret = bpf_xdp_attach(interface, bpf_program__fd(remote_prog), 0, NULL);
+	if (!ASSERT_OK(ret, "attach xdp program to remote veth")) {
+		close_netns(nstoken);
 		return -1;
+	}
 
+	close_netns(nstoken);
 	return 0;
 }
 
@@ -150,45 +149,73 @@ static int create_network(void)
 
 fail:
 	return -1;
-
 }
 
 static void cleanup_network(void)
 {
+	struct nstoken *nstoken;
 	int i;
 
-	/* Deleting namespaces is enough to automatically remove veth pairs as well
-	 */
-	for (i = 0; i < VETH_PAIRS_COUNT; i++)
+	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);
+	}
 }
 
+#define VETH_REDIRECT_SKEL_NB	3
 void test_xdp_veth_redirect(void)
 {
-	struct skeletons skeletons = {};
+	struct prog_configuration ping_config[VETH_PAIRS_COUNT] = {
+		{
+			.local_name = "xdp_redirect_map_0",
+			.remote_name = "xdp_dummy_prog",
+		},
+		{
+			.local_name = "xdp_redirect_map_1",
+			.remote_name = "xdp_tx",
+		},
+		{
+			.local_name = "xdp_redirect_map_2",
+			.remote_name = "xdp_dummy_prog",
+		}
+	};
+	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 (!ASSERT_OK(create_network(), "create_network"))
 		goto destroy_xdp_redirect_map;
 
 	/* Then configure the redirect map and attach programs to interfaces */
-	map_fd = bpf_map__fd(skeletons.xdp_redirect_maps->maps.tx_port);
+	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;
@@ -200,7 +227,7 @@ void test_xdp_veth_redirect(void)
 		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(&skeletons, i))
+		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB, ping_config, i))
 			goto destroy_xdp_redirect_map;
 	}
 
@@ -211,11 +238,11 @@ void test_xdp_veth_redirect(void)
 			     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();
 }
Signature · 1 line
2.47.1
#10 lore raw

PATCH v3 8/14 selftests/bpf: test_xdp_veth: Add XDP flags to prog_configuration

In reply to Bastien Curutchet (eBPF Foundation)

XDP flags are hardcoded to 0 at attachment.

Add flags attributes to the struct prog_configuration to allow flag modifications for each test case.

---
 tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +12 -2
index d1435490b96701a33236adc10dd1b120b95fda58..59fa742b16bd46853b8cfd0ea56a497351c7fb2a 100644
@@ -75,6 +75,8 @@ static struct veth_configuration net_config[VETH_PAIRS_COUNT] = {
 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,
@@ -104,7 +106,8 @@ static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
 	if (!ASSERT_NEQ(interface, 0, "non zero interface index"))
 		return -1;
 
-	ret = bpf_xdp_attach(interface, bpf_program__fd(local_prog), 0, NULL);
+	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;
 
@@ -118,7 +121,8 @@ static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
 		return -1;
 	}
 
-	ret = bpf_xdp_attach(interface, bpf_program__fd(remote_prog), 0, NULL);
+	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);
 		return -1;
@@ -176,14 +180,20 @@ void test_xdp_veth_redirect(void)
 		{
 			.local_name = "xdp_redirect_map_0",
 			.remote_name = "xdp_dummy_prog",
+			.local_flags = 0,
+			.remote_flags = 0,
 		},
 		{
 			.local_name = "xdp_redirect_map_1",
 			.remote_name = "xdp_tx",
+			.local_flags = 0,
+			.remote_flags = 0,
 		},
 		{
 			.local_name = "xdp_redirect_map_2",
 			.remote_name = "xdp_dummy_prog",
+			.local_flags = 0,
+			.remote_flags = 0,
 		}
 	};
 	struct bpf_object *bpf_objs[VETH_REDIRECT_SKEL_NB];
Signature · 1 line
2.47.1
#11 lore raw

PATCH v3 9/14 selftests/bpf: test_xdp_veth: Use unique names

In reply to Bastien Curutchet (eBPF Foundation)

The network namespaces and the veth used by the tests have hardcoded names that can conflict with other tests during parallel runs.

Use the append_tid() helper to ensure the uniqueness of these names. Use the static network configuration table as a template on which thread IDs are appended in each test.
Set a fixed size to remote_addr field so the struct veth_configuration can also have a fixed size.

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 52 ++++++++++++++--------
 1 file changed, 34 insertions(+), 18 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +34 -18
index 59fa742b16bd46853b8cfd0ea56a497351c7fb2a..b609cff198e44f71815e4b13a4b7c907223403d6 100644
@@ -35,40 +35,43 @@
 #include "xdp_tx.skel.h"
 
 #define VETH_PAIRS_COUNT	3
-#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 NO_IP				"NO_IP"
 #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 namespace[NS_NAME_MAX_LEN]; /* Namespace for the remote veth */
 	int next_veth; /* Local interface to redirect traffic to */
-	char *remote_addr; /* IP address of the remote veth */
+	char remote_addr[IP_MAX_LEN]; /* IP address of the remote veth */
 };
 
-static struct veth_configuration net_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 = 1,
 		.remote_addr = IP_SRC,
-		.namespace = "ns-veth11"
+		.namespace = "ns-veth11-"
 	},
 	{
-		.local_veth = "veth2",
+		.local_veth = "veth2-",
 		.remote_veth = "veth22",
 		.next_veth = 2,
-		.remote_addr = NULL,
-		.namespace = "ns-veth22"
+		.remote_addr = NO_IP,
+		.namespace = "ns-veth22-"
 	},
 	{
-		.local_veth = "veth3",
+		.local_veth = "veth3-",
 		.remote_veth = "veth33",
 		.next_veth = 0,
 		.remote_addr = IP_DST,
-		.namespace = "ns-veth33"
+		.namespace = "ns-veth33-"
 	}
 };
 
@@ -80,6 +83,7 @@ struct prog_configuration {
 };
 
 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;
@@ -132,17 +136,27 @@ static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
 	return 0;
 }
 
-static int create_network(void)
+static int create_network(struct veth_configuration *net_config)
 {
-	int i;
+	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++) {
+		err = append_tid(net_config[i].namespace, strlen(net_config[i].namespace));
+		if (!ASSERT_OK(err, "append TID to ns name"))
+			return -1;
+
+		err = append_tid(net_config[i].local_veth, strlen(net_config[i].local_veth));
+		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)
+		if (memcmp(net_config[i].remote_addr, NO_IP, 5))
 			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,
@@ -155,7 +169,7 @@ static int create_network(void)
 	return -1;
 }
 
-static void cleanup_network(void)
+static void cleanup_network(struct veth_configuration *net_config)
 {
 	struct nstoken *nstoken;
 	int i;
@@ -196,6 +210,7 @@ void test_xdp_veth_redirect(void)
 			.remote_flags = 0,
 		}
 	};
+	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;
@@ -215,7 +230,7 @@ void test_xdp_veth_redirect(void)
 	if (!ASSERT_OK_PTR(xdp_redirect_map, "xdp_redirect_map__open_and_load"))
 		goto destroy_xdp_tx;
 
-	if (!ASSERT_OK(create_network(), "create_network"))
+	if (!ASSERT_OK(create_network(net_config), "create network"))
 		goto destroy_xdp_redirect_map;
 
 	/* Then configure the redirect map and attach programs to interfaces */
@@ -237,7 +252,8 @@ void test_xdp_veth_redirect(void)
 		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, ping_config, i))
+		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB,
+						 net_config, ping_config, i))
 			goto destroy_xdp_redirect_map;
 	}
 
@@ -254,5 +270,5 @@ void test_xdp_veth_redirect(void)
 destroy_xdp_dummy:
 	xdp_dummy__destroy(xdp_dummy);
 
-	cleanup_network();
+	cleanup_network(net_config);
 }
Signature · 1 line
2.47.1
#12 lore raw

Re: [PATCH bpf-next v3 09/14] selftests/bpf: test_xdp_veth: Use unique names

Quoted 40 lines · Bastien Curutchet (eBPF Foundation)
(quoted diff) +15 -7
+#define NO_IP				"NO_IP"
  #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 namespace[NS_NAME_MAX_LEN]; /* Namespace for the remote veth */
  	int next_veth; /* Local interface to redirect traffic to */
-	char *remote_addr; /* IP address of the remote veth */
+	char remote_addr[IP_MAX_LEN]; /* IP address of the remote veth */
  };
 
 
  	{
-		.local_veth = "veth2",
+		.local_veth = "veth2-",
  		.remote_veth = "veth22",
  		.next_veth = 2,
-		.remote_addr = NULL,
-		.namespace = "ns-veth22"
+		.remote_addr = NO_IP,
+		.namespace = "ns-veth22-"
  	},
 
-static int create_network(void)
+static int create_network(struct veth_configuration *net_config)
  {
-	int i;
+	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++) {
+		err = append_tid(net_config[i].namespace, strlen(net_config[i].namespace));
+		if (!ASSERT_OK(err, "append TID to ns name"))
+			return -1;
+

Review

Quoted 10 lines
(quoted diff) +5 -1
+		err = append_tid(net_config[i].local_veth, strlen(net_config[i].local_veth));
+		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)
+		if (memcmp(net_config[i].remote_addr, NO_IP, 5))

nit. May be "if (net_config[i].remote_addr[0])" instead of defining a new "NO_IP" and then memcmp.

Quoted 6 lines

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,

tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +0 -0
@@ -155,7 +169,7 @@ static int create_network(void)
  	return -1;
  }
#13 lore raw

PATCH v3 10/14 selftests/bpf: test_xdp_veth: Add new test cases for XDP flags

In reply to Bastien Curutchet (eBPF Foundation)

The XDP redirection is tested without any flag provided to the xdp_attach() function.

Add two subtests that check the correct behaviour with XDP_FLAGS_{DRV/SKB}_MODE flags

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 27 ++++++++++++++++------
 1 file changed, 20 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +20 -7
index b609cff198e44f71815e4b13a4b7c907223403d6..64f72068a0100e05bfd327f8291cfc454bc6f518 100644
@@ -33,6 +33,7 @@
 #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 VETH_NAME_MAX_LEN	32
@@ -188,26 +189,26 @@ static void cleanup_network(struct veth_configuration *net_config)
 }
 
 #define VETH_REDIRECT_SKEL_NB	3
-void test_xdp_veth_redirect(void)
+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 = 0,
-			.remote_flags = 0,
+			.local_flags = flags,
+			.remote_flags = flags,
 		},
 		{
 			.local_name = "xdp_redirect_map_1",
 			.remote_name = "xdp_tx",
-			.local_flags = 0,
-			.remote_flags = 0,
+			.local_flags = flags,
+			.remote_flags = flags,
 		},
 		{
 			.local_name = "xdp_redirect_map_2",
 			.remote_name = "xdp_dummy_prog",
-			.local_flags = 0,
-			.remote_flags = 0,
+			.local_flags = flags,
+			.remote_flags = flags,
 		}
 	};
 	struct veth_configuration net_config[VETH_PAIRS_COUNT];
@@ -272,3 +273,15 @@ void test_xdp_veth_redirect(void)
 
 	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);
+
+	if (test__start_subtest("SKB_MODE"))
+		xdp_veth_redirect(XDP_FLAGS_SKB_MODE);
+}
Signature · 1 line
2.47.1
#14 lore raw

PATCH v3 11/14 selftests/bpf: Optionally select broadcasting flags

In reply to Bastien Curutchet (eBPF Foundation)

Broadcasting flags are hardcoded for each kind for protocol.

Create a redirect_flags map that allows to select the broadcasting flags to use in the bpf_redirect_map(). The protocol ID is used as a key. Set the old hardcoded values as default if the map isn't filled by the BPF caller.

---
 .../selftests/bpf/progs/xdp_redirect_multi_kern.c  | 41 +++++++++++++++-------
 1 file changed, 29 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c +29 -12
index 97b26a30b59a758f5a5f2152af509acef80031ce..bc2945ed8a8017021c2792671b4de9aa4928a3e4 100644
@@ -34,6 +34,14 @@ struct {
 	__uint(max_entries, 128);
 } mac_map SEC(".maps");
 
+/* map to store redirect flags for each protocol*/
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, __u16);
+	__type(value, __u64);
+	__uint(max_entries, 16);
+} redirect_flags SEC(".maps");
+
 SEC("xdp")
 int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
 {
@@ -41,25 +49,34 @@ int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
 	void *data = (void *)(long)ctx->data;
 	int if_index = ctx->ingress_ifindex;
 	struct ethhdr *eth = data;
+	__u64 *flags_from_map;
 	__u16 h_proto;
 	__u64 nh_off;
+	__u64 flags;
 
 	nh_off = sizeof(*eth);
 	if (data + nh_off > data_end)
 		return XDP_DROP;
 
-	h_proto = eth->h_proto;
-
-	/* Using IPv4 for (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) testing */
-	if (h_proto == bpf_htons(ETH_P_IP))
-		return bpf_redirect_map(&map_all, 0,
-					BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
-	/* Using IPv6 for none flag testing */
-	else if (h_proto == bpf_htons(ETH_P_IPV6))
-		return bpf_redirect_map(&map_all, if_index, 0);
-	/* All others for BPF_F_BROADCAST testing */
-	else
-		return bpf_redirect_map(&map_all, 0, BPF_F_BROADCAST);
+	h_proto = bpf_htons(eth->h_proto);
+
+	flags_from_map = bpf_map_lookup_elem(&redirect_flags, &h_proto);
+
+	/* Default flags for IPv4 : (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) */
+	if (h_proto == ETH_P_IP) {
+		flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS;
+		return bpf_redirect_map(&map_all, 0, flags);
+	}
+	/* Default flags for IPv6 : 0 */
+	if (h_proto == ETH_P_IPV6) {
+		flags = flags_from_map ? *flags_from_map : 0;
+		return bpf_redirect_map(&map_all, if_index, flags);
+	}
+	/* Default flags for others BPF_F_BROADCAST : 0 */
+	else {
+		flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST;
+		return bpf_redirect_map(&map_all, 0, flags);
+	}
 }
 
 /* The following 2 progs are for 2nd devmap prog testing */
Signature · 1 line
2.47.1
#15 lore raw

PATCH v3 12/14 selftests/bpf: test_xdp_veth: Add XDP broadcast redirection tests

In reply to Bastien Curutchet (eBPF Foundation)

XDP redirections with BPF_F_BROADCAST and BPF_F_EXCLUDE_INGRESS flags are tested by test_xdp_redirect_multi.sh but not within the test_progs framework.

Add a broadcast test case in test_xdp_veth.c to test them. Use the same BPF programs than the one used by test_xdp_redirect_multi.sh.
Use a BPF map to select the broadcast flags.
Use a BPF map with an entry per veth to check whether packets are received or not
Set the tests to run serially to avoid conflicts with test_xdp_veth_redirect

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 150 +++++++++++++++++++++
 .../testing/selftests/bpf/progs/xdp_redirect_map.c |  49 +++++++
 2 files changed, 199 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +150 -0
index 64f72068a0100e05bfd327f8291cfc454bc6f518..5d32015a3c3b4df7f55115b18aac337aaa8187d9 100644
@@ -24,6 +24,23 @@
  *      | |                | |                | |
  *      | ------------------ ------------------ |
  *      -----------------------------------------
+ *
+ * - [test_xdp_veth_broadcast_redirect]: broadcast from veth11
+ *     - IPv4 ping : BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS
+ *          -> echo request received by all except veth11
+ *     - IPv4 ping : BPF_F_BROADCAST
+ *          -> echo request received by all veth
+ *
+ *    veth11             veth22              veth33
+ *  (XDP_PASS)         (XDP_PASS)          (XDP_PASS)
+ *       |                  |                  |
+ *       |                  |                  |
+ *     veth1		  veth2              veth3
+ * (XDP_REDIRECT)     (XDP_REDIRECT)     (XDP_REDIRECT)
+ *      |                   ^                  ^
+ *      |                   |                  |
+ *      ----------------------------------------
+ *
  */
 
 #define _GNU_SOURCE
@@ -32,6 +49,7 @@
 #include "network_helpers.h"
 #include "xdp_dummy.skel.h"
 #include "xdp_redirect_map.skel.h"
+#include "xdp_redirect_multi_kern.skel.h"
 #include "xdp_tx.skel.h"
 #include <uapi/linux/if_link.h>
 
@@ -40,6 +58,7 @@
 #define IP_MAX_LEN		16
 #define IP_SRC				"10.1.1.11"
 #define IP_DST				"10.1.1.33"
+#define IP_NEIGH			"10.1.1.253"
 #define NO_IP				"NO_IP"
 #define PROG_NAME_MAX_LEN	128
 #define NS_NAME_MAX_LEN		32
@@ -274,6 +293,114 @@ static void xdp_veth_redirect(u32 flags)
 	cleanup_network(net_config);
 }
 
+#define BROADCAST_REDIRECT_SKEL_NB	2
+static void xdp_veth_broadcast_redirect(u32 attach_flags, u64 redirect_flags)
+{
+	struct prog_configuration prog_cfg[VETH_PAIRS_COUNT] = {
+		{
+			.local_name = "xdp_redirect_map_multi_prog",
+			.remote_name = "xdp_count_0",
+			.local_flags = attach_flags,
+			.remote_flags = attach_flags,
+		},
+		{
+			.local_name = "xdp_redirect_map_multi_prog",
+			.remote_name = "xdp_count_1",
+			.local_flags = attach_flags,
+			.remote_flags = attach_flags,
+		},
+		{
+			.local_name = "xdp_redirect_map_multi_prog",
+			.remote_name = "xdp_count_2",
+			.local_flags = attach_flags,
+			.remote_flags = attach_flags,
+		}
+	};
+	struct bpf_object *bpf_objs[BROADCAST_REDIRECT_SKEL_NB];
+	struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;
+	struct veth_configuration net_config[VETH_PAIRS_COUNT];
+	struct xdp_redirect_map *xdp_redirect_map;
+	struct bpf_devmap_val devmap_val = {};
+	u16 protocol = ETH_P_IP;
+	int group_map;
+	int flags_map;
+	int cnt_map;
+	u64 cnt = 0;
+	int i, err;
+
+	xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load();
+	if (!ASSERT_OK_PTR(xdp_redirect_multi_kern, "xdp_redirect_multi_kern__open_and_load"))
+		return;
+
+	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_redirect_multi_kern;
+
+	if (!ASSERT_OK(create_network(net_config), "create network"))
+		goto destroy_xdp_redirect_map;
+
+	group_map = bpf_map__fd(xdp_redirect_multi_kern->maps.map_all);
+	if (!ASSERT_OK_FD(group_map, "open map_all"))
+		goto destroy_xdp_redirect_map;
+
+	flags_map = bpf_map__fd(xdp_redirect_multi_kern->maps.redirect_flags);
+	if (!ASSERT_OK_FD(group_map, "open map_all"))
+		goto destroy_xdp_redirect_map;
+
+	err = bpf_map_update_elem(flags_map, &protocol, &redirect_flags, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "init IP count"))
+		goto destroy_xdp_redirect_map;
+
+	cnt_map = bpf_map__fd(xdp_redirect_map->maps.rxcnt);
+	if (!ASSERT_OK_FD(cnt_map, "open rxcnt map"))
+		goto destroy_xdp_redirect_map;
+
+	bpf_objs[0] = xdp_redirect_multi_kern->obj;
+	bpf_objs[1] = xdp_redirect_map->obj;
+	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
+		int ifindex = if_nametoindex(net_config[i].local_veth);
+
+		if (attach_programs_to_veth_pair(bpf_objs, BROADCAST_REDIRECT_SKEL_NB,
+						 net_config, prog_cfg, i))
+			goto destroy_xdp_redirect_map;
+
+		SYS(destroy_xdp_redirect_map,
+		    "ip -n %s neigh add %s lladdr 00:00:00:00:00:01 dev %s",
+		    net_config[i].namespace, IP_NEIGH, net_config[i].remote_veth);
+
+		devmap_val.ifindex = ifindex;
+		err = bpf_map_update_elem(group_map, &ifindex, &devmap_val, 0);
+		if (!ASSERT_OK(err, "bpf_map_update_elem"))
+			goto destroy_xdp_redirect_map;
+
+	}
+
+	SYS_NOFAIL("ip netns exec %s ping %s -i 0.1 -c 4 -W1 > /dev/null ",
+		    net_config[0].namespace, IP_NEIGH);
+
+	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
+		err =  bpf_map_lookup_elem(cnt_map, &i, &cnt);
+		if (!ASSERT_OK(err, "get IP cnt"))
+			goto destroy_xdp_redirect_map;
+
+		if (redirect_flags & BPF_F_EXCLUDE_INGRESS)
+			/* veth11 shouldn't receive the ICMP requests;
+			 * others should
+			 */
+			ASSERT_EQ(cnt, i ? 4 : 0, "compare IP cnt");
+		else
+			/* All remote veth should receive the ICMP requests */
+			ASSERT_EQ(cnt, 4, "compare IP cnt");
+	}
+
+destroy_xdp_redirect_map:
+	xdp_redirect_map__destroy(xdp_redirect_map);
+destroy_xdp_redirect_multi_kern:
+	xdp_redirect_multi_kern__destroy(xdp_redirect_multi_kern);
+
+	cleanup_network(net_config);
+}
+
 void test_xdp_veth_redirect(void)
 {
 	if (test__start_subtest("0"))
@@ -285,3 +412,26 @@ void test_xdp_veth_redirect(void)
 	if (test__start_subtest("SKB_MODE"))
 		xdp_veth_redirect(XDP_FLAGS_SKB_MODE);
 }
+
+void test_xdp_veth_broadcast_redirect(void)
+{
+	if (test__start_subtest("0/BROADCAST"))
+		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST);
+
+	if (test__start_subtest("0/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+
+	if (test__start_subtest("DRV_MODE/BROADCAST"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE, BPF_F_BROADCAST);
+
+	if (test__start_subtest("DRV_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE,
+					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+
+	if (test__start_subtest("SKB_MODE/BROADCAST"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE, BPF_F_BROADCAST);
+
+	if (test__start_subtest("SKB_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE,
+					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+}
tools/testing/selftests/bpf/progs/xdp_redirect_map.c +49 -0

60 lines not on this page · raw message

Signature · 1 line
2.47.1
#16 lore raw

Re: [PATCH bpf-next v3 12/14] selftests/bpf: test_xdp_veth: Add XDP broadcast redirection tests

Quoted 2 lines · Bastien Curutchet (eBPF Foundation)

Set the tests to run serially to avoid conflicts with test_xdp_veth_redirect

I think this has been fixed in v3?

Others lgtm also. Thanks for working on this.

Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +22 -0
+void test_xdp_veth_broadcast_redirect(void)
+{
+	if (test__start_subtest("0/BROADCAST"))
+		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST);
+
+	if (test__start_subtest("0/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+
+	if (test__start_subtest("DRV_MODE/BROADCAST"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE, BPF_F_BROADCAST);
+
+	if (test__start_subtest("DRV_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE,
+					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+
+	if (test__start_subtest("SKB_MODE/BROADCAST"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE, BPF_F_BROADCAST);
+
+	if (test__start_subtest("SKB_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
+		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE,
+					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+}
#17 lore raw

Re: [PATCH bpf-next v3 12/14] selftests/bpf: test_xdp_veth: Add XDP broadcast redirection tests

Hi Martin,

Quoted 6 lines · Martin KaFai Lau
Quoted 2 lines · Bastien Curutchet (eBPF Foundation)

Set the tests to run serially to avoid conflicts with test_xdp_veth_redirect

I think this has been fixed in v3?

Indeed, I forgot to update the commit log ...

Quoted 2 lines

Others lgtm also. Thanks for working on this.

Great, I'll send a V4 soon then, thank you.

Best regards,
Bastien

#18 lore raw

PATCH v3 13/14 selftests/bpf: test_xdp_veth: Add XDP program on egress test

In reply to Bastien Curutchet (eBPF Foundation)

XDP programs loaded on egress is tested by test_xdp_redirect_multi.sh but not by the test_progs framework.

Add a test case in test_xdp_veth.c to test the XDP program on egress. Use the same BPF program than test_xdp_redirect_multi.sh that replaces the source MAC address by one provided through a BPF map. Use a BPF program that stores the source MAC of received packets in a map to check the test results.

---
 .../selftests/bpf/prog_tests/test_xdp_veth.c       | 125 +++++++++++++++++++++
 .../testing/selftests/bpf/progs/xdp_redirect_map.c |  40 +++++++
 2 files changed, 165 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +125 -0
index 5d32015a3c3b4df7f55115b18aac337aaa8187d9..b3e2562b55ca5ab287ec0105fdfc8fcbd723699a 100644
@@ -30,6 +30,8 @@
  *          -> echo request received by all except veth11
  *     - IPv4 ping : BPF_F_BROADCAST
  *          -> echo request received by all veth
+ * - [test_xdp_veth_egress]:
+ *     - all src mac should be the magic mac
  *
  *    veth11             veth22              veth33
  *  (XDP_PASS)         (XDP_PASS)          (XDP_PASS)
@@ -401,6 +403,117 @@ static void xdp_veth_broadcast_redirect(u32 attach_flags, u64 redirect_flags)
 	cleanup_network(net_config);
 }
 
+#define VETH_EGRESS_SKEL_NB	3
+static void xdp_veth_egress(u32 flags)
+{
+	struct prog_configuration prog_cfg[VETH_PAIRS_COUNT] = {
+		{
+			.local_name = "xdp_redirect_map_all_prog",
+			.remote_name = "xdp_dummy_prog",
+			.local_flags = flags,
+			.remote_flags = flags,
+		},
+		{
+			.local_name = "xdp_redirect_map_all_prog",
+			.remote_name = "store_mac_1",
+			.local_flags = flags,
+			.remote_flags = flags,
+		},
+		{
+			.local_name = "xdp_redirect_map_all_prog",
+			.remote_name = "store_mac_2",
+			.local_flags = flags,
+			.remote_flags = flags,
+		}
+	};
+	const char magic_mac[6] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
+	struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;
+	struct veth_configuration net_config[VETH_PAIRS_COUNT];
+	struct bpf_object *bpf_objs[VETH_EGRESS_SKEL_NB];
+	struct xdp_redirect_map *xdp_redirect_map;
+	struct bpf_devmap_val devmap_val = {};
+	int mac_map, egress_map, res_map;
+	struct xdp_dummy *xdp_dummy;
+	int err;
+	int i;
+
+	xdp_dummy = xdp_dummy__open_and_load();
+	if (!ASSERT_OK_PTR(xdp_dummy, "xdp_dummy__open_and_load"))
+		return;
+
+	xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load();
+	if (!ASSERT_OK_PTR(xdp_redirect_multi_kern, "xdp_redirect_multi_kern__open_and_load"))
+		goto destroy_xdp_dummy;
+
+	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_redirect_multi_kern;
+
+	if (!ASSERT_OK(create_network(net_config), "create network"))
+		goto destroy_xdp_redirect_map;
+
+	mac_map = bpf_map__fd(xdp_redirect_multi_kern->maps.mac_map);
+	if (!ASSERT_OK_FD(mac_map, "open mac_map"))
+		goto destroy_xdp_redirect_map;
+
+	egress_map = bpf_map__fd(xdp_redirect_multi_kern->maps.map_egress);
+	if (!ASSERT_OK_FD(egress_map, "open map_egress"))
+		goto destroy_xdp_redirect_map;
+
+	devmap_val.bpf_prog.fd = bpf_program__fd(xdp_redirect_multi_kern->progs.xdp_devmap_prog);
+
+	bpf_objs[0] = xdp_dummy->obj;
+	bpf_objs[1] = xdp_redirect_multi_kern->obj;
+	bpf_objs[2] = xdp_redirect_map->obj;
+	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
+		int ifindex = if_nametoindex(net_config[i].local_veth);
+
+		SYS(destroy_xdp_redirect_map,
+		    "ip -n %s neigh add %s lladdr 00:00:00:00:00:01 dev %s",
+		    net_config[i].namespace, IP_NEIGH, net_config[i].remote_veth);
+
+		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB,
+						 net_config, prog_cfg, i))
+			goto destroy_xdp_redirect_map;
+
+		err = bpf_map_update_elem(mac_map, &ifindex, magic_mac, 0);
+		if (!ASSERT_OK(err, "bpf_map_update_elem"))
+			goto destroy_xdp_redirect_map;
+
+		devmap_val.ifindex = ifindex;
+		err = bpf_map_update_elem(egress_map, &ifindex, &devmap_val, 0);
+		if (!ASSERT_OK(err, "bpf_map_update_elem"))
+			goto destroy_xdp_redirect_map;
+	}
+
+	SYS_NOFAIL("ip netns exec %s ping %s -i 0.1 -c 4 -W1 > /dev/null ",
+		    net_config[0].namespace, IP_NEIGH);
+
+	res_map = bpf_map__fd(xdp_redirect_map->maps.rx_mac);
+	if (!ASSERT_OK_FD(res_map, "open rx_map"))
+		goto destroy_xdp_redirect_map;
+
+	for (i = 0; i < 2; i++) {
+		u32 key = i;
+		u64 res;
+
+		err = bpf_map_lookup_elem(res_map, &key, &res);
+		if (!ASSERT_OK(err, "get MAC res"))
+			goto destroy_xdp_redirect_map;
+
+		ASSERT_STRNEQ((const char *)&res, magic_mac, ETH_ALEN, "compare mac");
+	}
+
+destroy_xdp_redirect_map:
+	xdp_redirect_map__destroy(xdp_redirect_map);
+destroy_xdp_redirect_multi_kern:
+	xdp_redirect_multi_kern__destroy(xdp_redirect_multi_kern);
+destroy_xdp_dummy:
+	xdp_dummy__destroy(xdp_dummy);
+
+	cleanup_network(net_config);
+}
+
 void test_xdp_veth_redirect(void)
 {
 	if (test__start_subtest("0"))
@@ -435,3 +548,15 @@ void test_xdp_veth_broadcast_redirect(void)
 		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE,
 					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
 }
+
+void test_xdp_veth_egress(void)
+{
+	if (test__start_subtest("0/egress"))
+		xdp_veth_egress(0);
+
+	if (test__start_subtest("DRV_MODE/egress"))
+		xdp_veth_egress(XDP_FLAGS_DRV_MODE);
+
+	if (test__start_subtest("SKB_MODE/egress"))
+		xdp_veth_egress(XDP_FLAGS_SKB_MODE);
+}
tools/testing/selftests/bpf/progs/xdp_redirect_map.c +40 -0
index 14385df71d7fc40c3b0ee5c6ea0760d0e7336d71..3b8b4e0cdd071b0ce885a1f42d55da5257cf38c2 100644
@@ -77,4 +77,44 @@ int xdp_count_2(struct xdp_md *xdp)
 	return xdp_count(xdp, 2);
 }
 
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 2);
+	__type(key, __u32);
+	__type(value, __be64);
+} rx_mac SEC(".maps");
+
+static int store_mac(struct xdp_md *xdp, __u32 id)
+{
+	void *data_end = (void *)(long)xdp->data_end;
+	void *data = (void *)(long)xdp->data;
+	struct ethhdr *eth = data;
+	__u32 key = id;
+	__be64 mac = 0;
+
+	if (data + sizeof(*eth) > data_end)
+		return XDP_DROP;
+
+	/* Only store IPv4 MAC to avoid being polluted by IPv6 packets */
+	if (eth->h_proto == bpf_htons(ETH_P_IP)) {
+		__builtin_memcpy(&mac, eth->h_source, ETH_ALEN);
+		bpf_map_update_elem(&rx_mac, &key, &mac, 0);
+		bpf_printk("%s - %x", __func__, mac);
+	}
+
+	return XDP_PASS;
+}
+
+SEC("xdp")
+int store_mac_1(struct xdp_md *xdp)
+{
+	return store_mac(xdp, 0);
+}
+
+SEC("xdp")
+int store_mac_2(struct xdp_md *xdp)
+{
+	return store_mac(xdp, 1);
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.1
#19 lore raw

PATCH v3 14/14 selftests/bpf: Remove test_xdp_redirect_multi.sh

In reply to Bastien Curutchet (eBPF Foundation)

The tests done by test_xdp_redirect_multi.sh are now fully covered by the CI through test_xdp_veth.c.

Remove test_xdp_redirect_multi.sh
Remove xdp_redirect_multi.c that was used by the script to load and attach the BPF programs.
Remove their entries in the Makefile

---
 tools/testing/selftests/bpf/Makefile               |   2 -
 .../selftests/bpf/test_xdp_redirect_multi.sh       | 214 -------------------
 tools/testing/selftests/bpf/xdp_redirect_multi.c   | 226 ---------------------
 3 files changed, 442 deletions(-)
tools/testing/selftests/bpf/Makefile +0 -2
index 87551628e1129e8760d7f6499b00317ac3683ef9..4f641a8f56c2b3f07403296f401469f67fb15c67 100644
@@ -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_multi.sh \
 	test_tunnel.sh \
 	test_lwt_seg6local.sh \
 	test_lirc_mode2.sh \
@@ -135,7 +134,6 @@ TEST_GEN_PROGS_EXTENDED = \
 	veristat \
 	xdp_features \
 	xdp_hw_metadata \
-	xdp_redirect_multi \
 	xdp_synproxy \
 	xdping \
 	xskxceiver
tools/testing/selftests/bpf/test_xdp_redirect_multi.sh deleted +0 -214

214 lines not on this page · raw message

tools/testing/selftests/bpf/xdp_redirect_multi.c deleted +0 -226

226 lines not on this page · raw message

Signature · 1 line
2.47.1