Skip to content

Commit b9b6b68

Browse files
Björn TöpelAlexei Starovoitov
authored andcommitted
xsk: add Rx queue setup and mmap support
Another setsockopt (XDP_RX_QUEUE) is added to let the process allocate a queue, where the kernel can pass completed Rx frames from the kernel to user process. The mmapping of the queue is done using the XDP_PGOFF_RX_QUEUE offset. Signed-off-by: Björn Töpel <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 423f383 commit b9b6b68

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

include/net/xdp_sock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
#include <linux/mutex.h>
1919
#include <net/sock.h>
2020

21+
struct net_device;
22+
struct xsk_queue;
2123
struct xdp_umem;
2224

2325
struct xdp_sock {
2426
/* struct sock must be the first member of struct xdp_sock */
2527
struct sock sk;
28+
struct xsk_queue *rx;
29+
struct net_device *dev;
2630
struct xdp_umem *umem;
2731
/* Protects multiple processes in the control path */
2832
struct mutex mutex;

include/uapi/linux/if_xdp.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/types.h>
2323

2424
/* XDP socket options */
25+
#define XDP_RX_RING 1
2526
#define XDP_UMEM_REG 3
2627
#define XDP_UMEM_FILL_RING 4
2728

@@ -33,13 +34,28 @@ struct xdp_umem_reg {
3334
};
3435

3536
/* Pgoff for mmaping the rings */
37+
#define XDP_PGOFF_RX_RING 0
3638
#define XDP_UMEM_PGOFF_FILL_RING 0x100000000
3739

40+
struct xdp_desc {
41+
__u32 idx;
42+
__u32 len;
43+
__u16 offset;
44+
__u8 flags;
45+
__u8 padding[5];
46+
};
47+
3848
struct xdp_ring {
3949
__u32 producer __attribute__((aligned(64)));
4050
__u32 consumer __attribute__((aligned(64)));
4151
};
4252

53+
/* Used for the RX and TX queues for packets */
54+
struct xdp_rxtx_ring {
55+
struct xdp_ring ptrs;
56+
struct xdp_desc desc[0] __attribute__((aligned(64)));
57+
};
58+
4359
/* Used for the fill and completion queues for buffers */
4460
struct xdp_umem_ring {
4561
struct xdp_ring ptrs;

net/xdp/xsk.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/net.h>
3232
#include <linux/netdevice.h>
3333
#include <net/xdp_sock.h>
34+
#include <net/xdp.h>
3435

3536
#include "xsk_queue.h"
3637
#include "xdp_umem.h"
@@ -40,14 +41,15 @@ static struct xdp_sock *xdp_sk(struct sock *sk)
4041
return (struct xdp_sock *)sk;
4142
}
4243

43-
static int xsk_init_queue(u32 entries, struct xsk_queue **queue)
44+
static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
45+
bool umem_queue)
4446
{
4547
struct xsk_queue *q;
4648

4749
if (entries == 0 || *queue || !is_power_of_2(entries))
4850
return -EINVAL;
4951

50-
q = xskq_create(entries);
52+
q = xskq_create(entries, umem_queue);
5153
if (!q)
5254
return -ENOMEM;
5355

@@ -89,6 +91,22 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
8991
return -ENOPROTOOPT;
9092

9193
switch (optname) {
94+
case XDP_RX_RING:
95+
{
96+
struct xsk_queue **q;
97+
int entries;
98+
99+
if (optlen < sizeof(entries))
100+
return -EINVAL;
101+
if (copy_from_user(&entries, optval, sizeof(entries)))
102+
return -EFAULT;
103+
104+
mutex_lock(&xs->mutex);
105+
q = &xs->rx;
106+
err = xsk_init_queue(entries, q, false);
107+
mutex_unlock(&xs->mutex);
108+
return err;
109+
}
92110
case XDP_UMEM_REG:
93111
{
94112
struct xdp_umem_reg mr;
@@ -130,7 +148,7 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
130148

131149
mutex_lock(&xs->mutex);
132150
q = &xs->umem->fq;
133-
err = xsk_init_queue(entries, q);
151+
err = xsk_init_queue(entries, q, true);
134152
mutex_unlock(&xs->mutex);
135153
return err;
136154
}
@@ -151,13 +169,17 @@ static int xsk_mmap(struct file *file, struct socket *sock,
151169
unsigned long pfn;
152170
struct page *qpg;
153171

154-
if (!xs->umem)
155-
return -EINVAL;
172+
if (offset == XDP_PGOFF_RX_RING) {
173+
q = xs->rx;
174+
} else {
175+
if (!xs->umem)
176+
return -EINVAL;
156177

157-
if (offset == XDP_UMEM_PGOFF_FILL_RING)
158-
q = xs->umem->fq;
159-
else
160-
return -EINVAL;
178+
if (offset == XDP_UMEM_PGOFF_FILL_RING)
179+
q = xs->umem->fq;
180+
else
181+
return -EINVAL;
182+
}
161183

162184
if (!q)
163185
return -EINVAL;
@@ -205,6 +227,7 @@ static void xsk_destruct(struct sock *sk)
205227
if (!sock_flag(sk, SOCK_DEAD))
206228
return;
207229

230+
xskq_destroy(xs->rx);
208231
xdp_put_umem(xs->umem);
209232

210233
sk_refcnt_debug_dec(sk);

net/xdp/xsk_queue.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
2121
return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u32);
2222
}
2323

24-
struct xsk_queue *xskq_create(u32 nentries)
24+
static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q)
25+
{
26+
return (sizeof(struct xdp_ring) +
27+
q->nentries * sizeof(struct xdp_desc));
28+
}
29+
30+
struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
2531
{
2632
struct xsk_queue *q;
2733
gfp_t gfp_flags;
@@ -36,7 +42,8 @@ struct xsk_queue *xskq_create(u32 nentries)
3642

3743
gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
3844
__GFP_COMP | __GFP_NORETRY;
39-
size = xskq_umem_get_ring_size(q);
45+
size = umem_queue ? xskq_umem_get_ring_size(q) :
46+
xskq_rxtx_get_ring_size(q);
4047

4148
q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
4249
get_order(size));

net/xdp/xsk_queue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct xsk_queue {
3232
u64 invalid_descs;
3333
};
3434

35-
struct xsk_queue *xskq_create(u32 nentries);
35+
struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
3636
void xskq_destroy(struct xsk_queue *q);
3737

3838
#endif /* _LINUX_XSK_QUEUE_H */

0 commit comments

Comments
 (0)