File Index
Files and directories
Sign in
FILE

veth.c

File preview with syntax highlighting.

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