bpf: More sock_ops callbacks
Subsystems (8)
v6 cover bpf: More sock_ops callbacks
This patchset adds support for:
- direct R or R/W access to many tcp_sock fields
- passing up to 4 arguments to sock_ops BPF functions
- tcp_sock field bpf_sock_ops_cb_flags for controlling callbacks
- optionally calling sock_ops BPF program when RTO fires
- optionally calling sock_ops BPF program when packet is retransmitted
- optionally calling sock_ops BPF program when TCP state changes
- access to tclass and sk_txhash
- new selftest
v2: Fixed commit message 0/11. The commit is to "bpf-next" but the patch
below used "bpf" and Patchwork didn't work correctly.
v3: Cleaned RTO callback as per Yuchung's comment
Added BPF enum for TCP states as per Alexei's comment
v4: Fixed compile warnings related to detecting changes between TCP
internal states and the BPF defined states.
v5: Fixed comment issues in some selftest files
Fixed accesss issue with u64 fields in bpf_sock_ops struct
v6: Made fixes based on comments form Eric Dumazet:
The field bpf_sock_ops_cb_flags was addded in a hole on 64bit kernels Field bpf_sock_ops_cb_flags is now set through a helper function which returns an error when a BPF program tries to set bits for callbacks that are not supported in the current kernel. Added a comment indicating that when adding fields to bpf_sock_ops_kern they should be added before the field named "temp" if they need to be cleared before calling the BPF function.
Consists of the following patches:
[PATCH bpf-next v6 01/11] bpf: Make SOCK_OPS_GET_TCP size independent [PATCH bpf-next v6 02/11] bpf: Make SOCK_OPS_GET_TCP struct [PATCH bpf-next v6 03/11] bpf: Add write access to tcp_sock and sock [PATCH bpf-next v6 04/11] bpf: Support passing args to sock_ops bpf [PATCH bpf-next v6 05/11] bpf: Adds field bpf_sock_ops_cb_flags to [PATCH bpf-next v6 06/11] bpf: Add sock_ops RTO callback [PATCH bpf-next v6 07/11] bpf: Add support for reading sk_state and [PATCH bpf-next v6 08/11] bpf: Add sock_ops R/W access to tclass & [PATCH bpf-next v6 09/11] bpf: Add BPF_SOCK_OPS_RETRANS_CB [PATCH bpf-next v6 10/11] bpf: Add BPF_SOCK_OPS_STATE_CB [PATCH bpf-next v6 11/11] bpf: add selftest for tcpbpf
include/linux/filter.h | 10 ++ include/linux/tcp.h | 11 ++ include/net/tcp.h | 66 +++++++- include/uapi/linux/bpf.h | 72 +++++++- include/uapi/linux/tcp.h | 8 + net/core/filter.c | 281 +++++++++++++++++++++++++++++--- net/ipv4/tcp.c | 26 ++- net/ipv4/tcp_nv.c | 2 +- net/ipv4/tcp_output.c | 5 +- net/ipv4/tcp_timer.c | 7 + tools/include/uapi/linux/bpf.h | 74 ++++++++- tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 2 + tools/testing/selftests/bpf/tcp_client.py | 52 ++++++ tools/testing/selftests/bpf/tcp_server.py | 79 +++++++++ tools/testing/selftests/bpf/test_tcpbpf.h | 16 ++ tools/testing/selftests/bpf/test_tcpbpf_kern.c | 131 +++++++++++++++ tools/testing/selftests/bpf/test_tcpbpf_user.c | 126 ++++++++++++++ 18 files changed, 933 insertions(+), 39 deletions(-)
PATCH v6 1/11 bpf: Make SOCK_OPS_GET_TCP size independent
Make SOCK_OPS_GET_TCP helper macro size independent (before only worked with 4-byte fields.
--- net/core/filter.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
net/core/filter.c +8 -5
@@ -4465,9 +4465,10 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,break ;/* Helper macro for adding read access to tcp_sock fields. */ -#define SOCK_OPS_GET_TCP32 (FIELD_NAME ) \ +#define SOCK_OPS_GET_TCP (FIELD_NAME ) \ do { \ - BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD_NAME) != 4); \ + BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD_NAME) > \ + FIELD_SIZEOF(struct bpf_sock_ops, FIELD_NAME)); \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, \ is_fullsock), \
@@ -4479,16 +4480,18 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, struct bpf_sock_ops_kern, sk),\ si->dst_reg, si->src_reg, \ offsetof(struct bpf_sock_ops_kern, sk));\ - *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, \ + *insn++ = BPF_LDX_MEM(FIELD_SIZEOF(struct tcp_sock, \ +FIELD_NAME ), si->dst_reg, \ + si->dst_reg, \ offsetof(struct tcp_sock , FIELD_NAME)); \ }while (0 )case offsetof(struct bpf_sock_ops , snd_cwnd): -SOCK_OPS_GET_TCP32 (snd_cwnd); +SOCK_OPS_GET_TCP (snd_cwnd);break ;case offsetof(struct bpf_sock_ops , srtt_us): -SOCK_OPS_GET_TCP32 (srtt_us); +SOCK_OPS_GET_TCP (srtt_us);break ; }return insn - insn_buf;
Signature · 1 line
2.9.5
PATCH v6 2/11 bpf: Make SOCK_OPS_GET_TCP struct independent
In reply to Lawrence BrakmoChanged SOCK_OPS_GET_TCP to SOCK_OPS_GET_FIELD and added 2 arguments so now it can also work with struct sock fields. The first argument is the name of the field in the bpf_sock_ops struct, the 2nd argument is the name of the field in the OBJ struct.
Previous: SOCK_OPS_GET_TCP(FIELD_NAME) New: SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ)
Where OBJ is either "struct tcp_sock" or "struct sock" (without quotation). BPF_FIELD is the name of the field in the bpf_sock_ops struct and OBJ_FIELD is the name of the field in the OBJ struct.
Although the field names are currently the same, the kernel struct names could change in the future and this change makes it easier to support that.
Note that adding access to tcp_sock fields in sock_ops programs does not preclude the tcp_sock fields from being removed as long as we are willing to do one of the following:
1) Return a fixed value (e.x. 0 or 0xffffffff), or
2) Make the verifier fail if that field is accessed (i.e. program fails to load) so the user will know that field is no longer supported.
--- net/core/filter.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
net/core/filter.c +10 -10
@@ -4464,11 +4464,11 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, is_fullsock));break ; -/* Helper macro for adding read access to tcp_sock fields. */ -#define SOCK_OPS_GET_TCP (FIELD_NAME ) \ +/* Helper macro for adding read access to tcp_sock or sock fields. */ +#define SOCK_OPS_GET_FIELD (BPF_FIELD ,OBJ_FIELD ,OBJ ) \ do { \ - BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD_NAME) > \ - FIELD_SIZEOF(struct bpf_sock_ops, FIELD_NAME)); \ + BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \ + FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, \ is_fullsock), \
@@ -4480,18 +4480,18 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, struct bpf_sock_ops_kern, sk),\ si->dst_reg, si->src_reg, \ offsetof(struct bpf_sock_ops_kern, sk));\ - *insn++ = BPF_LDX_MEM(FIELD_SIZEOF(struct tcp_sock, \ -FIELD_NAME ), si->dst_reg, \ - si->dst_reg, \ - offsetof(struct tcp_sock , FIELD_NAME)); \ + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF (OBJ , \ +OBJ_FIELD ), \ + si->dst_reg, si->dst_reg, \ + offsetof(OBJ , OBJ_FIELD)); \ }while (0 )case offsetof(struct bpf_sock_ops , snd_cwnd): -SOCK_OPS_GET_TCP (snd_cwnd); + SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);break ;case offsetof(struct bpf_sock_ops , srtt_us): -SOCK_OPS_GET_TCP (srtt_us); + SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);break ; }return insn - insn_buf;
Signature · 1 line
2.9.5
PATCH v6 3/11 bpf: Add write access to tcp_sock and sock fields
In reply to Lawrence BrakmoThis patch adds a macro, SOCK_OPS_SET_FIELD, for writing to struct tcp_sock or struct sock fields. This required adding a new field "temp" to struct bpf_sock_ops_kern for temporary storage that is used by sock_ops_convert_ctx_access. It is used to store and recover the contents of a register, so the register can be used to store the address of the sk. Since we cannot overwrite the dst_reg because it contains the pointer to ctx, nor the src_reg since it contains the value we want to store, we need an extra register to contain the address of the sk.
Also adds the macro SOCK_OPS_GET_OR_SET_FIELD that calls one of the GET or SET macros depending on the value of the TYPE field.
--- include/linux/filter.h | 9 +++++++++ include/net/tcp.h | 2 +- net/core/filter.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-)
include/linux/filter.h +9 -0
@@ -1007,6 +1007,15 @@ struct bpf_sock_ops_kern { u32 replylong[4]; };u32 is_fullsock; +u64 temp;/* temp and everything after is not +* initialized to 0 before calling +* the BPF program. New fields that +* should be initialized to 0 should +* be inserted before temp. +* temp is scratch storage used by +* sock_ops_convert_ctx_access +* as temporary storage of a register. +*/ };#endif /* __LINUX_FILTER_H__ */
include/net/tcp.h +1 -1
@@ -2010,7 +2010,7 @@ static inline int tcp_call_bpf(struct sock *sk, int op)struct bpf_sock_ops_kern sock_ops;int ret; -memset (&sock_ops,0 ,sizeof (sock_ops)); +memset (&sock_ops,0 , offsetof(struct bpf_sock_ops_kern , temp));if (sk_fullsock (sk)) { sock_ops.is_fullsock =1 ;sock_owned_by_me (sk);
net/core/filter.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [PATCH bpf-next v6 03/11] bpf: Add write access to tcp_sock and sock fields
Quoted 10 lines · Lawrence Brakmo
This patch adds a macro,
SOCK_OPS_SET_FIELD, for writing to structtcp_sockor struct sock fields. This required adding a new field "temp" to structbpf_sock_ops_kernfor temporary storage that is used bysock_ops_convert_ctx_access. It is used to store and recover the contents of a register, so the register can be used to store the address of the sk. Since we cannot overwrite thedst_regbecause it contains the pointer to ctx, nor thesrc_regsince it contains the value we want to store, we need an extra register to contain the address of the sk.
Quoted 4 lines
Also adds the macro
SOCK_OPS_GET_OR_SET_FIELDthat calls one of the GET or SET macros depending on the value of the TYPE field.Signed-off-by Lawrence Brakmo <[email protected]>
that is really clever way of doing inline writes into fields. I suspect we will be using this approach in other places.
PATCH v6 4/11 bpf: Support passing args to sock_ops bpf function
In reply to Lawrence BrakmoAdds support for passing up to 4 arguments to sock_ops bpf functions. It reusues the reply union, so the bpf_sock_ops structures are not increased in size.
--- include/linux/filter.h | 1 + include/net/tcp.h | 64 ++++++++++++++++++++++++++++++++++++++++++++---- include/uapi/linux/bpf.h | 5 ++-- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_nv.c | 2 +- net/ipv4/tcp_output.c | 2 +- 6 files changed, 66 insertions(+), 10 deletions(-)
include/linux/filter.h +1 -0
@@ -1003,6 +1003,7 @@ struct bpf_sock_ops_kern {struct sock *sk;u32 op;union { +u32 args[4 ];u32 reply;u32 replylong[4 ]; };
include/net/tcp.h +59 -5
104 lines not on this page · raw message
include/uapi/linux/bpf.h +3 -2
@@ -952,8 +952,9 @@ struct bpf_map_info {struct bpf_sock_ops {__u32 op;union { -__u32 reply; -__u32 replylong[4 ]; +__u32 args[4 ];/* Optionally passed to bpf program */ +__u32 reply;/* Returned by bpf program */ +__u32 replylong[4 ];/* Optionally returned by bpf prog */ };__u32 family;__u32 remote_ip4;/* Stored in network byte order */
net/ipv4/tcp.c +1 -1
@@ -463,7 +463,7 @@ void tcp_init_transfer(struct sock *sk, int bpf_op)tcp_mtup_init (sk); icsk->icsk_af_ops->rebuild_header (sk);tcp_init_metrics (sk); -tcp_call_bpf (sk, bpf_op); +tcp_call_bpf (sk, bpf_op,0 ,NULL );tcp_init_congestion_control (sk);tcp_init_buffer_space (sk); }
net/ipv4/tcp_nv.c +1 -1
@@ -146,7 +146,7 @@ static void tcpnv_init(struct sock *sk)* within a datacenter, where we have reasonable estimates of * RTTs */ - base_rtt =tcp_call_bpf (sk,BPF_SOCK_OPS_BASE_RTT ); + base_rtt =tcp_call_bpf (sk,BPF_SOCK_OPS_BASE_RTT ,0 ,NULL );if (base_rtt >0 ) { ca->nv_base_rtt = base_rtt; ca->nv_lower_bound_rtt = (base_rtt *205 ) >>8 ;/* 80% */
net/ipv4/tcp_output.c +1 -1
@@ -3469,7 +3469,7 @@ int tcp_connect(struct sock *sk)struct sk_buff *buff;int err; -tcp_call_bpf (sk,BPF_SOCK_OPS_TCP_CONNECT_CB ); +tcp_call_bpf (sk,BPF_SOCK_OPS_TCP_CONNECT_CB ,0 ,NULL );if (inet_csk (sk)->icsk_af_ops->rebuild_header (sk))return -EHOSTUNREACH ;/* Routing failure or similar. */
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 04/11] bpf: Support passing args to sock_ops bpf function
Quoted 40 lines · Lawrence Brakmo
Adds support for passing up to 4 arguments to
sock_opsbpf functions. It reusues the reply union, so thebpf_sock_opsstructures are not increased in size.Signed-off-by Lawrence Brakmo <[email protected]>--- include/linux/filter.h | 1 + include/net/tcp.h | 64 ++++++++++++++++++++++++++++++++++++++++++++---- include/uapi/linux/bpf.h | 5 ++-- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_nv.c | 2 +- net/ipv4/tcp_output.c | 2 +- 6 files changed, 66 insertions(+), 10 deletions(-)include/linux/filter.h +1
-0@@ -1003,6 +1003,7 @@ struct bpf_sock_ops_kern {struct sock *sk;u32 op;union { +u32 args[4 ];u32 reply;u32 replylong[4 ]; };include/net/tcp.h +1
-1@@ -2005,7 +2005,7 @@ void tcp_cleanup_ulp(struct sock *sk);* program loaded). */ #ifdef CONFIG_BPF -static inline int tcp_call_bpf (struct sock *sk,int op) +static inline int tcp_call_bpf (struct sock *sk,int op,u32 nargs,u32 *args) {struct bpf_sock_ops_kern sock_ops;int ret;
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2022
Quoted 5 lines
include/net/tcp.h +2
-0@@ -2018,6 +2018,8 @@ static inline int tcp_call_bpf(struct sock *sk, int op) sock_ops.sk = sk; sock_ops.op = op; +if (nargs >0 ) +memcpy (sock_ops.args, args, nargs*sizeof (u32));
Small nit given respin: nargs * sizeof(*args)
Quoted 44 lines
include/net/tcp.h +33
-1ret =BPF_CGROUP_RUN_PROG_SOCK_OPS (&sock_ops);if (ret ==0 )@@ -2026,18 +2028,70 @@ static inline int tcp_call_bpf(struct sock *sk, int op) ret =-1 ;return ret; } + +static inline int tcp_call_bpf_1arg (struct sock *sk,int op,u32 arg) +{ +return tcp_call_bpf (sk, op,1 , &arg); +} + +static inline int tcp_call_bpf_2arg (struct sock *sk,int op,u32 arg1,u32 arg2) +{ +u32 args[2 ] = {arg1, arg2}; + +return tcp_call_bpf (sk, op,2 , args); +} + +static inline int tcp_call_bpf_3arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3) +{ +u32 args[3 ] = {arg1, arg2, arg3}; + +return tcp_call_bpf (sk, op,3 , args); +} + +static inline int tcp_call_bpf_4arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3,u32 arg4) +{ +u32 args[4 ] = {arg1, arg2, arg3, arg4}; + +return tcp_call_bpf (sk, op,4 , args); +} +#else -static inline int tcp_call_bpf (struct sock *sk,int op) +static inline int tcp_call_bpf (struct sock *sk,int op,u32 nargs,u32 *args) {return -EPERM ; } + +static inline int tcp_call_bpf_1arg (struct sock *sk,int op,u32 arg) +{
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2077
Quoted 10 lines
include/net/tcp.h +10
-0+return -EPERM ; +} + +static inline int tcp_call_bpf_2arg (struct sock *sk,int op,u32 arg1,u32 arg2) +{ +return -EPERM ; +} + +static inline int tcp_call_bpf_3arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3)
indent: arg3
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2088
Quoted 11 lines
include/net/tcp.h +10
-0+{ +return -EPERM ; +} + +static inline int tcp_call_bpf_4arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3,u32 arg4) +{ +return -EPERM ; +} +#endif
tcp_call_bpf_1arg() and tcp_call_bpf_4arg() unused for the time being?
Re: [PATCH bpf-next v6 04/11] bpf: Support passing args to sock_ops bpf function
On 1/23/18, 5:11 PM, "Daniel Borkmann" <[email protected]> wrote:
On 01/20/2018 02:45 AM, Lawrence Brakmo wrote:
> Adds support for passing up to 4 arguments to sock_ops bpf functions. It
> reusues the reply union, so the bpf_sock_ops structures are not
> increased in size.
>
> Signed-off-by: Lawrence Brakmo <[email protected]>
> ---
> include/linux/filter.h | 1 +
> include/net/tcp.h | 64 ++++++++++++++++++++++++++++++++++++++++++++----
> include/uapi/linux/bpf.h | 5 ++--
> net/ipv4/tcp.c | 2 +-
> net/ipv4/tcp_nv.c | 2 +-
> net/ipv4/tcp_output.c | 2 +-
> 6 files changed, 66 insertions(+), 10 deletions(-)
>
> diff --git a/include/linux/filter.h b/include/linux/filter.h
> index daa5a67..20384c4 100644
> --- a/include/linux/filter.h
> +++ b/include/linux/filter.h
> @@ -1003,6 +1003,7 @@ struct bpf_sock_ops_kern {
> struct sock *sk;
> u32 op;
> union {
> + u32 args[4];
> u32 reply;
> u32 replylong[4];
> };
> diff --git a/include/net/tcp.h b/include/net/tcp.h
> index 108d16a..8e9111f 100644
> --- a/include/net/tcp.h
> +++ b/include/net/tcp.h
> @@ -2005,7 +2005,7 @@ void tcp_cleanup_ulp(struct sock *sk);
> * program loaded).
> */
> #ifdef CONFIG_BPF
> -static inline int tcp_call_bpf(struct sock *sk, int op)
> +static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
> {
> struct bpf_sock_ops_kern sock_ops;
> int ret;
> @@ -2018,6 +2018,8 @@ static inline int tcp_call_bpf(struct sock *sk, int op)
>
> sock_ops.sk = sk;
> sock_ops.op = op;
> + if (nargs > 0)
> + memcpy(sock_ops.args, args, nargs*sizeof(u32));
Small nit given respin: nargs * sizeof(*args)Thanks, will fix.
Quoted 44 lines
include/net/tcp.h +33
-1ret =BPF_CGROUP_RUN_PROG_SOCK_OPS (&sock_ops);if (ret ==0 )@@ -2026,18 +2028,70 @@ static inline int tcp_call_bpf(struct sock *sk, int op) ret =-1 ;return ret; } + +static inline int tcp_call_bpf_1arg (struct sock *sk,int op,u32 arg) +{ +return tcp_call_bpf (sk, op,1 , &arg); +} + +static inline int tcp_call_bpf_2arg (struct sock *sk,int op,u32 arg1,u32 arg2) +{ +u32 args[2 ] = {arg1, arg2}; + +return tcp_call_bpf (sk, op,2 , args); +} + +static inline int tcp_call_bpf_3arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3) +{ +u32 args[3 ] = {arg1, arg2, arg3}; + +return tcp_call_bpf (sk, op,3 , args); +} + +static inline int tcp_call_bpf_4arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3,u32 arg4) +{ +u32 args[4 ] = {arg1, arg2, arg3, arg4}; + +return tcp_call_bpf (sk, op,4 , args); +} +#else -static inline int tcp_call_bpf (struct sock *sk,int op) +static inline int tcp_call_bpf (struct sock *sk,int op,u32 nargs,u32 *args) {return -EPERM ; } + +static inline int tcp_call_bpf_1arg (struct sock *sk,int op,u32 arg) +{
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2077
Quoted 10 lines
include/net/tcp.h +10
-0+return -EPERM ; +} + +static inline int tcp_call_bpf_2arg (struct sock *sk,int op,u32 arg1,u32 arg2) +{ +return -EPERM ; +} + +static inline int tcp_call_bpf_3arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3)
indent: arg3
OK
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2088
Quoted 11 lines
include/net/tcp.h +10
-0+{ +return -EPERM ; +} + +static inline int tcp_call_bpf_4arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3,u32 arg4) +{ +return -EPERM ; +} +#endif
tcp_call_bpf_1arg() and tcp_call_bpf_4arg() unused for the time being?
Yes, I just thought I should add them for completeness. Should I remove them until they are actually used?
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 04/11] bpf: Support passing args to sock_ops bpf function
Quoted 1 line · Lawrence Brakmo
On 1/23/18, 5:11 PM, "Daniel Borkmann" <[email protected]> wrote:
[...]
Quoted 14 lines
include/net/tcp.h static inline int tcp_call_bpf(struct sock *sk, int op) · line 2088
Quoted 11 lines
include/net/tcp.h +10
-0+{ +return -EPERM ; +} + +static inline int tcp_call_bpf_4arg (struct sock *sk,int op,u32 arg1,u32 arg2, +u32 arg3,u32 arg4) +{ +return -EPERM ; +} +#endif
tcp_call_bpf_1arg()andtcp_call_bpf_4arg()unused for the time being?
Quoted 2 lines
Yes, I just thought I should add them for completeness. Should I remove them until they are actually used?
Yeah, I think that would be preferred way.
Thanks again,
Daniel
PATCH v6 5/11 bpf: Adds field bpf_sock_ops_cb_flags to tcp_sock
In reply to Lawrence BrakmoAdds field bpf_sock_ops_cb_flags to tcp_sock and bpf_sock_ops. Its primary use is to determine if there should be calls to sock_ops bpf program at various points in the TCP code. The field is initialized to zero, disabling the calls. A sock_ops BPF program can set it, per connection and as necessary, when the connection is established.
It also adds support for reading and writting the field within a sock_ops BPF program. Reading is done by accessing the field directly. However, writing is done through the helper function bpf_sock_ops_cb_flags_set, in order to return an error if a BPF program is trying to set a callback that is not supported in the current kernel (i.e. running an older kernel). The helper function returns 0 if it was able to set all of the bits set in the argument, a positive number containing the bits that could not be set, or -EINVAL if the socket is not a full TCP socket.
Examples of where one could call the bpf program:
1) When RTO fires
2) When a packet is retransmitted
3) When the connection terminates
4) When a packet is sent
5) When a packet is received
--- include/linux/tcp.h | 11 +++++++++++ include/uapi/linux/bpf.h | 12 +++++++++++- include/uapi/linux/tcp.h | 5 +++++ net/core/filter.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-)
include/linux/tcp.h +11 -0
@@ -335,6 +335,17 @@ struct tcp_sock {int linger2; + +/* Sock_ops bpf program related variables */ +#ifdef CONFIG_BPF +u8 bpf_sock_ops_cb_flags;/* Control calling BPF programs +* values defined in uapi/linux/tcp.h +*/ +#define BPF_SOCK_OPS_TEST_FLAG (TP ,ARG ) (TP->bpf_sock_ops_cb_flags & ARG) +#else +#define BPF_SOCK_OPS_TEST_FLAG (TP ,ARG ) 0 +#endif +/* Receiver side RTT estimation */ struct {u32 rtt_us;
include/uapi/linux/bpf.h +11 -1
@@ -642,6 +642,14 @@ union bpf_attr {* @optlen: length of optval in bytes * Return: 0 or negative error * +* int bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags) +* Set callback flags for sock_ops +* @bpf_sock_ops: pointer to bpf_sock_ops_kern struct +* @flags: flags value +* Return: 0 for no error +* -EINVAL if there is no full tcp socket +* bits in flags that are not supported by current kernel +* * int bpf_skb_adjust_room(skb, len_diff, mode, flags) * Grow or shrink room in sk_buff. * @skb: pointer to skb
@@ -748,7 +756,8 @@ union bpf_attr {FN (perf_event_read_value), \FN (perf_prog_read_value), \FN (getsockopt), \ -FN (override_return), +FN (override_return), \ +FN (sock_ops_cb_flags_set),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -969,6 +978,7 @@ struct bpf_sock_ops {*/ __u32 snd_cwnd;__u32 srtt_us;/* Averaged RTT << 3 in usecs */ +__u32 bpf_sock_ops_cb_flags;/* flags defined in uapi/linux/tcp.h */ };/* List of known BPF sock_ops operators.
include/uapi/linux/tcp.h +5 -0
@@ -268,4 +268,9 @@ struct tcp_diag_md5sig { __u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; }; +/* Definitions for bpf_sock_ops_cb_flags */ +#define BPF_SOCK_OPS_ALL_CB_FLAGS 0/* Mask of all currently +* supported cb flags +*/ +#endif /* _UAPI_LINUX_TCP_H */
net/core/filter.c +34 -0
@@ -3324,6 +3324,33 @@ static const struct bpf_func_proto bpf_getsockopt_proto = { .arg5_type = ARG_CONST_SIZE, }; +BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, + int, argval) +{ +struct sock *sk = bpf_sock->sk; +int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; + +if (!sk_fullsock (sk)) +return -EINVAL ; + +#ifdef CONFIG_INET +if (val) +tcp_sk (sk)->bpf_sock_ops_cb_flags = val; + +return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS ); +#else +return -EINVAL ; +#endif +} + +static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = { + .func = bpf_sock_ops_cb_flags_set, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , +}; +static const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id) {
@@ -3504,6 +3531,8 @@ static const struct bpf_func_proto *return &bpf_setsockopt_proto;case BPF_FUNC_getsockopt:return &bpf_getsockopt_proto; +case BPF_FUNC_sock_ops_cb_flags_set: +return &bpf_sock_ops_cb_flags_set_proto;case BPF_FUNC_sock_map_update:return &bpf_sock_map_update_proto;default :
@@ -4541,6 +4570,11 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,case offsetof(struct bpf_sock_ops , srtt_us): SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);break ; + +case offsetof(struct bpf_sock_ops , bpf_sock_ops_cb_flags): +SOCK_OPS_GET_FIELD (bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags, + struct tcp_sock); +break ; }return insn - insn_buf; }
Signature · 1 line
2.9.5
Re: [PATCH bpf-next v6 05/11] bpf: Adds field bpf_sock_ops_cb_flags to tcp_sock
Quoted 15 lines · Lawrence Brakmo
Adds field
bpf_sock_ops_cb_flagstotcp_sockandbpf_sock_ops. Its primary use is to determine if there should be calls tosock_opsbpf program at various points in the TCP code. The field is initialized to zero, disabling the calls. Asock_opsBPF program can set it, per connection and as necessary, when the connection is established.It also adds support for reading and writting the field within a
sock_opsBPF program. Reading is done by accessing the field directly. However, writing is done through the helper functionbpf_sock_ops_cb_flags_set, in order to return an error if a BPF program is trying to set a callback that is not supported in the current kernel (i.e. running an older kernel). The helper function returns 0 if it was able to set all of the bits set in the argument, a positive number containing the bits that could not be set, or-EINVALif the socket is not a full TCP socket.
...
include/linux/tcp.h struct tcp_sock { · line 342
Quoted 4 lines
include/linux/tcp.h +4
-0+/* Sock_ops bpf program related variables */ +#ifdef CONFIG_BPF +u8 bpf_sock_ops_cb_flags;/* Control calling BPF programs +* values defined in uapi/linux/tcp.h
I guess we can extend u8 into u16 or more if necessary in the future.
Quoted 7 lines
+ * int
bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags)+ * Set callback flags for sock_ops + * @bpf_sock_ops: pointer to bpf_sock_ops_kern struct + * @flags: flags value + * Return: 0 for no error+ *
-EINVALif there is no full tcp socket
+ * bits in flags that are not supported by current kernel
...
Quoted 4 lines
net/core/filter.c +4
-0+BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, + int, argval) +{ +struct sock *sk = bpf_sock->sk;
net/core/filter.c static const struct bpf_func_proto bpf_getsockopt_proto = { · line 3340
Quoted 10 lines
net/core/filter.c +10
-0+int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; + +if (!sk_fullsock (sk)) +return -EINVAL ; + +#ifdef CONFIG_INET +if (val) +tcp_sk (sk)->bpf_sock_ops_cb_flags = val; + +return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS );
interesting idea! took me some time to realize the potential of such semantics, but now I like it a lot.
It blends 'set good flag' with 'which flags are supported' logic into single helper. Nice.
Thanks for adding a test for both ways.
Eric, does this approach address your concerns?
Re: [PATCH bpf-next v6 05/11] bpf: Adds field bpf_sock_ops_cb_flags to tcp_sock
On 1/19/18, 7:52 PM, "Alexei Starovoitov" <[email protected]> wrote:
On Fri, Jan 19, 2018 at 05:45:42PM -0800, Lawrence Brakmo wrote:
> Adds field bpf_sock_ops_cb_flags to tcp_sock and bpf_sock_ops. Its primary > use is to determine if there should be calls to sock_ops bpf program at > various points in the TCP code. The field is initialized to zero, > disabling the calls. A sock_ops BPF program can set it, per connection and > as necessary, when the connection is established.
>
> It also adds support for reading and writting the field within a > sock_ops BPF program. Reading is done by accessing the field directly. > However, writing is done through the helper function
> bpf_sock_ops_cb_flags_set, in order to return an error if a BPF program > is trying to set a callback that is not supported in the current kernel > (i.e. running an older kernel). The helper function returns 0 if it was > able to set all of the bits set in the argument, a positive number > containing the bits that could not be set, or -EINVAL if the socket is > not a full TCP socket.
...
> +/* Sock_ops bpf program related variables */
> +#ifdef CONFIG_BPF
> + u8 bpf_sock_ops_cb_flags; /* Control calling BPF programs
> + * values defined in uapi/linux/tcp.h
I guess we can extend u8 into u16 or more if necessary in the future.
Yes, that was my thought.
Quoted 7 lines
+ * int
bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags)+ * Set callback flags for sock_ops + * @bpf_sock_ops: pointer to bpf_sock_ops_kern struct + * @flags: flags value + * Return: 0 for no error+ *
-EINVALif there is no full tcp socket
+ * bits in flags that are not supported by current kernel
...
> +BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
> + int, argval)
> +{
> + struct sock *sk = bpf_sock->sk;
> + int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
> +
> + if (!sk_fullsock(sk))
> + return -EINVAL;
> +
> +#ifdef CONFIG_INET
> + if (val)
> + tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
> +
> + return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
interesting idea! took me some time to realize the potential
of such semantics, but now I like it a lot.
It blends 'set good flag' with 'which flags are supported' logic
into single helper. Nice.
Thanks for adding a test for both ways.
Acked-by: Alexei Starovoitov <[email protected]>
Eric, does this approach address your concerns?Re: [PATCH bpf-next v6 05/11] bpf: Adds field bpf_sock_ops_cb_flags to tcp_sock
In reply to Alexei StarovoitovQuoted 54 lines · Alexei Starovoitov
Quoted 15 lines · Lawrence Brakmo
Adds field
bpf_sock_ops_cb_flagstotcp_sockandbpf_sock_ops. Its primary use is to determine if there should be calls tosock_opsbpf program at various points in the TCP code. The field is initialized to zero, disabling the calls. Asock_opsBPF program can set it, per connection and as necessary, when the connection is established.It also adds support for reading and writting the field within a
sock_opsBPF program. Reading is done by accessing the field directly. However, writing is done through the helper functionbpf_sock_ops_cb_flags_set, in order to return an error if a BPF program is trying to set a callback that is not supported in the current kernel (i.e. running an older kernel). The helper function returns 0 if it was able to set all of the bits set in the argument, a positive number containing the bits that could not be set, or-EINVALif the socket is not a full TCP socket....
include/linux/tcp.h struct tcp_sock { · line 342
Quoted 4 lines
include/linux/tcp.h +4
-0+/* Sock_ops bpf program related variables */ +#ifdef CONFIG_BPF +u8 bpf_sock_ops_cb_flags;/* Control calling BPF programs +* values defined in uapi/linux/tcp.h I guess we can extend u8 into u16 or more if necessary in the future.
Quoted 7 lines
+ * int
bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags)+ * Set callback flags for sock_ops + * @bpf_sock_ops: pointer to bpf_sock_ops_kern struct + * @flags: flags value + * Return: 0 for no error+ *
-EINVALif there is no full tcp socket
+ * bits in flags that are not supported by current kernel...
Quoted 4 lines
net/core/filter.c +4
-0+BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, + int, argval) +{ +struct sock *sk = bpf_sock->sk;net/core/filter.c static const struct bpf_func_proto bpf_getsockopt_proto = { · line 3340
Quoted 10 lines
net/core/filter.c +10
-0+int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; + +if (!sk_fullsock (sk)) +return -EINVAL ; + +#ifdef CONFIG_INET +if (val) +tcp_sk (sk)->bpf_sock_ops_cb_flags = val; + +return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS );interesting idea! took me some time to realize the potential of such semantics, but now I like it a lot.
It blends 'set good flag' with 'which flags are supported' logic into single helper. Nice.
Thanks for adding a test for both ways.
Quoted 3 lines
Eric, does this approach address your concerns?
Yes, this seems fine, thanks.
PATCH v6 6/11 bpf: Add sock_ops RTO callback
In reply to Lawrence BrakmoAdds an optional call to sock_ops BPF program based on whether the BPF_SOCK_OPS_RTO_CB_FLAG is set in bpf_sock_ops_flags.
The BPF program is passed 2 arguments: icsk_retransmits and whether the RTO has expired.
--- include/uapi/linux/bpf.h | 5 +++++ include/uapi/linux/tcp.h | 3 ++- net/ipv4/tcp_timer.c | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +5 -0
@@ -1014,6 +1014,11 @@ enum {* a congestion threshold. RTTs above * this indicate congestion */ + BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. +* Arg1: value of icsk_retransmits +* Arg2: value of icsk_rto +* Arg3: whether RTO has expired +*/ };#define TCP_BPF_IW 1001/* Set TCP initial congestion window */
include/uapi/linux/tcp.h +2 -1
@@ -269,7 +269,8 @@ struct tcp_diag_md5sig { };/* Definitions for bpf_sock_ops_cb_flags */ -#define BPF_SOCK_OPS_ALL_CB_FLAGS 0/* Mask of all currently +#define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) +#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x1/* Mask of all currently * supported cb flags */
net/ipv4/tcp_timer.c +7 -0
@@ -213,11 +213,18 @@ static int tcp_write_timeout(struct sock *sk) icsk->icsk_user_timeout); }tcp_fastopen_active_detect_blackhole (sk, expired); + +if (BPF_SOCK_OPS_TEST_FLAG (tp,BPF_SOCK_OPS_RTO_CB_FLAG )) +tcp_call_bpf_3arg (sk,BPF_SOCK_OPS_RTO_CB , + icsk->icsk_retransmits, + icsk->icsk_rto, (int )expired); +if (expired) {/* Has it gone just too far? */ tcp_write_err (sk);return 1 ; } +return 0 ; }
Signature · 1 line
2.9.5
PATCH v6 7/11 bpf: Add support for reading sk_state and more
In reply to Lawrence BrakmoAdd support for reading many more tcp_sock fields
state, same as sk->sk_state
rtt_min same as sk->rtt_min.s[0].v (current rtt_min)
snd_ssthresh
rcv_nxt
snd_nxt
snd_una
mss_cache
ecn_flags
rate_delivered
rate_interval_us
packets_out
retrans_out
total_retrans
segs_in
data_segs_in
segs_out
data_segs_out
bytes_received (__u64)
bytes_acked (__u64)
--- include/uapi/linux/bpf.h | 19 +++++++ net/core/filter.c | 134 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 140 insertions(+), 13 deletions(-)
include/uapi/linux/bpf.h +19 -0
@@ -979,6 +979,25 @@ struct bpf_sock_ops {__u32 snd_cwnd;__u32 srtt_us;/* Averaged RTT << 3 in usecs */ __u32 bpf_sock_ops_cb_flags;/* flags defined in uapi/linux/tcp.h */ +__u32 state; +__u32 rtt_min; +__u32 snd_ssthresh; +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u32 mss_cache; +__u32 ecn_flags; +__u32 rate_delivered; +__u32 rate_interval_us; +__u32 packets_out; +__u32 retrans_out; +__u32 total_retrans; +__u32 segs_in; +__u32 data_segs_in; +__u32 segs_out; +__u32 data_segs_out; +__u64 bytes_received; +__u64 bytes_acked; };/* List of known BPF sock_ops operators.
net/core/filter.c +121 -13
167 lines not on this page · raw message
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 07/11] bpf: Add support for reading sk_state and more
Quoted 43 lines · Lawrence Brakmo
Add support for reading many more
tcp_sockfieldsstate, same as
sk->sk_state
rtt_minsame as sk->rtt_min.s[0].v (currentrtt_min)
snd_ssthresh
rcv_nxt
snd_nxt
snd_una
mss_cache
ecn_flags
rate_delivered
rate_interval_us
packets_out
retrans_out
total_retrans
segs_in
data_segs_in
segs_out
data_segs_out
bytes_received(__u64)
bytes_acked(__u64)Signed-off-by Lawrence Brakmo <[email protected]>--- include/uapi/linux/bpf.h | 19 +++++++ net/core/filter.c | 134 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 140 insertions(+), 13 deletions(-)include/uapi/linux/bpf.h +7
-0@@ -979,6 +979,25 @@ struct bpf_sock_ops {__u32 snd_cwnd;__u32 srtt_us;/* Averaged RTT << 3 in usecs */ __u32 bpf_sock_ops_cb_flags;/* flags defined in uapi/linux/tcp.h */ +__u32 state; +__u32 rtt_min; +__u32 snd_ssthresh; +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u32 mss_cache;
include/uapi/linux/bpf.h struct bpf_sock_ops { · line 998
Quoted 10 lines
include/uapi/linux/bpf.h +10
-0+__u32 ecn_flags; +__u32 rate_delivered; +__u32 rate_interval_us; +__u32 packets_out; +__u32 retrans_out; +__u32 total_retrans; +__u32 segs_in; +__u32 data_segs_in; +__u32 segs_out; +__u32 data_segs_out;
Btw, this will have a 4 bytes hole in here which the user can otherwise address out of the prog. Could you add the sk_txhash from the next patch in between here instead?
Quoted 64 lines
include/uapi/linux/bpf.h +2
-0+__u64 bytes_received; +__u64 bytes_acked; };/* List of known BPF sock_ops operators. net/core/filter.c +22
-13@@ -3849,34 +3849,43 @@ void bpf_warn_invalid_xdp_action(u32 act) }EXPORT_SYMBOL_GPL (bpf_warn_invalid_xdp_action); -static bool __is_valid_sock_ops_access (int off,int size) +static bool sock_ops_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info) { +const int size_default =sizeof (__u32); +if (off <0 || off >=sizeof (struct bpf_sock_ops ))return false; +/* The verifier guarantees that size > 0. */ if (off % size !=0 )return false; -if (size !=sizeof (__u32)) -return false; - -return true; -} -static bool sock_ops_is_valid_access (int off,int size, -enum bpf_access_type type, -struct bpf_insn_access_aux *info) -{if (type ==BPF_WRITE ) {switch (off) { - case offsetof(struct bpf_sock_ops, op) ... - offsetof(struct bpf_sock_ops, replylong[3]): + case bpf_ctx_range_till(struct bpf_sock_ops, op, replylong[3]): +if (size != size_default) +return false;break ;default :return false; } + }else { +switch (off) { + case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, + bytes_acked): +if (size !=sizeof (__u64)) +return false; +break ; +default : +if (size != size_default) +return false; +break ; + } } -return __is_valid_sock_ops_access (off, size); +return true; }
Re: [PATCH bpf-next v6 07/11] bpf: Add support for reading sk_state and more
On 1/23/18, 5:05 PM, "Daniel Borkmann" <[email protected]> wrote:
On 01/20/2018 02:45 AM, Lawrence Brakmo wrote:
> Add support for reading many more tcp_sock fields
>
> state, same as sk->sk_state
> rtt_min same as sk->rtt_min.s[0].v (current rtt_min)
> snd_ssthresh
> rcv_nxt
> snd_nxt
> snd_una
> mss_cache
> ecn_flags
> rate_delivered
> rate_interval_us
> packets_out
> retrans_out
> total_retrans
> segs_in
> data_segs_in
> segs_out
> data_segs_out
> bytes_received (__u64)
> bytes_acked (__u64)
>
> Signed-off-by: Lawrence Brakmo <[email protected]>
> ---
> include/uapi/linux/bpf.h | 19 +++++++
> net/core/filter.c | 134 ++++++++++++++++++++++++++++++++++++++++++-----
> 2 files changed, 140 insertions(+), 13 deletions(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 2a8c40a..ff34f3c 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -979,6 +979,25 @@ struct bpf_sock_ops {
> __u32 snd_cwnd;
> __u32 srtt_us; /* Averaged RTT << 3 in usecs */
> __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */
> + __u32 state;
> + __u32 rtt_min;
> + __u32 snd_ssthresh;
> + __u32 rcv_nxt;
> + __u32 snd_nxt;
> + __u32 snd_una;
> + __u32 mss_cache;
> + __u32 ecn_flags;
> + __u32 rate_delivered;
> + __u32 rate_interval_us;
> + __u32 packets_out;
> + __u32 retrans_out;
> + __u32 total_retrans;
> + __u32 segs_in;
> + __u32 data_segs_in;
> + __u32 segs_out;
> + __u32 data_segs_out;
Btw, this will have a 4 bytes hole in here which the user can otherwise
address out of the prog. Could you add the sk_txhash from the next patch
in between here instead?Good point. Will fix in new patch. Thanks Daniel.
Quoted 64 lines
include/uapi/linux/bpf.h +2
-0+__u64 bytes_received; +__u64 bytes_acked; };/* List of known BPF sock_ops operators. net/core/filter.c +22
-13@@ -3849,34 +3849,43 @@ void bpf_warn_invalid_xdp_action(u32 act) }EXPORT_SYMBOL_GPL (bpf_warn_invalid_xdp_action); -static bool __is_valid_sock_ops_access (int off,int size) +static bool sock_ops_is_valid_access (int off,int size, +enum bpf_access_type type, +struct bpf_insn_access_aux *info) { +const int size_default =sizeof (__u32); +if (off <0 || off >=sizeof (struct bpf_sock_ops ))return false; +/* The verifier guarantees that size > 0. */ if (off % size !=0 )return false; -if (size !=sizeof (__u32)) -return false; - -return true; -} -static bool sock_ops_is_valid_access (int off,int size, -enum bpf_access_type type, -struct bpf_insn_access_aux *info) -{if (type ==BPF_WRITE ) {switch (off) { - case offsetof(struct bpf_sock_ops, op) ... - offsetof(struct bpf_sock_ops, replylong[3]): + case bpf_ctx_range_till(struct bpf_sock_ops, op, replylong[3]): +if (size != size_default) +return false;break ;default :return false; } + }else { +switch (off) { + case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, + bytes_acked): +if (size !=sizeof (__u64)) +return false; +break ; +default : +if (size != size_default) +return false; +break ; + } } -return __is_valid_sock_ops_access (off, size); +return true; }
PATCH v6 8/11 bpf: Add sock_ops R/W access to tclass & sk_txhash
In reply to Lawrence BrakmoAdds direct R/W access to sk_txhash and access to tclass for ipv6 flows through getsockopt and setsockopt. Sample usage for tclass:
bpf_getsockopt(skops, SOL_IPV6, IPV6_TCLASS, &v, sizeof(v))
where skops is a pointer to the ctx (struct bpf_sock_ops).
--- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +1 -0
@@ -998,6 +998,7 @@ struct bpf_sock_ops {__u32 data_segs_out;__u64 bytes_received;__u64 bytes_acked; +__u32 sk_txhash; };/* List of known BPF sock_ops operators.
net/core/filter.c +47 -1
78 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH v6 9/11 bpf: Add BPF_SOCK_OPS_RETRANS_CB
In reply to Lawrence BrakmoAdds support for calling sock_ops BPF program when there is a retransmission. Two arguments are used; one for the sequence number and other for the number of segments retransmitted. Does not include syn-ack retransmissions.
New op: BPF_SOCK_OPS_RETRANS_CB.
--- include/uapi/linux/bpf.h | 4 ++++ include/uapi/linux/tcp.h | 3 ++- net/ipv4/tcp_output.c | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +4 -0
@@ -1039,6 +1039,10 @@ enum {* Arg2: value of icsk_rto * Arg3: whether RTO has expired */ + BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. +* Arg1: sequence number of 1st byte +* Arg2: # segments +*/ };#define TCP_BPF_IW 1001/* Set TCP initial congestion window */
include/uapi/linux/tcp.h +2 -1
@@ -270,7 +270,8 @@ struct tcp_diag_md5sig {/* Definitions for bpf_sock_ops_cb_flags */ #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) -#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x1/* Mask of all currently +#define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) +#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x3/* Mask of all currently * supported cb flags */
net/ipv4/tcp_output.c +3 -0
@@ -2908,6 +2908,9 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)if (likely (!err)) {TCP_SKB_CB (skb)->sacked |=TCPCB_EVER_RETRANS ;trace_tcp_retransmit_skb (sk, skb); +if (BPF_SOCK_OPS_TEST_FLAG (tp,BPF_SOCK_OPS_RETRANS_CB_FLAG )) +tcp_call_bpf_2arg (sk,BPF_SOCK_OPS_RETRANS_CB , +TCP_SKB_CB (skb)->seq, segs); }else if (err != -EBUSY ) {NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPRETRANSFAIL ); }
Signature · 1 line
2.9.5
PATCH v6 10/11 bpf: Add BPF_SOCK_OPS_STATE_CB
In reply to Lawrence BrakmoAdds support for calling sock_ops BPF program when there is a TCP state change. Two arguments are used; one for the old state and another for the new state.
There is a new enum in include/uapi/linux/bpf.h that exports the TCP states that prepends BPF_ to the current TCP state names. If it is ever necessary to change the internal TCP state values (other than adding more to the end), then it will become necessary to convert from the internal TCP state value to the BPF value before calling the BPF sock_ops function. There are a set of compile checks added in tcp.c to detect if the internal and BPF values differ so we can make the necessary fixes.
New op: BPF_SOCK_OPS_STATE_CB.
--- include/uapi/linux/bpf.h | 26 ++++++++++++++++++++++++++ include/uapi/linux/tcp.h | 3 ++- net/ipv4/tcp.c | 24 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +26 -0
@@ -1043,6 +1043,32 @@ enum {* Arg1: sequence number of 1st byte * Arg2: # segments */ + BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. +* Arg1: old_state +* Arg2: new_state +*/ +}; + +/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect +* changes between the TCP and BPF versions. Ideally this should never happen. +* If it does, we need to add code to convert them before calling +* the BPF sock_ops function. +*/ +enum { +BPF_TCP_ESTABLISHED =1 , +BPF_TCP_SYN_SENT , +BPF_TCP_SYN_RECV , +BPF_TCP_FIN_WAIT1 , +BPF_TCP_FIN_WAIT2 , +BPF_TCP_TIME_WAIT , +BPF_TCP_CLOSE , +BPF_TCP_CLOSE_WAIT , +BPF_TCP_LAST_ACK , +BPF_TCP_LISTEN , +BPF_TCP_CLOSING ,/* Now a valid state */ +BPF_TCP_NEW_SYN_RECV , + +BPF_TCP_MAX_STATES /* Leave at the end! */ };#define TCP_BPF_IW 1001/* Set TCP initial congestion window */
include/uapi/linux/tcp.h +2 -1
@@ -271,7 +271,8 @@ struct tcp_diag_md5sig {/* Definitions for bpf_sock_ops_cb_flags */ #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0)#define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) -#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x3/* Mask of all currently +#define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2) +#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7/* Mask of all currently * supported cb flags */
net/ipv4/tcp.c +24 -0
@@ -2042,6 +2042,30 @@ void tcp_set_state(struct sock *sk, int state) {int oldstate = sk->sk_state; +/* We defined a new enum for TCP states that are exported in BPF +* so as not force the internal TCP states to be frozen. The +* following checks will detect if an internal state value ever +* differs from the BPF value. If this ever happens, then we will +* need to remap the internal value to the BPF value before calling +* tcp_call_bpf_2arg. +*/ +BUILD_BUG_ON ((int )BPF_TCP_ESTABLISHED != (int )TCP_ESTABLISHED ); +BUILD_BUG_ON ((int )BPF_TCP_SYN_SENT != (int )TCP_SYN_SENT ); +BUILD_BUG_ON ((int )BPF_TCP_SYN_RECV != (int )TCP_SYN_RECV ); +BUILD_BUG_ON ((int )BPF_TCP_FIN_WAIT1 != (int )TCP_FIN_WAIT1 ); +BUILD_BUG_ON ((int )BPF_TCP_FIN_WAIT2 != (int )TCP_FIN_WAIT2 ); +BUILD_BUG_ON ((int )BPF_TCP_TIME_WAIT != (int )TCP_TIME_WAIT ); +BUILD_BUG_ON ((int )BPF_TCP_CLOSE != (int )TCP_CLOSE ); +BUILD_BUG_ON ((int )BPF_TCP_CLOSE_WAIT != (int )TCP_CLOSE_WAIT ); +BUILD_BUG_ON ((int )BPF_TCP_LAST_ACK != (int )TCP_LAST_ACK ); +BUILD_BUG_ON ((int )BPF_TCP_LISTEN != (int )TCP_LISTEN ); +BUILD_BUG_ON ((int )BPF_TCP_CLOSING != (int )TCP_CLOSING ); +BUILD_BUG_ON ((int )BPF_TCP_NEW_SYN_RECV != (int )TCP_NEW_SYN_RECV ); +BUILD_BUG_ON ((int )BPF_TCP_MAX_STATES != (int )TCP_MAX_STATES ); + +if (BPF_SOCK_OPS_TEST_FLAG (tcp_sk (sk),BPF_SOCK_OPS_STATE_CB_FLAG )) +tcp_call_bpf_2arg (sk,BPF_SOCK_OPS_STATE_CB , oldstate, state); +switch (state) {case TCP_ESTABLISHED :if (oldstate !=TCP_ESTABLISHED )
Signature · 1 line
2.9.5
PATCH v6 11/11 bpf: add selftest for tcpbpf
In reply to Lawrence BrakmoAdded a selftest for tcpbpf (sock_ops) that checks that the appropriate callbacks occured and that it can access tcp_sock fields and that their values are correct.
Run with command: ./test_tcpbpf_user
--- tools/include/uapi/linux/bpf.h | 74 +++++++++++++- tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 2 + tools/testing/selftests/bpf/tcp_client.py | 52 ++++++++++ tools/testing/selftests/bpf/tcp_server.py | 79 +++++++++++++++ tools/testing/selftests/bpf/test_tcpbpf.h | 16 +++ tools/testing/selftests/bpf/test_tcpbpf_kern.c | 131 +++++++++++++++++++++++++ tools/testing/selftests/bpf/test_tcpbpf_user.c | 126 ++++++++++++++++++++++++ 8 files changed, 478 insertions(+), 6 deletions(-) create mode 100755 tools/testing/selftests/bpf/tcp_client.py create mode 100755 tools/testing/selftests/bpf/tcp_server.py create mode 100644 tools/testing/selftests/bpf/test_tcpbpf.h create mode 100644 tools/testing/selftests/bpf/test_tcpbpf_kern.c create mode 100644 tools/testing/selftests/bpf/test_tcpbpf_user.c
tools/include/uapi/linux/bpf.h +70 -4
@@ -17,7 +17,7 @@#define BPF_ALU64 0x07/* alu mode in double word width */ /* ld/ldx fields */ -#define BPF_DW 0x18/* double word */ +#define BPF_DW 0x18/* double word (64-bit) */ #define BPF_XADD 0xc0/* exclusive add */ /* alu/jmp fields */
@@ -642,6 +642,14 @@ union bpf_attr {* @optlen: length of optval in bytes * Return: 0 or negative error * +* int bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags) +* Set callback flags for sock_ops +* @bpf_sock_ops: pointer to bpf_sock_ops_kern struct +* @flags: flags value +* Return: 0 for no error +* -EINVAL if there is no full tcp socket +* bits in flags that are not supported by current kernel +* * int bpf_skb_adjust_room(skb, len_diff, mode, flags) * Grow or shrink room in sk_buff. * @skb: pointer to skb
@@ -748,7 +756,8 @@ union bpf_attr {FN (perf_event_read_value), \FN (perf_prog_read_value), \FN (getsockopt), \ -FN (override_return), +FN (override_return), \ +FN (sock_ops_cb_flags_set),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -952,8 +961,9 @@ struct bpf_map_info {struct bpf_sock_ops {__u32 op;union { -__u32 reply; -__u32 replylong[4 ]; +__u32 args[4 ];/* Optionally passed to bpf program */ +__u32 reply;/* Returned by bpf program */ +__u32 replylong[4 ];/* Optionally returned by bpf prog */ };__u32 family;__u32 remote_ip4;/* Stored in network byte order */
@@ -968,6 +978,27 @@ struct bpf_sock_ops {*/ __u32 snd_cwnd;__u32 srtt_us;/* Averaged RTT << 3 in usecs */ +__u32 bpf_sock_ops_cb_flags;/* flags defined in uapi/linux/tcp.h */ +__u32 state; +__u32 rtt_min; +__u32 snd_ssthresh; +__u32 rcv_nxt; +__u32 snd_nxt; +__u32 snd_una; +__u32 mss_cache; +__u32 ecn_flags; +__u32 rate_delivered; +__u32 rate_interval_us; +__u32 packets_out; +__u32 retrans_out; +__u32 total_retrans; +__u32 segs_in; +__u32 data_segs_in; +__u32 segs_out; +__u32 data_segs_out; +__u64 bytes_received; +__u64 bytes_acked; +__u32 sk_txhash; };/* List of known BPF sock_ops operators.
@@ -1003,6 +1034,41 @@ enum {* a congestion threshold. RTTs above * this indicate congestion */ +BPF_SOCK_OPS_RTO_CB ,/* Called when an RTO has triggered. +* Arg1: value of icsk_retransmits +* Arg2: value of icsk_rto +* Arg3: whether RTO has expired +*/ +BPF_SOCK_OPS_RETRANS_CB ,/* Called when skb is retransmitted. +* Arg1: sequence number of 1st byte +* Arg2: # segments +*/ + BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. + * Arg1: old_state + * Arg2: new_state + */ +}; + +/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect +* changes between the TCP and BPF versions. Ideally this should never happen. +* If it does, we need to add code to convert them before calling +* the BPF sock_ops function. +*/ +enum { +BPF_TCP_ESTABLISHED =1 , +BPF_TCP_SYN_SENT , +BPF_TCP_SYN_RECV , +BPF_TCP_FIN_WAIT1 , +BPF_TCP_FIN_WAIT2 , +BPF_TCP_TIME_WAIT , +BPF_TCP_CLOSE , +BPF_TCP_CLOSE_WAIT , +BPF_TCP_LAST_ACK , +BPF_TCP_LISTEN , +BPF_TCP_CLOSING ,/* Now a valid state */ +BPF_TCP_NEW_SYN_RECV , + +BPF_TCP_MAX_STATES /* Leave at the end! */ };#define TCP_BPF_IW 1001/* Set TCP initial congestion window */
tools/testing/selftests/bpf/Makefile +2 -2
@@ -14,13 +14,13 @@ CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(GENDIR) $(GENFLAGS) -I../../../i LDLIBS += -lcap -lelf -lrt TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ - test_align test_verifier_log test_dev_cgroup + test_align test_verifier_log test_dev_cgroup test_tcpbpf_user TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \ sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \ test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \ - sample_map_ret0.o + sample_map_ret0.o test_tcpbpf_kern.o TEST_PROGS := test_kmod.sh test_xdp_redirect.sh test_xdp_meta.sh \ test_offload.py
tools/testing/selftests/bpf/bpf_helpers.h +2 -0
@@ -71,6 +71,8 @@ static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,static int (*bpf_getsockopt)(void *ctx,int level,int optname,void *optval,int optlen) = (void *) BPF_FUNC_getsockopt; +static int (*bpf_sock_ops_cb_flags_set)(void *ctx,int flags) = + (void *) BPF_FUNC_sock_ops_cb_flags_set;static int (*bpf_sk_redirect_map)(void *ctx,void *map,int key,int flags) = (void *) BPF_FUNC_sk_redirect_map; static int (*bpf_sock_map_update)(void *map, void *key, void *value,
tools/testing/selftests/bpf/tcp_client.py new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/bpf/tcp_server.py new file +79 -0
79 lines not on this page · raw message
tools/testing/selftests/bpf/test_tcpbpf.h new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#ifndef _TEST_TCPBPF_H +#define _TEST_TCPBPF_H + +struct tcpbpf_globals { +__u32 event_map; +__u32 total_retrans; +__u32 data_segs_in; +__u32 data_segs_out; +__u32 bad_cb_test_rv; +__u32 good_cb_test_rv; +__u64 bytes_received; +__u64 bytes_acked; +}; +#endif
tools/testing/selftests/bpf/test_tcpbpf_kern.c new file +131 -0
131 lines not on this page · raw message
tools/testing/selftests/bpf/test_tcpbpf_user.c new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [PATCH bpf-next v6 11/11] bpf: add selftest for tcpbpf
Quoted 7 lines · Lawrence Brakmo
Added a selftest for tcpbpf (
sock_ops) that checks that the appropriate callbacks occured and that it can accesstcp_sockfields and that their values are correct.Run with command: ./test_tcpbpf_user
Signed-off-by Lawrence Brakmo <[email protected]>
...
Quoted 15 lines
tools/testing/selftests/bpf/test_tcpbpf_kern.c new file +15
-0+__u32 key =0 ; +struct tcpbpf_globals g, *gp; + + gp =bpf_map_lookup_elem (&global_map, &key); +if (gp ==NULL ) { +struct tcpbpf_globals g = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + + g.event_map |= (1 << event); +bpf_map_update_elem (&global_map, &key, &g, +BPF_ANY ); + }else { + g = *gp; + g.event_map |= (1 << event); +bpf_map_update_elem (&global_map, &key, &g, +BPF_ANY );
...
tools/testing/selftests/bpf/test_tcpbpf_kern.c line 90
Quoted 11 lines
tools/testing/selftests/bpf/test_tcpbpf_kern.c new file +11
-0+__u32 key =0 ; +struct tcpbpf_globals g, *gp; + + gp =bpf_map_lookup_elem (&global_map, &key); +if (!gp) +break ; + g = *gp; + g.bad_cb_test_rv = bad_call_rv; + g.good_cb_test_rv = good_call_rv; +bpf_map_update_elem (&global_map, &key, &g, +BPF_ANY );
since 'g' is an array of one element and the tests designed for single flow anyway, there is no need to use map_update_elem. the program can directly assign into fields like:
gp->bad_cb_test_rv = bad_call_rv; gp->good_cb_test_rv = good_call_rv;
probably not worth respining just for that. Mainly fyi.