Skip to content

Commit 423f383

Browse files
magnus-karlssonAlexei Starovoitov
authored andcommitted
xsk: add umem fill queue support and mmap
Here, we add another setsockopt for registered user memory (umem) called XDP_UMEM_FILL_QUEUE. Using this socket option, the process can ask the kernel to allocate a queue (ring buffer) and also mmap it (XDP_UMEM_PGOFF_FILL_QUEUE) into the process. The queue is used to explicitly pass ownership of umem frames from the user process to the kernel. These frames will in a later patch be filled in with Rx packet data by the kernel. v2: Fixed potential crash in xsk_mmap. Signed-off-by: Magnus Karlsson <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c0c77d8 commit 423f383

File tree

7 files changed

+183
-2
lines changed

7 files changed

+183
-2
lines changed

include/uapi/linux/if_xdp.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/* XDP socket options */
2525
#define XDP_UMEM_REG 3
26+
#define XDP_UMEM_FILL_RING 4
2627

2728
struct xdp_umem_reg {
2829
__u64 addr; /* Start of packet data area */
@@ -31,4 +32,18 @@ struct xdp_umem_reg {
3132
__u32 frame_headroom; /* Frame head room */
3233
};
3334

35+
/* Pgoff for mmaping the rings */
36+
#define XDP_UMEM_PGOFF_FILL_RING 0x100000000
37+
38+
struct xdp_ring {
39+
__u32 producer __attribute__((aligned(64)));
40+
__u32 consumer __attribute__((aligned(64)));
41+
};
42+
43+
/* Used for the fill and completion queues for buffers */
44+
struct xdp_umem_ring {
45+
struct xdp_ring ptrs;
46+
__u32 desc[0] __attribute__((aligned(64)));
47+
};
48+
3449
#endif /* _LINUX_IF_XDP_H */

net/xdp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o
1+
obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o
22

net/xdp/xdp_umem.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ static void xdp_umem_release(struct xdp_umem *umem)
6565
struct task_struct *task;
6666
struct mm_struct *mm;
6767

68+
if (umem->fq) {
69+
xskq_destroy(umem->fq);
70+
umem->fq = NULL;
71+
}
72+
6873
if (umem->pgs) {
6974
xdp_umem_unpin_pages(umem);
7075

net/xdp/xdp_umem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#include <linux/if_xdp.h>
2020
#include <linux/workqueue.h>
2121

22+
#include "xsk_queue.h"
2223
#include "xdp_umem_props.h"
2324

2425
struct xdp_umem {
26+
struct xsk_queue *fq;
2527
struct page **pgs;
2628
struct xdp_umem_props props;
2729
u32 npgs;

net/xdp/xsk.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@
3232
#include <linux/netdevice.h>
3333
#include <net/xdp_sock.h>
3434

35+
#include "xsk_queue.h"
3536
#include "xdp_umem.h"
3637

3738
static struct xdp_sock *xdp_sk(struct sock *sk)
3839
{
3940
return (struct xdp_sock *)sk;
4041
}
4142

43+
static int xsk_init_queue(u32 entries, struct xsk_queue **queue)
44+
{
45+
struct xsk_queue *q;
46+
47+
if (entries == 0 || *queue || !is_power_of_2(entries))
48+
return -EINVAL;
49+
50+
q = xskq_create(entries);
51+
if (!q)
52+
return -ENOMEM;
53+
54+
*queue = q;
55+
return 0;
56+
}
57+
4258
static int xsk_release(struct socket *sock)
4359
{
4460
struct sock *sk = sock->sk;
@@ -101,13 +117,60 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
101117
mutex_unlock(&xs->mutex);
102118
return 0;
103119
}
120+
case XDP_UMEM_FILL_RING:
121+
{
122+
struct xsk_queue **q;
123+
int entries;
124+
125+
if (!xs->umem)
126+
return -EINVAL;
127+
128+
if (copy_from_user(&entries, optval, sizeof(entries)))
129+
return -EFAULT;
130+
131+
mutex_lock(&xs->mutex);
132+
q = &xs->umem->fq;
133+
err = xsk_init_queue(entries, q);
134+
mutex_unlock(&xs->mutex);
135+
return err;
136+
}
104137
default:
105138
break;
106139
}
107140

108141
return -ENOPROTOOPT;
109142
}
110143

144+
static int xsk_mmap(struct file *file, struct socket *sock,
145+
struct vm_area_struct *vma)
146+
{
147+
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
148+
unsigned long size = vma->vm_end - vma->vm_start;
149+
struct xdp_sock *xs = xdp_sk(sock->sk);
150+
struct xsk_queue *q = NULL;
151+
unsigned long pfn;
152+
struct page *qpg;
153+
154+
if (!xs->umem)
155+
return -EINVAL;
156+
157+
if (offset == XDP_UMEM_PGOFF_FILL_RING)
158+
q = xs->umem->fq;
159+
else
160+
return -EINVAL;
161+
162+
if (!q)
163+
return -EINVAL;
164+
165+
qpg = virt_to_head_page(q->ring);
166+
if (size > (PAGE_SIZE << compound_order(qpg)))
167+
return -EINVAL;
168+
169+
pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
170+
return remap_pfn_range(vma, vma->vm_start, pfn,
171+
size, vma->vm_page_prot);
172+
}
173+
111174
static struct proto xsk_proto = {
112175
.name = "XDP",
113176
.owner = THIS_MODULE,
@@ -131,7 +194,7 @@ static const struct proto_ops xsk_proto_ops = {
131194
.getsockopt = sock_no_getsockopt,
132195
.sendmsg = sock_no_sendmsg,
133196
.recvmsg = sock_no_recvmsg,
134-
.mmap = sock_no_mmap,
197+
.mmap = xsk_mmap,
135198
.sendpage = sock_no_sendpage,
136199
};
137200

net/xdp/xsk_queue.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* XDP user-space ring structure
3+
* Copyright(c) 2018 Intel Corporation.
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms and conditions of the GNU General Public License,
7+
* version 2, as published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12+
* more details.
13+
*/
14+
15+
#include <linux/slab.h>
16+
17+
#include "xsk_queue.h"
18+
19+
static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
20+
{
21+
return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u32);
22+
}
23+
24+
struct xsk_queue *xskq_create(u32 nentries)
25+
{
26+
struct xsk_queue *q;
27+
gfp_t gfp_flags;
28+
size_t size;
29+
30+
q = kzalloc(sizeof(*q), GFP_KERNEL);
31+
if (!q)
32+
return NULL;
33+
34+
q->nentries = nentries;
35+
q->ring_mask = nentries - 1;
36+
37+
gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
38+
__GFP_COMP | __GFP_NORETRY;
39+
size = xskq_umem_get_ring_size(q);
40+
41+
q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
42+
get_order(size));
43+
if (!q->ring) {
44+
kfree(q);
45+
return NULL;
46+
}
47+
48+
return q;
49+
}
50+
51+
void xskq_destroy(struct xsk_queue *q)
52+
{
53+
if (!q)
54+
return;
55+
56+
page_frag_free(q->ring);
57+
kfree(q);
58+
}

net/xdp/xsk_queue.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
* XDP user-space ring structure
3+
* Copyright(c) 2018 Intel Corporation.
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms and conditions of the GNU General Public License,
7+
* version 2, as published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12+
* more details.
13+
*/
14+
15+
#ifndef _LINUX_XSK_QUEUE_H
16+
#define _LINUX_XSK_QUEUE_H
17+
18+
#include <linux/types.h>
19+
#include <linux/if_xdp.h>
20+
21+
#include "xdp_umem_props.h"
22+
23+
struct xsk_queue {
24+
struct xdp_umem_props umem_props;
25+
u32 ring_mask;
26+
u32 nentries;
27+
u32 prod_head;
28+
u32 prod_tail;
29+
u32 cons_head;
30+
u32 cons_tail;
31+
struct xdp_ring *ring;
32+
u64 invalid_descs;
33+
};
34+
35+
struct xsk_queue *xskq_create(u32 nentries);
36+
void xskq_destroy(struct xsk_queue *q);
37+
38+
#endif /* _LINUX_XSK_QUEUE_H */

0 commit comments

Comments
 (0)