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