This patch implemented bpf_skc_to_mptcp_sock helper. Defined a new bpf_id
BTF_SOCK_TYPE_MPTCP, and added a new helper bpf_mptcp_sock_from_subflow()
to get struct bpf_mptcp_sock from a given subflow socket.
Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
include/linux/bpf.h | 9 +++++++++
include/linux/btf_ids.h | 3 ++-
include/uapi/linux/bpf.h | 7 +++++++
net/core/filter.c | 17 +++++++++++++++++
net/mptcp/Makefile | 2 ++
net/mptcp/bpf.c | 17 +++++++++++++++++
scripts/bpf_doc.py | 2 ++
tools/include/uapi/linux/bpf.h | 7 +++++++
8 files changed, 63 insertions(+), 1 deletion(-)
create mode 100644 net/mptcp/bpf.c
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index f19abc59b6cd..b28b3d8c3df2 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2354,6 +2354,15 @@ static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
}
#endif /* CONFIG_INET */
+#ifdef CONFIG_MPTCP
+struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
+#else /* CONFIG_MPTCP */
+static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
+{
+ return NULL;
+}
+#endif /* CONFIG_MPTCP */
+
enum bpf_text_poke_type {
BPF_MOD_CALL,
BPF_MOD_JUMP,
diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h
index bc5d9cc34e4c..335a19092368 100644
--- a/include/linux/btf_ids.h
+++ b/include/linux/btf_ids.h
@@ -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,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 3f6dcdf4b915..a88aac4457a9 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5086,6 +5086,12 @@ union bpf_attr {
* Return
* 0 on success, or a negative error in case of failure. On error
* *dst* buffer is zeroed out.
+ *
+ * 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), \
@@ -5280,6 +5286,7 @@ union bpf_attr {
FN(xdp_load_bytes), \
FN(xdp_store_bytes), \
FN(copy_from_user_task), \
+ FN(skc_to_mptcp_sock), \
/* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
diff --git a/net/core/filter.c b/net/core/filter.c
index f64454722a7e..538b6db4fd76 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -11121,6 +11121,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);
@@ -11163,6 +11176,10 @@ 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;
+#ifdef CONFIG_MPTCP
+ case BPF_FUNC_skc_to_mptcp_sock:
+ return &bpf_skc_to_mptcp_sock_proto;
+#endif /* CONFIG_MPTCP */
case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto;
default:
diff --git a/net/mptcp/Makefile b/net/mptcp/Makefile
index 48a9d978aaeb..0a0608b6b4b4 100644
--- a/net/mptcp/Makefile
+++ b/net/mptcp/Makefile
@@ -11,3 +11,5 @@ 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
+
+obj-$(CONFIG_BPF) += bpf.o
diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c
new file mode 100644
index 000000000000..d140979181f9
--- /dev/null
+++ b/net/mptcp/bpf.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Multipath TCP
+ *
+ * Copyright (c) 2022, SUSE.
+ */
+
+#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);
diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index 096625242475..0e5a9e69ae59 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -623,6 +623,7 @@ class PrinterHelpers(Printer):
'struct udp6_sock',
'struct unix_sock',
'struct task_struct',
+ 'struct mptcp_sock',
'struct __sk_buff',
'struct sk_msg_md',
@@ -682,6 +683,7 @@ class PrinterHelpers(Printer):
'struct socket',
'struct file',
'struct bpf_timer',
+ 'struct mptcp_sock',
}
mapped_types = {
'u8': '__u8',
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 3f6dcdf4b915..a88aac4457a9 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5086,6 +5086,12 @@ union bpf_attr {
* Return
* 0 on success, or a negative error in case of failure. On error
* *dst* buffer is zeroed out.
+ *
+ * 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), \
@@ -5280,6 +5286,7 @@ union bpf_attr {
FN(xdp_load_bytes), \
FN(xdp_store_bytes), \
FN(copy_from_user_task), \
+ FN(skc_to_mptcp_sock), \
/* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
--
2.34.1
Hi Geliang, On 06/03/2022 02:01, Geliang Tang wrote: > This patch implemented bpf_skc_to_mptcp_sock helper. Defined a new bpf_id > BTF_SOCK_TYPE_MPTCP, and added a new helper bpf_mptcp_sock_from_subflow() > to get struct bpf_mptcp_sock from a given subflow socket. Thank you for the new version. It looks more like what I had in mind after having re-read Alexei's review! Patches 1-3/4 looks good to me but I just have two small suggestions for this patch 3/4. My review for patch 4/4 will follow. Do you mind if I re-add Nicolas' SoB as a co-developer? It is fine for me to change the commiter name because after the recent review iterations we had recently, the code has been almost fully rewritten. But still, I would prefer to keep him as Co-dev as a "reward" for what he did as a student :) So we would have: Co-developed-by: Nicolas Rybowski <nicolas.rybowski@tessares.net> Signed-off-by: Nicolas Rybowski <nicolas.rybowski@tessares.net> Signed-off-by: Geliang Tang <geliang.tang@suse.com> No objection from you? (...) > diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c > new file mode 100644 > index 000000000000..d140979181f9 > --- /dev/null > +++ b/net/mptcp/bpf.c > @@ -0,0 +1,17 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Multipath TCP > + * > + * Copyright (c) 2022, SUSE. I understand your modification but is it OK if I re-add the previous before the new one? Copyright (c) 2020, Tessares SA. (I would prefer to keep it not to forget about very small companies contributing to the Linux kernel and I'm not sure it can be easily replaced :) ) I can do these modifications when applying the patches if there are no objections. Cheers, Matt -- Tessares | Belgium | Hybrid Access Solutions www.tessares.net
Hi Matt, On Mon, Mar 07, 2022 at 03:04:29PM +0100, Matthieu Baerts wrote: > Hi Geliang, > > On 06/03/2022 02:01, Geliang Tang wrote: > > This patch implemented bpf_skc_to_mptcp_sock helper. Defined a new bpf_id > > BTF_SOCK_TYPE_MPTCP, and added a new helper bpf_mptcp_sock_from_subflow() > > to get struct bpf_mptcp_sock from a given subflow socket. > > Thank you for the new version. It looks more like what I had in mind > after having re-read Alexei's review! > > Patches 1-3/4 looks good to me but I just have two small suggestions for > this patch 3/4. My review for patch 4/4 will follow. > > Do you mind if I re-add Nicolas' SoB as a co-developer? It is fine for > me to change the commiter name because after the recent review > iterations we had recently, the code has been almost fully rewritten. > But still, I would prefer to keep him as Co-dev as a "reward" for what > he did as a student :) > > So we would have: > > Co-developed-by: Nicolas Rybowski <nicolas.rybowski@tessares.net> > Signed-off-by: Nicolas Rybowski <nicolas.rybowski@tessares.net> > Signed-off-by: Geliang Tang <geliang.tang@suse.com> > > No objection from you? Sure! Please add Nicolas' SoB tag and your Suggested-by tag. I should have added them. :) > > (...) > > > diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c > > new file mode 100644 > > index 000000000000..d140979181f9 > > --- /dev/null > > +++ b/net/mptcp/bpf.c > > @@ -0,0 +1,17 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* Multipath TCP > > + * > > + * Copyright (c) 2022, SUSE. > > I understand your modification but is it OK if I re-add the previous > before the new one? > > Copyright (c) 2020, Tessares SA. > > (I would prefer to keep it not to forget about very small companies > contributing to the Linux kernel and I'm not sure it can be easily > replaced :) ) > > I can do these modifications when applying the patches if there are no > objections. I agree, please re-add: // SPDX-License-Identifier: GPL-2.0 /* Multipath TCP * * Copyright (c) 2020, Tessares SA. * * Author: Nicolas Rybowski <nicolas.rybowski@tessares.net> * */ Thanks, -Geliang > > Cheers, > Matt > -- > Tessares | Belgium | Hybrid Access Solutions > www.tessares.net >
© 2016 - 2026 Red Hat, Inc.