From: Geliang Tang <geliang.tang@suse.com>
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.
Co-developed-by: Nicolas Rybowski <nicolas.rybowski@tessares.net>
Signed-off-by: Nicolas Rybowski <nicolas.rybowski@tessares.net>
Co-developed-by: Matthieu Baerts <matthieu.baerts@tessares.net>
Signed-off-by: Matthieu Baerts <matthieu.baerts@tessares.net>
Signed-off-by: Geliang Tang <geliang.tang@suse.com>
Signed-off-by: Mat Martineau <mathew.j.martineau@linux.intel.com>
---
include/linux/btf_ids.h | 3 ++-
include/net/mptcp.h | 6 ++++++
include/uapi/linux/bpf.h | 7 +++++++
net/core/filter.c | 17 +++++++++++++++++
net/mptcp/Makefile | 4 ++++
net/mptcp/bpf.c | 22 ++++++++++++++++++++++
scripts/bpf_doc.py | 2 ++
tools/include/uapi/linux/bpf.h | 7 +++++++
8 files changed, 67 insertions(+), 1 deletion(-)
create mode 100644 net/mptcp/bpf.c
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/net/mptcp.h b/include/net/mptcp.h
index 0a3b0fb04a3b..5b3a6f783182 100644
--- a/include/net/mptcp.h
+++ b/include/net/mptcp.h
@@ -283,4 +283,10 @@ static inline int mptcpv6_init(void) { return 0; }
static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
#endif
+#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_JIT) && 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
+
#endif /* __NET_MPTCP_H */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 9ef1f3e1c22f..785f2cb15495 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -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
diff --git a/net/core/filter.c b/net/core/filter.c
index 7b1867f1f422..4081c55f6f78 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -78,6 +78,7 @@
#include <linux/btf_ids.h>
#include <net/tls.h>
#include <net/xdp.h>
+#include <net/mptcp.h>
static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id);
@@ -11279,6 +11280,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);
@@ -11321,6 +11335,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:
diff --git a/net/mptcp/Makefile b/net/mptcp/Makefile
index e54daceac58b..168c55d1c917 100644
--- a/net/mptcp/Makefile
+++ b/net/mptcp/Makefile
@@ -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
diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c
new file mode 100644
index 000000000000..535602ba2582
--- /dev/null
+++ b/net/mptcp/bpf.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Multipath TCP
+ *
+ * Copyright (c) 2020, Tessares SA.
+ * Copyright (c) 2022, SUSE.
+ *
+ * Author: Nicolas Rybowski <nicolas.rybowski@tessares.net>
+ */
+
+#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);
diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index 096625242475..d5452f7eb996 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -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',
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 9ef1f3e1c22f..785f2cb15495 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -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
--
2.36.0
On 4/21/22 12:24 AM, Mat Martineau wrote:
[...]
> static const struct bpf_func_proto *
> bpf_sk_base_func_proto(enum bpf_func_id func_id);
> @@ -11279,6 +11280,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 CI (https://github.com/kernel-patches/bpf/runs/6136052684?check_suite_focus=true) fails with:
#7 base:FAIL
libbpf: prog '_sockops': BPF program load failed: Invalid argument
libbpf: prog '_sockops': -- BEGIN PROG LOAD LOG --
0: R1=ctx(off=0,imm=0) R10=fp0
; int op = (int)ctx->op;
0: (61) r2 = *(u32 *)(r1 +0) ; R1=ctx(off=0,imm=0) R2_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff))
; if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
1: (56) if w2 != 0x3 goto pc+50 ; R2_w=3
; sk = ctx->sk;
2: (79) r6 = *(u64 *)(r1 +184) ; R1=ctx(off=0,imm=0) R6_w=sock_or_null(id=1,off=0,imm=0)
; if (!sk)
3: (15) if r6 == 0x0 goto pc+48 ; R6_w=sock(off=0,imm=0)
; tcp_sk = bpf_tcp_sock(sk);
4: (bf) r1 = r6 ; R1_w=sock(off=0,imm=0) R6_w=sock(off=0,imm=0)
5: (85) call bpf_tcp_sock#96 ; R0_w=tcp_sock_or_null(id=2,off=0,imm=0)
6: (bf) r7 = r0 ; R0=tcp_sock_or_null(id=2,off=0,imm=0) R7=tcp_sock_or_null(id=2,off=0,imm=0)
; if (!tcp_sk)
7: (15) if r7 == 0x0 goto pc+44 ; R7=tcp_sock(off=0,imm=0)
; if (!tcp_sk->is_mptcp) {
8: (61) r1 = *(u32 *)(r7 +112) ; R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) R7=tcp_sock(off=0,imm=0)
; if (!tcp_sk->is_mptcp) {
9: (56) if w1 != 0x0 goto pc+14 24: R0=tcp_sock(off=0,imm=0) R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) R6=sock(off=0,imm=0) R7=tcp_sock(off=0,imm=0) R10=fp0
; msk = bpf_skc_to_mptcp_sock(sk);
24: (bf) r1 = r6 ; R1_w=sock(off=0,imm=0) R6=sock(off=0,imm=0)
25: (85) call bpf_skc_to_mptcp_sock#194
invalid return type 8 of func bpf_skc_to_mptcp_sock#194
processed 34 insns (limit 1000000) max_states_per_insn 0 total_states 3 peak_states 3 mark_read 1
-- END PROG LOAD LOG --
libbpf: failed to load program '_sockops'
libbpf: failed to load object './mptcp_sock.o'
run_test:FAIL:165
test_base:FAIL:227
(network_helpers.c:88: errno: Protocol not supported) Failed to create server socket
test_base:FAIL:241
RTNETLINK answers: No such file or directory
Error talking to the kernel
[...]
Looking at bpf_skc_to_tcp6_sock(), do we similarly need a BTF_TYPE_EMIT() here?
Thanks,
Daniel
On Mon, 25 Apr 2022, Daniel Borkmann wrote:
> On 4/21/22 12:24 AM, Mat Martineau wrote:
> [...]
>> static const struct bpf_func_proto *
>> bpf_sk_base_func_proto(enum bpf_func_id func_id);
>> @@ -11279,6 +11280,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 CI
> (https://github.com/kernel-patches/bpf/runs/6136052684?check_suite_focus=true)
> fails with:
>
> #7 base:FAIL
> libbpf: prog '_sockops': BPF program load failed: Invalid argument
> libbpf: prog '_sockops': -- BEGIN PROG LOAD LOG --
> 0: R1=ctx(off=0,imm=0) R10=fp0
> ; int op = (int)ctx->op;
> 0: (61) r2 = *(u32 *)(r1 +0) ; R1=ctx(off=0,imm=0)
> R2_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff))
> ; if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
> 1: (56) if w2 != 0x3 goto pc+50 ; R2_w=3
> ; sk = ctx->sk;
> 2: (79) r6 = *(u64 *)(r1 +184) ; R1=ctx(off=0,imm=0)
> R6_w=sock_or_null(id=1,off=0,imm=0)
> ; if (!sk)
> 3: (15) if r6 == 0x0 goto pc+48 ; R6_w=sock(off=0,imm=0)
> ; tcp_sk = bpf_tcp_sock(sk);
> 4: (bf) r1 = r6 ; R1_w=sock(off=0,imm=0)
> R6_w=sock(off=0,imm=0)
> 5: (85) call bpf_tcp_sock#96 ;
> R0_w=tcp_sock_or_null(id=2,off=0,imm=0)
> 6: (bf) r7 = r0 ;
> R0=tcp_sock_or_null(id=2,off=0,imm=0) R7=tcp_sock_or_null(id=2,off=0,imm=0)
> ; if (!tcp_sk)
> 7: (15) if r7 == 0x0 goto pc+44 ; R7=tcp_sock(off=0,imm=0)
> ; if (!tcp_sk->is_mptcp) {
> 8: (61) r1 = *(u32 *)(r7 +112) ;
> R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff))
> R7=tcp_sock(off=0,imm=0)
> ; if (!tcp_sk->is_mptcp) {
> 9: (56) if w1 != 0x0 goto pc+14 24: R0=tcp_sock(off=0,imm=0)
> R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) R6=sock(off=0,imm=0)
> R7=tcp_sock(off=0,imm=0) R10=fp0
> ; msk = bpf_skc_to_mptcp_sock(sk);
> 24: (bf) r1 = r6 ; R1_w=sock(off=0,imm=0)
> R6=sock(off=0,imm=0)
> 25: (85) call bpf_skc_to_mptcp_sock#194
> invalid return type 8 of func bpf_skc_to_mptcp_sock#194
> processed 34 insns (limit 1000000) max_states_per_insn 0 total_states 3
> peak_states 3 mark_read 1
> -- END PROG LOAD LOG --
> libbpf: failed to load program '_sockops'
> libbpf: failed to load object './mptcp_sock.o'
> run_test:FAIL:165
> test_base:FAIL:227
> (network_helpers.c:88: errno: Protocol not supported) Failed to create
> server socket
> test_base:FAIL:241
> RTNETLINK answers: No such file or directory
> Error talking to the kernel
> [...]
>
> Looking at bpf_skc_to_tcp6_sock(), do we similarly need a BTF_TYPE_EMIT()
> here?
>
Geliang, in addition to the BTF_TYPE_EMIT() can you also take a look at
the places in kernel/trace/bpf_trace.c and kernel/bpf/verifier.c where
bpf_skc_to_tcp6_sock and bpf_skc_to_tcp6_sock_proto are referenced?
--
Mat Martineau
Intel
On 4/25/22 4:26 PM, Daniel Borkmann wrote: [...] > > Looking at bpf_skc_to_tcp6_sock(), do we similarly need a BTF_TYPE_EMIT() here? (Plus, CONFIG_MPTCP should be enabled in CI config (Andrii).) Cheers, Daniel
On Mon, Apr 25, 2022 at 7:29 AM Daniel Borkmann <daniel@iogearbox.net> wrote: > > On 4/25/22 4:26 PM, Daniel Borkmann wrote: > [...] > > > > Looking at bpf_skc_to_tcp6_sock(), do we similarly need a BTF_TYPE_EMIT() here? > > (Plus, CONFIG_MPTCP should be enabled in CI config (Andrii).) Should be done once [0] is merged in. [0] https://github.com/kernel-patches/vmtest/pull/77 > > Cheers, > Daniel
On 4/21/22 12:24 AM, Mat Martineau wrote:
[...]
> diff --git a/include/net/mptcp.h b/include/net/mptcp.h
> index 0a3b0fb04a3b..5b3a6f783182 100644
> --- a/include/net/mptcp.h
> +++ b/include/net/mptcp.h
> @@ -283,4 +283,10 @@ static inline int mptcpv6_init(void) { return 0; }
> static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
> #endif
>
> +#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_JIT) && 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
> +
Where is this relevant to JIT specifically?
Thanks,
Daniel
On Mon, 25 Apr 2022, Daniel Borkmann wrote:
> On 4/21/22 12:24 AM, Mat Martineau wrote:
> [...]
>> diff --git a/include/net/mptcp.h b/include/net/mptcp.h
>> index 0a3b0fb04a3b..5b3a6f783182 100644
>> --- a/include/net/mptcp.h
>> +++ b/include/net/mptcp.h
>> @@ -283,4 +283,10 @@ static inline int mptcpv6_init(void) { return 0; }
>> static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) {
>> }
>> #endif
>> +#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_JIT) &&
>> 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
>> +
>
> Where is this relevant to JIT specifically?
>
That's carried over from the build conditions for bpf_tcp_ca.c in
net/ipv4/Makefile:
ifeq ($(CONFIG_BPF_JIT),y)
obj-$(CONFIG_BPF_SYSCALL) += bpf_tcp_ca.o
endif
Looks like the reasoning for that (in the CA code) is the use of
bpf_struct_ops in bpf_tcp_ca.c
While this patch series for MPTCP does not use bpf_struct_ops, and JIT is
not necessary for bpf_mptcp_sock_from_subflow(), the upcoming MPTCP
scheduler-in-BPF patches do use bpf_struct_ops. So that dependency found
its way in to this series - but now that you point it out,
bpf_mptcp_sock_from_subflow() shouldn't be limited by CONFIG_BPF_JIT and
we can separately check for the JIT dependency for the scheduler code.
Will fix that in v2.
--
Mat Martineau
Intel
© 2016 - 2026 Red Hat, Inc.