xref: /linux/drivers/net/veth.c (revision b2f4c44d7d83770ee1f235a732d8e1fd831c4926)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  drivers/net/veth.c
4  *
5  *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6  *
7  * Author: Pavel Emelianov <xemul@openvz.org>
8  * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
9  *
10  */
11 
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ethtool.h>
15 #include <linux/etherdevice.h>
16 #include <linux/u64_stats_sync.h>
17 
18 #include <net/rtnetlink.h>
19 #include <net/dst.h>
20 #include <net/netdev_lock.h>
21 #include <net/xfrm.h>
22 #include <net/xdp.h>
23 #include <linux/veth.h>
24 #include <linux/module.h>
25 #include <linux/bpf.h>
26 #include <linux/filter.h>
27 #include <linux/ptr_ring.h>
28 #include <linux/bpf_trace.h>
29 #include <linux/net_tstamp.h>
30 #include <linux/skbuff_ref.h>
31 #include <net/page_pool/helpers.h>
32 
33 #define DRV_NAME	"veth"
34 #define DRV_VERSION	"1.0"
35 
36 #define VETH_XDP_FLAG		BIT(0)
37 #define VETH_RING_SIZE		256
38 #define VETH_XDP_HEADROOM	(XDP_PACKET_HEADROOM + NET_IP_ALIGN)
39 
40 #define VETH_XDP_TX_BULK_SIZE	16
41 #define VETH_XDP_BATCH		16
42 
43 struct veth_stats {
44 	u64	rx_drops;
45 	/* xdp */
46 	u64	xdp_packets;
47 	u64	xdp_bytes;
48 	u64	xdp_redirect;
49 	u64	xdp_drops;
50 	u64	xdp_tx;
51 	u64	xdp_tx_err;
52 	u64	peer_tq_xdp_xmit;
53 	u64	peer_tq_xdp_xmit_err;
54 };
55 
56 struct veth_rq_stats {
57 	struct veth_stats	vs;
58 	struct u64_stats_sync	syncp;
59 };
60 
61 struct veth_rq {
62 	struct napi_struct	xdp_napi;
63 	struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
64 	struct net_device	*dev;
65 	struct bpf_prog __rcu	*xdp_prog;
66 	struct xdp_mem_info	xdp_mem;
67 	struct veth_rq_stats	stats;
68 	bool			rx_notify_masked;
69 	struct ptr_ring		xdp_ring;
70 	struct xdp_rxq_info	xdp_rxq;
71 	struct page_pool	*page_pool;
72 };
73 
74 struct veth_priv {
75 	struct net_device __rcu	*peer;
76 	atomic64_t		dropped;
77 	struct bpf_prog		*_xdp_prog;
78 	struct veth_rq		*rq;
79 	unsigned int		requested_headroom;
80 	netdevice_tracker	peer_tracker;
81 };
82 
83 struct veth_xdp_tx_bq {
84 	struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
85 	unsigned int count;
86 };
87 
88 /*
89  * ethtool interface
90  */
91 
92 struct veth_q_stat_desc {
93 	char	desc[ETH_GSTRING_LEN];
94 	size_t	offset;
95 };
96 
97 #define VETH_RQ_STAT(m)	offsetof(struct veth_stats, m)
98 
99 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
100 	{ "xdp_packets",	VETH_RQ_STAT(xdp_packets) },
101 	{ "xdp_bytes",		VETH_RQ_STAT(xdp_bytes) },
102 	{ "drops",		VETH_RQ_STAT(rx_drops) },
103 	{ "xdp_redirect",	VETH_RQ_STAT(xdp_redirect) },
104 	{ "xdp_drops",		VETH_RQ_STAT(xdp_drops) },
105 	{ "xdp_tx",		VETH_RQ_STAT(xdp_tx) },
106 	{ "xdp_tx_errors",	VETH_RQ_STAT(xdp_tx_err) },
107 };
108 
109 #define VETH_RQ_STATS_LEN	ARRAY_SIZE(veth_rq_stats_desc)
110 
111 static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
112 	{ "xdp_xmit",		VETH_RQ_STAT(peer_tq_xdp_xmit) },
113 	{ "xdp_xmit_errors",	VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
114 };
115 
116 #define VETH_TQ_STATS_LEN	ARRAY_SIZE(veth_tq_stats_desc)
117 
118 static struct {
119 	const char string[ETH_GSTRING_LEN];
120 } ethtool_stats_keys[] = {
121 	{ "peer_ifindex" },
122 };
123 
124 struct veth_xdp_buff {
125 	struct xdp_buff xdp;
126 	struct sk_buff *skb;
127 };
128 
129 static int veth_get_link_ksettings(struct net_device *dev,
130 				   struct ethtool_link_ksettings *cmd)
131 {
132 	cmd->base.speed		= SPEED_10000;
133 	cmd->base.duplex	= DUPLEX_FULL;
134 	cmd->base.port		= PORT_TP;
135 	cmd->base.autoneg	= AUTONEG_DISABLE;
136 	return 0;
137 }
138 
139 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
140 {
141 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
142 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
143 }
144 
145 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
146 {
147 	u8 *p = buf;
148 	int i, j;
149 
150 	switch(stringset) {
151 	case ETH_SS_STATS:
152 		memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
153 		p += sizeof(ethtool_stats_keys);
154 		for (i = 0; i < dev->real_num_rx_queues; i++)
155 			for (j = 0; j < VETH_RQ_STATS_LEN; j++)
156 				ethtool_sprintf(&p, "rx_queue_%u_%.18s",
157 						i, veth_rq_stats_desc[j].desc);
158 
159 		for (i = 0; i < dev->real_num_tx_queues; i++)
160 			for (j = 0; j < VETH_TQ_STATS_LEN; j++)
161 				ethtool_sprintf(&p, "tx_queue_%u_%.18s",
162 						i, veth_tq_stats_desc[j].desc);
163 
164 		page_pool_ethtool_stats_get_strings(p);
165 		break;
166 	}
167 }
168 
169 static int veth_get_sset_count(struct net_device *dev, int sset)
170 {
171 	switch (sset) {
172 	case ETH_SS_STATS:
173 		return ARRAY_SIZE(ethtool_stats_keys) +
174 		       VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
175 		       VETH_TQ_STATS_LEN * dev->real_num_tx_queues +
176 		       page_pool_ethtool_stats_get_count();
177 	default:
178 		return -EOPNOTSUPP;
179 	}
180 }
181 
182 static void veth_get_page_pool_stats(struct net_device *dev, u64 *data)
183 {
184 #ifdef CONFIG_PAGE_POOL_STATS
185 	struct veth_priv *priv = netdev_priv(dev);
186 	struct page_pool_stats pp_stats = {};
187 	int i;
188 
189 	for (i = 0; i < dev->real_num_rx_queues; i++) {
190 		if (!priv->rq[i].page_pool)
191 			continue;
192 		page_pool_get_stats(priv->rq[i].page_pool, &pp_stats);
193 	}
194 	page_pool_ethtool_stats_get(data, &pp_stats);
195 #endif /* CONFIG_PAGE_POOL_STATS */
196 }
197 
198 static void veth_get_ethtool_stats(struct net_device *dev,
199 		struct ethtool_stats *stats, u64 *data)
200 {
201 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
202 	struct net_device *peer = rtnl_dereference(priv->peer);
203 	int i, j, idx, pp_idx;
204 
205 	data[0] = peer ? peer->ifindex : 0;
206 	idx = 1;
207 	for (i = 0; i < dev->real_num_rx_queues; i++) {
208 		const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
209 		const void *stats_base = (void *)&rq_stats->vs;
210 		unsigned int start;
211 		size_t offset;
212 
213 		do {
214 			start = u64_stats_fetch_begin(&rq_stats->syncp);
215 			for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
216 				offset = veth_rq_stats_desc[j].offset;
217 				data[idx + j] = *(u64 *)(stats_base + offset);
218 			}
219 		} while (u64_stats_fetch_retry(&rq_stats->syncp, start));
220 		idx += VETH_RQ_STATS_LEN;
221 	}
222 	pp_idx = idx;
223 
224 	if (!peer)
225 		goto page_pool_stats;
226 
227 	rcv_priv = netdev_priv(peer);
228 	for (i = 0; i < peer->real_num_rx_queues; i++) {
229 		const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
230 		const void *base = (void *)&rq_stats->vs;
231 		unsigned int start, tx_idx = idx;
232 		u64 buf[VETH_TQ_STATS_LEN];
233 		size_t offset;
234 
235 		do {
236 			start = u64_stats_fetch_begin(&rq_stats->syncp);
237 			for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
238 				offset = veth_tq_stats_desc[j].offset;
239 				buf[j] = *(u64 *)(base + offset);
240 			}
241 		} while (u64_stats_fetch_retry(&rq_stats->syncp, start));
242 
243 		tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
244 		for (j = 0; j < VETH_TQ_STATS_LEN; j++)
245 			data[tx_idx + j] += buf[j];
246 	}
247 	pp_idx = idx + dev->real_num_tx_queues * VETH_TQ_STATS_LEN;
248 
249 page_pool_stats:
250 	veth_get_page_pool_stats(dev, &data[pp_idx]);
251 }
252 
253 static void veth_get_channels(struct net_device *dev,
254 			      struct ethtool_channels *channels)
255 {
256 	channels->tx_count = dev->real_num_tx_queues;
257 	channels->rx_count = dev->real_num_rx_queues;
258 	channels->max_tx = dev->num_tx_queues;
259 	channels->max_rx = dev->num_rx_queues;
260 }
261 
262 static int veth_set_channels(struct net_device *dev,
263 			     struct ethtool_channels *ch);
264 
265 static const struct ethtool_ops veth_ethtool_ops = {
266 	.get_drvinfo		= veth_get_drvinfo,
267 	.get_link		= ethtool_op_get_link,
268 	.get_strings		= veth_get_strings,
269 	.get_sset_count		= veth_get_sset_count,
270 	.get_ethtool_stats	= veth_get_ethtool_stats,
271 	.get_link_ksettings	= veth_get_link_ksettings,
272 	.get_ts_info		= ethtool_op_get_ts_info,
273 	.get_channels		= veth_get_channels,
274 	.set_channels		= veth_set_channels,
275 };
276 
277 /* general routines */
278 
279 static bool veth_is_xdp_frame(void *ptr)
280 {
281 	return (unsigned long)ptr & VETH_XDP_FLAG;
282 }
283 
284 static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
285 {
286 	return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
287 }
288 
289 static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
290 {
291 	return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
292 }
293 
294 static void veth_ptr_free(void *ptr)
295 {
296 	if (veth_is_xdp_frame(ptr))
297 		xdp_return_frame(veth_ptr_to_xdp(ptr));
298 	else
299 		kfree_skb(ptr);
300 }
301 
302 static void __veth_xdp_flush(struct veth_rq *rq)
303 {
304 	/* Write ptr_ring before reading rx_notify_masked */
305 	smp_mb();
306 	if (!READ_ONCE(rq->rx_notify_masked) &&
307 	    napi_schedule_prep(&rq->xdp_napi)) {
308 		WRITE_ONCE(rq->rx_notify_masked, true);
309 		__napi_schedule(&rq->xdp_napi);
310 	}
311 }
312 
313 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
314 {
315 	if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb)))
316 		return NETDEV_TX_BUSY; /* signal qdisc layer */
317 
318 	return NET_RX_SUCCESS; /* same as NETDEV_TX_OK */
319 }
320 
321 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
322 			    struct veth_rq *rq, bool xdp)
323 {
324 	return __dev_forward_skb(dev, skb) ?: xdp ?
325 		veth_xdp_rx(rq, skb) :
326 		__netif_rx(skb);
327 }
328 
329 /* return true if the specified skb has chances of GRO aggregation
330  * Don't strive for accuracy, but try to avoid GRO overhead in the most
331  * common scenarios.
332  * When XDP is enabled, all traffic is considered eligible, as the xmit
333  * device has TSO off.
334  * When TSO is enabled on the xmit device, we are likely interested only
335  * in UDP aggregation, explicitly check for that if the skb is suspected
336  * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
337  * to belong to locally generated UDP traffic.
338  */
339 static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
340 					 const struct net_device *rcv,
341 					 const struct sk_buff *skb)
342 {
343 	return !(dev->features & NETIF_F_ALL_TSO) ||
344 		(skb->destructor == sock_wfree &&
345 		 rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
346 }
347 
348 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
349 {
350 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
351 	struct veth_rq *rq = NULL;
352 	struct netdev_queue *txq;
353 	struct net_device *rcv;
354 	int length = skb->len;
355 	bool use_napi = false;
356 	int ret, rxq;
357 
358 	rcu_read_lock();
359 	rcv = rcu_dereference(priv->peer);
360 	if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
361 		kfree_skb(skb);
362 		goto drop;
363 	}
364 
365 	rcv_priv = netdev_priv(rcv);
366 	rxq = skb_get_queue_mapping(skb);
367 	if (rxq < rcv->real_num_rx_queues) {
368 		rq = &rcv_priv->rq[rxq];
369 
370 		/* The napi pointer is available when an XDP program is
371 		 * attached or when GRO is enabled
372 		 * Don't bother with napi/GRO if the skb can't be aggregated
373 		 */
374 		use_napi = rcu_access_pointer(rq->napi) &&
375 			   veth_skb_is_eligible_for_gro(dev, rcv, skb);
376 	}
377 
378 	skb_tx_timestamp(skb);
379 
380 	/* tx queue index of this device, meaningless as rx queue of the peer */
381 	skb_set_queue_mapping(skb, 0);
382 
383 	ret = veth_forward_skb(rcv, skb, rq, use_napi);
384 	switch (ret) {
385 	case NET_RX_SUCCESS: /* same as NETDEV_TX_OK */
386 		if (!use_napi)
387 			dev_sw_netstats_tx_add(dev, 1, length);
388 		else
389 			__veth_xdp_flush(rq);
390 		break;
391 	case NETDEV_TX_BUSY:
392 		/* If a qdisc is attached to our virtual device, returning
393 		 * NETDEV_TX_BUSY is allowed.
394 		 */
395 		txq = netdev_get_tx_queue(dev, rxq);
396 
397 		if (qdisc_txq_has_no_queue(txq)) {
398 			dev_kfree_skb_any(skb);
399 			goto drop;
400 		}
401 		/* Restore Eth hdr pulled by dev_forward_skb/eth_type_trans */
402 		__skb_push(skb, ETH_HLEN);
403 		/* qdisc requeues the skb on the txq it points to */
404 		skb_set_queue_mapping(skb, rxq);
405 		netif_tx_stop_queue(txq);
406 		/* Makes sure NAPI peer consumer runs. Consumer is responsible
407 		 * for starting txq again, until then ndo_start_xmit (this
408 		 * function) will not be invoked by the netstack again.
409 		 */
410 		__veth_xdp_flush(rq);
411 		break;
412 	case NET_RX_DROP: /* same as NET_XMIT_DROP */
413 drop:
414 		atomic64_inc(&priv->dropped);
415 		ret = NET_XMIT_DROP;
416 		break;
417 	default:
418 		net_crit_ratelimited("%s(%s): Invalid return code(%d)",
419 				     __func__, dev->name, ret);
420 	}
421 	rcu_read_unlock();
422 
423 	return ret;
424 }
425 
426 static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
427 {
428 	struct veth_priv *priv = netdev_priv(dev);
429 	int i;
430 
431 	result->peer_tq_xdp_xmit_err = 0;
432 	result->xdp_packets = 0;
433 	result->xdp_tx_err = 0;
434 	result->xdp_bytes = 0;
435 	result->rx_drops = 0;
436 	for (i = 0; i < dev->num_rx_queues; i++) {
437 		u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
438 		struct veth_rq_stats *stats = &priv->rq[i].stats;
439 		unsigned int start;
440 
441 		do {
442 			start = u64_stats_fetch_begin(&stats->syncp);
443 			peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
444 			xdp_tx_err = stats->vs.xdp_tx_err;
445 			packets = stats->vs.xdp_packets;
446 			bytes = stats->vs.xdp_bytes;
447 			drops = stats->vs.rx_drops;
448 		} while (u64_stats_fetch_retry(&stats->syncp, start));
449 		result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
450 		result->xdp_tx_err += xdp_tx_err;
451 		result->xdp_packets += packets;
452 		result->xdp_bytes += bytes;
453 		result->rx_drops += drops;
454 	}
455 }
456 
457 static void veth_get_stats64(struct net_device *dev,
458 			     struct rtnl_link_stats64 *tot)
459 {
460 	struct veth_priv *priv = netdev_priv(dev);
461 	struct net_device *peer;
462 	struct veth_stats rx;
463 
464 	tot->tx_dropped = atomic64_read(&priv->dropped);
465 	dev_fetch_sw_netstats(tot, dev->tstats);
466 
467 	veth_stats_rx(&rx, dev);
468 	tot->tx_dropped += rx.xdp_tx_err;
469 	tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
470 	tot->rx_bytes += rx.xdp_bytes;
471 	tot->rx_packets += rx.xdp_packets;
472 
473 	rcu_read_lock();
474 	peer = rcu_dereference(priv->peer);
475 	if (peer) {
476 		struct rtnl_link_stats64 tot_peer = {};
477 
478 		dev_fetch_sw_netstats(&tot_peer, peer->tstats);
479 		tot->rx_bytes += tot_peer.tx_bytes;
480 		tot->rx_packets += tot_peer.tx_packets;
481 
482 		veth_stats_rx(&rx, peer);
483 		tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
484 		tot->rx_dropped += rx.xdp_tx_err;
485 		tot->tx_bytes += rx.xdp_bytes;
486 		tot->tx_packets += rx.xdp_packets;
487 	}
488 	rcu_read_unlock();
489 }
490 
491 /* fake multicast ability */
492 static void veth_set_multicast_list(struct net_device *dev)
493 {
494 }
495 
496 static int veth_select_rxq(struct net_device *dev)
497 {
498 	return smp_processor_id() % dev->real_num_rx_queues;
499 }
500 
501 static struct net_device *veth_peer_dev(struct net_device *dev)
502 {
503 	struct veth_priv *priv = netdev_priv(dev);
504 
505 	/* Callers must be under RCU read side. */
506 	return rcu_dereference(priv->peer);
507 }
508 
509 static int veth_xdp_xmit(struct net_device *dev, int n,
510 			 struct xdp_frame **frames,
511 			 u32 flags, bool ndo_xmit)
512 {
513 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
514 	int i, ret = -ENXIO, nxmit = 0;
515 	struct net_device *rcv;
516 	unsigned int max_len;
517 	struct veth_rq *rq;
518 
519 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
520 		return -EINVAL;
521 
522 	rcu_read_lock();
523 	rcv = rcu_dereference(priv->peer);
524 	if (unlikely(!rcv))
525 		goto out;
526 
527 	rcv_priv = netdev_priv(rcv);
528 	rq = &rcv_priv->rq[veth_select_rxq(rcv)];
529 	/* The napi pointer is set if NAPI is enabled, which ensures that
530 	 * xdp_ring is initialized on receive side and the peer device is up.
531 	 */
532 	if (!rcu_access_pointer(rq->napi))
533 		goto out;
534 
535 	max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
536 
537 	spin_lock(&rq->xdp_ring.producer_lock);
538 	for (i = 0; i < n; i++) {
539 		struct xdp_frame *frame = frames[i];
540 		void *ptr = veth_xdp_to_ptr(frame);
541 
542 		if (unlikely(xdp_get_frame_len(frame) > max_len ||
543 			     __ptr_ring_produce(&rq->xdp_ring, ptr)))
544 			break;
545 		nxmit++;
546 	}
547 	spin_unlock(&rq->xdp_ring.producer_lock);
548 
549 	if (flags & XDP_XMIT_FLUSH)
550 		__veth_xdp_flush(rq);
551 
552 	ret = nxmit;
553 	if (ndo_xmit) {
554 		u64_stats_update_begin(&rq->stats.syncp);
555 		rq->stats.vs.peer_tq_xdp_xmit += nxmit;
556 		rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
557 		u64_stats_update_end(&rq->stats.syncp);
558 	}
559 
560 out:
561 	rcu_read_unlock();
562 
563 	return ret;
564 }
565 
566 static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
567 			     struct xdp_frame **frames, u32 flags)
568 {
569 	int err;
570 
571 	err = veth_xdp_xmit(dev, n, frames, flags, true);
572 	if (err < 0) {
573 		struct veth_priv *priv = netdev_priv(dev);
574 
575 		atomic64_add(n, &priv->dropped);
576 	}
577 
578 	return err;
579 }
580 
581 static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
582 {
583 	int sent, i, err = 0, drops;
584 
585 	sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
586 	if (sent < 0) {
587 		err = sent;
588 		sent = 0;
589 	}
590 
591 	for (i = sent; unlikely(i < bq->count); i++)
592 		xdp_return_frame(bq->q[i]);
593 
594 	drops = bq->count - sent;
595 	trace_xdp_bulk_tx(rq->dev, sent, drops, err);
596 
597 	u64_stats_update_begin(&rq->stats.syncp);
598 	rq->stats.vs.xdp_tx += sent;
599 	rq->stats.vs.xdp_tx_err += drops;
600 	u64_stats_update_end(&rq->stats.syncp);
601 
602 	bq->count = 0;
603 }
604 
605 static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
606 {
607 	struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
608 	struct net_device *rcv;
609 	struct veth_rq *rcv_rq;
610 
611 	rcu_read_lock();
612 	veth_xdp_flush_bq(rq, bq);
613 	rcv = rcu_dereference(priv->peer);
614 	if (unlikely(!rcv))
615 		goto out;
616 
617 	rcv_priv = netdev_priv(rcv);
618 	rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
619 	/* xdp_ring is initialized on receive side? */
620 	if (unlikely(!rcu_access_pointer(rcv_rq->napi)))
621 		goto out;
622 
623 	__veth_xdp_flush(rcv_rq);
624 out:
625 	rcu_read_unlock();
626 }
627 
628 static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
629 		       struct veth_xdp_tx_bq *bq)
630 {
631 	struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
632 
633 	if (unlikely(!frame))
634 		return -EOVERFLOW;
635 
636 	if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
637 		veth_xdp_flush_bq(rq, bq);
638 
639 	bq->q[bq->count++] = frame;
640 
641 	return 0;
642 }
643 
644 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
645 					  struct xdp_frame *frame,
646 					  struct veth_xdp_tx_bq *bq,
647 					  struct veth_stats *stats)
648 {
649 	struct xdp_frame orig_frame;
650 	struct bpf_prog *xdp_prog;
651 
652 	rcu_read_lock();
653 	xdp_prog = rcu_dereference(rq->xdp_prog);
654 	if (likely(xdp_prog)) {
655 		struct veth_xdp_buff vxbuf;
656 		struct xdp_buff *xdp = &vxbuf.xdp;
657 		u32 act;
658 
659 		xdp_convert_frame_to_buff(frame, xdp);
660 		xdp->rxq = &rq->xdp_rxq;
661 		vxbuf.skb = NULL;
662 
663 		act = bpf_prog_run_xdp(xdp_prog, xdp);
664 
665 		switch (act) {
666 		case XDP_PASS:
667 			if (xdp_update_frame_from_buff(xdp, frame))
668 				goto err_xdp;
669 			break;
670 		case XDP_TX:
671 			orig_frame = *frame;
672 			xdp->rxq->mem.type = frame->mem_type;
673 			if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
674 				trace_xdp_exception(rq->dev, xdp_prog, act);
675 				frame = &orig_frame;
676 				stats->rx_drops++;
677 				goto err_xdp;
678 			}
679 			stats->xdp_tx++;
680 			rcu_read_unlock();
681 			goto xdp_xmit;
682 		case XDP_REDIRECT:
683 			orig_frame = *frame;
684 			xdp->rxq->mem.type = frame->mem_type;
685 			if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
686 				frame = &orig_frame;
687 				stats->rx_drops++;
688 				goto err_xdp;
689 			}
690 			stats->xdp_redirect++;
691 			rcu_read_unlock();
692 			goto xdp_xmit;
693 		default:
694 			bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
695 			fallthrough;
696 		case XDP_ABORTED:
697 			trace_xdp_exception(rq->dev, xdp_prog, act);
698 			fallthrough;
699 		case XDP_DROP:
700 			stats->xdp_drops++;
701 			goto err_xdp;
702 		}
703 	}
704 	rcu_read_unlock();
705 
706 	return frame;
707 err_xdp:
708 	rcu_read_unlock();
709 	xdp_return_frame(frame);
710 xdp_xmit:
711 	return NULL;
712 }
713 
714 /* frames array contains VETH_XDP_BATCH at most */
715 static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
716 				  int n_xdpf, struct veth_xdp_tx_bq *bq,
717 				  struct veth_stats *stats)
718 {
719 	void *skbs[VETH_XDP_BATCH];
720 	int i;
721 
722 	if (unlikely(!napi_skb_cache_get_bulk(skbs, n_xdpf))) {
723 		for (i = 0; i < n_xdpf; i++)
724 			xdp_return_frame(frames[i]);
725 		stats->rx_drops += n_xdpf;
726 
727 		return;
728 	}
729 
730 	for (i = 0; i < n_xdpf; i++) {
731 		struct sk_buff *skb = skbs[i];
732 
733 		skb = __xdp_build_skb_from_frame(frames[i], skb,
734 						 rq->dev);
735 		if (!skb) {
736 			xdp_return_frame(frames[i]);
737 			stats->rx_drops++;
738 			continue;
739 		}
740 		napi_gro_receive(&rq->xdp_napi, skb);
741 	}
742 }
743 
744 static void veth_xdp_get(struct xdp_buff *xdp)
745 {
746 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
747 	int i;
748 
749 	get_page(virt_to_page(xdp->data));
750 	if (likely(!xdp_buff_has_frags(xdp)))
751 		return;
752 
753 	for (i = 0; i < sinfo->nr_frags; i++)
754 		netmem_inc_ref(skb_frag_netmem(&sinfo->frags[i]));
755 }
756 
757 static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
758 					struct xdp_buff *xdp,
759 					struct sk_buff **pskb)
760 {
761 	struct sk_buff *skb = *pskb;
762 	u32 frame_sz;
763 
764 	if (skb_shared(skb) || skb_head_is_locked(skb) ||
765 	    skb_is_nonlinear(skb) ||
766 	    skb_headroom(skb) < XDP_PACKET_HEADROOM) {
767 		if (skb_pp_cow_data(rq->page_pool, pskb, XDP_PACKET_HEADROOM))
768 			goto drop;
769 
770 		skb = *pskb;
771 	}
772 
773 	/* SKB "head" area always have tailroom for skb_shared_info */
774 	frame_sz = skb_end_pointer(skb) - skb->head;
775 	frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
776 	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
777 	xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
778 			 skb_headlen(skb), true);
779 
780 	if (skb_shinfo(skb)->nr_frags) {
781 		skb_shinfo(skb)->xdp_frags_size = skb->data_len;
782 		xdp_buff_set_frags_flag(xdp);
783 	} else {
784 		xdp_buff_clear_frags_flag(xdp);
785 	}
786 	*pskb = skb;
787 
788 	return 0;
789 drop:
790 	consume_skb(skb);
791 	*pskb = NULL;
792 
793 	return -ENOMEM;
794 }
795 
796 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
797 					struct sk_buff *skb,
798 					struct veth_xdp_tx_bq *bq,
799 					struct veth_stats *stats)
800 {
801 	void *orig_data, *orig_data_end;
802 	struct bpf_prog *xdp_prog;
803 	struct veth_xdp_buff vxbuf;
804 	struct xdp_buff *xdp = &vxbuf.xdp;
805 	u32 act, metalen;
806 	int off;
807 
808 	skb_prepare_for_gro(skb);
809 
810 	rcu_read_lock();
811 	xdp_prog = rcu_dereference(rq->xdp_prog);
812 	if (unlikely(!xdp_prog)) {
813 		rcu_read_unlock();
814 		goto out;
815 	}
816 
817 	__skb_push(skb, skb->data - skb_mac_header(skb));
818 	if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
819 		goto drop;
820 	vxbuf.skb = skb;
821 
822 	orig_data = xdp->data;
823 	orig_data_end = xdp->data_end;
824 
825 	act = bpf_prog_run_xdp(xdp_prog, xdp);
826 
827 	switch (act) {
828 	case XDP_PASS:
829 		break;
830 	case XDP_TX:
831 		veth_xdp_get(xdp);
832 		consume_skb(skb);
833 		xdp->rxq->mem = rq->xdp_mem;
834 		if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
835 			trace_xdp_exception(rq->dev, xdp_prog, act);
836 			stats->rx_drops++;
837 			goto err_xdp;
838 		}
839 		stats->xdp_tx++;
840 		rcu_read_unlock();
841 		goto xdp_xmit;
842 	case XDP_REDIRECT:
843 		veth_xdp_get(xdp);
844 		consume_skb(skb);
845 		xdp->rxq->mem = rq->xdp_mem;
846 		if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
847 			stats->rx_drops++;
848 			goto err_xdp;
849 		}
850 		stats->xdp_redirect++;
851 		rcu_read_unlock();
852 		goto xdp_xmit;
853 	default:
854 		bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
855 		fallthrough;
856 	case XDP_ABORTED:
857 		trace_xdp_exception(rq->dev, xdp_prog, act);
858 		fallthrough;
859 	case XDP_DROP:
860 		stats->xdp_drops++;
861 		goto xdp_drop;
862 	}
863 	rcu_read_unlock();
864 
865 	/* check if bpf_xdp_adjust_head was used */
866 	off = orig_data - xdp->data;
867 	if (off > 0)
868 		__skb_push(skb, off);
869 	else if (off < 0)
870 		__skb_pull(skb, -off);
871 
872 	skb_reset_mac_header(skb);
873 
874 	/* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
875 	 * (e.g. bpf_xdp_adjust_tail). Remove the old fragment contribution
876 	 * from skb->len before updating data_len, then add the new one back.
877 	 */
878 	skb->len -= skb->data_len;
879 	if (xdp_buff_has_frags(xdp)) {
880 		skb->data_len = skb_shinfo(skb)->xdp_frags_size;
881 		skb->len += skb->data_len;
882 	} else {
883 		skb->data_len = 0;
884 	}
885 
886 	/* Synchronize the skb tail with XDP's updated linear area. */
887 	off = xdp->data_end - orig_data_end;
888 	if (off != 0) {
889 		skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
890 		skb->len += off; /* positive on grow, negative on shrink */
891 	}
892 
893 	skb->protocol = eth_type_trans(skb, rq->dev);
894 
895 	metalen = xdp->data - xdp->data_meta;
896 	if (metalen)
897 		skb_metadata_set(skb, metalen);
898 out:
899 	return skb;
900 drop:
901 	stats->rx_drops++;
902 xdp_drop:
903 	rcu_read_unlock();
904 	kfree_skb(skb);
905 	return NULL;
906 err_xdp:
907 	rcu_read_unlock();
908 	xdp_return_buff(xdp);
909 xdp_xmit:
910 	return NULL;
911 }
912 
913 static int veth_xdp_rcv(struct veth_rq *rq, int budget,
914 			struct veth_xdp_tx_bq *bq,
915 			struct veth_stats *stats)
916 {
917 	int i, done = 0, n_xdpf = 0;
918 	void *xdpf[VETH_XDP_BATCH];
919 
920 	for (i = 0; i < budget; i++) {
921 		void *ptr = __ptr_ring_consume(&rq->xdp_ring);
922 
923 		if (!ptr)
924 			break;
925 
926 		if (veth_is_xdp_frame(ptr)) {
927 			/* ndo_xdp_xmit */
928 			struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
929 
930 			stats->xdp_bytes += xdp_get_frame_len(frame);
931 			frame = veth_xdp_rcv_one(rq, frame, bq, stats);
932 			if (frame) {
933 				/* XDP_PASS */
934 				xdpf[n_xdpf++] = frame;
935 				if (n_xdpf == VETH_XDP_BATCH) {
936 					veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
937 							      bq, stats);
938 					n_xdpf = 0;
939 				}
940 			}
941 		} else {
942 			/* ndo_start_xmit */
943 			struct sk_buff *skb = ptr;
944 
945 			stats->xdp_bytes += skb->len;
946 			skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
947 			if (skb) {
948 				if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
949 					netif_receive_skb(skb);
950 				else
951 					napi_gro_receive(&rq->xdp_napi, skb);
952 			}
953 		}
954 		done++;
955 	}
956 
957 	if (n_xdpf)
958 		veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
959 
960 	u64_stats_update_begin(&rq->stats.syncp);
961 	rq->stats.vs.xdp_redirect += stats->xdp_redirect;
962 	rq->stats.vs.xdp_bytes += stats->xdp_bytes;
963 	rq->stats.vs.xdp_drops += stats->xdp_drops;
964 	rq->stats.vs.rx_drops += stats->rx_drops;
965 	rq->stats.vs.xdp_packets += done;
966 	u64_stats_update_end(&rq->stats.syncp);
967 
968 	return done;
969 }
970 
971 static int veth_poll(struct napi_struct *napi, int budget)
972 {
973 	struct veth_rq *rq =
974 		container_of(napi, struct veth_rq, xdp_napi);
975 	struct veth_priv *priv = netdev_priv(rq->dev);
976 	int queue_idx = rq - priv->rq;
977 	struct netdev_queue *peer_txq;
978 	struct veth_stats stats = {};
979 	struct net_device *peer_dev;
980 	struct veth_xdp_tx_bq bq;
981 	int done;
982 
983 	bq.count = 0;
984 
985 	/* NAPI functions as RCU section */
986 	peer_dev = rcu_dereference_check(priv->peer, rcu_read_lock_bh_held());
987 	peer_txq = (peer_dev && queue_idx < peer_dev->num_tx_queues) ?
988 		   netdev_get_tx_queue(peer_dev, queue_idx) : NULL;
989 
990 	xdp_set_return_frame_no_direct();
991 	done = veth_xdp_rcv(rq, budget, &bq, &stats);
992 
993 	if (stats.xdp_redirect > 0)
994 		xdp_do_flush();
995 	if (stats.xdp_tx > 0)
996 		veth_xdp_flush(rq, &bq);
997 	xdp_clear_return_frame_no_direct();
998 
999 	if (done < budget && napi_complete_done(napi, done)) {
1000 		/* Write rx_notify_masked before reading ptr_ring */
1001 		smp_store_mb(rq->rx_notify_masked, false);
1002 		if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
1003 			if (napi_schedule_prep(&rq->xdp_napi)) {
1004 				WRITE_ONCE(rq->rx_notify_masked, true);
1005 				__napi_schedule(&rq->xdp_napi);
1006 			}
1007 		}
1008 	}
1009 
1010 	/* Release backpressure per NAPI poll */
1011 	smp_rmb(); /* Paired with netif_tx_stop_queue set_bit */
1012 	if (peer_txq && netif_tx_queue_stopped(peer_txq)) {
1013 		txq_trans_cond_update(peer_txq);
1014 		netif_tx_wake_queue(peer_txq);
1015 	}
1016 
1017 	return done;
1018 }
1019 
1020 static int veth_create_page_pool(struct veth_rq *rq)
1021 {
1022 	struct page_pool_params pp_params = {
1023 		.order = 0,
1024 		.pool_size = VETH_RING_SIZE,
1025 		.nid = NUMA_NO_NODE,
1026 		.dev = &rq->dev->dev,
1027 	};
1028 
1029 	rq->page_pool = page_pool_create(&pp_params);
1030 	if (IS_ERR(rq->page_pool)) {
1031 		int err = PTR_ERR(rq->page_pool);
1032 
1033 		rq->page_pool = NULL;
1034 		return err;
1035 	}
1036 
1037 	return 0;
1038 }
1039 
1040 static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
1041 {
1042 	struct veth_priv *priv = netdev_priv(dev);
1043 	int err, i;
1044 
1045 	for (i = start; i < end; i++) {
1046 		err = veth_create_page_pool(&priv->rq[i]);
1047 		if (err)
1048 			goto err_page_pool;
1049 	}
1050 
1051 	for (i = start; i < end; i++) {
1052 		struct veth_rq *rq = &priv->rq[i];
1053 
1054 		err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
1055 		if (err)
1056 			goto err_xdp_ring;
1057 	}
1058 
1059 	for (i = start; i < end; i++) {
1060 		struct veth_rq *rq = &priv->rq[i];
1061 
1062 		rcu_assign_pointer(rq->xdp_prog, priv->_xdp_prog);
1063 		napi_enable(&rq->xdp_napi);
1064 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1065 	}
1066 
1067 	return 0;
1068 
1069 err_xdp_ring:
1070 	for (i--; i >= start; i--)
1071 		ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
1072 	i = end;
1073 err_page_pool:
1074 	for (i--; i >= start; i--) {
1075 		page_pool_destroy(priv->rq[i].page_pool);
1076 		priv->rq[i].page_pool = NULL;
1077 	}
1078 
1079 	return err;
1080 }
1081 
1082 static int __veth_napi_enable(struct net_device *dev)
1083 {
1084 	return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1085 }
1086 
1087 static void veth_napi_del_range(struct net_device *dev, int start, int end)
1088 {
1089 	struct veth_priv *priv = netdev_priv(dev);
1090 	struct net_device *peer;
1091 	int i;
1092 
1093 	for (i = start; i < end; i++) {
1094 		struct veth_rq *rq = &priv->rq[i];
1095 
1096 		rcu_assign_pointer(priv->rq[i].napi, NULL);
1097 		napi_disable(&rq->xdp_napi);
1098 		rcu_assign_pointer(rq->xdp_prog, NULL);
1099 		__netif_napi_del(&rq->xdp_napi);
1100 	}
1101 	synchronize_net();
1102 
1103 	peer = rtnl_dereference(priv->peer);
1104 
1105 	for (i = start; i < end; i++) {
1106 		struct veth_rq *rq = &priv->rq[i];
1107 
1108 		rq->rx_notify_masked = false;
1109 		ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
1110 
1111 		/* veth_poll() can no longer wake a txq stopped on a full ring */
1112 		if (peer && i < peer->num_tx_queues)
1113 			netif_wake_subqueue(peer, i);
1114 	}
1115 
1116 	for (i = start; i < end; i++) {
1117 		page_pool_destroy(priv->rq[i].page_pool);
1118 		priv->rq[i].page_pool = NULL;
1119 	}
1120 }
1121 
1122 static void veth_napi_del(struct net_device *dev)
1123 {
1124 	veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1125 }
1126 
1127 static bool veth_gro_requested(const struct net_device *dev)
1128 {
1129 	return !!(dev->wanted_features & NETIF_F_GRO);
1130 }
1131 
1132 static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1133 				 bool napi_already_on)
1134 {
1135 	struct veth_priv *priv = netdev_priv(dev);
1136 	int err, i;
1137 
1138 	for (i = start; i < end; i++) {
1139 		struct veth_rq *rq = &priv->rq[i];
1140 
1141 		if (!napi_already_on)
1142 			netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1143 		err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1144 		if (err < 0)
1145 			goto err_rxq_reg;
1146 
1147 		err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1148 						 MEM_TYPE_PAGE_SHARED,
1149 						 NULL);
1150 		if (err < 0)
1151 			goto err_reg_mem;
1152 
1153 		/* Save original mem info as it can be overwritten */
1154 		rq->xdp_mem = rq->xdp_rxq.mem;
1155 	}
1156 	return 0;
1157 
1158 err_reg_mem:
1159 	xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1160 err_rxq_reg:
1161 	if (!napi_already_on)
1162 		netif_napi_del(&priv->rq[i].xdp_napi);
1163 	for (i--; i >= start; i--) {
1164 		struct veth_rq *rq = &priv->rq[i];
1165 
1166 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1167 		if (!napi_already_on)
1168 			netif_napi_del(&rq->xdp_napi);
1169 	}
1170 
1171 	return err;
1172 }
1173 
1174 static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1175 				   bool delete_napi)
1176 {
1177 	struct veth_priv *priv = netdev_priv(dev);
1178 	int i;
1179 
1180 	for (i = start; i < end; i++) {
1181 		struct veth_rq *rq = &priv->rq[i];
1182 
1183 		rq->xdp_rxq.mem = rq->xdp_mem;
1184 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1185 
1186 		if (delete_napi)
1187 			netif_napi_del(&rq->xdp_napi);
1188 	}
1189 }
1190 
1191 static int veth_enable_xdp(struct net_device *dev)
1192 {
1193 	bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1194 	struct veth_priv *priv = netdev_priv(dev);
1195 	int err, i;
1196 
1197 	if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1198 		err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1199 		if (err)
1200 			return err;
1201 
1202 		if (!napi_already_on) {
1203 			err = __veth_napi_enable(dev);
1204 			if (err) {
1205 				veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1206 				return err;
1207 			}
1208 		}
1209 	}
1210 
1211 	for (i = 0; i < dev->real_num_rx_queues; i++) {
1212 		rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1213 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1214 	}
1215 
1216 	return 0;
1217 }
1218 
1219 static void veth_disable_xdp(struct net_device *dev)
1220 {
1221 	struct veth_priv *priv = netdev_priv(dev);
1222 	int i;
1223 
1224 	for (i = 0; i < dev->real_num_rx_queues; i++)
1225 		rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1226 
1227 	if (!netif_running(dev) || !veth_gro_requested(dev))
1228 		veth_napi_del(dev);
1229 
1230 	veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1231 }
1232 
1233 static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1234 {
1235 	struct veth_priv *priv = netdev_priv(dev);
1236 	int err, i;
1237 
1238 	for (i = start; i < end; i++) {
1239 		struct veth_rq *rq = &priv->rq[i];
1240 
1241 		netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1242 	}
1243 
1244 	err = __veth_napi_enable_range(dev, start, end);
1245 	if (err) {
1246 		for (i = start; i < end; i++) {
1247 			struct veth_rq *rq = &priv->rq[i];
1248 
1249 			netif_napi_del(&rq->xdp_napi);
1250 		}
1251 		return err;
1252 	}
1253 	return err;
1254 }
1255 
1256 static int veth_napi_enable(struct net_device *dev)
1257 {
1258 	return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1259 }
1260 
1261 static void veth_disable_range_safe(struct net_device *dev, int start, int end)
1262 {
1263 	struct veth_priv *priv = netdev_priv(dev);
1264 
1265 	if (start >= end)
1266 		return;
1267 
1268 	if (priv->_xdp_prog) {
1269 		veth_napi_del_range(dev, start, end);
1270 		veth_disable_xdp_range(dev, start, end, false);
1271 	} else if (veth_gro_requested(dev)) {
1272 		veth_napi_del_range(dev, start, end);
1273 	}
1274 }
1275 
1276 static int veth_enable_range_safe(struct net_device *dev, int start, int end)
1277 {
1278 	struct veth_priv *priv = netdev_priv(dev);
1279 	int err;
1280 
1281 	if (start >= end)
1282 		return 0;
1283 
1284 	if (priv->_xdp_prog) {
1285 		/* these channels are freshly initialized, napi is not on there even
1286 		 * when GRO is requeste
1287 		 */
1288 		err = veth_enable_xdp_range(dev, start, end, false);
1289 		if (err)
1290 			return err;
1291 
1292 		err = __veth_napi_enable_range(dev, start, end);
1293 		if (err) {
1294 			/* on error always delete the newly added napis */
1295 			veth_disable_xdp_range(dev, start, end, true);
1296 			return err;
1297 		}
1298 	} else if (veth_gro_requested(dev)) {
1299 		return veth_napi_enable_range(dev, start, end);
1300 	}
1301 	return 0;
1302 }
1303 
1304 static void veth_set_xdp_features(struct net_device *dev)
1305 {
1306 	struct veth_priv *priv = netdev_priv(dev);
1307 	struct net_device *peer;
1308 
1309 	peer = rtnl_dereference(priv->peer);
1310 	if (peer && peer->real_num_tx_queues <= dev->real_num_rx_queues) {
1311 		struct veth_priv *priv_peer = netdev_priv(peer);
1312 		xdp_features_t val = NETDEV_XDP_ACT_BASIC |
1313 				     NETDEV_XDP_ACT_REDIRECT |
1314 				     NETDEV_XDP_ACT_RX_SG;
1315 
1316 		if (priv_peer->_xdp_prog || veth_gro_requested(peer))
1317 			val |= NETDEV_XDP_ACT_NDO_XMIT |
1318 			       NETDEV_XDP_ACT_NDO_XMIT_SG;
1319 		xdp_set_features_flag(dev, val);
1320 	} else {
1321 		xdp_clear_features_flag(dev);
1322 	}
1323 }
1324 
1325 static int veth_set_channels(struct net_device *dev,
1326 			     struct ethtool_channels *ch)
1327 {
1328 	struct veth_priv *priv = netdev_priv(dev);
1329 	unsigned int old_rx_count, new_rx_count;
1330 	struct veth_priv *peer_priv;
1331 	struct net_device *peer;
1332 	int err;
1333 
1334 	/* sanity check. Upper bounds are already enforced by the caller */
1335 	if (!ch->rx_count || !ch->tx_count)
1336 		return -EINVAL;
1337 
1338 	/* avoid braking XDP, if that is enabled */
1339 	peer = rtnl_dereference(priv->peer);
1340 	peer_priv = peer ? netdev_priv(peer) : NULL;
1341 	if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
1342 		return -EINVAL;
1343 
1344 	if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
1345 		return -EINVAL;
1346 
1347 	old_rx_count = dev->real_num_rx_queues;
1348 	new_rx_count = ch->rx_count;
1349 	if (netif_running(dev)) {
1350 		/* turn device off */
1351 		netif_carrier_off(dev);
1352 		if (peer)
1353 			netif_carrier_off(peer);
1354 
1355 		/* try to allocate new resources, as needed*/
1356 		err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
1357 		if (err)
1358 			goto out;
1359 	}
1360 
1361 	err = netif_set_real_num_rx_queues(dev, ch->rx_count);
1362 	if (err)
1363 		goto revert;
1364 
1365 	err = netif_set_real_num_tx_queues(dev, ch->tx_count);
1366 	if (err) {
1367 		int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
1368 
1369 		/* this error condition could happen only if rx and tx change
1370 		 * in opposite directions (e.g. tx nr raises, rx nr decreases)
1371 		 * and we can't do anything to fully restore the original
1372 		 * status
1373 		 */
1374 		if (err2)
1375 			pr_warn("Can't restore rx queues config %d -> %d %d",
1376 				new_rx_count, old_rx_count, err2);
1377 		else
1378 			goto revert;
1379 	}
1380 
1381 out:
1382 	if (netif_running(dev)) {
1383 		/* note that we need to swap the arguments WRT the enable part
1384 		 * to identify the range we have to disable
1385 		 */
1386 		veth_disable_range_safe(dev, new_rx_count, old_rx_count);
1387 		if (peer && (peer->flags & IFF_UP)) {
1388 			netif_carrier_on(dev);
1389 			netif_carrier_on(peer);
1390 		}
1391 	}
1392 
1393 	/* update XDP supported features */
1394 	veth_set_xdp_features(dev);
1395 	if (peer)
1396 		veth_set_xdp_features(peer);
1397 
1398 	return err;
1399 
1400 revert:
1401 	new_rx_count = old_rx_count;
1402 	old_rx_count = ch->rx_count;
1403 	goto out;
1404 }
1405 
1406 static int veth_open(struct net_device *dev)
1407 {
1408 	struct veth_priv *priv = netdev_priv(dev);
1409 	struct net_device *peer = rtnl_dereference(priv->peer);
1410 	int err;
1411 
1412 	if (!peer)
1413 		return -ENOTCONN;
1414 
1415 	if (priv->_xdp_prog) {
1416 		err = veth_enable_xdp(dev);
1417 		if (err)
1418 			return err;
1419 	} else if (veth_gro_requested(dev)) {
1420 		err = veth_napi_enable(dev);
1421 		if (err)
1422 			return err;
1423 	}
1424 
1425 	if (peer->flags & IFF_UP) {
1426 		netif_carrier_on(dev);
1427 		netif_carrier_on(peer);
1428 	}
1429 
1430 	veth_set_xdp_features(dev);
1431 	veth_set_xdp_features(peer);
1432 
1433 	return 0;
1434 }
1435 
1436 static int veth_close(struct net_device *dev)
1437 {
1438 	struct veth_priv *priv = netdev_priv(dev);
1439 	struct net_device *peer = rtnl_dereference(priv->peer);
1440 
1441 	netif_carrier_off(dev);
1442 	if (peer)
1443 		netif_carrier_off(peer);
1444 
1445 	if (priv->_xdp_prog)
1446 		veth_disable_xdp(dev);
1447 	else if (veth_gro_requested(dev))
1448 		veth_napi_del(dev);
1449 
1450 	return 0;
1451 }
1452 
1453 static int is_valid_veth_mtu(int mtu)
1454 {
1455 	return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
1456 }
1457 
1458 static int veth_alloc_queues(struct net_device *dev)
1459 {
1460 	struct veth_priv *priv = netdev_priv(dev);
1461 	int i;
1462 
1463 	priv->rq = kvzalloc_objs(*priv->rq, dev->num_rx_queues,
1464 				 GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
1465 	if (!priv->rq)
1466 		return -ENOMEM;
1467 
1468 	for (i = 0; i < dev->num_rx_queues; i++) {
1469 		priv->rq[i].dev = dev;
1470 		u64_stats_init(&priv->rq[i].stats.syncp);
1471 	}
1472 
1473 	return 0;
1474 }
1475 
1476 static void veth_free_queues(struct net_device *dev)
1477 {
1478 	struct veth_priv *priv = netdev_priv(dev);
1479 
1480 	kvfree(priv->rq);
1481 }
1482 
1483 static int veth_dev_init(struct net_device *dev)
1484 {
1485 	netdev_lockdep_set_classes(dev);
1486 	return veth_alloc_queues(dev);
1487 }
1488 
1489 static void veth_dev_free(struct net_device *dev)
1490 {
1491 	veth_free_queues(dev);
1492 }
1493 
1494 #ifdef CONFIG_NET_POLL_CONTROLLER
1495 static void veth_poll_controller(struct net_device *dev)
1496 {
1497 	/* veth only receives frames when its peer sends one
1498 	 * Since it has nothing to do with disabling irqs, we are guaranteed
1499 	 * never to have pending data when we poll for it so
1500 	 * there is nothing to do here.
1501 	 *
1502 	 * We need this though so netpoll recognizes us as an interface that
1503 	 * supports polling, which enables bridge devices in virt setups to
1504 	 * still use netconsole
1505 	 */
1506 }
1507 #endif	/* CONFIG_NET_POLL_CONTROLLER */
1508 
1509 static int veth_get_iflink(const struct net_device *dev)
1510 {
1511 	struct veth_priv *priv = netdev_priv(dev);
1512 	struct net_device *peer;
1513 	int iflink;
1514 
1515 	rcu_read_lock();
1516 	peer = rcu_dereference(priv->peer);
1517 	iflink = peer ? READ_ONCE(peer->ifindex) : 0;
1518 	rcu_read_unlock();
1519 
1520 	return iflink;
1521 }
1522 
1523 static netdev_features_t veth_fix_features(struct net_device *dev,
1524 					   netdev_features_t features)
1525 {
1526 	struct veth_priv *priv = netdev_priv(dev);
1527 	struct net_device *peer;
1528 
1529 	peer = rtnl_dereference(priv->peer);
1530 	if (peer) {
1531 		struct veth_priv *peer_priv = netdev_priv(peer);
1532 
1533 		if (peer_priv->_xdp_prog)
1534 			features &= ~NETIF_F_GSO_SOFTWARE;
1535 	}
1536 
1537 	return features;
1538 }
1539 
1540 static int veth_set_features(struct net_device *dev,
1541 			     netdev_features_t features)
1542 {
1543 	netdev_features_t changed = features ^ dev->features;
1544 	struct veth_priv *priv = netdev_priv(dev);
1545 	struct net_device *peer;
1546 	int err;
1547 
1548 	if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1549 		return 0;
1550 
1551 	peer = rtnl_dereference(priv->peer);
1552 	if (features & NETIF_F_GRO) {
1553 		err = veth_napi_enable(dev);
1554 		if (err)
1555 			return err;
1556 
1557 		if (peer)
1558 			xdp_features_set_redirect_target(peer, true);
1559 	} else {
1560 		if (peer)
1561 			xdp_features_clear_redirect_target(peer);
1562 		veth_napi_del(dev);
1563 	}
1564 	return 0;
1565 }
1566 
1567 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1568 {
1569 	struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1570 	struct net_device *peer;
1571 
1572 	if (new_hr < 0)
1573 		new_hr = 0;
1574 
1575 	rcu_read_lock();
1576 	peer = rcu_dereference(priv->peer);
1577 	if (unlikely(!peer))
1578 		goto out;
1579 
1580 	peer_priv = netdev_priv(peer);
1581 	priv->requested_headroom = new_hr;
1582 	new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1583 	dev->needed_headroom = new_hr;
1584 	peer->needed_headroom = new_hr;
1585 
1586 out:
1587 	rcu_read_unlock();
1588 }
1589 
1590 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1591 			struct netlink_ext_ack *extack)
1592 {
1593 	struct veth_priv *priv = netdev_priv(dev);
1594 	struct bpf_prog *old_prog;
1595 	struct net_device *peer;
1596 	unsigned int max_mtu;
1597 	int err;
1598 
1599 	old_prog = priv->_xdp_prog;
1600 	priv->_xdp_prog = prog;
1601 	peer = rtnl_dereference(priv->peer);
1602 
1603 	if (prog) {
1604 		if (!peer) {
1605 			NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1606 			err = -ENOTCONN;
1607 			goto err;
1608 		}
1609 
1610 		max_mtu = SKB_WITH_OVERHEAD(PAGE_SIZE - VETH_XDP_HEADROOM) -
1611 			  peer->hard_header_len;
1612 		/* Allow increasing the max_mtu if the program supports
1613 		 * XDP fragments.
1614 		 */
1615 		if (prog->aux->xdp_has_frags)
1616 			max_mtu += PAGE_SIZE * MAX_SKB_FRAGS;
1617 
1618 		if (peer->mtu > max_mtu) {
1619 			NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1620 			err = -ERANGE;
1621 			goto err;
1622 		}
1623 
1624 		if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1625 			NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1626 			err = -ENOSPC;
1627 			goto err;
1628 		}
1629 
1630 		if (dev->flags & IFF_UP) {
1631 			err = veth_enable_xdp(dev);
1632 			if (err) {
1633 				NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1634 				goto err;
1635 			}
1636 		}
1637 
1638 		if (!old_prog)
1639 			peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1640 		peer->max_mtu = max_mtu;
1641 
1642 		xdp_features_set_redirect_target(peer, true);
1643 	}
1644 
1645 	if (old_prog) {
1646 		if (!prog) {
1647 			if (peer && !veth_gro_requested(dev))
1648 				xdp_features_clear_redirect_target(peer);
1649 
1650 			if (dev->flags & IFF_UP)
1651 				veth_disable_xdp(dev);
1652 
1653 			if (peer) {
1654 				peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1655 				peer->max_mtu = ETH_MAX_MTU;
1656 			}
1657 		}
1658 		bpf_prog_put(old_prog);
1659 	}
1660 
1661 	if ((!!old_prog ^ !!prog) && peer)
1662 		netdev_update_features(peer);
1663 
1664 	return 0;
1665 err:
1666 	priv->_xdp_prog = old_prog;
1667 
1668 	return err;
1669 }
1670 
1671 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1672 {
1673 	switch (xdp->command) {
1674 	case XDP_SETUP_PROG:
1675 		return veth_xdp_set(dev, xdp->prog, xdp->extack);
1676 	default:
1677 		return -EINVAL;
1678 	}
1679 }
1680 
1681 static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
1682 {
1683 	struct veth_xdp_buff *_ctx = (void *)ctx;
1684 
1685 	if (!_ctx->skb)
1686 		return -ENODATA;
1687 
1688 	*timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
1689 	return 0;
1690 }
1691 
1692 static int veth_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
1693 			    enum xdp_rss_hash_type *rss_type)
1694 {
1695 	struct veth_xdp_buff *_ctx = (void *)ctx;
1696 	struct sk_buff *skb = _ctx->skb;
1697 
1698 	if (!skb)
1699 		return -ENODATA;
1700 
1701 	*hash = skb_get_hash(skb);
1702 	*rss_type = skb->l4_hash ? XDP_RSS_TYPE_L4_ANY : XDP_RSS_TYPE_NONE;
1703 
1704 	return 0;
1705 }
1706 
1707 static int veth_xdp_rx_vlan_tag(const struct xdp_md *ctx, __be16 *vlan_proto,
1708 				u16 *vlan_tci)
1709 {
1710 	const struct veth_xdp_buff *_ctx = (void *)ctx;
1711 	const struct sk_buff *skb = _ctx->skb;
1712 	int err;
1713 
1714 	if (!skb)
1715 		return -ENODATA;
1716 
1717 	err = __vlan_hwaccel_get_tag(skb, vlan_tci);
1718 	if (err)
1719 		return err;
1720 
1721 	*vlan_proto = skb->vlan_proto;
1722 	return err;
1723 }
1724 
1725 static const struct net_device_ops veth_netdev_ops = {
1726 	.ndo_init            = veth_dev_init,
1727 	.ndo_open            = veth_open,
1728 	.ndo_stop            = veth_close,
1729 	.ndo_start_xmit      = veth_xmit,
1730 	.ndo_get_stats64     = veth_get_stats64,
1731 	.ndo_set_rx_mode     = veth_set_multicast_list,
1732 	.ndo_set_mac_address = eth_mac_addr,
1733 #ifdef CONFIG_NET_POLL_CONTROLLER
1734 	.ndo_poll_controller	= veth_poll_controller,
1735 #endif
1736 	.ndo_get_iflink		= veth_get_iflink,
1737 	.ndo_fix_features	= veth_fix_features,
1738 	.ndo_set_features	= veth_set_features,
1739 	.ndo_features_check	= passthru_features_check,
1740 	.ndo_set_rx_headroom	= veth_set_rx_headroom,
1741 	.ndo_bpf		= veth_xdp,
1742 	.ndo_xdp_xmit		= veth_ndo_xdp_xmit,
1743 	.ndo_get_peer_dev	= veth_peer_dev,
1744 };
1745 
1746 static const struct xdp_metadata_ops veth_xdp_metadata_ops = {
1747 	.xmo_rx_timestamp		= veth_xdp_rx_timestamp,
1748 	.xmo_rx_hash			= veth_xdp_rx_hash,
1749 	.xmo_rx_vlan_tag		= veth_xdp_rx_vlan_tag,
1750 };
1751 
1752 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1753 		       NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1754 		       NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1755 		       NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1756 		       NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1757 
1758 static void veth_setup(struct net_device *dev)
1759 {
1760 	ether_setup(dev);
1761 
1762 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1763 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1764 	dev->priv_flags |= IFF_NO_QUEUE;
1765 	dev->priv_flags |= IFF_PHONY_HEADROOM;
1766 	dev->priv_flags |= IFF_DISABLE_NETPOLL;
1767 	dev->lltx = true;
1768 
1769 	dev->netdev_ops = &veth_netdev_ops;
1770 	dev->xdp_metadata_ops = &veth_xdp_metadata_ops;
1771 	dev->ethtool_ops = &veth_ethtool_ops;
1772 	dev->features |= VETH_FEATURES;
1773 	dev->vlan_features = dev->features &
1774 			     ~(NETIF_F_HW_VLAN_CTAG_TX |
1775 			       NETIF_F_HW_VLAN_STAG_TX |
1776 			       NETIF_F_HW_VLAN_CTAG_RX |
1777 			       NETIF_F_HW_VLAN_STAG_RX);
1778 	dev->needs_free_netdev = true;
1779 	dev->priv_destructor = veth_dev_free;
1780 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1781 	dev->max_mtu = ETH_MAX_MTU;
1782 
1783 	dev->hw_features = VETH_FEATURES;
1784 	dev->hw_enc_features = VETH_FEATURES;
1785 	dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1786 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
1787 }
1788 
1789 /*
1790  * netlink interface
1791  */
1792 
1793 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1794 			 struct netlink_ext_ack *extack)
1795 {
1796 	if (tb[IFLA_ADDRESS]) {
1797 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1798 			return -EINVAL;
1799 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1800 			return -EADDRNOTAVAIL;
1801 	}
1802 	if (tb[IFLA_MTU]) {
1803 		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1804 			return -EINVAL;
1805 	}
1806 	return 0;
1807 }
1808 
1809 static struct rtnl_link_ops veth_link_ops;
1810 
1811 static void veth_disable_gro(struct net_device *dev)
1812 {
1813 	dev->features &= ~NETIF_F_GRO;
1814 	dev->wanted_features &= ~NETIF_F_GRO;
1815 	netdev_update_features(dev);
1816 }
1817 
1818 static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
1819 {
1820 	int err;
1821 
1822 	if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
1823 		err = netif_set_real_num_tx_queues(dev, 1);
1824 		if (err)
1825 			return err;
1826 	}
1827 	if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
1828 		err = netif_set_real_num_rx_queues(dev, 1);
1829 		if (err)
1830 			return err;
1831 	}
1832 	return 0;
1833 }
1834 
1835 static int veth_newlink(struct net_device *dev,
1836 			struct rtnl_newlink_params *params,
1837 			struct netlink_ext_ack *extack)
1838 {
1839 	struct net *peer_net = rtnl_newlink_peer_net(params);
1840 	struct nlattr **data = params->data;
1841 	struct nlattr **tb = params->tb;
1842 	int err;
1843 	struct net_device *peer;
1844 	struct veth_priv *priv;
1845 	char ifname[IFNAMSIZ];
1846 	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1847 	unsigned char name_assign_type;
1848 	struct ifinfomsg *ifmp;
1849 
1850 	/*
1851 	 * create and register peer first
1852 	 */
1853 	if (data && data[VETH_INFO_PEER]) {
1854 		struct nlattr *nla_peer = data[VETH_INFO_PEER];
1855 
1856 		ifmp = nla_data(nla_peer);
1857 		rtnl_nla_parse_ifinfomsg(peer_tb, nla_peer, extack);
1858 		tbp = peer_tb;
1859 	} else {
1860 		ifmp = NULL;
1861 		tbp = tb;
1862 	}
1863 
1864 	if (ifmp && tbp[IFLA_IFNAME]) {
1865 		nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1866 		name_assign_type = NET_NAME_USER;
1867 	} else {
1868 		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1869 		name_assign_type = NET_NAME_ENUM;
1870 	}
1871 
1872 	peer = rtnl_create_link(peer_net, ifname, name_assign_type,
1873 				&veth_link_ops, tbp, extack);
1874 	if (IS_ERR(peer))
1875 		return PTR_ERR(peer);
1876 
1877 	if (!ifmp || !tbp[IFLA_ADDRESS])
1878 		eth_hw_addr_random(peer);
1879 
1880 	if (ifmp && (dev->ifindex != 0))
1881 		peer->ifindex = ifmp->ifi_index;
1882 
1883 	netif_inherit_tso_max(peer, dev);
1884 
1885 	err = register_netdevice(peer);
1886 	if (err < 0)
1887 		goto err_register_peer;
1888 
1889 	/* keep GRO disabled by default to be consistent with the established
1890 	 * veth behavior
1891 	 */
1892 	veth_disable_gro(peer);
1893 	netif_carrier_off(peer);
1894 
1895 	err = rtnl_configure_link(peer, ifmp, 0, NULL);
1896 	if (err < 0)
1897 		goto err_configure_peer;
1898 
1899 	/*
1900 	 * register dev last
1901 	 *
1902 	 * note, that since we've registered new device the dev's name
1903 	 * should be re-allocated
1904 	 */
1905 
1906 	if (tb[IFLA_ADDRESS] == NULL)
1907 		eth_hw_addr_random(dev);
1908 
1909 	if (tb[IFLA_IFNAME])
1910 		nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1911 	else
1912 		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1913 
1914 	err = register_netdevice(dev);
1915 	if (err < 0)
1916 		goto err_register_dev;
1917 
1918 	netif_carrier_off(dev);
1919 
1920 	/*
1921 	 * tie the deviced together
1922 	 */
1923 
1924 	priv = netdev_priv(dev);
1925 	rcu_assign_pointer(priv->peer, peer);
1926 	netdev_hold(peer, &priv->peer_tracker, GFP_KERNEL);
1927 	err = veth_init_queues(dev, tb);
1928 	if (err)
1929 		goto err_queues;
1930 
1931 	priv = netdev_priv(peer);
1932 	rcu_assign_pointer(priv->peer, dev);
1933 	netdev_hold(dev, &priv->peer_tracker, GFP_KERNEL);
1934 	err = veth_init_queues(peer, tbp);
1935 	if (err)
1936 		goto err_peer_queues;
1937 
1938 	veth_disable_gro(dev);
1939 	/* update XDP supported features */
1940 	veth_set_xdp_features(dev);
1941 	veth_set_xdp_features(peer);
1942 
1943 	return 0;
1944 
1945 err_peer_queues:
1946 	netdev_put(dev, &priv->peer_tracker);
1947 	priv = netdev_priv(dev);
1948 err_queues:
1949 	netdev_put(peer, &priv->peer_tracker);
1950 	unregister_netdevice(dev);
1951 err_register_dev:
1952 	/* nothing to do */
1953 err_configure_peer:
1954 	unregister_netdevice(peer);
1955 	return err;
1956 
1957 err_register_peer:
1958 	free_netdev(peer);
1959 	return err;
1960 }
1961 
1962 static void veth_dellink(struct net_device *dev, struct list_head *head)
1963 {
1964 	netdevice_tracker *peer_tracker;
1965 	struct net_device *peer;
1966 	struct veth_priv *priv;
1967 
1968 	priv = netdev_priv(dev);
1969 	peer_tracker = &priv->peer_tracker;
1970 	peer = unrcu_pointer(xchg(&priv->peer, NULL));
1971 	if (!peer)
1972 		return;
1973 
1974 	unregister_netdevice_queue(dev, head);
1975 
1976 	priv = netdev_priv(peer);
1977 	dev = unrcu_pointer(xchg(&priv->peer, NULL));
1978 	if (dev)
1979 		unregister_netdevice_queue_net(dev_net(dev), peer, head);
1980 
1981 	netdev_put(peer, peer_tracker);
1982 	netdev_put(dev, &priv->peer_tracker);
1983 }
1984 
1985 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1986 	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
1987 };
1988 
1989 static struct net *veth_get_link_net(const struct net_device *dev)
1990 {
1991 	struct veth_priv *priv = netdev_priv(dev);
1992 	struct net_device *peer = rtnl_dereference(priv->peer);
1993 
1994 	return peer ? dev_net(peer) : dev_net(dev);
1995 }
1996 
1997 static unsigned int veth_get_num_queues(void)
1998 {
1999 	/* enforce the same queue limit as rtnl_create_link */
2000 	int queues = num_possible_cpus();
2001 
2002 	if (queues > 4096)
2003 		queues = 4096;
2004 	return queues;
2005 }
2006 
2007 static struct rtnl_link_ops veth_link_ops = {
2008 	.kind		= DRV_NAME,
2009 	.priv_size	= sizeof(struct veth_priv),
2010 	.setup		= veth_setup,
2011 	.validate	= veth_validate,
2012 	.newlink	= veth_newlink,
2013 	.dellink	= veth_dellink,
2014 	.policy		= veth_policy,
2015 	.peer_type	= VETH_INFO_PEER,
2016 	.maxtype	= VETH_INFO_MAX,
2017 	.get_link_net	= veth_get_link_net,
2018 	.get_num_tx_queues	= veth_get_num_queues,
2019 	.get_num_rx_queues	= veth_get_num_queues,
2020 };
2021 
2022 /*
2023  * init/fini
2024  */
2025 
2026 static __init int veth_init(void)
2027 {
2028 	return rtnl_link_register(&veth_link_ops);
2029 }
2030 
2031 static __exit void veth_exit(void)
2032 {
2033 	rtnl_link_unregister(&veth_link_ops);
2034 }
2035 
2036 module_init(veth_init);
2037 module_exit(veth_exit);
2038 
2039 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
2040 MODULE_LICENSE("GPL v2");
2041 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
2042