Skip to contentSkip to conversation

bpf: add get_netns_cookie helper to tc programs

PATCH Superseded Mahe Tardy 3 participants 9 messages Oct 2, 2024 – Oct 8, 2024

Subsystems (5)
lore
Share & tools
lore

Mahe Tardy

author
#0 lore raw

PATCH 1/2 bpf: add get_netns_cookie helper to tc programs

This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie.

---
 net/core/filter.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
net/core/filter.c +13 -0
index cd3524cb326b..6e80991125ba 100644
@@ -5138,6 +5138,17 @@ static u64 __bpf_get_netns_cookie(struct sock *sk)
 	return net->net_cookie;
 }
 
+BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb)
+{
+	return __bpf_get_netns_cookie(skb->sk ? skb->sk : NULL);
+}
+
+static const struct bpf_func_proto bpf_get_netns_cookie_proto = {
+	.func           = bpf_get_netns_cookie,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX_OR_NULL,
+};
+
 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
 {
 	return __bpf_get_netns_cookie(ctx);
@@ -8209,6 +8220,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_skb_under_cgroup_proto;
 	case BPF_FUNC_get_socket_cookie:
 		return &bpf_get_socket_cookie_proto;
+	case BPF_FUNC_get_netns_cookie:
+		return &bpf_get_netns_cookie_proto;
 	case BPF_FUNC_get_socket_uid:
 		return &bpf_get_socket_uid_proto;
 	case BPF_FUNC_fib_lookup:

--
2.34.1

author
#1 lore raw

PATCH 2/2 selftests/bpf: add tcx netns cookie tests

Add netns cookie test that verifies the helper is now supported and work in the context of tc programs.

---
 tools/testing/selftests/bpf/prog_tests/netns_cookie.c | 7 +++++++
 tools/testing/selftests/bpf/progs/netns_cookie_prog.c | 9 +++++++++
 2 files changed, 16 insertions(+)
tools/testing/selftests/bpf/prog_tests/netns_cookie.c +7 -0
index 71d8f3ba7d6b..233fd66f59ee 100644
@@ -12,6 +12,7 @@ static int duration;
 
 void test_netns_cookie(void)
 {
+	LIBBPF_OPTS(bpf_prog_attach_opts, opta);
 	int server_fd = -1, client_fd = -1, cgroup_fd = -1;
 	int err, val, ret, map, verdict;
 	struct netns_cookie_prog *skel;
@@ -38,6 +39,11 @@ void test_netns_cookie(void)
 	if (!ASSERT_OK(err, "prog_attach"))
 		goto done;
 
+	verdict = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
+	err = bpf_prog_attach_opts(verdict, 1, BPF_TCX_INGRESS, &opta);
+	if (!ASSERT_EQ(err, 0, "prog_attach"))
+		goto done;
+
 	server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
 	if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
 		goto done;
@@ -68,6 +74,7 @@ void test_netns_cookie(void)
 		goto done;
 
 	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
+	ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
 
 done:
 	if (server_fd != -1)
tools/testing/selftests/bpf/progs/netns_cookie_prog.c +9 -0
index aeff3a4f9287..207f0e6c20b7 100644
@@ -27,6 +27,8 @@ struct {
 	__type(value, __u64);
 } sock_map SEC(".maps");
 
+int tcx_netns_cookie;
+
 SEC("sockops")
 int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
 {
@@ -81,4 +83,11 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
 	return 1;
 }
 
+SEC("tcx/ingress")
+int get_netns_cookie_tcx(struct __sk_buff *skb)
+{
+	tcx_netns_cookie = bpf_get_netns_cookie(skb);
+	return TCX_PASS;
+}
+
 char _license[] SEC("license") = "GPL";

--
2.34.1

maintainer
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add tcx netns cookie tests

Quoted 34 lines · Mahe Tardy

Add netns cookie test that verifies the helper is now supported and work in the context of tc programs.

---
  tools/testing/selftests/bpf/prog_tests/netns_cookie.c | 7 +++++++
  tools/testing/selftests/bpf/progs/netns_cookie_prog.c | 9 +++++++++
  2 files changed, 16 insertions(+)
tools/testing/selftests/bpf/prog_tests/netns_cookie.c +6 -0
index 71d8f3ba7d6b..233fd66f59ee 100644
@@ -12,6 +12,7 @@ static int duration;
 
  void test_netns_cookie(void)
  {
+	LIBBPF_OPTS(bpf_prog_attach_opts, opta);
  	int server_fd = -1, client_fd = -1, cgroup_fd = -1;
  	int err, val, ret, map, verdict;
  	struct netns_cookie_prog *skel;
@@ -38,6 +39,11 @@ void test_netns_cookie(void)
  	if (!ASSERT_OK(err, "prog_attach"))
  		goto done;
 
+	verdict = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
+	err = bpf_prog_attach_opts(verdict, 1, BPF_TCX_INGRESS, &opta);
+	if (!ASSERT_EQ(err, 0, "prog_attach"))
+		goto done;
+
  	server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
  	if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
  		goto done;

tools/testing/selftests/bpf/prog_tests/netns_cookie.c void test_netns_cookie(void) · line 80

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/netns_cookie.c +1 -0
@@ -68,6 +74,7 @@ void test_netns_cookie(void)
  		goto done;
 
  	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
+	ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
 
  done:
  	if (server_fd != -1)

Looks like CI fails, as this is missing a bpf_prog_detach_opts().

Quoted 28 lines
tools/testing/selftests/bpf/progs/netns_cookie_prog.c +9 -1
index aeff3a4f9287..207f0e6c20b7 100644
@@ -27,6 +27,8 @@ struct {
  	__type(value, __u64);
  } sock_map SEC(".maps");
 
+int tcx_netns_cookie;
+
  SEC("sockops")
  int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
  {
@@ -81,4 +83,11 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
  	return 1;
  }
 
+SEC("tcx/ingress")
+int get_netns_cookie_tcx(struct __sk_buff *skb)
+{
+	tcx_netns_cookie = bpf_get_netns_cookie(skb);
+	return TCX_PASS;
+}
+
  char _license[] SEC("license") = "GPL";
--

2.34.1

author
#3 lore raw

PATCH v2 1/2 bpf: add get_netns_cookie helper to tc programs

This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie.

---
 net/core/filter.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
net/core/filter.c +13 -0
index cd3524cb326b..6e80991125ba 100644
@@ -5138,6 +5138,17 @@ static u64 __bpf_get_netns_cookie(struct sock *sk)
 	return net->net_cookie;
 }
 
+BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb)
+{
+	return __bpf_get_netns_cookie(skb->sk ? skb->sk : NULL);
+}
+
+static const struct bpf_func_proto bpf_get_netns_cookie_proto = {
+	.func           = bpf_get_netns_cookie,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX_OR_NULL,
+};
+
 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
 {
 	return __bpf_get_netns_cookie(ctx);
@@ -8209,6 +8220,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_skb_under_cgroup_proto;
 	case BPF_FUNC_get_socket_cookie:
 		return &bpf_get_socket_cookie_proto;
+	case BPF_FUNC_get_netns_cookie:
+		return &bpf_get_netns_cookie_proto;
 	case BPF_FUNC_get_socket_uid:
 		return &bpf_get_socket_uid_proto;
 	case BPF_FUNC_fib_lookup:

--
2.34.1

author
#4 lore raw

PATCH v2 2/2 selftests/bpf: add tcx netns cookie tests

Add netns cookie test that verifies the helper is now supported and work in the context of tc programs.

---
 .../selftests/bpf/prog_tests/netns_cookie.c   | 28 ++++++++++++++-----
 .../selftests/bpf/progs/netns_cookie_prog.c   |  9 ++++++
 2 files changed, 30 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/netns_cookie.c +21 -7
index 71d8f3ba7d6b..a014082d1e09 100644
@@ -8,12 +8,16 @@
 #define SO_NETNS_COOKIE 71
 #endif
 
+#define loopback 1
+
 static int duration;
 
 void test_netns_cookie(void)
 {
+	LIBBPF_OPTS(bpf_prog_attach_opts, opta);
+	LIBBPF_OPTS(bpf_prog_detach_opts, optd);
 	int server_fd = -1, client_fd = -1, cgroup_fd = -1;
-	int err, val, ret, map, verdict;
+	int err, val, ret, map, verdict, tc_fd;
 	struct netns_cookie_prog *skel;
 	uint64_t cookie_expected_value;
 	socklen_t vallen = sizeof(cookie_expected_value);
@@ -38,36 +42,46 @@ void test_netns_cookie(void)
 	if (!ASSERT_OK(err, "prog_attach"))
 		goto done;
 
+	tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
+	err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta);
+	if (!ASSERT_OK(err, "prog_attach"))
+		goto done;
+
 	server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
 	if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
-		goto done;
+		goto cleanup_tc;
 
 	client_fd = connect_to_fd(server_fd, 0);
 	if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
-		goto done;
+		goto cleanup_tc;
 
 	ret = send(client_fd, send_msg, sizeof(send_msg), 0);
 	if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret))
-		goto done;
+		goto cleanup_tc;
 
 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies),
 				  &client_fd, &val);
 	if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)"))
-		goto done;
+		goto cleanup_tc;
 
 	err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
 			 &cookie_expected_value, &vallen);
 	if (!ASSERT_OK(err, "getsockopt"))
-		goto done;
+		goto cleanup_tc;
 
 	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
 
 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies),
 				  &client_fd, &val);
 	if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)"))
-		goto done;
+		goto cleanup_tc;
 
 	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
+	ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
+
+cleanup_tc:
+	err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd);
+	ASSERT_OK(err, "prog_detach");
 
 done:
 	if (server_fd != -1)
tools/testing/selftests/bpf/progs/netns_cookie_prog.c +9 -0
index aeff3a4f9287..207f0e6c20b7 100644
@@ -27,6 +27,8 @@ struct {
 	__type(value, __u64);
 } sock_map SEC(".maps");
 
+int tcx_netns_cookie;
+
 SEC("sockops")
 int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
 {
@@ -81,4 +83,11 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
 	return 1;
 }
 
+SEC("tcx/ingress")
+int get_netns_cookie_tcx(struct __sk_buff *skb)
+{
+	tcx_netns_cookie = bpf_get_netns_cookie(skb);
+	return TCX_PASS;
+}
+
 char _license[] SEC("license") = "GPL";

--
2.34.1

maintainer
#5 lore raw
In reply to Mahe Tardy
Quoted 16 lines · Mahe Tardy

This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie.

---
  net/core/filter.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
net/core/filter.c +0 -0
index cd3524cb326b..6e80991125ba 100644
@@ -5138,6 +5138,17 @@ static u64 __bpf_get_netns_cookie(struct sock *sk)
  	return net->net_cookie;
  }

net/core/filter.c static u64 __bpf_get_netns_cookie(struct sock *sk) · line 5149

Quoted 10 lines
net/core/filter.c +9 -0
 
+BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb)
+{
+	return __bpf_get_netns_cookie(skb->sk ? skb->sk : NULL);
+}
+
+static const struct bpf_func_proto bpf_get_netns_cookie_proto = {
+	.func           = bpf_get_netns_cookie,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX_OR_NULL,

This basically tells the verifier that NULL or context can be passed to the helper. As-is above, the skb->sk ? will trigger a NULL pointer dereference.

If you look into other implementations, the above should rather look like:

  return __bpf_get_netns_cookie(skb && skb->sk ? skb->sk : NULL);
Quoted 17 lines
net/core/filter.c +4 -1
+};
+
  BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
  {
  	return __bpf_get_netns_cookie(ctx);
@@ -8209,6 +8220,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  		return &bpf_skb_under_cgroup_proto;
  	case BPF_FUNC_get_socket_cookie:
  		return &bpf_get_socket_cookie_proto;
+	case BPF_FUNC_get_netns_cookie:
+		return &bpf_get_netns_cookie_proto;
  	case BPF_FUNC_get_socket_uid:
  		return &bpf_get_socket_uid_proto;
  	case BPF_FUNC_fib_lookup:
--

2.34.1

author
#6 lore raw

PATCH v3 1/2 bpf: add get_netns_cookie helper to tc programs

This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie.

---
 net/core/filter.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
net/core/filter.c +13 -0
index cd3524cb326b..944bbe12a039 100644
@@ -5138,6 +5138,17 @@ static u64 __bpf_get_netns_cookie(struct sock *sk)
 	return net->net_cookie;
 }
 
+BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb)
+{
+	return __bpf_get_netns_cookie(skb && skb->sk ? skb->sk : NULL);
+}
+
+static const struct bpf_func_proto bpf_get_netns_cookie_proto = {
+	.func           = bpf_get_netns_cookie,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX_OR_NULL,
+};
+
 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
 {
 	return __bpf_get_netns_cookie(ctx);
@@ -8209,6 +8220,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_skb_under_cgroup_proto;
 	case BPF_FUNC_get_socket_cookie:
 		return &bpf_get_socket_cookie_proto;
+	case BPF_FUNC_get_netns_cookie:
+		return &bpf_get_netns_cookie_proto;
 	case BPF_FUNC_get_socket_uid:
 		return &bpf_get_socket_uid_proto;
 	case BPF_FUNC_fib_lookup:

--
2.34.1

author
#7 lore raw

PATCH v3 2/2 selftests/bpf: add tcx netns cookie tests

Add netns cookie test that verifies the helper is now supported and work in the context of tc programs.

---
 .../selftests/bpf/prog_tests/netns_cookie.c   | 29 ++++++++++++++-----
 .../selftests/bpf/progs/netns_cookie_prog.c   | 10 +++++++
 2 files changed, 32 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/netns_cookie.c +22 -7
index 71d8f3ba7d6b..ac3c3c097c0e 100644
@@ -8,12 +8,16 @@
 #define SO_NETNS_COOKIE 71
 #endif
 
+#define loopback 1
+
 static int duration;
 
 void test_netns_cookie(void)
 {
+	LIBBPF_OPTS(bpf_prog_attach_opts, opta);
+	LIBBPF_OPTS(bpf_prog_detach_opts, optd);
 	int server_fd = -1, client_fd = -1, cgroup_fd = -1;
-	int err, val, ret, map, verdict;
+	int err, val, ret, map, verdict, tc_fd;
 	struct netns_cookie_prog *skel;
 	uint64_t cookie_expected_value;
 	socklen_t vallen = sizeof(cookie_expected_value);
@@ -38,36 +42,47 @@ void test_netns_cookie(void)
 	if (!ASSERT_OK(err, "prog_attach"))
 		goto done;
 
+	tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
+	err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta);
+	if (!ASSERT_OK(err, "prog_attach"))
+		goto done;
+
 	server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
 	if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
-		goto done;
+		goto cleanup_tc;
 
 	client_fd = connect_to_fd(server_fd, 0);
 	if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
-		goto done;
+		goto cleanup_tc;
 
 	ret = send(client_fd, send_msg, sizeof(send_msg), 0);
 	if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret))
-		goto done;
+		goto cleanup_tc;
 
 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies),
 				  &client_fd, &val);
 	if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)"))
-		goto done;
+		goto cleanup_tc;
 
 	err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
 			 &cookie_expected_value, &vallen);
 	if (!ASSERT_OK(err, "getsockopt"))
-		goto done;
+		goto cleanup_tc;
 
 	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
 
 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies),
 				  &client_fd, &val);
 	if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)"))
-		goto done;
+		goto cleanup_tc;
 
 	ASSERT_EQ(val, cookie_expected_value, "cookie_value");
+	ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value");
+	ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
+
+cleanup_tc:
+	err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd);
+	ASSERT_OK(err, "prog_detach");
 
 done:
 	if (server_fd != -1)
tools/testing/selftests/bpf/progs/netns_cookie_prog.c +10 -0
index aeff3a4f9287..c6edf8dbefeb 100644
@@ -27,6 +27,8 @@ struct {
 	__type(value, __u64);
 } sock_map SEC(".maps");
 
+int tcx_init_netns_cookie, tcx_netns_cookie;
+
 SEC("sockops")
 int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
 {
@@ -81,4 +83,12 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
 	return 1;
 }
 
+SEC("tcx/ingress")
+int get_netns_cookie_tcx(struct __sk_buff *skb)
+{
+	tcx_init_netns_cookie = bpf_get_netns_cookie(NULL);
+	tcx_netns_cookie = bpf_get_netns_cookie(skb);
+	return TCX_PASS;
+}
+
 char _license[] SEC("license") = "GPL";

--
2.34.1

#8 lore raw
In reply to Mahe Tardy
This series was applied to bpf/bpf-next.git (net) by Martin KaFai Lau <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (net)
by Martin KaFai Lau <[email protected]>:

Quoted 10 lines · On Mon, 7 Oct 2024 09:59:57 +0000 you wrote:

This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie.

---
 net/core/filter.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

[...]

Here is the summary with links:
- [bpf-next,v3,1/2] bpf: add get_netns_cookie helper to tc programs

    https://git.kernel.org/bpf/bpf-next/c/eb62f49de7ec
  - [bpf-next,v3,2/2] selftests/bpf: add tcx netns cookie tests
    https://git.kernel.org/bpf/bpf-next/c/693fe954d61d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html