From: Xu Liu <liuxu623@gmail.com>
Date: Fri, 20 Aug 2021 15:17:11 +0800
Subject: bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_SK_MSG
Patch-mainline: v5.15-rc1
Git-commit: fab60e29fcc6d60396da20d63d45fd0d305ba4e4
References: jsc#PED-1377
We'd like to be able to identify netns from sk_msg hooks
to accelerate local process communication form different netns.
Signed-off-by: Xu Liu <liuxu623@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210820071712.52852-2-liuxu623@gmail.com
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
---
net/core/filter.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4705,6 +4705,18 @@ static const struct bpf_func_proto bpf_g
.arg1_type = ARG_PTR_TO_CTX_OR_NULL,
};
+BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
+{
+ return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
+}
+
+static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
+ .func = bpf_get_netns_cookie_sk_msg,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
+};
+
BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
{
struct sock *sk = sk_to_full_sk(skb->sk);
@@ -7568,6 +7580,8 @@ sk_msg_func_proto(enum bpf_func_id func_
return &bpf_sk_storage_get_proto;
case BPF_FUNC_sk_storage_delete:
return &bpf_sk_storage_delete_proto;
+ case BPF_FUNC_get_netns_cookie:
+ return &bpf_get_netns_cookie_sk_msg_proto;
#ifdef CONFIG_CGROUPS
case BPF_FUNC_get_current_cgroup_id:
return &bpf_get_current_cgroup_id_proto;