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 <net/xdp_sock_drv.h>
26 #include <net/busy_poll.h>
29 #include "xsk_queue.h"
33 #define TX_BATCH_SIZE 32
35 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
37 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
39 if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
42 pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
43 pool->cached_need_wakeup |= XDP_WAKEUP_RX;
45 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
47 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
51 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
55 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
56 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
60 pool->cached_need_wakeup |= XDP_WAKEUP_TX;
62 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
64 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
66 if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
69 pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
70 pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
72 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
74 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
78 if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
82 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
83 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
87 pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
89 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
91 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
93 return pool->uses_need_wakeup;
95 EXPORT_SYMBOL(xsk_uses_need_wakeup);
97 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
100 if (queue_id < dev->real_num_rx_queues)
101 return dev->_rx[queue_id].pool;
102 if (queue_id < dev->real_num_tx_queues)
103 return dev->_tx[queue_id].pool;
107 EXPORT_SYMBOL(xsk_get_pool_from_qid);
109 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
111 if (queue_id < dev->num_rx_queues)
112 dev->_rx[queue_id].pool = NULL;
113 if (queue_id < dev->num_tx_queues)
114 dev->_tx[queue_id].pool = NULL;
117 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
118 * not know if the device has more tx queues than rx, or the opposite.
119 * This might also change during run time.
121 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
124 if (queue_id >= max_t(unsigned int,
125 dev->real_num_rx_queues,
126 dev->real_num_tx_queues))
129 if (queue_id < dev->real_num_rx_queues)
130 dev->_rx[queue_id].pool = pool;
131 if (queue_id < dev->real_num_tx_queues)
132 dev->_tx[queue_id].pool = pool;
137 void xp_release(struct xdp_buff_xsk *xskb)
139 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
142 static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
144 u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
146 offset += xskb->pool->headroom;
147 if (!xskb->pool->unaligned)
148 return xskb->orig_addr + offset;
149 return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
152 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
154 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
158 addr = xp_get_handle(xskb);
159 err = xskq_prod_reserve_desc(xs->rx, addr, len);
169 static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
171 void *from_buf, *to_buf;
174 if (unlikely(xdp_data_meta_unsupported(from))) {
175 from_buf = from->data;
179 from_buf = from->data_meta;
180 metalen = from->data - from->data_meta;
181 to_buf = to->data - metalen;
184 memcpy(to_buf, from_buf, len + metalen);
187 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
189 struct xdp_buff *xsk_xdp;
193 len = xdp->data_end - xdp->data;
194 if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
199 xsk_xdp = xsk_buff_alloc(xs->pool);
205 xsk_copy_xdp(xsk_xdp, xdp, len);
206 err = __xsk_rcv_zc(xs, xsk_xdp, len);
208 xsk_buff_free(xsk_xdp);
214 static bool xsk_tx_writeable(struct xdp_sock *xs)
216 if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
222 static bool xsk_is_bound(struct xdp_sock *xs)
224 if (READ_ONCE(xs->state) == XSK_BOUND) {
225 /* Matches smp_wmb() in bind(). */
232 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp)
234 if (!xsk_is_bound(xs))
237 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
240 sk_mark_napi_id_once_xdp(&xs->sk, xdp);
244 static void xsk_flush(struct xdp_sock *xs)
246 xskq_prod_submit(xs->rx);
247 __xskq_cons_release(xs->pool->fq);
248 sock_def_readable(&xs->sk);
251 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
255 spin_lock_bh(&xs->rx_lock);
256 err = xsk_rcv_check(xs, xdp);
258 err = __xsk_rcv(xs, xdp);
261 spin_unlock_bh(&xs->rx_lock);
265 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
270 err = xsk_rcv_check(xs, xdp);
274 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
275 len = xdp->data_end - xdp->data;
276 return __xsk_rcv_zc(xs, xdp, len);
279 err = __xsk_rcv(xs, xdp);
281 xdp_return_buff(xdp);
285 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
287 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
290 err = xsk_rcv(xs, xdp);
294 if (!xs->flush_node.prev)
295 list_add(&xs->flush_node, flush_list);
300 void __xsk_map_flush(void)
302 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
303 struct xdp_sock *xs, *tmp;
305 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
307 __list_del_clearprev(&xs->flush_node);
311 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
313 xskq_prod_submit_n(pool->cq, nb_entries);
315 EXPORT_SYMBOL(xsk_tx_completed);
317 void xsk_tx_release(struct xsk_buff_pool *pool)
322 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
323 __xskq_cons_release(xs->tx);
324 if (xsk_tx_writeable(xs))
325 xs->sk.sk_write_space(&xs->sk);
329 EXPORT_SYMBOL(xsk_tx_release);
331 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
336 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
337 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
338 xs->tx->queue_empty_descs++;
342 /* This is the backpressure mechanism for the Tx path.
343 * Reserve space in the completion queue and only proceed
344 * if there is space in it. This avoids having to implement
345 * any buffering in the Tx path.
347 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
350 xskq_cons_release(xs->tx);
359 EXPORT_SYMBOL(xsk_tx_peek_desc);
361 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, struct xdp_desc *descs,
366 while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
369 xsk_tx_release(pool);
373 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, struct xdp_desc *descs,
380 if (!list_is_singular(&pool->xsk_tx_list)) {
381 /* Fallback to the non-batched version */
383 return xsk_tx_peek_release_fallback(pool, descs, max_entries);
386 xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
392 nb_pkts = xskq_cons_peek_desc_batch(xs->tx, descs, pool, max_entries);
394 xs->tx->queue_empty_descs++;
398 /* This is the backpressure mechanism for the Tx path. Try to
399 * reserve space in the completion queue for all packets, but
400 * if there are fewer slots available, just process that many
401 * packets. This avoids having to implement any buffering in
404 nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, descs, nb_pkts);
408 xskq_cons_release_n(xs->tx, nb_pkts);
409 __xskq_cons_release(xs->tx);
410 xs->sk.sk_write_space(&xs->sk);
416 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
418 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
420 struct net_device *dev = xs->dev;
424 err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
430 static int xsk_zc_xmit(struct xdp_sock *xs)
432 return xsk_wakeup(xs, XDP_WAKEUP_TX);
435 static void xsk_destruct_skb(struct sk_buff *skb)
437 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
438 struct xdp_sock *xs = xdp_sk(skb->sk);
441 spin_lock_irqsave(&xs->pool->cq_lock, flags);
442 xskq_prod_submit_addr(xs->pool->cq, addr);
443 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
448 static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
449 struct xdp_desc *desc)
451 struct xsk_buff_pool *pool = xs->pool;
452 u32 hr, len, ts, offset, copy, copied;
459 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
461 skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
465 skb_reserve(skb, hr);
469 ts = pool->unaligned ? len : pool->chunk_size;
471 buffer = xsk_buff_raw_get_data(pool, addr);
472 offset = offset_in_page(buffer);
473 addr = buffer - pool->addrs;
475 for (copied = 0, i = 0; copied < len; i++) {
476 page = pool->umem->pgs[addr >> PAGE_SHIFT];
479 copy = min_t(u32, PAGE_SIZE - offset, len - copied);
480 skb_fill_page_desc(skb, i, page, offset, copy);
488 skb->data_len += len;
491 refcount_add(ts, &xs->sk.sk_wmem_alloc);
496 static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
497 struct xdp_desc *desc)
499 struct net_device *dev = xs->dev;
502 if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
503 skb = xsk_build_skb_zerocopy(xs, desc);
511 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
512 tr = dev->needed_tailroom;
515 skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
519 skb_reserve(skb, hr);
522 buffer = xsk_buff_raw_get_data(xs->pool, desc->addr);
523 err = skb_store_bits(skb, 0, buffer, len);
531 skb->priority = xs->sk.sk_priority;
532 skb->mark = xs->sk.sk_mark;
533 skb_shinfo(skb)->destructor_arg = (void *)(long)desc->addr;
534 skb->destructor = xsk_destruct_skb;
539 static int xsk_generic_xmit(struct sock *sk)
541 struct xdp_sock *xs = xdp_sk(sk);
542 u32 max_batch = TX_BATCH_SIZE;
543 bool sent_frame = false;
544 struct xdp_desc desc;
549 mutex_lock(&xs->mutex);
551 if (xs->queue_id >= xs->dev->real_num_tx_queues)
554 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
555 if (max_batch-- == 0) {
560 skb = xsk_build_skb(xs, &desc);
566 /* This is the backpressure mechanism for the Tx path.
567 * Reserve space in the completion queue and only proceed
568 * if there is space in it. This avoids having to implement
569 * any buffering in the Tx path.
571 spin_lock_irqsave(&xs->pool->cq_lock, flags);
572 if (xskq_prod_reserve(xs->pool->cq)) {
573 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
577 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
579 err = __dev_direct_xmit(skb, xs->queue_id);
580 if (err == NETDEV_TX_BUSY) {
581 /* Tell user-space to retry the send */
582 skb->destructor = sock_wfree;
583 spin_lock_irqsave(&xs->pool->cq_lock, flags);
584 xskq_prod_cancel(xs->pool->cq);
585 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
586 /* Free skb without triggering the perf drop trace */
592 xskq_cons_release(xs->tx);
593 /* Ignore NET_XMIT_CN as packet might have been sent */
594 if (err == NET_XMIT_DROP) {
595 /* SKB completed but not sent */
603 xs->tx->queue_empty_descs++;
607 if (xsk_tx_writeable(xs))
608 sk->sk_write_space(sk);
610 mutex_unlock(&xs->mutex);
614 static int __xsk_sendmsg(struct sock *sk)
616 struct xdp_sock *xs = xdp_sk(sk);
618 if (unlikely(!(xs->dev->flags & IFF_UP)))
620 if (unlikely(!xs->tx))
623 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
626 static bool xsk_no_wakeup(struct sock *sk)
628 #ifdef CONFIG_NET_RX_BUSY_POLL
629 /* Prefer busy-polling, skip the wakeup. */
630 return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
631 READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
637 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
639 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
640 struct sock *sk = sock->sk;
641 struct xdp_sock *xs = xdp_sk(sk);
642 struct xsk_buff_pool *pool;
644 if (unlikely(!xsk_is_bound(xs)))
646 if (unlikely(need_wait))
649 if (sk_can_busy_loop(sk))
650 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
652 if (xsk_no_wakeup(sk))
656 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
657 return __xsk_sendmsg(sk);
661 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
663 bool need_wait = !(flags & MSG_DONTWAIT);
664 struct sock *sk = sock->sk;
665 struct xdp_sock *xs = xdp_sk(sk);
667 if (unlikely(!xsk_is_bound(xs)))
669 if (unlikely(!(xs->dev->flags & IFF_UP)))
671 if (unlikely(!xs->rx))
673 if (unlikely(need_wait))
676 if (sk_can_busy_loop(sk))
677 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
679 if (xsk_no_wakeup(sk))
682 if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
683 return xsk_wakeup(xs, XDP_WAKEUP_RX);
687 static __poll_t xsk_poll(struct file *file, struct socket *sock,
688 struct poll_table_struct *wait)
691 struct sock *sk = sock->sk;
692 struct xdp_sock *xs = xdp_sk(sk);
693 struct xsk_buff_pool *pool;
695 sock_poll_wait(file, sock, wait);
697 if (unlikely(!xsk_is_bound(xs)))
702 if (pool->cached_need_wakeup) {
704 xsk_wakeup(xs, pool->cached_need_wakeup);
706 /* Poll needs to drive Tx also in copy mode */
710 if (xs->rx && !xskq_prod_is_empty(xs->rx))
711 mask |= EPOLLIN | EPOLLRDNORM;
712 if (xs->tx && xsk_tx_writeable(xs))
713 mask |= EPOLLOUT | EPOLLWRNORM;
718 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
723 if (entries == 0 || *queue || !is_power_of_2(entries))
726 q = xskq_create(entries, umem_queue);
730 /* Make sure queue is ready before it can be seen by others */
732 WRITE_ONCE(*queue, q);
736 static void xsk_unbind_dev(struct xdp_sock *xs)
738 struct net_device *dev = xs->dev;
740 if (xs->state != XSK_BOUND)
742 WRITE_ONCE(xs->state, XSK_UNBOUND);
744 /* Wait for driver to stop using the xdp socket. */
745 xp_del_xsk(xs->pool, xs);
751 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
752 struct xdp_sock __rcu ***map_entry)
754 struct xsk_map *map = NULL;
755 struct xsk_map_node *node;
759 spin_lock_bh(&xs->map_list_lock);
760 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
763 bpf_map_inc(&node->map->map);
765 *map_entry = node->map_entry;
767 spin_unlock_bh(&xs->map_list_lock);
771 static void xsk_delete_from_maps(struct xdp_sock *xs)
773 /* This function removes the current XDP socket from all the
774 * maps it resides in. We need to take extra care here, due to
775 * the two locks involved. Each map has a lock synchronizing
776 * updates to the entries, and each socket has a lock that
777 * synchronizes access to the list of maps (map_list). For
778 * deadlock avoidance the locks need to be taken in the order
779 * "map lock"->"socket map list lock". We start off by
780 * accessing the socket map list, and take a reference to the
781 * map to guarantee existence between the
782 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
783 * calls. Then we ask the map to remove the socket, which
784 * tries to remove the socket from the map. Note that there
785 * might be updates to the map between
786 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
788 struct xdp_sock __rcu **map_entry = NULL;
791 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
792 xsk_map_try_sock_delete(map, xs, map_entry);
793 bpf_map_put(&map->map);
797 static int xsk_release(struct socket *sock)
799 struct sock *sk = sock->sk;
800 struct xdp_sock *xs = xdp_sk(sk);
808 mutex_lock(&net->xdp.lock);
809 sk_del_node_init_rcu(sk);
810 mutex_unlock(&net->xdp.lock);
813 sock_prot_inuse_add(net, sk->sk_prot, -1);
816 xsk_delete_from_maps(xs);
817 mutex_lock(&xs->mutex);
819 mutex_unlock(&xs->mutex);
821 xskq_destroy(xs->rx);
822 xskq_destroy(xs->tx);
823 xskq_destroy(xs->fq_tmp);
824 xskq_destroy(xs->cq_tmp);
829 sk_refcnt_debug_release(sk);
835 static struct socket *xsk_lookup_xsk_from_fd(int fd)
840 sock = sockfd_lookup(fd, &err);
842 return ERR_PTR(-ENOTSOCK);
844 if (sock->sk->sk_family != PF_XDP) {
846 return ERR_PTR(-ENOPROTOOPT);
852 static bool xsk_validate_queues(struct xdp_sock *xs)
854 return xs->fq_tmp && xs->cq_tmp;
857 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
859 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
860 struct sock *sk = sock->sk;
861 struct xdp_sock *xs = xdp_sk(sk);
862 struct net_device *dev;
866 if (addr_len < sizeof(struct sockaddr_xdp))
868 if (sxdp->sxdp_family != AF_XDP)
871 flags = sxdp->sxdp_flags;
872 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
873 XDP_USE_NEED_WAKEUP))
877 mutex_lock(&xs->mutex);
878 if (xs->state != XSK_READY) {
883 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
889 if (!xs->rx && !xs->tx) {
894 qid = sxdp->sxdp_queue_id;
896 if (flags & XDP_SHARED_UMEM) {
897 struct xdp_sock *umem_xs;
900 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
901 (flags & XDP_USE_NEED_WAKEUP)) {
902 /* Cannot specify flags for shared sockets. */
908 /* We have already our own. */
913 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
919 umem_xs = xdp_sk(sock->sk);
920 if (!xsk_is_bound(umem_xs)) {
926 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
927 /* Share the umem with another socket on another qid
930 xs->pool = xp_create_and_assign_umem(xs,
938 err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
941 xp_destroy(xs->pool);
947 /* Share the buffer pool with the other socket. */
948 if (xs->fq_tmp || xs->cq_tmp) {
949 /* Do not allow setting your own fq or cq. */
955 xp_get_pool(umem_xs->pool);
956 xs->pool = umem_xs->pool;
959 xdp_get_umem(umem_xs->umem);
960 WRITE_ONCE(xs->umem, umem_xs->umem);
962 } else if (!xs->umem || !xsk_validate_queues(xs)) {
966 /* This xsk has its own umem. */
967 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
973 err = xp_assign_dev(xs->pool, dev, qid, flags);
975 xp_destroy(xs->pool);
981 /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
986 xs->zc = xs->umem->zc;
988 xp_add_xsk(xs->pool, xs);
994 /* Matches smp_rmb() in bind() for shared umem
995 * sockets, and xsk_is_bound().
998 WRITE_ONCE(xs->state, XSK_BOUND);
1001 mutex_unlock(&xs->mutex);
1006 struct xdp_umem_reg_v1 {
1007 __u64 addr; /* Start of packet data area */
1008 __u64 len; /* Length of packet data area */
1013 static int xsk_setsockopt(struct socket *sock, int level, int optname,
1014 sockptr_t optval, unsigned int optlen)
1016 struct sock *sk = sock->sk;
1017 struct xdp_sock *xs = xdp_sk(sk);
1020 if (level != SOL_XDP)
1021 return -ENOPROTOOPT;
1027 struct xsk_queue **q;
1030 if (optlen < sizeof(entries))
1032 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1035 mutex_lock(&xs->mutex);
1036 if (xs->state != XSK_READY) {
1037 mutex_unlock(&xs->mutex);
1040 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
1041 err = xsk_init_queue(entries, q, false);
1042 if (!err && optname == XDP_TX_RING)
1043 /* Tx needs to be explicitly woken up the first time */
1044 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
1045 mutex_unlock(&xs->mutex);
1050 size_t mr_size = sizeof(struct xdp_umem_reg);
1051 struct xdp_umem_reg mr = {};
1052 struct xdp_umem *umem;
1054 if (optlen < sizeof(struct xdp_umem_reg_v1))
1056 else if (optlen < sizeof(mr))
1057 mr_size = sizeof(struct xdp_umem_reg_v1);
1059 if (copy_from_sockptr(&mr, optval, mr_size))
1062 mutex_lock(&xs->mutex);
1063 if (xs->state != XSK_READY || xs->umem) {
1064 mutex_unlock(&xs->mutex);
1068 umem = xdp_umem_create(&mr);
1070 mutex_unlock(&xs->mutex);
1071 return PTR_ERR(umem);
1074 /* Make sure umem is ready before it can be seen by others */
1076 WRITE_ONCE(xs->umem, umem);
1077 mutex_unlock(&xs->mutex);
1080 case XDP_UMEM_FILL_RING:
1081 case XDP_UMEM_COMPLETION_RING:
1083 struct xsk_queue **q;
1086 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1089 mutex_lock(&xs->mutex);
1090 if (xs->state != XSK_READY) {
1091 mutex_unlock(&xs->mutex);
1095 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1097 err = xsk_init_queue(entries, q, true);
1098 mutex_unlock(&xs->mutex);
1105 return -ENOPROTOOPT;
1108 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1110 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1111 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1112 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1115 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1117 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1118 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1119 ring->desc = offsetof(struct xdp_umem_ring, desc);
1122 struct xdp_statistics_v1 {
1124 __u64 rx_invalid_descs;
1125 __u64 tx_invalid_descs;
1128 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1129 char __user *optval, int __user *optlen)
1131 struct sock *sk = sock->sk;
1132 struct xdp_sock *xs = xdp_sk(sk);
1135 if (level != SOL_XDP)
1136 return -ENOPROTOOPT;
1138 if (get_user(len, optlen))
1144 case XDP_STATISTICS:
1146 struct xdp_statistics stats = {};
1147 bool extra_stats = true;
1150 if (len < sizeof(struct xdp_statistics_v1)) {
1152 } else if (len < sizeof(stats)) {
1153 extra_stats = false;
1154 stats_size = sizeof(struct xdp_statistics_v1);
1156 stats_size = sizeof(stats);
1159 mutex_lock(&xs->mutex);
1160 stats.rx_dropped = xs->rx_dropped;
1162 stats.rx_ring_full = xs->rx_queue_full;
1163 stats.rx_fill_ring_empty_descs =
1164 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1165 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1167 stats.rx_dropped += xs->rx_queue_full;
1169 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1170 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1171 mutex_unlock(&xs->mutex);
1173 if (copy_to_user(optval, &stats, stats_size))
1175 if (put_user(stats_size, optlen))
1180 case XDP_MMAP_OFFSETS:
1182 struct xdp_mmap_offsets off;
1183 struct xdp_mmap_offsets_v1 off_v1;
1184 bool flags_supported = true;
1187 if (len < sizeof(off_v1))
1189 else if (len < sizeof(off))
1190 flags_supported = false;
1192 if (flags_supported) {
1193 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1194 * except for the flags field added to the end.
1196 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1198 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1200 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1202 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1204 off.rx.flags = offsetof(struct xdp_rxtx_ring,
1206 off.tx.flags = offsetof(struct xdp_rxtx_ring,
1208 off.fr.flags = offsetof(struct xdp_umem_ring,
1210 off.cr.flags = offsetof(struct xdp_umem_ring,
1216 xsk_enter_rxtx_offsets(&off_v1.rx);
1217 xsk_enter_rxtx_offsets(&off_v1.tx);
1218 xsk_enter_umem_offsets(&off_v1.fr);
1219 xsk_enter_umem_offsets(&off_v1.cr);
1221 len = sizeof(off_v1);
1225 if (copy_to_user(optval, to_copy, len))
1227 if (put_user(len, optlen))
1234 struct xdp_options opts = {};
1236 if (len < sizeof(opts))
1239 mutex_lock(&xs->mutex);
1241 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1242 mutex_unlock(&xs->mutex);
1245 if (copy_to_user(optval, &opts, len))
1247 if (put_user(len, optlen))
1259 static int xsk_mmap(struct file *file, struct socket *sock,
1260 struct vm_area_struct *vma)
1262 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1263 unsigned long size = vma->vm_end - vma->vm_start;
1264 struct xdp_sock *xs = xdp_sk(sock->sk);
1265 struct xsk_queue *q = NULL;
1269 if (READ_ONCE(xs->state) != XSK_READY)
1272 if (offset == XDP_PGOFF_RX_RING) {
1273 q = READ_ONCE(xs->rx);
1274 } else if (offset == XDP_PGOFF_TX_RING) {
1275 q = READ_ONCE(xs->tx);
1277 /* Matches the smp_wmb() in XDP_UMEM_REG */
1279 if (offset == XDP_UMEM_PGOFF_FILL_RING)
1280 q = READ_ONCE(xs->fq_tmp);
1281 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1282 q = READ_ONCE(xs->cq_tmp);
1288 /* Matches the smp_wmb() in xsk_init_queue */
1290 qpg = virt_to_head_page(q->ring);
1291 if (size > page_size(qpg))
1294 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1295 return remap_pfn_range(vma, vma->vm_start, pfn,
1296 size, vma->vm_page_prot);
1299 static int xsk_notifier(struct notifier_block *this,
1300 unsigned long msg, void *ptr)
1302 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1303 struct net *net = dev_net(dev);
1307 case NETDEV_UNREGISTER:
1308 mutex_lock(&net->xdp.lock);
1309 sk_for_each(sk, &net->xdp.list) {
1310 struct xdp_sock *xs = xdp_sk(sk);
1312 mutex_lock(&xs->mutex);
1313 if (xs->dev == dev) {
1314 sk->sk_err = ENETDOWN;
1315 if (!sock_flag(sk, SOCK_DEAD))
1316 sk_error_report(sk);
1320 /* Clear device references. */
1321 xp_clear_dev(xs->pool);
1323 mutex_unlock(&xs->mutex);
1325 mutex_unlock(&net->xdp.lock);
1331 static struct proto xsk_proto = {
1333 .owner = THIS_MODULE,
1334 .obj_size = sizeof(struct xdp_sock),
1337 static const struct proto_ops xsk_proto_ops = {
1339 .owner = THIS_MODULE,
1340 .release = xsk_release,
1342 .connect = sock_no_connect,
1343 .socketpair = sock_no_socketpair,
1344 .accept = sock_no_accept,
1345 .getname = sock_no_getname,
1347 .ioctl = sock_no_ioctl,
1348 .listen = sock_no_listen,
1349 .shutdown = sock_no_shutdown,
1350 .setsockopt = xsk_setsockopt,
1351 .getsockopt = xsk_getsockopt,
1352 .sendmsg = xsk_sendmsg,
1353 .recvmsg = xsk_recvmsg,
1355 .sendpage = sock_no_sendpage,
1358 static void xsk_destruct(struct sock *sk)
1360 struct xdp_sock *xs = xdp_sk(sk);
1362 if (!sock_flag(sk, SOCK_DEAD))
1365 if (!xp_put_pool(xs->pool))
1366 xdp_put_umem(xs->umem, !xs->pool);
1368 sk_refcnt_debug_dec(sk);
1371 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1374 struct xdp_sock *xs;
1377 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1379 if (sock->type != SOCK_RAW)
1380 return -ESOCKTNOSUPPORT;
1383 return -EPROTONOSUPPORT;
1385 sock->state = SS_UNCONNECTED;
1387 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1391 sock->ops = &xsk_proto_ops;
1393 sock_init_data(sock, sk);
1395 sk->sk_family = PF_XDP;
1397 sk->sk_destruct = xsk_destruct;
1398 sk_refcnt_debug_inc(sk);
1400 sock_set_flag(sk, SOCK_RCU_FREE);
1403 xs->state = XSK_READY;
1404 mutex_init(&xs->mutex);
1405 spin_lock_init(&xs->rx_lock);
1407 INIT_LIST_HEAD(&xs->map_list);
1408 spin_lock_init(&xs->map_list_lock);
1410 mutex_lock(&net->xdp.lock);
1411 sk_add_node_rcu(sk, &net->xdp.list);
1412 mutex_unlock(&net->xdp.lock);
1415 sock_prot_inuse_add(net, &xsk_proto, 1);
1421 static const struct net_proto_family xsk_family_ops = {
1423 .create = xsk_create,
1424 .owner = THIS_MODULE,
1427 static struct notifier_block xsk_netdev_notifier = {
1428 .notifier_call = xsk_notifier,
1431 static int __net_init xsk_net_init(struct net *net)
1433 mutex_init(&net->xdp.lock);
1434 INIT_HLIST_HEAD(&net->xdp.list);
1438 static void __net_exit xsk_net_exit(struct net *net)
1440 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1443 static struct pernet_operations xsk_net_ops = {
1444 .init = xsk_net_init,
1445 .exit = xsk_net_exit,
1448 static int __init xsk_init(void)
1452 err = proto_register(&xsk_proto, 0 /* no slab */);
1456 err = sock_register(&xsk_family_ops);
1460 err = register_pernet_subsys(&xsk_net_ops);
1464 err = register_netdevice_notifier(&xsk_netdev_notifier);
1468 for_each_possible_cpu(cpu)
1469 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1473 unregister_pernet_subsys(&xsk_net_ops);
1475 sock_unregister(PF_XDP);
1477 proto_unregister(&xsk_proto);
1482 fs_initcall(xsk_init);