1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/atomic.h>
13 #include <linux/ethtool.h>
14 #include <linux/module.h>
15 #include <linux/highmem.h>
16 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/netdevice.h>
20 #include <linux/inetdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/pci.h>
23 #include <linux/skbuff.h>
24 #include <linux/if_vlan.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/netpoll.h>
29 #include <linux/bpf.h>
32 #include <net/route.h>
34 #include <net/pkt_sched.h>
35 #include <net/checksum.h>
36 #include <net/ip6_checksum.h>
38 #include "hyperv_net.h"
40 #define RING_SIZE_MIN 64
41 #define RETRY_US_LO 5000
42 #define RETRY_US_HI 10000
43 #define RETRY_MAX 2000 /* >10 sec */
45 #define LINKCHANGE_INT (2 * HZ)
46 #define VF_TAKEOVER_INT (HZ / 10)
48 static unsigned int ring_size __ro_after_init = 128;
49 module_param(ring_size, uint, 0444);
50 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
51 unsigned int netvsc_ring_bytes __ro_after_init;
53 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
54 NETIF_MSG_LINK | NETIF_MSG_IFUP |
55 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
58 static int debug = -1;
59 module_param(debug, int, 0444);
60 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
62 static LIST_HEAD(netvsc_dev_list);
64 static void netvsc_change_rx_flags(struct net_device *net, int change)
66 struct net_device_context *ndev_ctx = netdev_priv(net);
67 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
73 if (change & IFF_PROMISC) {
74 inc = (net->flags & IFF_PROMISC) ? 1 : -1;
75 dev_set_promiscuity(vf_netdev, inc);
78 if (change & IFF_ALLMULTI) {
79 inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
80 dev_set_allmulti(vf_netdev, inc);
84 static void netvsc_set_rx_mode(struct net_device *net)
86 struct net_device_context *ndev_ctx = netdev_priv(net);
87 struct net_device *vf_netdev;
88 struct netvsc_device *nvdev;
91 vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
93 dev_uc_sync(vf_netdev, net);
94 dev_mc_sync(vf_netdev, net);
97 nvdev = rcu_dereference(ndev_ctx->nvdev);
99 rndis_filter_update(nvdev);
103 static void netvsc_tx_enable(struct netvsc_device *nvscdev,
104 struct net_device *ndev)
106 nvscdev->tx_disable = false;
107 virt_wmb(); /* ensure queue wake up mechanism is on */
109 netif_tx_wake_all_queues(ndev);
112 static int netvsc_open(struct net_device *net)
114 struct net_device_context *ndev_ctx = netdev_priv(net);
115 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
116 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
117 struct rndis_device *rdev;
120 netif_carrier_off(net);
122 /* Open up the device */
123 ret = rndis_filter_open(nvdev);
125 netdev_err(net, "unable to open device (ret %d).\n", ret);
129 rdev = nvdev->extension;
130 if (!rdev->link_state) {
131 netif_carrier_on(net);
132 netvsc_tx_enable(nvdev, net);
136 /* Setting synthetic device up transparently sets
137 * slave as up. If open fails, then slave will be
138 * still be offline (and not used).
140 ret = dev_open(vf_netdev, NULL);
143 "unable to open slave: %s: %d\n",
144 vf_netdev->name, ret);
149 static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
151 unsigned int retry = 0;
154 /* Ensure pending bytes in ring are read */
158 for (i = 0; i < nvdev->num_chn; i++) {
159 struct vmbus_channel *chn
160 = nvdev->chan_table[i].channel;
165 /* make sure receive not running now */
166 napi_synchronize(&nvdev->chan_table[i].napi);
168 aread = hv_get_bytes_to_read(&chn->inbound);
172 aread = hv_get_bytes_to_read(&chn->outbound);
180 if (++retry > RETRY_MAX)
183 usleep_range(RETRY_US_LO, RETRY_US_HI);
187 static void netvsc_tx_disable(struct netvsc_device *nvscdev,
188 struct net_device *ndev)
191 nvscdev->tx_disable = true;
192 virt_wmb(); /* ensure txq will not wake up after stop */
195 netif_tx_disable(ndev);
198 static int netvsc_close(struct net_device *net)
200 struct net_device_context *net_device_ctx = netdev_priv(net);
201 struct net_device *vf_netdev
202 = rtnl_dereference(net_device_ctx->vf_netdev);
203 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
206 netvsc_tx_disable(nvdev, net);
208 /* No need to close rndis filter if it is removed already */
212 ret = rndis_filter_close(nvdev);
214 netdev_err(net, "unable to close device (ret %d).\n", ret);
218 ret = netvsc_wait_until_empty(nvdev);
220 netdev_err(net, "Ring buffer not empty after closing rndis\n");
223 dev_close(vf_netdev);
228 static inline void *init_ppi_data(struct rndis_message *msg,
229 u32 ppi_size, u32 pkt_type)
231 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
232 struct rndis_per_packet_info *ppi;
234 rndis_pkt->data_offset += ppi_size;
235 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
236 + rndis_pkt->per_pkt_info_len;
238 ppi->size = ppi_size;
239 ppi->type = pkt_type;
241 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
243 rndis_pkt->per_pkt_info_len += ppi_size;
248 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
249 * packets. We can use ethtool to change UDP hash level when necessary.
251 static inline u32 netvsc_get_hash(
253 const struct net_device_context *ndc)
255 struct flow_keys flow;
256 u32 hash, pkt_proto = 0;
257 static u32 hashrnd __read_mostly;
259 net_get_random_once(&hashrnd, sizeof(hashrnd));
261 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
264 switch (flow.basic.ip_proto) {
266 if (flow.basic.n_proto == htons(ETH_P_IP))
267 pkt_proto = HV_TCP4_L4HASH;
268 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
269 pkt_proto = HV_TCP6_L4HASH;
274 if (flow.basic.n_proto == htons(ETH_P_IP))
275 pkt_proto = HV_UDP4_L4HASH;
276 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
277 pkt_proto = HV_UDP6_L4HASH;
282 if (pkt_proto & ndc->l4_hash) {
283 return skb_get_hash(skb);
285 if (flow.basic.n_proto == htons(ETH_P_IP))
286 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
287 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
288 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
292 __skb_set_sw_hash(skb, hash, false);
298 static inline int netvsc_get_tx_queue(struct net_device *ndev,
299 struct sk_buff *skb, int old_idx)
301 const struct net_device_context *ndc = netdev_priv(ndev);
302 struct sock *sk = skb->sk;
305 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
306 (VRSS_SEND_TAB_SIZE - 1)];
308 /* If queue index changed record the new value */
309 if (q_idx != old_idx &&
310 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
311 sk_tx_queue_set(sk, q_idx);
317 * Select queue for transmit.
319 * If a valid queue has already been assigned, then use that.
320 * Otherwise compute tx queue based on hash and the send table.
322 * This is basically similar to default (netdev_pick_tx) with the added step
323 * of using the host send_table when no other queue has been assigned.
325 * TODO support XPS - but get_xps_queue not exported
327 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
329 int q_idx = sk_tx_queue_get(skb->sk);
331 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
332 /* If forwarding a packet, we use the recorded queue when
333 * available for better cache locality.
335 if (skb_rx_queue_recorded(skb))
336 q_idx = skb_get_rx_queue(skb);
338 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
344 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
345 struct net_device *sb_dev)
347 struct net_device_context *ndc = netdev_priv(ndev);
348 struct net_device *vf_netdev;
352 vf_netdev = rcu_dereference(ndc->vf_netdev);
354 const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
356 if (vf_ops->ndo_select_queue)
357 txq = vf_ops->ndo_select_queue(vf_netdev, skb, sb_dev);
359 txq = netdev_pick_tx(vf_netdev, skb, NULL);
361 /* Record the queue selected by VF so that it can be
362 * used for common case where VF has more queues than
363 * the synthetic device.
365 qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
367 txq = netvsc_pick_tx(ndev, skb);
371 while (txq >= ndev->real_num_tx_queues)
372 txq -= ndev->real_num_tx_queues;
377 static u32 fill_pg_buf(unsigned long hvpfn, u32 offset, u32 len,
378 struct hv_page_buffer *pb)
382 hvpfn += offset >> HV_HYP_PAGE_SHIFT;
383 offset = offset & ~HV_HYP_PAGE_MASK;
388 bytes = HV_HYP_PAGE_SIZE - offset;
392 pb[j].offset = offset;
398 if (offset == HV_HYP_PAGE_SIZE && len) {
408 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
409 struct hv_netvsc_packet *packet,
410 struct hv_page_buffer *pb)
413 char *data = skb->data;
414 int frags = skb_shinfo(skb)->nr_frags;
417 /* The packet is laid out thus:
418 * 1. hdr: RNDIS header and PPI
420 * 3. skb fragment data
422 slots_used += fill_pg_buf(virt_to_hvpfn(hdr),
423 offset_in_hvpage(hdr),
427 packet->rmsg_size = len;
428 packet->rmsg_pgcnt = slots_used;
430 slots_used += fill_pg_buf(virt_to_hvpfn(data),
431 offset_in_hvpage(data),
435 for (i = 0; i < frags; i++) {
436 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
438 slots_used += fill_pg_buf(page_to_hvpfn(skb_frag_page(frag)),
446 static int count_skb_frag_slots(struct sk_buff *skb)
448 int i, frags = skb_shinfo(skb)->nr_frags;
451 for (i = 0; i < frags; i++) {
452 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
453 unsigned long size = skb_frag_size(frag);
454 unsigned long offset = skb_frag_off(frag);
456 /* Skip unused frames from start of page */
457 offset &= ~HV_HYP_PAGE_MASK;
458 pages += HVPFN_UP(offset + size);
463 static int netvsc_get_slots(struct sk_buff *skb)
465 char *data = skb->data;
466 unsigned int offset = offset_in_hvpage(data);
467 unsigned int len = skb_headlen(skb);
471 slots = DIV_ROUND_UP(offset + len, HV_HYP_PAGE_SIZE);
472 frag_slots = count_skb_frag_slots(skb);
473 return slots + frag_slots;
476 static u32 net_checksum_info(struct sk_buff *skb)
478 if (skb->protocol == htons(ETH_P_IP)) {
479 struct iphdr *ip = ip_hdr(skb);
481 if (ip->protocol == IPPROTO_TCP)
482 return TRANSPORT_INFO_IPV4_TCP;
483 else if (ip->protocol == IPPROTO_UDP)
484 return TRANSPORT_INFO_IPV4_UDP;
486 struct ipv6hdr *ip6 = ipv6_hdr(skb);
488 if (ip6->nexthdr == IPPROTO_TCP)
489 return TRANSPORT_INFO_IPV6_TCP;
490 else if (ip6->nexthdr == IPPROTO_UDP)
491 return TRANSPORT_INFO_IPV6_UDP;
494 return TRANSPORT_INFO_NOT_IP;
497 /* Send skb on the slave VF device. */
498 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
501 struct net_device_context *ndev_ctx = netdev_priv(net);
502 unsigned int len = skb->len;
505 skb->dev = vf_netdev;
506 skb_record_rx_queue(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
508 rc = dev_queue_xmit(skb);
509 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
510 struct netvsc_vf_pcpu_stats *pcpu_stats
511 = this_cpu_ptr(ndev_ctx->vf_stats);
513 u64_stats_update_begin(&pcpu_stats->syncp);
514 pcpu_stats->tx_packets++;
515 pcpu_stats->tx_bytes += len;
516 u64_stats_update_end(&pcpu_stats->syncp);
518 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
524 static int netvsc_xmit(struct sk_buff *skb, struct net_device *net, bool xdp_tx)
526 struct net_device_context *net_device_ctx = netdev_priv(net);
527 struct hv_netvsc_packet *packet = NULL;
529 unsigned int num_data_pgs;
530 struct rndis_message *rndis_msg;
531 struct net_device *vf_netdev;
534 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
536 /* If VF is present and up then redirect packets to it.
537 * Skip the VF if it is marked down or has no carrier.
538 * If netpoll is in uses, then VF can not be used either.
540 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
541 if (vf_netdev && netif_running(vf_netdev) &&
542 netif_carrier_ok(vf_netdev) && !netpoll_tx_running(net) &&
543 net_device_ctx->data_path_is_vf)
544 return netvsc_vf_xmit(net, vf_netdev, skb);
546 /* We will atmost need two pages to describe the rndis
547 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
548 * of pages in a single packet. If skb is scattered around
549 * more pages we try linearizing it.
552 num_data_pgs = netvsc_get_slots(skb) + 2;
554 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
555 ++net_device_ctx->eth_stats.tx_scattered;
557 if (skb_linearize(skb))
560 num_data_pgs = netvsc_get_slots(skb) + 2;
561 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
562 ++net_device_ctx->eth_stats.tx_too_big;
568 * Place the rndis header in the skb head room and
569 * the skb->cb will be used for hv_netvsc_packet
572 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
576 /* Use the skb control buffer for building up the packet */
577 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
578 sizeof_field(struct sk_buff, cb));
579 packet = (struct hv_netvsc_packet *)skb->cb;
581 packet->q_idx = skb_get_queue_mapping(skb);
583 packet->total_data_buflen = skb->len;
584 packet->total_bytes = skb->len;
585 packet->total_packets = 1;
587 rndis_msg = (struct rndis_message *)skb->head;
589 /* Add the rndis header */
590 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
591 rndis_msg->msg_len = packet->total_data_buflen;
593 rndis_msg->msg.pkt = (struct rndis_packet) {
594 .data_offset = sizeof(struct rndis_packet),
595 .data_len = packet->total_data_buflen,
596 .per_pkt_info_offset = sizeof(struct rndis_packet),
599 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
601 hash = skb_get_hash_raw(skb);
602 if (hash != 0 && net->real_num_tx_queues > 1) {
605 rndis_msg_size += NDIS_HASH_PPI_SIZE;
606 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
611 /* When using AF_PACKET we need to drop VLAN header from
612 * the frame and update the SKB to allow the HOST OS
613 * to transmit the 802.1Q packet
615 if (skb->protocol == htons(ETH_P_8021Q)) {
618 skb_reset_mac_header(skb);
619 if (eth_type_vlan(eth_hdr(skb)->h_proto)) {
620 if (unlikely(__skb_vlan_pop(skb, &vlan_tci) != 0)) {
621 ++net_device_ctx->eth_stats.vlan_error;
625 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
626 /* Update the NDIS header pkt lengths */
627 packet->total_data_buflen -= VLAN_HLEN;
628 packet->total_bytes -= VLAN_HLEN;
629 rndis_msg->msg_len = packet->total_data_buflen;
630 rndis_msg->msg.pkt.data_len = packet->total_data_buflen;
634 if (skb_vlan_tag_present(skb)) {
635 struct ndis_pkt_8021q_info *vlan;
637 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
638 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
642 vlan->vlanid = skb_vlan_tag_get_id(skb);
643 vlan->cfi = skb_vlan_tag_get_cfi(skb);
644 vlan->pri = skb_vlan_tag_get_prio(skb);
647 if (skb_is_gso(skb)) {
648 struct ndis_tcp_lso_info *lso_info;
650 rndis_msg_size += NDIS_LSO_PPI_SIZE;
651 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
652 TCP_LARGESEND_PKTINFO);
655 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
656 if (skb->protocol == htons(ETH_P_IP)) {
657 lso_info->lso_v2_transmit.ip_version =
658 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
659 ip_hdr(skb)->tot_len = 0;
660 ip_hdr(skb)->check = 0;
661 tcp_hdr(skb)->check =
662 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
663 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
665 lso_info->lso_v2_transmit.ip_version =
666 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
667 tcp_v6_gso_csum_prep(skb);
669 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
670 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
671 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
672 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
673 struct ndis_tcp_ip_checksum_info *csum_info;
675 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
676 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
677 TCPIP_CHKSUM_PKTINFO);
679 csum_info->value = 0;
680 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
682 if (skb->protocol == htons(ETH_P_IP)) {
683 csum_info->transmit.is_ipv4 = 1;
685 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
686 csum_info->transmit.tcp_checksum = 1;
688 csum_info->transmit.udp_checksum = 1;
690 csum_info->transmit.is_ipv6 = 1;
692 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
693 csum_info->transmit.tcp_checksum = 1;
695 csum_info->transmit.udp_checksum = 1;
698 /* Can't do offload of this type of checksum */
699 if (skb_checksum_help(skb))
704 /* Start filling in the page buffers with the rndis hdr */
705 rndis_msg->msg_len += rndis_msg_size;
706 packet->total_data_buflen = rndis_msg->msg_len;
707 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
710 /* timestamp packet in software */
711 skb_tx_timestamp(skb);
713 ret = netvsc_send(net, packet, rndis_msg, pb, skb, xdp_tx);
714 if (likely(ret == 0))
717 if (ret == -EAGAIN) {
718 ++net_device_ctx->eth_stats.tx_busy;
719 return NETDEV_TX_BUSY;
723 ++net_device_ctx->eth_stats.tx_no_space;
726 dev_kfree_skb_any(skb);
727 net->stats.tx_dropped++;
732 ++net_device_ctx->eth_stats.tx_no_memory;
736 static netdev_tx_t netvsc_start_xmit(struct sk_buff *skb,
737 struct net_device *ndev)
739 return netvsc_xmit(skb, ndev, false);
743 * netvsc_linkstatus_callback - Link up/down notification
745 void netvsc_linkstatus_callback(struct net_device *net,
746 struct rndis_message *resp,
749 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
750 struct net_device_context *ndev_ctx = netdev_priv(net);
751 struct netvsc_reconfig *event;
754 /* Ensure the packet is big enough to access its fields */
755 if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_indicate_status)) {
756 netdev_err(net, "invalid rndis_indicate_status packet, len: %u\n",
761 /* Copy the RNDIS indicate status into nvchan->recv_buf */
762 memcpy(indicate, data + RNDIS_HEADER_SIZE, sizeof(*indicate));
764 /* Update the physical link speed when changing to another vSwitch */
765 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
768 /* Validate status_buf_offset */
769 if (indicate->status_buflen < sizeof(speed) ||
770 indicate->status_buf_offset < sizeof(*indicate) ||
771 resp->msg_len - RNDIS_HEADER_SIZE < indicate->status_buf_offset ||
772 resp->msg_len - RNDIS_HEADER_SIZE - indicate->status_buf_offset
773 < indicate->status_buflen) {
774 netdev_err(net, "invalid rndis_indicate_status packet\n");
778 speed = *(u32 *)(data + RNDIS_HEADER_SIZE + indicate->status_buf_offset) / 10000;
779 ndev_ctx->speed = speed;
783 /* Handle these link change statuses below */
784 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
785 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
786 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
789 if (net->reg_state != NETREG_REGISTERED)
792 event = kzalloc(sizeof(*event), GFP_ATOMIC);
795 event->event = indicate->status;
797 spin_lock_irqsave(&ndev_ctx->lock, flags);
798 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
799 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
801 schedule_delayed_work(&ndev_ctx->dwork, 0);
804 static void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev)
808 skb->queue_mapping = skb_get_rx_queue(skb);
809 __skb_push(skb, ETH_HLEN);
811 rc = netvsc_xmit(skb, ndev, true);
813 if (dev_xmit_complete(rc))
816 dev_kfree_skb_any(skb);
817 ndev->stats.tx_dropped++;
820 static void netvsc_comp_ipcsum(struct sk_buff *skb)
822 struct iphdr *iph = (struct iphdr *)skb->data;
825 iph->check = ip_fast_csum(iph, iph->ihl);
828 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
829 struct netvsc_channel *nvchan,
830 struct xdp_buff *xdp)
832 struct napi_struct *napi = &nvchan->napi;
833 const struct ndis_pkt_8021q_info *vlan = &nvchan->rsc.vlan;
834 const struct ndis_tcp_ip_checksum_info *csum_info =
835 &nvchan->rsc.csum_info;
836 const u32 *hash_info = &nvchan->rsc.hash_info;
837 u8 ppi_flags = nvchan->rsc.ppi_flags;
839 void *xbuf = xdp->data_hard_start;
843 unsigned int hdroom = xdp->data - xdp->data_hard_start;
844 unsigned int xlen = xdp->data_end - xdp->data;
845 unsigned int frag_size = xdp->frame_sz;
847 skb = build_skb(xbuf, frag_size);
850 __free_page(virt_to_page(xbuf));
854 skb_reserve(skb, hdroom);
856 skb->dev = napi->dev;
858 skb = napi_alloc_skb(napi, nvchan->rsc.pktlen);
863 /* Copy to skb. This copy is needed here since the memory
864 * pointed by hv_netvsc_packet cannot be deallocated.
866 for (i = 0; i < nvchan->rsc.cnt; i++)
867 skb_put_data(skb, nvchan->rsc.data[i],
871 skb->protocol = eth_type_trans(skb, net);
873 /* skb is already created with CHECKSUM_NONE */
874 skb_checksum_none_assert(skb);
876 /* Incoming packets may have IP header checksum verified by the host.
877 * They may not have IP header checksum computed after coalescing.
878 * We compute it here if the flags are set, because on Linux, the IP
879 * checksum is always checked.
881 if ((ppi_flags & NVSC_RSC_CSUM_INFO) && csum_info->receive.ip_checksum_value_invalid &&
882 csum_info->receive.ip_checksum_succeeded &&
883 skb->protocol == htons(ETH_P_IP)) {
884 /* Check that there is enough space to hold the IP header. */
885 if (skb_headlen(skb) < sizeof(struct iphdr)) {
889 netvsc_comp_ipcsum(skb);
892 /* Do L4 checksum offload if enabled and present. */
893 if ((ppi_flags & NVSC_RSC_CSUM_INFO) && (net->features & NETIF_F_RXCSUM)) {
894 if (csum_info->receive.tcp_checksum_succeeded ||
895 csum_info->receive.udp_checksum_succeeded)
896 skb->ip_summed = CHECKSUM_UNNECESSARY;
899 if ((ppi_flags & NVSC_RSC_HASH_INFO) && (net->features & NETIF_F_RXHASH))
900 skb_set_hash(skb, *hash_info, PKT_HASH_TYPE_L4);
902 if (ppi_flags & NVSC_RSC_VLAN) {
903 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT) |
904 (vlan->cfi ? VLAN_CFI_MASK : 0);
906 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
914 * netvsc_recv_callback - Callback when we receive a packet from the
915 * "wire" on the specified device.
917 int netvsc_recv_callback(struct net_device *net,
918 struct netvsc_device *net_device,
919 struct netvsc_channel *nvchan)
921 struct net_device_context *net_device_ctx = netdev_priv(net);
922 struct vmbus_channel *channel = nvchan->channel;
923 u16 q_idx = channel->offermsg.offer.sub_channel_index;
925 struct netvsc_stats *rx_stats = &nvchan->rx_stats;
929 if (net->reg_state != NETREG_REGISTERED)
930 return NVSP_STAT_FAIL;
932 act = netvsc_run_xdp(net, nvchan, &xdp);
934 if (act != XDP_PASS && act != XDP_TX) {
935 u64_stats_update_begin(&rx_stats->syncp);
936 rx_stats->xdp_drop++;
937 u64_stats_update_end(&rx_stats->syncp);
939 return NVSP_STAT_SUCCESS; /* consumed by XDP */
942 /* Allocate a skb - TODO direct I/O to pages? */
943 skb = netvsc_alloc_recv_skb(net, nvchan, &xdp);
945 if (unlikely(!skb)) {
946 ++net_device_ctx->eth_stats.rx_no_memory;
947 return NVSP_STAT_FAIL;
950 skb_record_rx_queue(skb, q_idx);
953 * Even if injecting the packet, record the statistics
954 * on the synthetic device because modifying the VF device
955 * statistics will not work correctly.
957 u64_stats_update_begin(&rx_stats->syncp);
959 rx_stats->bytes += nvchan->rsc.pktlen;
961 if (skb->pkt_type == PACKET_BROADCAST)
962 ++rx_stats->broadcast;
963 else if (skb->pkt_type == PACKET_MULTICAST)
964 ++rx_stats->multicast;
965 u64_stats_update_end(&rx_stats->syncp);
968 netvsc_xdp_xmit(skb, net);
969 return NVSP_STAT_SUCCESS;
972 napi_gro_receive(&nvchan->napi, skb);
973 return NVSP_STAT_SUCCESS;
976 static void netvsc_get_drvinfo(struct net_device *net,
977 struct ethtool_drvinfo *info)
979 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
980 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
983 static void netvsc_get_channels(struct net_device *net,
984 struct ethtool_channels *channel)
986 struct net_device_context *net_device_ctx = netdev_priv(net);
987 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
990 channel->max_combined = nvdev->max_chn;
991 channel->combined_count = nvdev->num_chn;
995 /* Alloc struct netvsc_device_info, and initialize it from either existing
996 * struct netvsc_device, or from default values.
999 struct netvsc_device_info *netvsc_devinfo_get(struct netvsc_device *nvdev)
1001 struct netvsc_device_info *dev_info;
1002 struct bpf_prog *prog;
1004 dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
1012 dev_info->num_chn = nvdev->num_chn;
1013 dev_info->send_sections = nvdev->send_section_cnt;
1014 dev_info->send_section_size = nvdev->send_section_size;
1015 dev_info->recv_sections = nvdev->recv_section_cnt;
1016 dev_info->recv_section_size = nvdev->recv_section_size;
1018 memcpy(dev_info->rss_key, nvdev->extension->rss_key,
1019 NETVSC_HASH_KEYLEN);
1021 prog = netvsc_xdp_get(nvdev);
1024 dev_info->bprog = prog;
1027 dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
1028 dev_info->send_sections = NETVSC_DEFAULT_TX;
1029 dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
1030 dev_info->recv_sections = NETVSC_DEFAULT_RX;
1031 dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
1037 /* Free struct netvsc_device_info */
1038 static void netvsc_devinfo_put(struct netvsc_device_info *dev_info)
1040 if (dev_info->bprog) {
1042 bpf_prog_put(dev_info->bprog);
1048 static int netvsc_detach(struct net_device *ndev,
1049 struct netvsc_device *nvdev)
1051 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1052 struct hv_device *hdev = ndev_ctx->device_ctx;
1055 /* Don't try continuing to try and setup sub channels */
1056 if (cancel_work_sync(&nvdev->subchan_work))
1059 netvsc_xdp_set(ndev, NULL, NULL, nvdev);
1061 /* If device was up (receiving) then shutdown */
1062 if (netif_running(ndev)) {
1063 netvsc_tx_disable(nvdev, ndev);
1065 ret = rndis_filter_close(nvdev);
1068 "unable to close device (ret %d).\n", ret);
1072 ret = netvsc_wait_until_empty(nvdev);
1075 "Ring buffer not empty after closing rndis\n");
1080 netif_device_detach(ndev);
1082 rndis_filter_device_remove(hdev, nvdev);
1087 static int netvsc_attach(struct net_device *ndev,
1088 struct netvsc_device_info *dev_info)
1090 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1091 struct hv_device *hdev = ndev_ctx->device_ctx;
1092 struct netvsc_device *nvdev;
1093 struct rndis_device *rdev;
1094 struct bpf_prog *prog;
1097 nvdev = rndis_filter_device_add(hdev, dev_info);
1099 return PTR_ERR(nvdev);
1101 if (nvdev->num_chn > 1) {
1102 ret = rndis_set_subchannel(ndev, nvdev, dev_info);
1104 /* if unavailable, just proceed with one queue */
1111 prog = dev_info->bprog;
1114 ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
1121 /* In any case device is now ready */
1122 nvdev->tx_disable = false;
1123 netif_device_attach(ndev);
1125 /* Note: enable and attach happen when sub-channels setup */
1126 netif_carrier_off(ndev);
1128 if (netif_running(ndev)) {
1129 ret = rndis_filter_open(nvdev);
1133 rdev = nvdev->extension;
1134 if (!rdev->link_state)
1135 netif_carrier_on(ndev);
1141 netif_device_detach(ndev);
1144 rndis_filter_device_remove(hdev, nvdev);
1149 static int netvsc_set_channels(struct net_device *net,
1150 struct ethtool_channels *channels)
1152 struct net_device_context *net_device_ctx = netdev_priv(net);
1153 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
1154 unsigned int orig, count = channels->combined_count;
1155 struct netvsc_device_info *device_info;
1158 /* We do not support separate count for rx, tx, or other */
1160 channels->rx_count || channels->tx_count || channels->other_count)
1163 if (!nvdev || nvdev->destroy)
1166 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1169 if (count > nvdev->max_chn)
1172 orig = nvdev->num_chn;
1174 device_info = netvsc_devinfo_get(nvdev);
1179 device_info->num_chn = count;
1181 ret = netvsc_detach(net, nvdev);
1185 ret = netvsc_attach(net, device_info);
1187 device_info->num_chn = orig;
1188 if (netvsc_attach(net, device_info))
1189 netdev_err(net, "restoring channel setting failed\n");
1193 netvsc_devinfo_put(device_info);
1197 static void netvsc_init_settings(struct net_device *dev)
1199 struct net_device_context *ndc = netdev_priv(dev);
1201 ndc->l4_hash = HV_DEFAULT_L4HASH;
1203 ndc->speed = SPEED_UNKNOWN;
1204 ndc->duplex = DUPLEX_FULL;
1206 dev->features = NETIF_F_LRO;
1209 static int netvsc_get_link_ksettings(struct net_device *dev,
1210 struct ethtool_link_ksettings *cmd)
1212 struct net_device_context *ndc = netdev_priv(dev);
1213 struct net_device *vf_netdev;
1215 vf_netdev = rtnl_dereference(ndc->vf_netdev);
1218 return __ethtool_get_link_ksettings(vf_netdev, cmd);
1220 cmd->base.speed = ndc->speed;
1221 cmd->base.duplex = ndc->duplex;
1222 cmd->base.port = PORT_OTHER;
1227 static int netvsc_set_link_ksettings(struct net_device *dev,
1228 const struct ethtool_link_ksettings *cmd)
1230 struct net_device_context *ndc = netdev_priv(dev);
1231 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1234 if (!vf_netdev->ethtool_ops->set_link_ksettings)
1237 return vf_netdev->ethtool_ops->set_link_ksettings(vf_netdev,
1241 return ethtool_virtdev_set_link_ksettings(dev, cmd,
1242 &ndc->speed, &ndc->duplex);
1245 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
1247 struct net_device_context *ndevctx = netdev_priv(ndev);
1248 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1249 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1250 int orig_mtu = ndev->mtu;
1251 struct netvsc_device_info *device_info;
1254 if (!nvdev || nvdev->destroy)
1257 device_info = netvsc_devinfo_get(nvdev);
1262 /* Change MTU of underlying VF netdev first. */
1264 ret = dev_set_mtu(vf_netdev, mtu);
1269 ret = netvsc_detach(ndev, nvdev);
1275 ret = netvsc_attach(ndev, device_info);
1279 /* Attempt rollback to original MTU */
1280 ndev->mtu = orig_mtu;
1282 if (netvsc_attach(ndev, device_info))
1283 netdev_err(ndev, "restoring mtu failed\n");
1286 dev_set_mtu(vf_netdev, orig_mtu);
1289 netvsc_devinfo_put(device_info);
1293 static void netvsc_get_vf_stats(struct net_device *net,
1294 struct netvsc_vf_pcpu_stats *tot)
1296 struct net_device_context *ndev_ctx = netdev_priv(net);
1299 memset(tot, 0, sizeof(*tot));
1301 for_each_possible_cpu(i) {
1302 const struct netvsc_vf_pcpu_stats *stats
1303 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1304 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1308 start = u64_stats_fetch_begin_irq(&stats->syncp);
1309 rx_packets = stats->rx_packets;
1310 tx_packets = stats->tx_packets;
1311 rx_bytes = stats->rx_bytes;
1312 tx_bytes = stats->tx_bytes;
1313 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1315 tot->rx_packets += rx_packets;
1316 tot->tx_packets += tx_packets;
1317 tot->rx_bytes += rx_bytes;
1318 tot->tx_bytes += tx_bytes;
1319 tot->tx_dropped += stats->tx_dropped;
1323 static void netvsc_get_pcpu_stats(struct net_device *net,
1324 struct netvsc_ethtool_pcpu_stats *pcpu_tot)
1326 struct net_device_context *ndev_ctx = netdev_priv(net);
1327 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1330 /* fetch percpu stats of vf */
1331 for_each_possible_cpu(i) {
1332 const struct netvsc_vf_pcpu_stats *stats =
1333 per_cpu_ptr(ndev_ctx->vf_stats, i);
1334 struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
1338 start = u64_stats_fetch_begin_irq(&stats->syncp);
1339 this_tot->vf_rx_packets = stats->rx_packets;
1340 this_tot->vf_tx_packets = stats->tx_packets;
1341 this_tot->vf_rx_bytes = stats->rx_bytes;
1342 this_tot->vf_tx_bytes = stats->tx_bytes;
1343 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1344 this_tot->rx_packets = this_tot->vf_rx_packets;
1345 this_tot->tx_packets = this_tot->vf_tx_packets;
1346 this_tot->rx_bytes = this_tot->vf_rx_bytes;
1347 this_tot->tx_bytes = this_tot->vf_tx_bytes;
1350 /* fetch percpu stats of netvsc */
1351 for (i = 0; i < nvdev->num_chn; i++) {
1352 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1353 const struct netvsc_stats *stats;
1354 struct netvsc_ethtool_pcpu_stats *this_tot =
1355 &pcpu_tot[nvchan->channel->target_cpu];
1359 stats = &nvchan->tx_stats;
1361 start = u64_stats_fetch_begin_irq(&stats->syncp);
1362 packets = stats->packets;
1363 bytes = stats->bytes;
1364 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1366 this_tot->tx_bytes += bytes;
1367 this_tot->tx_packets += packets;
1369 stats = &nvchan->rx_stats;
1371 start = u64_stats_fetch_begin_irq(&stats->syncp);
1372 packets = stats->packets;
1373 bytes = stats->bytes;
1374 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1376 this_tot->rx_bytes += bytes;
1377 this_tot->rx_packets += packets;
1381 static void netvsc_get_stats64(struct net_device *net,
1382 struct rtnl_link_stats64 *t)
1384 struct net_device_context *ndev_ctx = netdev_priv(net);
1385 struct netvsc_device *nvdev;
1386 struct netvsc_vf_pcpu_stats vf_tot;
1391 nvdev = rcu_dereference(ndev_ctx->nvdev);
1395 netdev_stats_to_stats64(t, &net->stats);
1397 netvsc_get_vf_stats(net, &vf_tot);
1398 t->rx_packets += vf_tot.rx_packets;
1399 t->tx_packets += vf_tot.tx_packets;
1400 t->rx_bytes += vf_tot.rx_bytes;
1401 t->tx_bytes += vf_tot.tx_bytes;
1402 t->tx_dropped += vf_tot.tx_dropped;
1404 for (i = 0; i < nvdev->num_chn; i++) {
1405 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1406 const struct netvsc_stats *stats;
1407 u64 packets, bytes, multicast;
1410 stats = &nvchan->tx_stats;
1412 start = u64_stats_fetch_begin_irq(&stats->syncp);
1413 packets = stats->packets;
1414 bytes = stats->bytes;
1415 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1417 t->tx_bytes += bytes;
1418 t->tx_packets += packets;
1420 stats = &nvchan->rx_stats;
1422 start = u64_stats_fetch_begin_irq(&stats->syncp);
1423 packets = stats->packets;
1424 bytes = stats->bytes;
1425 multicast = stats->multicast + stats->broadcast;
1426 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1428 t->rx_bytes += bytes;
1429 t->rx_packets += packets;
1430 t->multicast += multicast;
1436 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1438 struct net_device_context *ndc = netdev_priv(ndev);
1439 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1440 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1441 struct sockaddr *addr = p;
1444 err = eth_prepare_mac_addr_change(ndev, p);
1452 err = dev_set_mac_address(vf_netdev, addr, NULL);
1457 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1459 eth_commit_mac_addr_change(ndev, p);
1460 } else if (vf_netdev) {
1461 /* rollback change on VF */
1462 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1463 dev_set_mac_address(vf_netdev, addr, NULL);
1469 static const struct {
1470 char name[ETH_GSTRING_LEN];
1472 } netvsc_stats[] = {
1473 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1474 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1475 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1476 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1477 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1478 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1479 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1480 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1481 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1482 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1483 { "vlan_error", offsetof(struct netvsc_ethtool_stats, vlan_error) },
1485 { "cpu%u_rx_packets",
1486 offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
1488 offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
1489 { "cpu%u_tx_packets",
1490 offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
1492 offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
1493 { "cpu%u_vf_rx_packets",
1494 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
1495 { "cpu%u_vf_rx_bytes",
1496 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
1497 { "cpu%u_vf_tx_packets",
1498 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
1499 { "cpu%u_vf_tx_bytes",
1500 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
1502 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1503 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1504 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1505 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1506 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1509 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1510 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1512 /* statistics per queue (rx/tx packets/bytes) */
1513 #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
1515 /* 5 statistics per queue (rx/tx packets/bytes, rx xdp_drop) */
1516 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 5)
1518 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1520 struct net_device_context *ndc = netdev_priv(dev);
1521 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1526 switch (string_set) {
1528 return NETVSC_GLOBAL_STATS_LEN
1529 + NETVSC_VF_STATS_LEN
1530 + NETVSC_QUEUE_STATS_LEN(nvdev)
1531 + NETVSC_PCPU_STATS_LEN;
1537 static void netvsc_get_ethtool_stats(struct net_device *dev,
1538 struct ethtool_stats *stats, u64 *data)
1540 struct net_device_context *ndc = netdev_priv(dev);
1541 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1542 const void *nds = &ndc->eth_stats;
1543 const struct netvsc_stats *qstats;
1544 struct netvsc_vf_pcpu_stats sum;
1545 struct netvsc_ethtool_pcpu_stats *pcpu_sum;
1554 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1555 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1557 netvsc_get_vf_stats(dev, &sum);
1558 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1559 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1561 for (j = 0; j < nvdev->num_chn; j++) {
1562 qstats = &nvdev->chan_table[j].tx_stats;
1565 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1566 packets = qstats->packets;
1567 bytes = qstats->bytes;
1568 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1569 data[i++] = packets;
1572 qstats = &nvdev->chan_table[j].rx_stats;
1574 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1575 packets = qstats->packets;
1576 bytes = qstats->bytes;
1577 xdp_drop = qstats->xdp_drop;
1578 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1579 data[i++] = packets;
1581 data[i++] = xdp_drop;
1584 pcpu_sum = kvmalloc_array(num_possible_cpus(),
1585 sizeof(struct netvsc_ethtool_pcpu_stats),
1587 netvsc_get_pcpu_stats(dev, pcpu_sum);
1588 for_each_present_cpu(cpu) {
1589 struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
1591 for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
1592 data[i++] = *(u64 *)((void *)this_sum
1593 + pcpu_stats[j].offset);
1598 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1600 struct net_device_context *ndc = netdev_priv(dev);
1601 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1608 switch (stringset) {
1610 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1611 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1612 p += ETH_GSTRING_LEN;
1615 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1616 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1617 p += ETH_GSTRING_LEN;
1620 for (i = 0; i < nvdev->num_chn; i++) {
1621 sprintf(p, "tx_queue_%u_packets", i);
1622 p += ETH_GSTRING_LEN;
1623 sprintf(p, "tx_queue_%u_bytes", i);
1624 p += ETH_GSTRING_LEN;
1625 sprintf(p, "rx_queue_%u_packets", i);
1626 p += ETH_GSTRING_LEN;
1627 sprintf(p, "rx_queue_%u_bytes", i);
1628 p += ETH_GSTRING_LEN;
1629 sprintf(p, "rx_queue_%u_xdp_drop", i);
1630 p += ETH_GSTRING_LEN;
1633 for_each_present_cpu(cpu) {
1634 for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++) {
1635 sprintf(p, pcpu_stats[i].name, cpu);
1636 p += ETH_GSTRING_LEN;
1645 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1646 struct ethtool_rxnfc *info)
1648 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1650 info->data = RXH_IP_SRC | RXH_IP_DST;
1652 switch (info->flow_type) {
1654 if (ndc->l4_hash & HV_TCP4_L4HASH)
1655 info->data |= l4_flag;
1660 if (ndc->l4_hash & HV_TCP6_L4HASH)
1661 info->data |= l4_flag;
1666 if (ndc->l4_hash & HV_UDP4_L4HASH)
1667 info->data |= l4_flag;
1672 if (ndc->l4_hash & HV_UDP6_L4HASH)
1673 info->data |= l4_flag;
1689 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1692 struct net_device_context *ndc = netdev_priv(dev);
1693 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1698 switch (info->cmd) {
1699 case ETHTOOL_GRXRINGS:
1700 info->data = nvdev->num_chn;
1704 return netvsc_get_rss_hash_opts(ndc, info);
1709 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1710 struct ethtool_rxnfc *info)
1712 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1713 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1714 switch (info->flow_type) {
1716 ndc->l4_hash |= HV_TCP4_L4HASH;
1720 ndc->l4_hash |= HV_TCP6_L4HASH;
1724 ndc->l4_hash |= HV_UDP4_L4HASH;
1728 ndc->l4_hash |= HV_UDP6_L4HASH;
1738 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1739 switch (info->flow_type) {
1741 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1745 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1749 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1753 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1767 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1769 struct net_device_context *ndc = netdev_priv(ndev);
1771 if (info->cmd == ETHTOOL_SRXFH)
1772 return netvsc_set_rss_hash_opts(ndc, info);
1777 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1779 return NETVSC_HASH_KEYLEN;
1782 static u32 netvsc_rss_indir_size(struct net_device *dev)
1787 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1790 struct net_device_context *ndc = netdev_priv(dev);
1791 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1792 struct rndis_device *rndis_dev;
1799 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1801 rndis_dev = ndev->extension;
1803 for (i = 0; i < ITAB_NUM; i++)
1804 indir[i] = ndc->rx_table[i];
1808 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1813 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1814 const u8 *key, const u8 hfunc)
1816 struct net_device_context *ndc = netdev_priv(dev);
1817 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1818 struct rndis_device *rndis_dev;
1824 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1827 rndis_dev = ndev->extension;
1829 for (i = 0; i < ITAB_NUM; i++)
1830 if (indir[i] >= ndev->num_chn)
1833 for (i = 0; i < ITAB_NUM; i++)
1834 ndc->rx_table[i] = indir[i];
1841 key = rndis_dev->rss_key;
1844 return rndis_filter_set_rss_param(rndis_dev, key);
1847 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1848 * It does have pre-allocated receive area which is divided into sections.
1850 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1851 struct ethtool_ringparam *ring)
1855 ring->rx_pending = nvdev->recv_section_cnt;
1856 ring->tx_pending = nvdev->send_section_cnt;
1858 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1859 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1861 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1863 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1864 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1865 / nvdev->send_section_size;
1868 static void netvsc_get_ringparam(struct net_device *ndev,
1869 struct ethtool_ringparam *ring)
1871 struct net_device_context *ndevctx = netdev_priv(ndev);
1872 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1877 __netvsc_get_ringparam(nvdev, ring);
1880 static int netvsc_set_ringparam(struct net_device *ndev,
1881 struct ethtool_ringparam *ring)
1883 struct net_device_context *ndevctx = netdev_priv(ndev);
1884 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1885 struct netvsc_device_info *device_info;
1886 struct ethtool_ringparam orig;
1890 if (!nvdev || nvdev->destroy)
1893 memset(&orig, 0, sizeof(orig));
1894 __netvsc_get_ringparam(nvdev, &orig);
1896 new_tx = clamp_t(u32, ring->tx_pending,
1897 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1898 new_rx = clamp_t(u32, ring->rx_pending,
1899 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1901 if (new_tx == orig.tx_pending &&
1902 new_rx == orig.rx_pending)
1903 return 0; /* no change */
1905 device_info = netvsc_devinfo_get(nvdev);
1910 device_info->send_sections = new_tx;
1911 device_info->recv_sections = new_rx;
1913 ret = netvsc_detach(ndev, nvdev);
1917 ret = netvsc_attach(ndev, device_info);
1919 device_info->send_sections = orig.tx_pending;
1920 device_info->recv_sections = orig.rx_pending;
1922 if (netvsc_attach(ndev, device_info))
1923 netdev_err(ndev, "restoring ringparam failed");
1927 netvsc_devinfo_put(device_info);
1931 static netdev_features_t netvsc_fix_features(struct net_device *ndev,
1932 netdev_features_t features)
1934 struct net_device_context *ndevctx = netdev_priv(ndev);
1935 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1937 if (!nvdev || nvdev->destroy)
1940 if ((features & NETIF_F_LRO) && netvsc_xdp_get(nvdev)) {
1941 features ^= NETIF_F_LRO;
1942 netdev_info(ndev, "Skip LRO - unsupported with XDP\n");
1948 static int netvsc_set_features(struct net_device *ndev,
1949 netdev_features_t features)
1951 netdev_features_t change = features ^ ndev->features;
1952 struct net_device_context *ndevctx = netdev_priv(ndev);
1953 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1954 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1955 struct ndis_offload_params offloads;
1958 if (!nvdev || nvdev->destroy)
1961 if (!(change & NETIF_F_LRO))
1964 memset(&offloads, 0, sizeof(struct ndis_offload_params));
1966 if (features & NETIF_F_LRO) {
1967 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1968 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1970 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1971 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1974 ret = rndis_filter_set_offload_params(ndev, nvdev, &offloads);
1977 features ^= NETIF_F_LRO;
1978 ndev->features = features;
1985 vf_netdev->wanted_features = features;
1986 netdev_update_features(vf_netdev);
1991 static int netvsc_get_regs_len(struct net_device *netdev)
1993 return VRSS_SEND_TAB_SIZE * sizeof(u32);
1996 static void netvsc_get_regs(struct net_device *netdev,
1997 struct ethtool_regs *regs, void *p)
1999 struct net_device_context *ndc = netdev_priv(netdev);
2002 /* increase the version, if buffer format is changed. */
2005 memcpy(regs_buff, ndc->tx_table, VRSS_SEND_TAB_SIZE * sizeof(u32));
2008 static u32 netvsc_get_msglevel(struct net_device *ndev)
2010 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2012 return ndev_ctx->msg_enable;
2015 static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
2017 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2019 ndev_ctx->msg_enable = val;
2022 static const struct ethtool_ops ethtool_ops = {
2023 .get_drvinfo = netvsc_get_drvinfo,
2024 .get_regs_len = netvsc_get_regs_len,
2025 .get_regs = netvsc_get_regs,
2026 .get_msglevel = netvsc_get_msglevel,
2027 .set_msglevel = netvsc_set_msglevel,
2028 .get_link = ethtool_op_get_link,
2029 .get_ethtool_stats = netvsc_get_ethtool_stats,
2030 .get_sset_count = netvsc_get_sset_count,
2031 .get_strings = netvsc_get_strings,
2032 .get_channels = netvsc_get_channels,
2033 .set_channels = netvsc_set_channels,
2034 .get_ts_info = ethtool_op_get_ts_info,
2035 .get_rxnfc = netvsc_get_rxnfc,
2036 .set_rxnfc = netvsc_set_rxnfc,
2037 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
2038 .get_rxfh_indir_size = netvsc_rss_indir_size,
2039 .get_rxfh = netvsc_get_rxfh,
2040 .set_rxfh = netvsc_set_rxfh,
2041 .get_link_ksettings = netvsc_get_link_ksettings,
2042 .set_link_ksettings = netvsc_set_link_ksettings,
2043 .get_ringparam = netvsc_get_ringparam,
2044 .set_ringparam = netvsc_set_ringparam,
2047 static const struct net_device_ops device_ops = {
2048 .ndo_open = netvsc_open,
2049 .ndo_stop = netvsc_close,
2050 .ndo_start_xmit = netvsc_start_xmit,
2051 .ndo_change_rx_flags = netvsc_change_rx_flags,
2052 .ndo_set_rx_mode = netvsc_set_rx_mode,
2053 .ndo_fix_features = netvsc_fix_features,
2054 .ndo_set_features = netvsc_set_features,
2055 .ndo_change_mtu = netvsc_change_mtu,
2056 .ndo_validate_addr = eth_validate_addr,
2057 .ndo_set_mac_address = netvsc_set_mac_addr,
2058 .ndo_select_queue = netvsc_select_queue,
2059 .ndo_get_stats64 = netvsc_get_stats64,
2060 .ndo_bpf = netvsc_bpf,
2064 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
2065 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
2066 * present send GARP packet to network peers with netif_notify_peers().
2068 static void netvsc_link_change(struct work_struct *w)
2070 struct net_device_context *ndev_ctx =
2071 container_of(w, struct net_device_context, dwork.work);
2072 struct hv_device *device_obj = ndev_ctx->device_ctx;
2073 struct net_device *net = hv_get_drvdata(device_obj);
2074 unsigned long flags, next_reconfig, delay;
2075 struct netvsc_reconfig *event = NULL;
2076 struct netvsc_device *net_device;
2077 struct rndis_device *rdev;
2078 bool reschedule = false;
2080 /* if changes are happening, comeback later */
2081 if (!rtnl_trylock()) {
2082 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2086 net_device = rtnl_dereference(ndev_ctx->nvdev);
2090 rdev = net_device->extension;
2092 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
2093 if (time_is_after_jiffies(next_reconfig)) {
2094 /* link_watch only sends one notification with current state
2095 * per second, avoid doing reconfig more frequently. Handle
2098 delay = next_reconfig - jiffies;
2099 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
2100 schedule_delayed_work(&ndev_ctx->dwork, delay);
2103 ndev_ctx->last_reconfig = jiffies;
2105 spin_lock_irqsave(&ndev_ctx->lock, flags);
2106 if (!list_empty(&ndev_ctx->reconfig_events)) {
2107 event = list_first_entry(&ndev_ctx->reconfig_events,
2108 struct netvsc_reconfig, list);
2109 list_del(&event->list);
2110 reschedule = !list_empty(&ndev_ctx->reconfig_events);
2112 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2117 switch (event->event) {
2118 /* Only the following events are possible due to the check in
2119 * netvsc_linkstatus_callback()
2121 case RNDIS_STATUS_MEDIA_CONNECT:
2122 if (rdev->link_state) {
2123 rdev->link_state = false;
2124 netif_carrier_on(net);
2125 netvsc_tx_enable(net_device, net);
2127 __netdev_notify_peers(net);
2131 case RNDIS_STATUS_MEDIA_DISCONNECT:
2132 if (!rdev->link_state) {
2133 rdev->link_state = true;
2134 netif_carrier_off(net);
2135 netvsc_tx_disable(net_device, net);
2139 case RNDIS_STATUS_NETWORK_CHANGE:
2140 /* Only makes sense if carrier is present */
2141 if (!rdev->link_state) {
2142 rdev->link_state = true;
2143 netif_carrier_off(net);
2144 netvsc_tx_disable(net_device, net);
2145 event->event = RNDIS_STATUS_MEDIA_CONNECT;
2146 spin_lock_irqsave(&ndev_ctx->lock, flags);
2147 list_add(&event->list, &ndev_ctx->reconfig_events);
2148 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2156 /* link_watch only sends one notification with current state per
2157 * second, handle next reconfig event in 2 seconds.
2160 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2168 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
2170 struct net_device_context *net_device_ctx;
2171 struct net_device *dev;
2173 dev = netdev_master_upper_dev_get(vf_netdev);
2174 if (!dev || dev->netdev_ops != &device_ops)
2175 return NULL; /* not a netvsc device */
2177 net_device_ctx = netdev_priv(dev);
2178 if (!rtnl_dereference(net_device_ctx->nvdev))
2179 return NULL; /* device is removed */
2184 /* Called when VF is injecting data into network stack.
2185 * Change the associated network device from VF to netvsc.
2186 * note: already called with rcu_read_lock
2188 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
2190 struct sk_buff *skb = *pskb;
2191 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
2192 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2193 struct netvsc_vf_pcpu_stats *pcpu_stats
2194 = this_cpu_ptr(ndev_ctx->vf_stats);
2196 skb = skb_share_check(skb, GFP_ATOMIC);
2198 return RX_HANDLER_CONSUMED;
2204 u64_stats_update_begin(&pcpu_stats->syncp);
2205 pcpu_stats->rx_packets++;
2206 pcpu_stats->rx_bytes += skb->len;
2207 u64_stats_update_end(&pcpu_stats->syncp);
2209 return RX_HANDLER_ANOTHER;
2212 static int netvsc_vf_join(struct net_device *vf_netdev,
2213 struct net_device *ndev)
2215 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2218 ret = netdev_rx_handler_register(vf_netdev,
2219 netvsc_vf_handle_frame, ndev);
2221 netdev_err(vf_netdev,
2222 "can not register netvsc VF receive handler (err = %d)\n",
2224 goto rx_handler_failed;
2227 ret = netdev_master_upper_dev_link(vf_netdev, ndev,
2230 netdev_err(vf_netdev,
2231 "can not set master device %s (err = %d)\n",
2233 goto upper_link_failed;
2236 /* set slave flag before open to prevent IPv6 addrconf */
2237 vf_netdev->flags |= IFF_SLAVE;
2239 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
2241 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
2243 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
2247 netdev_rx_handler_unregister(vf_netdev);
2252 static void __netvsc_vf_setup(struct net_device *ndev,
2253 struct net_device *vf_netdev)
2257 /* Align MTU of VF with master */
2258 ret = dev_set_mtu(vf_netdev, ndev->mtu);
2260 netdev_warn(vf_netdev,
2261 "unable to change mtu to %u\n", ndev->mtu);
2263 /* set multicast etc flags on VF */
2264 dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE, NULL);
2266 /* sync address list from ndev to VF */
2267 netif_addr_lock_bh(ndev);
2268 dev_uc_sync(vf_netdev, ndev);
2269 dev_mc_sync(vf_netdev, ndev);
2270 netif_addr_unlock_bh(ndev);
2272 if (netif_running(ndev)) {
2273 ret = dev_open(vf_netdev, NULL);
2275 netdev_warn(vf_netdev,
2276 "unable to open: %d\n", ret);
2280 /* Setup VF as slave of the synthetic device.
2281 * Runs in workqueue to avoid recursion in netlink callbacks.
2283 static void netvsc_vf_setup(struct work_struct *w)
2285 struct net_device_context *ndev_ctx
2286 = container_of(w, struct net_device_context, vf_takeover.work);
2287 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2288 struct net_device *vf_netdev;
2290 if (!rtnl_trylock()) {
2291 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
2295 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2297 __netvsc_vf_setup(ndev, vf_netdev);
2302 /* Find netvsc by VF serial number.
2303 * The PCI hyperv controller records the serial number as the slot kobj name.
2305 static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
2307 struct device *parent = vf_netdev->dev.parent;
2308 struct net_device_context *ndev_ctx;
2309 struct pci_dev *pdev;
2312 if (!parent || !dev_is_pci(parent))
2313 return NULL; /* not a PCI device */
2315 pdev = to_pci_dev(parent);
2317 netdev_notice(vf_netdev, "no PCI slot information\n");
2321 if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
2322 netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
2323 pci_slot_name(pdev->slot));
2327 list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
2328 if (!ndev_ctx->vf_alloc)
2331 if (ndev_ctx->vf_serial == serial)
2332 return hv_get_drvdata(ndev_ctx->device_ctx);
2335 netdev_notice(vf_netdev,
2336 "no netdev found for vf serial:%u\n", serial);
2340 static int netvsc_register_vf(struct net_device *vf_netdev)
2342 struct net_device_context *net_device_ctx;
2343 struct netvsc_device *netvsc_dev;
2344 struct bpf_prog *prog;
2345 struct net_device *ndev;
2348 if (vf_netdev->addr_len != ETH_ALEN)
2351 ndev = get_netvsc_byslot(vf_netdev);
2355 net_device_ctx = netdev_priv(ndev);
2356 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2357 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
2360 /* if synthetic interface is a different namespace,
2361 * then move the VF to that namespace; join will be
2362 * done again in that context.
2364 if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
2365 ret = dev_change_net_namespace(vf_netdev,
2366 dev_net(ndev), "eth%d");
2368 netdev_err(vf_netdev,
2369 "could not move to same namespace as %s: %d\n",
2372 netdev_info(vf_netdev,
2373 "VF moved to namespace with: %s\n",
2378 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
2380 if (netvsc_vf_join(vf_netdev, ndev) != 0)
2383 dev_hold(vf_netdev);
2384 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
2386 vf_netdev->wanted_features = ndev->features;
2387 netdev_update_features(vf_netdev);
2389 prog = netvsc_xdp_get(netvsc_dev);
2390 netvsc_vf_setxdp(vf_netdev, prog);
2395 /* Change the data path when VF UP/DOWN/CHANGE are detected.
2397 * Typically a UP or DOWN event is followed by a CHANGE event, so
2398 * net_device_ctx->data_path_is_vf is used to cache the current data path
2399 * to avoid the duplicate call of netvsc_switch_datapath() and the duplicate
2402 * During hibernation, if a VF NIC driver (e.g. mlx5) preserves the network
2403 * interface, there is only the CHANGE event and no UP or DOWN event.
2405 static int netvsc_vf_changed(struct net_device *vf_netdev, unsigned long event)
2407 struct net_device_context *net_device_ctx;
2408 struct netvsc_device *netvsc_dev;
2409 struct net_device *ndev;
2410 bool vf_is_up = false;
2412 if (event != NETDEV_GOING_DOWN)
2413 vf_is_up = netif_running(vf_netdev);
2415 ndev = get_netvsc_byref(vf_netdev);
2419 net_device_ctx = netdev_priv(ndev);
2420 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2424 if (net_device_ctx->data_path_is_vf == vf_is_up)
2427 netvsc_switch_datapath(ndev, vf_is_up);
2428 netdev_info(ndev, "Data path switched %s VF: %s\n",
2429 vf_is_up ? "to" : "from", vf_netdev->name);
2434 static int netvsc_unregister_vf(struct net_device *vf_netdev)
2436 struct net_device *ndev;
2437 struct net_device_context *net_device_ctx;
2439 ndev = get_netvsc_byref(vf_netdev);
2443 net_device_ctx = netdev_priv(ndev);
2444 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
2446 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
2448 netvsc_vf_setxdp(vf_netdev, NULL);
2450 netdev_rx_handler_unregister(vf_netdev);
2451 netdev_upper_dev_unlink(vf_netdev, ndev);
2452 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
2458 static int netvsc_probe(struct hv_device *dev,
2459 const struct hv_vmbus_device_id *dev_id)
2461 struct net_device *net = NULL;
2462 struct net_device_context *net_device_ctx;
2463 struct netvsc_device_info *device_info = NULL;
2464 struct netvsc_device *nvdev;
2467 net = alloc_etherdev_mq(sizeof(struct net_device_context),
2472 netif_carrier_off(net);
2474 netvsc_init_settings(net);
2476 net_device_ctx = netdev_priv(net);
2477 net_device_ctx->device_ctx = dev;
2478 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
2479 if (netif_msg_probe(net_device_ctx))
2480 netdev_dbg(net, "netvsc msg_enable: %d\n",
2481 net_device_ctx->msg_enable);
2483 hv_set_drvdata(dev, net);
2485 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
2487 spin_lock_init(&net_device_ctx->lock);
2488 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
2489 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
2491 net_device_ctx->vf_stats
2492 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
2493 if (!net_device_ctx->vf_stats)
2496 net->netdev_ops = &device_ops;
2497 net->ethtool_ops = ðtool_ops;
2498 SET_NETDEV_DEV(net, &dev->device);
2500 /* We always need headroom for rndis header */
2501 net->needed_headroom = RNDIS_AND_PPI_SIZE;
2503 /* Initialize the number of queues to be 1, we may change it if more
2504 * channels are offered later.
2506 netif_set_real_num_tx_queues(net, 1);
2507 netif_set_real_num_rx_queues(net, 1);
2509 /* Notify the netvsc driver of the new device */
2510 device_info = netvsc_devinfo_get(NULL);
2514 goto devinfo_failed;
2517 nvdev = rndis_filter_device_add(dev, device_info);
2518 if (IS_ERR(nvdev)) {
2519 ret = PTR_ERR(nvdev);
2520 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2524 memcpy(net->dev_addr, device_info->mac_adr, ETH_ALEN);
2526 /* We must get rtnl lock before scheduling nvdev->subchan_work,
2527 * otherwise netvsc_subchan_work() can get rtnl lock first and wait
2528 * all subchannels to show up, but that may not happen because
2529 * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2530 * -> ... -> device_add() -> ... -> __device_attach() can't get
2531 * the device lock, so all the subchannels can't be processed --
2532 * finally netvsc_subchan_work() hangs forever.
2536 if (nvdev->num_chn > 1)
2537 schedule_work(&nvdev->subchan_work);
2539 /* hw_features computed in rndis_netdev_set_hwcaps() */
2540 net->features = net->hw_features |
2541 NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX |
2542 NETIF_F_HW_VLAN_CTAG_RX;
2543 net->vlan_features = net->features;
2545 netdev_lockdep_set_classes(net);
2547 /* MTU range: 68 - 1500 or 65521 */
2548 net->min_mtu = NETVSC_MTU_MIN;
2549 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2550 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2552 net->max_mtu = ETH_DATA_LEN;
2554 nvdev->tx_disable = false;
2556 ret = register_netdevice(net);
2558 pr_err("Unable to register netdev.\n");
2559 goto register_failed;
2562 list_add(&net_device_ctx->list, &netvsc_dev_list);
2565 netvsc_devinfo_put(device_info);
2570 rndis_filter_device_remove(dev, nvdev);
2572 netvsc_devinfo_put(device_info);
2574 free_percpu(net_device_ctx->vf_stats);
2576 hv_set_drvdata(dev, NULL);
2582 static int netvsc_remove(struct hv_device *dev)
2584 struct net_device_context *ndev_ctx;
2585 struct net_device *vf_netdev, *net;
2586 struct netvsc_device *nvdev;
2588 net = hv_get_drvdata(dev);
2590 dev_err(&dev->device, "No net device to remove\n");
2594 ndev_ctx = netdev_priv(net);
2596 cancel_delayed_work_sync(&ndev_ctx->dwork);
2599 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2601 cancel_work_sync(&nvdev->subchan_work);
2602 netvsc_xdp_set(net, NULL, NULL, nvdev);
2606 * Call to the vsc driver to let it know that the device is being
2607 * removed. Also blocks mtu and channel changes.
2609 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2611 netvsc_unregister_vf(vf_netdev);
2614 rndis_filter_device_remove(dev, nvdev);
2616 unregister_netdevice(net);
2617 list_del(&ndev_ctx->list);
2621 hv_set_drvdata(dev, NULL);
2623 free_percpu(ndev_ctx->vf_stats);
2628 static int netvsc_suspend(struct hv_device *dev)
2630 struct net_device_context *ndev_ctx;
2631 struct netvsc_device *nvdev;
2632 struct net_device *net;
2635 net = hv_get_drvdata(dev);
2637 ndev_ctx = netdev_priv(net);
2638 cancel_delayed_work_sync(&ndev_ctx->dwork);
2642 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2643 if (nvdev == NULL) {
2648 /* Save the current config info */
2649 ndev_ctx->saved_netvsc_dev_info = netvsc_devinfo_get(nvdev);
2651 ret = netvsc_detach(net, nvdev);
2658 static int netvsc_resume(struct hv_device *dev)
2660 struct net_device *net = hv_get_drvdata(dev);
2661 struct net_device_context *net_device_ctx;
2662 struct netvsc_device_info *device_info;
2667 net_device_ctx = netdev_priv(net);
2669 /* Reset the data path to the netvsc NIC before re-opening the vmbus
2670 * channel. Later netvsc_netdev_event() will switch the data path to
2671 * the VF upon the UP or CHANGE event.
2673 net_device_ctx->data_path_is_vf = false;
2674 device_info = net_device_ctx->saved_netvsc_dev_info;
2676 ret = netvsc_attach(net, device_info);
2678 netvsc_devinfo_put(device_info);
2679 net_device_ctx->saved_netvsc_dev_info = NULL;
2685 static const struct hv_vmbus_device_id id_table[] = {
2691 MODULE_DEVICE_TABLE(vmbus, id_table);
2693 /* The one and only one */
2694 static struct hv_driver netvsc_drv = {
2695 .name = KBUILD_MODNAME,
2696 .id_table = id_table,
2697 .probe = netvsc_probe,
2698 .remove = netvsc_remove,
2699 .suspend = netvsc_suspend,
2700 .resume = netvsc_resume,
2702 .probe_type = PROBE_FORCE_SYNCHRONOUS,
2707 * On Hyper-V, every VF interface is matched with a corresponding
2708 * synthetic interface. The synthetic interface is presented first
2709 * to the guest. When the corresponding VF instance is registered,
2710 * we will take care of switching the data path.
2712 static int netvsc_netdev_event(struct notifier_block *this,
2713 unsigned long event, void *ptr)
2715 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2717 /* Skip our own events */
2718 if (event_dev->netdev_ops == &device_ops)
2721 /* Avoid non-Ethernet type devices */
2722 if (event_dev->type != ARPHRD_ETHER)
2725 /* Avoid Vlan dev with same MAC registering as VF */
2726 if (is_vlan_dev(event_dev))
2729 /* Avoid Bonding master dev with same MAC registering as VF */
2730 if ((event_dev->priv_flags & IFF_BONDING) &&
2731 (event_dev->flags & IFF_MASTER))
2735 case NETDEV_REGISTER:
2736 return netvsc_register_vf(event_dev);
2737 case NETDEV_UNREGISTER:
2738 return netvsc_unregister_vf(event_dev);
2742 case NETDEV_GOING_DOWN:
2743 return netvsc_vf_changed(event_dev, event);
2749 static struct notifier_block netvsc_netdev_notifier = {
2750 .notifier_call = netvsc_netdev_event,
2753 static void __exit netvsc_drv_exit(void)
2755 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2756 vmbus_driver_unregister(&netvsc_drv);
2759 static int __init netvsc_drv_init(void)
2763 if (ring_size < RING_SIZE_MIN) {
2764 ring_size = RING_SIZE_MIN;
2765 pr_info("Increased ring_size to %u (min allowed)\n",
2768 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2770 ret = vmbus_driver_register(&netvsc_drv);
2774 register_netdevice_notifier(&netvsc_netdev_notifier);
2778 MODULE_LICENSE("GPL");
2779 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2781 module_init(netvsc_drv_init);
2782 module_exit(netvsc_drv_exit);