1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
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>
18 #include <net/rtnetlink.h>
22 #include <linux/veth.h>
23 #include <linux/module.h>
24 #include <linux/bpf.h>
25 #include <linux/filter.h>
26 #include <linux/ptr_ring.h>
27 #include <linux/bpf_trace.h>
28 #include <linux/net_tstamp.h>
30 #define DRV_NAME "veth"
31 #define DRV_VERSION "1.0"
33 #define VETH_XDP_FLAG BIT(0)
34 #define VETH_RING_SIZE 256
35 #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
37 #define VETH_XDP_TX_BULK_SIZE 16
38 #define VETH_XDP_BATCH 16
50 u64 peer_tq_xdp_xmit_err;
53 struct veth_rq_stats {
55 struct u64_stats_sync syncp;
59 struct napi_struct xdp_napi;
60 struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
61 struct net_device *dev;
62 struct bpf_prog __rcu *xdp_prog;
63 struct xdp_mem_info xdp_mem;
64 struct veth_rq_stats stats;
65 bool rx_notify_masked;
66 struct ptr_ring xdp_ring;
67 struct xdp_rxq_info xdp_rxq;
71 struct net_device __rcu *peer;
73 struct bpf_prog *_xdp_prog;
75 unsigned int requested_headroom;
78 struct veth_xdp_tx_bq {
79 struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
87 struct veth_q_stat_desc {
88 char desc[ETH_GSTRING_LEN];
92 #define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
94 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
95 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
96 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
97 { "drops", VETH_RQ_STAT(rx_drops) },
98 { "xdp_redirect", VETH_RQ_STAT(xdp_redirect) },
99 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
100 { "xdp_tx", VETH_RQ_STAT(xdp_tx) },
101 { "xdp_tx_errors", VETH_RQ_STAT(xdp_tx_err) },
104 #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
106 static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
107 { "xdp_xmit", VETH_RQ_STAT(peer_tq_xdp_xmit) },
108 { "xdp_xmit_errors", VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
111 #define VETH_TQ_STATS_LEN ARRAY_SIZE(veth_tq_stats_desc)
114 const char string[ETH_GSTRING_LEN];
115 } ethtool_stats_keys[] = {
119 struct veth_xdp_buff {
124 static int veth_get_link_ksettings(struct net_device *dev,
125 struct ethtool_link_ksettings *cmd)
127 cmd->base.speed = SPEED_10000;
128 cmd->base.duplex = DUPLEX_FULL;
129 cmd->base.port = PORT_TP;
130 cmd->base.autoneg = AUTONEG_DISABLE;
134 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
136 strscpy(info->driver, DRV_NAME, sizeof(info->driver));
137 strscpy(info->version, DRV_VERSION, sizeof(info->version));
140 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
147 memcpy(p, ðtool_stats_keys, sizeof(ethtool_stats_keys));
148 p += sizeof(ethtool_stats_keys);
149 for (i = 0; i < dev->real_num_rx_queues; i++)
150 for (j = 0; j < VETH_RQ_STATS_LEN; j++)
151 ethtool_sprintf(&p, "rx_queue_%u_%.18s",
152 i, veth_rq_stats_desc[j].desc);
154 for (i = 0; i < dev->real_num_tx_queues; i++)
155 for (j = 0; j < VETH_TQ_STATS_LEN; j++)
156 ethtool_sprintf(&p, "tx_queue_%u_%.18s",
157 i, veth_tq_stats_desc[j].desc);
162 static int veth_get_sset_count(struct net_device *dev, int sset)
166 return ARRAY_SIZE(ethtool_stats_keys) +
167 VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
168 VETH_TQ_STATS_LEN * dev->real_num_tx_queues;
174 static void veth_get_ethtool_stats(struct net_device *dev,
175 struct ethtool_stats *stats, u64 *data)
177 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
178 struct net_device *peer = rtnl_dereference(priv->peer);
181 data[0] = peer ? peer->ifindex : 0;
183 for (i = 0; i < dev->real_num_rx_queues; i++) {
184 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
185 const void *stats_base = (void *)&rq_stats->vs;
190 start = u64_stats_fetch_begin(&rq_stats->syncp);
191 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
192 offset = veth_rq_stats_desc[j].offset;
193 data[idx + j] = *(u64 *)(stats_base + offset);
195 } while (u64_stats_fetch_retry(&rq_stats->syncp, start));
196 idx += VETH_RQ_STATS_LEN;
202 rcv_priv = netdev_priv(peer);
203 for (i = 0; i < peer->real_num_rx_queues; i++) {
204 const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
205 const void *base = (void *)&rq_stats->vs;
206 unsigned int start, tx_idx = idx;
209 tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
211 start = u64_stats_fetch_begin(&rq_stats->syncp);
212 for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
213 offset = veth_tq_stats_desc[j].offset;
214 data[tx_idx + j] += *(u64 *)(base + offset);
216 } while (u64_stats_fetch_retry(&rq_stats->syncp, start));
220 static void veth_get_channels(struct net_device *dev,
221 struct ethtool_channels *channels)
223 channels->tx_count = dev->real_num_tx_queues;
224 channels->rx_count = dev->real_num_rx_queues;
225 channels->max_tx = dev->num_tx_queues;
226 channels->max_rx = dev->num_rx_queues;
229 static int veth_set_channels(struct net_device *dev,
230 struct ethtool_channels *ch);
232 static const struct ethtool_ops veth_ethtool_ops = {
233 .get_drvinfo = veth_get_drvinfo,
234 .get_link = ethtool_op_get_link,
235 .get_strings = veth_get_strings,
236 .get_sset_count = veth_get_sset_count,
237 .get_ethtool_stats = veth_get_ethtool_stats,
238 .get_link_ksettings = veth_get_link_ksettings,
239 .get_ts_info = ethtool_op_get_ts_info,
240 .get_channels = veth_get_channels,
241 .set_channels = veth_set_channels,
244 /* general routines */
246 static bool veth_is_xdp_frame(void *ptr)
248 return (unsigned long)ptr & VETH_XDP_FLAG;
251 static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
253 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
256 static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
258 return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
261 static void veth_ptr_free(void *ptr)
263 if (veth_is_xdp_frame(ptr))
264 xdp_return_frame(veth_ptr_to_xdp(ptr));
269 static void __veth_xdp_flush(struct veth_rq *rq)
271 /* Write ptr_ring before reading rx_notify_masked */
273 if (!READ_ONCE(rq->rx_notify_masked) &&
274 napi_schedule_prep(&rq->xdp_napi)) {
275 WRITE_ONCE(rq->rx_notify_masked, true);
276 __napi_schedule(&rq->xdp_napi);
280 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
282 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
283 dev_kfree_skb_any(skb);
287 return NET_RX_SUCCESS;
290 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
291 struct veth_rq *rq, bool xdp)
293 return __dev_forward_skb(dev, skb) ?: xdp ?
294 veth_xdp_rx(rq, skb) :
298 /* return true if the specified skb has chances of GRO aggregation
299 * Don't strive for accuracy, but try to avoid GRO overhead in the most
301 * When XDP is enabled, all traffic is considered eligible, as the xmit
302 * device has TSO off.
303 * When TSO is enabled on the xmit device, we are likely interested only
304 * in UDP aggregation, explicitly check for that if the skb is suspected
305 * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
306 * to belong to locally generated UDP traffic.
308 static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
309 const struct net_device *rcv,
310 const struct sk_buff *skb)
312 return !(dev->features & NETIF_F_ALL_TSO) ||
313 (skb->destructor == sock_wfree &&
314 rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
317 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
319 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
320 struct veth_rq *rq = NULL;
321 struct net_device *rcv;
322 int length = skb->len;
323 bool use_napi = false;
327 rcv = rcu_dereference(priv->peer);
328 if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
333 rcv_priv = netdev_priv(rcv);
334 rxq = skb_get_queue_mapping(skb);
335 if (rxq < rcv->real_num_rx_queues) {
336 rq = &rcv_priv->rq[rxq];
338 /* The napi pointer is available when an XDP program is
339 * attached or when GRO is enabled
340 * Don't bother with napi/GRO if the skb can't be aggregated
342 use_napi = rcu_access_pointer(rq->napi) &&
343 veth_skb_is_eligible_for_gro(dev, rcv, skb);
346 skb_tx_timestamp(skb);
347 if (likely(veth_forward_skb(rcv, skb, rq, use_napi) == NET_RX_SUCCESS)) {
349 dev_lstats_add(dev, length);
352 atomic64_inc(&priv->dropped);
356 __veth_xdp_flush(rq);
363 static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
365 struct veth_priv *priv = netdev_priv(dev);
367 dev_lstats_read(dev, packets, bytes);
368 return atomic64_read(&priv->dropped);
371 static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
373 struct veth_priv *priv = netdev_priv(dev);
376 result->peer_tq_xdp_xmit_err = 0;
377 result->xdp_packets = 0;
378 result->xdp_tx_err = 0;
379 result->xdp_bytes = 0;
380 result->rx_drops = 0;
381 for (i = 0; i < dev->num_rx_queues; i++) {
382 u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
383 struct veth_rq_stats *stats = &priv->rq[i].stats;
387 start = u64_stats_fetch_begin(&stats->syncp);
388 peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
389 xdp_tx_err = stats->vs.xdp_tx_err;
390 packets = stats->vs.xdp_packets;
391 bytes = stats->vs.xdp_bytes;
392 drops = stats->vs.rx_drops;
393 } while (u64_stats_fetch_retry(&stats->syncp, start));
394 result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
395 result->xdp_tx_err += xdp_tx_err;
396 result->xdp_packets += packets;
397 result->xdp_bytes += bytes;
398 result->rx_drops += drops;
402 static void veth_get_stats64(struct net_device *dev,
403 struct rtnl_link_stats64 *tot)
405 struct veth_priv *priv = netdev_priv(dev);
406 struct net_device *peer;
407 struct veth_stats rx;
410 tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
411 tot->tx_bytes = bytes;
412 tot->tx_packets = packets;
414 veth_stats_rx(&rx, dev);
415 tot->tx_dropped += rx.xdp_tx_err;
416 tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
417 tot->rx_bytes = rx.xdp_bytes;
418 tot->rx_packets = rx.xdp_packets;
421 peer = rcu_dereference(priv->peer);
423 veth_stats_tx(peer, &packets, &bytes);
424 tot->rx_bytes += bytes;
425 tot->rx_packets += packets;
427 veth_stats_rx(&rx, peer);
428 tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
429 tot->rx_dropped += rx.xdp_tx_err;
430 tot->tx_bytes += rx.xdp_bytes;
431 tot->tx_packets += rx.xdp_packets;
436 /* fake multicast ability */
437 static void veth_set_multicast_list(struct net_device *dev)
441 static int veth_select_rxq(struct net_device *dev)
443 return smp_processor_id() % dev->real_num_rx_queues;
446 static struct net_device *veth_peer_dev(struct net_device *dev)
448 struct veth_priv *priv = netdev_priv(dev);
450 /* Callers must be under RCU read side. */
451 return rcu_dereference(priv->peer);
454 static int veth_xdp_xmit(struct net_device *dev, int n,
455 struct xdp_frame **frames,
456 u32 flags, bool ndo_xmit)
458 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
459 int i, ret = -ENXIO, nxmit = 0;
460 struct net_device *rcv;
461 unsigned int max_len;
464 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
468 rcv = rcu_dereference(priv->peer);
472 rcv_priv = netdev_priv(rcv);
473 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
474 /* The napi pointer is set if NAPI is enabled, which ensures that
475 * xdp_ring is initialized on receive side and the peer device is up.
477 if (!rcu_access_pointer(rq->napi))
480 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
482 spin_lock(&rq->xdp_ring.producer_lock);
483 for (i = 0; i < n; i++) {
484 struct xdp_frame *frame = frames[i];
485 void *ptr = veth_xdp_to_ptr(frame);
487 if (unlikely(xdp_get_frame_len(frame) > max_len ||
488 __ptr_ring_produce(&rq->xdp_ring, ptr)))
492 spin_unlock(&rq->xdp_ring.producer_lock);
494 if (flags & XDP_XMIT_FLUSH)
495 __veth_xdp_flush(rq);
499 u64_stats_update_begin(&rq->stats.syncp);
500 rq->stats.vs.peer_tq_xdp_xmit += nxmit;
501 rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
502 u64_stats_update_end(&rq->stats.syncp);
511 static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
512 struct xdp_frame **frames, u32 flags)
516 err = veth_xdp_xmit(dev, n, frames, flags, true);
518 struct veth_priv *priv = netdev_priv(dev);
520 atomic64_add(n, &priv->dropped);
526 static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
528 int sent, i, err = 0, drops;
530 sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
536 for (i = sent; unlikely(i < bq->count); i++)
537 xdp_return_frame(bq->q[i]);
539 drops = bq->count - sent;
540 trace_xdp_bulk_tx(rq->dev, sent, drops, err);
542 u64_stats_update_begin(&rq->stats.syncp);
543 rq->stats.vs.xdp_tx += sent;
544 rq->stats.vs.xdp_tx_err += drops;
545 u64_stats_update_end(&rq->stats.syncp);
550 static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
552 struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
553 struct net_device *rcv;
554 struct veth_rq *rcv_rq;
557 veth_xdp_flush_bq(rq, bq);
558 rcv = rcu_dereference(priv->peer);
562 rcv_priv = netdev_priv(rcv);
563 rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
564 /* xdp_ring is initialized on receive side? */
565 if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
568 __veth_xdp_flush(rcv_rq);
573 static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
574 struct veth_xdp_tx_bq *bq)
576 struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
578 if (unlikely(!frame))
581 if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
582 veth_xdp_flush_bq(rq, bq);
584 bq->q[bq->count++] = frame;
589 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
590 struct xdp_frame *frame,
591 struct veth_xdp_tx_bq *bq,
592 struct veth_stats *stats)
594 struct xdp_frame orig_frame;
595 struct bpf_prog *xdp_prog;
598 xdp_prog = rcu_dereference(rq->xdp_prog);
599 if (likely(xdp_prog)) {
600 struct veth_xdp_buff vxbuf;
601 struct xdp_buff *xdp = &vxbuf.xdp;
604 xdp_convert_frame_to_buff(frame, xdp);
605 xdp->rxq = &rq->xdp_rxq;
608 act = bpf_prog_run_xdp(xdp_prog, xdp);
612 if (xdp_update_frame_from_buff(xdp, frame))
617 xdp->rxq->mem = frame->mem;
618 if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
619 trace_xdp_exception(rq->dev, xdp_prog, act);
629 xdp->rxq->mem = frame->mem;
630 if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
635 stats->xdp_redirect++;
639 bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
642 trace_xdp_exception(rq->dev, xdp_prog, act);
654 xdp_return_frame(frame);
659 /* frames array contains VETH_XDP_BATCH at most */
660 static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
661 int n_xdpf, struct veth_xdp_tx_bq *bq,
662 struct veth_stats *stats)
664 void *skbs[VETH_XDP_BATCH];
667 if (xdp_alloc_skb_bulk(skbs, n_xdpf,
668 GFP_ATOMIC | __GFP_ZERO) < 0) {
669 for (i = 0; i < n_xdpf; i++)
670 xdp_return_frame(frames[i]);
671 stats->rx_drops += n_xdpf;
676 for (i = 0; i < n_xdpf; i++) {
677 struct sk_buff *skb = skbs[i];
679 skb = __xdp_build_skb_from_frame(frames[i], skb,
682 xdp_return_frame(frames[i]);
686 napi_gro_receive(&rq->xdp_napi, skb);
690 static void veth_xdp_get(struct xdp_buff *xdp)
692 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
695 get_page(virt_to_page(xdp->data));
696 if (likely(!xdp_buff_has_frags(xdp)))
699 for (i = 0; i < sinfo->nr_frags; i++)
700 __skb_frag_ref(&sinfo->frags[i]);
703 static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
704 struct xdp_buff *xdp,
705 struct sk_buff **pskb)
707 struct sk_buff *skb = *pskb;
710 if (skb_shared(skb) || skb_head_is_locked(skb) ||
711 skb_shinfo(skb)->nr_frags ||
712 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
713 u32 size, len, max_head_size, off;
714 struct sk_buff *nskb;
718 /* We need a private copy of the skb and data buffers since
719 * the ebpf program can modify it. We segment the original skb
720 * into order-0 pages without linearize it.
722 * Make sure we have enough space for linear and paged area
724 max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE -
726 if (skb->len > PAGE_SIZE * MAX_SKB_FRAGS + max_head_size)
729 /* Allocate skb head */
730 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
734 nskb = build_skb(page_address(page), PAGE_SIZE);
740 skb_reserve(nskb, VETH_XDP_HEADROOM);
741 size = min_t(u32, skb->len, max_head_size);
742 if (skb_copy_bits(skb, 0, nskb->data, size)) {
748 skb_copy_header(nskb, skb);
749 head_off = skb_headroom(nskb) - skb_headroom(skb);
750 skb_headers_offset_update(nskb, head_off);
752 /* Allocate paged area of new skb */
754 len = skb->len - off;
756 for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
757 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
763 size = min_t(u32, len, PAGE_SIZE);
764 skb_add_rx_frag(nskb, i, page, 0, size, PAGE_SIZE);
765 if (skb_copy_bits(skb, off, page_address(page),
779 /* SKB "head" area always have tailroom for skb_shared_info */
780 frame_sz = skb_end_pointer(skb) - skb->head;
781 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
782 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
783 xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
784 skb_headlen(skb), true);
786 if (skb_is_nonlinear(skb)) {
787 skb_shinfo(skb)->xdp_frags_size = skb->data_len;
788 xdp_buff_set_frags_flag(xdp);
790 xdp_buff_clear_frags_flag(xdp);
802 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
804 struct veth_xdp_tx_bq *bq,
805 struct veth_stats *stats)
807 void *orig_data, *orig_data_end;
808 struct bpf_prog *xdp_prog;
809 struct veth_xdp_buff vxbuf;
810 struct xdp_buff *xdp = &vxbuf.xdp;
814 skb_prepare_for_gro(skb);
817 xdp_prog = rcu_dereference(rq->xdp_prog);
818 if (unlikely(!xdp_prog)) {
823 __skb_push(skb, skb->data - skb_mac_header(skb));
824 if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
828 orig_data = xdp->data;
829 orig_data_end = xdp->data_end;
831 act = bpf_prog_run_xdp(xdp_prog, xdp);
839 xdp->rxq->mem = rq->xdp_mem;
840 if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
841 trace_xdp_exception(rq->dev, xdp_prog, act);
851 xdp->rxq->mem = rq->xdp_mem;
852 if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
856 stats->xdp_redirect++;
860 bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
863 trace_xdp_exception(rq->dev, xdp_prog, act);
871 /* check if bpf_xdp_adjust_head was used */
872 off = orig_data - xdp->data;
874 __skb_push(skb, off);
876 __skb_pull(skb, -off);
878 skb_reset_mac_header(skb);
880 /* check if bpf_xdp_adjust_tail was used */
881 off = xdp->data_end - orig_data_end;
883 __skb_put(skb, off); /* positive on grow, negative on shrink */
885 /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
886 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
888 if (xdp_buff_has_frags(xdp))
889 skb->data_len = skb_shinfo(skb)->xdp_frags_size;
893 skb->protocol = eth_type_trans(skb, rq->dev);
895 metalen = xdp->data - xdp->data_meta;
897 skb_metadata_set(skb, metalen);
908 xdp_return_buff(xdp);
913 static int veth_xdp_rcv(struct veth_rq *rq, int budget,
914 struct veth_xdp_tx_bq *bq,
915 struct veth_stats *stats)
917 int i, done = 0, n_xdpf = 0;
918 void *xdpf[VETH_XDP_BATCH];
920 for (i = 0; i < budget; i++) {
921 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
926 if (veth_is_xdp_frame(ptr)) {
928 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
930 stats->xdp_bytes += xdp_get_frame_len(frame);
931 frame = veth_xdp_rcv_one(rq, frame, bq, stats);
934 xdpf[n_xdpf++] = frame;
935 if (n_xdpf == VETH_XDP_BATCH) {
936 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
943 struct sk_buff *skb = ptr;
945 stats->xdp_bytes += skb->len;
946 skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
948 if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
949 netif_receive_skb(skb);
951 napi_gro_receive(&rq->xdp_napi, skb);
958 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
960 u64_stats_update_begin(&rq->stats.syncp);
961 rq->stats.vs.xdp_redirect += stats->xdp_redirect;
962 rq->stats.vs.xdp_bytes += stats->xdp_bytes;
963 rq->stats.vs.xdp_drops += stats->xdp_drops;
964 rq->stats.vs.rx_drops += stats->rx_drops;
965 rq->stats.vs.xdp_packets += done;
966 u64_stats_update_end(&rq->stats.syncp);
971 static int veth_poll(struct napi_struct *napi, int budget)
974 container_of(napi, struct veth_rq, xdp_napi);
975 struct veth_stats stats = {};
976 struct veth_xdp_tx_bq bq;
981 xdp_set_return_frame_no_direct();
982 done = veth_xdp_rcv(rq, budget, &bq, &stats);
984 if (stats.xdp_redirect > 0)
987 if (done < budget && napi_complete_done(napi, done)) {
988 /* Write rx_notify_masked before reading ptr_ring */
989 smp_store_mb(rq->rx_notify_masked, false);
990 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
991 if (napi_schedule_prep(&rq->xdp_napi)) {
992 WRITE_ONCE(rq->rx_notify_masked, true);
993 __napi_schedule(&rq->xdp_napi);
998 if (stats.xdp_tx > 0)
999 veth_xdp_flush(rq, &bq);
1000 xdp_clear_return_frame_no_direct();
1005 static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
1007 struct veth_priv *priv = netdev_priv(dev);
1010 for (i = start; i < end; i++) {
1011 struct veth_rq *rq = &priv->rq[i];
1013 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
1018 for (i = start; i < end; i++) {
1019 struct veth_rq *rq = &priv->rq[i];
1021 napi_enable(&rq->xdp_napi);
1022 rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1028 for (i--; i >= start; i--)
1029 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
1034 static int __veth_napi_enable(struct net_device *dev)
1036 return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1039 static void veth_napi_del_range(struct net_device *dev, int start, int end)
1041 struct veth_priv *priv = netdev_priv(dev);
1044 for (i = start; i < end; i++) {
1045 struct veth_rq *rq = &priv->rq[i];
1047 rcu_assign_pointer(priv->rq[i].napi, NULL);
1048 napi_disable(&rq->xdp_napi);
1049 __netif_napi_del(&rq->xdp_napi);
1053 for (i = start; i < end; i++) {
1054 struct veth_rq *rq = &priv->rq[i];
1056 rq->rx_notify_masked = false;
1057 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
1061 static void veth_napi_del(struct net_device *dev)
1063 veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1066 static bool veth_gro_requested(const struct net_device *dev)
1068 return !!(dev->wanted_features & NETIF_F_GRO);
1071 static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1072 bool napi_already_on)
1074 struct veth_priv *priv = netdev_priv(dev);
1077 for (i = start; i < end; i++) {
1078 struct veth_rq *rq = &priv->rq[i];
1080 if (!napi_already_on)
1081 netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1082 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1086 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1087 MEM_TYPE_PAGE_SHARED,
1092 /* Save original mem info as it can be overwritten */
1093 rq->xdp_mem = rq->xdp_rxq.mem;
1098 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1100 for (i--; i >= start; i--) {
1101 struct veth_rq *rq = &priv->rq[i];
1103 xdp_rxq_info_unreg(&rq->xdp_rxq);
1104 if (!napi_already_on)
1105 netif_napi_del(&rq->xdp_napi);
1111 static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1114 struct veth_priv *priv = netdev_priv(dev);
1117 for (i = start; i < end; i++) {
1118 struct veth_rq *rq = &priv->rq[i];
1120 rq->xdp_rxq.mem = rq->xdp_mem;
1121 xdp_rxq_info_unreg(&rq->xdp_rxq);
1124 netif_napi_del(&rq->xdp_napi);
1128 static int veth_enable_xdp(struct net_device *dev)
1130 bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1131 struct veth_priv *priv = netdev_priv(dev);
1134 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1135 err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1139 if (!napi_already_on) {
1140 err = __veth_napi_enable(dev);
1142 veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1146 if (!veth_gro_requested(dev)) {
1147 /* user-space did not require GRO, but adding XDP
1148 * is supposed to get GRO working
1150 dev->features |= NETIF_F_GRO;
1151 netdev_features_change(dev);
1156 for (i = 0; i < dev->real_num_rx_queues; i++) {
1157 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1158 rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1164 static void veth_disable_xdp(struct net_device *dev)
1166 struct veth_priv *priv = netdev_priv(dev);
1169 for (i = 0; i < dev->real_num_rx_queues; i++)
1170 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1172 if (!netif_running(dev) || !veth_gro_requested(dev)) {
1175 /* if user-space did not require GRO, since adding XDP
1176 * enabled it, clear it now
1178 if (!veth_gro_requested(dev) && netif_running(dev)) {
1179 dev->features &= ~NETIF_F_GRO;
1180 netdev_features_change(dev);
1184 veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1187 static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1189 struct veth_priv *priv = netdev_priv(dev);
1192 for (i = start; i < end; i++) {
1193 struct veth_rq *rq = &priv->rq[i];
1195 netif_napi_add(dev, &rq->xdp_napi, veth_poll);
1198 err = __veth_napi_enable_range(dev, start, end);
1200 for (i = start; i < end; i++) {
1201 struct veth_rq *rq = &priv->rq[i];
1203 netif_napi_del(&rq->xdp_napi);
1210 static int veth_napi_enable(struct net_device *dev)
1212 return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1215 static void veth_disable_range_safe(struct net_device *dev, int start, int end)
1217 struct veth_priv *priv = netdev_priv(dev);
1222 if (priv->_xdp_prog) {
1223 veth_napi_del_range(dev, start, end);
1224 veth_disable_xdp_range(dev, start, end, false);
1225 } else if (veth_gro_requested(dev)) {
1226 veth_napi_del_range(dev, start, end);
1230 static int veth_enable_range_safe(struct net_device *dev, int start, int end)
1232 struct veth_priv *priv = netdev_priv(dev);
1238 if (priv->_xdp_prog) {
1239 /* these channels are freshly initialized, napi is not on there even
1240 * when GRO is requeste
1242 err = veth_enable_xdp_range(dev, start, end, false);
1246 err = __veth_napi_enable_range(dev, start, end);
1248 /* on error always delete the newly added napis */
1249 veth_disable_xdp_range(dev, start, end, true);
1252 } else if (veth_gro_requested(dev)) {
1253 return veth_napi_enable_range(dev, start, end);
1258 static void veth_set_xdp_features(struct net_device *dev)
1260 struct veth_priv *priv = netdev_priv(dev);
1261 struct net_device *peer;
1263 peer = rtnl_dereference(priv->peer);
1264 if (peer && peer->real_num_tx_queues <= dev->real_num_rx_queues) {
1265 xdp_features_t val = NETDEV_XDP_ACT_BASIC |
1266 NETDEV_XDP_ACT_REDIRECT |
1267 NETDEV_XDP_ACT_RX_SG;
1269 if (priv->_xdp_prog || veth_gro_requested(dev))
1270 val |= NETDEV_XDP_ACT_NDO_XMIT |
1271 NETDEV_XDP_ACT_NDO_XMIT_SG;
1272 xdp_set_features_flag(dev, val);
1274 xdp_clear_features_flag(dev);
1278 static int veth_set_channels(struct net_device *dev,
1279 struct ethtool_channels *ch)
1281 struct veth_priv *priv = netdev_priv(dev);
1282 unsigned int old_rx_count, new_rx_count;
1283 struct veth_priv *peer_priv;
1284 struct net_device *peer;
1287 /* sanity check. Upper bounds are already enforced by the caller */
1288 if (!ch->rx_count || !ch->tx_count)
1291 /* avoid braking XDP, if that is enabled */
1292 peer = rtnl_dereference(priv->peer);
1293 peer_priv = peer ? netdev_priv(peer) : NULL;
1294 if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
1297 if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
1300 old_rx_count = dev->real_num_rx_queues;
1301 new_rx_count = ch->rx_count;
1302 if (netif_running(dev)) {
1303 /* turn device off */
1304 netif_carrier_off(dev);
1306 netif_carrier_off(peer);
1308 /* try to allocate new resurces, as needed*/
1309 err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
1314 err = netif_set_real_num_rx_queues(dev, ch->rx_count);
1318 err = netif_set_real_num_tx_queues(dev, ch->tx_count);
1320 int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
1322 /* this error condition could happen only if rx and tx change
1323 * in opposite directions (e.g. tx nr raises, rx nr decreases)
1324 * and we can't do anything to fully restore the original
1328 pr_warn("Can't restore rx queues config %d -> %d %d",
1329 new_rx_count, old_rx_count, err2);
1335 if (netif_running(dev)) {
1336 /* note that we need to swap the arguments WRT the enable part
1337 * to identify the range we have to disable
1339 veth_disable_range_safe(dev, new_rx_count, old_rx_count);
1340 netif_carrier_on(dev);
1342 netif_carrier_on(peer);
1345 /* update XDP supported features */
1346 veth_set_xdp_features(dev);
1348 veth_set_xdp_features(peer);
1353 new_rx_count = old_rx_count;
1354 old_rx_count = ch->rx_count;
1358 static int veth_open(struct net_device *dev)
1360 struct veth_priv *priv = netdev_priv(dev);
1361 struct net_device *peer = rtnl_dereference(priv->peer);
1367 if (priv->_xdp_prog) {
1368 err = veth_enable_xdp(dev);
1371 } else if (veth_gro_requested(dev)) {
1372 err = veth_napi_enable(dev);
1377 if (peer->flags & IFF_UP) {
1378 netif_carrier_on(dev);
1379 netif_carrier_on(peer);
1385 static int veth_close(struct net_device *dev)
1387 struct veth_priv *priv = netdev_priv(dev);
1388 struct net_device *peer = rtnl_dereference(priv->peer);
1390 netif_carrier_off(dev);
1392 netif_carrier_off(peer);
1394 if (priv->_xdp_prog)
1395 veth_disable_xdp(dev);
1396 else if (veth_gro_requested(dev))
1402 static int is_valid_veth_mtu(int mtu)
1404 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
1407 static int veth_alloc_queues(struct net_device *dev)
1409 struct veth_priv *priv = netdev_priv(dev);
1412 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL_ACCOUNT);
1416 for (i = 0; i < dev->num_rx_queues; i++) {
1417 priv->rq[i].dev = dev;
1418 u64_stats_init(&priv->rq[i].stats.syncp);
1424 static void veth_free_queues(struct net_device *dev)
1426 struct veth_priv *priv = netdev_priv(dev);
1431 static int veth_dev_init(struct net_device *dev)
1435 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
1439 err = veth_alloc_queues(dev);
1441 free_percpu(dev->lstats);
1448 static void veth_dev_free(struct net_device *dev)
1450 veth_free_queues(dev);
1451 free_percpu(dev->lstats);
1454 #ifdef CONFIG_NET_POLL_CONTROLLER
1455 static void veth_poll_controller(struct net_device *dev)
1457 /* veth only receives frames when its peer sends one
1458 * Since it has nothing to do with disabling irqs, we are guaranteed
1459 * never to have pending data when we poll for it so
1460 * there is nothing to do here.
1462 * We need this though so netpoll recognizes us as an interface that
1463 * supports polling, which enables bridge devices in virt setups to
1464 * still use netconsole
1467 #endif /* CONFIG_NET_POLL_CONTROLLER */
1469 static int veth_get_iflink(const struct net_device *dev)
1471 struct veth_priv *priv = netdev_priv(dev);
1472 struct net_device *peer;
1476 peer = rcu_dereference(priv->peer);
1477 iflink = peer ? peer->ifindex : 0;
1483 static netdev_features_t veth_fix_features(struct net_device *dev,
1484 netdev_features_t features)
1486 struct veth_priv *priv = netdev_priv(dev);
1487 struct net_device *peer;
1489 peer = rtnl_dereference(priv->peer);
1491 struct veth_priv *peer_priv = netdev_priv(peer);
1493 if (peer_priv->_xdp_prog)
1494 features &= ~NETIF_F_GSO_SOFTWARE;
1496 if (priv->_xdp_prog)
1497 features |= NETIF_F_GRO;
1502 static int veth_set_features(struct net_device *dev,
1503 netdev_features_t features)
1505 netdev_features_t changed = features ^ dev->features;
1506 struct veth_priv *priv = netdev_priv(dev);
1509 if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1512 if (features & NETIF_F_GRO) {
1513 err = veth_napi_enable(dev);
1517 xdp_features_set_redirect_target(dev, true);
1519 xdp_features_clear_redirect_target(dev);
1525 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1527 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1528 struct net_device *peer;
1534 peer = rcu_dereference(priv->peer);
1535 if (unlikely(!peer))
1538 peer_priv = netdev_priv(peer);
1539 priv->requested_headroom = new_hr;
1540 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1541 dev->needed_headroom = new_hr;
1542 peer->needed_headroom = new_hr;
1548 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1549 struct netlink_ext_ack *extack)
1551 struct veth_priv *priv = netdev_priv(dev);
1552 struct bpf_prog *old_prog;
1553 struct net_device *peer;
1554 unsigned int max_mtu;
1557 old_prog = priv->_xdp_prog;
1558 priv->_xdp_prog = prog;
1559 peer = rtnl_dereference(priv->peer);
1563 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1568 max_mtu = SKB_WITH_OVERHEAD(PAGE_SIZE - VETH_XDP_HEADROOM) -
1569 peer->hard_header_len;
1570 /* Allow increasing the max_mtu if the program supports
1573 if (prog->aux->xdp_has_frags)
1574 max_mtu += PAGE_SIZE * MAX_SKB_FRAGS;
1576 if (peer->mtu > max_mtu) {
1577 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1582 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1583 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1588 if (dev->flags & IFF_UP) {
1589 err = veth_enable_xdp(dev);
1591 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1597 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1598 peer->max_mtu = max_mtu;
1601 xdp_features_set_redirect_target(dev, true);
1606 if (!veth_gro_requested(dev))
1607 xdp_features_clear_redirect_target(dev);
1609 if (dev->flags & IFF_UP)
1610 veth_disable_xdp(dev);
1613 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1614 peer->max_mtu = ETH_MAX_MTU;
1617 bpf_prog_put(old_prog);
1620 if ((!!old_prog ^ !!prog) && peer)
1621 netdev_update_features(peer);
1625 priv->_xdp_prog = old_prog;
1630 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1632 switch (xdp->command) {
1633 case XDP_SETUP_PROG:
1634 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1640 static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
1642 struct veth_xdp_buff *_ctx = (void *)ctx;
1647 *timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
1651 static int veth_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash)
1653 struct veth_xdp_buff *_ctx = (void *)ctx;
1658 *hash = skb_get_hash(_ctx->skb);
1662 static const struct net_device_ops veth_netdev_ops = {
1663 .ndo_init = veth_dev_init,
1664 .ndo_open = veth_open,
1665 .ndo_stop = veth_close,
1666 .ndo_start_xmit = veth_xmit,
1667 .ndo_get_stats64 = veth_get_stats64,
1668 .ndo_set_rx_mode = veth_set_multicast_list,
1669 .ndo_set_mac_address = eth_mac_addr,
1670 #ifdef CONFIG_NET_POLL_CONTROLLER
1671 .ndo_poll_controller = veth_poll_controller,
1673 .ndo_get_iflink = veth_get_iflink,
1674 .ndo_fix_features = veth_fix_features,
1675 .ndo_set_features = veth_set_features,
1676 .ndo_features_check = passthru_features_check,
1677 .ndo_set_rx_headroom = veth_set_rx_headroom,
1678 .ndo_bpf = veth_xdp,
1679 .ndo_xdp_xmit = veth_ndo_xdp_xmit,
1680 .ndo_get_peer_dev = veth_peer_dev,
1683 static const struct xdp_metadata_ops veth_xdp_metadata_ops = {
1684 .xmo_rx_timestamp = veth_xdp_rx_timestamp,
1685 .xmo_rx_hash = veth_xdp_rx_hash,
1688 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1689 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1690 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1691 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1692 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1694 static void veth_setup(struct net_device *dev)
1698 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1699 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1700 dev->priv_flags |= IFF_NO_QUEUE;
1701 dev->priv_flags |= IFF_PHONY_HEADROOM;
1703 dev->netdev_ops = &veth_netdev_ops;
1704 dev->xdp_metadata_ops = &veth_xdp_metadata_ops;
1705 dev->ethtool_ops = &veth_ethtool_ops;
1706 dev->features |= NETIF_F_LLTX;
1707 dev->features |= VETH_FEATURES;
1708 dev->vlan_features = dev->features &
1709 ~(NETIF_F_HW_VLAN_CTAG_TX |
1710 NETIF_F_HW_VLAN_STAG_TX |
1711 NETIF_F_HW_VLAN_CTAG_RX |
1712 NETIF_F_HW_VLAN_STAG_RX);
1713 dev->needs_free_netdev = true;
1714 dev->priv_destructor = veth_dev_free;
1715 dev->max_mtu = ETH_MAX_MTU;
1717 dev->hw_features = VETH_FEATURES;
1718 dev->hw_enc_features = VETH_FEATURES;
1719 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1720 netif_set_tso_max_size(dev, GSO_MAX_SIZE);
1727 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1728 struct netlink_ext_ack *extack)
1730 if (tb[IFLA_ADDRESS]) {
1731 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1733 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1734 return -EADDRNOTAVAIL;
1737 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1743 static struct rtnl_link_ops veth_link_ops;
1745 static void veth_disable_gro(struct net_device *dev)
1747 dev->features &= ~NETIF_F_GRO;
1748 dev->wanted_features &= ~NETIF_F_GRO;
1749 netdev_update_features(dev);
1752 static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
1756 if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
1757 err = netif_set_real_num_tx_queues(dev, 1);
1761 if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
1762 err = netif_set_real_num_rx_queues(dev, 1);
1769 static int veth_newlink(struct net *src_net, struct net_device *dev,
1770 struct nlattr *tb[], struct nlattr *data[],
1771 struct netlink_ext_ack *extack)
1774 struct net_device *peer;
1775 struct veth_priv *priv;
1776 char ifname[IFNAMSIZ];
1777 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1778 unsigned char name_assign_type;
1779 struct ifinfomsg *ifmp;
1783 * create and register peer first
1785 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1786 struct nlattr *nla_peer;
1788 nla_peer = data[VETH_INFO_PEER];
1789 ifmp = nla_data(nla_peer);
1790 err = rtnl_nla_parse_ifla(peer_tb,
1791 nla_data(nla_peer) + sizeof(struct ifinfomsg),
1792 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1797 err = veth_validate(peer_tb, NULL, extack);
1807 if (ifmp && tbp[IFLA_IFNAME]) {
1808 nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1809 name_assign_type = NET_NAME_USER;
1811 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1812 name_assign_type = NET_NAME_ENUM;
1815 net = rtnl_link_get_net(src_net, tbp);
1817 return PTR_ERR(net);
1819 peer = rtnl_create_link(net, ifname, name_assign_type,
1820 &veth_link_ops, tbp, extack);
1823 return PTR_ERR(peer);
1826 if (!ifmp || !tbp[IFLA_ADDRESS])
1827 eth_hw_addr_random(peer);
1829 if (ifmp && (dev->ifindex != 0))
1830 peer->ifindex = ifmp->ifi_index;
1832 netif_inherit_tso_max(peer, dev);
1834 err = register_netdevice(peer);
1838 goto err_register_peer;
1840 /* keep GRO disabled by default to be consistent with the established
1843 veth_disable_gro(peer);
1844 netif_carrier_off(peer);
1846 err = rtnl_configure_link(peer, ifmp, 0, NULL);
1848 goto err_configure_peer;
1853 * note, that since we've registered new device the dev's name
1854 * should be re-allocated
1857 if (tb[IFLA_ADDRESS] == NULL)
1858 eth_hw_addr_random(dev);
1860 if (tb[IFLA_IFNAME])
1861 nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1863 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1865 err = register_netdevice(dev);
1867 goto err_register_dev;
1869 netif_carrier_off(dev);
1872 * tie the deviced together
1875 priv = netdev_priv(dev);
1876 rcu_assign_pointer(priv->peer, peer);
1877 err = veth_init_queues(dev, tb);
1881 priv = netdev_priv(peer);
1882 rcu_assign_pointer(priv->peer, dev);
1883 err = veth_init_queues(peer, tb);
1887 veth_disable_gro(dev);
1888 /* update XDP supported features */
1889 veth_set_xdp_features(dev);
1890 veth_set_xdp_features(peer);
1895 unregister_netdevice(dev);
1899 unregister_netdevice(peer);
1907 static void veth_dellink(struct net_device *dev, struct list_head *head)
1909 struct veth_priv *priv;
1910 struct net_device *peer;
1912 priv = netdev_priv(dev);
1913 peer = rtnl_dereference(priv->peer);
1915 /* Note : dellink() is called from default_device_exit_batch(),
1916 * before a rcu_synchronize() point. The devices are guaranteed
1917 * not being freed before one RCU grace period.
1919 RCU_INIT_POINTER(priv->peer, NULL);
1920 unregister_netdevice_queue(dev, head);
1923 priv = netdev_priv(peer);
1924 RCU_INIT_POINTER(priv->peer, NULL);
1925 unregister_netdevice_queue(peer, head);
1929 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1930 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1933 static struct net *veth_get_link_net(const struct net_device *dev)
1935 struct veth_priv *priv = netdev_priv(dev);
1936 struct net_device *peer = rtnl_dereference(priv->peer);
1938 return peer ? dev_net(peer) : dev_net(dev);
1941 static unsigned int veth_get_num_queues(void)
1943 /* enforce the same queue limit as rtnl_create_link */
1944 int queues = num_possible_cpus();
1951 static struct rtnl_link_ops veth_link_ops = {
1953 .priv_size = sizeof(struct veth_priv),
1954 .setup = veth_setup,
1955 .validate = veth_validate,
1956 .newlink = veth_newlink,
1957 .dellink = veth_dellink,
1958 .policy = veth_policy,
1959 .maxtype = VETH_INFO_MAX,
1960 .get_link_net = veth_get_link_net,
1961 .get_num_tx_queues = veth_get_num_queues,
1962 .get_num_rx_queues = veth_get_num_queues,
1969 static __init int veth_init(void)
1971 return rtnl_link_register(&veth_link_ops);
1974 static __exit void veth_exit(void)
1976 rtnl_link_unregister(&veth_link_ops);
1979 module_init(veth_init);
1980 module_exit(veth_exit);
1982 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1983 MODULE_LICENSE("GPL v2");
1984 MODULE_ALIAS_RTNL_LINK(DRV_NAME);