xref: /openbmc/linux/drivers/net/veth.c (revision 718a18a0)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e314dbdcSPavel Emelyanov /*
3e314dbdcSPavel Emelyanov  *  drivers/net/veth.c
4e314dbdcSPavel Emelyanov  *
5e314dbdcSPavel Emelyanov  *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6e314dbdcSPavel Emelyanov  *
7e314dbdcSPavel Emelyanov  * Author: Pavel Emelianov <xemul@openvz.org>
8e314dbdcSPavel Emelyanov  * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
9e314dbdcSPavel Emelyanov  *
10e314dbdcSPavel Emelyanov  */
11e314dbdcSPavel Emelyanov 
12e314dbdcSPavel Emelyanov #include <linux/netdevice.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14e314dbdcSPavel Emelyanov #include <linux/ethtool.h>
15e314dbdcSPavel Emelyanov #include <linux/etherdevice.h>
16cf05c700SEric Dumazet #include <linux/u64_stats_sync.h>
17e314dbdcSPavel Emelyanov 
18f7b12606SJiri Pirko #include <net/rtnetlink.h>
19e314dbdcSPavel Emelyanov #include <net/dst.h>
20e314dbdcSPavel Emelyanov #include <net/xfrm.h>
21af87a3aaSToshiaki Makita #include <net/xdp.h>
22ecef969eSStephen Hemminger #include <linux/veth.h>
239d9779e7SPaul Gortmaker #include <linux/module.h>
24948d4f21SToshiaki Makita #include <linux/bpf.h>
25948d4f21SToshiaki Makita #include <linux/filter.h>
26948d4f21SToshiaki Makita #include <linux/ptr_ring.h>
27948d4f21SToshiaki Makita #include <linux/bpf_trace.h>
28aa4e689eSMichael Walle #include <linux/net_tstamp.h>
29e314dbdcSPavel Emelyanov 
30e314dbdcSPavel Emelyanov #define DRV_NAME	"veth"
31e314dbdcSPavel Emelyanov #define DRV_VERSION	"1.0"
32e314dbdcSPavel Emelyanov 
339fc8d518SToshiaki Makita #define VETH_XDP_FLAG		BIT(0)
34948d4f21SToshiaki Makita #define VETH_RING_SIZE		256
35948d4f21SToshiaki Makita #define VETH_XDP_HEADROOM	(XDP_PACKET_HEADROOM + NET_IP_ALIGN)
36948d4f21SToshiaki Makita 
379cda7807SToshiaki Makita #define VETH_XDP_TX_BULK_SIZE	16
3865e6dcf7SLorenzo Bianconi #define VETH_XDP_BATCH		16
399cda7807SToshiaki Makita 
4065780c56SLorenzo Bianconi struct veth_stats {
411c5b82e5SLorenzo Bianconi 	u64	rx_drops;
421c5b82e5SLorenzo Bianconi 	/* xdp */
434195e54aSToshiaki Makita 	u64	xdp_packets;
444195e54aSToshiaki Makita 	u64	xdp_bytes;
451c5b82e5SLorenzo Bianconi 	u64	xdp_redirect;
464195e54aSToshiaki Makita 	u64	xdp_drops;
471c5b82e5SLorenzo Bianconi 	u64	xdp_tx;
489152cff0SLorenzo Bianconi 	u64	xdp_tx_err;
495fe6e567SLorenzo Bianconi 	u64	peer_tq_xdp_xmit;
505fe6e567SLorenzo Bianconi 	u64	peer_tq_xdp_xmit_err;
5165780c56SLorenzo Bianconi };
5265780c56SLorenzo Bianconi 
5365780c56SLorenzo Bianconi struct veth_rq_stats {
5465780c56SLorenzo Bianconi 	struct veth_stats	vs;
554195e54aSToshiaki Makita 	struct u64_stats_sync	syncp;
564195e54aSToshiaki Makita };
574195e54aSToshiaki Makita 
58638264dcSToshiaki Makita struct veth_rq {
59948d4f21SToshiaki Makita 	struct napi_struct	xdp_napi;
60d3256efdSPaolo Abeni 	struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
61948d4f21SToshiaki Makita 	struct net_device	*dev;
62948d4f21SToshiaki Makita 	struct bpf_prog __rcu	*xdp_prog;
63d1396004SToshiaki Makita 	struct xdp_mem_info	xdp_mem;
644195e54aSToshiaki Makita 	struct veth_rq_stats	stats;
65948d4f21SToshiaki Makita 	bool			rx_notify_masked;
66948d4f21SToshiaki Makita 	struct ptr_ring		xdp_ring;
67948d4f21SToshiaki Makita 	struct xdp_rxq_info	xdp_rxq;
68e314dbdcSPavel Emelyanov };
69e314dbdcSPavel Emelyanov 
70638264dcSToshiaki Makita struct veth_priv {
71638264dcSToshiaki Makita 	struct net_device __rcu	*peer;
72638264dcSToshiaki Makita 	atomic64_t		dropped;
73638264dcSToshiaki Makita 	struct bpf_prog		*_xdp_prog;
74638264dcSToshiaki Makita 	struct veth_rq		*rq;
75638264dcSToshiaki Makita 	unsigned int		requested_headroom;
76638264dcSToshiaki Makita };
77638264dcSToshiaki Makita 
789cda7807SToshiaki Makita struct veth_xdp_tx_bq {
799cda7807SToshiaki Makita 	struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
809cda7807SToshiaki Makita 	unsigned int count;
819cda7807SToshiaki Makita };
829cda7807SToshiaki Makita 
83e314dbdcSPavel Emelyanov /*
84e314dbdcSPavel Emelyanov  * ethtool interface
85e314dbdcSPavel Emelyanov  */
86e314dbdcSPavel Emelyanov 
87d397b968SToshiaki Makita struct veth_q_stat_desc {
88d397b968SToshiaki Makita 	char	desc[ETH_GSTRING_LEN];
89d397b968SToshiaki Makita 	size_t	offset;
90d397b968SToshiaki Makita };
91d397b968SToshiaki Makita 
9265780c56SLorenzo Bianconi #define VETH_RQ_STAT(m)	offsetof(struct veth_stats, m)
93d397b968SToshiaki Makita 
94d397b968SToshiaki Makita static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
95d397b968SToshiaki Makita 	{ "xdp_packets",	VETH_RQ_STAT(xdp_packets) },
96d397b968SToshiaki Makita 	{ "xdp_bytes",		VETH_RQ_STAT(xdp_bytes) },
975fe6e567SLorenzo Bianconi 	{ "drops",		VETH_RQ_STAT(rx_drops) },
985fe6e567SLorenzo Bianconi 	{ "xdp_redirect",	VETH_RQ_STAT(xdp_redirect) },
995fe6e567SLorenzo Bianconi 	{ "xdp_drops",		VETH_RQ_STAT(xdp_drops) },
1005fe6e567SLorenzo Bianconi 	{ "xdp_tx",		VETH_RQ_STAT(xdp_tx) },
1015fe6e567SLorenzo Bianconi 	{ "xdp_tx_errors",	VETH_RQ_STAT(xdp_tx_err) },
102d397b968SToshiaki Makita };
103d397b968SToshiaki Makita 
104d397b968SToshiaki Makita #define VETH_RQ_STATS_LEN	ARRAY_SIZE(veth_rq_stats_desc)
105d397b968SToshiaki Makita 
1065fe6e567SLorenzo Bianconi static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
1075fe6e567SLorenzo Bianconi 	{ "xdp_xmit",		VETH_RQ_STAT(peer_tq_xdp_xmit) },
1085fe6e567SLorenzo Bianconi 	{ "xdp_xmit_errors",	VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
1095fe6e567SLorenzo Bianconi };
1105fe6e567SLorenzo Bianconi 
1115fe6e567SLorenzo Bianconi #define VETH_TQ_STATS_LEN	ARRAY_SIZE(veth_tq_stats_desc)
1125fe6e567SLorenzo Bianconi 
113e314dbdcSPavel Emelyanov static struct {
114e314dbdcSPavel Emelyanov 	const char string[ETH_GSTRING_LEN];
115e314dbdcSPavel Emelyanov } ethtool_stats_keys[] = {
116e314dbdcSPavel Emelyanov 	{ "peer_ifindex" },
117e314dbdcSPavel Emelyanov };
118e314dbdcSPavel Emelyanov 
11956607b98SPhilippe Reynes static int veth_get_link_ksettings(struct net_device *dev,
12056607b98SPhilippe Reynes 				   struct ethtool_link_ksettings *cmd)
121e314dbdcSPavel Emelyanov {
12256607b98SPhilippe Reynes 	cmd->base.speed		= SPEED_10000;
12356607b98SPhilippe Reynes 	cmd->base.duplex	= DUPLEX_FULL;
12456607b98SPhilippe Reynes 	cmd->base.port		= PORT_TP;
12556607b98SPhilippe Reynes 	cmd->base.autoneg	= AUTONEG_DISABLE;
126e314dbdcSPavel Emelyanov 	return 0;
127e314dbdcSPavel Emelyanov }
128e314dbdcSPavel Emelyanov 
129e314dbdcSPavel Emelyanov static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
130e314dbdcSPavel Emelyanov {
13133a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
13233a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
133e314dbdcSPavel Emelyanov }
134e314dbdcSPavel Emelyanov 
135e314dbdcSPavel Emelyanov static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
136e314dbdcSPavel Emelyanov {
137a0341b73STonghao Zhang 	u8 *p = buf;
138d397b968SToshiaki Makita 	int i, j;
139d397b968SToshiaki Makita 
140e314dbdcSPavel Emelyanov 	switch(stringset) {
141e314dbdcSPavel Emelyanov 	case ETH_SS_STATS:
142d397b968SToshiaki Makita 		memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
143d397b968SToshiaki Makita 		p += sizeof(ethtool_stats_keys);
144a0341b73STonghao Zhang 		for (i = 0; i < dev->real_num_rx_queues; i++)
145a0341b73STonghao Zhang 			for (j = 0; j < VETH_RQ_STATS_LEN; j++)
146a0341b73STonghao Zhang 				ethtool_sprintf(&p, "rx_queue_%u_%.18s",
147d397b968SToshiaki Makita 						i, veth_rq_stats_desc[j].desc);
148a0341b73STonghao Zhang 
149a0341b73STonghao Zhang 		for (i = 0; i < dev->real_num_tx_queues; i++)
150a0341b73STonghao Zhang 			for (j = 0; j < VETH_TQ_STATS_LEN; j++)
151a0341b73STonghao Zhang 				ethtool_sprintf(&p, "tx_queue_%u_%.18s",
1525fe6e567SLorenzo Bianconi 						i, veth_tq_stats_desc[j].desc);
153e314dbdcSPavel Emelyanov 		break;
154e314dbdcSPavel Emelyanov 	}
155e314dbdcSPavel Emelyanov }
156e314dbdcSPavel Emelyanov 
157b9f2c044SJeff Garzik static int veth_get_sset_count(struct net_device *dev, int sset)
158e314dbdcSPavel Emelyanov {
159b9f2c044SJeff Garzik 	switch (sset) {
160b9f2c044SJeff Garzik 	case ETH_SS_STATS:
161d397b968SToshiaki Makita 		return ARRAY_SIZE(ethtool_stats_keys) +
1625fe6e567SLorenzo Bianconi 		       VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
1635fe6e567SLorenzo Bianconi 		       VETH_TQ_STATS_LEN * dev->real_num_tx_queues;
164b9f2c044SJeff Garzik 	default:
165b9f2c044SJeff Garzik 		return -EOPNOTSUPP;
166b9f2c044SJeff Garzik 	}
167e314dbdcSPavel Emelyanov }
168e314dbdcSPavel Emelyanov 
169e314dbdcSPavel Emelyanov static void veth_get_ethtool_stats(struct net_device *dev,
170e314dbdcSPavel Emelyanov 		struct ethtool_stats *stats, u64 *data)
171e314dbdcSPavel Emelyanov {
1725fe6e567SLorenzo Bianconi 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
173d0e2c55eSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
174d397b968SToshiaki Makita 	int i, j, idx;
175e314dbdcSPavel Emelyanov 
176d0e2c55eSEric Dumazet 	data[0] = peer ? peer->ifindex : 0;
177d397b968SToshiaki Makita 	idx = 1;
178d397b968SToshiaki Makita 	for (i = 0; i < dev->real_num_rx_queues; i++) {
179d397b968SToshiaki Makita 		const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
18065780c56SLorenzo Bianconi 		const void *stats_base = (void *)&rq_stats->vs;
181d397b968SToshiaki Makita 		unsigned int start;
182d397b968SToshiaki Makita 		size_t offset;
183d397b968SToshiaki Makita 
184d397b968SToshiaki Makita 		do {
185d397b968SToshiaki Makita 			start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
186d397b968SToshiaki Makita 			for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
187d397b968SToshiaki Makita 				offset = veth_rq_stats_desc[j].offset;
188d397b968SToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
189d397b968SToshiaki Makita 			}
190d397b968SToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
191d397b968SToshiaki Makita 		idx += VETH_RQ_STATS_LEN;
192d397b968SToshiaki Makita 	}
1935fe6e567SLorenzo Bianconi 
1945fe6e567SLorenzo Bianconi 	if (!peer)
1955fe6e567SLorenzo Bianconi 		return;
1965fe6e567SLorenzo Bianconi 
1975fe6e567SLorenzo Bianconi 	rcv_priv = netdev_priv(peer);
1985fe6e567SLorenzo Bianconi 	for (i = 0; i < peer->real_num_rx_queues; i++) {
1995fe6e567SLorenzo Bianconi 		const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
2005fe6e567SLorenzo Bianconi 		const void *base = (void *)&rq_stats->vs;
2015fe6e567SLorenzo Bianconi 		unsigned int start, tx_idx = idx;
2025fe6e567SLorenzo Bianconi 		size_t offset;
2035fe6e567SLorenzo Bianconi 
2045fe6e567SLorenzo Bianconi 		tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
2055fe6e567SLorenzo Bianconi 		do {
2065fe6e567SLorenzo Bianconi 			start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
2075fe6e567SLorenzo Bianconi 			for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
2085fe6e567SLorenzo Bianconi 				offset = veth_tq_stats_desc[j].offset;
2095fe6e567SLorenzo Bianconi 				data[tx_idx + j] += *(u64 *)(base + offset);
2105fe6e567SLorenzo Bianconi 			}
2115fe6e567SLorenzo Bianconi 		} while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
2125fe6e567SLorenzo Bianconi 	}
213e314dbdcSPavel Emelyanov }
214e314dbdcSPavel Emelyanov 
21534829eecSMaciej Fijalkowski static void veth_get_channels(struct net_device *dev,
21634829eecSMaciej Fijalkowski 			      struct ethtool_channels *channels)
21734829eecSMaciej Fijalkowski {
21834829eecSMaciej Fijalkowski 	channels->tx_count = dev->real_num_tx_queues;
21934829eecSMaciej Fijalkowski 	channels->rx_count = dev->real_num_rx_queues;
2204752eeb3SPaolo Abeni 	channels->max_tx = dev->num_tx_queues;
2214752eeb3SPaolo Abeni 	channels->max_rx = dev->num_rx_queues;
22234829eecSMaciej Fijalkowski }
22334829eecSMaciej Fijalkowski 
2244752eeb3SPaolo Abeni static int veth_set_channels(struct net_device *dev,
2254752eeb3SPaolo Abeni 			     struct ethtool_channels *ch);
2264752eeb3SPaolo Abeni 
2270fc0b732SStephen Hemminger static const struct ethtool_ops veth_ethtool_ops = {
228e314dbdcSPavel Emelyanov 	.get_drvinfo		= veth_get_drvinfo,
229e314dbdcSPavel Emelyanov 	.get_link		= ethtool_op_get_link,
230e314dbdcSPavel Emelyanov 	.get_strings		= veth_get_strings,
231b9f2c044SJeff Garzik 	.get_sset_count		= veth_get_sset_count,
232e314dbdcSPavel Emelyanov 	.get_ethtool_stats	= veth_get_ethtool_stats,
23356607b98SPhilippe Reynes 	.get_link_ksettings	= veth_get_link_ksettings,
234056b21fbSJulian Wiedmann 	.get_ts_info		= ethtool_op_get_ts_info,
23534829eecSMaciej Fijalkowski 	.get_channels		= veth_get_channels,
2364752eeb3SPaolo Abeni 	.set_channels		= veth_set_channels,
237e314dbdcSPavel Emelyanov };
238e314dbdcSPavel Emelyanov 
239948d4f21SToshiaki Makita /* general routines */
240948d4f21SToshiaki Makita 
2419fc8d518SToshiaki Makita static bool veth_is_xdp_frame(void *ptr)
2429fc8d518SToshiaki Makita {
2439fc8d518SToshiaki Makita 	return (unsigned long)ptr & VETH_XDP_FLAG;
2449fc8d518SToshiaki Makita }
2459fc8d518SToshiaki Makita 
246defcffebSMaciej Żenczykowski static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
2479fc8d518SToshiaki Makita {
2489fc8d518SToshiaki Makita 	return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
2499fc8d518SToshiaki Makita }
2509fc8d518SToshiaki Makita 
251defcffebSMaciej Żenczykowski static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
252af87a3aaSToshiaki Makita {
253defcffebSMaciej Żenczykowski 	return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
254af87a3aaSToshiaki Makita }
255af87a3aaSToshiaki Makita 
2569fc8d518SToshiaki Makita static void veth_ptr_free(void *ptr)
2579fc8d518SToshiaki Makita {
2589fc8d518SToshiaki Makita 	if (veth_is_xdp_frame(ptr))
2599fc8d518SToshiaki Makita 		xdp_return_frame(veth_ptr_to_xdp(ptr));
2609fc8d518SToshiaki Makita 	else
2619fc8d518SToshiaki Makita 		kfree_skb(ptr);
2629fc8d518SToshiaki Makita }
2639fc8d518SToshiaki Makita 
264638264dcSToshiaki Makita static void __veth_xdp_flush(struct veth_rq *rq)
265948d4f21SToshiaki Makita {
266948d4f21SToshiaki Makita 	/* Write ptr_ring before reading rx_notify_masked */
267948d4f21SToshiaki Makita 	smp_mb();
26868468d8cSEric Dumazet 	if (!READ_ONCE(rq->rx_notify_masked) &&
26968468d8cSEric Dumazet 	    napi_schedule_prep(&rq->xdp_napi)) {
27068468d8cSEric Dumazet 		WRITE_ONCE(rq->rx_notify_masked, true);
27168468d8cSEric Dumazet 		__napi_schedule(&rq->xdp_napi);
272948d4f21SToshiaki Makita 	}
273948d4f21SToshiaki Makita }
274948d4f21SToshiaki Makita 
275638264dcSToshiaki Makita static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
276948d4f21SToshiaki Makita {
277638264dcSToshiaki Makita 	if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
278948d4f21SToshiaki Makita 		dev_kfree_skb_any(skb);
279948d4f21SToshiaki Makita 		return NET_RX_DROP;
280948d4f21SToshiaki Makita 	}
281948d4f21SToshiaki Makita 
282948d4f21SToshiaki Makita 	return NET_RX_SUCCESS;
283948d4f21SToshiaki Makita }
284948d4f21SToshiaki Makita 
285638264dcSToshiaki Makita static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
286638264dcSToshiaki Makita 			    struct veth_rq *rq, bool xdp)
287e314dbdcSPavel Emelyanov {
288948d4f21SToshiaki Makita 	return __dev_forward_skb(dev, skb) ?: xdp ?
289638264dcSToshiaki Makita 		veth_xdp_rx(rq, skb) :
290baebdf48SSebastian Andrzej Siewior 		__netif_rx(skb);
291948d4f21SToshiaki Makita }
292948d4f21SToshiaki Makita 
29347e550e0SPaolo Abeni /* return true if the specified skb has chances of GRO aggregation
29447e550e0SPaolo Abeni  * Don't strive for accuracy, but try to avoid GRO overhead in the most
29547e550e0SPaolo Abeni  * common scenarios.
29647e550e0SPaolo Abeni  * When XDP is enabled, all traffic is considered eligible, as the xmit
29747e550e0SPaolo Abeni  * device has TSO off.
29847e550e0SPaolo Abeni  * When TSO is enabled on the xmit device, we are likely interested only
29947e550e0SPaolo Abeni  * in UDP aggregation, explicitly check for that if the skb is suspected
30047e550e0SPaolo Abeni  * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
30147e550e0SPaolo Abeni  * to belong to locally generated UDP traffic.
30247e550e0SPaolo Abeni  */
30347e550e0SPaolo Abeni static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
30447e550e0SPaolo Abeni 					 const struct net_device *rcv,
30547e550e0SPaolo Abeni 					 const struct sk_buff *skb)
30647e550e0SPaolo Abeni {
30747e550e0SPaolo Abeni 	return !(dev->features & NETIF_F_ALL_TSO) ||
30847e550e0SPaolo Abeni 		(skb->destructor == sock_wfree &&
30947e550e0SPaolo Abeni 		 rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
31047e550e0SPaolo Abeni }
31147e550e0SPaolo Abeni 
312948d4f21SToshiaki Makita static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
313948d4f21SToshiaki Makita {
314948d4f21SToshiaki Makita 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
315638264dcSToshiaki Makita 	struct veth_rq *rq = NULL;
316d0e2c55eSEric Dumazet 	struct net_device *rcv;
3172681128fSEric Dumazet 	int length = skb->len;
318d3256efdSPaolo Abeni 	bool use_napi = false;
319638264dcSToshiaki Makita 	int rxq;
320e314dbdcSPavel Emelyanov 
321d0e2c55eSEric Dumazet 	rcu_read_lock();
322d0e2c55eSEric Dumazet 	rcv = rcu_dereference(priv->peer);
323d0e2c55eSEric Dumazet 	if (unlikely(!rcv)) {
324d0e2c55eSEric Dumazet 		kfree_skb(skb);
325d0e2c55eSEric Dumazet 		goto drop;
326d0e2c55eSEric Dumazet 	}
327e314dbdcSPavel Emelyanov 
328948d4f21SToshiaki Makita 	rcv_priv = netdev_priv(rcv);
329638264dcSToshiaki Makita 	rxq = skb_get_queue_mapping(skb);
330638264dcSToshiaki Makita 	if (rxq < rcv->real_num_rx_queues) {
331638264dcSToshiaki Makita 		rq = &rcv_priv->rq[rxq];
332d3256efdSPaolo Abeni 
333d3256efdSPaolo Abeni 		/* The napi pointer is available when an XDP program is
334d3256efdSPaolo Abeni 		 * attached or when GRO is enabled
33547e550e0SPaolo Abeni 		 * Don't bother with napi/GRO if the skb can't be aggregated
336d3256efdSPaolo Abeni 		 */
33747e550e0SPaolo Abeni 		use_napi = rcu_access_pointer(rq->napi) &&
33847e550e0SPaolo Abeni 			   veth_skb_is_eligible_for_gro(dev, rcv, skb);
339638264dcSToshiaki Makita 	}
340948d4f21SToshiaki Makita 
341aa4e689eSMichael Walle 	skb_tx_timestamp(skb);
342d3256efdSPaolo Abeni 	if (likely(veth_forward_skb(rcv, skb, rq, use_napi) == NET_RX_SUCCESS)) {
343d3256efdSPaolo Abeni 		if (!use_napi)
344b4fba476SEric Dumazet 			dev_lstats_add(dev, length);
3452681128fSEric Dumazet 	} else {
346d0e2c55eSEric Dumazet drop:
3472681128fSEric Dumazet 		atomic64_inc(&priv->dropped);
3482681128fSEric Dumazet 	}
349948d4f21SToshiaki Makita 
350d3256efdSPaolo Abeni 	if (use_napi)
351638264dcSToshiaki Makita 		__veth_xdp_flush(rq);
352948d4f21SToshiaki Makita 
353d0e2c55eSEric Dumazet 	rcu_read_unlock();
354948d4f21SToshiaki Makita 
3556ed10654SPatrick McHardy 	return NETDEV_TX_OK;
356e314dbdcSPavel Emelyanov }
357e314dbdcSPavel Emelyanov 
358b4fba476SEric Dumazet static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
359e314dbdcSPavel Emelyanov {
360cf05c700SEric Dumazet 	struct veth_priv *priv = netdev_priv(dev);
36111687a10SDavid S. Miller 
362b4fba476SEric Dumazet 	dev_lstats_read(dev, packets, bytes);
3632681128fSEric Dumazet 	return atomic64_read(&priv->dropped);
3642681128fSEric Dumazet }
3652681128fSEric Dumazet 
36665780c56SLorenzo Bianconi static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
3674195e54aSToshiaki Makita {
3684195e54aSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
3694195e54aSToshiaki Makita 	int i;
3704195e54aSToshiaki Makita 
3715fe6e567SLorenzo Bianconi 	result->peer_tq_xdp_xmit_err = 0;
3724195e54aSToshiaki Makita 	result->xdp_packets = 0;
373d99a7c2fSLorenzo Bianconi 	result->xdp_tx_err = 0;
3744195e54aSToshiaki Makita 	result->xdp_bytes = 0;
37566fe4a07SLorenzo Bianconi 	result->rx_drops = 0;
3764195e54aSToshiaki Makita 	for (i = 0; i < dev->num_rx_queues; i++) {
3775fe6e567SLorenzo Bianconi 		u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
3784195e54aSToshiaki Makita 		struct veth_rq_stats *stats = &priv->rq[i].stats;
3794195e54aSToshiaki Makita 		unsigned int start;
3804195e54aSToshiaki Makita 
3814195e54aSToshiaki Makita 		do {
3824195e54aSToshiaki Makita 			start = u64_stats_fetch_begin_irq(&stats->syncp);
3835fe6e567SLorenzo Bianconi 			peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
384d99a7c2fSLorenzo Bianconi 			xdp_tx_err = stats->vs.xdp_tx_err;
38565780c56SLorenzo Bianconi 			packets = stats->vs.xdp_packets;
38665780c56SLorenzo Bianconi 			bytes = stats->vs.xdp_bytes;
38766fe4a07SLorenzo Bianconi 			drops = stats->vs.rx_drops;
3884195e54aSToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
3895fe6e567SLorenzo Bianconi 		result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
390d99a7c2fSLorenzo Bianconi 		result->xdp_tx_err += xdp_tx_err;
3914195e54aSToshiaki Makita 		result->xdp_packets += packets;
3924195e54aSToshiaki Makita 		result->xdp_bytes += bytes;
39366fe4a07SLorenzo Bianconi 		result->rx_drops += drops;
3944195e54aSToshiaki Makita 	}
3954195e54aSToshiaki Makita }
3964195e54aSToshiaki Makita 
397bc1f4470Sstephen hemminger static void veth_get_stats64(struct net_device *dev,
3982681128fSEric Dumazet 			     struct rtnl_link_stats64 *tot)
3992681128fSEric Dumazet {
4002681128fSEric Dumazet 	struct veth_priv *priv = netdev_priv(dev);
401d0e2c55eSEric Dumazet 	struct net_device *peer;
40265780c56SLorenzo Bianconi 	struct veth_stats rx;
403b4fba476SEric Dumazet 	u64 packets, bytes;
4042681128fSEric Dumazet 
405b4fba476SEric Dumazet 	tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
406b4fba476SEric Dumazet 	tot->tx_bytes = bytes;
407b4fba476SEric Dumazet 	tot->tx_packets = packets;
4084195e54aSToshiaki Makita 
4094195e54aSToshiaki Makita 	veth_stats_rx(&rx, dev);
4105fe6e567SLorenzo Bianconi 	tot->tx_dropped += rx.xdp_tx_err;
4115fe6e567SLorenzo Bianconi 	tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
4124195e54aSToshiaki Makita 	tot->rx_bytes = rx.xdp_bytes;
4134195e54aSToshiaki Makita 	tot->rx_packets = rx.xdp_packets;
4142681128fSEric Dumazet 
415d0e2c55eSEric Dumazet 	rcu_read_lock();
416d0e2c55eSEric Dumazet 	peer = rcu_dereference(priv->peer);
417d0e2c55eSEric Dumazet 	if (peer) {
418e25d5dbcSJiang Lidong 		veth_stats_tx(peer, &packets, &bytes);
419b4fba476SEric Dumazet 		tot->rx_bytes += bytes;
420b4fba476SEric Dumazet 		tot->rx_packets += packets;
4214195e54aSToshiaki Makita 
4224195e54aSToshiaki Makita 		veth_stats_rx(&rx, peer);
4235fe6e567SLorenzo Bianconi 		tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
4245fe6e567SLorenzo Bianconi 		tot->rx_dropped += rx.xdp_tx_err;
4254195e54aSToshiaki Makita 		tot->tx_bytes += rx.xdp_bytes;
4264195e54aSToshiaki Makita 		tot->tx_packets += rx.xdp_packets;
427d0e2c55eSEric Dumazet 	}
428d0e2c55eSEric Dumazet 	rcu_read_unlock();
429e314dbdcSPavel Emelyanov }
430e314dbdcSPavel Emelyanov 
4315c70ef85SGao feng /* fake multicast ability */
4325c70ef85SGao feng static void veth_set_multicast_list(struct net_device *dev)
4335c70ef85SGao feng {
4345c70ef85SGao feng }
4355c70ef85SGao feng 
436638264dcSToshiaki Makita static int veth_select_rxq(struct net_device *dev)
437638264dcSToshiaki Makita {
438638264dcSToshiaki Makita 	return smp_processor_id() % dev->real_num_rx_queues;
439638264dcSToshiaki Makita }
440638264dcSToshiaki Makita 
4419aa1206eSDaniel Borkmann static struct net_device *veth_peer_dev(struct net_device *dev)
4429aa1206eSDaniel Borkmann {
4439aa1206eSDaniel Borkmann 	struct veth_priv *priv = netdev_priv(dev);
4449aa1206eSDaniel Borkmann 
4459aa1206eSDaniel Borkmann 	/* Callers must be under RCU read side. */
4469aa1206eSDaniel Borkmann 	return rcu_dereference(priv->peer);
4479aa1206eSDaniel Borkmann }
4489aa1206eSDaniel Borkmann 
449af87a3aaSToshiaki Makita static int veth_xdp_xmit(struct net_device *dev, int n,
4509152cff0SLorenzo Bianconi 			 struct xdp_frame **frames,
4519152cff0SLorenzo Bianconi 			 u32 flags, bool ndo_xmit)
452af87a3aaSToshiaki Makita {
453af87a3aaSToshiaki Makita 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
454fdc13979SLorenzo Bianconi 	int i, ret = -ENXIO, nxmit = 0;
455af87a3aaSToshiaki Makita 	struct net_device *rcv;
4565fe6e567SLorenzo Bianconi 	unsigned int max_len;
457638264dcSToshiaki Makita 	struct veth_rq *rq;
458af87a3aaSToshiaki Makita 
4595fe6e567SLorenzo Bianconi 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
460d99a7c2fSLorenzo Bianconi 		return -EINVAL;
461af87a3aaSToshiaki Makita 
4625fe6e567SLorenzo Bianconi 	rcu_read_lock();
463af87a3aaSToshiaki Makita 	rcv = rcu_dereference(priv->peer);
4645fe6e567SLorenzo Bianconi 	if (unlikely(!rcv))
4655fe6e567SLorenzo Bianconi 		goto out;
466af87a3aaSToshiaki Makita 
467af87a3aaSToshiaki Makita 	rcv_priv = netdev_priv(rcv);
4685fe6e567SLorenzo Bianconi 	rq = &rcv_priv->rq[veth_select_rxq(rcv)];
4690e672f30SToke Høiland-Jørgensen 	/* The napi pointer is set if NAPI is enabled, which ensures that
4700e672f30SToke Høiland-Jørgensen 	 * xdp_ring is initialized on receive side and the peer device is up.
471af87a3aaSToshiaki Makita 	 */
4720e672f30SToke Høiland-Jørgensen 	if (!rcu_access_pointer(rq->napi))
4735fe6e567SLorenzo Bianconi 		goto out;
474af87a3aaSToshiaki Makita 
475af87a3aaSToshiaki Makita 	max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
476af87a3aaSToshiaki Makita 
477638264dcSToshiaki Makita 	spin_lock(&rq->xdp_ring.producer_lock);
478af87a3aaSToshiaki Makita 	for (i = 0; i < n; i++) {
479af87a3aaSToshiaki Makita 		struct xdp_frame *frame = frames[i];
480af87a3aaSToshiaki Makita 		void *ptr = veth_xdp_to_ptr(frame);
481af87a3aaSToshiaki Makita 
4825142239aSLorenzo Bianconi 		if (unlikely(xdp_get_frame_len(frame) > max_len ||
483fdc13979SLorenzo Bianconi 			     __ptr_ring_produce(&rq->xdp_ring, ptr)))
484fdc13979SLorenzo Bianconi 			break;
485fdc13979SLorenzo Bianconi 		nxmit++;
486af87a3aaSToshiaki Makita 	}
487638264dcSToshiaki Makita 	spin_unlock(&rq->xdp_ring.producer_lock);
488af87a3aaSToshiaki Makita 
489af87a3aaSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH)
490638264dcSToshiaki Makita 		__veth_xdp_flush(rq);
491af87a3aaSToshiaki Makita 
492fdc13979SLorenzo Bianconi 	ret = nxmit;
4939152cff0SLorenzo Bianconi 	if (ndo_xmit) {
4945fe6e567SLorenzo Bianconi 		u64_stats_update_begin(&rq->stats.syncp);
495fdc13979SLorenzo Bianconi 		rq->stats.vs.peer_tq_xdp_xmit += nxmit;
496fdc13979SLorenzo Bianconi 		rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
4979152cff0SLorenzo Bianconi 		u64_stats_update_end(&rq->stats.syncp);
4985fe6e567SLorenzo Bianconi 	}
4999152cff0SLorenzo Bianconi 
5005fe6e567SLorenzo Bianconi out:
501b23bfa56SJohn Fastabend 	rcu_read_unlock();
5022131479dSToshiaki Makita 
5032131479dSToshiaki Makita 	return ret;
504af87a3aaSToshiaki Makita }
505af87a3aaSToshiaki Makita 
5069152cff0SLorenzo Bianconi static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
5079152cff0SLorenzo Bianconi 			     struct xdp_frame **frames, u32 flags)
5089152cff0SLorenzo Bianconi {
5095fe6e567SLorenzo Bianconi 	int err;
5105fe6e567SLorenzo Bianconi 
5115fe6e567SLorenzo Bianconi 	err = veth_xdp_xmit(dev, n, frames, flags, true);
5125fe6e567SLorenzo Bianconi 	if (err < 0) {
5135fe6e567SLorenzo Bianconi 		struct veth_priv *priv = netdev_priv(dev);
5145fe6e567SLorenzo Bianconi 
5155fe6e567SLorenzo Bianconi 		atomic64_add(n, &priv->dropped);
5165fe6e567SLorenzo Bianconi 	}
5175fe6e567SLorenzo Bianconi 
5185fe6e567SLorenzo Bianconi 	return err;
5199152cff0SLorenzo Bianconi }
5209152cff0SLorenzo Bianconi 
521bd32aa1fSLorenzo Bianconi static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
5229cda7807SToshiaki Makita {
523fdc13979SLorenzo Bianconi 	int sent, i, err = 0, drops;
5249cda7807SToshiaki Makita 
525bd32aa1fSLorenzo Bianconi 	sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
5269cda7807SToshiaki Makita 	if (sent < 0) {
5279cda7807SToshiaki Makita 		err = sent;
5289cda7807SToshiaki Makita 		sent = 0;
5299cda7807SToshiaki Makita 	}
530fdc13979SLorenzo Bianconi 
531fdc13979SLorenzo Bianconi 	for (i = sent; unlikely(i < bq->count); i++)
532fdc13979SLorenzo Bianconi 		xdp_return_frame(bq->q[i]);
533fdc13979SLorenzo Bianconi 
534fdc13979SLorenzo Bianconi 	drops = bq->count - sent;
535fdc13979SLorenzo Bianconi 	trace_xdp_bulk_tx(rq->dev, sent, drops, err);
5369cda7807SToshiaki Makita 
5375fe6e567SLorenzo Bianconi 	u64_stats_update_begin(&rq->stats.syncp);
5385fe6e567SLorenzo Bianconi 	rq->stats.vs.xdp_tx += sent;
539fdc13979SLorenzo Bianconi 	rq->stats.vs.xdp_tx_err += drops;
5405fe6e567SLorenzo Bianconi 	u64_stats_update_end(&rq->stats.syncp);
5415fe6e567SLorenzo Bianconi 
5429cda7807SToshiaki Makita 	bq->count = 0;
5439cda7807SToshiaki Makita }
5449cda7807SToshiaki Makita 
545bd32aa1fSLorenzo Bianconi static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
546d1396004SToshiaki Makita {
547bd32aa1fSLorenzo Bianconi 	struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
548d1396004SToshiaki Makita 	struct net_device *rcv;
549bd32aa1fSLorenzo Bianconi 	struct veth_rq *rcv_rq;
550d1396004SToshiaki Makita 
551d1396004SToshiaki Makita 	rcu_read_lock();
552bd32aa1fSLorenzo Bianconi 	veth_xdp_flush_bq(rq, bq);
553d1396004SToshiaki Makita 	rcv = rcu_dereference(priv->peer);
554d1396004SToshiaki Makita 	if (unlikely(!rcv))
555d1396004SToshiaki Makita 		goto out;
556d1396004SToshiaki Makita 
557d1396004SToshiaki Makita 	rcv_priv = netdev_priv(rcv);
558bd32aa1fSLorenzo Bianconi 	rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
559d1396004SToshiaki Makita 	/* xdp_ring is initialized on receive side? */
560bd32aa1fSLorenzo Bianconi 	if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
561d1396004SToshiaki Makita 		goto out;
562d1396004SToshiaki Makita 
563bd32aa1fSLorenzo Bianconi 	__veth_xdp_flush(rcv_rq);
564d1396004SToshiaki Makita out:
565d1396004SToshiaki Makita 	rcu_read_unlock();
566d1396004SToshiaki Makita }
567d1396004SToshiaki Makita 
568bd32aa1fSLorenzo Bianconi static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
5699cda7807SToshiaki Makita 		       struct veth_xdp_tx_bq *bq)
570d1396004SToshiaki Makita {
5711b698fa5SLorenzo Bianconi 	struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
572d1396004SToshiaki Makita 
573d1396004SToshiaki Makita 	if (unlikely(!frame))
574d1396004SToshiaki Makita 		return -EOVERFLOW;
575d1396004SToshiaki Makita 
5769cda7807SToshiaki Makita 	if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
577bd32aa1fSLorenzo Bianconi 		veth_xdp_flush_bq(rq, bq);
5789cda7807SToshiaki Makita 
5799cda7807SToshiaki Makita 	bq->q[bq->count++] = frame;
5809cda7807SToshiaki Makita 
5819cda7807SToshiaki Makita 	return 0;
582d1396004SToshiaki Makita }
583d1396004SToshiaki Makita 
58465e6dcf7SLorenzo Bianconi static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
585d1396004SToshiaki Makita 					  struct xdp_frame *frame,
5861c5b82e5SLorenzo Bianconi 					  struct veth_xdp_tx_bq *bq,
5871c5b82e5SLorenzo Bianconi 					  struct veth_stats *stats)
5889fc8d518SToshiaki Makita {
589d1396004SToshiaki Makita 	struct xdp_frame orig_frame;
5909fc8d518SToshiaki Makita 	struct bpf_prog *xdp_prog;
5919fc8d518SToshiaki Makita 
5929fc8d518SToshiaki Makita 	rcu_read_lock();
593638264dcSToshiaki Makita 	xdp_prog = rcu_dereference(rq->xdp_prog);
5949fc8d518SToshiaki Makita 	if (likely(xdp_prog)) {
5959fc8d518SToshiaki Makita 		struct xdp_buff xdp;
5969fc8d518SToshiaki Makita 		u32 act;
5979fc8d518SToshiaki Makita 
598fc379872SLorenzo Bianconi 		xdp_convert_frame_to_buff(frame, &xdp);
599638264dcSToshiaki Makita 		xdp.rxq = &rq->xdp_rxq;
6009fc8d518SToshiaki Makita 
6019fc8d518SToshiaki Makita 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
6029fc8d518SToshiaki Makita 
6039fc8d518SToshiaki Makita 		switch (act) {
6049fc8d518SToshiaki Makita 		case XDP_PASS:
60589f479f0SLorenzo Bianconi 			if (xdp_update_frame_from_buff(&xdp, frame))
60689f479f0SLorenzo Bianconi 				goto err_xdp;
6079fc8d518SToshiaki Makita 			break;
608d1396004SToshiaki Makita 		case XDP_TX:
609d1396004SToshiaki Makita 			orig_frame = *frame;
610d1396004SToshiaki Makita 			xdp.rxq->mem = frame->mem;
611bd32aa1fSLorenzo Bianconi 			if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
612638264dcSToshiaki Makita 				trace_xdp_exception(rq->dev, xdp_prog, act);
613d1396004SToshiaki Makita 				frame = &orig_frame;
6141c5b82e5SLorenzo Bianconi 				stats->rx_drops++;
615d1396004SToshiaki Makita 				goto err_xdp;
616d1396004SToshiaki Makita 			}
6171c5b82e5SLorenzo Bianconi 			stats->xdp_tx++;
618d1396004SToshiaki Makita 			rcu_read_unlock();
619d1396004SToshiaki Makita 			goto xdp_xmit;
620d1396004SToshiaki Makita 		case XDP_REDIRECT:
621d1396004SToshiaki Makita 			orig_frame = *frame;
622d1396004SToshiaki Makita 			xdp.rxq->mem = frame->mem;
623638264dcSToshiaki Makita 			if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
624d1396004SToshiaki Makita 				frame = &orig_frame;
6251c5b82e5SLorenzo Bianconi 				stats->rx_drops++;
626d1396004SToshiaki Makita 				goto err_xdp;
627d1396004SToshiaki Makita 			}
6281c5b82e5SLorenzo Bianconi 			stats->xdp_redirect++;
629d1396004SToshiaki Makita 			rcu_read_unlock();
630d1396004SToshiaki Makita 			goto xdp_xmit;
6319fc8d518SToshiaki Makita 		default:
632c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
633df561f66SGustavo A. R. Silva 			fallthrough;
6349fc8d518SToshiaki Makita 		case XDP_ABORTED:
635638264dcSToshiaki Makita 			trace_xdp_exception(rq->dev, xdp_prog, act);
636df561f66SGustavo A. R. Silva 			fallthrough;
6379fc8d518SToshiaki Makita 		case XDP_DROP:
6381c5b82e5SLorenzo Bianconi 			stats->xdp_drops++;
6399fc8d518SToshiaki Makita 			goto err_xdp;
6409fc8d518SToshiaki Makita 		}
6419fc8d518SToshiaki Makita 	}
6429fc8d518SToshiaki Makita 	rcu_read_unlock();
6439fc8d518SToshiaki Makita 
64465e6dcf7SLorenzo Bianconi 	return frame;
6459fc8d518SToshiaki Makita err_xdp:
6469fc8d518SToshiaki Makita 	rcu_read_unlock();
6479fc8d518SToshiaki Makita 	xdp_return_frame(frame);
648d1396004SToshiaki Makita xdp_xmit:
6499fc8d518SToshiaki Makita 	return NULL;
6509fc8d518SToshiaki Makita }
6519fc8d518SToshiaki Makita 
65265e6dcf7SLorenzo Bianconi /* frames array contains VETH_XDP_BATCH at most */
65365e6dcf7SLorenzo Bianconi static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
65465e6dcf7SLorenzo Bianconi 				  int n_xdpf, struct veth_xdp_tx_bq *bq,
65565e6dcf7SLorenzo Bianconi 				  struct veth_stats *stats)
65665e6dcf7SLorenzo Bianconi {
65765e6dcf7SLorenzo Bianconi 	void *skbs[VETH_XDP_BATCH];
65865e6dcf7SLorenzo Bianconi 	int i;
65965e6dcf7SLorenzo Bianconi 
66065e6dcf7SLorenzo Bianconi 	if (xdp_alloc_skb_bulk(skbs, n_xdpf,
66165e6dcf7SLorenzo Bianconi 			       GFP_ATOMIC | __GFP_ZERO) < 0) {
66265e6dcf7SLorenzo Bianconi 		for (i = 0; i < n_xdpf; i++)
66365e6dcf7SLorenzo Bianconi 			xdp_return_frame(frames[i]);
66465e6dcf7SLorenzo Bianconi 		stats->rx_drops += n_xdpf;
66565e6dcf7SLorenzo Bianconi 
66665e6dcf7SLorenzo Bianconi 		return;
66765e6dcf7SLorenzo Bianconi 	}
66865e6dcf7SLorenzo Bianconi 
66965e6dcf7SLorenzo Bianconi 	for (i = 0; i < n_xdpf; i++) {
67065e6dcf7SLorenzo Bianconi 		struct sk_buff *skb = skbs[i];
67165e6dcf7SLorenzo Bianconi 
67265e6dcf7SLorenzo Bianconi 		skb = __xdp_build_skb_from_frame(frames[i], skb,
67365e6dcf7SLorenzo Bianconi 						 rq->dev);
67465e6dcf7SLorenzo Bianconi 		if (!skb) {
67565e6dcf7SLorenzo Bianconi 			xdp_return_frame(frames[i]);
67665e6dcf7SLorenzo Bianconi 			stats->rx_drops++;
67765e6dcf7SLorenzo Bianconi 			continue;
67865e6dcf7SLorenzo Bianconi 		}
67965e6dcf7SLorenzo Bianconi 		napi_gro_receive(&rq->xdp_napi, skb);
68065e6dcf7SLorenzo Bianconi 	}
68165e6dcf7SLorenzo Bianconi }
68265e6dcf7SLorenzo Bianconi 
683*718a18a0SLorenzo Bianconi static void veth_xdp_get(struct xdp_buff *xdp)
684*718a18a0SLorenzo Bianconi {
685*718a18a0SLorenzo Bianconi 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
686*718a18a0SLorenzo Bianconi 	int i;
687*718a18a0SLorenzo Bianconi 
688*718a18a0SLorenzo Bianconi 	get_page(virt_to_page(xdp->data));
689*718a18a0SLorenzo Bianconi 	if (likely(!xdp_buff_has_frags(xdp)))
690*718a18a0SLorenzo Bianconi 		return;
691*718a18a0SLorenzo Bianconi 
692*718a18a0SLorenzo Bianconi 	for (i = 0; i < sinfo->nr_frags; i++)
693*718a18a0SLorenzo Bianconi 		__skb_frag_ref(&sinfo->frags[i]);
694*718a18a0SLorenzo Bianconi }
695*718a18a0SLorenzo Bianconi 
696*718a18a0SLorenzo Bianconi static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
697*718a18a0SLorenzo Bianconi 					struct xdp_buff *xdp,
698*718a18a0SLorenzo Bianconi 					struct sk_buff **pskb)
699*718a18a0SLorenzo Bianconi {
700*718a18a0SLorenzo Bianconi 	struct sk_buff *skb = *pskb;
701*718a18a0SLorenzo Bianconi 	u32 frame_sz;
702*718a18a0SLorenzo Bianconi 
703*718a18a0SLorenzo Bianconi 	if (skb_shared(skb) || skb_head_is_locked(skb) ||
704*718a18a0SLorenzo Bianconi 	    skb_shinfo(skb)->nr_frags) {
705*718a18a0SLorenzo Bianconi 		u32 size, len, max_head_size, off;
706*718a18a0SLorenzo Bianconi 		struct sk_buff *nskb;
707*718a18a0SLorenzo Bianconi 		struct page *page;
708*718a18a0SLorenzo Bianconi 		int i, head_off;
709*718a18a0SLorenzo Bianconi 
710*718a18a0SLorenzo Bianconi 		/* We need a private copy of the skb and data buffers since
711*718a18a0SLorenzo Bianconi 		 * the ebpf program can modify it. We segment the original skb
712*718a18a0SLorenzo Bianconi 		 * into order-0 pages without linearize it.
713*718a18a0SLorenzo Bianconi 		 *
714*718a18a0SLorenzo Bianconi 		 * Make sure we have enough space for linear and paged area
715*718a18a0SLorenzo Bianconi 		 */
716*718a18a0SLorenzo Bianconi 		max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE -
717*718a18a0SLorenzo Bianconi 						  VETH_XDP_HEADROOM);
718*718a18a0SLorenzo Bianconi 		if (skb->len > PAGE_SIZE * MAX_SKB_FRAGS + max_head_size)
719*718a18a0SLorenzo Bianconi 			goto drop;
720*718a18a0SLorenzo Bianconi 
721*718a18a0SLorenzo Bianconi 		/* Allocate skb head */
722*718a18a0SLorenzo Bianconi 		page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
723*718a18a0SLorenzo Bianconi 		if (!page)
724*718a18a0SLorenzo Bianconi 			goto drop;
725*718a18a0SLorenzo Bianconi 
726*718a18a0SLorenzo Bianconi 		nskb = build_skb(page_address(page), PAGE_SIZE);
727*718a18a0SLorenzo Bianconi 		if (!nskb) {
728*718a18a0SLorenzo Bianconi 			put_page(page);
729*718a18a0SLorenzo Bianconi 			goto drop;
730*718a18a0SLorenzo Bianconi 		}
731*718a18a0SLorenzo Bianconi 
732*718a18a0SLorenzo Bianconi 		skb_reserve(nskb, VETH_XDP_HEADROOM);
733*718a18a0SLorenzo Bianconi 		size = min_t(u32, skb->len, max_head_size);
734*718a18a0SLorenzo Bianconi 		if (skb_copy_bits(skb, 0, nskb->data, size)) {
735*718a18a0SLorenzo Bianconi 			consume_skb(nskb);
736*718a18a0SLorenzo Bianconi 			goto drop;
737*718a18a0SLorenzo Bianconi 		}
738*718a18a0SLorenzo Bianconi 		skb_put(nskb, size);
739*718a18a0SLorenzo Bianconi 
740*718a18a0SLorenzo Bianconi 		skb_copy_header(nskb, skb);
741*718a18a0SLorenzo Bianconi 		head_off = skb_headroom(nskb) - skb_headroom(skb);
742*718a18a0SLorenzo Bianconi 		skb_headers_offset_update(nskb, head_off);
743*718a18a0SLorenzo Bianconi 
744*718a18a0SLorenzo Bianconi 		/* Allocate paged area of new skb */
745*718a18a0SLorenzo Bianconi 		off = size;
746*718a18a0SLorenzo Bianconi 		len = skb->len - off;
747*718a18a0SLorenzo Bianconi 
748*718a18a0SLorenzo Bianconi 		for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
749*718a18a0SLorenzo Bianconi 			page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
750*718a18a0SLorenzo Bianconi 			if (!page) {
751*718a18a0SLorenzo Bianconi 				consume_skb(nskb);
752*718a18a0SLorenzo Bianconi 				goto drop;
753*718a18a0SLorenzo Bianconi 			}
754*718a18a0SLorenzo Bianconi 
755*718a18a0SLorenzo Bianconi 			size = min_t(u32, len, PAGE_SIZE);
756*718a18a0SLorenzo Bianconi 			skb_add_rx_frag(nskb, i, page, 0, size, PAGE_SIZE);
757*718a18a0SLorenzo Bianconi 			if (skb_copy_bits(skb, off, page_address(page),
758*718a18a0SLorenzo Bianconi 					  size)) {
759*718a18a0SLorenzo Bianconi 				consume_skb(nskb);
760*718a18a0SLorenzo Bianconi 				goto drop;
761*718a18a0SLorenzo Bianconi 			}
762*718a18a0SLorenzo Bianconi 
763*718a18a0SLorenzo Bianconi 			len -= size;
764*718a18a0SLorenzo Bianconi 			off += size;
765*718a18a0SLorenzo Bianconi 		}
766*718a18a0SLorenzo Bianconi 
767*718a18a0SLorenzo Bianconi 		consume_skb(skb);
768*718a18a0SLorenzo Bianconi 		skb = nskb;
769*718a18a0SLorenzo Bianconi 	} else if (skb_headroom(skb) < XDP_PACKET_HEADROOM &&
770*718a18a0SLorenzo Bianconi 		   pskb_expand_head(skb, VETH_XDP_HEADROOM, 0, GFP_ATOMIC)) {
771*718a18a0SLorenzo Bianconi 		goto drop;
772*718a18a0SLorenzo Bianconi 	}
773*718a18a0SLorenzo Bianconi 
774*718a18a0SLorenzo Bianconi 	/* SKB "head" area always have tailroom for skb_shared_info */
775*718a18a0SLorenzo Bianconi 	frame_sz = skb_end_pointer(skb) - skb->head;
776*718a18a0SLorenzo Bianconi 	frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
777*718a18a0SLorenzo Bianconi 	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
778*718a18a0SLorenzo Bianconi 	xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
779*718a18a0SLorenzo Bianconi 			 skb_headlen(skb), true);
780*718a18a0SLorenzo Bianconi 
781*718a18a0SLorenzo Bianconi 	if (skb_is_nonlinear(skb)) {
782*718a18a0SLorenzo Bianconi 		skb_shinfo(skb)->xdp_frags_size = skb->data_len;
783*718a18a0SLorenzo Bianconi 		xdp_buff_set_frags_flag(xdp);
784*718a18a0SLorenzo Bianconi 	} else {
785*718a18a0SLorenzo Bianconi 		xdp_buff_clear_frags_flag(xdp);
786*718a18a0SLorenzo Bianconi 	}
787*718a18a0SLorenzo Bianconi 	*pskb = skb;
788*718a18a0SLorenzo Bianconi 
789*718a18a0SLorenzo Bianconi 	return 0;
790*718a18a0SLorenzo Bianconi drop:
791*718a18a0SLorenzo Bianconi 	consume_skb(skb);
792*718a18a0SLorenzo Bianconi 	*pskb = NULL;
793*718a18a0SLorenzo Bianconi 
794*718a18a0SLorenzo Bianconi 	return -ENOMEM;
795*718a18a0SLorenzo Bianconi }
796*718a18a0SLorenzo Bianconi 
7971c5b82e5SLorenzo Bianconi static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
7981c5b82e5SLorenzo Bianconi 					struct sk_buff *skb,
7991c5b82e5SLorenzo Bianconi 					struct veth_xdp_tx_bq *bq,
8001c5b82e5SLorenzo Bianconi 					struct veth_stats *stats)
801948d4f21SToshiaki Makita {
802948d4f21SToshiaki Makita 	void *orig_data, *orig_data_end;
803948d4f21SToshiaki Makita 	struct bpf_prog *xdp_prog;
804948d4f21SToshiaki Makita 	struct xdp_buff xdp;
805*718a18a0SLorenzo Bianconi 	u32 act, metalen;
806*718a18a0SLorenzo Bianconi 	int off;
807948d4f21SToshiaki Makita 
808d504fff0SPaolo Abeni 	skb_prepare_for_gro(skb);
8094bf9ffa0SToshiaki Makita 
810948d4f21SToshiaki Makita 	rcu_read_lock();
811638264dcSToshiaki Makita 	xdp_prog = rcu_dereference(rq->xdp_prog);
812948d4f21SToshiaki Makita 	if (unlikely(!xdp_prog)) {
813948d4f21SToshiaki Makita 		rcu_read_unlock();
814948d4f21SToshiaki Makita 		goto out;
815948d4f21SToshiaki Makita 	}
816948d4f21SToshiaki Makita 
817*718a18a0SLorenzo Bianconi 	__skb_push(skb, skb->data - skb_mac_header(skb));
818*718a18a0SLorenzo Bianconi 	if (veth_convert_skb_to_xdp_buff(rq, &xdp, &skb))
819948d4f21SToshiaki Makita 		goto drop;
820948d4f21SToshiaki Makita 
821948d4f21SToshiaki Makita 	orig_data = xdp.data;
822948d4f21SToshiaki Makita 	orig_data_end = xdp.data_end;
823948d4f21SToshiaki Makita 
824948d4f21SToshiaki Makita 	act = bpf_prog_run_xdp(xdp_prog, &xdp);
825948d4f21SToshiaki Makita 
826948d4f21SToshiaki Makita 	switch (act) {
827948d4f21SToshiaki Makita 	case XDP_PASS:
828948d4f21SToshiaki Makita 		break;
829d1396004SToshiaki Makita 	case XDP_TX:
830*718a18a0SLorenzo Bianconi 		veth_xdp_get(&xdp);
831d1396004SToshiaki Makita 		consume_skb(skb);
832638264dcSToshiaki Makita 		xdp.rxq->mem = rq->xdp_mem;
833bd32aa1fSLorenzo Bianconi 		if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
834638264dcSToshiaki Makita 			trace_xdp_exception(rq->dev, xdp_prog, act);
8351c5b82e5SLorenzo Bianconi 			stats->rx_drops++;
836d1396004SToshiaki Makita 			goto err_xdp;
837d1396004SToshiaki Makita 		}
8381c5b82e5SLorenzo Bianconi 		stats->xdp_tx++;
839d1396004SToshiaki Makita 		rcu_read_unlock();
840d1396004SToshiaki Makita 		goto xdp_xmit;
841d1396004SToshiaki Makita 	case XDP_REDIRECT:
842*718a18a0SLorenzo Bianconi 		veth_xdp_get(&xdp);
843d1396004SToshiaki Makita 		consume_skb(skb);
844638264dcSToshiaki Makita 		xdp.rxq->mem = rq->xdp_mem;
8451c5b82e5SLorenzo Bianconi 		if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
8461c5b82e5SLorenzo Bianconi 			stats->rx_drops++;
847d1396004SToshiaki Makita 			goto err_xdp;
8481c5b82e5SLorenzo Bianconi 		}
8491c5b82e5SLorenzo Bianconi 		stats->xdp_redirect++;
850d1396004SToshiaki Makita 		rcu_read_unlock();
851d1396004SToshiaki Makita 		goto xdp_xmit;
852948d4f21SToshiaki Makita 	default:
853c8064e5bSPaolo Abeni 		bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
854df561f66SGustavo A. R. Silva 		fallthrough;
855948d4f21SToshiaki Makita 	case XDP_ABORTED:
856638264dcSToshiaki Makita 		trace_xdp_exception(rq->dev, xdp_prog, act);
857df561f66SGustavo A. R. Silva 		fallthrough;
858948d4f21SToshiaki Makita 	case XDP_DROP:
8591c5b82e5SLorenzo Bianconi 		stats->xdp_drops++;
8601c5b82e5SLorenzo Bianconi 		goto xdp_drop;
861948d4f21SToshiaki Makita 	}
862948d4f21SToshiaki Makita 	rcu_read_unlock();
863948d4f21SToshiaki Makita 
86445a9e6d8SJesper Dangaard Brouer 	/* check if bpf_xdp_adjust_head was used */
865*718a18a0SLorenzo Bianconi 	off = orig_data - xdp.data;
866948d4f21SToshiaki Makita 	if (off > 0)
867948d4f21SToshiaki Makita 		__skb_push(skb, off);
868948d4f21SToshiaki Makita 	else if (off < 0)
869948d4f21SToshiaki Makita 		__skb_pull(skb, -off);
870*718a18a0SLorenzo Bianconi 
871*718a18a0SLorenzo Bianconi 	skb_reset_mac_header(skb);
87245a9e6d8SJesper Dangaard Brouer 
87345a9e6d8SJesper Dangaard Brouer 	/* check if bpf_xdp_adjust_tail was used */
874948d4f21SToshiaki Makita 	off = xdp.data_end - orig_data_end;
875948d4f21SToshiaki Makita 	if (off != 0)
87645a9e6d8SJesper Dangaard Brouer 		__skb_put(skb, off); /* positive on grow, negative on shrink */
877*718a18a0SLorenzo Bianconi 
878*718a18a0SLorenzo Bianconi 	/* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
879*718a18a0SLorenzo Bianconi 	 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
880*718a18a0SLorenzo Bianconi 	 */
881*718a18a0SLorenzo Bianconi 	if (xdp_buff_has_frags(&xdp))
882*718a18a0SLorenzo Bianconi 		skb->data_len = skb_shinfo(skb)->xdp_frags_size;
883*718a18a0SLorenzo Bianconi 	else
884*718a18a0SLorenzo Bianconi 		skb->data_len = 0;
885*718a18a0SLorenzo Bianconi 
886638264dcSToshiaki Makita 	skb->protocol = eth_type_trans(skb, rq->dev);
887948d4f21SToshiaki Makita 
888948d4f21SToshiaki Makita 	metalen = xdp.data - xdp.data_meta;
889948d4f21SToshiaki Makita 	if (metalen)
890948d4f21SToshiaki Makita 		skb_metadata_set(skb, metalen);
891948d4f21SToshiaki Makita out:
892948d4f21SToshiaki Makita 	return skb;
893948d4f21SToshiaki Makita drop:
8941c5b82e5SLorenzo Bianconi 	stats->rx_drops++;
8951c5b82e5SLorenzo Bianconi xdp_drop:
896948d4f21SToshiaki Makita 	rcu_read_unlock();
897948d4f21SToshiaki Makita 	kfree_skb(skb);
898948d4f21SToshiaki Makita 	return NULL;
899d1396004SToshiaki Makita err_xdp:
900d1396004SToshiaki Makita 	rcu_read_unlock();
901*718a18a0SLorenzo Bianconi 	xdp_return_buff(&xdp);
902d1396004SToshiaki Makita xdp_xmit:
903d1396004SToshiaki Makita 	return NULL;
904948d4f21SToshiaki Makita }
905948d4f21SToshiaki Makita 
9061c5b82e5SLorenzo Bianconi static int veth_xdp_rcv(struct veth_rq *rq, int budget,
9071c5b82e5SLorenzo Bianconi 			struct veth_xdp_tx_bq *bq,
9081c5b82e5SLorenzo Bianconi 			struct veth_stats *stats)
909948d4f21SToshiaki Makita {
91065e6dcf7SLorenzo Bianconi 	int i, done = 0, n_xdpf = 0;
91165e6dcf7SLorenzo Bianconi 	void *xdpf[VETH_XDP_BATCH];
912948d4f21SToshiaki Makita 
913948d4f21SToshiaki Makita 	for (i = 0; i < budget; i++) {
914638264dcSToshiaki Makita 		void *ptr = __ptr_ring_consume(&rq->xdp_ring);
915948d4f21SToshiaki Makita 
9169fc8d518SToshiaki Makita 		if (!ptr)
917948d4f21SToshiaki Makita 			break;
918948d4f21SToshiaki Makita 
919d1396004SToshiaki Makita 		if (veth_is_xdp_frame(ptr)) {
92065e6dcf7SLorenzo Bianconi 			/* ndo_xdp_xmit */
9214195e54aSToshiaki Makita 			struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
9224195e54aSToshiaki Makita 
9235142239aSLorenzo Bianconi 			stats->xdp_bytes += xdp_get_frame_len(frame);
92465e6dcf7SLorenzo Bianconi 			frame = veth_xdp_rcv_one(rq, frame, bq, stats);
92565e6dcf7SLorenzo Bianconi 			if (frame) {
92665e6dcf7SLorenzo Bianconi 				/* XDP_PASS */
92765e6dcf7SLorenzo Bianconi 				xdpf[n_xdpf++] = frame;
92865e6dcf7SLorenzo Bianconi 				if (n_xdpf == VETH_XDP_BATCH) {
92965e6dcf7SLorenzo Bianconi 					veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
93065e6dcf7SLorenzo Bianconi 							      bq, stats);
93165e6dcf7SLorenzo Bianconi 					n_xdpf = 0;
93265e6dcf7SLorenzo Bianconi 				}
93365e6dcf7SLorenzo Bianconi 			}
934d1396004SToshiaki Makita 		} else {
93565e6dcf7SLorenzo Bianconi 			/* ndo_start_xmit */
93665e6dcf7SLorenzo Bianconi 			struct sk_buff *skb = ptr;
93765e6dcf7SLorenzo Bianconi 
9381c5b82e5SLorenzo Bianconi 			stats->xdp_bytes += skb->len;
9391c5b82e5SLorenzo Bianconi 			skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
9409695b7deSPaolo Abeni 			if (skb) {
9419695b7deSPaolo Abeni 				if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
9429695b7deSPaolo Abeni 					netif_receive_skb(skb);
9439695b7deSPaolo Abeni 				else
944638264dcSToshiaki Makita 					napi_gro_receive(&rq->xdp_napi, skb);
94565e6dcf7SLorenzo Bianconi 			}
9469695b7deSPaolo Abeni 		}
947948d4f21SToshiaki Makita 		done++;
948948d4f21SToshiaki Makita 	}
949948d4f21SToshiaki Makita 
95065e6dcf7SLorenzo Bianconi 	if (n_xdpf)
95165e6dcf7SLorenzo Bianconi 		veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
95265e6dcf7SLorenzo Bianconi 
9534195e54aSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
9549152cff0SLorenzo Bianconi 	rq->stats.vs.xdp_redirect += stats->xdp_redirect;
9551c5b82e5SLorenzo Bianconi 	rq->stats.vs.xdp_bytes += stats->xdp_bytes;
95666fe4a07SLorenzo Bianconi 	rq->stats.vs.xdp_drops += stats->xdp_drops;
95766fe4a07SLorenzo Bianconi 	rq->stats.vs.rx_drops += stats->rx_drops;
95865780c56SLorenzo Bianconi 	rq->stats.vs.xdp_packets += done;
9594195e54aSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
9604195e54aSToshiaki Makita 
961948d4f21SToshiaki Makita 	return done;
962948d4f21SToshiaki Makita }
963948d4f21SToshiaki Makita 
964948d4f21SToshiaki Makita static int veth_poll(struct napi_struct *napi, int budget)
965948d4f21SToshiaki Makita {
966638264dcSToshiaki Makita 	struct veth_rq *rq =
967638264dcSToshiaki Makita 		container_of(napi, struct veth_rq, xdp_napi);
9681c5b82e5SLorenzo Bianconi 	struct veth_stats stats = {};
9699cda7807SToshiaki Makita 	struct veth_xdp_tx_bq bq;
970948d4f21SToshiaki Makita 	int done;
971948d4f21SToshiaki Makita 
9729cda7807SToshiaki Makita 	bq.count = 0;
9739cda7807SToshiaki Makita 
974d1396004SToshiaki Makita 	xdp_set_return_frame_no_direct();
9751c5b82e5SLorenzo Bianconi 	done = veth_xdp_rcv(rq, budget, &bq, &stats);
976948d4f21SToshiaki Makita 
977948d4f21SToshiaki Makita 	if (done < budget && napi_complete_done(napi, done)) {
978948d4f21SToshiaki Makita 		/* Write rx_notify_masked before reading ptr_ring */
979638264dcSToshiaki Makita 		smp_store_mb(rq->rx_notify_masked, false);
980638264dcSToshiaki Makita 		if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
98168468d8cSEric Dumazet 			if (napi_schedule_prep(&rq->xdp_napi)) {
98268468d8cSEric Dumazet 				WRITE_ONCE(rq->rx_notify_masked, true);
98368468d8cSEric Dumazet 				__napi_schedule(&rq->xdp_napi);
98468468d8cSEric Dumazet 			}
985948d4f21SToshiaki Makita 		}
986948d4f21SToshiaki Makita 	}
987948d4f21SToshiaki Makita 
9881c5b82e5SLorenzo Bianconi 	if (stats.xdp_tx > 0)
989bd32aa1fSLorenzo Bianconi 		veth_xdp_flush(rq, &bq);
9901c5b82e5SLorenzo Bianconi 	if (stats.xdp_redirect > 0)
9911d233886SToke Høiland-Jørgensen 		xdp_do_flush();
992d1396004SToshiaki Makita 	xdp_clear_return_frame_no_direct();
993d1396004SToshiaki Makita 
994948d4f21SToshiaki Makita 	return done;
995948d4f21SToshiaki Makita }
996948d4f21SToshiaki Makita 
997dedd53c5SPaolo Abeni static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
998948d4f21SToshiaki Makita {
999948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1000638264dcSToshiaki Makita 	int err, i;
1001948d4f21SToshiaki Makita 
1002dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1003638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1004638264dcSToshiaki Makita 
1005638264dcSToshiaki Makita 		err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
1006948d4f21SToshiaki Makita 		if (err)
1007638264dcSToshiaki Makita 			goto err_xdp_ring;
1008638264dcSToshiaki Makita 	}
1009948d4f21SToshiaki Makita 
1010dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1011638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1012638264dcSToshiaki Makita 
1013638264dcSToshiaki Makita 		napi_enable(&rq->xdp_napi);
1014d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1015638264dcSToshiaki Makita 	}
1016948d4f21SToshiaki Makita 
1017948d4f21SToshiaki Makita 	return 0;
1018dedd53c5SPaolo Abeni 
1019638264dcSToshiaki Makita err_xdp_ring:
1020dedd53c5SPaolo Abeni 	for (i--; i >= start; i--)
1021638264dcSToshiaki Makita 		ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
1022638264dcSToshiaki Makita 
1023638264dcSToshiaki Makita 	return err;
1024948d4f21SToshiaki Makita }
1025948d4f21SToshiaki Makita 
1026dedd53c5SPaolo Abeni static int __veth_napi_enable(struct net_device *dev)
1027dedd53c5SPaolo Abeni {
1028dedd53c5SPaolo Abeni 	return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1029dedd53c5SPaolo Abeni }
1030dedd53c5SPaolo Abeni 
1031dedd53c5SPaolo Abeni static void veth_napi_del_range(struct net_device *dev, int start, int end)
1032948d4f21SToshiaki Makita {
1033948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1034638264dcSToshiaki Makita 	int i;
1035948d4f21SToshiaki Makita 
1036dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1037638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1038638264dcSToshiaki Makita 
1039d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, NULL);
1040638264dcSToshiaki Makita 		napi_disable(&rq->xdp_napi);
10415198d545SJakub Kicinski 		__netif_napi_del(&rq->xdp_napi);
1042638264dcSToshiaki Makita 	}
1043638264dcSToshiaki Makita 	synchronize_net();
1044638264dcSToshiaki Makita 
1045dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1046638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1047638264dcSToshiaki Makita 
1048638264dcSToshiaki Makita 		rq->rx_notify_masked = false;
1049638264dcSToshiaki Makita 		ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
1050638264dcSToshiaki Makita 	}
1051948d4f21SToshiaki Makita }
1052948d4f21SToshiaki Makita 
1053dedd53c5SPaolo Abeni static void veth_napi_del(struct net_device *dev)
1054dedd53c5SPaolo Abeni {
1055dedd53c5SPaolo Abeni 	veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1056dedd53c5SPaolo Abeni }
1057dedd53c5SPaolo Abeni 
1058d3256efdSPaolo Abeni static bool veth_gro_requested(const struct net_device *dev)
1059d3256efdSPaolo Abeni {
1060d3256efdSPaolo Abeni 	return !!(dev->wanted_features & NETIF_F_GRO);
1061d3256efdSPaolo Abeni }
1062d3256efdSPaolo Abeni 
1063dedd53c5SPaolo Abeni static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1064dedd53c5SPaolo Abeni 				 bool napi_already_on)
1065948d4f21SToshiaki Makita {
1066948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1067638264dcSToshiaki Makita 	int err, i;
1068948d4f21SToshiaki Makita 
1069dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1070638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1071948d4f21SToshiaki Makita 
1072d3256efdSPaolo Abeni 		if (!napi_already_on)
1073b02e5a0eSBjörn Töpel 			netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1074b02e5a0eSBjörn Töpel 		err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1075948d4f21SToshiaki Makita 		if (err < 0)
1076638264dcSToshiaki Makita 			goto err_rxq_reg;
1077638264dcSToshiaki Makita 
1078638264dcSToshiaki Makita 		err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1079638264dcSToshiaki Makita 						 MEM_TYPE_PAGE_SHARED,
1080638264dcSToshiaki Makita 						 NULL);
1081638264dcSToshiaki Makita 		if (err < 0)
1082638264dcSToshiaki Makita 			goto err_reg_mem;
1083638264dcSToshiaki Makita 
1084638264dcSToshiaki Makita 		/* Save original mem info as it can be overwritten */
1085638264dcSToshiaki Makita 		rq->xdp_mem = rq->xdp_rxq.mem;
1086638264dcSToshiaki Makita 	}
1087dedd53c5SPaolo Abeni 	return 0;
1088dedd53c5SPaolo Abeni 
1089dedd53c5SPaolo Abeni err_reg_mem:
1090dedd53c5SPaolo Abeni 	xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1091dedd53c5SPaolo Abeni err_rxq_reg:
1092dedd53c5SPaolo Abeni 	for (i--; i >= start; i--) {
1093dedd53c5SPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1094dedd53c5SPaolo Abeni 
1095dedd53c5SPaolo Abeni 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1096dedd53c5SPaolo Abeni 		if (!napi_already_on)
1097dedd53c5SPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1098dedd53c5SPaolo Abeni 	}
1099dedd53c5SPaolo Abeni 
1100dedd53c5SPaolo Abeni 	return err;
1101dedd53c5SPaolo Abeni }
1102dedd53c5SPaolo Abeni 
1103dedd53c5SPaolo Abeni static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1104dedd53c5SPaolo Abeni 				   bool delete_napi)
1105dedd53c5SPaolo Abeni {
1106dedd53c5SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1107dedd53c5SPaolo Abeni 	int i;
1108dedd53c5SPaolo Abeni 
1109dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1110dedd53c5SPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1111dedd53c5SPaolo Abeni 
1112dedd53c5SPaolo Abeni 		rq->xdp_rxq.mem = rq->xdp_mem;
1113dedd53c5SPaolo Abeni 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1114dedd53c5SPaolo Abeni 
1115dedd53c5SPaolo Abeni 		if (delete_napi)
1116dedd53c5SPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1117dedd53c5SPaolo Abeni 	}
1118dedd53c5SPaolo Abeni }
1119dedd53c5SPaolo Abeni 
1120dedd53c5SPaolo Abeni static int veth_enable_xdp(struct net_device *dev)
1121dedd53c5SPaolo Abeni {
1122dedd53c5SPaolo Abeni 	bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1123dedd53c5SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1124dedd53c5SPaolo Abeni 	int err, i;
1125dedd53c5SPaolo Abeni 
1126dedd53c5SPaolo Abeni 	if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1127dedd53c5SPaolo Abeni 		err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1128dedd53c5SPaolo Abeni 		if (err)
1129dedd53c5SPaolo Abeni 			return err;
1130948d4f21SToshiaki Makita 
1131d3256efdSPaolo Abeni 		if (!napi_already_on) {
1132d3256efdSPaolo Abeni 			err = __veth_napi_enable(dev);
1133dedd53c5SPaolo Abeni 			if (err) {
1134dedd53c5SPaolo Abeni 				veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1135dedd53c5SPaolo Abeni 				return err;
1136dedd53c5SPaolo Abeni 			}
1137d3256efdSPaolo Abeni 
1138d3256efdSPaolo Abeni 			if (!veth_gro_requested(dev)) {
1139d3256efdSPaolo Abeni 				/* user-space did not require GRO, but adding XDP
1140d3256efdSPaolo Abeni 				 * is supposed to get GRO working
1141d3256efdSPaolo Abeni 				 */
1142d3256efdSPaolo Abeni 				dev->features |= NETIF_F_GRO;
1143d3256efdSPaolo Abeni 				netdev_features_change(dev);
1144d3256efdSPaolo Abeni 			}
1145d3256efdSPaolo Abeni 		}
1146948d4f21SToshiaki Makita 	}
1147948d4f21SToshiaki Makita 
1148d3256efdSPaolo Abeni 	for (i = 0; i < dev->real_num_rx_queues; i++) {
1149638264dcSToshiaki Makita 		rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1150d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1151d3256efdSPaolo Abeni 	}
1152948d4f21SToshiaki Makita 
1153948d4f21SToshiaki Makita 	return 0;
1154948d4f21SToshiaki Makita }
1155948d4f21SToshiaki Makita 
1156948d4f21SToshiaki Makita static void veth_disable_xdp(struct net_device *dev)
1157948d4f21SToshiaki Makita {
1158948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1159638264dcSToshiaki Makita 	int i;
1160948d4f21SToshiaki Makita 
1161638264dcSToshiaki Makita 	for (i = 0; i < dev->real_num_rx_queues; i++)
1162638264dcSToshiaki Makita 		rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1163d3256efdSPaolo Abeni 
1164d3256efdSPaolo Abeni 	if (!netif_running(dev) || !veth_gro_requested(dev)) {
1165948d4f21SToshiaki Makita 		veth_napi_del(dev);
1166d3256efdSPaolo Abeni 
1167d3256efdSPaolo Abeni 		/* if user-space did not require GRO, since adding XDP
1168d3256efdSPaolo Abeni 		 * enabled it, clear it now
1169d3256efdSPaolo Abeni 		 */
1170d3256efdSPaolo Abeni 		if (!veth_gro_requested(dev) && netif_running(dev)) {
1171d3256efdSPaolo Abeni 			dev->features &= ~NETIF_F_GRO;
1172d3256efdSPaolo Abeni 			netdev_features_change(dev);
1173d3256efdSPaolo Abeni 		}
1174d3256efdSPaolo Abeni 	}
1175d3256efdSPaolo Abeni 
1176dedd53c5SPaolo Abeni 	veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1177948d4f21SToshiaki Makita }
1178948d4f21SToshiaki Makita 
1179dedd53c5SPaolo Abeni static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1180d3256efdSPaolo Abeni {
1181d3256efdSPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1182d3256efdSPaolo Abeni 	int err, i;
1183d3256efdSPaolo Abeni 
1184dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1185d3256efdSPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1186d3256efdSPaolo Abeni 
1187d3256efdSPaolo Abeni 		netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1188d3256efdSPaolo Abeni 	}
1189d3256efdSPaolo Abeni 
1190dedd53c5SPaolo Abeni 	err = __veth_napi_enable_range(dev, start, end);
1191d3256efdSPaolo Abeni 	if (err) {
1192dedd53c5SPaolo Abeni 		for (i = start; i < end; i++) {
1193d3256efdSPaolo Abeni 			struct veth_rq *rq = &priv->rq[i];
1194d3256efdSPaolo Abeni 
1195d3256efdSPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1196d3256efdSPaolo Abeni 		}
1197d3256efdSPaolo Abeni 		return err;
1198d3256efdSPaolo Abeni 	}
1199d3256efdSPaolo Abeni 	return err;
1200d3256efdSPaolo Abeni }
1201d3256efdSPaolo Abeni 
1202dedd53c5SPaolo Abeni static int veth_napi_enable(struct net_device *dev)
1203dedd53c5SPaolo Abeni {
1204dedd53c5SPaolo Abeni 	return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1205dedd53c5SPaolo Abeni }
1206dedd53c5SPaolo Abeni 
12074752eeb3SPaolo Abeni static void veth_disable_range_safe(struct net_device *dev, int start, int end)
12084752eeb3SPaolo Abeni {
12094752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
12104752eeb3SPaolo Abeni 
12114752eeb3SPaolo Abeni 	if (start >= end)
12124752eeb3SPaolo Abeni 		return;
12134752eeb3SPaolo Abeni 
12144752eeb3SPaolo Abeni 	if (priv->_xdp_prog) {
12154752eeb3SPaolo Abeni 		veth_napi_del_range(dev, start, end);
12164752eeb3SPaolo Abeni 		veth_disable_xdp_range(dev, start, end, false);
12174752eeb3SPaolo Abeni 	} else if (veth_gro_requested(dev)) {
12184752eeb3SPaolo Abeni 		veth_napi_del_range(dev, start, end);
12194752eeb3SPaolo Abeni 	}
12204752eeb3SPaolo Abeni }
12214752eeb3SPaolo Abeni 
12224752eeb3SPaolo Abeni static int veth_enable_range_safe(struct net_device *dev, int start, int end)
12234752eeb3SPaolo Abeni {
12244752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
12254752eeb3SPaolo Abeni 	int err;
12264752eeb3SPaolo Abeni 
12274752eeb3SPaolo Abeni 	if (start >= end)
12284752eeb3SPaolo Abeni 		return 0;
12294752eeb3SPaolo Abeni 
12304752eeb3SPaolo Abeni 	if (priv->_xdp_prog) {
12314752eeb3SPaolo Abeni 		/* these channels are freshly initialized, napi is not on there even
12324752eeb3SPaolo Abeni 		 * when GRO is requeste
12334752eeb3SPaolo Abeni 		 */
12344752eeb3SPaolo Abeni 		err = veth_enable_xdp_range(dev, start, end, false);
12354752eeb3SPaolo Abeni 		if (err)
12364752eeb3SPaolo Abeni 			return err;
12374752eeb3SPaolo Abeni 
12384752eeb3SPaolo Abeni 		err = __veth_napi_enable_range(dev, start, end);
12394752eeb3SPaolo Abeni 		if (err) {
12404752eeb3SPaolo Abeni 			/* on error always delete the newly added napis */
12414752eeb3SPaolo Abeni 			veth_disable_xdp_range(dev, start, end, true);
12424752eeb3SPaolo Abeni 			return err;
12434752eeb3SPaolo Abeni 		}
12444752eeb3SPaolo Abeni 	} else if (veth_gro_requested(dev)) {
12454752eeb3SPaolo Abeni 		return veth_napi_enable_range(dev, start, end);
12464752eeb3SPaolo Abeni 	}
12474752eeb3SPaolo Abeni 	return 0;
12484752eeb3SPaolo Abeni }
12494752eeb3SPaolo Abeni 
12504752eeb3SPaolo Abeni static int veth_set_channels(struct net_device *dev,
12514752eeb3SPaolo Abeni 			     struct ethtool_channels *ch)
12524752eeb3SPaolo Abeni {
12534752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
12544752eeb3SPaolo Abeni 	unsigned int old_rx_count, new_rx_count;
12554752eeb3SPaolo Abeni 	struct veth_priv *peer_priv;
12564752eeb3SPaolo Abeni 	struct net_device *peer;
12574752eeb3SPaolo Abeni 	int err;
12584752eeb3SPaolo Abeni 
12594752eeb3SPaolo Abeni 	/* sanity check. Upper bounds are already enforced by the caller */
12604752eeb3SPaolo Abeni 	if (!ch->rx_count || !ch->tx_count)
12614752eeb3SPaolo Abeni 		return -EINVAL;
12624752eeb3SPaolo Abeni 
12634752eeb3SPaolo Abeni 	/* avoid braking XDP, if that is enabled */
12644752eeb3SPaolo Abeni 	peer = rtnl_dereference(priv->peer);
12654752eeb3SPaolo Abeni 	peer_priv = peer ? netdev_priv(peer) : NULL;
12664752eeb3SPaolo Abeni 	if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
12674752eeb3SPaolo Abeni 		return -EINVAL;
12684752eeb3SPaolo Abeni 
12694752eeb3SPaolo Abeni 	if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
12704752eeb3SPaolo Abeni 		return -EINVAL;
12714752eeb3SPaolo Abeni 
12724752eeb3SPaolo Abeni 	old_rx_count = dev->real_num_rx_queues;
12734752eeb3SPaolo Abeni 	new_rx_count = ch->rx_count;
12744752eeb3SPaolo Abeni 	if (netif_running(dev)) {
12754752eeb3SPaolo Abeni 		/* turn device off */
12764752eeb3SPaolo Abeni 		netif_carrier_off(dev);
12774752eeb3SPaolo Abeni 		if (peer)
12784752eeb3SPaolo Abeni 			netif_carrier_off(peer);
12794752eeb3SPaolo Abeni 
12804752eeb3SPaolo Abeni 		/* try to allocate new resurces, as needed*/
12814752eeb3SPaolo Abeni 		err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
12824752eeb3SPaolo Abeni 		if (err)
12834752eeb3SPaolo Abeni 			goto out;
12844752eeb3SPaolo Abeni 	}
12854752eeb3SPaolo Abeni 
12864752eeb3SPaolo Abeni 	err = netif_set_real_num_rx_queues(dev, ch->rx_count);
12874752eeb3SPaolo Abeni 	if (err)
12884752eeb3SPaolo Abeni 		goto revert;
12894752eeb3SPaolo Abeni 
12904752eeb3SPaolo Abeni 	err = netif_set_real_num_tx_queues(dev, ch->tx_count);
12914752eeb3SPaolo Abeni 	if (err) {
12924752eeb3SPaolo Abeni 		int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
12934752eeb3SPaolo Abeni 
12944752eeb3SPaolo Abeni 		/* this error condition could happen only if rx and tx change
12954752eeb3SPaolo Abeni 		 * in opposite directions (e.g. tx nr raises, rx nr decreases)
12964752eeb3SPaolo Abeni 		 * and we can't do anything to fully restore the original
12974752eeb3SPaolo Abeni 		 * status
12984752eeb3SPaolo Abeni 		 */
12994752eeb3SPaolo Abeni 		if (err2)
13004752eeb3SPaolo Abeni 			pr_warn("Can't restore rx queues config %d -> %d %d",
13014752eeb3SPaolo Abeni 				new_rx_count, old_rx_count, err2);
13024752eeb3SPaolo Abeni 		else
13034752eeb3SPaolo Abeni 			goto revert;
13044752eeb3SPaolo Abeni 	}
13054752eeb3SPaolo Abeni 
13064752eeb3SPaolo Abeni out:
13074752eeb3SPaolo Abeni 	if (netif_running(dev)) {
13084752eeb3SPaolo Abeni 		/* note that we need to swap the arguments WRT the enable part
13094752eeb3SPaolo Abeni 		 * to identify the range we have to disable
13104752eeb3SPaolo Abeni 		 */
13114752eeb3SPaolo Abeni 		veth_disable_range_safe(dev, new_rx_count, old_rx_count);
13124752eeb3SPaolo Abeni 		netif_carrier_on(dev);
13134752eeb3SPaolo Abeni 		if (peer)
13144752eeb3SPaolo Abeni 			netif_carrier_on(peer);
13154752eeb3SPaolo Abeni 	}
13164752eeb3SPaolo Abeni 	return err;
13174752eeb3SPaolo Abeni 
13184752eeb3SPaolo Abeni revert:
13194752eeb3SPaolo Abeni 	new_rx_count = old_rx_count;
13204752eeb3SPaolo Abeni 	old_rx_count = ch->rx_count;
13214752eeb3SPaolo Abeni 	goto out;
13224752eeb3SPaolo Abeni }
13234752eeb3SPaolo Abeni 
1324e314dbdcSPavel Emelyanov static int veth_open(struct net_device *dev)
1325e314dbdcSPavel Emelyanov {
1326d0e2c55eSEric Dumazet 	struct veth_priv *priv = netdev_priv(dev);
1327d0e2c55eSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
1328948d4f21SToshiaki Makita 	int err;
1329e314dbdcSPavel Emelyanov 
1330d0e2c55eSEric Dumazet 	if (!peer)
1331e314dbdcSPavel Emelyanov 		return -ENOTCONN;
1332e314dbdcSPavel Emelyanov 
1333948d4f21SToshiaki Makita 	if (priv->_xdp_prog) {
1334948d4f21SToshiaki Makita 		err = veth_enable_xdp(dev);
1335948d4f21SToshiaki Makita 		if (err)
1336948d4f21SToshiaki Makita 			return err;
1337d3256efdSPaolo Abeni 	} else if (veth_gro_requested(dev)) {
1338d3256efdSPaolo Abeni 		err = veth_napi_enable(dev);
1339d3256efdSPaolo Abeni 		if (err)
1340d3256efdSPaolo Abeni 			return err;
1341948d4f21SToshiaki Makita 	}
1342948d4f21SToshiaki Makita 
1343d0e2c55eSEric Dumazet 	if (peer->flags & IFF_UP) {
1344e314dbdcSPavel Emelyanov 		netif_carrier_on(dev);
1345d0e2c55eSEric Dumazet 		netif_carrier_on(peer);
1346e314dbdcSPavel Emelyanov 	}
1347948d4f21SToshiaki Makita 
1348e314dbdcSPavel Emelyanov 	return 0;
1349e314dbdcSPavel Emelyanov }
1350e314dbdcSPavel Emelyanov 
13512cf48a10SEric W. Biederman static int veth_close(struct net_device *dev)
13522cf48a10SEric W. Biederman {
13532cf48a10SEric W. Biederman 	struct veth_priv *priv = netdev_priv(dev);
13542efd32eeSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
13552cf48a10SEric W. Biederman 
13562cf48a10SEric W. Biederman 	netif_carrier_off(dev);
13572efd32eeSEric Dumazet 	if (peer)
13582efd32eeSEric Dumazet 		netif_carrier_off(peer);
13592cf48a10SEric W. Biederman 
1360948d4f21SToshiaki Makita 	if (priv->_xdp_prog)
1361948d4f21SToshiaki Makita 		veth_disable_xdp(dev);
1362d3256efdSPaolo Abeni 	else if (veth_gro_requested(dev))
1363d3256efdSPaolo Abeni 		veth_napi_del(dev);
1364948d4f21SToshiaki Makita 
13652cf48a10SEric W. Biederman 	return 0;
13662cf48a10SEric W. Biederman }
13672cf48a10SEric W. Biederman 
136891572088SJarod Wilson static int is_valid_veth_mtu(int mtu)
136938d40815SEric Biederman {
137091572088SJarod Wilson 	return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
137138d40815SEric Biederman }
137238d40815SEric Biederman 
13737797b93bSToshiaki Makita static int veth_alloc_queues(struct net_device *dev)
13747797b93bSToshiaki Makita {
13757797b93bSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
13767797b93bSToshiaki Makita 	int i;
13777797b93bSToshiaki Makita 
13787797b93bSToshiaki Makita 	priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
13797797b93bSToshiaki Makita 	if (!priv->rq)
13807797b93bSToshiaki Makita 		return -ENOMEM;
13817797b93bSToshiaki Makita 
13824195e54aSToshiaki Makita 	for (i = 0; i < dev->num_rx_queues; i++) {
13837797b93bSToshiaki Makita 		priv->rq[i].dev = dev;
13844195e54aSToshiaki Makita 		u64_stats_init(&priv->rq[i].stats.syncp);
13854195e54aSToshiaki Makita 	}
13867797b93bSToshiaki Makita 
13877797b93bSToshiaki Makita 	return 0;
13887797b93bSToshiaki Makita }
13897797b93bSToshiaki Makita 
13907797b93bSToshiaki Makita static void veth_free_queues(struct net_device *dev)
13917797b93bSToshiaki Makita {
13927797b93bSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
13937797b93bSToshiaki Makita 
13947797b93bSToshiaki Makita 	kfree(priv->rq);
13957797b93bSToshiaki Makita }
13967797b93bSToshiaki Makita 
1397e314dbdcSPavel Emelyanov static int veth_dev_init(struct net_device *dev)
1398e314dbdcSPavel Emelyanov {
13997797b93bSToshiaki Makita 	int err;
14007797b93bSToshiaki Makita 
140114d73416SLi RongQing 	dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
140214d73416SLi RongQing 	if (!dev->lstats)
1403e314dbdcSPavel Emelyanov 		return -ENOMEM;
14047797b93bSToshiaki Makita 
14057797b93bSToshiaki Makita 	err = veth_alloc_queues(dev);
14067797b93bSToshiaki Makita 	if (err) {
140714d73416SLi RongQing 		free_percpu(dev->lstats);
14087797b93bSToshiaki Makita 		return err;
14097797b93bSToshiaki Makita 	}
14107797b93bSToshiaki Makita 
1411e314dbdcSPavel Emelyanov 	return 0;
1412e314dbdcSPavel Emelyanov }
1413e314dbdcSPavel Emelyanov 
141411687a10SDavid S. Miller static void veth_dev_free(struct net_device *dev)
141511687a10SDavid S. Miller {
14167797b93bSToshiaki Makita 	veth_free_queues(dev);
141714d73416SLi RongQing 	free_percpu(dev->lstats);
141811687a10SDavid S. Miller }
141911687a10SDavid S. Miller 
1420bb446c19SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
1421bb446c19SWANG Cong static void veth_poll_controller(struct net_device *dev)
1422bb446c19SWANG Cong {
1423bb446c19SWANG Cong 	/* veth only receives frames when its peer sends one
1424948d4f21SToshiaki Makita 	 * Since it has nothing to do with disabling irqs, we are guaranteed
1425bb446c19SWANG Cong 	 * never to have pending data when we poll for it so
1426bb446c19SWANG Cong 	 * there is nothing to do here.
1427bb446c19SWANG Cong 	 *
1428bb446c19SWANG Cong 	 * We need this though so netpoll recognizes us as an interface that
1429bb446c19SWANG Cong 	 * supports polling, which enables bridge devices in virt setups to
1430bb446c19SWANG Cong 	 * still use netconsole
1431bb446c19SWANG Cong 	 */
1432bb446c19SWANG Cong }
1433bb446c19SWANG Cong #endif	/* CONFIG_NET_POLL_CONTROLLER */
1434bb446c19SWANG Cong 
1435a45253bfSNicolas Dichtel static int veth_get_iflink(const struct net_device *dev)
1436a45253bfSNicolas Dichtel {
1437a45253bfSNicolas Dichtel 	struct veth_priv *priv = netdev_priv(dev);
1438a45253bfSNicolas Dichtel 	struct net_device *peer;
1439a45253bfSNicolas Dichtel 	int iflink;
1440a45253bfSNicolas Dichtel 
1441a45253bfSNicolas Dichtel 	rcu_read_lock();
1442a45253bfSNicolas Dichtel 	peer = rcu_dereference(priv->peer);
1443a45253bfSNicolas Dichtel 	iflink = peer ? peer->ifindex : 0;
1444a45253bfSNicolas Dichtel 	rcu_read_unlock();
1445a45253bfSNicolas Dichtel 
1446a45253bfSNicolas Dichtel 	return iflink;
1447a45253bfSNicolas Dichtel }
1448a45253bfSNicolas Dichtel 
1449dc224822SToshiaki Makita static netdev_features_t veth_fix_features(struct net_device *dev,
1450dc224822SToshiaki Makita 					   netdev_features_t features)
1451dc224822SToshiaki Makita {
1452dc224822SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1453dc224822SToshiaki Makita 	struct net_device *peer;
1454dc224822SToshiaki Makita 
1455dc224822SToshiaki Makita 	peer = rtnl_dereference(priv->peer);
1456dc224822SToshiaki Makita 	if (peer) {
1457dc224822SToshiaki Makita 		struct veth_priv *peer_priv = netdev_priv(peer);
1458dc224822SToshiaki Makita 
1459dc224822SToshiaki Makita 		if (peer_priv->_xdp_prog)
1460dc224822SToshiaki Makita 			features &= ~NETIF_F_GSO_SOFTWARE;
1461dc224822SToshiaki Makita 	}
1462d3256efdSPaolo Abeni 	if (priv->_xdp_prog)
1463d3256efdSPaolo Abeni 		features |= NETIF_F_GRO;
1464dc224822SToshiaki Makita 
1465dc224822SToshiaki Makita 	return features;
1466dc224822SToshiaki Makita }
1467dc224822SToshiaki Makita 
1468d3256efdSPaolo Abeni static int veth_set_features(struct net_device *dev,
1469d3256efdSPaolo Abeni 			     netdev_features_t features)
1470d3256efdSPaolo Abeni {
1471d3256efdSPaolo Abeni 	netdev_features_t changed = features ^ dev->features;
1472d3256efdSPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1473d3256efdSPaolo Abeni 	int err;
1474d3256efdSPaolo Abeni 
1475d3256efdSPaolo Abeni 	if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1476d3256efdSPaolo Abeni 		return 0;
1477d3256efdSPaolo Abeni 
1478d3256efdSPaolo Abeni 	if (features & NETIF_F_GRO) {
1479d3256efdSPaolo Abeni 		err = veth_napi_enable(dev);
1480d3256efdSPaolo Abeni 		if (err)
1481d3256efdSPaolo Abeni 			return err;
1482d3256efdSPaolo Abeni 	} else {
1483d3256efdSPaolo Abeni 		veth_napi_del(dev);
1484d3256efdSPaolo Abeni 	}
1485d3256efdSPaolo Abeni 	return 0;
1486d3256efdSPaolo Abeni }
1487d3256efdSPaolo Abeni 
1488163e5292SPaolo Abeni static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1489163e5292SPaolo Abeni {
1490163e5292SPaolo Abeni 	struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1491163e5292SPaolo Abeni 	struct net_device *peer;
1492163e5292SPaolo Abeni 
1493163e5292SPaolo Abeni 	if (new_hr < 0)
1494163e5292SPaolo Abeni 		new_hr = 0;
1495163e5292SPaolo Abeni 
1496163e5292SPaolo Abeni 	rcu_read_lock();
1497163e5292SPaolo Abeni 	peer = rcu_dereference(priv->peer);
1498163e5292SPaolo Abeni 	if (unlikely(!peer))
1499163e5292SPaolo Abeni 		goto out;
1500163e5292SPaolo Abeni 
1501163e5292SPaolo Abeni 	peer_priv = netdev_priv(peer);
1502163e5292SPaolo Abeni 	priv->requested_headroom = new_hr;
1503163e5292SPaolo Abeni 	new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1504163e5292SPaolo Abeni 	dev->needed_headroom = new_hr;
1505163e5292SPaolo Abeni 	peer->needed_headroom = new_hr;
1506163e5292SPaolo Abeni 
1507163e5292SPaolo Abeni out:
1508163e5292SPaolo Abeni 	rcu_read_unlock();
1509163e5292SPaolo Abeni }
1510163e5292SPaolo Abeni 
1511948d4f21SToshiaki Makita static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1512948d4f21SToshiaki Makita 			struct netlink_ext_ack *extack)
1513948d4f21SToshiaki Makita {
1514948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1515948d4f21SToshiaki Makita 	struct bpf_prog *old_prog;
1516948d4f21SToshiaki Makita 	struct net_device *peer;
1517dc224822SToshiaki Makita 	unsigned int max_mtu;
1518948d4f21SToshiaki Makita 	int err;
1519948d4f21SToshiaki Makita 
1520948d4f21SToshiaki Makita 	old_prog = priv->_xdp_prog;
1521948d4f21SToshiaki Makita 	priv->_xdp_prog = prog;
1522948d4f21SToshiaki Makita 	peer = rtnl_dereference(priv->peer);
1523948d4f21SToshiaki Makita 
1524948d4f21SToshiaki Makita 	if (prog) {
1525948d4f21SToshiaki Makita 		if (!peer) {
1526948d4f21SToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1527948d4f21SToshiaki Makita 			err = -ENOTCONN;
1528948d4f21SToshiaki Makita 			goto err;
1529948d4f21SToshiaki Makita 		}
1530948d4f21SToshiaki Makita 
1531dc224822SToshiaki Makita 		max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1532dc224822SToshiaki Makita 			  peer->hard_header_len -
1533dc224822SToshiaki Makita 			  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1534dc224822SToshiaki Makita 		if (peer->mtu > max_mtu) {
1535dc224822SToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1536dc224822SToshiaki Makita 			err = -ERANGE;
1537dc224822SToshiaki Makita 			goto err;
1538dc224822SToshiaki Makita 		}
1539dc224822SToshiaki Makita 
1540638264dcSToshiaki Makita 		if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1541638264dcSToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1542638264dcSToshiaki Makita 			err = -ENOSPC;
1543638264dcSToshiaki Makita 			goto err;
1544638264dcSToshiaki Makita 		}
1545638264dcSToshiaki Makita 
1546948d4f21SToshiaki Makita 		if (dev->flags & IFF_UP) {
1547948d4f21SToshiaki Makita 			err = veth_enable_xdp(dev);
1548948d4f21SToshiaki Makita 			if (err) {
1549948d4f21SToshiaki Makita 				NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1550948d4f21SToshiaki Makita 				goto err;
1551948d4f21SToshiaki Makita 			}
1552948d4f21SToshiaki Makita 		}
1553dc224822SToshiaki Makita 
1554dc224822SToshiaki Makita 		if (!old_prog) {
1555dc224822SToshiaki Makita 			peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1556dc224822SToshiaki Makita 			peer->max_mtu = max_mtu;
1557dc224822SToshiaki Makita 		}
1558948d4f21SToshiaki Makita 	}
1559948d4f21SToshiaki Makita 
1560948d4f21SToshiaki Makita 	if (old_prog) {
1561dc224822SToshiaki Makita 		if (!prog) {
1562dc224822SToshiaki Makita 			if (dev->flags & IFF_UP)
1563948d4f21SToshiaki Makita 				veth_disable_xdp(dev);
1564dc224822SToshiaki Makita 
1565dc224822SToshiaki Makita 			if (peer) {
1566dc224822SToshiaki Makita 				peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1567dc224822SToshiaki Makita 				peer->max_mtu = ETH_MAX_MTU;
1568dc224822SToshiaki Makita 			}
1569dc224822SToshiaki Makita 		}
1570948d4f21SToshiaki Makita 		bpf_prog_put(old_prog);
1571948d4f21SToshiaki Makita 	}
1572948d4f21SToshiaki Makita 
1573dc224822SToshiaki Makita 	if ((!!old_prog ^ !!prog) && peer)
1574dc224822SToshiaki Makita 		netdev_update_features(peer);
1575dc224822SToshiaki Makita 
1576948d4f21SToshiaki Makita 	return 0;
1577948d4f21SToshiaki Makita err:
1578948d4f21SToshiaki Makita 	priv->_xdp_prog = old_prog;
1579948d4f21SToshiaki Makita 
1580948d4f21SToshiaki Makita 	return err;
1581948d4f21SToshiaki Makita }
1582948d4f21SToshiaki Makita 
1583948d4f21SToshiaki Makita static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1584948d4f21SToshiaki Makita {
1585948d4f21SToshiaki Makita 	switch (xdp->command) {
1586948d4f21SToshiaki Makita 	case XDP_SETUP_PROG:
1587948d4f21SToshiaki Makita 		return veth_xdp_set(dev, xdp->prog, xdp->extack);
1588948d4f21SToshiaki Makita 	default:
1589948d4f21SToshiaki Makita 		return -EINVAL;
1590948d4f21SToshiaki Makita 	}
1591948d4f21SToshiaki Makita }
1592948d4f21SToshiaki Makita 
15934456e7bdSStephen Hemminger static const struct net_device_ops veth_netdev_ops = {
15944456e7bdSStephen Hemminger 	.ndo_init            = veth_dev_init,
15954456e7bdSStephen Hemminger 	.ndo_open            = veth_open,
15962cf48a10SEric W. Biederman 	.ndo_stop            = veth_close,
159700829823SStephen Hemminger 	.ndo_start_xmit      = veth_xmit,
15986311cc44Sstephen hemminger 	.ndo_get_stats64     = veth_get_stats64,
15995c70ef85SGao feng 	.ndo_set_rx_mode     = veth_set_multicast_list,
1600ee923623SDaniel Lezcano 	.ndo_set_mac_address = eth_mac_addr,
1601bb446c19SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
1602bb446c19SWANG Cong 	.ndo_poll_controller	= veth_poll_controller,
1603bb446c19SWANG Cong #endif
1604a45253bfSNicolas Dichtel 	.ndo_get_iflink		= veth_get_iflink,
1605dc224822SToshiaki Makita 	.ndo_fix_features	= veth_fix_features,
1606d3256efdSPaolo Abeni 	.ndo_set_features	= veth_set_features,
16071a04a821SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
1608163e5292SPaolo Abeni 	.ndo_set_rx_headroom	= veth_set_rx_headroom,
1609948d4f21SToshiaki Makita 	.ndo_bpf		= veth_xdp,
16109152cff0SLorenzo Bianconi 	.ndo_xdp_xmit		= veth_ndo_xdp_xmit,
16119aa1206eSDaniel Borkmann 	.ndo_get_peer_dev	= veth_peer_dev,
16124456e7bdSStephen Hemminger };
16134456e7bdSStephen Hemminger 
1614732912d7SAlexander Duyck #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1615c80fafbbSXin Long 		       NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1616732912d7SAlexander Duyck 		       NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
161728d2b136SPatrick McHardy 		       NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
161828d2b136SPatrick McHardy 		       NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
16198093315aSEric Dumazet 
1620e314dbdcSPavel Emelyanov static void veth_setup(struct net_device *dev)
1621e314dbdcSPavel Emelyanov {
1622e314dbdcSPavel Emelyanov 	ether_setup(dev);
1623e314dbdcSPavel Emelyanov 
1624550fd08cSNeil Horman 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
162523ea5a96SHannes Frederic Sowa 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
162602f01ec1SPhil Sutter 	dev->priv_flags |= IFF_NO_QUEUE;
1627163e5292SPaolo Abeni 	dev->priv_flags |= IFF_PHONY_HEADROOM;
1628550fd08cSNeil Horman 
16294456e7bdSStephen Hemminger 	dev->netdev_ops = &veth_netdev_ops;
1630e314dbdcSPavel Emelyanov 	dev->ethtool_ops = &veth_ethtool_ops;
1631e314dbdcSPavel Emelyanov 	dev->features |= NETIF_F_LLTX;
16328093315aSEric Dumazet 	dev->features |= VETH_FEATURES;
16338d0d21f4SToshiaki Makita 	dev->vlan_features = dev->features &
16343f8c707bSVlad Yasevich 			     ~(NETIF_F_HW_VLAN_CTAG_TX |
16353f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_STAG_TX |
16363f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_CTAG_RX |
16373f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_STAG_RX);
1638cf124db5SDavid S. Miller 	dev->needs_free_netdev = true;
1639cf124db5SDavid S. Miller 	dev->priv_destructor = veth_dev_free;
164091572088SJarod Wilson 	dev->max_mtu = ETH_MAX_MTU;
1641a2c725faSMichał Mirosław 
16428093315aSEric Dumazet 	dev->hw_features = VETH_FEATURES;
164382d81898SEric Dumazet 	dev->hw_enc_features = VETH_FEATURES;
1644607fca9aSDavid Ahern 	dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1645e314dbdcSPavel Emelyanov }
1646e314dbdcSPavel Emelyanov 
1647e314dbdcSPavel Emelyanov /*
1648e314dbdcSPavel Emelyanov  * netlink interface
1649e314dbdcSPavel Emelyanov  */
1650e314dbdcSPavel Emelyanov 
1651a8b8a889SMatthias Schiffer static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1652a8b8a889SMatthias Schiffer 			 struct netlink_ext_ack *extack)
1653e314dbdcSPavel Emelyanov {
1654e314dbdcSPavel Emelyanov 	if (tb[IFLA_ADDRESS]) {
1655e314dbdcSPavel Emelyanov 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1656e314dbdcSPavel Emelyanov 			return -EINVAL;
1657e314dbdcSPavel Emelyanov 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1658e314dbdcSPavel Emelyanov 			return -EADDRNOTAVAIL;
1659e314dbdcSPavel Emelyanov 	}
166038d40815SEric Biederman 	if (tb[IFLA_MTU]) {
166138d40815SEric Biederman 		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
166238d40815SEric Biederman 			return -EINVAL;
166338d40815SEric Biederman 	}
1664e314dbdcSPavel Emelyanov 	return 0;
1665e314dbdcSPavel Emelyanov }
1666e314dbdcSPavel Emelyanov 
1667e314dbdcSPavel Emelyanov static struct rtnl_link_ops veth_link_ops;
1668e314dbdcSPavel Emelyanov 
1669d3256efdSPaolo Abeni static void veth_disable_gro(struct net_device *dev)
1670d3256efdSPaolo Abeni {
1671d3256efdSPaolo Abeni 	dev->features &= ~NETIF_F_GRO;
1672d3256efdSPaolo Abeni 	dev->wanted_features &= ~NETIF_F_GRO;
1673d3256efdSPaolo Abeni 	netdev_update_features(dev);
1674d3256efdSPaolo Abeni }
1675d3256efdSPaolo Abeni 
16769d3684c2SPaolo Abeni static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
16779d3684c2SPaolo Abeni {
16789d3684c2SPaolo Abeni 	int err;
16799d3684c2SPaolo Abeni 
16809d3684c2SPaolo Abeni 	if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
16819d3684c2SPaolo Abeni 		err = netif_set_real_num_tx_queues(dev, 1);
16829d3684c2SPaolo Abeni 		if (err)
16839d3684c2SPaolo Abeni 			return err;
16849d3684c2SPaolo Abeni 	}
16859d3684c2SPaolo Abeni 	if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
16869d3684c2SPaolo Abeni 		err = netif_set_real_num_rx_queues(dev, 1);
16879d3684c2SPaolo Abeni 		if (err)
16889d3684c2SPaolo Abeni 			return err;
16899d3684c2SPaolo Abeni 	}
16909d3684c2SPaolo Abeni 	return 0;
16919d3684c2SPaolo Abeni }
16929d3684c2SPaolo Abeni 
169381adee47SEric W. Biederman static int veth_newlink(struct net *src_net, struct net_device *dev,
16947a3f4a18SMatthias Schiffer 			struct nlattr *tb[], struct nlattr *data[],
16957a3f4a18SMatthias Schiffer 			struct netlink_ext_ack *extack)
1696e314dbdcSPavel Emelyanov {
16977797b93bSToshiaki Makita 	int err;
1698e314dbdcSPavel Emelyanov 	struct net_device *peer;
1699e314dbdcSPavel Emelyanov 	struct veth_priv *priv;
1700e314dbdcSPavel Emelyanov 	char ifname[IFNAMSIZ];
1701e314dbdcSPavel Emelyanov 	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
17025517750fSTom Gundersen 	unsigned char name_assign_type;
17033729d502SPatrick McHardy 	struct ifinfomsg *ifmp;
170481adee47SEric W. Biederman 	struct net *net;
1705e314dbdcSPavel Emelyanov 
1706e314dbdcSPavel Emelyanov 	/*
1707e314dbdcSPavel Emelyanov 	 * create and register peer first
1708e314dbdcSPavel Emelyanov 	 */
1709e314dbdcSPavel Emelyanov 	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1710e314dbdcSPavel Emelyanov 		struct nlattr *nla_peer;
1711e314dbdcSPavel Emelyanov 
1712e314dbdcSPavel Emelyanov 		nla_peer = data[VETH_INFO_PEER];
17133729d502SPatrick McHardy 		ifmp = nla_data(nla_peer);
1714f7b12606SJiri Pirko 		err = rtnl_nla_parse_ifla(peer_tb,
1715e314dbdcSPavel Emelyanov 					  nla_data(nla_peer) + sizeof(struct ifinfomsg),
1716fceb6435SJohannes Berg 					  nla_len(nla_peer) - sizeof(struct ifinfomsg),
1717fceb6435SJohannes Berg 					  NULL);
1718e314dbdcSPavel Emelyanov 		if (err < 0)
1719e314dbdcSPavel Emelyanov 			return err;
1720e314dbdcSPavel Emelyanov 
1721a8b8a889SMatthias Schiffer 		err = veth_validate(peer_tb, NULL, extack);
1722e314dbdcSPavel Emelyanov 		if (err < 0)
1723e314dbdcSPavel Emelyanov 			return err;
1724e314dbdcSPavel Emelyanov 
1725e314dbdcSPavel Emelyanov 		tbp = peer_tb;
17263729d502SPatrick McHardy 	} else {
17273729d502SPatrick McHardy 		ifmp = NULL;
1728e314dbdcSPavel Emelyanov 		tbp = tb;
17293729d502SPatrick McHardy 	}
1730e314dbdcSPavel Emelyanov 
1731191cdb38SSerhey Popovych 	if (ifmp && tbp[IFLA_IFNAME]) {
1732872f6903SFrancis Laniel 		nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
17335517750fSTom Gundersen 		name_assign_type = NET_NAME_USER;
17345517750fSTom Gundersen 	} else {
1735e314dbdcSPavel Emelyanov 		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
17365517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
17375517750fSTom Gundersen 	}
1738e314dbdcSPavel Emelyanov 
173981adee47SEric W. Biederman 	net = rtnl_link_get_net(src_net, tbp);
174081adee47SEric W. Biederman 	if (IS_ERR(net))
174181adee47SEric W. Biederman 		return PTR_ERR(net);
174281adee47SEric W. Biederman 
17435517750fSTom Gundersen 	peer = rtnl_create_link(net, ifname, name_assign_type,
1744d0522f1cSDavid Ahern 				&veth_link_ops, tbp, extack);
174581adee47SEric W. Biederman 	if (IS_ERR(peer)) {
174681adee47SEric W. Biederman 		put_net(net);
1747e314dbdcSPavel Emelyanov 		return PTR_ERR(peer);
174881adee47SEric W. Biederman 	}
1749e314dbdcSPavel Emelyanov 
1750191cdb38SSerhey Popovych 	if (!ifmp || !tbp[IFLA_ADDRESS])
1751f2cedb63SDanny Kukawka 		eth_hw_addr_random(peer);
1752e314dbdcSPavel Emelyanov 
1753e6f8f1a7SPavel Emelyanov 	if (ifmp && (dev->ifindex != 0))
1754e6f8f1a7SPavel Emelyanov 		peer->ifindex = ifmp->ifi_index;
1755e6f8f1a7SPavel Emelyanov 
17564b66d216SEric Dumazet 	netif_set_gso_max_size(peer, dev->gso_max_size);
17576d872df3SEric Dumazet 	netif_set_gso_max_segs(peer, dev->gso_max_segs);
175872d24955SStephen Hemminger 
1759e314dbdcSPavel Emelyanov 	err = register_netdevice(peer);
176081adee47SEric W. Biederman 	put_net(net);
176181adee47SEric W. Biederman 	net = NULL;
1762e314dbdcSPavel Emelyanov 	if (err < 0)
1763e314dbdcSPavel Emelyanov 		goto err_register_peer;
1764e314dbdcSPavel Emelyanov 
1765d3256efdSPaolo Abeni 	/* keep GRO disabled by default to be consistent with the established
1766d3256efdSPaolo Abeni 	 * veth behavior
1767d3256efdSPaolo Abeni 	 */
1768d3256efdSPaolo Abeni 	veth_disable_gro(peer);
1769e314dbdcSPavel Emelyanov 	netif_carrier_off(peer);
1770e314dbdcSPavel Emelyanov 
17713729d502SPatrick McHardy 	err = rtnl_configure_link(peer, ifmp);
17723729d502SPatrick McHardy 	if (err < 0)
17733729d502SPatrick McHardy 		goto err_configure_peer;
17743729d502SPatrick McHardy 
1775e314dbdcSPavel Emelyanov 	/*
1776e314dbdcSPavel Emelyanov 	 * register dev last
1777e314dbdcSPavel Emelyanov 	 *
1778e314dbdcSPavel Emelyanov 	 * note, that since we've registered new device the dev's name
1779e314dbdcSPavel Emelyanov 	 * should be re-allocated
1780e314dbdcSPavel Emelyanov 	 */
1781e314dbdcSPavel Emelyanov 
1782e314dbdcSPavel Emelyanov 	if (tb[IFLA_ADDRESS] == NULL)
1783f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1784e314dbdcSPavel Emelyanov 
17856c8c4446SJiri Pirko 	if (tb[IFLA_IFNAME])
1786872f6903SFrancis Laniel 		nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
17876c8c4446SJiri Pirko 	else
17886c8c4446SJiri Pirko 		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
17896c8c4446SJiri Pirko 
1790e314dbdcSPavel Emelyanov 	err = register_netdevice(dev);
1791e314dbdcSPavel Emelyanov 	if (err < 0)
1792e314dbdcSPavel Emelyanov 		goto err_register_dev;
1793e314dbdcSPavel Emelyanov 
1794e314dbdcSPavel Emelyanov 	netif_carrier_off(dev);
1795e314dbdcSPavel Emelyanov 
1796e314dbdcSPavel Emelyanov 	/*
1797e314dbdcSPavel Emelyanov 	 * tie the deviced together
1798e314dbdcSPavel Emelyanov 	 */
1799e314dbdcSPavel Emelyanov 
1800e314dbdcSPavel Emelyanov 	priv = netdev_priv(dev);
1801d0e2c55eSEric Dumazet 	rcu_assign_pointer(priv->peer, peer);
18029d3684c2SPaolo Abeni 	err = veth_init_queues(dev, tb);
18039d3684c2SPaolo Abeni 	if (err)
18049d3684c2SPaolo Abeni 		goto err_queues;
1805e314dbdcSPavel Emelyanov 
1806e314dbdcSPavel Emelyanov 	priv = netdev_priv(peer);
1807d0e2c55eSEric Dumazet 	rcu_assign_pointer(priv->peer, dev);
18089d3684c2SPaolo Abeni 	err = veth_init_queues(peer, tb);
18099d3684c2SPaolo Abeni 	if (err)
18109d3684c2SPaolo Abeni 		goto err_queues;
1811948d4f21SToshiaki Makita 
1812d3256efdSPaolo Abeni 	veth_disable_gro(dev);
1813e314dbdcSPavel Emelyanov 	return 0;
1814e314dbdcSPavel Emelyanov 
18159d3684c2SPaolo Abeni err_queues:
18169d3684c2SPaolo Abeni 	unregister_netdevice(dev);
1817e314dbdcSPavel Emelyanov err_register_dev:
1818e314dbdcSPavel Emelyanov 	/* nothing to do */
18193729d502SPatrick McHardy err_configure_peer:
1820e314dbdcSPavel Emelyanov 	unregister_netdevice(peer);
1821e314dbdcSPavel Emelyanov 	return err;
1822e314dbdcSPavel Emelyanov 
1823e314dbdcSPavel Emelyanov err_register_peer:
1824e314dbdcSPavel Emelyanov 	free_netdev(peer);
1825e314dbdcSPavel Emelyanov 	return err;
1826e314dbdcSPavel Emelyanov }
1827e314dbdcSPavel Emelyanov 
182823289a37SEric Dumazet static void veth_dellink(struct net_device *dev, struct list_head *head)
1829e314dbdcSPavel Emelyanov {
1830e314dbdcSPavel Emelyanov 	struct veth_priv *priv;
1831e314dbdcSPavel Emelyanov 	struct net_device *peer;
1832e314dbdcSPavel Emelyanov 
1833e314dbdcSPavel Emelyanov 	priv = netdev_priv(dev);
1834d0e2c55eSEric Dumazet 	peer = rtnl_dereference(priv->peer);
1835d0e2c55eSEric Dumazet 
1836d0e2c55eSEric Dumazet 	/* Note : dellink() is called from default_device_exit_batch(),
1837d0e2c55eSEric Dumazet 	 * before a rcu_synchronize() point. The devices are guaranteed
1838d0e2c55eSEric Dumazet 	 * not being freed before one RCU grace period.
1839d0e2c55eSEric Dumazet 	 */
1840d0e2c55eSEric Dumazet 	RCU_INIT_POINTER(priv->peer, NULL);
1841f45a5c26SEric Dumazet 	unregister_netdevice_queue(dev, head);
1842d0e2c55eSEric Dumazet 
1843f45a5c26SEric Dumazet 	if (peer) {
1844d0e2c55eSEric Dumazet 		priv = netdev_priv(peer);
1845d0e2c55eSEric Dumazet 		RCU_INIT_POINTER(priv->peer, NULL);
184624540535SEric Dumazet 		unregister_netdevice_queue(peer, head);
1847e314dbdcSPavel Emelyanov 	}
1848f45a5c26SEric Dumazet }
1849e314dbdcSPavel Emelyanov 
185023711438SThomas Graf static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
185123711438SThomas Graf 	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
185223711438SThomas Graf };
1853e314dbdcSPavel Emelyanov 
1854e5f4e7b9SNicolas Dichtel static struct net *veth_get_link_net(const struct net_device *dev)
1855e5f4e7b9SNicolas Dichtel {
1856e5f4e7b9SNicolas Dichtel 	struct veth_priv *priv = netdev_priv(dev);
1857e5f4e7b9SNicolas Dichtel 	struct net_device *peer = rtnl_dereference(priv->peer);
1858e5f4e7b9SNicolas Dichtel 
1859e5f4e7b9SNicolas Dichtel 	return peer ? dev_net(peer) : dev_net(dev);
1860e5f4e7b9SNicolas Dichtel }
1861e5f4e7b9SNicolas Dichtel 
18629d3684c2SPaolo Abeni static unsigned int veth_get_num_queues(void)
18639d3684c2SPaolo Abeni {
18649d3684c2SPaolo Abeni 	/* enforce the same queue limit as rtnl_create_link */
18659d3684c2SPaolo Abeni 	int queues = num_possible_cpus();
18669d3684c2SPaolo Abeni 
18679d3684c2SPaolo Abeni 	if (queues > 4096)
18689d3684c2SPaolo Abeni 		queues = 4096;
18699d3684c2SPaolo Abeni 	return queues;
18709d3684c2SPaolo Abeni }
18719d3684c2SPaolo Abeni 
1872e314dbdcSPavel Emelyanov static struct rtnl_link_ops veth_link_ops = {
1873e314dbdcSPavel Emelyanov 	.kind		= DRV_NAME,
1874e314dbdcSPavel Emelyanov 	.priv_size	= sizeof(struct veth_priv),
1875e314dbdcSPavel Emelyanov 	.setup		= veth_setup,
1876e314dbdcSPavel Emelyanov 	.validate	= veth_validate,
1877e314dbdcSPavel Emelyanov 	.newlink	= veth_newlink,
1878e314dbdcSPavel Emelyanov 	.dellink	= veth_dellink,
1879e314dbdcSPavel Emelyanov 	.policy		= veth_policy,
1880e314dbdcSPavel Emelyanov 	.maxtype	= VETH_INFO_MAX,
1881e5f4e7b9SNicolas Dichtel 	.get_link_net	= veth_get_link_net,
18829d3684c2SPaolo Abeni 	.get_num_tx_queues	= veth_get_num_queues,
18839d3684c2SPaolo Abeni 	.get_num_rx_queues	= veth_get_num_queues,
1884e314dbdcSPavel Emelyanov };
1885e314dbdcSPavel Emelyanov 
1886e314dbdcSPavel Emelyanov /*
1887e314dbdcSPavel Emelyanov  * init/fini
1888e314dbdcSPavel Emelyanov  */
1889e314dbdcSPavel Emelyanov 
1890e314dbdcSPavel Emelyanov static __init int veth_init(void)
1891e314dbdcSPavel Emelyanov {
1892e314dbdcSPavel Emelyanov 	return rtnl_link_register(&veth_link_ops);
1893e314dbdcSPavel Emelyanov }
1894e314dbdcSPavel Emelyanov 
1895e314dbdcSPavel Emelyanov static __exit void veth_exit(void)
1896e314dbdcSPavel Emelyanov {
189768365458SPatrick McHardy 	rtnl_link_unregister(&veth_link_ops);
1898e314dbdcSPavel Emelyanov }
1899e314dbdcSPavel Emelyanov 
1900e314dbdcSPavel Emelyanov module_init(veth_init);
1901e314dbdcSPavel Emelyanov module_exit(veth_exit);
1902e314dbdcSPavel Emelyanov 
1903e314dbdcSPavel Emelyanov MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1904e314dbdcSPavel Emelyanov MODULE_LICENSE("GPL v2");
1905e314dbdcSPavel Emelyanov MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1906