Commit 1d0dc069 authored by Björn Töpel's avatar Björn Töpel Committed by Daniel Borkmann

net: xsk: track AF_XDP sockets on a per-netns list

Track each AF_XDP socket in a per-netns list. This will be used later
by the sock_diag interface for querying sockets from userspace.
Signed-off-by: default avatarBjörn Töpel <bjorn.topel@intel.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 2f092126
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <net/netns/xfrm.h> #include <net/netns/xfrm.h>
#include <net/netns/mpls.h> #include <net/netns/mpls.h>
#include <net/netns/can.h> #include <net/netns/can.h>
#include <net/netns/xdp.h>
#include <linux/ns_common.h> #include <linux/ns_common.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
...@@ -160,6 +161,9 @@ struct net { ...@@ -160,6 +161,9 @@ struct net {
#endif #endif
#if IS_ENABLED(CONFIG_CAN) #if IS_ENABLED(CONFIG_CAN)
struct netns_can can; struct netns_can can;
#endif
#ifdef CONFIG_XDP_SOCKETS
struct netns_xdp xdp;
#endif #endif
struct sock *diag_nlsk; struct sock *diag_nlsk;
atomic_t fnhe_genid; atomic_t fnhe_genid;
......
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __NETNS_XDP_H__
#define __NETNS_XDP_H__
#include <linux/rculist.h>
#include <linux/mutex.h>
struct netns_xdp {
struct mutex lock;
struct hlist_head list;
};
#endif /* __NETNS_XDP_H__ */
...@@ -350,6 +350,10 @@ static int xsk_release(struct socket *sock) ...@@ -350,6 +350,10 @@ static int xsk_release(struct socket *sock)
net = sock_net(sk); net = sock_net(sk);
mutex_lock(&net->xdp.lock);
sk_del_node_init_rcu(sk);
mutex_unlock(&net->xdp.lock);
local_bh_disable(); local_bh_disable();
sock_prot_inuse_add(net, sk->sk_prot, -1); sock_prot_inuse_add(net, sk->sk_prot, -1);
local_bh_enable(); local_bh_enable();
...@@ -746,6 +750,10 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol, ...@@ -746,6 +750,10 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
mutex_init(&xs->mutex); mutex_init(&xs->mutex);
spin_lock_init(&xs->tx_completion_lock); spin_lock_init(&xs->tx_completion_lock);
mutex_lock(&net->xdp.lock);
sk_add_node_rcu(sk, &net->xdp.list);
mutex_unlock(&net->xdp.lock);
local_bh_disable(); local_bh_disable();
sock_prot_inuse_add(net, &xsk_proto, 1); sock_prot_inuse_add(net, &xsk_proto, 1);
local_bh_enable(); local_bh_enable();
...@@ -759,6 +767,23 @@ static const struct net_proto_family xsk_family_ops = { ...@@ -759,6 +767,23 @@ static const struct net_proto_family xsk_family_ops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
}; };
static int __net_init xsk_net_init(struct net *net)
{
mutex_init(&net->xdp.lock);
INIT_HLIST_HEAD(&net->xdp.list);
return 0;
}
static void __net_exit xsk_net_exit(struct net *net)
{
WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
}
static struct pernet_operations xsk_net_ops = {
.init = xsk_net_init,
.exit = xsk_net_exit,
};
static int __init xsk_init(void) static int __init xsk_init(void)
{ {
int err; int err;
...@@ -771,8 +796,13 @@ static int __init xsk_init(void) ...@@ -771,8 +796,13 @@ static int __init xsk_init(void)
if (err) if (err)
goto out_proto; goto out_proto;
err = register_pernet_subsys(&xsk_net_ops);
if (err)
goto out_sk;
return 0; return 0;
out_sk:
sock_unregister(PF_XDP);
out_proto: out_proto:
proto_unregister(&xsk_proto); proto_unregister(&xsk_proto);
out: out:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment