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 net_device *dev;
61 struct bpf_prog __rcu *xdp_prog;
62 struct xdp_mem_info xdp_mem;
63 struct veth_rq_stats stats;
64 bool rx_notify_masked;
65 struct ptr_ring xdp_ring;
66 struct xdp_rxq_info xdp_rxq;
70 struct net_device __rcu *peer;
72 struct bpf_prog *_xdp_prog;
74 unsigned int requested_headroom;
77 struct veth_xdp_tx_bq {
78 struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
86 struct veth_q_stat_desc {
87 char desc[ETH_GSTRING_LEN];
91 #define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
93 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
94 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
95 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
96 { "drops", VETH_RQ_STAT(rx_drops) },
97 { "xdp_redirect", VETH_RQ_STAT(xdp_redirect) },
98 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
99 { "xdp_tx", VETH_RQ_STAT(xdp_tx) },
100 { "xdp_tx_errors", VETH_RQ_STAT(xdp_tx_err) },
103 #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
105 static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
106 { "xdp_xmit", VETH_RQ_STAT(peer_tq_xdp_xmit) },
107 { "xdp_xmit_errors", VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
110 #define VETH_TQ_STATS_LEN ARRAY_SIZE(veth_tq_stats_desc)
113 const char string[ETH_GSTRING_LEN];
114 } ethtool_stats_keys[] = {
118 static int veth_get_link_ksettings(struct net_device *dev,
119 struct ethtool_link_ksettings *cmd)
121 cmd->base.speed = SPEED_10000;
122 cmd->base.duplex = DUPLEX_FULL;
123 cmd->base.port = PORT_TP;
124 cmd->base.autoneg = AUTONEG_DISABLE;
128 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
130 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
131 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
134 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
136 char *p = (char *)buf;
141 memcpy(p, ðtool_stats_keys, sizeof(ethtool_stats_keys));
142 p += sizeof(ethtool_stats_keys);
143 for (i = 0; i < dev->real_num_rx_queues; i++) {
144 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
145 snprintf(p, ETH_GSTRING_LEN,
147 i, veth_rq_stats_desc[j].desc);
148 p += ETH_GSTRING_LEN;
151 for (i = 0; i < dev->real_num_tx_queues; i++) {
152 for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
153 snprintf(p, ETH_GSTRING_LEN,
155 i, veth_tq_stats_desc[j].desc);
156 p += ETH_GSTRING_LEN;
163 static int veth_get_sset_count(struct net_device *dev, int sset)
167 return ARRAY_SIZE(ethtool_stats_keys) +
168 VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
169 VETH_TQ_STATS_LEN * dev->real_num_tx_queues;
175 static void veth_get_ethtool_stats(struct net_device *dev,
176 struct ethtool_stats *stats, u64 *data)
178 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
179 struct net_device *peer = rtnl_dereference(priv->peer);
182 data[0] = peer ? peer->ifindex : 0;
184 for (i = 0; i < dev->real_num_rx_queues; i++) {
185 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
186 const void *stats_base = (void *)&rq_stats->vs;
191 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
192 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
193 offset = veth_rq_stats_desc[j].offset;
194 data[idx + j] = *(u64 *)(stats_base + offset);
196 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
197 idx += VETH_RQ_STATS_LEN;
203 rcv_priv = netdev_priv(peer);
204 for (i = 0; i < peer->real_num_rx_queues; i++) {
205 const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
206 const void *base = (void *)&rq_stats->vs;
207 unsigned int start, tx_idx = idx;
210 tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
212 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
213 for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
214 offset = veth_tq_stats_desc[j].offset;
215 data[tx_idx + j] += *(u64 *)(base + offset);
217 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
221 static const struct ethtool_ops veth_ethtool_ops = {
222 .get_drvinfo = veth_get_drvinfo,
223 .get_link = ethtool_op_get_link,
224 .get_strings = veth_get_strings,
225 .get_sset_count = veth_get_sset_count,
226 .get_ethtool_stats = veth_get_ethtool_stats,
227 .get_link_ksettings = veth_get_link_ksettings,
228 .get_ts_info = ethtool_op_get_ts_info,
231 /* general routines */
233 static bool veth_is_xdp_frame(void *ptr)
235 return (unsigned long)ptr & VETH_XDP_FLAG;
238 static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
240 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
243 static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
245 return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
248 static void veth_ptr_free(void *ptr)
250 if (veth_is_xdp_frame(ptr))
251 xdp_return_frame(veth_ptr_to_xdp(ptr));
256 static void __veth_xdp_flush(struct veth_rq *rq)
258 /* Write ptr_ring before reading rx_notify_masked */
260 if (!rq->rx_notify_masked) {
261 rq->rx_notify_masked = true;
262 napi_schedule(&rq->xdp_napi);
266 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
268 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
269 dev_kfree_skb_any(skb);
273 return NET_RX_SUCCESS;
276 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
277 struct veth_rq *rq, bool xdp)
279 return __dev_forward_skb(dev, skb) ?: xdp ?
280 veth_xdp_rx(rq, skb) :
284 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
286 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
287 struct veth_rq *rq = NULL;
288 struct net_device *rcv;
289 int length = skb->len;
290 bool rcv_xdp = false;
294 rcv = rcu_dereference(priv->peer);
295 if (unlikely(!rcv)) {
300 rcv_priv = netdev_priv(rcv);
301 rxq = skb_get_queue_mapping(skb);
302 if (rxq < rcv->real_num_rx_queues) {
303 rq = &rcv_priv->rq[rxq];
304 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
306 skb_record_rx_queue(skb, rxq);
309 skb_tx_timestamp(skb);
310 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
312 dev_lstats_add(dev, length);
315 atomic64_inc(&priv->dropped);
319 __veth_xdp_flush(rq);
326 static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
328 struct veth_priv *priv = netdev_priv(dev);
330 dev_lstats_read(dev, packets, bytes);
331 return atomic64_read(&priv->dropped);
334 static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
336 struct veth_priv *priv = netdev_priv(dev);
339 result->peer_tq_xdp_xmit_err = 0;
340 result->xdp_packets = 0;
341 result->xdp_tx_err = 0;
342 result->xdp_bytes = 0;
343 result->rx_drops = 0;
344 for (i = 0; i < dev->num_rx_queues; i++) {
345 u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
346 struct veth_rq_stats *stats = &priv->rq[i].stats;
350 start = u64_stats_fetch_begin_irq(&stats->syncp);
351 peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
352 xdp_tx_err = stats->vs.xdp_tx_err;
353 packets = stats->vs.xdp_packets;
354 bytes = stats->vs.xdp_bytes;
355 drops = stats->vs.rx_drops;
356 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
357 result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
358 result->xdp_tx_err += xdp_tx_err;
359 result->xdp_packets += packets;
360 result->xdp_bytes += bytes;
361 result->rx_drops += drops;
365 static void veth_get_stats64(struct net_device *dev,
366 struct rtnl_link_stats64 *tot)
368 struct veth_priv *priv = netdev_priv(dev);
369 struct net_device *peer;
370 struct veth_stats rx;
373 tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
374 tot->tx_bytes = bytes;
375 tot->tx_packets = packets;
377 veth_stats_rx(&rx, dev);
378 tot->tx_dropped += rx.xdp_tx_err;
379 tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
380 tot->rx_bytes = rx.xdp_bytes;
381 tot->rx_packets = rx.xdp_packets;
384 peer = rcu_dereference(priv->peer);
386 veth_stats_tx(peer, &packets, &bytes);
387 tot->rx_bytes += bytes;
388 tot->rx_packets += packets;
390 veth_stats_rx(&rx, peer);
391 tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
392 tot->rx_dropped += rx.xdp_tx_err;
393 tot->tx_bytes += rx.xdp_bytes;
394 tot->tx_packets += rx.xdp_packets;
399 /* fake multicast ability */
400 static void veth_set_multicast_list(struct net_device *dev)
404 static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
409 skb = build_skb(head, buflen);
413 skb_reserve(skb, headroom);
419 static int veth_select_rxq(struct net_device *dev)
421 return smp_processor_id() % dev->real_num_rx_queues;
424 static struct net_device *veth_peer_dev(struct net_device *dev)
426 struct veth_priv *priv = netdev_priv(dev);
428 /* Callers must be under RCU read side. */
429 return rcu_dereference(priv->peer);
432 static int veth_xdp_xmit(struct net_device *dev, int n,
433 struct xdp_frame **frames,
434 u32 flags, bool ndo_xmit)
436 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
437 int i, ret = -ENXIO, drops = 0;
438 struct net_device *rcv;
439 unsigned int max_len;
442 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
446 rcv = rcu_dereference(priv->peer);
450 rcv_priv = netdev_priv(rcv);
451 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
452 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
453 * side. This means an XDP program is loaded on the peer and the peer
456 if (!rcu_access_pointer(rq->xdp_prog))
459 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
461 spin_lock(&rq->xdp_ring.producer_lock);
462 for (i = 0; i < n; i++) {
463 struct xdp_frame *frame = frames[i];
464 void *ptr = veth_xdp_to_ptr(frame);
466 if (unlikely(frame->len > max_len ||
467 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
468 xdp_return_frame_rx_napi(frame);
472 spin_unlock(&rq->xdp_ring.producer_lock);
474 if (flags & XDP_XMIT_FLUSH)
475 __veth_xdp_flush(rq);
479 u64_stats_update_begin(&rq->stats.syncp);
480 rq->stats.vs.peer_tq_xdp_xmit += n - drops;
481 rq->stats.vs.peer_tq_xdp_xmit_err += drops;
482 u64_stats_update_end(&rq->stats.syncp);
491 static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
492 struct xdp_frame **frames, u32 flags)
496 err = veth_xdp_xmit(dev, n, frames, flags, true);
498 struct veth_priv *priv = netdev_priv(dev);
500 atomic64_add(n, &priv->dropped);
506 static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
508 int sent, i, err = 0;
510 sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
514 for (i = 0; i < bq->count; i++)
515 xdp_return_frame(bq->q[i]);
517 trace_xdp_bulk_tx(rq->dev, sent, bq->count - sent, err);
519 u64_stats_update_begin(&rq->stats.syncp);
520 rq->stats.vs.xdp_tx += sent;
521 rq->stats.vs.xdp_tx_err += bq->count - sent;
522 u64_stats_update_end(&rq->stats.syncp);
527 static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
529 struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
530 struct net_device *rcv;
531 struct veth_rq *rcv_rq;
534 veth_xdp_flush_bq(rq, bq);
535 rcv = rcu_dereference(priv->peer);
539 rcv_priv = netdev_priv(rcv);
540 rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
541 /* xdp_ring is initialized on receive side? */
542 if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
545 __veth_xdp_flush(rcv_rq);
550 static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
551 struct veth_xdp_tx_bq *bq)
553 struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
555 if (unlikely(!frame))
558 if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
559 veth_xdp_flush_bq(rq, bq);
561 bq->q[bq->count++] = frame;
566 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
567 struct xdp_frame *frame,
568 struct veth_xdp_tx_bq *bq,
569 struct veth_stats *stats)
571 struct xdp_frame orig_frame;
572 struct bpf_prog *xdp_prog;
575 xdp_prog = rcu_dereference(rq->xdp_prog);
576 if (likely(xdp_prog)) {
580 xdp_convert_frame_to_buff(frame, &xdp);
581 xdp.rxq = &rq->xdp_rxq;
583 act = bpf_prog_run_xdp(xdp_prog, &xdp);
587 if (xdp_update_frame_from_buff(&xdp, frame))
592 xdp.rxq->mem = frame->mem;
593 if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
594 trace_xdp_exception(rq->dev, xdp_prog, act);
604 xdp.rxq->mem = frame->mem;
605 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
610 stats->xdp_redirect++;
614 bpf_warn_invalid_xdp_action(act);
617 trace_xdp_exception(rq->dev, xdp_prog, act);
629 xdp_return_frame(frame);
634 /* frames array contains VETH_XDP_BATCH at most */
635 static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
636 int n_xdpf, struct veth_xdp_tx_bq *bq,
637 struct veth_stats *stats)
639 void *skbs[VETH_XDP_BATCH];
642 if (xdp_alloc_skb_bulk(skbs, n_xdpf,
643 GFP_ATOMIC | __GFP_ZERO) < 0) {
644 for (i = 0; i < n_xdpf; i++)
645 xdp_return_frame(frames[i]);
646 stats->rx_drops += n_xdpf;
651 for (i = 0; i < n_xdpf; i++) {
652 struct sk_buff *skb = skbs[i];
654 skb = __xdp_build_skb_from_frame(frames[i], skb,
657 xdp_return_frame(frames[i]);
661 napi_gro_receive(&rq->xdp_napi, skb);
665 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
667 struct veth_xdp_tx_bq *bq,
668 struct veth_stats *stats)
670 u32 pktlen, headroom, act, metalen, frame_sz;
671 void *orig_data, *orig_data_end;
672 struct bpf_prog *xdp_prog;
673 int mac_len, delta, off;
679 xdp_prog = rcu_dereference(rq->xdp_prog);
680 if (unlikely(!xdp_prog)) {
685 mac_len = skb->data - skb_mac_header(skb);
686 pktlen = skb->len + mac_len;
687 headroom = skb_headroom(skb) - mac_len;
689 if (skb_shared(skb) || skb_head_is_locked(skb) ||
690 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
691 struct sk_buff *nskb;
696 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
697 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
698 if (size > PAGE_SIZE)
701 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
705 head = page_address(page);
706 start = head + VETH_XDP_HEADROOM;
707 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
708 page_frag_free(head);
712 nskb = veth_build_skb(head, VETH_XDP_HEADROOM + mac_len,
713 skb->len, PAGE_SIZE);
715 page_frag_free(head);
719 skb_copy_header(nskb, skb);
720 head_off = skb_headroom(nskb) - skb_headroom(skb);
721 skb_headers_offset_update(nskb, head_off);
726 /* SKB "head" area always have tailroom for skb_shared_info */
727 frame_sz = skb_end_pointer(skb) - skb->head;
728 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
729 xdp_init_buff(&xdp, frame_sz, &rq->xdp_rxq);
730 xdp_prepare_buff(&xdp, skb->head, skb->mac_header, pktlen, true);
732 orig_data = xdp.data;
733 orig_data_end = xdp.data_end;
735 act = bpf_prog_run_xdp(xdp_prog, &xdp);
741 get_page(virt_to_page(xdp.data));
743 xdp.rxq->mem = rq->xdp_mem;
744 if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
745 trace_xdp_exception(rq->dev, xdp_prog, act);
753 get_page(virt_to_page(xdp.data));
755 xdp.rxq->mem = rq->xdp_mem;
756 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
760 stats->xdp_redirect++;
764 bpf_warn_invalid_xdp_action(act);
767 trace_xdp_exception(rq->dev, xdp_prog, act);
775 /* check if bpf_xdp_adjust_head was used */
776 delta = orig_data - xdp.data;
777 off = mac_len + delta;
779 __skb_push(skb, off);
781 __skb_pull(skb, -off);
782 skb->mac_header -= delta;
784 /* check if bpf_xdp_adjust_tail was used */
785 off = xdp.data_end - orig_data_end;
787 __skb_put(skb, off); /* positive on grow, negative on shrink */
788 skb->protocol = eth_type_trans(skb, rq->dev);
790 metalen = xdp.data - xdp.data_meta;
792 skb_metadata_set(skb, metalen);
803 page_frag_free(xdp.data);
808 static int veth_xdp_rcv(struct veth_rq *rq, int budget,
809 struct veth_xdp_tx_bq *bq,
810 struct veth_stats *stats)
812 int i, done = 0, n_xdpf = 0;
813 void *xdpf[VETH_XDP_BATCH];
815 for (i = 0; i < budget; i++) {
816 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
821 if (veth_is_xdp_frame(ptr)) {
823 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
825 stats->xdp_bytes += frame->len;
826 frame = veth_xdp_rcv_one(rq, frame, bq, stats);
829 xdpf[n_xdpf++] = frame;
830 if (n_xdpf == VETH_XDP_BATCH) {
831 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
838 struct sk_buff *skb = ptr;
840 stats->xdp_bytes += skb->len;
841 skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
843 napi_gro_receive(&rq->xdp_napi, skb);
849 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
851 u64_stats_update_begin(&rq->stats.syncp);
852 rq->stats.vs.xdp_redirect += stats->xdp_redirect;
853 rq->stats.vs.xdp_bytes += stats->xdp_bytes;
854 rq->stats.vs.xdp_drops += stats->xdp_drops;
855 rq->stats.vs.rx_drops += stats->rx_drops;
856 rq->stats.vs.xdp_packets += done;
857 u64_stats_update_end(&rq->stats.syncp);
862 static int veth_poll(struct napi_struct *napi, int budget)
865 container_of(napi, struct veth_rq, xdp_napi);
866 struct veth_stats stats = {};
867 struct veth_xdp_tx_bq bq;
872 xdp_set_return_frame_no_direct();
873 done = veth_xdp_rcv(rq, budget, &bq, &stats);
875 if (done < budget && napi_complete_done(napi, done)) {
876 /* Write rx_notify_masked before reading ptr_ring */
877 smp_store_mb(rq->rx_notify_masked, false);
878 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
879 rq->rx_notify_masked = true;
880 napi_schedule(&rq->xdp_napi);
884 if (stats.xdp_tx > 0)
885 veth_xdp_flush(rq, &bq);
886 if (stats.xdp_redirect > 0)
888 xdp_clear_return_frame_no_direct();
893 static int veth_napi_add(struct net_device *dev)
895 struct veth_priv *priv = netdev_priv(dev);
898 for (i = 0; i < dev->real_num_rx_queues; i++) {
899 struct veth_rq *rq = &priv->rq[i];
901 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
906 for (i = 0; i < dev->real_num_rx_queues; i++) {
907 struct veth_rq *rq = &priv->rq[i];
909 napi_enable(&rq->xdp_napi);
914 for (i--; i >= 0; i--)
915 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
920 static void veth_napi_del(struct net_device *dev)
922 struct veth_priv *priv = netdev_priv(dev);
925 for (i = 0; i < dev->real_num_rx_queues; i++) {
926 struct veth_rq *rq = &priv->rq[i];
928 napi_disable(&rq->xdp_napi);
929 __netif_napi_del(&rq->xdp_napi);
933 for (i = 0; i < dev->real_num_rx_queues; i++) {
934 struct veth_rq *rq = &priv->rq[i];
936 rq->rx_notify_masked = false;
937 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
941 static int veth_enable_xdp(struct net_device *dev)
943 struct veth_priv *priv = netdev_priv(dev);
946 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
947 for (i = 0; i < dev->real_num_rx_queues; i++) {
948 struct veth_rq *rq = &priv->rq[i];
950 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
951 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
955 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
956 MEM_TYPE_PAGE_SHARED,
961 /* Save original mem info as it can be overwritten */
962 rq->xdp_mem = rq->xdp_rxq.mem;
965 err = veth_napi_add(dev);
970 for (i = 0; i < dev->real_num_rx_queues; i++)
971 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
975 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
977 for (i--; i >= 0; i--) {
978 struct veth_rq *rq = &priv->rq[i];
980 xdp_rxq_info_unreg(&rq->xdp_rxq);
981 netif_napi_del(&rq->xdp_napi);
987 static void veth_disable_xdp(struct net_device *dev)
989 struct veth_priv *priv = netdev_priv(dev);
992 for (i = 0; i < dev->real_num_rx_queues; i++)
993 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
995 for (i = 0; i < dev->real_num_rx_queues; i++) {
996 struct veth_rq *rq = &priv->rq[i];
998 rq->xdp_rxq.mem = rq->xdp_mem;
999 xdp_rxq_info_unreg(&rq->xdp_rxq);
1003 static int veth_open(struct net_device *dev)
1005 struct veth_priv *priv = netdev_priv(dev);
1006 struct net_device *peer = rtnl_dereference(priv->peer);
1012 if (priv->_xdp_prog) {
1013 err = veth_enable_xdp(dev);
1018 if (peer->flags & IFF_UP) {
1019 netif_carrier_on(dev);
1020 netif_carrier_on(peer);
1026 static int veth_close(struct net_device *dev)
1028 struct veth_priv *priv = netdev_priv(dev);
1029 struct net_device *peer = rtnl_dereference(priv->peer);
1031 netif_carrier_off(dev);
1033 netif_carrier_off(peer);
1035 if (priv->_xdp_prog)
1036 veth_disable_xdp(dev);
1041 static int is_valid_veth_mtu(int mtu)
1043 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
1046 static int veth_alloc_queues(struct net_device *dev)
1048 struct veth_priv *priv = netdev_priv(dev);
1051 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
1055 for (i = 0; i < dev->num_rx_queues; i++) {
1056 priv->rq[i].dev = dev;
1057 u64_stats_init(&priv->rq[i].stats.syncp);
1063 static void veth_free_queues(struct net_device *dev)
1065 struct veth_priv *priv = netdev_priv(dev);
1070 static int veth_dev_init(struct net_device *dev)
1074 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
1078 err = veth_alloc_queues(dev);
1080 free_percpu(dev->lstats);
1087 static void veth_dev_free(struct net_device *dev)
1089 veth_free_queues(dev);
1090 free_percpu(dev->lstats);
1093 #ifdef CONFIG_NET_POLL_CONTROLLER
1094 static void veth_poll_controller(struct net_device *dev)
1096 /* veth only receives frames when its peer sends one
1097 * Since it has nothing to do with disabling irqs, we are guaranteed
1098 * never to have pending data when we poll for it so
1099 * there is nothing to do here.
1101 * We need this though so netpoll recognizes us as an interface that
1102 * supports polling, which enables bridge devices in virt setups to
1103 * still use netconsole
1106 #endif /* CONFIG_NET_POLL_CONTROLLER */
1108 static int veth_get_iflink(const struct net_device *dev)
1110 struct veth_priv *priv = netdev_priv(dev);
1111 struct net_device *peer;
1115 peer = rcu_dereference(priv->peer);
1116 iflink = peer ? peer->ifindex : 0;
1122 static netdev_features_t veth_fix_features(struct net_device *dev,
1123 netdev_features_t features)
1125 struct veth_priv *priv = netdev_priv(dev);
1126 struct net_device *peer;
1128 peer = rtnl_dereference(priv->peer);
1130 struct veth_priv *peer_priv = netdev_priv(peer);
1132 if (peer_priv->_xdp_prog)
1133 features &= ~NETIF_F_GSO_SOFTWARE;
1139 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1141 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1142 struct net_device *peer;
1148 peer = rcu_dereference(priv->peer);
1149 if (unlikely(!peer))
1152 peer_priv = netdev_priv(peer);
1153 priv->requested_headroom = new_hr;
1154 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1155 dev->needed_headroom = new_hr;
1156 peer->needed_headroom = new_hr;
1162 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1163 struct netlink_ext_ack *extack)
1165 struct veth_priv *priv = netdev_priv(dev);
1166 struct bpf_prog *old_prog;
1167 struct net_device *peer;
1168 unsigned int max_mtu;
1171 old_prog = priv->_xdp_prog;
1172 priv->_xdp_prog = prog;
1173 peer = rtnl_dereference(priv->peer);
1177 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1182 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1183 peer->hard_header_len -
1184 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1185 if (peer->mtu > max_mtu) {
1186 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1191 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1192 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1197 if (dev->flags & IFF_UP) {
1198 err = veth_enable_xdp(dev);
1200 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1206 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1207 peer->max_mtu = max_mtu;
1213 if (dev->flags & IFF_UP)
1214 veth_disable_xdp(dev);
1217 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1218 peer->max_mtu = ETH_MAX_MTU;
1221 bpf_prog_put(old_prog);
1224 if ((!!old_prog ^ !!prog) && peer)
1225 netdev_update_features(peer);
1229 priv->_xdp_prog = old_prog;
1234 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1236 switch (xdp->command) {
1237 case XDP_SETUP_PROG:
1238 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1244 static const struct net_device_ops veth_netdev_ops = {
1245 .ndo_init = veth_dev_init,
1246 .ndo_open = veth_open,
1247 .ndo_stop = veth_close,
1248 .ndo_start_xmit = veth_xmit,
1249 .ndo_get_stats64 = veth_get_stats64,
1250 .ndo_set_rx_mode = veth_set_multicast_list,
1251 .ndo_set_mac_address = eth_mac_addr,
1252 #ifdef CONFIG_NET_POLL_CONTROLLER
1253 .ndo_poll_controller = veth_poll_controller,
1255 .ndo_get_iflink = veth_get_iflink,
1256 .ndo_fix_features = veth_fix_features,
1257 .ndo_features_check = passthru_features_check,
1258 .ndo_set_rx_headroom = veth_set_rx_headroom,
1259 .ndo_bpf = veth_xdp,
1260 .ndo_xdp_xmit = veth_ndo_xdp_xmit,
1261 .ndo_get_peer_dev = veth_peer_dev,
1264 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1265 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1266 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1267 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1268 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1270 static void veth_setup(struct net_device *dev)
1274 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1275 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1276 dev->priv_flags |= IFF_NO_QUEUE;
1277 dev->priv_flags |= IFF_PHONY_HEADROOM;
1279 dev->netdev_ops = &veth_netdev_ops;
1280 dev->ethtool_ops = &veth_ethtool_ops;
1281 dev->features |= NETIF_F_LLTX;
1282 dev->features |= VETH_FEATURES;
1283 dev->vlan_features = dev->features &
1284 ~(NETIF_F_HW_VLAN_CTAG_TX |
1285 NETIF_F_HW_VLAN_STAG_TX |
1286 NETIF_F_HW_VLAN_CTAG_RX |
1287 NETIF_F_HW_VLAN_STAG_RX);
1288 dev->needs_free_netdev = true;
1289 dev->priv_destructor = veth_dev_free;
1290 dev->max_mtu = ETH_MAX_MTU;
1292 dev->hw_features = VETH_FEATURES;
1293 dev->hw_enc_features = VETH_FEATURES;
1294 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1301 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1302 struct netlink_ext_ack *extack)
1304 if (tb[IFLA_ADDRESS]) {
1305 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1307 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1308 return -EADDRNOTAVAIL;
1311 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1317 static struct rtnl_link_ops veth_link_ops;
1319 static int veth_newlink(struct net *src_net, struct net_device *dev,
1320 struct nlattr *tb[], struct nlattr *data[],
1321 struct netlink_ext_ack *extack)
1324 struct net_device *peer;
1325 struct veth_priv *priv;
1326 char ifname[IFNAMSIZ];
1327 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1328 unsigned char name_assign_type;
1329 struct ifinfomsg *ifmp;
1333 * create and register peer first
1335 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1336 struct nlattr *nla_peer;
1338 nla_peer = data[VETH_INFO_PEER];
1339 ifmp = nla_data(nla_peer);
1340 err = rtnl_nla_parse_ifla(peer_tb,
1341 nla_data(nla_peer) + sizeof(struct ifinfomsg),
1342 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1347 err = veth_validate(peer_tb, NULL, extack);
1357 if (ifmp && tbp[IFLA_IFNAME]) {
1358 nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1359 name_assign_type = NET_NAME_USER;
1361 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1362 name_assign_type = NET_NAME_ENUM;
1365 net = rtnl_link_get_net(src_net, tbp);
1367 return PTR_ERR(net);
1369 peer = rtnl_create_link(net, ifname, name_assign_type,
1370 &veth_link_ops, tbp, extack);
1373 return PTR_ERR(peer);
1376 if (!ifmp || !tbp[IFLA_ADDRESS])
1377 eth_hw_addr_random(peer);
1379 if (ifmp && (dev->ifindex != 0))
1380 peer->ifindex = ifmp->ifi_index;
1382 peer->gso_max_size = dev->gso_max_size;
1383 peer->gso_max_segs = dev->gso_max_segs;
1385 err = register_netdevice(peer);
1389 goto err_register_peer;
1391 netif_carrier_off(peer);
1393 err = rtnl_configure_link(peer, ifmp);
1395 goto err_configure_peer;
1400 * note, that since we've registered new device the dev's name
1401 * should be re-allocated
1404 if (tb[IFLA_ADDRESS] == NULL)
1405 eth_hw_addr_random(dev);
1407 if (tb[IFLA_IFNAME])
1408 nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1410 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1412 err = register_netdevice(dev);
1414 goto err_register_dev;
1416 netif_carrier_off(dev);
1419 * tie the deviced together
1422 priv = netdev_priv(dev);
1423 rcu_assign_pointer(priv->peer, peer);
1425 priv = netdev_priv(peer);
1426 rcu_assign_pointer(priv->peer, dev);
1433 unregister_netdevice(peer);
1441 static void veth_dellink(struct net_device *dev, struct list_head *head)
1443 struct veth_priv *priv;
1444 struct net_device *peer;
1446 priv = netdev_priv(dev);
1447 peer = rtnl_dereference(priv->peer);
1449 /* Note : dellink() is called from default_device_exit_batch(),
1450 * before a rcu_synchronize() point. The devices are guaranteed
1451 * not being freed before one RCU grace period.
1453 RCU_INIT_POINTER(priv->peer, NULL);
1454 unregister_netdevice_queue(dev, head);
1457 priv = netdev_priv(peer);
1458 RCU_INIT_POINTER(priv->peer, NULL);
1459 unregister_netdevice_queue(peer, head);
1463 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1464 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1467 static struct net *veth_get_link_net(const struct net_device *dev)
1469 struct veth_priv *priv = netdev_priv(dev);
1470 struct net_device *peer = rtnl_dereference(priv->peer);
1472 return peer ? dev_net(peer) : dev_net(dev);
1475 static struct rtnl_link_ops veth_link_ops = {
1477 .priv_size = sizeof(struct veth_priv),
1478 .setup = veth_setup,
1479 .validate = veth_validate,
1480 .newlink = veth_newlink,
1481 .dellink = veth_dellink,
1482 .policy = veth_policy,
1483 .maxtype = VETH_INFO_MAX,
1484 .get_link_net = veth_get_link_net,
1491 static __init int veth_init(void)
1493 return rtnl_link_register(&veth_link_ops);
1496 static __exit void veth_exit(void)
1498 rtnl_link_unregister(&veth_link_ops);
1501 module_init(veth_init);
1502 module_exit(veth_exit);
1504 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1505 MODULE_LICENSE("GPL v2");
1506 MODULE_ALIAS_RTNL_LINK(DRV_NAME);