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 <linux/vmalloc.h>
26 #include <net/xdp_sock_drv.h>
27 #include <net/busy_poll.h>
28 #include <net/netdev_rx_queue.h>
31 #include "xsk_queue.h"
35 #define TX_BATCH_SIZE 32
36 #define MAX_PER_SOCKET_BUDGET (TX_BATCH_SIZE)
38 void xsk_set_rx_need_wakeup(struct xsk_buff_pool
*pool
)
40 if (pool
->cached_need_wakeup
& XDP_WAKEUP_RX
)
43 pool
->fq
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
44 pool
->cached_need_wakeup
|= XDP_WAKEUP_RX
;
46 EXPORT_SYMBOL(xsk_set_rx_need_wakeup
);
48 void xsk_set_tx_need_wakeup(struct xsk_buff_pool
*pool
)
52 if (pool
->cached_need_wakeup
& XDP_WAKEUP_TX
)
56 list_for_each_entry_rcu(xs
, &pool
->xsk_tx_list
, tx_list
) {
57 xs
->tx
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
61 pool
->cached_need_wakeup
|= XDP_WAKEUP_TX
;
63 EXPORT_SYMBOL(xsk_set_tx_need_wakeup
);
65 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool
*pool
)
67 if (!(pool
->cached_need_wakeup
& XDP_WAKEUP_RX
))
70 pool
->fq
->ring
->flags
&= ~XDP_RING_NEED_WAKEUP
;
71 pool
->cached_need_wakeup
&= ~XDP_WAKEUP_RX
;
73 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup
);
75 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool
*pool
)
79 if (!(pool
->cached_need_wakeup
& XDP_WAKEUP_TX
))
83 list_for_each_entry_rcu(xs
, &pool
->xsk_tx_list
, tx_list
) {
84 xs
->tx
->ring
->flags
&= ~XDP_RING_NEED_WAKEUP
;
88 pool
->cached_need_wakeup
&= ~XDP_WAKEUP_TX
;
90 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup
);
92 bool xsk_uses_need_wakeup(struct xsk_buff_pool
*pool
)
94 return pool
->uses_need_wakeup
;
96 EXPORT_SYMBOL(xsk_uses_need_wakeup
);
98 struct xsk_buff_pool
*xsk_get_pool_from_qid(struct net_device
*dev
,
101 if (queue_id
< dev
->real_num_rx_queues
)
102 return dev
->_rx
[queue_id
].pool
;
103 if (queue_id
< dev
->real_num_tx_queues
)
104 return dev
->_tx
[queue_id
].pool
;
108 EXPORT_SYMBOL(xsk_get_pool_from_qid
);
110 void xsk_clear_pool_at_qid(struct net_device
*dev
, u16 queue_id
)
112 if (queue_id
< dev
->num_rx_queues
)
113 dev
->_rx
[queue_id
].pool
= NULL
;
114 if (queue_id
< dev
->num_tx_queues
)
115 dev
->_tx
[queue_id
].pool
= NULL
;
118 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
119 * not know if the device has more tx queues than rx, or the opposite.
120 * This might also change during run time.
122 int xsk_reg_pool_at_qid(struct net_device
*dev
, struct xsk_buff_pool
*pool
,
125 if (queue_id
>= max_t(unsigned int,
126 dev
->real_num_rx_queues
,
127 dev
->real_num_tx_queues
))
130 if (queue_id
< dev
->real_num_rx_queues
)
131 dev
->_rx
[queue_id
].pool
= pool
;
132 if (queue_id
< dev
->real_num_tx_queues
)
133 dev
->_tx
[queue_id
].pool
= pool
;
138 static int __xsk_rcv_zc(struct xdp_sock
*xs
, struct xdp_buff_xsk
*xskb
, u32 len
,
144 addr
= xp_get_handle(xskb
, xskb
->pool
);
145 err
= xskq_prod_reserve_desc(xs
->rx
, addr
, len
, flags
);
155 static int xsk_rcv_zc(struct xdp_sock
*xs
, struct xdp_buff
*xdp
, u32 len
)
157 struct xdp_buff_xsk
*xskb
= container_of(xdp
, struct xdp_buff_xsk
, xdp
);
158 u32 frags
= xdp_buff_has_frags(xdp
);
159 struct xdp_buff_xsk
*pos
, *tmp
;
160 struct list_head
*xskb_list
;
165 contd
= XDP_PKT_CONTD
;
167 err
= __xsk_rcv_zc(xs
, xskb
, len
, contd
);
173 xskb_list
= &xskb
->pool
->xskb_list
;
174 list_for_each_entry_safe(pos
, tmp
, xskb_list
, list_node
) {
175 if (list_is_singular(xskb_list
))
177 len
= pos
->xdp
.data_end
- pos
->xdp
.data
;
178 err
= __xsk_rcv_zc(xs
, pos
, len
, contd
);
181 list_del(&pos
->list_node
);
190 static void *xsk_copy_xdp_start(struct xdp_buff
*from
)
192 if (unlikely(xdp_data_meta_unsupported(from
)))
195 return from
->data_meta
;
198 static u32
xsk_copy_xdp(void *to
, void **from
, u32 to_len
,
199 u32
*from_len
, skb_frag_t
**frag
, u32 rem
)
204 u32 copy_len
= min_t(u32
, *from_len
, to_len
);
206 memcpy(to
, *from
, copy_len
);
211 if (*from_len
== copy_len
) {
212 *from
= skb_frag_address(*frag
);
213 *from_len
= skb_frag_size((*frag
)++);
216 *from_len
-= copy_len
;
218 if (to_len
== copy_len
)
226 static int __xsk_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
, u32 len
)
228 u32 frame_size
= xsk_pool_get_rx_frame_size(xs
->pool
);
229 void *copy_from
= xsk_copy_xdp_start(xdp
), *copy_to
;
230 u32 from_len
, meta_len
, rem
, num_desc
;
231 struct xdp_buff_xsk
*xskb
;
232 struct xdp_buff
*xsk_xdp
;
235 from_len
= xdp
->data_end
- copy_from
;
236 meta_len
= xdp
->data
- copy_from
;
237 rem
= len
+ meta_len
;
239 if (len
<= frame_size
&& !xdp_buff_has_frags(xdp
)) {
242 xsk_xdp
= xsk_buff_alloc(xs
->pool
);
247 memcpy(xsk_xdp
->data
- meta_len
, copy_from
, rem
);
248 xskb
= container_of(xsk_xdp
, struct xdp_buff_xsk
, xdp
);
249 err
= __xsk_rcv_zc(xs
, xskb
, len
, 0);
251 xsk_buff_free(xsk_xdp
);
258 num_desc
= (len
- 1) / frame_size
+ 1;
260 if (!xsk_buff_can_alloc(xs
->pool
, num_desc
)) {
264 if (xskq_prod_nb_free(xs
->rx
, num_desc
) < num_desc
) {
269 if (xdp_buff_has_frags(xdp
)) {
270 struct skb_shared_info
*sinfo
;
272 sinfo
= xdp_get_shared_info_from_buff(xdp
);
273 frag
= &sinfo
->frags
[0];
277 u32 to_len
= frame_size
+ meta_len
;
280 xsk_xdp
= xsk_buff_alloc(xs
->pool
);
281 copy_to
= xsk_xdp
->data
- meta_len
;
283 copied
= xsk_copy_xdp(copy_to
, ©_from
, to_len
, &from_len
, &frag
, rem
);
286 xskb
= container_of(xsk_xdp
, struct xdp_buff_xsk
, xdp
);
287 __xsk_rcv_zc(xs
, xskb
, copied
- meta_len
, rem
? XDP_PKT_CONTD
: 0);
294 static bool xsk_tx_writeable(struct xdp_sock
*xs
)
296 if (xskq_cons_present_entries(xs
->tx
) > xs
->tx
->nentries
/ 2)
302 static bool xsk_is_bound(struct xdp_sock
*xs
)
304 if (READ_ONCE(xs
->state
) == XSK_BOUND
) {
305 /* Matches smp_wmb() in bind(). */
312 static int xsk_rcv_check(struct xdp_sock
*xs
, struct xdp_buff
*xdp
, u32 len
)
314 if (!xsk_is_bound(xs
))
317 if (xs
->dev
!= xdp
->rxq
->dev
|| xs
->queue_id
!= xdp
->rxq
->queue_index
)
320 if (len
> xsk_pool_get_rx_frame_size(xs
->pool
) && !xs
->sg
) {
325 sk_mark_napi_id_once_xdp(&xs
->sk
, xdp
);
329 static void xsk_flush(struct xdp_sock
*xs
)
331 xskq_prod_submit(xs
->rx
);
332 __xskq_cons_release(xs
->pool
->fq
);
333 sock_def_readable(&xs
->sk
);
336 int xsk_generic_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
)
338 u32 len
= xdp_get_buff_len(xdp
);
341 spin_lock_bh(&xs
->rx_lock
);
342 err
= xsk_rcv_check(xs
, xdp
, len
);
344 err
= __xsk_rcv(xs
, xdp
, len
);
347 spin_unlock_bh(&xs
->rx_lock
);
351 static int xsk_rcv(struct xdp_sock
*xs
, struct xdp_buff
*xdp
)
353 u32 len
= xdp_get_buff_len(xdp
);
356 err
= xsk_rcv_check(xs
, xdp
, len
);
360 if (xdp
->rxq
->mem
.type
== MEM_TYPE_XSK_BUFF_POOL
) {
361 len
= xdp
->data_end
- xdp
->data
;
362 return xsk_rcv_zc(xs
, xdp
, len
);
365 err
= __xsk_rcv(xs
, xdp
, len
);
367 xdp_return_buff(xdp
);
371 int __xsk_map_redirect(struct xdp_sock
*xs
, struct xdp_buff
*xdp
)
375 err
= xsk_rcv(xs
, xdp
);
379 if (!xs
->flush_node
.prev
) {
380 struct list_head
*flush_list
= bpf_net_ctx_get_xskmap_flush_list();
382 list_add(&xs
->flush_node
, flush_list
);
388 void __xsk_map_flush(struct list_head
*flush_list
)
390 struct xdp_sock
*xs
, *tmp
;
392 list_for_each_entry_safe(xs
, tmp
, flush_list
, flush_node
) {
394 __list_del_clearprev(&xs
->flush_node
);
398 void xsk_tx_completed(struct xsk_buff_pool
*pool
, u32 nb_entries
)
400 xskq_prod_submit_n(pool
->cq
, nb_entries
);
402 EXPORT_SYMBOL(xsk_tx_completed
);
404 void xsk_tx_release(struct xsk_buff_pool
*pool
)
409 list_for_each_entry_rcu(xs
, &pool
->xsk_tx_list
, tx_list
) {
410 __xskq_cons_release(xs
->tx
);
411 if (xsk_tx_writeable(xs
))
412 xs
->sk
.sk_write_space(&xs
->sk
);
416 EXPORT_SYMBOL(xsk_tx_release
);
418 bool xsk_tx_peek_desc(struct xsk_buff_pool
*pool
, struct xdp_desc
*desc
)
420 bool budget_exhausted
= false;
425 list_for_each_entry_rcu(xs
, &pool
->xsk_tx_list
, tx_list
) {
426 if (xs
->tx_budget_spent
>= MAX_PER_SOCKET_BUDGET
) {
427 budget_exhausted
= true;
431 if (!xskq_cons_peek_desc(xs
->tx
, desc
, pool
)) {
432 if (xskq_has_descs(xs
->tx
))
433 xskq_cons_release(xs
->tx
);
437 xs
->tx_budget_spent
++;
439 /* This is the backpressure mechanism for the Tx path.
440 * Reserve space in the completion queue and only proceed
441 * if there is space in it. This avoids having to implement
442 * any buffering in the Tx path.
444 if (xskq_prod_reserve_addr(pool
->cq
, desc
->addr
))
447 xskq_cons_release(xs
->tx
);
452 if (budget_exhausted
) {
453 list_for_each_entry_rcu(xs
, &pool
->xsk_tx_list
, tx_list
)
454 xs
->tx_budget_spent
= 0;
456 budget_exhausted
= false;
464 EXPORT_SYMBOL(xsk_tx_peek_desc
);
466 static u32
xsk_tx_peek_release_fallback(struct xsk_buff_pool
*pool
, u32 max_entries
)
468 struct xdp_desc
*descs
= pool
->tx_descs
;
471 while (nb_pkts
< max_entries
&& xsk_tx_peek_desc(pool
, &descs
[nb_pkts
]))
474 xsk_tx_release(pool
);
478 u32
xsk_tx_peek_release_desc_batch(struct xsk_buff_pool
*pool
, u32 nb_pkts
)
483 if (!list_is_singular(&pool
->xsk_tx_list
)) {
484 /* Fallback to the non-batched version */
486 return xsk_tx_peek_release_fallback(pool
, nb_pkts
);
489 xs
= list_first_or_null_rcu(&pool
->xsk_tx_list
, struct xdp_sock
, tx_list
);
495 nb_pkts
= xskq_cons_nb_entries(xs
->tx
, nb_pkts
);
497 /* This is the backpressure mechanism for the Tx path. Try to
498 * reserve space in the completion queue for all packets, but
499 * if there are fewer slots available, just process that many
500 * packets. This avoids having to implement any buffering in
503 nb_pkts
= xskq_prod_nb_free(pool
->cq
, nb_pkts
);
507 nb_pkts
= xskq_cons_read_desc_batch(xs
->tx
, pool
, nb_pkts
);
509 xs
->tx
->queue_empty_descs
++;
513 __xskq_cons_release(xs
->tx
);
514 xskq_prod_write_addr_batch(pool
->cq
, pool
->tx_descs
, nb_pkts
);
515 xs
->sk
.sk_write_space(&xs
->sk
);
521 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch
);
523 static int xsk_wakeup(struct xdp_sock
*xs
, u8 flags
)
525 struct net_device
*dev
= xs
->dev
;
527 return dev
->netdev_ops
->ndo_xsk_wakeup(dev
, xs
->queue_id
, flags
);
530 static int xsk_cq_reserve_addr_locked(struct xsk_buff_pool
*pool
, u64 addr
)
535 spin_lock_irqsave(&pool
->cq_lock
, flags
);
536 ret
= xskq_prod_reserve_addr(pool
->cq
, addr
);
537 spin_unlock_irqrestore(&pool
->cq_lock
, flags
);
542 static void xsk_cq_submit_locked(struct xsk_buff_pool
*pool
, u32 n
)
546 spin_lock_irqsave(&pool
->cq_lock
, flags
);
547 xskq_prod_submit_n(pool
->cq
, n
);
548 spin_unlock_irqrestore(&pool
->cq_lock
, flags
);
551 static void xsk_cq_cancel_locked(struct xsk_buff_pool
*pool
, u32 n
)
555 spin_lock_irqsave(&pool
->cq_lock
, flags
);
556 xskq_prod_cancel_n(pool
->cq
, n
);
557 spin_unlock_irqrestore(&pool
->cq_lock
, flags
);
560 static u32
xsk_get_num_desc(struct sk_buff
*skb
)
562 return skb
? (long)skb_shinfo(skb
)->destructor_arg
: 0;
565 static void xsk_destruct_skb(struct sk_buff
*skb
)
567 struct xsk_tx_metadata_compl
*compl = &skb_shinfo(skb
)->xsk_meta
;
569 if (compl->tx_timestamp
) {
570 /* sw completion timestamp, not a real one */
571 *compl->tx_timestamp
= ktime_get_tai_fast_ns();
574 xsk_cq_submit_locked(xdp_sk(skb
->sk
)->pool
, xsk_get_num_desc(skb
));
578 static void xsk_set_destructor_arg(struct sk_buff
*skb
)
580 long num
= xsk_get_num_desc(xdp_sk(skb
->sk
)->skb
) + 1;
582 skb_shinfo(skb
)->destructor_arg
= (void *)num
;
585 static void xsk_consume_skb(struct sk_buff
*skb
)
587 struct xdp_sock
*xs
= xdp_sk(skb
->sk
);
589 skb
->destructor
= sock_wfree
;
590 xsk_cq_cancel_locked(xs
->pool
, xsk_get_num_desc(skb
));
591 /* Free skb without triggering the perf drop trace */
596 static void xsk_drop_skb(struct sk_buff
*skb
)
598 xdp_sk(skb
->sk
)->tx
->invalid_descs
+= xsk_get_num_desc(skb
);
599 xsk_consume_skb(skb
);
602 static struct sk_buff
*xsk_build_skb_zerocopy(struct xdp_sock
*xs
,
603 struct xdp_desc
*desc
)
605 struct xsk_buff_pool
*pool
= xs
->pool
;
606 u32 hr
, len
, ts
, offset
, copy
, copied
;
607 struct sk_buff
*skb
= xs
->skb
;
614 hr
= max(NET_SKB_PAD
, L1_CACHE_ALIGN(xs
->dev
->needed_headroom
));
616 skb
= sock_alloc_send_skb(&xs
->sk
, hr
, 1, &err
);
620 skb_reserve(skb
, hr
);
625 ts
= pool
->unaligned
? len
: pool
->chunk_size
;
627 buffer
= xsk_buff_raw_get_data(pool
, addr
);
628 offset
= offset_in_page(buffer
);
629 addr
= buffer
- pool
->addrs
;
631 for (copied
= 0, i
= skb_shinfo(skb
)->nr_frags
; copied
< len
; i
++) {
632 if (unlikely(i
>= MAX_SKB_FRAGS
))
633 return ERR_PTR(-EOVERFLOW
);
635 page
= pool
->umem
->pgs
[addr
>> PAGE_SHIFT
];
638 copy
= min_t(u32
, PAGE_SIZE
- offset
, len
- copied
);
639 skb_fill_page_desc(skb
, i
, page
, offset
, copy
);
647 skb
->data_len
+= len
;
650 refcount_add(ts
, &xs
->sk
.sk_wmem_alloc
);
655 static struct sk_buff
*xsk_build_skb(struct xdp_sock
*xs
,
656 struct xdp_desc
*desc
)
658 struct xsk_tx_metadata
*meta
= NULL
;
659 struct net_device
*dev
= xs
->dev
;
660 struct sk_buff
*skb
= xs
->skb
;
661 bool first_frag
= false;
664 if (dev
->priv_flags
& IFF_TX_SKB_NO_LINEAR
) {
665 skb
= xsk_build_skb_zerocopy(xs
, desc
);
674 buffer
= xsk_buff_raw_get_data(xs
->pool
, desc
->addr
);
680 hr
= max(NET_SKB_PAD
, L1_CACHE_ALIGN(dev
->needed_headroom
));
681 tr
= dev
->needed_tailroom
;
682 skb
= sock_alloc_send_skb(&xs
->sk
, hr
+ len
+ tr
, 1, &err
);
686 skb_reserve(skb
, hr
);
689 err
= skb_store_bits(skb
, 0, buffer
, len
);
693 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
697 if (unlikely(nr_frags
== (MAX_SKB_FRAGS
- 1) && xp_mb_desc(desc
))) {
702 page
= alloc_page(xs
->sk
.sk_allocation
);
703 if (unlikely(!page
)) {
708 vaddr
= kmap_local_page(page
);
709 memcpy(vaddr
, buffer
, len
);
712 skb_add_rx_frag(skb
, nr_frags
, page
, 0, len
, PAGE_SIZE
);
713 refcount_add(PAGE_SIZE
, &xs
->sk
.sk_wmem_alloc
);
716 if (first_frag
&& desc
->options
& XDP_TX_METADATA
) {
717 if (unlikely(xs
->pool
->tx_metadata_len
== 0)) {
722 meta
= buffer
- xs
->pool
->tx_metadata_len
;
723 if (unlikely(!xsk_buff_valid_tx_metadata(meta
))) {
728 if (meta
->flags
& XDP_TXMD_FLAGS_CHECKSUM
) {
729 if (unlikely(meta
->request
.csum_start
+
730 meta
->request
.csum_offset
+
731 sizeof(__sum16
) > len
)) {
736 skb
->csum_start
= hr
+ meta
->request
.csum_start
;
737 skb
->csum_offset
= meta
->request
.csum_offset
;
738 skb
->ip_summed
= CHECKSUM_PARTIAL
;
740 if (unlikely(xs
->pool
->tx_sw_csum
)) {
741 err
= skb_checksum_help(skb
);
750 skb
->priority
= READ_ONCE(xs
->sk
.sk_priority
);
751 skb
->mark
= READ_ONCE(xs
->sk
.sk_mark
);
752 skb
->destructor
= xsk_destruct_skb
;
753 xsk_tx_metadata_to_compl(meta
, &skb_shinfo(skb
)->xsk_meta
);
754 xsk_set_destructor_arg(skb
);
759 if (first_frag
&& skb
)
762 if (err
== -EOVERFLOW
) {
763 /* Drop the packet */
764 xsk_set_destructor_arg(xs
->skb
);
765 xsk_drop_skb(xs
->skb
);
766 xskq_cons_release(xs
->tx
);
768 /* Let application retry */
769 xsk_cq_cancel_locked(xs
->pool
, 1);
775 static int __xsk_generic_xmit(struct sock
*sk
)
777 struct xdp_sock
*xs
= xdp_sk(sk
);
778 u32 max_batch
= TX_BATCH_SIZE
;
779 bool sent_frame
= false;
780 struct xdp_desc desc
;
784 mutex_lock(&xs
->mutex
);
786 /* Since we dropped the RCU read lock, the socket state might have changed. */
787 if (unlikely(!xsk_is_bound(xs
))) {
792 if (xs
->queue_id
>= xs
->dev
->real_num_tx_queues
)
795 while (xskq_cons_peek_desc(xs
->tx
, &desc
, xs
->pool
)) {
796 if (max_batch
-- == 0) {
801 /* This is the backpressure mechanism for the Tx path.
802 * Reserve space in the completion queue and only proceed
803 * if there is space in it. This avoids having to implement
804 * any buffering in the Tx path.
806 if (xsk_cq_reserve_addr_locked(xs
->pool
, desc
.addr
))
809 skb
= xsk_build_skb(xs
, &desc
);
812 if (err
!= -EOVERFLOW
)
818 xskq_cons_release(xs
->tx
);
820 if (xp_mb_desc(&desc
)) {
825 err
= __dev_direct_xmit(skb
, xs
->queue_id
);
826 if (err
== NETDEV_TX_BUSY
) {
827 /* Tell user-space to retry the send */
828 xskq_cons_cancel_n(xs
->tx
, xsk_get_num_desc(skb
));
829 xsk_consume_skb(skb
);
834 /* Ignore NET_XMIT_CN as packet might have been sent */
835 if (err
== NET_XMIT_DROP
) {
836 /* SKB completed but not sent */
846 if (xskq_has_descs(xs
->tx
)) {
848 xsk_drop_skb(xs
->skb
);
849 xskq_cons_release(xs
->tx
);
854 if (xsk_tx_writeable(xs
))
855 sk
->sk_write_space(sk
);
857 mutex_unlock(&xs
->mutex
);
861 static int xsk_generic_xmit(struct sock
*sk
)
865 /* Drop the RCU lock since the SKB path might sleep. */
867 ret
= __xsk_generic_xmit(sk
);
868 /* Reaquire RCU lock before going into common code. */
874 static bool xsk_no_wakeup(struct sock
*sk
)
876 #ifdef CONFIG_NET_RX_BUSY_POLL
877 /* Prefer busy-polling, skip the wakeup. */
878 return READ_ONCE(sk
->sk_prefer_busy_poll
) && READ_ONCE(sk
->sk_ll_usec
) &&
879 READ_ONCE(sk
->sk_napi_id
) >= MIN_NAPI_ID
;
885 static int xsk_check_common(struct xdp_sock
*xs
)
887 if (unlikely(!xsk_is_bound(xs
)))
889 if (unlikely(!(xs
->dev
->flags
& IFF_UP
)))
895 static int __xsk_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t total_len
)
897 bool need_wait
= !(m
->msg_flags
& MSG_DONTWAIT
);
898 struct sock
*sk
= sock
->sk
;
899 struct xdp_sock
*xs
= xdp_sk(sk
);
900 struct xsk_buff_pool
*pool
;
903 err
= xsk_check_common(xs
);
906 if (unlikely(need_wait
))
908 if (unlikely(!xs
->tx
))
911 if (sk_can_busy_loop(sk
)) {
913 __sk_mark_napi_id_once(sk
, xsk_pool_get_napi_id(xs
->pool
));
914 sk_busy_loop(sk
, 1); /* only support non-blocking sockets */
917 if (xs
->zc
&& xsk_no_wakeup(sk
))
921 if (pool
->cached_need_wakeup
& XDP_WAKEUP_TX
) {
923 return xsk_wakeup(xs
, XDP_WAKEUP_TX
);
924 return xsk_generic_xmit(sk
);
929 static int xsk_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t total_len
)
934 ret
= __xsk_sendmsg(sock
, m
, total_len
);
940 static int __xsk_recvmsg(struct socket
*sock
, struct msghdr
*m
, size_t len
, int flags
)
942 bool need_wait
= !(flags
& MSG_DONTWAIT
);
943 struct sock
*sk
= sock
->sk
;
944 struct xdp_sock
*xs
= xdp_sk(sk
);
947 err
= xsk_check_common(xs
);
950 if (unlikely(!xs
->rx
))
952 if (unlikely(need_wait
))
955 if (sk_can_busy_loop(sk
))
956 sk_busy_loop(sk
, 1); /* only support non-blocking sockets */
958 if (xsk_no_wakeup(sk
))
961 if (xs
->pool
->cached_need_wakeup
& XDP_WAKEUP_RX
&& xs
->zc
)
962 return xsk_wakeup(xs
, XDP_WAKEUP_RX
);
966 static int xsk_recvmsg(struct socket
*sock
, struct msghdr
*m
, size_t len
, int flags
)
971 ret
= __xsk_recvmsg(sock
, m
, len
, flags
);
977 static __poll_t
xsk_poll(struct file
*file
, struct socket
*sock
,
978 struct poll_table_struct
*wait
)
981 struct sock
*sk
= sock
->sk
;
982 struct xdp_sock
*xs
= xdp_sk(sk
);
983 struct xsk_buff_pool
*pool
;
985 sock_poll_wait(file
, sock
, wait
);
988 if (xsk_check_common(xs
))
993 if (pool
->cached_need_wakeup
) {
995 xsk_wakeup(xs
, pool
->cached_need_wakeup
);
997 /* Poll needs to drive Tx also in copy mode */
998 xsk_generic_xmit(sk
);
1001 if (xs
->rx
&& !xskq_prod_is_empty(xs
->rx
))
1002 mask
|= EPOLLIN
| EPOLLRDNORM
;
1003 if (xs
->tx
&& xsk_tx_writeable(xs
))
1004 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1010 static int xsk_init_queue(u32 entries
, struct xsk_queue
**queue
,
1013 struct xsk_queue
*q
;
1015 if (entries
== 0 || *queue
|| !is_power_of_2(entries
))
1018 q
= xskq_create(entries
, umem_queue
);
1022 /* Make sure queue is ready before it can be seen by others */
1024 WRITE_ONCE(*queue
, q
);
1028 static void xsk_unbind_dev(struct xdp_sock
*xs
)
1030 struct net_device
*dev
= xs
->dev
;
1032 if (xs
->state
!= XSK_BOUND
)
1034 WRITE_ONCE(xs
->state
, XSK_UNBOUND
);
1036 /* Wait for driver to stop using the xdp socket. */
1037 xp_del_xsk(xs
->pool
, xs
);
1042 static struct xsk_map
*xsk_get_map_list_entry(struct xdp_sock
*xs
,
1043 struct xdp_sock __rcu
***map_entry
)
1045 struct xsk_map
*map
= NULL
;
1046 struct xsk_map_node
*node
;
1050 spin_lock_bh(&xs
->map_list_lock
);
1051 node
= list_first_entry_or_null(&xs
->map_list
, struct xsk_map_node
,
1054 bpf_map_inc(&node
->map
->map
);
1056 *map_entry
= node
->map_entry
;
1058 spin_unlock_bh(&xs
->map_list_lock
);
1062 static void xsk_delete_from_maps(struct xdp_sock
*xs
)
1064 /* This function removes the current XDP socket from all the
1065 * maps it resides in. We need to take extra care here, due to
1066 * the two locks involved. Each map has a lock synchronizing
1067 * updates to the entries, and each socket has a lock that
1068 * synchronizes access to the list of maps (map_list). For
1069 * deadlock avoidance the locks need to be taken in the order
1070 * "map lock"->"socket map list lock". We start off by
1071 * accessing the socket map list, and take a reference to the
1072 * map to guarantee existence between the
1073 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
1074 * calls. Then we ask the map to remove the socket, which
1075 * tries to remove the socket from the map. Note that there
1076 * might be updates to the map between
1077 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
1079 struct xdp_sock __rcu
**map_entry
= NULL
;
1080 struct xsk_map
*map
;
1082 while ((map
= xsk_get_map_list_entry(xs
, &map_entry
))) {
1083 xsk_map_try_sock_delete(map
, xs
, map_entry
);
1084 bpf_map_put(&map
->map
);
1088 static int xsk_release(struct socket
*sock
)
1090 struct sock
*sk
= sock
->sk
;
1091 struct xdp_sock
*xs
= xdp_sk(sk
);
1100 xsk_drop_skb(xs
->skb
);
1102 mutex_lock(&net
->xdp
.lock
);
1103 sk_del_node_init_rcu(sk
);
1104 mutex_unlock(&net
->xdp
.lock
);
1106 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
1108 xsk_delete_from_maps(xs
);
1109 mutex_lock(&xs
->mutex
);
1111 mutex_unlock(&xs
->mutex
);
1113 xskq_destroy(xs
->rx
);
1114 xskq_destroy(xs
->tx
);
1115 xskq_destroy(xs
->fq_tmp
);
1116 xskq_destroy(xs
->cq_tmp
);
1126 static struct socket
*xsk_lookup_xsk_from_fd(int fd
)
1128 struct socket
*sock
;
1131 sock
= sockfd_lookup(fd
, &err
);
1133 return ERR_PTR(-ENOTSOCK
);
1135 if (sock
->sk
->sk_family
!= PF_XDP
) {
1137 return ERR_PTR(-ENOPROTOOPT
);
1143 static bool xsk_validate_queues(struct xdp_sock
*xs
)
1145 return xs
->fq_tmp
&& xs
->cq_tmp
;
1148 static int xsk_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
1150 struct sockaddr_xdp
*sxdp
= (struct sockaddr_xdp
*)addr
;
1151 struct sock
*sk
= sock
->sk
;
1152 struct xdp_sock
*xs
= xdp_sk(sk
);
1153 struct net_device
*dev
;
1158 if (addr_len
< sizeof(struct sockaddr_xdp
))
1160 if (sxdp
->sxdp_family
!= AF_XDP
)
1163 flags
= sxdp
->sxdp_flags
;
1164 if (flags
& ~(XDP_SHARED_UMEM
| XDP_COPY
| XDP_ZEROCOPY
|
1165 XDP_USE_NEED_WAKEUP
| XDP_USE_SG
))
1168 bound_dev_if
= READ_ONCE(sk
->sk_bound_dev_if
);
1169 if (bound_dev_if
&& bound_dev_if
!= sxdp
->sxdp_ifindex
)
1173 mutex_lock(&xs
->mutex
);
1174 if (xs
->state
!= XSK_READY
) {
1179 dev
= dev_get_by_index(sock_net(sk
), sxdp
->sxdp_ifindex
);
1185 if (!xs
->rx
&& !xs
->tx
) {
1190 qid
= sxdp
->sxdp_queue_id
;
1192 if (flags
& XDP_SHARED_UMEM
) {
1193 struct xdp_sock
*umem_xs
;
1194 struct socket
*sock
;
1196 if ((flags
& XDP_COPY
) || (flags
& XDP_ZEROCOPY
) ||
1197 (flags
& XDP_USE_NEED_WAKEUP
) || (flags
& XDP_USE_SG
)) {
1198 /* Cannot specify flags for shared sockets. */
1204 /* We have already our own. */
1209 sock
= xsk_lookup_xsk_from_fd(sxdp
->sxdp_shared_umem_fd
);
1211 err
= PTR_ERR(sock
);
1215 umem_xs
= xdp_sk(sock
->sk
);
1216 if (!xsk_is_bound(umem_xs
)) {
1222 if (umem_xs
->queue_id
!= qid
|| umem_xs
->dev
!= dev
) {
1223 /* Share the umem with another socket on another qid
1226 xs
->pool
= xp_create_and_assign_umem(xs
,
1234 err
= xp_assign_dev_shared(xs
->pool
, umem_xs
, dev
,
1237 xp_destroy(xs
->pool
);
1243 /* Share the buffer pool with the other socket. */
1244 if (xs
->fq_tmp
|| xs
->cq_tmp
) {
1245 /* Do not allow setting your own fq or cq. */
1251 xp_get_pool(umem_xs
->pool
);
1252 xs
->pool
= umem_xs
->pool
;
1254 /* If underlying shared umem was created without Tx
1255 * ring, allocate Tx descs array that Tx batching API
1258 if (xs
->tx
&& !xs
->pool
->tx_descs
) {
1259 err
= xp_alloc_tx_descs(xs
->pool
, xs
);
1261 xp_put_pool(xs
->pool
);
1269 xdp_get_umem(umem_xs
->umem
);
1270 WRITE_ONCE(xs
->umem
, umem_xs
->umem
);
1272 } else if (!xs
->umem
|| !xsk_validate_queues(xs
)) {
1276 /* This xsk has its own umem. */
1277 xs
->pool
= xp_create_and_assign_umem(xs
, xs
->umem
);
1283 err
= xp_assign_dev(xs
->pool
, dev
, qid
, flags
);
1285 xp_destroy(xs
->pool
);
1291 /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
1296 xs
->zc
= xs
->umem
->zc
;
1297 xs
->sg
= !!(xs
->umem
->flags
& XDP_UMEM_SG_FLAG
);
1299 xp_add_xsk(xs
->pool
, xs
);
1305 /* Matches smp_rmb() in bind() for shared umem
1306 * sockets, and xsk_is_bound().
1309 WRITE_ONCE(xs
->state
, XSK_BOUND
);
1312 mutex_unlock(&xs
->mutex
);
1317 struct xdp_umem_reg_v1
{
1318 __u64 addr
; /* Start of packet data area */
1319 __u64 len
; /* Length of packet data area */
1324 static int xsk_setsockopt(struct socket
*sock
, int level
, int optname
,
1325 sockptr_t optval
, unsigned int optlen
)
1327 struct sock
*sk
= sock
->sk
;
1328 struct xdp_sock
*xs
= xdp_sk(sk
);
1331 if (level
!= SOL_XDP
)
1332 return -ENOPROTOOPT
;
1338 struct xsk_queue
**q
;
1341 if (optlen
< sizeof(entries
))
1343 if (copy_from_sockptr(&entries
, optval
, sizeof(entries
)))
1346 mutex_lock(&xs
->mutex
);
1347 if (xs
->state
!= XSK_READY
) {
1348 mutex_unlock(&xs
->mutex
);
1351 q
= (optname
== XDP_TX_RING
) ? &xs
->tx
: &xs
->rx
;
1352 err
= xsk_init_queue(entries
, q
, false);
1353 if (!err
&& optname
== XDP_TX_RING
)
1354 /* Tx needs to be explicitly woken up the first time */
1355 xs
->tx
->ring
->flags
|= XDP_RING_NEED_WAKEUP
;
1356 mutex_unlock(&xs
->mutex
);
1361 size_t mr_size
= sizeof(struct xdp_umem_reg
);
1362 struct xdp_umem_reg mr
= {};
1363 struct xdp_umem
*umem
;
1365 if (optlen
< sizeof(struct xdp_umem_reg_v1
))
1367 else if (optlen
< sizeof(mr
))
1368 mr_size
= sizeof(struct xdp_umem_reg_v1
);
1370 BUILD_BUG_ON(sizeof(struct xdp_umem_reg_v1
) >= sizeof(struct xdp_umem_reg
));
1372 /* Make sure the last field of the struct doesn't have
1373 * uninitialized padding. All padding has to be explicit
1374 * and has to be set to zero by the userspace to make
1375 * struct xdp_umem_reg extensible in the future.
1377 BUILD_BUG_ON(offsetof(struct xdp_umem_reg
, tx_metadata_len
) +
1378 sizeof_field(struct xdp_umem_reg
, tx_metadata_len
) !=
1379 sizeof(struct xdp_umem_reg
));
1381 if (copy_from_sockptr(&mr
, optval
, mr_size
))
1384 mutex_lock(&xs
->mutex
);
1385 if (xs
->state
!= XSK_READY
|| xs
->umem
) {
1386 mutex_unlock(&xs
->mutex
);
1390 umem
= xdp_umem_create(&mr
);
1392 mutex_unlock(&xs
->mutex
);
1393 return PTR_ERR(umem
);
1396 /* Make sure umem is ready before it can be seen by others */
1398 WRITE_ONCE(xs
->umem
, umem
);
1399 mutex_unlock(&xs
->mutex
);
1402 case XDP_UMEM_FILL_RING
:
1403 case XDP_UMEM_COMPLETION_RING
:
1405 struct xsk_queue
**q
;
1408 if (optlen
< sizeof(entries
))
1410 if (copy_from_sockptr(&entries
, optval
, sizeof(entries
)))
1413 mutex_lock(&xs
->mutex
);
1414 if (xs
->state
!= XSK_READY
) {
1415 mutex_unlock(&xs
->mutex
);
1419 q
= (optname
== XDP_UMEM_FILL_RING
) ? &xs
->fq_tmp
:
1421 err
= xsk_init_queue(entries
, q
, true);
1422 mutex_unlock(&xs
->mutex
);
1429 return -ENOPROTOOPT
;
1432 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1
*ring
)
1434 ring
->producer
= offsetof(struct xdp_rxtx_ring
, ptrs
.producer
);
1435 ring
->consumer
= offsetof(struct xdp_rxtx_ring
, ptrs
.consumer
);
1436 ring
->desc
= offsetof(struct xdp_rxtx_ring
, desc
);
1439 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1
*ring
)
1441 ring
->producer
= offsetof(struct xdp_umem_ring
, ptrs
.producer
);
1442 ring
->consumer
= offsetof(struct xdp_umem_ring
, ptrs
.consumer
);
1443 ring
->desc
= offsetof(struct xdp_umem_ring
, desc
);
1446 struct xdp_statistics_v1
{
1448 __u64 rx_invalid_descs
;
1449 __u64 tx_invalid_descs
;
1452 static int xsk_getsockopt(struct socket
*sock
, int level
, int optname
,
1453 char __user
*optval
, int __user
*optlen
)
1455 struct sock
*sk
= sock
->sk
;
1456 struct xdp_sock
*xs
= xdp_sk(sk
);
1459 if (level
!= SOL_XDP
)
1460 return -ENOPROTOOPT
;
1462 if (get_user(len
, optlen
))
1468 case XDP_STATISTICS
:
1470 struct xdp_statistics stats
= {};
1471 bool extra_stats
= true;
1474 if (len
< sizeof(struct xdp_statistics_v1
)) {
1476 } else if (len
< sizeof(stats
)) {
1477 extra_stats
= false;
1478 stats_size
= sizeof(struct xdp_statistics_v1
);
1480 stats_size
= sizeof(stats
);
1483 mutex_lock(&xs
->mutex
);
1484 stats
.rx_dropped
= xs
->rx_dropped
;
1486 stats
.rx_ring_full
= xs
->rx_queue_full
;
1487 stats
.rx_fill_ring_empty_descs
=
1488 xs
->pool
? xskq_nb_queue_empty_descs(xs
->pool
->fq
) : 0;
1489 stats
.tx_ring_empty_descs
= xskq_nb_queue_empty_descs(xs
->tx
);
1491 stats
.rx_dropped
+= xs
->rx_queue_full
;
1493 stats
.rx_invalid_descs
= xskq_nb_invalid_descs(xs
->rx
);
1494 stats
.tx_invalid_descs
= xskq_nb_invalid_descs(xs
->tx
);
1495 mutex_unlock(&xs
->mutex
);
1497 if (copy_to_user(optval
, &stats
, stats_size
))
1499 if (put_user(stats_size
, optlen
))
1504 case XDP_MMAP_OFFSETS
:
1506 struct xdp_mmap_offsets off
;
1507 struct xdp_mmap_offsets_v1 off_v1
;
1508 bool flags_supported
= true;
1511 if (len
< sizeof(off_v1
))
1513 else if (len
< sizeof(off
))
1514 flags_supported
= false;
1516 if (flags_supported
) {
1517 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1518 * except for the flags field added to the end.
1520 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
1522 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1
*)
1524 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
1526 xsk_enter_umem_offsets((struct xdp_ring_offset_v1
*)
1528 off
.rx
.flags
= offsetof(struct xdp_rxtx_ring
,
1530 off
.tx
.flags
= offsetof(struct xdp_rxtx_ring
,
1532 off
.fr
.flags
= offsetof(struct xdp_umem_ring
,
1534 off
.cr
.flags
= offsetof(struct xdp_umem_ring
,
1540 xsk_enter_rxtx_offsets(&off_v1
.rx
);
1541 xsk_enter_rxtx_offsets(&off_v1
.tx
);
1542 xsk_enter_umem_offsets(&off_v1
.fr
);
1543 xsk_enter_umem_offsets(&off_v1
.cr
);
1545 len
= sizeof(off_v1
);
1549 if (copy_to_user(optval
, to_copy
, len
))
1551 if (put_user(len
, optlen
))
1558 struct xdp_options opts
= {};
1560 if (len
< sizeof(opts
))
1563 mutex_lock(&xs
->mutex
);
1565 opts
.flags
|= XDP_OPTIONS_ZEROCOPY
;
1566 mutex_unlock(&xs
->mutex
);
1569 if (copy_to_user(optval
, &opts
, len
))
1571 if (put_user(len
, optlen
))
1583 static int xsk_mmap(struct file
*file
, struct socket
*sock
,
1584 struct vm_area_struct
*vma
)
1586 loff_t offset
= (loff_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
1587 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1588 struct xdp_sock
*xs
= xdp_sk(sock
->sk
);
1589 int state
= READ_ONCE(xs
->state
);
1590 struct xsk_queue
*q
= NULL
;
1592 if (state
!= XSK_READY
&& state
!= XSK_BOUND
)
1595 if (offset
== XDP_PGOFF_RX_RING
) {
1596 q
= READ_ONCE(xs
->rx
);
1597 } else if (offset
== XDP_PGOFF_TX_RING
) {
1598 q
= READ_ONCE(xs
->tx
);
1600 /* Matches the smp_wmb() in XDP_UMEM_REG */
1602 if (offset
== XDP_UMEM_PGOFF_FILL_RING
)
1603 q
= state
== XSK_READY
? READ_ONCE(xs
->fq_tmp
) :
1604 READ_ONCE(xs
->pool
->fq
);
1605 else if (offset
== XDP_UMEM_PGOFF_COMPLETION_RING
)
1606 q
= state
== XSK_READY
? READ_ONCE(xs
->cq_tmp
) :
1607 READ_ONCE(xs
->pool
->cq
);
1613 /* Matches the smp_wmb() in xsk_init_queue */
1615 if (size
> q
->ring_vmalloc_size
)
1618 return remap_vmalloc_range(vma
, q
->ring
, 0);
1621 static int xsk_notifier(struct notifier_block
*this,
1622 unsigned long msg
, void *ptr
)
1624 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1625 struct net
*net
= dev_net(dev
);
1629 case NETDEV_UNREGISTER
:
1630 mutex_lock(&net
->xdp
.lock
);
1631 sk_for_each(sk
, &net
->xdp
.list
) {
1632 struct xdp_sock
*xs
= xdp_sk(sk
);
1634 mutex_lock(&xs
->mutex
);
1635 if (xs
->dev
== dev
) {
1636 sk
->sk_err
= ENETDOWN
;
1637 if (!sock_flag(sk
, SOCK_DEAD
))
1638 sk_error_report(sk
);
1642 /* Clear device references. */
1643 xp_clear_dev(xs
->pool
);
1645 mutex_unlock(&xs
->mutex
);
1647 mutex_unlock(&net
->xdp
.lock
);
1653 static struct proto xsk_proto
= {
1655 .owner
= THIS_MODULE
,
1656 .obj_size
= sizeof(struct xdp_sock
),
1659 static const struct proto_ops xsk_proto_ops
= {
1661 .owner
= THIS_MODULE
,
1662 .release
= xsk_release
,
1664 .connect
= sock_no_connect
,
1665 .socketpair
= sock_no_socketpair
,
1666 .accept
= sock_no_accept
,
1667 .getname
= sock_no_getname
,
1669 .ioctl
= sock_no_ioctl
,
1670 .listen
= sock_no_listen
,
1671 .shutdown
= sock_no_shutdown
,
1672 .setsockopt
= xsk_setsockopt
,
1673 .getsockopt
= xsk_getsockopt
,
1674 .sendmsg
= xsk_sendmsg
,
1675 .recvmsg
= xsk_recvmsg
,
1679 static void xsk_destruct(struct sock
*sk
)
1681 struct xdp_sock
*xs
= xdp_sk(sk
);
1683 if (!sock_flag(sk
, SOCK_DEAD
))
1686 if (!xp_put_pool(xs
->pool
))
1687 xdp_put_umem(xs
->umem
, !xs
->pool
);
1690 static int xsk_create(struct net
*net
, struct socket
*sock
, int protocol
,
1693 struct xdp_sock
*xs
;
1696 if (!ns_capable(net
->user_ns
, CAP_NET_RAW
))
1698 if (sock
->type
!= SOCK_RAW
)
1699 return -ESOCKTNOSUPPORT
;
1702 return -EPROTONOSUPPORT
;
1704 sock
->state
= SS_UNCONNECTED
;
1706 sk
= sk_alloc(net
, PF_XDP
, GFP_KERNEL
, &xsk_proto
, kern
);
1710 sock
->ops
= &xsk_proto_ops
;
1712 sock_init_data(sock
, sk
);
1714 sk
->sk_family
= PF_XDP
;
1716 sk
->sk_destruct
= xsk_destruct
;
1718 sock_set_flag(sk
, SOCK_RCU_FREE
);
1721 xs
->state
= XSK_READY
;
1722 mutex_init(&xs
->mutex
);
1723 spin_lock_init(&xs
->rx_lock
);
1725 INIT_LIST_HEAD(&xs
->map_list
);
1726 spin_lock_init(&xs
->map_list_lock
);
1728 mutex_lock(&net
->xdp
.lock
);
1729 sk_add_node_rcu(sk
, &net
->xdp
.list
);
1730 mutex_unlock(&net
->xdp
.lock
);
1732 sock_prot_inuse_add(net
, &xsk_proto
, 1);
1737 static const struct net_proto_family xsk_family_ops
= {
1739 .create
= xsk_create
,
1740 .owner
= THIS_MODULE
,
1743 static struct notifier_block xsk_netdev_notifier
= {
1744 .notifier_call
= xsk_notifier
,
1747 static int __net_init
xsk_net_init(struct net
*net
)
1749 mutex_init(&net
->xdp
.lock
);
1750 INIT_HLIST_HEAD(&net
->xdp
.list
);
1754 static void __net_exit
xsk_net_exit(struct net
*net
)
1756 WARN_ON_ONCE(!hlist_empty(&net
->xdp
.list
));
1759 static struct pernet_operations xsk_net_ops
= {
1760 .init
= xsk_net_init
,
1761 .exit
= xsk_net_exit
,
1764 static int __init
xsk_init(void)
1768 err
= proto_register(&xsk_proto
, 0 /* no slab */);
1772 err
= sock_register(&xsk_family_ops
);
1776 err
= register_pernet_subsys(&xsk_net_ops
);
1780 err
= register_netdevice_notifier(&xsk_netdev_notifier
);
1787 unregister_pernet_subsys(&xsk_net_ops
);
1789 sock_unregister(PF_XDP
);
1791 proto_unregister(&xsk_proto
);
1796 fs_initcall(xsk_init
);