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/skbuff.h>
33 #include <linux/if_vlan.h>
35 #include <linux/slab.h>
37 #include <net/route.h>
39 #include <net/pkt_sched.h>
41 #include "hyperv_net.h"
43 /* Restrict GSO size to account for NVGRE */
44 #define NETVSC_GSO_MAX_SIZE 62768
46 #define RING_SIZE_MIN 64
47 static int ring_size = 128;
48 module_param(ring_size, int, S_IRUGO);
49 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
51 static int max_num_vrss_chns = 8;
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, S_IRUGO);
60 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
62 static void do_set_multicast(struct work_struct *w)
64 struct net_device_context *ndevctx =
65 container_of(w, struct net_device_context, work);
66 struct netvsc_device *nvdev;
67 struct rndis_device *rdev;
69 nvdev = hv_get_drvdata(ndevctx->device_ctx);
70 if (nvdev == NULL || nvdev->ndev == NULL)
73 rdev = nvdev->extension;
77 if (nvdev->ndev->flags & IFF_PROMISC)
78 rndis_filter_set_packet_filter(rdev,
79 NDIS_PACKET_TYPE_PROMISCUOUS);
81 rndis_filter_set_packet_filter(rdev,
82 NDIS_PACKET_TYPE_BROADCAST |
83 NDIS_PACKET_TYPE_ALL_MULTICAST |
84 NDIS_PACKET_TYPE_DIRECTED);
87 static void netvsc_set_multicast_list(struct net_device *net)
89 struct net_device_context *net_device_ctx = netdev_priv(net);
91 schedule_work(&net_device_ctx->work);
94 static int netvsc_open(struct net_device *net)
96 struct net_device_context *net_device_ctx = netdev_priv(net);
97 struct hv_device *device_obj = net_device_ctx->device_ctx;
98 struct netvsc_device *nvdev;
99 struct rndis_device *rdev;
102 netif_carrier_off(net);
104 /* Open up the device */
105 ret = rndis_filter_open(device_obj);
107 netdev_err(net, "unable to open device (ret %d).\n", ret);
111 netif_tx_wake_all_queues(net);
113 nvdev = hv_get_drvdata(device_obj);
114 rdev = nvdev->extension;
115 if (!rdev->link_state)
116 netif_carrier_on(net);
121 static int netvsc_close(struct net_device *net)
123 struct net_device_context *net_device_ctx = netdev_priv(net);
124 struct hv_device *device_obj = net_device_ctx->device_ctx;
125 struct netvsc_device *nvdev = hv_get_drvdata(device_obj);
127 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
128 struct vmbus_channel *chn;
130 netif_tx_disable(net);
132 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
133 cancel_work_sync(&net_device_ctx->work);
134 ret = rndis_filter_close(device_obj);
136 netdev_err(net, "unable to close device (ret %d).\n", ret);
140 /* Ensure pending bytes in ring are read */
143 for (i = 0; i < nvdev->num_chn; i++) {
144 chn = nvdev->chn_table[i];
148 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
154 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
162 if (retry > retry_max || aread == 0)
172 netdev_err(net, "Ring buffer not empty after closing rndis\n");
179 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
182 struct rndis_packet *rndis_pkt;
183 struct rndis_per_packet_info *ppi;
185 rndis_pkt = &msg->msg.pkt;
186 rndis_pkt->data_offset += ppi_size;
188 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
189 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
191 ppi->size = ppi_size;
192 ppi->type = pkt_type;
193 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
195 rndis_pkt->per_pkt_info_len += ppi_size;
200 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
201 void *accel_priv, select_queue_fallback_t fallback)
203 struct net_device_context *net_device_ctx = netdev_priv(ndev);
204 struct hv_device *hdev = net_device_ctx->device_ctx;
205 struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
209 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
212 hash = skb_get_hash(skb);
213 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
214 ndev->real_num_tx_queues;
219 void netvsc_xmit_completion(void *context)
221 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
222 struct sk_buff *skb = (struct sk_buff *)
223 (unsigned long)packet->send_completion_tid;
226 dev_kfree_skb_any(skb);
229 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
230 struct hv_page_buffer *pb)
234 /* Deal with compund pages by ignoring unused part
237 page += (offset >> PAGE_SHIFT);
238 offset &= ~PAGE_MASK;
243 bytes = PAGE_SIZE - offset;
246 pb[j].pfn = page_to_pfn(page);
247 pb[j].offset = offset;
253 if (offset == PAGE_SIZE && len) {
263 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
264 struct hv_netvsc_packet *packet)
266 struct hv_page_buffer *pb = packet->page_buf;
268 char *data = skb->data;
269 int frags = skb_shinfo(skb)->nr_frags;
272 /* The packet is laid out thus:
273 * 1. hdr: RNDIS header and PPI
275 * 3. skb fragment data
278 slots_used += fill_pg_buf(virt_to_page(hdr),
280 len, &pb[slots_used]);
282 packet->rmsg_size = len;
283 packet->rmsg_pgcnt = slots_used;
285 slots_used += fill_pg_buf(virt_to_page(data),
286 offset_in_page(data),
287 skb_headlen(skb), &pb[slots_used]);
289 for (i = 0; i < frags; i++) {
290 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
292 slots_used += fill_pg_buf(skb_frag_page(frag),
294 skb_frag_size(frag), &pb[slots_used]);
299 static int count_skb_frag_slots(struct sk_buff *skb)
301 int i, frags = skb_shinfo(skb)->nr_frags;
304 for (i = 0; i < frags; i++) {
305 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
306 unsigned long size = skb_frag_size(frag);
307 unsigned long offset = frag->page_offset;
309 /* Skip unused frames from start of page */
310 offset &= ~PAGE_MASK;
311 pages += PFN_UP(offset + size);
316 static int netvsc_get_slots(struct sk_buff *skb)
318 char *data = skb->data;
319 unsigned int offset = offset_in_page(data);
320 unsigned int len = skb_headlen(skb);
324 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
325 frag_slots = count_skb_frag_slots(skb);
326 return slots + frag_slots;
329 static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
331 u32 ret_val = TRANSPORT_INFO_NOT_IP;
333 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
334 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
338 *trans_off = skb_transport_offset(skb);
340 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
341 struct iphdr *iphdr = ip_hdr(skb);
343 if (iphdr->protocol == IPPROTO_TCP)
344 ret_val = TRANSPORT_INFO_IPV4_TCP;
345 else if (iphdr->protocol == IPPROTO_UDP)
346 ret_val = TRANSPORT_INFO_IPV4_UDP;
348 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
349 ret_val = TRANSPORT_INFO_IPV6_TCP;
350 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
351 ret_val = TRANSPORT_INFO_IPV6_UDP;
358 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
360 struct net_device_context *net_device_ctx = netdev_priv(net);
361 struct hv_netvsc_packet *packet = NULL;
363 unsigned int num_data_pgs;
364 struct rndis_message *rndis_msg;
365 struct rndis_packet *rndis_pkt;
369 struct rndis_per_packet_info *ppi;
370 struct ndis_tcp_ip_checksum_info *csum_info;
371 struct ndis_tcp_lso_info *lso_info;
377 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
378 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
380 /* We will atmost need two pages to describe the rndis
381 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
382 * of pages in a single packet. If skb is scattered around
383 * more pages we try linearizing it.
387 skb_length = skb->len;
388 num_data_pgs = netvsc_get_slots(skb) + 2;
389 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
390 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
391 num_data_pgs, skb->len);
394 } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
395 if (skb_linearize(skb)) {
396 net_alert_ratelimited("failed to linearize skb\n");
404 pkt_sz = sizeof(struct hv_netvsc_packet) + RNDIS_AND_PPI_SIZE;
406 ret = skb_cow_head(skb, pkt_sz);
408 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
412 /* Use the headroom for building up the packet */
413 packet = (struct hv_netvsc_packet *)skb->head;
416 packet->xmit_more = skb->xmit_more;
418 packet->vlan_tci = skb->vlan_tci;
419 packet->page_buf = page_buf;
421 packet->q_idx = skb_get_queue_mapping(skb);
423 packet->is_data_pkt = true;
424 packet->total_data_buflen = skb->len;
426 packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
427 sizeof(struct hv_netvsc_packet));
429 memset(packet->rndis_msg, 0, RNDIS_AND_PPI_SIZE);
431 /* Set the completion routine */
432 packet->send_completion = netvsc_xmit_completion;
433 packet->send_completion_ctx = packet;
434 packet->send_completion_tid = (unsigned long)skb;
436 isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
438 /* Add the rndis header */
439 rndis_msg = packet->rndis_msg;
440 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
441 rndis_msg->msg_len = packet->total_data_buflen;
442 rndis_pkt = &rndis_msg->msg.pkt;
443 rndis_pkt->data_offset = sizeof(struct rndis_packet);
444 rndis_pkt->data_len = packet->total_data_buflen;
445 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
447 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
449 hash = skb_get_hash_raw(skb);
450 if (hash != 0 && net->real_num_tx_queues > 1) {
451 rndis_msg_size += NDIS_HASH_PPI_SIZE;
452 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
454 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
458 struct ndis_pkt_8021q_info *vlan;
460 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
461 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
463 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
465 vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
466 vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
470 net_trans_info = get_net_transport_info(skb, &hdr_offset);
471 if (net_trans_info == TRANSPORT_INFO_NOT_IP)
475 * Setup the sendside checksum offload only if this is not a
481 if ((skb->ip_summed == CHECKSUM_NONE) ||
482 (skb->ip_summed == CHECKSUM_UNNECESSARY))
485 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
486 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
487 TCPIP_CHKSUM_PKTINFO);
489 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
492 if (net_trans_info & (INFO_IPV4 << 16))
493 csum_info->transmit.is_ipv4 = 1;
495 csum_info->transmit.is_ipv6 = 1;
497 if (net_trans_info & INFO_TCP) {
498 csum_info->transmit.tcp_checksum = 1;
499 csum_info->transmit.tcp_header_offset = hdr_offset;
500 } else if (net_trans_info & INFO_UDP) {
501 /* UDP checksum offload is not supported on ws2008r2.
502 * Furthermore, on ws2012 and ws2012r2, there are some
503 * issues with udp checksum offload from Linux guests.
504 * (these are host issues).
505 * For now compute the checksum here.
510 ret = skb_cow_head(skb, 0);
515 udp_len = ntohs(uh->len);
517 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
519 udp_len, IPPROTO_UDP,
520 csum_partial(uh, udp_len, 0));
522 uh->check = CSUM_MANGLED_0;
524 csum_info->transmit.udp_checksum = 0;
529 rndis_msg_size += NDIS_LSO_PPI_SIZE;
530 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
531 TCP_LARGESEND_PKTINFO);
533 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
536 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
537 if (net_trans_info & (INFO_IPV4 << 16)) {
538 lso_info->lso_v2_transmit.ip_version =
539 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
540 ip_hdr(skb)->tot_len = 0;
541 ip_hdr(skb)->check = 0;
542 tcp_hdr(skb)->check =
543 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
544 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
546 lso_info->lso_v2_transmit.ip_version =
547 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
548 ipv6_hdr(skb)->payload_len = 0;
549 tcp_hdr(skb)->check =
550 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
551 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
553 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
554 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
557 /* Start filling in the page buffers with the rndis hdr */
558 rndis_msg->msg_len += rndis_msg_size;
559 packet->total_data_buflen = rndis_msg->msg_len;
560 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
563 ret = netvsc_send(net_device_ctx->device_ctx, packet);
567 u64_stats_update_begin(&tx_stats->syncp);
569 tx_stats->bytes += skb_length;
570 u64_stats_update_end(&tx_stats->syncp);
572 if (ret != -EAGAIN) {
573 dev_kfree_skb_any(skb);
574 net->stats.tx_dropped++;
578 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
582 * netvsc_linkstatus_callback - Link up/down notification
584 void netvsc_linkstatus_callback(struct hv_device *device_obj,
585 struct rndis_message *resp)
587 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
588 struct net_device *net;
589 struct net_device_context *ndev_ctx;
590 struct netvsc_device *net_device;
591 struct rndis_device *rdev;
593 net_device = hv_get_drvdata(device_obj);
594 rdev = net_device->extension;
596 switch (indicate->status) {
597 case RNDIS_STATUS_MEDIA_CONNECT:
598 rdev->link_state = false;
600 case RNDIS_STATUS_MEDIA_DISCONNECT:
601 rdev->link_state = true;
603 case RNDIS_STATUS_NETWORK_CHANGE:
604 rdev->link_change = true;
610 net = net_device->ndev;
612 if (!net || net->reg_state != NETREG_REGISTERED)
615 ndev_ctx = netdev_priv(net);
616 if (!rdev->link_state) {
617 schedule_delayed_work(&ndev_ctx->dwork, 0);
618 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
620 schedule_delayed_work(&ndev_ctx->dwork, 0);
625 * netvsc_recv_callback - Callback when we receive a packet from the
626 * "wire" on the specified device.
628 int netvsc_recv_callback(struct hv_device *device_obj,
629 struct hv_netvsc_packet *packet,
630 struct ndis_tcp_ip_checksum_info *csum_info)
632 struct net_device *net;
633 struct net_device_context *net_device_ctx;
635 struct netvsc_stats *rx_stats;
637 net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
638 if (!net || net->reg_state != NETREG_REGISTERED) {
639 packet->status = NVSP_STAT_FAIL;
642 net_device_ctx = netdev_priv(net);
643 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
645 /* Allocate a skb - TODO direct I/O to pages? */
646 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
647 if (unlikely(!skb)) {
648 ++net->stats.rx_dropped;
649 packet->status = NVSP_STAT_FAIL;
654 * Copy to skb. This copy is needed here since the memory pointed by
655 * hv_netvsc_packet cannot be deallocated
657 memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
658 packet->total_data_buflen);
660 skb->protocol = eth_type_trans(skb, net);
662 /* We only look at the IP checksum here.
663 * Should we be dropping the packet if checksum
664 * failed? How do we deal with other checksums - TCP/UDP?
666 if (csum_info->receive.ip_checksum_succeeded)
667 skb->ip_summed = CHECKSUM_UNNECESSARY;
669 skb->ip_summed = CHECKSUM_NONE;
672 if (packet->vlan_tci & VLAN_TAG_PRESENT)
673 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
676 skb_record_rx_queue(skb, packet->channel->
677 offermsg.offer.sub_channel_index);
679 u64_stats_update_begin(&rx_stats->syncp);
681 rx_stats->bytes += packet->total_data_buflen;
682 u64_stats_update_end(&rx_stats->syncp);
685 * Pass the skb back up. Network stack will deallocate the skb when it
694 static void netvsc_get_drvinfo(struct net_device *net,
695 struct ethtool_drvinfo *info)
697 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
698 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
701 static void netvsc_get_channels(struct net_device *net,
702 struct ethtool_channels *channel)
704 struct net_device_context *net_device_ctx = netdev_priv(net);
705 struct hv_device *dev = net_device_ctx->device_ctx;
706 struct netvsc_device *nvdev = hv_get_drvdata(dev);
709 channel->max_combined = nvdev->max_chn;
710 channel->combined_count = nvdev->num_chn;
714 static int netvsc_set_channels(struct net_device *net,
715 struct ethtool_channels *channels)
717 struct net_device_context *net_device_ctx = netdev_priv(net);
718 struct hv_device *dev = net_device_ctx->device_ctx;
719 struct netvsc_device *nvdev = hv_get_drvdata(dev);
720 struct netvsc_device_info device_info;
724 bool recovering = false;
726 if (!nvdev || nvdev->destroy)
729 num_chn = nvdev->num_chn;
730 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
732 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
733 pr_info("vRSS unsupported before NVSP Version 5\n");
737 /* We do not support rx, tx, or other */
739 channels->rx_count ||
740 channels->tx_count ||
741 channels->other_count ||
742 (channels->combined_count < 1))
745 if (channels->combined_count > max_chn) {
746 pr_info("combined channels too high, using %d\n", max_chn);
747 channels->combined_count = max_chn;
750 ret = netvsc_close(net);
755 nvdev->start_remove = true;
756 rndis_filter_device_remove(dev);
758 nvdev->num_chn = channels->combined_count;
760 net_device_ctx->device_ctx = dev;
761 hv_set_drvdata(dev, net);
763 memset(&device_info, 0, sizeof(device_info));
764 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
765 device_info.ring_size = ring_size;
766 device_info.max_num_vrss_chns = max_num_vrss_chns;
768 ret = rndis_filter_device_add(dev, &device_info);
771 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
777 nvdev = hv_get_drvdata(dev);
779 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
782 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
788 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
791 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
803 /* If the above failed, we attempt to recover through the same
804 * process but with the original number of channels.
806 netdev_err(net, "could not set channels, recovering\n");
808 channels->combined_count = num_chn;
812 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
814 struct net_device_context *ndevctx = netdev_priv(ndev);
815 struct hv_device *hdev = ndevctx->device_ctx;
816 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
817 struct netvsc_device_info device_info;
818 int limit = ETH_DATA_LEN;
821 if (nvdev == NULL || nvdev->destroy)
824 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
825 limit = NETVSC_MTU - ETH_HLEN;
827 if (mtu < NETVSC_MTU_MIN || mtu > limit)
830 ret = netvsc_close(ndev);
834 nvdev->start_remove = true;
835 rndis_filter_device_remove(hdev);
839 ndevctx->device_ctx = hdev;
840 hv_set_drvdata(hdev, ndev);
842 memset(&device_info, 0, sizeof(device_info));
843 device_info.ring_size = ring_size;
844 device_info.num_chn = nvdev->num_chn;
845 device_info.max_num_vrss_chns = max_num_vrss_chns;
846 rndis_filter_device_add(hdev, &device_info);
854 static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
855 struct rtnl_link_stats64 *t)
857 struct net_device_context *ndev_ctx = netdev_priv(net);
860 for_each_possible_cpu(cpu) {
861 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
863 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
865 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
869 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
870 tx_packets = tx_stats->packets;
871 tx_bytes = tx_stats->bytes;
872 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
875 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
876 rx_packets = rx_stats->packets;
877 rx_bytes = rx_stats->bytes;
878 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
880 t->tx_bytes += tx_bytes;
881 t->tx_packets += tx_packets;
882 t->rx_bytes += rx_bytes;
883 t->rx_packets += rx_packets;
886 t->tx_dropped = net->stats.tx_dropped;
887 t->tx_errors = net->stats.tx_dropped;
889 t->rx_dropped = net->stats.rx_dropped;
890 t->rx_errors = net->stats.rx_errors;
895 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
897 struct net_device_context *ndevctx = netdev_priv(ndev);
898 struct hv_device *hdev = ndevctx->device_ctx;
899 struct sockaddr *addr = p;
900 char save_adr[ETH_ALEN];
901 unsigned char save_aatype;
904 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
905 save_aatype = ndev->addr_assign_type;
907 err = eth_mac_addr(ndev, p);
911 err = rndis_filter_set_device_mac(hdev, addr->sa_data);
913 /* roll back to saved MAC */
914 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
915 ndev->addr_assign_type = save_aatype;
921 #ifdef CONFIG_NET_POLL_CONTROLLER
922 static void netvsc_poll_controller(struct net_device *net)
924 /* As netvsc_start_xmit() works synchronous we don't have to
925 * trigger anything here.
930 static const struct ethtool_ops ethtool_ops = {
931 .get_drvinfo = netvsc_get_drvinfo,
932 .get_link = ethtool_op_get_link,
933 .get_channels = netvsc_get_channels,
934 .set_channels = netvsc_set_channels,
937 static const struct net_device_ops device_ops = {
938 .ndo_open = netvsc_open,
939 .ndo_stop = netvsc_close,
940 .ndo_start_xmit = netvsc_start_xmit,
941 .ndo_set_rx_mode = netvsc_set_multicast_list,
942 .ndo_change_mtu = netvsc_change_mtu,
943 .ndo_validate_addr = eth_validate_addr,
944 .ndo_set_mac_address = netvsc_set_mac_addr,
945 .ndo_select_queue = netvsc_select_queue,
946 .ndo_get_stats64 = netvsc_get_stats64,
947 #ifdef CONFIG_NET_POLL_CONTROLLER
948 .ndo_poll_controller = netvsc_poll_controller,
953 * Send GARP packet to network peers after migrations.
954 * After Quick Migration, the network is not immediately operational in the
955 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
956 * another netif_notify_peers() into a delayed work, otherwise GARP packet
957 * will not be sent after quick migration, and cause network disconnection.
958 * Also, we update the carrier status here.
960 static void netvsc_link_change(struct work_struct *w)
962 struct net_device_context *ndev_ctx;
963 struct net_device *net;
964 struct netvsc_device *net_device;
965 struct rndis_device *rdev;
966 bool notify, refresh = false;
967 char *argv[] = { "/etc/init.d/network", "restart", NULL };
968 char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
972 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
973 net_device = hv_get_drvdata(ndev_ctx->device_ctx);
974 rdev = net_device->extension;
975 net = net_device->ndev;
977 if (rdev->link_state) {
978 netif_carrier_off(net);
981 netif_carrier_on(net);
983 if (rdev->link_change) {
984 rdev->link_change = false;
992 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
995 netdev_notify_peers(net);
998 static void netvsc_free_netdev(struct net_device *netdev)
1000 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1002 free_percpu(net_device_ctx->tx_stats);
1003 free_percpu(net_device_ctx->rx_stats);
1004 free_netdev(netdev);
1007 static int netvsc_probe(struct hv_device *dev,
1008 const struct hv_vmbus_device_id *dev_id)
1010 struct net_device *net = NULL;
1011 struct net_device_context *net_device_ctx;
1012 struct netvsc_device_info device_info;
1013 struct netvsc_device *nvdev;
1015 u32 max_needed_headroom;
1017 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1022 max_needed_headroom = sizeof(struct hv_netvsc_packet) +
1025 netif_carrier_off(net);
1027 net_device_ctx = netdev_priv(net);
1028 net_device_ctx->device_ctx = dev;
1029 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1030 if (netif_msg_probe(net_device_ctx))
1031 netdev_dbg(net, "netvsc msg_enable: %d\n",
1032 net_device_ctx->msg_enable);
1034 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1035 if (!net_device_ctx->tx_stats) {
1039 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1040 if (!net_device_ctx->rx_stats) {
1041 free_percpu(net_device_ctx->tx_stats);
1046 hv_set_drvdata(dev, net);
1047 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1048 INIT_WORK(&net_device_ctx->work, do_set_multicast);
1050 net->netdev_ops = &device_ops;
1052 net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
1054 net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
1055 NETIF_F_IP_CSUM | NETIF_F_TSO;
1057 net->ethtool_ops = ðtool_ops;
1058 SET_NETDEV_DEV(net, &dev->device);
1061 * Request additional head room in the skb.
1062 * We will use this space to build the rndis
1063 * heaser and other state we need to maintain.
1065 net->needed_headroom = max_needed_headroom;
1067 /* Notify the netvsc driver of the new device */
1068 memset(&device_info, 0, sizeof(device_info));
1069 device_info.ring_size = ring_size;
1070 device_info.max_num_vrss_chns = max_num_vrss_chns;
1071 ret = rndis_filter_device_add(dev, &device_info);
1073 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1074 netvsc_free_netdev(net);
1075 hv_set_drvdata(dev, NULL);
1078 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1080 nvdev = hv_get_drvdata(dev);
1081 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1082 netif_set_real_num_rx_queues(net, nvdev->num_chn);
1083 netif_set_gso_max_size(net, NETVSC_GSO_MAX_SIZE);
1085 ret = register_netdev(net);
1087 pr_err("Unable to register netdev.\n");
1088 rndis_filter_device_remove(dev);
1089 netvsc_free_netdev(net);
1091 schedule_delayed_work(&net_device_ctx->dwork, 0);
1097 static int netvsc_remove(struct hv_device *dev)
1099 struct net_device *net;
1100 struct net_device_context *ndev_ctx;
1101 struct netvsc_device *net_device;
1103 net_device = hv_get_drvdata(dev);
1104 net = net_device->ndev;
1107 dev_err(&dev->device, "No net device to remove\n");
1111 net_device->start_remove = true;
1113 ndev_ctx = netdev_priv(net);
1114 cancel_delayed_work_sync(&ndev_ctx->dwork);
1115 cancel_work_sync(&ndev_ctx->work);
1117 /* Stop outbound asap */
1118 netif_tx_disable(net);
1120 unregister_netdev(net);
1123 * Call to the vsc driver to let it know that the device is being
1126 rndis_filter_device_remove(dev);
1128 netvsc_free_netdev(net);
1132 static const struct hv_vmbus_device_id id_table[] = {
1138 MODULE_DEVICE_TABLE(vmbus, id_table);
1140 /* The one and only one */
1141 static struct hv_driver netvsc_drv = {
1142 .name = KBUILD_MODNAME,
1143 .id_table = id_table,
1144 .probe = netvsc_probe,
1145 .remove = netvsc_remove,
1148 static void __exit netvsc_drv_exit(void)
1150 vmbus_driver_unregister(&netvsc_drv);
1153 static int __init netvsc_drv_init(void)
1155 if (ring_size < RING_SIZE_MIN) {
1156 ring_size = RING_SIZE_MIN;
1157 pr_info("Increased ring_size to %d (min allowed)\n",
1160 return vmbus_driver_register(&netvsc_drv);
1163 MODULE_LICENSE("GPL");
1164 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1166 module_init(netvsc_drv_init);
1167 module_exit(netvsc_drv_exit);