Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  drivers/net/veth.c
0004  *
0005  *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
0006  *
0007  * Author: Pavel Emelianov <xemul@openvz.org>
0008  * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
0009  *
0010  */
0011 
0012 #include <linux/netdevice.h>
0013 #include <linux/slab.h>
0014 #include <linux/ethtool.h>
0015 #include <linux/etherdevice.h>
0016 #include <linux/u64_stats_sync.h>
0017 
0018 #include <net/rtnetlink.h>
0019 #include <net/dst.h>
0020 #include <net/xfrm.h>
0021 #include <net/xdp.h>
0022 #include <linux/veth.h>
0023 #include <linux/module.h>
0024 #include <linux/bpf.h>
0025 #include <linux/filter.h>
0026 #include <linux/ptr_ring.h>
0027 #include <linux/bpf_trace.h>
0028 #include <linux/net_tstamp.h>
0029 
0030 #define DRV_NAME    "veth"
0031 #define DRV_VERSION "1.0"
0032 
0033 #define VETH_XDP_FLAG       BIT(0)
0034 #define VETH_RING_SIZE      256
0035 #define VETH_XDP_HEADROOM   (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
0036 
0037 #define VETH_XDP_TX_BULK_SIZE   16
0038 #define VETH_XDP_BATCH      16
0039 
0040 struct veth_stats {
0041     u64 rx_drops;
0042     /* xdp */
0043     u64 xdp_packets;
0044     u64 xdp_bytes;
0045     u64 xdp_redirect;
0046     u64 xdp_drops;
0047     u64 xdp_tx;
0048     u64 xdp_tx_err;
0049     u64 peer_tq_xdp_xmit;
0050     u64 peer_tq_xdp_xmit_err;
0051 };
0052 
0053 struct veth_rq_stats {
0054     struct veth_stats   vs;
0055     struct u64_stats_sync   syncp;
0056 };
0057 
0058 struct veth_rq {
0059     struct napi_struct  xdp_napi;
0060     struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
0061     struct net_device   *dev;
0062     struct bpf_prog __rcu   *xdp_prog;
0063     struct xdp_mem_info xdp_mem;
0064     struct veth_rq_stats    stats;
0065     bool            rx_notify_masked;
0066     struct ptr_ring     xdp_ring;
0067     struct xdp_rxq_info xdp_rxq;
0068 };
0069 
0070 struct veth_priv {
0071     struct net_device __rcu *peer;
0072     atomic64_t      dropped;
0073     struct bpf_prog     *_xdp_prog;
0074     struct veth_rq      *rq;
0075     unsigned int        requested_headroom;
0076 };
0077 
0078 struct veth_xdp_tx_bq {
0079     struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
0080     unsigned int count;
0081 };
0082 
0083 /*
0084  * ethtool interface
0085  */
0086 
0087 struct veth_q_stat_desc {
0088     char    desc[ETH_GSTRING_LEN];
0089     size_t  offset;
0090 };
0091 
0092 #define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
0093 
0094 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
0095     { "xdp_packets",    VETH_RQ_STAT(xdp_packets) },
0096     { "xdp_bytes",      VETH_RQ_STAT(xdp_bytes) },
0097     { "drops",      VETH_RQ_STAT(rx_drops) },
0098     { "xdp_redirect",   VETH_RQ_STAT(xdp_redirect) },
0099     { "xdp_drops",      VETH_RQ_STAT(xdp_drops) },
0100     { "xdp_tx",     VETH_RQ_STAT(xdp_tx) },
0101     { "xdp_tx_errors",  VETH_RQ_STAT(xdp_tx_err) },
0102 };
0103 
0104 #define VETH_RQ_STATS_LEN   ARRAY_SIZE(veth_rq_stats_desc)
0105 
0106 static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
0107     { "xdp_xmit",       VETH_RQ_STAT(peer_tq_xdp_xmit) },
0108     { "xdp_xmit_errors",    VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
0109 };
0110 
0111 #define VETH_TQ_STATS_LEN   ARRAY_SIZE(veth_tq_stats_desc)
0112 
0113 static struct {
0114     const char string[ETH_GSTRING_LEN];
0115 } ethtool_stats_keys[] = {
0116     { "peer_ifindex" },
0117 };
0118 
0119 static int veth_get_link_ksettings(struct net_device *dev,
0120                    struct ethtool_link_ksettings *cmd)
0121 {
0122     cmd->base.speed     = SPEED_10000;
0123     cmd->base.duplex    = DUPLEX_FULL;
0124     cmd->base.port      = PORT_TP;
0125     cmd->base.autoneg   = AUTONEG_DISABLE;
0126     return 0;
0127 }
0128 
0129 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
0130 {
0131     strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
0132     strlcpy(info->version, DRV_VERSION, sizeof(info->version));
0133 }
0134 
0135 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
0136 {
0137     u8 *p = buf;
0138     int i, j;
0139 
0140     switch(stringset) {
0141     case ETH_SS_STATS:
0142         memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
0143         p += sizeof(ethtool_stats_keys);
0144         for (i = 0; i < dev->real_num_rx_queues; i++)
0145             for (j = 0; j < VETH_RQ_STATS_LEN; j++)
0146                 ethtool_sprintf(&p, "rx_queue_%u_%.18s",
0147                         i, veth_rq_stats_desc[j].desc);
0148 
0149         for (i = 0; i < dev->real_num_tx_queues; i++)
0150             for (j = 0; j < VETH_TQ_STATS_LEN; j++)
0151                 ethtool_sprintf(&p, "tx_queue_%u_%.18s",
0152                         i, veth_tq_stats_desc[j].desc);
0153         break;
0154     }
0155 }
0156 
0157 static int veth_get_sset_count(struct net_device *dev, int sset)
0158 {
0159     switch (sset) {
0160     case ETH_SS_STATS:
0161         return ARRAY_SIZE(ethtool_stats_keys) +
0162                VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
0163                VETH_TQ_STATS_LEN * dev->real_num_tx_queues;
0164     default:
0165         return -EOPNOTSUPP;
0166     }
0167 }
0168 
0169 static void veth_get_ethtool_stats(struct net_device *dev,
0170         struct ethtool_stats *stats, u64 *data)
0171 {
0172     struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
0173     struct net_device *peer = rtnl_dereference(priv->peer);
0174     int i, j, idx;
0175 
0176     data[0] = peer ? peer->ifindex : 0;
0177     idx = 1;
0178     for (i = 0; i < dev->real_num_rx_queues; i++) {
0179         const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
0180         const void *stats_base = (void *)&rq_stats->vs;
0181         unsigned int start;
0182         size_t offset;
0183 
0184         do {
0185             start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
0186             for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
0187                 offset = veth_rq_stats_desc[j].offset;
0188                 data[idx + j] = *(u64 *)(stats_base + offset);
0189             }
0190         } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
0191         idx += VETH_RQ_STATS_LEN;
0192     }
0193 
0194     if (!peer)
0195         return;
0196 
0197     rcv_priv = netdev_priv(peer);
0198     for (i = 0; i < peer->real_num_rx_queues; i++) {
0199         const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
0200         const void *base = (void *)&rq_stats->vs;
0201         unsigned int start, tx_idx = idx;
0202         size_t offset;
0203 
0204         tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
0205         do {
0206             start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
0207             for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
0208                 offset = veth_tq_stats_desc[j].offset;
0209                 data[tx_idx + j] += *(u64 *)(base + offset);
0210             }
0211         } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
0212     }
0213 }
0214 
0215 static void veth_get_channels(struct net_device *dev,
0216                   struct ethtool_channels *channels)
0217 {
0218     channels->tx_count = dev->real_num_tx_queues;
0219     channels->rx_count = dev->real_num_rx_queues;
0220     channels->max_tx = dev->num_tx_queues;
0221     channels->max_rx = dev->num_rx_queues;
0222 }
0223 
0224 static int veth_set_channels(struct net_device *dev,
0225                  struct ethtool_channels *ch);
0226 
0227 static const struct ethtool_ops veth_ethtool_ops = {
0228     .get_drvinfo        = veth_get_drvinfo,
0229     .get_link       = ethtool_op_get_link,
0230     .get_strings        = veth_get_strings,
0231     .get_sset_count     = veth_get_sset_count,
0232     .get_ethtool_stats  = veth_get_ethtool_stats,
0233     .get_link_ksettings = veth_get_link_ksettings,
0234     .get_ts_info        = ethtool_op_get_ts_info,
0235     .get_channels       = veth_get_channels,
0236     .set_channels       = veth_set_channels,
0237 };
0238 
0239 /* general routines */
0240 
0241 static bool veth_is_xdp_frame(void *ptr)
0242 {
0243     return (unsigned long)ptr & VETH_XDP_FLAG;
0244 }
0245 
0246 static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
0247 {
0248     return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
0249 }
0250 
0251 static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
0252 {
0253     return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
0254 }
0255 
0256 static void veth_ptr_free(void *ptr)
0257 {
0258     if (veth_is_xdp_frame(ptr))
0259         xdp_return_frame(veth_ptr_to_xdp(ptr));
0260     else
0261         kfree_skb(ptr);
0262 }
0263 
0264 static void __veth_xdp_flush(struct veth_rq *rq)
0265 {
0266     /* Write ptr_ring before reading rx_notify_masked */
0267     smp_mb();
0268     if (!READ_ONCE(rq->rx_notify_masked) &&
0269         napi_schedule_prep(&rq->xdp_napi)) {
0270         WRITE_ONCE(rq->rx_notify_masked, true);
0271         __napi_schedule(&rq->xdp_napi);
0272     }
0273 }
0274 
0275 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
0276 {
0277     if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
0278         dev_kfree_skb_any(skb);
0279         return NET_RX_DROP;
0280     }
0281 
0282     return NET_RX_SUCCESS;
0283 }
0284 
0285 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
0286                 struct veth_rq *rq, bool xdp)
0287 {
0288     return __dev_forward_skb(dev, skb) ?: xdp ?
0289         veth_xdp_rx(rq, skb) :
0290         __netif_rx(skb);
0291 }
0292 
0293 /* return true if the specified skb has chances of GRO aggregation
0294  * Don't strive for accuracy, but try to avoid GRO overhead in the most
0295  * common scenarios.
0296  * When XDP is enabled, all traffic is considered eligible, as the xmit
0297  * device has TSO off.
0298  * When TSO is enabled on the xmit device, we are likely interested only
0299  * in UDP aggregation, explicitly check for that if the skb is suspected
0300  * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
0301  * to belong to locally generated UDP traffic.
0302  */
0303 static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
0304                      const struct net_device *rcv,
0305                      const struct sk_buff *skb)
0306 {
0307     return !(dev->features & NETIF_F_ALL_TSO) ||
0308         (skb->destructor == sock_wfree &&
0309          rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
0310 }
0311 
0312 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
0313 {
0314     struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
0315     struct veth_rq *rq = NULL;
0316     struct net_device *rcv;
0317     int length = skb->len;
0318     bool use_napi = false;
0319     int rxq;
0320 
0321     rcu_read_lock();
0322     rcv = rcu_dereference(priv->peer);
0323     if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
0324         kfree_skb(skb);
0325         goto drop;
0326     }
0327 
0328     rcv_priv = netdev_priv(rcv);
0329     rxq = skb_get_queue_mapping(skb);
0330     if (rxq < rcv->real_num_rx_queues) {
0331         rq = &rcv_priv->rq[rxq];
0332 
0333         /* The napi pointer is available when an XDP program is
0334          * attached or when GRO is enabled
0335          * Don't bother with napi/GRO if the skb can't be aggregated
0336          */
0337         use_napi = rcu_access_pointer(rq->napi) &&
0338                veth_skb_is_eligible_for_gro(dev, rcv, skb);
0339     }
0340 
0341     skb_tx_timestamp(skb);
0342     if (likely(veth_forward_skb(rcv, skb, rq, use_napi) == NET_RX_SUCCESS)) {
0343         if (!use_napi)
0344             dev_lstats_add(dev, length);
0345     } else {
0346 drop:
0347         atomic64_inc(&priv->dropped);
0348     }
0349 
0350     if (use_napi)
0351         __veth_xdp_flush(rq);
0352 
0353     rcu_read_unlock();
0354 
0355     return NETDEV_TX_OK;
0356 }
0357 
0358 static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
0359 {
0360     struct veth_priv *priv = netdev_priv(dev);
0361 
0362     dev_lstats_read(dev, packets, bytes);
0363     return atomic64_read(&priv->dropped);
0364 }
0365 
0366 static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
0367 {
0368     struct veth_priv *priv = netdev_priv(dev);
0369     int i;
0370 
0371     result->peer_tq_xdp_xmit_err = 0;
0372     result->xdp_packets = 0;
0373     result->xdp_tx_err = 0;
0374     result->xdp_bytes = 0;
0375     result->rx_drops = 0;
0376     for (i = 0; i < dev->num_rx_queues; i++) {
0377         u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
0378         struct veth_rq_stats *stats = &priv->rq[i].stats;
0379         unsigned int start;
0380 
0381         do {
0382             start = u64_stats_fetch_begin_irq(&stats->syncp);
0383             peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
0384             xdp_tx_err = stats->vs.xdp_tx_err;
0385             packets = stats->vs.xdp_packets;
0386             bytes = stats->vs.xdp_bytes;
0387             drops = stats->vs.rx_drops;
0388         } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
0389         result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
0390         result->xdp_tx_err += xdp_tx_err;
0391         result->xdp_packets += packets;
0392         result->xdp_bytes += bytes;
0393         result->rx_drops += drops;
0394     }
0395 }
0396 
0397 static void veth_get_stats64(struct net_device *dev,
0398                  struct rtnl_link_stats64 *tot)
0399 {
0400     struct veth_priv *priv = netdev_priv(dev);
0401     struct net_device *peer;
0402     struct veth_stats rx;
0403     u64 packets, bytes;
0404 
0405     tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
0406     tot->tx_bytes = bytes;
0407     tot->tx_packets = packets;
0408 
0409     veth_stats_rx(&rx, dev);
0410     tot->tx_dropped += rx.xdp_tx_err;
0411     tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
0412     tot->rx_bytes = rx.xdp_bytes;
0413     tot->rx_packets = rx.xdp_packets;
0414 
0415     rcu_read_lock();
0416     peer = rcu_dereference(priv->peer);
0417     if (peer) {
0418         veth_stats_tx(peer, &packets, &bytes);
0419         tot->rx_bytes += bytes;
0420         tot->rx_packets += packets;
0421 
0422         veth_stats_rx(&rx, peer);
0423         tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
0424         tot->rx_dropped += rx.xdp_tx_err;
0425         tot->tx_bytes += rx.xdp_bytes;
0426         tot->tx_packets += rx.xdp_packets;
0427     }
0428     rcu_read_unlock();
0429 }
0430 
0431 /* fake multicast ability */
0432 static void veth_set_multicast_list(struct net_device *dev)
0433 {
0434 }
0435 
0436 static int veth_select_rxq(struct net_device *dev)
0437 {
0438     return smp_processor_id() % dev->real_num_rx_queues;
0439 }
0440 
0441 static struct net_device *veth_peer_dev(struct net_device *dev)
0442 {
0443     struct veth_priv *priv = netdev_priv(dev);
0444 
0445     /* Callers must be under RCU read side. */
0446     return rcu_dereference(priv->peer);
0447 }
0448 
0449 static int veth_xdp_xmit(struct net_device *dev, int n,
0450              struct xdp_frame **frames,
0451              u32 flags, bool ndo_xmit)
0452 {
0453     struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
0454     int i, ret = -ENXIO, nxmit = 0;
0455     struct net_device *rcv;
0456     unsigned int max_len;
0457     struct veth_rq *rq;
0458 
0459     if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
0460         return -EINVAL;
0461 
0462     rcu_read_lock();
0463     rcv = rcu_dereference(priv->peer);
0464     if (unlikely(!rcv))
0465         goto out;
0466 
0467     rcv_priv = netdev_priv(rcv);
0468     rq = &rcv_priv->rq[veth_select_rxq(rcv)];
0469     /* The napi pointer is set if NAPI is enabled, which ensures that
0470      * xdp_ring is initialized on receive side and the peer device is up.
0471      */
0472     if (!rcu_access_pointer(rq->napi))
0473         goto out;
0474 
0475     max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
0476 
0477     spin_lock(&rq->xdp_ring.producer_lock);
0478     for (i = 0; i < n; i++) {
0479         struct xdp_frame *frame = frames[i];
0480         void *ptr = veth_xdp_to_ptr(frame);
0481 
0482         if (unlikely(xdp_get_frame_len(frame) > max_len ||
0483                  __ptr_ring_produce(&rq->xdp_ring, ptr)))
0484             break;
0485         nxmit++;
0486     }
0487     spin_unlock(&rq->xdp_ring.producer_lock);
0488 
0489     if (flags & XDP_XMIT_FLUSH)
0490         __veth_xdp_flush(rq);
0491 
0492     ret = nxmit;
0493     if (ndo_xmit) {
0494         u64_stats_update_begin(&rq->stats.syncp);
0495         rq->stats.vs.peer_tq_xdp_xmit += nxmit;
0496         rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
0497         u64_stats_update_end(&rq->stats.syncp);
0498     }
0499 
0500 out:
0501     rcu_read_unlock();
0502 
0503     return ret;
0504 }
0505 
0506 static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
0507                  struct xdp_frame **frames, u32 flags)
0508 {
0509     int err;
0510 
0511     err = veth_xdp_xmit(dev, n, frames, flags, true);
0512     if (err < 0) {
0513         struct veth_priv *priv = netdev_priv(dev);
0514 
0515         atomic64_add(n, &priv->dropped);
0516     }
0517 
0518     return err;
0519 }
0520 
0521 static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
0522 {
0523     int sent, i, err = 0, drops;
0524 
0525     sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
0526     if (sent < 0) {
0527         err = sent;
0528         sent = 0;
0529     }
0530 
0531     for (i = sent; unlikely(i < bq->count); i++)
0532         xdp_return_frame(bq->q[i]);
0533 
0534     drops = bq->count - sent;
0535     trace_xdp_bulk_tx(rq->dev, sent, drops, err);
0536 
0537     u64_stats_update_begin(&rq->stats.syncp);
0538     rq->stats.vs.xdp_tx += sent;
0539     rq->stats.vs.xdp_tx_err += drops;
0540     u64_stats_update_end(&rq->stats.syncp);
0541 
0542     bq->count = 0;
0543 }
0544 
0545 static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
0546 {
0547     struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
0548     struct net_device *rcv;
0549     struct veth_rq *rcv_rq;
0550 
0551     rcu_read_lock();
0552     veth_xdp_flush_bq(rq, bq);
0553     rcv = rcu_dereference(priv->peer);
0554     if (unlikely(!rcv))
0555         goto out;
0556 
0557     rcv_priv = netdev_priv(rcv);
0558     rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
0559     /* xdp_ring is initialized on receive side? */
0560     if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
0561         goto out;
0562 
0563     __veth_xdp_flush(rcv_rq);
0564 out:
0565     rcu_read_unlock();
0566 }
0567 
0568 static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
0569                struct veth_xdp_tx_bq *bq)
0570 {
0571     struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
0572 
0573     if (unlikely(!frame))
0574         return -EOVERFLOW;
0575 
0576     if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
0577         veth_xdp_flush_bq(rq, bq);
0578 
0579     bq->q[bq->count++] = frame;
0580 
0581     return 0;
0582 }
0583 
0584 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
0585                       struct xdp_frame *frame,
0586                       struct veth_xdp_tx_bq *bq,
0587                       struct veth_stats *stats)
0588 {
0589     struct xdp_frame orig_frame;
0590     struct bpf_prog *xdp_prog;
0591 
0592     rcu_read_lock();
0593     xdp_prog = rcu_dereference(rq->xdp_prog);
0594     if (likely(xdp_prog)) {
0595         struct xdp_buff xdp;
0596         u32 act;
0597 
0598         xdp_convert_frame_to_buff(frame, &xdp);
0599         xdp.rxq = &rq->xdp_rxq;
0600 
0601         act = bpf_prog_run_xdp(xdp_prog, &xdp);
0602 
0603         switch (act) {
0604         case XDP_PASS:
0605             if (xdp_update_frame_from_buff(&xdp, frame))
0606                 goto err_xdp;
0607             break;
0608         case XDP_TX:
0609             orig_frame = *frame;
0610             xdp.rxq->mem = frame->mem;
0611             if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
0612                 trace_xdp_exception(rq->dev, xdp_prog, act);
0613                 frame = &orig_frame;
0614                 stats->rx_drops++;
0615                 goto err_xdp;
0616             }
0617             stats->xdp_tx++;
0618             rcu_read_unlock();
0619             goto xdp_xmit;
0620         case XDP_REDIRECT:
0621             orig_frame = *frame;
0622             xdp.rxq->mem = frame->mem;
0623             if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
0624                 frame = &orig_frame;
0625                 stats->rx_drops++;
0626                 goto err_xdp;
0627             }
0628             stats->xdp_redirect++;
0629             rcu_read_unlock();
0630             goto xdp_xmit;
0631         default:
0632             bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
0633             fallthrough;
0634         case XDP_ABORTED:
0635             trace_xdp_exception(rq->dev, xdp_prog, act);
0636             fallthrough;
0637         case XDP_DROP:
0638             stats->xdp_drops++;
0639             goto err_xdp;
0640         }
0641     }
0642     rcu_read_unlock();
0643 
0644     return frame;
0645 err_xdp:
0646     rcu_read_unlock();
0647     xdp_return_frame(frame);
0648 xdp_xmit:
0649     return NULL;
0650 }
0651 
0652 /* frames array contains VETH_XDP_BATCH at most */
0653 static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
0654                   int n_xdpf, struct veth_xdp_tx_bq *bq,
0655                   struct veth_stats *stats)
0656 {
0657     void *skbs[VETH_XDP_BATCH];
0658     int i;
0659 
0660     if (xdp_alloc_skb_bulk(skbs, n_xdpf,
0661                    GFP_ATOMIC | __GFP_ZERO) < 0) {
0662         for (i = 0; i < n_xdpf; i++)
0663             xdp_return_frame(frames[i]);
0664         stats->rx_drops += n_xdpf;
0665 
0666         return;
0667     }
0668 
0669     for (i = 0; i < n_xdpf; i++) {
0670         struct sk_buff *skb = skbs[i];
0671 
0672         skb = __xdp_build_skb_from_frame(frames[i], skb,
0673                          rq->dev);
0674         if (!skb) {
0675             xdp_return_frame(frames[i]);
0676             stats->rx_drops++;
0677             continue;
0678         }
0679         napi_gro_receive(&rq->xdp_napi, skb);
0680     }
0681 }
0682 
0683 static void veth_xdp_get(struct xdp_buff *xdp)
0684 {
0685     struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
0686     int i;
0687 
0688     get_page(virt_to_page(xdp->data));
0689     if (likely(!xdp_buff_has_frags(xdp)))
0690         return;
0691 
0692     for (i = 0; i < sinfo->nr_frags; i++)
0693         __skb_frag_ref(&sinfo->frags[i]);
0694 }
0695 
0696 static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
0697                     struct xdp_buff *xdp,
0698                     struct sk_buff **pskb)
0699 {
0700     struct sk_buff *skb = *pskb;
0701     u32 frame_sz;
0702 
0703     if (skb_shared(skb) || skb_head_is_locked(skb) ||
0704         skb_shinfo(skb)->nr_frags) {
0705         u32 size, len, max_head_size, off;
0706         struct sk_buff *nskb;
0707         struct page *page;
0708         int i, head_off;
0709 
0710         /* We need a private copy of the skb and data buffers since
0711          * the ebpf program can modify it. We segment the original skb
0712          * into order-0 pages without linearize it.
0713          *
0714          * Make sure we have enough space for linear and paged area
0715          */
0716         max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE -
0717                           VETH_XDP_HEADROOM);
0718         if (skb->len > PAGE_SIZE * MAX_SKB_FRAGS + max_head_size)
0719             goto drop;
0720 
0721         /* Allocate skb head */
0722         page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
0723         if (!page)
0724             goto drop;
0725 
0726         nskb = build_skb(page_address(page), PAGE_SIZE);
0727         if (!nskb) {
0728             put_page(page);
0729             goto drop;
0730         }
0731 
0732         skb_reserve(nskb, VETH_XDP_HEADROOM);
0733         size = min_t(u32, skb->len, max_head_size);
0734         if (skb_copy_bits(skb, 0, nskb->data, size)) {
0735             consume_skb(nskb);
0736             goto drop;
0737         }
0738         skb_put(nskb, size);
0739 
0740         skb_copy_header(nskb, skb);
0741         head_off = skb_headroom(nskb) - skb_headroom(skb);
0742         skb_headers_offset_update(nskb, head_off);
0743 
0744         /* Allocate paged area of new skb */
0745         off = size;
0746         len = skb->len - off;
0747 
0748         for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
0749             page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
0750             if (!page) {
0751                 consume_skb(nskb);
0752                 goto drop;
0753             }
0754 
0755             size = min_t(u32, len, PAGE_SIZE);
0756             skb_add_rx_frag(nskb, i, page, 0, size, PAGE_SIZE);
0757             if (skb_copy_bits(skb, off, page_address(page),
0758                       size)) {
0759                 consume_skb(nskb);
0760                 goto drop;
0761             }
0762 
0763             len -= size;
0764             off += size;
0765         }
0766 
0767         consume_skb(skb);
0768         skb = nskb;
0769     } else if (skb_headroom(skb) < XDP_PACKET_HEADROOM &&
0770            pskb_expand_head(skb, VETH_XDP_HEADROOM, 0, GFP_ATOMIC)) {
0771         goto drop;
0772     }
0773 
0774     /* SKB "head" area always have tailroom for skb_shared_info */
0775     frame_sz = skb_end_pointer(skb) - skb->head;
0776     frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
0777     xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
0778     xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
0779              skb_headlen(skb), true);
0780 
0781     if (skb_is_nonlinear(skb)) {
0782         skb_shinfo(skb)->xdp_frags_size = skb->data_len;
0783         xdp_buff_set_frags_flag(xdp);
0784     } else {
0785         xdp_buff_clear_frags_flag(xdp);
0786     }
0787     *pskb = skb;
0788 
0789     return 0;
0790 drop:
0791     consume_skb(skb);
0792     *pskb = NULL;
0793 
0794     return -ENOMEM;
0795 }
0796 
0797 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
0798                     struct sk_buff *skb,
0799                     struct veth_xdp_tx_bq *bq,
0800                     struct veth_stats *stats)
0801 {
0802     void *orig_data, *orig_data_end;
0803     struct bpf_prog *xdp_prog;
0804     struct xdp_buff xdp;
0805     u32 act, metalen;
0806     int off;
0807 
0808     skb_prepare_for_gro(skb);
0809 
0810     rcu_read_lock();
0811     xdp_prog = rcu_dereference(rq->xdp_prog);
0812     if (unlikely(!xdp_prog)) {
0813         rcu_read_unlock();
0814         goto out;
0815     }
0816 
0817     __skb_push(skb, skb->data - skb_mac_header(skb));
0818     if (veth_convert_skb_to_xdp_buff(rq, &xdp, &skb))
0819         goto drop;
0820 
0821     orig_data = xdp.data;
0822     orig_data_end = xdp.data_end;
0823 
0824     act = bpf_prog_run_xdp(xdp_prog, &xdp);
0825 
0826     switch (act) {
0827     case XDP_PASS:
0828         break;
0829     case XDP_TX:
0830         veth_xdp_get(&xdp);
0831         consume_skb(skb);
0832         xdp.rxq->mem = rq->xdp_mem;
0833         if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
0834             trace_xdp_exception(rq->dev, xdp_prog, act);
0835             stats->rx_drops++;
0836             goto err_xdp;
0837         }
0838         stats->xdp_tx++;
0839         rcu_read_unlock();
0840         goto xdp_xmit;
0841     case XDP_REDIRECT:
0842         veth_xdp_get(&xdp);
0843         consume_skb(skb);
0844         xdp.rxq->mem = rq->xdp_mem;
0845         if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
0846             stats->rx_drops++;
0847             goto err_xdp;
0848         }
0849         stats->xdp_redirect++;
0850         rcu_read_unlock();
0851         goto xdp_xmit;
0852     default:
0853         bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
0854         fallthrough;
0855     case XDP_ABORTED:
0856         trace_xdp_exception(rq->dev, xdp_prog, act);
0857         fallthrough;
0858     case XDP_DROP:
0859         stats->xdp_drops++;
0860         goto xdp_drop;
0861     }
0862     rcu_read_unlock();
0863 
0864     /* check if bpf_xdp_adjust_head was used */
0865     off = orig_data - xdp.data;
0866     if (off > 0)
0867         __skb_push(skb, off);
0868     else if (off < 0)
0869         __skb_pull(skb, -off);
0870 
0871     skb_reset_mac_header(skb);
0872 
0873     /* check if bpf_xdp_adjust_tail was used */
0874     off = xdp.data_end - orig_data_end;
0875     if (off != 0)
0876         __skb_put(skb, off); /* positive on grow, negative on shrink */
0877 
0878     /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
0879      * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
0880      */
0881     if (xdp_buff_has_frags(&xdp))
0882         skb->data_len = skb_shinfo(skb)->xdp_frags_size;
0883     else
0884         skb->data_len = 0;
0885 
0886     skb->protocol = eth_type_trans(skb, rq->dev);
0887 
0888     metalen = xdp.data - xdp.data_meta;
0889     if (metalen)
0890         skb_metadata_set(skb, metalen);
0891 out:
0892     return skb;
0893 drop:
0894     stats->rx_drops++;
0895 xdp_drop:
0896     rcu_read_unlock();
0897     kfree_skb(skb);
0898     return NULL;
0899 err_xdp:
0900     rcu_read_unlock();
0901     xdp_return_buff(&xdp);
0902 xdp_xmit:
0903     return NULL;
0904 }
0905 
0906 static int veth_xdp_rcv(struct veth_rq *rq, int budget,
0907             struct veth_xdp_tx_bq *bq,
0908             struct veth_stats *stats)
0909 {
0910     int i, done = 0, n_xdpf = 0;
0911     void *xdpf[VETH_XDP_BATCH];
0912 
0913     for (i = 0; i < budget; i++) {
0914         void *ptr = __ptr_ring_consume(&rq->xdp_ring);
0915 
0916         if (!ptr)
0917             break;
0918 
0919         if (veth_is_xdp_frame(ptr)) {
0920             /* ndo_xdp_xmit */
0921             struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
0922 
0923             stats->xdp_bytes += xdp_get_frame_len(frame);
0924             frame = veth_xdp_rcv_one(rq, frame, bq, stats);
0925             if (frame) {
0926                 /* XDP_PASS */
0927                 xdpf[n_xdpf++] = frame;
0928                 if (n_xdpf == VETH_XDP_BATCH) {
0929                     veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
0930                                   bq, stats);
0931                     n_xdpf = 0;
0932                 }
0933             }
0934         } else {
0935             /* ndo_start_xmit */
0936             struct sk_buff *skb = ptr;
0937 
0938             stats->xdp_bytes += skb->len;
0939             skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
0940             if (skb) {
0941                 if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
0942                     netif_receive_skb(skb);
0943                 else
0944                     napi_gro_receive(&rq->xdp_napi, skb);
0945             }
0946         }
0947         done++;
0948     }
0949 
0950     if (n_xdpf)
0951         veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
0952 
0953     u64_stats_update_begin(&rq->stats.syncp);
0954     rq->stats.vs.xdp_redirect += stats->xdp_redirect;
0955     rq->stats.vs.xdp_bytes += stats->xdp_bytes;
0956     rq->stats.vs.xdp_drops += stats->xdp_drops;
0957     rq->stats.vs.rx_drops += stats->rx_drops;
0958     rq->stats.vs.xdp_packets += done;
0959     u64_stats_update_end(&rq->stats.syncp);
0960 
0961     return done;
0962 }
0963 
0964 static int veth_poll(struct napi_struct *napi, int budget)
0965 {
0966     struct veth_rq *rq =
0967         container_of(napi, struct veth_rq, xdp_napi);
0968     struct veth_stats stats = {};
0969     struct veth_xdp_tx_bq bq;
0970     int done;
0971 
0972     bq.count = 0;
0973 
0974     xdp_set_return_frame_no_direct();
0975     done = veth_xdp_rcv(rq, budget, &bq, &stats);
0976 
0977     if (done < budget && napi_complete_done(napi, done)) {
0978         /* Write rx_notify_masked before reading ptr_ring */
0979         smp_store_mb(rq->rx_notify_masked, false);
0980         if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
0981             if (napi_schedule_prep(&rq->xdp_napi)) {
0982                 WRITE_ONCE(rq->rx_notify_masked, true);
0983                 __napi_schedule(&rq->xdp_napi);
0984             }
0985         }
0986     }
0987 
0988     if (stats.xdp_tx > 0)
0989         veth_xdp_flush(rq, &bq);
0990     if (stats.xdp_redirect > 0)
0991         xdp_do_flush();
0992     xdp_clear_return_frame_no_direct();
0993 
0994     return done;
0995 }
0996 
0997 static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
0998 {
0999     struct veth_priv *priv = netdev_priv(dev);
1000     int err, i;
1001 
1002     for (i = start; i < end; i++) {
1003         struct veth_rq *rq = &priv->rq[i];
1004 
1005         err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
1006         if (err)
1007             goto err_xdp_ring;
1008     }
1009 
1010     for (i = start; i < end; i++) {
1011         struct veth_rq *rq = &priv->rq[i];
1012 
1013         napi_enable(&rq->xdp_napi);
1014         rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1015     }
1016 
1017     return 0;
1018 
1019 err_xdp_ring:
1020     for (i--; i >= start; i--)
1021         ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
1022 
1023     return err;
1024 }
1025 
1026 static int __veth_napi_enable(struct net_device *dev)
1027 {
1028     return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1029 }
1030 
1031 static void veth_napi_del_range(struct net_device *dev, int start, int end)
1032 {
1033     struct veth_priv *priv = netdev_priv(dev);
1034     int i;
1035 
1036     for (i = start; i < end; i++) {
1037         struct veth_rq *rq = &priv->rq[i];
1038 
1039         rcu_assign_pointer(priv->rq[i].napi, NULL);
1040         napi_disable(&rq->xdp_napi);
1041         __netif_napi_del(&rq->xdp_napi);
1042     }
1043     synchronize_net();
1044 
1045     for (i = start; i < end; i++) {
1046         struct veth_rq *rq = &priv->rq[i];
1047 
1048         rq->rx_notify_masked = false;
1049         ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
1050     }
1051 }
1052 
1053 static void veth_napi_del(struct net_device *dev)
1054 {
1055     veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1056 }
1057 
1058 static bool veth_gro_requested(const struct net_device *dev)
1059 {
1060     return !!(dev->wanted_features & NETIF_F_GRO);
1061 }
1062 
1063 static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1064                  bool napi_already_on)
1065 {
1066     struct veth_priv *priv = netdev_priv(dev);
1067     int err, i;
1068 
1069     for (i = start; i < end; i++) {
1070         struct veth_rq *rq = &priv->rq[i];
1071 
1072         if (!napi_already_on)
1073             netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1074         err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1075         if (err < 0)
1076             goto err_rxq_reg;
1077 
1078         err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1079                          MEM_TYPE_PAGE_SHARED,
1080                          NULL);
1081         if (err < 0)
1082             goto err_reg_mem;
1083 
1084         /* Save original mem info as it can be overwritten */
1085         rq->xdp_mem = rq->xdp_rxq.mem;
1086     }
1087     return 0;
1088 
1089 err_reg_mem:
1090     xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1091 err_rxq_reg:
1092     for (i--; i >= start; i--) {
1093         struct veth_rq *rq = &priv->rq[i];
1094 
1095         xdp_rxq_info_unreg(&rq->xdp_rxq);
1096         if (!napi_already_on)
1097             netif_napi_del(&rq->xdp_napi);
1098     }
1099 
1100     return err;
1101 }
1102 
1103 static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1104                    bool delete_napi)
1105 {
1106     struct veth_priv *priv = netdev_priv(dev);
1107     int i;
1108 
1109     for (i = start; i < end; i++) {
1110         struct veth_rq *rq = &priv->rq[i];
1111 
1112         rq->xdp_rxq.mem = rq->xdp_mem;
1113         xdp_rxq_info_unreg(&rq->xdp_rxq);
1114 
1115         if (delete_napi)
1116             netif_napi_del(&rq->xdp_napi);
1117     }
1118 }
1119 
1120 static int veth_enable_xdp(struct net_device *dev)
1121 {
1122     bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1123     struct veth_priv *priv = netdev_priv(dev);
1124     int err, i;
1125 
1126     if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1127         err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1128         if (err)
1129             return err;
1130 
1131         if (!napi_already_on) {
1132             err = __veth_napi_enable(dev);
1133             if (err) {
1134                 veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1135                 return err;
1136             }
1137 
1138             if (!veth_gro_requested(dev)) {
1139                 /* user-space did not require GRO, but adding XDP
1140                  * is supposed to get GRO working
1141                  */
1142                 dev->features |= NETIF_F_GRO;
1143                 netdev_features_change(dev);
1144             }
1145         }
1146     }
1147 
1148     for (i = 0; i < dev->real_num_rx_queues; i++) {
1149         rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1150         rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1151     }
1152 
1153     return 0;
1154 }
1155 
1156 static void veth_disable_xdp(struct net_device *dev)
1157 {
1158     struct veth_priv *priv = netdev_priv(dev);
1159     int i;
1160 
1161     for (i = 0; i < dev->real_num_rx_queues; i++)
1162         rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1163 
1164     if (!netif_running(dev) || !veth_gro_requested(dev)) {
1165         veth_napi_del(dev);
1166 
1167         /* if user-space did not require GRO, since adding XDP
1168          * enabled it, clear it now
1169          */
1170         if (!veth_gro_requested(dev) && netif_running(dev)) {
1171             dev->features &= ~NETIF_F_GRO;
1172             netdev_features_change(dev);
1173         }
1174     }
1175 
1176     veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1177 }
1178 
1179 static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1180 {
1181     struct veth_priv *priv = netdev_priv(dev);
1182     int err, i;
1183 
1184     for (i = start; i < end; i++) {
1185         struct veth_rq *rq = &priv->rq[i];
1186 
1187         netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1188     }
1189 
1190     err = __veth_napi_enable_range(dev, start, end);
1191     if (err) {
1192         for (i = start; i < end; i++) {
1193             struct veth_rq *rq = &priv->rq[i];
1194 
1195             netif_napi_del(&rq->xdp_napi);
1196         }
1197         return err;
1198     }
1199     return err;
1200 }
1201 
1202 static int veth_napi_enable(struct net_device *dev)
1203 {
1204     return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1205 }
1206 
1207 static void veth_disable_range_safe(struct net_device *dev, int start, int end)
1208 {
1209     struct veth_priv *priv = netdev_priv(dev);
1210 
1211     if (start >= end)
1212         return;
1213 
1214     if (priv->_xdp_prog) {
1215         veth_napi_del_range(dev, start, end);
1216         veth_disable_xdp_range(dev, start, end, false);
1217     } else if (veth_gro_requested(dev)) {
1218         veth_napi_del_range(dev, start, end);
1219     }
1220 }
1221 
1222 static int veth_enable_range_safe(struct net_device *dev, int start, int end)
1223 {
1224     struct veth_priv *priv = netdev_priv(dev);
1225     int err;
1226 
1227     if (start >= end)
1228         return 0;
1229 
1230     if (priv->_xdp_prog) {
1231         /* these channels are freshly initialized, napi is not on there even
1232          * when GRO is requeste
1233          */
1234         err = veth_enable_xdp_range(dev, start, end, false);
1235         if (err)
1236             return err;
1237 
1238         err = __veth_napi_enable_range(dev, start, end);
1239         if (err) {
1240             /* on error always delete the newly added napis */
1241             veth_disable_xdp_range(dev, start, end, true);
1242             return err;
1243         }
1244     } else if (veth_gro_requested(dev)) {
1245         return veth_napi_enable_range(dev, start, end);
1246     }
1247     return 0;
1248 }
1249 
1250 static int veth_set_channels(struct net_device *dev,
1251                  struct ethtool_channels *ch)
1252 {
1253     struct veth_priv *priv = netdev_priv(dev);
1254     unsigned int old_rx_count, new_rx_count;
1255     struct veth_priv *peer_priv;
1256     struct net_device *peer;
1257     int err;
1258 
1259     /* sanity check. Upper bounds are already enforced by the caller */
1260     if (!ch->rx_count || !ch->tx_count)
1261         return -EINVAL;
1262 
1263     /* avoid braking XDP, if that is enabled */
1264     peer = rtnl_dereference(priv->peer);
1265     peer_priv = peer ? netdev_priv(peer) : NULL;
1266     if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
1267         return -EINVAL;
1268 
1269     if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
1270         return -EINVAL;
1271 
1272     old_rx_count = dev->real_num_rx_queues;
1273     new_rx_count = ch->rx_count;
1274     if (netif_running(dev)) {
1275         /* turn device off */
1276         netif_carrier_off(dev);
1277         if (peer)
1278             netif_carrier_off(peer);
1279 
1280         /* try to allocate new resurces, as needed*/
1281         err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
1282         if (err)
1283             goto out;
1284     }
1285 
1286     err = netif_set_real_num_rx_queues(dev, ch->rx_count);
1287     if (err)
1288         goto revert;
1289 
1290     err = netif_set_real_num_tx_queues(dev, ch->tx_count);
1291     if (err) {
1292         int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
1293 
1294         /* this error condition could happen only if rx and tx change
1295          * in opposite directions (e.g. tx nr raises, rx nr decreases)
1296          * and we can't do anything to fully restore the original
1297          * status
1298          */
1299         if (err2)
1300             pr_warn("Can't restore rx queues config %d -> %d %d",
1301                 new_rx_count, old_rx_count, err2);
1302         else
1303             goto revert;
1304     }
1305 
1306 out:
1307     if (netif_running(dev)) {
1308         /* note that we need to swap the arguments WRT the enable part
1309          * to identify the range we have to disable
1310          */
1311         veth_disable_range_safe(dev, new_rx_count, old_rx_count);
1312         netif_carrier_on(dev);
1313         if (peer)
1314             netif_carrier_on(peer);
1315     }
1316     return err;
1317 
1318 revert:
1319     new_rx_count = old_rx_count;
1320     old_rx_count = ch->rx_count;
1321     goto out;
1322 }
1323 
1324 static int veth_open(struct net_device *dev)
1325 {
1326     struct veth_priv *priv = netdev_priv(dev);
1327     struct net_device *peer = rtnl_dereference(priv->peer);
1328     int err;
1329 
1330     if (!peer)
1331         return -ENOTCONN;
1332 
1333     if (priv->_xdp_prog) {
1334         err = veth_enable_xdp(dev);
1335         if (err)
1336             return err;
1337     } else if (veth_gro_requested(dev)) {
1338         err = veth_napi_enable(dev);
1339         if (err)
1340             return err;
1341     }
1342 
1343     if (peer->flags & IFF_UP) {
1344         netif_carrier_on(dev);
1345         netif_carrier_on(peer);
1346     }
1347 
1348     return 0;
1349 }
1350 
1351 static int veth_close(struct net_device *dev)
1352 {
1353     struct veth_priv *priv = netdev_priv(dev);
1354     struct net_device *peer = rtnl_dereference(priv->peer);
1355 
1356     netif_carrier_off(dev);
1357     if (peer)
1358         netif_carrier_off(peer);
1359 
1360     if (priv->_xdp_prog)
1361         veth_disable_xdp(dev);
1362     else if (veth_gro_requested(dev))
1363         veth_napi_del(dev);
1364 
1365     return 0;
1366 }
1367 
1368 static int is_valid_veth_mtu(int mtu)
1369 {
1370     return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
1371 }
1372 
1373 static int veth_alloc_queues(struct net_device *dev)
1374 {
1375     struct veth_priv *priv = netdev_priv(dev);
1376     int i;
1377 
1378     priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL_ACCOUNT);
1379     if (!priv->rq)
1380         return -ENOMEM;
1381 
1382     for (i = 0; i < dev->num_rx_queues; i++) {
1383         priv->rq[i].dev = dev;
1384         u64_stats_init(&priv->rq[i].stats.syncp);
1385     }
1386 
1387     return 0;
1388 }
1389 
1390 static void veth_free_queues(struct net_device *dev)
1391 {
1392     struct veth_priv *priv = netdev_priv(dev);
1393 
1394     kfree(priv->rq);
1395 }
1396 
1397 static int veth_dev_init(struct net_device *dev)
1398 {
1399     int err;
1400 
1401     dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
1402     if (!dev->lstats)
1403         return -ENOMEM;
1404 
1405     err = veth_alloc_queues(dev);
1406     if (err) {
1407         free_percpu(dev->lstats);
1408         return err;
1409     }
1410 
1411     return 0;
1412 }
1413 
1414 static void veth_dev_free(struct net_device *dev)
1415 {
1416     veth_free_queues(dev);
1417     free_percpu(dev->lstats);
1418 }
1419 
1420 #ifdef CONFIG_NET_POLL_CONTROLLER
1421 static void veth_poll_controller(struct net_device *dev)
1422 {
1423     /* veth only receives frames when its peer sends one
1424      * Since it has nothing to do with disabling irqs, we are guaranteed
1425      * never to have pending data when we poll for it so
1426      * there is nothing to do here.
1427      *
1428      * We need this though so netpoll recognizes us as an interface that
1429      * supports polling, which enables bridge devices in virt setups to
1430      * still use netconsole
1431      */
1432 }
1433 #endif  /* CONFIG_NET_POLL_CONTROLLER */
1434 
1435 static int veth_get_iflink(const struct net_device *dev)
1436 {
1437     struct veth_priv *priv = netdev_priv(dev);
1438     struct net_device *peer;
1439     int iflink;
1440 
1441     rcu_read_lock();
1442     peer = rcu_dereference(priv->peer);
1443     iflink = peer ? peer->ifindex : 0;
1444     rcu_read_unlock();
1445 
1446     return iflink;
1447 }
1448 
1449 static netdev_features_t veth_fix_features(struct net_device *dev,
1450                        netdev_features_t features)
1451 {
1452     struct veth_priv *priv = netdev_priv(dev);
1453     struct net_device *peer;
1454 
1455     peer = rtnl_dereference(priv->peer);
1456     if (peer) {
1457         struct veth_priv *peer_priv = netdev_priv(peer);
1458 
1459         if (peer_priv->_xdp_prog)
1460             features &= ~NETIF_F_GSO_SOFTWARE;
1461     }
1462     if (priv->_xdp_prog)
1463         features |= NETIF_F_GRO;
1464 
1465     return features;
1466 }
1467 
1468 static int veth_set_features(struct net_device *dev,
1469                  netdev_features_t features)
1470 {
1471     netdev_features_t changed = features ^ dev->features;
1472     struct veth_priv *priv = netdev_priv(dev);
1473     int err;
1474 
1475     if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1476         return 0;
1477 
1478     if (features & NETIF_F_GRO) {
1479         err = veth_napi_enable(dev);
1480         if (err)
1481             return err;
1482     } else {
1483         veth_napi_del(dev);
1484     }
1485     return 0;
1486 }
1487 
1488 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1489 {
1490     struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1491     struct net_device *peer;
1492 
1493     if (new_hr < 0)
1494         new_hr = 0;
1495 
1496     rcu_read_lock();
1497     peer = rcu_dereference(priv->peer);
1498     if (unlikely(!peer))
1499         goto out;
1500 
1501     peer_priv = netdev_priv(peer);
1502     priv->requested_headroom = new_hr;
1503     new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1504     dev->needed_headroom = new_hr;
1505     peer->needed_headroom = new_hr;
1506 
1507 out:
1508     rcu_read_unlock();
1509 }
1510 
1511 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1512             struct netlink_ext_ack *extack)
1513 {
1514     struct veth_priv *priv = netdev_priv(dev);
1515     struct bpf_prog *old_prog;
1516     struct net_device *peer;
1517     unsigned int max_mtu;
1518     int err;
1519 
1520     old_prog = priv->_xdp_prog;
1521     priv->_xdp_prog = prog;
1522     peer = rtnl_dereference(priv->peer);
1523 
1524     if (prog) {
1525         if (!peer) {
1526             NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1527             err = -ENOTCONN;
1528             goto err;
1529         }
1530 
1531         max_mtu = SKB_WITH_OVERHEAD(PAGE_SIZE - VETH_XDP_HEADROOM) -
1532               peer->hard_header_len;
1533         /* Allow increasing the max_mtu if the program supports
1534          * XDP fragments.
1535          */
1536         if (prog->aux->xdp_has_frags)
1537             max_mtu += PAGE_SIZE * MAX_SKB_FRAGS;
1538 
1539         if (peer->mtu > max_mtu) {
1540             NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1541             err = -ERANGE;
1542             goto err;
1543         }
1544 
1545         if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1546             NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1547             err = -ENOSPC;
1548             goto err;
1549         }
1550 
1551         if (dev->flags & IFF_UP) {
1552             err = veth_enable_xdp(dev);
1553             if (err) {
1554                 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1555                 goto err;
1556             }
1557         }
1558 
1559         if (!old_prog) {
1560             peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1561             peer->max_mtu = max_mtu;
1562         }
1563     }
1564 
1565     if (old_prog) {
1566         if (!prog) {
1567             if (dev->flags & IFF_UP)
1568                 veth_disable_xdp(dev);
1569 
1570             if (peer) {
1571                 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1572                 peer->max_mtu = ETH_MAX_MTU;
1573             }
1574         }
1575         bpf_prog_put(old_prog);
1576     }
1577 
1578     if ((!!old_prog ^ !!prog) && peer)
1579         netdev_update_features(peer);
1580 
1581     return 0;
1582 err:
1583     priv->_xdp_prog = old_prog;
1584 
1585     return err;
1586 }
1587 
1588 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1589 {
1590     switch (xdp->command) {
1591     case XDP_SETUP_PROG:
1592         return veth_xdp_set(dev, xdp->prog, xdp->extack);
1593     default:
1594         return -EINVAL;
1595     }
1596 }
1597 
1598 static const struct net_device_ops veth_netdev_ops = {
1599     .ndo_init            = veth_dev_init,
1600     .ndo_open            = veth_open,
1601     .ndo_stop            = veth_close,
1602     .ndo_start_xmit      = veth_xmit,
1603     .ndo_get_stats64     = veth_get_stats64,
1604     .ndo_set_rx_mode     = veth_set_multicast_list,
1605     .ndo_set_mac_address = eth_mac_addr,
1606 #ifdef CONFIG_NET_POLL_CONTROLLER
1607     .ndo_poll_controller    = veth_poll_controller,
1608 #endif
1609     .ndo_get_iflink     = veth_get_iflink,
1610     .ndo_fix_features   = veth_fix_features,
1611     .ndo_set_features   = veth_set_features,
1612     .ndo_features_check = passthru_features_check,
1613     .ndo_set_rx_headroom    = veth_set_rx_headroom,
1614     .ndo_bpf        = veth_xdp,
1615     .ndo_xdp_xmit       = veth_ndo_xdp_xmit,
1616     .ndo_get_peer_dev   = veth_peer_dev,
1617 };
1618 
1619 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1620                NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1621                NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1622                NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1623                NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1624 
1625 static void veth_setup(struct net_device *dev)
1626 {
1627     ether_setup(dev);
1628 
1629     dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1630     dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1631     dev->priv_flags |= IFF_NO_QUEUE;
1632     dev->priv_flags |= IFF_PHONY_HEADROOM;
1633 
1634     dev->netdev_ops = &veth_netdev_ops;
1635     dev->ethtool_ops = &veth_ethtool_ops;
1636     dev->features |= NETIF_F_LLTX;
1637     dev->features |= VETH_FEATURES;
1638     dev->vlan_features = dev->features &
1639                  ~(NETIF_F_HW_VLAN_CTAG_TX |
1640                    NETIF_F_HW_VLAN_STAG_TX |
1641                    NETIF_F_HW_VLAN_CTAG_RX |
1642                    NETIF_F_HW_VLAN_STAG_RX);
1643     dev->needs_free_netdev = true;
1644     dev->priv_destructor = veth_dev_free;
1645     dev->max_mtu = ETH_MAX_MTU;
1646 
1647     dev->hw_features = VETH_FEATURES;
1648     dev->hw_enc_features = VETH_FEATURES;
1649     dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1650     netif_set_tso_max_size(dev, GSO_MAX_SIZE);
1651 }
1652 
1653 /*
1654  * netlink interface
1655  */
1656 
1657 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1658              struct netlink_ext_ack *extack)
1659 {
1660     if (tb[IFLA_ADDRESS]) {
1661         if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1662             return -EINVAL;
1663         if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1664             return -EADDRNOTAVAIL;
1665     }
1666     if (tb[IFLA_MTU]) {
1667         if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1668             return -EINVAL;
1669     }
1670     return 0;
1671 }
1672 
1673 static struct rtnl_link_ops veth_link_ops;
1674 
1675 static void veth_disable_gro(struct net_device *dev)
1676 {
1677     dev->features &= ~NETIF_F_GRO;
1678     dev->wanted_features &= ~NETIF_F_GRO;
1679     netdev_update_features(dev);
1680 }
1681 
1682 static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
1683 {
1684     int err;
1685 
1686     if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
1687         err = netif_set_real_num_tx_queues(dev, 1);
1688         if (err)
1689             return err;
1690     }
1691     if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
1692         err = netif_set_real_num_rx_queues(dev, 1);
1693         if (err)
1694             return err;
1695     }
1696     return 0;
1697 }
1698 
1699 static int veth_newlink(struct net *src_net, struct net_device *dev,
1700             struct nlattr *tb[], struct nlattr *data[],
1701             struct netlink_ext_ack *extack)
1702 {
1703     int err;
1704     struct net_device *peer;
1705     struct veth_priv *priv;
1706     char ifname[IFNAMSIZ];
1707     struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1708     unsigned char name_assign_type;
1709     struct ifinfomsg *ifmp;
1710     struct net *net;
1711 
1712     /*
1713      * create and register peer first
1714      */
1715     if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1716         struct nlattr *nla_peer;
1717 
1718         nla_peer = data[VETH_INFO_PEER];
1719         ifmp = nla_data(nla_peer);
1720         err = rtnl_nla_parse_ifla(peer_tb,
1721                       nla_data(nla_peer) + sizeof(struct ifinfomsg),
1722                       nla_len(nla_peer) - sizeof(struct ifinfomsg),
1723                       NULL);
1724         if (err < 0)
1725             return err;
1726 
1727         err = veth_validate(peer_tb, NULL, extack);
1728         if (err < 0)
1729             return err;
1730 
1731         tbp = peer_tb;
1732     } else {
1733         ifmp = NULL;
1734         tbp = tb;
1735     }
1736 
1737     if (ifmp && tbp[IFLA_IFNAME]) {
1738         nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1739         name_assign_type = NET_NAME_USER;
1740     } else {
1741         snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1742         name_assign_type = NET_NAME_ENUM;
1743     }
1744 
1745     net = rtnl_link_get_net(src_net, tbp);
1746     if (IS_ERR(net))
1747         return PTR_ERR(net);
1748 
1749     peer = rtnl_create_link(net, ifname, name_assign_type,
1750                 &veth_link_ops, tbp, extack);
1751     if (IS_ERR(peer)) {
1752         put_net(net);
1753         return PTR_ERR(peer);
1754     }
1755 
1756     if (!ifmp || !tbp[IFLA_ADDRESS])
1757         eth_hw_addr_random(peer);
1758 
1759     if (ifmp && (dev->ifindex != 0))
1760         peer->ifindex = ifmp->ifi_index;
1761 
1762     netif_inherit_tso_max(peer, dev);
1763 
1764     err = register_netdevice(peer);
1765     put_net(net);
1766     net = NULL;
1767     if (err < 0)
1768         goto err_register_peer;
1769 
1770     /* keep GRO disabled by default to be consistent with the established
1771      * veth behavior
1772      */
1773     veth_disable_gro(peer);
1774     netif_carrier_off(peer);
1775 
1776     err = rtnl_configure_link(peer, ifmp);
1777     if (err < 0)
1778         goto err_configure_peer;
1779 
1780     /*
1781      * register dev last
1782      *
1783      * note, that since we've registered new device the dev's name
1784      * should be re-allocated
1785      */
1786 
1787     if (tb[IFLA_ADDRESS] == NULL)
1788         eth_hw_addr_random(dev);
1789 
1790     if (tb[IFLA_IFNAME])
1791         nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1792     else
1793         snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1794 
1795     err = register_netdevice(dev);
1796     if (err < 0)
1797         goto err_register_dev;
1798 
1799     netif_carrier_off(dev);
1800 
1801     /*
1802      * tie the deviced together
1803      */
1804 
1805     priv = netdev_priv(dev);
1806     rcu_assign_pointer(priv->peer, peer);
1807     err = veth_init_queues(dev, tb);
1808     if (err)
1809         goto err_queues;
1810 
1811     priv = netdev_priv(peer);
1812     rcu_assign_pointer(priv->peer, dev);
1813     err = veth_init_queues(peer, tb);
1814     if (err)
1815         goto err_queues;
1816 
1817     veth_disable_gro(dev);
1818     return 0;
1819 
1820 err_queues:
1821     unregister_netdevice(dev);
1822 err_register_dev:
1823     /* nothing to do */
1824 err_configure_peer:
1825     unregister_netdevice(peer);
1826     return err;
1827 
1828 err_register_peer:
1829     free_netdev(peer);
1830     return err;
1831 }
1832 
1833 static void veth_dellink(struct net_device *dev, struct list_head *head)
1834 {
1835     struct veth_priv *priv;
1836     struct net_device *peer;
1837 
1838     priv = netdev_priv(dev);
1839     peer = rtnl_dereference(priv->peer);
1840 
1841     /* Note : dellink() is called from default_device_exit_batch(),
1842      * before a rcu_synchronize() point. The devices are guaranteed
1843      * not being freed before one RCU grace period.
1844      */
1845     RCU_INIT_POINTER(priv->peer, NULL);
1846     unregister_netdevice_queue(dev, head);
1847 
1848     if (peer) {
1849         priv = netdev_priv(peer);
1850         RCU_INIT_POINTER(priv->peer, NULL);
1851         unregister_netdevice_queue(peer, head);
1852     }
1853 }
1854 
1855 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1856     [VETH_INFO_PEER]    = { .len = sizeof(struct ifinfomsg) },
1857 };
1858 
1859 static struct net *veth_get_link_net(const struct net_device *dev)
1860 {
1861     struct veth_priv *priv = netdev_priv(dev);
1862     struct net_device *peer = rtnl_dereference(priv->peer);
1863 
1864     return peer ? dev_net(peer) : dev_net(dev);
1865 }
1866 
1867 static unsigned int veth_get_num_queues(void)
1868 {
1869     /* enforce the same queue limit as rtnl_create_link */
1870     int queues = num_possible_cpus();
1871 
1872     if (queues > 4096)
1873         queues = 4096;
1874     return queues;
1875 }
1876 
1877 static struct rtnl_link_ops veth_link_ops = {
1878     .kind       = DRV_NAME,
1879     .priv_size  = sizeof(struct veth_priv),
1880     .setup      = veth_setup,
1881     .validate   = veth_validate,
1882     .newlink    = veth_newlink,
1883     .dellink    = veth_dellink,
1884     .policy     = veth_policy,
1885     .maxtype    = VETH_INFO_MAX,
1886     .get_link_net   = veth_get_link_net,
1887     .get_num_tx_queues  = veth_get_num_queues,
1888     .get_num_rx_queues  = veth_get_num_queues,
1889 };
1890 
1891 /*
1892  * init/fini
1893  */
1894 
1895 static __init int veth_init(void)
1896 {
1897     return rtnl_link_register(&veth_link_ops);
1898 }
1899 
1900 static __exit void veth_exit(void)
1901 {
1902     rtnl_link_unregister(&veth_link_ops);
1903 }
1904 
1905 module_init(veth_init);
1906 module_exit(veth_exit);
1907 
1908 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1909 MODULE_LICENSE("GPL v2");
1910 MODULE_ALIAS_RTNL_LINK(DRV_NAME);