diff mbox series

[bpf-next,03/15] selftests/bpf: Use start_server_str in lwt_redirect

Message ID 44231acb3ed09db3bca33a4b5f959f0ec476efe3.1721771340.git.tanggeliang@kylinos.cn
State New
Headers show
Series [bpf-next,01/15] selftests/bpf: Use start_server_str in skc_to_unix_sock | expand

Commit Message

Geliang Tang July 23, 2024, 10:22 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

AF_PACKET can be passed to start_server_str() now. Use it to simplify
the code in lwt_redirect.

In this test, use sprintf() to save the "ifindex" value into addr_str
string, then pass it to the helper.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../selftests/bpf/prog_tests/lwt_redirect.c   | 21 +++++--------------
 1 file changed, 5 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
index 835a1d756c16..f4c54bb7e416 100644
--- a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
+++ b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
@@ -94,30 +94,19 @@  static int new_packet_sock(const char *ifname)
 {
 	int err = 0;
 	int ignore_outgoing = 1;
+	char addr_str[32];
 	int ifindex = -1;
 	int s = -1;
 
-	s = socket(AF_PACKET, SOCK_RAW, 0);
-	if (!ASSERT_GE(s, 0, "socket(AF_PACKET)"))
-		return -1;
-
 	ifindex = if_nametoindex(ifname);
-	if (!ASSERT_GE(ifindex, 0, "if_nametoindex")) {
-		close(s);
+	if (!ASSERT_GE(ifindex, 0, "if_nametoindex"))
 		return -1;
-	}
 
-	struct sockaddr_ll addr = {
-		.sll_family = AF_PACKET,
-		.sll_protocol = htons(ETH_P_IP),
-		.sll_ifindex = ifindex,
-	};
+	sprintf(addr_str, "%d", ifindex);
 
-	err = bind(s, (struct sockaddr *)&addr, sizeof(addr));
-	if (!ASSERT_OK(err, "bind(AF_PACKET)")) {
-		close(s);
+	s = start_server_str(AF_PACKET, SOCK_RAW, addr_str, 0, NULL);
+	if (!ASSERT_OK_FD(s, "start_server_str"))
 		return -1;
-	}
 
 	/* Use packet socket to capture only the ingress, so we can distinguish
 	 * the case where a regression that actually redirects the packet to