2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
17 * Haiyang Zhang <haiyangz@microsoft.com>
18 * Hank Janssen <hjanssen@microsoft.com>
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/init.h>
23 #include <linux/atomic.h>
24 #include <linux/module.h>
25 #include <linux/highmem.h>
26 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/inetdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/pci.h>
33 #include <linux/skbuff.h>
34 #include <linux/if_vlan.h>
36 #include <linux/slab.h>
38 #include <net/route.h>
40 #include <net/pkt_sched.h>
42 #include "hyperv_net.h"
44 #define RING_SIZE_MIN 64
45 #define LINKCHANGE_INT (2 * HZ)
46 #define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
52 /* Restrict GSO size to account for NVGRE */
53 #define NETVSC_GSO_MAX_SIZE 62768
55 static int ring_size = 128;
56 module_param(ring_size, int, S_IRUGO);
57 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
59 static int max_num_vrss_chns = 8;
61 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
62 NETIF_MSG_LINK | NETIF_MSG_IFUP |
63 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
66 static int debug = -1;
67 module_param(debug, int, S_IRUGO);
68 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70 static void do_set_multicast(struct work_struct *w)
72 struct net_device_context *ndevctx =
73 container_of(w, struct net_device_context, work);
74 struct hv_device *device_obj = ndevctx->device_ctx;
75 struct net_device *ndev = hv_get_drvdata(device_obj);
76 struct netvsc_device *nvdev = ndevctx->nvdev;
77 struct rndis_device *rdev;
82 rdev = nvdev->extension;
86 if (ndev->flags & IFF_PROMISC)
87 rndis_filter_set_packet_filter(rdev,
88 NDIS_PACKET_TYPE_PROMISCUOUS);
90 rndis_filter_set_packet_filter(rdev,
91 NDIS_PACKET_TYPE_BROADCAST |
92 NDIS_PACKET_TYPE_ALL_MULTICAST |
93 NDIS_PACKET_TYPE_DIRECTED);
96 static void netvsc_set_multicast_list(struct net_device *net)
98 struct net_device_context *net_device_ctx = netdev_priv(net);
100 schedule_work(&net_device_ctx->work);
103 static int netvsc_open(struct net_device *net)
105 struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
106 struct rndis_device *rdev;
109 netif_carrier_off(net);
111 /* Open up the device */
112 ret = rndis_filter_open(nvdev);
114 netdev_err(net, "unable to open device (ret %d).\n", ret);
118 netif_tx_wake_all_queues(net);
120 rdev = nvdev->extension;
121 if (!rdev->link_state)
122 netif_carrier_on(net);
127 static int netvsc_close(struct net_device *net)
129 struct net_device_context *net_device_ctx = netdev_priv(net);
130 struct netvsc_device *nvdev = net_device_ctx->nvdev;
132 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
133 struct vmbus_channel *chn;
135 netif_tx_disable(net);
137 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
138 cancel_work_sync(&net_device_ctx->work);
139 ret = rndis_filter_close(nvdev);
141 netdev_err(net, "unable to close device (ret %d).\n", ret);
145 /* Ensure pending bytes in ring are read */
148 for (i = 0; i < nvdev->num_chn; i++) {
149 chn = nvdev->chn_table[i];
153 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
159 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
167 if (retry > retry_max || aread == 0)
177 netdev_err(net, "Ring buffer not empty after closing rndis\n");
184 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
187 struct rndis_packet *rndis_pkt;
188 struct rndis_per_packet_info *ppi;
190 rndis_pkt = &msg->msg.pkt;
191 rndis_pkt->data_offset += ppi_size;
193 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
194 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
196 ppi->size = ppi_size;
197 ppi->type = pkt_type;
198 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
200 rndis_pkt->per_pkt_info_len += ppi_size;
205 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
206 void *accel_priv, select_queue_fallback_t fallback)
208 struct net_device_context *net_device_ctx = netdev_priv(ndev);
209 struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
213 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
216 hash = skb_get_hash(skb);
217 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
218 ndev->real_num_tx_queues;
220 if (!nvsc_dev->chn_table[q_idx])
226 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
227 struct hv_page_buffer *pb)
231 /* Deal with compund pages by ignoring unused part
234 page += (offset >> PAGE_SHIFT);
235 offset &= ~PAGE_MASK;
240 bytes = PAGE_SIZE - offset;
243 pb[j].pfn = page_to_pfn(page);
244 pb[j].offset = offset;
250 if (offset == PAGE_SIZE && len) {
260 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
261 struct hv_netvsc_packet *packet,
262 struct hv_page_buffer **page_buf)
264 struct hv_page_buffer *pb = *page_buf;
266 char *data = skb->data;
267 int frags = skb_shinfo(skb)->nr_frags;
270 /* The packet is laid out thus:
271 * 1. hdr: RNDIS header and PPI
273 * 3. skb fragment data
276 slots_used += fill_pg_buf(virt_to_page(hdr),
278 len, &pb[slots_used]);
280 packet->rmsg_size = len;
281 packet->rmsg_pgcnt = slots_used;
283 slots_used += fill_pg_buf(virt_to_page(data),
284 offset_in_page(data),
285 skb_headlen(skb), &pb[slots_used]);
287 for (i = 0; i < frags; i++) {
288 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
290 slots_used += fill_pg_buf(skb_frag_page(frag),
292 skb_frag_size(frag), &pb[slots_used]);
297 static int count_skb_frag_slots(struct sk_buff *skb)
299 int i, frags = skb_shinfo(skb)->nr_frags;
302 for (i = 0; i < frags; i++) {
303 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
304 unsigned long size = skb_frag_size(frag);
305 unsigned long offset = frag->page_offset;
307 /* Skip unused frames from start of page */
308 offset &= ~PAGE_MASK;
309 pages += PFN_UP(offset + size);
314 static int netvsc_get_slots(struct sk_buff *skb)
316 char *data = skb->data;
317 unsigned int offset = offset_in_page(data);
318 unsigned int len = skb_headlen(skb);
322 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
323 frag_slots = count_skb_frag_slots(skb);
324 return slots + frag_slots;
327 static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
329 u32 ret_val = TRANSPORT_INFO_NOT_IP;
331 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
332 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
336 *trans_off = skb_transport_offset(skb);
338 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
339 struct iphdr *iphdr = ip_hdr(skb);
341 if (iphdr->protocol == IPPROTO_TCP)
342 ret_val = TRANSPORT_INFO_IPV4_TCP;
343 else if (iphdr->protocol == IPPROTO_UDP)
344 ret_val = TRANSPORT_INFO_IPV4_UDP;
346 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
347 ret_val = TRANSPORT_INFO_IPV6_TCP;
348 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
349 ret_val = TRANSPORT_INFO_IPV6_UDP;
356 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
358 struct net_device_context *net_device_ctx = netdev_priv(net);
359 struct hv_netvsc_packet *packet = NULL;
361 unsigned int num_data_pgs;
362 struct rndis_message *rndis_msg;
363 struct rndis_packet *rndis_pkt;
365 struct rndis_per_packet_info *ppi;
366 struct ndis_tcp_ip_checksum_info *csum_info;
371 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
372 struct hv_page_buffer *pb = page_buf;
374 /* We will atmost need two pages to describe the rndis
375 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
376 * of pages in a single packet. If skb is scattered around
377 * more pages we try linearizing it.
380 skb_length = skb->len;
381 num_data_pgs = netvsc_get_slots(skb) + 2;
383 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
384 ++net_device_ctx->eth_stats.tx_scattered;
386 if (skb_linearize(skb))
389 num_data_pgs = netvsc_get_slots(skb) + 2;
390 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
391 ++net_device_ctx->eth_stats.tx_too_big;
397 * Place the rndis header in the skb head room and
398 * the skb->cb will be used for hv_netvsc_packet
401 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
405 /* Use the skb control buffer for building up the packet */
406 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
407 FIELD_SIZEOF(struct sk_buff, cb));
408 packet = (struct hv_netvsc_packet *)skb->cb;
410 packet->q_idx = skb_get_queue_mapping(skb);
412 packet->total_data_buflen = skb->len;
414 rndis_msg = (struct rndis_message *)skb->head;
416 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
418 /* Add the rndis header */
419 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
420 rndis_msg->msg_len = packet->total_data_buflen;
421 rndis_pkt = &rndis_msg->msg.pkt;
422 rndis_pkt->data_offset = sizeof(struct rndis_packet);
423 rndis_pkt->data_len = packet->total_data_buflen;
424 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
426 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
428 hash = skb_get_hash_raw(skb);
429 if (hash != 0 && net->real_num_tx_queues > 1) {
430 rndis_msg_size += NDIS_HASH_PPI_SIZE;
431 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
433 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
436 if (skb_vlan_tag_present(skb)) {
437 struct ndis_pkt_8021q_info *vlan;
439 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
440 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
442 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
444 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
445 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
449 net_trans_info = get_net_transport_info(skb, &hdr_offset);
452 * Setup the sendside checksum offload only if this is not a
455 if ((net_trans_info & (INFO_TCP | INFO_UDP)) && skb_is_gso(skb)) {
456 struct ndis_tcp_lso_info *lso_info;
458 rndis_msg_size += NDIS_LSO_PPI_SIZE;
459 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
460 TCP_LARGESEND_PKTINFO);
462 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
465 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
466 if (net_trans_info & (INFO_IPV4 << 16)) {
467 lso_info->lso_v2_transmit.ip_version =
468 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
469 ip_hdr(skb)->tot_len = 0;
470 ip_hdr(skb)->check = 0;
471 tcp_hdr(skb)->check =
472 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
473 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
475 lso_info->lso_v2_transmit.ip_version =
476 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
477 ipv6_hdr(skb)->payload_len = 0;
478 tcp_hdr(skb)->check =
479 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
480 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
482 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
483 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
484 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
485 if (net_trans_info & INFO_TCP) {
486 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
487 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
488 TCPIP_CHKSUM_PKTINFO);
490 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
493 if (net_trans_info & (INFO_IPV4 << 16))
494 csum_info->transmit.is_ipv4 = 1;
496 csum_info->transmit.is_ipv6 = 1;
498 csum_info->transmit.tcp_checksum = 1;
499 csum_info->transmit.tcp_header_offset = hdr_offset;
501 /* UDP checksum (and other) offload is not supported. */
502 if (skb_checksum_help(skb))
507 /* Start filling in the page buffers with the rndis hdr */
508 rndis_msg->msg_len += rndis_msg_size;
509 packet->total_data_buflen = rndis_msg->msg_len;
510 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
513 /* timestamp packet in software */
514 skb_tx_timestamp(skb);
515 ret = netvsc_send(net_device_ctx->device_ctx, packet,
516 rndis_msg, &pb, skb);
517 if (likely(ret == 0)) {
518 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
520 u64_stats_update_begin(&tx_stats->syncp);
522 tx_stats->bytes += skb_length;
523 u64_stats_update_end(&tx_stats->syncp);
527 if (ret == -EAGAIN) {
528 ++net_device_ctx->eth_stats.tx_busy;
529 return NETDEV_TX_BUSY;
533 ++net_device_ctx->eth_stats.tx_no_space;
536 dev_kfree_skb_any(skb);
537 net->stats.tx_dropped++;
542 ++net_device_ctx->eth_stats.tx_no_memory;
547 * netvsc_linkstatus_callback - Link up/down notification
549 void netvsc_linkstatus_callback(struct hv_device *device_obj,
550 struct rndis_message *resp)
552 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
553 struct net_device *net;
554 struct net_device_context *ndev_ctx;
555 struct netvsc_reconfig *event;
558 net = hv_get_drvdata(device_obj);
563 ndev_ctx = netdev_priv(net);
565 /* Update the physical link speed when changing to another vSwitch */
566 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
569 speed = *(u32 *)((void *)indicate + indicate->
570 status_buf_offset) / 10000;
571 ndev_ctx->speed = speed;
575 /* Handle these link change statuses below */
576 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
577 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
578 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
581 if (net->reg_state != NETREG_REGISTERED)
584 event = kzalloc(sizeof(*event), GFP_ATOMIC);
587 event->event = indicate->status;
589 spin_lock_irqsave(&ndev_ctx->lock, flags);
590 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
591 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
593 schedule_delayed_work(&ndev_ctx->dwork, 0);
596 static void netvsc_comp_ipcsum(struct sk_buff *skb)
598 struct iphdr *iph = (struct iphdr *)skb->data;
601 iph->check = ip_fast_csum(iph, iph->ihl);
604 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
605 struct hv_netvsc_packet *packet,
606 struct ndis_tcp_ip_checksum_info *csum_info,
607 void *data, u16 vlan_tci)
611 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
616 * Copy to skb. This copy is needed here since the memory pointed by
617 * hv_netvsc_packet cannot be deallocated
619 memcpy(skb_put(skb, packet->total_data_buflen), data,
620 packet->total_data_buflen);
622 skb->protocol = eth_type_trans(skb, net);
624 /* skb is already created with CHECKSUM_NONE */
625 skb_checksum_none_assert(skb);
627 /* Incoming packets may have IP header checksum verified by the host.
628 * They may not have IP header checksum computed after coalescing.
629 * We compute it here if the flags are set, because on Linux, the IP
630 * checksum is always checked.
632 if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
633 csum_info->receive.ip_checksum_succeeded &&
634 skb->protocol == htons(ETH_P_IP))
635 netvsc_comp_ipcsum(skb);
637 /* Do L4 checksum offload if enabled and present.
639 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
640 if (csum_info->receive.tcp_checksum_succeeded ||
641 csum_info->receive.udp_checksum_succeeded)
642 skb->ip_summed = CHECKSUM_UNNECESSARY;
645 if (vlan_tci & VLAN_TAG_PRESENT)
646 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
653 * netvsc_recv_callback - Callback when we receive a packet from the
654 * "wire" on the specified device.
656 int netvsc_recv_callback(struct hv_device *device_obj,
657 struct hv_netvsc_packet *packet,
659 struct ndis_tcp_ip_checksum_info *csum_info,
660 struct vmbus_channel *channel,
663 struct net_device *net = hv_get_drvdata(device_obj);
664 struct net_device_context *net_device_ctx = netdev_priv(net);
665 struct net_device *vf_netdev;
667 struct netvsc_stats *rx_stats;
669 if (net->reg_state != NETREG_REGISTERED)
670 return NVSP_STAT_FAIL;
673 * If necessary, inject this packet into the VF interface.
674 * On Hyper-V, multicast and brodcast packets are only delivered
675 * to the synthetic interface (after subjecting these to
676 * policy filters on the host). Deliver these via the VF
677 * interface in the guest.
680 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
681 if (vf_netdev && (vf_netdev->flags & IFF_UP))
684 /* Allocate a skb - TODO direct I/O to pages? */
685 skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
686 if (unlikely(!skb)) {
687 ++net->stats.rx_dropped;
689 return NVSP_STAT_FAIL;
692 if (net != vf_netdev)
693 skb_record_rx_queue(skb,
694 channel->offermsg.offer.sub_channel_index);
697 * Even if injecting the packet, record the statistics
698 * on the synthetic device because modifying the VF device
699 * statistics will not work correctly.
701 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
702 u64_stats_update_begin(&rx_stats->syncp);
704 rx_stats->bytes += packet->total_data_buflen;
706 if (skb->pkt_type == PACKET_BROADCAST)
707 ++rx_stats->broadcast;
708 else if (skb->pkt_type == PACKET_MULTICAST)
709 ++rx_stats->multicast;
710 u64_stats_update_end(&rx_stats->syncp);
713 * Pass the skb back up. Network stack will deallocate the skb when it
723 static void netvsc_get_drvinfo(struct net_device *net,
724 struct ethtool_drvinfo *info)
726 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
727 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
730 static void netvsc_get_channels(struct net_device *net,
731 struct ethtool_channels *channel)
733 struct net_device_context *net_device_ctx = netdev_priv(net);
734 struct netvsc_device *nvdev = net_device_ctx->nvdev;
737 channel->max_combined = nvdev->max_chn;
738 channel->combined_count = nvdev->num_chn;
742 static int netvsc_set_channels(struct net_device *net,
743 struct ethtool_channels *channels)
745 struct net_device_context *net_device_ctx = netdev_priv(net);
746 struct hv_device *dev = net_device_ctx->device_ctx;
747 struct netvsc_device *nvdev = net_device_ctx->nvdev;
748 struct netvsc_device_info device_info;
752 bool recovering = false;
754 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
757 num_chn = nvdev->num_chn;
758 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
760 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
761 pr_info("vRSS unsupported before NVSP Version 5\n");
765 /* We do not support rx, tx, or other */
767 channels->rx_count ||
768 channels->tx_count ||
769 channels->other_count ||
770 (channels->combined_count < 1))
773 if (channels->combined_count > max_chn) {
774 pr_info("combined channels too high, using %d\n", max_chn);
775 channels->combined_count = max_chn;
778 ret = netvsc_close(net);
783 net_device_ctx->start_remove = true;
784 rndis_filter_device_remove(dev);
786 nvdev->num_chn = channels->combined_count;
788 memset(&device_info, 0, sizeof(device_info));
789 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
790 device_info.ring_size = ring_size;
791 device_info.max_num_vrss_chns = max_num_vrss_chns;
793 ret = rndis_filter_device_add(dev, &device_info);
796 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
802 nvdev = net_device_ctx->nvdev;
804 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
807 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
813 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
816 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
824 net_device_ctx->start_remove = false;
825 /* We may have missed link change notifications */
826 schedule_delayed_work(&net_device_ctx->dwork, 0);
831 /* If the above failed, we attempt to recover through the same
832 * process but with the original number of channels.
834 netdev_err(net, "could not set channels, recovering\n");
836 channels->combined_count = num_chn;
840 static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
842 struct ethtool_cmd diff1 = *cmd;
843 struct ethtool_cmd diff2 = {};
845 ethtool_cmd_speed_set(&diff1, 0);
847 /* advertising and cmd are usually set */
848 diff1.advertising = 0;
850 /* We set port to PORT_OTHER */
851 diff2.port = PORT_OTHER;
853 return !memcmp(&diff1, &diff2, sizeof(diff1));
856 static void netvsc_init_settings(struct net_device *dev)
858 struct net_device_context *ndc = netdev_priv(dev);
860 ndc->speed = SPEED_UNKNOWN;
861 ndc->duplex = DUPLEX_UNKNOWN;
864 static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
866 struct net_device_context *ndc = netdev_priv(dev);
868 ethtool_cmd_speed_set(cmd, ndc->speed);
869 cmd->duplex = ndc->duplex;
870 cmd->port = PORT_OTHER;
875 static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
877 struct net_device_context *ndc = netdev_priv(dev);
880 speed = ethtool_cmd_speed(cmd);
881 if (!ethtool_validate_speed(speed) ||
882 !ethtool_validate_duplex(cmd->duplex) ||
883 !netvsc_validate_ethtool_ss_cmd(cmd))
887 ndc->duplex = cmd->duplex;
892 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
894 struct net_device_context *ndevctx = netdev_priv(ndev);
895 struct netvsc_device *nvdev = ndevctx->nvdev;
896 struct hv_device *hdev = ndevctx->device_ctx;
897 struct netvsc_device_info device_info;
898 int limit = ETH_DATA_LEN;
902 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
905 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
906 limit = NETVSC_MTU - ETH_HLEN;
908 if (mtu < NETVSC_MTU_MIN || mtu > limit)
911 ret = netvsc_close(ndev);
915 num_chn = nvdev->num_chn;
917 ndevctx->start_remove = true;
918 rndis_filter_device_remove(hdev);
922 memset(&device_info, 0, sizeof(device_info));
923 device_info.ring_size = ring_size;
924 device_info.num_chn = num_chn;
925 device_info.max_num_vrss_chns = max_num_vrss_chns;
926 rndis_filter_device_add(hdev, &device_info);
930 ndevctx->start_remove = false;
932 /* We may have missed link change notifications */
933 schedule_delayed_work(&ndevctx->dwork, 0);
938 static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
939 struct rtnl_link_stats64 *t)
941 struct net_device_context *ndev_ctx = netdev_priv(net);
944 for_each_possible_cpu(cpu) {
945 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
947 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
949 u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
953 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
954 tx_packets = tx_stats->packets;
955 tx_bytes = tx_stats->bytes;
956 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
959 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
960 rx_packets = rx_stats->packets;
961 rx_bytes = rx_stats->bytes;
962 rx_multicast = rx_stats->multicast + rx_stats->broadcast;
963 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
965 t->tx_bytes += tx_bytes;
966 t->tx_packets += tx_packets;
967 t->rx_bytes += rx_bytes;
968 t->rx_packets += rx_packets;
969 t->multicast += rx_multicast;
972 t->tx_dropped = net->stats.tx_dropped;
973 t->tx_errors = net->stats.tx_dropped;
975 t->rx_dropped = net->stats.rx_dropped;
976 t->rx_errors = net->stats.rx_errors;
981 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
983 struct sockaddr *addr = p;
984 char save_adr[ETH_ALEN];
985 unsigned char save_aatype;
988 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
989 save_aatype = ndev->addr_assign_type;
991 err = eth_mac_addr(ndev, p);
995 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
997 /* roll back to saved MAC */
998 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
999 ndev->addr_assign_type = save_aatype;
1005 static const struct {
1006 char name[ETH_GSTRING_LEN];
1008 } netvsc_stats[] = {
1009 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1010 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1011 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1012 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1013 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1016 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1018 switch (string_set) {
1020 return ARRAY_SIZE(netvsc_stats);
1026 static void netvsc_get_ethtool_stats(struct net_device *dev,
1027 struct ethtool_stats *stats, u64 *data)
1029 struct net_device_context *ndc = netdev_priv(dev);
1030 const void *nds = &ndc->eth_stats;
1033 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1034 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1037 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1041 switch (stringset) {
1043 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1044 memcpy(data + i * ETH_GSTRING_LEN,
1045 netvsc_stats[i].name, ETH_GSTRING_LEN);
1050 #ifdef CONFIG_NET_POLL_CONTROLLER
1051 static void netvsc_poll_controller(struct net_device *net)
1053 /* As netvsc_start_xmit() works synchronous we don't have to
1054 * trigger anything here.
1059 static const struct ethtool_ops ethtool_ops = {
1060 .get_drvinfo = netvsc_get_drvinfo,
1061 .get_link = ethtool_op_get_link,
1062 .get_ethtool_stats = netvsc_get_ethtool_stats,
1063 .get_sset_count = netvsc_get_sset_count,
1064 .get_strings = netvsc_get_strings,
1065 .get_channels = netvsc_get_channels,
1066 .set_channels = netvsc_set_channels,
1067 .get_ts_info = ethtool_op_get_ts_info,
1068 .get_settings = netvsc_get_settings,
1069 .set_settings = netvsc_set_settings,
1072 static const struct net_device_ops device_ops = {
1073 .ndo_open = netvsc_open,
1074 .ndo_stop = netvsc_close,
1075 .ndo_start_xmit = netvsc_start_xmit,
1076 .ndo_set_rx_mode = netvsc_set_multicast_list,
1077 .ndo_change_mtu = netvsc_change_mtu,
1078 .ndo_validate_addr = eth_validate_addr,
1079 .ndo_set_mac_address = netvsc_set_mac_addr,
1080 .ndo_select_queue = netvsc_select_queue,
1081 .ndo_get_stats64 = netvsc_get_stats64,
1082 #ifdef CONFIG_NET_POLL_CONTROLLER
1083 .ndo_poll_controller = netvsc_poll_controller,
1088 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1089 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1090 * present send GARP packet to network peers with netif_notify_peers().
1092 static void netvsc_link_change(struct work_struct *w)
1094 struct net_device_context *ndev_ctx =
1095 container_of(w, struct net_device_context, dwork.work);
1096 struct hv_device *device_obj = ndev_ctx->device_ctx;
1097 struct net_device *net = hv_get_drvdata(device_obj);
1098 struct netvsc_device *net_device;
1099 struct rndis_device *rdev;
1100 struct netvsc_reconfig *event = NULL;
1101 bool notify = false, reschedule = false;
1102 unsigned long flags, next_reconfig, delay;
1104 /* if changes are happening, comeback later */
1105 if (!rtnl_trylock()) {
1106 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1110 if (ndev_ctx->start_remove)
1113 net_device = ndev_ctx->nvdev;
1114 rdev = net_device->extension;
1116 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1117 if (time_is_after_jiffies(next_reconfig)) {
1118 /* link_watch only sends one notification with current state
1119 * per second, avoid doing reconfig more frequently. Handle
1122 delay = next_reconfig - jiffies;
1123 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1124 schedule_delayed_work(&ndev_ctx->dwork, delay);
1127 ndev_ctx->last_reconfig = jiffies;
1129 spin_lock_irqsave(&ndev_ctx->lock, flags);
1130 if (!list_empty(&ndev_ctx->reconfig_events)) {
1131 event = list_first_entry(&ndev_ctx->reconfig_events,
1132 struct netvsc_reconfig, list);
1133 list_del(&event->list);
1134 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1136 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1141 switch (event->event) {
1142 /* Only the following events are possible due to the check in
1143 * netvsc_linkstatus_callback()
1145 case RNDIS_STATUS_MEDIA_CONNECT:
1146 if (rdev->link_state) {
1147 rdev->link_state = false;
1148 netif_carrier_on(net);
1149 netif_tx_wake_all_queues(net);
1155 case RNDIS_STATUS_MEDIA_DISCONNECT:
1156 if (!rdev->link_state) {
1157 rdev->link_state = true;
1158 netif_carrier_off(net);
1159 netif_tx_stop_all_queues(net);
1163 case RNDIS_STATUS_NETWORK_CHANGE:
1164 /* Only makes sense if carrier is present */
1165 if (!rdev->link_state) {
1166 rdev->link_state = true;
1167 netif_carrier_off(net);
1168 netif_tx_stop_all_queues(net);
1169 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1170 spin_lock_irqsave(&ndev_ctx->lock, flags);
1171 list_add(&event->list, &ndev_ctx->reconfig_events);
1172 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1181 netdev_notify_peers(net);
1183 /* link_watch only sends one notification with current state per
1184 * second, handle next reconfig event in 2 seconds.
1187 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1195 static void netvsc_free_netdev(struct net_device *netdev)
1197 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1199 free_percpu(net_device_ctx->tx_stats);
1200 free_percpu(net_device_ctx->rx_stats);
1201 free_netdev(netdev);
1204 static struct net_device *get_netvsc_bymac(const u8 *mac)
1206 struct net_device *dev;
1210 for_each_netdev(&init_net, dev) {
1211 if (dev->netdev_ops != &device_ops)
1212 continue; /* not a netvsc device */
1214 if (ether_addr_equal(mac, dev->perm_addr))
1221 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1223 struct net_device *dev;
1227 for_each_netdev(&init_net, dev) {
1228 struct net_device_context *net_device_ctx;
1230 if (dev->netdev_ops != &device_ops)
1231 continue; /* not a netvsc device */
1233 net_device_ctx = netdev_priv(dev);
1234 if (net_device_ctx->nvdev == NULL)
1235 continue; /* device is removed */
1237 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1238 return dev; /* a match */
1244 static int netvsc_register_vf(struct net_device *vf_netdev)
1246 struct net_device *ndev;
1247 struct net_device_context *net_device_ctx;
1248 struct device *pdev = vf_netdev->dev.parent;
1249 struct netvsc_device *netvsc_dev;
1251 if (vf_netdev->addr_len != ETH_ALEN)
1254 if (!pdev || !dev_is_pci(pdev) || dev_is_pf(pdev))
1258 * We will use the MAC address to locate the synthetic interface to
1259 * associate with the VF interface. If we don't find a matching
1260 * synthetic interface, move on.
1262 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1266 net_device_ctx = netdev_priv(ndev);
1267 netvsc_dev = net_device_ctx->nvdev;
1268 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1271 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1273 * Take a reference on the module.
1275 try_module_get(THIS_MODULE);
1277 dev_hold(vf_netdev);
1278 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1282 static int netvsc_vf_up(struct net_device *vf_netdev)
1284 struct net_device *ndev;
1285 struct netvsc_device *netvsc_dev;
1286 struct net_device_context *net_device_ctx;
1288 ndev = get_netvsc_byref(vf_netdev);
1292 net_device_ctx = netdev_priv(ndev);
1293 netvsc_dev = net_device_ctx->nvdev;
1295 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
1298 * Open the device before switching data path.
1300 rndis_filter_open(netvsc_dev);
1303 * notify the host to switch the data path.
1305 netvsc_switch_datapath(ndev, true);
1306 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1308 netif_carrier_off(ndev);
1310 /* Now notify peers through VF device. */
1311 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
1316 static int netvsc_vf_down(struct net_device *vf_netdev)
1318 struct net_device *ndev;
1319 struct netvsc_device *netvsc_dev;
1320 struct net_device_context *net_device_ctx;
1322 ndev = get_netvsc_byref(vf_netdev);
1326 net_device_ctx = netdev_priv(ndev);
1327 netvsc_dev = net_device_ctx->nvdev;
1329 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
1330 netvsc_switch_datapath(ndev, false);
1331 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1332 rndis_filter_close(netvsc_dev);
1333 netif_carrier_on(ndev);
1335 /* Now notify peers through netvsc device. */
1336 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
1341 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1343 struct net_device *ndev;
1344 struct netvsc_device *netvsc_dev;
1345 struct net_device_context *net_device_ctx;
1347 ndev = get_netvsc_byref(vf_netdev);
1351 net_device_ctx = netdev_priv(ndev);
1352 netvsc_dev = net_device_ctx->nvdev;
1354 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1356 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1358 module_put(THIS_MODULE);
1362 static int netvsc_probe(struct hv_device *dev,
1363 const struct hv_vmbus_device_id *dev_id)
1365 struct net_device *net = NULL;
1366 struct net_device_context *net_device_ctx;
1367 struct netvsc_device_info device_info;
1368 struct netvsc_device *nvdev;
1371 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1376 netif_carrier_off(net);
1378 netvsc_init_settings(net);
1380 net_device_ctx = netdev_priv(net);
1381 net_device_ctx->device_ctx = dev;
1382 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1383 if (netif_msg_probe(net_device_ctx))
1384 netdev_dbg(net, "netvsc msg_enable: %d\n",
1385 net_device_ctx->msg_enable);
1387 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1388 if (!net_device_ctx->tx_stats) {
1392 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1393 if (!net_device_ctx->rx_stats) {
1394 free_percpu(net_device_ctx->tx_stats);
1399 hv_set_drvdata(dev, net);
1401 net_device_ctx->start_remove = false;
1403 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1404 INIT_WORK(&net_device_ctx->work, do_set_multicast);
1406 spin_lock_init(&net_device_ctx->lock);
1407 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1409 net->netdev_ops = &device_ops;
1411 net->hw_features = NETVSC_HW_FEATURES;
1412 net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
1414 net->ethtool_ops = ðtool_ops;
1415 SET_NETDEV_DEV(net, &dev->device);
1417 /* We always need headroom for rndis header */
1418 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1420 /* Notify the netvsc driver of the new device */
1421 memset(&device_info, 0, sizeof(device_info));
1422 device_info.ring_size = ring_size;
1423 device_info.max_num_vrss_chns = max_num_vrss_chns;
1424 ret = rndis_filter_device_add(dev, &device_info);
1426 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1427 netvsc_free_netdev(net);
1428 hv_set_drvdata(dev, NULL);
1431 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1433 nvdev = net_device_ctx->nvdev;
1434 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1435 netif_set_real_num_rx_queues(net, nvdev->num_chn);
1436 netif_set_gso_max_size(net, NETVSC_GSO_MAX_SIZE);
1438 ret = register_netdev(net);
1440 pr_err("Unable to register netdev.\n");
1441 rndis_filter_device_remove(dev);
1442 netvsc_free_netdev(net);
1448 static int netvsc_remove(struct hv_device *dev)
1450 struct net_device *net;
1451 struct net_device_context *ndev_ctx;
1452 struct netvsc_device *net_device;
1454 net = hv_get_drvdata(dev);
1457 dev_err(&dev->device, "No net device to remove\n");
1461 ndev_ctx = netdev_priv(net);
1462 net_device = ndev_ctx->nvdev;
1464 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1465 * removing the device.
1468 ndev_ctx->start_remove = true;
1471 cancel_delayed_work_sync(&ndev_ctx->dwork);
1472 cancel_work_sync(&ndev_ctx->work);
1474 /* Stop outbound asap */
1475 netif_tx_disable(net);
1477 unregister_netdev(net);
1480 * Call to the vsc driver to let it know that the device is being
1483 rndis_filter_device_remove(dev);
1485 hv_set_drvdata(dev, NULL);
1487 netvsc_free_netdev(net);
1491 static const struct hv_vmbus_device_id id_table[] = {
1497 MODULE_DEVICE_TABLE(vmbus, id_table);
1499 /* The one and only one */
1500 static struct hv_driver netvsc_drv = {
1501 .name = KBUILD_MODNAME,
1502 .id_table = id_table,
1503 .probe = netvsc_probe,
1504 .remove = netvsc_remove,
1508 * On Hyper-V, every VF interface is matched with a corresponding
1509 * synthetic interface. The synthetic interface is presented first
1510 * to the guest. When the corresponding VF instance is registered,
1511 * we will take care of switching the data path.
1513 static int netvsc_netdev_event(struct notifier_block *this,
1514 unsigned long event, void *ptr)
1516 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1518 /* Skip our own events */
1519 if (event_dev->netdev_ops == &device_ops)
1522 /* Avoid non-Ethernet type devices */
1523 if (event_dev->type != ARPHRD_ETHER)
1526 /* Avoid Vlan dev with same MAC registering as VF */
1527 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1530 /* Avoid Bonding master dev with same MAC registering as VF */
1531 if ((event_dev->priv_flags & IFF_BONDING) &&
1532 (event_dev->flags & IFF_MASTER))
1536 case NETDEV_REGISTER:
1537 return netvsc_register_vf(event_dev);
1538 case NETDEV_UNREGISTER:
1539 return netvsc_unregister_vf(event_dev);
1541 return netvsc_vf_up(event_dev);
1543 return netvsc_vf_down(event_dev);
1549 static struct notifier_block netvsc_netdev_notifier = {
1550 .notifier_call = netvsc_netdev_event,
1553 static void __exit netvsc_drv_exit(void)
1555 unregister_netdevice_notifier(&netvsc_netdev_notifier);
1556 vmbus_driver_unregister(&netvsc_drv);
1559 static int __init netvsc_drv_init(void)
1563 if (ring_size < RING_SIZE_MIN) {
1564 ring_size = RING_SIZE_MIN;
1565 pr_info("Increased ring_size to %d (min allowed)\n",
1568 ret = vmbus_driver_register(&netvsc_drv);
1573 register_netdevice_notifier(&netvsc_netdev_notifier);
1577 MODULE_LICENSE("GPL");
1578 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1580 module_init(netvsc_drv_init);
1581 module_exit(netvsc_drv_exit);