diff mbox series

[bpf-next,v3,2/2] selftests/bpf: Setget_sockopt add a test for tcp over ipv4 via ipv6

Message ID 20240914103226.71109-3-zhoufeng.zf@bytedance.com
State New
Headers show
Series Fix bpf_get/setsockopt failed when TCP over IPv4 via INET6 API | expand

Commit Message

Feng Zhou Sept. 14, 2024, 10:32 a.m. UTC
From: Feng Zhou <zhoufeng.zf@bytedance.com>

This patch adds a test for TCP over IPv4 via INET6 API.

Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
---
 .../selftests/bpf/prog_tests/setget_sockopt.c | 33 +++++++++++++++++++
 .../selftests/bpf/progs/setget_sockopt.c      | 13 ++++++--
 2 files changed, 43 insertions(+), 3 deletions(-)

Comments

Martin KaFai Lau Oct. 1, 2024, 2:33 a.m. UTC | #1
On 9/14/24 3:32 AM, Feng zhou wrote:
> From: Feng Zhou <zhoufeng.zf@bytedance.com>
> 
> This patch adds a test for TCP over IPv4 via INET6 API.
> 
> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
> ---
>   .../selftests/bpf/prog_tests/setget_sockopt.c | 33 +++++++++++++++++++
>   .../selftests/bpf/progs/setget_sockopt.c      | 13 ++++++--
>   2 files changed, 43 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
> index 7d4a9b3d3722..3cad92128e60 100644
> --- a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
> @@ -15,8 +15,11 @@
>   
>   #define CG_NAME "/setget-sockopt-test"
>   
> +#define INT_PORT	8008
> +
>   static const char addr4_str[] = "127.0.0.1";
>   static const char addr6_str[] = "::1";
> +static const char addr6_any_str[] = "::";
>   static struct setget_sockopt *skel;
>   static int cg_fd;
>   
> @@ -67,6 +70,35 @@ static void test_tcp(int family)
>   	ASSERT_EQ(bss->nr_binddev, 2, "nr_bind");
>   }
>   
> +static void test_tcp_over_ipv4_via_ipv6(void)
> +{
> +	struct setget_sockopt__bss *bss = skel->bss;
> +	int sfd, cfd;
> +
> +	memset(bss, 0, sizeof(*bss));
> +	skel->bss->test_tcp_over_ipv4_via_ipv6 = 1;
> +
> +	sfd = start_server(AF_INET6, SOCK_STREAM,
> +			   addr6_any_str, INT_PORT, 0);
> +	if (!ASSERT_GE(sfd, 0, "start_server"))
> +		return;
> +
> +	cfd = connect_to_addr_str(AF_INET, SOCK_STREAM, addr4_str, INT_PORT, NULL);
> +	if (!ASSERT_GE(cfd, 0, "connect_to_addr_str")) {
> +		close(sfd);
> +		return;
> +	}
> +	close(sfd);
> +	close(cfd);
> +
> +	ASSERT_EQ(bss->nr_listen, 1, "nr_listen");
> +	ASSERT_EQ(bss->nr_connect, 1, "nr_connect");
> +	ASSERT_EQ(bss->nr_active, 1, "nr_active");
> +	ASSERT_EQ(bss->nr_passive, 1, "nr_passive");
> +	ASSERT_EQ(bss->nr_socket_post_create, 2, "nr_socket_post_create");
> +	ASSERT_EQ(bss->nr_binddev, 2, "nr_bind");
> +}
> +
>   static void test_udp(int family)
>   {
>   	struct setget_sockopt__bss *bss = skel->bss;
> @@ -191,6 +223,7 @@ void test_setget_sockopt(void)
>   	test_udp(AF_INET);
>   	test_ktls(AF_INET6);
>   	test_ktls(AF_INET);
> +	test_tcp_over_ipv4_via_ipv6();

This has a conflict with commit d53050934e66.

pw-bot: cr

>   
>   done:
>   	setget_sockopt__destroy(skel);
> diff --git a/tools/testing/selftests/bpf/progs/setget_sockopt.c b/tools/testing/selftests/bpf/progs/setget_sockopt.c
> index 60518aed1ffc..ff834d94dd23 100644
> --- a/tools/testing/selftests/bpf/progs/setget_sockopt.c
> +++ b/tools/testing/selftests/bpf/progs/setget_sockopt.c
> @@ -20,6 +20,7 @@ int nr_connect;
>   int nr_binddev;
>   int nr_socket_post_create;
>   int nr_fin_wait1;
> +int test_tcp_over_ipv4_via_ipv6;
>   
>   struct sockopt_test {
>   	int opt;
> @@ -262,9 +263,15 @@ static int bpf_test_sockopt(void *ctx, struct sock *sk)
>   		if (n != ARRAY_SIZE(sol_ip_tests))
>   			return -1;
>   	} else {
> -		n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
> -		if (n != ARRAY_SIZE(sol_ipv6_tests))
> -			return -1;
> +		if (test_tcp_over_ipv4_via_ipv6) {
> +			n = bpf_loop(ARRAY_SIZE(sol_ip_tests), bpf_test_ip_sockopt, &lc, 0);

Can this bpf_loop(..., bpf_test_ip_sockopt, ...) be always run? Then the above 
test_tcp_over_ipv4_via_ipv6() addition will not be needed.


> +			if (n != ARRAY_SIZE(sol_ip_tests))
> +				return -1;
> +		} else {
> +			n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
> +			if (n != ARRAY_SIZE(sol_ipv6_tests))
> +				return -1;
> +		}
>   	}
>   
>   	return 0;
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
index 7d4a9b3d3722..3cad92128e60 100644
--- a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
+++ b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c
@@ -15,8 +15,11 @@ 
 
 #define CG_NAME "/setget-sockopt-test"
 
+#define INT_PORT	8008
+
 static const char addr4_str[] = "127.0.0.1";
 static const char addr6_str[] = "::1";
+static const char addr6_any_str[] = "::";
 static struct setget_sockopt *skel;
 static int cg_fd;
 
@@ -67,6 +70,35 @@  static void test_tcp(int family)
 	ASSERT_EQ(bss->nr_binddev, 2, "nr_bind");
 }
 
+static void test_tcp_over_ipv4_via_ipv6(void)
+{
+	struct setget_sockopt__bss *bss = skel->bss;
+	int sfd, cfd;
+
+	memset(bss, 0, sizeof(*bss));
+	skel->bss->test_tcp_over_ipv4_via_ipv6 = 1;
+
+	sfd = start_server(AF_INET6, SOCK_STREAM,
+			   addr6_any_str, INT_PORT, 0);
+	if (!ASSERT_GE(sfd, 0, "start_server"))
+		return;
+
+	cfd = connect_to_addr_str(AF_INET, SOCK_STREAM, addr4_str, INT_PORT, NULL);
+	if (!ASSERT_GE(cfd, 0, "connect_to_addr_str")) {
+		close(sfd);
+		return;
+	}
+	close(sfd);
+	close(cfd);
+
+	ASSERT_EQ(bss->nr_listen, 1, "nr_listen");
+	ASSERT_EQ(bss->nr_connect, 1, "nr_connect");
+	ASSERT_EQ(bss->nr_active, 1, "nr_active");
+	ASSERT_EQ(bss->nr_passive, 1, "nr_passive");
+	ASSERT_EQ(bss->nr_socket_post_create, 2, "nr_socket_post_create");
+	ASSERT_EQ(bss->nr_binddev, 2, "nr_bind");
+}
+
 static void test_udp(int family)
 {
 	struct setget_sockopt__bss *bss = skel->bss;
@@ -191,6 +223,7 @@  void test_setget_sockopt(void)
 	test_udp(AF_INET);
 	test_ktls(AF_INET6);
 	test_ktls(AF_INET);
+	test_tcp_over_ipv4_via_ipv6();
 
 done:
 	setget_sockopt__destroy(skel);
diff --git a/tools/testing/selftests/bpf/progs/setget_sockopt.c b/tools/testing/selftests/bpf/progs/setget_sockopt.c
index 60518aed1ffc..ff834d94dd23 100644
--- a/tools/testing/selftests/bpf/progs/setget_sockopt.c
+++ b/tools/testing/selftests/bpf/progs/setget_sockopt.c
@@ -20,6 +20,7 @@  int nr_connect;
 int nr_binddev;
 int nr_socket_post_create;
 int nr_fin_wait1;
+int test_tcp_over_ipv4_via_ipv6;
 
 struct sockopt_test {
 	int opt;
@@ -262,9 +263,15 @@  static int bpf_test_sockopt(void *ctx, struct sock *sk)
 		if (n != ARRAY_SIZE(sol_ip_tests))
 			return -1;
 	} else {
-		n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
-		if (n != ARRAY_SIZE(sol_ipv6_tests))
-			return -1;
+		if (test_tcp_over_ipv4_via_ipv6) {
+			n = bpf_loop(ARRAY_SIZE(sol_ip_tests), bpf_test_ip_sockopt, &lc, 0);
+			if (n != ARRAY_SIZE(sol_ip_tests))
+				return -1;
+		} else {
+			n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
+			if (n != ARRAY_SIZE(sol_ipv6_tests))
+				return -1;
+		}
 	}
 
 	return 0;