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.h>
28 #include "xsk_queue.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
)
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
)
74 if (umem
->need_wakeup
& XDP_WAKEUP_TX
)
78 list_for_each_entry_rcu(xs
, &umem
->xsk_list
, list
) {
79 xs
->tx
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
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
))
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
)
101 if (!(umem
->need_wakeup
& XDP_WAKEUP_TX
))
105 list_for_each_entry_rcu(xs
, &umem
->xsk_list
, list
) {
106 xs
->tx
->ring
->flags
&= ~XDP_RING_NEED_WAKEUP
;
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
);
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
;
152 if (!xskq_cons_peek_addr(xs
->umem
->fq
, &addr
, xs
->umem
) ||
153 len
> xs
->umem
->chunk_size_nohr
- XDP_PACKET_HEADROOM
) {
158 if (unlikely(xdp_data_meta_unsupported(xdp
))) {
159 from_buf
= xdp
->data
;
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
);
170 addr
= xsk_umem_adjust_offset(xs
->umem
, addr
, offset
);
171 err
= xskq_prod_reserve_desc(xs
->rx
, addr
, len
);
173 xskq_cons_release(xs
->umem
->fq
);
174 xdp_return_buff(xdp
);
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
);
192 static bool xsk_is_bound(struct xdp_sock
*xs
)
194 if (READ_ONCE(xs
->state
) == XSK_BOUND
) {
195 /* Matches smp_wmb() in bind(). */
202 static int xsk_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
)
206 if (!xsk_is_bound(xs
))
209 if (xs
->dev
!= xdp
->rxq
->dev
|| xs
->queue_id
!= xdp
->rxq
->queue_index
)
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
;
234 spin_lock_bh(&xs
->rx_lock
);
236 if (xs
->dev
!= xdp
->rxq
->dev
|| xs
->queue_id
!= xdp
->rxq
->queue_index
) {
241 if (!xskq_cons_peek_addr(xs
->umem
->fq
, &addr
, xs
->umem
) ||
242 len
> xs
->umem
->chunk_size_nohr
- XDP_PACKET_HEADROOM
) {
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
);
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
);
267 spin_unlock_bh(&xs
->rx_lock
);
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
);
276 err
= xsk_rcv(xs
, xdp
);
280 if (!xs
->flush_node
.prev
)
281 list_add(&xs
->flush_node
, flush_list
);
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
) {
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
)
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
);
314 EXPORT_SYMBOL(xsk_umem_consume_tx_done
);
316 bool xsk_umem_consume_tx(struct xdp_umem
*umem
, struct xdp_desc
*desc
)
321 list_for_each_entry_rcu(xs
, &umem
->xsk_list
, list
) {
322 if (!xskq_cons_peek_desc(xs
->tx
, desc
, umem
))
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
))
333 xskq_cons_release(xs
->tx
);
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
;
350 err
= dev
->netdev_ops
->ndo_xsk_wakeup(dev
, xs
->queue_id
, flags
);
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
);
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
);
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
;
383 mutex_lock(&xs
->mutex
);
385 if (xs
->queue_id
>= xs
->dev
->real_num_tx_queues
)
388 while (xskq_cons_peek_desc(xs
->tx
, &desc
, xs
->umem
)) {
393 if (max_batch
-- == 0) {
399 skb
= sock_alloc_send_skb(sk
, len
, 1, &err
);
400 if (unlikely(!skb
)) {
407 buffer
= xdp_umem_get_data(xs
->umem
, addr
);
408 err
= skb_store_bits(skb
, 0, buffer
, len
);
409 /* This is the backpreassure mechanism for the Tx path.
410 * Reserve space in the completion queue and only proceed
411 * if there is space in it. This avoids having to implement
412 * any buffering in the Tx path.
414 if (unlikely(err
) || xskq_prod_reserve(xs
->umem
->cq
)) {
420 skb
->priority
= sk
->sk_priority
;
421 skb
->mark
= sk
->sk_mark
;
422 skb_shinfo(skb
)->destructor_arg
= (void *)(long)desc
.addr
;
423 skb
->destructor
= xsk_destruct_skb
;
425 err
= dev_direct_xmit(skb
, xs
->queue_id
);
426 xskq_cons_release(xs
->tx
);
427 /* Ignore NET_XMIT_CN as packet might have been sent */
428 if (err
== NET_XMIT_DROP
|| err
== NETDEV_TX_BUSY
) {
429 /* SKB completed but not sent */
439 sk
->sk_write_space(sk
);
441 mutex_unlock(&xs
->mutex
);
445 static int __xsk_sendmsg(struct sock
*sk
)
447 struct xdp_sock
*xs
= xdp_sk(sk
);
449 if (unlikely(!(xs
->dev
->flags
& IFF_UP
)))
451 if (unlikely(!xs
->tx
))
454 return xs
->zc
? xsk_zc_xmit(xs
) : xsk_generic_xmit(sk
);
457 static int xsk_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t total_len
)
459 bool need_wait
= !(m
->msg_flags
& MSG_DONTWAIT
);
460 struct sock
*sk
= sock
->sk
;
461 struct xdp_sock
*xs
= xdp_sk(sk
);
463 if (unlikely(!xsk_is_bound(xs
)))
465 if (unlikely(need_wait
))
468 return __xsk_sendmsg(sk
);
471 static __poll_t
xsk_poll(struct file
*file
, struct socket
*sock
,
472 struct poll_table_struct
*wait
)
474 __poll_t mask
= datagram_poll(file
, sock
, wait
);
475 struct sock
*sk
= sock
->sk
;
476 struct xdp_sock
*xs
= xdp_sk(sk
);
477 struct xdp_umem
*umem
;
479 if (unlikely(!xsk_is_bound(xs
)))
484 if (umem
->need_wakeup
) {
486 xsk_wakeup(xs
, umem
->need_wakeup
);
488 /* Poll needs to drive Tx also in copy mode */
492 if (xs
->rx
&& !xskq_prod_is_empty(xs
->rx
))
493 mask
|= EPOLLIN
| EPOLLRDNORM
;
494 if (xs
->tx
&& !xskq_cons_is_full(xs
->tx
))
495 mask
|= EPOLLOUT
| EPOLLWRNORM
;
500 static int xsk_init_queue(u32 entries
, struct xsk_queue
**queue
,
505 if (entries
== 0 || *queue
|| !is_power_of_2(entries
))
508 q
= xskq_create(entries
, umem_queue
);
512 /* Make sure queue is ready before it can be seen by others */
514 WRITE_ONCE(*queue
, q
);
518 static void xsk_unbind_dev(struct xdp_sock
*xs
)
520 struct net_device
*dev
= xs
->dev
;
522 if (xs
->state
!= XSK_BOUND
)
524 WRITE_ONCE(xs
->state
, XSK_UNBOUND
);
526 /* Wait for driver to stop using the xdp socket. */
527 xdp_del_sk_umem(xs
->umem
, xs
);
533 static struct xsk_map
*xsk_get_map_list_entry(struct xdp_sock
*xs
,
534 struct xdp_sock
***map_entry
)
536 struct xsk_map
*map
= NULL
;
537 struct xsk_map_node
*node
;
541 spin_lock_bh(&xs
->map_list_lock
);
542 node
= list_first_entry_or_null(&xs
->map_list
, struct xsk_map_node
,
545 WARN_ON(xsk_map_inc(node
->map
));
547 *map_entry
= node
->map_entry
;
549 spin_unlock_bh(&xs
->map_list_lock
);
553 static void xsk_delete_from_maps(struct xdp_sock
*xs
)
555 /* This function removes the current XDP socket from all the
556 * maps it resides in. We need to take extra care here, due to
557 * the two locks involved. Each map has a lock synchronizing
558 * updates to the entries, and each socket has a lock that
559 * synchronizes access to the list of maps (map_list). For
560 * deadlock avoidance the locks need to be taken in the order
561 * "map lock"->"socket map list lock". We start off by
562 * accessing the socket map list, and take a reference to the
563 * map to guarantee existence between the
564 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
565 * calls. Then we ask the map to remove the socket, which
566 * tries to remove the socket from the map. Note that there
567 * might be updates to the map between
568 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
570 struct xdp_sock
**map_entry
= NULL
;
573 while ((map
= xsk_get_map_list_entry(xs
, &map_entry
))) {
574 xsk_map_try_sock_delete(map
, xs
, map_entry
);
579 static int xsk_release(struct socket
*sock
)
581 struct sock
*sk
= sock
->sk
;
582 struct xdp_sock
*xs
= xdp_sk(sk
);
590 mutex_lock(&net
->xdp
.lock
);
591 sk_del_node_init_rcu(sk
);
592 mutex_unlock(&net
->xdp
.lock
);
595 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
598 xsk_delete_from_maps(xs
);
599 mutex_lock(&xs
->mutex
);
601 mutex_unlock(&xs
->mutex
);
603 xskq_destroy(xs
->rx
);
604 xskq_destroy(xs
->tx
);
609 sk_refcnt_debug_release(sk
);
615 static struct socket
*xsk_lookup_xsk_from_fd(int fd
)
620 sock
= sockfd_lookup(fd
, &err
);
622 return ERR_PTR(-ENOTSOCK
);
624 if (sock
->sk
->sk_family
!= PF_XDP
) {
626 return ERR_PTR(-ENOPROTOOPT
);
632 /* Check if umem pages are contiguous.
633 * If zero-copy mode, use the DMA address to do the page contiguity check
634 * For all other modes we use addr (kernel virtual address)
635 * Store the result in the low bits of addr.
637 static void xsk_check_page_contiguity(struct xdp_umem
*umem
, u32 flags
)
639 struct xdp_umem_page
*pgs
= umem
->pages
;
642 for (i
= 0; i
< umem
->npgs
- 1; i
++) {
643 is_contig
= (flags
& XDP_ZEROCOPY
) ?
644 (pgs
[i
].dma
+ PAGE_SIZE
== pgs
[i
+ 1].dma
) :
645 (pgs
[i
].addr
+ PAGE_SIZE
== pgs
[i
+ 1].addr
);
646 pgs
[i
].addr
+= is_contig
<< XSK_NEXT_PG_CONTIG_SHIFT
;
650 static int xsk_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
652 struct sockaddr_xdp
*sxdp
= (struct sockaddr_xdp
*)addr
;
653 struct sock
*sk
= sock
->sk
;
654 struct xdp_sock
*xs
= xdp_sk(sk
);
655 struct net_device
*dev
;
659 if (addr_len
< sizeof(struct sockaddr_xdp
))
661 if (sxdp
->sxdp_family
!= AF_XDP
)
664 flags
= sxdp
->sxdp_flags
;
665 if (flags
& ~(XDP_SHARED_UMEM
| XDP_COPY
| XDP_ZEROCOPY
|
666 XDP_USE_NEED_WAKEUP
))
670 mutex_lock(&xs
->mutex
);
671 if (xs
->state
!= XSK_READY
) {
676 dev
= dev_get_by_index(sock_net(sk
), sxdp
->sxdp_ifindex
);
682 if (!xs
->rx
&& !xs
->tx
) {
687 qid
= sxdp
->sxdp_queue_id
;
689 if (flags
& XDP_SHARED_UMEM
) {
690 struct xdp_sock
*umem_xs
;
693 if ((flags
& XDP_COPY
) || (flags
& XDP_ZEROCOPY
) ||
694 (flags
& XDP_USE_NEED_WAKEUP
)) {
695 /* Cannot specify flags for shared sockets. */
701 /* We have already our own. */
706 sock
= xsk_lookup_xsk_from_fd(sxdp
->sxdp_shared_umem_fd
);
712 umem_xs
= xdp_sk(sock
->sk
);
713 if (!xsk_is_bound(umem_xs
)) {
718 if (umem_xs
->dev
!= dev
|| umem_xs
->queue_id
!= qid
) {
724 xdp_get_umem(umem_xs
->umem
);
725 WRITE_ONCE(xs
->umem
, umem_xs
->umem
);
727 } else if (!xs
->umem
|| !xdp_umem_validate_queues(xs
->umem
)) {
731 /* This xsk has its own umem. */
732 xskq_set_umem(xs
->umem
->fq
, xs
->umem
->size
,
733 xs
->umem
->chunk_mask
);
734 xskq_set_umem(xs
->umem
->cq
, xs
->umem
->size
,
735 xs
->umem
->chunk_mask
);
737 err
= xdp_umem_assign_dev(xs
->umem
, dev
, qid
, flags
);
741 xsk_check_page_contiguity(xs
->umem
, flags
);
745 xs
->zc
= xs
->umem
->zc
;
747 xskq_set_umem(xs
->rx
, xs
->umem
->size
, xs
->umem
->chunk_mask
);
748 xskq_set_umem(xs
->tx
, xs
->umem
->size
, xs
->umem
->chunk_mask
);
749 xdp_add_sk_umem(xs
->umem
, xs
);
755 /* Matches smp_rmb() in bind() for shared umem
756 * sockets, and xsk_is_bound().
759 WRITE_ONCE(xs
->state
, XSK_BOUND
);
762 mutex_unlock(&xs
->mutex
);
767 struct xdp_umem_reg_v1
{
768 __u64 addr
; /* Start of packet data area */
769 __u64 len
; /* Length of packet data area */
774 static int xsk_setsockopt(struct socket
*sock
, int level
, int optname
,
775 char __user
*optval
, unsigned int optlen
)
777 struct sock
*sk
= sock
->sk
;
778 struct xdp_sock
*xs
= xdp_sk(sk
);
781 if (level
!= SOL_XDP
)
788 struct xsk_queue
**q
;
791 if (optlen
< sizeof(entries
))
793 if (copy_from_user(&entries
, optval
, sizeof(entries
)))
796 mutex_lock(&xs
->mutex
);
797 if (xs
->state
!= XSK_READY
) {
798 mutex_unlock(&xs
->mutex
);
801 q
= (optname
== XDP_TX_RING
) ? &xs
->tx
: &xs
->rx
;
802 err
= xsk_init_queue(entries
, q
, false);
803 if (!err
&& optname
== XDP_TX_RING
)
804 /* Tx needs to be explicitly woken up the first time */
805 xs
->tx
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
806 mutex_unlock(&xs
->mutex
);
811 size_t mr_size
= sizeof(struct xdp_umem_reg
);
812 struct xdp_umem_reg mr
= {};
813 struct xdp_umem
*umem
;
815 if (optlen
< sizeof(struct xdp_umem_reg_v1
))
817 else if (optlen
< sizeof(mr
))
818 mr_size
= sizeof(struct xdp_umem_reg_v1
);
820 if (copy_from_user(&mr
, optval
, mr_size
))
823 mutex_lock(&xs
->mutex
);
824 if (xs
->state
!= XSK_READY
|| xs
->umem
) {
825 mutex_unlock(&xs
->mutex
);
829 umem
= xdp_umem_create(&mr
);
831 mutex_unlock(&xs
->mutex
);
832 return PTR_ERR(umem
);
835 /* Make sure umem is ready before it can be seen by others */
837 WRITE_ONCE(xs
->umem
, umem
);
838 mutex_unlock(&xs
->mutex
);
841 case XDP_UMEM_FILL_RING
:
842 case XDP_UMEM_COMPLETION_RING
:
844 struct xsk_queue
**q
;
847 if (copy_from_user(&entries
, optval
, sizeof(entries
)))
850 mutex_lock(&xs
->mutex
);
851 if (xs
->state
!= XSK_READY
) {
852 mutex_unlock(&xs
->mutex
);
856 mutex_unlock(&xs
->mutex
);
860 q
= (optname
== XDP_UMEM_FILL_RING
) ? &xs
->umem
->fq
:
862 err
= xsk_init_queue(entries
, q
, true);
863 mutex_unlock(&xs
->mutex
);
873 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1
*ring
)
875 ring
->producer
= offsetof(struct xdp_rxtx_ring
, ptrs
.producer
);
876 ring
->consumer
= offsetof(struct xdp_rxtx_ring
, ptrs
.consumer
);
877 ring
->desc
= offsetof(struct xdp_rxtx_ring
, desc
);
880 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1
*ring
)
882 ring
->producer
= offsetof(struct xdp_umem_ring
, ptrs
.producer
);
883 ring
->consumer
= offsetof(struct xdp_umem_ring
, ptrs
.consumer
);
884 ring
->desc
= offsetof(struct xdp_umem_ring
, desc
);
887 static int xsk_getsockopt(struct socket
*sock
, int level
, int optname
,
888 char __user
*optval
, int __user
*optlen
)
890 struct sock
*sk
= sock
->sk
;
891 struct xdp_sock
*xs
= xdp_sk(sk
);
894 if (level
!= SOL_XDP
)
897 if (get_user(len
, optlen
))
905 struct xdp_statistics stats
;
907 if (len
< sizeof(stats
))
910 mutex_lock(&xs
->mutex
);
911 stats
.rx_dropped
= xs
->rx_dropped
;
912 stats
.rx_invalid_descs
= xskq_nb_invalid_descs(xs
->rx
);
913 stats
.tx_invalid_descs
= xskq_nb_invalid_descs(xs
->tx
);
914 mutex_unlock(&xs
->mutex
);
916 if (copy_to_user(optval
, &stats
, sizeof(stats
)))
918 if (put_user(sizeof(stats
), optlen
))
923 case XDP_MMAP_OFFSETS
:
925 struct xdp_mmap_offsets off
;
926 struct xdp_mmap_offsets_v1 off_v1
;
927 bool flags_supported
= true;
930 if (len
< sizeof(off_v1
))
932 else if (len
< sizeof(off
))
933 flags_supported
= false;
935 if (flags_supported
) {
936 /* xdp_ring_offset is identical to xdp_ring_offset_v1
937 * except for the flags field added to the end.
939 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
941 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
943 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
945 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
947 off
.rx
.flags
= offsetof(struct xdp_rxtx_ring
,
949 off
.tx
.flags
= offsetof(struct xdp_rxtx_ring
,
951 off
.fr
.flags
= offsetof(struct xdp_umem_ring
,
953 off
.cr
.flags
= offsetof(struct xdp_umem_ring
,
959 xsk_enter_rxtx_offsets(&off_v1
.rx
);
960 xsk_enter_rxtx_offsets(&off_v1
.tx
);
961 xsk_enter_umem_offsets(&off_v1
.fr
);
962 xsk_enter_umem_offsets(&off_v1
.cr
);
964 len
= sizeof(off_v1
);
968 if (copy_to_user(optval
, to_copy
, len
))
970 if (put_user(len
, optlen
))
977 struct xdp_options opts
= {};
979 if (len
< sizeof(opts
))
982 mutex_lock(&xs
->mutex
);
984 opts
.flags
|= XDP_OPTIONS_ZEROCOPY
;
985 mutex_unlock(&xs
->mutex
);
988 if (copy_to_user(optval
, &opts
, len
))
990 if (put_user(len
, optlen
))
1002 static int xsk_mmap(struct file
*file
, struct socket
*sock
,
1003 struct vm_area_struct
*vma
)
1005 loff_t offset
= (loff_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
1006 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1007 struct xdp_sock
*xs
= xdp_sk(sock
->sk
);
1008 struct xsk_queue
*q
= NULL
;
1009 struct xdp_umem
*umem
;
1013 if (READ_ONCE(xs
->state
) != XSK_READY
)
1016 if (offset
== XDP_PGOFF_RX_RING
) {
1017 q
= READ_ONCE(xs
->rx
);
1018 } else if (offset
== XDP_PGOFF_TX_RING
) {
1019 q
= READ_ONCE(xs
->tx
);
1021 umem
= READ_ONCE(xs
->umem
);
1025 /* Matches the smp_wmb() in XDP_UMEM_REG */
1027 if (offset
== XDP_UMEM_PGOFF_FILL_RING
)
1028 q
= READ_ONCE(umem
->fq
);
1029 else if (offset
== XDP_UMEM_PGOFF_COMPLETION_RING
)
1030 q
= READ_ONCE(umem
->cq
);
1036 /* Matches the smp_wmb() in xsk_init_queue */
1038 qpg
= virt_to_head_page(q
->ring
);
1039 if (size
> page_size(qpg
))
1042 pfn
= virt_to_phys(q
->ring
) >> PAGE_SHIFT
;
1043 return remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1044 size
, vma
->vm_page_prot
);
1047 static int xsk_notifier(struct notifier_block
*this,
1048 unsigned long msg
, void *ptr
)
1050 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1051 struct net
*net
= dev_net(dev
);
1055 case NETDEV_UNREGISTER
:
1056 mutex_lock(&net
->xdp
.lock
);
1057 sk_for_each(sk
, &net
->xdp
.list
) {
1058 struct xdp_sock
*xs
= xdp_sk(sk
);
1060 mutex_lock(&xs
->mutex
);
1061 if (xs
->dev
== dev
) {
1062 sk
->sk_err
= ENETDOWN
;
1063 if (!sock_flag(sk
, SOCK_DEAD
))
1064 sk
->sk_error_report(sk
);
1068 /* Clear device references in umem. */
1069 xdp_umem_clear_dev(xs
->umem
);
1071 mutex_unlock(&xs
->mutex
);
1073 mutex_unlock(&net
->xdp
.lock
);
1079 static struct proto xsk_proto
= {
1081 .owner
= THIS_MODULE
,
1082 .obj_size
= sizeof(struct xdp_sock
),
1085 static const struct proto_ops xsk_proto_ops
= {
1087 .owner
= THIS_MODULE
,
1088 .release
= xsk_release
,
1090 .connect
= sock_no_connect
,
1091 .socketpair
= sock_no_socketpair
,
1092 .accept
= sock_no_accept
,
1093 .getname
= sock_no_getname
,
1095 .ioctl
= sock_no_ioctl
,
1096 .listen
= sock_no_listen
,
1097 .shutdown
= sock_no_shutdown
,
1098 .setsockopt
= xsk_setsockopt
,
1099 .getsockopt
= xsk_getsockopt
,
1100 .sendmsg
= xsk_sendmsg
,
1101 .recvmsg
= sock_no_recvmsg
,
1103 .sendpage
= sock_no_sendpage
,
1106 static void xsk_destruct(struct sock
*sk
)
1108 struct xdp_sock
*xs
= xdp_sk(sk
);
1110 if (!sock_flag(sk
, SOCK_DEAD
))
1113 xdp_put_umem(xs
->umem
);
1115 sk_refcnt_debug_dec(sk
);
1118 static int xsk_create(struct net
*net
, struct socket
*sock
, int protocol
,
1122 struct xdp_sock
*xs
;
1124 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
))
1126 if (sock
->type
!= SOCK_RAW
)
1127 return -ESOCKTNOSUPPORT
;
1130 return -EPROTONOSUPPORT
;
1132 sock
->state
= SS_UNCONNECTED
;
1134 sk
= sk_alloc(net
, PF_XDP
, GFP_KERNEL
, &xsk_proto
, kern
);
1138 sock
->ops
= &xsk_proto_ops
;
1140 sock_init_data(sock
, sk
);
1142 sk
->sk_family
= PF_XDP
;
1144 sk
->sk_destruct
= xsk_destruct
;
1145 sk_refcnt_debug_inc(sk
);
1147 sock_set_flag(sk
, SOCK_RCU_FREE
);
1150 xs
->state
= XSK_READY
;
1151 mutex_init(&xs
->mutex
);
1152 spin_lock_init(&xs
->rx_lock
);
1153 spin_lock_init(&xs
->tx_completion_lock
);
1155 INIT_LIST_HEAD(&xs
->map_list
);
1156 spin_lock_init(&xs
->map_list_lock
);
1158 mutex_lock(&net
->xdp
.lock
);
1159 sk_add_node_rcu(sk
, &net
->xdp
.list
);
1160 mutex_unlock(&net
->xdp
.lock
);
1163 sock_prot_inuse_add(net
, &xsk_proto
, 1);
1169 static const struct net_proto_family xsk_family_ops
= {
1171 .create
= xsk_create
,
1172 .owner
= THIS_MODULE
,
1175 static struct notifier_block xsk_netdev_notifier
= {
1176 .notifier_call
= xsk_notifier
,
1179 static int __net_init
xsk_net_init(struct net
*net
)
1181 mutex_init(&net
->xdp
.lock
);
1182 INIT_HLIST_HEAD(&net
->xdp
.list
);
1186 static void __net_exit
xsk_net_exit(struct net
*net
)
1188 WARN_ON_ONCE(!hlist_empty(&net
->xdp
.list
));
1191 static struct pernet_operations xsk_net_ops
= {
1192 .init
= xsk_net_init
,
1193 .exit
= xsk_net_exit
,
1196 static int __init
xsk_init(void)
1200 err
= proto_register(&xsk_proto
, 0 /* no slab */);
1204 err
= sock_register(&xsk_family_ops
);
1208 err
= register_pernet_subsys(&xsk_net_ops
);
1212 err
= register_netdevice_notifier(&xsk_netdev_notifier
);
1216 for_each_possible_cpu(cpu
)
1217 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list
, cpu
));
1221 unregister_pernet_subsys(&xsk_net_ops
);
1223 sock_unregister(PF_XDP
);
1225 proto_unregister(&xsk_proto
);
1230 fs_initcall(xsk_init
);