xref: /openbmc/linux/drivers/net/veth.c (revision a7b862ab)
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>
29a9ca9f9cSYunsheng Lin #include <net/page_pool/helpers.h>
30e314dbdcSPavel Emelyanov 
31e314dbdcSPavel Emelyanov #define DRV_NAME	"veth"
32e314dbdcSPavel Emelyanov #define DRV_VERSION	"1.0"
33e314dbdcSPavel Emelyanov 
349fc8d518SToshiaki Makita #define VETH_XDP_FLAG		BIT(0)
35948d4f21SToshiaki Makita #define VETH_RING_SIZE		256
36948d4f21SToshiaki Makita #define VETH_XDP_HEADROOM	(XDP_PACKET_HEADROOM + NET_IP_ALIGN)
37948d4f21SToshiaki Makita 
389cda7807SToshiaki Makita #define VETH_XDP_TX_BULK_SIZE	16
3965e6dcf7SLorenzo Bianconi #define VETH_XDP_BATCH		16
409cda7807SToshiaki Makita 
4165780c56SLorenzo Bianconi struct veth_stats {
421c5b82e5SLorenzo Bianconi 	u64	rx_drops;
431c5b82e5SLorenzo Bianconi 	/* xdp */
444195e54aSToshiaki Makita 	u64	xdp_packets;
454195e54aSToshiaki Makita 	u64	xdp_bytes;
461c5b82e5SLorenzo Bianconi 	u64	xdp_redirect;
474195e54aSToshiaki Makita 	u64	xdp_drops;
481c5b82e5SLorenzo Bianconi 	u64	xdp_tx;
499152cff0SLorenzo Bianconi 	u64	xdp_tx_err;
505fe6e567SLorenzo Bianconi 	u64	peer_tq_xdp_xmit;
515fe6e567SLorenzo Bianconi 	u64	peer_tq_xdp_xmit_err;
5265780c56SLorenzo Bianconi };
5365780c56SLorenzo Bianconi 
5465780c56SLorenzo Bianconi struct veth_rq_stats {
5565780c56SLorenzo Bianconi 	struct veth_stats	vs;
564195e54aSToshiaki Makita 	struct u64_stats_sync	syncp;
574195e54aSToshiaki Makita };
584195e54aSToshiaki Makita 
59638264dcSToshiaki Makita struct veth_rq {
60948d4f21SToshiaki Makita 	struct napi_struct	xdp_napi;
61d3256efdSPaolo Abeni 	struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
62948d4f21SToshiaki Makita 	struct net_device	*dev;
63948d4f21SToshiaki Makita 	struct bpf_prog __rcu	*xdp_prog;
64d1396004SToshiaki Makita 	struct xdp_mem_info	xdp_mem;
654195e54aSToshiaki Makita 	struct veth_rq_stats	stats;
66948d4f21SToshiaki Makita 	bool			rx_notify_masked;
67948d4f21SToshiaki Makita 	struct ptr_ring		xdp_ring;
68948d4f21SToshiaki Makita 	struct xdp_rxq_info	xdp_rxq;
690ebab78cSLorenzo Bianconi 	struct page_pool	*page_pool;
70e314dbdcSPavel Emelyanov };
71e314dbdcSPavel Emelyanov 
72638264dcSToshiaki Makita struct veth_priv {
73638264dcSToshiaki Makita 	struct net_device __rcu	*peer;
74638264dcSToshiaki Makita 	atomic64_t		dropped;
75638264dcSToshiaki Makita 	struct bpf_prog		*_xdp_prog;
76638264dcSToshiaki Makita 	struct veth_rq		*rq;
77638264dcSToshiaki Makita 	unsigned int		requested_headroom;
78638264dcSToshiaki Makita };
79638264dcSToshiaki Makita 
809cda7807SToshiaki Makita struct veth_xdp_tx_bq {
819cda7807SToshiaki Makita 	struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
829cda7807SToshiaki Makita 	unsigned int count;
839cda7807SToshiaki Makita };
849cda7807SToshiaki Makita 
85e314dbdcSPavel Emelyanov /*
86e314dbdcSPavel Emelyanov  * ethtool interface
87e314dbdcSPavel Emelyanov  */
88e314dbdcSPavel Emelyanov 
89d397b968SToshiaki Makita struct veth_q_stat_desc {
90d397b968SToshiaki Makita 	char	desc[ETH_GSTRING_LEN];
91d397b968SToshiaki Makita 	size_t	offset;
92d397b968SToshiaki Makita };
93d397b968SToshiaki Makita 
9465780c56SLorenzo Bianconi #define VETH_RQ_STAT(m)	offsetof(struct veth_stats, m)
95d397b968SToshiaki Makita 
96d397b968SToshiaki Makita static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
97d397b968SToshiaki Makita 	{ "xdp_packets",	VETH_RQ_STAT(xdp_packets) },
98d397b968SToshiaki Makita 	{ "xdp_bytes",		VETH_RQ_STAT(xdp_bytes) },
995fe6e567SLorenzo Bianconi 	{ "drops",		VETH_RQ_STAT(rx_drops) },
1005fe6e567SLorenzo Bianconi 	{ "xdp_redirect",	VETH_RQ_STAT(xdp_redirect) },
1015fe6e567SLorenzo Bianconi 	{ "xdp_drops",		VETH_RQ_STAT(xdp_drops) },
1025fe6e567SLorenzo Bianconi 	{ "xdp_tx",		VETH_RQ_STAT(xdp_tx) },
1035fe6e567SLorenzo Bianconi 	{ "xdp_tx_errors",	VETH_RQ_STAT(xdp_tx_err) },
104d397b968SToshiaki Makita };
105d397b968SToshiaki Makita 
106d397b968SToshiaki Makita #define VETH_RQ_STATS_LEN	ARRAY_SIZE(veth_rq_stats_desc)
107d397b968SToshiaki Makita 
1085fe6e567SLorenzo Bianconi static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
1095fe6e567SLorenzo Bianconi 	{ "xdp_xmit",		VETH_RQ_STAT(peer_tq_xdp_xmit) },
1105fe6e567SLorenzo Bianconi 	{ "xdp_xmit_errors",	VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
1115fe6e567SLorenzo Bianconi };
1125fe6e567SLorenzo Bianconi 
1135fe6e567SLorenzo Bianconi #define VETH_TQ_STATS_LEN	ARRAY_SIZE(veth_tq_stats_desc)
1145fe6e567SLorenzo Bianconi 
115e314dbdcSPavel Emelyanov static struct {
116e314dbdcSPavel Emelyanov 	const char string[ETH_GSTRING_LEN];
117e314dbdcSPavel Emelyanov } ethtool_stats_keys[] = {
118e314dbdcSPavel Emelyanov 	{ "peer_ifindex" },
119e314dbdcSPavel Emelyanov };
120e314dbdcSPavel Emelyanov 
121fefb695aSStanislav Fomichev struct veth_xdp_buff {
122fefb695aSStanislav Fomichev 	struct xdp_buff xdp;
123306531f0SStanislav Fomichev 	struct sk_buff *skb;
124fefb695aSStanislav Fomichev };
125fefb695aSStanislav Fomichev 
veth_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)12656607b98SPhilippe Reynes static int veth_get_link_ksettings(struct net_device *dev,
12756607b98SPhilippe Reynes 				   struct ethtool_link_ksettings *cmd)
128e314dbdcSPavel Emelyanov {
12956607b98SPhilippe Reynes 	cmd->base.speed		= SPEED_10000;
13056607b98SPhilippe Reynes 	cmd->base.duplex	= DUPLEX_FULL;
13156607b98SPhilippe Reynes 	cmd->base.port		= PORT_TP;
13256607b98SPhilippe Reynes 	cmd->base.autoneg	= AUTONEG_DISABLE;
133e314dbdcSPavel Emelyanov 	return 0;
134e314dbdcSPavel Emelyanov }
135e314dbdcSPavel Emelyanov 
veth_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)136e314dbdcSPavel Emelyanov static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
137e314dbdcSPavel Emelyanov {
138fb3ceec1SWolfram Sang 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
139fb3ceec1SWolfram Sang 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
140e314dbdcSPavel Emelyanov }
141e314dbdcSPavel Emelyanov 
veth_get_strings(struct net_device * dev,u32 stringset,u8 * buf)142e314dbdcSPavel Emelyanov static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
143e314dbdcSPavel Emelyanov {
144a0341b73STonghao Zhang 	u8 *p = buf;
145d397b968SToshiaki Makita 	int i, j;
146d397b968SToshiaki Makita 
147e314dbdcSPavel Emelyanov 	switch(stringset) {
148e314dbdcSPavel Emelyanov 	case ETH_SS_STATS:
149d397b968SToshiaki Makita 		memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
150d397b968SToshiaki Makita 		p += sizeof(ethtool_stats_keys);
151a0341b73STonghao Zhang 		for (i = 0; i < dev->real_num_rx_queues; i++)
152a0341b73STonghao Zhang 			for (j = 0; j < VETH_RQ_STATS_LEN; j++)
153a0341b73STonghao Zhang 				ethtool_sprintf(&p, "rx_queue_%u_%.18s",
154d397b968SToshiaki Makita 						i, veth_rq_stats_desc[j].desc);
155a0341b73STonghao Zhang 
156a0341b73STonghao Zhang 		for (i = 0; i < dev->real_num_tx_queues; i++)
157a0341b73STonghao Zhang 			for (j = 0; j < VETH_TQ_STATS_LEN; j++)
158a0341b73STonghao Zhang 				ethtool_sprintf(&p, "tx_queue_%u_%.18s",
1595fe6e567SLorenzo Bianconi 						i, veth_tq_stats_desc[j].desc);
1604fc41805SLorenzo Bianconi 
1614fc41805SLorenzo Bianconi 		page_pool_ethtool_stats_get_strings(p);
162e314dbdcSPavel Emelyanov 		break;
163e314dbdcSPavel Emelyanov 	}
164e314dbdcSPavel Emelyanov }
165e314dbdcSPavel Emelyanov 
veth_get_sset_count(struct net_device * dev,int sset)166b9f2c044SJeff Garzik static int veth_get_sset_count(struct net_device *dev, int sset)
167e314dbdcSPavel Emelyanov {
168b9f2c044SJeff Garzik 	switch (sset) {
169b9f2c044SJeff Garzik 	case ETH_SS_STATS:
170d397b968SToshiaki Makita 		return ARRAY_SIZE(ethtool_stats_keys) +
1715fe6e567SLorenzo Bianconi 		       VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
1724fc41805SLorenzo Bianconi 		       VETH_TQ_STATS_LEN * dev->real_num_tx_queues +
1734fc41805SLorenzo Bianconi 		       page_pool_ethtool_stats_get_count();
174b9f2c044SJeff Garzik 	default:
175b9f2c044SJeff Garzik 		return -EOPNOTSUPP;
176b9f2c044SJeff Garzik 	}
177e314dbdcSPavel Emelyanov }
178e314dbdcSPavel Emelyanov 
veth_get_page_pool_stats(struct net_device * dev,u64 * data)1795e316a81SLorenzo Bianconi static void veth_get_page_pool_stats(struct net_device *dev, u64 *data)
1805e316a81SLorenzo Bianconi {
1815e316a81SLorenzo Bianconi #ifdef CONFIG_PAGE_POOL_STATS
1825e316a81SLorenzo Bianconi 	struct veth_priv *priv = netdev_priv(dev);
1835e316a81SLorenzo Bianconi 	struct page_pool_stats pp_stats = {};
1845e316a81SLorenzo Bianconi 	int i;
1855e316a81SLorenzo Bianconi 
1865e316a81SLorenzo Bianconi 	for (i = 0; i < dev->real_num_rx_queues; i++) {
1875e316a81SLorenzo Bianconi 		if (!priv->rq[i].page_pool)
1885e316a81SLorenzo Bianconi 			continue;
1895e316a81SLorenzo Bianconi 		page_pool_get_stats(priv->rq[i].page_pool, &pp_stats);
1905e316a81SLorenzo Bianconi 	}
1915e316a81SLorenzo Bianconi 	page_pool_ethtool_stats_get(data, &pp_stats);
1925e316a81SLorenzo Bianconi #endif /* CONFIG_PAGE_POOL_STATS */
1935e316a81SLorenzo Bianconi }
1945e316a81SLorenzo Bianconi 
veth_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)195e314dbdcSPavel Emelyanov static void veth_get_ethtool_stats(struct net_device *dev,
196e314dbdcSPavel Emelyanov 		struct ethtool_stats *stats, u64 *data)
197e314dbdcSPavel Emelyanov {
1985fe6e567SLorenzo Bianconi 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
199d0e2c55eSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
2004fc41805SLorenzo Bianconi 	int i, j, idx, pp_idx;
201e314dbdcSPavel Emelyanov 
202d0e2c55eSEric Dumazet 	data[0] = peer ? peer->ifindex : 0;
203d397b968SToshiaki Makita 	idx = 1;
204d397b968SToshiaki Makita 	for (i = 0; i < dev->real_num_rx_queues; i++) {
205d397b968SToshiaki Makita 		const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
20665780c56SLorenzo Bianconi 		const void *stats_base = (void *)&rq_stats->vs;
207d397b968SToshiaki Makita 		unsigned int start;
208d397b968SToshiaki Makita 		size_t offset;
209d397b968SToshiaki Makita 
210d397b968SToshiaki Makita 		do {
211068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq_stats->syncp);
212d397b968SToshiaki Makita 			for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
213d397b968SToshiaki Makita 				offset = veth_rq_stats_desc[j].offset;
214d397b968SToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
215d397b968SToshiaki Makita 			}
216068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq_stats->syncp, start));
217d397b968SToshiaki Makita 		idx += VETH_RQ_STATS_LEN;
218d397b968SToshiaki Makita 	}
2194fc41805SLorenzo Bianconi 	pp_idx = idx;
2205fe6e567SLorenzo Bianconi 
2215fe6e567SLorenzo Bianconi 	if (!peer)
2224fc41805SLorenzo Bianconi 		goto page_pool_stats;
2235fe6e567SLorenzo Bianconi 
2245fe6e567SLorenzo Bianconi 	rcv_priv = netdev_priv(peer);
2255fe6e567SLorenzo Bianconi 	for (i = 0; i < peer->real_num_rx_queues; i++) {
2265fe6e567SLorenzo Bianconi 		const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
2275fe6e567SLorenzo Bianconi 		const void *base = (void *)&rq_stats->vs;
2285fe6e567SLorenzo Bianconi 		unsigned int start, tx_idx = idx;
2295fe6e567SLorenzo Bianconi 		size_t offset;
2305fe6e567SLorenzo Bianconi 
2315fe6e567SLorenzo Bianconi 		tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
2325fe6e567SLorenzo Bianconi 		do {
233068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq_stats->syncp);
2345fe6e567SLorenzo Bianconi 			for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
2355fe6e567SLorenzo Bianconi 				offset = veth_tq_stats_desc[j].offset;
2365fe6e567SLorenzo Bianconi 				data[tx_idx + j] += *(u64 *)(base + offset);
2375fe6e567SLorenzo Bianconi 			}
238068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq_stats->syncp, start));
2395fe6e567SLorenzo Bianconi 	}
240db7b6161SLorenzo Bianconi 	pp_idx = idx + dev->real_num_tx_queues * VETH_TQ_STATS_LEN;
2414fc41805SLorenzo Bianconi 
2424fc41805SLorenzo Bianconi page_pool_stats:
2435e316a81SLorenzo Bianconi 	veth_get_page_pool_stats(dev, &data[pp_idx]);
244e314dbdcSPavel Emelyanov }
245e314dbdcSPavel Emelyanov 
veth_get_channels(struct net_device * dev,struct ethtool_channels * channels)24634829eecSMaciej Fijalkowski static void veth_get_channels(struct net_device *dev,
24734829eecSMaciej Fijalkowski 			      struct ethtool_channels *channels)
24834829eecSMaciej Fijalkowski {
24934829eecSMaciej Fijalkowski 	channels->tx_count = dev->real_num_tx_queues;
25034829eecSMaciej Fijalkowski 	channels->rx_count = dev->real_num_rx_queues;
2514752eeb3SPaolo Abeni 	channels->max_tx = dev->num_tx_queues;
2524752eeb3SPaolo Abeni 	channels->max_rx = dev->num_rx_queues;
25334829eecSMaciej Fijalkowski }
25434829eecSMaciej Fijalkowski 
2554752eeb3SPaolo Abeni static int veth_set_channels(struct net_device *dev,
2564752eeb3SPaolo Abeni 			     struct ethtool_channels *ch);
2574752eeb3SPaolo Abeni 
2580fc0b732SStephen Hemminger static const struct ethtool_ops veth_ethtool_ops = {
259e314dbdcSPavel Emelyanov 	.get_drvinfo		= veth_get_drvinfo,
260e314dbdcSPavel Emelyanov 	.get_link		= ethtool_op_get_link,
261e314dbdcSPavel Emelyanov 	.get_strings		= veth_get_strings,
262b9f2c044SJeff Garzik 	.get_sset_count		= veth_get_sset_count,
263e314dbdcSPavel Emelyanov 	.get_ethtool_stats	= veth_get_ethtool_stats,
26456607b98SPhilippe Reynes 	.get_link_ksettings	= veth_get_link_ksettings,
265056b21fbSJulian Wiedmann 	.get_ts_info		= ethtool_op_get_ts_info,
26634829eecSMaciej Fijalkowski 	.get_channels		= veth_get_channels,
2674752eeb3SPaolo Abeni 	.set_channels		= veth_set_channels,
268e314dbdcSPavel Emelyanov };
269e314dbdcSPavel Emelyanov 
270948d4f21SToshiaki Makita /* general routines */
271948d4f21SToshiaki Makita 
veth_is_xdp_frame(void * ptr)2729fc8d518SToshiaki Makita static bool veth_is_xdp_frame(void *ptr)
2739fc8d518SToshiaki Makita {
2749fc8d518SToshiaki Makita 	return (unsigned long)ptr & VETH_XDP_FLAG;
2759fc8d518SToshiaki Makita }
2769fc8d518SToshiaki Makita 
veth_ptr_to_xdp(void * ptr)277defcffebSMaciej Żenczykowski static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
2789fc8d518SToshiaki Makita {
2799fc8d518SToshiaki Makita 	return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
2809fc8d518SToshiaki Makita }
2819fc8d518SToshiaki Makita 
veth_xdp_to_ptr(struct xdp_frame * xdp)282defcffebSMaciej Żenczykowski static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
283af87a3aaSToshiaki Makita {
284defcffebSMaciej Żenczykowski 	return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
285af87a3aaSToshiaki Makita }
286af87a3aaSToshiaki Makita 
veth_ptr_free(void * ptr)2879fc8d518SToshiaki Makita static void veth_ptr_free(void *ptr)
2889fc8d518SToshiaki Makita {
2899fc8d518SToshiaki Makita 	if (veth_is_xdp_frame(ptr))
2909fc8d518SToshiaki Makita 		xdp_return_frame(veth_ptr_to_xdp(ptr));
2919fc8d518SToshiaki Makita 	else
2929fc8d518SToshiaki Makita 		kfree_skb(ptr);
2939fc8d518SToshiaki Makita }
2949fc8d518SToshiaki Makita 
__veth_xdp_flush(struct veth_rq * rq)295638264dcSToshiaki Makita static void __veth_xdp_flush(struct veth_rq *rq)
296948d4f21SToshiaki Makita {
297948d4f21SToshiaki Makita 	/* Write ptr_ring before reading rx_notify_masked */
298948d4f21SToshiaki Makita 	smp_mb();
29968468d8cSEric Dumazet 	if (!READ_ONCE(rq->rx_notify_masked) &&
30068468d8cSEric Dumazet 	    napi_schedule_prep(&rq->xdp_napi)) {
30168468d8cSEric Dumazet 		WRITE_ONCE(rq->rx_notify_masked, true);
30268468d8cSEric Dumazet 		__napi_schedule(&rq->xdp_napi);
303948d4f21SToshiaki Makita 	}
304948d4f21SToshiaki Makita }
305948d4f21SToshiaki Makita 
veth_xdp_rx(struct veth_rq * rq,struct sk_buff * skb)306638264dcSToshiaki Makita static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
307948d4f21SToshiaki Makita {
308638264dcSToshiaki Makita 	if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
309948d4f21SToshiaki Makita 		dev_kfree_skb_any(skb);
310948d4f21SToshiaki Makita 		return NET_RX_DROP;
311948d4f21SToshiaki Makita 	}
312948d4f21SToshiaki Makita 
313948d4f21SToshiaki Makita 	return NET_RX_SUCCESS;
314948d4f21SToshiaki Makita }
315948d4f21SToshiaki Makita 
veth_forward_skb(struct net_device * dev,struct sk_buff * skb,struct veth_rq * rq,bool xdp)316638264dcSToshiaki Makita static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
317638264dcSToshiaki Makita 			    struct veth_rq *rq, bool xdp)
318e314dbdcSPavel Emelyanov {
319948d4f21SToshiaki Makita 	return __dev_forward_skb(dev, skb) ?: xdp ?
320638264dcSToshiaki Makita 		veth_xdp_rx(rq, skb) :
321baebdf48SSebastian Andrzej Siewior 		__netif_rx(skb);
322948d4f21SToshiaki Makita }
323948d4f21SToshiaki Makita 
32447e550e0SPaolo Abeni /* return true if the specified skb has chances of GRO aggregation
32547e550e0SPaolo Abeni  * Don't strive for accuracy, but try to avoid GRO overhead in the most
32647e550e0SPaolo Abeni  * common scenarios.
32747e550e0SPaolo Abeni  * When XDP is enabled, all traffic is considered eligible, as the xmit
32847e550e0SPaolo Abeni  * device has TSO off.
32947e550e0SPaolo Abeni  * When TSO is enabled on the xmit device, we are likely interested only
33047e550e0SPaolo Abeni  * in UDP aggregation, explicitly check for that if the skb is suspected
33147e550e0SPaolo Abeni  * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
33247e550e0SPaolo Abeni  * to belong to locally generated UDP traffic.
33347e550e0SPaolo Abeni  */
veth_skb_is_eligible_for_gro(const struct net_device * dev,const struct net_device * rcv,const struct sk_buff * skb)33447e550e0SPaolo Abeni static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
33547e550e0SPaolo Abeni 					 const struct net_device *rcv,
33647e550e0SPaolo Abeni 					 const struct sk_buff *skb)
33747e550e0SPaolo Abeni {
33847e550e0SPaolo Abeni 	return !(dev->features & NETIF_F_ALL_TSO) ||
33947e550e0SPaolo Abeni 		(skb->destructor == sock_wfree &&
34047e550e0SPaolo Abeni 		 rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
34147e550e0SPaolo Abeni }
34247e550e0SPaolo Abeni 
veth_xmit(struct sk_buff * skb,struct net_device * dev)343948d4f21SToshiaki Makita static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
344948d4f21SToshiaki Makita {
345948d4f21SToshiaki Makita 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
346638264dcSToshiaki Makita 	struct veth_rq *rq = NULL;
347151e887dSLiang Chen 	int ret = NETDEV_TX_OK;
348d0e2c55eSEric Dumazet 	struct net_device *rcv;
3492681128fSEric Dumazet 	int length = skb->len;
350d3256efdSPaolo Abeni 	bool use_napi = false;
351638264dcSToshiaki Makita 	int rxq;
352e314dbdcSPavel Emelyanov 
353d0e2c55eSEric Dumazet 	rcu_read_lock();
354d0e2c55eSEric Dumazet 	rcv = rcu_dereference(priv->peer);
355726e2c59SGuillaume Nault 	if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
356d0e2c55eSEric Dumazet 		kfree_skb(skb);
357d0e2c55eSEric Dumazet 		goto drop;
358d0e2c55eSEric Dumazet 	}
359e314dbdcSPavel Emelyanov 
360948d4f21SToshiaki Makita 	rcv_priv = netdev_priv(rcv);
361638264dcSToshiaki Makita 	rxq = skb_get_queue_mapping(skb);
362638264dcSToshiaki Makita 	if (rxq < rcv->real_num_rx_queues) {
363638264dcSToshiaki Makita 		rq = &rcv_priv->rq[rxq];
364d3256efdSPaolo Abeni 
365d3256efdSPaolo Abeni 		/* The napi pointer is available when an XDP program is
366d3256efdSPaolo Abeni 		 * attached or when GRO is enabled
36747e550e0SPaolo Abeni 		 * Don't bother with napi/GRO if the skb can't be aggregated
368d3256efdSPaolo Abeni 		 */
36947e550e0SPaolo Abeni 		use_napi = rcu_access_pointer(rq->napi) &&
37047e550e0SPaolo Abeni 			   veth_skb_is_eligible_for_gro(dev, rcv, skb);
371638264dcSToshiaki Makita 	}
372948d4f21SToshiaki Makita 
373aa4e689eSMichael Walle 	skb_tx_timestamp(skb);
374d3256efdSPaolo Abeni 	if (likely(veth_forward_skb(rcv, skb, rq, use_napi) == NET_RX_SUCCESS)) {
375d3256efdSPaolo Abeni 		if (!use_napi)
376b74095a4SPeilin Ye 			dev_sw_netstats_tx_add(dev, 1, length);
377215eb9f9SLiang Chen 		else
378215eb9f9SLiang Chen 			__veth_xdp_flush(rq);
3792681128fSEric Dumazet 	} else {
380d0e2c55eSEric Dumazet drop:
3812681128fSEric Dumazet 		atomic64_inc(&priv->dropped);
382151e887dSLiang Chen 		ret = NET_XMIT_DROP;
3832681128fSEric Dumazet 	}
384948d4f21SToshiaki Makita 
385d0e2c55eSEric Dumazet 	rcu_read_unlock();
386948d4f21SToshiaki Makita 
387151e887dSLiang Chen 	return ret;
388e314dbdcSPavel Emelyanov }
389e314dbdcSPavel Emelyanov 
veth_stats_rx(struct veth_stats * result,struct net_device * dev)39065780c56SLorenzo Bianconi static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
3914195e54aSToshiaki Makita {
3924195e54aSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
3934195e54aSToshiaki Makita 	int i;
3944195e54aSToshiaki Makita 
3955fe6e567SLorenzo Bianconi 	result->peer_tq_xdp_xmit_err = 0;
3964195e54aSToshiaki Makita 	result->xdp_packets = 0;
397d99a7c2fSLorenzo Bianconi 	result->xdp_tx_err = 0;
3984195e54aSToshiaki Makita 	result->xdp_bytes = 0;
39966fe4a07SLorenzo Bianconi 	result->rx_drops = 0;
4004195e54aSToshiaki Makita 	for (i = 0; i < dev->num_rx_queues; i++) {
4015fe6e567SLorenzo Bianconi 		u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
4024195e54aSToshiaki Makita 		struct veth_rq_stats *stats = &priv->rq[i].stats;
4034195e54aSToshiaki Makita 		unsigned int start;
4044195e54aSToshiaki Makita 
4054195e54aSToshiaki Makita 		do {
406068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&stats->syncp);
4075fe6e567SLorenzo Bianconi 			peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
408d99a7c2fSLorenzo Bianconi 			xdp_tx_err = stats->vs.xdp_tx_err;
40965780c56SLorenzo Bianconi 			packets = stats->vs.xdp_packets;
41065780c56SLorenzo Bianconi 			bytes = stats->vs.xdp_bytes;
41166fe4a07SLorenzo Bianconi 			drops = stats->vs.rx_drops;
412068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&stats->syncp, start));
4135fe6e567SLorenzo Bianconi 		result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
414d99a7c2fSLorenzo Bianconi 		result->xdp_tx_err += xdp_tx_err;
4154195e54aSToshiaki Makita 		result->xdp_packets += packets;
4164195e54aSToshiaki Makita 		result->xdp_bytes += bytes;
41766fe4a07SLorenzo Bianconi 		result->rx_drops += drops;
4184195e54aSToshiaki Makita 	}
4194195e54aSToshiaki Makita }
4204195e54aSToshiaki Makita 
veth_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * tot)421bc1f4470Sstephen hemminger static void veth_get_stats64(struct net_device *dev,
4222681128fSEric Dumazet 			     struct rtnl_link_stats64 *tot)
4232681128fSEric Dumazet {
4242681128fSEric Dumazet 	struct veth_priv *priv = netdev_priv(dev);
425d0e2c55eSEric Dumazet 	struct net_device *peer;
42665780c56SLorenzo Bianconi 	struct veth_stats rx;
4272681128fSEric Dumazet 
428b74095a4SPeilin Ye 	tot->tx_dropped = atomic64_read(&priv->dropped);
429b74095a4SPeilin Ye 	dev_fetch_sw_netstats(tot, dev->tstats);
4304195e54aSToshiaki Makita 
4314195e54aSToshiaki Makita 	veth_stats_rx(&rx, dev);
4325fe6e567SLorenzo Bianconi 	tot->tx_dropped += rx.xdp_tx_err;
4335fe6e567SLorenzo Bianconi 	tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
434b74095a4SPeilin Ye 	tot->rx_bytes += rx.xdp_bytes;
435b74095a4SPeilin Ye 	tot->rx_packets += rx.xdp_packets;
4362681128fSEric Dumazet 
437d0e2c55eSEric Dumazet 	rcu_read_lock();
438d0e2c55eSEric Dumazet 	peer = rcu_dereference(priv->peer);
439d0e2c55eSEric Dumazet 	if (peer) {
440b74095a4SPeilin Ye 		struct rtnl_link_stats64 tot_peer = {};
441b74095a4SPeilin Ye 
442b74095a4SPeilin Ye 		dev_fetch_sw_netstats(&tot_peer, peer->tstats);
443b74095a4SPeilin Ye 		tot->rx_bytes += tot_peer.tx_bytes;
444b74095a4SPeilin Ye 		tot->rx_packets += tot_peer.tx_packets;
4454195e54aSToshiaki Makita 
4464195e54aSToshiaki Makita 		veth_stats_rx(&rx, peer);
4475fe6e567SLorenzo Bianconi 		tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
4485fe6e567SLorenzo Bianconi 		tot->rx_dropped += rx.xdp_tx_err;
4494195e54aSToshiaki Makita 		tot->tx_bytes += rx.xdp_bytes;
4504195e54aSToshiaki Makita 		tot->tx_packets += rx.xdp_packets;
451d0e2c55eSEric Dumazet 	}
452d0e2c55eSEric Dumazet 	rcu_read_unlock();
453e314dbdcSPavel Emelyanov }
454e314dbdcSPavel Emelyanov 
4555c70ef85SGao feng /* fake multicast ability */
veth_set_multicast_list(struct net_device * dev)4565c70ef85SGao feng static void veth_set_multicast_list(struct net_device *dev)
4575c70ef85SGao feng {
4585c70ef85SGao feng }
4595c70ef85SGao feng 
veth_select_rxq(struct net_device * dev)460638264dcSToshiaki Makita static int veth_select_rxq(struct net_device *dev)
461638264dcSToshiaki Makita {
462638264dcSToshiaki Makita 	return smp_processor_id() % dev->real_num_rx_queues;
463638264dcSToshiaki Makita }
464638264dcSToshiaki Makita 
veth_peer_dev(struct net_device * dev)4659aa1206eSDaniel Borkmann static struct net_device *veth_peer_dev(struct net_device *dev)
4669aa1206eSDaniel Borkmann {
4679aa1206eSDaniel Borkmann 	struct veth_priv *priv = netdev_priv(dev);
4689aa1206eSDaniel Borkmann 
4699aa1206eSDaniel Borkmann 	/* Callers must be under RCU read side. */
4709aa1206eSDaniel Borkmann 	return rcu_dereference(priv->peer);
4719aa1206eSDaniel Borkmann }
4729aa1206eSDaniel Borkmann 
veth_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags,bool ndo_xmit)473af87a3aaSToshiaki Makita static int veth_xdp_xmit(struct net_device *dev, int n,
4749152cff0SLorenzo Bianconi 			 struct xdp_frame **frames,
4759152cff0SLorenzo Bianconi 			 u32 flags, bool ndo_xmit)
476af87a3aaSToshiaki Makita {
477af87a3aaSToshiaki Makita 	struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
478fdc13979SLorenzo Bianconi 	int i, ret = -ENXIO, nxmit = 0;
479af87a3aaSToshiaki Makita 	struct net_device *rcv;
4805fe6e567SLorenzo Bianconi 	unsigned int max_len;
481638264dcSToshiaki Makita 	struct veth_rq *rq;
482af87a3aaSToshiaki Makita 
4835fe6e567SLorenzo Bianconi 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
484d99a7c2fSLorenzo Bianconi 		return -EINVAL;
485af87a3aaSToshiaki Makita 
4865fe6e567SLorenzo Bianconi 	rcu_read_lock();
487af87a3aaSToshiaki Makita 	rcv = rcu_dereference(priv->peer);
4885fe6e567SLorenzo Bianconi 	if (unlikely(!rcv))
4895fe6e567SLorenzo Bianconi 		goto out;
490af87a3aaSToshiaki Makita 
491af87a3aaSToshiaki Makita 	rcv_priv = netdev_priv(rcv);
4925fe6e567SLorenzo Bianconi 	rq = &rcv_priv->rq[veth_select_rxq(rcv)];
4930e672f30SToke Høiland-Jørgensen 	/* The napi pointer is set if NAPI is enabled, which ensures that
4940e672f30SToke Høiland-Jørgensen 	 * xdp_ring is initialized on receive side and the peer device is up.
495af87a3aaSToshiaki Makita 	 */
4960e672f30SToke Høiland-Jørgensen 	if (!rcu_access_pointer(rq->napi))
4975fe6e567SLorenzo Bianconi 		goto out;
498af87a3aaSToshiaki Makita 
499af87a3aaSToshiaki Makita 	max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
500af87a3aaSToshiaki Makita 
501638264dcSToshiaki Makita 	spin_lock(&rq->xdp_ring.producer_lock);
502af87a3aaSToshiaki Makita 	for (i = 0; i < n; i++) {
503af87a3aaSToshiaki Makita 		struct xdp_frame *frame = frames[i];
504af87a3aaSToshiaki Makita 		void *ptr = veth_xdp_to_ptr(frame);
505af87a3aaSToshiaki Makita 
5065142239aSLorenzo Bianconi 		if (unlikely(xdp_get_frame_len(frame) > max_len ||
507fdc13979SLorenzo Bianconi 			     __ptr_ring_produce(&rq->xdp_ring, ptr)))
508fdc13979SLorenzo Bianconi 			break;
509fdc13979SLorenzo Bianconi 		nxmit++;
510af87a3aaSToshiaki Makita 	}
511638264dcSToshiaki Makita 	spin_unlock(&rq->xdp_ring.producer_lock);
512af87a3aaSToshiaki Makita 
513af87a3aaSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH)
514638264dcSToshiaki Makita 		__veth_xdp_flush(rq);
515af87a3aaSToshiaki Makita 
516fdc13979SLorenzo Bianconi 	ret = nxmit;
5179152cff0SLorenzo Bianconi 	if (ndo_xmit) {
5185fe6e567SLorenzo Bianconi 		u64_stats_update_begin(&rq->stats.syncp);
519fdc13979SLorenzo Bianconi 		rq->stats.vs.peer_tq_xdp_xmit += nxmit;
520fdc13979SLorenzo Bianconi 		rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
5219152cff0SLorenzo Bianconi 		u64_stats_update_end(&rq->stats.syncp);
5225fe6e567SLorenzo Bianconi 	}
5239152cff0SLorenzo Bianconi 
5245fe6e567SLorenzo Bianconi out:
525b23bfa56SJohn Fastabend 	rcu_read_unlock();
5262131479dSToshiaki Makita 
5272131479dSToshiaki Makita 	return ret;
528af87a3aaSToshiaki Makita }
529af87a3aaSToshiaki Makita 
veth_ndo_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)5309152cff0SLorenzo Bianconi static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
5319152cff0SLorenzo Bianconi 			     struct xdp_frame **frames, u32 flags)
5329152cff0SLorenzo Bianconi {
5335fe6e567SLorenzo Bianconi 	int err;
5345fe6e567SLorenzo Bianconi 
5355fe6e567SLorenzo Bianconi 	err = veth_xdp_xmit(dev, n, frames, flags, true);
5365fe6e567SLorenzo Bianconi 	if (err < 0) {
5375fe6e567SLorenzo Bianconi 		struct veth_priv *priv = netdev_priv(dev);
5385fe6e567SLorenzo Bianconi 
5395fe6e567SLorenzo Bianconi 		atomic64_add(n, &priv->dropped);
5405fe6e567SLorenzo Bianconi 	}
5415fe6e567SLorenzo Bianconi 
5425fe6e567SLorenzo Bianconi 	return err;
5439152cff0SLorenzo Bianconi }
5449152cff0SLorenzo Bianconi 
veth_xdp_flush_bq(struct veth_rq * rq,struct veth_xdp_tx_bq * bq)545bd32aa1fSLorenzo Bianconi static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
5469cda7807SToshiaki Makita {
547fdc13979SLorenzo Bianconi 	int sent, i, err = 0, drops;
5489cda7807SToshiaki Makita 
549bd32aa1fSLorenzo Bianconi 	sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
5509cda7807SToshiaki Makita 	if (sent < 0) {
5519cda7807SToshiaki Makita 		err = sent;
5529cda7807SToshiaki Makita 		sent = 0;
5539cda7807SToshiaki Makita 	}
554fdc13979SLorenzo Bianconi 
555fdc13979SLorenzo Bianconi 	for (i = sent; unlikely(i < bq->count); i++)
556fdc13979SLorenzo Bianconi 		xdp_return_frame(bq->q[i]);
557fdc13979SLorenzo Bianconi 
558fdc13979SLorenzo Bianconi 	drops = bq->count - sent;
559fdc13979SLorenzo Bianconi 	trace_xdp_bulk_tx(rq->dev, sent, drops, err);
5609cda7807SToshiaki Makita 
5615fe6e567SLorenzo Bianconi 	u64_stats_update_begin(&rq->stats.syncp);
5625fe6e567SLorenzo Bianconi 	rq->stats.vs.xdp_tx += sent;
563fdc13979SLorenzo Bianconi 	rq->stats.vs.xdp_tx_err += drops;
5645fe6e567SLorenzo Bianconi 	u64_stats_update_end(&rq->stats.syncp);
5655fe6e567SLorenzo Bianconi 
5669cda7807SToshiaki Makita 	bq->count = 0;
5679cda7807SToshiaki Makita }
5689cda7807SToshiaki Makita 
veth_xdp_flush(struct veth_rq * rq,struct veth_xdp_tx_bq * bq)569bd32aa1fSLorenzo Bianconi static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
570d1396004SToshiaki Makita {
571bd32aa1fSLorenzo Bianconi 	struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
572d1396004SToshiaki Makita 	struct net_device *rcv;
573bd32aa1fSLorenzo Bianconi 	struct veth_rq *rcv_rq;
574d1396004SToshiaki Makita 
575d1396004SToshiaki Makita 	rcu_read_lock();
576bd32aa1fSLorenzo Bianconi 	veth_xdp_flush_bq(rq, bq);
577d1396004SToshiaki Makita 	rcv = rcu_dereference(priv->peer);
578d1396004SToshiaki Makita 	if (unlikely(!rcv))
579d1396004SToshiaki Makita 		goto out;
580d1396004SToshiaki Makita 
581d1396004SToshiaki Makita 	rcv_priv = netdev_priv(rcv);
582bd32aa1fSLorenzo Bianconi 	rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
583d1396004SToshiaki Makita 	/* xdp_ring is initialized on receive side? */
584bd32aa1fSLorenzo Bianconi 	if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
585d1396004SToshiaki Makita 		goto out;
586d1396004SToshiaki Makita 
587bd32aa1fSLorenzo Bianconi 	__veth_xdp_flush(rcv_rq);
588d1396004SToshiaki Makita out:
589d1396004SToshiaki Makita 	rcu_read_unlock();
590d1396004SToshiaki Makita }
591d1396004SToshiaki Makita 
veth_xdp_tx(struct veth_rq * rq,struct xdp_buff * xdp,struct veth_xdp_tx_bq * bq)592bd32aa1fSLorenzo Bianconi static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
5939cda7807SToshiaki Makita 		       struct veth_xdp_tx_bq *bq)
594d1396004SToshiaki Makita {
5951b698fa5SLorenzo Bianconi 	struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
596d1396004SToshiaki Makita 
597d1396004SToshiaki Makita 	if (unlikely(!frame))
598d1396004SToshiaki Makita 		return -EOVERFLOW;
599d1396004SToshiaki Makita 
6009cda7807SToshiaki Makita 	if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
601bd32aa1fSLorenzo Bianconi 		veth_xdp_flush_bq(rq, bq);
6029cda7807SToshiaki Makita 
6039cda7807SToshiaki Makita 	bq->q[bq->count++] = frame;
6049cda7807SToshiaki Makita 
6059cda7807SToshiaki Makita 	return 0;
606d1396004SToshiaki Makita }
607d1396004SToshiaki Makita 
veth_xdp_rcv_one(struct veth_rq * rq,struct xdp_frame * frame,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)60865e6dcf7SLorenzo Bianconi static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
609d1396004SToshiaki Makita 					  struct xdp_frame *frame,
6101c5b82e5SLorenzo Bianconi 					  struct veth_xdp_tx_bq *bq,
6111c5b82e5SLorenzo Bianconi 					  struct veth_stats *stats)
6129fc8d518SToshiaki Makita {
613d1396004SToshiaki Makita 	struct xdp_frame orig_frame;
6149fc8d518SToshiaki Makita 	struct bpf_prog *xdp_prog;
6159fc8d518SToshiaki Makita 
6169fc8d518SToshiaki Makita 	rcu_read_lock();
617638264dcSToshiaki Makita 	xdp_prog = rcu_dereference(rq->xdp_prog);
6189fc8d518SToshiaki Makita 	if (likely(xdp_prog)) {
619fefb695aSStanislav Fomichev 		struct veth_xdp_buff vxbuf;
620fefb695aSStanislav Fomichev 		struct xdp_buff *xdp = &vxbuf.xdp;
6219fc8d518SToshiaki Makita 		u32 act;
6229fc8d518SToshiaki Makita 
623fefb695aSStanislav Fomichev 		xdp_convert_frame_to_buff(frame, xdp);
624fefb695aSStanislav Fomichev 		xdp->rxq = &rq->xdp_rxq;
625306531f0SStanislav Fomichev 		vxbuf.skb = NULL;
6269fc8d518SToshiaki Makita 
627fefb695aSStanislav Fomichev 		act = bpf_prog_run_xdp(xdp_prog, xdp);
6289fc8d518SToshiaki Makita 
6299fc8d518SToshiaki Makita 		switch (act) {
6309fc8d518SToshiaki Makita 		case XDP_PASS:
631fefb695aSStanislav Fomichev 			if (xdp_update_frame_from_buff(xdp, frame))
63289f479f0SLorenzo Bianconi 				goto err_xdp;
6339fc8d518SToshiaki Makita 			break;
634d1396004SToshiaki Makita 		case XDP_TX:
635d1396004SToshiaki Makita 			orig_frame = *frame;
636fefb695aSStanislav Fomichev 			xdp->rxq->mem = frame->mem;
637fefb695aSStanislav Fomichev 			if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
638638264dcSToshiaki Makita 				trace_xdp_exception(rq->dev, xdp_prog, act);
639d1396004SToshiaki Makita 				frame = &orig_frame;
6401c5b82e5SLorenzo Bianconi 				stats->rx_drops++;
641d1396004SToshiaki Makita 				goto err_xdp;
642d1396004SToshiaki Makita 			}
6431c5b82e5SLorenzo Bianconi 			stats->xdp_tx++;
644d1396004SToshiaki Makita 			rcu_read_unlock();
645d1396004SToshiaki Makita 			goto xdp_xmit;
646d1396004SToshiaki Makita 		case XDP_REDIRECT:
647d1396004SToshiaki Makita 			orig_frame = *frame;
648fefb695aSStanislav Fomichev 			xdp->rxq->mem = frame->mem;
649fefb695aSStanislav Fomichev 			if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
650d1396004SToshiaki Makita 				frame = &orig_frame;
6511c5b82e5SLorenzo Bianconi 				stats->rx_drops++;
652d1396004SToshiaki Makita 				goto err_xdp;
653d1396004SToshiaki Makita 			}
6541c5b82e5SLorenzo Bianconi 			stats->xdp_redirect++;
655d1396004SToshiaki Makita 			rcu_read_unlock();
656d1396004SToshiaki Makita 			goto xdp_xmit;
6579fc8d518SToshiaki Makita 		default:
658c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
659df561f66SGustavo A. R. Silva 			fallthrough;
6609fc8d518SToshiaki Makita 		case XDP_ABORTED:
661638264dcSToshiaki Makita 			trace_xdp_exception(rq->dev, xdp_prog, act);
662df561f66SGustavo A. R. Silva 			fallthrough;
6639fc8d518SToshiaki Makita 		case XDP_DROP:
6641c5b82e5SLorenzo Bianconi 			stats->xdp_drops++;
6659fc8d518SToshiaki Makita 			goto err_xdp;
6669fc8d518SToshiaki Makita 		}
6679fc8d518SToshiaki Makita 	}
6689fc8d518SToshiaki Makita 	rcu_read_unlock();
6699fc8d518SToshiaki Makita 
67065e6dcf7SLorenzo Bianconi 	return frame;
6719fc8d518SToshiaki Makita err_xdp:
6729fc8d518SToshiaki Makita 	rcu_read_unlock();
6739fc8d518SToshiaki Makita 	xdp_return_frame(frame);
674d1396004SToshiaki Makita xdp_xmit:
6759fc8d518SToshiaki Makita 	return NULL;
6769fc8d518SToshiaki Makita }
6779fc8d518SToshiaki Makita 
67865e6dcf7SLorenzo Bianconi /* frames array contains VETH_XDP_BATCH at most */
veth_xdp_rcv_bulk_skb(struct veth_rq * rq,void ** frames,int n_xdpf,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)67965e6dcf7SLorenzo Bianconi static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
68065e6dcf7SLorenzo Bianconi 				  int n_xdpf, struct veth_xdp_tx_bq *bq,
68165e6dcf7SLorenzo Bianconi 				  struct veth_stats *stats)
68265e6dcf7SLorenzo Bianconi {
68365e6dcf7SLorenzo Bianconi 	void *skbs[VETH_XDP_BATCH];
68465e6dcf7SLorenzo Bianconi 	int i;
68565e6dcf7SLorenzo Bianconi 
68665e6dcf7SLorenzo Bianconi 	if (xdp_alloc_skb_bulk(skbs, n_xdpf,
68765e6dcf7SLorenzo Bianconi 			       GFP_ATOMIC | __GFP_ZERO) < 0) {
68865e6dcf7SLorenzo Bianconi 		for (i = 0; i < n_xdpf; i++)
68965e6dcf7SLorenzo Bianconi 			xdp_return_frame(frames[i]);
69065e6dcf7SLorenzo Bianconi 		stats->rx_drops += n_xdpf;
69165e6dcf7SLorenzo Bianconi 
69265e6dcf7SLorenzo Bianconi 		return;
69365e6dcf7SLorenzo Bianconi 	}
69465e6dcf7SLorenzo Bianconi 
69565e6dcf7SLorenzo Bianconi 	for (i = 0; i < n_xdpf; i++) {
69665e6dcf7SLorenzo Bianconi 		struct sk_buff *skb = skbs[i];
69765e6dcf7SLorenzo Bianconi 
69865e6dcf7SLorenzo Bianconi 		skb = __xdp_build_skb_from_frame(frames[i], skb,
69965e6dcf7SLorenzo Bianconi 						 rq->dev);
70065e6dcf7SLorenzo Bianconi 		if (!skb) {
70165e6dcf7SLorenzo Bianconi 			xdp_return_frame(frames[i]);
70265e6dcf7SLorenzo Bianconi 			stats->rx_drops++;
70365e6dcf7SLorenzo Bianconi 			continue;
70465e6dcf7SLorenzo Bianconi 		}
70565e6dcf7SLorenzo Bianconi 		napi_gro_receive(&rq->xdp_napi, skb);
70665e6dcf7SLorenzo Bianconi 	}
70765e6dcf7SLorenzo Bianconi }
70865e6dcf7SLorenzo Bianconi 
veth_xdp_get(struct xdp_buff * xdp)709718a18a0SLorenzo Bianconi static void veth_xdp_get(struct xdp_buff *xdp)
710718a18a0SLorenzo Bianconi {
711718a18a0SLorenzo Bianconi 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
712718a18a0SLorenzo Bianconi 	int i;
713718a18a0SLorenzo Bianconi 
714718a18a0SLorenzo Bianconi 	get_page(virt_to_page(xdp->data));
715718a18a0SLorenzo Bianconi 	if (likely(!xdp_buff_has_frags(xdp)))
716718a18a0SLorenzo Bianconi 		return;
717718a18a0SLorenzo Bianconi 
718718a18a0SLorenzo Bianconi 	for (i = 0; i < sinfo->nr_frags; i++)
719718a18a0SLorenzo Bianconi 		__skb_frag_ref(&sinfo->frags[i]);
720718a18a0SLorenzo Bianconi }
721718a18a0SLorenzo Bianconi 
veth_convert_skb_to_xdp_buff(struct veth_rq * rq,struct xdp_buff * xdp,struct sk_buff ** pskb)722718a18a0SLorenzo Bianconi static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
723718a18a0SLorenzo Bianconi 					struct xdp_buff *xdp,
724718a18a0SLorenzo Bianconi 					struct sk_buff **pskb)
725718a18a0SLorenzo Bianconi {
726718a18a0SLorenzo Bianconi 	struct sk_buff *skb = *pskb;
727718a18a0SLorenzo Bianconi 	u32 frame_sz;
728718a18a0SLorenzo Bianconi 
729718a18a0SLorenzo Bianconi 	if (skb_shared(skb) || skb_head_is_locked(skb) ||
7307c101318SShawn Bohrer 	    skb_shinfo(skb)->nr_frags ||
7317c101318SShawn Bohrer 	    skb_headroom(skb) < XDP_PACKET_HEADROOM) {
732718a18a0SLorenzo Bianconi 		u32 size, len, max_head_size, off;
733718a18a0SLorenzo Bianconi 		struct sk_buff *nskb;
734718a18a0SLorenzo Bianconi 		struct page *page;
735718a18a0SLorenzo Bianconi 		int i, head_off;
736718a18a0SLorenzo Bianconi 
737718a18a0SLorenzo Bianconi 		/* We need a private copy of the skb and data buffers since
738718a18a0SLorenzo Bianconi 		 * the ebpf program can modify it. We segment the original skb
739718a18a0SLorenzo Bianconi 		 * into order-0 pages without linearize it.
740718a18a0SLorenzo Bianconi 		 *
741718a18a0SLorenzo Bianconi 		 * Make sure we have enough space for linear and paged area
742718a18a0SLorenzo Bianconi 		 */
743718a18a0SLorenzo Bianconi 		max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE -
744718a18a0SLorenzo Bianconi 						  VETH_XDP_HEADROOM);
745718a18a0SLorenzo Bianconi 		if (skb->len > PAGE_SIZE * MAX_SKB_FRAGS + max_head_size)
746718a18a0SLorenzo Bianconi 			goto drop;
747718a18a0SLorenzo Bianconi 
748718a18a0SLorenzo Bianconi 		/* Allocate skb head */
7490ebab78cSLorenzo Bianconi 		page = page_pool_dev_alloc_pages(rq->page_pool);
750718a18a0SLorenzo Bianconi 		if (!page)
751718a18a0SLorenzo Bianconi 			goto drop;
752718a18a0SLorenzo Bianconi 
7539d142ed4SLorenzo Bianconi 		nskb = napi_build_skb(page_address(page), PAGE_SIZE);
754718a18a0SLorenzo Bianconi 		if (!nskb) {
7550ebab78cSLorenzo Bianconi 			page_pool_put_full_page(rq->page_pool, page, true);
756718a18a0SLorenzo Bianconi 			goto drop;
757718a18a0SLorenzo Bianconi 		}
758718a18a0SLorenzo Bianconi 
759718a18a0SLorenzo Bianconi 		skb_reserve(nskb, VETH_XDP_HEADROOM);
7600ebab78cSLorenzo Bianconi 		skb_copy_header(nskb, skb);
7610ebab78cSLorenzo Bianconi 		skb_mark_for_recycle(nskb);
7620ebab78cSLorenzo Bianconi 
763718a18a0SLorenzo Bianconi 		size = min_t(u32, skb->len, max_head_size);
764718a18a0SLorenzo Bianconi 		if (skb_copy_bits(skb, 0, nskb->data, size)) {
765718a18a0SLorenzo Bianconi 			consume_skb(nskb);
766718a18a0SLorenzo Bianconi 			goto drop;
767718a18a0SLorenzo Bianconi 		}
768718a18a0SLorenzo Bianconi 		skb_put(nskb, size);
769718a18a0SLorenzo Bianconi 
770718a18a0SLorenzo Bianconi 		head_off = skb_headroom(nskb) - skb_headroom(skb);
771718a18a0SLorenzo Bianconi 		skb_headers_offset_update(nskb, head_off);
772718a18a0SLorenzo Bianconi 
773718a18a0SLorenzo Bianconi 		/* Allocate paged area of new skb */
774718a18a0SLorenzo Bianconi 		off = size;
775718a18a0SLorenzo Bianconi 		len = skb->len - off;
776718a18a0SLorenzo Bianconi 
777718a18a0SLorenzo Bianconi 		for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
7780ebab78cSLorenzo Bianconi 			page = page_pool_dev_alloc_pages(rq->page_pool);
779718a18a0SLorenzo Bianconi 			if (!page) {
780718a18a0SLorenzo Bianconi 				consume_skb(nskb);
781718a18a0SLorenzo Bianconi 				goto drop;
782718a18a0SLorenzo Bianconi 			}
783718a18a0SLorenzo Bianconi 
784718a18a0SLorenzo Bianconi 			size = min_t(u32, len, PAGE_SIZE);
785718a18a0SLorenzo Bianconi 			skb_add_rx_frag(nskb, i, page, 0, size, PAGE_SIZE);
786718a18a0SLorenzo Bianconi 			if (skb_copy_bits(skb, off, page_address(page),
787718a18a0SLorenzo Bianconi 					  size)) {
788718a18a0SLorenzo Bianconi 				consume_skb(nskb);
789718a18a0SLorenzo Bianconi 				goto drop;
790718a18a0SLorenzo Bianconi 			}
791718a18a0SLorenzo Bianconi 
792718a18a0SLorenzo Bianconi 			len -= size;
793718a18a0SLorenzo Bianconi 			off += size;
794718a18a0SLorenzo Bianconi 		}
795718a18a0SLorenzo Bianconi 
796718a18a0SLorenzo Bianconi 		consume_skb(skb);
797718a18a0SLorenzo Bianconi 		skb = nskb;
798718a18a0SLorenzo Bianconi 	}
799718a18a0SLorenzo Bianconi 
800718a18a0SLorenzo Bianconi 	/* SKB "head" area always have tailroom for skb_shared_info */
801718a18a0SLorenzo Bianconi 	frame_sz = skb_end_pointer(skb) - skb->head;
802718a18a0SLorenzo Bianconi 	frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
803718a18a0SLorenzo Bianconi 	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
804718a18a0SLorenzo Bianconi 	xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
805718a18a0SLorenzo Bianconi 			 skb_headlen(skb), true);
806718a18a0SLorenzo Bianconi 
807718a18a0SLorenzo Bianconi 	if (skb_is_nonlinear(skb)) {
808718a18a0SLorenzo Bianconi 		skb_shinfo(skb)->xdp_frags_size = skb->data_len;
809718a18a0SLorenzo Bianconi 		xdp_buff_set_frags_flag(xdp);
810718a18a0SLorenzo Bianconi 	} else {
811718a18a0SLorenzo Bianconi 		xdp_buff_clear_frags_flag(xdp);
812718a18a0SLorenzo Bianconi 	}
813718a18a0SLorenzo Bianconi 	*pskb = skb;
814718a18a0SLorenzo Bianconi 
815718a18a0SLorenzo Bianconi 	return 0;
816718a18a0SLorenzo Bianconi drop:
817718a18a0SLorenzo Bianconi 	consume_skb(skb);
818718a18a0SLorenzo Bianconi 	*pskb = NULL;
819718a18a0SLorenzo Bianconi 
820718a18a0SLorenzo Bianconi 	return -ENOMEM;
821718a18a0SLorenzo Bianconi }
822718a18a0SLorenzo Bianconi 
veth_xdp_rcv_skb(struct veth_rq * rq,struct sk_buff * skb,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)8231c5b82e5SLorenzo Bianconi static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
8241c5b82e5SLorenzo Bianconi 					struct sk_buff *skb,
8251c5b82e5SLorenzo Bianconi 					struct veth_xdp_tx_bq *bq,
8261c5b82e5SLorenzo Bianconi 					struct veth_stats *stats)
827948d4f21SToshiaki Makita {
828948d4f21SToshiaki Makita 	void *orig_data, *orig_data_end;
829948d4f21SToshiaki Makita 	struct bpf_prog *xdp_prog;
830fefb695aSStanislav Fomichev 	struct veth_xdp_buff vxbuf;
831fefb695aSStanislav Fomichev 	struct xdp_buff *xdp = &vxbuf.xdp;
832718a18a0SLorenzo Bianconi 	u32 act, metalen;
833718a18a0SLorenzo Bianconi 	int off;
834948d4f21SToshiaki Makita 
835d504fff0SPaolo Abeni 	skb_prepare_for_gro(skb);
8364bf9ffa0SToshiaki Makita 
837948d4f21SToshiaki Makita 	rcu_read_lock();
838638264dcSToshiaki Makita 	xdp_prog = rcu_dereference(rq->xdp_prog);
839948d4f21SToshiaki Makita 	if (unlikely(!xdp_prog)) {
840948d4f21SToshiaki Makita 		rcu_read_unlock();
841948d4f21SToshiaki Makita 		goto out;
842948d4f21SToshiaki Makita 	}
843948d4f21SToshiaki Makita 
844718a18a0SLorenzo Bianconi 	__skb_push(skb, skb->data - skb_mac_header(skb));
845fefb695aSStanislav Fomichev 	if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
846948d4f21SToshiaki Makita 		goto drop;
847306531f0SStanislav Fomichev 	vxbuf.skb = skb;
848948d4f21SToshiaki Makita 
849fefb695aSStanislav Fomichev 	orig_data = xdp->data;
850fefb695aSStanislav Fomichev 	orig_data_end = xdp->data_end;
851948d4f21SToshiaki Makita 
852fefb695aSStanislav Fomichev 	act = bpf_prog_run_xdp(xdp_prog, xdp);
853948d4f21SToshiaki Makita 
854948d4f21SToshiaki Makita 	switch (act) {
855948d4f21SToshiaki Makita 	case XDP_PASS:
856948d4f21SToshiaki Makita 		break;
857d1396004SToshiaki Makita 	case XDP_TX:
858fefb695aSStanislav Fomichev 		veth_xdp_get(xdp);
859d1396004SToshiaki Makita 		consume_skb(skb);
860fefb695aSStanislav Fomichev 		xdp->rxq->mem = rq->xdp_mem;
861fefb695aSStanislav Fomichev 		if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
862638264dcSToshiaki Makita 			trace_xdp_exception(rq->dev, xdp_prog, act);
8631c5b82e5SLorenzo Bianconi 			stats->rx_drops++;
864d1396004SToshiaki Makita 			goto err_xdp;
865d1396004SToshiaki Makita 		}
8661c5b82e5SLorenzo Bianconi 		stats->xdp_tx++;
867d1396004SToshiaki Makita 		rcu_read_unlock();
868d1396004SToshiaki Makita 		goto xdp_xmit;
869d1396004SToshiaki Makita 	case XDP_REDIRECT:
870fefb695aSStanislav Fomichev 		veth_xdp_get(xdp);
871d1396004SToshiaki Makita 		consume_skb(skb);
872fefb695aSStanislav Fomichev 		xdp->rxq->mem = rq->xdp_mem;
873fefb695aSStanislav Fomichev 		if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
8741c5b82e5SLorenzo Bianconi 			stats->rx_drops++;
875d1396004SToshiaki Makita 			goto err_xdp;
8761c5b82e5SLorenzo Bianconi 		}
8771c5b82e5SLorenzo Bianconi 		stats->xdp_redirect++;
878d1396004SToshiaki Makita 		rcu_read_unlock();
879d1396004SToshiaki Makita 		goto xdp_xmit;
880948d4f21SToshiaki Makita 	default:
881c8064e5bSPaolo Abeni 		bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
882df561f66SGustavo A. R. Silva 		fallthrough;
883948d4f21SToshiaki Makita 	case XDP_ABORTED:
884638264dcSToshiaki Makita 		trace_xdp_exception(rq->dev, xdp_prog, act);
885df561f66SGustavo A. R. Silva 		fallthrough;
886948d4f21SToshiaki Makita 	case XDP_DROP:
8871c5b82e5SLorenzo Bianconi 		stats->xdp_drops++;
8881c5b82e5SLorenzo Bianconi 		goto xdp_drop;
889948d4f21SToshiaki Makita 	}
890948d4f21SToshiaki Makita 	rcu_read_unlock();
891948d4f21SToshiaki Makita 
89245a9e6d8SJesper Dangaard Brouer 	/* check if bpf_xdp_adjust_head was used */
893fefb695aSStanislav Fomichev 	off = orig_data - xdp->data;
894948d4f21SToshiaki Makita 	if (off > 0)
895948d4f21SToshiaki Makita 		__skb_push(skb, off);
896948d4f21SToshiaki Makita 	else if (off < 0)
897948d4f21SToshiaki Makita 		__skb_pull(skb, -off);
898718a18a0SLorenzo Bianconi 
899718a18a0SLorenzo Bianconi 	skb_reset_mac_header(skb);
90045a9e6d8SJesper Dangaard Brouer 
90145a9e6d8SJesper Dangaard Brouer 	/* check if bpf_xdp_adjust_tail was used */
902fefb695aSStanislav Fomichev 	off = xdp->data_end - orig_data_end;
903948d4f21SToshiaki Makita 	if (off != 0)
90445a9e6d8SJesper Dangaard Brouer 		__skb_put(skb, off); /* positive on grow, negative on shrink */
905718a18a0SLorenzo Bianconi 
906718a18a0SLorenzo Bianconi 	/* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
907718a18a0SLorenzo Bianconi 	 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
908718a18a0SLorenzo Bianconi 	 */
909fefb695aSStanislav Fomichev 	if (xdp_buff_has_frags(xdp))
910718a18a0SLorenzo Bianconi 		skb->data_len = skb_shinfo(skb)->xdp_frags_size;
911718a18a0SLorenzo Bianconi 	else
912718a18a0SLorenzo Bianconi 		skb->data_len = 0;
913718a18a0SLorenzo Bianconi 
914638264dcSToshiaki Makita 	skb->protocol = eth_type_trans(skb, rq->dev);
915948d4f21SToshiaki Makita 
916fefb695aSStanislav Fomichev 	metalen = xdp->data - xdp->data_meta;
917948d4f21SToshiaki Makita 	if (metalen)
918948d4f21SToshiaki Makita 		skb_metadata_set(skb, metalen);
919948d4f21SToshiaki Makita out:
920948d4f21SToshiaki Makita 	return skb;
921948d4f21SToshiaki Makita drop:
9221c5b82e5SLorenzo Bianconi 	stats->rx_drops++;
9231c5b82e5SLorenzo Bianconi xdp_drop:
924948d4f21SToshiaki Makita 	rcu_read_unlock();
925948d4f21SToshiaki Makita 	kfree_skb(skb);
926948d4f21SToshiaki Makita 	return NULL;
927d1396004SToshiaki Makita err_xdp:
928d1396004SToshiaki Makita 	rcu_read_unlock();
929fefb695aSStanislav Fomichev 	xdp_return_buff(xdp);
930d1396004SToshiaki Makita xdp_xmit:
931d1396004SToshiaki Makita 	return NULL;
932948d4f21SToshiaki Makita }
933948d4f21SToshiaki Makita 
veth_xdp_rcv(struct veth_rq * rq,int budget,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)9341c5b82e5SLorenzo Bianconi static int veth_xdp_rcv(struct veth_rq *rq, int budget,
9351c5b82e5SLorenzo Bianconi 			struct veth_xdp_tx_bq *bq,
9361c5b82e5SLorenzo Bianconi 			struct veth_stats *stats)
937948d4f21SToshiaki Makita {
93865e6dcf7SLorenzo Bianconi 	int i, done = 0, n_xdpf = 0;
93965e6dcf7SLorenzo Bianconi 	void *xdpf[VETH_XDP_BATCH];
940948d4f21SToshiaki Makita 
941948d4f21SToshiaki Makita 	for (i = 0; i < budget; i++) {
942638264dcSToshiaki Makita 		void *ptr = __ptr_ring_consume(&rq->xdp_ring);
943948d4f21SToshiaki Makita 
9449fc8d518SToshiaki Makita 		if (!ptr)
945948d4f21SToshiaki Makita 			break;
946948d4f21SToshiaki Makita 
947d1396004SToshiaki Makita 		if (veth_is_xdp_frame(ptr)) {
94865e6dcf7SLorenzo Bianconi 			/* ndo_xdp_xmit */
9494195e54aSToshiaki Makita 			struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
9504195e54aSToshiaki Makita 
9515142239aSLorenzo Bianconi 			stats->xdp_bytes += xdp_get_frame_len(frame);
95265e6dcf7SLorenzo Bianconi 			frame = veth_xdp_rcv_one(rq, frame, bq, stats);
95365e6dcf7SLorenzo Bianconi 			if (frame) {
95465e6dcf7SLorenzo Bianconi 				/* XDP_PASS */
95565e6dcf7SLorenzo Bianconi 				xdpf[n_xdpf++] = frame;
95665e6dcf7SLorenzo Bianconi 				if (n_xdpf == VETH_XDP_BATCH) {
95765e6dcf7SLorenzo Bianconi 					veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
95865e6dcf7SLorenzo Bianconi 							      bq, stats);
95965e6dcf7SLorenzo Bianconi 					n_xdpf = 0;
96065e6dcf7SLorenzo Bianconi 				}
96165e6dcf7SLorenzo Bianconi 			}
962d1396004SToshiaki Makita 		} else {
96365e6dcf7SLorenzo Bianconi 			/* ndo_start_xmit */
96465e6dcf7SLorenzo Bianconi 			struct sk_buff *skb = ptr;
96565e6dcf7SLorenzo Bianconi 
9661c5b82e5SLorenzo Bianconi 			stats->xdp_bytes += skb->len;
9671c5b82e5SLorenzo Bianconi 			skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
9689695b7deSPaolo Abeni 			if (skb) {
9699695b7deSPaolo Abeni 				if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
9709695b7deSPaolo Abeni 					netif_receive_skb(skb);
9719695b7deSPaolo Abeni 				else
972638264dcSToshiaki Makita 					napi_gro_receive(&rq->xdp_napi, skb);
97365e6dcf7SLorenzo Bianconi 			}
9749695b7deSPaolo Abeni 		}
975948d4f21SToshiaki Makita 		done++;
976948d4f21SToshiaki Makita 	}
977948d4f21SToshiaki Makita 
97865e6dcf7SLorenzo Bianconi 	if (n_xdpf)
97965e6dcf7SLorenzo Bianconi 		veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
98065e6dcf7SLorenzo Bianconi 
9814195e54aSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
9829152cff0SLorenzo Bianconi 	rq->stats.vs.xdp_redirect += stats->xdp_redirect;
9831c5b82e5SLorenzo Bianconi 	rq->stats.vs.xdp_bytes += stats->xdp_bytes;
98466fe4a07SLorenzo Bianconi 	rq->stats.vs.xdp_drops += stats->xdp_drops;
98566fe4a07SLorenzo Bianconi 	rq->stats.vs.rx_drops += stats->rx_drops;
98665780c56SLorenzo Bianconi 	rq->stats.vs.xdp_packets += done;
9874195e54aSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
9884195e54aSToshiaki Makita 
989948d4f21SToshiaki Makita 	return done;
990948d4f21SToshiaki Makita }
991948d4f21SToshiaki Makita 
veth_poll(struct napi_struct * napi,int budget)992948d4f21SToshiaki Makita static int veth_poll(struct napi_struct *napi, int budget)
993948d4f21SToshiaki Makita {
994638264dcSToshiaki Makita 	struct veth_rq *rq =
995638264dcSToshiaki Makita 		container_of(napi, struct veth_rq, xdp_napi);
9961c5b82e5SLorenzo Bianconi 	struct veth_stats stats = {};
9979cda7807SToshiaki Makita 	struct veth_xdp_tx_bq bq;
998948d4f21SToshiaki Makita 	int done;
999948d4f21SToshiaki Makita 
10009cda7807SToshiaki Makita 	bq.count = 0;
10019cda7807SToshiaki Makita 
1002d1396004SToshiaki Makita 	xdp_set_return_frame_no_direct();
10031c5b82e5SLorenzo Bianconi 	done = veth_xdp_rcv(rq, budget, &bq, &stats);
1004948d4f21SToshiaki Makita 
1005fa349e39SShawn Bohrer 	if (stats.xdp_redirect > 0)
1006fa349e39SShawn Bohrer 		xdp_do_flush();
1007fa349e39SShawn Bohrer 
1008948d4f21SToshiaki Makita 	if (done < budget && napi_complete_done(napi, done)) {
1009948d4f21SToshiaki Makita 		/* Write rx_notify_masked before reading ptr_ring */
1010638264dcSToshiaki Makita 		smp_store_mb(rq->rx_notify_masked, false);
1011638264dcSToshiaki Makita 		if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
101268468d8cSEric Dumazet 			if (napi_schedule_prep(&rq->xdp_napi)) {
101368468d8cSEric Dumazet 				WRITE_ONCE(rq->rx_notify_masked, true);
101468468d8cSEric Dumazet 				__napi_schedule(&rq->xdp_napi);
101568468d8cSEric Dumazet 			}
1016948d4f21SToshiaki Makita 		}
1017948d4f21SToshiaki Makita 	}
1018948d4f21SToshiaki Makita 
10191c5b82e5SLorenzo Bianconi 	if (stats.xdp_tx > 0)
1020bd32aa1fSLorenzo Bianconi 		veth_xdp_flush(rq, &bq);
1021d1396004SToshiaki Makita 	xdp_clear_return_frame_no_direct();
1022d1396004SToshiaki Makita 
1023948d4f21SToshiaki Makita 	return done;
1024948d4f21SToshiaki Makita }
1025948d4f21SToshiaki Makita 
veth_create_page_pool(struct veth_rq * rq)10260ebab78cSLorenzo Bianconi static int veth_create_page_pool(struct veth_rq *rq)
10270ebab78cSLorenzo Bianconi {
10280ebab78cSLorenzo Bianconi 	struct page_pool_params pp_params = {
10290ebab78cSLorenzo Bianconi 		.order = 0,
10300ebab78cSLorenzo Bianconi 		.pool_size = VETH_RING_SIZE,
10310ebab78cSLorenzo Bianconi 		.nid = NUMA_NO_NODE,
10320ebab78cSLorenzo Bianconi 		.dev = &rq->dev->dev,
10330ebab78cSLorenzo Bianconi 	};
10340ebab78cSLorenzo Bianconi 
10350ebab78cSLorenzo Bianconi 	rq->page_pool = page_pool_create(&pp_params);
10360ebab78cSLorenzo Bianconi 	if (IS_ERR(rq->page_pool)) {
10370ebab78cSLorenzo Bianconi 		int err = PTR_ERR(rq->page_pool);
10380ebab78cSLorenzo Bianconi 
10390ebab78cSLorenzo Bianconi 		rq->page_pool = NULL;
10400ebab78cSLorenzo Bianconi 		return err;
10410ebab78cSLorenzo Bianconi 	}
10420ebab78cSLorenzo Bianconi 
10430ebab78cSLorenzo Bianconi 	return 0;
10440ebab78cSLorenzo Bianconi }
10450ebab78cSLorenzo Bianconi 
__veth_napi_enable_range(struct net_device * dev,int start,int end)1046dedd53c5SPaolo Abeni static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
1047948d4f21SToshiaki Makita {
1048948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1049638264dcSToshiaki Makita 	int err, i;
1050948d4f21SToshiaki Makita 
1051dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
10520ebab78cSLorenzo Bianconi 		err = veth_create_page_pool(&priv->rq[i]);
10530ebab78cSLorenzo Bianconi 		if (err)
10540ebab78cSLorenzo Bianconi 			goto err_page_pool;
10550ebab78cSLorenzo Bianconi 	}
10560ebab78cSLorenzo Bianconi 
10570ebab78cSLorenzo Bianconi 	for (i = start; i < end; i++) {
1058638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1059638264dcSToshiaki Makita 
1060638264dcSToshiaki Makita 		err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
1061948d4f21SToshiaki Makita 		if (err)
1062638264dcSToshiaki Makita 			goto err_xdp_ring;
1063638264dcSToshiaki Makita 	}
1064948d4f21SToshiaki Makita 
1065dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1066638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1067638264dcSToshiaki Makita 
1068638264dcSToshiaki Makita 		napi_enable(&rq->xdp_napi);
1069d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1070638264dcSToshiaki Makita 	}
1071948d4f21SToshiaki Makita 
1072948d4f21SToshiaki Makita 	return 0;
1073dedd53c5SPaolo Abeni 
1074638264dcSToshiaki Makita err_xdp_ring:
1075dedd53c5SPaolo Abeni 	for (i--; i >= start; i--)
1076638264dcSToshiaki Makita 		ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
10778a519a57SLiang Chen 	i = end;
10780ebab78cSLorenzo Bianconi err_page_pool:
10798a519a57SLiang Chen 	for (i--; i >= start; i--) {
10800ebab78cSLorenzo Bianconi 		page_pool_destroy(priv->rq[i].page_pool);
10810ebab78cSLorenzo Bianconi 		priv->rq[i].page_pool = NULL;
10820ebab78cSLorenzo Bianconi 	}
1083638264dcSToshiaki Makita 
1084638264dcSToshiaki Makita 	return err;
1085948d4f21SToshiaki Makita }
1086948d4f21SToshiaki Makita 
__veth_napi_enable(struct net_device * dev)1087dedd53c5SPaolo Abeni static int __veth_napi_enable(struct net_device *dev)
1088dedd53c5SPaolo Abeni {
1089dedd53c5SPaolo Abeni 	return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1090dedd53c5SPaolo Abeni }
1091dedd53c5SPaolo Abeni 
veth_napi_del_range(struct net_device * dev,int start,int end)1092dedd53c5SPaolo Abeni static void veth_napi_del_range(struct net_device *dev, int start, int end)
1093948d4f21SToshiaki Makita {
1094948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1095638264dcSToshiaki Makita 	int i;
1096948d4f21SToshiaki Makita 
1097dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1098638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1099638264dcSToshiaki Makita 
1100d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, NULL);
1101638264dcSToshiaki Makita 		napi_disable(&rq->xdp_napi);
11025198d545SJakub Kicinski 		__netif_napi_del(&rq->xdp_napi);
1103638264dcSToshiaki Makita 	}
1104638264dcSToshiaki Makita 	synchronize_net();
1105638264dcSToshiaki Makita 
1106dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1107638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1108638264dcSToshiaki Makita 
1109638264dcSToshiaki Makita 		rq->rx_notify_masked = false;
1110638264dcSToshiaki Makita 		ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
1111638264dcSToshiaki Makita 	}
11120ebab78cSLorenzo Bianconi 
11130ebab78cSLorenzo Bianconi 	for (i = start; i < end; i++) {
11140ebab78cSLorenzo Bianconi 		page_pool_destroy(priv->rq[i].page_pool);
11150ebab78cSLorenzo Bianconi 		priv->rq[i].page_pool = NULL;
11160ebab78cSLorenzo Bianconi 	}
1117948d4f21SToshiaki Makita }
1118948d4f21SToshiaki Makita 
veth_napi_del(struct net_device * dev)1119dedd53c5SPaolo Abeni static void veth_napi_del(struct net_device *dev)
1120dedd53c5SPaolo Abeni {
1121dedd53c5SPaolo Abeni 	veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1122dedd53c5SPaolo Abeni }
1123dedd53c5SPaolo Abeni 
veth_gro_requested(const struct net_device * dev)1124d3256efdSPaolo Abeni static bool veth_gro_requested(const struct net_device *dev)
1125d3256efdSPaolo Abeni {
1126d3256efdSPaolo Abeni 	return !!(dev->wanted_features & NETIF_F_GRO);
1127d3256efdSPaolo Abeni }
1128d3256efdSPaolo Abeni 
veth_enable_xdp_range(struct net_device * dev,int start,int end,bool napi_already_on)1129dedd53c5SPaolo Abeni static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1130dedd53c5SPaolo Abeni 				 bool napi_already_on)
1131948d4f21SToshiaki Makita {
1132948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1133638264dcSToshiaki Makita 	int err, i;
1134948d4f21SToshiaki Makita 
1135dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1136638264dcSToshiaki Makita 		struct veth_rq *rq = &priv->rq[i];
1137948d4f21SToshiaki Makita 
1138d3256efdSPaolo Abeni 		if (!napi_already_on)
1139b48b89f9SJakub Kicinski 			netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1140b02e5a0eSBjörn Töpel 		err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1141948d4f21SToshiaki Makita 		if (err < 0)
1142638264dcSToshiaki Makita 			goto err_rxq_reg;
1143638264dcSToshiaki Makita 
1144638264dcSToshiaki Makita 		err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1145638264dcSToshiaki Makita 						 MEM_TYPE_PAGE_SHARED,
1146638264dcSToshiaki Makita 						 NULL);
1147638264dcSToshiaki Makita 		if (err < 0)
1148638264dcSToshiaki Makita 			goto err_reg_mem;
1149638264dcSToshiaki Makita 
1150638264dcSToshiaki Makita 		/* Save original mem info as it can be overwritten */
1151638264dcSToshiaki Makita 		rq->xdp_mem = rq->xdp_rxq.mem;
1152638264dcSToshiaki Makita 	}
1153dedd53c5SPaolo Abeni 	return 0;
1154dedd53c5SPaolo Abeni 
1155dedd53c5SPaolo Abeni err_reg_mem:
1156dedd53c5SPaolo Abeni 	xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1157dedd53c5SPaolo Abeni err_rxq_reg:
1158dedd53c5SPaolo Abeni 	for (i--; i >= start; i--) {
1159dedd53c5SPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1160dedd53c5SPaolo Abeni 
1161dedd53c5SPaolo Abeni 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1162dedd53c5SPaolo Abeni 		if (!napi_already_on)
1163dedd53c5SPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1164dedd53c5SPaolo Abeni 	}
1165dedd53c5SPaolo Abeni 
1166dedd53c5SPaolo Abeni 	return err;
1167dedd53c5SPaolo Abeni }
1168dedd53c5SPaolo Abeni 
veth_disable_xdp_range(struct net_device * dev,int start,int end,bool delete_napi)1169dedd53c5SPaolo Abeni static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1170dedd53c5SPaolo Abeni 				   bool delete_napi)
1171dedd53c5SPaolo Abeni {
1172dedd53c5SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1173dedd53c5SPaolo Abeni 	int i;
1174dedd53c5SPaolo Abeni 
1175dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1176dedd53c5SPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1177dedd53c5SPaolo Abeni 
1178dedd53c5SPaolo Abeni 		rq->xdp_rxq.mem = rq->xdp_mem;
1179dedd53c5SPaolo Abeni 		xdp_rxq_info_unreg(&rq->xdp_rxq);
1180dedd53c5SPaolo Abeni 
1181dedd53c5SPaolo Abeni 		if (delete_napi)
1182dedd53c5SPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1183dedd53c5SPaolo Abeni 	}
1184dedd53c5SPaolo Abeni }
1185dedd53c5SPaolo Abeni 
veth_enable_xdp(struct net_device * dev)1186dedd53c5SPaolo Abeni static int veth_enable_xdp(struct net_device *dev)
1187dedd53c5SPaolo Abeni {
11885e8d3dc7SHeng Qi 	bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1189dedd53c5SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1190dedd53c5SPaolo Abeni 	int err, i;
1191dedd53c5SPaolo Abeni 
1192dedd53c5SPaolo Abeni 	if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1193dedd53c5SPaolo Abeni 		err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1194dedd53c5SPaolo Abeni 		if (err)
1195dedd53c5SPaolo Abeni 			return err;
1196948d4f21SToshiaki Makita 
1197d3256efdSPaolo Abeni 		if (!napi_already_on) {
1198d3256efdSPaolo Abeni 			err = __veth_napi_enable(dev);
1199dedd53c5SPaolo Abeni 			if (err) {
1200dedd53c5SPaolo Abeni 				veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1201dedd53c5SPaolo Abeni 				return err;
1202dedd53c5SPaolo Abeni 			}
1203d3256efdSPaolo Abeni 		}
1204948d4f21SToshiaki Makita 	}
1205948d4f21SToshiaki Makita 
1206d3256efdSPaolo Abeni 	for (i = 0; i < dev->real_num_rx_queues; i++) {
1207638264dcSToshiaki Makita 		rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1208d3256efdSPaolo Abeni 		rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1209d3256efdSPaolo Abeni 	}
1210948d4f21SToshiaki Makita 
1211948d4f21SToshiaki Makita 	return 0;
1212948d4f21SToshiaki Makita }
1213948d4f21SToshiaki Makita 
veth_disable_xdp(struct net_device * dev)1214948d4f21SToshiaki Makita static void veth_disable_xdp(struct net_device *dev)
1215948d4f21SToshiaki Makita {
1216948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1217638264dcSToshiaki Makita 	int i;
1218948d4f21SToshiaki Makita 
1219638264dcSToshiaki Makita 	for (i = 0; i < dev->real_num_rx_queues; i++)
1220638264dcSToshiaki Makita 		rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1221d3256efdSPaolo Abeni 
122216edf51fSJakub Kicinski 	if (!netif_running(dev) || !veth_gro_requested(dev))
1223948d4f21SToshiaki Makita 		veth_napi_del(dev);
1224d3256efdSPaolo Abeni 
1225dedd53c5SPaolo Abeni 	veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1226948d4f21SToshiaki Makita }
1227948d4f21SToshiaki Makita 
veth_napi_enable_range(struct net_device * dev,int start,int end)1228dedd53c5SPaolo Abeni static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1229d3256efdSPaolo Abeni {
1230d3256efdSPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
1231d3256efdSPaolo Abeni 	int err, i;
1232d3256efdSPaolo Abeni 
1233dedd53c5SPaolo Abeni 	for (i = start; i < end; i++) {
1234d3256efdSPaolo Abeni 		struct veth_rq *rq = &priv->rq[i];
1235d3256efdSPaolo Abeni 
1236b48b89f9SJakub Kicinski 		netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1237d3256efdSPaolo Abeni 	}
1238d3256efdSPaolo Abeni 
1239dedd53c5SPaolo Abeni 	err = __veth_napi_enable_range(dev, start, end);
1240d3256efdSPaolo Abeni 	if (err) {
1241dedd53c5SPaolo Abeni 		for (i = start; i < end; i++) {
1242d3256efdSPaolo Abeni 			struct veth_rq *rq = &priv->rq[i];
1243d3256efdSPaolo Abeni 
1244d3256efdSPaolo Abeni 			netif_napi_del(&rq->xdp_napi);
1245d3256efdSPaolo Abeni 		}
1246d3256efdSPaolo Abeni 		return err;
1247d3256efdSPaolo Abeni 	}
1248d3256efdSPaolo Abeni 	return err;
1249d3256efdSPaolo Abeni }
1250d3256efdSPaolo Abeni 
veth_napi_enable(struct net_device * dev)1251dedd53c5SPaolo Abeni static int veth_napi_enable(struct net_device *dev)
1252dedd53c5SPaolo Abeni {
1253dedd53c5SPaolo Abeni 	return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1254dedd53c5SPaolo Abeni }
1255dedd53c5SPaolo Abeni 
veth_disable_range_safe(struct net_device * dev,int start,int end)12564752eeb3SPaolo Abeni static void veth_disable_range_safe(struct net_device *dev, int start, int end)
12574752eeb3SPaolo Abeni {
12584752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
12594752eeb3SPaolo Abeni 
12604752eeb3SPaolo Abeni 	if (start >= end)
12614752eeb3SPaolo Abeni 		return;
12624752eeb3SPaolo Abeni 
12634752eeb3SPaolo Abeni 	if (priv->_xdp_prog) {
12644752eeb3SPaolo Abeni 		veth_napi_del_range(dev, start, end);
12654752eeb3SPaolo Abeni 		veth_disable_xdp_range(dev, start, end, false);
12664752eeb3SPaolo Abeni 	} else if (veth_gro_requested(dev)) {
12674752eeb3SPaolo Abeni 		veth_napi_del_range(dev, start, end);
12684752eeb3SPaolo Abeni 	}
12694752eeb3SPaolo Abeni }
12704752eeb3SPaolo Abeni 
veth_enable_range_safe(struct net_device * dev,int start,int end)12714752eeb3SPaolo Abeni static int veth_enable_range_safe(struct net_device *dev, int start, int end)
12724752eeb3SPaolo Abeni {
12734752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
12744752eeb3SPaolo Abeni 	int err;
12754752eeb3SPaolo Abeni 
12764752eeb3SPaolo Abeni 	if (start >= end)
12774752eeb3SPaolo Abeni 		return 0;
12784752eeb3SPaolo Abeni 
12794752eeb3SPaolo Abeni 	if (priv->_xdp_prog) {
12804752eeb3SPaolo Abeni 		/* these channels are freshly initialized, napi is not on there even
12814752eeb3SPaolo Abeni 		 * when GRO is requeste
12824752eeb3SPaolo Abeni 		 */
12834752eeb3SPaolo Abeni 		err = veth_enable_xdp_range(dev, start, end, false);
12844752eeb3SPaolo Abeni 		if (err)
12854752eeb3SPaolo Abeni 			return err;
12864752eeb3SPaolo Abeni 
12874752eeb3SPaolo Abeni 		err = __veth_napi_enable_range(dev, start, end);
12884752eeb3SPaolo Abeni 		if (err) {
12894752eeb3SPaolo Abeni 			/* on error always delete the newly added napis */
12904752eeb3SPaolo Abeni 			veth_disable_xdp_range(dev, start, end, true);
12914752eeb3SPaolo Abeni 			return err;
12924752eeb3SPaolo Abeni 		}
12934752eeb3SPaolo Abeni 	} else if (veth_gro_requested(dev)) {
12944752eeb3SPaolo Abeni 		return veth_napi_enable_range(dev, start, end);
12954752eeb3SPaolo Abeni 	}
12964752eeb3SPaolo Abeni 	return 0;
12974752eeb3SPaolo Abeni }
12984752eeb3SPaolo Abeni 
veth_set_xdp_features(struct net_device * dev)1299fccca038SLorenzo Bianconi static void veth_set_xdp_features(struct net_device *dev)
1300fccca038SLorenzo Bianconi {
1301fccca038SLorenzo Bianconi 	struct veth_priv *priv = netdev_priv(dev);
1302fccca038SLorenzo Bianconi 	struct net_device *peer;
1303fccca038SLorenzo Bianconi 
13045ce76fe1SLorenzo Bianconi 	peer = rtnl_dereference(priv->peer);
1305fccca038SLorenzo Bianconi 	if (peer && peer->real_num_tx_queues <= dev->real_num_rx_queues) {
13068267fc71SLorenzo Bianconi 		struct veth_priv *priv_peer = netdev_priv(peer);
1307fccca038SLorenzo Bianconi 		xdp_features_t val = NETDEV_XDP_ACT_BASIC |
1308fccca038SLorenzo Bianconi 				     NETDEV_XDP_ACT_REDIRECT |
1309fccca038SLorenzo Bianconi 				     NETDEV_XDP_ACT_RX_SG;
1310fccca038SLorenzo Bianconi 
13118267fc71SLorenzo Bianconi 		if (priv_peer->_xdp_prog || veth_gro_requested(peer))
1312fccca038SLorenzo Bianconi 			val |= NETDEV_XDP_ACT_NDO_XMIT |
1313fccca038SLorenzo Bianconi 			       NETDEV_XDP_ACT_NDO_XMIT_SG;
1314fccca038SLorenzo Bianconi 		xdp_set_features_flag(dev, val);
1315fccca038SLorenzo Bianconi 	} else {
1316fccca038SLorenzo Bianconi 		xdp_clear_features_flag(dev);
1317fccca038SLorenzo Bianconi 	}
1318fccca038SLorenzo Bianconi }
1319fccca038SLorenzo Bianconi 
veth_set_channels(struct net_device * dev,struct ethtool_channels * ch)13204752eeb3SPaolo Abeni static int veth_set_channels(struct net_device *dev,
13214752eeb3SPaolo Abeni 			     struct ethtool_channels *ch)
13224752eeb3SPaolo Abeni {
13234752eeb3SPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
13244752eeb3SPaolo Abeni 	unsigned int old_rx_count, new_rx_count;
13254752eeb3SPaolo Abeni 	struct veth_priv *peer_priv;
13264752eeb3SPaolo Abeni 	struct net_device *peer;
13274752eeb3SPaolo Abeni 	int err;
13284752eeb3SPaolo Abeni 
13294752eeb3SPaolo Abeni 	/* sanity check. Upper bounds are already enforced by the caller */
13304752eeb3SPaolo Abeni 	if (!ch->rx_count || !ch->tx_count)
13314752eeb3SPaolo Abeni 		return -EINVAL;
13324752eeb3SPaolo Abeni 
13334752eeb3SPaolo Abeni 	/* avoid braking XDP, if that is enabled */
13344752eeb3SPaolo Abeni 	peer = rtnl_dereference(priv->peer);
13354752eeb3SPaolo Abeni 	peer_priv = peer ? netdev_priv(peer) : NULL;
13364752eeb3SPaolo Abeni 	if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
13374752eeb3SPaolo Abeni 		return -EINVAL;
13384752eeb3SPaolo Abeni 
13394752eeb3SPaolo Abeni 	if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
13404752eeb3SPaolo Abeni 		return -EINVAL;
13414752eeb3SPaolo Abeni 
13424752eeb3SPaolo Abeni 	old_rx_count = dev->real_num_rx_queues;
13434752eeb3SPaolo Abeni 	new_rx_count = ch->rx_count;
13444752eeb3SPaolo Abeni 	if (netif_running(dev)) {
13454752eeb3SPaolo Abeni 		/* turn device off */
13464752eeb3SPaolo Abeni 		netif_carrier_off(dev);
13474752eeb3SPaolo Abeni 		if (peer)
13484752eeb3SPaolo Abeni 			netif_carrier_off(peer);
13494752eeb3SPaolo Abeni 
13504752eeb3SPaolo Abeni 		/* try to allocate new resurces, as needed*/
13514752eeb3SPaolo Abeni 		err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
13524752eeb3SPaolo Abeni 		if (err)
13534752eeb3SPaolo Abeni 			goto out;
13544752eeb3SPaolo Abeni 	}
13554752eeb3SPaolo Abeni 
13564752eeb3SPaolo Abeni 	err = netif_set_real_num_rx_queues(dev, ch->rx_count);
13574752eeb3SPaolo Abeni 	if (err)
13584752eeb3SPaolo Abeni 		goto revert;
13594752eeb3SPaolo Abeni 
13604752eeb3SPaolo Abeni 	err = netif_set_real_num_tx_queues(dev, ch->tx_count);
13614752eeb3SPaolo Abeni 	if (err) {
13624752eeb3SPaolo Abeni 		int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
13634752eeb3SPaolo Abeni 
13644752eeb3SPaolo Abeni 		/* this error condition could happen only if rx and tx change
13654752eeb3SPaolo Abeni 		 * in opposite directions (e.g. tx nr raises, rx nr decreases)
13664752eeb3SPaolo Abeni 		 * and we can't do anything to fully restore the original
13674752eeb3SPaolo Abeni 		 * status
13684752eeb3SPaolo Abeni 		 */
13694752eeb3SPaolo Abeni 		if (err2)
13704752eeb3SPaolo Abeni 			pr_warn("Can't restore rx queues config %d -> %d %d",
13714752eeb3SPaolo Abeni 				new_rx_count, old_rx_count, err2);
13724752eeb3SPaolo Abeni 		else
13734752eeb3SPaolo Abeni 			goto revert;
13744752eeb3SPaolo Abeni 	}
13754752eeb3SPaolo Abeni 
13764752eeb3SPaolo Abeni out:
13774752eeb3SPaolo Abeni 	if (netif_running(dev)) {
13784752eeb3SPaolo Abeni 		/* note that we need to swap the arguments WRT the enable part
13794752eeb3SPaolo Abeni 		 * to identify the range we have to disable
13804752eeb3SPaolo Abeni 		 */
13814752eeb3SPaolo Abeni 		veth_disable_range_safe(dev, new_rx_count, old_rx_count);
13824752eeb3SPaolo Abeni 		netif_carrier_on(dev);
13834752eeb3SPaolo Abeni 		if (peer)
13844752eeb3SPaolo Abeni 			netif_carrier_on(peer);
13854752eeb3SPaolo Abeni 	}
1386fccca038SLorenzo Bianconi 
1387fccca038SLorenzo Bianconi 	/* update XDP supported features */
1388fccca038SLorenzo Bianconi 	veth_set_xdp_features(dev);
1389fccca038SLorenzo Bianconi 	if (peer)
1390fccca038SLorenzo Bianconi 		veth_set_xdp_features(peer);
1391fccca038SLorenzo Bianconi 
13924752eeb3SPaolo Abeni 	return err;
13934752eeb3SPaolo Abeni 
13944752eeb3SPaolo Abeni revert:
13954752eeb3SPaolo Abeni 	new_rx_count = old_rx_count;
13964752eeb3SPaolo Abeni 	old_rx_count = ch->rx_count;
13974752eeb3SPaolo Abeni 	goto out;
13984752eeb3SPaolo Abeni }
13994752eeb3SPaolo Abeni 
veth_open(struct net_device * dev)1400e314dbdcSPavel Emelyanov static int veth_open(struct net_device *dev)
1401e314dbdcSPavel Emelyanov {
14025e8d3dc7SHeng Qi 	struct veth_priv *priv = netdev_priv(dev);
1403d0e2c55eSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
1404948d4f21SToshiaki Makita 	int err;
1405e314dbdcSPavel Emelyanov 
1406d0e2c55eSEric Dumazet 	if (!peer)
1407e314dbdcSPavel Emelyanov 		return -ENOTCONN;
1408e314dbdcSPavel Emelyanov 
1409948d4f21SToshiaki Makita 	if (priv->_xdp_prog) {
1410948d4f21SToshiaki Makita 		err = veth_enable_xdp(dev);
1411948d4f21SToshiaki Makita 		if (err)
1412948d4f21SToshiaki Makita 			return err;
14135e8d3dc7SHeng Qi 	} else if (veth_gro_requested(dev)) {
1414d3256efdSPaolo Abeni 		err = veth_napi_enable(dev);
1415d3256efdSPaolo Abeni 		if (err)
1416d3256efdSPaolo Abeni 			return err;
1417948d4f21SToshiaki Makita 	}
1418948d4f21SToshiaki Makita 
1419d0e2c55eSEric Dumazet 	if (peer->flags & IFF_UP) {
1420e314dbdcSPavel Emelyanov 		netif_carrier_on(dev);
1421d0e2c55eSEric Dumazet 		netif_carrier_on(peer);
1422e314dbdcSPavel Emelyanov 	}
1423948d4f21SToshiaki Makita 
14247a6102aaSToke Høiland-Jørgensen 	veth_set_xdp_features(dev);
14257a6102aaSToke Høiland-Jørgensen 
1426e314dbdcSPavel Emelyanov 	return 0;
1427e314dbdcSPavel Emelyanov }
1428e314dbdcSPavel Emelyanov 
veth_close(struct net_device * dev)14292cf48a10SEric W. Biederman static int veth_close(struct net_device *dev)
14302cf48a10SEric W. Biederman {
14315e8d3dc7SHeng Qi 	struct veth_priv *priv = netdev_priv(dev);
14322efd32eeSEric Dumazet 	struct net_device *peer = rtnl_dereference(priv->peer);
14332cf48a10SEric W. Biederman 
14342cf48a10SEric W. Biederman 	netif_carrier_off(dev);
14352efd32eeSEric Dumazet 	if (peer)
14362efd32eeSEric Dumazet 		netif_carrier_off(peer);
14372cf48a10SEric W. Biederman 
14385e8d3dc7SHeng Qi 	if (priv->_xdp_prog)
14395e8d3dc7SHeng Qi 		veth_disable_xdp(dev);
14405e8d3dc7SHeng Qi 	else if (veth_gro_requested(dev))
14415e8d3dc7SHeng Qi 		veth_napi_del(dev);
14425e8d3dc7SHeng Qi 
14432cf48a10SEric W. Biederman 	return 0;
14442cf48a10SEric W. Biederman }
14452cf48a10SEric W. Biederman 
is_valid_veth_mtu(int mtu)144691572088SJarod Wilson static int is_valid_veth_mtu(int mtu)
144738d40815SEric Biederman {
144891572088SJarod Wilson 	return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
144938d40815SEric Biederman }
145038d40815SEric Biederman 
veth_alloc_queues(struct net_device * dev)14517797b93bSToshiaki Makita static int veth_alloc_queues(struct net_device *dev)
14527797b93bSToshiaki Makita {
14537797b93bSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
14547797b93bSToshiaki Makita 	int i;
14557797b93bSToshiaki Makita 
14562d8003e5SJakub Kicinski 	priv->rq = kvcalloc(dev->num_rx_queues, sizeof(*priv->rq),
14572d8003e5SJakub Kicinski 			    GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
14587797b93bSToshiaki Makita 	if (!priv->rq)
14597797b93bSToshiaki Makita 		return -ENOMEM;
14607797b93bSToshiaki Makita 
14614195e54aSToshiaki Makita 	for (i = 0; i < dev->num_rx_queues; i++) {
14627797b93bSToshiaki Makita 		priv->rq[i].dev = dev;
14634195e54aSToshiaki Makita 		u64_stats_init(&priv->rq[i].stats.syncp);
14644195e54aSToshiaki Makita 	}
14657797b93bSToshiaki Makita 
14667797b93bSToshiaki Makita 	return 0;
14677797b93bSToshiaki Makita }
14687797b93bSToshiaki Makita 
veth_free_queues(struct net_device * dev)14697797b93bSToshiaki Makita static void veth_free_queues(struct net_device *dev)
14707797b93bSToshiaki Makita {
14717797b93bSToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
14727797b93bSToshiaki Makita 
14732d8003e5SJakub Kicinski 	kvfree(priv->rq);
14747797b93bSToshiaki Makita }
14757797b93bSToshiaki Makita 
veth_dev_init(struct net_device * dev)1476e314dbdcSPavel Emelyanov static int veth_dev_init(struct net_device *dev)
1477e314dbdcSPavel Emelyanov {
1478a7b862abSEric Dumazet 	netdev_lockdep_set_classes(dev);
14796ae7b3fcSDaniel Borkmann 	return veth_alloc_queues(dev);
1480e314dbdcSPavel Emelyanov }
1481e314dbdcSPavel Emelyanov 
veth_dev_free(struct net_device * dev)148211687a10SDavid S. Miller static void veth_dev_free(struct net_device *dev)
148311687a10SDavid S. Miller {
14847797b93bSToshiaki Makita 	veth_free_queues(dev);
148511687a10SDavid S. Miller }
148611687a10SDavid S. Miller 
1487bb446c19SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
veth_poll_controller(struct net_device * dev)1488bb446c19SWANG Cong static void veth_poll_controller(struct net_device *dev)
1489bb446c19SWANG Cong {
1490bb446c19SWANG Cong 	/* veth only receives frames when its peer sends one
1491948d4f21SToshiaki Makita 	 * Since it has nothing to do with disabling irqs, we are guaranteed
1492bb446c19SWANG Cong 	 * never to have pending data when we poll for it so
1493bb446c19SWANG Cong 	 * there is nothing to do here.
1494bb446c19SWANG Cong 	 *
1495bb446c19SWANG Cong 	 * We need this though so netpoll recognizes us as an interface that
1496bb446c19SWANG Cong 	 * supports polling, which enables bridge devices in virt setups to
1497bb446c19SWANG Cong 	 * still use netconsole
1498bb446c19SWANG Cong 	 */
1499bb446c19SWANG Cong }
1500bb446c19SWANG Cong #endif	/* CONFIG_NET_POLL_CONTROLLER */
1501bb446c19SWANG Cong 
veth_get_iflink(const struct net_device * dev)1502a45253bfSNicolas Dichtel static int veth_get_iflink(const struct net_device *dev)
1503a45253bfSNicolas Dichtel {
1504a45253bfSNicolas Dichtel 	struct veth_priv *priv = netdev_priv(dev);
1505a45253bfSNicolas Dichtel 	struct net_device *peer;
1506a45253bfSNicolas Dichtel 	int iflink;
1507a45253bfSNicolas Dichtel 
1508a45253bfSNicolas Dichtel 	rcu_read_lock();
1509a45253bfSNicolas Dichtel 	peer = rcu_dereference(priv->peer);
1510a45253bfSNicolas Dichtel 	iflink = peer ? peer->ifindex : 0;
1511a45253bfSNicolas Dichtel 	rcu_read_unlock();
1512a45253bfSNicolas Dichtel 
1513a45253bfSNicolas Dichtel 	return iflink;
1514a45253bfSNicolas Dichtel }
1515a45253bfSNicolas Dichtel 
veth_fix_features(struct net_device * dev,netdev_features_t features)1516dc224822SToshiaki Makita static netdev_features_t veth_fix_features(struct net_device *dev,
1517dc224822SToshiaki Makita 					   netdev_features_t features)
1518dc224822SToshiaki Makita {
1519dc224822SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1520dc224822SToshiaki Makita 	struct net_device *peer;
1521dc224822SToshiaki Makita 
1522dc224822SToshiaki Makita 	peer = rtnl_dereference(priv->peer);
1523dc224822SToshiaki Makita 	if (peer) {
1524dc224822SToshiaki Makita 		struct veth_priv *peer_priv = netdev_priv(peer);
1525dc224822SToshiaki Makita 
1526dc224822SToshiaki Makita 		if (peer_priv->_xdp_prog)
1527dc224822SToshiaki Makita 			features &= ~NETIF_F_GSO_SOFTWARE;
1528dc224822SToshiaki Makita 	}
1529dc224822SToshiaki Makita 
1530dc224822SToshiaki Makita 	return features;
1531dc224822SToshiaki Makita }
1532dc224822SToshiaki Makita 
veth_set_features(struct net_device * dev,netdev_features_t features)1533d3256efdSPaolo Abeni static int veth_set_features(struct net_device *dev,
1534d3256efdSPaolo Abeni 			     netdev_features_t features)
1535d3256efdSPaolo Abeni {
1536d3256efdSPaolo Abeni 	netdev_features_t changed = features ^ dev->features;
1537d3256efdSPaolo Abeni 	struct veth_priv *priv = netdev_priv(dev);
15388267fc71SLorenzo Bianconi 	struct net_device *peer;
1539d3256efdSPaolo Abeni 	int err;
1540d3256efdSPaolo Abeni 
1541d3256efdSPaolo Abeni 	if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1542d3256efdSPaolo Abeni 		return 0;
1543d3256efdSPaolo Abeni 
15448267fc71SLorenzo Bianconi 	peer = rtnl_dereference(priv->peer);
1545d3256efdSPaolo Abeni 	if (features & NETIF_F_GRO) {
1546d3256efdSPaolo Abeni 		err = veth_napi_enable(dev);
1547d3256efdSPaolo Abeni 		if (err)
1548d3256efdSPaolo Abeni 			return err;
1549fccca038SLorenzo Bianconi 
15508267fc71SLorenzo Bianconi 		if (peer)
15518267fc71SLorenzo Bianconi 			xdp_features_set_redirect_target(peer, true);
1552d3256efdSPaolo Abeni 	} else {
15538267fc71SLorenzo Bianconi 		if (peer)
15548267fc71SLorenzo Bianconi 			xdp_features_clear_redirect_target(peer);
1555d3256efdSPaolo Abeni 		veth_napi_del(dev);
1556d3256efdSPaolo Abeni 	}
1557d3256efdSPaolo Abeni 	return 0;
1558d3256efdSPaolo Abeni }
1559d3256efdSPaolo Abeni 
veth_set_rx_headroom(struct net_device * dev,int new_hr)1560163e5292SPaolo Abeni static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1561163e5292SPaolo Abeni {
1562163e5292SPaolo Abeni 	struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1563163e5292SPaolo Abeni 	struct net_device *peer;
1564163e5292SPaolo Abeni 
1565163e5292SPaolo Abeni 	if (new_hr < 0)
1566163e5292SPaolo Abeni 		new_hr = 0;
1567163e5292SPaolo Abeni 
1568163e5292SPaolo Abeni 	rcu_read_lock();
1569163e5292SPaolo Abeni 	peer = rcu_dereference(priv->peer);
1570163e5292SPaolo Abeni 	if (unlikely(!peer))
1571163e5292SPaolo Abeni 		goto out;
1572163e5292SPaolo Abeni 
1573163e5292SPaolo Abeni 	peer_priv = netdev_priv(peer);
1574163e5292SPaolo Abeni 	priv->requested_headroom = new_hr;
1575163e5292SPaolo Abeni 	new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1576163e5292SPaolo Abeni 	dev->needed_headroom = new_hr;
1577163e5292SPaolo Abeni 	peer->needed_headroom = new_hr;
1578163e5292SPaolo Abeni 
1579163e5292SPaolo Abeni out:
1580163e5292SPaolo Abeni 	rcu_read_unlock();
1581163e5292SPaolo Abeni }
1582163e5292SPaolo Abeni 
veth_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)1583948d4f21SToshiaki Makita static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1584948d4f21SToshiaki Makita 			struct netlink_ext_ack *extack)
1585948d4f21SToshiaki Makita {
1586948d4f21SToshiaki Makita 	struct veth_priv *priv = netdev_priv(dev);
1587948d4f21SToshiaki Makita 	struct bpf_prog *old_prog;
1588948d4f21SToshiaki Makita 	struct net_device *peer;
1589dc224822SToshiaki Makita 	unsigned int max_mtu;
1590948d4f21SToshiaki Makita 	int err;
1591948d4f21SToshiaki Makita 
1592948d4f21SToshiaki Makita 	old_prog = priv->_xdp_prog;
1593948d4f21SToshiaki Makita 	priv->_xdp_prog = prog;
1594948d4f21SToshiaki Makita 	peer = rtnl_dereference(priv->peer);
1595948d4f21SToshiaki Makita 
1596948d4f21SToshiaki Makita 	if (prog) {
1597948d4f21SToshiaki Makita 		if (!peer) {
1598948d4f21SToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1599948d4f21SToshiaki Makita 			err = -ENOTCONN;
1600948d4f21SToshiaki Makita 			goto err;
1601948d4f21SToshiaki Makita 		}
1602948d4f21SToshiaki Makita 
16037cda76d8SLorenzo Bianconi 		max_mtu = SKB_WITH_OVERHEAD(PAGE_SIZE - VETH_XDP_HEADROOM) -
16047cda76d8SLorenzo Bianconi 			  peer->hard_header_len;
16057cda76d8SLorenzo Bianconi 		/* Allow increasing the max_mtu if the program supports
16067cda76d8SLorenzo Bianconi 		 * XDP fragments.
16077cda76d8SLorenzo Bianconi 		 */
16087cda76d8SLorenzo Bianconi 		if (prog->aux->xdp_has_frags)
16097cda76d8SLorenzo Bianconi 			max_mtu += PAGE_SIZE * MAX_SKB_FRAGS;
16107cda76d8SLorenzo Bianconi 
1611dc224822SToshiaki Makita 		if (peer->mtu > max_mtu) {
1612dc224822SToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1613dc224822SToshiaki Makita 			err = -ERANGE;
1614dc224822SToshiaki Makita 			goto err;
1615dc224822SToshiaki Makita 		}
1616dc224822SToshiaki Makita 
1617638264dcSToshiaki Makita 		if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1618638264dcSToshiaki Makita 			NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1619638264dcSToshiaki Makita 			err = -ENOSPC;
1620638264dcSToshiaki Makita 			goto err;
1621638264dcSToshiaki Makita 		}
1622638264dcSToshiaki Makita 
1623948d4f21SToshiaki Makita 		if (dev->flags & IFF_UP) {
1624948d4f21SToshiaki Makita 			err = veth_enable_xdp(dev);
1625948d4f21SToshiaki Makita 			if (err) {
1626948d4f21SToshiaki Makita 				NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1627948d4f21SToshiaki Makita 				goto err;
1628948d4f21SToshiaki Makita 			}
1629948d4f21SToshiaki Makita 		}
1630dc224822SToshiaki Makita 
1631dc224822SToshiaki Makita 		if (!old_prog) {
1632dc224822SToshiaki Makita 			peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1633dc224822SToshiaki Makita 			peer->max_mtu = max_mtu;
1634dc224822SToshiaki Makita 		}
1635fccca038SLorenzo Bianconi 
16368267fc71SLorenzo Bianconi 		xdp_features_set_redirect_target(peer, true);
1637948d4f21SToshiaki Makita 	}
1638948d4f21SToshiaki Makita 
1639948d4f21SToshiaki Makita 	if (old_prog) {
1640dc224822SToshiaki Makita 		if (!prog) {
16418267fc71SLorenzo Bianconi 			if (peer && !veth_gro_requested(dev))
16428267fc71SLorenzo Bianconi 				xdp_features_clear_redirect_target(peer);
1643fccca038SLorenzo Bianconi 
1644dc224822SToshiaki Makita 			if (dev->flags & IFF_UP)
1645948d4f21SToshiaki Makita 				veth_disable_xdp(dev);
1646dc224822SToshiaki Makita 
1647dc224822SToshiaki Makita 			if (peer) {
1648dc224822SToshiaki Makita 				peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1649dc224822SToshiaki Makita 				peer->max_mtu = ETH_MAX_MTU;
1650dc224822SToshiaki Makita 			}
1651dc224822SToshiaki Makita 		}
1652948d4f21SToshiaki Makita 		bpf_prog_put(old_prog);
1653948d4f21SToshiaki Makita 	}
1654948d4f21SToshiaki Makita 
1655dc224822SToshiaki Makita 	if ((!!old_prog ^ !!prog) && peer)
1656dc224822SToshiaki Makita 		netdev_update_features(peer);
1657dc224822SToshiaki Makita 
1658948d4f21SToshiaki Makita 	return 0;
1659948d4f21SToshiaki Makita err:
1660948d4f21SToshiaki Makita 	priv->_xdp_prog = old_prog;
1661948d4f21SToshiaki Makita 
1662948d4f21SToshiaki Makita 	return err;
1663948d4f21SToshiaki Makita }
1664948d4f21SToshiaki Makita 
veth_xdp(struct net_device * dev,struct netdev_bpf * xdp)1665948d4f21SToshiaki Makita static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1666948d4f21SToshiaki Makita {
1667948d4f21SToshiaki Makita 	switch (xdp->command) {
1668948d4f21SToshiaki Makita 	case XDP_SETUP_PROG:
1669948d4f21SToshiaki Makita 		return veth_xdp_set(dev, xdp->prog, xdp->extack);
1670948d4f21SToshiaki Makita 	default:
1671948d4f21SToshiaki Makita 		return -EINVAL;
1672948d4f21SToshiaki Makita 	}
1673948d4f21SToshiaki Makita }
1674948d4f21SToshiaki Makita 
veth_xdp_rx_timestamp(const struct xdp_md * ctx,u64 * timestamp)1675306531f0SStanislav Fomichev static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
1676306531f0SStanislav Fomichev {
1677306531f0SStanislav Fomichev 	struct veth_xdp_buff *_ctx = (void *)ctx;
1678306531f0SStanislav Fomichev 
1679306531f0SStanislav Fomichev 	if (!_ctx->skb)
1680915efd8aSJesper Dangaard Brouer 		return -ENODATA;
1681306531f0SStanislav Fomichev 
1682306531f0SStanislav Fomichev 	*timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
1683306531f0SStanislav Fomichev 	return 0;
1684306531f0SStanislav Fomichev }
1685306531f0SStanislav Fomichev 
veth_xdp_rx_hash(const struct xdp_md * ctx,u32 * hash,enum xdp_rss_hash_type * rss_type)16860cd917a4SJesper Dangaard Brouer static int veth_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
16870cd917a4SJesper Dangaard Brouer 			    enum xdp_rss_hash_type *rss_type)
1688306531f0SStanislav Fomichev {
1689306531f0SStanislav Fomichev 	struct veth_xdp_buff *_ctx = (void *)ctx;
169096b1a098SJesper Dangaard Brouer 	struct sk_buff *skb = _ctx->skb;
1691306531f0SStanislav Fomichev 
169296b1a098SJesper Dangaard Brouer 	if (!skb)
1693915efd8aSJesper Dangaard Brouer 		return -ENODATA;
1694306531f0SStanislav Fomichev 
169596b1a098SJesper Dangaard Brouer 	*hash = skb_get_hash(skb);
169696b1a098SJesper Dangaard Brouer 	*rss_type = skb->l4_hash ? XDP_RSS_TYPE_L4_ANY : XDP_RSS_TYPE_NONE;
169796b1a098SJesper Dangaard Brouer 
1698306531f0SStanislav Fomichev 	return 0;
1699306531f0SStanislav Fomichev }
1700306531f0SStanislav Fomichev 
17014456e7bdSStephen Hemminger static const struct net_device_ops veth_netdev_ops = {
17024456e7bdSStephen Hemminger 	.ndo_init            = veth_dev_init,
17034456e7bdSStephen Hemminger 	.ndo_open            = veth_open,
17042cf48a10SEric W. Biederman 	.ndo_stop            = veth_close,
170500829823SStephen Hemminger 	.ndo_start_xmit      = veth_xmit,
17066311cc44Sstephen hemminger 	.ndo_get_stats64     = veth_get_stats64,
17075c70ef85SGao feng 	.ndo_set_rx_mode     = veth_set_multicast_list,
1708ee923623SDaniel Lezcano 	.ndo_set_mac_address = eth_mac_addr,
1709bb446c19SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
1710bb446c19SWANG Cong 	.ndo_poll_controller	= veth_poll_controller,
1711bb446c19SWANG Cong #endif
1712a45253bfSNicolas Dichtel 	.ndo_get_iflink		= veth_get_iflink,
1713dc224822SToshiaki Makita 	.ndo_fix_features	= veth_fix_features,
1714d3256efdSPaolo Abeni 	.ndo_set_features	= veth_set_features,
17151a04a821SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
1716163e5292SPaolo Abeni 	.ndo_set_rx_headroom	= veth_set_rx_headroom,
1717948d4f21SToshiaki Makita 	.ndo_bpf		= veth_xdp,
17189152cff0SLorenzo Bianconi 	.ndo_xdp_xmit		= veth_ndo_xdp_xmit,
17199aa1206eSDaniel Borkmann 	.ndo_get_peer_dev	= veth_peer_dev,
17204456e7bdSStephen Hemminger };
17214456e7bdSStephen Hemminger 
1722306531f0SStanislav Fomichev static const struct xdp_metadata_ops veth_xdp_metadata_ops = {
1723306531f0SStanislav Fomichev 	.xmo_rx_timestamp		= veth_xdp_rx_timestamp,
1724306531f0SStanislav Fomichev 	.xmo_rx_hash			= veth_xdp_rx_hash,
1725306531f0SStanislav Fomichev };
1726306531f0SStanislav Fomichev 
1727732912d7SAlexander Duyck #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1728c80fafbbSXin Long 		       NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1729732912d7SAlexander Duyck 		       NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
173028d2b136SPatrick McHardy 		       NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
173128d2b136SPatrick McHardy 		       NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
17328093315aSEric Dumazet 
veth_setup(struct net_device * dev)1733e314dbdcSPavel Emelyanov static void veth_setup(struct net_device *dev)
1734e314dbdcSPavel Emelyanov {
1735e314dbdcSPavel Emelyanov 	ether_setup(dev);
1736e314dbdcSPavel Emelyanov 
1737550fd08cSNeil Horman 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
173823ea5a96SHannes Frederic Sowa 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
173902f01ec1SPhil Sutter 	dev->priv_flags |= IFF_NO_QUEUE;
1740163e5292SPaolo Abeni 	dev->priv_flags |= IFF_PHONY_HEADROOM;
1741550fd08cSNeil Horman 
17424456e7bdSStephen Hemminger 	dev->netdev_ops = &veth_netdev_ops;
1743306531f0SStanislav Fomichev 	dev->xdp_metadata_ops = &veth_xdp_metadata_ops;
1744e314dbdcSPavel Emelyanov 	dev->ethtool_ops = &veth_ethtool_ops;
1745e314dbdcSPavel Emelyanov 	dev->features |= NETIF_F_LLTX;
17468093315aSEric Dumazet 	dev->features |= VETH_FEATURES;
17478d0d21f4SToshiaki Makita 	dev->vlan_features = dev->features &
17483f8c707bSVlad Yasevich 			     ~(NETIF_F_HW_VLAN_CTAG_TX |
17493f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_STAG_TX |
17503f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_CTAG_RX |
17513f8c707bSVlad Yasevich 			       NETIF_F_HW_VLAN_STAG_RX);
1752cf124db5SDavid S. Miller 	dev->needs_free_netdev = true;
1753cf124db5SDavid S. Miller 	dev->priv_destructor = veth_dev_free;
1754b74095a4SPeilin Ye 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
175591572088SJarod Wilson 	dev->max_mtu = ETH_MAX_MTU;
1756a2c725faSMichał Mirosław 
17578093315aSEric Dumazet 	dev->hw_features = VETH_FEATURES;
175882d81898SEric Dumazet 	dev->hw_enc_features = VETH_FEATURES;
1759607fca9aSDavid Ahern 	dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1760d406099dSEric Dumazet 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
1761e314dbdcSPavel Emelyanov }
1762e314dbdcSPavel Emelyanov 
1763e314dbdcSPavel Emelyanov /*
1764e314dbdcSPavel Emelyanov  * netlink interface
1765e314dbdcSPavel Emelyanov  */
1766e314dbdcSPavel Emelyanov 
veth_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1767a8b8a889SMatthias Schiffer static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1768a8b8a889SMatthias Schiffer 			 struct netlink_ext_ack *extack)
1769e314dbdcSPavel Emelyanov {
1770e314dbdcSPavel Emelyanov 	if (tb[IFLA_ADDRESS]) {
1771e314dbdcSPavel Emelyanov 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1772e314dbdcSPavel Emelyanov 			return -EINVAL;
1773e314dbdcSPavel Emelyanov 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1774e314dbdcSPavel Emelyanov 			return -EADDRNOTAVAIL;
1775e314dbdcSPavel Emelyanov 	}
177638d40815SEric Biederman 	if (tb[IFLA_MTU]) {
177738d40815SEric Biederman 		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
177838d40815SEric Biederman 			return -EINVAL;
177938d40815SEric Biederman 	}
1780e314dbdcSPavel Emelyanov 	return 0;
1781e314dbdcSPavel Emelyanov }
1782e314dbdcSPavel Emelyanov 
1783e314dbdcSPavel Emelyanov static struct rtnl_link_ops veth_link_ops;
1784e314dbdcSPavel Emelyanov 
veth_disable_gro(struct net_device * dev)1785d3256efdSPaolo Abeni static void veth_disable_gro(struct net_device *dev)
1786d3256efdSPaolo Abeni {
1787d3256efdSPaolo Abeni 	dev->features &= ~NETIF_F_GRO;
1788d3256efdSPaolo Abeni 	dev->wanted_features &= ~NETIF_F_GRO;
1789d3256efdSPaolo Abeni 	netdev_update_features(dev);
1790d3256efdSPaolo Abeni }
1791d3256efdSPaolo Abeni 
veth_init_queues(struct net_device * dev,struct nlattr * tb[])17929d3684c2SPaolo Abeni static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
17939d3684c2SPaolo Abeni {
17949d3684c2SPaolo Abeni 	int err;
17959d3684c2SPaolo Abeni 
17969d3684c2SPaolo Abeni 	if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
17979d3684c2SPaolo Abeni 		err = netif_set_real_num_tx_queues(dev, 1);
17989d3684c2SPaolo Abeni 		if (err)
17999d3684c2SPaolo Abeni 			return err;
18009d3684c2SPaolo Abeni 	}
18019d3684c2SPaolo Abeni 	if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
18029d3684c2SPaolo Abeni 		err = netif_set_real_num_rx_queues(dev, 1);
18039d3684c2SPaolo Abeni 		if (err)
18049d3684c2SPaolo Abeni 			return err;
18059d3684c2SPaolo Abeni 	}
18069d3684c2SPaolo Abeni 	return 0;
18079d3684c2SPaolo Abeni }
18089d3684c2SPaolo Abeni 
veth_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)180981adee47SEric W. Biederman static int veth_newlink(struct net *src_net, struct net_device *dev,
18107a3f4a18SMatthias Schiffer 			struct nlattr *tb[], struct nlattr *data[],
18117a3f4a18SMatthias Schiffer 			struct netlink_ext_ack *extack)
1812e314dbdcSPavel Emelyanov {
18137797b93bSToshiaki Makita 	int err;
1814e314dbdcSPavel Emelyanov 	struct net_device *peer;
1815e314dbdcSPavel Emelyanov 	struct veth_priv *priv;
1816e314dbdcSPavel Emelyanov 	char ifname[IFNAMSIZ];
1817e314dbdcSPavel Emelyanov 	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
18185517750fSTom Gundersen 	unsigned char name_assign_type;
18193729d502SPatrick McHardy 	struct ifinfomsg *ifmp;
182081adee47SEric W. Biederman 	struct net *net;
1821e314dbdcSPavel Emelyanov 
1822e314dbdcSPavel Emelyanov 	/*
1823e314dbdcSPavel Emelyanov 	 * create and register peer first
1824e314dbdcSPavel Emelyanov 	 */
1825e314dbdcSPavel Emelyanov 	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1826e314dbdcSPavel Emelyanov 		struct nlattr *nla_peer;
1827e314dbdcSPavel Emelyanov 
1828e314dbdcSPavel Emelyanov 		nla_peer = data[VETH_INFO_PEER];
18293729d502SPatrick McHardy 		ifmp = nla_data(nla_peer);
1830f534f658SJakub Kicinski 		err = rtnl_nla_parse_ifinfomsg(peer_tb, nla_peer, extack);
1831e314dbdcSPavel Emelyanov 		if (err < 0)
1832e314dbdcSPavel Emelyanov 			return err;
1833e314dbdcSPavel Emelyanov 
1834a8b8a889SMatthias Schiffer 		err = veth_validate(peer_tb, NULL, extack);
1835e314dbdcSPavel Emelyanov 		if (err < 0)
1836e314dbdcSPavel Emelyanov 			return err;
1837e314dbdcSPavel Emelyanov 
1838e314dbdcSPavel Emelyanov 		tbp = peer_tb;
18393729d502SPatrick McHardy 	} else {
18403729d502SPatrick McHardy 		ifmp = NULL;
1841e314dbdcSPavel Emelyanov 		tbp = tb;
18423729d502SPatrick McHardy 	}
1843e314dbdcSPavel Emelyanov 
1844191cdb38SSerhey Popovych 	if (ifmp && tbp[IFLA_IFNAME]) {
1845872f6903SFrancis Laniel 		nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
18465517750fSTom Gundersen 		name_assign_type = NET_NAME_USER;
18475517750fSTom Gundersen 	} else {
1848e314dbdcSPavel Emelyanov 		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
18495517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
18505517750fSTom Gundersen 	}
1851e314dbdcSPavel Emelyanov 
185281adee47SEric W. Biederman 	net = rtnl_link_get_net(src_net, tbp);
185381adee47SEric W. Biederman 	if (IS_ERR(net))
185481adee47SEric W. Biederman 		return PTR_ERR(net);
185581adee47SEric W. Biederman 
18565517750fSTom Gundersen 	peer = rtnl_create_link(net, ifname, name_assign_type,
1857d0522f1cSDavid Ahern 				&veth_link_ops, tbp, extack);
185881adee47SEric W. Biederman 	if (IS_ERR(peer)) {
185981adee47SEric W. Biederman 		put_net(net);
1860e314dbdcSPavel Emelyanov 		return PTR_ERR(peer);
186181adee47SEric W. Biederman 	}
1862e314dbdcSPavel Emelyanov 
1863191cdb38SSerhey Popovych 	if (!ifmp || !tbp[IFLA_ADDRESS])
1864f2cedb63SDanny Kukawka 		eth_hw_addr_random(peer);
1865e314dbdcSPavel Emelyanov 
1866e6f8f1a7SPavel Emelyanov 	if (ifmp && (dev->ifindex != 0))
1867e6f8f1a7SPavel Emelyanov 		peer->ifindex = ifmp->ifi_index;
1868e6f8f1a7SPavel Emelyanov 
18696df6398fSJakub Kicinski 	netif_inherit_tso_max(peer, dev);
187072d24955SStephen Hemminger 
1871e314dbdcSPavel Emelyanov 	err = register_netdevice(peer);
187281adee47SEric W. Biederman 	put_net(net);
187381adee47SEric W. Biederman 	net = NULL;
1874e314dbdcSPavel Emelyanov 	if (err < 0)
1875e314dbdcSPavel Emelyanov 		goto err_register_peer;
1876e314dbdcSPavel Emelyanov 
1877d3256efdSPaolo Abeni 	/* keep GRO disabled by default to be consistent with the established
1878d3256efdSPaolo Abeni 	 * veth behavior
1879d3256efdSPaolo Abeni 	 */
1880d3256efdSPaolo Abeni 	veth_disable_gro(peer);
1881e314dbdcSPavel Emelyanov 	netif_carrier_off(peer);
1882e314dbdcSPavel Emelyanov 
18831d997f10SHangbin Liu 	err = rtnl_configure_link(peer, ifmp, 0, NULL);
18843729d502SPatrick McHardy 	if (err < 0)
18853729d502SPatrick McHardy 		goto err_configure_peer;
18863729d502SPatrick McHardy 
1887e314dbdcSPavel Emelyanov 	/*
1888e314dbdcSPavel Emelyanov 	 * register dev last
1889e314dbdcSPavel Emelyanov 	 *
1890e314dbdcSPavel Emelyanov 	 * note, that since we've registered new device the dev's name
1891e314dbdcSPavel Emelyanov 	 * should be re-allocated
1892e314dbdcSPavel Emelyanov 	 */
1893e314dbdcSPavel Emelyanov 
1894e314dbdcSPavel Emelyanov 	if (tb[IFLA_ADDRESS] == NULL)
1895f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1896e314dbdcSPavel Emelyanov 
18976c8c4446SJiri Pirko 	if (tb[IFLA_IFNAME])
1898872f6903SFrancis Laniel 		nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
18996c8c4446SJiri Pirko 	else
19006c8c4446SJiri Pirko 		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
19016c8c4446SJiri Pirko 
1902e314dbdcSPavel Emelyanov 	err = register_netdevice(dev);
1903e314dbdcSPavel Emelyanov 	if (err < 0)
1904e314dbdcSPavel Emelyanov 		goto err_register_dev;
1905e314dbdcSPavel Emelyanov 
1906e314dbdcSPavel Emelyanov 	netif_carrier_off(dev);
1907e314dbdcSPavel Emelyanov 
1908e314dbdcSPavel Emelyanov 	/*
1909e314dbdcSPavel Emelyanov 	 * tie the deviced together
1910e314dbdcSPavel Emelyanov 	 */
1911e314dbdcSPavel Emelyanov 
1912e314dbdcSPavel Emelyanov 	priv = netdev_priv(dev);
1913d0e2c55eSEric Dumazet 	rcu_assign_pointer(priv->peer, peer);
19149d3684c2SPaolo Abeni 	err = veth_init_queues(dev, tb);
19159d3684c2SPaolo Abeni 	if (err)
19169d3684c2SPaolo Abeni 		goto err_queues;
1917e314dbdcSPavel Emelyanov 
1918e314dbdcSPavel Emelyanov 	priv = netdev_priv(peer);
1919d0e2c55eSEric Dumazet 	rcu_assign_pointer(priv->peer, dev);
19209d3684c2SPaolo Abeni 	err = veth_init_queues(peer, tb);
19219d3684c2SPaolo Abeni 	if (err)
19229d3684c2SPaolo Abeni 		goto err_queues;
1923948d4f21SToshiaki Makita 
1924d3256efdSPaolo Abeni 	veth_disable_gro(dev);
1925fccca038SLorenzo Bianconi 	/* update XDP supported features */
1926fccca038SLorenzo Bianconi 	veth_set_xdp_features(dev);
1927fccca038SLorenzo Bianconi 	veth_set_xdp_features(peer);
1928fccca038SLorenzo Bianconi 
1929e314dbdcSPavel Emelyanov 	return 0;
1930e314dbdcSPavel Emelyanov 
19319d3684c2SPaolo Abeni err_queues:
19329d3684c2SPaolo Abeni 	unregister_netdevice(dev);
1933e314dbdcSPavel Emelyanov err_register_dev:
1934e314dbdcSPavel Emelyanov 	/* nothing to do */
19353729d502SPatrick McHardy err_configure_peer:
1936e314dbdcSPavel Emelyanov 	unregister_netdevice(peer);
1937e314dbdcSPavel Emelyanov 	return err;
1938e314dbdcSPavel Emelyanov 
1939e314dbdcSPavel Emelyanov err_register_peer:
1940e314dbdcSPavel Emelyanov 	free_netdev(peer);
1941e314dbdcSPavel Emelyanov 	return err;
1942e314dbdcSPavel Emelyanov }
1943e314dbdcSPavel Emelyanov 
veth_dellink(struct net_device * dev,struct list_head * head)194423289a37SEric Dumazet static void veth_dellink(struct net_device *dev, struct list_head *head)
1945e314dbdcSPavel Emelyanov {
1946e314dbdcSPavel Emelyanov 	struct veth_priv *priv;
1947e314dbdcSPavel Emelyanov 	struct net_device *peer;
1948e314dbdcSPavel Emelyanov 
1949e314dbdcSPavel Emelyanov 	priv = netdev_priv(dev);
1950d0e2c55eSEric Dumazet 	peer = rtnl_dereference(priv->peer);
1951d0e2c55eSEric Dumazet 
1952d0e2c55eSEric Dumazet 	/* Note : dellink() is called from default_device_exit_batch(),
1953d0e2c55eSEric Dumazet 	 * before a rcu_synchronize() point. The devices are guaranteed
1954d0e2c55eSEric Dumazet 	 * not being freed before one RCU grace period.
1955d0e2c55eSEric Dumazet 	 */
1956d0e2c55eSEric Dumazet 	RCU_INIT_POINTER(priv->peer, NULL);
1957f45a5c26SEric Dumazet 	unregister_netdevice_queue(dev, head);
1958d0e2c55eSEric Dumazet 
1959f45a5c26SEric Dumazet 	if (peer) {
1960d0e2c55eSEric Dumazet 		priv = netdev_priv(peer);
1961d0e2c55eSEric Dumazet 		RCU_INIT_POINTER(priv->peer, NULL);
196224540535SEric Dumazet 		unregister_netdevice_queue(peer, head);
1963e314dbdcSPavel Emelyanov 	}
1964f45a5c26SEric Dumazet }
1965e314dbdcSPavel Emelyanov 
196623711438SThomas Graf static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
196723711438SThomas Graf 	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
196823711438SThomas Graf };
1969e314dbdcSPavel Emelyanov 
veth_get_link_net(const struct net_device * dev)1970e5f4e7b9SNicolas Dichtel static struct net *veth_get_link_net(const struct net_device *dev)
1971e5f4e7b9SNicolas Dichtel {
1972e5f4e7b9SNicolas Dichtel 	struct veth_priv *priv = netdev_priv(dev);
1973e5f4e7b9SNicolas Dichtel 	struct net_device *peer = rtnl_dereference(priv->peer);
1974e5f4e7b9SNicolas Dichtel 
1975e5f4e7b9SNicolas Dichtel 	return peer ? dev_net(peer) : dev_net(dev);
1976e5f4e7b9SNicolas Dichtel }
1977e5f4e7b9SNicolas Dichtel 
veth_get_num_queues(void)19789d3684c2SPaolo Abeni static unsigned int veth_get_num_queues(void)
19799d3684c2SPaolo Abeni {
19809d3684c2SPaolo Abeni 	/* enforce the same queue limit as rtnl_create_link */
19819d3684c2SPaolo Abeni 	int queues = num_possible_cpus();
19829d3684c2SPaolo Abeni 
19839d3684c2SPaolo Abeni 	if (queues > 4096)
19849d3684c2SPaolo Abeni 		queues = 4096;
19859d3684c2SPaolo Abeni 	return queues;
19869d3684c2SPaolo Abeni }
19879d3684c2SPaolo Abeni 
1988e314dbdcSPavel Emelyanov static struct rtnl_link_ops veth_link_ops = {
1989e314dbdcSPavel Emelyanov 	.kind		= DRV_NAME,
1990e314dbdcSPavel Emelyanov 	.priv_size	= sizeof(struct veth_priv),
1991e314dbdcSPavel Emelyanov 	.setup		= veth_setup,
1992e314dbdcSPavel Emelyanov 	.validate	= veth_validate,
1993e314dbdcSPavel Emelyanov 	.newlink	= veth_newlink,
1994e314dbdcSPavel Emelyanov 	.dellink	= veth_dellink,
1995e314dbdcSPavel Emelyanov 	.policy		= veth_policy,
1996e314dbdcSPavel Emelyanov 	.maxtype	= VETH_INFO_MAX,
1997e5f4e7b9SNicolas Dichtel 	.get_link_net	= veth_get_link_net,
19989d3684c2SPaolo Abeni 	.get_num_tx_queues	= veth_get_num_queues,
19999d3684c2SPaolo Abeni 	.get_num_rx_queues	= veth_get_num_queues,
2000e314dbdcSPavel Emelyanov };
2001e314dbdcSPavel Emelyanov 
2002e314dbdcSPavel Emelyanov /*
2003e314dbdcSPavel Emelyanov  * init/fini
2004e314dbdcSPavel Emelyanov  */
2005e314dbdcSPavel Emelyanov 
veth_init(void)2006e314dbdcSPavel Emelyanov static __init int veth_init(void)
2007e314dbdcSPavel Emelyanov {
2008e314dbdcSPavel Emelyanov 	return rtnl_link_register(&veth_link_ops);
2009e314dbdcSPavel Emelyanov }
2010e314dbdcSPavel Emelyanov 
veth_exit(void)2011e314dbdcSPavel Emelyanov static __exit void veth_exit(void)
2012e314dbdcSPavel Emelyanov {
201368365458SPatrick McHardy 	rtnl_link_unregister(&veth_link_ops);
2014e314dbdcSPavel Emelyanov }
2015e314dbdcSPavel Emelyanov 
2016e314dbdcSPavel Emelyanov module_init(veth_init);
2017e314dbdcSPavel Emelyanov module_exit(veth_exit);
2018e314dbdcSPavel Emelyanov 
2019e314dbdcSPavel Emelyanov MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
2020e314dbdcSPavel Emelyanov MODULE_LICENSE("GPL v2");
2021e314dbdcSPavel Emelyanov MODULE_ALIAS_RTNL_LINK(DRV_NAME);
2022