Skip to content

Commit dbbd79a

Browse files
committed
Merge branch 'af-xdp-sock-diag'
Björn Töpel says: ==================== This series adds an AF_XDP sock_diag interface for querying sockets from user-space. Tools like iproute2 ss(8) can use this interface to list open AF_XDP sockets. The diagnostic provides information about the Rx/Tx/fill/completetion rings, umem, memory usage and such. For a complete list, please refer to the xsk_diag.c file. The AF_XDP sock_diag interface is optional, and can be built as a module. A separate patch series, adding ss(8) iproute2 support, will follow. v1->v2: * Removed extra newline * Zero-out all user-space facing structures prior setting the members * Added explicit "pad" member in _msg struct * Removed unused variable "req" in xsk_diag_handler_dump() Thanks to Daniel for reviewing the series! ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents 2f09212 + a36b38a commit dbbd79a

10 files changed

Lines changed: 346 additions & 5 deletions

File tree

‎include/net/net_namespace.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <net/netns/xfrm.h>
3232
#include <net/netns/mpls.h>
3333
#include <net/netns/can.h>
34+
#include <net/netns/xdp.h>
3435
#include <linux/ns_common.h>
3536
#include <linux/idr.h>
3637
#include <linux/skbuff.h>
@@ -160,6 +161,9 @@ struct net {
160161
#endif
161162
#if IS_ENABLED(CONFIG_CAN)
162163
struct netns_can can;
164+
#endif
165+
#ifdef CONFIG_XDP_SOCKETS
166+
struct netns_xdp xdp;
163167
#endif
164168
struct sock *diag_nlsk;
165169
atomic_t fnhe_genid;

‎include/net/netns/xdp.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __NETNS_XDP_H__
3+
#define __NETNS_XDP_H__
4+
5+
#include <linux/rculist.h>
6+
#include <linux/mutex.h>
7+
8+
struct netns_xdp {
9+
struct mutex lock;
10+
struct hlist_head list;
11+
};
12+
13+
#endif /* __NETNS_XDP_H__ */

‎include/net/xdp_sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct xdp_umem {
4242
struct work_struct work;
4343
struct page **pgs;
4444
u32 npgs;
45+
int id;
4546
struct net_device *dev;
4647
struct xdp_umem_fq_reuse *fq_reuse;
4748
u16 queue_id;

‎include/uapi/linux/xdp_diag.h‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* xdp_diag: interface for query/monitor XDP sockets
4+
* Copyright(c) 2019 Intel Corporation.
5+
*/
6+
7+
#ifndef _LINUX_XDP_DIAG_H
8+
#define _LINUX_XDP_DIAG_H
9+
10+
#include <linux/types.h>
11+
12+
struct xdp_diag_req {
13+
__u8 sdiag_family;
14+
__u8 sdiag_protocol;
15+
__u16 pad;
16+
__u32 xdiag_ino;
17+
__u32 xdiag_show;
18+
__u32 xdiag_cookie[2];
19+
};
20+
21+
struct xdp_diag_msg {
22+
__u8 xdiag_family;
23+
__u8 xdiag_type;
24+
__u16 pad;
25+
__u32 xdiag_ino;
26+
__u32 xdiag_cookie[2];
27+
};
28+
29+
#define XDP_SHOW_INFO (1 << 0) /* Basic information */
30+
#define XDP_SHOW_RING_CFG (1 << 1)
31+
#define XDP_SHOW_UMEM (1 << 2)
32+
#define XDP_SHOW_MEMINFO (1 << 3)
33+
34+
enum {
35+
XDP_DIAG_NONE,
36+
XDP_DIAG_INFO,
37+
XDP_DIAG_UID,
38+
XDP_DIAG_RX_RING,
39+
XDP_DIAG_TX_RING,
40+
XDP_DIAG_UMEM,
41+
XDP_DIAG_UMEM_FILL_RING,
42+
XDP_DIAG_UMEM_COMPLETION_RING,
43+
XDP_DIAG_MEMINFO,
44+
__XDP_DIAG_MAX,
45+
};
46+
47+
#define XDP_DIAG_MAX (__XDP_DIAG_MAX - 1)
48+
49+
struct xdp_diag_info {
50+
__u32 ifindex;
51+
__u32 queue_id;
52+
};
53+
54+
struct xdp_diag_ring {
55+
__u32 entries; /*num descs */
56+
};
57+
58+
#define XDP_DU_F_ZEROCOPY (1 << 0)
59+
60+
struct xdp_diag_umem {
61+
__u64 size;
62+
__u32 id;
63+
__u32 num_pages;
64+
__u32 chunk_size;
65+
__u32 headroom;
66+
__u32 ifindex;
67+
__u32 queue_id;
68+
__u32 flags;
69+
__u32 refs;
70+
};
71+
72+
#endif /* _LINUX_XDP_DIAG_H */

‎net/xdp/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ config XDP_SOCKETS
55
help
66
XDP sockets allows a channel between XDP programs and
77
userspace applications.
8+
9+
config XDP_SOCKETS_DIAG
10+
tristate "XDP sockets: monitoring interface"
11+
depends on XDP_SOCKETS
12+
default n
13+
help
14+
Support for PF_XDP sockets monitoring interface used by the ss tool.
15+
If unsure, say Y.

‎net/xdp/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o
2+
obj-$(CONFIG_XDP_SOCKETS_DIAG) += xsk_diag.o

‎net/xdp/xdp_umem.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
#include <linux/mm.h>
1414
#include <linux/netdevice.h>
1515
#include <linux/rtnetlink.h>
16+
#include <linux/idr.h>
1617

1718
#include "xdp_umem.h"
1819
#include "xsk_queue.h"
1920

2021
#define XDP_UMEM_MIN_CHUNK_SIZE 2048
2122

23+
static DEFINE_IDA(umem_ida);
24+
2225
void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs)
2326
{
2427
unsigned long flags;
@@ -183,6 +186,8 @@ static void xdp_umem_release(struct xdp_umem *umem)
183186

184187
xdp_umem_clear_dev(umem);
185188

189+
ida_simple_remove(&umem_ida, umem->id);
190+
186191
if (umem->fq) {
187192
xskq_destroy(umem->fq);
188193
umem->fq = NULL;
@@ -389,8 +394,16 @@ struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr)
389394
if (!umem)
390395
return ERR_PTR(-ENOMEM);
391396

397+
err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL);
398+
if (err < 0) {
399+
kfree(umem);
400+
return ERR_PTR(err);
401+
}
402+
umem->id = err;
403+
392404
err = xdp_umem_reg(umem, mr);
393405
if (err) {
406+
ida_simple_remove(&umem_ida, umem->id);
394407
kfree(umem);
395408
return ERR_PTR(err);
396409
}

‎net/xdp/xsk.c‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@
2727

2828
#include "xsk_queue.h"
2929
#include "xdp_umem.h"
30+
#include "xsk.h"
3031

3132
#define TX_BATCH_SIZE 16
3233

33-
static struct xdp_sock *xdp_sk(struct sock *sk)
34-
{
35-
return (struct xdp_sock *)sk;
36-
}
37-
3834
bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
3935
{
4036
return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
@@ -350,6 +346,10 @@ static int xsk_release(struct socket *sock)
350346

351347
net = sock_net(sk);
352348

349+
mutex_lock(&net->xdp.lock);
350+
sk_del_node_init_rcu(sk);
351+
mutex_unlock(&net->xdp.lock);
352+
353353
local_bh_disable();
354354
sock_prot_inuse_add(net, sk->sk_prot, -1);
355355
local_bh_enable();
@@ -746,6 +746,10 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
746746
mutex_init(&xs->mutex);
747747
spin_lock_init(&xs->tx_completion_lock);
748748

749+
mutex_lock(&net->xdp.lock);
750+
sk_add_node_rcu(sk, &net->xdp.list);
751+
mutex_unlock(&net->xdp.lock);
752+
749753
local_bh_disable();
750754
sock_prot_inuse_add(net, &xsk_proto, 1);
751755
local_bh_enable();
@@ -759,6 +763,23 @@ static const struct net_proto_family xsk_family_ops = {
759763
.owner = THIS_MODULE,
760764
};
761765

766+
static int __net_init xsk_net_init(struct net *net)
767+
{
768+
mutex_init(&net->xdp.lock);
769+
INIT_HLIST_HEAD(&net->xdp.list);
770+
return 0;
771+
}
772+
773+
static void __net_exit xsk_net_exit(struct net *net)
774+
{
775+
WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
776+
}
777+
778+
static struct pernet_operations xsk_net_ops = {
779+
.init = xsk_net_init,
780+
.exit = xsk_net_exit,
781+
};
782+
762783
static int __init xsk_init(void)
763784
{
764785
int err;
@@ -771,8 +792,13 @@ static int __init xsk_init(void)
771792
if (err)
772793
goto out_proto;
773794

795+
err = register_pernet_subsys(&xsk_net_ops);
796+
if (err)
797+
goto out_sk;
774798
return 0;
775799

800+
out_sk:
801+
sock_unregister(PF_XDP);
776802
out_proto:
777803
proto_unregister(&xsk_proto);
778804
out:

‎net/xdp/xsk.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright(c) 2019 Intel Corporation. */
3+
4+
#ifndef XSK_H_
5+
#define XSK_H_
6+
7+
static inline struct xdp_sock *xdp_sk(struct sock *sk)
8+
{
9+
return (struct xdp_sock *)sk;
10+
}
11+
12+
#endif /* XSK_H_ */

0 commit comments

Comments
 (0)