1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP sockets
3 *
4 * AF_XDP sockets allows a channel between XDP programs and userspace
5 * applications.
6 * Copyright(c) 2018 Intel Corporation.
7 *
8 * Author(s): Björn Töpel <bjorn.topel@intel.com>
9 * Magnus Karlsson <magnus.karlsson@intel.com>
10 */
11
12 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
13
14 #include <linux/if_xdp.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/task.h>
19 #include <linux/socket.h>
20 #include <linux/file.h>
21 #include <linux/uaccess.h>
22 #include <linux/net.h>
23 #include <linux/netdevice.h>
24 #include <linux/rculist.h>
25 #include <linux/vmalloc.h>
26 #include <net/xdp_sock_drv.h>
27 #include <net/busy_poll.h>
28 #include <net/netdev_rx_queue.h>
29 #include <net/xdp.h>
30
31 #include "xsk_queue.h"
32 #include "xdp_umem.h"
33 #include "xsk.h"
34
35 #define TX_BATCH_SIZE 32
36 #define MAX_PER_SOCKET_BUDGET (TX_BATCH_SIZE)
37
xsk_set_rx_need_wakeup(struct xsk_buff_pool * pool)38 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
39 {
40 if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
41 return;
42
43 pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
44 pool->cached_need_wakeup |= XDP_WAKEUP_RX;
45 }
46 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
47
xsk_set_tx_need_wakeup(struct xsk_buff_pool * pool)48 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
49 {
50 struct xdp_sock *xs;
51
52 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
53 return;
54
55 rcu_read_lock();
56 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
57 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
58 }
59 rcu_read_unlock();
60
61 pool->cached_need_wakeup |= XDP_WAKEUP_TX;
62 }
63 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
64
xsk_clear_rx_need_wakeup(struct xsk_buff_pool * pool)65 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
66 {
67 if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
68 return;
69
70 pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
71 pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
72 }
73 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
74
xsk_clear_tx_need_wakeup(struct xsk_buff_pool * pool)75 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
76 {
77 struct xdp_sock *xs;
78
79 if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
80 return;
81
82 rcu_read_lock();
83 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
84 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
85 }
86 rcu_read_unlock();
87
88 pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
89 }
90 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
91
xsk_uses_need_wakeup(struct xsk_buff_pool * pool)92 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
93 {
94 return pool->uses_need_wakeup;
95 }
96 EXPORT_SYMBOL(xsk_uses_need_wakeup);
97
xsk_get_pool_from_qid(struct net_device * dev,u16 queue_id)98 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
99 u16 queue_id)
100 {
101 if (queue_id < dev->real_num_rx_queues)
102 return dev->_rx[queue_id].pool;
103 if (queue_id < dev->real_num_tx_queues)
104 return dev->_tx[queue_id].pool;
105
106 return NULL;
107 }
108 EXPORT_SYMBOL(xsk_get_pool_from_qid);
109
xsk_clear_pool_at_qid(struct net_device * dev,u16 queue_id)110 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
111 {
112 if (queue_id < dev->num_rx_queues)
113 dev->_rx[queue_id].pool = NULL;
114 if (queue_id < dev->num_tx_queues)
115 dev->_tx[queue_id].pool = NULL;
116 }
117
118 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
119 * not know if the device has more tx queues than rx, or the opposite.
120 * This might also change during run time.
121 */
xsk_reg_pool_at_qid(struct net_device * dev,struct xsk_buff_pool * pool,u16 queue_id)122 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
123 u16 queue_id)
124 {
125 if (queue_id >= max_t(unsigned int,
126 dev->real_num_rx_queues,
127 dev->real_num_tx_queues))
128 return -EINVAL;
129
130 if (queue_id < dev->real_num_rx_queues)
131 dev->_rx[queue_id].pool = pool;
132 if (queue_id < dev->real_num_tx_queues)
133 dev->_tx[queue_id].pool = pool;
134
135 return 0;
136 }
137
__xsk_rcv_zc(struct xdp_sock * xs,struct xdp_buff_xsk * xskb,u32 len,u32 flags)138 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff_xsk *xskb, u32 len,
139 u32 flags)
140 {
141 u64 addr;
142 int err;
143
144 addr = xp_get_handle(xskb, xskb->pool);
145 err = xskq_prod_reserve_desc(xs->rx, addr, len, flags);
146 if (err) {
147 xs->rx_queue_full++;
148 return err;
149 }
150
151 xp_release(xskb);
152 return 0;
153 }
154
xsk_rcv_zc(struct xdp_sock * xs,struct xdp_buff * xdp,u32 len)155 static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
156 {
157 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
158 u32 frags = xdp_buff_has_frags(xdp);
159 struct xdp_buff_xsk *pos, *tmp;
160 struct list_head *xskb_list;
161 u32 contd = 0;
162 int err;
163
164 if (frags)
165 contd = XDP_PKT_CONTD;
166
167 err = __xsk_rcv_zc(xs, xskb, len, contd);
168 if (err)
169 goto err;
170 if (likely(!frags))
171 return 0;
172
173 xskb_list = &xskb->pool->xskb_list;
174 list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
175 if (list_is_singular(xskb_list))
176 contd = 0;
177 len = pos->xdp.data_end - pos->xdp.data;
178 err = __xsk_rcv_zc(xs, pos, len, contd);
179 if (err)
180 goto err;
181 list_del(&pos->list_node);
182 }
183
184 return 0;
185 err:
186 xsk_buff_free(xdp);
187 return err;
188 }
189
xsk_copy_xdp_start(struct xdp_buff * from)190 static void *xsk_copy_xdp_start(struct xdp_buff *from)
191 {
192 if (unlikely(xdp_data_meta_unsupported(from)))
193 return from->data;
194 else
195 return from->data_meta;
196 }
197
xsk_copy_xdp(void * to,void ** from,u32 to_len,u32 * from_len,skb_frag_t ** frag,u32 rem)198 static u32 xsk_copy_xdp(void *to, void **from, u32 to_len,
199 u32 *from_len, skb_frag_t **frag, u32 rem)
200 {
201 u32 copied = 0;
202
203 while (1) {
204 u32 copy_len = min_t(u32, *from_len, to_len);
205
206 memcpy(to, *from, copy_len);
207 copied += copy_len;
208 if (rem == copied)
209 return copied;
210
211 if (*from_len == copy_len) {
212 *from = skb_frag_address(*frag);
213 *from_len = skb_frag_size((*frag)++);
214 } else {
215 *from += copy_len;
216 *from_len -= copy_len;
217 }
218 if (to_len == copy_len)
219 return copied;
220
221 to_len -= copy_len;
222 to += copy_len;
223 }
224 }
225
__xsk_rcv(struct xdp_sock * xs,struct xdp_buff * xdp,u32 len)226 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
227 {
228 u32 frame_size = xsk_pool_get_rx_frame_size(xs->pool);
229 void *copy_from = xsk_copy_xdp_start(xdp), *copy_to;
230 u32 from_len, meta_len, rem, num_desc;
231 struct xdp_buff_xsk *xskb;
232 struct xdp_buff *xsk_xdp;
233 skb_frag_t *frag;
234
235 from_len = xdp->data_end - copy_from;
236 meta_len = xdp->data - copy_from;
237 rem = len + meta_len;
238
239 if (len <= frame_size && !xdp_buff_has_frags(xdp)) {
240 int err;
241
242 xsk_xdp = xsk_buff_alloc(xs->pool);
243 if (!xsk_xdp) {
244 xs->rx_dropped++;
245 return -ENOMEM;
246 }
247 memcpy(xsk_xdp->data - meta_len, copy_from, rem);
248 xskb = container_of(xsk_xdp, struct xdp_buff_xsk, xdp);
249 err = __xsk_rcv_zc(xs, xskb, len, 0);
250 if (err) {
251 xsk_buff_free(xsk_xdp);
252 return err;
253 }
254
255 return 0;
256 }
257
258 num_desc = (len - 1) / frame_size + 1;
259
260 if (!xsk_buff_can_alloc(xs->pool, num_desc)) {
261 xs->rx_dropped++;
262 return -ENOMEM;
263 }
264 if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
265 xs->rx_queue_full++;
266 return -ENOBUFS;
267 }
268
269 if (xdp_buff_has_frags(xdp)) {
270 struct skb_shared_info *sinfo;
271
272 sinfo = xdp_get_shared_info_from_buff(xdp);
273 frag = &sinfo->frags[0];
274 }
275
276 do {
277 u32 to_len = frame_size + meta_len;
278 u32 copied;
279
280 xsk_xdp = xsk_buff_alloc(xs->pool);
281 copy_to = xsk_xdp->data - meta_len;
282
283 copied = xsk_copy_xdp(copy_to, ©_from, to_len, &from_len, &frag, rem);
284 rem -= copied;
285
286 xskb = container_of(xsk_xdp, struct xdp_buff_xsk, xdp);
287 __xsk_rcv_zc(xs, xskb, copied - meta_len, rem ? XDP_PKT_CONTD : 0);
288 meta_len = 0;
289 } while (rem);
290
291 return 0;
292 }
293
xsk_tx_writeable(struct xdp_sock * xs)294 static bool xsk_tx_writeable(struct xdp_sock *xs)
295 {
296 if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
297 return false;
298
299 return true;
300 }
301
xsk_is_bound(struct xdp_sock * xs)302 static bool xsk_is_bound(struct xdp_sock *xs)
303 {
304 if (READ_ONCE(xs->state) == XSK_BOUND) {
305 /* Matches smp_wmb() in bind(). */
306 smp_rmb();
307 return true;
308 }
309 return false;
310 }
311
xsk_rcv_check(struct xdp_sock * xs,struct xdp_buff * xdp,u32 len)312 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
313 {
314 if (!xsk_is_bound(xs))
315 return -ENXIO;
316
317 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
318 return -EINVAL;
319
320 if (len > xsk_pool_get_rx_frame_size(xs->pool) && !xs->sg) {
321 xs->rx_dropped++;
322 return -ENOSPC;
323 }
324
325 sk_mark_napi_id_once_xdp(&xs->sk, xdp);
326 return 0;
327 }
328
xsk_flush(struct xdp_sock * xs)329 static void xsk_flush(struct xdp_sock *xs)
330 {
331 xskq_prod_submit(xs->rx);
332 __xskq_cons_release(xs->pool->fq);
333 sock_def_readable(&xs->sk);
334 }
335
xsk_generic_rcv(struct xdp_sock * xs,struct xdp_buff * xdp)336 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
337 {
338 u32 len = xdp_get_buff_len(xdp);
339 int err;
340
341 spin_lock_bh(&xs->rx_lock);
342 err = xsk_rcv_check(xs, xdp, len);
343 if (!err) {
344 err = __xsk_rcv(xs, xdp, len);
345 xsk_flush(xs);
346 }
347 spin_unlock_bh(&xs->rx_lock);
348 return err;
349 }
350
xsk_rcv(struct xdp_sock * xs,struct xdp_buff * xdp)351 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
352 {
353 u32 len = xdp_get_buff_len(xdp);
354 int err;
355
356 err = xsk_rcv_check(xs, xdp, len);
357 if (err)
358 return err;
359
360 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
361 len = xdp->data_end - xdp->data;
362 return xsk_rcv_zc(xs, xdp, len);
363 }
364
365 err = __xsk_rcv(xs, xdp, len);
366 if (!err)
367 xdp_return_buff(xdp);
368 return err;
369 }
370
__xsk_map_redirect(struct xdp_sock * xs,struct xdp_buff * xdp)371 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
372 {
373 int err;
374
375 err = xsk_rcv(xs, xdp);
376 if (err)
377 return err;
378
379 if (!xs->flush_node.prev) {
380 struct list_head *flush_list = bpf_net_ctx_get_xskmap_flush_list();
381
382 list_add(&xs->flush_node, flush_list);
383 }
384
385 return 0;
386 }
387
__xsk_map_flush(struct list_head * flush_list)388 void __xsk_map_flush(struct list_head *flush_list)
389 {
390 struct xdp_sock *xs, *tmp;
391
392 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
393 xsk_flush(xs);
394 __list_del_clearprev(&xs->flush_node);
395 }
396 }
397
xsk_tx_completed(struct xsk_buff_pool * pool,u32 nb_entries)398 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
399 {
400 xskq_prod_submit_n(pool->cq, nb_entries);
401 }
402 EXPORT_SYMBOL(xsk_tx_completed);
403
xsk_tx_release(struct xsk_buff_pool * pool)404 void xsk_tx_release(struct xsk_buff_pool *pool)
405 {
406 struct xdp_sock *xs;
407
408 rcu_read_lock();
409 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
410 __xskq_cons_release(xs->tx);
411 if (xsk_tx_writeable(xs))
412 xs->sk.sk_write_space(&xs->sk);
413 }
414 rcu_read_unlock();
415 }
416 EXPORT_SYMBOL(xsk_tx_release);
417
xsk_tx_peek_desc(struct xsk_buff_pool * pool,struct xdp_desc * desc)418 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
419 {
420 bool budget_exhausted = false;
421 struct xdp_sock *xs;
422
423 rcu_read_lock();
424 again:
425 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
426 if (xs->tx_budget_spent >= MAX_PER_SOCKET_BUDGET) {
427 budget_exhausted = true;
428 continue;
429 }
430
431 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
432 if (xskq_has_descs(xs->tx))
433 xskq_cons_release(xs->tx);
434 continue;
435 }
436
437 xs->tx_budget_spent++;
438
439 /* This is the backpressure mechanism for the Tx path.
440 * Reserve space in the completion queue and only proceed
441 * if there is space in it. This avoids having to implement
442 * any buffering in the Tx path.
443 */
444 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
445 goto out;
446
447 xskq_cons_release(xs->tx);
448 rcu_read_unlock();
449 return true;
450 }
451
452 if (budget_exhausted) {
453 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list)
454 xs->tx_budget_spent = 0;
455
456 budget_exhausted = false;
457 goto again;
458 }
459
460 out:
461 rcu_read_unlock();
462 return false;
463 }
464 EXPORT_SYMBOL(xsk_tx_peek_desc);
465
xsk_tx_peek_release_fallback(struct xsk_buff_pool * pool,u32 max_entries)466 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
467 {
468 struct xdp_desc *descs = pool->tx_descs;
469 u32 nb_pkts = 0;
470
471 while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
472 nb_pkts++;
473
474 xsk_tx_release(pool);
475 return nb_pkts;
476 }
477
xsk_tx_peek_release_desc_batch(struct xsk_buff_pool * pool,u32 nb_pkts)478 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts)
479 {
480 struct xdp_sock *xs;
481
482 rcu_read_lock();
483 if (!list_is_singular(&pool->xsk_tx_list)) {
484 /* Fallback to the non-batched version */
485 rcu_read_unlock();
486 return xsk_tx_peek_release_fallback(pool, nb_pkts);
487 }
488
489 xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
490 if (!xs) {
491 nb_pkts = 0;
492 goto out;
493 }
494
495 nb_pkts = xskq_cons_nb_entries(xs->tx, nb_pkts);
496
497 /* This is the backpressure mechanism for the Tx path. Try to
498 * reserve space in the completion queue for all packets, but
499 * if there are fewer slots available, just process that many
500 * packets. This avoids having to implement any buffering in
501 * the Tx path.
502 */
503 nb_pkts = xskq_prod_nb_free(pool->cq, nb_pkts);
504 if (!nb_pkts)
505 goto out;
506
507 nb_pkts = xskq_cons_read_desc_batch(xs->tx, pool, nb_pkts);
508 if (!nb_pkts) {
509 xs->tx->queue_empty_descs++;
510 goto out;
511 }
512
513 __xskq_cons_release(xs->tx);
514 xskq_prod_write_addr_batch(pool->cq, pool->tx_descs, nb_pkts);
515 xs->sk.sk_write_space(&xs->sk);
516
517 out:
518 rcu_read_unlock();
519 return nb_pkts;
520 }
521 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
522
xsk_wakeup(struct xdp_sock * xs,u8 flags)523 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
524 {
525 struct net_device *dev = xs->dev;
526
527 return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
528 }
529
xsk_cq_reserve_addr_locked(struct xsk_buff_pool * pool,u64 addr)530 static int xsk_cq_reserve_addr_locked(struct xsk_buff_pool *pool, u64 addr)
531 {
532 unsigned long flags;
533 int ret;
534
535 spin_lock_irqsave(&pool->cq_lock, flags);
536 ret = xskq_prod_reserve_addr(pool->cq, addr);
537 spin_unlock_irqrestore(&pool->cq_lock, flags);
538
539 return ret;
540 }
541
xsk_cq_submit_locked(struct xsk_buff_pool * pool,u32 n)542 static void xsk_cq_submit_locked(struct xsk_buff_pool *pool, u32 n)
543 {
544 unsigned long flags;
545
546 spin_lock_irqsave(&pool->cq_lock, flags);
547 xskq_prod_submit_n(pool->cq, n);
548 spin_unlock_irqrestore(&pool->cq_lock, flags);
549 }
550
xsk_cq_cancel_locked(struct xsk_buff_pool * pool,u32 n)551 static void xsk_cq_cancel_locked(struct xsk_buff_pool *pool, u32 n)
552 {
553 unsigned long flags;
554
555 spin_lock_irqsave(&pool->cq_lock, flags);
556 xskq_prod_cancel_n(pool->cq, n);
557 spin_unlock_irqrestore(&pool->cq_lock, flags);
558 }
559
xsk_get_num_desc(struct sk_buff * skb)560 static u32 xsk_get_num_desc(struct sk_buff *skb)
561 {
562 return skb ? (long)skb_shinfo(skb)->destructor_arg : 0;
563 }
564
xsk_destruct_skb(struct sk_buff * skb)565 static void xsk_destruct_skb(struct sk_buff *skb)
566 {
567 struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta;
568
569 if (compl->tx_timestamp) {
570 /* sw completion timestamp, not a real one */
571 *compl->tx_timestamp = ktime_get_tai_fast_ns();
572 }
573
574 xsk_cq_submit_locked(xdp_sk(skb->sk)->pool, xsk_get_num_desc(skb));
575 sock_wfree(skb);
576 }
577
xsk_set_destructor_arg(struct sk_buff * skb)578 static void xsk_set_destructor_arg(struct sk_buff *skb)
579 {
580 long num = xsk_get_num_desc(xdp_sk(skb->sk)->skb) + 1;
581
582 skb_shinfo(skb)->destructor_arg = (void *)num;
583 }
584
xsk_consume_skb(struct sk_buff * skb)585 static void xsk_consume_skb(struct sk_buff *skb)
586 {
587 struct xdp_sock *xs = xdp_sk(skb->sk);
588
589 skb->destructor = sock_wfree;
590 xsk_cq_cancel_locked(xs->pool, xsk_get_num_desc(skb));
591 /* Free skb without triggering the perf drop trace */
592 consume_skb(skb);
593 xs->skb = NULL;
594 }
595
xsk_drop_skb(struct sk_buff * skb)596 static void xsk_drop_skb(struct sk_buff *skb)
597 {
598 xdp_sk(skb->sk)->tx->invalid_descs += xsk_get_num_desc(skb);
599 xsk_consume_skb(skb);
600 }
601
xsk_build_skb_zerocopy(struct xdp_sock * xs,struct xdp_desc * desc)602 static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
603 struct xdp_desc *desc)
604 {
605 struct xsk_buff_pool *pool = xs->pool;
606 u32 hr, len, ts, offset, copy, copied;
607 struct sk_buff *skb = xs->skb;
608 struct page *page;
609 void *buffer;
610 int err, i;
611 u64 addr;
612
613 if (!skb) {
614 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
615
616 skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
617 if (unlikely(!skb))
618 return ERR_PTR(err);
619
620 skb_reserve(skb, hr);
621 }
622
623 addr = desc->addr;
624 len = desc->len;
625 ts = pool->unaligned ? len : pool->chunk_size;
626
627 buffer = xsk_buff_raw_get_data(pool, addr);
628 offset = offset_in_page(buffer);
629 addr = buffer - pool->addrs;
630
631 for (copied = 0, i = skb_shinfo(skb)->nr_frags; copied < len; i++) {
632 if (unlikely(i >= MAX_SKB_FRAGS))
633 return ERR_PTR(-EOVERFLOW);
634
635 page = pool->umem->pgs[addr >> PAGE_SHIFT];
636 get_page(page);
637
638 copy = min_t(u32, PAGE_SIZE - offset, len - copied);
639 skb_fill_page_desc(skb, i, page, offset, copy);
640
641 copied += copy;
642 addr += copy;
643 offset = 0;
644 }
645
646 skb->len += len;
647 skb->data_len += len;
648 skb->truesize += ts;
649
650 refcount_add(ts, &xs->sk.sk_wmem_alloc);
651
652 return skb;
653 }
654
xsk_build_skb(struct xdp_sock * xs,struct xdp_desc * desc)655 static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
656 struct xdp_desc *desc)
657 {
658 struct xsk_tx_metadata *meta = NULL;
659 struct net_device *dev = xs->dev;
660 struct sk_buff *skb = xs->skb;
661 bool first_frag = false;
662 int err;
663
664 if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
665 skb = xsk_build_skb_zerocopy(xs, desc);
666 if (IS_ERR(skb)) {
667 err = PTR_ERR(skb);
668 goto free_err;
669 }
670 } else {
671 u32 hr, tr, len;
672 void *buffer;
673
674 buffer = xsk_buff_raw_get_data(xs->pool, desc->addr);
675 len = desc->len;
676
677 if (!skb) {
678 first_frag = true;
679
680 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
681 tr = dev->needed_tailroom;
682 skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
683 if (unlikely(!skb))
684 goto free_err;
685
686 skb_reserve(skb, hr);
687 skb_put(skb, len);
688
689 err = skb_store_bits(skb, 0, buffer, len);
690 if (unlikely(err))
691 goto free_err;
692 } else {
693 int nr_frags = skb_shinfo(skb)->nr_frags;
694 struct page *page;
695 u8 *vaddr;
696
697 if (unlikely(nr_frags == (MAX_SKB_FRAGS - 1) && xp_mb_desc(desc))) {
698 err = -EOVERFLOW;
699 goto free_err;
700 }
701
702 page = alloc_page(xs->sk.sk_allocation);
703 if (unlikely(!page)) {
704 err = -EAGAIN;
705 goto free_err;
706 }
707
708 vaddr = kmap_local_page(page);
709 memcpy(vaddr, buffer, len);
710 kunmap_local(vaddr);
711
712 skb_add_rx_frag(skb, nr_frags, page, 0, len, PAGE_SIZE);
713 refcount_add(PAGE_SIZE, &xs->sk.sk_wmem_alloc);
714 }
715
716 if (first_frag && desc->options & XDP_TX_METADATA) {
717 if (unlikely(xs->pool->tx_metadata_len == 0)) {
718 err = -EINVAL;
719 goto free_err;
720 }
721
722 meta = buffer - xs->pool->tx_metadata_len;
723 if (unlikely(!xsk_buff_valid_tx_metadata(meta))) {
724 err = -EINVAL;
725 goto free_err;
726 }
727
728 if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
729 if (unlikely(meta->request.csum_start +
730 meta->request.csum_offset +
731 sizeof(__sum16) > len)) {
732 err = -EINVAL;
733 goto free_err;
734 }
735
736 skb->csum_start = hr + meta->request.csum_start;
737 skb->csum_offset = meta->request.csum_offset;
738 skb->ip_summed = CHECKSUM_PARTIAL;
739
740 if (unlikely(xs->pool->tx_sw_csum)) {
741 err = skb_checksum_help(skb);
742 if (err)
743 goto free_err;
744 }
745 }
746 }
747 }
748
749 skb->dev = dev;
750 skb->priority = READ_ONCE(xs->sk.sk_priority);
751 skb->mark = READ_ONCE(xs->sk.sk_mark);
752 skb->destructor = xsk_destruct_skb;
753 xsk_tx_metadata_to_compl(meta, &skb_shinfo(skb)->xsk_meta);
754 xsk_set_destructor_arg(skb);
755
756 return skb;
757
758 free_err:
759 if (first_frag && skb)
760 kfree_skb(skb);
761
762 if (err == -EOVERFLOW) {
763 /* Drop the packet */
764 xsk_set_destructor_arg(xs->skb);
765 xsk_drop_skb(xs->skb);
766 xskq_cons_release(xs->tx);
767 } else {
768 /* Let application retry */
769 xsk_cq_cancel_locked(xs->pool, 1);
770 }
771
772 return ERR_PTR(err);
773 }
774
__xsk_generic_xmit(struct sock * sk)775 static int __xsk_generic_xmit(struct sock *sk)
776 {
777 struct xdp_sock *xs = xdp_sk(sk);
778 u32 max_batch = TX_BATCH_SIZE;
779 bool sent_frame = false;
780 struct xdp_desc desc;
781 struct sk_buff *skb;
782 int err = 0;
783
784 mutex_lock(&xs->mutex);
785
786 /* Since we dropped the RCU read lock, the socket state might have changed. */
787 if (unlikely(!xsk_is_bound(xs))) {
788 err = -ENXIO;
789 goto out;
790 }
791
792 if (xs->queue_id >= xs->dev->real_num_tx_queues)
793 goto out;
794
795 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
796 if (max_batch-- == 0) {
797 err = -EAGAIN;
798 goto out;
799 }
800
801 /* This is the backpressure mechanism for the Tx path.
802 * Reserve space in the completion queue and only proceed
803 * if there is space in it. This avoids having to implement
804 * any buffering in the Tx path.
805 */
806 if (xsk_cq_reserve_addr_locked(xs->pool, desc.addr))
807 goto out;
808
809 skb = xsk_build_skb(xs, &desc);
810 if (IS_ERR(skb)) {
811 err = PTR_ERR(skb);
812 if (err != -EOVERFLOW)
813 goto out;
814 err = 0;
815 continue;
816 }
817
818 xskq_cons_release(xs->tx);
819
820 if (xp_mb_desc(&desc)) {
821 xs->skb = skb;
822 continue;
823 }
824
825 err = __dev_direct_xmit(skb, xs->queue_id);
826 if (err == NETDEV_TX_BUSY) {
827 /* Tell user-space to retry the send */
828 xskq_cons_cancel_n(xs->tx, xsk_get_num_desc(skb));
829 xsk_consume_skb(skb);
830 err = -EAGAIN;
831 goto out;
832 }
833
834 /* Ignore NET_XMIT_CN as packet might have been sent */
835 if (err == NET_XMIT_DROP) {
836 /* SKB completed but not sent */
837 err = -EBUSY;
838 xs->skb = NULL;
839 goto out;
840 }
841
842 sent_frame = true;
843 xs->skb = NULL;
844 }
845
846 if (xskq_has_descs(xs->tx)) {
847 if (xs->skb)
848 xsk_drop_skb(xs->skb);
849 xskq_cons_release(xs->tx);
850 }
851
852 out:
853 if (sent_frame)
854 if (xsk_tx_writeable(xs))
855 sk->sk_write_space(sk);
856
857 mutex_unlock(&xs->mutex);
858 return err;
859 }
860
xsk_generic_xmit(struct sock * sk)861 static int xsk_generic_xmit(struct sock *sk)
862 {
863 int ret;
864
865 /* Drop the RCU lock since the SKB path might sleep. */
866 rcu_read_unlock();
867 ret = __xsk_generic_xmit(sk);
868 /* Reaquire RCU lock before going into common code. */
869 rcu_read_lock();
870
871 return ret;
872 }
873
xsk_no_wakeup(struct sock * sk)874 static bool xsk_no_wakeup(struct sock *sk)
875 {
876 #ifdef CONFIG_NET_RX_BUSY_POLL
877 /* Prefer busy-polling, skip the wakeup. */
878 return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
879 READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
880 #else
881 return false;
882 #endif
883 }
884
xsk_check_common(struct xdp_sock * xs)885 static int xsk_check_common(struct xdp_sock *xs)
886 {
887 if (unlikely(!xsk_is_bound(xs)))
888 return -ENXIO;
889 if (unlikely(!(xs->dev->flags & IFF_UP)))
890 return -ENETDOWN;
891
892 return 0;
893 }
894
__xsk_sendmsg(struct socket * sock,struct msghdr * m,size_t total_len)895 static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
896 {
897 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
898 struct sock *sk = sock->sk;
899 struct xdp_sock *xs = xdp_sk(sk);
900 struct xsk_buff_pool *pool;
901 int err;
902
903 err = xsk_check_common(xs);
904 if (err)
905 return err;
906 if (unlikely(need_wait))
907 return -EOPNOTSUPP;
908 if (unlikely(!xs->tx))
909 return -ENOBUFS;
910
911 if (sk_can_busy_loop(sk)) {
912 if (xs->zc)
913 __sk_mark_napi_id_once(sk, xsk_pool_get_napi_id(xs->pool));
914 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
915 }
916
917 if (xs->zc && xsk_no_wakeup(sk))
918 return 0;
919
920 pool = xs->pool;
921 if (pool->cached_need_wakeup & XDP_WAKEUP_TX) {
922 if (xs->zc)
923 return xsk_wakeup(xs, XDP_WAKEUP_TX);
924 return xsk_generic_xmit(sk);
925 }
926 return 0;
927 }
928
xsk_sendmsg(struct socket * sock,struct msghdr * m,size_t total_len)929 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
930 {
931 int ret;
932
933 rcu_read_lock();
934 ret = __xsk_sendmsg(sock, m, total_len);
935 rcu_read_unlock();
936
937 return ret;
938 }
939
__xsk_recvmsg(struct socket * sock,struct msghdr * m,size_t len,int flags)940 static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
941 {
942 bool need_wait = !(flags & MSG_DONTWAIT);
943 struct sock *sk = sock->sk;
944 struct xdp_sock *xs = xdp_sk(sk);
945 int err;
946
947 err = xsk_check_common(xs);
948 if (err)
949 return err;
950 if (unlikely(!xs->rx))
951 return -ENOBUFS;
952 if (unlikely(need_wait))
953 return -EOPNOTSUPP;
954
955 if (sk_can_busy_loop(sk))
956 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
957
958 if (xsk_no_wakeup(sk))
959 return 0;
960
961 if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
962 return xsk_wakeup(xs, XDP_WAKEUP_RX);
963 return 0;
964 }
965
xsk_recvmsg(struct socket * sock,struct msghdr * m,size_t len,int flags)966 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
967 {
968 int ret;
969
970 rcu_read_lock();
971 ret = __xsk_recvmsg(sock, m, len, flags);
972 rcu_read_unlock();
973
974 return ret;
975 }
976
xsk_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)977 static __poll_t xsk_poll(struct file *file, struct socket *sock,
978 struct poll_table_struct *wait)
979 {
980 __poll_t mask = 0;
981 struct sock *sk = sock->sk;
982 struct xdp_sock *xs = xdp_sk(sk);
983 struct xsk_buff_pool *pool;
984
985 sock_poll_wait(file, sock, wait);
986
987 rcu_read_lock();
988 if (xsk_check_common(xs))
989 goto out;
990
991 pool = xs->pool;
992
993 if (pool->cached_need_wakeup) {
994 if (xs->zc)
995 xsk_wakeup(xs, pool->cached_need_wakeup);
996 else if (xs->tx)
997 /* Poll needs to drive Tx also in copy mode */
998 xsk_generic_xmit(sk);
999 }
1000
1001 if (xs->rx && !xskq_prod_is_empty(xs->rx))
1002 mask |= EPOLLIN | EPOLLRDNORM;
1003 if (xs->tx && xsk_tx_writeable(xs))
1004 mask |= EPOLLOUT | EPOLLWRNORM;
1005 out:
1006 rcu_read_unlock();
1007 return mask;
1008 }
1009
xsk_init_queue(u32 entries,struct xsk_queue ** queue,bool umem_queue)1010 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
1011 bool umem_queue)
1012 {
1013 struct xsk_queue *q;
1014
1015 if (entries == 0 || *queue || !is_power_of_2(entries))
1016 return -EINVAL;
1017
1018 q = xskq_create(entries, umem_queue);
1019 if (!q)
1020 return -ENOMEM;
1021
1022 /* Make sure queue is ready before it can be seen by others */
1023 smp_wmb();
1024 WRITE_ONCE(*queue, q);
1025 return 0;
1026 }
1027
xsk_unbind_dev(struct xdp_sock * xs)1028 static void xsk_unbind_dev(struct xdp_sock *xs)
1029 {
1030 struct net_device *dev = xs->dev;
1031
1032 if (xs->state != XSK_BOUND)
1033 return;
1034 WRITE_ONCE(xs->state, XSK_UNBOUND);
1035
1036 /* Wait for driver to stop using the xdp socket. */
1037 xp_del_xsk(xs->pool, xs);
1038 synchronize_net();
1039 dev_put(dev);
1040 }
1041
xsk_get_map_list_entry(struct xdp_sock * xs,struct xdp_sock __rcu *** map_entry)1042 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
1043 struct xdp_sock __rcu ***map_entry)
1044 {
1045 struct xsk_map *map = NULL;
1046 struct xsk_map_node *node;
1047
1048 *map_entry = NULL;
1049
1050 spin_lock_bh(&xs->map_list_lock);
1051 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
1052 node);
1053 if (node) {
1054 bpf_map_inc(&node->map->map);
1055 map = node->map;
1056 *map_entry = node->map_entry;
1057 }
1058 spin_unlock_bh(&xs->map_list_lock);
1059 return map;
1060 }
1061
xsk_delete_from_maps(struct xdp_sock * xs)1062 static void xsk_delete_from_maps(struct xdp_sock *xs)
1063 {
1064 /* This function removes the current XDP socket from all the
1065 * maps it resides in. We need to take extra care here, due to
1066 * the two locks involved. Each map has a lock synchronizing
1067 * updates to the entries, and each socket has a lock that
1068 * synchronizes access to the list of maps (map_list). For
1069 * deadlock avoidance the locks need to be taken in the order
1070 * "map lock"->"socket map list lock". We start off by
1071 * accessing the socket map list, and take a reference to the
1072 * map to guarantee existence between the
1073 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
1074 * calls. Then we ask the map to remove the socket, which
1075 * tries to remove the socket from the map. Note that there
1076 * might be updates to the map between
1077 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
1078 */
1079 struct xdp_sock __rcu **map_entry = NULL;
1080 struct xsk_map *map;
1081
1082 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
1083 xsk_map_try_sock_delete(map, xs, map_entry);
1084 bpf_map_put(&map->map);
1085 }
1086 }
1087
xsk_release(struct socket * sock)1088 static int xsk_release(struct socket *sock)
1089 {
1090 struct sock *sk = sock->sk;
1091 struct xdp_sock *xs = xdp_sk(sk);
1092 struct net *net;
1093
1094 if (!sk)
1095 return 0;
1096
1097 net = sock_net(sk);
1098
1099 if (xs->skb)
1100 xsk_drop_skb(xs->skb);
1101
1102 mutex_lock(&net->xdp.lock);
1103 sk_del_node_init_rcu(sk);
1104 mutex_unlock(&net->xdp.lock);
1105
1106 sock_prot_inuse_add(net, sk->sk_prot, -1);
1107
1108 xsk_delete_from_maps(xs);
1109 mutex_lock(&xs->mutex);
1110 xsk_unbind_dev(xs);
1111 mutex_unlock(&xs->mutex);
1112
1113 xskq_destroy(xs->rx);
1114 xskq_destroy(xs->tx);
1115 xskq_destroy(xs->fq_tmp);
1116 xskq_destroy(xs->cq_tmp);
1117
1118 sock_orphan(sk);
1119 sock->sk = NULL;
1120
1121 sock_put(sk);
1122
1123 return 0;
1124 }
1125
xsk_lookup_xsk_from_fd(int fd)1126 static struct socket *xsk_lookup_xsk_from_fd(int fd)
1127 {
1128 struct socket *sock;
1129 int err;
1130
1131 sock = sockfd_lookup(fd, &err);
1132 if (!sock)
1133 return ERR_PTR(-ENOTSOCK);
1134
1135 if (sock->sk->sk_family != PF_XDP) {
1136 sockfd_put(sock);
1137 return ERR_PTR(-ENOPROTOOPT);
1138 }
1139
1140 return sock;
1141 }
1142
xsk_validate_queues(struct xdp_sock * xs)1143 static bool xsk_validate_queues(struct xdp_sock *xs)
1144 {
1145 return xs->fq_tmp && xs->cq_tmp;
1146 }
1147
xsk_bind(struct socket * sock,struct sockaddr * addr,int addr_len)1148 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
1149 {
1150 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
1151 struct sock *sk = sock->sk;
1152 struct xdp_sock *xs = xdp_sk(sk);
1153 struct net_device *dev;
1154 int bound_dev_if;
1155 u32 flags, qid;
1156 int err = 0;
1157
1158 if (addr_len < sizeof(struct sockaddr_xdp))
1159 return -EINVAL;
1160 if (sxdp->sxdp_family != AF_XDP)
1161 return -EINVAL;
1162
1163 flags = sxdp->sxdp_flags;
1164 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
1165 XDP_USE_NEED_WAKEUP | XDP_USE_SG))
1166 return -EINVAL;
1167
1168 bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1169 if (bound_dev_if && bound_dev_if != sxdp->sxdp_ifindex)
1170 return -EINVAL;
1171
1172 rtnl_lock();
1173 mutex_lock(&xs->mutex);
1174 if (xs->state != XSK_READY) {
1175 err = -EBUSY;
1176 goto out_release;
1177 }
1178
1179 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
1180 if (!dev) {
1181 err = -ENODEV;
1182 goto out_release;
1183 }
1184
1185 if (!xs->rx && !xs->tx) {
1186 err = -EINVAL;
1187 goto out_unlock;
1188 }
1189
1190 qid = sxdp->sxdp_queue_id;
1191
1192 if (flags & XDP_SHARED_UMEM) {
1193 struct xdp_sock *umem_xs;
1194 struct socket *sock;
1195
1196 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
1197 (flags & XDP_USE_NEED_WAKEUP) || (flags & XDP_USE_SG)) {
1198 /* Cannot specify flags for shared sockets. */
1199 err = -EINVAL;
1200 goto out_unlock;
1201 }
1202
1203 if (xs->umem) {
1204 /* We have already our own. */
1205 err = -EINVAL;
1206 goto out_unlock;
1207 }
1208
1209 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
1210 if (IS_ERR(sock)) {
1211 err = PTR_ERR(sock);
1212 goto out_unlock;
1213 }
1214
1215 umem_xs = xdp_sk(sock->sk);
1216 if (!xsk_is_bound(umem_xs)) {
1217 err = -EBADF;
1218 sockfd_put(sock);
1219 goto out_unlock;
1220 }
1221
1222 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
1223 /* Share the umem with another socket on another qid
1224 * and/or device.
1225 */
1226 xs->pool = xp_create_and_assign_umem(xs,
1227 umem_xs->umem);
1228 if (!xs->pool) {
1229 err = -ENOMEM;
1230 sockfd_put(sock);
1231 goto out_unlock;
1232 }
1233
1234 err = xp_assign_dev_shared(xs->pool, umem_xs, dev,
1235 qid);
1236 if (err) {
1237 xp_destroy(xs->pool);
1238 xs->pool = NULL;
1239 sockfd_put(sock);
1240 goto out_unlock;
1241 }
1242 } else {
1243 /* Share the buffer pool with the other socket. */
1244 if (xs->fq_tmp || xs->cq_tmp) {
1245 /* Do not allow setting your own fq or cq. */
1246 err = -EINVAL;
1247 sockfd_put(sock);
1248 goto out_unlock;
1249 }
1250
1251 xp_get_pool(umem_xs->pool);
1252 xs->pool = umem_xs->pool;
1253
1254 /* If underlying shared umem was created without Tx
1255 * ring, allocate Tx descs array that Tx batching API
1256 * utilizes
1257 */
1258 if (xs->tx && !xs->pool->tx_descs) {
1259 err = xp_alloc_tx_descs(xs->pool, xs);
1260 if (err) {
1261 xp_put_pool(xs->pool);
1262 xs->pool = NULL;
1263 sockfd_put(sock);
1264 goto out_unlock;
1265 }
1266 }
1267 }
1268
1269 xdp_get_umem(umem_xs->umem);
1270 WRITE_ONCE(xs->umem, umem_xs->umem);
1271 sockfd_put(sock);
1272 } else if (!xs->umem || !xsk_validate_queues(xs)) {
1273 err = -EINVAL;
1274 goto out_unlock;
1275 } else {
1276 /* This xsk has its own umem. */
1277 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
1278 if (!xs->pool) {
1279 err = -ENOMEM;
1280 goto out_unlock;
1281 }
1282
1283 err = xp_assign_dev(xs->pool, dev, qid, flags);
1284 if (err) {
1285 xp_destroy(xs->pool);
1286 xs->pool = NULL;
1287 goto out_unlock;
1288 }
1289 }
1290
1291 /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
1292 xs->fq_tmp = NULL;
1293 xs->cq_tmp = NULL;
1294
1295 xs->dev = dev;
1296 xs->zc = xs->umem->zc;
1297 xs->sg = !!(xs->umem->flags & XDP_UMEM_SG_FLAG);
1298 xs->queue_id = qid;
1299 xp_add_xsk(xs->pool, xs);
1300
1301 out_unlock:
1302 if (err) {
1303 dev_put(dev);
1304 } else {
1305 /* Matches smp_rmb() in bind() for shared umem
1306 * sockets, and xsk_is_bound().
1307 */
1308 smp_wmb();
1309 WRITE_ONCE(xs->state, XSK_BOUND);
1310 }
1311 out_release:
1312 mutex_unlock(&xs->mutex);
1313 rtnl_unlock();
1314 return err;
1315 }
1316
1317 struct xdp_umem_reg_v1 {
1318 __u64 addr; /* Start of packet data area */
1319 __u64 len; /* Length of packet data area */
1320 __u32 chunk_size;
1321 __u32 headroom;
1322 };
1323
xsk_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)1324 static int xsk_setsockopt(struct socket *sock, int level, int optname,
1325 sockptr_t optval, unsigned int optlen)
1326 {
1327 struct sock *sk = sock->sk;
1328 struct xdp_sock *xs = xdp_sk(sk);
1329 int err;
1330
1331 if (level != SOL_XDP)
1332 return -ENOPROTOOPT;
1333
1334 switch (optname) {
1335 case XDP_RX_RING:
1336 case XDP_TX_RING:
1337 {
1338 struct xsk_queue **q;
1339 int entries;
1340
1341 if (optlen < sizeof(entries))
1342 return -EINVAL;
1343 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1344 return -EFAULT;
1345
1346 mutex_lock(&xs->mutex);
1347 if (xs->state != XSK_READY) {
1348 mutex_unlock(&xs->mutex);
1349 return -EBUSY;
1350 }
1351 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
1352 err = xsk_init_queue(entries, q, false);
1353 if (!err && optname == XDP_TX_RING)
1354 /* Tx needs to be explicitly woken up the first time */
1355 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
1356 mutex_unlock(&xs->mutex);
1357 return err;
1358 }
1359 case XDP_UMEM_REG:
1360 {
1361 size_t mr_size = sizeof(struct xdp_umem_reg);
1362 struct xdp_umem_reg mr = {};
1363 struct xdp_umem *umem;
1364
1365 if (optlen < sizeof(struct xdp_umem_reg_v1))
1366 return -EINVAL;
1367 else if (optlen < sizeof(mr))
1368 mr_size = sizeof(struct xdp_umem_reg_v1);
1369
1370 BUILD_BUG_ON(sizeof(struct xdp_umem_reg_v1) >= sizeof(struct xdp_umem_reg));
1371
1372 /* Make sure the last field of the struct doesn't have
1373 * uninitialized padding. All padding has to be explicit
1374 * and has to be set to zero by the userspace to make
1375 * struct xdp_umem_reg extensible in the future.
1376 */
1377 BUILD_BUG_ON(offsetof(struct xdp_umem_reg, tx_metadata_len) +
1378 sizeof_field(struct xdp_umem_reg, tx_metadata_len) !=
1379 sizeof(struct xdp_umem_reg));
1380
1381 if (copy_from_sockptr(&mr, optval, mr_size))
1382 return -EFAULT;
1383
1384 mutex_lock(&xs->mutex);
1385 if (xs->state != XSK_READY || xs->umem) {
1386 mutex_unlock(&xs->mutex);
1387 return -EBUSY;
1388 }
1389
1390 umem = xdp_umem_create(&mr);
1391 if (IS_ERR(umem)) {
1392 mutex_unlock(&xs->mutex);
1393 return PTR_ERR(umem);
1394 }
1395
1396 /* Make sure umem is ready before it can be seen by others */
1397 smp_wmb();
1398 WRITE_ONCE(xs->umem, umem);
1399 mutex_unlock(&xs->mutex);
1400 return 0;
1401 }
1402 case XDP_UMEM_FILL_RING:
1403 case XDP_UMEM_COMPLETION_RING:
1404 {
1405 struct xsk_queue **q;
1406 int entries;
1407
1408 if (optlen < sizeof(entries))
1409 return -EINVAL;
1410 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1411 return -EFAULT;
1412
1413 mutex_lock(&xs->mutex);
1414 if (xs->state != XSK_READY) {
1415 mutex_unlock(&xs->mutex);
1416 return -EBUSY;
1417 }
1418
1419 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1420 &xs->cq_tmp;
1421 err = xsk_init_queue(entries, q, true);
1422 mutex_unlock(&xs->mutex);
1423 return err;
1424 }
1425 default:
1426 break;
1427 }
1428
1429 return -ENOPROTOOPT;
1430 }
1431
xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 * ring)1432 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1433 {
1434 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1435 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1436 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1437 }
1438
xsk_enter_umem_offsets(struct xdp_ring_offset_v1 * ring)1439 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1440 {
1441 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1442 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1443 ring->desc = offsetof(struct xdp_umem_ring, desc);
1444 }
1445
1446 struct xdp_statistics_v1 {
1447 __u64 rx_dropped;
1448 __u64 rx_invalid_descs;
1449 __u64 tx_invalid_descs;
1450 };
1451
xsk_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)1452 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1453 char __user *optval, int __user *optlen)
1454 {
1455 struct sock *sk = sock->sk;
1456 struct xdp_sock *xs = xdp_sk(sk);
1457 int len;
1458
1459 if (level != SOL_XDP)
1460 return -ENOPROTOOPT;
1461
1462 if (get_user(len, optlen))
1463 return -EFAULT;
1464 if (len < 0)
1465 return -EINVAL;
1466
1467 switch (optname) {
1468 case XDP_STATISTICS:
1469 {
1470 struct xdp_statistics stats = {};
1471 bool extra_stats = true;
1472 size_t stats_size;
1473
1474 if (len < sizeof(struct xdp_statistics_v1)) {
1475 return -EINVAL;
1476 } else if (len < sizeof(stats)) {
1477 extra_stats = false;
1478 stats_size = sizeof(struct xdp_statistics_v1);
1479 } else {
1480 stats_size = sizeof(stats);
1481 }
1482
1483 mutex_lock(&xs->mutex);
1484 stats.rx_dropped = xs->rx_dropped;
1485 if (extra_stats) {
1486 stats.rx_ring_full = xs->rx_queue_full;
1487 stats.rx_fill_ring_empty_descs =
1488 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1489 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1490 } else {
1491 stats.rx_dropped += xs->rx_queue_full;
1492 }
1493 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1494 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1495 mutex_unlock(&xs->mutex);
1496
1497 if (copy_to_user(optval, &stats, stats_size))
1498 return -EFAULT;
1499 if (put_user(stats_size, optlen))
1500 return -EFAULT;
1501
1502 return 0;
1503 }
1504 case XDP_MMAP_OFFSETS:
1505 {
1506 struct xdp_mmap_offsets off;
1507 struct xdp_mmap_offsets_v1 off_v1;
1508 bool flags_supported = true;
1509 void *to_copy;
1510
1511 if (len < sizeof(off_v1))
1512 return -EINVAL;
1513 else if (len < sizeof(off))
1514 flags_supported = false;
1515
1516 if (flags_supported) {
1517 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1518 * except for the flags field added to the end.
1519 */
1520 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1521 &off.rx);
1522 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1523 &off.tx);
1524 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1525 &off.fr);
1526 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1527 &off.cr);
1528 off.rx.flags = offsetof(struct xdp_rxtx_ring,
1529 ptrs.flags);
1530 off.tx.flags = offsetof(struct xdp_rxtx_ring,
1531 ptrs.flags);
1532 off.fr.flags = offsetof(struct xdp_umem_ring,
1533 ptrs.flags);
1534 off.cr.flags = offsetof(struct xdp_umem_ring,
1535 ptrs.flags);
1536
1537 len = sizeof(off);
1538 to_copy = &off;
1539 } else {
1540 xsk_enter_rxtx_offsets(&off_v1.rx);
1541 xsk_enter_rxtx_offsets(&off_v1.tx);
1542 xsk_enter_umem_offsets(&off_v1.fr);
1543 xsk_enter_umem_offsets(&off_v1.cr);
1544
1545 len = sizeof(off_v1);
1546 to_copy = &off_v1;
1547 }
1548
1549 if (copy_to_user(optval, to_copy, len))
1550 return -EFAULT;
1551 if (put_user(len, optlen))
1552 return -EFAULT;
1553
1554 return 0;
1555 }
1556 case XDP_OPTIONS:
1557 {
1558 struct xdp_options opts = {};
1559
1560 if (len < sizeof(opts))
1561 return -EINVAL;
1562
1563 mutex_lock(&xs->mutex);
1564 if (xs->zc)
1565 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1566 mutex_unlock(&xs->mutex);
1567
1568 len = sizeof(opts);
1569 if (copy_to_user(optval, &opts, len))
1570 return -EFAULT;
1571 if (put_user(len, optlen))
1572 return -EFAULT;
1573
1574 return 0;
1575 }
1576 default:
1577 break;
1578 }
1579
1580 return -EOPNOTSUPP;
1581 }
1582
xsk_mmap(struct file * file,struct socket * sock,struct vm_area_struct * vma)1583 static int xsk_mmap(struct file *file, struct socket *sock,
1584 struct vm_area_struct *vma)
1585 {
1586 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1587 unsigned long size = vma->vm_end - vma->vm_start;
1588 struct xdp_sock *xs = xdp_sk(sock->sk);
1589 int state = READ_ONCE(xs->state);
1590 struct xsk_queue *q = NULL;
1591
1592 if (state != XSK_READY && state != XSK_BOUND)
1593 return -EBUSY;
1594
1595 if (offset == XDP_PGOFF_RX_RING) {
1596 q = READ_ONCE(xs->rx);
1597 } else if (offset == XDP_PGOFF_TX_RING) {
1598 q = READ_ONCE(xs->tx);
1599 } else {
1600 /* Matches the smp_wmb() in XDP_UMEM_REG */
1601 smp_rmb();
1602 if (offset == XDP_UMEM_PGOFF_FILL_RING)
1603 q = state == XSK_READY ? READ_ONCE(xs->fq_tmp) :
1604 READ_ONCE(xs->pool->fq);
1605 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1606 q = state == XSK_READY ? READ_ONCE(xs->cq_tmp) :
1607 READ_ONCE(xs->pool->cq);
1608 }
1609
1610 if (!q)
1611 return -EINVAL;
1612
1613 /* Matches the smp_wmb() in xsk_init_queue */
1614 smp_rmb();
1615 if (size > q->ring_vmalloc_size)
1616 return -EINVAL;
1617
1618 return remap_vmalloc_range(vma, q->ring, 0);
1619 }
1620
xsk_notifier(struct notifier_block * this,unsigned long msg,void * ptr)1621 static int xsk_notifier(struct notifier_block *this,
1622 unsigned long msg, void *ptr)
1623 {
1624 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1625 struct net *net = dev_net(dev);
1626 struct sock *sk;
1627
1628 switch (msg) {
1629 case NETDEV_UNREGISTER:
1630 mutex_lock(&net->xdp.lock);
1631 sk_for_each(sk, &net->xdp.list) {
1632 struct xdp_sock *xs = xdp_sk(sk);
1633
1634 mutex_lock(&xs->mutex);
1635 if (xs->dev == dev) {
1636 sk->sk_err = ENETDOWN;
1637 if (!sock_flag(sk, SOCK_DEAD))
1638 sk_error_report(sk);
1639
1640 xsk_unbind_dev(xs);
1641
1642 /* Clear device references. */
1643 xp_clear_dev(xs->pool);
1644 }
1645 mutex_unlock(&xs->mutex);
1646 }
1647 mutex_unlock(&net->xdp.lock);
1648 break;
1649 }
1650 return NOTIFY_DONE;
1651 }
1652
1653 static struct proto xsk_proto = {
1654 .name = "XDP",
1655 .owner = THIS_MODULE,
1656 .obj_size = sizeof(struct xdp_sock),
1657 };
1658
1659 static const struct proto_ops xsk_proto_ops = {
1660 .family = PF_XDP,
1661 .owner = THIS_MODULE,
1662 .release = xsk_release,
1663 .bind = xsk_bind,
1664 .connect = sock_no_connect,
1665 .socketpair = sock_no_socketpair,
1666 .accept = sock_no_accept,
1667 .getname = sock_no_getname,
1668 .poll = xsk_poll,
1669 .ioctl = sock_no_ioctl,
1670 .listen = sock_no_listen,
1671 .shutdown = sock_no_shutdown,
1672 .setsockopt = xsk_setsockopt,
1673 .getsockopt = xsk_getsockopt,
1674 .sendmsg = xsk_sendmsg,
1675 .recvmsg = xsk_recvmsg,
1676 .mmap = xsk_mmap,
1677 };
1678
xsk_destruct(struct sock * sk)1679 static void xsk_destruct(struct sock *sk)
1680 {
1681 struct xdp_sock *xs = xdp_sk(sk);
1682
1683 if (!sock_flag(sk, SOCK_DEAD))
1684 return;
1685
1686 if (!xp_put_pool(xs->pool))
1687 xdp_put_umem(xs->umem, !xs->pool);
1688 }
1689
xsk_create(struct net * net,struct socket * sock,int protocol,int kern)1690 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1691 int kern)
1692 {
1693 struct xdp_sock *xs;
1694 struct sock *sk;
1695
1696 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1697 return -EPERM;
1698 if (sock->type != SOCK_RAW)
1699 return -ESOCKTNOSUPPORT;
1700
1701 if (protocol)
1702 return -EPROTONOSUPPORT;
1703
1704 sock->state = SS_UNCONNECTED;
1705
1706 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1707 if (!sk)
1708 return -ENOBUFS;
1709
1710 sock->ops = &xsk_proto_ops;
1711
1712 sock_init_data(sock, sk);
1713
1714 sk->sk_family = PF_XDP;
1715
1716 sk->sk_destruct = xsk_destruct;
1717
1718 sock_set_flag(sk, SOCK_RCU_FREE);
1719
1720 xs = xdp_sk(sk);
1721 xs->state = XSK_READY;
1722 mutex_init(&xs->mutex);
1723 spin_lock_init(&xs->rx_lock);
1724
1725 INIT_LIST_HEAD(&xs->map_list);
1726 spin_lock_init(&xs->map_list_lock);
1727
1728 mutex_lock(&net->xdp.lock);
1729 sk_add_node_rcu(sk, &net->xdp.list);
1730 mutex_unlock(&net->xdp.lock);
1731
1732 sock_prot_inuse_add(net, &xsk_proto, 1);
1733
1734 return 0;
1735 }
1736
1737 static const struct net_proto_family xsk_family_ops = {
1738 .family = PF_XDP,
1739 .create = xsk_create,
1740 .owner = THIS_MODULE,
1741 };
1742
1743 static struct notifier_block xsk_netdev_notifier = {
1744 .notifier_call = xsk_notifier,
1745 };
1746
xsk_net_init(struct net * net)1747 static int __net_init xsk_net_init(struct net *net)
1748 {
1749 mutex_init(&net->xdp.lock);
1750 INIT_HLIST_HEAD(&net->xdp.list);
1751 return 0;
1752 }
1753
xsk_net_exit(struct net * net)1754 static void __net_exit xsk_net_exit(struct net *net)
1755 {
1756 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1757 }
1758
1759 static struct pernet_operations xsk_net_ops = {
1760 .init = xsk_net_init,
1761 .exit = xsk_net_exit,
1762 };
1763
xsk_init(void)1764 static int __init xsk_init(void)
1765 {
1766 int err;
1767
1768 err = proto_register(&xsk_proto, 0 /* no slab */);
1769 if (err)
1770 goto out;
1771
1772 err = sock_register(&xsk_family_ops);
1773 if (err)
1774 goto out_proto;
1775
1776 err = register_pernet_subsys(&xsk_net_ops);
1777 if (err)
1778 goto out_sk;
1779
1780 err = register_netdevice_notifier(&xsk_netdev_notifier);
1781 if (err)
1782 goto out_pernet;
1783
1784 return 0;
1785
1786 out_pernet:
1787 unregister_pernet_subsys(&xsk_net_ops);
1788 out_sk:
1789 sock_unregister(PF_XDP);
1790 out_proto:
1791 proto_unregister(&xsk_proto);
1792 out:
1793 return err;
1794 }
1795
1796 fs_initcall(xsk_init);
1797