Skip to contentSkip to conversation

add skc_to_mptcp_sock

PATCH In review Geliang Tang 3 participants 9 messages Mar 26, 2022 – Mar 29, 2022

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v14 cover add skc_to_mptcp_sock

This series addresses to Alexei comments in [1].

[1]
https://lore.kernel.org/netdev/[email protected]/

v14:
- merge the squash-to patch for patch 1.
- rename progs/mptcp.c to progs/mptcp_sock.c in patch 2.
- split the token verifying as a new patch from patch 3.
- update the commit logs.

v13:
patch 1:
As Mat mentioned in the "BPF packet scheduler" series, BPF packet
scheduler depends on CONFIG_BPF_SYSCALL. In fact, the series depends on CONFIG_BPF_SYSCALL too.
- change "CONFIG_BPF" to "CONFIG_BPF_SYSCALL" in Makefile
- change "ifdef CONFIG_MPTCP" in include/linux/bpf.h to:

    "#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)"

- drop "#ifdef CONFIG_MPTCP" in net/core/filter.c
patch 2:
Address to Matt's comments in v12:
- use bpf_object__find_program_by_name, bpf_object__find_map_by_name
- use -EIO
- drop close_client_fd label
- add more CHECK_FAIL

v12:
patch 1:
- merged the squash-to patch for v11.
- rebased to export/20220324T054815.
patch 2:
- fix the libbpf 0.8 build error.
patch 4:
- use fgets instead of fread to get null terminated string.

v11:
- merge "check more values from mptcp_sock" series into this series.
- patch 2:
- rebased.
- patch 3:
- rebased.
- add 'Copyright' entry as Matt suggested in v9.
- patch 4:
- add test__start_subtest.
- add some 'Copyright' entries.
- patch 5:
- change monitor_log_path to static
- drop bpf_trace_printk()
- patch 6:
- use TCP_CA_NAME_MAX instead of strlen(val.ca_name) in strncmp().
- use 'sysctl -b net.ipv4.tcp_congestion_control' to get ca_name.

v10:
- merge the squash-to patch.
- update patch 3 as Matt suggested.
- add sync() in get_msk_token() before read().
- add a comment for get_msk_token().
- update subjects and commit logs.

v9:
- update progs/mptcp.c in patch 4 and 5

v8:
- update as Matt suggested.

v7:
- parse msk token from the output of 'ip mptcp monitor'.
- add Nicolas and Matt's SoB tags.

v6:
- add skc_to_mptcp_sock helper and test

RESEND:
- fix the CI Build Failure.
v5:
- fix incorrect token value
- verify the token in selftest

v4:
- define bpf_mptcp_sock_proto as a static function, no longer export it in linux/bpf.h

v3:
- use RET_PTR_TO_BTF_ID_OR_NULL instead of RET_PTR_TO_MPTCP_SOCK_OR_NULL
- add a new bpf_id BTF_SOCK_TYPE_MPTCP

v2:
- keep RET_PTR_TO_MPTCP_SOCK_OR_NULL. If we use RET_PTR_TO_BTF_ID_OR_NULL
instead of RET_PTR_TO_MPTCP_SOCK_OR_NULL as Alexei suggested, the "userspace" tests developed by Nicolas will break.

Geliang Tang (6):
bpf: add bpf_skc_to_mptcp_sock_proto
Squash to "selftests: bpf: add MPTCP test base"
selftests: bpf: test bpf_skc_to_mptcp_sock
selftests: bpf: verify token of struct mptcp_sock
selftests: bpf: verify ca_name of struct mptcp_sock
selftests: bpf: verify first of struct mptcp_sock

 include/linux/bpf.h                           |   9 +
 include/linux/btf_ids.h                       |   3 +-
 include/uapi/linux/bpf.h                      |   7 +
 net/core/filter.c                             |  16 ++
 net/mptcp/Makefile                            |   4 +
 net/mptcp/bpf.c                               |  22 ++
 scripts/bpf_doc.py                            |   2 +
 tools/include/uapi/linux/bpf.h                |   7 +
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  12 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 209 +++++++++++++++---
 tools/testing/selftests/bpf/progs/mptcp.c     |  48 ----
 .../testing/selftests/bpf/progs/mptcp_sock.c  |  76 +++++++
 12 files changed, 331 insertions(+), 84 deletions(-)
 create mode 100644 net/mptcp/bpf.c
 delete mode 100644 tools/testing/selftests/bpf/progs/mptcp.c
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_sock.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v14 1/6 bpf: add bpf_skc_to_mptcp_sock_proto

This patch implements a new struct bpf_func_proto, named bpf_skc_to_mptcp_sock_proto. Define a new bpf_id BTF_SOCK_TYPE_MPTCP, and a new helper bpf_skc_to_mptcp_sock(), which invokes another new helper bpf_mptcp_sock_from_subflow() in net/mptcp/bpf.c to get struct mptcp_sock from a given subflow socket.

---
 include/linux/bpf.h            |  9 +++++++++
 include/linux/btf_ids.h        |  3 ++-
 include/uapi/linux/bpf.h       |  7 +++++++
 net/core/filter.c              | 16 ++++++++++++++++
 net/mptcp/Makefile             |  4 ++++
 net/mptcp/bpf.c                | 22 ++++++++++++++++++++++
 scripts/bpf_doc.py             |  2 ++
 tools/include/uapi/linux/bpf.h |  7 +++++++
 8 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 net/mptcp/bpf.c
include/linux/bpf.h +9 -0
index bdb5298735ce..117ae3f9451e 100644
@@ -2372,6 +2372,15 @@ static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
 }
 #endif /* CONFIG_INET */
 
+#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
+struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
+#else
+static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
+{
+	return NULL;
+}
+#endif /* defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL) */
+
 enum bpf_text_poke_type {
 	BPF_MOD_CALL,
 	BPF_MOD_JUMP,
include/linux/btf_ids.h +2 -1
index bc5d9cc34e4c..335a19092368 100644
@@ -178,7 +178,8 @@ extern struct btf_id_set name;
 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock)			\
 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock)			\
 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)			\
-	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock)
+	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock)			\
+	BTF_SOCK_TYPE(BTF_SOCK_TYPE_MPTCP, mptcp_sock)
 
 enum {
 #define BTF_SOCK_TYPE(name, str) name,
include/uapi/linux/bpf.h +7 -0
index 9ef1f3e1c22f..785f2cb15495 100644
@@ -5143,6 +5143,12 @@ union bpf_attr {
  *		The **hash_algo** is returned on success,
  *		**-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if
  *		invalid arguments are passed.
+ *
+ * struct mptcp_sock *bpf_skc_to_mptcp_sock(void *sk)
+ *	Description
+ *		Dynamically cast a *sk* pointer to a *mptcp_sock* pointer.
+ *	Return
+ *		*sk* if casting is valid, or **NULL** otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5339,6 +5345,7 @@ union bpf_attr {
 	FN(copy_from_user_task),	\
 	FN(skb_set_tstamp),		\
 	FN(ima_file_hash),		\
+	FN(skc_to_mptcp_sock),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
net/core/filter.c +16 -0
index 258821e58ec6..53f353b81a1f 100644
@@ -11270,6 +11270,19 @@ const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
 	.ret_btf_id		= &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
 };
 
+BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
+{
+	return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
+}
+
+static const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
+	.func		= bpf_skc_to_mptcp_sock,
+	.gpl_only	= false,
+	.ret_type	= RET_PTR_TO_BTF_ID_OR_NULL,
+	.arg1_type	= ARG_PTR_TO_SOCK_COMMON,
+	.ret_btf_id	= &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
+};
+
 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
 {
 	return (unsigned long)sock_from_file(file);
@@ -11312,6 +11325,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
 	case BPF_FUNC_skc_to_unix_sock:
 		func = &bpf_skc_to_unix_sock_proto;
 		break;
+	case BPF_FUNC_skc_to_mptcp_sock:
+		func = &bpf_skc_to_mptcp_sock_proto;
+		break;
 	case BPF_FUNC_ktime_get_coarse_ns:
 		return &bpf_ktime_get_coarse_ns_proto;
 	default:
net/mptcp/Makefile +4 -0
index e54daceac58b..168c55d1c917 100644
@@ -10,3 +10,7 @@ obj-$(CONFIG_INET_MPTCP_DIAG) += mptcp_diag.o
 mptcp_crypto_test-objs := crypto_test.o
 mptcp_token_test-objs := token_test.o
 obj-$(CONFIG_MPTCP_KUNIT_TEST) += mptcp_crypto_test.o mptcp_token_test.o
+
+ifeq ($(CONFIG_BPF_JIT),y)
+obj-$(CONFIG_BPF_SYSCALL) += bpf.o
+endif
net/mptcp/bpf.c new file +22 -0
new file mode 100644
index 000000000000..535602ba2582
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Multipath TCP
+ *
+ * Copyright (c) 2020, Tessares SA.
+ * Copyright (c) 2022, SUSE.
+ *
+ * Author: Nicolas Rybowski <[email protected]>
+ */
+
+#define pr_fmt(fmt) "MPTCP: " fmt
+
+#include <linux/bpf.h>
+#include "protocol.h"
+
+struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
+{
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
+		return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
+
+	return NULL;
+}
+EXPORT_SYMBOL(bpf_mptcp_sock_from_subflow);
scripts/bpf_doc.py +2 -0
index 096625242475..d5452f7eb996 100755
@@ -633,6 +633,7 @@ class PrinterHelpers(Printer):
             'struct socket',
             'struct file',
             'struct bpf_timer',
+            'struct mptcp_sock',
     ]
     known_types = {
             '...',
@@ -682,6 +683,7 @@ class PrinterHelpers(Printer):
             'struct socket',
             'struct file',
             'struct bpf_timer',
+            'struct mptcp_sock',
     }
     mapped_types = {
             'u8': '__u8',
tools/include/uapi/linux/bpf.h +7 -0
index 9ef1f3e1c22f..785f2cb15495 100644
@@ -5143,6 +5143,12 @@ union bpf_attr {
  *		The **hash_algo** is returned on success,
  *		**-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if
  *		invalid arguments are passed.
+ *
+ * struct mptcp_sock *bpf_skc_to_mptcp_sock(void *sk)
+ *	Description
+ *		Dynamically cast a *sk* pointer to a *mptcp_sock* pointer.
+ *	Return
+ *		*sk* if casting is valid, or **NULL** otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5339,6 +5345,7 @@ union bpf_attr {
 	FN(copy_from_user_task),	\
 	FN(skb_set_tstamp),		\
 	FN(ima_file_hash),		\
+	FN(skc_to_mptcp_sock),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.34.1
#2 lore raw

PATCH v14 2/6 Squash to "selftests: bpf: add MPTCP test base"

In reply to Geliang Tang

1 fix libbpf 0.8 build break:

prog_tests/mptcp.c:176:2: error: 'bpf_prog_load_xattr' is deprecated: libbpf v0.8+: use bpf_object__open() and bpf_object__load() instead [-Werror=deprecated-declarations]

2 do some cleanups.

3 rename progs/mptcp.c to progs/mptcp_sock.c

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 77 +++++++++++--------
 .../bpf/progs/{mptcp.c => mptcp_sock.c}       | 16 ++--
 2 files changed, 56 insertions(+), 37 deletions(-)
 rename tools/testing/selftests/bpf/progs/{mptcp.c => mptcp_sock.c} (95%)
tools/testing/selftests/bpf/prog_tests/mptcp.c +47 -30
index 04aef0f147dc..cd548bb2828f 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020, Tessares SA. */
+
 #include <test_progs.h>
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
@@ -13,12 +15,6 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 is_mptcp)
 	int err = 0, cfd = client_fd;
 	struct mptcp_storage val;
 
-	/* Currently there is no easy way to get back the subflow sk from the MPTCP
-	 * sk, thus we cannot access here the sk_storage associated to the subflow
-	 * sk. Also, there is no sk_storage associated with the MPTCP sk since it
-	 * does not trigger sockops events.
-	 * We silently pass this situation at the moment.
-	 */
 	if (is_mptcp == 1)
 		return 0;
 
@@ -28,14 +24,14 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 is_mptcp)
 	}
 
 	if (val.invoked != 1) {
-		log_err("%s: unexpected invoked count %d != %d",
-			msg, val.invoked, 1);
+		log_err("%s: unexpected invoked count %d != 1",
+			msg, val.invoked);
 		err++;
 	}
 
-	if (val.is_mptcp != is_mptcp) {
-		log_err("%s: unexpected bpf_tcp_sock.is_mptcp %d != %d",
-			msg, val.is_mptcp, is_mptcp);
+	if (val.is_mptcp != 0) {
+		log_err("%s: unexpected bpf_tcp_sock.is_mptcp %d != 0",
+			msg, val.is_mptcp);
 		err++;
 	}
 
@@ -45,49 +41,64 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 is_mptcp)
 static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
 {
 	int client_fd, prog_fd, map_fd, err;
+	struct bpf_program *prog;
 	struct bpf_object *obj;
 	struct bpf_map *map;
 
-	struct bpf_prog_load_attr attr = {
-		.prog_type = BPF_PROG_TYPE_SOCK_OPS,
-		.file = "./mptcp.o",
-		.expected_attach_type = BPF_CGROUP_SOCK_OPS,
-	};
+	obj = bpf_object__open("./mptcp_sock.o");
+	if (libbpf_get_error(obj))
+		return -EIO;
 
-	err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
-	if (err) {
-		log_err("Failed to load BPF object");
-		return -1;
+	err = bpf_object__load(obj);
+	if (CHECK_FAIL(err))
+		goto out;
+
+	prog = bpf_object__find_program_by_name(obj, "_sockops");
+	if (CHECK_FAIL(!prog)) {
+		err = -EIO;
+		goto out;
+	}
+
+	prog_fd = bpf_program__fd(prog);
+	if (CHECK_FAIL(prog_fd < 0)) {
+		err = -EIO;
+		goto out;
+	}
+
+	map = bpf_object__find_map_by_name(obj, "socket_storage_map");
+	if (CHECK_FAIL(!map)) {
+		err = -EIO;
+		goto out;
 	}
 
-	map = bpf_object__next_map(obj, NULL);
 	map_fd = bpf_map__fd(map);
+	if (CHECK_FAIL(map_fd < 0)) {
+		err = -EIO;
+		goto out;
+	}
 
 	err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
-	if (err) {
-		log_err("Failed to attach BPF program");
-		goto close_bpf_object;
-	}
+	if (CHECK_FAIL(err))
+		goto out;
 
 	client_fd = is_mptcp ? connect_to_mptcp_fd(server_fd, 0) :
 			       connect_to_fd(server_fd, 0);
 	if (client_fd < 0) {
-		err = -1;
-		goto close_client_fd;
+		err = -EIO;
+		goto out;
 	}
 
 	err += is_mptcp ? verify_sk(map_fd, client_fd, "MPTCP subflow socket", 1) :
 			  verify_sk(map_fd, client_fd, "plain TCP socket", 0);
 
-close_client_fd:
 	close(client_fd);
 
-close_bpf_object:
+out:
 	bpf_object__close(obj);
 	return err;
 }
 
-void test_mptcp(void)
+void test_base(void)
 {
 	int server_fd, cgroup_fd;
 
@@ -117,3 +128,9 @@ void test_mptcp(void)
 close_cgroup_fd:
 	close(cgroup_fd);
 }
+
+void test_mptcp(void)
+{
+	if (test__start_subtest("base"))
+		test_base();
+}
tools/testing/selftests/bpf/progs/mptcp_sock.c renamed from tools/testing/selftests/bpf/progs/mptcp.c +9 -7
similarity index 95%
rename from tools/testing/selftests/bpf/progs/mptcp.c
rename to tools/testing/selftests/bpf/progs/mptcp_sock.c
index be5ee8dac2b3..0d65fb889d03 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020, Tessares SA. */
+
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 
@@ -25,22 +27,22 @@ int _sockops(struct bpf_sock_ops *ctx)
 	int op = (int)ctx->op;
 	struct bpf_sock *sk;
 
+	if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
+		return 1;
+
 	sk = ctx->sk;
 	if (!sk)
 		return 1;
 
+	tcp_sk = bpf_tcp_sock(sk);
+	if (!tcp_sk)
+		return 1;
+
 	storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
 				     BPF_SK_STORAGE_GET_F_CREATE);
 	if (!storage)
 		return 1;
 
-	if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
-		return 1;
-
-	tcp_sk = bpf_tcp_sock(sk);
-	if (!tcp_sk)
-		return 1;
-
 	storage->invoked++;
 	storage->is_mptcp = tcp_sk->is_mptcp;
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH v14 3/6 selftests: bpf: test bpf_skc_to_mptcp_sock

In reply to Geliang Tang

This patch extends the MPTCP test base, to test the new helper bpf_skc_to_mptcp_sock().

Define struct mptcp_sock in bpf_tcp_helpers.h, use bpf_skc_to_mptcp_sock to get the msk socket in progs/mptcp_sock.c and store the infos in socket_storage_map.

Get the infos from socket_storage_map in prog_tests/mptcp.c. Add a new function verify_msk() to verify the infos of MPTCP socket, and rename verify_sk() to verify_tsk() to verify TCP socket only.

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  5 +++
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 36 +++++++++++++++----
 .../testing/selftests/bpf/progs/mptcp_sock.c  | 22 +++++++++---
 3 files changed, 52 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/bpf_tcp_helpers.h +5 -0
index b1ede6f0b821..d71616873a3c 100644
@@ -83,6 +83,11 @@ struct tcp_sock {
 	__u64	tcp_mstamp;	/* most recent packet received/sent */
 } __attribute__((preserve_access_index));
 
+struct mptcp_sock {
+	struct inet_connection_sock	sk;
+
+} __attribute__((preserve_access_index));
+
 static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
 {
 	return (struct inet_connection_sock *)sk;
tools/testing/selftests/bpf/prog_tests/mptcp.c +30 -6
index cd548bb2828f..4b40bbdaf91f 100644
@@ -10,14 +10,12 @@ struct mptcp_storage {
 	__u32 is_mptcp;
 };
 
-static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 is_mptcp)
+static int verify_tsk(int map_fd, int client_fd)
 {
+	char *msg = "plain TCP socket";
 	int err = 0, cfd = client_fd;
 	struct mptcp_storage val;
 
-	if (is_mptcp == 1)
-		return 0;
-
 	if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &cfd, &val) < 0)) {
 		perror("Failed to read socket storage");
 		return -1;
@@ -38,6 +36,32 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 is_mptcp)
 	return err;
 }
 
+static int verify_msk(int map_fd, int client_fd)
+{
+	char *msg = "MPTCP subflow socket";
+	int err = 0, cfd = client_fd;
+	struct mptcp_storage val;
+
+	if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &cfd, &val) < 0)) {
+		perror("Failed to read socket storage");
+		return -1;
+	}
+
+	if (val.invoked != 1) {
+		log_err("%s: unexpected invoked count %d != 1",
+			msg, val.invoked);
+		err++;
+	}
+
+	if (val.is_mptcp != 1) {
+		log_err("%s: unexpected bpf_tcp_sock.is_mptcp %d != 1",
+			msg, val.is_mptcp);
+		err++;
+	}
+
+	return err;
+}
+
 static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
 {
 	int client_fd, prog_fd, map_fd, err;
@@ -88,8 +112,8 @@ static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
 		goto out;
 	}
 
-	err += is_mptcp ? verify_sk(map_fd, client_fd, "MPTCP subflow socket", 1) :
-			  verify_sk(map_fd, client_fd, "plain TCP socket", 0);
+	err += is_mptcp ? verify_msk(map_fd, client_fd) :
+			  verify_tsk(map_fd, client_fd);
 
 	close(client_fd);
 
tools/testing/selftests/bpf/progs/mptcp_sock.c +17 -5
index 0d65fb889d03..a58cd24da777 100644
@@ -3,6 +3,7 @@
 
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_tcp_helpers.h"
 
 char _license[] SEC("license") = "GPL";
 __u32 _version SEC("version") = 1;
@@ -24,6 +25,7 @@ int _sockops(struct bpf_sock_ops *ctx)
 {
 	struct mptcp_storage *storage;
 	struct bpf_tcp_sock *tcp_sk;
+	struct mptcp_sock *msk;
 	int op = (int)ctx->op;
 	struct bpf_sock *sk;
 
@@ -38,11 +40,21 @@ int _sockops(struct bpf_sock_ops *ctx)
 	if (!tcp_sk)
 		return 1;
 
-	storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
-				     BPF_SK_STORAGE_GET_F_CREATE);
-	if (!storage)
-		return 1;
-
+	if (!tcp_sk->is_mptcp) {
+		storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
+					     BPF_SK_STORAGE_GET_F_CREATE);
+		if (!storage)
+			return 1;
+	} else {
+		msk = bpf_skc_to_mptcp_sock(sk);
+		if (!msk)
+			return 1;
+
+		storage = bpf_sk_storage_get(&socket_storage_map, msk, 0,
+					     BPF_SK_STORAGE_GET_F_CREATE);
+		if (!storage)
+			return 1;
+	}
 	storage->invoked++;
 	storage->is_mptcp = tcp_sk->is_mptcp;
 
Signature · 1 line
2.34.1
#4 lore raw

PATCH v14 4/6 selftests: bpf: verify token of struct mptcp_sock

In reply to Geliang Tang

This patch verifies the struct member token of struct mptcp_sock. Add a new function get_msk_token() to parse the msk token from the output of the command 'ip mptcp monitor', and verify it in verify_msk().

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  1 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 66 +++++++++++++++++++
 .../testing/selftests/bpf/progs/mptcp_sock.c  |  5 ++
 3 files changed, 72 insertions(+)
tools/testing/selftests/bpf/bpf_tcp_helpers.h +1 -0
index d71616873a3c..9ea6687c8f4d 100644
@@ -86,6 +86,7 @@ struct tcp_sock {
 struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
+	__u32		token;
 } __attribute__((preserve_access_index));
 
 static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
tools/testing/selftests/bpf/prog_tests/mptcp.c +66 -0

109 lines not on this page · raw message

tools/testing/selftests/bpf/progs/mptcp_sock.c +5 -0
index a58cd24da777..afacea5ad9ca 100644
@@ -11,6 +11,7 @@ __u32 _version SEC("version") = 1;
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
+	__u32 token;
 };
 
 struct {
@@ -45,6 +46,8 @@ int _sockops(struct bpf_sock_ops *ctx)
 					     BPF_SK_STORAGE_GET_F_CREATE);
 		if (!storage)
 			return 1;
+
+		storage->token = 0;
 	} else {
 		msk = bpf_skc_to_mptcp_sock(sk);
 		if (!msk)
@@ -54,6 +57,8 @@ int _sockops(struct bpf_sock_ops *ctx)
 					     BPF_SK_STORAGE_GET_F_CREATE);
 		if (!storage)
 			return 1;
+
+		storage->token = msk->token;
 	}
 	storage->invoked++;
 	storage->is_mptcp = tcp_sk->is_mptcp;
Signature · 1 line
2.34.1
#5 lore raw

PATCH v14 5/6 selftests: bpf: verify ca_name of struct mptcp_sock

In reply to Geliang Tang

This patch verifies another member of struct mptcp_sock, ca_name. Add a new function get_msk_ca_name() to read the sysctl tcp_congestion_control and verify it in verify_msk().

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  5 ++++
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 24 +++++++++++++++++++
 .../testing/selftests/bpf/progs/mptcp_sock.c  |  4 ++++
 3 files changed, 33 insertions(+)
tools/testing/selftests/bpf/bpf_tcp_helpers.h +5 -0
index 9ea6687c8f4d..bebe382bcc7a 100644
@@ -16,6 +16,10 @@ BPF_PROG(name, args)
 #define SOL_TCP 6
 #endif
 
+#ifndef TCP_CA_NAME_MAX
+#define TCP_CA_NAME_MAX	16
+#endif
+
 #define tcp_jiffies32 ((__u32)bpf_jiffies64())
 
 struct sock_common {
@@ -87,6 +91,7 @@ struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
 	__u32		token;
+	char		ca_name[TCP_CA_NAME_MAX];
 } __attribute__((preserve_access_index));
 
 static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
tools/testing/selftests/bpf/prog_tests/mptcp.c +24 -0
index c5d96ba81e04..c6bdd9c5f126 100644
@@ -5,10 +5,15 @@
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
 
+#ifndef TCP_CA_NAME_MAX
+#define TCP_CA_NAME_MAX	16
+#endif
+
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
 	__u32 token;
+	char ca_name[TCP_CA_NAME_MAX];
 };
 
 static char monitor_log_path[64];
@@ -79,11 +84,22 @@ static __u32 get_msk_token(void)
 	return token;
 }
 
+void get_msk_ca_name(char ca_name[])
+{
+	FILE *stream = popen("sysctl -b net.ipv4.tcp_congestion_control", "r");
+
+	if (fgets(ca_name, TCP_CA_NAME_MAX, stream) == NULL)
+		log_err("Failed to read ca_name");
+
+	pclose(stream);
+}
+
 static int verify_msk(int map_fd, int client_fd)
 {
 	char *msg = "MPTCP subflow socket";
 	int err = 0, cfd = client_fd;
 	struct mptcp_storage val;
+	char ca_name[TCP_CA_NAME_MAX];
 	__u32 token;
 
 	token = get_msk_token();
@@ -92,6 +108,8 @@ static int verify_msk(int map_fd, int client_fd)
 		return -1;
 	}
 
+	get_msk_ca_name(ca_name);
+
 	if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &cfd, &val) < 0)) {
 		perror("Failed to read socket storage");
 		return -1;
@@ -115,6 +133,12 @@ static int verify_msk(int map_fd, int client_fd)
 		err++;
 	}
 
+	if (strncmp(val.ca_name, ca_name, TCP_CA_NAME_MAX)) {
+		log_err("Unexpected mptcp_sock.ca_name %s != %s",
+			val.ca_name, ca_name);
+		err++;
+	}
+
 	return err;
 }
 
tools/testing/selftests/bpf/progs/mptcp_sock.c +4 -0
index afacea5ad9ca..6e7d2abf6ce3 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020, Tessares SA. */
 
+#include <string.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_tcp_helpers.h"
@@ -12,6 +13,7 @@ struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
 	__u32 token;
+	char ca_name[TCP_CA_NAME_MAX];
 };
 
 struct {
@@ -48,6 +50,7 @@ int _sockops(struct bpf_sock_ops *ctx)
 			return 1;
 
 		storage->token = 0;
+		bzero(storage->ca_name, TCP_CA_NAME_MAX);
 	} else {
 		msk = bpf_skc_to_mptcp_sock(sk);
 		if (!msk)
@@ -59,6 +62,7 @@ int _sockops(struct bpf_sock_ops *ctx)
 			return 1;
 
 		storage->token = msk->token;
+		memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX);
 	}
 	storage->invoked++;
 	storage->is_mptcp = tcp_sk->is_mptcp;
Signature · 1 line
2.34.1
#6 lore raw

PATCH v14 6/6 selftests: bpf: verify first of struct mptcp_sock

In reply to Geliang Tang

This patch verifies the 'first' struct member of struct mptcp_sock, which points to the first subflow of msk. Save 'sk' in mptcp_storage, and verify it with 'first' in verify_msk().

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h  | 1 +
 tools/testing/selftests/bpf/prog_tests/mptcp.c | 8 ++++++++
 tools/testing/selftests/bpf/progs/mptcp_sock.c | 5 +++++
 3 files changed, 14 insertions(+)
tools/testing/selftests/bpf/bpf_tcp_helpers.h +1 -0
index bebe382bcc7a..f92357597e63 100644
@@ -91,6 +91,7 @@ struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
 	__u32		token;
+	struct sock	*first;
 	char		ca_name[TCP_CA_NAME_MAX];
 } __attribute__((preserve_access_index));
 
tools/testing/selftests/bpf/prog_tests/mptcp.c +8 -0
index c6bdd9c5f126..9b73ef62ee74 100644
@@ -12,7 +12,9 @@
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
+	struct sock *sk;
 	__u32 token;
+	struct sock *first;
 	char ca_name[TCP_CA_NAME_MAX];
 };
 
@@ -133,6 +135,12 @@ static int verify_msk(int map_fd, int client_fd)
 		err++;
 	}
 
+	if (val.first != val.sk) {
+		log_err("Unexpected mptcp_sock.first %p != %p",
+			val.first, val.sk);
+		err++;
+	}
+
 	if (strncmp(val.ca_name, ca_name, TCP_CA_NAME_MAX)) {
 		log_err("Unexpected mptcp_sock.ca_name %s != %s",
 			val.ca_name, ca_name);
tools/testing/selftests/bpf/progs/mptcp_sock.c +5 -0
index 6e7d2abf6ce3..16055f553f29 100644
@@ -12,7 +12,9 @@ __u32 _version SEC("version") = 1;
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
+	struct sock *sk;
 	__u32 token;
+	struct sock *first;
 	char ca_name[TCP_CA_NAME_MAX];
 };
 
@@ -51,6 +53,7 @@ int _sockops(struct bpf_sock_ops *ctx)
 
 		storage->token = 0;
 		bzero(storage->ca_name, TCP_CA_NAME_MAX);
+		storage->first = NULL;
 	} else {
 		msk = bpf_skc_to_mptcp_sock(sk);
 		if (!msk)
@@ -63,9 +66,11 @@ int _sockops(struct bpf_sock_ops *ctx)
 
 		storage->token = msk->token;
 		memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX);
+		storage->first = msk->first;
 	}
 	storage->invoked++;
 	storage->is_mptcp = tcp_sk->is_mptcp;
+	storage->sk = (struct sock *)sk;
 
 	return 1;
 }
Signature · 1 line
2.34.1
#7 lore raw
In reply to Geliang Tang
Quoted 10 lines · Geliang Tang

This series addresses to Alexei comments in [1].

[1]
https://lore.kernel.org/netdev/[email protected]/

v14:
- merge the squash-to patch for patch 1.
- rename progs/mptcp.c to progs/mptcp_sock.c in patch 2.
- split the token verifying as a new patch from patch 3.
- update the commit logs.

Looks ok to me, Matthieu had acked the previous rev so he can take another look too.

- Mat

Quoted 103 lines

v13:
patch 1:
As Mat mentioned in the "BPF packet scheduler" series, BPF packet
scheduler depends on CONFIG_BPF_SYSCALL. In fact, the series depends on CONFIG_BPF_SYSCALL too.
- change "CONFIG_BPF" to "CONFIG_BPF_SYSCALL" in Makefile
- change "ifdef CONFIG_MPTCP" in include/linux/bpf.h to:

   "#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)"

- drop "#ifdef CONFIG_MPTCP" in net/core/filter.c
patch 2:
Address to Matt's comments in v12:
- use bpf_object__find_program_by_name, bpf_object__find_map_by_name
- use -EIO
- drop close_client_fd label
- add more CHECK_FAIL

v12:
patch 1:
- merged the squash-to patch for v11.
- rebased to export/20220324T054815.
patch 2:
- fix the libbpf 0.8 build error.
patch 4:
- use fgets instead of fread to get null terminated string.

v11:
- merge "check more values from mptcp_sock" series into this series.
- patch 2:
- rebased.
- patch 3:
- rebased.
- add 'Copyright' entry as Matt suggested in v9.
- patch 4:
- add test__start_subtest.
- add some 'Copyright' entries.
- patch 5:
- change monitor_log_path to static
- drop bpf_trace_printk()
- patch 6:
- use TCP_CA_NAME_MAX instead of strlen(val.ca_name) in strncmp().
- use 'sysctl -b net.ipv4.tcp_congestion_control' to get ca_name.

v10:
- merge the squash-to patch.
- update patch 3 as Matt suggested.
- add sync() in get_msk_token() before read().
- add a comment for get_msk_token().
- update subjects and commit logs.

v9:
- update progs/mptcp.c in patch 4 and 5

v8:
- update as Matt suggested.

v7:
- parse msk token from the output of 'ip mptcp monitor'.
- add Nicolas and Matt's SoB tags.

v6:
- add skc_to_mptcp_sock helper and test

RESEND:
- fix the CI Build Failure.
v5:
- fix incorrect token value
- verify the token in selftest

v4:
- define bpf_mptcp_sock_proto as a static function, no longer export it in linux/bpf.h

v3:
- use RET_PTR_TO_BTF_ID_OR_NULL instead of RET_PTR_TO_MPTCP_SOCK_OR_NULL
- add a new bpf_id BTF_SOCK_TYPE_MPTCP

v2:
- keep RET_PTR_TO_MPTCP_SOCK_OR_NULL. If we use RET_PTR_TO_BTF_ID_OR_NULL instead of RET_PTR_TO_MPTCP_SOCK_OR_NULL as Alexei suggested, the "userspace" tests developed by Nicolas will break.

Geliang Tang (6):
bpf: add bpf_skc_to_mptcp_sock_proto
Squash to "selftests: bpf: add MPTCP test base"
selftests: bpf: test bpf_skc_to_mptcp_sock
selftests: bpf: verify token of struct mptcp_sock
selftests: bpf: verify ca_name of struct mptcp_sock
selftests: bpf: verify first of struct mptcp_sock

include/linux/bpf.h                           |   9 +
include/linux/btf_ids.h                       |   3 +-
include/uapi/linux/bpf.h                      |   7 +
net/core/filter.c                             |  16 ++
net/mptcp/Makefile                            |   4 +
net/mptcp/bpf.c                               |  22 ++
scripts/bpf_doc.py                            |   2 +
tools/include/uapi/linux/bpf.h                |   7 +
tools/testing/selftests/bpf/bpf_tcp_helpers.h |  12 +
.../testing/selftests/bpf/prog_tests/mptcp.c  | 209 +++++++++++++++---
tools/testing/selftests/bpf/progs/mptcp.c     |  48 ----
.../testing/selftests/bpf/progs/mptcp_sock.c  |  76 +++++++
12 files changed, 331 insertions(+), 84 deletions(-)
Quoted 9 lines

create mode 100644 net/mptcp/bpf.c
delete mode 100644 tools/testing/selftests/bpf/progs/mptcp.c create mode 100644 tools/testing/selftests/bpf/progs/mptcp_sock.c

Signature · 1 line
2.34.1

--
Mat Martineau
Intel

#8 lore raw
In reply to Geliang Tang

Hi Geliang, Mat,

Quoted 10 lines · Geliang Tang

This series addresses to Alexei comments in [1].

[1]
https://lore.kernel.org/netdev/[email protected]/

v14:
- merge the squash-to patch for patch 1.
- rename progs/mptcp.c to progs/mptcp_sock.c in patch 2.
- split the token verifying as a new patch from patch 3.
- update the commit logs.

Thank you for this new version and the reviews!

Now in our tree with Mat's ACK:

New patches for t/upstream:

- f12408559134: bpf: add bpf_skc_to_mptcp_sock_proto
- 3666ef4c06e3: "squashed" patch 2/6 in "selftests: bpf: add MPTCP test base"
- 0e2e98491bb3: selftests: bpf: test bpf_skc_to_mptcp_sock
- fc48505b75eb: selftests: bpf: verify token of struct mptcp_sock
- d74bab4b4875: selftests: bpf: verify ca_name of struct mptcp_sock
- 0b2f88af324f: selftests: bpf: verify first of struct mptcp_sock
- Results: e1bfedef8dde..d2b06f03e554 (export)

Builds and tests are now in progress:

https://cirrus-ci.com/github/multipath-tcp/mptcp_net-next/export/20220329T195411
https://github.com/multipath-tcp/mptcp_net-next/actions/workflows/build-validation.yml?query=branch:export

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net