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 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
139 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
143 addr = xp_get_handle(xskb);
144 err = xskq_prod_reserve_desc(xs->rx, addr, len);
154 static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
156 void *from_buf, *to_buf;
159 if (unlikely(xdp_data_meta_unsupported(from))) {
160 from_buf = from->data;
164 from_buf = from->data_meta;
165 metalen = from->data - from->data_meta;
166 to_buf = to->data - metalen;
169 memcpy(to_buf, from_buf, len + metalen);
172 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
174 struct xdp_buff *xsk_xdp;
178 len = xdp->data_end - xdp->data;
179 if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
184 xsk_xdp = xsk_buff_alloc(xs->pool);
190 xsk_copy_xdp(xsk_xdp, xdp, len);
191 err = __xsk_rcv_zc(xs, xsk_xdp, len);
193 xsk_buff_free(xsk_xdp);
199 static bool xsk_tx_writeable(struct xdp_sock *xs)
201 if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
207 static bool xsk_is_bound(struct xdp_sock *xs)
209 if (READ_ONCE(xs->state) == XSK_BOUND) {
210 /* Matches smp_wmb() in bind(). */
217 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp)
219 if (!xsk_is_bound(xs))
222 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
225 sk_mark_napi_id_once_xdp(&xs->sk, xdp);
229 static void xsk_flush(struct xdp_sock *xs)
231 xskq_prod_submit(xs->rx);
232 __xskq_cons_release(xs->pool->fq);
233 sock_def_readable(&xs->sk);
236 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
240 spin_lock_bh(&xs->rx_lock);
241 err = xsk_rcv_check(xs, xdp);
243 err = __xsk_rcv(xs, xdp);
246 spin_unlock_bh(&xs->rx_lock);
250 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
255 err = xsk_rcv_check(xs, xdp);
259 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
260 len = xdp->data_end - xdp->data;
261 return __xsk_rcv_zc(xs, xdp, len);
264 err = __xsk_rcv(xs, xdp);
266 xdp_return_buff(xdp);
270 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
272 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
275 err = xsk_rcv(xs, xdp);
279 if (!xs->flush_node.prev)
280 list_add(&xs->flush_node, flush_list);
285 void __xsk_map_flush(void)
287 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
288 struct xdp_sock *xs, *tmp;
290 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
292 __list_del_clearprev(&xs->flush_node);
296 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
298 xskq_prod_submit_n(pool->cq, nb_entries);
300 EXPORT_SYMBOL(xsk_tx_completed);
302 void xsk_tx_release(struct xsk_buff_pool *pool)
307 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
308 __xskq_cons_release(xs->tx);
309 if (xsk_tx_writeable(xs))
310 xs->sk.sk_write_space(&xs->sk);
314 EXPORT_SYMBOL(xsk_tx_release);
316 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
321 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
322 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
323 xs->tx->queue_empty_descs++;
327 /* This is the backpressure mechanism for the Tx path.
328 * Reserve space in the completion queue and only proceed
329 * if there is space in it. This avoids having to implement
330 * any buffering in the Tx path.
332 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
335 xskq_cons_release(xs->tx);
344 EXPORT_SYMBOL(xsk_tx_peek_desc);
346 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
348 struct xdp_desc *descs = pool->tx_descs;
351 while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
354 xsk_tx_release(pool);
358 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max_entries)
364 if (!list_is_singular(&pool->xsk_tx_list)) {
365 /* Fallback to the non-batched version */
367 return xsk_tx_peek_release_fallback(pool, max_entries);
370 xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
376 nb_pkts = xskq_cons_peek_desc_batch(xs->tx, pool, max_entries);
378 xs->tx->queue_empty_descs++;
382 /* This is the backpressure mechanism for the Tx path. Try to
383 * reserve space in the completion queue for all packets, but
384 * if there are fewer slots available, just process that many
385 * packets. This avoids having to implement any buffering in
388 nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, pool->tx_descs, nb_pkts);
392 xskq_cons_release_n(xs->tx, nb_pkts);
393 __xskq_cons_release(xs->tx);
394 xs->sk.sk_write_space(&xs->sk);
400 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
402 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
404 struct net_device *dev = xs->dev;
406 return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
409 static void xsk_destruct_skb(struct sk_buff *skb)
411 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
412 struct xdp_sock *xs = xdp_sk(skb->sk);
415 spin_lock_irqsave(&xs->pool->cq_lock, flags);
416 xskq_prod_submit_addr(xs->pool->cq, addr);
417 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
422 static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
423 struct xdp_desc *desc)
425 struct xsk_buff_pool *pool = xs->pool;
426 u32 hr, len, ts, offset, copy, copied;
433 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
435 skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
439 skb_reserve(skb, hr);
443 ts = pool->unaligned ? len : pool->chunk_size;
445 buffer = xsk_buff_raw_get_data(pool, addr);
446 offset = offset_in_page(buffer);
447 addr = buffer - pool->addrs;
449 for (copied = 0, i = 0; copied < len; i++) {
450 page = pool->umem->pgs[addr >> PAGE_SHIFT];
453 copy = min_t(u32, PAGE_SIZE - offset, len - copied);
454 skb_fill_page_desc(skb, i, page, offset, copy);
462 skb->data_len += len;
465 refcount_add(ts, &xs->sk.sk_wmem_alloc);
470 static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
471 struct xdp_desc *desc)
473 struct net_device *dev = xs->dev;
476 if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
477 skb = xsk_build_skb_zerocopy(xs, desc);
485 hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
486 tr = dev->needed_tailroom;
489 skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
493 skb_reserve(skb, hr);
496 buffer = xsk_buff_raw_get_data(xs->pool, desc->addr);
497 err = skb_store_bits(skb, 0, buffer, len);
505 skb->priority = xs->sk.sk_priority;
506 skb->mark = xs->sk.sk_mark;
507 skb_shinfo(skb)->destructor_arg = (void *)(long)desc->addr;
508 skb->destructor = xsk_destruct_skb;
513 static int xsk_generic_xmit(struct sock *sk)
515 struct xdp_sock *xs = xdp_sk(sk);
516 u32 max_batch = TX_BATCH_SIZE;
517 bool sent_frame = false;
518 struct xdp_desc desc;
523 mutex_lock(&xs->mutex);
525 /* Since we dropped the RCU read lock, the socket state might have changed. */
526 if (unlikely(!xsk_is_bound(xs))) {
531 if (xs->queue_id >= xs->dev->real_num_tx_queues)
534 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
535 if (max_batch-- == 0) {
540 skb = xsk_build_skb(xs, &desc);
546 /* This is the backpressure mechanism for the Tx path.
547 * Reserve space in the completion queue and only proceed
548 * if there is space in it. This avoids having to implement
549 * any buffering in the Tx path.
551 spin_lock_irqsave(&xs->pool->cq_lock, flags);
552 if (xskq_prod_reserve(xs->pool->cq)) {
553 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
557 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
559 err = __dev_direct_xmit(skb, xs->queue_id);
560 if (err == NETDEV_TX_BUSY) {
561 /* Tell user-space to retry the send */
562 skb->destructor = sock_wfree;
563 spin_lock_irqsave(&xs->pool->cq_lock, flags);
564 xskq_prod_cancel(xs->pool->cq);
565 spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
566 /* Free skb without triggering the perf drop trace */
572 xskq_cons_release(xs->tx);
573 /* Ignore NET_XMIT_CN as packet might have been sent */
574 if (err == NET_XMIT_DROP) {
575 /* SKB completed but not sent */
583 xs->tx->queue_empty_descs++;
587 if (xsk_tx_writeable(xs))
588 sk->sk_write_space(sk);
590 mutex_unlock(&xs->mutex);
594 static int xsk_xmit(struct sock *sk)
596 struct xdp_sock *xs = xdp_sk(sk);
599 if (unlikely(!(xs->dev->flags & IFF_UP)))
601 if (unlikely(!xs->tx))
605 return xsk_wakeup(xs, XDP_WAKEUP_TX);
607 /* Drop the RCU lock since the SKB path might sleep. */
609 ret = xsk_generic_xmit(sk);
610 /* Reaquire RCU lock before going into common code. */
616 static bool xsk_no_wakeup(struct sock *sk)
618 #ifdef CONFIG_NET_RX_BUSY_POLL
619 /* Prefer busy-polling, skip the wakeup. */
620 return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
621 READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
627 static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
629 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
630 struct sock *sk = sock->sk;
631 struct xdp_sock *xs = xdp_sk(sk);
632 struct xsk_buff_pool *pool;
634 if (unlikely(!xsk_is_bound(xs)))
636 if (unlikely(need_wait))
639 if (sk_can_busy_loop(sk))
640 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
642 if (xs->zc && xsk_no_wakeup(sk))
646 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
651 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
656 ret = __xsk_sendmsg(sock, m, total_len);
662 static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
664 bool need_wait = !(flags & MSG_DONTWAIT);
665 struct sock *sk = sock->sk;
666 struct xdp_sock *xs = xdp_sk(sk);
668 if (unlikely(!xsk_is_bound(xs)))
670 if (unlikely(!(xs->dev->flags & IFF_UP)))
672 if (unlikely(!xs->rx))
674 if (unlikely(need_wait))
677 if (sk_can_busy_loop(sk))
678 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
680 if (xsk_no_wakeup(sk))
683 if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
684 return xsk_wakeup(xs, XDP_WAKEUP_RX);
688 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
693 ret = __xsk_recvmsg(sock, m, len, flags);
699 static __poll_t xsk_poll(struct file *file, struct socket *sock,
700 struct poll_table_struct *wait)
703 struct sock *sk = sock->sk;
704 struct xdp_sock *xs = xdp_sk(sk);
705 struct xsk_buff_pool *pool;
707 sock_poll_wait(file, sock, wait);
710 if (unlikely(!xsk_is_bound(xs))) {
717 if (pool->cached_need_wakeup) {
719 xsk_wakeup(xs, pool->cached_need_wakeup);
721 /* Poll needs to drive Tx also in copy mode */
725 if (xs->rx && !xskq_prod_is_empty(xs->rx))
726 mask |= EPOLLIN | EPOLLRDNORM;
727 if (xs->tx && xsk_tx_writeable(xs))
728 mask |= EPOLLOUT | EPOLLWRNORM;
734 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
739 if (entries == 0 || *queue || !is_power_of_2(entries))
742 q = xskq_create(entries, umem_queue);
746 /* Make sure queue is ready before it can be seen by others */
748 WRITE_ONCE(*queue, q);
752 static void xsk_unbind_dev(struct xdp_sock *xs)
754 struct net_device *dev = xs->dev;
756 if (xs->state != XSK_BOUND)
758 WRITE_ONCE(xs->state, XSK_UNBOUND);
760 /* Wait for driver to stop using the xdp socket. */
761 xp_del_xsk(xs->pool, xs);
766 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
767 struct xdp_sock __rcu ***map_entry)
769 struct xsk_map *map = NULL;
770 struct xsk_map_node *node;
774 spin_lock_bh(&xs->map_list_lock);
775 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
778 bpf_map_inc(&node->map->map);
780 *map_entry = node->map_entry;
782 spin_unlock_bh(&xs->map_list_lock);
786 static void xsk_delete_from_maps(struct xdp_sock *xs)
788 /* This function removes the current XDP socket from all the
789 * maps it resides in. We need to take extra care here, due to
790 * the two locks involved. Each map has a lock synchronizing
791 * updates to the entries, and each socket has a lock that
792 * synchronizes access to the list of maps (map_list). For
793 * deadlock avoidance the locks need to be taken in the order
794 * "map lock"->"socket map list lock". We start off by
795 * accessing the socket map list, and take a reference to the
796 * map to guarantee existence between the
797 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
798 * calls. Then we ask the map to remove the socket, which
799 * tries to remove the socket from the map. Note that there
800 * might be updates to the map between
801 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
803 struct xdp_sock __rcu **map_entry = NULL;
806 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
807 xsk_map_try_sock_delete(map, xs, map_entry);
808 bpf_map_put(&map->map);
812 static int xsk_release(struct socket *sock)
814 struct sock *sk = sock->sk;
815 struct xdp_sock *xs = xdp_sk(sk);
823 mutex_lock(&net->xdp.lock);
824 sk_del_node_init_rcu(sk);
825 mutex_unlock(&net->xdp.lock);
827 sock_prot_inuse_add(net, sk->sk_prot, -1);
829 xsk_delete_from_maps(xs);
830 mutex_lock(&xs->mutex);
832 mutex_unlock(&xs->mutex);
834 xskq_destroy(xs->rx);
835 xskq_destroy(xs->tx);
836 xskq_destroy(xs->fq_tmp);
837 xskq_destroy(xs->cq_tmp);
842 sk_refcnt_debug_release(sk);
848 static struct socket *xsk_lookup_xsk_from_fd(int fd)
853 sock = sockfd_lookup(fd, &err);
855 return ERR_PTR(-ENOTSOCK);
857 if (sock->sk->sk_family != PF_XDP) {
859 return ERR_PTR(-ENOPROTOOPT);
865 static bool xsk_validate_queues(struct xdp_sock *xs)
867 return xs->fq_tmp && xs->cq_tmp;
870 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
872 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
873 struct sock *sk = sock->sk;
874 struct xdp_sock *xs = xdp_sk(sk);
875 struct net_device *dev;
879 if (addr_len < sizeof(struct sockaddr_xdp))
881 if (sxdp->sxdp_family != AF_XDP)
884 flags = sxdp->sxdp_flags;
885 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
886 XDP_USE_NEED_WAKEUP))
890 mutex_lock(&xs->mutex);
891 if (xs->state != XSK_READY) {
896 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
902 if (!xs->rx && !xs->tx) {
907 qid = sxdp->sxdp_queue_id;
909 if (flags & XDP_SHARED_UMEM) {
910 struct xdp_sock *umem_xs;
913 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
914 (flags & XDP_USE_NEED_WAKEUP)) {
915 /* Cannot specify flags for shared sockets. */
921 /* We have already our own. */
926 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
932 umem_xs = xdp_sk(sock->sk);
933 if (!xsk_is_bound(umem_xs)) {
939 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
940 /* Share the umem with another socket on another qid
943 xs->pool = xp_create_and_assign_umem(xs,
951 err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
954 xp_destroy(xs->pool);
960 /* Share the buffer pool with the other socket. */
961 if (xs->fq_tmp || xs->cq_tmp) {
962 /* Do not allow setting your own fq or cq. */
968 xp_get_pool(umem_xs->pool);
969 xs->pool = umem_xs->pool;
971 /* If underlying shared umem was created without Tx
972 * ring, allocate Tx descs array that Tx batching API
975 if (xs->tx && !xs->pool->tx_descs) {
976 err = xp_alloc_tx_descs(xs->pool, xs);
978 xp_put_pool(xs->pool);
985 xdp_get_umem(umem_xs->umem);
986 WRITE_ONCE(xs->umem, umem_xs->umem);
988 } else if (!xs->umem || !xsk_validate_queues(xs)) {
992 /* This xsk has its own umem. */
993 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
999 err = xp_assign_dev(xs->pool, dev, qid, flags);
1001 xp_destroy(xs->pool);
1007 /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
1012 xs->zc = xs->umem->zc;
1014 xp_add_xsk(xs->pool, xs);
1020 /* Matches smp_rmb() in bind() for shared umem
1021 * sockets, and xsk_is_bound().
1024 WRITE_ONCE(xs->state, XSK_BOUND);
1027 mutex_unlock(&xs->mutex);
1032 struct xdp_umem_reg_v1 {
1033 __u64 addr; /* Start of packet data area */
1034 __u64 len; /* Length of packet data area */
1039 static int xsk_setsockopt(struct socket *sock, int level, int optname,
1040 sockptr_t optval, unsigned int optlen)
1042 struct sock *sk = sock->sk;
1043 struct xdp_sock *xs = xdp_sk(sk);
1046 if (level != SOL_XDP)
1047 return -ENOPROTOOPT;
1053 struct xsk_queue **q;
1056 if (optlen < sizeof(entries))
1058 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1061 mutex_lock(&xs->mutex);
1062 if (xs->state != XSK_READY) {
1063 mutex_unlock(&xs->mutex);
1066 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
1067 err = xsk_init_queue(entries, q, false);
1068 if (!err && optname == XDP_TX_RING)
1069 /* Tx needs to be explicitly woken up the first time */
1070 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
1071 mutex_unlock(&xs->mutex);
1076 size_t mr_size = sizeof(struct xdp_umem_reg);
1077 struct xdp_umem_reg mr = {};
1078 struct xdp_umem *umem;
1080 if (optlen < sizeof(struct xdp_umem_reg_v1))
1082 else if (optlen < sizeof(mr))
1083 mr_size = sizeof(struct xdp_umem_reg_v1);
1085 if (copy_from_sockptr(&mr, optval, mr_size))
1088 mutex_lock(&xs->mutex);
1089 if (xs->state != XSK_READY || xs->umem) {
1090 mutex_unlock(&xs->mutex);
1094 umem = xdp_umem_create(&mr);
1096 mutex_unlock(&xs->mutex);
1097 return PTR_ERR(umem);
1100 /* Make sure umem is ready before it can be seen by others */
1102 WRITE_ONCE(xs->umem, umem);
1103 mutex_unlock(&xs->mutex);
1106 case XDP_UMEM_FILL_RING:
1107 case XDP_UMEM_COMPLETION_RING:
1109 struct xsk_queue **q;
1112 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1115 mutex_lock(&xs->mutex);
1116 if (xs->state != XSK_READY) {
1117 mutex_unlock(&xs->mutex);
1121 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1123 err = xsk_init_queue(entries, q, true);
1124 mutex_unlock(&xs->mutex);
1131 return -ENOPROTOOPT;
1134 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1136 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1137 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1138 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1141 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1143 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1144 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1145 ring->desc = offsetof(struct xdp_umem_ring, desc);
1148 struct xdp_statistics_v1 {
1150 __u64 rx_invalid_descs;
1151 __u64 tx_invalid_descs;
1154 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1155 char __user *optval, int __user *optlen)
1157 struct sock *sk = sock->sk;
1158 struct xdp_sock *xs = xdp_sk(sk);
1161 if (level != SOL_XDP)
1162 return -ENOPROTOOPT;
1164 if (get_user(len, optlen))
1170 case XDP_STATISTICS:
1172 struct xdp_statistics stats = {};
1173 bool extra_stats = true;
1176 if (len < sizeof(struct xdp_statistics_v1)) {
1178 } else if (len < sizeof(stats)) {
1179 extra_stats = false;
1180 stats_size = sizeof(struct xdp_statistics_v1);
1182 stats_size = sizeof(stats);
1185 mutex_lock(&xs->mutex);
1186 stats.rx_dropped = xs->rx_dropped;
1188 stats.rx_ring_full = xs->rx_queue_full;
1189 stats.rx_fill_ring_empty_descs =
1190 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1191 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1193 stats.rx_dropped += xs->rx_queue_full;
1195 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1196 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1197 mutex_unlock(&xs->mutex);
1199 if (copy_to_user(optval, &stats, stats_size))
1201 if (put_user(stats_size, optlen))
1206 case XDP_MMAP_OFFSETS:
1208 struct xdp_mmap_offsets off;
1209 struct xdp_mmap_offsets_v1 off_v1;
1210 bool flags_supported = true;
1213 if (len < sizeof(off_v1))
1215 else if (len < sizeof(off))
1216 flags_supported = false;
1218 if (flags_supported) {
1219 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1220 * except for the flags field added to the end.
1222 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1224 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1226 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1228 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1230 off.rx.flags = offsetof(struct xdp_rxtx_ring,
1232 off.tx.flags = offsetof(struct xdp_rxtx_ring,
1234 off.fr.flags = offsetof(struct xdp_umem_ring,
1236 off.cr.flags = offsetof(struct xdp_umem_ring,
1242 xsk_enter_rxtx_offsets(&off_v1.rx);
1243 xsk_enter_rxtx_offsets(&off_v1.tx);
1244 xsk_enter_umem_offsets(&off_v1.fr);
1245 xsk_enter_umem_offsets(&off_v1.cr);
1247 len = sizeof(off_v1);
1251 if (copy_to_user(optval, to_copy, len))
1253 if (put_user(len, optlen))
1260 struct xdp_options opts = {};
1262 if (len < sizeof(opts))
1265 mutex_lock(&xs->mutex);
1267 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1268 mutex_unlock(&xs->mutex);
1271 if (copy_to_user(optval, &opts, len))
1273 if (put_user(len, optlen))
1285 static int xsk_mmap(struct file *file, struct socket *sock,
1286 struct vm_area_struct *vma)
1288 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1289 unsigned long size = vma->vm_end - vma->vm_start;
1290 struct xdp_sock *xs = xdp_sk(sock->sk);
1291 struct xsk_queue *q = NULL;
1295 if (READ_ONCE(xs->state) != XSK_READY)
1298 if (offset == XDP_PGOFF_RX_RING) {
1299 q = READ_ONCE(xs->rx);
1300 } else if (offset == XDP_PGOFF_TX_RING) {
1301 q = READ_ONCE(xs->tx);
1303 /* Matches the smp_wmb() in XDP_UMEM_REG */
1305 if (offset == XDP_UMEM_PGOFF_FILL_RING)
1306 q = READ_ONCE(xs->fq_tmp);
1307 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1308 q = READ_ONCE(xs->cq_tmp);
1314 /* Matches the smp_wmb() in xsk_init_queue */
1316 qpg = virt_to_head_page(q->ring);
1317 if (size > page_size(qpg))
1320 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1321 return remap_pfn_range(vma, vma->vm_start, pfn,
1322 size, vma->vm_page_prot);
1325 static int xsk_notifier(struct notifier_block *this,
1326 unsigned long msg, void *ptr)
1328 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1329 struct net *net = dev_net(dev);
1333 case NETDEV_UNREGISTER:
1334 mutex_lock(&net->xdp.lock);
1335 sk_for_each(sk, &net->xdp.list) {
1336 struct xdp_sock *xs = xdp_sk(sk);
1338 mutex_lock(&xs->mutex);
1339 if (xs->dev == dev) {
1340 sk->sk_err = ENETDOWN;
1341 if (!sock_flag(sk, SOCK_DEAD))
1342 sk_error_report(sk);
1346 /* Clear device references. */
1347 xp_clear_dev(xs->pool);
1349 mutex_unlock(&xs->mutex);
1351 mutex_unlock(&net->xdp.lock);
1357 static struct proto xsk_proto = {
1359 .owner = THIS_MODULE,
1360 .obj_size = sizeof(struct xdp_sock),
1363 static const struct proto_ops xsk_proto_ops = {
1365 .owner = THIS_MODULE,
1366 .release = xsk_release,
1368 .connect = sock_no_connect,
1369 .socketpair = sock_no_socketpair,
1370 .accept = sock_no_accept,
1371 .getname = sock_no_getname,
1373 .ioctl = sock_no_ioctl,
1374 .listen = sock_no_listen,
1375 .shutdown = sock_no_shutdown,
1376 .setsockopt = xsk_setsockopt,
1377 .getsockopt = xsk_getsockopt,
1378 .sendmsg = xsk_sendmsg,
1379 .recvmsg = xsk_recvmsg,
1381 .sendpage = sock_no_sendpage,
1384 static void xsk_destruct(struct sock *sk)
1386 struct xdp_sock *xs = xdp_sk(sk);
1388 if (!sock_flag(sk, SOCK_DEAD))
1391 if (!xp_put_pool(xs->pool))
1392 xdp_put_umem(xs->umem, !xs->pool);
1394 sk_refcnt_debug_dec(sk);
1397 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1400 struct xdp_sock *xs;
1403 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1405 if (sock->type != SOCK_RAW)
1406 return -ESOCKTNOSUPPORT;
1409 return -EPROTONOSUPPORT;
1411 sock->state = SS_UNCONNECTED;
1413 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1417 sock->ops = &xsk_proto_ops;
1419 sock_init_data(sock, sk);
1421 sk->sk_family = PF_XDP;
1423 sk->sk_destruct = xsk_destruct;
1424 sk_refcnt_debug_inc(sk);
1426 sock_set_flag(sk, SOCK_RCU_FREE);
1429 xs->state = XSK_READY;
1430 mutex_init(&xs->mutex);
1431 spin_lock_init(&xs->rx_lock);
1433 INIT_LIST_HEAD(&xs->map_list);
1434 spin_lock_init(&xs->map_list_lock);
1436 mutex_lock(&net->xdp.lock);
1437 sk_add_node_rcu(sk, &net->xdp.list);
1438 mutex_unlock(&net->xdp.lock);
1440 sock_prot_inuse_add(net, &xsk_proto, 1);
1445 static const struct net_proto_family xsk_family_ops = {
1447 .create = xsk_create,
1448 .owner = THIS_MODULE,
1451 static struct notifier_block xsk_netdev_notifier = {
1452 .notifier_call = xsk_notifier,
1455 static int __net_init xsk_net_init(struct net *net)
1457 mutex_init(&net->xdp.lock);
1458 INIT_HLIST_HEAD(&net->xdp.list);
1462 static void __net_exit xsk_net_exit(struct net *net)
1464 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1467 static struct pernet_operations xsk_net_ops = {
1468 .init = xsk_net_init,
1469 .exit = xsk_net_exit,
1472 static int __init xsk_init(void)
1476 err = proto_register(&xsk_proto, 0 /* no slab */);
1480 err = sock_register(&xsk_family_ops);
1484 err = register_pernet_subsys(&xsk_net_ops);
1488 err = register_netdevice_notifier(&xsk_netdev_notifier);
1492 for_each_possible_cpu(cpu)
1493 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1497 unregister_pernet_subsys(&xsk_net_ops);
1499 sock_unregister(PF_XDP);
1501 proto_unregister(&xsk_proto);
1506 fs_initcall(xsk_init);