xref: /linux/net/xdp/xsk.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP sockets
3  *
4  * AF_XDP sockets allows a channel between XDP programs and userspace
5  * applications.
6  * Copyright(c) 2018 Intel Corporation.
7  *
8  * Author(s): Björn Töpel <bjorn.topel@intel.com>
9  *	      Magnus Karlsson <magnus.karlsson@intel.com>
10  */
11 
12 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
13 
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>
27 #include <net/xdp.h>
28 
29 #include "xsk_queue.h"
30 #include "xdp_umem.h"
31 #include "xsk.h"
32 
33 #define TX_BATCH_SIZE 32
34 
35 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
36 
37 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
38 {
39 	if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
40 		return;
41 
42 	pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
43 	pool->cached_need_wakeup |= XDP_WAKEUP_RX;
44 }
45 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
46 
47 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
48 {
49 	struct xdp_sock *xs;
50 
51 	if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
52 		return;
53 
54 	rcu_read_lock();
55 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
56 		xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
57 	}
58 	rcu_read_unlock();
59 
60 	pool->cached_need_wakeup |= XDP_WAKEUP_TX;
61 }
62 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
63 
64 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
65 {
66 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
67 		return;
68 
69 	pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
70 	pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
71 }
72 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
73 
74 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
75 {
76 	struct xdp_sock *xs;
77 
78 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
79 		return;
80 
81 	rcu_read_lock();
82 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
83 		xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
84 	}
85 	rcu_read_unlock();
86 
87 	pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
88 }
89 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
90 
91 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
92 {
93 	return pool->uses_need_wakeup;
94 }
95 EXPORT_SYMBOL(xsk_uses_need_wakeup);
96 
97 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
98 					    u16 queue_id)
99 {
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;
104 
105 	return NULL;
106 }
107 EXPORT_SYMBOL(xsk_get_pool_from_qid);
108 
109 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
110 {
111 	if (queue_id < dev->num_rx_queues)
112 		dev->_rx[queue_id].pool = NULL;
113 	if (queue_id < dev->num_tx_queues)
114 		dev->_tx[queue_id].pool = NULL;
115 }
116 
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.
120  */
121 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
122 			u16 queue_id)
123 {
124 	if (queue_id >= max_t(unsigned int,
125 			      dev->real_num_rx_queues,
126 			      dev->real_num_tx_queues))
127 		return -EINVAL;
128 
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;
133 
134 	return 0;
135 }
136 
137 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
138 {
139 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
140 	u64 addr;
141 	int err;
142 
143 	addr = xp_get_handle(xskb);
144 	err = xskq_prod_reserve_desc(xs->rx, addr, len);
145 	if (err) {
146 		xs->rx_queue_full++;
147 		return err;
148 	}
149 
150 	xp_release(xskb);
151 	return 0;
152 }
153 
154 static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
155 {
156 	void *from_buf, *to_buf;
157 	u32 metalen;
158 
159 	if (unlikely(xdp_data_meta_unsupported(from))) {
160 		from_buf = from->data;
161 		to_buf = to->data;
162 		metalen = 0;
163 	} else {
164 		from_buf = from->data_meta;
165 		metalen = from->data - from->data_meta;
166 		to_buf = to->data - metalen;
167 	}
168 
169 	memcpy(to_buf, from_buf, len + metalen);
170 }
171 
172 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
173 {
174 	struct xdp_buff *xsk_xdp;
175 	int err;
176 	u32 len;
177 
178 	len = xdp->data_end - xdp->data;
179 	if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
180 		xs->rx_dropped++;
181 		return -ENOSPC;
182 	}
183 
184 	xsk_xdp = xsk_buff_alloc(xs->pool);
185 	if (!xsk_xdp) {
186 		xs->rx_dropped++;
187 		return -ENOMEM;
188 	}
189 
190 	xsk_copy_xdp(xsk_xdp, xdp, len);
191 	err = __xsk_rcv_zc(xs, xsk_xdp, len);
192 	if (err) {
193 		xsk_buff_free(xsk_xdp);
194 		return err;
195 	}
196 	return 0;
197 }
198 
199 static bool xsk_tx_writeable(struct xdp_sock *xs)
200 {
201 	if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
202 		return false;
203 
204 	return true;
205 }
206 
207 static bool xsk_is_bound(struct xdp_sock *xs)
208 {
209 	if (READ_ONCE(xs->state) == XSK_BOUND) {
210 		/* Matches smp_wmb() in bind(). */
211 		smp_rmb();
212 		return true;
213 	}
214 	return false;
215 }
216 
217 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp)
218 {
219 	if (!xsk_is_bound(xs))
220 		return -ENXIO;
221 
222 	if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
223 		return -EINVAL;
224 
225 	sk_mark_napi_id_once_xdp(&xs->sk, xdp);
226 	return 0;
227 }
228 
229 static void xsk_flush(struct xdp_sock *xs)
230 {
231 	xskq_prod_submit(xs->rx);
232 	__xskq_cons_release(xs->pool->fq);
233 	sock_def_readable(&xs->sk);
234 }
235 
236 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
237 {
238 	int err;
239 
240 	spin_lock_bh(&xs->rx_lock);
241 	err = xsk_rcv_check(xs, xdp);
242 	if (!err) {
243 		err = __xsk_rcv(xs, xdp);
244 		xsk_flush(xs);
245 	}
246 	spin_unlock_bh(&xs->rx_lock);
247 	return err;
248 }
249 
250 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
251 {
252 	int err;
253 	u32 len;
254 
255 	err = xsk_rcv_check(xs, xdp);
256 	if (err)
257 		return err;
258 
259 	if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
260 		len = xdp->data_end - xdp->data;
261 		return __xsk_rcv_zc(xs, xdp, len);
262 	}
263 
264 	err = __xsk_rcv(xs, xdp);
265 	if (!err)
266 		xdp_return_buff(xdp);
267 	return err;
268 }
269 
270 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
271 {
272 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
273 	int err;
274 
275 	err = xsk_rcv(xs, xdp);
276 	if (err)
277 		return err;
278 
279 	if (!xs->flush_node.prev)
280 		list_add(&xs->flush_node, flush_list);
281 
282 	return 0;
283 }
284 
285 void __xsk_map_flush(void)
286 {
287 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
288 	struct xdp_sock *xs, *tmp;
289 
290 	list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
291 		xsk_flush(xs);
292 		__list_del_clearprev(&xs->flush_node);
293 	}
294 }
295 
296 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
297 {
298 	xskq_prod_submit_n(pool->cq, nb_entries);
299 }
300 EXPORT_SYMBOL(xsk_tx_completed);
301 
302 void xsk_tx_release(struct xsk_buff_pool *pool)
303 {
304 	struct xdp_sock *xs;
305 
306 	rcu_read_lock();
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);
311 	}
312 	rcu_read_unlock();
313 }
314 EXPORT_SYMBOL(xsk_tx_release);
315 
316 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
317 {
318 	struct xdp_sock *xs;
319 
320 	rcu_read_lock();
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++;
324 			continue;
325 		}
326 
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.
331 		 */
332 		if (xskq_prod_reserve_addr(pool->cq, desc->addr))
333 			goto out;
334 
335 		xskq_cons_release(xs->tx);
336 		rcu_read_unlock();
337 		return true;
338 	}
339 
340 out:
341 	rcu_read_unlock();
342 	return false;
343 }
344 EXPORT_SYMBOL(xsk_tx_peek_desc);
345 
346 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
347 {
348 	struct xdp_desc *descs = pool->tx_descs;
349 	u32 nb_pkts = 0;
350 
351 	while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
352 		nb_pkts++;
353 
354 	xsk_tx_release(pool);
355 	return nb_pkts;
356 }
357 
358 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max_entries)
359 {
360 	struct xdp_sock *xs;
361 	u32 nb_pkts;
362 
363 	rcu_read_lock();
364 	if (!list_is_singular(&pool->xsk_tx_list)) {
365 		/* Fallback to the non-batched version */
366 		rcu_read_unlock();
367 		return xsk_tx_peek_release_fallback(pool, max_entries);
368 	}
369 
370 	xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
371 	if (!xs) {
372 		nb_pkts = 0;
373 		goto out;
374 	}
375 
376 	max_entries = xskq_cons_nb_entries(xs->tx, max_entries);
377 	nb_pkts = xskq_cons_read_desc_batch(xs->tx, pool, max_entries);
378 	if (!nb_pkts) {
379 		xs->tx->queue_empty_descs++;
380 		goto out;
381 	}
382 
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
387 	 * the Tx path.
388 	 */
389 	nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, pool->tx_descs, nb_pkts);
390 	if (!nb_pkts)
391 		goto out;
392 
393 	xskq_cons_release_n(xs->tx, max_entries);
394 	__xskq_cons_release(xs->tx);
395 	xs->sk.sk_write_space(&xs->sk);
396 
397 out:
398 	rcu_read_unlock();
399 	return nb_pkts;
400 }
401 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
402 
403 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
404 {
405 	struct net_device *dev = xs->dev;
406 
407 	return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
408 }
409 
410 static void xsk_destruct_skb(struct sk_buff *skb)
411 {
412 	u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
413 	struct xdp_sock *xs = xdp_sk(skb->sk);
414 	unsigned long flags;
415 
416 	spin_lock_irqsave(&xs->pool->cq_lock, flags);
417 	xskq_prod_submit_addr(xs->pool->cq, addr);
418 	spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
419 
420 	sock_wfree(skb);
421 }
422 
423 static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
424 					      struct xdp_desc *desc)
425 {
426 	struct xsk_buff_pool *pool = xs->pool;
427 	u32 hr, len, ts, offset, copy, copied;
428 	struct sk_buff *skb;
429 	struct page *page;
430 	void *buffer;
431 	int err, i;
432 	u64 addr;
433 
434 	hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
435 
436 	skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
437 	if (unlikely(!skb))
438 		return ERR_PTR(err);
439 
440 	skb_reserve(skb, hr);
441 
442 	addr = desc->addr;
443 	len = desc->len;
444 	ts = pool->unaligned ? len : pool->chunk_size;
445 
446 	buffer = xsk_buff_raw_get_data(pool, addr);
447 	offset = offset_in_page(buffer);
448 	addr = buffer - pool->addrs;
449 
450 	for (copied = 0, i = 0; copied < len; i++) {
451 		page = pool->umem->pgs[addr >> PAGE_SHIFT];
452 		get_page(page);
453 
454 		copy = min_t(u32, PAGE_SIZE - offset, len - copied);
455 		skb_fill_page_desc(skb, i, page, offset, copy);
456 
457 		copied += copy;
458 		addr += copy;
459 		offset = 0;
460 	}
461 
462 	skb->len += len;
463 	skb->data_len += len;
464 	skb->truesize += ts;
465 
466 	refcount_add(ts, &xs->sk.sk_wmem_alloc);
467 
468 	return skb;
469 }
470 
471 static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
472 				     struct xdp_desc *desc)
473 {
474 	struct net_device *dev = xs->dev;
475 	struct sk_buff *skb;
476 
477 	if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
478 		skb = xsk_build_skb_zerocopy(xs, desc);
479 		if (IS_ERR(skb))
480 			return skb;
481 	} else {
482 		u32 hr, tr, len;
483 		void *buffer;
484 		int err;
485 
486 		hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
487 		tr = dev->needed_tailroom;
488 		len = desc->len;
489 
490 		skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
491 		if (unlikely(!skb))
492 			return ERR_PTR(err);
493 
494 		skb_reserve(skb, hr);
495 		skb_put(skb, len);
496 
497 		buffer = xsk_buff_raw_get_data(xs->pool, desc->addr);
498 		err = skb_store_bits(skb, 0, buffer, len);
499 		if (unlikely(err)) {
500 			kfree_skb(skb);
501 			return ERR_PTR(err);
502 		}
503 	}
504 
505 	skb->dev = dev;
506 	skb->priority = xs->sk.sk_priority;
507 	skb->mark = xs->sk.sk_mark;
508 	skb_shinfo(skb)->destructor_arg = (void *)(long)desc->addr;
509 	skb->destructor = xsk_destruct_skb;
510 
511 	return skb;
512 }
513 
514 static int xsk_generic_xmit(struct sock *sk)
515 {
516 	struct xdp_sock *xs = xdp_sk(sk);
517 	u32 max_batch = TX_BATCH_SIZE;
518 	bool sent_frame = false;
519 	struct xdp_desc desc;
520 	struct sk_buff *skb;
521 	unsigned long flags;
522 	int err = 0;
523 
524 	mutex_lock(&xs->mutex);
525 
526 	/* Since we dropped the RCU read lock, the socket state might have changed. */
527 	if (unlikely(!xsk_is_bound(xs))) {
528 		err = -ENXIO;
529 		goto out;
530 	}
531 
532 	if (xs->queue_id >= xs->dev->real_num_tx_queues)
533 		goto out;
534 
535 	while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
536 		if (max_batch-- == 0) {
537 			err = -EAGAIN;
538 			goto out;
539 		}
540 
541 		/* This is the backpressure mechanism for the Tx path.
542 		 * Reserve space in the completion queue and only proceed
543 		 * if there is space in it. This avoids having to implement
544 		 * any buffering in the Tx path.
545 		 */
546 		spin_lock_irqsave(&xs->pool->cq_lock, flags);
547 		if (xskq_prod_reserve(xs->pool->cq)) {
548 			spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
549 			goto out;
550 		}
551 		spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
552 
553 		skb = xsk_build_skb(xs, &desc);
554 		if (IS_ERR(skb)) {
555 			err = PTR_ERR(skb);
556 			spin_lock_irqsave(&xs->pool->cq_lock, flags);
557 			xskq_prod_cancel(xs->pool->cq);
558 			spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
559 			goto out;
560 		}
561 
562 		err = __dev_direct_xmit(skb, xs->queue_id);
563 		if  (err == NETDEV_TX_BUSY) {
564 			/* Tell user-space to retry the send */
565 			skb->destructor = sock_wfree;
566 			spin_lock_irqsave(&xs->pool->cq_lock, flags);
567 			xskq_prod_cancel(xs->pool->cq);
568 			spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
569 			/* Free skb without triggering the perf drop trace */
570 			consume_skb(skb);
571 			err = -EAGAIN;
572 			goto out;
573 		}
574 
575 		xskq_cons_release(xs->tx);
576 		/* Ignore NET_XMIT_CN as packet might have been sent */
577 		if (err == NET_XMIT_DROP) {
578 			/* SKB completed but not sent */
579 			err = -EBUSY;
580 			goto out;
581 		}
582 
583 		sent_frame = true;
584 	}
585 
586 	xs->tx->queue_empty_descs++;
587 
588 out:
589 	if (sent_frame)
590 		if (xsk_tx_writeable(xs))
591 			sk->sk_write_space(sk);
592 
593 	mutex_unlock(&xs->mutex);
594 	return err;
595 }
596 
597 static int xsk_xmit(struct sock *sk)
598 {
599 	struct xdp_sock *xs = xdp_sk(sk);
600 	int ret;
601 
602 	if (unlikely(!(xs->dev->flags & IFF_UP)))
603 		return -ENETDOWN;
604 	if (unlikely(!xs->tx))
605 		return -ENOBUFS;
606 
607 	if (xs->zc)
608 		return xsk_wakeup(xs, XDP_WAKEUP_TX);
609 
610 	/* Drop the RCU lock since the SKB path might sleep. */
611 	rcu_read_unlock();
612 	ret = xsk_generic_xmit(sk);
613 	/* Reaquire RCU lock before going into common code. */
614 	rcu_read_lock();
615 
616 	return ret;
617 }
618 
619 static bool xsk_no_wakeup(struct sock *sk)
620 {
621 #ifdef CONFIG_NET_RX_BUSY_POLL
622 	/* Prefer busy-polling, skip the wakeup. */
623 	return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
624 		READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
625 #else
626 	return false;
627 #endif
628 }
629 
630 static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
631 {
632 	bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
633 	struct sock *sk = sock->sk;
634 	struct xdp_sock *xs = xdp_sk(sk);
635 	struct xsk_buff_pool *pool;
636 
637 	if (unlikely(!xsk_is_bound(xs)))
638 		return -ENXIO;
639 	if (unlikely(need_wait))
640 		return -EOPNOTSUPP;
641 
642 	if (sk_can_busy_loop(sk)) {
643 		if (xs->zc)
644 			__sk_mark_napi_id_once(sk, xsk_pool_get_napi_id(xs->pool));
645 		sk_busy_loop(sk, 1); /* only support non-blocking sockets */
646 	}
647 
648 	if (xs->zc && xsk_no_wakeup(sk))
649 		return 0;
650 
651 	pool = xs->pool;
652 	if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
653 		return xsk_xmit(sk);
654 	return 0;
655 }
656 
657 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
658 {
659 	int ret;
660 
661 	rcu_read_lock();
662 	ret = __xsk_sendmsg(sock, m, total_len);
663 	rcu_read_unlock();
664 
665 	return ret;
666 }
667 
668 static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
669 {
670 	bool need_wait = !(flags & MSG_DONTWAIT);
671 	struct sock *sk = sock->sk;
672 	struct xdp_sock *xs = xdp_sk(sk);
673 
674 	if (unlikely(!xsk_is_bound(xs)))
675 		return -ENXIO;
676 	if (unlikely(!(xs->dev->flags & IFF_UP)))
677 		return -ENETDOWN;
678 	if (unlikely(!xs->rx))
679 		return -ENOBUFS;
680 	if (unlikely(need_wait))
681 		return -EOPNOTSUPP;
682 
683 	if (sk_can_busy_loop(sk))
684 		sk_busy_loop(sk, 1); /* only support non-blocking sockets */
685 
686 	if (xsk_no_wakeup(sk))
687 		return 0;
688 
689 	if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
690 		return xsk_wakeup(xs, XDP_WAKEUP_RX);
691 	return 0;
692 }
693 
694 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
695 {
696 	int ret;
697 
698 	rcu_read_lock();
699 	ret = __xsk_recvmsg(sock, m, len, flags);
700 	rcu_read_unlock();
701 
702 	return ret;
703 }
704 
705 static __poll_t xsk_poll(struct file *file, struct socket *sock,
706 			     struct poll_table_struct *wait)
707 {
708 	__poll_t mask = 0;
709 	struct sock *sk = sock->sk;
710 	struct xdp_sock *xs = xdp_sk(sk);
711 	struct xsk_buff_pool *pool;
712 
713 	sock_poll_wait(file, sock, wait);
714 
715 	rcu_read_lock();
716 	if (unlikely(!xsk_is_bound(xs))) {
717 		rcu_read_unlock();
718 		return mask;
719 	}
720 
721 	pool = xs->pool;
722 
723 	if (pool->cached_need_wakeup) {
724 		if (xs->zc)
725 			xsk_wakeup(xs, pool->cached_need_wakeup);
726 		else
727 			/* Poll needs to drive Tx also in copy mode */
728 			xsk_xmit(sk);
729 	}
730 
731 	if (xs->rx && !xskq_prod_is_empty(xs->rx))
732 		mask |= EPOLLIN | EPOLLRDNORM;
733 	if (xs->tx && xsk_tx_writeable(xs))
734 		mask |= EPOLLOUT | EPOLLWRNORM;
735 
736 	rcu_read_unlock();
737 	return mask;
738 }
739 
740 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
741 			  bool umem_queue)
742 {
743 	struct xsk_queue *q;
744 
745 	if (entries == 0 || *queue || !is_power_of_2(entries))
746 		return -EINVAL;
747 
748 	q = xskq_create(entries, umem_queue);
749 	if (!q)
750 		return -ENOMEM;
751 
752 	/* Make sure queue is ready before it can be seen by others */
753 	smp_wmb();
754 	WRITE_ONCE(*queue, q);
755 	return 0;
756 }
757 
758 static void xsk_unbind_dev(struct xdp_sock *xs)
759 {
760 	struct net_device *dev = xs->dev;
761 
762 	if (xs->state != XSK_BOUND)
763 		return;
764 	WRITE_ONCE(xs->state, XSK_UNBOUND);
765 
766 	/* Wait for driver to stop using the xdp socket. */
767 	xp_del_xsk(xs->pool, xs);
768 	synchronize_net();
769 	dev_put(dev);
770 }
771 
772 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
773 					      struct xdp_sock __rcu ***map_entry)
774 {
775 	struct xsk_map *map = NULL;
776 	struct xsk_map_node *node;
777 
778 	*map_entry = NULL;
779 
780 	spin_lock_bh(&xs->map_list_lock);
781 	node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
782 					node);
783 	if (node) {
784 		bpf_map_inc(&node->map->map);
785 		map = node->map;
786 		*map_entry = node->map_entry;
787 	}
788 	spin_unlock_bh(&xs->map_list_lock);
789 	return map;
790 }
791 
792 static void xsk_delete_from_maps(struct xdp_sock *xs)
793 {
794 	/* This function removes the current XDP socket from all the
795 	 * maps it resides in. We need to take extra care here, due to
796 	 * the two locks involved. Each map has a lock synchronizing
797 	 * updates to the entries, and each socket has a lock that
798 	 * synchronizes access to the list of maps (map_list). For
799 	 * deadlock avoidance the locks need to be taken in the order
800 	 * "map lock"->"socket map list lock". We start off by
801 	 * accessing the socket map list, and take a reference to the
802 	 * map to guarantee existence between the
803 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
804 	 * calls. Then we ask the map to remove the socket, which
805 	 * tries to remove the socket from the map. Note that there
806 	 * might be updates to the map between
807 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
808 	 */
809 	struct xdp_sock __rcu **map_entry = NULL;
810 	struct xsk_map *map;
811 
812 	while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
813 		xsk_map_try_sock_delete(map, xs, map_entry);
814 		bpf_map_put(&map->map);
815 	}
816 }
817 
818 static int xsk_release(struct socket *sock)
819 {
820 	struct sock *sk = sock->sk;
821 	struct xdp_sock *xs = xdp_sk(sk);
822 	struct net *net;
823 
824 	if (!sk)
825 		return 0;
826 
827 	net = sock_net(sk);
828 
829 	mutex_lock(&net->xdp.lock);
830 	sk_del_node_init_rcu(sk);
831 	mutex_unlock(&net->xdp.lock);
832 
833 	sock_prot_inuse_add(net, sk->sk_prot, -1);
834 
835 	xsk_delete_from_maps(xs);
836 	mutex_lock(&xs->mutex);
837 	xsk_unbind_dev(xs);
838 	mutex_unlock(&xs->mutex);
839 
840 	xskq_destroy(xs->rx);
841 	xskq_destroy(xs->tx);
842 	xskq_destroy(xs->fq_tmp);
843 	xskq_destroy(xs->cq_tmp);
844 
845 	sock_orphan(sk);
846 	sock->sk = NULL;
847 
848 	sk_refcnt_debug_release(sk);
849 	sock_put(sk);
850 
851 	return 0;
852 }
853 
854 static struct socket *xsk_lookup_xsk_from_fd(int fd)
855 {
856 	struct socket *sock;
857 	int err;
858 
859 	sock = sockfd_lookup(fd, &err);
860 	if (!sock)
861 		return ERR_PTR(-ENOTSOCK);
862 
863 	if (sock->sk->sk_family != PF_XDP) {
864 		sockfd_put(sock);
865 		return ERR_PTR(-ENOPROTOOPT);
866 	}
867 
868 	return sock;
869 }
870 
871 static bool xsk_validate_queues(struct xdp_sock *xs)
872 {
873 	return xs->fq_tmp && xs->cq_tmp;
874 }
875 
876 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
877 {
878 	struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
879 	struct sock *sk = sock->sk;
880 	struct xdp_sock *xs = xdp_sk(sk);
881 	struct net_device *dev;
882 	u32 flags, qid;
883 	int err = 0;
884 
885 	if (addr_len < sizeof(struct sockaddr_xdp))
886 		return -EINVAL;
887 	if (sxdp->sxdp_family != AF_XDP)
888 		return -EINVAL;
889 
890 	flags = sxdp->sxdp_flags;
891 	if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
892 		      XDP_USE_NEED_WAKEUP))
893 		return -EINVAL;
894 
895 	rtnl_lock();
896 	mutex_lock(&xs->mutex);
897 	if (xs->state != XSK_READY) {
898 		err = -EBUSY;
899 		goto out_release;
900 	}
901 
902 	dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
903 	if (!dev) {
904 		err = -ENODEV;
905 		goto out_release;
906 	}
907 
908 	if (!xs->rx && !xs->tx) {
909 		err = -EINVAL;
910 		goto out_unlock;
911 	}
912 
913 	qid = sxdp->sxdp_queue_id;
914 
915 	if (flags & XDP_SHARED_UMEM) {
916 		struct xdp_sock *umem_xs;
917 		struct socket *sock;
918 
919 		if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
920 		    (flags & XDP_USE_NEED_WAKEUP)) {
921 			/* Cannot specify flags for shared sockets. */
922 			err = -EINVAL;
923 			goto out_unlock;
924 		}
925 
926 		if (xs->umem) {
927 			/* We have already our own. */
928 			err = -EINVAL;
929 			goto out_unlock;
930 		}
931 
932 		sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
933 		if (IS_ERR(sock)) {
934 			err = PTR_ERR(sock);
935 			goto out_unlock;
936 		}
937 
938 		umem_xs = xdp_sk(sock->sk);
939 		if (!xsk_is_bound(umem_xs)) {
940 			err = -EBADF;
941 			sockfd_put(sock);
942 			goto out_unlock;
943 		}
944 
945 		if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
946 			/* Share the umem with another socket on another qid
947 			 * and/or device.
948 			 */
949 			xs->pool = xp_create_and_assign_umem(xs,
950 							     umem_xs->umem);
951 			if (!xs->pool) {
952 				err = -ENOMEM;
953 				sockfd_put(sock);
954 				goto out_unlock;
955 			}
956 
957 			err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
958 						   dev, qid);
959 			if (err) {
960 				xp_destroy(xs->pool);
961 				xs->pool = NULL;
962 				sockfd_put(sock);
963 				goto out_unlock;
964 			}
965 		} else {
966 			/* Share the buffer pool with the other socket. */
967 			if (xs->fq_tmp || xs->cq_tmp) {
968 				/* Do not allow setting your own fq or cq. */
969 				err = -EINVAL;
970 				sockfd_put(sock);
971 				goto out_unlock;
972 			}
973 
974 			xp_get_pool(umem_xs->pool);
975 			xs->pool = umem_xs->pool;
976 
977 			/* If underlying shared umem was created without Tx
978 			 * ring, allocate Tx descs array that Tx batching API
979 			 * utilizes
980 			 */
981 			if (xs->tx && !xs->pool->tx_descs) {
982 				err = xp_alloc_tx_descs(xs->pool, xs);
983 				if (err) {
984 					xp_put_pool(xs->pool);
985 					sockfd_put(sock);
986 					goto out_unlock;
987 				}
988 			}
989 		}
990 
991 		xdp_get_umem(umem_xs->umem);
992 		WRITE_ONCE(xs->umem, umem_xs->umem);
993 		sockfd_put(sock);
994 	} else if (!xs->umem || !xsk_validate_queues(xs)) {
995 		err = -EINVAL;
996 		goto out_unlock;
997 	} else {
998 		/* This xsk has its own umem. */
999 		xs->pool = xp_create_and_assign_umem(xs, xs->umem);
1000 		if (!xs->pool) {
1001 			err = -ENOMEM;
1002 			goto out_unlock;
1003 		}
1004 
1005 		err = xp_assign_dev(xs->pool, dev, qid, flags);
1006 		if (err) {
1007 			xp_destroy(xs->pool);
1008 			xs->pool = NULL;
1009 			goto out_unlock;
1010 		}
1011 	}
1012 
1013 	/* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
1014 	xs->fq_tmp = NULL;
1015 	xs->cq_tmp = NULL;
1016 
1017 	xs->dev = dev;
1018 	xs->zc = xs->umem->zc;
1019 	xs->queue_id = qid;
1020 	xp_add_xsk(xs->pool, xs);
1021 
1022 out_unlock:
1023 	if (err) {
1024 		dev_put(dev);
1025 	} else {
1026 		/* Matches smp_rmb() in bind() for shared umem
1027 		 * sockets, and xsk_is_bound().
1028 		 */
1029 		smp_wmb();
1030 		WRITE_ONCE(xs->state, XSK_BOUND);
1031 	}
1032 out_release:
1033 	mutex_unlock(&xs->mutex);
1034 	rtnl_unlock();
1035 	return err;
1036 }
1037 
1038 struct xdp_umem_reg_v1 {
1039 	__u64 addr; /* Start of packet data area */
1040 	__u64 len; /* Length of packet data area */
1041 	__u32 chunk_size;
1042 	__u32 headroom;
1043 };
1044 
1045 static int xsk_setsockopt(struct socket *sock, int level, int optname,
1046 			  sockptr_t optval, unsigned int optlen)
1047 {
1048 	struct sock *sk = sock->sk;
1049 	struct xdp_sock *xs = xdp_sk(sk);
1050 	int err;
1051 
1052 	if (level != SOL_XDP)
1053 		return -ENOPROTOOPT;
1054 
1055 	switch (optname) {
1056 	case XDP_RX_RING:
1057 	case XDP_TX_RING:
1058 	{
1059 		struct xsk_queue **q;
1060 		int entries;
1061 
1062 		if (optlen < sizeof(entries))
1063 			return -EINVAL;
1064 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1065 			return -EFAULT;
1066 
1067 		mutex_lock(&xs->mutex);
1068 		if (xs->state != XSK_READY) {
1069 			mutex_unlock(&xs->mutex);
1070 			return -EBUSY;
1071 		}
1072 		q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
1073 		err = xsk_init_queue(entries, q, false);
1074 		if (!err && optname == XDP_TX_RING)
1075 			/* Tx needs to be explicitly woken up the first time */
1076 			xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
1077 		mutex_unlock(&xs->mutex);
1078 		return err;
1079 	}
1080 	case XDP_UMEM_REG:
1081 	{
1082 		size_t mr_size = sizeof(struct xdp_umem_reg);
1083 		struct xdp_umem_reg mr = {};
1084 		struct xdp_umem *umem;
1085 
1086 		if (optlen < sizeof(struct xdp_umem_reg_v1))
1087 			return -EINVAL;
1088 		else if (optlen < sizeof(mr))
1089 			mr_size = sizeof(struct xdp_umem_reg_v1);
1090 
1091 		if (copy_from_sockptr(&mr, optval, mr_size))
1092 			return -EFAULT;
1093 
1094 		mutex_lock(&xs->mutex);
1095 		if (xs->state != XSK_READY || xs->umem) {
1096 			mutex_unlock(&xs->mutex);
1097 			return -EBUSY;
1098 		}
1099 
1100 		umem = xdp_umem_create(&mr);
1101 		if (IS_ERR(umem)) {
1102 			mutex_unlock(&xs->mutex);
1103 			return PTR_ERR(umem);
1104 		}
1105 
1106 		/* Make sure umem is ready before it can be seen by others */
1107 		smp_wmb();
1108 		WRITE_ONCE(xs->umem, umem);
1109 		mutex_unlock(&xs->mutex);
1110 		return 0;
1111 	}
1112 	case XDP_UMEM_FILL_RING:
1113 	case XDP_UMEM_COMPLETION_RING:
1114 	{
1115 		struct xsk_queue **q;
1116 		int entries;
1117 
1118 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
1119 			return -EFAULT;
1120 
1121 		mutex_lock(&xs->mutex);
1122 		if (xs->state != XSK_READY) {
1123 			mutex_unlock(&xs->mutex);
1124 			return -EBUSY;
1125 		}
1126 
1127 		q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1128 			&xs->cq_tmp;
1129 		err = xsk_init_queue(entries, q, true);
1130 		mutex_unlock(&xs->mutex);
1131 		return err;
1132 	}
1133 	default:
1134 		break;
1135 	}
1136 
1137 	return -ENOPROTOOPT;
1138 }
1139 
1140 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1141 {
1142 	ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1143 	ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1144 	ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1145 }
1146 
1147 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1148 {
1149 	ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1150 	ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1151 	ring->desc = offsetof(struct xdp_umem_ring, desc);
1152 }
1153 
1154 struct xdp_statistics_v1 {
1155 	__u64 rx_dropped;
1156 	__u64 rx_invalid_descs;
1157 	__u64 tx_invalid_descs;
1158 };
1159 
1160 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1161 			  char __user *optval, int __user *optlen)
1162 {
1163 	struct sock *sk = sock->sk;
1164 	struct xdp_sock *xs = xdp_sk(sk);
1165 	int len;
1166 
1167 	if (level != SOL_XDP)
1168 		return -ENOPROTOOPT;
1169 
1170 	if (get_user(len, optlen))
1171 		return -EFAULT;
1172 	if (len < 0)
1173 		return -EINVAL;
1174 
1175 	switch (optname) {
1176 	case XDP_STATISTICS:
1177 	{
1178 		struct xdp_statistics stats = {};
1179 		bool extra_stats = true;
1180 		size_t stats_size;
1181 
1182 		if (len < sizeof(struct xdp_statistics_v1)) {
1183 			return -EINVAL;
1184 		} else if (len < sizeof(stats)) {
1185 			extra_stats = false;
1186 			stats_size = sizeof(struct xdp_statistics_v1);
1187 		} else {
1188 			stats_size = sizeof(stats);
1189 		}
1190 
1191 		mutex_lock(&xs->mutex);
1192 		stats.rx_dropped = xs->rx_dropped;
1193 		if (extra_stats) {
1194 			stats.rx_ring_full = xs->rx_queue_full;
1195 			stats.rx_fill_ring_empty_descs =
1196 				xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1197 			stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1198 		} else {
1199 			stats.rx_dropped += xs->rx_queue_full;
1200 		}
1201 		stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1202 		stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1203 		mutex_unlock(&xs->mutex);
1204 
1205 		if (copy_to_user(optval, &stats, stats_size))
1206 			return -EFAULT;
1207 		if (put_user(stats_size, optlen))
1208 			return -EFAULT;
1209 
1210 		return 0;
1211 	}
1212 	case XDP_MMAP_OFFSETS:
1213 	{
1214 		struct xdp_mmap_offsets off;
1215 		struct xdp_mmap_offsets_v1 off_v1;
1216 		bool flags_supported = true;
1217 		void *to_copy;
1218 
1219 		if (len < sizeof(off_v1))
1220 			return -EINVAL;
1221 		else if (len < sizeof(off))
1222 			flags_supported = false;
1223 
1224 		if (flags_supported) {
1225 			/* xdp_ring_offset is identical to xdp_ring_offset_v1
1226 			 * except for the flags field added to the end.
1227 			 */
1228 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1229 					       &off.rx);
1230 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1231 					       &off.tx);
1232 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1233 					       &off.fr);
1234 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1235 					       &off.cr);
1236 			off.rx.flags = offsetof(struct xdp_rxtx_ring,
1237 						ptrs.flags);
1238 			off.tx.flags = offsetof(struct xdp_rxtx_ring,
1239 						ptrs.flags);
1240 			off.fr.flags = offsetof(struct xdp_umem_ring,
1241 						ptrs.flags);
1242 			off.cr.flags = offsetof(struct xdp_umem_ring,
1243 						ptrs.flags);
1244 
1245 			len = sizeof(off);
1246 			to_copy = &off;
1247 		} else {
1248 			xsk_enter_rxtx_offsets(&off_v1.rx);
1249 			xsk_enter_rxtx_offsets(&off_v1.tx);
1250 			xsk_enter_umem_offsets(&off_v1.fr);
1251 			xsk_enter_umem_offsets(&off_v1.cr);
1252 
1253 			len = sizeof(off_v1);
1254 			to_copy = &off_v1;
1255 		}
1256 
1257 		if (copy_to_user(optval, to_copy, len))
1258 			return -EFAULT;
1259 		if (put_user(len, optlen))
1260 			return -EFAULT;
1261 
1262 		return 0;
1263 	}
1264 	case XDP_OPTIONS:
1265 	{
1266 		struct xdp_options opts = {};
1267 
1268 		if (len < sizeof(opts))
1269 			return -EINVAL;
1270 
1271 		mutex_lock(&xs->mutex);
1272 		if (xs->zc)
1273 			opts.flags |= XDP_OPTIONS_ZEROCOPY;
1274 		mutex_unlock(&xs->mutex);
1275 
1276 		len = sizeof(opts);
1277 		if (copy_to_user(optval, &opts, len))
1278 			return -EFAULT;
1279 		if (put_user(len, optlen))
1280 			return -EFAULT;
1281 
1282 		return 0;
1283 	}
1284 	default:
1285 		break;
1286 	}
1287 
1288 	return -EOPNOTSUPP;
1289 }
1290 
1291 static int xsk_mmap(struct file *file, struct socket *sock,
1292 		    struct vm_area_struct *vma)
1293 {
1294 	loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1295 	unsigned long size = vma->vm_end - vma->vm_start;
1296 	struct xdp_sock *xs = xdp_sk(sock->sk);
1297 	struct xsk_queue *q = NULL;
1298 	unsigned long pfn;
1299 	struct page *qpg;
1300 
1301 	if (READ_ONCE(xs->state) != XSK_READY)
1302 		return -EBUSY;
1303 
1304 	if (offset == XDP_PGOFF_RX_RING) {
1305 		q = READ_ONCE(xs->rx);
1306 	} else if (offset == XDP_PGOFF_TX_RING) {
1307 		q = READ_ONCE(xs->tx);
1308 	} else {
1309 		/* Matches the smp_wmb() in XDP_UMEM_REG */
1310 		smp_rmb();
1311 		if (offset == XDP_UMEM_PGOFF_FILL_RING)
1312 			q = READ_ONCE(xs->fq_tmp);
1313 		else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1314 			q = READ_ONCE(xs->cq_tmp);
1315 	}
1316 
1317 	if (!q)
1318 		return -EINVAL;
1319 
1320 	/* Matches the smp_wmb() in xsk_init_queue */
1321 	smp_rmb();
1322 	qpg = virt_to_head_page(q->ring);
1323 	if (size > page_size(qpg))
1324 		return -EINVAL;
1325 
1326 	pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1327 	return remap_pfn_range(vma, vma->vm_start, pfn,
1328 			       size, vma->vm_page_prot);
1329 }
1330 
1331 static int xsk_notifier(struct notifier_block *this,
1332 			unsigned long msg, void *ptr)
1333 {
1334 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1335 	struct net *net = dev_net(dev);
1336 	struct sock *sk;
1337 
1338 	switch (msg) {
1339 	case NETDEV_UNREGISTER:
1340 		mutex_lock(&net->xdp.lock);
1341 		sk_for_each(sk, &net->xdp.list) {
1342 			struct xdp_sock *xs = xdp_sk(sk);
1343 
1344 			mutex_lock(&xs->mutex);
1345 			if (xs->dev == dev) {
1346 				sk->sk_err = ENETDOWN;
1347 				if (!sock_flag(sk, SOCK_DEAD))
1348 					sk_error_report(sk);
1349 
1350 				xsk_unbind_dev(xs);
1351 
1352 				/* Clear device references. */
1353 				xp_clear_dev(xs->pool);
1354 			}
1355 			mutex_unlock(&xs->mutex);
1356 		}
1357 		mutex_unlock(&net->xdp.lock);
1358 		break;
1359 	}
1360 	return NOTIFY_DONE;
1361 }
1362 
1363 static struct proto xsk_proto = {
1364 	.name =		"XDP",
1365 	.owner =	THIS_MODULE,
1366 	.obj_size =	sizeof(struct xdp_sock),
1367 };
1368 
1369 static const struct proto_ops xsk_proto_ops = {
1370 	.family		= PF_XDP,
1371 	.owner		= THIS_MODULE,
1372 	.release	= xsk_release,
1373 	.bind		= xsk_bind,
1374 	.connect	= sock_no_connect,
1375 	.socketpair	= sock_no_socketpair,
1376 	.accept		= sock_no_accept,
1377 	.getname	= sock_no_getname,
1378 	.poll		= xsk_poll,
1379 	.ioctl		= sock_no_ioctl,
1380 	.listen		= sock_no_listen,
1381 	.shutdown	= sock_no_shutdown,
1382 	.setsockopt	= xsk_setsockopt,
1383 	.getsockopt	= xsk_getsockopt,
1384 	.sendmsg	= xsk_sendmsg,
1385 	.recvmsg	= xsk_recvmsg,
1386 	.mmap		= xsk_mmap,
1387 	.sendpage	= sock_no_sendpage,
1388 };
1389 
1390 static void xsk_destruct(struct sock *sk)
1391 {
1392 	struct xdp_sock *xs = xdp_sk(sk);
1393 
1394 	if (!sock_flag(sk, SOCK_DEAD))
1395 		return;
1396 
1397 	if (!xp_put_pool(xs->pool))
1398 		xdp_put_umem(xs->umem, !xs->pool);
1399 
1400 	sk_refcnt_debug_dec(sk);
1401 }
1402 
1403 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1404 		      int kern)
1405 {
1406 	struct xdp_sock *xs;
1407 	struct sock *sk;
1408 
1409 	if (!ns_capable(net->user_ns, CAP_NET_RAW))
1410 		return -EPERM;
1411 	if (sock->type != SOCK_RAW)
1412 		return -ESOCKTNOSUPPORT;
1413 
1414 	if (protocol)
1415 		return -EPROTONOSUPPORT;
1416 
1417 	sock->state = SS_UNCONNECTED;
1418 
1419 	sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1420 	if (!sk)
1421 		return -ENOBUFS;
1422 
1423 	sock->ops = &xsk_proto_ops;
1424 
1425 	sock_init_data(sock, sk);
1426 
1427 	sk->sk_family = PF_XDP;
1428 
1429 	sk->sk_destruct = xsk_destruct;
1430 	sk_refcnt_debug_inc(sk);
1431 
1432 	sock_set_flag(sk, SOCK_RCU_FREE);
1433 
1434 	xs = xdp_sk(sk);
1435 	xs->state = XSK_READY;
1436 	mutex_init(&xs->mutex);
1437 	spin_lock_init(&xs->rx_lock);
1438 
1439 	INIT_LIST_HEAD(&xs->map_list);
1440 	spin_lock_init(&xs->map_list_lock);
1441 
1442 	mutex_lock(&net->xdp.lock);
1443 	sk_add_node_rcu(sk, &net->xdp.list);
1444 	mutex_unlock(&net->xdp.lock);
1445 
1446 	sock_prot_inuse_add(net, &xsk_proto, 1);
1447 
1448 	return 0;
1449 }
1450 
1451 static const struct net_proto_family xsk_family_ops = {
1452 	.family = PF_XDP,
1453 	.create = xsk_create,
1454 	.owner	= THIS_MODULE,
1455 };
1456 
1457 static struct notifier_block xsk_netdev_notifier = {
1458 	.notifier_call	= xsk_notifier,
1459 };
1460 
1461 static int __net_init xsk_net_init(struct net *net)
1462 {
1463 	mutex_init(&net->xdp.lock);
1464 	INIT_HLIST_HEAD(&net->xdp.list);
1465 	return 0;
1466 }
1467 
1468 static void __net_exit xsk_net_exit(struct net *net)
1469 {
1470 	WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1471 }
1472 
1473 static struct pernet_operations xsk_net_ops = {
1474 	.init = xsk_net_init,
1475 	.exit = xsk_net_exit,
1476 };
1477 
1478 static int __init xsk_init(void)
1479 {
1480 	int err, cpu;
1481 
1482 	err = proto_register(&xsk_proto, 0 /* no slab */);
1483 	if (err)
1484 		goto out;
1485 
1486 	err = sock_register(&xsk_family_ops);
1487 	if (err)
1488 		goto out_proto;
1489 
1490 	err = register_pernet_subsys(&xsk_net_ops);
1491 	if (err)
1492 		goto out_sk;
1493 
1494 	err = register_netdevice_notifier(&xsk_netdev_notifier);
1495 	if (err)
1496 		goto out_pernet;
1497 
1498 	for_each_possible_cpu(cpu)
1499 		INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1500 	return 0;
1501 
1502 out_pernet:
1503 	unregister_pernet_subsys(&xsk_net_ops);
1504 out_sk:
1505 	sock_unregister(PF_XDP);
1506 out_proto:
1507 	proto_unregister(&xsk_proto);
1508 out:
1509 	return err;
1510 }
1511 
1512 fs_initcall(xsk_init);
1513