1 // SPDX-License-Identifier: GPL-2.0
4 * AF_XDP sockets allows a channel between XDP programs and userspace
6 * Copyright(c) 2018 Intel Corporation.
8 * Author(s): Björn Töpel <bjorn.topel@intel.com>
9 * Magnus Karlsson <magnus.karlsson@intel.com>
12 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
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>
31 #include "xsk_queue.h"
35 #define TX_BATCH_SIZE 32
36 #define MAX_PER_SOCKET_BUDGET (TX_BATCH_SIZE)
38 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
40 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
42 if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
45 pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
46 pool->cached_need_wakeup |= XDP_WAKEUP_RX;
48 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
50 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
54 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
58 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
59 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
63 pool->cached_need_wakeup |= XDP_WAKEUP_TX;
65 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
67 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
69 if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
72 pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
73 pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
75 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
77 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
81 if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
85 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
86 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
90 pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
92 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
94 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
96 return pool->uses_need_wakeup;
98 EXPORT_SYMBOL(xsk_uses_need_wakeup);
100 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
103 if (queue_id < dev->real_num_rx_queues)
104 return dev->_rx[queue_id].pool;
105 if (queue_id < dev->real_num_tx_queues)
106 return dev->_tx[queue_id].pool;
110 EXPORT_SYMBOL(xsk_get_pool_from_qid);
112 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
114 if (queue_id < dev->num_rx_queues)
115 dev->_rx[queue_id].pool = NULL;
116 if (queue_id < dev->num_tx_queues)
117 dev->_tx[queue_id].pool = NULL;
120 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
121 * not know if the device has more tx queues than rx, or the opposite.
122 * This might also change during run time.
124 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
127 if (queue_id >= max_t(unsigned int,
128 dev->real_num_rx_queues,
129 dev->real_num_tx_queues))
132 if (queue_id < dev->real_num_rx_queues)
133 dev->_rx[queue_id].pool = pool;
134 if (queue_id < dev->real_num_tx_queues)
135 dev->_tx[queue_id].pool = pool;
140 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff_xsk *xskb, u32 len,
146 addr = xp_get_handle(xskb);
147 err = xskq_prod_reserve_desc(xs->rx, addr, len, flags);
157 static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
159 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
160 u32 frags = xdp_buff_has_frags(xdp);
161 struct xdp_buff_xsk *pos, *tmp;
162 struct list_head *xskb_list;
167 contd = XDP_PKT_CONTD;
169 err = __xsk_rcv_zc(xs, xskb, len, contd);
170 if (err || likely(!frags))
173 xskb_list = &xskb->pool->xskb_list;
174 list_for_each_entry_safe(pos, tmp, xskb_list, xskb_list_node) {
175 if (list_is_singular(xskb_list))
177 len = pos->xdp.data_end - pos->xdp.data;
178 err = __xsk_rcv_zc(xs, pos, len, contd);
181 list_del(&pos->xskb_list_node);
188 static void *xsk_copy_xdp_start(struct xdp_buff *from)
190 if (unlikely(xdp_data_meta_unsupported(from)))
193 return from->data_meta;
196 static u32 xsk_copy_xdp(void *to, void **from, u32 to_len,
197 u32 *from_len, skb_frag_t **frag, u32 rem)
202 u32 copy_len = min_t(u32, *from_len, to_len);
204 memcpy(to, *from, copy_len);
209 if (*from_len == copy_len) {
210 *from = skb_frag_address(*frag);
211 *from_len = skb_frag_size((*frag)++);
214 *from_len -= copy_len;
216 if (to_len == copy_len)
224 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
226 u32 frame_size = xsk_pool_get_rx_frame_size(xs->pool);
227 void *copy_from = xsk_copy_xdp_start(xdp), *copy_to;
228 u32 from_len, meta_len, rem, num_desc;
229 struct xdp_buff_xsk *xskb;
230 struct xdp_buff *xsk_xdp;
233 from_len = xdp->data_end - copy_from;
234 meta_len = xdp->data - copy_from;
235 rem = len + meta_len;
237 if (len <= frame_size && !xdp_buff_has_frags(xdp)) {
240 xsk_xdp = xsk_buff_alloc(xs->pool);
245 memcpy(xsk_xdp->data - meta_len, copy_from, rem);
246 xskb = container_of(xsk_xdp, struct xdp_buff_xsk, xdp);
247 err = __xsk_rcv_zc(xs, xskb, len, 0);
249 xsk_buff_free(xsk_xdp);
256 num_desc = (len - 1) / frame_size + 1;
258 if (!xsk_buff_can_alloc(xs->pool, num_desc)) {
262 if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
267 if (xdp_buff_has_frags(xdp)) {
268 struct skb_shared_info *sinfo;
270 sinfo = xdp_get_shared_info_from_buff(xdp);
271 frag = &sinfo->frags[0];
275 u32 to_len = frame_size + meta_len;
278 xsk_xdp = xsk_buff_alloc(xs->pool);
279 copy_to = xsk_xdp->data - meta_len;
281 copied = xsk_copy_xdp(copy_to, ©_from, to_len, &from_len, &frag, rem);
284 xskb = container_of(xsk_xdp, struct xdp_buff_xsk, xdp);
285 __xsk_rcv_zc(xs, xskb, copied - meta_len, rem ? XDP_PKT_CONTD : 0);
292 static bool xsk_tx_writeable(struct xdp_sock *xs)
294 if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
300 static bool xsk_is_bound(struct xdp_sock *xs)
302 if (READ_ONCE(xs->state) == XSK_BOUND) {
303 /* Matches smp_wmb() in bind(). */
310 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
312 if (!xsk_is_bound(xs))
315 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
318 if (len > xsk_pool_get_rx_frame_size(xs->pool) && !xs->sg) {
323 sk_mark_napi_id_once_xdp(&xs->sk, xdp);
327 static void xsk_flush(struct xdp_sock *xs)
329 xskq_prod_submit(xs->rx);
330 __xskq_cons_release(xs->pool->fq);
331 sock_def_readable(&xs->sk);
334 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
336 u32 len = xdp_get_buff_len(xdp);
339 spin_lock_bh(&xs->rx_lock);
340 err = xsk_rcv_check(xs, xdp, len);
342 err = __xsk_rcv(xs, xdp, len);
345 spin_unlock_bh(&xs->rx_lock);
349 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
351 u32 len = xdp_get_buff_len(xdp);
354 err = xsk_rcv_check(xs, xdp, len);
358 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
359 len = xdp->data_end - xdp->data;
360 return xsk_rcv_zc(xs, xdp, len);
363 err = __xsk_rcv(xs, xdp, len);
365 xdp_return_buff(xdp);
369 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
371 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
374 err = xsk_rcv(xs, xdp);
378 if (!xs->flush_node.prev)
379 list_add(&xs->flush_node, flush_list);
384 void __xsk_map_flush(void)
386 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
387 struct xdp_sock *xs, *tmp;
389 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
391 __list_del_clearprev(&xs->flush_node);
395 #ifdef CONFIG_DEBUG_NET
396 bool xsk_map_check_flush(void)
398 if (list_empty(this_cpu_ptr(&xskmap_flush_list)))
405 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
407 xskq_prod_submit_n(pool->cq, nb_entries);
409 EXPORT_SYMBOL(xsk_tx_completed);
411 void xsk_tx_release(struct xsk_buff_pool *pool)
416 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
417 __xskq_cons_release(xs->tx);
418 if (xsk_tx_writeable(xs))
419 xs->sk.sk_write_space(&xs->sk);
423 EXPORT_SYMBOL(xsk_tx_release);
425 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
427 bool budget_exhausted = false;
432 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
433 if (xs->tx_budget_spent >= MAX_PER_SOCKET_BUDGET) {
434 budget_exhausted = true;
438 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
439 if (xskq_has_descs(xs->tx))
440 xskq_cons_release(xs->tx);
444 xs->tx_budget_spent++;
446 /* This is the backpressure mechanism for the Tx path.
447 * Reserve space in the completion queue and only proceed
448 * if there is space in it. This avoids having to implement
449 * any buffering in the Tx path.
451 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
454 xskq_cons_release(xs->tx);
459 if (budget_exhausted) {
460 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list)
461 xs->tx_budget_spent = 0;
463 budget_exhausted = false;
471 EXPORT_SYMBOL(xsk_tx_peek_desc);
473 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
475 struct xdp_desc *descs = pool->tx_descs;
478 while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
481 xsk_tx_release(pool);
485 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts)
490 if (!list_is_singular(&pool->xsk_tx_list)) {
491 /* Fallback to the non-batched version */
493 return xsk_tx_peek_release_fallback(pool, nb_pkts);
496 xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
502 nb_pkts = xskq_cons_nb_entries(xs->tx, nb_pkts);
504 /* This is the backpressure mechanism for the Tx path. Try to
505 * reserve space in the completion queue for all packets, but
506 * if there are fewer slots available, just process that many
507 * packets. This avoids having to implement any buffering in
510 nb_pkts = xskq_prod_nb_free(pool->cq, nb_pkts);
514 nb_pkts = xskq_cons_read_desc_batch(xs->tx, pool, nb_pkts);
516 xs->tx->queue_empty_descs++;
520 __xskq_cons_release(xs->tx);
521 xskq_prod_write_addr_batch(pool->cq, pool->tx_descs, nb_pkts);
522 xs->sk.sk_write_space(&xs->sk);
528 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
530 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
532 struct net_device *dev = xs->dev;
534 return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
537 static int xsk_cq_reserve_addr_locked(struct xdp_sock *xs, u64 addr)
542 spin_lock_irqsave(&xs->pool->cq_lock, flags);
543 ret = xskq_prod_reserve_addr(xs->pool->cq, addr);
544 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
549 static void xsk_cq_submit_locked(struct xdp_sock *xs, u32 n)
553 spin_lock_irqsave(&xs->pool->cq_lock, flags);
554 xskq_prod_submit_n(xs->pool->cq, n);
555 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
558 static void xsk_cq_cancel_locked(struct xdp_sock *xs, u32 n)
562 spin_lock_irqsave(&xs->pool->cq_lock, flags);
563 xskq_prod_cancel_n(xs->pool->cq, n);
564 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
567 static u32 xsk_get_num_desc(struct sk_buff *skb)
569 return skb ? (long)skb_shinfo(skb)->destructor_arg : 0;
572 static void xsk_destruct_skb(struct sk_buff *skb)
574 xsk_cq_submit_locked(xdp_sk(skb->sk), xsk_get_num_desc(skb));
578 static void xsk_set_destructor_arg(struct sk_buff *skb)
580 long num = xsk_get_num_desc(xdp_sk(skb->sk)->skb) + 1;
582 skb_shinfo(skb)->destructor_arg = (void *)num;
585 static void xsk_consume_skb(struct sk_buff *skb)
587 struct xdp_sock *xs = xdp_sk(skb->sk);
589 skb->destructor = sock_wfree;
590 xsk_cq_cancel_locked(xs, xsk_get_num_desc(skb));
591 /* Free skb without triggering the perf drop trace */
596 static void xsk_drop_skb(struct sk_buff *skb)
598 xdp_sk(skb->sk)->tx->invalid_descs += xsk_get_num_desc(skb);
599 xsk_consume_skb(skb);
602 static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
603 struct xdp_desc *desc)
605 struct xsk_buff_pool *pool = xs->pool;
606 u32 hr, len, ts, offset, copy, copied;
607 struct sk_buff *skb = xs->skb;
614 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
616 skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
620 skb_reserve(skb, hr);
625 ts = pool->unaligned ? len : pool->chunk_size;
627 buffer = xsk_buff_raw_get_data(pool, addr);
628 offset = offset_in_page(buffer);
629 addr = buffer - pool->addrs;
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);
635 page = pool->umem->pgs[addr >> PAGE_SHIFT];
638 copy = min_t(u32, PAGE_SIZE - offset, len - copied);
639 skb_fill_page_desc(skb, i, page, offset, copy);
647 skb->data_len += len;
650 refcount_add(ts, &xs->sk.sk_wmem_alloc);
655 static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
656 struct xdp_desc *desc)
658 struct net_device *dev = xs->dev;
659 struct sk_buff *skb = xs->skb;
662 if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
663 skb = xsk_build_skb_zerocopy(xs, desc);
672 buffer = xsk_buff_raw_get_data(xs->pool, desc->addr);
676 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
677 tr = dev->needed_tailroom;
678 skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
682 skb_reserve(skb, hr);
685 err = skb_store_bits(skb, 0, buffer, len);
691 int nr_frags = skb_shinfo(skb)->nr_frags;
695 if (unlikely(nr_frags == (MAX_SKB_FRAGS - 1) && xp_mb_desc(desc))) {
700 page = alloc_page(xs->sk.sk_allocation);
701 if (unlikely(!page)) {
706 vaddr = kmap_local_page(page);
707 memcpy(vaddr, buffer, len);
710 skb_add_rx_frag(skb, nr_frags, page, 0, len, 0);
715 skb->priority = READ_ONCE(xs->sk.sk_priority);
716 skb->mark = READ_ONCE(xs->sk.sk_mark);
717 skb->destructor = xsk_destruct_skb;
718 xsk_set_destructor_arg(skb);
723 if (err == -EOVERFLOW) {
724 /* Drop the packet */
725 xsk_set_destructor_arg(xs->skb);
726 xsk_drop_skb(xs->skb);
727 xskq_cons_release(xs->tx);
729 /* Let application retry */
730 xsk_cq_cancel_locked(xs, 1);
736 static int __xsk_generic_xmit(struct sock *sk)
738 struct xdp_sock *xs = xdp_sk(sk);
739 u32 max_batch = TX_BATCH_SIZE;
740 bool sent_frame = false;
741 struct xdp_desc desc;
745 mutex_lock(&xs->mutex);
747 /* Since we dropped the RCU read lock, the socket state might have changed. */
748 if (unlikely(!xsk_is_bound(xs))) {
753 if (xs->queue_id >= xs->dev->real_num_tx_queues)
756 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
757 if (max_batch-- == 0) {
762 /* This is the backpressure mechanism for the Tx path.
763 * Reserve space in the completion queue and only proceed
764 * if there is space in it. This avoids having to implement
765 * any buffering in the Tx path.
767 if (xsk_cq_reserve_addr_locked(xs, desc.addr))
770 skb = xsk_build_skb(xs, &desc);
773 if (err != -EOVERFLOW)
779 xskq_cons_release(xs->tx);
781 if (xp_mb_desc(&desc)) {
786 err = __dev_direct_xmit(skb, xs->queue_id);
787 if (err == NETDEV_TX_BUSY) {
788 /* Tell user-space to retry the send */
789 xskq_cons_cancel_n(xs->tx, xsk_get_num_desc(skb));
790 xsk_consume_skb(skb);
795 /* Ignore NET_XMIT_CN as packet might have been sent */
796 if (err == NET_XMIT_DROP) {
797 /* SKB completed but not sent */
807 if (xskq_has_descs(xs->tx)) {
809 xsk_drop_skb(xs->skb);
810 xskq_cons_release(xs->tx);
815 if (xsk_tx_writeable(xs))
816 sk->sk_write_space(sk);
818 mutex_unlock(&xs->mutex);
822 static int xsk_generic_xmit(struct sock *sk)
826 /* Drop the RCU lock since the SKB path might sleep. */
828 ret = __xsk_generic_xmit(sk);
829 /* Reaquire RCU lock before going into common code. */
835 static bool xsk_no_wakeup(struct sock *sk)
837 #ifdef CONFIG_NET_RX_BUSY_POLL
838 /* Prefer busy-polling, skip the wakeup. */
839 return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
840 READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
846 static int xsk_check_common(struct xdp_sock *xs)
848 if (unlikely(!xsk_is_bound(xs)))
850 if (unlikely(!(xs->dev->flags & IFF_UP)))
856 static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
858 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
859 struct sock *sk = sock->sk;
860 struct xdp_sock *xs = xdp_sk(sk);
861 struct xsk_buff_pool *pool;
864 err = xsk_check_common(xs);
867 if (unlikely(need_wait))
869 if (unlikely(!xs->tx))
872 if (sk_can_busy_loop(sk)) {
874 __sk_mark_napi_id_once(sk, xsk_pool_get_napi_id(xs->pool));
875 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
878 if (xs->zc && xsk_no_wakeup(sk))
882 if (pool->cached_need_wakeup & XDP_WAKEUP_TX) {
884 return xsk_wakeup(xs, XDP_WAKEUP_TX);
885 return xsk_generic_xmit(sk);
890 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
895 ret = __xsk_sendmsg(sock, m, total_len);
901 static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
903 bool need_wait = !(flags & MSG_DONTWAIT);
904 struct sock *sk = sock->sk;
905 struct xdp_sock *xs = xdp_sk(sk);
908 err = xsk_check_common(xs);
911 if (unlikely(!xs->rx))
913 if (unlikely(need_wait))
916 if (sk_can_busy_loop(sk))
917 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
919 if (xsk_no_wakeup(sk))
922 if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
923 return xsk_wakeup(xs, XDP_WAKEUP_RX);
927 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
932 ret = __xsk_recvmsg(sock, m, len, flags);
938 static __poll_t xsk_poll(struct file *file, struct socket *sock,
939 struct poll_table_struct *wait)
942 struct sock *sk = sock->sk;
943 struct xdp_sock *xs = xdp_sk(sk);
944 struct xsk_buff_pool *pool;
946 sock_poll_wait(file, sock, wait);
949 if (xsk_check_common(xs))
954 if (pool->cached_need_wakeup) {
956 xsk_wakeup(xs, pool->cached_need_wakeup);
958 /* Poll needs to drive Tx also in copy mode */
959 xsk_generic_xmit(sk);
962 if (xs->rx && !xskq_prod_is_empty(xs->rx))
963 mask |= EPOLLIN | EPOLLRDNORM;
964 if (xs->tx && xsk_tx_writeable(xs))
965 mask |= EPOLLOUT | EPOLLWRNORM;
971 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
976 if (entries == 0 || *queue || !is_power_of_2(entries))
979 q = xskq_create(entries, umem_queue);
983 /* Make sure queue is ready before it can be seen by others */
985 WRITE_ONCE(*queue, q);
989 static void xsk_unbind_dev(struct xdp_sock *xs)
991 struct net_device *dev = xs->dev;
993 if (xs->state != XSK_BOUND)
995 WRITE_ONCE(xs->state, XSK_UNBOUND);
997 /* Wait for driver to stop using the xdp socket. */
998 xp_del_xsk(xs->pool, xs);
1003 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
1004 struct xdp_sock __rcu ***map_entry)
1006 struct xsk_map *map = NULL;
1007 struct xsk_map_node *node;
1011 spin_lock_bh(&xs->map_list_lock);
1012 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
1015 bpf_map_inc(&node->map->map);
1017 *map_entry = node->map_entry;
1019 spin_unlock_bh(&xs->map_list_lock);
1023 static void xsk_delete_from_maps(struct xdp_sock *xs)
1025 /* This function removes the current XDP socket from all the
1026 * maps it resides in. We need to take extra care here, due to
1027 * the two locks involved. Each map has a lock synchronizing
1028 * updates to the entries, and each socket has a lock that
1029 * synchronizes access to the list of maps (map_list). For
1030 * deadlock avoidance the locks need to be taken in the order
1031 * "map lock"->"socket map list lock". We start off by
1032 * accessing the socket map list, and take a reference to the
1033 * map to guarantee existence between the
1034 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
1035 * calls. Then we ask the map to remove the socket, which
1036 * tries to remove the socket from the map. Note that there
1037 * might be updates to the map between
1038 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
1040 struct xdp_sock __rcu **map_entry = NULL;
1041 struct xsk_map *map;
1043 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
1044 xsk_map_try_sock_delete(map, xs, map_entry);
1045 bpf_map_put(&map->map);
1049 static int xsk_release(struct socket *sock)
1051 struct sock *sk = sock->sk;
1052 struct xdp_sock *xs = xdp_sk(sk);
1061 xsk_drop_skb(xs->skb);
1063 mutex_lock(&net->xdp.lock);
1064 sk_del_node_init_rcu(sk);
1065 mutex_unlock(&net->xdp.lock);
1067 sock_prot_inuse_add(net, sk->sk_prot, -1);
1069 xsk_delete_from_maps(xs);
1070 mutex_lock(&xs->mutex);
1072 mutex_unlock(&xs->mutex);
1074 xskq_destroy(xs->rx);
1075 xskq_destroy(xs->tx);
1076 xskq_destroy(xs->fq_tmp);
1077 xskq_destroy(xs->cq_tmp);
1087 static struct socket *xsk_lookup_xsk_from_fd(int fd)
1089 struct socket *sock;
1092 sock = sockfd_lookup(fd, &err);
1094 return ERR_PTR(-ENOTSOCK);
1096 if (sock->sk->sk_family != PF_XDP) {
1098 return ERR_PTR(-ENOPROTOOPT);
1104 static bool xsk_validate_queues(struct xdp_sock *xs)
1106 return xs->fq_tmp && xs->cq_tmp;
1109 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
1111 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
1112 struct sock *sk = sock->sk;
1113 struct xdp_sock *xs = xdp_sk(sk);
1114 struct net_device *dev;
1119 if (addr_len < sizeof(struct sockaddr_xdp))
1121 if (sxdp->sxdp_family != AF_XDP)
1124 flags = sxdp->sxdp_flags;
1125 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
1126 XDP_USE_NEED_WAKEUP | XDP_USE_SG))
1129 bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1130 if (bound_dev_if && bound_dev_if != sxdp->sxdp_ifindex)
1134 mutex_lock(&xs->mutex);
1135 if (xs->state != XSK_READY) {
1140 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
1146 if (!xs->rx && !xs->tx) {
1151 qid = sxdp->sxdp_queue_id;
1153 if (flags & XDP_SHARED_UMEM) {
1154 struct xdp_sock *umem_xs;
1155 struct socket *sock;
1157 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
1158 (flags & XDP_USE_NEED_WAKEUP) || (flags & XDP_USE_SG)) {
1159 /* Cannot specify flags for shared sockets. */
1165 /* We have already our own. */
1170 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
1172 err = PTR_ERR(sock);
1176 umem_xs = xdp_sk(sock->sk);
1177 if (!xsk_is_bound(umem_xs)) {
1183 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
1184 /* Share the umem with another socket on another qid
1187 xs->pool = xp_create_and_assign_umem(xs,
1195 err = xp_assign_dev_shared(xs->pool, umem_xs, dev,
1198 xp_destroy(xs->pool);
1204 /* Share the buffer pool with the other socket. */
1205 if (xs->fq_tmp || xs->cq_tmp) {
1206 /* Do not allow setting your own fq or cq. */
1212 xp_get_pool(umem_xs->pool);
1213 xs->pool = umem_xs->pool;
1215 /* If underlying shared umem was created without Tx
1216 * ring, allocate Tx descs array that Tx batching API
1219 if (xs->tx && !xs->pool->tx_descs) {
1220 err = xp_alloc_tx_descs(xs->pool, xs);
1222 xp_put_pool(xs->pool);
1230 xdp_get_umem(umem_xs->umem);
1231 WRITE_ONCE(xs->umem, umem_xs->umem);
1233 } else if (!xs->umem || !xsk_validate_queues(xs)) {
1237 /* This xsk has its own umem. */
1238 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
1244 err = xp_assign_dev(xs->pool, dev, qid, flags);
1246 xp_destroy(xs->pool);
1252 /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
1257 xs->zc = xs->umem->zc;
1258 xs->sg = !!(xs->umem->flags & XDP_UMEM_SG_FLAG);
1260 xp_add_xsk(xs->pool, xs);
1266 /* Matches smp_rmb() in bind() for shared umem
1267 * sockets, and xsk_is_bound().
1270 WRITE_ONCE(xs->state, XSK_BOUND);
1273 mutex_unlock(&xs->mutex);
1278 struct xdp_umem_reg_v1 {
1279 __u64 addr; /* Start of packet data area */
1280 __u64 len; /* Length of packet data area */
1285 static int xsk_setsockopt(struct socket *sock, int level, int optname,
1286 sockptr_t optval, unsigned int optlen)
1288 struct sock *sk = sock->sk;
1289 struct xdp_sock *xs = xdp_sk(sk);
1292 if (level != SOL_XDP)
1293 return -ENOPROTOOPT;
1299 struct xsk_queue **q;
1302 if (optlen < sizeof(entries))
1304 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1307 mutex_lock(&xs->mutex);
1308 if (xs->state != XSK_READY) {
1309 mutex_unlock(&xs->mutex);
1312 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
1313 err = xsk_init_queue(entries, q, false);
1314 if (!err && optname == XDP_TX_RING)
1315 /* Tx needs to be explicitly woken up the first time */
1316 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
1317 mutex_unlock(&xs->mutex);
1322 size_t mr_size = sizeof(struct xdp_umem_reg);
1323 struct xdp_umem_reg mr = {};
1324 struct xdp_umem *umem;
1326 if (optlen < sizeof(struct xdp_umem_reg_v1))
1328 else if (optlen < sizeof(mr))
1329 mr_size = sizeof(struct xdp_umem_reg_v1);
1331 if (copy_from_sockptr(&mr, optval, mr_size))
1334 mutex_lock(&xs->mutex);
1335 if (xs->state != XSK_READY || xs->umem) {
1336 mutex_unlock(&xs->mutex);
1340 umem = xdp_umem_create(&mr);
1342 mutex_unlock(&xs->mutex);
1343 return PTR_ERR(umem);
1346 /* Make sure umem is ready before it can be seen by others */
1348 WRITE_ONCE(xs->umem, umem);
1349 mutex_unlock(&xs->mutex);
1352 case XDP_UMEM_FILL_RING:
1353 case XDP_UMEM_COMPLETION_RING:
1355 struct xsk_queue **q;
1358 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1361 mutex_lock(&xs->mutex);
1362 if (xs->state != XSK_READY) {
1363 mutex_unlock(&xs->mutex);
1367 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1369 err = xsk_init_queue(entries, q, true);
1370 mutex_unlock(&xs->mutex);
1377 return -ENOPROTOOPT;
1380 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1382 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1383 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1384 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1387 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1389 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1390 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1391 ring->desc = offsetof(struct xdp_umem_ring, desc);
1394 struct xdp_statistics_v1 {
1396 __u64 rx_invalid_descs;
1397 __u64 tx_invalid_descs;
1400 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1401 char __user *optval, int __user *optlen)
1403 struct sock *sk = sock->sk;
1404 struct xdp_sock *xs = xdp_sk(sk);
1407 if (level != SOL_XDP)
1408 return -ENOPROTOOPT;
1410 if (get_user(len, optlen))
1416 case XDP_STATISTICS:
1418 struct xdp_statistics stats = {};
1419 bool extra_stats = true;
1422 if (len < sizeof(struct xdp_statistics_v1)) {
1424 } else if (len < sizeof(stats)) {
1425 extra_stats = false;
1426 stats_size = sizeof(struct xdp_statistics_v1);
1428 stats_size = sizeof(stats);
1431 mutex_lock(&xs->mutex);
1432 stats.rx_dropped = xs->rx_dropped;
1434 stats.rx_ring_full = xs->rx_queue_full;
1435 stats.rx_fill_ring_empty_descs =
1436 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1437 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1439 stats.rx_dropped += xs->rx_queue_full;
1441 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1442 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1443 mutex_unlock(&xs->mutex);
1445 if (copy_to_user(optval, &stats, stats_size))
1447 if (put_user(stats_size, optlen))
1452 case XDP_MMAP_OFFSETS:
1454 struct xdp_mmap_offsets off;
1455 struct xdp_mmap_offsets_v1 off_v1;
1456 bool flags_supported = true;
1459 if (len < sizeof(off_v1))
1461 else if (len < sizeof(off))
1462 flags_supported = false;
1464 if (flags_supported) {
1465 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1466 * except for the flags field added to the end.
1468 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1470 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1472 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1474 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1476 off.rx.flags = offsetof(struct xdp_rxtx_ring,
1478 off.tx.flags = offsetof(struct xdp_rxtx_ring,
1480 off.fr.flags = offsetof(struct xdp_umem_ring,
1482 off.cr.flags = offsetof(struct xdp_umem_ring,
1488 xsk_enter_rxtx_offsets(&off_v1.rx);
1489 xsk_enter_rxtx_offsets(&off_v1.tx);
1490 xsk_enter_umem_offsets(&off_v1.fr);
1491 xsk_enter_umem_offsets(&off_v1.cr);
1493 len = sizeof(off_v1);
1497 if (copy_to_user(optval, to_copy, len))
1499 if (put_user(len, optlen))
1506 struct xdp_options opts = {};
1508 if (len < sizeof(opts))
1511 mutex_lock(&xs->mutex);
1513 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1514 mutex_unlock(&xs->mutex);
1517 if (copy_to_user(optval, &opts, len))
1519 if (put_user(len, optlen))
1531 static int xsk_mmap(struct file *file, struct socket *sock,
1532 struct vm_area_struct *vma)
1534 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1535 unsigned long size = vma->vm_end - vma->vm_start;
1536 struct xdp_sock *xs = xdp_sk(sock->sk);
1537 int state = READ_ONCE(xs->state);
1538 struct xsk_queue *q = NULL;
1540 if (state != XSK_READY && state != XSK_BOUND)
1543 if (offset == XDP_PGOFF_RX_RING) {
1544 q = READ_ONCE(xs->rx);
1545 } else if (offset == XDP_PGOFF_TX_RING) {
1546 q = READ_ONCE(xs->tx);
1548 /* Matches the smp_wmb() in XDP_UMEM_REG */
1550 if (offset == XDP_UMEM_PGOFF_FILL_RING)
1551 q = state == XSK_READY ? READ_ONCE(xs->fq_tmp) :
1552 READ_ONCE(xs->pool->fq);
1553 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1554 q = state == XSK_READY ? READ_ONCE(xs->cq_tmp) :
1555 READ_ONCE(xs->pool->cq);
1561 /* Matches the smp_wmb() in xsk_init_queue */
1563 if (size > q->ring_vmalloc_size)
1566 return remap_vmalloc_range(vma, q->ring, 0);
1569 static int xsk_notifier(struct notifier_block *this,
1570 unsigned long msg, void *ptr)
1572 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1573 struct net *net = dev_net(dev);
1577 case NETDEV_UNREGISTER:
1578 mutex_lock(&net->xdp.lock);
1579 sk_for_each(sk, &net->xdp.list) {
1580 struct xdp_sock *xs = xdp_sk(sk);
1582 mutex_lock(&xs->mutex);
1583 if (xs->dev == dev) {
1584 sk->sk_err = ENETDOWN;
1585 if (!sock_flag(sk, SOCK_DEAD))
1586 sk_error_report(sk);
1590 /* Clear device references. */
1591 xp_clear_dev(xs->pool);
1593 mutex_unlock(&xs->mutex);
1595 mutex_unlock(&net->xdp.lock);
1601 static struct proto xsk_proto = {
1603 .owner = THIS_MODULE,
1604 .obj_size = sizeof(struct xdp_sock),
1607 static const struct proto_ops xsk_proto_ops = {
1609 .owner = THIS_MODULE,
1610 .release = xsk_release,
1612 .connect = sock_no_connect,
1613 .socketpair = sock_no_socketpair,
1614 .accept = sock_no_accept,
1615 .getname = sock_no_getname,
1617 .ioctl = sock_no_ioctl,
1618 .listen = sock_no_listen,
1619 .shutdown = sock_no_shutdown,
1620 .setsockopt = xsk_setsockopt,
1621 .getsockopt = xsk_getsockopt,
1622 .sendmsg = xsk_sendmsg,
1623 .recvmsg = xsk_recvmsg,
1627 static void xsk_destruct(struct sock *sk)
1629 struct xdp_sock *xs = xdp_sk(sk);
1631 if (!sock_flag(sk, SOCK_DEAD))
1634 if (!xp_put_pool(xs->pool))
1635 xdp_put_umem(xs->umem, !xs->pool);
1638 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1641 struct xdp_sock *xs;
1644 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1646 if (sock->type != SOCK_RAW)
1647 return -ESOCKTNOSUPPORT;
1650 return -EPROTONOSUPPORT;
1652 sock->state = SS_UNCONNECTED;
1654 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1658 sock->ops = &xsk_proto_ops;
1660 sock_init_data(sock, sk);
1662 sk->sk_family = PF_XDP;
1664 sk->sk_destruct = xsk_destruct;
1666 sock_set_flag(sk, SOCK_RCU_FREE);
1669 xs->state = XSK_READY;
1670 mutex_init(&xs->mutex);
1671 spin_lock_init(&xs->rx_lock);
1673 INIT_LIST_HEAD(&xs->map_list);
1674 spin_lock_init(&xs->map_list_lock);
1676 mutex_lock(&net->xdp.lock);
1677 sk_add_node_rcu(sk, &net->xdp.list);
1678 mutex_unlock(&net->xdp.lock);
1680 sock_prot_inuse_add(net, &xsk_proto, 1);
1685 static const struct net_proto_family xsk_family_ops = {
1687 .create = xsk_create,
1688 .owner = THIS_MODULE,
1691 static struct notifier_block xsk_netdev_notifier = {
1692 .notifier_call = xsk_notifier,
1695 static int __net_init xsk_net_init(struct net *net)
1697 mutex_init(&net->xdp.lock);
1698 INIT_HLIST_HEAD(&net->xdp.list);
1702 static void __net_exit xsk_net_exit(struct net *net)
1704 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1707 static struct pernet_operations xsk_net_ops = {
1708 .init = xsk_net_init,
1709 .exit = xsk_net_exit,
1712 static int __init xsk_init(void)
1716 err = proto_register(&xsk_proto, 0 /* no slab */);
1720 err = sock_register(&xsk_family_ops);
1724 err = register_pernet_subsys(&xsk_net_ops);
1728 err = register_netdevice_notifier(&xsk_netdev_notifier);
1732 for_each_possible_cpu(cpu)
1733 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1737 unregister_pernet_subsys(&xsk_net_ops);
1739 sock_unregister(PF_XDP);
1741 proto_unregister(&xsk_proto);
1746 fs_initcall(xsk_init);