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 /* Separating two types of XDP xmit */
38 #define VETH_XDP_TX BIT(0)
39 #define VETH_XDP_REDIR BIT(1)
41 struct veth_rq_stats {
45 struct u64_stats_sync syncp;
49 struct napi_struct xdp_napi;
50 struct net_device *dev;
51 struct bpf_prog __rcu *xdp_prog;
52 struct xdp_mem_info xdp_mem;
53 struct veth_rq_stats stats;
54 bool rx_notify_masked;
55 struct ptr_ring xdp_ring;
56 struct xdp_rxq_info xdp_rxq;
60 struct net_device __rcu *peer;
62 struct bpf_prog *_xdp_prog;
64 unsigned int requested_headroom;
71 struct veth_q_stat_desc {
72 char desc[ETH_GSTRING_LEN];
76 #define VETH_RQ_STAT(m) offsetof(struct veth_rq_stats, m)
78 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
79 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
80 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
81 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
84 #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
87 const char string[ETH_GSTRING_LEN];
88 } ethtool_stats_keys[] = {
92 static int veth_get_link_ksettings(struct net_device *dev,
93 struct ethtool_link_ksettings *cmd)
95 cmd->base.speed = SPEED_10000;
96 cmd->base.duplex = DUPLEX_FULL;
97 cmd->base.port = PORT_TP;
98 cmd->base.autoneg = AUTONEG_DISABLE;
102 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
104 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
105 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
108 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
110 char *p = (char *)buf;
115 memcpy(p, ðtool_stats_keys, sizeof(ethtool_stats_keys));
116 p += sizeof(ethtool_stats_keys);
117 for (i = 0; i < dev->real_num_rx_queues; i++) {
118 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
119 snprintf(p, ETH_GSTRING_LEN,
121 i, veth_rq_stats_desc[j].desc);
122 p += ETH_GSTRING_LEN;
129 static int veth_get_sset_count(struct net_device *dev, int sset)
133 return ARRAY_SIZE(ethtool_stats_keys) +
134 VETH_RQ_STATS_LEN * dev->real_num_rx_queues;
140 static void veth_get_ethtool_stats(struct net_device *dev,
141 struct ethtool_stats *stats, u64 *data)
143 struct veth_priv *priv = netdev_priv(dev);
144 struct net_device *peer = rtnl_dereference(priv->peer);
147 data[0] = peer ? peer->ifindex : 0;
149 for (i = 0; i < dev->real_num_rx_queues; i++) {
150 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
151 const void *stats_base = (void *)rq_stats;
156 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
157 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
158 offset = veth_rq_stats_desc[j].offset;
159 data[idx + j] = *(u64 *)(stats_base + offset);
161 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
162 idx += VETH_RQ_STATS_LEN;
166 static const struct ethtool_ops veth_ethtool_ops = {
167 .get_drvinfo = veth_get_drvinfo,
168 .get_link = ethtool_op_get_link,
169 .get_strings = veth_get_strings,
170 .get_sset_count = veth_get_sset_count,
171 .get_ethtool_stats = veth_get_ethtool_stats,
172 .get_link_ksettings = veth_get_link_ksettings,
173 .get_ts_info = ethtool_op_get_ts_info,
176 /* general routines */
178 static bool veth_is_xdp_frame(void *ptr)
180 return (unsigned long)ptr & VETH_XDP_FLAG;
183 static void *veth_ptr_to_xdp(void *ptr)
185 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
188 static void *veth_xdp_to_ptr(void *ptr)
190 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
193 static void veth_ptr_free(void *ptr)
195 if (veth_is_xdp_frame(ptr))
196 xdp_return_frame(veth_ptr_to_xdp(ptr));
201 static void __veth_xdp_flush(struct veth_rq *rq)
203 /* Write ptr_ring before reading rx_notify_masked */
205 if (!rq->rx_notify_masked) {
206 rq->rx_notify_masked = true;
207 napi_schedule(&rq->xdp_napi);
211 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
213 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
214 dev_kfree_skb_any(skb);
218 return NET_RX_SUCCESS;
221 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
222 struct veth_rq *rq, bool xdp)
224 return __dev_forward_skb(dev, skb) ?: xdp ?
225 veth_xdp_rx(rq, skb) :
229 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
231 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
232 struct veth_rq *rq = NULL;
233 struct net_device *rcv;
234 int length = skb->len;
235 bool rcv_xdp = false;
239 rcv = rcu_dereference(priv->peer);
240 if (unlikely(!rcv)) {
245 rcv_priv = netdev_priv(rcv);
246 rxq = skb_get_queue_mapping(skb);
247 if (rxq < rcv->real_num_rx_queues) {
248 rq = &rcv_priv->rq[rxq];
249 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
251 skb_record_rx_queue(skb, rxq);
254 skb_tx_timestamp(skb);
255 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
257 struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
259 u64_stats_update_begin(&stats->syncp);
260 stats->bytes += length;
262 u64_stats_update_end(&stats->syncp);
266 atomic64_inc(&priv->dropped);
270 __veth_xdp_flush(rq);
277 static u64 veth_stats_tx(struct pcpu_lstats *result, struct net_device *dev)
279 struct veth_priv *priv = netdev_priv(dev);
284 for_each_possible_cpu(cpu) {
285 struct pcpu_lstats *stats = per_cpu_ptr(dev->lstats, cpu);
290 start = u64_stats_fetch_begin_irq(&stats->syncp);
291 packets = stats->packets;
292 bytes = stats->bytes;
293 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
294 result->packets += packets;
295 result->bytes += bytes;
297 return atomic64_read(&priv->dropped);
300 static void veth_stats_rx(struct veth_rq_stats *result, struct net_device *dev)
302 struct veth_priv *priv = netdev_priv(dev);
305 result->xdp_packets = 0;
306 result->xdp_bytes = 0;
307 result->xdp_drops = 0;
308 for (i = 0; i < dev->num_rx_queues; i++) {
309 struct veth_rq_stats *stats = &priv->rq[i].stats;
310 u64 packets, bytes, drops;
314 start = u64_stats_fetch_begin_irq(&stats->syncp);
315 packets = stats->xdp_packets;
316 bytes = stats->xdp_bytes;
317 drops = stats->xdp_drops;
318 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
319 result->xdp_packets += packets;
320 result->xdp_bytes += bytes;
321 result->xdp_drops += drops;
325 static void veth_get_stats64(struct net_device *dev,
326 struct rtnl_link_stats64 *tot)
328 struct veth_priv *priv = netdev_priv(dev);
329 struct net_device *peer;
330 struct veth_rq_stats rx;
331 struct pcpu_lstats tx;
333 tot->tx_dropped = veth_stats_tx(&tx, dev);
334 tot->tx_bytes = tx.bytes;
335 tot->tx_packets = tx.packets;
337 veth_stats_rx(&rx, dev);
338 tot->rx_dropped = rx.xdp_drops;
339 tot->rx_bytes = rx.xdp_bytes;
340 tot->rx_packets = rx.xdp_packets;
343 peer = rcu_dereference(priv->peer);
345 tot->rx_dropped += veth_stats_tx(&tx, peer);
346 tot->rx_bytes += tx.bytes;
347 tot->rx_packets += tx.packets;
349 veth_stats_rx(&rx, peer);
350 tot->tx_bytes += rx.xdp_bytes;
351 tot->tx_packets += rx.xdp_packets;
356 /* fake multicast ability */
357 static void veth_set_multicast_list(struct net_device *dev)
361 static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
367 buflen = SKB_DATA_ALIGN(headroom + len) +
368 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
370 skb = build_skb(head, buflen);
374 skb_reserve(skb, headroom);
380 static int veth_select_rxq(struct net_device *dev)
382 return smp_processor_id() % dev->real_num_rx_queues;
385 static int veth_xdp_xmit(struct net_device *dev, int n,
386 struct xdp_frame **frames, u32 flags)
388 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
389 struct net_device *rcv;
390 int i, ret, drops = n;
391 unsigned int max_len;
394 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
399 rcv = rcu_dereference(priv->peer);
400 if (unlikely(!rcv)) {
405 rcv_priv = netdev_priv(rcv);
406 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
407 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
408 * side. This means an XDP program is loaded on the peer and the peer
411 if (!rcu_access_pointer(rq->xdp_prog)) {
417 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
419 spin_lock(&rq->xdp_ring.producer_lock);
420 for (i = 0; i < n; i++) {
421 struct xdp_frame *frame = frames[i];
422 void *ptr = veth_xdp_to_ptr(frame);
424 if (unlikely(frame->len > max_len ||
425 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
426 xdp_return_frame_rx_napi(frame);
430 spin_unlock(&rq->xdp_ring.producer_lock);
432 if (flags & XDP_XMIT_FLUSH)
433 __veth_xdp_flush(rq);
440 atomic64_add(drops, &priv->dropped);
445 static void veth_xdp_flush(struct net_device *dev)
447 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
448 struct net_device *rcv;
452 rcv = rcu_dereference(priv->peer);
456 rcv_priv = netdev_priv(rcv);
457 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
458 /* xdp_ring is initialized on receive side? */
459 if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
462 __veth_xdp_flush(rq);
467 static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
469 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
471 if (unlikely(!frame))
474 return veth_xdp_xmit(dev, 1, &frame, 0);
477 static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
478 struct xdp_frame *frame,
479 unsigned int *xdp_xmit)
481 void *hard_start = frame->data - frame->headroom;
482 void *head = hard_start - sizeof(struct xdp_frame);
483 int len = frame->len, delta = 0;
484 struct xdp_frame orig_frame;
485 struct bpf_prog *xdp_prog;
486 unsigned int headroom;
490 xdp_prog = rcu_dereference(rq->xdp_prog);
491 if (likely(xdp_prog)) {
495 xdp.data_hard_start = hard_start;
496 xdp.data = frame->data;
497 xdp.data_end = frame->data + frame->len;
498 xdp.data_meta = frame->data - frame->metasize;
499 xdp.rxq = &rq->xdp_rxq;
501 act = bpf_prog_run_xdp(xdp_prog, &xdp);
505 delta = frame->data - xdp.data;
506 len = xdp.data_end - xdp.data;
510 xdp.data_hard_start = head;
511 xdp.rxq->mem = frame->mem;
512 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
513 trace_xdp_exception(rq->dev, xdp_prog, act);
517 *xdp_xmit |= VETH_XDP_TX;
522 xdp.data_hard_start = head;
523 xdp.rxq->mem = frame->mem;
524 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
528 *xdp_xmit |= VETH_XDP_REDIR;
532 bpf_warn_invalid_xdp_action(act);
535 trace_xdp_exception(rq->dev, xdp_prog, act);
543 headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
544 skb = veth_build_skb(head, headroom, len, 0);
546 xdp_return_frame(frame);
550 xdp_release_frame(frame);
551 xdp_scrub_frame(frame);
552 skb->protocol = eth_type_trans(skb, rq->dev);
557 xdp_return_frame(frame);
562 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb,
563 unsigned int *xdp_xmit)
565 u32 pktlen, headroom, act, metalen;
566 void *orig_data, *orig_data_end;
567 struct bpf_prog *xdp_prog;
568 int mac_len, delta, off;
574 xdp_prog = rcu_dereference(rq->xdp_prog);
575 if (unlikely(!xdp_prog)) {
580 mac_len = skb->data - skb_mac_header(skb);
581 pktlen = skb->len + mac_len;
582 headroom = skb_headroom(skb) - mac_len;
584 if (skb_shared(skb) || skb_head_is_locked(skb) ||
585 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
586 struct sk_buff *nskb;
591 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
592 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
593 if (size > PAGE_SIZE)
596 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
600 head = page_address(page);
601 start = head + VETH_XDP_HEADROOM;
602 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
603 page_frag_free(head);
607 nskb = veth_build_skb(head,
608 VETH_XDP_HEADROOM + mac_len, skb->len,
611 page_frag_free(head);
615 skb_copy_header(nskb, skb);
616 head_off = skb_headroom(nskb) - skb_headroom(skb);
617 skb_headers_offset_update(nskb, head_off);
622 xdp.data_hard_start = skb->head;
623 xdp.data = skb_mac_header(skb);
624 xdp.data_end = xdp.data + pktlen;
625 xdp.data_meta = xdp.data;
626 xdp.rxq = &rq->xdp_rxq;
627 orig_data = xdp.data;
628 orig_data_end = xdp.data_end;
630 act = bpf_prog_run_xdp(xdp_prog, &xdp);
636 get_page(virt_to_page(xdp.data));
638 xdp.rxq->mem = rq->xdp_mem;
639 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
640 trace_xdp_exception(rq->dev, xdp_prog, act);
643 *xdp_xmit |= VETH_XDP_TX;
647 get_page(virt_to_page(xdp.data));
649 xdp.rxq->mem = rq->xdp_mem;
650 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
652 *xdp_xmit |= VETH_XDP_REDIR;
656 bpf_warn_invalid_xdp_action(act);
659 trace_xdp_exception(rq->dev, xdp_prog, act);
666 delta = orig_data - xdp.data;
667 off = mac_len + delta;
669 __skb_push(skb, off);
671 __skb_pull(skb, -off);
672 skb->mac_header -= delta;
673 off = xdp.data_end - orig_data_end;
676 skb->protocol = eth_type_trans(skb, rq->dev);
678 metalen = xdp.data - xdp.data_meta;
680 skb_metadata_set(skb, metalen);
689 page_frag_free(xdp.data);
694 static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit)
696 int i, done = 0, drops = 0, bytes = 0;
698 for (i = 0; i < budget; i++) {
699 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
700 unsigned int xdp_xmit_one = 0;
706 if (veth_is_xdp_frame(ptr)) {
707 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
710 skb = veth_xdp_rcv_one(rq, frame, &xdp_xmit_one);
714 skb = veth_xdp_rcv_skb(rq, skb, &xdp_xmit_one);
716 *xdp_xmit |= xdp_xmit_one;
719 napi_gro_receive(&rq->xdp_napi, skb);
720 else if (!xdp_xmit_one)
726 u64_stats_update_begin(&rq->stats.syncp);
727 rq->stats.xdp_packets += done;
728 rq->stats.xdp_bytes += bytes;
729 rq->stats.xdp_drops += drops;
730 u64_stats_update_end(&rq->stats.syncp);
735 static int veth_poll(struct napi_struct *napi, int budget)
738 container_of(napi, struct veth_rq, xdp_napi);
739 unsigned int xdp_xmit = 0;
742 xdp_set_return_frame_no_direct();
743 done = veth_xdp_rcv(rq, budget, &xdp_xmit);
745 if (done < budget && napi_complete_done(napi, done)) {
746 /* Write rx_notify_masked before reading ptr_ring */
747 smp_store_mb(rq->rx_notify_masked, false);
748 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
749 rq->rx_notify_masked = true;
750 napi_schedule(&rq->xdp_napi);
754 if (xdp_xmit & VETH_XDP_TX)
755 veth_xdp_flush(rq->dev);
756 if (xdp_xmit & VETH_XDP_REDIR)
758 xdp_clear_return_frame_no_direct();
763 static int veth_napi_add(struct net_device *dev)
765 struct veth_priv *priv = netdev_priv(dev);
768 for (i = 0; i < dev->real_num_rx_queues; i++) {
769 struct veth_rq *rq = &priv->rq[i];
771 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
776 for (i = 0; i < dev->real_num_rx_queues; i++) {
777 struct veth_rq *rq = &priv->rq[i];
779 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
780 napi_enable(&rq->xdp_napi);
785 for (i--; i >= 0; i--)
786 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
791 static void veth_napi_del(struct net_device *dev)
793 struct veth_priv *priv = netdev_priv(dev);
796 for (i = 0; i < dev->real_num_rx_queues; i++) {
797 struct veth_rq *rq = &priv->rq[i];
799 napi_disable(&rq->xdp_napi);
800 napi_hash_del(&rq->xdp_napi);
804 for (i = 0; i < dev->real_num_rx_queues; i++) {
805 struct veth_rq *rq = &priv->rq[i];
807 netif_napi_del(&rq->xdp_napi);
808 rq->rx_notify_masked = false;
809 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
813 static int veth_enable_xdp(struct net_device *dev)
815 struct veth_priv *priv = netdev_priv(dev);
818 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
819 for (i = 0; i < dev->real_num_rx_queues; i++) {
820 struct veth_rq *rq = &priv->rq[i];
822 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
826 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
827 MEM_TYPE_PAGE_SHARED,
832 /* Save original mem info as it can be overwritten */
833 rq->xdp_mem = rq->xdp_rxq.mem;
836 err = veth_napi_add(dev);
841 for (i = 0; i < dev->real_num_rx_queues; i++)
842 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
846 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
848 for (i--; i >= 0; i--)
849 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
854 static void veth_disable_xdp(struct net_device *dev)
856 struct veth_priv *priv = netdev_priv(dev);
859 for (i = 0; i < dev->real_num_rx_queues; i++)
860 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
862 for (i = 0; i < dev->real_num_rx_queues; i++) {
863 struct veth_rq *rq = &priv->rq[i];
865 rq->xdp_rxq.mem = rq->xdp_mem;
866 xdp_rxq_info_unreg(&rq->xdp_rxq);
870 static int veth_open(struct net_device *dev)
872 struct veth_priv *priv = netdev_priv(dev);
873 struct net_device *peer = rtnl_dereference(priv->peer);
879 if (priv->_xdp_prog) {
880 err = veth_enable_xdp(dev);
885 if (peer->flags & IFF_UP) {
886 netif_carrier_on(dev);
887 netif_carrier_on(peer);
893 static int veth_close(struct net_device *dev)
895 struct veth_priv *priv = netdev_priv(dev);
896 struct net_device *peer = rtnl_dereference(priv->peer);
898 netif_carrier_off(dev);
900 netif_carrier_off(peer);
903 veth_disable_xdp(dev);
908 static int is_valid_veth_mtu(int mtu)
910 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
913 static int veth_alloc_queues(struct net_device *dev)
915 struct veth_priv *priv = netdev_priv(dev);
918 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
922 for (i = 0; i < dev->num_rx_queues; i++) {
923 priv->rq[i].dev = dev;
924 u64_stats_init(&priv->rq[i].stats.syncp);
930 static void veth_free_queues(struct net_device *dev)
932 struct veth_priv *priv = netdev_priv(dev);
937 static int veth_dev_init(struct net_device *dev)
941 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
945 err = veth_alloc_queues(dev);
947 free_percpu(dev->lstats);
954 static void veth_dev_free(struct net_device *dev)
956 veth_free_queues(dev);
957 free_percpu(dev->lstats);
960 #ifdef CONFIG_NET_POLL_CONTROLLER
961 static void veth_poll_controller(struct net_device *dev)
963 /* veth only receives frames when its peer sends one
964 * Since it has nothing to do with disabling irqs, we are guaranteed
965 * never to have pending data when we poll for it so
966 * there is nothing to do here.
968 * We need this though so netpoll recognizes us as an interface that
969 * supports polling, which enables bridge devices in virt setups to
970 * still use netconsole
973 #endif /* CONFIG_NET_POLL_CONTROLLER */
975 static int veth_get_iflink(const struct net_device *dev)
977 struct veth_priv *priv = netdev_priv(dev);
978 struct net_device *peer;
982 peer = rcu_dereference(priv->peer);
983 iflink = peer ? peer->ifindex : 0;
989 static netdev_features_t veth_fix_features(struct net_device *dev,
990 netdev_features_t features)
992 struct veth_priv *priv = netdev_priv(dev);
993 struct net_device *peer;
995 peer = rtnl_dereference(priv->peer);
997 struct veth_priv *peer_priv = netdev_priv(peer);
999 if (peer_priv->_xdp_prog)
1000 features &= ~NETIF_F_GSO_SOFTWARE;
1006 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1008 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1009 struct net_device *peer;
1015 peer = rcu_dereference(priv->peer);
1016 if (unlikely(!peer))
1019 peer_priv = netdev_priv(peer);
1020 priv->requested_headroom = new_hr;
1021 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1022 dev->needed_headroom = new_hr;
1023 peer->needed_headroom = new_hr;
1029 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1030 struct netlink_ext_ack *extack)
1032 struct veth_priv *priv = netdev_priv(dev);
1033 struct bpf_prog *old_prog;
1034 struct net_device *peer;
1035 unsigned int max_mtu;
1038 old_prog = priv->_xdp_prog;
1039 priv->_xdp_prog = prog;
1040 peer = rtnl_dereference(priv->peer);
1044 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1049 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1050 peer->hard_header_len -
1051 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1052 if (peer->mtu > max_mtu) {
1053 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1058 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1059 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1064 if (dev->flags & IFF_UP) {
1065 err = veth_enable_xdp(dev);
1067 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1073 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1074 peer->max_mtu = max_mtu;
1080 if (dev->flags & IFF_UP)
1081 veth_disable_xdp(dev);
1084 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1085 peer->max_mtu = ETH_MAX_MTU;
1088 bpf_prog_put(old_prog);
1091 if ((!!old_prog ^ !!prog) && peer)
1092 netdev_update_features(peer);
1096 priv->_xdp_prog = old_prog;
1101 static u32 veth_xdp_query(struct net_device *dev)
1103 struct veth_priv *priv = netdev_priv(dev);
1104 const struct bpf_prog *xdp_prog;
1106 xdp_prog = priv->_xdp_prog;
1108 return xdp_prog->aux->id;
1113 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1115 switch (xdp->command) {
1116 case XDP_SETUP_PROG:
1117 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1118 case XDP_QUERY_PROG:
1119 xdp->prog_id = veth_xdp_query(dev);
1126 static const struct net_device_ops veth_netdev_ops = {
1127 .ndo_init = veth_dev_init,
1128 .ndo_open = veth_open,
1129 .ndo_stop = veth_close,
1130 .ndo_start_xmit = veth_xmit,
1131 .ndo_get_stats64 = veth_get_stats64,
1132 .ndo_set_rx_mode = veth_set_multicast_list,
1133 .ndo_set_mac_address = eth_mac_addr,
1134 #ifdef CONFIG_NET_POLL_CONTROLLER
1135 .ndo_poll_controller = veth_poll_controller,
1137 .ndo_get_iflink = veth_get_iflink,
1138 .ndo_fix_features = veth_fix_features,
1139 .ndo_features_check = passthru_features_check,
1140 .ndo_set_rx_headroom = veth_set_rx_headroom,
1141 .ndo_bpf = veth_xdp,
1142 .ndo_xdp_xmit = veth_xdp_xmit,
1145 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1146 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1147 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1148 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1149 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1151 static void veth_setup(struct net_device *dev)
1155 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1156 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1157 dev->priv_flags |= IFF_NO_QUEUE;
1158 dev->priv_flags |= IFF_PHONY_HEADROOM;
1160 dev->netdev_ops = &veth_netdev_ops;
1161 dev->ethtool_ops = &veth_ethtool_ops;
1162 dev->features |= NETIF_F_LLTX;
1163 dev->features |= VETH_FEATURES;
1164 dev->vlan_features = dev->features &
1165 ~(NETIF_F_HW_VLAN_CTAG_TX |
1166 NETIF_F_HW_VLAN_STAG_TX |
1167 NETIF_F_HW_VLAN_CTAG_RX |
1168 NETIF_F_HW_VLAN_STAG_RX);
1169 dev->needs_free_netdev = true;
1170 dev->priv_destructor = veth_dev_free;
1171 dev->max_mtu = ETH_MAX_MTU;
1173 dev->hw_features = VETH_FEATURES;
1174 dev->hw_enc_features = VETH_FEATURES;
1175 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1182 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1183 struct netlink_ext_ack *extack)
1185 if (tb[IFLA_ADDRESS]) {
1186 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1188 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1189 return -EADDRNOTAVAIL;
1192 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1198 static struct rtnl_link_ops veth_link_ops;
1200 static int veth_newlink(struct net *src_net, struct net_device *dev,
1201 struct nlattr *tb[], struct nlattr *data[],
1202 struct netlink_ext_ack *extack)
1205 struct net_device *peer;
1206 struct veth_priv *priv;
1207 char ifname[IFNAMSIZ];
1208 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1209 unsigned char name_assign_type;
1210 struct ifinfomsg *ifmp;
1214 * create and register peer first
1216 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1217 struct nlattr *nla_peer;
1219 nla_peer = data[VETH_INFO_PEER];
1220 ifmp = nla_data(nla_peer);
1221 err = rtnl_nla_parse_ifla(peer_tb,
1222 nla_data(nla_peer) + sizeof(struct ifinfomsg),
1223 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1228 err = veth_validate(peer_tb, NULL, extack);
1238 if (ifmp && tbp[IFLA_IFNAME]) {
1239 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1240 name_assign_type = NET_NAME_USER;
1242 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1243 name_assign_type = NET_NAME_ENUM;
1246 net = rtnl_link_get_net(src_net, tbp);
1248 return PTR_ERR(net);
1250 peer = rtnl_create_link(net, ifname, name_assign_type,
1251 &veth_link_ops, tbp, extack);
1254 return PTR_ERR(peer);
1257 if (!ifmp || !tbp[IFLA_ADDRESS])
1258 eth_hw_addr_random(peer);
1260 if (ifmp && (dev->ifindex != 0))
1261 peer->ifindex = ifmp->ifi_index;
1263 peer->gso_max_size = dev->gso_max_size;
1264 peer->gso_max_segs = dev->gso_max_segs;
1266 err = register_netdevice(peer);
1270 goto err_register_peer;
1272 netif_carrier_off(peer);
1274 err = rtnl_configure_link(peer, ifmp);
1276 goto err_configure_peer;
1281 * note, that since we've registered new device the dev's name
1282 * should be re-allocated
1285 if (tb[IFLA_ADDRESS] == NULL)
1286 eth_hw_addr_random(dev);
1288 if (tb[IFLA_IFNAME])
1289 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1291 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1293 err = register_netdevice(dev);
1295 goto err_register_dev;
1297 netif_carrier_off(dev);
1300 * tie the deviced together
1303 priv = netdev_priv(dev);
1304 rcu_assign_pointer(priv->peer, peer);
1306 priv = netdev_priv(peer);
1307 rcu_assign_pointer(priv->peer, dev);
1314 unregister_netdevice(peer);
1322 static void veth_dellink(struct net_device *dev, struct list_head *head)
1324 struct veth_priv *priv;
1325 struct net_device *peer;
1327 priv = netdev_priv(dev);
1328 peer = rtnl_dereference(priv->peer);
1330 /* Note : dellink() is called from default_device_exit_batch(),
1331 * before a rcu_synchronize() point. The devices are guaranteed
1332 * not being freed before one RCU grace period.
1334 RCU_INIT_POINTER(priv->peer, NULL);
1335 unregister_netdevice_queue(dev, head);
1338 priv = netdev_priv(peer);
1339 RCU_INIT_POINTER(priv->peer, NULL);
1340 unregister_netdevice_queue(peer, head);
1344 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1345 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1348 static struct net *veth_get_link_net(const struct net_device *dev)
1350 struct veth_priv *priv = netdev_priv(dev);
1351 struct net_device *peer = rtnl_dereference(priv->peer);
1353 return peer ? dev_net(peer) : dev_net(dev);
1356 static struct rtnl_link_ops veth_link_ops = {
1358 .priv_size = sizeof(struct veth_priv),
1359 .setup = veth_setup,
1360 .validate = veth_validate,
1361 .newlink = veth_newlink,
1362 .dellink = veth_dellink,
1363 .policy = veth_policy,
1364 .maxtype = VETH_INFO_MAX,
1365 .get_link_net = veth_get_link_net,
1372 static __init int veth_init(void)
1374 return rtnl_link_register(&veth_link_ops);
1377 static __exit void veth_exit(void)
1379 rtnl_link_unregister(&veth_link_ops);
1382 module_init(veth_init);
1383 module_exit(veth_exit);
1385 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1386 MODULE_LICENSE("GPL v2");
1387 MODULE_ALIAS_RTNL_LINK(DRV_NAME);