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.
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.h>
28 #include "xsk_queue.h"
32 #define TX_BATCH_SIZE 16
34 bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
36 return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
37 READ_ONCE(xs->umem->fq);
40 bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt)
42 return xskq_has_addrs(umem->fq, cnt);
44 EXPORT_SYMBOL(xsk_umem_has_addrs);
46 u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
48 return xskq_peek_addr(umem->fq, addr, umem);
50 EXPORT_SYMBOL(xsk_umem_peek_addr);
52 void xsk_umem_discard_addr(struct xdp_umem *umem)
54 xskq_discard_addr(umem->fq);
56 EXPORT_SYMBOL(xsk_umem_discard_addr);
58 void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
60 if (umem->need_wakeup & XDP_WAKEUP_RX)
63 umem->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
64 umem->need_wakeup |= XDP_WAKEUP_RX;
66 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
68 void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
72 if (umem->need_wakeup & XDP_WAKEUP_TX)
76 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
77 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
81 umem->need_wakeup |= XDP_WAKEUP_TX;
83 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
85 void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
87 if (!(umem->need_wakeup & XDP_WAKEUP_RX))
90 umem->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
91 umem->need_wakeup &= ~XDP_WAKEUP_RX;
93 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
95 void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
99 if (!(umem->need_wakeup & XDP_WAKEUP_TX))
103 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
104 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
108 umem->need_wakeup &= ~XDP_WAKEUP_TX;
110 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
112 bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
114 return umem->flags & XDP_UMEM_USES_NEED_WAKEUP;
116 EXPORT_SYMBOL(xsk_umem_uses_need_wakeup);
118 /* If a buffer crosses a page boundary, we need to do 2 memcpy's, one for
119 * each page. This is only required in copy mode.
121 static void __xsk_rcv_memcpy(struct xdp_umem *umem, u64 addr, void *from_buf,
122 u32 len, u32 metalen)
124 void *to_buf = xdp_umem_get_data(umem, addr);
126 addr = xsk_umem_add_offset_to_addr(addr);
127 if (xskq_crosses_non_contig_pg(umem, addr, len + metalen)) {
128 void *next_pg_addr = umem->pages[(addr >> PAGE_SHIFT) + 1].addr;
129 u64 page_start = addr & ~(PAGE_SIZE - 1);
130 u64 first_len = PAGE_SIZE - (addr - page_start);
132 memcpy(to_buf, from_buf, first_len + metalen);
133 memcpy(next_pg_addr, from_buf + first_len, len - first_len);
138 memcpy(to_buf, from_buf, len + metalen);
141 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
143 u64 offset = xs->umem->headroom;
144 u64 addr, memcpy_addr;
149 if (!xskq_peek_addr(xs->umem->fq, &addr, xs->umem) ||
150 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
155 if (unlikely(xdp_data_meta_unsupported(xdp))) {
156 from_buf = xdp->data;
159 from_buf = xdp->data_meta;
160 metalen = xdp->data - xdp->data_meta;
163 memcpy_addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
164 __xsk_rcv_memcpy(xs->umem, memcpy_addr, from_buf, len, metalen);
167 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
168 err = xskq_produce_batch_desc(xs->rx, addr, len);
170 xskq_discard_addr(xs->umem->fq);
171 xdp_return_buff(xdp);
179 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
181 int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);
189 static bool xsk_is_bound(struct xdp_sock *xs)
191 if (READ_ONCE(xs->state) == XSK_BOUND) {
192 /* Matches smp_wmb() in bind(). */
199 int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
203 if (!xsk_is_bound(xs))
206 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
209 len = xdp->data_end - xdp->data;
211 return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ?
212 __xsk_rcv_zc(xs, xdp, len) : __xsk_rcv(xs, xdp, len);
215 void xsk_flush(struct xdp_sock *xs)
217 xskq_produce_flush_desc(xs->rx);
218 xs->sk.sk_data_ready(&xs->sk);
221 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
223 u32 metalen = xdp->data - xdp->data_meta;
224 u32 len = xdp->data_end - xdp->data;
225 u64 offset = xs->umem->headroom;
230 spin_lock_bh(&xs->rx_lock);
232 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) {
237 if (!xskq_peek_addr(xs->umem->fq, &addr, xs->umem) ||
238 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
243 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
244 buffer = xdp_umem_get_data(xs->umem, addr);
245 memcpy(buffer, xdp->data_meta, len + metalen);
247 addr = xsk_umem_adjust_offset(xs->umem, addr, metalen);
248 err = xskq_produce_batch_desc(xs->rx, addr, len);
252 xskq_discard_addr(xs->umem->fq);
253 xskq_produce_flush_desc(xs->rx);
255 spin_unlock_bh(&xs->rx_lock);
257 xs->sk.sk_data_ready(&xs->sk);
263 spin_unlock_bh(&xs->rx_lock);
267 void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
269 xskq_produce_flush_addr_n(umem->cq, nb_entries);
271 EXPORT_SYMBOL(xsk_umem_complete_tx);
273 void xsk_umem_consume_tx_done(struct xdp_umem *umem)
278 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
279 xs->sk.sk_write_space(&xs->sk);
283 EXPORT_SYMBOL(xsk_umem_consume_tx_done);
285 bool xsk_umem_consume_tx(struct xdp_umem *umem, struct xdp_desc *desc)
290 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
291 if (!xskq_peek_desc(xs->tx, desc, umem))
294 if (xskq_produce_addr_lazy(umem->cq, desc->addr))
297 xskq_discard_desc(xs->tx);
306 EXPORT_SYMBOL(xsk_umem_consume_tx);
308 static int xsk_zc_xmit(struct xdp_sock *xs)
310 struct net_device *dev = xs->dev;
312 return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id,
316 static void xsk_destruct_skb(struct sk_buff *skb)
318 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
319 struct xdp_sock *xs = xdp_sk(skb->sk);
322 spin_lock_irqsave(&xs->tx_completion_lock, flags);
323 WARN_ON_ONCE(xskq_produce_addr(xs->umem->cq, addr));
324 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
329 static int xsk_generic_xmit(struct sock *sk)
331 struct xdp_sock *xs = xdp_sk(sk);
332 u32 max_batch = TX_BATCH_SIZE;
333 bool sent_frame = false;
334 struct xdp_desc desc;
338 mutex_lock(&xs->mutex);
340 if (xs->queue_id >= xs->dev->real_num_tx_queues)
343 while (xskq_peek_desc(xs->tx, &desc, xs->umem)) {
348 if (max_batch-- == 0) {
354 skb = sock_alloc_send_skb(sk, len, 1, &err);
355 if (unlikely(!skb)) {
362 buffer = xdp_umem_get_data(xs->umem, addr);
363 err = skb_store_bits(skb, 0, buffer, len);
364 if (unlikely(err) || xskq_reserve_addr(xs->umem->cq)) {
370 skb->priority = sk->sk_priority;
371 skb->mark = sk->sk_mark;
372 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
373 skb->destructor = xsk_destruct_skb;
375 err = dev_direct_xmit(skb, xs->queue_id);
376 xskq_discard_desc(xs->tx);
377 /* Ignore NET_XMIT_CN as packet might have been sent */
378 if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
379 /* SKB completed but not sent */
389 sk->sk_write_space(sk);
391 mutex_unlock(&xs->mutex);
395 static int __xsk_sendmsg(struct sock *sk)
397 struct xdp_sock *xs = xdp_sk(sk);
399 if (unlikely(!(xs->dev->flags & IFF_UP)))
401 if (unlikely(!xs->tx))
404 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
407 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
409 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
410 struct sock *sk = sock->sk;
411 struct xdp_sock *xs = xdp_sk(sk);
413 if (unlikely(!xsk_is_bound(xs)))
415 if (unlikely(need_wait))
418 return __xsk_sendmsg(sk);
421 static unsigned int xsk_poll(struct file *file, struct socket *sock,
422 struct poll_table_struct *wait)
424 unsigned int mask = datagram_poll(file, sock, wait);
425 struct sock *sk = sock->sk;
426 struct xdp_sock *xs = xdp_sk(sk);
427 struct net_device *dev;
428 struct xdp_umem *umem;
430 if (unlikely(!xsk_is_bound(xs)))
436 if (umem->need_wakeup) {
437 if (dev->netdev_ops->ndo_xsk_wakeup)
438 dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id,
441 /* Poll needs to drive Tx also in copy mode */
445 if (xs->rx && !xskq_empty_desc(xs->rx))
446 mask |= POLLIN | POLLRDNORM;
447 if (xs->tx && !xskq_full_desc(xs->tx))
448 mask |= POLLOUT | POLLWRNORM;
453 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
458 if (entries == 0 || *queue || !is_power_of_2(entries))
461 q = xskq_create(entries, umem_queue);
465 /* Make sure queue is ready before it can be seen by others */
467 WRITE_ONCE(*queue, q);
471 static void xsk_unbind_dev(struct xdp_sock *xs)
473 struct net_device *dev = xs->dev;
475 if (xs->state != XSK_BOUND)
477 WRITE_ONCE(xs->state, XSK_UNBOUND);
479 /* Wait for driver to stop using the xdp socket. */
480 xdp_del_sk_umem(xs->umem, xs);
486 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
487 struct xdp_sock ***map_entry)
489 struct xsk_map *map = NULL;
490 struct xsk_map_node *node;
494 spin_lock_bh(&xs->map_list_lock);
495 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
498 WARN_ON(xsk_map_inc(node->map));
500 *map_entry = node->map_entry;
502 spin_unlock_bh(&xs->map_list_lock);
506 static void xsk_delete_from_maps(struct xdp_sock *xs)
508 /* This function removes the current XDP socket from all the
509 * maps it resides in. We need to take extra care here, due to
510 * the two locks involved. Each map has a lock synchronizing
511 * updates to the entries, and each socket has a lock that
512 * synchronizes access to the list of maps (map_list). For
513 * deadlock avoidance the locks need to be taken in the order
514 * "map lock"->"socket map list lock". We start off by
515 * accessing the socket map list, and take a reference to the
516 * map to guarantee existence between the
517 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
518 * calls. Then we ask the map to remove the socket, which
519 * tries to remove the socket from the map. Note that there
520 * might be updates to the map between
521 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
523 struct xdp_sock **map_entry = NULL;
526 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
527 xsk_map_try_sock_delete(map, xs, map_entry);
532 static int xsk_release(struct socket *sock)
534 struct sock *sk = sock->sk;
535 struct xdp_sock *xs = xdp_sk(sk);
543 mutex_lock(&net->xdp.lock);
544 sk_del_node_init_rcu(sk);
545 mutex_unlock(&net->xdp.lock);
548 sock_prot_inuse_add(net, sk->sk_prot, -1);
551 xsk_delete_from_maps(xs);
552 mutex_lock(&xs->mutex);
554 mutex_unlock(&xs->mutex);
556 xskq_destroy(xs->rx);
557 xskq_destroy(xs->tx);
562 sk_refcnt_debug_release(sk);
568 static struct socket *xsk_lookup_xsk_from_fd(int fd)
573 sock = sockfd_lookup(fd, &err);
575 return ERR_PTR(-ENOTSOCK);
577 if (sock->sk->sk_family != PF_XDP) {
579 return ERR_PTR(-ENOPROTOOPT);
585 /* Check if umem pages are contiguous.
586 * If zero-copy mode, use the DMA address to do the page contiguity check
587 * For all other modes we use addr (kernel virtual address)
588 * Store the result in the low bits of addr.
590 static void xsk_check_page_contiguity(struct xdp_umem *umem, u32 flags)
592 struct xdp_umem_page *pgs = umem->pages;
595 for (i = 0; i < umem->npgs - 1; i++) {
596 is_contig = (flags & XDP_ZEROCOPY) ?
597 (pgs[i].dma + PAGE_SIZE == pgs[i + 1].dma) :
598 (pgs[i].addr + PAGE_SIZE == pgs[i + 1].addr);
599 pgs[i].addr += is_contig << XSK_NEXT_PG_CONTIG_SHIFT;
603 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
605 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
606 struct sock *sk = sock->sk;
607 struct xdp_sock *xs = xdp_sk(sk);
608 struct net_device *dev;
612 if (addr_len < sizeof(struct sockaddr_xdp))
614 if (sxdp->sxdp_family != AF_XDP)
617 flags = sxdp->sxdp_flags;
618 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
619 XDP_USE_NEED_WAKEUP))
623 mutex_lock(&xs->mutex);
624 if (xs->state != XSK_READY) {
629 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
635 if (!xs->rx && !xs->tx) {
640 qid = sxdp->sxdp_queue_id;
642 if (flags & XDP_SHARED_UMEM) {
643 struct xdp_sock *umem_xs;
646 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
647 (flags & XDP_USE_NEED_WAKEUP)) {
648 /* Cannot specify flags for shared sockets. */
654 /* We have already our own. */
659 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
665 umem_xs = xdp_sk(sock->sk);
666 if (!xsk_is_bound(umem_xs)) {
671 if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
677 xdp_get_umem(umem_xs->umem);
678 WRITE_ONCE(xs->umem, umem_xs->umem);
680 } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
684 /* This xsk has its own umem. */
685 xskq_set_umem(xs->umem->fq, xs->umem->size,
686 xs->umem->chunk_mask);
687 xskq_set_umem(xs->umem->cq, xs->umem->size,
688 xs->umem->chunk_mask);
690 err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
694 xsk_check_page_contiguity(xs->umem, flags);
698 xs->zc = xs->umem->zc;
700 xskq_set_umem(xs->rx, xs->umem->size, xs->umem->chunk_mask);
701 xskq_set_umem(xs->tx, xs->umem->size, xs->umem->chunk_mask);
702 xdp_add_sk_umem(xs->umem, xs);
708 /* Matches smp_rmb() in bind() for shared umem
709 * sockets, and xsk_is_bound().
712 WRITE_ONCE(xs->state, XSK_BOUND);
715 mutex_unlock(&xs->mutex);
720 struct xdp_umem_reg_v1 {
721 __u64 addr; /* Start of packet data area */
722 __u64 len; /* Length of packet data area */
727 static int xsk_setsockopt(struct socket *sock, int level, int optname,
728 char __user *optval, unsigned int optlen)
730 struct sock *sk = sock->sk;
731 struct xdp_sock *xs = xdp_sk(sk);
734 if (level != SOL_XDP)
741 struct xsk_queue **q;
744 if (optlen < sizeof(entries))
746 if (copy_from_user(&entries, optval, sizeof(entries)))
749 mutex_lock(&xs->mutex);
750 if (xs->state != XSK_READY) {
751 mutex_unlock(&xs->mutex);
754 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
755 err = xsk_init_queue(entries, q, false);
756 if (!err && optname == XDP_TX_RING)
757 /* Tx needs to be explicitly woken up the first time */
758 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
759 mutex_unlock(&xs->mutex);
764 size_t mr_size = sizeof(struct xdp_umem_reg);
765 struct xdp_umem_reg mr = {};
766 struct xdp_umem *umem;
768 if (optlen < sizeof(struct xdp_umem_reg_v1))
770 else if (optlen < sizeof(mr))
771 mr_size = sizeof(struct xdp_umem_reg_v1);
773 if (copy_from_user(&mr, optval, mr_size))
776 mutex_lock(&xs->mutex);
777 if (xs->state != XSK_READY || xs->umem) {
778 mutex_unlock(&xs->mutex);
782 umem = xdp_umem_create(&mr);
784 mutex_unlock(&xs->mutex);
785 return PTR_ERR(umem);
788 /* Make sure umem is ready before it can be seen by others */
790 WRITE_ONCE(xs->umem, umem);
791 mutex_unlock(&xs->mutex);
794 case XDP_UMEM_FILL_RING:
795 case XDP_UMEM_COMPLETION_RING:
797 struct xsk_queue **q;
800 if (copy_from_user(&entries, optval, sizeof(entries)))
803 mutex_lock(&xs->mutex);
804 if (xs->state != XSK_READY) {
805 mutex_unlock(&xs->mutex);
809 mutex_unlock(&xs->mutex);
813 q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
815 err = xsk_init_queue(entries, q, true);
816 mutex_unlock(&xs->mutex);
826 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
828 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
829 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
830 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
833 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
835 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
836 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
837 ring->desc = offsetof(struct xdp_umem_ring, desc);
840 static int xsk_getsockopt(struct socket *sock, int level, int optname,
841 char __user *optval, int __user *optlen)
843 struct sock *sk = sock->sk;
844 struct xdp_sock *xs = xdp_sk(sk);
847 if (level != SOL_XDP)
850 if (get_user(len, optlen))
858 struct xdp_statistics stats;
860 if (len < sizeof(stats))
863 mutex_lock(&xs->mutex);
864 stats.rx_dropped = xs->rx_dropped;
865 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
866 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
867 mutex_unlock(&xs->mutex);
869 if (copy_to_user(optval, &stats, sizeof(stats)))
871 if (put_user(sizeof(stats), optlen))
876 case XDP_MMAP_OFFSETS:
878 struct xdp_mmap_offsets off;
879 struct xdp_mmap_offsets_v1 off_v1;
880 bool flags_supported = true;
883 if (len < sizeof(off_v1))
885 else if (len < sizeof(off))
886 flags_supported = false;
888 if (flags_supported) {
889 /* xdp_ring_offset is identical to xdp_ring_offset_v1
890 * except for the flags field added to the end.
892 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
894 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
896 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
898 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
900 off.rx.flags = offsetof(struct xdp_rxtx_ring,
902 off.tx.flags = offsetof(struct xdp_rxtx_ring,
904 off.fr.flags = offsetof(struct xdp_umem_ring,
906 off.cr.flags = offsetof(struct xdp_umem_ring,
912 xsk_enter_rxtx_offsets(&off_v1.rx);
913 xsk_enter_rxtx_offsets(&off_v1.tx);
914 xsk_enter_umem_offsets(&off_v1.fr);
915 xsk_enter_umem_offsets(&off_v1.cr);
917 len = sizeof(off_v1);
921 if (copy_to_user(optval, to_copy, len))
923 if (put_user(len, optlen))
930 struct xdp_options opts = {};
932 if (len < sizeof(opts))
935 mutex_lock(&xs->mutex);
937 opts.flags |= XDP_OPTIONS_ZEROCOPY;
938 mutex_unlock(&xs->mutex);
941 if (copy_to_user(optval, &opts, len))
943 if (put_user(len, optlen))
955 static int xsk_mmap(struct file *file, struct socket *sock,
956 struct vm_area_struct *vma)
958 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
959 unsigned long size = vma->vm_end - vma->vm_start;
960 struct xdp_sock *xs = xdp_sk(sock->sk);
961 struct xsk_queue *q = NULL;
962 struct xdp_umem *umem;
966 if (READ_ONCE(xs->state) != XSK_READY)
969 if (offset == XDP_PGOFF_RX_RING) {
970 q = READ_ONCE(xs->rx);
971 } else if (offset == XDP_PGOFF_TX_RING) {
972 q = READ_ONCE(xs->tx);
974 umem = READ_ONCE(xs->umem);
978 /* Matches the smp_wmb() in XDP_UMEM_REG */
980 if (offset == XDP_UMEM_PGOFF_FILL_RING)
981 q = READ_ONCE(umem->fq);
982 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
983 q = READ_ONCE(umem->cq);
989 /* Matches the smp_wmb() in xsk_init_queue */
991 qpg = virt_to_head_page(q->ring);
992 if (size > page_size(qpg))
995 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
996 return remap_pfn_range(vma, vma->vm_start, pfn,
997 size, vma->vm_page_prot);
1000 static int xsk_notifier(struct notifier_block *this,
1001 unsigned long msg, void *ptr)
1003 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1004 struct net *net = dev_net(dev);
1008 case NETDEV_UNREGISTER:
1009 mutex_lock(&net->xdp.lock);
1010 sk_for_each(sk, &net->xdp.list) {
1011 struct xdp_sock *xs = xdp_sk(sk);
1013 mutex_lock(&xs->mutex);
1014 if (xs->dev == dev) {
1015 sk->sk_err = ENETDOWN;
1016 if (!sock_flag(sk, SOCK_DEAD))
1017 sk->sk_error_report(sk);
1021 /* Clear device references in umem. */
1022 xdp_umem_clear_dev(xs->umem);
1024 mutex_unlock(&xs->mutex);
1026 mutex_unlock(&net->xdp.lock);
1032 static struct proto xsk_proto = {
1034 .owner = THIS_MODULE,
1035 .obj_size = sizeof(struct xdp_sock),
1038 static const struct proto_ops xsk_proto_ops = {
1040 .owner = THIS_MODULE,
1041 .release = xsk_release,
1043 .connect = sock_no_connect,
1044 .socketpair = sock_no_socketpair,
1045 .accept = sock_no_accept,
1046 .getname = sock_no_getname,
1048 .ioctl = sock_no_ioctl,
1049 .listen = sock_no_listen,
1050 .shutdown = sock_no_shutdown,
1051 .setsockopt = xsk_setsockopt,
1052 .getsockopt = xsk_getsockopt,
1053 .sendmsg = xsk_sendmsg,
1054 .recvmsg = sock_no_recvmsg,
1056 .sendpage = sock_no_sendpage,
1059 static void xsk_destruct(struct sock *sk)
1061 struct xdp_sock *xs = xdp_sk(sk);
1063 if (!sock_flag(sk, SOCK_DEAD))
1066 xdp_put_umem(xs->umem);
1068 sk_refcnt_debug_dec(sk);
1071 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1075 struct xdp_sock *xs;
1077 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1079 if (sock->type != SOCK_RAW)
1080 return -ESOCKTNOSUPPORT;
1083 return -EPROTONOSUPPORT;
1085 sock->state = SS_UNCONNECTED;
1087 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1091 sock->ops = &xsk_proto_ops;
1093 sock_init_data(sock, sk);
1095 sk->sk_family = PF_XDP;
1097 sk->sk_destruct = xsk_destruct;
1098 sk_refcnt_debug_inc(sk);
1100 sock_set_flag(sk, SOCK_RCU_FREE);
1103 xs->state = XSK_READY;
1104 mutex_init(&xs->mutex);
1105 spin_lock_init(&xs->rx_lock);
1106 spin_lock_init(&xs->tx_completion_lock);
1108 INIT_LIST_HEAD(&xs->map_list);
1109 spin_lock_init(&xs->map_list_lock);
1111 mutex_lock(&net->xdp.lock);
1112 sk_add_node_rcu(sk, &net->xdp.list);
1113 mutex_unlock(&net->xdp.lock);
1116 sock_prot_inuse_add(net, &xsk_proto, 1);
1122 static const struct net_proto_family xsk_family_ops = {
1124 .create = xsk_create,
1125 .owner = THIS_MODULE,
1128 static struct notifier_block xsk_netdev_notifier = {
1129 .notifier_call = xsk_notifier,
1132 static int __net_init xsk_net_init(struct net *net)
1134 mutex_init(&net->xdp.lock);
1135 INIT_HLIST_HEAD(&net->xdp.list);
1139 static void __net_exit xsk_net_exit(struct net *net)
1141 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1144 static struct pernet_operations xsk_net_ops = {
1145 .init = xsk_net_init,
1146 .exit = xsk_net_exit,
1149 static int __init xsk_init(void)
1153 err = proto_register(&xsk_proto, 0 /* no slab */);
1157 err = sock_register(&xsk_family_ops);
1161 err = register_pernet_subsys(&xsk_net_ops);
1165 err = register_netdevice_notifier(&xsk_netdev_notifier);
1172 unregister_pernet_subsys(&xsk_net_ops);
1174 sock_unregister(PF_XDP);
1176 proto_unregister(&xsk_proto);
1181 fs_initcall(xsk_init);