When an error occurs in bpf_tcp_ops.{enqueue,dequeue}_rcvq(), we want to clear BPF_SOCK_OPS_RCVQ_CB_FLAG to stop invoking the callbacks. In addition, bpf_sock_ops_cb_flags_set() is often used during setup or just after 3WHS completes to enable opt-in hooks. Let's support bpf_sock_ops_cb_flags_set() in the following callbacks: connect, listen, {active,passive}_established, {enqueue,dequeue}_rcvq. Signed-off-by: Kuniyuki Iwashima Acked-by: Stanislav Fomichev --- include/uapi/linux/bpf.h | 2 +- net/ipv4/bpf_tcp_ops.c | 27 +++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index fe122242b096..8cdf22667775 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3264,7 +3264,7 @@ union bpf_attr { * Return * 0 * - * long bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) + * long bpf_sock_ops_cb_flags_set(void *bpf_sock, int argval) * Description * Attempt to set the value of the **bpf_sock_ops_cb_flags** field * for the full TCP socket associated to *bpf_sock_ops* to diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c index c963e2cc21b5..4b48711d92a2 100644 --- a/net/ipv4/bpf_tcp_ops.c +++ b/net/ipv4/bpf_tcp_ops.c @@ -220,6 +220,24 @@ const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = { .ret_type = RET_INTEGER, }; +BPF_CALL_2(bpf_tcp_ops_cb_flags_set, struct sock *, sk, int, argval) +{ + int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; + + tcp_sk(sk)->bpf_sock_ops_cb_flags = val; + + return argval & ~BPF_SOCK_OPS_ALL_CB_FLAGS; +} + +static const struct bpf_func_proto bpf_tcp_ops_cb_flags_set_proto = { + .func = bpf_tcp_ops_cb_flags_set, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP], + .arg2_type = ARG_ANYTHING, +}; + static bool is_sockopt_supported(u32 moff) { switch (moff) { @@ -274,6 +292,15 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) if (moff == offsetof(struct bpf_tcp_ops, write_hdr_opt)) return &bpf_tcp_ops_store_hdr_opt_proto; return NULL; + case BPF_FUNC_sock_ops_cb_flags_set: + if (moff == offsetof(struct bpf_tcp_ops, connect) || + moff == offsetof(struct bpf_tcp_ops, listen) || + moff == offsetof(struct bpf_tcp_ops, active_established) || + moff == offsetof(struct bpf_tcp_ops, passive_established) || + moff == offsetof(struct bpf_tcp_ops, enqueue_rcvq) || + moff == offsetof(struct bpf_tcp_ops, dequeue_rcvq)) + return &bpf_tcp_ops_cb_flags_set_proto; + return NULL; default: return bpf_base_func_proto(func_id, prog); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index fe122242b096..8cdf22667775 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3264,7 +3264,7 @@ union bpf_attr { * Return * 0 * - * long bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) + * long bpf_sock_ops_cb_flags_set(void *bpf_sock, int argval) * Description * Attempt to set the value of the **bpf_sock_ops_cb_flags** field * for the full TCP socket associated to *bpf_sock_ops* to -- 2.56.0.rc1.315.gc6ed9934b7-goog