From: Eric Dumazet Subject: Re: [PATCH bpf 1/4] net: add SO_NETNS_COOKIE socket option Date: Wed, 10 Feb 2021 15:51:32 +0100 Message-ID: References: <20210210120425.53438-1-lmb@cloudflare.com> <20210210120425.53438-2-lmb@cloudflare.com> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=zjhXKo3lnsUCaAO03EAAKy7XedvCevX8vo7EKjAoj3Q=; b=jJ8LF025NOB5tTRRmh45iSwVNW9BiH2jesDquvAlwBgGfOycUUj9SneIWmt0+ZsKNg G4jfNkdmBdVwNrf6KziMjZyDhDfsh0VTabiZ88CqjMSEyWAAB9KcF0LqWF574ojQJWfB uEcBjTGL5J1sN7VltBi5masq/ykxXWJ71FekRuG1mMOVHDXcYAaB69OZ0pm/EZyGUojM E8ghijNjy9n51CKDhciWDBttcc7mGU9VLnekLrNRsHhHEJw6eo3kNbDjNnFbJryP9S1m HqCjtV55tT0SgA+5jTrOqavLEvYCCRxhlE/v9SL/9kkxp+24Qjc3js9+kLYBwSORR2i+ /jyg== In-Reply-To: <20210210120425.53438-2-lmb@cloudflare.com> Content-Language: en-US List-ID: Content-Type: text/plain; charset="us-ascii" To: Lorenz Bauer , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Jakub Kicinski Cc: kernel-team@cloudflare.com, linux-api@vger.kernel.org, linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org On 2/10/21 1:04 PM, Lorenz Bauer wrote: > We need to distinguish which network namespace a socket belongs to. > BPF has the useful bpf_get_netns_cookie helper for this, but accessing > it from user space isn't possible. Add a read-only socket option that > returns the netns cookie, similar to SO_COOKIE. If network namespaces > are disabled, SO_NETNS_COOKIE returns the cookie of init_net. > > The BPF helpers change slightly: instead of returning 0 when network > namespaces are disabled we return the init_net cookie as for the > socket option. > > Cc: linux-api@vger.kernel.org > Signed-off-by: Lorenz Bauer > --- ... > > +static inline u64 __sock_gen_netns_cookie(struct sock *sk) > +{ > +#ifdef CONFIG_NET_NS > + return __net_gen_cookie(sk->sk_net.net); > +#else > + return __net_gen_cookie(&init_net); > +#endif > +} > + > +static inline u64 sock_gen_netns_cookie(struct sock *sk) > +{ > + u64 cookie; > + > + preempt_disable(); > + cookie = __sock_gen_netns_cookie(sk); > + preempt_enable(); > + > + return cookie; > +} > + > I suggest we make net->net_cookie a mere u64 initialized in setup_net(), instead of having to preempt_disable() around reading it. (Here and in your patch 2/4) Your patches would be much simpler. Cleanup patch : https://patchwork.kernel.org/project/netdevbpf/patch/20210210144144.24284-1-eric.dumazet@gmail.com/