Linux 5.7.7
[linux/fpc-iii.git] / net / xdp / xsk.c
bloba4676107fad0557ca175212e8235e2e9cddb4631
1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP sockets
4 * AF_XDP sockets allows a channel between XDP programs and userspace
5 * applications.
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.h>
26 #include <net/xdp.h>
28 #include "xsk_queue.h"
29 #include "xdp_umem.h"
30 #include "xsk.h"
32 #define TX_BATCH_SIZE 16
34 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
36 bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
38 return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
39 READ_ONCE(xs->umem->fq);
42 bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt)
44 return xskq_cons_has_entries(umem->fq, cnt);
46 EXPORT_SYMBOL(xsk_umem_has_addrs);
48 bool xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
50 return xskq_cons_peek_addr(umem->fq, addr, umem);
52 EXPORT_SYMBOL(xsk_umem_peek_addr);
54 void xsk_umem_release_addr(struct xdp_umem *umem)
56 xskq_cons_release(umem->fq);
58 EXPORT_SYMBOL(xsk_umem_release_addr);
60 void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
62 if (umem->need_wakeup & XDP_WAKEUP_RX)
63 return;
65 umem->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
66 umem->need_wakeup |= XDP_WAKEUP_RX;
68 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
70 void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
72 struct xdp_sock *xs;
74 if (umem->need_wakeup & XDP_WAKEUP_TX)
75 return;
77 rcu_read_lock();
78 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
79 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
81 rcu_read_unlock();
83 umem->need_wakeup |= XDP_WAKEUP_TX;
85 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
87 void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
89 if (!(umem->need_wakeup & XDP_WAKEUP_RX))
90 return;
92 umem->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
93 umem->need_wakeup &= ~XDP_WAKEUP_RX;
95 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
97 void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
99 struct xdp_sock *xs;
101 if (!(umem->need_wakeup & XDP_WAKEUP_TX))
102 return;
104 rcu_read_lock();
105 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
106 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
108 rcu_read_unlock();
110 umem->need_wakeup &= ~XDP_WAKEUP_TX;
112 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
114 bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
116 return umem->flags & XDP_UMEM_USES_NEED_WAKEUP;
118 EXPORT_SYMBOL(xsk_umem_uses_need_wakeup);
120 /* If a buffer crosses a page boundary, we need to do 2 memcpy's, one for
121 * each page. This is only required in copy mode.
123 static void __xsk_rcv_memcpy(struct xdp_umem *umem, u64 addr, void *from_buf,
124 u32 len, u32 metalen)
126 void *to_buf = xdp_umem_get_data(umem, addr);
128 addr = xsk_umem_add_offset_to_addr(addr);
129 if (xskq_cons_crosses_non_contig_pg(umem, addr, len + metalen)) {
130 void *next_pg_addr = umem->pages[(addr >> PAGE_SHIFT) + 1].addr;
131 u64 page_start = addr & ~(PAGE_SIZE - 1);
132 u64 first_len = PAGE_SIZE - (addr - page_start);
134 memcpy(to_buf, from_buf, first_len);
135 memcpy(next_pg_addr, from_buf + first_len,
136 len + metalen - first_len);
138 return;
141 memcpy(to_buf, from_buf, len + metalen);
144 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
146 u64 offset = xs->umem->headroom;
147 u64 addr, memcpy_addr;
148 void *from_buf;
149 u32 metalen;
150 int err;
152 if (!xskq_cons_peek_addr(xs->umem->fq, &addr, xs->umem) ||
153 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
154 xs->rx_dropped++;
155 return -ENOSPC;
158 if (unlikely(xdp_data_meta_unsupported(xdp))) {
159 from_buf = xdp->data;
160 metalen = 0;
161 } else {
162 from_buf = xdp->data_meta;
163 metalen = xdp->data - xdp->data_meta;
166 memcpy_addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
167 __xsk_rcv_memcpy(xs->umem, memcpy_addr, from_buf, len, metalen);
169 offset += metalen;
170 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
171 err = xskq_prod_reserve_desc(xs->rx, addr, len);
172 if (!err) {
173 xskq_cons_release(xs->umem->fq);
174 xdp_return_buff(xdp);
175 return 0;
178 xs->rx_dropped++;
179 return err;
182 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
184 int err = xskq_prod_reserve_desc(xs->rx, xdp->handle, len);
186 if (err)
187 xs->rx_dropped++;
189 return err;
192 static bool xsk_is_bound(struct xdp_sock *xs)
194 if (READ_ONCE(xs->state) == XSK_BOUND) {
195 /* Matches smp_wmb() in bind(). */
196 smp_rmb();
197 return true;
199 return false;
202 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
204 u32 len;
206 if (!xsk_is_bound(xs))
207 return -EINVAL;
209 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
210 return -EINVAL;
212 len = xdp->data_end - xdp->data;
214 return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ?
215 __xsk_rcv_zc(xs, xdp, len) : __xsk_rcv(xs, xdp, len);
218 static void xsk_flush(struct xdp_sock *xs)
220 xskq_prod_submit(xs->rx);
221 __xskq_cons_release(xs->umem->fq);
222 sock_def_readable(&xs->sk);
225 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
227 u32 metalen = xdp->data - xdp->data_meta;
228 u32 len = xdp->data_end - xdp->data;
229 u64 offset = xs->umem->headroom;
230 void *buffer;
231 u64 addr;
232 int err;
234 spin_lock_bh(&xs->rx_lock);
236 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) {
237 err = -EINVAL;
238 goto out_unlock;
241 if (!xskq_cons_peek_addr(xs->umem->fq, &addr, xs->umem) ||
242 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
243 err = -ENOSPC;
244 goto out_drop;
247 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
248 buffer = xdp_umem_get_data(xs->umem, addr);
249 memcpy(buffer, xdp->data_meta, len + metalen);
251 addr = xsk_umem_adjust_offset(xs->umem, addr, metalen);
252 err = xskq_prod_reserve_desc(xs->rx, addr, len);
253 if (err)
254 goto out_drop;
256 xskq_cons_release(xs->umem->fq);
257 xskq_prod_submit(xs->rx);
259 spin_unlock_bh(&xs->rx_lock);
261 xs->sk.sk_data_ready(&xs->sk);
262 return 0;
264 out_drop:
265 xs->rx_dropped++;
266 out_unlock:
267 spin_unlock_bh(&xs->rx_lock);
268 return err;
271 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
273 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
274 int err;
276 err = xsk_rcv(xs, xdp);
277 if (err)
278 return err;
280 if (!xs->flush_node.prev)
281 list_add(&xs->flush_node, flush_list);
283 return 0;
286 void __xsk_map_flush(void)
288 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
289 struct xdp_sock *xs, *tmp;
291 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
292 xsk_flush(xs);
293 __list_del_clearprev(&xs->flush_node);
297 void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
299 xskq_prod_submit_n(umem->cq, nb_entries);
301 EXPORT_SYMBOL(xsk_umem_complete_tx);
303 void xsk_umem_consume_tx_done(struct xdp_umem *umem)
305 struct xdp_sock *xs;
307 rcu_read_lock();
308 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
309 __xskq_cons_release(xs->tx);
310 xs->sk.sk_write_space(&xs->sk);
312 rcu_read_unlock();
314 EXPORT_SYMBOL(xsk_umem_consume_tx_done);
316 bool xsk_umem_consume_tx(struct xdp_umem *umem, struct xdp_desc *desc)
318 struct xdp_sock *xs;
320 rcu_read_lock();
321 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
322 if (!xskq_cons_peek_desc(xs->tx, desc, umem))
323 continue;
325 /* This is the backpreassure mechanism for the Tx path.
326 * Reserve space in the completion queue and only proceed
327 * if there is space in it. This avoids having to implement
328 * any buffering in the Tx path.
330 if (xskq_prod_reserve_addr(umem->cq, desc->addr))
331 goto out;
333 xskq_cons_release(xs->tx);
334 rcu_read_unlock();
335 return true;
338 out:
339 rcu_read_unlock();
340 return false;
342 EXPORT_SYMBOL(xsk_umem_consume_tx);
344 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
346 struct net_device *dev = xs->dev;
347 int err;
349 rcu_read_lock();
350 err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
351 rcu_read_unlock();
353 return err;
356 static int xsk_zc_xmit(struct xdp_sock *xs)
358 return xsk_wakeup(xs, XDP_WAKEUP_TX);
361 static void xsk_destruct_skb(struct sk_buff *skb)
363 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
364 struct xdp_sock *xs = xdp_sk(skb->sk);
365 unsigned long flags;
367 spin_lock_irqsave(&xs->tx_completion_lock, flags);
368 xskq_prod_submit_addr(xs->umem->cq, addr);
369 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
371 sock_wfree(skb);
374 static int xsk_generic_xmit(struct sock *sk)
376 struct xdp_sock *xs = xdp_sk(sk);
377 u32 max_batch = TX_BATCH_SIZE;
378 bool sent_frame = false;
379 struct xdp_desc desc;
380 struct sk_buff *skb;
381 int err = 0;
383 mutex_lock(&xs->mutex);
385 if (xs->queue_id >= xs->dev->real_num_tx_queues)
386 goto out;
388 while (xskq_cons_peek_desc(xs->tx, &desc, xs->umem)) {
389 char *buffer;
390 u64 addr;
391 u32 len;
393 if (max_batch-- == 0) {
394 err = -EAGAIN;
395 goto out;
398 len = desc.len;
399 skb = sock_alloc_send_skb(sk, len, 1, &err);
400 if (unlikely(!skb))
401 goto out;
403 skb_put(skb, len);
404 addr = desc.addr;
405 buffer = xdp_umem_get_data(xs->umem, addr);
406 err = skb_store_bits(skb, 0, buffer, len);
407 /* This is the backpreassure mechanism for the Tx path.
408 * Reserve space in the completion queue and only proceed
409 * if there is space in it. This avoids having to implement
410 * any buffering in the Tx path.
412 if (unlikely(err) || xskq_prod_reserve(xs->umem->cq)) {
413 kfree_skb(skb);
414 goto out;
417 skb->dev = xs->dev;
418 skb->priority = sk->sk_priority;
419 skb->mark = sk->sk_mark;
420 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
421 skb->destructor = xsk_destruct_skb;
423 err = dev_direct_xmit(skb, xs->queue_id);
424 xskq_cons_release(xs->tx);
425 /* Ignore NET_XMIT_CN as packet might have been sent */
426 if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
427 /* SKB completed but not sent */
428 err = -EBUSY;
429 goto out;
432 sent_frame = true;
435 out:
436 if (sent_frame)
437 sk->sk_write_space(sk);
439 mutex_unlock(&xs->mutex);
440 return err;
443 static int __xsk_sendmsg(struct sock *sk)
445 struct xdp_sock *xs = xdp_sk(sk);
447 if (unlikely(!(xs->dev->flags & IFF_UP)))
448 return -ENETDOWN;
449 if (unlikely(!xs->tx))
450 return -ENOBUFS;
452 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
455 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
457 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
458 struct sock *sk = sock->sk;
459 struct xdp_sock *xs = xdp_sk(sk);
461 if (unlikely(!xsk_is_bound(xs)))
462 return -ENXIO;
463 if (unlikely(need_wait))
464 return -EOPNOTSUPP;
466 return __xsk_sendmsg(sk);
469 static __poll_t xsk_poll(struct file *file, struct socket *sock,
470 struct poll_table_struct *wait)
472 __poll_t mask = datagram_poll(file, sock, wait);
473 struct sock *sk = sock->sk;
474 struct xdp_sock *xs = xdp_sk(sk);
475 struct xdp_umem *umem;
477 if (unlikely(!xsk_is_bound(xs)))
478 return mask;
480 umem = xs->umem;
482 if (umem->need_wakeup) {
483 if (xs->zc)
484 xsk_wakeup(xs, umem->need_wakeup);
485 else
486 /* Poll needs to drive Tx also in copy mode */
487 __xsk_sendmsg(sk);
490 if (xs->rx && !xskq_prod_is_empty(xs->rx))
491 mask |= EPOLLIN | EPOLLRDNORM;
492 if (xs->tx && !xskq_cons_is_full(xs->tx))
493 mask |= EPOLLOUT | EPOLLWRNORM;
495 return mask;
498 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
499 bool umem_queue)
501 struct xsk_queue *q;
503 if (entries == 0 || *queue || !is_power_of_2(entries))
504 return -EINVAL;
506 q = xskq_create(entries, umem_queue);
507 if (!q)
508 return -ENOMEM;
510 /* Make sure queue is ready before it can be seen by others */
511 smp_wmb();
512 WRITE_ONCE(*queue, q);
513 return 0;
516 static void xsk_unbind_dev(struct xdp_sock *xs)
518 struct net_device *dev = xs->dev;
520 if (xs->state != XSK_BOUND)
521 return;
522 WRITE_ONCE(xs->state, XSK_UNBOUND);
524 /* Wait for driver to stop using the xdp socket. */
525 xdp_del_sk_umem(xs->umem, xs);
526 xs->dev = NULL;
527 synchronize_net();
528 dev_put(dev);
531 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
532 struct xdp_sock ***map_entry)
534 struct xsk_map *map = NULL;
535 struct xsk_map_node *node;
537 *map_entry = NULL;
539 spin_lock_bh(&xs->map_list_lock);
540 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
541 node);
542 if (node) {
543 WARN_ON(xsk_map_inc(node->map));
544 map = node->map;
545 *map_entry = node->map_entry;
547 spin_unlock_bh(&xs->map_list_lock);
548 return map;
551 static void xsk_delete_from_maps(struct xdp_sock *xs)
553 /* This function removes the current XDP socket from all the
554 * maps it resides in. We need to take extra care here, due to
555 * the two locks involved. Each map has a lock synchronizing
556 * updates to the entries, and each socket has a lock that
557 * synchronizes access to the list of maps (map_list). For
558 * deadlock avoidance the locks need to be taken in the order
559 * "map lock"->"socket map list lock". We start off by
560 * accessing the socket map list, and take a reference to the
561 * map to guarantee existence between the
562 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
563 * calls. Then we ask the map to remove the socket, which
564 * tries to remove the socket from the map. Note that there
565 * might be updates to the map between
566 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
568 struct xdp_sock **map_entry = NULL;
569 struct xsk_map *map;
571 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
572 xsk_map_try_sock_delete(map, xs, map_entry);
573 xsk_map_put(map);
577 static int xsk_release(struct socket *sock)
579 struct sock *sk = sock->sk;
580 struct xdp_sock *xs = xdp_sk(sk);
581 struct net *net;
583 if (!sk)
584 return 0;
586 net = sock_net(sk);
588 mutex_lock(&net->xdp.lock);
589 sk_del_node_init_rcu(sk);
590 mutex_unlock(&net->xdp.lock);
592 local_bh_disable();
593 sock_prot_inuse_add(net, sk->sk_prot, -1);
594 local_bh_enable();
596 xsk_delete_from_maps(xs);
597 mutex_lock(&xs->mutex);
598 xsk_unbind_dev(xs);
599 mutex_unlock(&xs->mutex);
601 xskq_destroy(xs->rx);
602 xskq_destroy(xs->tx);
604 sock_orphan(sk);
605 sock->sk = NULL;
607 sk_refcnt_debug_release(sk);
608 sock_put(sk);
610 return 0;
613 static struct socket *xsk_lookup_xsk_from_fd(int fd)
615 struct socket *sock;
616 int err;
618 sock = sockfd_lookup(fd, &err);
619 if (!sock)
620 return ERR_PTR(-ENOTSOCK);
622 if (sock->sk->sk_family != PF_XDP) {
623 sockfd_put(sock);
624 return ERR_PTR(-ENOPROTOOPT);
627 return sock;
630 /* Check if umem pages are contiguous.
631 * If zero-copy mode, use the DMA address to do the page contiguity check
632 * For all other modes we use addr (kernel virtual address)
633 * Store the result in the low bits of addr.
635 static void xsk_check_page_contiguity(struct xdp_umem *umem, u32 flags)
637 struct xdp_umem_page *pgs = umem->pages;
638 int i, is_contig;
640 for (i = 0; i < umem->npgs - 1; i++) {
641 is_contig = (flags & XDP_ZEROCOPY) ?
642 (pgs[i].dma + PAGE_SIZE == pgs[i + 1].dma) :
643 (pgs[i].addr + PAGE_SIZE == pgs[i + 1].addr);
644 pgs[i].addr += is_contig << XSK_NEXT_PG_CONTIG_SHIFT;
648 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
650 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
651 struct sock *sk = sock->sk;
652 struct xdp_sock *xs = xdp_sk(sk);
653 struct net_device *dev;
654 u32 flags, qid;
655 int err = 0;
657 if (addr_len < sizeof(struct sockaddr_xdp))
658 return -EINVAL;
659 if (sxdp->sxdp_family != AF_XDP)
660 return -EINVAL;
662 flags = sxdp->sxdp_flags;
663 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
664 XDP_USE_NEED_WAKEUP))
665 return -EINVAL;
667 rtnl_lock();
668 mutex_lock(&xs->mutex);
669 if (xs->state != XSK_READY) {
670 err = -EBUSY;
671 goto out_release;
674 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
675 if (!dev) {
676 err = -ENODEV;
677 goto out_release;
680 if (!xs->rx && !xs->tx) {
681 err = -EINVAL;
682 goto out_unlock;
685 qid = sxdp->sxdp_queue_id;
687 if (flags & XDP_SHARED_UMEM) {
688 struct xdp_sock *umem_xs;
689 struct socket *sock;
691 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
692 (flags & XDP_USE_NEED_WAKEUP)) {
693 /* Cannot specify flags for shared sockets. */
694 err = -EINVAL;
695 goto out_unlock;
698 if (xs->umem) {
699 /* We have already our own. */
700 err = -EINVAL;
701 goto out_unlock;
704 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
705 if (IS_ERR(sock)) {
706 err = PTR_ERR(sock);
707 goto out_unlock;
710 umem_xs = xdp_sk(sock->sk);
711 if (!xsk_is_bound(umem_xs)) {
712 err = -EBADF;
713 sockfd_put(sock);
714 goto out_unlock;
716 if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
717 err = -EINVAL;
718 sockfd_put(sock);
719 goto out_unlock;
722 xdp_get_umem(umem_xs->umem);
723 WRITE_ONCE(xs->umem, umem_xs->umem);
724 sockfd_put(sock);
725 } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
726 err = -EINVAL;
727 goto out_unlock;
728 } else {
729 /* This xsk has its own umem. */
730 xskq_set_umem(xs->umem->fq, xs->umem->size,
731 xs->umem->chunk_mask);
732 xskq_set_umem(xs->umem->cq, xs->umem->size,
733 xs->umem->chunk_mask);
735 err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
736 if (err)
737 goto out_unlock;
739 xsk_check_page_contiguity(xs->umem, flags);
742 xs->dev = dev;
743 xs->zc = xs->umem->zc;
744 xs->queue_id = qid;
745 xskq_set_umem(xs->rx, xs->umem->size, xs->umem->chunk_mask);
746 xskq_set_umem(xs->tx, xs->umem->size, xs->umem->chunk_mask);
747 xdp_add_sk_umem(xs->umem, xs);
749 out_unlock:
750 if (err) {
751 dev_put(dev);
752 } else {
753 /* Matches smp_rmb() in bind() for shared umem
754 * sockets, and xsk_is_bound().
756 smp_wmb();
757 WRITE_ONCE(xs->state, XSK_BOUND);
759 out_release:
760 mutex_unlock(&xs->mutex);
761 rtnl_unlock();
762 return err;
765 struct xdp_umem_reg_v1 {
766 __u64 addr; /* Start of packet data area */
767 __u64 len; /* Length of packet data area */
768 __u32 chunk_size;
769 __u32 headroom;
772 static int xsk_setsockopt(struct socket *sock, int level, int optname,
773 char __user *optval, unsigned int optlen)
775 struct sock *sk = sock->sk;
776 struct xdp_sock *xs = xdp_sk(sk);
777 int err;
779 if (level != SOL_XDP)
780 return -ENOPROTOOPT;
782 switch (optname) {
783 case XDP_RX_RING:
784 case XDP_TX_RING:
786 struct xsk_queue **q;
787 int entries;
789 if (optlen < sizeof(entries))
790 return -EINVAL;
791 if (copy_from_user(&entries, optval, sizeof(entries)))
792 return -EFAULT;
794 mutex_lock(&xs->mutex);
795 if (xs->state != XSK_READY) {
796 mutex_unlock(&xs->mutex);
797 return -EBUSY;
799 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
800 err = xsk_init_queue(entries, q, false);
801 if (!err && optname == XDP_TX_RING)
802 /* Tx needs to be explicitly woken up the first time */
803 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
804 mutex_unlock(&xs->mutex);
805 return err;
807 case XDP_UMEM_REG:
809 size_t mr_size = sizeof(struct xdp_umem_reg);
810 struct xdp_umem_reg mr = {};
811 struct xdp_umem *umem;
813 if (optlen < sizeof(struct xdp_umem_reg_v1))
814 return -EINVAL;
815 else if (optlen < sizeof(mr))
816 mr_size = sizeof(struct xdp_umem_reg_v1);
818 if (copy_from_user(&mr, optval, mr_size))
819 return -EFAULT;
821 mutex_lock(&xs->mutex);
822 if (xs->state != XSK_READY || xs->umem) {
823 mutex_unlock(&xs->mutex);
824 return -EBUSY;
827 umem = xdp_umem_create(&mr);
828 if (IS_ERR(umem)) {
829 mutex_unlock(&xs->mutex);
830 return PTR_ERR(umem);
833 /* Make sure umem is ready before it can be seen by others */
834 smp_wmb();
835 WRITE_ONCE(xs->umem, umem);
836 mutex_unlock(&xs->mutex);
837 return 0;
839 case XDP_UMEM_FILL_RING:
840 case XDP_UMEM_COMPLETION_RING:
842 struct xsk_queue **q;
843 int entries;
845 if (copy_from_user(&entries, optval, sizeof(entries)))
846 return -EFAULT;
848 mutex_lock(&xs->mutex);
849 if (xs->state != XSK_READY) {
850 mutex_unlock(&xs->mutex);
851 return -EBUSY;
853 if (!xs->umem) {
854 mutex_unlock(&xs->mutex);
855 return -EINVAL;
858 q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
859 &xs->umem->cq;
860 err = xsk_init_queue(entries, q, true);
861 mutex_unlock(&xs->mutex);
862 return err;
864 default:
865 break;
868 return -ENOPROTOOPT;
871 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
873 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
874 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
875 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
878 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
880 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
881 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
882 ring->desc = offsetof(struct xdp_umem_ring, desc);
885 static int xsk_getsockopt(struct socket *sock, int level, int optname,
886 char __user *optval, int __user *optlen)
888 struct sock *sk = sock->sk;
889 struct xdp_sock *xs = xdp_sk(sk);
890 int len;
892 if (level != SOL_XDP)
893 return -ENOPROTOOPT;
895 if (get_user(len, optlen))
896 return -EFAULT;
897 if (len < 0)
898 return -EINVAL;
900 switch (optname) {
901 case XDP_STATISTICS:
903 struct xdp_statistics stats;
905 if (len < sizeof(stats))
906 return -EINVAL;
908 mutex_lock(&xs->mutex);
909 stats.rx_dropped = xs->rx_dropped;
910 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
911 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
912 mutex_unlock(&xs->mutex);
914 if (copy_to_user(optval, &stats, sizeof(stats)))
915 return -EFAULT;
916 if (put_user(sizeof(stats), optlen))
917 return -EFAULT;
919 return 0;
921 case XDP_MMAP_OFFSETS:
923 struct xdp_mmap_offsets off;
924 struct xdp_mmap_offsets_v1 off_v1;
925 bool flags_supported = true;
926 void *to_copy;
928 if (len < sizeof(off_v1))
929 return -EINVAL;
930 else if (len < sizeof(off))
931 flags_supported = false;
933 if (flags_supported) {
934 /* xdp_ring_offset is identical to xdp_ring_offset_v1
935 * except for the flags field added to the end.
937 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
938 &off.rx);
939 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
940 &off.tx);
941 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
942 &off.fr);
943 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
944 &off.cr);
945 off.rx.flags = offsetof(struct xdp_rxtx_ring,
946 ptrs.flags);
947 off.tx.flags = offsetof(struct xdp_rxtx_ring,
948 ptrs.flags);
949 off.fr.flags = offsetof(struct xdp_umem_ring,
950 ptrs.flags);
951 off.cr.flags = offsetof(struct xdp_umem_ring,
952 ptrs.flags);
954 len = sizeof(off);
955 to_copy = &off;
956 } else {
957 xsk_enter_rxtx_offsets(&off_v1.rx);
958 xsk_enter_rxtx_offsets(&off_v1.tx);
959 xsk_enter_umem_offsets(&off_v1.fr);
960 xsk_enter_umem_offsets(&off_v1.cr);
962 len = sizeof(off_v1);
963 to_copy = &off_v1;
966 if (copy_to_user(optval, to_copy, len))
967 return -EFAULT;
968 if (put_user(len, optlen))
969 return -EFAULT;
971 return 0;
973 case XDP_OPTIONS:
975 struct xdp_options opts = {};
977 if (len < sizeof(opts))
978 return -EINVAL;
980 mutex_lock(&xs->mutex);
981 if (xs->zc)
982 opts.flags |= XDP_OPTIONS_ZEROCOPY;
983 mutex_unlock(&xs->mutex);
985 len = sizeof(opts);
986 if (copy_to_user(optval, &opts, len))
987 return -EFAULT;
988 if (put_user(len, optlen))
989 return -EFAULT;
991 return 0;
993 default:
994 break;
997 return -EOPNOTSUPP;
1000 static int xsk_mmap(struct file *file, struct socket *sock,
1001 struct vm_area_struct *vma)
1003 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1004 unsigned long size = vma->vm_end - vma->vm_start;
1005 struct xdp_sock *xs = xdp_sk(sock->sk);
1006 struct xsk_queue *q = NULL;
1007 struct xdp_umem *umem;
1008 unsigned long pfn;
1009 struct page *qpg;
1011 if (READ_ONCE(xs->state) != XSK_READY)
1012 return -EBUSY;
1014 if (offset == XDP_PGOFF_RX_RING) {
1015 q = READ_ONCE(xs->rx);
1016 } else if (offset == XDP_PGOFF_TX_RING) {
1017 q = READ_ONCE(xs->tx);
1018 } else {
1019 umem = READ_ONCE(xs->umem);
1020 if (!umem)
1021 return -EINVAL;
1023 /* Matches the smp_wmb() in XDP_UMEM_REG */
1024 smp_rmb();
1025 if (offset == XDP_UMEM_PGOFF_FILL_RING)
1026 q = READ_ONCE(umem->fq);
1027 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1028 q = READ_ONCE(umem->cq);
1031 if (!q)
1032 return -EINVAL;
1034 /* Matches the smp_wmb() in xsk_init_queue */
1035 smp_rmb();
1036 qpg = virt_to_head_page(q->ring);
1037 if (size > page_size(qpg))
1038 return -EINVAL;
1040 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1041 return remap_pfn_range(vma, vma->vm_start, pfn,
1042 size, vma->vm_page_prot);
1045 static int xsk_notifier(struct notifier_block *this,
1046 unsigned long msg, void *ptr)
1048 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1049 struct net *net = dev_net(dev);
1050 struct sock *sk;
1052 switch (msg) {
1053 case NETDEV_UNREGISTER:
1054 mutex_lock(&net->xdp.lock);
1055 sk_for_each(sk, &net->xdp.list) {
1056 struct xdp_sock *xs = xdp_sk(sk);
1058 mutex_lock(&xs->mutex);
1059 if (xs->dev == dev) {
1060 sk->sk_err = ENETDOWN;
1061 if (!sock_flag(sk, SOCK_DEAD))
1062 sk->sk_error_report(sk);
1064 xsk_unbind_dev(xs);
1066 /* Clear device references in umem. */
1067 xdp_umem_clear_dev(xs->umem);
1069 mutex_unlock(&xs->mutex);
1071 mutex_unlock(&net->xdp.lock);
1072 break;
1074 return NOTIFY_DONE;
1077 static struct proto xsk_proto = {
1078 .name = "XDP",
1079 .owner = THIS_MODULE,
1080 .obj_size = sizeof(struct xdp_sock),
1083 static const struct proto_ops xsk_proto_ops = {
1084 .family = PF_XDP,
1085 .owner = THIS_MODULE,
1086 .release = xsk_release,
1087 .bind = xsk_bind,
1088 .connect = sock_no_connect,
1089 .socketpair = sock_no_socketpair,
1090 .accept = sock_no_accept,
1091 .getname = sock_no_getname,
1092 .poll = xsk_poll,
1093 .ioctl = sock_no_ioctl,
1094 .listen = sock_no_listen,
1095 .shutdown = sock_no_shutdown,
1096 .setsockopt = xsk_setsockopt,
1097 .getsockopt = xsk_getsockopt,
1098 .sendmsg = xsk_sendmsg,
1099 .recvmsg = sock_no_recvmsg,
1100 .mmap = xsk_mmap,
1101 .sendpage = sock_no_sendpage,
1104 static void xsk_destruct(struct sock *sk)
1106 struct xdp_sock *xs = xdp_sk(sk);
1108 if (!sock_flag(sk, SOCK_DEAD))
1109 return;
1111 xdp_put_umem(xs->umem);
1113 sk_refcnt_debug_dec(sk);
1116 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1117 int kern)
1119 struct sock *sk;
1120 struct xdp_sock *xs;
1122 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1123 return -EPERM;
1124 if (sock->type != SOCK_RAW)
1125 return -ESOCKTNOSUPPORT;
1127 if (protocol)
1128 return -EPROTONOSUPPORT;
1130 sock->state = SS_UNCONNECTED;
1132 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1133 if (!sk)
1134 return -ENOBUFS;
1136 sock->ops = &xsk_proto_ops;
1138 sock_init_data(sock, sk);
1140 sk->sk_family = PF_XDP;
1142 sk->sk_destruct = xsk_destruct;
1143 sk_refcnt_debug_inc(sk);
1145 sock_set_flag(sk, SOCK_RCU_FREE);
1147 xs = xdp_sk(sk);
1148 xs->state = XSK_READY;
1149 mutex_init(&xs->mutex);
1150 spin_lock_init(&xs->rx_lock);
1151 spin_lock_init(&xs->tx_completion_lock);
1153 INIT_LIST_HEAD(&xs->map_list);
1154 spin_lock_init(&xs->map_list_lock);
1156 mutex_lock(&net->xdp.lock);
1157 sk_add_node_rcu(sk, &net->xdp.list);
1158 mutex_unlock(&net->xdp.lock);
1160 local_bh_disable();
1161 sock_prot_inuse_add(net, &xsk_proto, 1);
1162 local_bh_enable();
1164 return 0;
1167 static const struct net_proto_family xsk_family_ops = {
1168 .family = PF_XDP,
1169 .create = xsk_create,
1170 .owner = THIS_MODULE,
1173 static struct notifier_block xsk_netdev_notifier = {
1174 .notifier_call = xsk_notifier,
1177 static int __net_init xsk_net_init(struct net *net)
1179 mutex_init(&net->xdp.lock);
1180 INIT_HLIST_HEAD(&net->xdp.list);
1181 return 0;
1184 static void __net_exit xsk_net_exit(struct net *net)
1186 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1189 static struct pernet_operations xsk_net_ops = {
1190 .init = xsk_net_init,
1191 .exit = xsk_net_exit,
1194 static int __init xsk_init(void)
1196 int err, cpu;
1198 err = proto_register(&xsk_proto, 0 /* no slab */);
1199 if (err)
1200 goto out;
1202 err = sock_register(&xsk_family_ops);
1203 if (err)
1204 goto out_proto;
1206 err = register_pernet_subsys(&xsk_net_ops);
1207 if (err)
1208 goto out_sk;
1210 err = register_netdevice_notifier(&xsk_netdev_notifier);
1211 if (err)
1212 goto out_pernet;
1214 for_each_possible_cpu(cpu)
1215 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1216 return 0;
1218 out_pernet:
1219 unregister_pernet_subsys(&xsk_net_ops);
1220 out_sk:
1221 sock_unregister(PF_XDP);
1222 out_proto:
1223 proto_unregister(&xsk_proto);
1224 out:
1225 return err;
1228 fs_initcall(xsk_init);