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 16
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
->real_num_rx_queues
)
112 dev
->_rx
[queue_id
].pool
= NULL
;
113 if (queue_id
< dev
->real_num_tx_queues
)
114 dev
->_tx
[queue_id
].pool
= NULL
;
117 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
118 * not know if the device has more tx queues than rx, or the opposite.
119 * This might also change during run time.
121 int xsk_reg_pool_at_qid(struct net_device
*dev
, struct xsk_buff_pool
*pool
,
124 if (queue_id
>= max_t(unsigned int,
125 dev
->real_num_rx_queues
,
126 dev
->real_num_tx_queues
))
129 if (queue_id
< dev
->real_num_rx_queues
)
130 dev
->_rx
[queue_id
].pool
= pool
;
131 if (queue_id
< dev
->real_num_tx_queues
)
132 dev
->_tx
[queue_id
].pool
= pool
;
137 void xp_release(struct xdp_buff_xsk
*xskb
)
139 xskb
->pool
->free_heads
[xskb
->pool
->free_heads_cnt
++] = xskb
;
142 static u64
xp_get_handle(struct xdp_buff_xsk
*xskb
)
144 u64 offset
= xskb
->xdp
.data
- xskb
->xdp
.data_hard_start
;
146 offset
+= xskb
->pool
->headroom
;
147 if (!xskb
->pool
->unaligned
)
148 return xskb
->orig_addr
+ offset
;
149 return xskb
->orig_addr
+ (offset
<< XSK_UNALIGNED_BUF_OFFSET_SHIFT
);
152 static int __xsk_rcv_zc(struct xdp_sock
*xs
, struct xdp_buff
*xdp
, u32 len
)
154 struct xdp_buff_xsk
*xskb
= container_of(xdp
, struct xdp_buff_xsk
, xdp
);
158 addr
= xp_get_handle(xskb
);
159 err
= xskq_prod_reserve_desc(xs
->rx
, addr
, len
);
169 static void xsk_copy_xdp(struct xdp_buff
*to
, struct xdp_buff
*from
, u32 len
)
171 void *from_buf
, *to_buf
;
174 if (unlikely(xdp_data_meta_unsupported(from
))) {
175 from_buf
= from
->data
;
179 from_buf
= from
->data_meta
;
180 metalen
= from
->data
- from
->data_meta
;
181 to_buf
= to
->data
- metalen
;
184 memcpy(to_buf
, from_buf
, len
+ metalen
);
187 static int __xsk_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
, u32 len
,
190 struct xdp_buff
*xsk_xdp
;
193 if (len
> xsk_pool_get_rx_frame_size(xs
->pool
)) {
198 xsk_xdp
= xsk_buff_alloc(xs
->pool
);
204 xsk_copy_xdp(xsk_xdp
, xdp
, len
);
205 err
= __xsk_rcv_zc(xs
, xsk_xdp
, len
);
207 xsk_buff_free(xsk_xdp
);
211 xdp_return_buff(xdp
);
215 static bool xsk_tx_writeable(struct xdp_sock
*xs
)
217 if (xskq_cons_present_entries(xs
->tx
) > xs
->tx
->nentries
/ 2)
223 static bool xsk_is_bound(struct xdp_sock
*xs
)
225 if (READ_ONCE(xs
->state
) == XSK_BOUND
) {
226 /* Matches smp_wmb() in bind(). */
233 static int xsk_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
,
238 if (!xsk_is_bound(xs
))
241 if (xs
->dev
!= xdp
->rxq
->dev
|| xs
->queue_id
!= xdp
->rxq
->queue_index
)
244 sk_mark_napi_id_once_xdp(&xs
->sk
, xdp
);
245 len
= xdp
->data_end
- xdp
->data
;
247 return xdp
->rxq
->mem
.type
== MEM_TYPE_XSK_BUFF_POOL
?
248 __xsk_rcv_zc(xs
, xdp
, len
) :
249 __xsk_rcv(xs
, xdp
, len
, explicit_free
);
252 static void xsk_flush(struct xdp_sock
*xs
)
254 xskq_prod_submit(xs
->rx
);
255 __xskq_cons_release(xs
->pool
->fq
);
256 sock_def_readable(&xs
->sk
);
259 int xsk_generic_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
)
263 spin_lock_bh(&xs
->rx_lock
);
264 err
= xsk_rcv(xs
, xdp
, false);
266 spin_unlock_bh(&xs
->rx_lock
);
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
, true);
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
, struct xdp_desc
*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
, struct xdp_desc
*descs
,
365 if (!list_is_singular(&pool
->xsk_tx_list
)) {
366 /* Fallback to the non-batched version */
368 return xsk_tx_peek_release_fallback(pool
, descs
, max_entries
);
371 xs
= list_first_or_null_rcu(&pool
->xsk_tx_list
, struct xdp_sock
, tx_list
);
377 nb_pkts
= xskq_cons_peek_desc_batch(xs
->tx
, descs
, pool
, max_entries
);
379 xs
->tx
->queue_empty_descs
++;
383 /* This is the backpressure mechanism for the Tx path. Try to
384 * reserve space in the completion queue for all packets, but
385 * if there are fewer slots available, just process that many
386 * packets. This avoids having to implement any buffering in
389 nb_pkts
= xskq_prod_reserve_addr_batch(pool
->cq
, descs
, nb_pkts
);
393 xskq_cons_release_n(xs
->tx
, nb_pkts
);
394 __xskq_cons_release(xs
->tx
);
395 xs
->sk
.sk_write_space(&xs
->sk
);
401 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch
);
403 static int xsk_wakeup(struct xdp_sock
*xs
, u8 flags
)
405 struct net_device
*dev
= xs
->dev
;
409 err
= dev
->netdev_ops
->ndo_xsk_wakeup(dev
, xs
->queue_id
, flags
);
415 static int xsk_zc_xmit(struct xdp_sock
*xs
)
417 return xsk_wakeup(xs
, XDP_WAKEUP_TX
);
420 static void xsk_destruct_skb(struct sk_buff
*skb
)
422 u64 addr
= (u64
)(long)skb_shinfo(skb
)->destructor_arg
;
423 struct xdp_sock
*xs
= xdp_sk(skb
->sk
);
426 spin_lock_irqsave(&xs
->tx_completion_lock
, flags
);
427 xskq_prod_submit_addr(xs
->pool
->cq
, addr
);
428 spin_unlock_irqrestore(&xs
->tx_completion_lock
, flags
);
433 static int xsk_generic_xmit(struct sock
*sk
)
435 struct xdp_sock
*xs
= xdp_sk(sk
);
436 u32 max_batch
= TX_BATCH_SIZE
;
437 bool sent_frame
= false;
438 struct xdp_desc desc
;
442 mutex_lock(&xs
->mutex
);
444 if (xs
->queue_id
>= xs
->dev
->real_num_tx_queues
)
447 while (xskq_cons_peek_desc(xs
->tx
, &desc
, xs
->pool
)) {
452 if (max_batch
-- == 0) {
458 skb
= sock_alloc_send_skb(sk
, len
, 1, &err
);
464 buffer
= xsk_buff_raw_get_data(xs
->pool
, addr
);
465 err
= skb_store_bits(skb
, 0, buffer
, len
);
466 /* This is the backpressure mechanism for the Tx path.
467 * Reserve space in the completion queue and only proceed
468 * if there is space in it. This avoids having to implement
469 * any buffering in the Tx path.
471 if (unlikely(err
) || xskq_prod_reserve(xs
->pool
->cq
)) {
477 skb
->priority
= sk
->sk_priority
;
478 skb
->mark
= sk
->sk_mark
;
479 skb_shinfo(skb
)->destructor_arg
= (void *)(long)desc
.addr
;
480 skb
->destructor
= xsk_destruct_skb
;
482 err
= __dev_direct_xmit(skb
, xs
->queue_id
);
483 if (err
== NETDEV_TX_BUSY
) {
484 /* Tell user-space to retry the send */
485 skb
->destructor
= sock_wfree
;
486 /* Free skb without triggering the perf drop trace */
492 xskq_cons_release(xs
->tx
);
493 /* Ignore NET_XMIT_CN as packet might have been sent */
494 if (err
== NET_XMIT_DROP
) {
495 /* SKB completed but not sent */
503 xs
->tx
->queue_empty_descs
++;
507 if (xsk_tx_writeable(xs
))
508 sk
->sk_write_space(sk
);
510 mutex_unlock(&xs
->mutex
);
514 static int __xsk_sendmsg(struct sock
*sk
)
516 struct xdp_sock
*xs
= xdp_sk(sk
);
518 if (unlikely(!(xs
->dev
->flags
& IFF_UP
)))
520 if (unlikely(!xs
->tx
))
523 return xs
->zc
? xsk_zc_xmit(xs
) : xsk_generic_xmit(sk
);
526 static bool xsk_no_wakeup(struct sock
*sk
)
528 #ifdef CONFIG_NET_RX_BUSY_POLL
529 /* Prefer busy-polling, skip the wakeup. */
530 return READ_ONCE(sk
->sk_prefer_busy_poll
) && READ_ONCE(sk
->sk_ll_usec
) &&
531 READ_ONCE(sk
->sk_napi_id
) >= MIN_NAPI_ID
;
537 static int xsk_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t total_len
)
539 bool need_wait
= !(m
->msg_flags
& MSG_DONTWAIT
);
540 struct sock
*sk
= sock
->sk
;
541 struct xdp_sock
*xs
= xdp_sk(sk
);
542 struct xsk_buff_pool
*pool
;
544 if (unlikely(!xsk_is_bound(xs
)))
546 if (unlikely(need_wait
))
549 if (sk_can_busy_loop(sk
))
550 sk_busy_loop(sk
, 1); /* only support non-blocking sockets */
552 if (xsk_no_wakeup(sk
))
556 if (pool
->cached_need_wakeup
& XDP_WAKEUP_TX
)
557 return __xsk_sendmsg(sk
);
561 static int xsk_recvmsg(struct socket
*sock
, struct msghdr
*m
, size_t len
, int flags
)
563 bool need_wait
= !(flags
& MSG_DONTWAIT
);
564 struct sock
*sk
= sock
->sk
;
565 struct xdp_sock
*xs
= xdp_sk(sk
);
567 if (unlikely(!xsk_is_bound(xs
)))
569 if (unlikely(!(xs
->dev
->flags
& IFF_UP
)))
571 if (unlikely(!xs
->rx
))
573 if (unlikely(need_wait
))
576 if (sk_can_busy_loop(sk
))
577 sk_busy_loop(sk
, 1); /* only support non-blocking sockets */
579 if (xsk_no_wakeup(sk
))
582 if (xs
->pool
->cached_need_wakeup
& XDP_WAKEUP_RX
&& xs
->zc
)
583 return xsk_wakeup(xs
, XDP_WAKEUP_RX
);
587 static __poll_t
xsk_poll(struct file
*file
, struct socket
*sock
,
588 struct poll_table_struct
*wait
)
591 struct sock
*sk
= sock
->sk
;
592 struct xdp_sock
*xs
= xdp_sk(sk
);
593 struct xsk_buff_pool
*pool
;
595 sock_poll_wait(file
, sock
, wait
);
597 if (unlikely(!xsk_is_bound(xs
)))
602 if (pool
->cached_need_wakeup
) {
604 xsk_wakeup(xs
, pool
->cached_need_wakeup
);
606 /* Poll needs to drive Tx also in copy mode */
610 if (xs
->rx
&& !xskq_prod_is_empty(xs
->rx
))
611 mask
|= EPOLLIN
| EPOLLRDNORM
;
612 if (xs
->tx
&& xsk_tx_writeable(xs
))
613 mask
|= EPOLLOUT
| EPOLLWRNORM
;
618 static int xsk_init_queue(u32 entries
, struct xsk_queue
**queue
,
623 if (entries
== 0 || *queue
|| !is_power_of_2(entries
))
626 q
= xskq_create(entries
, umem_queue
);
630 /* Make sure queue is ready before it can be seen by others */
632 WRITE_ONCE(*queue
, q
);
636 static void xsk_unbind_dev(struct xdp_sock
*xs
)
638 struct net_device
*dev
= xs
->dev
;
640 if (xs
->state
!= XSK_BOUND
)
642 WRITE_ONCE(xs
->state
, XSK_UNBOUND
);
644 /* Wait for driver to stop using the xdp socket. */
645 xp_del_xsk(xs
->pool
, xs
);
651 static struct xsk_map
*xsk_get_map_list_entry(struct xdp_sock
*xs
,
652 struct xdp_sock
***map_entry
)
654 struct xsk_map
*map
= NULL
;
655 struct xsk_map_node
*node
;
659 spin_lock_bh(&xs
->map_list_lock
);
660 node
= list_first_entry_or_null(&xs
->map_list
, struct xsk_map_node
,
663 bpf_map_inc(&node
->map
->map
);
665 *map_entry
= node
->map_entry
;
667 spin_unlock_bh(&xs
->map_list_lock
);
671 static void xsk_delete_from_maps(struct xdp_sock
*xs
)
673 /* This function removes the current XDP socket from all the
674 * maps it resides in. We need to take extra care here, due to
675 * the two locks involved. Each map has a lock synchronizing
676 * updates to the entries, and each socket has a lock that
677 * synchronizes access to the list of maps (map_list). For
678 * deadlock avoidance the locks need to be taken in the order
679 * "map lock"->"socket map list lock". We start off by
680 * accessing the socket map list, and take a reference to the
681 * map to guarantee existence between the
682 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
683 * calls. Then we ask the map to remove the socket, which
684 * tries to remove the socket from the map. Note that there
685 * might be updates to the map between
686 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
688 struct xdp_sock
**map_entry
= NULL
;
691 while ((map
= xsk_get_map_list_entry(xs
, &map_entry
))) {
692 xsk_map_try_sock_delete(map
, xs
, map_entry
);
693 bpf_map_put(&map
->map
);
697 static int xsk_release(struct socket
*sock
)
699 struct sock
*sk
= sock
->sk
;
700 struct xdp_sock
*xs
= xdp_sk(sk
);
708 mutex_lock(&net
->xdp
.lock
);
709 sk_del_node_init_rcu(sk
);
710 mutex_unlock(&net
->xdp
.lock
);
713 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
716 xsk_delete_from_maps(xs
);
717 mutex_lock(&xs
->mutex
);
719 mutex_unlock(&xs
->mutex
);
721 xskq_destroy(xs
->rx
);
722 xskq_destroy(xs
->tx
);
723 xskq_destroy(xs
->fq_tmp
);
724 xskq_destroy(xs
->cq_tmp
);
729 sk_refcnt_debug_release(sk
);
735 static struct socket
*xsk_lookup_xsk_from_fd(int fd
)
740 sock
= sockfd_lookup(fd
, &err
);
742 return ERR_PTR(-ENOTSOCK
);
744 if (sock
->sk
->sk_family
!= PF_XDP
) {
746 return ERR_PTR(-ENOPROTOOPT
);
752 static bool xsk_validate_queues(struct xdp_sock
*xs
)
754 return xs
->fq_tmp
&& xs
->cq_tmp
;
757 static int xsk_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
759 struct sockaddr_xdp
*sxdp
= (struct sockaddr_xdp
*)addr
;
760 struct sock
*sk
= sock
->sk
;
761 struct xdp_sock
*xs
= xdp_sk(sk
);
762 struct net_device
*dev
;
766 if (addr_len
< sizeof(struct sockaddr_xdp
))
768 if (sxdp
->sxdp_family
!= AF_XDP
)
771 flags
= sxdp
->sxdp_flags
;
772 if (flags
& ~(XDP_SHARED_UMEM
| XDP_COPY
| XDP_ZEROCOPY
|
773 XDP_USE_NEED_WAKEUP
))
777 mutex_lock(&xs
->mutex
);
778 if (xs
->state
!= XSK_READY
) {
783 dev
= dev_get_by_index(sock_net(sk
), sxdp
->sxdp_ifindex
);
789 if (!xs
->rx
&& !xs
->tx
) {
794 qid
= sxdp
->sxdp_queue_id
;
796 if (flags
& XDP_SHARED_UMEM
) {
797 struct xdp_sock
*umem_xs
;
800 if ((flags
& XDP_COPY
) || (flags
& XDP_ZEROCOPY
) ||
801 (flags
& XDP_USE_NEED_WAKEUP
)) {
802 /* Cannot specify flags for shared sockets. */
808 /* We have already our own. */
813 sock
= xsk_lookup_xsk_from_fd(sxdp
->sxdp_shared_umem_fd
);
819 umem_xs
= xdp_sk(sock
->sk
);
820 if (!xsk_is_bound(umem_xs
)) {
826 if (umem_xs
->queue_id
!= qid
|| umem_xs
->dev
!= dev
) {
827 /* Share the umem with another socket on another qid
830 xs
->pool
= xp_create_and_assign_umem(xs
,
838 err
= xp_assign_dev_shared(xs
->pool
, umem_xs
->umem
,
841 xp_destroy(xs
->pool
);
847 /* Share the buffer pool with the other socket. */
848 if (xs
->fq_tmp
|| xs
->cq_tmp
) {
849 /* Do not allow setting your own fq or cq. */
855 xp_get_pool(umem_xs
->pool
);
856 xs
->pool
= umem_xs
->pool
;
859 xdp_get_umem(umem_xs
->umem
);
860 WRITE_ONCE(xs
->umem
, umem_xs
->umem
);
862 } else if (!xs
->umem
|| !xsk_validate_queues(xs
)) {
866 /* This xsk has its own umem. */
867 xs
->pool
= xp_create_and_assign_umem(xs
, xs
->umem
);
873 err
= xp_assign_dev(xs
->pool
, dev
, qid
, flags
);
875 xp_destroy(xs
->pool
);
882 xs
->zc
= xs
->umem
->zc
;
884 xp_add_xsk(xs
->pool
, xs
);
890 /* Matches smp_rmb() in bind() for shared umem
891 * sockets, and xsk_is_bound().
894 WRITE_ONCE(xs
->state
, XSK_BOUND
);
897 mutex_unlock(&xs
->mutex
);
902 struct xdp_umem_reg_v1
{
903 __u64 addr
; /* Start of packet data area */
904 __u64 len
; /* Length of packet data area */
909 static int xsk_setsockopt(struct socket
*sock
, int level
, int optname
,
910 sockptr_t optval
, unsigned int optlen
)
912 struct sock
*sk
= sock
->sk
;
913 struct xdp_sock
*xs
= xdp_sk(sk
);
916 if (level
!= SOL_XDP
)
923 struct xsk_queue
**q
;
926 if (optlen
< sizeof(entries
))
928 if (copy_from_sockptr(&entries
, optval
, sizeof(entries
)))
931 mutex_lock(&xs
->mutex
);
932 if (xs
->state
!= XSK_READY
) {
933 mutex_unlock(&xs
->mutex
);
936 q
= (optname
== XDP_TX_RING
) ? &xs
->tx
: &xs
->rx
;
937 err
= xsk_init_queue(entries
, q
, false);
938 if (!err
&& optname
== XDP_TX_RING
)
939 /* Tx needs to be explicitly woken up the first time */
940 xs
->tx
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
941 mutex_unlock(&xs
->mutex
);
946 size_t mr_size
= sizeof(struct xdp_umem_reg
);
947 struct xdp_umem_reg mr
= {};
948 struct xdp_umem
*umem
;
950 if (optlen
< sizeof(struct xdp_umem_reg_v1
))
952 else if (optlen
< sizeof(mr
))
953 mr_size
= sizeof(struct xdp_umem_reg_v1
);
955 if (copy_from_sockptr(&mr
, optval
, mr_size
))
958 mutex_lock(&xs
->mutex
);
959 if (xs
->state
!= XSK_READY
|| xs
->umem
) {
960 mutex_unlock(&xs
->mutex
);
964 umem
= xdp_umem_create(&mr
);
966 mutex_unlock(&xs
->mutex
);
967 return PTR_ERR(umem
);
970 /* Make sure umem is ready before it can be seen by others */
972 WRITE_ONCE(xs
->umem
, umem
);
973 mutex_unlock(&xs
->mutex
);
976 case XDP_UMEM_FILL_RING
:
977 case XDP_UMEM_COMPLETION_RING
:
979 struct xsk_queue
**q
;
982 if (copy_from_sockptr(&entries
, optval
, sizeof(entries
)))
985 mutex_lock(&xs
->mutex
);
986 if (xs
->state
!= XSK_READY
) {
987 mutex_unlock(&xs
->mutex
);
991 q
= (optname
== XDP_UMEM_FILL_RING
) ? &xs
->fq_tmp
:
993 err
= xsk_init_queue(entries
, q
, true);
994 mutex_unlock(&xs
->mutex
);
1001 return -ENOPROTOOPT
;
1004 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1
*ring
)
1006 ring
->producer
= offsetof(struct xdp_rxtx_ring
, ptrs
.producer
);
1007 ring
->consumer
= offsetof(struct xdp_rxtx_ring
, ptrs
.consumer
);
1008 ring
->desc
= offsetof(struct xdp_rxtx_ring
, desc
);
1011 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1
*ring
)
1013 ring
->producer
= offsetof(struct xdp_umem_ring
, ptrs
.producer
);
1014 ring
->consumer
= offsetof(struct xdp_umem_ring
, ptrs
.consumer
);
1015 ring
->desc
= offsetof(struct xdp_umem_ring
, desc
);
1018 struct xdp_statistics_v1
{
1020 __u64 rx_invalid_descs
;
1021 __u64 tx_invalid_descs
;
1024 static int xsk_getsockopt(struct socket
*sock
, int level
, int optname
,
1025 char __user
*optval
, int __user
*optlen
)
1027 struct sock
*sk
= sock
->sk
;
1028 struct xdp_sock
*xs
= xdp_sk(sk
);
1031 if (level
!= SOL_XDP
)
1032 return -ENOPROTOOPT
;
1034 if (get_user(len
, optlen
))
1040 case XDP_STATISTICS
:
1042 struct xdp_statistics stats
= {};
1043 bool extra_stats
= true;
1046 if (len
< sizeof(struct xdp_statistics_v1
)) {
1048 } else if (len
< sizeof(stats
)) {
1049 extra_stats
= false;
1050 stats_size
= sizeof(struct xdp_statistics_v1
);
1052 stats_size
= sizeof(stats
);
1055 mutex_lock(&xs
->mutex
);
1056 stats
.rx_dropped
= xs
->rx_dropped
;
1058 stats
.rx_ring_full
= xs
->rx_queue_full
;
1059 stats
.rx_fill_ring_empty_descs
=
1060 xs
->pool
? xskq_nb_queue_empty_descs(xs
->pool
->fq
) : 0;
1061 stats
.tx_ring_empty_descs
= xskq_nb_queue_empty_descs(xs
->tx
);
1063 stats
.rx_dropped
+= xs
->rx_queue_full
;
1065 stats
.rx_invalid_descs
= xskq_nb_invalid_descs(xs
->rx
);
1066 stats
.tx_invalid_descs
= xskq_nb_invalid_descs(xs
->tx
);
1067 mutex_unlock(&xs
->mutex
);
1069 if (copy_to_user(optval
, &stats
, stats_size
))
1071 if (put_user(stats_size
, optlen
))
1076 case XDP_MMAP_OFFSETS
:
1078 struct xdp_mmap_offsets off
;
1079 struct xdp_mmap_offsets_v1 off_v1
;
1080 bool flags_supported
= true;
1083 if (len
< sizeof(off_v1
))
1085 else if (len
< sizeof(off
))
1086 flags_supported
= false;
1088 if (flags_supported
) {
1089 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1090 * except for the flags field added to the end.
1092 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
1094 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
1096 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
1098 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
1100 off
.rx
.flags
= offsetof(struct xdp_rxtx_ring
,
1102 off
.tx
.flags
= offsetof(struct xdp_rxtx_ring
,
1104 off
.fr
.flags
= offsetof(struct xdp_umem_ring
,
1106 off
.cr
.flags
= offsetof(struct xdp_umem_ring
,
1112 xsk_enter_rxtx_offsets(&off_v1
.rx
);
1113 xsk_enter_rxtx_offsets(&off_v1
.tx
);
1114 xsk_enter_umem_offsets(&off_v1
.fr
);
1115 xsk_enter_umem_offsets(&off_v1
.cr
);
1117 len
= sizeof(off_v1
);
1121 if (copy_to_user(optval
, to_copy
, len
))
1123 if (put_user(len
, optlen
))
1130 struct xdp_options opts
= {};
1132 if (len
< sizeof(opts
))
1135 mutex_lock(&xs
->mutex
);
1137 opts
.flags
|= XDP_OPTIONS_ZEROCOPY
;
1138 mutex_unlock(&xs
->mutex
);
1141 if (copy_to_user(optval
, &opts
, len
))
1143 if (put_user(len
, optlen
))
1155 static int xsk_mmap(struct file
*file
, struct socket
*sock
,
1156 struct vm_area_struct
*vma
)
1158 loff_t offset
= (loff_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
1159 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1160 struct xdp_sock
*xs
= xdp_sk(sock
->sk
);
1161 struct xsk_queue
*q
= NULL
;
1165 if (READ_ONCE(xs
->state
) != XSK_READY
)
1168 if (offset
== XDP_PGOFF_RX_RING
) {
1169 q
= READ_ONCE(xs
->rx
);
1170 } else if (offset
== XDP_PGOFF_TX_RING
) {
1171 q
= READ_ONCE(xs
->tx
);
1173 /* Matches the smp_wmb() in XDP_UMEM_REG */
1175 if (offset
== XDP_UMEM_PGOFF_FILL_RING
)
1176 q
= READ_ONCE(xs
->fq_tmp
);
1177 else if (offset
== XDP_UMEM_PGOFF_COMPLETION_RING
)
1178 q
= READ_ONCE(xs
->cq_tmp
);
1184 /* Matches the smp_wmb() in xsk_init_queue */
1186 qpg
= virt_to_head_page(q
->ring
);
1187 if (size
> page_size(qpg
))
1190 pfn
= virt_to_phys(q
->ring
) >> PAGE_SHIFT
;
1191 return remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1192 size
, vma
->vm_page_prot
);
1195 static int xsk_notifier(struct notifier_block
*this,
1196 unsigned long msg
, void *ptr
)
1198 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1199 struct net
*net
= dev_net(dev
);
1203 case NETDEV_UNREGISTER
:
1204 mutex_lock(&net
->xdp
.lock
);
1205 sk_for_each(sk
, &net
->xdp
.list
) {
1206 struct xdp_sock
*xs
= xdp_sk(sk
);
1208 mutex_lock(&xs
->mutex
);
1209 if (xs
->dev
== dev
) {
1210 sk
->sk_err
= ENETDOWN
;
1211 if (!sock_flag(sk
, SOCK_DEAD
))
1212 sk
->sk_error_report(sk
);
1216 /* Clear device references. */
1217 xp_clear_dev(xs
->pool
);
1219 mutex_unlock(&xs
->mutex
);
1221 mutex_unlock(&net
->xdp
.lock
);
1227 static struct proto xsk_proto
= {
1229 .owner
= THIS_MODULE
,
1230 .obj_size
= sizeof(struct xdp_sock
),
1233 static const struct proto_ops xsk_proto_ops
= {
1235 .owner
= THIS_MODULE
,
1236 .release
= xsk_release
,
1238 .connect
= sock_no_connect
,
1239 .socketpair
= sock_no_socketpair
,
1240 .accept
= sock_no_accept
,
1241 .getname
= sock_no_getname
,
1243 .ioctl
= sock_no_ioctl
,
1244 .listen
= sock_no_listen
,
1245 .shutdown
= sock_no_shutdown
,
1246 .setsockopt
= xsk_setsockopt
,
1247 .getsockopt
= xsk_getsockopt
,
1248 .sendmsg
= xsk_sendmsg
,
1249 .recvmsg
= xsk_recvmsg
,
1251 .sendpage
= sock_no_sendpage
,
1254 static void xsk_destruct(struct sock
*sk
)
1256 struct xdp_sock
*xs
= xdp_sk(sk
);
1258 if (!sock_flag(sk
, SOCK_DEAD
))
1261 if (!xp_put_pool(xs
->pool
))
1262 xdp_put_umem(xs
->umem
, !xs
->pool
);
1264 sk_refcnt_debug_dec(sk
);
1267 static int xsk_create(struct net
*net
, struct socket
*sock
, int protocol
,
1270 struct xdp_sock
*xs
;
1273 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
))
1275 if (sock
->type
!= SOCK_RAW
)
1276 return -ESOCKTNOSUPPORT
;
1279 return -EPROTONOSUPPORT
;
1281 sock
->state
= SS_UNCONNECTED
;
1283 sk
= sk_alloc(net
, PF_XDP
, GFP_KERNEL
, &xsk_proto
, kern
);
1287 sock
->ops
= &xsk_proto_ops
;
1289 sock_init_data(sock
, sk
);
1291 sk
->sk_family
= PF_XDP
;
1293 sk
->sk_destruct
= xsk_destruct
;
1294 sk_refcnt_debug_inc(sk
);
1296 sock_set_flag(sk
, SOCK_RCU_FREE
);
1299 xs
->state
= XSK_READY
;
1300 mutex_init(&xs
->mutex
);
1301 spin_lock_init(&xs
->rx_lock
);
1302 spin_lock_init(&xs
->tx_completion_lock
);
1304 INIT_LIST_HEAD(&xs
->map_list
);
1305 spin_lock_init(&xs
->map_list_lock
);
1307 mutex_lock(&net
->xdp
.lock
);
1308 sk_add_node_rcu(sk
, &net
->xdp
.list
);
1309 mutex_unlock(&net
->xdp
.lock
);
1312 sock_prot_inuse_add(net
, &xsk_proto
, 1);
1318 static const struct net_proto_family xsk_family_ops
= {
1320 .create
= xsk_create
,
1321 .owner
= THIS_MODULE
,
1324 static struct notifier_block xsk_netdev_notifier
= {
1325 .notifier_call
= xsk_notifier
,
1328 static int __net_init
xsk_net_init(struct net
*net
)
1330 mutex_init(&net
->xdp
.lock
);
1331 INIT_HLIST_HEAD(&net
->xdp
.list
);
1335 static void __net_exit
xsk_net_exit(struct net
*net
)
1337 WARN_ON_ONCE(!hlist_empty(&net
->xdp
.list
));
1340 static struct pernet_operations xsk_net_ops
= {
1341 .init
= xsk_net_init
,
1342 .exit
= xsk_net_exit
,
1345 static int __init
xsk_init(void)
1349 err
= proto_register(&xsk_proto
, 0 /* no slab */);
1353 err
= sock_register(&xsk_family_ops
);
1357 err
= register_pernet_subsys(&xsk_net_ops
);
1361 err
= register_netdevice_notifier(&xsk_netdev_notifier
);
1365 for_each_possible_cpu(cpu
)
1366 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list
, cpu
));
1370 unregister_pernet_subsys(&xsk_net_ops
);
1372 sock_unregister(PF_XDP
);
1374 proto_unregister(&xsk_proto
);
1379 fs_initcall(xsk_init
);