xref: /openbmc/linux/net/xdp/xsk.c (revision 81113b04)
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/xdp.h>
27 
28 #include "xsk_queue.h"
29 #include "xdp_umem.h"
30 #include "xsk.h"
31 
32 #define TX_BATCH_SIZE 16
33 
34 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
35 
36 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
37 {
38 	if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
39 		return;
40 
41 	pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
42 	pool->cached_need_wakeup |= XDP_WAKEUP_RX;
43 }
44 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
45 
46 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
47 {
48 	struct xdp_sock *xs;
49 
50 	if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
51 		return;
52 
53 	rcu_read_lock();
54 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
55 		xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
56 	}
57 	rcu_read_unlock();
58 
59 	pool->cached_need_wakeup |= XDP_WAKEUP_TX;
60 }
61 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
62 
63 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
64 {
65 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
66 		return;
67 
68 	pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
69 	pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
70 }
71 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
72 
73 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
74 {
75 	struct xdp_sock *xs;
76 
77 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
78 		return;
79 
80 	rcu_read_lock();
81 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
82 		xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
83 	}
84 	rcu_read_unlock();
85 
86 	pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
87 }
88 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
89 
90 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
91 {
92 	return pool->uses_need_wakeup;
93 }
94 EXPORT_SYMBOL(xsk_uses_need_wakeup);
95 
96 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
97 					    u16 queue_id)
98 {
99 	if (queue_id < dev->real_num_rx_queues)
100 		return dev->_rx[queue_id].pool;
101 	if (queue_id < dev->real_num_tx_queues)
102 		return dev->_tx[queue_id].pool;
103 
104 	return NULL;
105 }
106 EXPORT_SYMBOL(xsk_get_pool_from_qid);
107 
108 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
109 {
110 	if (queue_id < dev->real_num_rx_queues)
111 		dev->_rx[queue_id].pool = NULL;
112 	if (queue_id < dev->real_num_tx_queues)
113 		dev->_tx[queue_id].pool = NULL;
114 }
115 
116 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
117  * not know if the device has more tx queues than rx, or the opposite.
118  * This might also change during run time.
119  */
120 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
121 			u16 queue_id)
122 {
123 	if (queue_id >= max_t(unsigned int,
124 			      dev->real_num_rx_queues,
125 			      dev->real_num_tx_queues))
126 		return -EINVAL;
127 
128 	if (queue_id < dev->real_num_rx_queues)
129 		dev->_rx[queue_id].pool = pool;
130 	if (queue_id < dev->real_num_tx_queues)
131 		dev->_tx[queue_id].pool = pool;
132 
133 	return 0;
134 }
135 
136 void xp_release(struct xdp_buff_xsk *xskb)
137 {
138 	xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
139 }
140 
141 static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
142 {
143 	u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
144 
145 	offset += xskb->pool->headroom;
146 	if (!xskb->pool->unaligned)
147 		return xskb->orig_addr + offset;
148 	return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
149 }
150 
151 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
152 {
153 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
154 	u64 addr;
155 	int err;
156 
157 	addr = xp_get_handle(xskb);
158 	err = xskq_prod_reserve_desc(xs->rx, addr, len);
159 	if (err) {
160 		xs->rx_queue_full++;
161 		return err;
162 	}
163 
164 	xp_release(xskb);
165 	return 0;
166 }
167 
168 static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
169 {
170 	void *from_buf, *to_buf;
171 	u32 metalen;
172 
173 	if (unlikely(xdp_data_meta_unsupported(from))) {
174 		from_buf = from->data;
175 		to_buf = to->data;
176 		metalen = 0;
177 	} else {
178 		from_buf = from->data_meta;
179 		metalen = from->data - from->data_meta;
180 		to_buf = to->data - metalen;
181 	}
182 
183 	memcpy(to_buf, from_buf, len + metalen);
184 }
185 
186 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len,
187 		     bool explicit_free)
188 {
189 	struct xdp_buff *xsk_xdp;
190 	int err;
191 
192 	if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
193 		xs->rx_dropped++;
194 		return -ENOSPC;
195 	}
196 
197 	xsk_xdp = xsk_buff_alloc(xs->pool);
198 	if (!xsk_xdp) {
199 		xs->rx_dropped++;
200 		return -ENOSPC;
201 	}
202 
203 	xsk_copy_xdp(xsk_xdp, xdp, len);
204 	err = __xsk_rcv_zc(xs, xsk_xdp, len);
205 	if (err) {
206 		xsk_buff_free(xsk_xdp);
207 		return err;
208 	}
209 	if (explicit_free)
210 		xdp_return_buff(xdp);
211 	return 0;
212 }
213 
214 static bool xsk_is_bound(struct xdp_sock *xs)
215 {
216 	if (READ_ONCE(xs->state) == XSK_BOUND) {
217 		/* Matches smp_wmb() in bind(). */
218 		smp_rmb();
219 		return true;
220 	}
221 	return false;
222 }
223 
224 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp,
225 		   bool explicit_free)
226 {
227 	u32 len;
228 
229 	if (!xsk_is_bound(xs))
230 		return -EINVAL;
231 
232 	if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
233 		return -EINVAL;
234 
235 	len = xdp->data_end - xdp->data;
236 
237 	return xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL ?
238 		__xsk_rcv_zc(xs, xdp, len) :
239 		__xsk_rcv(xs, xdp, len, explicit_free);
240 }
241 
242 static void xsk_flush(struct xdp_sock *xs)
243 {
244 	xskq_prod_submit(xs->rx);
245 	__xskq_cons_release(xs->pool->fq);
246 	sock_def_readable(&xs->sk);
247 }
248 
249 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
250 {
251 	int err;
252 
253 	spin_lock_bh(&xs->rx_lock);
254 	err = xsk_rcv(xs, xdp, false);
255 	xsk_flush(xs);
256 	spin_unlock_bh(&xs->rx_lock);
257 	return err;
258 }
259 
260 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
261 {
262 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
263 	int err;
264 
265 	err = xsk_rcv(xs, xdp, true);
266 	if (err)
267 		return err;
268 
269 	if (!xs->flush_node.prev)
270 		list_add(&xs->flush_node, flush_list);
271 
272 	return 0;
273 }
274 
275 void __xsk_map_flush(void)
276 {
277 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
278 	struct xdp_sock *xs, *tmp;
279 
280 	list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
281 		xsk_flush(xs);
282 		__list_del_clearprev(&xs->flush_node);
283 	}
284 }
285 
286 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
287 {
288 	xskq_prod_submit_n(pool->cq, nb_entries);
289 }
290 EXPORT_SYMBOL(xsk_tx_completed);
291 
292 void xsk_tx_release(struct xsk_buff_pool *pool)
293 {
294 	struct xdp_sock *xs;
295 
296 	rcu_read_lock();
297 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
298 		__xskq_cons_release(xs->tx);
299 		xs->sk.sk_write_space(&xs->sk);
300 	}
301 	rcu_read_unlock();
302 }
303 EXPORT_SYMBOL(xsk_tx_release);
304 
305 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
306 {
307 	struct xdp_sock *xs;
308 
309 	rcu_read_lock();
310 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
311 		if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
312 			xs->tx->queue_empty_descs++;
313 			continue;
314 		}
315 
316 		/* This is the backpressure mechanism for the Tx path.
317 		 * Reserve space in the completion queue and only proceed
318 		 * if there is space in it. This avoids having to implement
319 		 * any buffering in the Tx path.
320 		 */
321 		if (xskq_prod_reserve_addr(pool->cq, desc->addr))
322 			goto out;
323 
324 		xskq_cons_release(xs->tx);
325 		rcu_read_unlock();
326 		return true;
327 	}
328 
329 out:
330 	rcu_read_unlock();
331 	return false;
332 }
333 EXPORT_SYMBOL(xsk_tx_peek_desc);
334 
335 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
336 {
337 	struct net_device *dev = xs->dev;
338 	int err;
339 
340 	rcu_read_lock();
341 	err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
342 	rcu_read_unlock();
343 
344 	return err;
345 }
346 
347 static int xsk_zc_xmit(struct xdp_sock *xs)
348 {
349 	return xsk_wakeup(xs, XDP_WAKEUP_TX);
350 }
351 
352 static void xsk_destruct_skb(struct sk_buff *skb)
353 {
354 	u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
355 	struct xdp_sock *xs = xdp_sk(skb->sk);
356 	unsigned long flags;
357 
358 	spin_lock_irqsave(&xs->tx_completion_lock, flags);
359 	xskq_prod_submit_addr(xs->pool->cq, addr);
360 	spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
361 
362 	sock_wfree(skb);
363 }
364 
365 static int xsk_generic_xmit(struct sock *sk)
366 {
367 	struct xdp_sock *xs = xdp_sk(sk);
368 	u32 max_batch = TX_BATCH_SIZE;
369 	bool sent_frame = false;
370 	struct xdp_desc desc;
371 	struct sk_buff *skb;
372 	int err = 0;
373 
374 	mutex_lock(&xs->mutex);
375 
376 	if (xs->queue_id >= xs->dev->real_num_tx_queues)
377 		goto out;
378 
379 	while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
380 		char *buffer;
381 		u64 addr;
382 		u32 len;
383 
384 		if (max_batch-- == 0) {
385 			err = -EAGAIN;
386 			goto out;
387 		}
388 
389 		len = desc.len;
390 		skb = sock_alloc_send_skb(sk, len, 1, &err);
391 		if (unlikely(!skb))
392 			goto out;
393 
394 		skb_put(skb, len);
395 		addr = desc.addr;
396 		buffer = xsk_buff_raw_get_data(xs->pool, addr);
397 		err = skb_store_bits(skb, 0, buffer, len);
398 		/* This is the backpressure mechanism for the Tx path.
399 		 * Reserve space in the completion queue and only proceed
400 		 * if there is space in it. This avoids having to implement
401 		 * any buffering in the Tx path.
402 		 */
403 		if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) {
404 			kfree_skb(skb);
405 			goto out;
406 		}
407 
408 		skb->dev = xs->dev;
409 		skb->priority = sk->sk_priority;
410 		skb->mark = sk->sk_mark;
411 		skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
412 		skb->destructor = xsk_destruct_skb;
413 
414 		err = __dev_direct_xmit(skb, xs->queue_id);
415 		if  (err == NETDEV_TX_BUSY) {
416 			/* Tell user-space to retry the send */
417 			skb->destructor = sock_wfree;
418 			/* Free skb without triggering the perf drop trace */
419 			consume_skb(skb);
420 			err = -EAGAIN;
421 			goto out;
422 		}
423 
424 		xskq_cons_release(xs->tx);
425 		/* Ignore NET_XMIT_CN as packet might have been sent */
426 		if (err == NET_XMIT_DROP) {
427 			/* SKB completed but not sent */
428 			err = -EBUSY;
429 			goto out;
430 		}
431 
432 		sent_frame = true;
433 	}
434 
435 	xs->tx->queue_empty_descs++;
436 
437 out:
438 	if (sent_frame)
439 		sk->sk_write_space(sk);
440 
441 	mutex_unlock(&xs->mutex);
442 	return err;
443 }
444 
445 static int __xsk_sendmsg(struct sock *sk)
446 {
447 	struct xdp_sock *xs = xdp_sk(sk);
448 
449 	if (unlikely(!(xs->dev->flags & IFF_UP)))
450 		return -ENETDOWN;
451 	if (unlikely(!xs->tx))
452 		return -ENOBUFS;
453 
454 	return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
455 }
456 
457 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
458 {
459 	bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
460 	struct sock *sk = sock->sk;
461 	struct xdp_sock *xs = xdp_sk(sk);
462 
463 	if (unlikely(!xsk_is_bound(xs)))
464 		return -ENXIO;
465 	if (unlikely(need_wait))
466 		return -EOPNOTSUPP;
467 
468 	return __xsk_sendmsg(sk);
469 }
470 
471 static __poll_t xsk_poll(struct file *file, struct socket *sock,
472 			     struct poll_table_struct *wait)
473 {
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 xsk_buff_pool *pool;
478 
479 	if (unlikely(!xsk_is_bound(xs)))
480 		return mask;
481 
482 	pool = xs->pool;
483 
484 	if (pool->cached_need_wakeup) {
485 		if (xs->zc)
486 			xsk_wakeup(xs, pool->cached_need_wakeup);
487 		else
488 			/* Poll needs to drive Tx also in copy mode */
489 			__xsk_sendmsg(sk);
490 	}
491 
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;
496 
497 	return mask;
498 }
499 
500 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
501 			  bool umem_queue)
502 {
503 	struct xsk_queue *q;
504 
505 	if (entries == 0 || *queue || !is_power_of_2(entries))
506 		return -EINVAL;
507 
508 	q = xskq_create(entries, umem_queue);
509 	if (!q)
510 		return -ENOMEM;
511 
512 	/* Make sure queue is ready before it can be seen by others */
513 	smp_wmb();
514 	WRITE_ONCE(*queue, q);
515 	return 0;
516 }
517 
518 static void xsk_unbind_dev(struct xdp_sock *xs)
519 {
520 	struct net_device *dev = xs->dev;
521 
522 	if (xs->state != XSK_BOUND)
523 		return;
524 	WRITE_ONCE(xs->state, XSK_UNBOUND);
525 
526 	/* Wait for driver to stop using the xdp socket. */
527 	xp_del_xsk(xs->pool, xs);
528 	xs->dev = NULL;
529 	synchronize_net();
530 	dev_put(dev);
531 }
532 
533 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
534 					      struct xdp_sock ***map_entry)
535 {
536 	struct xsk_map *map = NULL;
537 	struct xsk_map_node *node;
538 
539 	*map_entry = NULL;
540 
541 	spin_lock_bh(&xs->map_list_lock);
542 	node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
543 					node);
544 	if (node) {
545 		WARN_ON(xsk_map_inc(node->map));
546 		map = node->map;
547 		*map_entry = node->map_entry;
548 	}
549 	spin_unlock_bh(&xs->map_list_lock);
550 	return map;
551 }
552 
553 static void xsk_delete_from_maps(struct xdp_sock *xs)
554 {
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().
569 	 */
570 	struct xdp_sock **map_entry = NULL;
571 	struct xsk_map *map;
572 
573 	while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
574 		xsk_map_try_sock_delete(map, xs, map_entry);
575 		xsk_map_put(map);
576 	}
577 }
578 
579 static int xsk_release(struct socket *sock)
580 {
581 	struct sock *sk = sock->sk;
582 	struct xdp_sock *xs = xdp_sk(sk);
583 	struct net *net;
584 
585 	if (!sk)
586 		return 0;
587 
588 	net = sock_net(sk);
589 
590 	mutex_lock(&net->xdp.lock);
591 	sk_del_node_init_rcu(sk);
592 	mutex_unlock(&net->xdp.lock);
593 
594 	local_bh_disable();
595 	sock_prot_inuse_add(net, sk->sk_prot, -1);
596 	local_bh_enable();
597 
598 	xsk_delete_from_maps(xs);
599 	mutex_lock(&xs->mutex);
600 	xsk_unbind_dev(xs);
601 	mutex_unlock(&xs->mutex);
602 
603 	xskq_destroy(xs->rx);
604 	xskq_destroy(xs->tx);
605 	xskq_destroy(xs->fq_tmp);
606 	xskq_destroy(xs->cq_tmp);
607 
608 	sock_orphan(sk);
609 	sock->sk = NULL;
610 
611 	sk_refcnt_debug_release(sk);
612 	sock_put(sk);
613 
614 	return 0;
615 }
616 
617 static struct socket *xsk_lookup_xsk_from_fd(int fd)
618 {
619 	struct socket *sock;
620 	int err;
621 
622 	sock = sockfd_lookup(fd, &err);
623 	if (!sock)
624 		return ERR_PTR(-ENOTSOCK);
625 
626 	if (sock->sk->sk_family != PF_XDP) {
627 		sockfd_put(sock);
628 		return ERR_PTR(-ENOPROTOOPT);
629 	}
630 
631 	return sock;
632 }
633 
634 static bool xsk_validate_queues(struct xdp_sock *xs)
635 {
636 	return xs->fq_tmp && xs->cq_tmp;
637 }
638 
639 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
640 {
641 	struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
642 	struct sock *sk = sock->sk;
643 	struct xdp_sock *xs = xdp_sk(sk);
644 	struct net_device *dev;
645 	u32 flags, qid;
646 	int err = 0;
647 
648 	if (addr_len < sizeof(struct sockaddr_xdp))
649 		return -EINVAL;
650 	if (sxdp->sxdp_family != AF_XDP)
651 		return -EINVAL;
652 
653 	flags = sxdp->sxdp_flags;
654 	if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
655 		      XDP_USE_NEED_WAKEUP))
656 		return -EINVAL;
657 
658 	rtnl_lock();
659 	mutex_lock(&xs->mutex);
660 	if (xs->state != XSK_READY) {
661 		err = -EBUSY;
662 		goto out_release;
663 	}
664 
665 	dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
666 	if (!dev) {
667 		err = -ENODEV;
668 		goto out_release;
669 	}
670 
671 	if (!xs->rx && !xs->tx) {
672 		err = -EINVAL;
673 		goto out_unlock;
674 	}
675 
676 	qid = sxdp->sxdp_queue_id;
677 
678 	if (flags & XDP_SHARED_UMEM) {
679 		struct xdp_sock *umem_xs;
680 		struct socket *sock;
681 
682 		if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
683 		    (flags & XDP_USE_NEED_WAKEUP)) {
684 			/* Cannot specify flags for shared sockets. */
685 			err = -EINVAL;
686 			goto out_unlock;
687 		}
688 
689 		if (xs->umem) {
690 			/* We have already our own. */
691 			err = -EINVAL;
692 			goto out_unlock;
693 		}
694 
695 		sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
696 		if (IS_ERR(sock)) {
697 			err = PTR_ERR(sock);
698 			goto out_unlock;
699 		}
700 
701 		umem_xs = xdp_sk(sock->sk);
702 		if (!xsk_is_bound(umem_xs)) {
703 			err = -EBADF;
704 			sockfd_put(sock);
705 			goto out_unlock;
706 		}
707 
708 		if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
709 			/* Share the umem with another socket on another qid
710 			 * and/or device.
711 			 */
712 			xs->pool = xp_create_and_assign_umem(xs,
713 							     umem_xs->umem);
714 			if (!xs->pool) {
715 				err = -ENOMEM;
716 				sockfd_put(sock);
717 				goto out_unlock;
718 			}
719 
720 			err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
721 						   dev, qid);
722 			if (err) {
723 				xp_destroy(xs->pool);
724 				xs->pool = NULL;
725 				sockfd_put(sock);
726 				goto out_unlock;
727 			}
728 		} else {
729 			/* Share the buffer pool with the other socket. */
730 			if (xs->fq_tmp || xs->cq_tmp) {
731 				/* Do not allow setting your own fq or cq. */
732 				err = -EINVAL;
733 				sockfd_put(sock);
734 				goto out_unlock;
735 			}
736 
737 			xp_get_pool(umem_xs->pool);
738 			xs->pool = umem_xs->pool;
739 		}
740 
741 		xdp_get_umem(umem_xs->umem);
742 		WRITE_ONCE(xs->umem, umem_xs->umem);
743 		sockfd_put(sock);
744 	} else if (!xs->umem || !xsk_validate_queues(xs)) {
745 		err = -EINVAL;
746 		goto out_unlock;
747 	} else {
748 		/* This xsk has its own umem. */
749 		xs->pool = xp_create_and_assign_umem(xs, xs->umem);
750 		if (!xs->pool) {
751 			err = -ENOMEM;
752 			goto out_unlock;
753 		}
754 
755 		err = xp_assign_dev(xs->pool, dev, qid, flags);
756 		if (err) {
757 			xp_destroy(xs->pool);
758 			xs->pool = NULL;
759 			goto out_unlock;
760 		}
761 	}
762 
763 	xs->dev = dev;
764 	xs->zc = xs->umem->zc;
765 	xs->queue_id = qid;
766 	xp_add_xsk(xs->pool, xs);
767 
768 out_unlock:
769 	if (err) {
770 		dev_put(dev);
771 	} else {
772 		/* Matches smp_rmb() in bind() for shared umem
773 		 * sockets, and xsk_is_bound().
774 		 */
775 		smp_wmb();
776 		WRITE_ONCE(xs->state, XSK_BOUND);
777 	}
778 out_release:
779 	mutex_unlock(&xs->mutex);
780 	rtnl_unlock();
781 	return err;
782 }
783 
784 struct xdp_umem_reg_v1 {
785 	__u64 addr; /* Start of packet data area */
786 	__u64 len; /* Length of packet data area */
787 	__u32 chunk_size;
788 	__u32 headroom;
789 };
790 
791 static int xsk_setsockopt(struct socket *sock, int level, int optname,
792 			  sockptr_t optval, unsigned int optlen)
793 {
794 	struct sock *sk = sock->sk;
795 	struct xdp_sock *xs = xdp_sk(sk);
796 	int err;
797 
798 	if (level != SOL_XDP)
799 		return -ENOPROTOOPT;
800 
801 	switch (optname) {
802 	case XDP_RX_RING:
803 	case XDP_TX_RING:
804 	{
805 		struct xsk_queue **q;
806 		int entries;
807 
808 		if (optlen < sizeof(entries))
809 			return -EINVAL;
810 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
811 			return -EFAULT;
812 
813 		mutex_lock(&xs->mutex);
814 		if (xs->state != XSK_READY) {
815 			mutex_unlock(&xs->mutex);
816 			return -EBUSY;
817 		}
818 		q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
819 		err = xsk_init_queue(entries, q, false);
820 		if (!err && optname == XDP_TX_RING)
821 			/* Tx needs to be explicitly woken up the first time */
822 			xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
823 		mutex_unlock(&xs->mutex);
824 		return err;
825 	}
826 	case XDP_UMEM_REG:
827 	{
828 		size_t mr_size = sizeof(struct xdp_umem_reg);
829 		struct xdp_umem_reg mr = {};
830 		struct xdp_umem *umem;
831 
832 		if (optlen < sizeof(struct xdp_umem_reg_v1))
833 			return -EINVAL;
834 		else if (optlen < sizeof(mr))
835 			mr_size = sizeof(struct xdp_umem_reg_v1);
836 
837 		if (copy_from_sockptr(&mr, optval, mr_size))
838 			return -EFAULT;
839 
840 		mutex_lock(&xs->mutex);
841 		if (xs->state != XSK_READY || xs->umem) {
842 			mutex_unlock(&xs->mutex);
843 			return -EBUSY;
844 		}
845 
846 		umem = xdp_umem_create(&mr);
847 		if (IS_ERR(umem)) {
848 			mutex_unlock(&xs->mutex);
849 			return PTR_ERR(umem);
850 		}
851 
852 		/* Make sure umem is ready before it can be seen by others */
853 		smp_wmb();
854 		WRITE_ONCE(xs->umem, umem);
855 		mutex_unlock(&xs->mutex);
856 		return 0;
857 	}
858 	case XDP_UMEM_FILL_RING:
859 	case XDP_UMEM_COMPLETION_RING:
860 	{
861 		struct xsk_queue **q;
862 		int entries;
863 
864 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
865 			return -EFAULT;
866 
867 		mutex_lock(&xs->mutex);
868 		if (xs->state != XSK_READY) {
869 			mutex_unlock(&xs->mutex);
870 			return -EBUSY;
871 		}
872 
873 		q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
874 			&xs->cq_tmp;
875 		err = xsk_init_queue(entries, q, true);
876 		mutex_unlock(&xs->mutex);
877 		return err;
878 	}
879 	default:
880 		break;
881 	}
882 
883 	return -ENOPROTOOPT;
884 }
885 
886 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
887 {
888 	ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
889 	ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
890 	ring->desc = offsetof(struct xdp_rxtx_ring, desc);
891 }
892 
893 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
894 {
895 	ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
896 	ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
897 	ring->desc = offsetof(struct xdp_umem_ring, desc);
898 }
899 
900 struct xdp_statistics_v1 {
901 	__u64 rx_dropped;
902 	__u64 rx_invalid_descs;
903 	__u64 tx_invalid_descs;
904 };
905 
906 static int xsk_getsockopt(struct socket *sock, int level, int optname,
907 			  char __user *optval, int __user *optlen)
908 {
909 	struct sock *sk = sock->sk;
910 	struct xdp_sock *xs = xdp_sk(sk);
911 	int len;
912 
913 	if (level != SOL_XDP)
914 		return -ENOPROTOOPT;
915 
916 	if (get_user(len, optlen))
917 		return -EFAULT;
918 	if (len < 0)
919 		return -EINVAL;
920 
921 	switch (optname) {
922 	case XDP_STATISTICS:
923 	{
924 		struct xdp_statistics stats = {};
925 		bool extra_stats = true;
926 		size_t stats_size;
927 
928 		if (len < sizeof(struct xdp_statistics_v1)) {
929 			return -EINVAL;
930 		} else if (len < sizeof(stats)) {
931 			extra_stats = false;
932 			stats_size = sizeof(struct xdp_statistics_v1);
933 		} else {
934 			stats_size = sizeof(stats);
935 		}
936 
937 		mutex_lock(&xs->mutex);
938 		stats.rx_dropped = xs->rx_dropped;
939 		if (extra_stats) {
940 			stats.rx_ring_full = xs->rx_queue_full;
941 			stats.rx_fill_ring_empty_descs =
942 				xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
943 			stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
944 		} else {
945 			stats.rx_dropped += xs->rx_queue_full;
946 		}
947 		stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
948 		stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
949 		mutex_unlock(&xs->mutex);
950 
951 		if (copy_to_user(optval, &stats, stats_size))
952 			return -EFAULT;
953 		if (put_user(stats_size, optlen))
954 			return -EFAULT;
955 
956 		return 0;
957 	}
958 	case XDP_MMAP_OFFSETS:
959 	{
960 		struct xdp_mmap_offsets off;
961 		struct xdp_mmap_offsets_v1 off_v1;
962 		bool flags_supported = true;
963 		void *to_copy;
964 
965 		if (len < sizeof(off_v1))
966 			return -EINVAL;
967 		else if (len < sizeof(off))
968 			flags_supported = false;
969 
970 		if (flags_supported) {
971 			/* xdp_ring_offset is identical to xdp_ring_offset_v1
972 			 * except for the flags field added to the end.
973 			 */
974 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
975 					       &off.rx);
976 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
977 					       &off.tx);
978 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
979 					       &off.fr);
980 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
981 					       &off.cr);
982 			off.rx.flags = offsetof(struct xdp_rxtx_ring,
983 						ptrs.flags);
984 			off.tx.flags = offsetof(struct xdp_rxtx_ring,
985 						ptrs.flags);
986 			off.fr.flags = offsetof(struct xdp_umem_ring,
987 						ptrs.flags);
988 			off.cr.flags = offsetof(struct xdp_umem_ring,
989 						ptrs.flags);
990 
991 			len = sizeof(off);
992 			to_copy = &off;
993 		} else {
994 			xsk_enter_rxtx_offsets(&off_v1.rx);
995 			xsk_enter_rxtx_offsets(&off_v1.tx);
996 			xsk_enter_umem_offsets(&off_v1.fr);
997 			xsk_enter_umem_offsets(&off_v1.cr);
998 
999 			len = sizeof(off_v1);
1000 			to_copy = &off_v1;
1001 		}
1002 
1003 		if (copy_to_user(optval, to_copy, len))
1004 			return -EFAULT;
1005 		if (put_user(len, optlen))
1006 			return -EFAULT;
1007 
1008 		return 0;
1009 	}
1010 	case XDP_OPTIONS:
1011 	{
1012 		struct xdp_options opts = {};
1013 
1014 		if (len < sizeof(opts))
1015 			return -EINVAL;
1016 
1017 		mutex_lock(&xs->mutex);
1018 		if (xs->zc)
1019 			opts.flags |= XDP_OPTIONS_ZEROCOPY;
1020 		mutex_unlock(&xs->mutex);
1021 
1022 		len = sizeof(opts);
1023 		if (copy_to_user(optval, &opts, len))
1024 			return -EFAULT;
1025 		if (put_user(len, optlen))
1026 			return -EFAULT;
1027 
1028 		return 0;
1029 	}
1030 	default:
1031 		break;
1032 	}
1033 
1034 	return -EOPNOTSUPP;
1035 }
1036 
1037 static int xsk_mmap(struct file *file, struct socket *sock,
1038 		    struct vm_area_struct *vma)
1039 {
1040 	loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1041 	unsigned long size = vma->vm_end - vma->vm_start;
1042 	struct xdp_sock *xs = xdp_sk(sock->sk);
1043 	struct xsk_queue *q = NULL;
1044 	unsigned long pfn;
1045 	struct page *qpg;
1046 
1047 	if (READ_ONCE(xs->state) != XSK_READY)
1048 		return -EBUSY;
1049 
1050 	if (offset == XDP_PGOFF_RX_RING) {
1051 		q = READ_ONCE(xs->rx);
1052 	} else if (offset == XDP_PGOFF_TX_RING) {
1053 		q = READ_ONCE(xs->tx);
1054 	} else {
1055 		/* Matches the smp_wmb() in XDP_UMEM_REG */
1056 		smp_rmb();
1057 		if (offset == XDP_UMEM_PGOFF_FILL_RING)
1058 			q = READ_ONCE(xs->fq_tmp);
1059 		else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1060 			q = READ_ONCE(xs->cq_tmp);
1061 	}
1062 
1063 	if (!q)
1064 		return -EINVAL;
1065 
1066 	/* Matches the smp_wmb() in xsk_init_queue */
1067 	smp_rmb();
1068 	qpg = virt_to_head_page(q->ring);
1069 	if (size > page_size(qpg))
1070 		return -EINVAL;
1071 
1072 	pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1073 	return remap_pfn_range(vma, vma->vm_start, pfn,
1074 			       size, vma->vm_page_prot);
1075 }
1076 
1077 static int xsk_notifier(struct notifier_block *this,
1078 			unsigned long msg, void *ptr)
1079 {
1080 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1081 	struct net *net = dev_net(dev);
1082 	struct sock *sk;
1083 
1084 	switch (msg) {
1085 	case NETDEV_UNREGISTER:
1086 		mutex_lock(&net->xdp.lock);
1087 		sk_for_each(sk, &net->xdp.list) {
1088 			struct xdp_sock *xs = xdp_sk(sk);
1089 
1090 			mutex_lock(&xs->mutex);
1091 			if (xs->dev == dev) {
1092 				sk->sk_err = ENETDOWN;
1093 				if (!sock_flag(sk, SOCK_DEAD))
1094 					sk->sk_error_report(sk);
1095 
1096 				xsk_unbind_dev(xs);
1097 
1098 				/* Clear device references. */
1099 				xp_clear_dev(xs->pool);
1100 			}
1101 			mutex_unlock(&xs->mutex);
1102 		}
1103 		mutex_unlock(&net->xdp.lock);
1104 		break;
1105 	}
1106 	return NOTIFY_DONE;
1107 }
1108 
1109 static struct proto xsk_proto = {
1110 	.name =		"XDP",
1111 	.owner =	THIS_MODULE,
1112 	.obj_size =	sizeof(struct xdp_sock),
1113 };
1114 
1115 static const struct proto_ops xsk_proto_ops = {
1116 	.family		= PF_XDP,
1117 	.owner		= THIS_MODULE,
1118 	.release	= xsk_release,
1119 	.bind		= xsk_bind,
1120 	.connect	= sock_no_connect,
1121 	.socketpair	= sock_no_socketpair,
1122 	.accept		= sock_no_accept,
1123 	.getname	= sock_no_getname,
1124 	.poll		= xsk_poll,
1125 	.ioctl		= sock_no_ioctl,
1126 	.listen		= sock_no_listen,
1127 	.shutdown	= sock_no_shutdown,
1128 	.setsockopt	= xsk_setsockopt,
1129 	.getsockopt	= xsk_getsockopt,
1130 	.sendmsg	= xsk_sendmsg,
1131 	.recvmsg	= sock_no_recvmsg,
1132 	.mmap		= xsk_mmap,
1133 	.sendpage	= sock_no_sendpage,
1134 };
1135 
1136 static void xsk_destruct(struct sock *sk)
1137 {
1138 	struct xdp_sock *xs = xdp_sk(sk);
1139 
1140 	if (!sock_flag(sk, SOCK_DEAD))
1141 		return;
1142 
1143 	if (!xp_put_pool(xs->pool))
1144 		xdp_put_umem(xs->umem, !xs->pool);
1145 
1146 	sk_refcnt_debug_dec(sk);
1147 }
1148 
1149 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1150 		      int kern)
1151 {
1152 	struct xdp_sock *xs;
1153 	struct sock *sk;
1154 
1155 	if (!ns_capable(net->user_ns, CAP_NET_RAW))
1156 		return -EPERM;
1157 	if (sock->type != SOCK_RAW)
1158 		return -ESOCKTNOSUPPORT;
1159 
1160 	if (protocol)
1161 		return -EPROTONOSUPPORT;
1162 
1163 	sock->state = SS_UNCONNECTED;
1164 
1165 	sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1166 	if (!sk)
1167 		return -ENOBUFS;
1168 
1169 	sock->ops = &xsk_proto_ops;
1170 
1171 	sock_init_data(sock, sk);
1172 
1173 	sk->sk_family = PF_XDP;
1174 
1175 	sk->sk_destruct = xsk_destruct;
1176 	sk_refcnt_debug_inc(sk);
1177 
1178 	sock_set_flag(sk, SOCK_RCU_FREE);
1179 
1180 	xs = xdp_sk(sk);
1181 	xs->state = XSK_READY;
1182 	mutex_init(&xs->mutex);
1183 	spin_lock_init(&xs->rx_lock);
1184 	spin_lock_init(&xs->tx_completion_lock);
1185 
1186 	INIT_LIST_HEAD(&xs->map_list);
1187 	spin_lock_init(&xs->map_list_lock);
1188 
1189 	mutex_lock(&net->xdp.lock);
1190 	sk_add_node_rcu(sk, &net->xdp.list);
1191 	mutex_unlock(&net->xdp.lock);
1192 
1193 	local_bh_disable();
1194 	sock_prot_inuse_add(net, &xsk_proto, 1);
1195 	local_bh_enable();
1196 
1197 	return 0;
1198 }
1199 
1200 static const struct net_proto_family xsk_family_ops = {
1201 	.family = PF_XDP,
1202 	.create = xsk_create,
1203 	.owner	= THIS_MODULE,
1204 };
1205 
1206 static struct notifier_block xsk_netdev_notifier = {
1207 	.notifier_call	= xsk_notifier,
1208 };
1209 
1210 static int __net_init xsk_net_init(struct net *net)
1211 {
1212 	mutex_init(&net->xdp.lock);
1213 	INIT_HLIST_HEAD(&net->xdp.list);
1214 	return 0;
1215 }
1216 
1217 static void __net_exit xsk_net_exit(struct net *net)
1218 {
1219 	WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1220 }
1221 
1222 static struct pernet_operations xsk_net_ops = {
1223 	.init = xsk_net_init,
1224 	.exit = xsk_net_exit,
1225 };
1226 
1227 static int __init xsk_init(void)
1228 {
1229 	int err, cpu;
1230 
1231 	err = proto_register(&xsk_proto, 0 /* no slab */);
1232 	if (err)
1233 		goto out;
1234 
1235 	err = sock_register(&xsk_family_ops);
1236 	if (err)
1237 		goto out_proto;
1238 
1239 	err = register_pernet_subsys(&xsk_net_ops);
1240 	if (err)
1241 		goto out_sk;
1242 
1243 	err = register_netdevice_notifier(&xsk_netdev_notifier);
1244 	if (err)
1245 		goto out_pernet;
1246 
1247 	for_each_possible_cpu(cpu)
1248 		INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1249 	return 0;
1250 
1251 out_pernet:
1252 	unregister_pernet_subsys(&xsk_net_ops);
1253 out_sk:
1254 	sock_unregister(PF_XDP);
1255 out_proto:
1256 	proto_unregister(&xsk_proto);
1257 out:
1258 	return err;
1259 }
1260 
1261 fs_initcall(xsk_init);
1262