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