bpf: add MPTCP subflow support
Subsystems (7)
Nicolas Rybowski
v2 cover bpf: add MPTCP subflow support
Previously it was not possible to make a distinction between plain TCP sockets and MPTCP subflow sockets on the BPF_PROG_TYPE_SOCK_OPS hook.
This patch series now enables a fine control of subflow sockets. In its current state, it allows to put different sockopt on each subflow from a same MPTCP connection (socket mark, TCP congestion algorithm, ...) using BPF programs.
It should also be the basis of exposing MPTCP-specific fields through BPF.
v1 -> v2:
- add basic mandatory selftests for the new helper and is_mptcp field (Alexei)
- rebase on latest bpf-next
Nicolas Rybowski (5):
bpf: expose is_mptcp flag to bpf_tcp_sock
mptcp: attach subflow socket to parent cgroup
bpf: add 'bpf_mptcp_sock' structure and helper bpf:selftests: add MPTCP test base bpf:selftests: add bpf_mptcp_sock() verifier tests
include/linux/bpf.h | 33 +++++ include/uapi/linux/bpf.h | 15 +++ kernel/bpf/verifier.c | 30 +++++ net/core/filter.c | 13 +- net/mptcp/Makefile | 2 + net/mptcp/bpf.c | 72 +++++++++++ net/mptcp/subflow.c | 27 ++++ scripts/bpf_helpers_doc.py | 2 + tools/include/uapi/linux/bpf.h | 15 +++ tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/network_helpers.c | 37 +++++- tools/testing/selftests/bpf/network_helpers.h | 3 + .../testing/selftests/bpf/prog_tests/mptcp.c | 119 ++++++++++++++++++ tools/testing/selftests/bpf/progs/mptcp.c | 48 +++++++ tools/testing/selftests/bpf/verifier/sock.c | 63 ++++++++++ 15 files changed, 474 insertions(+), 6 deletions(-) create mode 100644 net/mptcp/bpf.c create mode 100644 tools/testing/selftests/bpf/prog_tests/mptcp.c create mode 100644 tools/testing/selftests/bpf/progs/mptcp.c
Signature · 1 line
2.28.0
Nicolas Rybowski
PATCH v2 1/5 bpf: expose is_mptcp flag to bpf_tcp_sock
is_mptcp is a field from struct tcp_sock used to indicate that the current tcp_sock is part of the MPTCP protocol.
In this protocol, a first socket (mptcp_sock) is created with sk_protocol set to IPPROTO_MPTCP (=262) for control purpose but it isn't directly on the wire. This is the role of the subflow (kernel) sockets which are classical tcp_sock with sk_protocol set to IPPROTO_TCP. The only way to differentiate such sockets from plain TCP sockets is the is_mptcp field from tcp_sock.
Such an exposure in BPF is thus required to be able to differentiate plain TCP sockets from MPTCP subflow sockets in BPF_PROG_TYPE_SOCK_OPS programs.
The choice has been made to silently pass the case when CONFIG_MPTCP is unset by defaulting is_mptcp to 0 in order to make BPF independent of the MPTCP configuration. Another solution is to make the verifier fail in 'bpf_tcp_sock_is_valid_ctx_access' but this will add an additional '#ifdef CONFIG_MPTCP' in the BPF code and a same injected BPF program will not run if MPTCP is not set.
An example use-case is provided in
https://github.com/multipath-tcp/mptcp_net-next/tree/scripts/bpf/examples
--- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 9 ++++++++- tools/include/uapi/linux/bpf.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +1 -0
@@ -4057,6 +4057,7 @@ struct bpf_tcp_sock {__u32 delivered;/* Total data packets delivered incl. rexmits */ __u32 delivered_ce;/* Like the above but only ECE marked packets */ __u32 icsk_retransmits;/* Number of unrecovered [RTO] timeouts */ +__u32 is_mptcp;/* Is MPTCP subflow? */ };struct bpf_sock_tuple {
net/core/filter.c +8 -1
@@ -5847,7 +5847,7 @@ bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, struct bpf_insn_access_aux *info) { if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, - icsk_retransmits)) + is_mptcp))return false;if (off % size !=0 )
@@ -5981,6 +5981,13 @@ u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,case offsetof(struct bpf_tcp_sock , icsk_retransmits):BPF_INET_SOCK_GET_COMMON (icsk_retransmits);break ; +case offsetof(struct bpf_tcp_sock , is_mptcp): +#ifdef CONFIG_MPTCP +BPF_TCP_SOCK_GET_COMMON (is_mptcp); +#else + *insn++ =BPF_MOV32_IMM (si->dst_reg,0 ); +#endif +break ; }return insn - insn_buf;
tools/include/uapi/linux/bpf.h +1 -0
@@ -4057,6 +4057,7 @@ struct bpf_tcp_sock {__u32 delivered;/* Total data packets delivered incl. rexmits */ __u32 delivered_ce;/* Like the above but only ECE marked packets */ __u32 icsk_retransmits;/* Number of unrecovered [RTO] timeouts */ +__u32 is_mptcp;/* Is MPTCP subflow? */ };struct bpf_sock_tuple {
Signature · 1 line
2.28.0
Nicolas Rybowski
PATCH v2 2/5 mptcp: attach subflow socket to parent cgroup
In reply to Nicolas RybowskiIt has been observed that the kernel sockets created for the subflows (except the first one) are not in the same cgroup as their parents. That's because the additional subflows are created by kernel workers.
This is a problem with eBPF programs attached to the parent's cgroup won't be executed for the children. But also with any other features of CGroup linked to a sk.
This patch fixes this behaviour.
As the subflow sockets are created by the kernel, we can't use 'mem_cgroup_sk_alloc' because of the current context being the one of the kworker. This is why we have to do low level memcg manipulation, if required.
--- net/mptcp/subflow.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
net/mptcp/subflow.c +27 -0
@@ -1130,6 +1130,30 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,return err; } +static void mptcp_attach_cgroup (struct sock *parent,struct sock *child) +{ +#ifdef CONFIG_SOCK_CGROUP_DATA +struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, + *child_skcd = &child->sk_cgrp_data; + +/* only the additional subflows created by kworkers have to be modified */ +if (cgroup_id (sock_cgroup_ptr (parent_skcd)) != +cgroup_id (sock_cgroup_ptr (child_skcd))) { +#ifdef CONFIG_MEMCG +struct mem_cgroup *memcg = parent->sk_memcg; + +mem_cgroup_sk_free (child); +if (memcg &&css_tryget (&memcg->css)) + child->sk_memcg = memcg; +#endif /* CONFIG_MEMCG */ + +cgroup_sk_free (child_skcd); + *child_skcd = *parent_skcd; +cgroup_sk_clone (child_skcd); + } +#endif /* CONFIG_SOCK_CGROUP_DATA */ +} +int mptcp_subflow_create_socket (struct sock *sk,struct socket **new_sock) {struct mptcp_subflow_context *subflow;
@@ -1150,6 +1174,9 @@ int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)lock_sock (sf->sk); +/* the newly created socket has to be in the same cgroup as its parent */ +mptcp_attach_cgroup (sk, sf->sk); +/* kernel sockets do not by default acquire net ref, but TCP timer * needs it. */
Signature · 1 line
2.28.0
Nicolas Rybowski
PATCH v2 3/5 bpf: add 'bpf_mptcp_sock' structure and helper
In reply to Nicolas RybowskiIn order to precisely identify the parent MPTCP connection of a subflow, it is required to access the mptcp_sock's token which uniquely identify a MPTCP connection.
This patch adds a new structure 'bpf_mptcp_sock' exposing the 'token' field of the 'mptcp_sock' extracted from a subflow's 'tcp_sock'. It also adds the declaration of a new BPF helper of the same name to expose the newly defined structure in the userspace BPF API.
This is the foundation to expose more MPTCP-specific fields through BPF.
Currently, it is limited to the field 'token' of the msk but it is easily extensible.
--- include/linux/bpf.h | 33 ++++++++++++++++ include/uapi/linux/bpf.h | 14 +++++++ kernel/bpf/verifier.c | 30 ++++++++++++++ net/core/filter.c | 4 ++ net/mptcp/Makefile | 2 + net/mptcp/bpf.c | 72 ++++++++++++++++++++++++++++++++++ scripts/bpf_helpers_doc.py | 2 + tools/include/uapi/linux/bpf.h | 14 +++++++ 8 files changed, 171 insertions(+) create mode 100644 net/mptcp/bpf.c
include/linux/bpf.h +33 -0
@@ -305,6 +305,7 @@ enum bpf_return_type {RET_PTR_TO_SOCK_COMMON_OR_NULL ,/* returns a pointer to a sock_common or NULL */ RET_PTR_TO_ALLOC_MEM_OR_NULL ,/* returns a pointer to dynamically allocated memory or NULL */ RET_PTR_TO_BTF_ID_OR_NULL ,/* returns a pointer to a btf_id or NULL */ + RET_PTR_TO_MPTCP_SOCK_OR_NULL, /* returns a pointer to mptcp_sock or NULL */ };/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
@@ -385,6 +386,8 @@ enum bpf_reg_type {PTR_TO_RDONLY_BUF_OR_NULL ,/* reg points to a readonly buffer or NULL */ PTR_TO_RDWR_BUF ,/* reg points to a read/write buffer */ PTR_TO_RDWR_BUF_OR_NULL ,/* reg points to a read/write buffer or NULL */ +PTR_TO_MPTCP_SOCK ,/* reg points to struct mptcp_sock */ + PTR_TO_MPTCP_SOCK_OR_NULL, /* reg points to struct mptcp_sock or NULL */ };/* The information passed from prog-specific *_is_valid_access
@@ -1785,6 +1788,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;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_copy_from_user_proto; +extern const struct bpf_func_proto bpf_mptcp_sock_proto;const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
@@ -1841,6 +1845,35 @@ struct sk_reuseport_kern {u32 reuseport_id;bool bind_inany; }; + +#ifdef CONFIG_MPTCP +bool bpf_mptcp_sock_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info); + +u32 bpf_mptcp_sock_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si, +struct bpf_insn *insn_buf, +struct bpf_prog *prog, +u32 *target_size); +#else /* CONFIG_MPTCP */ +static inline bool bpf_mptcp_sock_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info) +{ +return false; +} + +static inline u32 bpf_mptcp_sock_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si, +struct bpf_insn *insn_buf, +struct bpf_prog *prog, +u32 *target_size) +{ +return 0 ; +} +#endif /* CONFIG_MPTCP */ +bool bpf_tcp_sock_is_valid_access (int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux *info);
include/uapi/linux/bpf.h +14 -0
@@ -3576,6 +3576,15 @@ union bpf_attr {* the data in *dst*. This is a wrapper of copy_from_user(). * Return * 0 on success, or a negative error in case of failure. +* +* struct bpf_mptcp_sock *bpf_mptcp_sock(struct bpf_sock *sk) +* Description +* This helper gets a **struct bpf_mptcp_sock** pointer from a +* **struct bpf_sock** pointer. +* Return +* A **struct bpf_mptcp_sock** pointer on success, or **NULL** in +* case of failure. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3727,6 +3736,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (mptcp_sock), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4060,6 +4070,10 @@ struct bpf_tcp_sock {__u32 is_mptcp;/* Is MPTCP subflow? */ }; +struct bpf_mptcp_sock { +__u32 token;/* msk token */ +}; + struct bpf_sock_tuple { union { struct {
kernel/bpf/verifier.c +30 -0
@@ -392,6 +392,7 @@ static bool type_is_sk_pointer(enum bpf_reg_type type)return type ==PTR_TO_SOCKET || type ==PTR_TO_SOCK_COMMON || type ==PTR_TO_TCP_SOCK || + type ==PTR_TO_MPTCP_SOCK || type ==PTR_TO_XDP_SOCK ; }
@@ -399,6 +400,7 @@ static bool reg_type_not_null(enum bpf_reg_type type) {return type ==PTR_TO_SOCKET || type ==PTR_TO_TCP_SOCK || + type ==PTR_TO_MPTCP_SOCK || type ==PTR_TO_MAP_VALUE || type ==PTR_TO_SOCK_COMMON ; }
@@ -409,6 +411,7 @@ static bool reg_type_may_be_null(enum bpf_reg_type type) type ==PTR_TO_SOCKET_OR_NULL || type ==PTR_TO_SOCK_COMMON_OR_NULL || type ==PTR_TO_TCP_SOCK_OR_NULL || + type ==PTR_TO_MPTCP_SOCK_OR_NULL || type ==PTR_TO_BTF_ID_OR_NULL || type ==PTR_TO_MEM_OR_NULL || type == PTR_TO_RDONLY_BUF_OR_NULL ||
@@ -427,6 +430,8 @@ static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) type ==PTR_TO_SOCKET_OR_NULL || type ==PTR_TO_TCP_SOCK || type ==PTR_TO_TCP_SOCK_OR_NULL || + type ==PTR_TO_MPTCP_SOCK || + type ==PTR_TO_MPTCP_SOCK_OR_NULL || type ==PTR_TO_MEM || type ==PTR_TO_MEM_OR_NULL ; }
@@ -500,6 +505,8 @@ static const char * const reg_type_str[] = { [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", [PTR_TO_TCP_SOCK] = "tcp_sock", [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", + [PTR_TO_MPTCP_SOCK] = "mptcp_sock", + [PTR_TO_MPTCP_SOCK_OR_NULL] = "mptcp_sock_or_null", [PTR_TO_TP_BUFFER] = "tp_buffer", [PTR_TO_XDP_SOCK] = "xdp_sock", [PTR_TO_BTF_ID] = "ptr_",
@@ -2177,6 +2184,8 @@ static bool is_spillable_regtype(enum bpf_reg_type type)case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK :case PTR_TO_TCP_SOCK_OR_NULL : +case PTR_TO_MPTCP_SOCK : +case PTR_TO_MPTCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :case PTR_TO_BTF_ID :case PTR_TO_BTF_ID_OR_NULL :
@@ -2786,6 +2795,9 @@ static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,case PTR_TO_TCP_SOCK : valid =bpf_tcp_sock_is_valid_access (off, size, t, &info);break ; +case PTR_TO_MPTCP_SOCK : + valid =bpf_mptcp_sock_is_valid_access (off, size, t, &info); +break ;case PTR_TO_XDP_SOCK : valid =bpf_xdp_sock_is_valid_access (off, size, t, &info);break ;
@@ -2944,6 +2956,9 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,case PTR_TO_TCP_SOCK : pointer_desc ="tcp_sock " ;break ; +case PTR_TO_MPTCP_SOCK : + pointer_desc ="mptcp_sock " ; +break ;case PTR_TO_XDP_SOCK : pointer_desc ="xdp_sock " ;break ;
@@ -4997,6 +5012,10 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insnmark_reg_known_zero (env, regs,BPF_REG_0 ); regs[BPF_REG_0 ].type =PTR_TO_TCP_SOCK_OR_NULL ; regs[BPF_REG_0 ].id = ++env->id_gen; + }else if (fn->ret_type ==RET_PTR_TO_MPTCP_SOCK_OR_NULL ) { +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_MPTCP_SOCK_OR_NULL ; + regs[BPF_REG_0 ].id = ++env->id_gen; }else if (fn->ret_type ==RET_PTR_TO_ALLOC_MEM_OR_NULL ) {mark_reg_known_zero (env, regs,BPF_REG_0 ); regs[BPF_REG_0 ].type =PTR_TO_MEM_OR_NULL ;
@@ -5324,6 +5343,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK :case PTR_TO_TCP_SOCK_OR_NULL : +case PTR_TO_MPTCP_SOCK : +case PTR_TO_MPTCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :verbose (env,"R%d pointer arithmetic on %s prohibited\n" , dst, reg_type_str[ptr_reg->type]);
@@ -7044,6 +7065,8 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state, reg->type =PTR_TO_SOCK_COMMON ; }else if (reg->type ==PTR_TO_TCP_SOCK_OR_NULL ) { reg->type =PTR_TO_TCP_SOCK ; + }else if (reg->type ==PTR_TO_MPTCP_SOCK_OR_NULL ) { + reg->type =PTR_TO_MPTCP_SOCK ; }else if (reg->type ==PTR_TO_BTF_ID_OR_NULL ) { reg->type =PTR_TO_BTF_ID ; }else if (reg->type ==PTR_TO_MEM_OR_NULL ) {
@@ -8407,6 +8430,8 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK :case PTR_TO_TCP_SOCK_OR_NULL : +case PTR_TO_MPTCP_SOCK : +case PTR_TO_MPTCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :/* Only valid matches are exact, which memcmp() above * would have accepted
@@ -8934,6 +8959,8 @@ static bool reg_type_mismatch_ok(enum bpf_reg_type type)case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK :case PTR_TO_TCP_SOCK_OR_NULL : +case PTR_TO_MPTCP_SOCK : +case PTR_TO_MPTCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :case PTR_TO_BTF_ID :case PTR_TO_BTF_ID_OR_NULL :
@@ -10072,6 +10099,9 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)case PTR_TO_TCP_SOCK : convert_ctx_access = bpf_tcp_sock_convert_ctx_access;break ; +case PTR_TO_MPTCP_SOCK : + convert_ctx_access = bpf_mptcp_sock_convert_ctx_access; +break ;case PTR_TO_XDP_SOCK : convert_ctx_access = bpf_xdp_sock_convert_ctx_access;break ;
net/core/filter.c +4 -0
@@ -6900,6 +6900,10 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_tcp_sock:return &bpf_tcp_sock_proto;#endif /* CONFIG_INET */ +#ifdef CONFIG_MPTCP +case BPF_FUNC_mptcp_sock: +return &bpf_mptcp_sock_proto; +#endif /* CONFIG_MPTCP */ default :return bpf_base_func_proto (func_id); }
net/mptcp/Makefile +2 -0
@@ -10,3 +10,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_TESTS) += mptcp_crypto_test.o mptcp_token_test.o + +obj-$(CONFIG_BPF) += bpf.o
net/mptcp/bpf.c new file +72 -0
72 lines not on this page · raw message
scripts/bpf_helpers_doc.py +2 -0
@@ -428,6 +428,7 @@ class PrinterHelpers(Printer): 'struct tcp_request_sock', 'struct udp6_sock', 'struct task_struct', + 'struct bpf_mptcp_sock', 'struct __sk_buff', 'struct sk_msg_md',
@@ -474,6 +475,7 @@ class PrinterHelpers(Printer): 'struct udp6_sock', 'struct task_struct', 'struct path', + 'struct bpf_mptcp_sock', } mapped_types = { 'u8': '__u8',
tools/include/uapi/linux/bpf.h +14 -0
@@ -3576,6 +3576,15 @@ union bpf_attr {* the data in *dst*. This is a wrapper of copy_from_user(). * Return * 0 on success, or a negative error in case of failure. +* +* struct bpf_mptcp_sock *bpf_mptcp_sock(struct bpf_sock *sk) +* Description +* This helper gets a **struct bpf_mptcp_sock** pointer from a +* **struct bpf_sock** pointer. +* Return +* A **struct bpf_mptcp_sock** pointer on success, or **NULL** in +* case of failure. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3727,6 +3736,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (mptcp_sock), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4060,6 +4070,10 @@ struct bpf_tcp_sock {__u32 is_mptcp;/* Is MPTCP subflow? */ }; +struct bpf_mptcp_sock { +__u32 token;/* msk token */ +}; + struct bpf_sock_tuple { union { struct {
Signature · 1 line
2.28.0
Nicolas Rybowski
PATCH v2 4/5 bpf:selftests: add MPTCP test base
In reply to Nicolas RybowskiThis patch adds a base for MPTCP specific tests.
It is currently limited to the is_mptcp field in case of plain TCP connection because there is no easy way to get the subflow sk from a msk in userspace. This implies that we cannot lookup the sk_storage attached to the subflow sk in the sockops program.
---
Notes:
v1 -> v2:
- new patch: mandatory selftests (Alexei)
tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/network_helpers.c | 37 +++++- tools/testing/selftests/bpf/network_helpers.h | 3 + .../testing/selftests/bpf/prog_tests/mptcp.c | 119 ++++++++++++++++++ tools/testing/selftests/bpf/progs/mptcp.c | 48 +++++++ 5 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/mptcp.c create mode 100644 tools/testing/selftests/bpf/progs/mptcp.c
tools/testing/selftests/bpf/config +1 -0
@@ -39,3 +39,4 @@ CONFIG_BPF_JIT=y CONFIG_BPF_LSM=y CONFIG_SECURITY=y CONFIG_LIRC=y +CONFIG_MPTCP=y
tools/testing/selftests/bpf/network_helpers.c +32 -5
@@ -14,6 +14,10 @@#include "bpf_util.h" #include "network_helpers.h" +#ifndef IPPROTO_MPTCP +#define IPPROTO_MPTCP 262 +#endif +#define clean_errno () (errno == 0 ? "None" : strerror(errno))#define log_err (MSG , ...) ({ \ int __save = errno; \
@@ -66,8 +70,8 @@ static int settimeo(int fd, int timeout_ms)#define save_errno_close (fd) ({ int __save = errno; close(fd); errno = __save; }) -int start_server (int family,int type,const char *addr_str,__u16 port, -int timeout_ms) +static int start_server_proto (int family,int type,int protocol, +const char *addr_str,__u16 port,int timeout_ms) {struct sockaddr_storage addr = {};socklen_t len;
@@ -76,7 +80,7 @@ int start_server(int family, int type, const char *addr_str, __u16 port,if (make_sockaddr (family, addr_str, port, &addr, &len))return -1 ; - fd =socket (family, type,0 ); + fd =socket (family, type, protocol);if (fd <0 ) {log_err ("Failed to create server socket" );return -1 ;
@@ -104,6 +108,19 @@ int start_server(int family, int type, const char *addr_str, __u16 port,return -1 ; } +int start_server (int family,int type,const char *addr_str,__u16 port, +int timeout_ms) +{ +return start_server_proto (family, type,0 , addr_str, port, timeout_ms); +} + +int start_mptcp_server (int family,const char *addr_str,__u16 port, +int timeout_ms) +{ +return start_server_proto (family,SOCK_STREAM ,IPPROTO_MPTCP , addr_str, + port, timeout_ms); +} +int fastopen_connect (int server_fd,const char *data,unsigned int data_len,int timeout_ms) {
@@ -153,7 +170,7 @@ static int connect_fd_to_addr(int fd,return 0 ; } -int connect_to_fd (int server_fd,int timeout_ms) +static int connect_to_fd_proto (int server_fd,int protocol,int timeout_ms) {struct sockaddr_storage addr;struct sockaddr_in *addr_in;
@@ -173,7 +190,7 @@ int connect_to_fd(int server_fd, int timeout_ms) } addr_in = (struct sockaddr_in *)&addr; - fd =socket (addr_in->sin_family, type,0 ); + fd =socket (addr_in->sin_family, type, protocol);if (fd <0 ) {log_err ("Failed to create client socket" );return -1 ;
@@ -192,6 +209,16 @@ int connect_to_fd(int server_fd, int timeout_ms)return -1 ; } +int connect_to_fd (int server_fd,int timeout_ms) +{ +return connect_to_fd_proto (server_fd,0 , timeout_ms); +} + +int connect_to_mptcp_fd (int server_fd,int timeout_ms) +{ +return connect_to_fd_proto (server_fd,IPPROTO_MPTCP , timeout_ms); +} +int connect_fd_to_fd (int client_fd,int server_fd,int timeout_ms) {struct sockaddr_storage addr;
tools/testing/selftests/bpf/network_helpers.h +3 -0
@@ -35,7 +35,10 @@ extern struct ipv6_packet pkt_v6;int start_server (int family,int type,const char *addr,__u16 port,int timeout_ms); +int start_mptcp_server (int family,const char *addr,__u16 port, +int timeout_ms);int connect_to_fd (int server_fd,int timeout_ms); +int connect_to_mptcp_fd (int server_fd,int timeout_ms);int connect_fd_to_fd (int client_fd,int server_fd,int timeout_ms);int fastopen_connect (int server_fd,const char *data,unsigned int data_len,int timeout_ms);
tools/testing/selftests/bpf/prog_tests/mptcp.c new file +119 -0
119 lines not on this page · raw message
tools/testing/selftests/bpf/progs/mptcp.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.28.0
Matthieu Baerts
[MPTCP] Re: [PATCH mptcp-next v2 4/5] bpf:selftests: add MPTCP test base
Hi Nicolas,
Quoted 9 lines · Nicolas Rybowski
This patch adds a base for MPTCP specific tests.
It is currently limited to the
is_mptcpfield in case of plain TCP connection because there is no easy way to get the subflow sk from a msk in userspace. This implies that we cannot lookup thesk_storageattached to the subflow sk in the sockops program.Acked-by Matthieu Baerts <matthieu.baerts(a)tessares.net>Signed-off-by Nicolas Rybowski <nicolas.rybowski(a)tessares.net>
(...)
Quoted 4 lines
tools/testing/selftests/bpf/network_helpers.c +0
-0
(...)
tools/testing/selftests/bpf/network_helpers.c static int settimeo(int fd, int timeout_ms) · line 74
Quoted 8 lines
tools/testing/selftests/bpf/network_helpers.c +2
-2@@ -66,8 +70,8 @@ static int settimeo(int fd, int timeout_ms)#define save_errno_close (fd) ({ int __save = errno; close(fd); errno = __save; }) -int start_server (int family,int type,const char *addr_str,__u16 port, -int timeout_ms) +static int start_server_proto (int family,int type,int protocol, +const char *addr_str,__u16 port,int timeout_ms)
Here I add to remove an extra space (before const) to satisfy checkpatch.pl.
(...)
Quoted 21 lines
tools/testing/selftests/bpf/progs/mptcp.c new file +15
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1; + +struct mptcp_storage { +__u32 invoked; +__u32 is_mptcp; +}; + +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC);
tools/testing/selftests/bpf/progs/mptcp.c line 25
Quoted 10 lines
tools/testing/selftests/bpf/progs/mptcp.c new file +10
-0+__type (key, int); + __type(value, struct mptcp_storage); +} socket_storage_map SEC(".maps"); + +SEC("sockops") +int _sockops (struct bpf_sock_ops *ctx) +{ +struct mptcp_storage *storage; +struct bpf_tcp_sock *tcp_sk; +int op = (int ) ctx->op;
Here I had to remove the space between (int) and ctx.
Cheers,
Matt
Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions www.tessares.net
Nicolas Rybowski
PATCH v2 5/5 bpf:selftests: add bpf_mptcp_sock() verifier tests
In reply to Nicolas RybowskiThis patch adds verifier side tests for the new bpf_mptcp_sock() helper.
Currently, there is no easy way to test the token field since we cannot get back the mptcp_sock, this could be a future amelioration.
---
Notes:
v1 -> v2:
- new patch: mandatory selftests (Alexei)
tools/testing/selftests/bpf/verifier/sock.c | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+)
tools/testing/selftests/bpf/verifier/sock.c +63 -0
@@ -631,3 +631,66 @@ .prog_type = BPF_PROG_TYPE_SK_REUSEPORT, .result = ACCEPT, }, +{ + "bpf_mptcp_sock(skops->sk): no !skops->sk check", + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 , offsetof(struct bpf_sock_ops , sk)), +BPF_EMIT_CALL (BPF_FUNC_mptcp_sock), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SOCK_OPS , + .result = REJECT, + .errstr = "type=sock_or_null expected=sock_common", +}, +{ +"bpf_mptcp_sock(skops->sk): no NULL check on ret" , + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 , offsetof(struct bpf_sock_ops , sk)), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_1 ,0 ,2 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), +BPF_EMIT_CALL (BPF_FUNC_mptcp_sock), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_0 , offsetof(struct bpf_mptcp_sock , token)), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SOCK_OPS , + .result = REJECT, + .errstr = "invalid mem access 'mptcp_sock_or_null'", +}, +{ +"bpf_mptcp_sock(skops->sk): msk->token" , + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 , offsetof(struct bpf_sock_ops , sk)), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_1 ,0 ,2 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), +BPF_EMIT_CALL (BPF_FUNC_mptcp_sock), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_0 , offsetof(struct bpf_mptcp_sock , token)), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SOCK_OPS , + .result = ACCEPT, +}, +{ +"bpf_mptcp_sock(skops->sk): msk->token cannot be modified" , + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 , offsetof(struct bpf_sock_ops , sk)), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_1 ,0 ,2 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), +BPF_EMIT_CALL (BPF_FUNC_mptcp_sock), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_ST_MEM (BPF_W ,BPF_REG_0 , offsetof(struct bpf_mptcp_sock , token),0x2a ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SOCK_OPS , + .result = REJECT, + .errstr = "cannot write into mptcp_sock", +},
Signature · 1 line
2.28.0
Matthieu Baerts
Hi Nicolas,
Quoted 9 lines · Nicolas Rybowski
Previously it was not possible to make a distinction between plain TCP sockets and MPTCP subflow sockets on the
BPF_PROG_TYPE_SOCK_OPShook.This patch series now enables a fine control of subflow sockets. In its current state, it allows to put different sockopt on each subflow from a same MPTCP connection (socket mark, TCP congestion algorithm, ...) using BPF programs.
It should also be the basis of exposing MPTCP-specific fields through BPF.
Thank you for the v2. I just added these patches at the beginning of the tree. I did two small modifications, nothing big, see patch 4/5 for more details.
CI + export are in progress.
Cheers,
Matt
Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions www.tessares.net