blob: 3dd99e1c04f548bc1c7ca38d99439bca4366c48f [file] [log] [blame]
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +01001/* net/core/xdp.c
2 *
3 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4 * Released under terms in GPL version 2. See COPYING.
5 */
Jakub Kicinski05296622018-07-11 20:36:40 -07006#include <linux/bpf.h>
7#include <linux/filter.h>
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +01008#include <linux/types.h>
9#include <linux/mm.h>
Jakub Kicinski05296622018-07-11 20:36:40 -070010#include <linux/netdevice.h>
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +020011#include <linux/slab.h>
12#include <linux/idr.h>
13#include <linux/rhashtable.h>
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +020014#include <net/page_pool.h>
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +010015
16#include <net/xdp.h>
17
18#define REG_STATE_NEW 0x0
19#define REG_STATE_REGISTERED 0x1
20#define REG_STATE_UNREGISTERED 0x2
21#define REG_STATE_UNUSED 0x3
22
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +020023static DEFINE_IDA(mem_id_pool);
24static DEFINE_MUTEX(mem_id_lock);
25#define MEM_ID_MAX 0xFFFE
26#define MEM_ID_MIN 1
27static int mem_id_next = MEM_ID_MIN;
28
29static bool mem_id_init; /* false */
30static struct rhashtable *mem_id_ht;
31
32struct xdp_mem_allocator {
33 struct xdp_mem_info mem;
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +020034 union {
35 void *allocator;
36 struct page_pool *page_pool;
Björn Töpel02b55e52018-06-04 14:05:54 +020037 struct zero_copy_allocator *zc_alloc;
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +020038 };
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +020039 struct rhash_head node;
40 struct rcu_head rcu;
41};
42
43static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
44{
45 const u32 *k = data;
46 const u32 key = *k;
47
48 BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id)
49 != sizeof(u32));
50
NeilBrown9f9a7072018-06-18 12:52:50 +100051 /* Use cyclic increasing ID as direct hash key */
52 return key;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +020053}
54
55static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
56 const void *ptr)
57{
58 const struct xdp_mem_allocator *xa = ptr;
59 u32 mem_id = *(u32 *)arg->key;
60
61 return xa->mem.id != mem_id;
62}
63
64static const struct rhashtable_params mem_id_rht_params = {
65 .nelem_hint = 64,
66 .head_offset = offsetof(struct xdp_mem_allocator, node),
67 .key_offset = offsetof(struct xdp_mem_allocator, mem.id),
68 .key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id),
69 .max_size = MEM_ID_MAX,
70 .min_size = 8,
71 .automatic_shrinking = true,
72 .hashfn = xdp_mem_id_hashfn,
73 .obj_cmpfn = xdp_mem_id_cmp,
74};
75
76static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
77{
78 struct xdp_mem_allocator *xa;
79
80 xa = container_of(rcu, struct xdp_mem_allocator, rcu);
81
82 /* Allow this ID to be reused */
83 ida_simple_remove(&mem_id_pool, xa->mem.id);
84
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +020085 /* Notice, driver is expected to free the *allocator,
86 * e.g. page_pool, and MUST also use RCU free.
87 */
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +020088
89 /* Poison memory */
90 xa->mem.id = 0xFFFF;
91 xa->mem.type = 0xF0F0;
92 xa->allocator = (void *)0xDEAD9001;
93
94 kfree(xa);
95}
96
97static void __xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
98{
99 struct xdp_mem_allocator *xa;
100 int id = xdp_rxq->mem.id;
101 int err;
102
103 if (id == 0)
104 return;
105
106 mutex_lock(&mem_id_lock);
107
108 xa = rhashtable_lookup(mem_id_ht, &id, mem_id_rht_params);
109 if (!xa) {
110 mutex_unlock(&mem_id_lock);
111 return;
112 }
113
114 err = rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params);
115 WARN_ON(err);
116
117 call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
118
119 mutex_unlock(&mem_id_lock);
120}
121
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +0100122void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
123{
124 /* Simplify driver cleanup code paths, allow unreg "unused" */
125 if (xdp_rxq->reg_state == REG_STATE_UNUSED)
126 return;
127
128 WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
129
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200130 __xdp_rxq_info_unreg_mem_model(xdp_rxq);
131
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +0100132 xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
133 xdp_rxq->dev = NULL;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200134
135 /* Reset mem info to defaults */
136 xdp_rxq->mem.id = 0;
137 xdp_rxq->mem.type = 0;
Jesper Dangaard Broueraecd67b2018-01-03 11:25:13 +0100138}
139EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
140
141static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
142{
143 memset(xdp_rxq, 0, sizeof(*xdp_rxq));
144}
145
146/* Returns 0 on success, negative on failure */
147int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
148 struct net_device *dev, u32 queue_index)
149{
150 if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
151 WARN(1, "Driver promised not to register this");
152 return -EINVAL;
153 }
154
155 if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
156 WARN(1, "Missing unregister, handled but fix driver");
157 xdp_rxq_info_unreg(xdp_rxq);
158 }
159
160 if (!dev) {
161 WARN(1, "Missing net_device from driver");
162 return -ENODEV;
163 }
164
165 /* State either UNREGISTERED or NEW */
166 xdp_rxq_info_init(xdp_rxq);
167 xdp_rxq->dev = dev;
168 xdp_rxq->queue_index = queue_index;
169
170 xdp_rxq->reg_state = REG_STATE_REGISTERED;
171 return 0;
172}
173EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
174
175void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
176{
177 xdp_rxq->reg_state = REG_STATE_UNUSED;
178}
179EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
Jesper Dangaard Brouerc0124f32018-01-03 11:25:34 +0100180
181bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
182{
183 return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
184}
185EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200186
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200187static int __mem_id_init_hash_table(void)
188{
189 struct rhashtable *rht;
190 int ret;
191
192 if (unlikely(mem_id_init))
193 return 0;
194
195 rht = kzalloc(sizeof(*rht), GFP_KERNEL);
196 if (!rht)
197 return -ENOMEM;
198
199 ret = rhashtable_init(rht, &mem_id_rht_params);
200 if (ret < 0) {
201 kfree(rht);
202 return ret;
203 }
204 mem_id_ht = rht;
205 smp_mb(); /* mutex lock should provide enough pairing */
206 mem_id_init = true;
207
208 return 0;
209}
210
211/* Allocate a cyclic ID that maps to allocator pointer.
212 * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
213 *
214 * Caller must lock mem_id_lock.
215 */
216static int __mem_id_cyclic_get(gfp_t gfp)
217{
218 int retries = 1;
219 int id;
220
221again:
222 id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
223 if (id < 0) {
224 if (id == -ENOSPC) {
225 /* Cyclic allocator, reset next id */
226 if (retries--) {
227 mem_id_next = MEM_ID_MIN;
228 goto again;
229 }
230 }
231 return id; /* errno */
232 }
233 mem_id_next = id + 1;
234
235 return id;
236}
237
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200238static bool __is_supported_mem_type(enum xdp_mem_type type)
239{
240 if (type == MEM_TYPE_PAGE_POOL)
241 return is_page_pool_compiled_in();
242
243 if (type >= MEM_TYPE_MAX)
244 return false;
245
246 return true;
247}
248
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200249int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
250 enum xdp_mem_type type, void *allocator)
251{
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200252 struct xdp_mem_allocator *xdp_alloc;
253 gfp_t gfp = GFP_KERNEL;
254 int id, errno, ret;
255 void *ptr;
256
257 if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
258 WARN(1, "Missing register, driver bug");
259 return -EFAULT;
260 }
261
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200262 if (!__is_supported_mem_type(type))
263 return -EOPNOTSUPP;
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200264
265 xdp_rxq->mem.type = type;
266
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200267 if (!allocator) {
Björn Töpel02b55e52018-06-04 14:05:54 +0200268 if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200269 return -EINVAL; /* Setup time check page_pool req */
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200270 return 0;
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200271 }
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200272
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200273 /* Delay init of rhashtable to save memory if feature isn't used */
274 if (!mem_id_init) {
275 mutex_lock(&mem_id_lock);
276 ret = __mem_id_init_hash_table();
277 mutex_unlock(&mem_id_lock);
278 if (ret < 0) {
279 WARN_ON(1);
280 return ret;
281 }
282 }
283
284 xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
285 if (!xdp_alloc)
286 return -ENOMEM;
287
288 mutex_lock(&mem_id_lock);
289 id = __mem_id_cyclic_get(gfp);
290 if (id < 0) {
291 errno = id;
292 goto err;
293 }
294 xdp_rxq->mem.id = id;
295 xdp_alloc->mem = xdp_rxq->mem;
296 xdp_alloc->allocator = allocator;
297
298 /* Insert allocator into ID lookup table */
299 ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
300 if (IS_ERR(ptr)) {
301 errno = PTR_ERR(ptr);
302 goto err;
303 }
304
305 mutex_unlock(&mem_id_lock);
306
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200307 return 0;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200308err:
309 mutex_unlock(&mem_id_lock);
310 kfree(xdp_alloc);
311 return errno;
Jesper Dangaard Brouer5ab073f2018-04-17 16:45:26 +0200312}
313EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200314
Jesper Dangaard Brouer389ab7f2018-05-24 16:46:07 +0200315/* XDP RX runs under NAPI protection, and in different delivery error
316 * scenarios (e.g. queue full), it is possible to return the xdp_frame
317 * while still leveraging this protection. The @napi_direct boolian
318 * is used for those calls sites. Thus, allowing for faster recycling
319 * of xdp_frames/pages in those cases.
320 */
Björn Töpel02b55e52018-06-04 14:05:54 +0200321static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
322 unsigned long handle)
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200323{
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200324 struct xdp_mem_allocator *xa;
325 struct page *page;
326
327 switch (mem->type) {
328 case MEM_TYPE_PAGE_POOL:
329 rcu_read_lock();
330 /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
331 xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
332 page = virt_to_head_page(data);
Toshiaki Makita25396502018-08-03 16:58:16 +0900333 if (xa) {
334 napi_direct &= !xdp_return_frame_no_direct();
Jesper Dangaard Brouer389ab7f2018-05-24 16:46:07 +0200335 page_pool_put_page(xa->page_pool, page, napi_direct);
Toshiaki Makita25396502018-08-03 16:58:16 +0900336 } else {
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200337 put_page(page);
Toshiaki Makita25396502018-08-03 16:58:16 +0900338 }
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200339 rcu_read_unlock();
340 break;
341 case MEM_TYPE_PAGE_SHARED:
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200342 page_frag_free(data);
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200343 break;
344 case MEM_TYPE_PAGE_ORDER0:
345 page = virt_to_page(data); /* Assumes order0 page*/
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200346 put_page(page);
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200347 break;
Björn Töpel02b55e52018-06-04 14:05:54 +0200348 case MEM_TYPE_ZERO_COPY:
349 /* NB! Only valid from an xdp_buff! */
350 rcu_read_lock();
351 /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
352 xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
Björn Töpeleb91e4d2018-08-10 11:28:02 +0200353 xa->zc_alloc->free(xa->zc_alloc, handle);
Björn Töpel02b55e52018-06-04 14:05:54 +0200354 rcu_read_unlock();
Jesper Dangaard Brouer57d0a1c2018-04-17 16:46:22 +0200355 default:
356 /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
357 break;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200358 }
359}
Björn Töpelc4971762018-05-02 13:01:27 +0200360
361void xdp_return_frame(struct xdp_frame *xdpf)
362{
Björn Töpel02b55e52018-06-04 14:05:54 +0200363 __xdp_return(xdpf->data, &xdpf->mem, false, 0);
Björn Töpelc4971762018-05-02 13:01:27 +0200364}
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200365EXPORT_SYMBOL_GPL(xdp_return_frame);
Björn Töpelc4971762018-05-02 13:01:27 +0200366
Jesper Dangaard Brouer389ab7f2018-05-24 16:46:07 +0200367void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
368{
Björn Töpel02b55e52018-06-04 14:05:54 +0200369 __xdp_return(xdpf->data, &xdpf->mem, true, 0);
Jesper Dangaard Brouer389ab7f2018-05-24 16:46:07 +0200370}
371EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
372
Björn Töpelc4971762018-05-02 13:01:27 +0200373void xdp_return_buff(struct xdp_buff *xdp)
374{
Björn Töpel02b55e52018-06-04 14:05:54 +0200375 __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
Björn Töpelc4971762018-05-02 13:01:27 +0200376}
377EXPORT_SYMBOL_GPL(xdp_return_buff);
Jakub Kicinski05296622018-07-11 20:36:40 -0700378
379int xdp_attachment_query(struct xdp_attachment_info *info,
380 struct netdev_bpf *bpf)
381{
382 bpf->prog_id = info->prog ? info->prog->aux->id : 0;
383 bpf->prog_flags = info->prog ? info->flags : 0;
384 return 0;
385}
386EXPORT_SYMBOL_GPL(xdp_attachment_query);
387
388bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
389 struct netdev_bpf *bpf)
390{
391 if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) {
392 NL_SET_ERR_MSG(bpf->extack,
393 "program loaded with different flags");
394 return false;
395 }
396 return true;
397}
398EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok);
399
400void xdp_attachment_setup(struct xdp_attachment_info *info,
401 struct netdev_bpf *bpf)
402{
403 if (info->prog)
404 bpf_prog_put(info->prog);
405 info->prog = bpf->prog;
406 info->flags = bpf->flags;
407}
408EXPORT_SYMBOL_GPL(xdp_attachment_setup);