Skip to contentSkip to conversation

bpf: Add bpf_sk_storage support to bpf_tcp_ca

PATCH Superseded Martin KaFai Lau 2 participants 6 messages Mar 19, 2020 – Mar 20, 2020

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bpf: Add bpf_sk_storage support to bpf_tcp_ca

This set adds bpf_sk_storage support to bpf_tcp_ca. That will allow bpf-tcp-cc to share sk's private data with other bpf_progs and also allow bpf-tcp-cc to use extra private storage if the existing icsk_ca_priv is not enough.

Martin KaFai Lau (2):
bpf: Add bpf_sk_storage support to bpf_tcp_ca
bpf: Add tests for bpf_sk_storage to bpf_tcp_ca

 net/ipv4/bpf_tcp_ca.c                         | 33 +++++++++++++++++++
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 28 ++++++++++++++--
 tools/testing/selftests/bpf/progs/bpf_dctcp.c | 16 +++++++++
 3 files changed, 74 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 bpf: Add bpf_sk_storage support to bpf_tcp_ca

This patch adds bpf_sk_storage_get() and bpf_sk_storage_delete() helper to the bpf_tcp_ca's struct_ops. That would allow bpf-tcp-cc to:
1) share sk private data with other bpf progs.
2) use bpf_sk_storage as a private storage for a bpf-tcp-cc if the existing icsk_ca_priv is not big enough.

---
 net/ipv4/bpf_tcp_ca.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
net/ipv4/bpf_tcp_ca.c +33 -0
index 574972bc7299..0fd8bfde2448 100644
@@ -7,6 +7,7 @@
 #include <linux/btf.h>
 #include <linux/filter.h>
 #include <net/tcp.h>
+#include <net/bpf_sk_storage.h>
 
 static u32 optional_ops[] = {
 	offsetof(struct tcp_congestion_ops, init),
@@ -27,6 +28,27 @@ static u32 unsupported_ops[] = {
 static const struct btf_type *tcp_sock_type;
 static u32 tcp_sock_id, sock_id;
 
+static int btf_sk_storage_get_ids[5];
+static struct bpf_func_proto btf_sk_storage_get_proto __read_mostly;
+
+static int btf_sk_storage_delete_ids[5];
+static struct bpf_func_proto btf_sk_storage_delete_proto __read_mostly;
+
+static void convert_sk_func_proto(struct bpf_func_proto *to, int *to_btf_ids,
+				  const struct bpf_func_proto *from)
+{
+	int i;
+
+	*to = *from;
+	to->btf_id = to_btf_ids;
+	for (i = 0; i < ARRAY_SIZE(to->arg_type); i++) {
+		if (to->arg_type[i] == ARG_PTR_TO_SOCKET) {
+			to->arg_type[i] = ARG_PTR_TO_BTF_ID;
+			to->btf_id[i] = tcp_sock_id;
+		}
+	}
+}
+
 static int bpf_tcp_ca_init(struct btf *btf)
 {
 	s32 type_id;
@@ -42,6 +64,13 @@ static int bpf_tcp_ca_init(struct btf *btf)
 	tcp_sock_id = type_id;
 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 
+	convert_sk_func_proto(&btf_sk_storage_get_proto,
+			      btf_sk_storage_get_ids,
+			      &bpf_sk_storage_get_proto);
+	convert_sk_func_proto(&btf_sk_storage_delete_proto,
+			      btf_sk_storage_delete_ids,
+			      &bpf_sk_storage_delete_proto);
+
 	return 0;
 }
 
@@ -167,6 +196,10 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
 	switch (func_id) {
 	case BPF_FUNC_tcp_send_ack:
 		return &bpf_tcp_send_ack_proto;
+	case BPF_FUNC_sk_storage_get:
+		return &btf_sk_storage_get_proto;
+	case BPF_FUNC_sk_storage_delete:
+		return &btf_sk_storage_delete_proto;
 	default:
 		return bpf_base_func_proto(func_id);
 	}
Signature · 1 line
2.17.1
Acked-by on v1 1/2
#2 lore raw
Quoted 8 lines · Martin KaFai Lau

This patch adds bpf_sk_storage_get() and bpf_sk_storage_delete() helper to the bpf_tcp_ca's struct_ops. That would allow bpf-tcp-cc to:
1) share sk private data with other bpf progs.
2) use bpf_sk_storage as a private storage for a bpf-tcp-cc if the existing icsk_ca_priv is not big enough.

#3 lore raw

PATCH 2/2 bpf: Add tests for bpf_sk_storage to bpf_tcp_ca

In reply to Martin KaFai Lau

This patch adds test to exercise the bpf_sk_storage_get() and bpf_sk_storage_delete() helper from the bpf_dctcp.c.

---
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 28 +++++++++++++++++--
 tools/testing/selftests/bpf/progs/bpf_dctcp.c | 16 +++++++++++
 2 files changed, 41 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +25 -3
index 8482bbc67eec..9aaecce0bc3c 100644
@@ -11,6 +11,7 @@
 static const unsigned int total_bytes = 10 * 1024 * 1024;
 static const struct timeval timeo_sec = { .tv_sec = 10 };
 static const size_t timeo_optlen = sizeof(timeo_sec);
+static int expected_stg = 0xeB9F;
 static int stop, duration;
 
 static int settimeo(int fd)
@@ -88,7 +89,7 @@ static void *server(void *arg)
 	return NULL;
 }
 
-static void do_test(const char *tcp_ca)
+static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
 {
 	struct sockaddr_in6 sa6 = {};
 	ssize_t nr_recv = 0, bytes = 0;
@@ -110,6 +111,14 @@ static void do_test(const char *tcp_ca)
 		return;
 	}
 
+	if (sk_stg_map) {
+		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &expected_stg, BPF_NOEXIST);
+		if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
+			  "err:%d errno:%d\n", err, errno))
+			goto done;
+	}
+
 	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
 	    settimeo(lfd) || settimeo(fd))
 		goto done;
@@ -149,6 +158,16 @@ static void do_test(const char *tcp_ca)
 	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
 	      bytes, total_bytes, nr_recv, errno);
 
+	if (sk_stg_map) {
+		int tmp_stg;
+
+		err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &tmp_stg);
+		CHECK(!err || errno != ENOENT,
+		      "bpf_map_lookup_elem(sk_stg_map)",
+		      "err:%d errno:%d\n", err, errno);
+	}
+
 wait_thread:
 	WRITE_ONCE(stop, 1);
 	pthread_join(srv_thread, &thread_ret);
@@ -175,7 +194,7 @@ static void test_cubic(void)
 		return;
 	}
 
-	do_test("bpf_cubic");
+	do_test("bpf_cubic", NULL);
 
 	bpf_link__destroy(link);
 	bpf_cubic__destroy(cubic_skel);
@@ -197,7 +216,10 @@ static void test_dctcp(void)
 		return;
 	}
 
-	do_test("bpf_dctcp");
+	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
+	CHECK(dctcp_skel->bss->stg_result != expected_stg,
+	      "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
+	      dctcp_skel->bss->stg_result, expected_stg);
 
 	bpf_link__destroy(link);
 	bpf_dctcp__destroy(dctcp_skel);
tools/testing/selftests/bpf/progs/bpf_dctcp.c +16 -0
index 127ea762a062..5c1fc584f3ae 100644
@@ -6,6 +6,7 @@
  * the kernel BPF logic.
  */
 
+#include <stddef.h>
 #include <linux/bpf.h>
 #include <linux/types.h>
 #include <bpf/bpf_helpers.h>
@@ -14,6 +15,15 @@
 
 char _license[] SEC("license") = "GPL";
 
+static volatile int stg_result;
+
+struct {
+	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__type(key, int);
+	__type(value, int);
+} sk_stg_map SEC(".maps");
+
 #define DCTCP_MAX_ALPHA	1024U
 
 struct dctcp {
@@ -43,12 +53,18 @@ void BPF_PROG(dctcp_init, struct sock *sk)
 {
 	const struct tcp_sock *tp = tcp_sk(sk);
 	struct dctcp *ca = inet_csk_ca(sk);
+	int *stg;
 
 	ca->prior_rcv_nxt = tp->rcv_nxt;
 	ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
 	ca->loss_cwnd = 0;
 	ca->ce_state = 0;
 
+	stg = bpf_sk_storage_get(&sk_stg_map, (void *)tp, NULL, 0);
+	if (stg) {
+		stg_result = *stg;
+		bpf_sk_storage_delete(&sk_stg_map, (void *)tp);
+	}
 	dctcp_reset(tp, ca);
 }
 
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH bpf-next 2/2] bpf: Add tests for bpf_sk_storage to bpf_tcp_ca

Quoted 46 lines · Martin KaFai Lau

This patch adds test to exercise the bpf_sk_storage_get() and bpf_sk_storage_delete() helper from the bpf_dctcp.c.

---
  .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 28 +++++++++++++++++--
  tools/testing/selftests/bpf/progs/bpf_dctcp.c | 16 +++++++++++
  2 files changed, 41 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +10 -1
index 8482bbc67eec..9aaecce0bc3c 100644
@@ -11,6 +11,7 @@
  static const unsigned int total_bytes = 10 * 1024 * 1024;
  static const struct timeval timeo_sec = { .tv_sec = 10 };
  static const size_t timeo_optlen = sizeof(timeo_sec);
+static int expected_stg = 0xeB9F;
  static int stop, duration;
 
  static int settimeo(int fd)
@@ -88,7 +89,7 @@ static void *server(void *arg)
  	return NULL;
  }
 
-static void do_test(const char *tcp_ca)
+static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
  {
  	struct sockaddr_in6 sa6 = {};
  	ssize_t nr_recv = 0, bytes = 0;
@@ -110,6 +111,14 @@ static void do_test(const char *tcp_ca)
  		return;
  	}
 
+	if (sk_stg_map) {
+		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &expected_stg, BPF_NOEXIST);
+		if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
+			  "err:%d errno:%d\n", err, errno))
+			goto done;
+	}
+
  	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
  	    settimeo(lfd) || settimeo(fd))
  		goto done;

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c static void do_test(const char *tcp_ca) · line 159

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +0 -0
@@ -149,6 +158,16 @@ static void do_test(const char *tcp_ca)
  	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
  	      bytes, total_bytes, nr_recv, errno);

Should the control go to "wait_thread" here if failure?

Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +15 -2
+	if (sk_stg_map) {
+		int tmp_stg;
+
+		err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &tmp_stg);
+		CHECK(!err || errno != ENOENT,
+		      "bpf_map_lookup_elem(sk_stg_map)",
+		      "err:%d errno:%d\n", err, errno);
+	}
+
  wait_thread:
  	WRITE_ONCE(stop, 1);
  	pthread_join(srv_thread, &thread_ret);
@@ -175,7 +194,7 @@ static void test_cubic(void)
  		return;
  	}
 
-	do_test("bpf_cubic");
+	do_test("bpf_cubic", NULL);
 
  	bpf_link__destroy(link);
  	bpf_cubic__destroy(cubic_skel);
@@ -197,7 +216,10 @@ static void test_dctcp(void)
  		return;
  	}
 
-	do_test("bpf_dctcp");
+	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
+	CHECK(dctcp_skel->bss->stg_result != expected_stg,
+	      "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
+	      dctcp_skel->bss->stg_result, expected_stg);
 
  	bpf_link__destroy(link);
  	bpf_dctcp__destroy(dctcp_skel);
tools/testing/selftests/bpf/progs/bpf_dctcp.c +1 -0
index 127ea762a062..5c1fc584f3ae 100644
@@ -6,6 +6,7 @@
   * the kernel BPF logic.
   */
 
+#include <stddef.h>
  #include <linux/bpf.h>
  #include <linux/types.h>
  #include <bpf/bpf_helpers.h>

tools/testing/selftests/bpf/progs/bpf_dctcp.c line 18

Quoted 4 lines
tools/testing/selftests/bpf/progs/bpf_dctcp.c +1 -0
@@ -14,6 +15,15 @@
 
  char _license[] SEC("license") = "GPL";
 
+static volatile int stg_result;

"int stg_result = 0;" should work too.

Quoted 31 lines
tools/testing/selftests/bpf/progs/bpf_dctcp.c +14 -0
+
+struct {
+	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__type(key, int);
+	__type(value, int);
+} sk_stg_map SEC(".maps");
+
  #define DCTCP_MAX_ALPHA	1024U
 
  struct dctcp {
@@ -43,12 +53,18 @@ void BPF_PROG(dctcp_init, struct sock *sk)
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
  	struct dctcp *ca = inet_csk_ca(sk);
+	int *stg;
 
  	ca->prior_rcv_nxt = tp->rcv_nxt;
  	ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
  	ca->loss_cwnd = 0;
  	ca->ce_state = 0;
 
+	stg = bpf_sk_storage_get(&sk_stg_map, (void *)tp, NULL, 0);
+	if (stg) {
+		stg_result = *stg;
+		bpf_sk_storage_delete(&sk_stg_map, (void *)tp);
+	}
  	dctcp_reset(tp, ca);
  }
 
#5 lore raw

Re: [PATCH bpf-next 2/2] bpf: Add tests for bpf_sk_storage to bpf_tcp_ca

Quoted 42 lines · Yonghong Song
Quoted 39 lines · Martin KaFai Lau

This patch adds test to exercise the bpf_sk_storage_get() and bpf_sk_storage_delete() helper from the bpf_dctcp.c.

---
  .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 28 +++++++++++++++++--
  tools/testing/selftests/bpf/progs/bpf_dctcp.c | 16 +++++++++++
  2 files changed, 41 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +10 -1
index 8482bbc67eec..9aaecce0bc3c 100644
@@ -11,6 +11,7 @@
  static const unsigned int total_bytes = 10 * 1024 * 1024;
  static const struct timeval timeo_sec = { .tv_sec = 10 };
  static const size_t timeo_optlen = sizeof(timeo_sec);
+static int expected_stg = 0xeB9F;
  static int stop, duration;
  static int settimeo(int fd)
@@ -88,7 +89,7 @@ static void *server(void *arg)
  	return NULL;
  }
-static void do_test(const char *tcp_ca)
+static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
  {
  	struct sockaddr_in6 sa6 = {};
  	ssize_t nr_recv = 0, bytes = 0;
@@ -110,6 +111,14 @@ static void do_test(const char *tcp_ca)
  		return;
  	}
+	if (sk_stg_map) {
+		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &expected_stg, BPF_NOEXIST);
+		if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
+			  "err:%d errno:%d\n", err, errno))
+			goto done;
+	}
+
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c static void do_test(const char *tcp_ca) · line 159

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +0 -0
  	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
  	    settimeo(lfd) || settimeo(fd))
  		goto done;
@@ -149,6 +158,16 @@ static void do_test(const char *tcp_ca)
  	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
  	      bytes, total_bytes, nr_recv, errno);

Should the control go to "wait_thread" here if failure?

Thanks for the review!

I did think about that. I did not bail on this because the sk_stg_map check below does not depend on the about check. Hence, I didn't bail here. I moved the sk_stg_map test to the very bottom is for this reason also.

Since you asked, I think it makes sense to go back to my first approach which is to do the below test immediately after connect() and just bail there.

I will also take this chance to postpone the thread creation after connect().

Quoted 39 lines
Quoted 39 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +15 -2
+	if (sk_stg_map) {
+		int tmp_stg;
+
+		err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
+					  &tmp_stg);
+		CHECK(!err || errno != ENOENT,
+		      "bpf_map_lookup_elem(sk_stg_map)",
+		      "err:%d errno:%d\n", err, errno);
+	}
+
  wait_thread:
  	WRITE_ONCE(stop, 1);
  	pthread_join(srv_thread, &thread_ret);
@@ -175,7 +194,7 @@ static void test_cubic(void)
  		return;
  	}
-	do_test("bpf_cubic");
+	do_test("bpf_cubic", NULL);
  	bpf_link__destroy(link);
  	bpf_cubic__destroy(cubic_skel);
@@ -197,7 +216,10 @@ static void test_dctcp(void)
  		return;
  	}
-	do_test("bpf_dctcp");
+	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
+	CHECK(dctcp_skel->bss->stg_result != expected_stg,
+	      "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
+	      dctcp_skel->bss->stg_result, expected_stg);
  	bpf_link__destroy(link);
  	bpf_dctcp__destroy(dctcp_skel);
tools/testing/selftests/bpf/progs/bpf_dctcp.c +1 -0
index 127ea762a062..5c1fc584f3ae 100644
@@ -6,6 +6,7 @@
   * the kernel BPF logic.
   */
+#include <stddef.h>
Quoted 8 lines

tools/testing/selftests/bpf/progs/bpf_dctcp.c line 16

Quoted 6 lines
tools/testing/selftests/bpf/progs/bpf_dctcp.c +1 -0
  #include <linux/bpf.h>
  #include <linux/types.h>
  #include <bpf/bpf_helpers.h>
@@ -14,6 +15,15 @@
  char _license[] SEC("license") = "GPL";
+static volatile int stg_result;

"int stg_result = 0;" should work too.

will use.