[PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper

Geliang Tang posted 4 patches 2 years, 7 months ago
Maintainers: Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Andrii Nakryiko <andrii@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>, John Fastabend <john.fastabend@gmail.com>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>, Matthieu Baerts <matthieu.baerts@tessares.net>, Mat Martineau <martineau@kernel.org>
There is a newer version of this series
[PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper
Posted by Geliang Tang 2 years, 7 months ago
This patch implements a new struct bpf_func_proto bpf_mptcpify_proto. And
define a new helper bpf_mptcpify() to mptcpify a TCP socket dynamically as
an MPTCP one.

In bpf_mptcpify(), if the protocol ID of sk is IPPROTO_TCP, set it to
IPPROTO_MPTCP.

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       |  7 +++++++
 kernel/bpf/cgroup.c            |  2 ++
 net/core/filter.c              | 18 ++++++++++++++++++
 scripts/bpf_doc.py             |  1 +
 tools/include/uapi/linux/bpf.h |  7 +++++++
 6 files changed, 36 insertions(+)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index f58895830ada..424056fd5335 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2883,6 +2883,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
+extern const struct bpf_func_proto bpf_mptcpify_proto;
 extern const struct bpf_func_proto bpf_copy_from_user_proto;
 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
 extern const struct bpf_func_proto bpf_snprintf_proto;
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 60a9d59beeab..fa8a80024b67 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5573,6 +5573,12 @@ union bpf_attr {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * struct sock *bpf_mptcpify(void *sk)
+ *	Description
+ *		Dynamically mptcpify a TCP socket *sk* pointer as an MPTCP one.
+ *	Return
+ *		*sk* if it's valid, or **NULL** otherwise.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -5787,6 +5793,7 @@ union bpf_attr {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(mptcpify, 212, ##ctx)			\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 5b2741aa0d9b..6b56de24e1a2 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -2542,6 +2542,8 @@ cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		default:
 			return &bpf_set_retval_proto;
 		}
+	case BPF_FUNC_mptcpify:
+		return &bpf_mptcpify_proto;
 	default:
 		return NULL;
 	}
diff --git a/net/core/filter.c b/net/core/filter.c
index 06ba0e56e369..e43b2c42c094 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -11678,6 +11678,24 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
 	.ret_btf_id	= &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
 };
 
+BPF_CALL_1(bpf_mptcpify, struct sock *, sk)
+{
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) {
+		sk->sk_protocol = IPPROTO_MPTCP;
+		return (unsigned long)sk;
+	}
+
+	return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_mptcpify_proto = {
+	.func		= bpf_mptcpify,
+	.gpl_only	= false,
+	.ret_type	= RET_PTR_TO_BTF_ID_OR_NULL,
+	.ret_btf_id	= &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
+	.arg1_type	= ARG_PTR_TO_CTX,
+};
+
 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
 {
 	return (unsigned long)sock_from_file(file);
diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index eaae2ce78381..7a20ab9c6513 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -751,6 +751,7 @@ class PrinterHelpers(Printer):
             'struct file',
             'struct bpf_timer',
             'struct mptcp_sock',
+            'struct sock',
             'struct bpf_dynptr',
             'const struct bpf_dynptr',
             'struct iphdr',
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 60a9d59beeab..fa8a80024b67 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5573,6 +5573,12 @@ union bpf_attr {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * struct sock *bpf_mptcpify(void *sk)
+ *	Description
+ *		Dynamically mptcpify a TCP socket *sk* pointer as an MPTCP one.
+ *	Return
+ *		*sk* if it's valid, or **NULL** otherwise.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -5787,6 +5793,7 @@ union bpf_attr {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(mptcpify, 212, ##ctx)			\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
-- 
2.35.3
Re: [PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper
Posted by kernel test robot 2 years, 7 months ago
Hi Geliang,

kernel test robot noticed the following build errors:

[auto build test ERROR on mptcp/export]
[also build test ERROR on mptcp/export-net linus/master v6.4 next-20230630]
[cannot apply to bpf-next/master bpf/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Geliang-Tang/Squash-to-selftests-bpf-add-two-mptcp-netns-helpers/20230701-205922
base:   https://github.com/multipath-tcp/mptcp_net-next.git export
patch link:    https://lore.kernel.org/r/daf3d1e26f19ee83b427ef9e17183cd09fc26de6.1688215769.git.geliang.tang%40suse.com
patch subject: [PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper
config: nios2-randconfig-r021-20230701 (https://download.01.org/0day-ci/archive/20230702/202307020056.CI5KNYyK-lkp@intel.com/config)
compiler: nios2-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230702/202307020056.CI5KNYyK-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202307020056.CI5KNYyK-lkp@intel.com/

All errors (new ones prefixed by >>):

   nios2-linux-ld: kernel/bpf/cgroup.o: in function `cgroup_dev_func_proto':
   kernel/bpf/cgroup.c:1607: undefined reference to `bpf_mptcpify_proto'
>> nios2-linux-ld: kernel/bpf/cgroup.c:1607: undefined reference to `bpf_mptcpify_proto'
   nios2-linux-ld: kernel/bpf/cgroup.o: in function `cg_sockopt_func_proto':
   kernel/bpf/cgroup.c:2297: undefined reference to `bpf_mptcpify_proto'
   nios2-linux-ld: kernel/bpf/cgroup.c:2297: undefined reference to `bpf_mptcpify_proto'
   nios2-linux-ld: kernel/bpf/cgroup.o: in function `sysctl_func_proto':
   kernel/bpf/cgroup.c:2155: undefined reference to `bpf_mptcpify_proto'
   nios2-linux-ld: kernel/bpf/cgroup.o:kernel/bpf/cgroup.c:2155: more undefined references to `bpf_mptcpify_proto' follow

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper
Posted by kernel test robot 2 years, 7 months ago
Hi Geliang,

kernel test robot noticed the following build errors:

[auto build test ERROR on mptcp/export]
[also build test ERROR on mptcp/export-net linus/master v6.4 next-20230630]
[cannot apply to bpf-next/master bpf/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Geliang-Tang/Squash-to-selftests-bpf-add-two-mptcp-netns-helpers/20230701-205922
base:   https://github.com/multipath-tcp/mptcp_net-next.git export
patch link:    https://lore.kernel.org/r/daf3d1e26f19ee83b427ef9e17183cd09fc26de6.1688215769.git.geliang.tang%40suse.com
patch subject: [PATCH mptcp-next v2 2/4] bpf: Add bpf_mptcpify helper
config: x86_64-buildonly-randconfig-r001-20230701 (https://download.01.org/0day-ci/archive/20230701/202307012313.1sND4cvO-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230701/202307012313.1sND4cvO-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202307012313.1sND4cvO-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: kernel/bpf/cgroup.o: in function `cgroup_common_func_proto':
>> kernel/bpf/cgroup.c:2512: undefined reference to `bpf_mptcpify_proto'


vim +2512 kernel/bpf/cgroup.c

dea6a4e1701338 Stanislav Fomichev 2022-08-23  2507  
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2508  /* Common helpers for cgroup hooks. */
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2509  const struct bpf_func_proto *
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2510  cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2511  {
dea6a4e1701338 Stanislav Fomichev 2022-08-23 @2512  	switch (func_id) {
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2513  	case BPF_FUNC_get_local_storage:
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2514  		return &bpf_get_local_storage_proto;
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2515  	case BPF_FUNC_get_retval:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2516  		switch (prog->expected_attach_type) {
bed89185af0de0 Stanislav Fomichev 2022-08-23  2517  		case BPF_CGROUP_INET_INGRESS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2518  		case BPF_CGROUP_INET_EGRESS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2519  		case BPF_CGROUP_SOCK_OPS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2520  		case BPF_CGROUP_UDP4_RECVMSG:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2521  		case BPF_CGROUP_UDP6_RECVMSG:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2522  		case BPF_CGROUP_INET4_GETPEERNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2523  		case BPF_CGROUP_INET6_GETPEERNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2524  		case BPF_CGROUP_INET4_GETSOCKNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2525  		case BPF_CGROUP_INET6_GETSOCKNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2526  			return NULL;
bed89185af0de0 Stanislav Fomichev 2022-08-23  2527  		default:
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2528  			return &bpf_get_retval_proto;
bed89185af0de0 Stanislav Fomichev 2022-08-23  2529  		}
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2530  	case BPF_FUNC_set_retval:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2531  		switch (prog->expected_attach_type) {
bed89185af0de0 Stanislav Fomichev 2022-08-23  2532  		case BPF_CGROUP_INET_INGRESS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2533  		case BPF_CGROUP_INET_EGRESS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2534  		case BPF_CGROUP_SOCK_OPS:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2535  		case BPF_CGROUP_UDP4_RECVMSG:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2536  		case BPF_CGROUP_UDP6_RECVMSG:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2537  		case BPF_CGROUP_INET4_GETPEERNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2538  		case BPF_CGROUP_INET6_GETPEERNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2539  		case BPF_CGROUP_INET4_GETSOCKNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2540  		case BPF_CGROUP_INET6_GETSOCKNAME:
bed89185af0de0 Stanislav Fomichev 2022-08-23  2541  			return NULL;
bed89185af0de0 Stanislav Fomichev 2022-08-23  2542  		default:
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2543  			return &bpf_set_retval_proto;
bed89185af0de0 Stanislav Fomichev 2022-08-23  2544  		}
96703c9170f3b5 Geliang Tang       2023-07-01  2545  	case BPF_FUNC_mptcpify:
96703c9170f3b5 Geliang Tang       2023-07-01  2546  		return &bpf_mptcpify_proto;
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2547  	default:
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2548  		return NULL;
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2549  	}
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2550  }
dea6a4e1701338 Stanislav Fomichev 2022-08-23  2551  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki