Skip to content

Commit b13d880

Browse files
Lawrence BrakmoAlexei Starovoitov
Lawrence Brakmo
authored and
Alexei Starovoitov
committed
bpf: Adds field bpf_sock_ops_cb_flags to tcp_sock
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. 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 Signed-off-by: Lawrence Brakmo <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent de525be commit b13d880

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

include/linux/tcp.h

+11
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,17 @@ struct tcp_sock {
335335

336336
int linger2;
337337

338+
339+
/* Sock_ops bpf program related variables */
340+
#ifdef CONFIG_BPF
341+
u8 bpf_sock_ops_cb_flags; /* Control calling BPF programs
342+
* values defined in uapi/linux/tcp.h
343+
*/
344+
#define BPF_SOCK_OPS_TEST_FLAG(TP, ARG) (TP->bpf_sock_ops_cb_flags & ARG)
345+
#else
346+
#define BPF_SOCK_OPS_TEST_FLAG(TP, ARG) 0
347+
#endif
348+
338349
/* Receiver side RTT estimation */
339350
struct {
340351
u32 rtt_us;

include/uapi/linux/bpf.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ union bpf_attr {
642642
* @optlen: length of optval in bytes
643643
* Return: 0 or negative error
644644
*
645+
* int bpf_sock_ops_cb_flags_set(bpf_sock_ops, flags)
646+
* Set callback flags for sock_ops
647+
* @bpf_sock_ops: pointer to bpf_sock_ops_kern struct
648+
* @flags: flags value
649+
* Return: 0 for no error
650+
* -EINVAL if there is no full tcp socket
651+
* bits in flags that are not supported by current kernel
652+
*
645653
* int bpf_skb_adjust_room(skb, len_diff, mode, flags)
646654
* Grow or shrink room in sk_buff.
647655
* @skb: pointer to skb
@@ -748,7 +756,8 @@ union bpf_attr {
748756
FN(perf_event_read_value), \
749757
FN(perf_prog_read_value), \
750758
FN(getsockopt), \
751-
FN(override_return),
759+
FN(override_return), \
760+
FN(sock_ops_cb_flags_set),
752761

753762
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
754763
* function eBPF program intends to call
@@ -969,8 +978,14 @@ struct bpf_sock_ops {
969978
*/
970979
__u32 snd_cwnd;
971980
__u32 srtt_us; /* Averaged RTT << 3 in usecs */
981+
__u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */
972982
};
973983

984+
/* Definitions for bpf_sock_ops_cb_flags */
985+
#define BPF_SOCK_OPS_ALL_CB_FLAGS 0 /* Mask of all currently
986+
* supported cb flags
987+
*/
988+
974989
/* List of known BPF sock_ops operators.
975990
* New entries can only be added at the end
976991
*/

net/core/filter.c

+34
Original file line numberDiff line numberDiff line change
@@ -3328,6 +3328,33 @@ static const struct bpf_func_proto bpf_getsockopt_proto = {
33283328
.arg5_type = ARG_CONST_SIZE,
33293329
};
33303330

3331+
BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3332+
int, argval)
3333+
{
3334+
struct sock *sk = bpf_sock->sk;
3335+
int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3336+
3337+
if (!sk_fullsock(sk))
3338+
return -EINVAL;
3339+
3340+
#ifdef CONFIG_INET
3341+
if (val)
3342+
tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3343+
3344+
return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3345+
#else
3346+
return -EINVAL;
3347+
#endif
3348+
}
3349+
3350+
static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3351+
.func = bpf_sock_ops_cb_flags_set,
3352+
.gpl_only = false,
3353+
.ret_type = RET_INTEGER,
3354+
.arg1_type = ARG_PTR_TO_CTX,
3355+
.arg2_type = ARG_ANYTHING,
3356+
};
3357+
33313358
static const struct bpf_func_proto *
33323359
bpf_base_func_proto(enum bpf_func_id func_id)
33333360
{
@@ -3510,6 +3537,8 @@ static const struct bpf_func_proto *
35103537
return &bpf_setsockopt_proto;
35113538
case BPF_FUNC_getsockopt:
35123539
return &bpf_getsockopt_proto;
3540+
case BPF_FUNC_sock_ops_cb_flags_set:
3541+
return &bpf_sock_ops_cb_flags_set_proto;
35133542
case BPF_FUNC_sock_map_update:
35143543
return &bpf_sock_map_update_proto;
35153544
default:
@@ -4546,6 +4575,11 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
45464575
case offsetof(struct bpf_sock_ops, srtt_us):
45474576
SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
45484577
break;
4578+
4579+
case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
4580+
SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
4581+
struct tcp_sock);
4582+
break;
45494583
}
45504584
return insn - insn_buf;
45514585
}

0 commit comments

Comments
 (0)