From 4fa08afda6726cf4511ef0a2090e6ac482726e2d Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Wed, 13 Nov 2024 17:34:15 +0800 Subject: [PATCH] bpf_mptcp_ssk_cast Signed-off-by: Geliang Tang --- include/uapi/linux/bpf.h | 7 +++++++ net/mptcp/bpf.c | 16 ++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4162afc6b5d0..0f105ff47ca0 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5795,6 +5795,12 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * void *bpf_mptcp_ssk_cast(void *sk) + * Description + * Dynamically cast a *sk* pointer to a *sock* pointer. + * Return + * *sk* if casting is valid, or **NULL** otherwise. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6009,6 +6015,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(mptcp_ssk_cast, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 5a491fe069e5..396157b127a6 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -281,6 +281,20 @@ static struct bpf_struct_ops bpf_mptcp_pm_ops = { /* MPTCP BPF packet scheduler */ +BPF_CALL_1(bpf_mptcp_ssk_cast, struct sock *, sk) +{ + BTF_TYPE_EMIT(struct sock); + return (unsigned long)sk; +} + +static const struct bpf_func_proto bpf_mptcp_ssk_cast_proto = { + .func = bpf_mptcp_ssk_cast, + .gpl_only = false, + .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, + .arg1_type = ARG_ANYTHING, + .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], +}; + static const struct bpf_func_proto * bpf_mptcp_sched_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) @@ -294,6 +308,8 @@ bpf_mptcp_sched_get_func_proto(enum bpf_func_id func_id, return &bpf_skc_to_tcp6_sock_proto; case BPF_FUNC_skc_to_tcp_sock: return &bpf_skc_to_tcp_sock_proto; + case BPF_FUNC_mptcp_ssk_cast: + return &bpf_mptcp_ssk_cast_proto; 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 4162afc6b5d0..0f105ff47ca0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5795,6 +5795,12 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * void *bpf_mptcp_ssk_cast(void *sk) + * Description + * Dynamically cast a *sk* pointer to a *sock* pointer. + * Return + * *sk* if casting is valid, or **NULL** otherwise. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6009,6 +6015,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(mptcp_ssk_cast, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't