xref: /openbmc/linux/net/xdp/xsk.c (revision 61bf3293)
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_dropped++;
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 			continue;
279 
280 		/* This is the backpressure mechanism for the Tx path.
281 		 * Reserve space in the completion queue and only proceed
282 		 * if there is space in it. This avoids having to implement
283 		 * any buffering in the Tx path.
284 		 */
285 		if (xskq_prod_reserve_addr(umem->cq, desc->addr))
286 			goto out;
287 
288 		xskq_cons_release(xs->tx);
289 		rcu_read_unlock();
290 		return true;
291 	}
292 
293 out:
294 	rcu_read_unlock();
295 	return false;
296 }
297 EXPORT_SYMBOL(xsk_umem_consume_tx);
298 
299 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
300 {
301 	struct net_device *dev = xs->dev;
302 	int err;
303 
304 	rcu_read_lock();
305 	err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
306 	rcu_read_unlock();
307 
308 	return err;
309 }
310 
311 static int xsk_zc_xmit(struct xdp_sock *xs)
312 {
313 	return xsk_wakeup(xs, XDP_WAKEUP_TX);
314 }
315 
316 static void xsk_destruct_skb(struct sk_buff *skb)
317 {
318 	u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
319 	struct xdp_sock *xs = xdp_sk(skb->sk);
320 	unsigned long flags;
321 
322 	spin_lock_irqsave(&xs->tx_completion_lock, flags);
323 	xskq_prod_submit_addr(xs->umem->cq, addr);
324 	spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
325 
326 	sock_wfree(skb);
327 }
328 
329 static int xsk_generic_xmit(struct sock *sk)
330 {
331 	struct xdp_sock *xs = xdp_sk(sk);
332 	u32 max_batch = TX_BATCH_SIZE;
333 	bool sent_frame = false;
334 	struct xdp_desc desc;
335 	struct sk_buff *skb;
336 	int err = 0;
337 
338 	mutex_lock(&xs->mutex);
339 
340 	if (xs->queue_id >= xs->dev->real_num_tx_queues)
341 		goto out;
342 
343 	while (xskq_cons_peek_desc(xs->tx, &desc, xs->umem)) {
344 		char *buffer;
345 		u64 addr;
346 		u32 len;
347 
348 		if (max_batch-- == 0) {
349 			err = -EAGAIN;
350 			goto out;
351 		}
352 
353 		len = desc.len;
354 		skb = sock_alloc_send_skb(sk, len, 1, &err);
355 		if (unlikely(!skb))
356 			goto out;
357 
358 		skb_put(skb, len);
359 		addr = desc.addr;
360 		buffer = xsk_buff_raw_get_data(xs->umem, addr);
361 		err = skb_store_bits(skb, 0, buffer, len);
362 		/* This is the backpressure mechanism for the Tx path.
363 		 * Reserve space in the completion queue and only proceed
364 		 * if there is space in it. This avoids having to implement
365 		 * any buffering in the Tx path.
366 		 */
367 		if (unlikely(err) || xskq_prod_reserve(xs->umem->cq)) {
368 			kfree_skb(skb);
369 			goto out;
370 		}
371 
372 		skb->dev = xs->dev;
373 		skb->priority = sk->sk_priority;
374 		skb->mark = sk->sk_mark;
375 		skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
376 		skb->destructor = xsk_destruct_skb;
377 
378 		err = dev_direct_xmit(skb, xs->queue_id);
379 		xskq_cons_release(xs->tx);
380 		/* Ignore NET_XMIT_CN as packet might have been sent */
381 		if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
382 			/* SKB completed but not sent */
383 			err = -EBUSY;
384 			goto out;
385 		}
386 
387 		sent_frame = true;
388 	}
389 
390 out:
391 	if (sent_frame)
392 		sk->sk_write_space(sk);
393 
394 	mutex_unlock(&xs->mutex);
395 	return err;
396 }
397 
398 static int __xsk_sendmsg(struct sock *sk)
399 {
400 	struct xdp_sock *xs = xdp_sk(sk);
401 
402 	if (unlikely(!(xs->dev->flags & IFF_UP)))
403 		return -ENETDOWN;
404 	if (unlikely(!xs->tx))
405 		return -ENOBUFS;
406 
407 	return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
408 }
409 
410 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
411 {
412 	bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
413 	struct sock *sk = sock->sk;
414 	struct xdp_sock *xs = xdp_sk(sk);
415 
416 	if (unlikely(!xsk_is_bound(xs)))
417 		return -ENXIO;
418 	if (unlikely(need_wait))
419 		return -EOPNOTSUPP;
420 
421 	return __xsk_sendmsg(sk);
422 }
423 
424 static __poll_t xsk_poll(struct file *file, struct socket *sock,
425 			     struct poll_table_struct *wait)
426 {
427 	__poll_t mask = datagram_poll(file, sock, wait);
428 	struct sock *sk = sock->sk;
429 	struct xdp_sock *xs = xdp_sk(sk);
430 	struct xdp_umem *umem;
431 
432 	if (unlikely(!xsk_is_bound(xs)))
433 		return mask;
434 
435 	umem = xs->umem;
436 
437 	if (umem->need_wakeup) {
438 		if (xs->zc)
439 			xsk_wakeup(xs, umem->need_wakeup);
440 		else
441 			/* Poll needs to drive Tx also in copy mode */
442 			__xsk_sendmsg(sk);
443 	}
444 
445 	if (xs->rx && !xskq_prod_is_empty(xs->rx))
446 		mask |= EPOLLIN | EPOLLRDNORM;
447 	if (xs->tx && !xskq_cons_is_full(xs->tx))
448 		mask |= EPOLLOUT | EPOLLWRNORM;
449 
450 	return mask;
451 }
452 
453 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
454 			  bool umem_queue)
455 {
456 	struct xsk_queue *q;
457 
458 	if (entries == 0 || *queue || !is_power_of_2(entries))
459 		return -EINVAL;
460 
461 	q = xskq_create(entries, umem_queue);
462 	if (!q)
463 		return -ENOMEM;
464 
465 	/* Make sure queue is ready before it can be seen by others */
466 	smp_wmb();
467 	WRITE_ONCE(*queue, q);
468 	return 0;
469 }
470 
471 static void xsk_unbind_dev(struct xdp_sock *xs)
472 {
473 	struct net_device *dev = xs->dev;
474 
475 	if (xs->state != XSK_BOUND)
476 		return;
477 	WRITE_ONCE(xs->state, XSK_UNBOUND);
478 
479 	/* Wait for driver to stop using the xdp socket. */
480 	xdp_del_sk_umem(xs->umem, xs);
481 	xs->dev = NULL;
482 	synchronize_net();
483 	dev_put(dev);
484 }
485 
486 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
487 					      struct xdp_sock ***map_entry)
488 {
489 	struct xsk_map *map = NULL;
490 	struct xsk_map_node *node;
491 
492 	*map_entry = NULL;
493 
494 	spin_lock_bh(&xs->map_list_lock);
495 	node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
496 					node);
497 	if (node) {
498 		WARN_ON(xsk_map_inc(node->map));
499 		map = node->map;
500 		*map_entry = node->map_entry;
501 	}
502 	spin_unlock_bh(&xs->map_list_lock);
503 	return map;
504 }
505 
506 static void xsk_delete_from_maps(struct xdp_sock *xs)
507 {
508 	/* This function removes the current XDP socket from all the
509 	 * maps it resides in. We need to take extra care here, due to
510 	 * the two locks involved. Each map has a lock synchronizing
511 	 * updates to the entries, and each socket has a lock that
512 	 * synchronizes access to the list of maps (map_list). For
513 	 * deadlock avoidance the locks need to be taken in the order
514 	 * "map lock"->"socket map list lock". We start off by
515 	 * accessing the socket map list, and take a reference to the
516 	 * map to guarantee existence between the
517 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
518 	 * calls. Then we ask the map to remove the socket, which
519 	 * tries to remove the socket from the map. Note that there
520 	 * might be updates to the map between
521 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
522 	 */
523 	struct xdp_sock **map_entry = NULL;
524 	struct xsk_map *map;
525 
526 	while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
527 		xsk_map_try_sock_delete(map, xs, map_entry);
528 		xsk_map_put(map);
529 	}
530 }
531 
532 static int xsk_release(struct socket *sock)
533 {
534 	struct sock *sk = sock->sk;
535 	struct xdp_sock *xs = xdp_sk(sk);
536 	struct net *net;
537 
538 	if (!sk)
539 		return 0;
540 
541 	net = sock_net(sk);
542 
543 	mutex_lock(&net->xdp.lock);
544 	sk_del_node_init_rcu(sk);
545 	mutex_unlock(&net->xdp.lock);
546 
547 	local_bh_disable();
548 	sock_prot_inuse_add(net, sk->sk_prot, -1);
549 	local_bh_enable();
550 
551 	xsk_delete_from_maps(xs);
552 	mutex_lock(&xs->mutex);
553 	xsk_unbind_dev(xs);
554 	mutex_unlock(&xs->mutex);
555 
556 	xskq_destroy(xs->rx);
557 	xskq_destroy(xs->tx);
558 
559 	sock_orphan(sk);
560 	sock->sk = NULL;
561 
562 	sk_refcnt_debug_release(sk);
563 	sock_put(sk);
564 
565 	return 0;
566 }
567 
568 static struct socket *xsk_lookup_xsk_from_fd(int fd)
569 {
570 	struct socket *sock;
571 	int err;
572 
573 	sock = sockfd_lookup(fd, &err);
574 	if (!sock)
575 		return ERR_PTR(-ENOTSOCK);
576 
577 	if (sock->sk->sk_family != PF_XDP) {
578 		sockfd_put(sock);
579 		return ERR_PTR(-ENOPROTOOPT);
580 	}
581 
582 	return sock;
583 }
584 
585 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
586 {
587 	struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
588 	struct sock *sk = sock->sk;
589 	struct xdp_sock *xs = xdp_sk(sk);
590 	struct net_device *dev;
591 	u32 flags, qid;
592 	int err = 0;
593 
594 	if (addr_len < sizeof(struct sockaddr_xdp))
595 		return -EINVAL;
596 	if (sxdp->sxdp_family != AF_XDP)
597 		return -EINVAL;
598 
599 	flags = sxdp->sxdp_flags;
600 	if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
601 		      XDP_USE_NEED_WAKEUP))
602 		return -EINVAL;
603 
604 	rtnl_lock();
605 	mutex_lock(&xs->mutex);
606 	if (xs->state != XSK_READY) {
607 		err = -EBUSY;
608 		goto out_release;
609 	}
610 
611 	dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
612 	if (!dev) {
613 		err = -ENODEV;
614 		goto out_release;
615 	}
616 
617 	if (!xs->rx && !xs->tx) {
618 		err = -EINVAL;
619 		goto out_unlock;
620 	}
621 
622 	qid = sxdp->sxdp_queue_id;
623 
624 	if (flags & XDP_SHARED_UMEM) {
625 		struct xdp_sock *umem_xs;
626 		struct socket *sock;
627 
628 		if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
629 		    (flags & XDP_USE_NEED_WAKEUP)) {
630 			/* Cannot specify flags for shared sockets. */
631 			err = -EINVAL;
632 			goto out_unlock;
633 		}
634 
635 		if (xs->umem) {
636 			/* We have already our own. */
637 			err = -EINVAL;
638 			goto out_unlock;
639 		}
640 
641 		sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
642 		if (IS_ERR(sock)) {
643 			err = PTR_ERR(sock);
644 			goto out_unlock;
645 		}
646 
647 		umem_xs = xdp_sk(sock->sk);
648 		if (!xsk_is_bound(umem_xs)) {
649 			err = -EBADF;
650 			sockfd_put(sock);
651 			goto out_unlock;
652 		}
653 		if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
654 			err = -EINVAL;
655 			sockfd_put(sock);
656 			goto out_unlock;
657 		}
658 
659 		xdp_get_umem(umem_xs->umem);
660 		WRITE_ONCE(xs->umem, umem_xs->umem);
661 		sockfd_put(sock);
662 	} else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
663 		err = -EINVAL;
664 		goto out_unlock;
665 	} else {
666 		/* This xsk has its own umem. */
667 		err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
668 		if (err)
669 			goto out_unlock;
670 	}
671 
672 	xs->dev = dev;
673 	xs->zc = xs->umem->zc;
674 	xs->queue_id = qid;
675 	xdp_add_sk_umem(xs->umem, xs);
676 
677 out_unlock:
678 	if (err) {
679 		dev_put(dev);
680 	} else {
681 		/* Matches smp_rmb() in bind() for shared umem
682 		 * sockets, and xsk_is_bound().
683 		 */
684 		smp_wmb();
685 		WRITE_ONCE(xs->state, XSK_BOUND);
686 	}
687 out_release:
688 	mutex_unlock(&xs->mutex);
689 	rtnl_unlock();
690 	return err;
691 }
692 
693 struct xdp_umem_reg_v1 {
694 	__u64 addr; /* Start of packet data area */
695 	__u64 len; /* Length of packet data area */
696 	__u32 chunk_size;
697 	__u32 headroom;
698 };
699 
700 static int xsk_setsockopt(struct socket *sock, int level, int optname,
701 			  char __user *optval, unsigned int optlen)
702 {
703 	struct sock *sk = sock->sk;
704 	struct xdp_sock *xs = xdp_sk(sk);
705 	int err;
706 
707 	if (level != SOL_XDP)
708 		return -ENOPROTOOPT;
709 
710 	switch (optname) {
711 	case XDP_RX_RING:
712 	case XDP_TX_RING:
713 	{
714 		struct xsk_queue **q;
715 		int entries;
716 
717 		if (optlen < sizeof(entries))
718 			return -EINVAL;
719 		if (copy_from_user(&entries, optval, sizeof(entries)))
720 			return -EFAULT;
721 
722 		mutex_lock(&xs->mutex);
723 		if (xs->state != XSK_READY) {
724 			mutex_unlock(&xs->mutex);
725 			return -EBUSY;
726 		}
727 		q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
728 		err = xsk_init_queue(entries, q, false);
729 		if (!err && optname == XDP_TX_RING)
730 			/* Tx needs to be explicitly woken up the first time */
731 			xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
732 		mutex_unlock(&xs->mutex);
733 		return err;
734 	}
735 	case XDP_UMEM_REG:
736 	{
737 		size_t mr_size = sizeof(struct xdp_umem_reg);
738 		struct xdp_umem_reg mr = {};
739 		struct xdp_umem *umem;
740 
741 		if (optlen < sizeof(struct xdp_umem_reg_v1))
742 			return -EINVAL;
743 		else if (optlen < sizeof(mr))
744 			mr_size = sizeof(struct xdp_umem_reg_v1);
745 
746 		if (copy_from_user(&mr, optval, mr_size))
747 			return -EFAULT;
748 
749 		mutex_lock(&xs->mutex);
750 		if (xs->state != XSK_READY || xs->umem) {
751 			mutex_unlock(&xs->mutex);
752 			return -EBUSY;
753 		}
754 
755 		umem = xdp_umem_create(&mr);
756 		if (IS_ERR(umem)) {
757 			mutex_unlock(&xs->mutex);
758 			return PTR_ERR(umem);
759 		}
760 
761 		/* Make sure umem is ready before it can be seen by others */
762 		smp_wmb();
763 		WRITE_ONCE(xs->umem, umem);
764 		mutex_unlock(&xs->mutex);
765 		return 0;
766 	}
767 	case XDP_UMEM_FILL_RING:
768 	case XDP_UMEM_COMPLETION_RING:
769 	{
770 		struct xsk_queue **q;
771 		int entries;
772 
773 		if (copy_from_user(&entries, optval, sizeof(entries)))
774 			return -EFAULT;
775 
776 		mutex_lock(&xs->mutex);
777 		if (xs->state != XSK_READY) {
778 			mutex_unlock(&xs->mutex);
779 			return -EBUSY;
780 		}
781 		if (!xs->umem) {
782 			mutex_unlock(&xs->mutex);
783 			return -EINVAL;
784 		}
785 
786 		q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
787 			&xs->umem->cq;
788 		err = xsk_init_queue(entries, q, true);
789 		if (optname == XDP_UMEM_FILL_RING)
790 			xp_set_fq(xs->umem->pool, *q);
791 		mutex_unlock(&xs->mutex);
792 		return err;
793 	}
794 	default:
795 		break;
796 	}
797 
798 	return -ENOPROTOOPT;
799 }
800 
801 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
802 {
803 	ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
804 	ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
805 	ring->desc = offsetof(struct xdp_rxtx_ring, desc);
806 }
807 
808 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
809 {
810 	ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
811 	ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
812 	ring->desc = offsetof(struct xdp_umem_ring, desc);
813 }
814 
815 static int xsk_getsockopt(struct socket *sock, int level, int optname,
816 			  char __user *optval, int __user *optlen)
817 {
818 	struct sock *sk = sock->sk;
819 	struct xdp_sock *xs = xdp_sk(sk);
820 	int len;
821 
822 	if (level != SOL_XDP)
823 		return -ENOPROTOOPT;
824 
825 	if (get_user(len, optlen))
826 		return -EFAULT;
827 	if (len < 0)
828 		return -EINVAL;
829 
830 	switch (optname) {
831 	case XDP_STATISTICS:
832 	{
833 		struct xdp_statistics stats;
834 
835 		if (len < sizeof(stats))
836 			return -EINVAL;
837 
838 		mutex_lock(&xs->mutex);
839 		stats.rx_dropped = xs->rx_dropped;
840 		stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
841 		stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
842 		mutex_unlock(&xs->mutex);
843 
844 		if (copy_to_user(optval, &stats, sizeof(stats)))
845 			return -EFAULT;
846 		if (put_user(sizeof(stats), optlen))
847 			return -EFAULT;
848 
849 		return 0;
850 	}
851 	case XDP_MMAP_OFFSETS:
852 	{
853 		struct xdp_mmap_offsets off;
854 		struct xdp_mmap_offsets_v1 off_v1;
855 		bool flags_supported = true;
856 		void *to_copy;
857 
858 		if (len < sizeof(off_v1))
859 			return -EINVAL;
860 		else if (len < sizeof(off))
861 			flags_supported = false;
862 
863 		if (flags_supported) {
864 			/* xdp_ring_offset is identical to xdp_ring_offset_v1
865 			 * except for the flags field added to the end.
866 			 */
867 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
868 					       &off.rx);
869 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
870 					       &off.tx);
871 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
872 					       &off.fr);
873 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
874 					       &off.cr);
875 			off.rx.flags = offsetof(struct xdp_rxtx_ring,
876 						ptrs.flags);
877 			off.tx.flags = offsetof(struct xdp_rxtx_ring,
878 						ptrs.flags);
879 			off.fr.flags = offsetof(struct xdp_umem_ring,
880 						ptrs.flags);
881 			off.cr.flags = offsetof(struct xdp_umem_ring,
882 						ptrs.flags);
883 
884 			len = sizeof(off);
885 			to_copy = &off;
886 		} else {
887 			xsk_enter_rxtx_offsets(&off_v1.rx);
888 			xsk_enter_rxtx_offsets(&off_v1.tx);
889 			xsk_enter_umem_offsets(&off_v1.fr);
890 			xsk_enter_umem_offsets(&off_v1.cr);
891 
892 			len = sizeof(off_v1);
893 			to_copy = &off_v1;
894 		}
895 
896 		if (copy_to_user(optval, to_copy, len))
897 			return -EFAULT;
898 		if (put_user(len, optlen))
899 			return -EFAULT;
900 
901 		return 0;
902 	}
903 	case XDP_OPTIONS:
904 	{
905 		struct xdp_options opts = {};
906 
907 		if (len < sizeof(opts))
908 			return -EINVAL;
909 
910 		mutex_lock(&xs->mutex);
911 		if (xs->zc)
912 			opts.flags |= XDP_OPTIONS_ZEROCOPY;
913 		mutex_unlock(&xs->mutex);
914 
915 		len = sizeof(opts);
916 		if (copy_to_user(optval, &opts, len))
917 			return -EFAULT;
918 		if (put_user(len, optlen))
919 			return -EFAULT;
920 
921 		return 0;
922 	}
923 	default:
924 		break;
925 	}
926 
927 	return -EOPNOTSUPP;
928 }
929 
930 static int xsk_mmap(struct file *file, struct socket *sock,
931 		    struct vm_area_struct *vma)
932 {
933 	loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
934 	unsigned long size = vma->vm_end - vma->vm_start;
935 	struct xdp_sock *xs = xdp_sk(sock->sk);
936 	struct xsk_queue *q = NULL;
937 	struct xdp_umem *umem;
938 	unsigned long pfn;
939 	struct page *qpg;
940 
941 	if (READ_ONCE(xs->state) != XSK_READY)
942 		return -EBUSY;
943 
944 	if (offset == XDP_PGOFF_RX_RING) {
945 		q = READ_ONCE(xs->rx);
946 	} else if (offset == XDP_PGOFF_TX_RING) {
947 		q = READ_ONCE(xs->tx);
948 	} else {
949 		umem = READ_ONCE(xs->umem);
950 		if (!umem)
951 			return -EINVAL;
952 
953 		/* Matches the smp_wmb() in XDP_UMEM_REG */
954 		smp_rmb();
955 		if (offset == XDP_UMEM_PGOFF_FILL_RING)
956 			q = READ_ONCE(umem->fq);
957 		else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
958 			q = READ_ONCE(umem->cq);
959 	}
960 
961 	if (!q)
962 		return -EINVAL;
963 
964 	/* Matches the smp_wmb() in xsk_init_queue */
965 	smp_rmb();
966 	qpg = virt_to_head_page(q->ring);
967 	if (size > page_size(qpg))
968 		return -EINVAL;
969 
970 	pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
971 	return remap_pfn_range(vma, vma->vm_start, pfn,
972 			       size, vma->vm_page_prot);
973 }
974 
975 static int xsk_notifier(struct notifier_block *this,
976 			unsigned long msg, void *ptr)
977 {
978 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
979 	struct net *net = dev_net(dev);
980 	struct sock *sk;
981 
982 	switch (msg) {
983 	case NETDEV_UNREGISTER:
984 		mutex_lock(&net->xdp.lock);
985 		sk_for_each(sk, &net->xdp.list) {
986 			struct xdp_sock *xs = xdp_sk(sk);
987 
988 			mutex_lock(&xs->mutex);
989 			if (xs->dev == dev) {
990 				sk->sk_err = ENETDOWN;
991 				if (!sock_flag(sk, SOCK_DEAD))
992 					sk->sk_error_report(sk);
993 
994 				xsk_unbind_dev(xs);
995 
996 				/* Clear device references in umem. */
997 				xdp_umem_clear_dev(xs->umem);
998 			}
999 			mutex_unlock(&xs->mutex);
1000 		}
1001 		mutex_unlock(&net->xdp.lock);
1002 		break;
1003 	}
1004 	return NOTIFY_DONE;
1005 }
1006 
1007 static struct proto xsk_proto = {
1008 	.name =		"XDP",
1009 	.owner =	THIS_MODULE,
1010 	.obj_size =	sizeof(struct xdp_sock),
1011 };
1012 
1013 static const struct proto_ops xsk_proto_ops = {
1014 	.family		= PF_XDP,
1015 	.owner		= THIS_MODULE,
1016 	.release	= xsk_release,
1017 	.bind		= xsk_bind,
1018 	.connect	= sock_no_connect,
1019 	.socketpair	= sock_no_socketpair,
1020 	.accept		= sock_no_accept,
1021 	.getname	= sock_no_getname,
1022 	.poll		= xsk_poll,
1023 	.ioctl		= sock_no_ioctl,
1024 	.listen		= sock_no_listen,
1025 	.shutdown	= sock_no_shutdown,
1026 	.setsockopt	= xsk_setsockopt,
1027 	.getsockopt	= xsk_getsockopt,
1028 	.sendmsg	= xsk_sendmsg,
1029 	.recvmsg	= sock_no_recvmsg,
1030 	.mmap		= xsk_mmap,
1031 	.sendpage	= sock_no_sendpage,
1032 };
1033 
1034 static void xsk_destruct(struct sock *sk)
1035 {
1036 	struct xdp_sock *xs = xdp_sk(sk);
1037 
1038 	if (!sock_flag(sk, SOCK_DEAD))
1039 		return;
1040 
1041 	xdp_put_umem(xs->umem);
1042 
1043 	sk_refcnt_debug_dec(sk);
1044 }
1045 
1046 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1047 		      int kern)
1048 {
1049 	struct sock *sk;
1050 	struct xdp_sock *xs;
1051 
1052 	if (!ns_capable(net->user_ns, CAP_NET_RAW))
1053 		return -EPERM;
1054 	if (sock->type != SOCK_RAW)
1055 		return -ESOCKTNOSUPPORT;
1056 
1057 	if (protocol)
1058 		return -EPROTONOSUPPORT;
1059 
1060 	sock->state = SS_UNCONNECTED;
1061 
1062 	sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1063 	if (!sk)
1064 		return -ENOBUFS;
1065 
1066 	sock->ops = &xsk_proto_ops;
1067 
1068 	sock_init_data(sock, sk);
1069 
1070 	sk->sk_family = PF_XDP;
1071 
1072 	sk->sk_destruct = xsk_destruct;
1073 	sk_refcnt_debug_inc(sk);
1074 
1075 	sock_set_flag(sk, SOCK_RCU_FREE);
1076 
1077 	xs = xdp_sk(sk);
1078 	xs->state = XSK_READY;
1079 	mutex_init(&xs->mutex);
1080 	spin_lock_init(&xs->rx_lock);
1081 	spin_lock_init(&xs->tx_completion_lock);
1082 
1083 	INIT_LIST_HEAD(&xs->map_list);
1084 	spin_lock_init(&xs->map_list_lock);
1085 
1086 	mutex_lock(&net->xdp.lock);
1087 	sk_add_node_rcu(sk, &net->xdp.list);
1088 	mutex_unlock(&net->xdp.lock);
1089 
1090 	local_bh_disable();
1091 	sock_prot_inuse_add(net, &xsk_proto, 1);
1092 	local_bh_enable();
1093 
1094 	return 0;
1095 }
1096 
1097 static const struct net_proto_family xsk_family_ops = {
1098 	.family = PF_XDP,
1099 	.create = xsk_create,
1100 	.owner	= THIS_MODULE,
1101 };
1102 
1103 static struct notifier_block xsk_netdev_notifier = {
1104 	.notifier_call	= xsk_notifier,
1105 };
1106 
1107 static int __net_init xsk_net_init(struct net *net)
1108 {
1109 	mutex_init(&net->xdp.lock);
1110 	INIT_HLIST_HEAD(&net->xdp.list);
1111 	return 0;
1112 }
1113 
1114 static void __net_exit xsk_net_exit(struct net *net)
1115 {
1116 	WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1117 }
1118 
1119 static struct pernet_operations xsk_net_ops = {
1120 	.init = xsk_net_init,
1121 	.exit = xsk_net_exit,
1122 };
1123 
1124 static int __init xsk_init(void)
1125 {
1126 	int err, cpu;
1127 
1128 	err = proto_register(&xsk_proto, 0 /* no slab */);
1129 	if (err)
1130 		goto out;
1131 
1132 	err = sock_register(&xsk_family_ops);
1133 	if (err)
1134 		goto out_proto;
1135 
1136 	err = register_pernet_subsys(&xsk_net_ops);
1137 	if (err)
1138 		goto out_sk;
1139 
1140 	err = register_netdevice_notifier(&xsk_netdev_notifier);
1141 	if (err)
1142 		goto out_pernet;
1143 
1144 	for_each_possible_cpu(cpu)
1145 		INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1146 	return 0;
1147 
1148 out_pernet:
1149 	unregister_pernet_subsys(&xsk_net_ops);
1150 out_sk:
1151 	sock_unregister(PF_XDP);
1152 out_proto:
1153 	proto_unregister(&xsk_proto);
1154 out:
1155 	return err;
1156 }
1157 
1158 fs_initcall(xsk_init);
1159