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>
40 #include <net/checksum.h>
41 #include <net/ip6_checksum.h>
43 #include "hyperv_net.h"
45 #define RING_SIZE_MIN 64
46 #define LINKCHANGE_INT (2 * HZ)
48 static int ring_size = 128;
49 module_param(ring_size, int, S_IRUGO);
50 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
52 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
53 NETIF_MSG_LINK | NETIF_MSG_IFUP |
54 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
57 static int debug = -1;
58 module_param(debug, int, S_IRUGO);
59 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
61 static void netvsc_set_multicast_list(struct net_device *net)
63 struct net_device_context *net_device_ctx = netdev_priv(net);
64 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
66 rndis_filter_update(nvdev);
69 static int netvsc_open(struct net_device *net)
71 struct net_device_context *ndev_ctx = netdev_priv(net);
72 struct netvsc_device *nvdev = ndev_ctx->nvdev;
73 struct rndis_device *rdev;
76 netif_carrier_off(net);
78 /* Open up the device */
79 ret = rndis_filter_open(nvdev);
81 netdev_err(net, "unable to open device (ret %d).\n", ret);
85 netif_tx_wake_all_queues(net);
87 rdev = nvdev->extension;
88 if (!rdev->link_state && !ndev_ctx->datapath)
89 netif_carrier_on(net);
94 static int netvsc_close(struct net_device *net)
96 struct net_device_context *net_device_ctx = netdev_priv(net);
97 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
99 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
100 struct vmbus_channel *chn;
102 netif_tx_disable(net);
104 ret = rndis_filter_close(nvdev);
106 netdev_err(net, "unable to close device (ret %d).\n", ret);
110 /* Ensure pending bytes in ring are read */
113 for (i = 0; i < nvdev->num_chn; i++) {
114 chn = nvdev->chan_table[i].channel;
118 aread = hv_get_bytes_to_read(&chn->inbound);
122 aread = hv_get_bytes_to_read(&chn->outbound);
128 if (retry > retry_max || aread == 0)
138 netdev_err(net, "Ring buffer not empty after closing rndis\n");
145 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
148 struct rndis_packet *rndis_pkt;
149 struct rndis_per_packet_info *ppi;
151 rndis_pkt = &msg->msg.pkt;
152 rndis_pkt->data_offset += ppi_size;
154 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
155 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
157 ppi->size = ppi_size;
158 ppi->type = pkt_type;
159 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
161 rndis_pkt->per_pkt_info_len += ppi_size;
166 /* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
167 * hash for non-TCP traffic with only IP numbers.
169 static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
171 struct flow_keys flow;
173 static u32 hashrnd __read_mostly;
175 net_get_random_once(&hashrnd, sizeof(hashrnd));
177 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
180 if (flow.basic.ip_proto == IPPROTO_TCP) {
181 return skb_get_hash(skb);
183 if (flow.basic.n_proto == htons(ETH_P_IP))
184 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
185 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
186 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
190 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
196 static inline int netvsc_get_tx_queue(struct net_device *ndev,
197 struct sk_buff *skb, int old_idx)
199 const struct net_device_context *ndc = netdev_priv(ndev);
200 struct sock *sk = skb->sk;
203 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
204 (VRSS_SEND_TAB_SIZE - 1)];
206 /* If queue index changed record the new value */
207 if (q_idx != old_idx &&
208 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
209 sk_tx_queue_set(sk, q_idx);
215 * Select queue for transmit.
217 * If a valid queue has already been assigned, then use that.
218 * Otherwise compute tx queue based on hash and the send table.
220 * This is basically similar to default (__netdev_pick_tx) with the added step
221 * of using the host send_table when no other queue has been assigned.
223 * TODO support XPS - but get_xps_queue not exported
225 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
226 void *accel_priv, select_queue_fallback_t fallback)
228 unsigned int num_tx_queues = ndev->real_num_tx_queues;
229 int q_idx = sk_tx_queue_get(skb->sk);
231 if (q_idx < 0 || skb->ooo_okay) {
232 /* If forwarding a packet, we use the recorded queue when
233 * available for better cache locality.
235 if (skb_rx_queue_recorded(skb))
236 q_idx = skb_get_rx_queue(skb);
238 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
241 while (unlikely(q_idx >= num_tx_queues))
242 q_idx -= num_tx_queues;
247 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
248 struct hv_page_buffer *pb)
252 /* Deal with compund pages by ignoring unused part
255 page += (offset >> PAGE_SHIFT);
256 offset &= ~PAGE_MASK;
261 bytes = PAGE_SIZE - offset;
264 pb[j].pfn = page_to_pfn(page);
265 pb[j].offset = offset;
271 if (offset == PAGE_SIZE && len) {
281 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
282 struct hv_netvsc_packet *packet,
283 struct hv_page_buffer **page_buf)
285 struct hv_page_buffer *pb = *page_buf;
287 char *data = skb->data;
288 int frags = skb_shinfo(skb)->nr_frags;
291 /* The packet is laid out thus:
292 * 1. hdr: RNDIS header and PPI
294 * 3. skb fragment data
297 slots_used += fill_pg_buf(virt_to_page(hdr),
299 len, &pb[slots_used]);
301 packet->rmsg_size = len;
302 packet->rmsg_pgcnt = slots_used;
304 slots_used += fill_pg_buf(virt_to_page(data),
305 offset_in_page(data),
306 skb_headlen(skb), &pb[slots_used]);
308 for (i = 0; i < frags; i++) {
309 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
311 slots_used += fill_pg_buf(skb_frag_page(frag),
313 skb_frag_size(frag), &pb[slots_used]);
318 static int count_skb_frag_slots(struct sk_buff *skb)
320 int i, frags = skb_shinfo(skb)->nr_frags;
323 for (i = 0; i < frags; i++) {
324 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
325 unsigned long size = skb_frag_size(frag);
326 unsigned long offset = frag->page_offset;
328 /* Skip unused frames from start of page */
329 offset &= ~PAGE_MASK;
330 pages += PFN_UP(offset + size);
335 static int netvsc_get_slots(struct sk_buff *skb)
337 char *data = skb->data;
338 unsigned int offset = offset_in_page(data);
339 unsigned int len = skb_headlen(skb);
343 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
344 frag_slots = count_skb_frag_slots(skb);
345 return slots + frag_slots;
348 static u32 net_checksum_info(struct sk_buff *skb)
350 if (skb->protocol == htons(ETH_P_IP)) {
351 struct iphdr *ip = ip_hdr(skb);
353 if (ip->protocol == IPPROTO_TCP)
354 return TRANSPORT_INFO_IPV4_TCP;
355 else if (ip->protocol == IPPROTO_UDP)
356 return TRANSPORT_INFO_IPV4_UDP;
358 struct ipv6hdr *ip6 = ipv6_hdr(skb);
360 if (ip6->nexthdr == IPPROTO_TCP)
361 return TRANSPORT_INFO_IPV6_TCP;
362 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
363 return TRANSPORT_INFO_IPV6_UDP;
366 return TRANSPORT_INFO_NOT_IP;
369 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
371 struct net_device_context *net_device_ctx = netdev_priv(net);
372 struct hv_netvsc_packet *packet = NULL;
374 unsigned int num_data_pgs;
375 struct rndis_message *rndis_msg;
376 struct rndis_packet *rndis_pkt;
378 struct rndis_per_packet_info *ppi;
380 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
381 struct hv_page_buffer *pb = page_buf;
383 /* We will atmost need two pages to describe the rndis
384 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
385 * of pages in a single packet. If skb is scattered around
386 * more pages we try linearizing it.
389 num_data_pgs = netvsc_get_slots(skb) + 2;
391 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
392 ++net_device_ctx->eth_stats.tx_scattered;
394 if (skb_linearize(skb))
397 num_data_pgs = netvsc_get_slots(skb) + 2;
398 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
399 ++net_device_ctx->eth_stats.tx_too_big;
405 * Place the rndis header in the skb head room and
406 * the skb->cb will be used for hv_netvsc_packet
409 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
413 /* Use the skb control buffer for building up the packet */
414 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
415 FIELD_SIZEOF(struct sk_buff, cb));
416 packet = (struct hv_netvsc_packet *)skb->cb;
418 packet->q_idx = skb_get_queue_mapping(skb);
420 packet->total_data_buflen = skb->len;
421 packet->total_bytes = skb->len;
422 packet->total_packets = 1;
424 rndis_msg = (struct rndis_message *)skb->head;
426 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
428 /* Add the rndis header */
429 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
430 rndis_msg->msg_len = packet->total_data_buflen;
431 rndis_pkt = &rndis_msg->msg.pkt;
432 rndis_pkt->data_offset = sizeof(struct rndis_packet);
433 rndis_pkt->data_len = packet->total_data_buflen;
434 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
436 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
438 hash = skb_get_hash_raw(skb);
439 if (hash != 0 && net->real_num_tx_queues > 1) {
440 rndis_msg_size += NDIS_HASH_PPI_SIZE;
441 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
443 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
446 if (skb_vlan_tag_present(skb)) {
447 struct ndis_pkt_8021q_info *vlan;
449 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
450 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
452 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
454 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
455 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
459 if (skb_is_gso(skb)) {
460 struct ndis_tcp_lso_info *lso_info;
462 rndis_msg_size += NDIS_LSO_PPI_SIZE;
463 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
464 TCP_LARGESEND_PKTINFO);
466 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
469 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
470 if (skb->protocol == htons(ETH_P_IP)) {
471 lso_info->lso_v2_transmit.ip_version =
472 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
473 ip_hdr(skb)->tot_len = 0;
474 ip_hdr(skb)->check = 0;
475 tcp_hdr(skb)->check =
476 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
477 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
479 lso_info->lso_v2_transmit.ip_version =
480 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
481 ipv6_hdr(skb)->payload_len = 0;
482 tcp_hdr(skb)->check =
483 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
484 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
486 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
487 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
488 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
489 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
490 struct ndis_tcp_ip_checksum_info *csum_info;
492 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
493 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
494 TCPIP_CHKSUM_PKTINFO);
496 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
499 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
501 if (skb->protocol == htons(ETH_P_IP)) {
502 csum_info->transmit.is_ipv4 = 1;
504 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
505 csum_info->transmit.tcp_checksum = 1;
507 csum_info->transmit.udp_checksum = 1;
509 csum_info->transmit.is_ipv6 = 1;
511 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
512 csum_info->transmit.tcp_checksum = 1;
514 csum_info->transmit.udp_checksum = 1;
517 /* Can't do offload of this type of checksum */
518 if (skb_checksum_help(skb))
523 /* Start filling in the page buffers with the rndis hdr */
524 rndis_msg->msg_len += rndis_msg_size;
525 packet->total_data_buflen = rndis_msg->msg_len;
526 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
529 /* timestamp packet in software */
530 skb_tx_timestamp(skb);
531 ret = netvsc_send(net_device_ctx->device_ctx, packet,
532 rndis_msg, &pb, skb);
533 if (likely(ret == 0))
536 if (ret == -EAGAIN) {
537 ++net_device_ctx->eth_stats.tx_busy;
538 return NETDEV_TX_BUSY;
542 ++net_device_ctx->eth_stats.tx_no_space;
545 dev_kfree_skb_any(skb);
546 net->stats.tx_dropped++;
551 ++net_device_ctx->eth_stats.tx_no_memory;
555 * netvsc_linkstatus_callback - Link up/down notification
557 void netvsc_linkstatus_callback(struct hv_device *device_obj,
558 struct rndis_message *resp)
560 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
561 struct net_device *net;
562 struct net_device_context *ndev_ctx;
563 struct netvsc_reconfig *event;
566 net = hv_get_drvdata(device_obj);
571 ndev_ctx = netdev_priv(net);
573 /* Update the physical link speed when changing to another vSwitch */
574 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
577 speed = *(u32 *)((void *)indicate + indicate->
578 status_buf_offset) / 10000;
579 ndev_ctx->speed = speed;
583 /* Handle these link change statuses below */
584 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
585 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
586 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
589 if (net->reg_state != NETREG_REGISTERED)
592 event = kzalloc(sizeof(*event), GFP_ATOMIC);
595 event->event = indicate->status;
597 spin_lock_irqsave(&ndev_ctx->lock, flags);
598 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
599 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
601 schedule_delayed_work(&ndev_ctx->dwork, 0);
604 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
605 struct napi_struct *napi,
606 const struct ndis_tcp_ip_checksum_info *csum_info,
607 const struct ndis_pkt_8021q_info *vlan,
608 void *data, u32 buflen)
612 skb = napi_alloc_skb(napi, buflen);
617 * Copy to skb. This copy is needed here since the memory pointed by
618 * hv_netvsc_packet cannot be deallocated
620 skb_put_data(skb, data, buflen);
622 skb->protocol = eth_type_trans(skb, net);
624 /* skb is already created with CHECKSUM_NONE */
625 skb_checksum_none_assert(skb);
628 * In Linux, the IP checksum is always checked.
629 * Do L4 checksum offload if enabled and present.
631 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
632 if (csum_info->receive.tcp_checksum_succeeded ||
633 csum_info->receive.udp_checksum_succeeded)
634 skb->ip_summed = CHECKSUM_UNNECESSARY;
638 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
640 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
648 * netvsc_recv_callback - Callback when we receive a packet from the
649 * "wire" on the specified device.
651 int netvsc_recv_callback(struct net_device *net,
652 struct vmbus_channel *channel,
654 const struct ndis_tcp_ip_checksum_info *csum_info,
655 const struct ndis_pkt_8021q_info *vlan)
657 struct net_device_context *net_device_ctx = netdev_priv(net);
658 struct netvsc_device *net_device;
659 u16 q_idx = channel->offermsg.offer.sub_channel_index;
660 struct netvsc_channel *nvchan;
661 struct net_device *vf_netdev;
663 struct netvsc_stats *rx_stats;
665 if (net->reg_state != NETREG_REGISTERED)
666 return NVSP_STAT_FAIL;
669 * If necessary, inject this packet into the VF interface.
670 * On Hyper-V, multicast and brodcast packets are only delivered
671 * to the synthetic interface (after subjecting these to
672 * policy filters on the host). Deliver these via the VF
673 * interface in the guest.
676 net_device = rcu_dereference(net_device_ctx->nvdev);
677 if (unlikely(!net_device))
680 nvchan = &net_device->chan_table[q_idx];
681 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
682 if (vf_netdev && (vf_netdev->flags & IFF_UP))
685 /* Allocate a skb - TODO direct I/O to pages? */
686 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
687 csum_info, vlan, data, len);
688 if (unlikely(!skb)) {
690 ++net->stats.rx_dropped;
692 return NVSP_STAT_FAIL;
695 if (net != vf_netdev)
696 skb_record_rx_queue(skb, q_idx);
699 * Even if injecting the packet, record the statistics
700 * on the synthetic device because modifying the VF device
701 * statistics will not work correctly.
703 rx_stats = &nvchan->rx_stats;
704 u64_stats_update_begin(&rx_stats->syncp);
706 rx_stats->bytes += len;
708 if (skb->pkt_type == PACKET_BROADCAST)
709 ++rx_stats->broadcast;
710 else if (skb->pkt_type == PACKET_MULTICAST)
711 ++rx_stats->multicast;
712 u64_stats_update_end(&rx_stats->syncp);
714 napi_gro_receive(&nvchan->napi, skb);
720 static void netvsc_get_drvinfo(struct net_device *net,
721 struct ethtool_drvinfo *info)
723 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
724 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
727 static void netvsc_get_channels(struct net_device *net,
728 struct ethtool_channels *channel)
730 struct net_device_context *net_device_ctx = netdev_priv(net);
731 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
734 channel->max_combined = nvdev->max_chn;
735 channel->combined_count = nvdev->num_chn;
739 static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
742 struct netvsc_device_info device_info;
745 memset(&device_info, 0, sizeof(device_info));
746 device_info.num_chn = num_chn;
747 device_info.ring_size = ring_size;
748 device_info.max_num_vrss_chns = num_chn;
750 ret = rndis_filter_device_add(dev, &device_info);
754 ret = netif_set_real_num_tx_queues(net, num_chn);
758 ret = netif_set_real_num_rx_queues(net, num_chn);
763 static int netvsc_set_channels(struct net_device *net,
764 struct ethtool_channels *channels)
766 struct net_device_context *net_device_ctx = netdev_priv(net);
767 struct hv_device *dev = net_device_ctx->device_ctx;
768 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
769 unsigned int count = channels->combined_count;
773 /* We do not support separate count for rx, tx, or other */
775 channels->rx_count || channels->tx_count || channels->other_count)
778 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
781 if (!nvdev || nvdev->destroy)
784 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
787 if (count > nvdev->max_chn)
790 was_running = netif_running(net);
792 ret = netvsc_close(net);
797 rndis_filter_device_remove(dev, nvdev);
799 ret = netvsc_set_queues(net, dev, count);
801 nvdev->num_chn = count;
803 netvsc_set_queues(net, dev, nvdev->num_chn);
806 ret = netvsc_open(net);
808 /* We may have missed link change notifications */
809 schedule_delayed_work(&net_device_ctx->dwork, 0);
815 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
817 struct ethtool_link_ksettings diff1 = *cmd;
818 struct ethtool_link_ksettings diff2 = {};
820 diff1.base.speed = 0;
821 diff1.base.duplex = 0;
822 /* advertising and cmd are usually set */
823 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
825 /* We set port to PORT_OTHER */
826 diff2.base.port = PORT_OTHER;
828 return !memcmp(&diff1, &diff2, sizeof(diff1));
831 static void netvsc_init_settings(struct net_device *dev)
833 struct net_device_context *ndc = netdev_priv(dev);
835 ndc->speed = SPEED_UNKNOWN;
836 ndc->duplex = DUPLEX_FULL;
839 static int netvsc_get_link_ksettings(struct net_device *dev,
840 struct ethtool_link_ksettings *cmd)
842 struct net_device_context *ndc = netdev_priv(dev);
844 cmd->base.speed = ndc->speed;
845 cmd->base.duplex = ndc->duplex;
846 cmd->base.port = PORT_OTHER;
851 static int netvsc_set_link_ksettings(struct net_device *dev,
852 const struct ethtool_link_ksettings *cmd)
854 struct net_device_context *ndc = netdev_priv(dev);
857 speed = cmd->base.speed;
858 if (!ethtool_validate_speed(speed) ||
859 !ethtool_validate_duplex(cmd->base.duplex) ||
860 !netvsc_validate_ethtool_ss_cmd(cmd))
864 ndc->duplex = cmd->base.duplex;
869 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
871 struct net_device_context *ndevctx = netdev_priv(ndev);
872 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
873 struct hv_device *hdev = ndevctx->device_ctx;
874 struct netvsc_device_info device_info;
878 if (!nvdev || nvdev->destroy)
881 was_running = netif_running(ndev);
883 ret = netvsc_close(ndev);
888 memset(&device_info, 0, sizeof(device_info));
889 device_info.ring_size = ring_size;
890 device_info.num_chn = nvdev->num_chn;
891 device_info.max_num_vrss_chns = nvdev->num_chn;
893 rndis_filter_device_remove(hdev, nvdev);
895 /* 'nvdev' has been freed in rndis_filter_device_remove() ->
896 * netvsc_device_remove () -> free_netvsc_device().
897 * We mustn't access it before it's re-created in
898 * rndis_filter_device_add() -> netvsc_device_add().
903 rndis_filter_device_add(hdev, &device_info);
906 ret = netvsc_open(ndev);
908 /* We may have missed link change notifications */
909 schedule_delayed_work(&ndevctx->dwork, 0);
914 static void netvsc_get_stats64(struct net_device *net,
915 struct rtnl_link_stats64 *t)
917 struct net_device_context *ndev_ctx = netdev_priv(net);
918 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
924 for (i = 0; i < nvdev->num_chn; i++) {
925 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
926 const struct netvsc_stats *stats;
927 u64 packets, bytes, multicast;
930 stats = &nvchan->tx_stats;
932 start = u64_stats_fetch_begin_irq(&stats->syncp);
933 packets = stats->packets;
934 bytes = stats->bytes;
935 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
937 t->tx_bytes += bytes;
938 t->tx_packets += packets;
940 stats = &nvchan->rx_stats;
942 start = u64_stats_fetch_begin_irq(&stats->syncp);
943 packets = stats->packets;
944 bytes = stats->bytes;
945 multicast = stats->multicast + stats->broadcast;
946 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
948 t->rx_bytes += bytes;
949 t->rx_packets += packets;
950 t->multicast += multicast;
953 t->tx_dropped = net->stats.tx_dropped;
954 t->tx_errors = net->stats.tx_errors;
956 t->rx_dropped = net->stats.rx_dropped;
957 t->rx_errors = net->stats.rx_errors;
960 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
962 struct sockaddr *addr = p;
963 char save_adr[ETH_ALEN];
964 unsigned char save_aatype;
967 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
968 save_aatype = ndev->addr_assign_type;
970 err = eth_mac_addr(ndev, p);
974 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
976 /* roll back to saved MAC */
977 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
978 ndev->addr_assign_type = save_aatype;
984 static const struct {
985 char name[ETH_GSTRING_LEN];
988 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
989 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
990 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
991 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
992 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
995 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
997 /* 4 statistics per queue (rx/tx packets/bytes) */
998 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1000 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1002 struct net_device_context *ndc = netdev_priv(dev);
1003 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1008 switch (string_set) {
1010 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
1016 static void netvsc_get_ethtool_stats(struct net_device *dev,
1017 struct ethtool_stats *stats, u64 *data)
1019 struct net_device_context *ndc = netdev_priv(dev);
1020 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1021 const void *nds = &ndc->eth_stats;
1022 const struct netvsc_stats *qstats;
1030 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1031 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1033 for (j = 0; j < nvdev->num_chn; j++) {
1034 qstats = &nvdev->chan_table[j].tx_stats;
1037 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1038 packets = qstats->packets;
1039 bytes = qstats->bytes;
1040 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1041 data[i++] = packets;
1044 qstats = &nvdev->chan_table[j].rx_stats;
1046 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1047 packets = qstats->packets;
1048 bytes = qstats->bytes;
1049 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1050 data[i++] = packets;
1055 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1057 struct net_device_context *ndc = netdev_priv(dev);
1058 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1065 switch (stringset) {
1067 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1068 memcpy(p + i * ETH_GSTRING_LEN,
1069 netvsc_stats[i].name, ETH_GSTRING_LEN);
1071 p += i * ETH_GSTRING_LEN;
1072 for (i = 0; i < nvdev->num_chn; i++) {
1073 sprintf(p, "tx_queue_%u_packets", i);
1074 p += ETH_GSTRING_LEN;
1075 sprintf(p, "tx_queue_%u_bytes", i);
1076 p += ETH_GSTRING_LEN;
1077 sprintf(p, "rx_queue_%u_packets", i);
1078 p += ETH_GSTRING_LEN;
1079 sprintf(p, "rx_queue_%u_bytes", i);
1080 p += ETH_GSTRING_LEN;
1088 netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1089 struct ethtool_rxnfc *info)
1091 info->data = RXH_IP_SRC | RXH_IP_DST;
1093 switch (info->flow_type) {
1096 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1112 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1115 struct net_device_context *ndc = netdev_priv(dev);
1116 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1121 switch (info->cmd) {
1122 case ETHTOOL_GRXRINGS:
1123 info->data = nvdev->num_chn;
1127 return netvsc_get_rss_hash_opts(nvdev, info);
1132 #ifdef CONFIG_NET_POLL_CONTROLLER
1133 static void netvsc_poll_controller(struct net_device *dev)
1135 struct net_device_context *ndc = netdev_priv(dev);
1136 struct netvsc_device *ndev;
1140 ndev = rcu_dereference(ndc->nvdev);
1142 for (i = 0; i < ndev->num_chn; i++) {
1143 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1145 napi_schedule(&nvchan->napi);
1152 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1154 return NETVSC_HASH_KEYLEN;
1157 static u32 netvsc_rss_indir_size(struct net_device *dev)
1162 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1165 struct net_device_context *ndc = netdev_priv(dev);
1166 struct netvsc_device *ndev = rcu_dereference(ndc->nvdev);
1167 struct rndis_device *rndis_dev;
1174 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1176 rndis_dev = ndev->extension;
1178 for (i = 0; i < ITAB_NUM; i++)
1179 indir[i] = rndis_dev->ind_table[i];
1183 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1188 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1189 const u8 *key, const u8 hfunc)
1191 struct net_device_context *ndc = netdev_priv(dev);
1192 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1193 struct rndis_device *rndis_dev;
1199 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1202 rndis_dev = ndev->extension;
1204 for (i = 0; i < ITAB_NUM; i++)
1205 if (indir[i] >= VRSS_CHANNEL_MAX)
1208 for (i = 0; i < ITAB_NUM; i++)
1209 rndis_dev->ind_table[i] = indir[i];
1216 key = rndis_dev->rss_key;
1219 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1222 static const struct ethtool_ops ethtool_ops = {
1223 .get_drvinfo = netvsc_get_drvinfo,
1224 .get_link = ethtool_op_get_link,
1225 .get_ethtool_stats = netvsc_get_ethtool_stats,
1226 .get_sset_count = netvsc_get_sset_count,
1227 .get_strings = netvsc_get_strings,
1228 .get_channels = netvsc_get_channels,
1229 .set_channels = netvsc_set_channels,
1230 .get_ts_info = ethtool_op_get_ts_info,
1231 .get_rxnfc = netvsc_get_rxnfc,
1232 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1233 .get_rxfh_indir_size = netvsc_rss_indir_size,
1234 .get_rxfh = netvsc_get_rxfh,
1235 .set_rxfh = netvsc_set_rxfh,
1236 .get_link_ksettings = netvsc_get_link_ksettings,
1237 .set_link_ksettings = netvsc_set_link_ksettings,
1240 static const struct net_device_ops device_ops = {
1241 .ndo_open = netvsc_open,
1242 .ndo_stop = netvsc_close,
1243 .ndo_start_xmit = netvsc_start_xmit,
1244 .ndo_set_rx_mode = netvsc_set_multicast_list,
1245 .ndo_change_mtu = netvsc_change_mtu,
1246 .ndo_validate_addr = eth_validate_addr,
1247 .ndo_set_mac_address = netvsc_set_mac_addr,
1248 .ndo_select_queue = netvsc_select_queue,
1249 .ndo_get_stats64 = netvsc_get_stats64,
1250 #ifdef CONFIG_NET_POLL_CONTROLLER
1251 .ndo_poll_controller = netvsc_poll_controller,
1256 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1257 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1258 * present send GARP packet to network peers with netif_notify_peers().
1260 static void netvsc_link_change(struct work_struct *w)
1262 struct net_device_context *ndev_ctx =
1263 container_of(w, struct net_device_context, dwork.work);
1264 struct hv_device *device_obj = ndev_ctx->device_ctx;
1265 struct net_device *net = hv_get_drvdata(device_obj);
1266 struct netvsc_device *net_device;
1267 struct rndis_device *rdev;
1268 struct netvsc_reconfig *event = NULL;
1269 bool notify = false, reschedule = false;
1270 unsigned long flags, next_reconfig, delay;
1272 /* if changes are happening, comeback later */
1273 if (!rtnl_trylock()) {
1274 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1278 net_device = rtnl_dereference(ndev_ctx->nvdev);
1282 rdev = net_device->extension;
1284 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1285 if (time_is_after_jiffies(next_reconfig)) {
1286 /* link_watch only sends one notification with current state
1287 * per second, avoid doing reconfig more frequently. Handle
1290 delay = next_reconfig - jiffies;
1291 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1292 schedule_delayed_work(&ndev_ctx->dwork, delay);
1295 ndev_ctx->last_reconfig = jiffies;
1297 spin_lock_irqsave(&ndev_ctx->lock, flags);
1298 if (!list_empty(&ndev_ctx->reconfig_events)) {
1299 event = list_first_entry(&ndev_ctx->reconfig_events,
1300 struct netvsc_reconfig, list);
1301 list_del(&event->list);
1302 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1304 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1309 switch (event->event) {
1310 /* Only the following events are possible due to the check in
1311 * netvsc_linkstatus_callback()
1313 case RNDIS_STATUS_MEDIA_CONNECT:
1314 if (rdev->link_state) {
1315 rdev->link_state = false;
1316 if (!ndev_ctx->datapath)
1317 netif_carrier_on(net);
1318 netif_tx_wake_all_queues(net);
1324 case RNDIS_STATUS_MEDIA_DISCONNECT:
1325 if (!rdev->link_state) {
1326 rdev->link_state = true;
1327 netif_carrier_off(net);
1328 netif_tx_stop_all_queues(net);
1332 case RNDIS_STATUS_NETWORK_CHANGE:
1333 /* Only makes sense if carrier is present */
1334 if (!rdev->link_state) {
1335 rdev->link_state = true;
1336 netif_carrier_off(net);
1337 netif_tx_stop_all_queues(net);
1338 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1339 spin_lock_irqsave(&ndev_ctx->lock, flags);
1340 list_add(&event->list, &ndev_ctx->reconfig_events);
1341 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1350 netdev_notify_peers(net);
1352 /* link_watch only sends one notification with current state per
1353 * second, handle next reconfig event in 2 seconds.
1356 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1364 static struct net_device *get_netvsc_bymac(const u8 *mac)
1366 struct net_device *dev;
1370 for_each_netdev(&init_net, dev) {
1371 if (dev->netdev_ops != &device_ops)
1372 continue; /* not a netvsc device */
1374 if (ether_addr_equal(mac, dev->perm_addr))
1381 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1383 struct net_device *dev;
1387 for_each_netdev(&init_net, dev) {
1388 struct net_device_context *net_device_ctx;
1390 if (dev->netdev_ops != &device_ops)
1391 continue; /* not a netvsc device */
1393 net_device_ctx = netdev_priv(dev);
1394 if (net_device_ctx->nvdev == NULL)
1395 continue; /* device is removed */
1397 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1398 return dev; /* a match */
1404 static int netvsc_register_vf(struct net_device *vf_netdev)
1406 struct net_device *ndev;
1407 struct net_device_context *net_device_ctx;
1408 struct netvsc_device *netvsc_dev;
1410 if (vf_netdev->addr_len != ETH_ALEN)
1414 * We will use the MAC address to locate the synthetic interface to
1415 * associate with the VF interface. If we don't find a matching
1416 * synthetic interface, move on.
1418 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1422 net_device_ctx = netdev_priv(ndev);
1423 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1424 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1427 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1429 * Take a reference on the module.
1431 try_module_get(THIS_MODULE);
1433 dev_hold(vf_netdev);
1434 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1438 static int netvsc_vf_up(struct net_device *vf_netdev)
1440 struct net_device *ndev;
1441 struct netvsc_device *netvsc_dev;
1442 struct net_device_context *net_device_ctx;
1444 ndev = get_netvsc_byref(vf_netdev);
1448 net_device_ctx = netdev_priv(ndev);
1449 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1451 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
1454 * Open the device before switching data path.
1456 rndis_filter_open(netvsc_dev);
1459 * notify the host to switch the data path.
1461 netvsc_switch_datapath(ndev, true);
1462 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1464 netif_carrier_off(ndev);
1466 /* Now notify peers through VF device. */
1467 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
1472 static int netvsc_vf_down(struct net_device *vf_netdev)
1474 struct net_device *ndev;
1475 struct netvsc_device *netvsc_dev;
1476 struct net_device_context *net_device_ctx;
1478 ndev = get_netvsc_byref(vf_netdev);
1482 net_device_ctx = netdev_priv(ndev);
1483 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1485 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
1486 netvsc_switch_datapath(ndev, false);
1487 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1488 rndis_filter_close(netvsc_dev);
1489 netif_carrier_on(ndev);
1491 /* Now notify peers through netvsc device. */
1492 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
1497 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1499 struct net_device *ndev;
1500 struct net_device_context *net_device_ctx;
1502 ndev = get_netvsc_byref(vf_netdev);
1506 net_device_ctx = netdev_priv(ndev);
1508 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1510 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1512 module_put(THIS_MODULE);
1516 static int netvsc_probe(struct hv_device *dev,
1517 const struct hv_vmbus_device_id *dev_id)
1519 struct net_device *net = NULL;
1520 struct net_device_context *net_device_ctx;
1521 struct netvsc_device_info device_info;
1522 struct netvsc_device *nvdev;
1525 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1530 netif_carrier_off(net);
1532 netvsc_init_settings(net);
1534 net_device_ctx = netdev_priv(net);
1535 net_device_ctx->device_ctx = dev;
1536 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1537 if (netif_msg_probe(net_device_ctx))
1538 netdev_dbg(net, "netvsc msg_enable: %d\n",
1539 net_device_ctx->msg_enable);
1541 hv_set_drvdata(dev, net);
1543 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1545 spin_lock_init(&net_device_ctx->lock);
1546 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1548 net->netdev_ops = &device_ops;
1549 net->ethtool_ops = ðtool_ops;
1550 SET_NETDEV_DEV(net, &dev->device);
1552 /* We always need headroom for rndis header */
1553 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1555 /* Notify the netvsc driver of the new device */
1556 memset(&device_info, 0, sizeof(device_info));
1557 device_info.ring_size = ring_size;
1558 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1559 ret = rndis_filter_device_add(dev, &device_info);
1561 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1563 hv_set_drvdata(dev, NULL);
1566 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1568 /* hw_features computed in rndis_filter_device_add */
1569 net->features = net->hw_features |
1570 NETIF_F_HIGHDMA | NETIF_F_SG |
1571 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1572 net->vlan_features = net->features;
1574 /* RCU not necessary here, device not registered */
1575 nvdev = net_device_ctx->nvdev;
1576 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1577 netif_set_real_num_rx_queues(net, nvdev->num_chn);
1579 /* MTU range: 68 - 1500 or 65521 */
1580 net->min_mtu = NETVSC_MTU_MIN;
1581 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1582 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1584 net->max_mtu = ETH_DATA_LEN;
1586 ret = register_netdev(net);
1588 pr_err("Unable to register netdev.\n");
1589 rndis_filter_device_remove(dev, nvdev);
1596 static int netvsc_remove(struct hv_device *dev)
1598 struct net_device *net;
1599 struct net_device_context *ndev_ctx;
1601 net = hv_get_drvdata(dev);
1604 dev_err(&dev->device, "No net device to remove\n");
1608 ndev_ctx = netdev_priv(net);
1610 netif_device_detach(net);
1612 cancel_delayed_work_sync(&ndev_ctx->dwork);
1615 * Call to the vsc driver to let it know that the device is being
1616 * removed. Also blocks mtu and channel changes.
1619 rndis_filter_device_remove(dev, ndev_ctx->nvdev);
1622 unregister_netdev(net);
1624 hv_set_drvdata(dev, NULL);
1630 static const struct hv_vmbus_device_id id_table[] = {
1636 MODULE_DEVICE_TABLE(vmbus, id_table);
1638 /* The one and only one */
1639 static struct hv_driver netvsc_drv = {
1640 .name = KBUILD_MODNAME,
1641 .id_table = id_table,
1642 .probe = netvsc_probe,
1643 .remove = netvsc_remove,
1647 * On Hyper-V, every VF interface is matched with a corresponding
1648 * synthetic interface. The synthetic interface is presented first
1649 * to the guest. When the corresponding VF instance is registered,
1650 * we will take care of switching the data path.
1652 static int netvsc_netdev_event(struct notifier_block *this,
1653 unsigned long event, void *ptr)
1655 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1657 /* Skip our own events */
1658 if (event_dev->netdev_ops == &device_ops)
1661 /* Avoid non-Ethernet type devices */
1662 if (event_dev->type != ARPHRD_ETHER)
1665 /* Avoid Vlan dev with same MAC registering as VF */
1666 if (is_vlan_dev(event_dev))
1669 /* Avoid Bonding master dev with same MAC registering as VF */
1670 if ((event_dev->priv_flags & IFF_BONDING) &&
1671 (event_dev->flags & IFF_MASTER))
1675 case NETDEV_REGISTER:
1676 return netvsc_register_vf(event_dev);
1677 case NETDEV_UNREGISTER:
1678 return netvsc_unregister_vf(event_dev);
1680 return netvsc_vf_up(event_dev);
1682 return netvsc_vf_down(event_dev);
1688 static struct notifier_block netvsc_netdev_notifier = {
1689 .notifier_call = netvsc_netdev_event,
1692 static void __exit netvsc_drv_exit(void)
1694 unregister_netdevice_notifier(&netvsc_netdev_notifier);
1695 vmbus_driver_unregister(&netvsc_drv);
1698 static int __init netvsc_drv_init(void)
1702 if (ring_size < RING_SIZE_MIN) {
1703 ring_size = RING_SIZE_MIN;
1704 pr_info("Increased ring_size to %d (min allowed)\n",
1707 ret = vmbus_driver_register(&netvsc_drv);
1712 register_netdevice_notifier(&netvsc_netdev_notifier);
1716 MODULE_LICENSE("GPL");
1717 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1719 module_init(netvsc_drv_init);
1720 module_exit(netvsc_drv_exit);