4 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
11 #include <linux/netdevice.h>
12 #include <linux/slab.h>
13 #include <linux/ethtool.h>
14 #include <linux/etherdevice.h>
15 #include <linux/u64_stats_sync.h>
17 #include <net/rtnetlink.h>
21 #include <linux/veth.h>
22 #include <linux/module.h>
23 #include <linux/bpf.h>
24 #include <linux/filter.h>
25 #include <linux/ptr_ring.h>
26 #include <linux/bpf_trace.h>
28 #define DRV_NAME "veth"
29 #define DRV_VERSION "1.0"
31 #define VETH_XDP_FLAG BIT(0)
32 #define VETH_RING_SIZE 256
33 #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
35 /* Separating two types of XDP xmit */
36 #define VETH_XDP_TX BIT(0)
37 #define VETH_XDP_REDIR BIT(1)
42 struct u64_stats_sync syncp;
46 struct napi_struct xdp_napi;
47 struct net_device *dev;
48 struct bpf_prog __rcu *xdp_prog;
49 struct xdp_mem_info xdp_mem;
50 bool rx_notify_masked;
51 struct ptr_ring xdp_ring;
52 struct xdp_rxq_info xdp_rxq;
56 struct net_device __rcu *peer;
58 struct bpf_prog *_xdp_prog;
60 unsigned int requested_headroom;
68 const char string[ETH_GSTRING_LEN];
69 } ethtool_stats_keys[] = {
73 static int veth_get_link_ksettings(struct net_device *dev,
74 struct ethtool_link_ksettings *cmd)
76 cmd->base.speed = SPEED_10000;
77 cmd->base.duplex = DUPLEX_FULL;
78 cmd->base.port = PORT_TP;
79 cmd->base.autoneg = AUTONEG_DISABLE;
83 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
85 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
86 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
89 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
93 memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys));
98 static int veth_get_sset_count(struct net_device *dev, int sset)
102 return ARRAY_SIZE(ethtool_stats_keys);
108 static void veth_get_ethtool_stats(struct net_device *dev,
109 struct ethtool_stats *stats, u64 *data)
111 struct veth_priv *priv = netdev_priv(dev);
112 struct net_device *peer = rtnl_dereference(priv->peer);
114 data[0] = peer ? peer->ifindex : 0;
117 static const struct ethtool_ops veth_ethtool_ops = {
118 .get_drvinfo = veth_get_drvinfo,
119 .get_link = ethtool_op_get_link,
120 .get_strings = veth_get_strings,
121 .get_sset_count = veth_get_sset_count,
122 .get_ethtool_stats = veth_get_ethtool_stats,
123 .get_link_ksettings = veth_get_link_ksettings,
126 /* general routines */
128 static bool veth_is_xdp_frame(void *ptr)
130 return (unsigned long)ptr & VETH_XDP_FLAG;
133 static void *veth_ptr_to_xdp(void *ptr)
135 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
138 static void *veth_xdp_to_ptr(void *ptr)
140 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
143 static void veth_ptr_free(void *ptr)
145 if (veth_is_xdp_frame(ptr))
146 xdp_return_frame(veth_ptr_to_xdp(ptr));
151 static void __veth_xdp_flush(struct veth_rq *rq)
153 /* Write ptr_ring before reading rx_notify_masked */
155 if (!READ_ONCE(rq->rx_notify_masked) &&
156 napi_schedule_prep(&rq->xdp_napi)) {
157 WRITE_ONCE(rq->rx_notify_masked, true);
158 __napi_schedule(&rq->xdp_napi);
162 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
164 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
165 dev_kfree_skb_any(skb);
169 return NET_RX_SUCCESS;
172 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
173 struct veth_rq *rq, bool xdp)
175 return __dev_forward_skb(dev, skb) ?: xdp ?
176 veth_xdp_rx(rq, skb) :
180 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
182 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
183 struct veth_rq *rq = NULL;
184 int ret = NETDEV_TX_OK;
185 struct net_device *rcv;
186 int length = skb->len;
187 bool rcv_xdp = false;
191 rcv = rcu_dereference(priv->peer);
192 if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
197 rcv_priv = netdev_priv(rcv);
198 rxq = skb_get_queue_mapping(skb);
199 if (rxq < rcv->real_num_rx_queues) {
200 rq = &rcv_priv->rq[rxq];
201 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
204 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
205 struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
207 u64_stats_update_begin(&stats->syncp);
208 stats->bytes += length;
210 u64_stats_update_end(&stats->syncp);
213 atomic64_inc(&priv->dropped);
218 __veth_xdp_flush(rq);
225 static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
227 struct veth_priv *priv = netdev_priv(dev);
232 for_each_possible_cpu(cpu) {
233 struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
238 start = u64_stats_fetch_begin_irq(&stats->syncp);
239 packets = stats->packets;
240 bytes = stats->bytes;
241 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
242 result->packets += packets;
243 result->bytes += bytes;
245 return atomic64_read(&priv->dropped);
248 static void veth_get_stats64(struct net_device *dev,
249 struct rtnl_link_stats64 *tot)
251 struct veth_priv *priv = netdev_priv(dev);
252 struct net_device *peer;
253 struct pcpu_vstats one;
255 tot->tx_dropped = veth_stats_one(&one, dev);
256 tot->tx_bytes = one.bytes;
257 tot->tx_packets = one.packets;
260 peer = rcu_dereference(priv->peer);
262 tot->rx_dropped = veth_stats_one(&one, peer);
263 tot->rx_bytes = one.bytes;
264 tot->rx_packets = one.packets;
269 /* fake multicast ability */
270 static void veth_set_multicast_list(struct net_device *dev)
274 static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
280 buflen = SKB_DATA_ALIGN(headroom + len) +
281 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
283 skb = build_skb(head, buflen);
287 skb_reserve(skb, headroom);
293 static int veth_select_rxq(struct net_device *dev)
295 return smp_processor_id() % dev->real_num_rx_queues;
298 static int veth_xdp_xmit(struct net_device *dev, int n,
299 struct xdp_frame **frames, u32 flags)
301 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
302 struct net_device *rcv;
303 unsigned int max_len;
307 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
310 rcv = rcu_dereference(priv->peer);
314 rcv_priv = netdev_priv(rcv);
315 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
316 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
317 * side. This means an XDP program is loaded on the peer and the peer
320 if (!rcu_access_pointer(rq->xdp_prog))
323 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
325 spin_lock(&rq->xdp_ring.producer_lock);
326 for (i = 0; i < n; i++) {
327 struct xdp_frame *frame = frames[i];
328 void *ptr = veth_xdp_to_ptr(frame);
330 if (unlikely(frame->len > max_len ||
331 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
332 xdp_return_frame_rx_napi(frame);
336 spin_unlock(&rq->xdp_ring.producer_lock);
338 if (flags & XDP_XMIT_FLUSH)
339 __veth_xdp_flush(rq);
344 static void veth_xdp_flush(struct net_device *dev)
346 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
347 struct net_device *rcv;
351 rcv = rcu_dereference(priv->peer);
355 rcv_priv = netdev_priv(rcv);
356 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
357 /* xdp_ring is initialized on receive side? */
358 if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
361 __veth_xdp_flush(rq);
366 static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
368 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
370 if (unlikely(!frame))
373 return veth_xdp_xmit(dev, 1, &frame, 0);
376 static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
377 struct xdp_frame *frame,
378 unsigned int *xdp_xmit)
380 void *hard_start = frame->data - frame->headroom;
381 int len = frame->len, delta = 0;
382 struct xdp_frame orig_frame;
383 struct bpf_prog *xdp_prog;
384 unsigned int headroom;
387 /* bpf_xdp_adjust_head() assures BPF cannot access xdp_frame area */
388 hard_start -= sizeof(struct xdp_frame);
391 xdp_prog = rcu_dereference(rq->xdp_prog);
392 if (likely(xdp_prog)) {
396 xdp.data_hard_start = hard_start;
397 xdp.data = frame->data;
398 xdp.data_end = frame->data + frame->len;
399 xdp.data_meta = frame->data - frame->metasize;
400 xdp.rxq = &rq->xdp_rxq;
402 act = bpf_prog_run_xdp(xdp_prog, &xdp);
406 delta = frame->data - xdp.data;
407 len = xdp.data_end - xdp.data;
411 xdp.rxq->mem = frame->mem;
412 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
413 trace_xdp_exception(rq->dev, xdp_prog, act);
417 *xdp_xmit |= VETH_XDP_TX;
422 xdp.rxq->mem = frame->mem;
423 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
427 *xdp_xmit |= VETH_XDP_REDIR;
431 bpf_warn_invalid_xdp_action(act);
433 trace_xdp_exception(rq->dev, xdp_prog, act);
440 headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
441 skb = veth_build_skb(hard_start, headroom, len, 0);
443 xdp_return_frame(frame);
447 xdp_scrub_frame(frame);
448 skb->protocol = eth_type_trans(skb, rq->dev);
453 xdp_return_frame(frame);
458 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb,
459 unsigned int *xdp_xmit)
461 u32 pktlen, headroom, act, metalen;
462 void *orig_data, *orig_data_end;
463 struct bpf_prog *xdp_prog;
464 int mac_len, delta, off;
470 xdp_prog = rcu_dereference(rq->xdp_prog);
471 if (unlikely(!xdp_prog)) {
476 mac_len = skb->data - skb_mac_header(skb);
477 pktlen = skb->len + mac_len;
478 headroom = skb_headroom(skb) - mac_len;
480 if (skb_shared(skb) || skb_head_is_locked(skb) ||
481 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
482 struct sk_buff *nskb;
487 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
488 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
489 if (size > PAGE_SIZE)
492 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
496 head = page_address(page);
497 start = head + VETH_XDP_HEADROOM;
498 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
499 page_frag_free(head);
503 nskb = veth_build_skb(head,
504 VETH_XDP_HEADROOM + mac_len, skb->len,
507 page_frag_free(head);
511 skb_copy_header(nskb, skb);
512 head_off = skb_headroom(nskb) - skb_headroom(skb);
513 skb_headers_offset_update(nskb, head_off);
518 xdp.data_hard_start = skb->head;
519 xdp.data = skb_mac_header(skb);
520 xdp.data_end = xdp.data + pktlen;
521 xdp.data_meta = xdp.data;
522 xdp.rxq = &rq->xdp_rxq;
523 orig_data = xdp.data;
524 orig_data_end = xdp.data_end;
526 act = bpf_prog_run_xdp(xdp_prog, &xdp);
532 get_page(virt_to_page(xdp.data));
534 xdp.rxq->mem = rq->xdp_mem;
535 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
536 trace_xdp_exception(rq->dev, xdp_prog, act);
539 *xdp_xmit |= VETH_XDP_TX;
543 get_page(virt_to_page(xdp.data));
545 xdp.rxq->mem = rq->xdp_mem;
546 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
548 *xdp_xmit |= VETH_XDP_REDIR;
552 bpf_warn_invalid_xdp_action(act);
554 trace_xdp_exception(rq->dev, xdp_prog, act);
560 delta = orig_data - xdp.data;
561 off = mac_len + delta;
563 __skb_push(skb, off);
565 __skb_pull(skb, -off);
566 skb->mac_header -= delta;
567 off = xdp.data_end - orig_data_end;
570 skb->protocol = eth_type_trans(skb, rq->dev);
572 metalen = xdp.data - xdp.data_meta;
574 skb_metadata_set(skb, metalen);
583 page_frag_free(xdp.data);
588 static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit)
592 for (i = 0; i < budget; i++) {
593 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
599 if (veth_is_xdp_frame(ptr)) {
600 skb = veth_xdp_rcv_one(rq, veth_ptr_to_xdp(ptr),
603 skb = veth_xdp_rcv_skb(rq, ptr, xdp_xmit);
607 napi_gro_receive(&rq->xdp_napi, skb);
615 static int veth_poll(struct napi_struct *napi, int budget)
618 container_of(napi, struct veth_rq, xdp_napi);
619 unsigned int xdp_xmit = 0;
622 xdp_set_return_frame_no_direct();
623 done = veth_xdp_rcv(rq, budget, &xdp_xmit);
625 if (done < budget && napi_complete_done(napi, done)) {
626 /* Write rx_notify_masked before reading ptr_ring */
627 smp_store_mb(rq->rx_notify_masked, false);
628 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
629 if (napi_schedule_prep(&rq->xdp_napi)) {
630 WRITE_ONCE(rq->rx_notify_masked, true);
631 __napi_schedule(&rq->xdp_napi);
636 if (xdp_xmit & VETH_XDP_TX)
637 veth_xdp_flush(rq->dev);
638 if (xdp_xmit & VETH_XDP_REDIR)
640 xdp_clear_return_frame_no_direct();
645 static int veth_napi_add(struct net_device *dev)
647 struct veth_priv *priv = netdev_priv(dev);
650 for (i = 0; i < dev->real_num_rx_queues; i++) {
651 struct veth_rq *rq = &priv->rq[i];
653 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
658 for (i = 0; i < dev->real_num_rx_queues; i++) {
659 struct veth_rq *rq = &priv->rq[i];
661 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
662 napi_enable(&rq->xdp_napi);
667 for (i--; i >= 0; i--)
668 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
673 static void veth_napi_del(struct net_device *dev)
675 struct veth_priv *priv = netdev_priv(dev);
678 for (i = 0; i < dev->real_num_rx_queues; i++) {
679 struct veth_rq *rq = &priv->rq[i];
681 napi_disable(&rq->xdp_napi);
682 napi_hash_del(&rq->xdp_napi);
686 for (i = 0; i < dev->real_num_rx_queues; i++) {
687 struct veth_rq *rq = &priv->rq[i];
689 netif_napi_del(&rq->xdp_napi);
690 rq->rx_notify_masked = false;
691 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
695 static int veth_enable_xdp(struct net_device *dev)
697 struct veth_priv *priv = netdev_priv(dev);
700 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
701 for (i = 0; i < dev->real_num_rx_queues; i++) {
702 struct veth_rq *rq = &priv->rq[i];
704 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
708 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
709 MEM_TYPE_PAGE_SHARED,
714 /* Save original mem info as it can be overwritten */
715 rq->xdp_mem = rq->xdp_rxq.mem;
718 err = veth_napi_add(dev);
723 for (i = 0; i < dev->real_num_rx_queues; i++)
724 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
728 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
730 for (i--; i >= 0; i--)
731 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
736 static void veth_disable_xdp(struct net_device *dev)
738 struct veth_priv *priv = netdev_priv(dev);
741 for (i = 0; i < dev->real_num_rx_queues; i++)
742 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
744 for (i = 0; i < dev->real_num_rx_queues; i++) {
745 struct veth_rq *rq = &priv->rq[i];
747 rq->xdp_rxq.mem = rq->xdp_mem;
748 xdp_rxq_info_unreg(&rq->xdp_rxq);
752 static int veth_open(struct net_device *dev)
754 struct veth_priv *priv = netdev_priv(dev);
755 struct net_device *peer = rtnl_dereference(priv->peer);
761 if (priv->_xdp_prog) {
762 err = veth_enable_xdp(dev);
767 if (peer->flags & IFF_UP) {
768 netif_carrier_on(dev);
769 netif_carrier_on(peer);
775 static int veth_close(struct net_device *dev)
777 struct veth_priv *priv = netdev_priv(dev);
778 struct net_device *peer = rtnl_dereference(priv->peer);
780 netif_carrier_off(dev);
782 netif_carrier_off(peer);
785 veth_disable_xdp(dev);
790 static int is_valid_veth_mtu(int mtu)
792 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
795 static int veth_alloc_queues(struct net_device *dev)
797 struct veth_priv *priv = netdev_priv(dev);
800 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
804 for (i = 0; i < dev->num_rx_queues; i++)
805 priv->rq[i].dev = dev;
810 static void veth_free_queues(struct net_device *dev)
812 struct veth_priv *priv = netdev_priv(dev);
817 static int veth_dev_init(struct net_device *dev)
821 dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
825 err = veth_alloc_queues(dev);
827 free_percpu(dev->vstats);
834 static void veth_dev_free(struct net_device *dev)
836 veth_free_queues(dev);
837 free_percpu(dev->vstats);
840 #ifdef CONFIG_NET_POLL_CONTROLLER
841 static void veth_poll_controller(struct net_device *dev)
843 /* veth only receives frames when its peer sends one
844 * Since it has nothing to do with disabling irqs, we are guaranteed
845 * never to have pending data when we poll for it so
846 * there is nothing to do here.
848 * We need this though so netpoll recognizes us as an interface that
849 * supports polling, which enables bridge devices in virt setups to
850 * still use netconsole
853 #endif /* CONFIG_NET_POLL_CONTROLLER */
855 static int veth_get_iflink(const struct net_device *dev)
857 struct veth_priv *priv = netdev_priv(dev);
858 struct net_device *peer;
862 peer = rcu_dereference(priv->peer);
863 iflink = peer ? peer->ifindex : 0;
869 static netdev_features_t veth_fix_features(struct net_device *dev,
870 netdev_features_t features)
872 struct veth_priv *priv = netdev_priv(dev);
873 struct net_device *peer;
875 peer = rtnl_dereference(priv->peer);
877 struct veth_priv *peer_priv = netdev_priv(peer);
879 if (peer_priv->_xdp_prog)
880 features &= ~NETIF_F_GSO_SOFTWARE;
886 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
888 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
889 struct net_device *peer;
895 peer = rcu_dereference(priv->peer);
899 peer_priv = netdev_priv(peer);
900 priv->requested_headroom = new_hr;
901 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
902 dev->needed_headroom = new_hr;
903 peer->needed_headroom = new_hr;
909 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
910 struct netlink_ext_ack *extack)
912 struct veth_priv *priv = netdev_priv(dev);
913 struct bpf_prog *old_prog;
914 struct net_device *peer;
915 unsigned int max_mtu;
918 old_prog = priv->_xdp_prog;
919 priv->_xdp_prog = prog;
920 peer = rtnl_dereference(priv->peer);
924 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
929 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
930 peer->hard_header_len -
931 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
932 if (peer->mtu > max_mtu) {
933 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
938 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
939 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
944 if (dev->flags & IFF_UP) {
945 err = veth_enable_xdp(dev);
947 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
953 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
954 peer->max_mtu = max_mtu;
960 if (dev->flags & IFF_UP)
961 veth_disable_xdp(dev);
964 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
965 peer->max_mtu = ETH_MAX_MTU;
968 bpf_prog_put(old_prog);
971 if ((!!old_prog ^ !!prog) && peer)
972 netdev_update_features(peer);
976 priv->_xdp_prog = old_prog;
981 static u32 veth_xdp_query(struct net_device *dev)
983 struct veth_priv *priv = netdev_priv(dev);
984 const struct bpf_prog *xdp_prog;
986 xdp_prog = priv->_xdp_prog;
988 return xdp_prog->aux->id;
993 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
995 switch (xdp->command) {
997 return veth_xdp_set(dev, xdp->prog, xdp->extack);
999 xdp->prog_id = veth_xdp_query(dev);
1006 static const struct net_device_ops veth_netdev_ops = {
1007 .ndo_init = veth_dev_init,
1008 .ndo_open = veth_open,
1009 .ndo_stop = veth_close,
1010 .ndo_start_xmit = veth_xmit,
1011 .ndo_get_stats64 = veth_get_stats64,
1012 .ndo_set_rx_mode = veth_set_multicast_list,
1013 .ndo_set_mac_address = eth_mac_addr,
1014 #ifdef CONFIG_NET_POLL_CONTROLLER
1015 .ndo_poll_controller = veth_poll_controller,
1017 .ndo_get_iflink = veth_get_iflink,
1018 .ndo_fix_features = veth_fix_features,
1019 .ndo_features_check = passthru_features_check,
1020 .ndo_set_rx_headroom = veth_set_rx_headroom,
1021 .ndo_bpf = veth_xdp,
1022 .ndo_xdp_xmit = veth_xdp_xmit,
1025 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1026 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1027 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1028 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1029 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1031 static void veth_setup(struct net_device *dev)
1035 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1036 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1037 dev->priv_flags |= IFF_NO_QUEUE;
1038 dev->priv_flags |= IFF_PHONY_HEADROOM;
1040 dev->netdev_ops = &veth_netdev_ops;
1041 dev->ethtool_ops = &veth_ethtool_ops;
1042 dev->features |= NETIF_F_LLTX;
1043 dev->features |= VETH_FEATURES;
1044 dev->vlan_features = dev->features &
1045 ~(NETIF_F_HW_VLAN_CTAG_TX |
1046 NETIF_F_HW_VLAN_STAG_TX |
1047 NETIF_F_HW_VLAN_CTAG_RX |
1048 NETIF_F_HW_VLAN_STAG_RX);
1049 dev->needs_free_netdev = true;
1050 dev->priv_destructor = veth_dev_free;
1051 dev->max_mtu = ETH_MAX_MTU;
1053 dev->hw_features = VETH_FEATURES;
1054 dev->hw_enc_features = VETH_FEATURES;
1055 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1062 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1063 struct netlink_ext_ack *extack)
1065 if (tb[IFLA_ADDRESS]) {
1066 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1068 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1069 return -EADDRNOTAVAIL;
1072 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1078 static struct rtnl_link_ops veth_link_ops;
1080 static int veth_newlink(struct net *src_net, struct net_device *dev,
1081 struct nlattr *tb[], struct nlattr *data[],
1082 struct netlink_ext_ack *extack)
1085 struct net_device *peer;
1086 struct veth_priv *priv;
1087 char ifname[IFNAMSIZ];
1088 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1089 unsigned char name_assign_type;
1090 struct ifinfomsg *ifmp;
1094 * create and register peer first
1096 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1097 struct nlattr *nla_peer;
1099 nla_peer = data[VETH_INFO_PEER];
1100 ifmp = nla_data(nla_peer);
1101 err = rtnl_nla_parse_ifla(peer_tb,
1102 nla_data(nla_peer) + sizeof(struct ifinfomsg),
1103 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1108 err = veth_validate(peer_tb, NULL, extack);
1118 if (ifmp && tbp[IFLA_IFNAME]) {
1119 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1120 name_assign_type = NET_NAME_USER;
1122 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1123 name_assign_type = NET_NAME_ENUM;
1126 net = rtnl_link_get_net(src_net, tbp);
1128 return PTR_ERR(net);
1130 peer = rtnl_create_link(net, ifname, name_assign_type,
1131 &veth_link_ops, tbp);
1134 return PTR_ERR(peer);
1137 if (!ifmp || !tbp[IFLA_ADDRESS])
1138 eth_hw_addr_random(peer);
1140 if (ifmp && (dev->ifindex != 0))
1141 peer->ifindex = ifmp->ifi_index;
1143 peer->gso_max_size = dev->gso_max_size;
1144 peer->gso_max_segs = dev->gso_max_segs;
1146 err = register_netdevice(peer);
1150 goto err_register_peer;
1152 netif_carrier_off(peer);
1154 err = rtnl_configure_link(peer, ifmp);
1156 goto err_configure_peer;
1161 * note, that since we've registered new device the dev's name
1162 * should be re-allocated
1165 if (tb[IFLA_ADDRESS] == NULL)
1166 eth_hw_addr_random(dev);
1168 if (tb[IFLA_IFNAME])
1169 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1171 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1173 err = register_netdevice(dev);
1175 goto err_register_dev;
1177 netif_carrier_off(dev);
1180 * tie the deviced together
1183 priv = netdev_priv(dev);
1184 rcu_assign_pointer(priv->peer, peer);
1186 priv = netdev_priv(peer);
1187 rcu_assign_pointer(priv->peer, dev);
1194 unregister_netdevice(peer);
1202 static void veth_dellink(struct net_device *dev, struct list_head *head)
1204 struct veth_priv *priv;
1205 struct net_device *peer;
1207 priv = netdev_priv(dev);
1208 peer = rtnl_dereference(priv->peer);
1210 /* Note : dellink() is called from default_device_exit_batch(),
1211 * before a rcu_synchronize() point. The devices are guaranteed
1212 * not being freed before one RCU grace period.
1214 RCU_INIT_POINTER(priv->peer, NULL);
1215 unregister_netdevice_queue(dev, head);
1218 priv = netdev_priv(peer);
1219 RCU_INIT_POINTER(priv->peer, NULL);
1220 unregister_netdevice_queue(peer, head);
1224 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1225 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1228 static struct net *veth_get_link_net(const struct net_device *dev)
1230 struct veth_priv *priv = netdev_priv(dev);
1231 struct net_device *peer = rtnl_dereference(priv->peer);
1233 return peer ? dev_net(peer) : dev_net(dev);
1236 static struct rtnl_link_ops veth_link_ops = {
1238 .priv_size = sizeof(struct veth_priv),
1239 .setup = veth_setup,
1240 .validate = veth_validate,
1241 .newlink = veth_newlink,
1242 .dellink = veth_dellink,
1243 .policy = veth_policy,
1244 .maxtype = VETH_INFO_MAX,
1245 .get_link_net = veth_get_link_net,
1252 static __init int veth_init(void)
1254 return rtnl_link_register(&veth_link_ops);
1257 static __exit void veth_exit(void)
1259 rtnl_link_unregister(&veth_link_ops);
1262 module_init(veth_init);
1263 module_exit(veth_exit);
1265 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1266 MODULE_LICENSE("GPL v2");
1267 MODULE_ALIAS_RTNL_LINK(DRV_NAME);