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"
31 #define TX_BATCH_SIZE 16
33 static struct xdp_sock *xdp_sk(struct sock *sk)
35 return (struct xdp_sock *)sk;
38 bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
40 return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
41 READ_ONCE(xs->umem->fq);
44 u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
46 return xskq_peek_addr(umem->fq, addr);
48 EXPORT_SYMBOL(xsk_umem_peek_addr);
50 void xsk_umem_discard_addr(struct xdp_umem *umem)
52 xskq_discard_addr(umem->fq);
54 EXPORT_SYMBOL(xsk_umem_discard_addr);
56 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
62 if (!xskq_peek_addr(xs->umem->fq, &addr) ||
63 len > xs->umem->chunk_size_nohr) {
68 addr += xs->umem->headroom;
70 buffer = xdp_umem_get_data(xs->umem, addr);
71 memcpy(buffer, xdp->data, len);
72 err = xskq_produce_batch_desc(xs->rx, addr, len);
74 xskq_discard_addr(xs->umem->fq);
83 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
85 int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);
95 int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
99 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
102 len = xdp->data_end - xdp->data;
104 return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ?
105 __xsk_rcv_zc(xs, xdp, len) : __xsk_rcv(xs, xdp, len);
108 void xsk_flush(struct xdp_sock *xs)
110 xskq_produce_flush_desc(xs->rx);
111 xs->sk.sk_data_ready(&xs->sk);
114 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
116 u32 len = xdp->data_end - xdp->data;
121 if (!xskq_peek_addr(xs->umem->fq, &addr) ||
122 len > xs->umem->chunk_size_nohr) {
127 addr += xs->umem->headroom;
129 buffer = xdp_umem_get_data(xs->umem, addr);
130 memcpy(buffer, xdp->data, len);
131 err = xskq_produce_batch_desc(xs->rx, addr, len);
133 xskq_discard_addr(xs->umem->fq);
142 void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
144 xskq_produce_flush_addr_n(umem->cq, nb_entries);
146 EXPORT_SYMBOL(xsk_umem_complete_tx);
148 void xsk_umem_consume_tx_done(struct xdp_umem *umem)
153 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
154 xs->sk.sk_write_space(&xs->sk);
158 EXPORT_SYMBOL(xsk_umem_consume_tx_done);
160 bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len)
162 struct xdp_desc desc;
166 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
167 if (!xskq_peek_desc(xs->tx, &desc))
170 if (xskq_produce_addr_lazy(umem->cq, desc.addr))
173 *dma = xdp_umem_get_dma(umem, desc.addr);
176 xskq_discard_desc(xs->tx);
185 EXPORT_SYMBOL(xsk_umem_consume_tx);
187 static int xsk_zc_xmit(struct sock *sk)
189 struct xdp_sock *xs = xdp_sk(sk);
190 struct net_device *dev = xs->dev;
192 return dev->netdev_ops->ndo_xsk_async_xmit(dev, xs->queue_id);
195 static void xsk_destruct_skb(struct sk_buff *skb)
197 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
198 struct xdp_sock *xs = xdp_sk(skb->sk);
200 WARN_ON_ONCE(xskq_produce_addr(xs->umem->cq, addr));
205 static int xsk_generic_xmit(struct sock *sk, struct msghdr *m,
208 u32 max_batch = TX_BATCH_SIZE;
209 struct xdp_sock *xs = xdp_sk(sk);
210 bool sent_frame = false;
211 struct xdp_desc desc;
215 if (unlikely(!xs->tx))
218 mutex_lock(&xs->mutex);
220 while (xskq_peek_desc(xs->tx, &desc)) {
225 if (max_batch-- == 0) {
230 if (xskq_reserve_addr(xs->umem->cq)) {
236 if (unlikely(len > xs->dev->mtu)) {
241 if (xs->queue_id >= xs->dev->real_num_tx_queues) {
246 skb = sock_alloc_send_skb(sk, len, 1, &err);
247 if (unlikely(!skb)) {
254 buffer = xdp_umem_get_data(xs->umem, addr);
255 err = skb_store_bits(skb, 0, buffer, len);
262 skb->priority = sk->sk_priority;
263 skb->mark = sk->sk_mark;
264 skb_shinfo(skb)->destructor_arg = (void *)(long)addr;
265 skb->destructor = xsk_destruct_skb;
267 err = dev_direct_xmit(skb, xs->queue_id);
268 /* Ignore NET_XMIT_CN as packet might have been sent */
269 if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
271 /* SKB consumed by dev_direct_xmit() */
276 xskq_discard_desc(xs->tx);
281 sk->sk_write_space(sk);
283 mutex_unlock(&xs->mutex);
287 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
289 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
290 struct sock *sk = sock->sk;
291 struct xdp_sock *xs = xdp_sk(sk);
293 if (unlikely(!xs->dev))
295 if (unlikely(!(xs->dev->flags & IFF_UP)))
300 return (xs->zc) ? xsk_zc_xmit(sk) : xsk_generic_xmit(sk, m, total_len);
303 static __poll_t xsk_poll_mask(struct socket *sock, __poll_t events)
305 __poll_t mask = datagram_poll_mask(sock, events);
306 struct sock *sk = sock->sk;
307 struct xdp_sock *xs = xdp_sk(sk);
309 if (xs->rx && !xskq_empty_desc(xs->rx))
310 mask |= POLLIN | POLLRDNORM;
311 if (xs->tx && !xskq_full_desc(xs->tx))
312 mask |= POLLOUT | POLLWRNORM;
317 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
322 if (entries == 0 || *queue || !is_power_of_2(entries))
325 q = xskq_create(entries, umem_queue);
329 /* Make sure queue is ready before it can be seen by others */
335 static int xsk_release(struct socket *sock)
337 struct sock *sk = sock->sk;
338 struct xdp_sock *xs = xdp_sk(sk);
347 sock_prot_inuse_add(net, sk->sk_prot, -1);
351 /* Wait for driver to stop using the xdp socket. */
360 sk_refcnt_debug_release(sk);
366 static struct socket *xsk_lookup_xsk_from_fd(int fd)
371 sock = sockfd_lookup(fd, &err);
373 return ERR_PTR(-ENOTSOCK);
375 if (sock->sk->sk_family != PF_XDP) {
377 return ERR_PTR(-ENOPROTOOPT);
383 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
385 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
386 struct sock *sk = sock->sk;
387 struct xdp_sock *xs = xdp_sk(sk);
388 struct net_device *dev;
392 if (addr_len < sizeof(struct sockaddr_xdp))
394 if (sxdp->sxdp_family != AF_XDP)
397 mutex_lock(&xs->mutex);
403 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
409 if (!xs->rx && !xs->tx) {
414 qid = sxdp->sxdp_queue_id;
416 if ((xs->rx && qid >= dev->real_num_rx_queues) ||
417 (xs->tx && qid >= dev->real_num_tx_queues)) {
422 flags = sxdp->sxdp_flags;
424 if (flags & XDP_SHARED_UMEM) {
425 struct xdp_sock *umem_xs;
428 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY)) {
429 /* Cannot specify flags for shared sockets. */
435 /* We have already our own. */
440 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
446 umem_xs = xdp_sk(sock->sk);
447 if (!umem_xs->umem) {
448 /* No umem to inherit. */
452 } else if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
458 xdp_get_umem(umem_xs->umem);
459 xs->umem = umem_xs->umem;
461 } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
465 /* This xsk has its own umem. */
466 xskq_set_umem(xs->umem->fq, &xs->umem->props);
467 xskq_set_umem(xs->umem->cq, &xs->umem->props);
469 err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
475 xs->zc = xs->umem->zc;
477 xskq_set_umem(xs->rx, &xs->umem->props);
478 xskq_set_umem(xs->tx, &xs->umem->props);
479 xdp_add_sk_umem(xs->umem, xs);
485 mutex_unlock(&xs->mutex);
489 static int xsk_setsockopt(struct socket *sock, int level, int optname,
490 char __user *optval, unsigned int optlen)
492 struct sock *sk = sock->sk;
493 struct xdp_sock *xs = xdp_sk(sk);
496 if (level != SOL_XDP)
503 struct xsk_queue **q;
506 if (optlen < sizeof(entries))
508 if (copy_from_user(&entries, optval, sizeof(entries)))
511 mutex_lock(&xs->mutex);
512 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
513 err = xsk_init_queue(entries, q, false);
514 mutex_unlock(&xs->mutex);
519 struct xdp_umem_reg mr;
520 struct xdp_umem *umem;
522 if (copy_from_user(&mr, optval, sizeof(mr)))
525 mutex_lock(&xs->mutex);
527 mutex_unlock(&xs->mutex);
531 umem = xdp_umem_create(&mr);
533 mutex_unlock(&xs->mutex);
534 return PTR_ERR(umem);
537 /* Make sure umem is ready before it can be seen by others */
540 mutex_unlock(&xs->mutex);
543 case XDP_UMEM_FILL_RING:
544 case XDP_UMEM_COMPLETION_RING:
546 struct xsk_queue **q;
549 if (copy_from_user(&entries, optval, sizeof(entries)))
552 mutex_lock(&xs->mutex);
554 mutex_unlock(&xs->mutex);
558 q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
560 err = xsk_init_queue(entries, q, true);
561 mutex_unlock(&xs->mutex);
571 static int xsk_getsockopt(struct socket *sock, int level, int optname,
572 char __user *optval, int __user *optlen)
574 struct sock *sk = sock->sk;
575 struct xdp_sock *xs = xdp_sk(sk);
578 if (level != SOL_XDP)
581 if (get_user(len, optlen))
589 struct xdp_statistics stats;
591 if (len < sizeof(stats))
594 mutex_lock(&xs->mutex);
595 stats.rx_dropped = xs->rx_dropped;
596 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
597 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
598 mutex_unlock(&xs->mutex);
600 if (copy_to_user(optval, &stats, sizeof(stats)))
602 if (put_user(sizeof(stats), optlen))
607 case XDP_MMAP_OFFSETS:
609 struct xdp_mmap_offsets off;
611 if (len < sizeof(off))
614 off.rx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
615 off.rx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
616 off.rx.desc = offsetof(struct xdp_rxtx_ring, desc);
617 off.tx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
618 off.tx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
619 off.tx.desc = offsetof(struct xdp_rxtx_ring, desc);
621 off.fr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
622 off.fr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
623 off.fr.desc = offsetof(struct xdp_umem_ring, desc);
624 off.cr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
625 off.cr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
626 off.cr.desc = offsetof(struct xdp_umem_ring, desc);
629 if (copy_to_user(optval, &off, len))
631 if (put_user(len, optlen))
643 static int xsk_mmap(struct file *file, struct socket *sock,
644 struct vm_area_struct *vma)
646 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
647 unsigned long size = vma->vm_end - vma->vm_start;
648 struct xdp_sock *xs = xdp_sk(sock->sk);
649 struct xsk_queue *q = NULL;
650 struct xdp_umem *umem;
654 if (offset == XDP_PGOFF_RX_RING) {
655 q = READ_ONCE(xs->rx);
656 } else if (offset == XDP_PGOFF_TX_RING) {
657 q = READ_ONCE(xs->tx);
659 umem = READ_ONCE(xs->umem);
663 if (offset == XDP_UMEM_PGOFF_FILL_RING)
664 q = READ_ONCE(umem->fq);
665 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
666 q = READ_ONCE(umem->cq);
672 qpg = virt_to_head_page(q->ring);
673 if (size > (PAGE_SIZE << compound_order(qpg)))
676 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
677 return remap_pfn_range(vma, vma->vm_start, pfn,
678 size, vma->vm_page_prot);
681 static struct proto xsk_proto = {
683 .owner = THIS_MODULE,
684 .obj_size = sizeof(struct xdp_sock),
687 static const struct proto_ops xsk_proto_ops = {
689 .owner = THIS_MODULE,
690 .release = xsk_release,
692 .connect = sock_no_connect,
693 .socketpair = sock_no_socketpair,
694 .accept = sock_no_accept,
695 .getname = sock_no_getname,
696 .poll_mask = xsk_poll_mask,
697 .ioctl = sock_no_ioctl,
698 .listen = sock_no_listen,
699 .shutdown = sock_no_shutdown,
700 .setsockopt = xsk_setsockopt,
701 .getsockopt = xsk_getsockopt,
702 .sendmsg = xsk_sendmsg,
703 .recvmsg = sock_no_recvmsg,
705 .sendpage = sock_no_sendpage,
708 static void xsk_destruct(struct sock *sk)
710 struct xdp_sock *xs = xdp_sk(sk);
712 if (!sock_flag(sk, SOCK_DEAD))
715 xskq_destroy(xs->rx);
716 xskq_destroy(xs->tx);
717 xdp_del_sk_umem(xs->umem, xs);
718 xdp_put_umem(xs->umem);
720 sk_refcnt_debug_dec(sk);
723 static int xsk_create(struct net *net, struct socket *sock, int protocol,
729 if (!ns_capable(net->user_ns, CAP_NET_RAW))
731 if (sock->type != SOCK_RAW)
732 return -ESOCKTNOSUPPORT;
735 return -EPROTONOSUPPORT;
737 sock->state = SS_UNCONNECTED;
739 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
743 sock->ops = &xsk_proto_ops;
745 sock_init_data(sock, sk);
747 sk->sk_family = PF_XDP;
749 sk->sk_destruct = xsk_destruct;
750 sk_refcnt_debug_inc(sk);
753 mutex_init(&xs->mutex);
756 sock_prot_inuse_add(net, &xsk_proto, 1);
762 static const struct net_proto_family xsk_family_ops = {
764 .create = xsk_create,
765 .owner = THIS_MODULE,
768 static int __init xsk_init(void)
772 err = proto_register(&xsk_proto, 0 /* no slab */);
776 err = sock_register(&xsk_family_ops);
783 proto_unregister(&xsk_proto);
788 fs_initcall(xsk_init);