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 #define RING_SIZE_MIN 64
44 #define LINKCHANGE_INT (2 * HZ)
46 static int ring_size = 128;
47 module_param(ring_size, int, S_IRUGO);
48 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
50 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
51 NETIF_MSG_LINK | NETIF_MSG_IFUP |
52 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
55 static int debug = -1;
56 module_param(debug, int, S_IRUGO);
57 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
59 static void netvsc_set_multicast_list(struct net_device *net)
61 struct net_device_context *net_device_ctx = netdev_priv(net);
62 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
64 rndis_filter_update(nvdev);
67 static int netvsc_open(struct net_device *net)
69 struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
70 struct rndis_device *rdev;
73 netif_carrier_off(net);
75 /* Open up the device */
76 ret = rndis_filter_open(nvdev);
78 netdev_err(net, "unable to open device (ret %d).\n", ret);
82 netif_tx_wake_all_queues(net);
84 rdev = nvdev->extension;
85 if (!rdev->link_state)
86 netif_carrier_on(net);
91 static int netvsc_close(struct net_device *net)
93 struct net_device_context *net_device_ctx = netdev_priv(net);
94 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
96 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
97 struct vmbus_channel *chn;
99 netif_tx_disable(net);
101 ret = rndis_filter_close(nvdev);
103 netdev_err(net, "unable to close device (ret %d).\n", ret);
107 /* Ensure pending bytes in ring are read */
110 for (i = 0; i < nvdev->num_chn; i++) {
111 chn = nvdev->chan_table[i].channel;
115 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
121 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
129 if (retry > retry_max || aread == 0)
139 netdev_err(net, "Ring buffer not empty after closing rndis\n");
146 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
149 struct rndis_packet *rndis_pkt;
150 struct rndis_per_packet_info *ppi;
152 rndis_pkt = &msg->msg.pkt;
153 rndis_pkt->data_offset += ppi_size;
155 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
156 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
158 ppi->size = ppi_size;
159 ppi->type = pkt_type;
160 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
162 rndis_pkt->per_pkt_info_len += ppi_size;
167 /* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
168 * hash for non-TCP traffic with only IP numbers.
170 static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
172 struct flow_keys flow;
174 static u32 hashrnd __read_mostly;
176 net_get_random_once(&hashrnd, sizeof(hashrnd));
178 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
181 if (flow.basic.ip_proto == IPPROTO_TCP) {
182 return skb_get_hash(skb);
184 if (flow.basic.n_proto == htons(ETH_P_IP))
185 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
186 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
187 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
191 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
197 static inline int netvsc_get_tx_queue(struct net_device *ndev,
198 struct sk_buff *skb, int old_idx)
200 const struct net_device_context *ndc = netdev_priv(ndev);
201 struct sock *sk = skb->sk;
204 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
205 (VRSS_SEND_TAB_SIZE - 1)];
207 /* If queue index changed record the new value */
208 if (q_idx != old_idx &&
209 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
210 sk_tx_queue_set(sk, q_idx);
216 * Select queue for transmit.
218 * If a valid queue has already been assigned, then use that.
219 * Otherwise compute tx queue based on hash and the send table.
221 * This is basically similar to default (__netdev_pick_tx) with the added step
222 * of using the host send_table when no other queue has been assigned.
224 * TODO support XPS - but get_xps_queue not exported
226 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
227 void *accel_priv, select_queue_fallback_t fallback)
229 unsigned int num_tx_queues = ndev->real_num_tx_queues;
230 int q_idx = sk_tx_queue_get(skb->sk);
232 if (q_idx < 0 || skb->ooo_okay) {
233 /* If forwarding a packet, we use the recorded queue when
234 * available for better cache locality.
236 if (skb_rx_queue_recorded(skb))
237 q_idx = skb_get_rx_queue(skb);
239 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
242 while (unlikely(q_idx >= num_tx_queues))
243 q_idx -= num_tx_queues;
248 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
249 struct hv_page_buffer *pb)
253 /* Deal with compund pages by ignoring unused part
256 page += (offset >> PAGE_SHIFT);
257 offset &= ~PAGE_MASK;
262 bytes = PAGE_SIZE - offset;
265 pb[j].pfn = page_to_pfn(page);
266 pb[j].offset = offset;
272 if (offset == PAGE_SIZE && len) {
282 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
283 struct hv_netvsc_packet *packet,
284 struct hv_page_buffer **page_buf)
286 struct hv_page_buffer *pb = *page_buf;
288 char *data = skb->data;
289 int frags = skb_shinfo(skb)->nr_frags;
292 /* The packet is laid out thus:
293 * 1. hdr: RNDIS header and PPI
295 * 3. skb fragment data
298 slots_used += fill_pg_buf(virt_to_page(hdr),
300 len, &pb[slots_used]);
302 packet->rmsg_size = len;
303 packet->rmsg_pgcnt = slots_used;
305 slots_used += fill_pg_buf(virt_to_page(data),
306 offset_in_page(data),
307 skb_headlen(skb), &pb[slots_used]);
309 for (i = 0; i < frags; i++) {
310 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
312 slots_used += fill_pg_buf(skb_frag_page(frag),
314 skb_frag_size(frag), &pb[slots_used]);
319 static int count_skb_frag_slots(struct sk_buff *skb)
321 int i, frags = skb_shinfo(skb)->nr_frags;
324 for (i = 0; i < frags; i++) {
325 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
326 unsigned long size = skb_frag_size(frag);
327 unsigned long offset = frag->page_offset;
329 /* Skip unused frames from start of page */
330 offset &= ~PAGE_MASK;
331 pages += PFN_UP(offset + size);
336 static int netvsc_get_slots(struct sk_buff *skb)
338 char *data = skb->data;
339 unsigned int offset = offset_in_page(data);
340 unsigned int len = skb_headlen(skb);
344 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
345 frag_slots = count_skb_frag_slots(skb);
346 return slots + frag_slots;
349 static u32 net_checksum_info(struct sk_buff *skb)
351 if (skb->protocol == htons(ETH_P_IP)) {
352 struct iphdr *ip = ip_hdr(skb);
354 if (ip->protocol == IPPROTO_TCP)
355 return TRANSPORT_INFO_IPV4_TCP;
356 else if (ip->protocol == IPPROTO_UDP)
357 return TRANSPORT_INFO_IPV4_UDP;
359 struct ipv6hdr *ip6 = ipv6_hdr(skb);
361 if (ip6->nexthdr == IPPROTO_TCP)
362 return TRANSPORT_INFO_IPV6_TCP;
363 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
364 return TRANSPORT_INFO_IPV6_UDP;
367 return TRANSPORT_INFO_NOT_IP;
370 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
372 struct net_device_context *net_device_ctx = netdev_priv(net);
373 struct hv_netvsc_packet *packet = NULL;
375 unsigned int num_data_pgs;
376 struct rndis_message *rndis_msg;
377 struct rndis_packet *rndis_pkt;
379 struct rndis_per_packet_info *ppi;
381 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
382 struct hv_page_buffer *pb = page_buf;
384 /* We will atmost need two pages to describe the rndis
385 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
386 * of pages in a single packet. If skb is scattered around
387 * more pages we try linearizing it.
390 num_data_pgs = netvsc_get_slots(skb) + 2;
392 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
393 ++net_device_ctx->eth_stats.tx_scattered;
395 if (skb_linearize(skb))
398 num_data_pgs = netvsc_get_slots(skb) + 2;
399 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
400 ++net_device_ctx->eth_stats.tx_too_big;
406 * Place the rndis header in the skb head room and
407 * the skb->cb will be used for hv_netvsc_packet
410 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
414 /* Use the skb control buffer for building up the packet */
415 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
416 FIELD_SIZEOF(struct sk_buff, cb));
417 packet = (struct hv_netvsc_packet *)skb->cb;
419 packet->q_idx = skb_get_queue_mapping(skb);
421 packet->total_data_buflen = skb->len;
422 packet->total_bytes = skb->len;
423 packet->total_packets = 1;
425 rndis_msg = (struct rndis_message *)skb->head;
427 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
429 /* Add the rndis header */
430 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
431 rndis_msg->msg_len = packet->total_data_buflen;
432 rndis_pkt = &rndis_msg->msg.pkt;
433 rndis_pkt->data_offset = sizeof(struct rndis_packet);
434 rndis_pkt->data_len = packet->total_data_buflen;
435 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
437 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
439 hash = skb_get_hash_raw(skb);
440 if (hash != 0 && net->real_num_tx_queues > 1) {
441 rndis_msg_size += NDIS_HASH_PPI_SIZE;
442 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
444 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
447 if (skb_vlan_tag_present(skb)) {
448 struct ndis_pkt_8021q_info *vlan;
450 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
451 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
453 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
455 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
456 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
460 if (skb_is_gso(skb)) {
461 struct ndis_tcp_lso_info *lso_info;
463 rndis_msg_size += NDIS_LSO_PPI_SIZE;
464 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
465 TCP_LARGESEND_PKTINFO);
467 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
470 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
471 if (skb->protocol == htons(ETH_P_IP)) {
472 lso_info->lso_v2_transmit.ip_version =
473 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
474 ip_hdr(skb)->tot_len = 0;
475 ip_hdr(skb)->check = 0;
476 tcp_hdr(skb)->check =
477 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
478 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
480 lso_info->lso_v2_transmit.ip_version =
481 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
482 ipv6_hdr(skb)->payload_len = 0;
483 tcp_hdr(skb)->check =
484 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
485 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
487 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
488 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
489 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
490 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
491 struct ndis_tcp_ip_checksum_info *csum_info;
493 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
494 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
495 TCPIP_CHKSUM_PKTINFO);
497 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
500 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
502 if (skb->protocol == htons(ETH_P_IP)) {
503 csum_info->transmit.is_ipv4 = 1;
505 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
506 csum_info->transmit.tcp_checksum = 1;
508 csum_info->transmit.udp_checksum = 1;
510 csum_info->transmit.is_ipv6 = 1;
512 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
513 csum_info->transmit.tcp_checksum = 1;
515 csum_info->transmit.udp_checksum = 1;
518 /* Can't do offload of this type of checksum */
519 if (skb_checksum_help(skb))
524 /* Start filling in the page buffers with the rndis hdr */
525 rndis_msg->msg_len += rndis_msg_size;
526 packet->total_data_buflen = rndis_msg->msg_len;
527 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
530 /* timestamp packet in software */
531 skb_tx_timestamp(skb);
532 ret = netvsc_send(net_device_ctx->device_ctx, packet,
533 rndis_msg, &pb, skb);
534 if (likely(ret == 0))
537 if (ret == -EAGAIN) {
538 ++net_device_ctx->eth_stats.tx_busy;
539 return NETDEV_TX_BUSY;
543 ++net_device_ctx->eth_stats.tx_no_space;
546 dev_kfree_skb_any(skb);
547 net->stats.tx_dropped++;
552 ++net_device_ctx->eth_stats.tx_no_memory;
556 * netvsc_linkstatus_callback - Link up/down notification
558 void netvsc_linkstatus_callback(struct hv_device *device_obj,
559 struct rndis_message *resp)
561 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
562 struct net_device *net;
563 struct net_device_context *ndev_ctx;
564 struct netvsc_reconfig *event;
567 net = hv_get_drvdata(device_obj);
572 ndev_ctx = netdev_priv(net);
574 /* Update the physical link speed when changing to another vSwitch */
575 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
578 speed = *(u32 *)((void *)indicate + indicate->
579 status_buf_offset) / 10000;
580 ndev_ctx->speed = speed;
584 /* Handle these link change statuses below */
585 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
586 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
587 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
590 if (net->reg_state != NETREG_REGISTERED)
593 event = kzalloc(sizeof(*event), GFP_ATOMIC);
596 event->event = indicate->status;
598 spin_lock_irqsave(&ndev_ctx->lock, flags);
599 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
600 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
602 schedule_delayed_work(&ndev_ctx->dwork, 0);
605 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
606 struct napi_struct *napi,
607 const struct ndis_tcp_ip_checksum_info *csum_info,
608 const struct ndis_pkt_8021q_info *vlan,
609 void *data, u32 buflen)
613 skb = napi_alloc_skb(napi, buflen);
618 * Copy to skb. This copy is needed here since the memory pointed by
619 * hv_netvsc_packet cannot be deallocated
621 memcpy(skb_put(skb, buflen), data, buflen);
623 skb->protocol = eth_type_trans(skb, net);
625 /* skb is already created with CHECKSUM_NONE */
626 skb_checksum_none_assert(skb);
629 * In Linux, the IP checksum is always checked.
630 * Do L4 checksum offload if enabled and present.
632 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
633 if (csum_info->receive.tcp_checksum_succeeded ||
634 csum_info->receive.udp_checksum_succeeded)
635 skb->ip_summed = CHECKSUM_UNNECESSARY;
639 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
641 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
649 * netvsc_recv_callback - Callback when we receive a packet from the
650 * "wire" on the specified device.
652 int netvsc_recv_callback(struct net_device *net,
653 struct vmbus_channel *channel,
655 const struct ndis_tcp_ip_checksum_info *csum_info,
656 const struct ndis_pkt_8021q_info *vlan)
658 struct net_device_context *net_device_ctx = netdev_priv(net);
659 struct netvsc_device *net_device;
660 u16 q_idx = channel->offermsg.offer.sub_channel_index;
661 struct netvsc_channel *nvchan;
662 struct net_device *vf_netdev;
664 struct netvsc_stats *rx_stats;
666 if (net->reg_state != NETREG_REGISTERED)
667 return NVSP_STAT_FAIL;
670 * If necessary, inject this packet into the VF interface.
671 * On Hyper-V, multicast and brodcast packets are only delivered
672 * to the synthetic interface (after subjecting these to
673 * policy filters on the host). Deliver these via the VF
674 * interface in the guest.
677 net_device = rcu_dereference(net_device_ctx->nvdev);
678 if (unlikely(!net_device))
681 nvchan = &net_device->chan_table[q_idx];
682 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
683 if (vf_netdev && (vf_netdev->flags & IFF_UP))
686 /* Allocate a skb - TODO direct I/O to pages? */
687 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
688 csum_info, vlan, data, len);
689 if (unlikely(!skb)) {
691 ++net->stats.rx_dropped;
693 return NVSP_STAT_FAIL;
696 if (net != vf_netdev)
697 skb_record_rx_queue(skb, q_idx);
700 * Even if injecting the packet, record the statistics
701 * on the synthetic device because modifying the VF device
702 * statistics will not work correctly.
704 rx_stats = &nvchan->rx_stats;
705 u64_stats_update_begin(&rx_stats->syncp);
707 rx_stats->bytes += len;
709 if (skb->pkt_type == PACKET_BROADCAST)
710 ++rx_stats->broadcast;
711 else if (skb->pkt_type == PACKET_MULTICAST)
712 ++rx_stats->multicast;
713 u64_stats_update_end(&rx_stats->syncp);
715 napi_gro_receive(&nvchan->napi, skb);
721 static void netvsc_get_drvinfo(struct net_device *net,
722 struct ethtool_drvinfo *info)
724 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
725 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
728 static void netvsc_get_channels(struct net_device *net,
729 struct ethtool_channels *channel)
731 struct net_device_context *net_device_ctx = netdev_priv(net);
732 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
735 channel->max_combined = nvdev->max_chn;
736 channel->combined_count = nvdev->num_chn;
740 static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
743 struct netvsc_device_info device_info;
746 memset(&device_info, 0, sizeof(device_info));
747 device_info.num_chn = num_chn;
748 device_info.ring_size = ring_size;
749 device_info.max_num_vrss_chns = num_chn;
751 ret = rndis_filter_device_add(dev, &device_info);
755 ret = netif_set_real_num_tx_queues(net, num_chn);
759 ret = netif_set_real_num_rx_queues(net, num_chn);
764 static int netvsc_set_channels(struct net_device *net,
765 struct ethtool_channels *channels)
767 struct net_device_context *net_device_ctx = netdev_priv(net);
768 struct hv_device *dev = net_device_ctx->device_ctx;
769 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
770 unsigned int count = channels->combined_count;
774 /* We do not support separate count for rx, tx, or other */
776 channels->rx_count || channels->tx_count || channels->other_count)
779 if (count > net->num_tx_queues || count > net->num_rx_queues)
782 if (!nvdev || nvdev->destroy)
785 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
788 if (count > nvdev->max_chn)
791 was_running = netif_running(net);
793 ret = netvsc_close(net);
798 rndis_filter_device_remove(dev, nvdev);
800 ret = netvsc_set_queues(net, dev, count);
802 nvdev->num_chn = count;
804 netvsc_set_queues(net, dev, nvdev->num_chn);
807 ret = netvsc_open(net);
809 /* We may have missed link change notifications */
810 schedule_delayed_work(&net_device_ctx->dwork, 0);
816 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
818 struct ethtool_link_ksettings diff1 = *cmd;
819 struct ethtool_link_ksettings diff2 = {};
821 diff1.base.speed = 0;
822 diff1.base.duplex = 0;
823 /* advertising and cmd are usually set */
824 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
826 /* We set port to PORT_OTHER */
827 diff2.base.port = PORT_OTHER;
829 return !memcmp(&diff1, &diff2, sizeof(diff1));
832 static void netvsc_init_settings(struct net_device *dev)
834 struct net_device_context *ndc = netdev_priv(dev);
836 ndc->speed = SPEED_UNKNOWN;
837 ndc->duplex = DUPLEX_FULL;
840 static int netvsc_get_link_ksettings(struct net_device *dev,
841 struct ethtool_link_ksettings *cmd)
843 struct net_device_context *ndc = netdev_priv(dev);
845 cmd->base.speed = ndc->speed;
846 cmd->base.duplex = ndc->duplex;
847 cmd->base.port = PORT_OTHER;
852 static int netvsc_set_link_ksettings(struct net_device *dev,
853 const struct ethtool_link_ksettings *cmd)
855 struct net_device_context *ndc = netdev_priv(dev);
858 speed = cmd->base.speed;
859 if (!ethtool_validate_speed(speed) ||
860 !ethtool_validate_duplex(cmd->base.duplex) ||
861 !netvsc_validate_ethtool_ss_cmd(cmd))
865 ndc->duplex = cmd->base.duplex;
870 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
872 struct net_device_context *ndevctx = netdev_priv(ndev);
873 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
874 struct hv_device *hdev = ndevctx->device_ctx;
875 struct netvsc_device_info device_info;
879 if (!nvdev || nvdev->destroy)
882 was_running = netif_running(ndev);
884 ret = netvsc_close(ndev);
889 memset(&device_info, 0, sizeof(device_info));
890 device_info.ring_size = ring_size;
891 device_info.num_chn = nvdev->num_chn;
892 device_info.max_num_vrss_chns = nvdev->num_chn;
894 rndis_filter_device_remove(hdev, nvdev);
896 /* 'nvdev' has been freed in rndis_filter_device_remove() ->
897 * netvsc_device_remove () -> free_netvsc_device().
898 * We mustn't access it before it's re-created in
899 * rndis_filter_device_add() -> netvsc_device_add().
904 rndis_filter_device_add(hdev, &device_info);
907 ret = netvsc_open(ndev);
909 /* We may have missed link change notifications */
910 schedule_delayed_work(&ndevctx->dwork, 0);
915 static void netvsc_get_stats64(struct net_device *net,
916 struct rtnl_link_stats64 *t)
918 struct net_device_context *ndev_ctx = netdev_priv(net);
919 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
925 for (i = 0; i < nvdev->num_chn; i++) {
926 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
927 const struct netvsc_stats *stats;
928 u64 packets, bytes, multicast;
931 stats = &nvchan->tx_stats;
933 start = u64_stats_fetch_begin_irq(&stats->syncp);
934 packets = stats->packets;
935 bytes = stats->bytes;
936 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
938 t->tx_bytes += bytes;
939 t->tx_packets += packets;
941 stats = &nvchan->rx_stats;
943 start = u64_stats_fetch_begin_irq(&stats->syncp);
944 packets = stats->packets;
945 bytes = stats->bytes;
946 multicast = stats->multicast + stats->broadcast;
947 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
949 t->rx_bytes += bytes;
950 t->rx_packets += packets;
951 t->multicast += multicast;
954 t->tx_dropped = net->stats.tx_dropped;
955 t->tx_errors = net->stats.tx_errors;
957 t->rx_dropped = net->stats.rx_dropped;
958 t->rx_errors = net->stats.rx_errors;
961 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
963 struct sockaddr *addr = p;
964 char save_adr[ETH_ALEN];
965 unsigned char save_aatype;
968 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
969 save_aatype = ndev->addr_assign_type;
971 err = eth_mac_addr(ndev, p);
975 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
977 /* roll back to saved MAC */
978 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
979 ndev->addr_assign_type = save_aatype;
985 static const struct {
986 char name[ETH_GSTRING_LEN];
989 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
990 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
991 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
992 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
993 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
996 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
998 /* 4 statistics per queue (rx/tx packets/bytes) */
999 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1001 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1003 struct net_device_context *ndc = netdev_priv(dev);
1004 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1009 switch (string_set) {
1011 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
1017 static void netvsc_get_ethtool_stats(struct net_device *dev,
1018 struct ethtool_stats *stats, u64 *data)
1020 struct net_device_context *ndc = netdev_priv(dev);
1021 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1022 const void *nds = &ndc->eth_stats;
1023 const struct netvsc_stats *qstats;
1031 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1032 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1034 for (j = 0; j < nvdev->num_chn; j++) {
1035 qstats = &nvdev->chan_table[j].tx_stats;
1038 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1039 packets = qstats->packets;
1040 bytes = qstats->bytes;
1041 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1042 data[i++] = packets;
1045 qstats = &nvdev->chan_table[j].rx_stats;
1047 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1048 packets = qstats->packets;
1049 bytes = qstats->bytes;
1050 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1051 data[i++] = packets;
1056 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1058 struct net_device_context *ndc = netdev_priv(dev);
1059 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1066 switch (stringset) {
1068 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1069 memcpy(p + i * ETH_GSTRING_LEN,
1070 netvsc_stats[i].name, ETH_GSTRING_LEN);
1072 p += i * ETH_GSTRING_LEN;
1073 for (i = 0; i < nvdev->num_chn; i++) {
1074 sprintf(p, "tx_queue_%u_packets", i);
1075 p += ETH_GSTRING_LEN;
1076 sprintf(p, "tx_queue_%u_bytes", i);
1077 p += ETH_GSTRING_LEN;
1078 sprintf(p, "rx_queue_%u_packets", i);
1079 p += ETH_GSTRING_LEN;
1080 sprintf(p, "rx_queue_%u_bytes", i);
1081 p += ETH_GSTRING_LEN;
1089 netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1090 struct ethtool_rxnfc *info)
1092 info->data = RXH_IP_SRC | RXH_IP_DST;
1094 switch (info->flow_type) {
1097 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1113 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1116 struct net_device_context *ndc = netdev_priv(dev);
1117 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1122 switch (info->cmd) {
1123 case ETHTOOL_GRXRINGS:
1124 info->data = nvdev->num_chn;
1128 return netvsc_get_rss_hash_opts(nvdev, info);
1133 #ifdef CONFIG_NET_POLL_CONTROLLER
1134 static void netvsc_poll_controller(struct net_device *dev)
1136 struct net_device_context *ndc = netdev_priv(dev);
1137 struct netvsc_device *ndev;
1141 ndev = rcu_dereference(ndc->nvdev);
1143 for (i = 0; i < ndev->num_chn; i++) {
1144 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1146 napi_schedule(&nvchan->napi);
1153 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1155 return NETVSC_HASH_KEYLEN;
1158 static u32 netvsc_rss_indir_size(struct net_device *dev)
1163 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1166 struct net_device_context *ndc = netdev_priv(dev);
1167 struct netvsc_device *ndev = rcu_dereference(ndc->nvdev);
1168 struct rndis_device *rndis_dev;
1175 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1177 rndis_dev = ndev->extension;
1179 for (i = 0; i < ITAB_NUM; i++)
1180 indir[i] = rndis_dev->ind_table[i];
1184 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1189 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1190 const u8 *key, const u8 hfunc)
1192 struct net_device_context *ndc = netdev_priv(dev);
1193 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1194 struct rndis_device *rndis_dev;
1200 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1203 rndis_dev = ndev->extension;
1205 for (i = 0; i < ITAB_NUM; i++)
1206 if (indir[i] >= dev->num_rx_queues)
1209 for (i = 0; i < ITAB_NUM; i++)
1210 rndis_dev->ind_table[i] = indir[i];
1217 key = rndis_dev->rss_key;
1220 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1223 static const struct ethtool_ops ethtool_ops = {
1224 .get_drvinfo = netvsc_get_drvinfo,
1225 .get_link = ethtool_op_get_link,
1226 .get_ethtool_stats = netvsc_get_ethtool_stats,
1227 .get_sset_count = netvsc_get_sset_count,
1228 .get_strings = netvsc_get_strings,
1229 .get_channels = netvsc_get_channels,
1230 .set_channels = netvsc_set_channels,
1231 .get_ts_info = ethtool_op_get_ts_info,
1232 .get_rxnfc = netvsc_get_rxnfc,
1233 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1234 .get_rxfh_indir_size = netvsc_rss_indir_size,
1235 .get_rxfh = netvsc_get_rxfh,
1236 .set_rxfh = netvsc_set_rxfh,
1237 .get_link_ksettings = netvsc_get_link_ksettings,
1238 .set_link_ksettings = netvsc_set_link_ksettings,
1241 static const struct net_device_ops device_ops = {
1242 .ndo_open = netvsc_open,
1243 .ndo_stop = netvsc_close,
1244 .ndo_start_xmit = netvsc_start_xmit,
1245 .ndo_set_rx_mode = netvsc_set_multicast_list,
1246 .ndo_change_mtu = netvsc_change_mtu,
1247 .ndo_validate_addr = eth_validate_addr,
1248 .ndo_set_mac_address = netvsc_set_mac_addr,
1249 .ndo_select_queue = netvsc_select_queue,
1250 .ndo_get_stats64 = netvsc_get_stats64,
1251 #ifdef CONFIG_NET_POLL_CONTROLLER
1252 .ndo_poll_controller = netvsc_poll_controller,
1257 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1258 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1259 * present send GARP packet to network peers with netif_notify_peers().
1261 static void netvsc_link_change(struct work_struct *w)
1263 struct net_device_context *ndev_ctx =
1264 container_of(w, struct net_device_context, dwork.work);
1265 struct hv_device *device_obj = ndev_ctx->device_ctx;
1266 struct net_device *net = hv_get_drvdata(device_obj);
1267 struct netvsc_device *net_device;
1268 struct rndis_device *rdev;
1269 struct netvsc_reconfig *event = NULL;
1270 bool notify = false, reschedule = false;
1271 unsigned long flags, next_reconfig, delay;
1274 net_device = rtnl_dereference(ndev_ctx->nvdev);
1278 rdev = net_device->extension;
1280 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1281 if (time_is_after_jiffies(next_reconfig)) {
1282 /* link_watch only sends one notification with current state
1283 * per second, avoid doing reconfig more frequently. Handle
1286 delay = next_reconfig - jiffies;
1287 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1288 schedule_delayed_work(&ndev_ctx->dwork, delay);
1291 ndev_ctx->last_reconfig = jiffies;
1293 spin_lock_irqsave(&ndev_ctx->lock, flags);
1294 if (!list_empty(&ndev_ctx->reconfig_events)) {
1295 event = list_first_entry(&ndev_ctx->reconfig_events,
1296 struct netvsc_reconfig, list);
1297 list_del(&event->list);
1298 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1300 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1305 switch (event->event) {
1306 /* Only the following events are possible due to the check in
1307 * netvsc_linkstatus_callback()
1309 case RNDIS_STATUS_MEDIA_CONNECT:
1310 if (rdev->link_state) {
1311 rdev->link_state = false;
1312 netif_carrier_on(net);
1313 netif_tx_wake_all_queues(net);
1319 case RNDIS_STATUS_MEDIA_DISCONNECT:
1320 if (!rdev->link_state) {
1321 rdev->link_state = true;
1322 netif_carrier_off(net);
1323 netif_tx_stop_all_queues(net);
1327 case RNDIS_STATUS_NETWORK_CHANGE:
1328 /* Only makes sense if carrier is present */
1329 if (!rdev->link_state) {
1330 rdev->link_state = true;
1331 netif_carrier_off(net);
1332 netif_tx_stop_all_queues(net);
1333 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1334 spin_lock_irqsave(&ndev_ctx->lock, flags);
1335 list_add(&event->list, &ndev_ctx->reconfig_events);
1336 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1345 netdev_notify_peers(net);
1347 /* link_watch only sends one notification with current state per
1348 * second, handle next reconfig event in 2 seconds.
1351 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1359 static struct net_device *get_netvsc_bymac(const u8 *mac)
1361 struct net_device *dev;
1365 for_each_netdev(&init_net, dev) {
1366 if (dev->netdev_ops != &device_ops)
1367 continue; /* not a netvsc device */
1369 if (ether_addr_equal(mac, dev->perm_addr))
1376 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1378 struct net_device *dev;
1382 for_each_netdev(&init_net, dev) {
1383 struct net_device_context *net_device_ctx;
1385 if (dev->netdev_ops != &device_ops)
1386 continue; /* not a netvsc device */
1388 net_device_ctx = netdev_priv(dev);
1389 if (net_device_ctx->nvdev == NULL)
1390 continue; /* device is removed */
1392 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1393 return dev; /* a match */
1399 static int netvsc_register_vf(struct net_device *vf_netdev)
1401 struct net_device *ndev;
1402 struct net_device_context *net_device_ctx;
1403 struct netvsc_device *netvsc_dev;
1405 if (vf_netdev->addr_len != ETH_ALEN)
1409 * We will use the MAC address to locate the synthetic interface to
1410 * associate with the VF interface. If we don't find a matching
1411 * synthetic interface, move on.
1413 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1417 net_device_ctx = netdev_priv(ndev);
1418 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1419 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1422 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1424 * Take a reference on the module.
1426 try_module_get(THIS_MODULE);
1428 dev_hold(vf_netdev);
1429 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1433 static int netvsc_vf_up(struct net_device *vf_netdev)
1435 struct net_device *ndev;
1436 struct netvsc_device *netvsc_dev;
1437 struct net_device_context *net_device_ctx;
1439 ndev = get_netvsc_byref(vf_netdev);
1443 net_device_ctx = netdev_priv(ndev);
1444 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1446 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
1449 * Open the device before switching data path.
1451 rndis_filter_open(netvsc_dev);
1454 * notify the host to switch the data path.
1456 netvsc_switch_datapath(ndev, true);
1457 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1459 netif_carrier_off(ndev);
1461 /* Now notify peers through VF device. */
1462 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
1467 static int netvsc_vf_down(struct net_device *vf_netdev)
1469 struct net_device *ndev;
1470 struct netvsc_device *netvsc_dev;
1471 struct net_device_context *net_device_ctx;
1473 ndev = get_netvsc_byref(vf_netdev);
1477 net_device_ctx = netdev_priv(ndev);
1478 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1480 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
1481 netvsc_switch_datapath(ndev, false);
1482 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1483 rndis_filter_close(netvsc_dev);
1484 netif_carrier_on(ndev);
1486 /* Now notify peers through netvsc device. */
1487 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
1492 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1494 struct net_device *ndev;
1495 struct net_device_context *net_device_ctx;
1497 ndev = get_netvsc_byref(vf_netdev);
1501 net_device_ctx = netdev_priv(ndev);
1503 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1505 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1507 module_put(THIS_MODULE);
1511 static int netvsc_probe(struct hv_device *dev,
1512 const struct hv_vmbus_device_id *dev_id)
1514 struct net_device *net = NULL;
1515 struct net_device_context *net_device_ctx;
1516 struct netvsc_device_info device_info;
1517 struct netvsc_device *nvdev;
1520 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1525 netif_carrier_off(net);
1527 netvsc_init_settings(net);
1529 net_device_ctx = netdev_priv(net);
1530 net_device_ctx->device_ctx = dev;
1531 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1532 if (netif_msg_probe(net_device_ctx))
1533 netdev_dbg(net, "netvsc msg_enable: %d\n",
1534 net_device_ctx->msg_enable);
1536 hv_set_drvdata(dev, net);
1538 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1540 spin_lock_init(&net_device_ctx->lock);
1541 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1543 net->netdev_ops = &device_ops;
1544 net->ethtool_ops = ðtool_ops;
1545 SET_NETDEV_DEV(net, &dev->device);
1547 /* We always need headroom for rndis header */
1548 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1550 /* Notify the netvsc driver of the new device */
1551 memset(&device_info, 0, sizeof(device_info));
1552 device_info.ring_size = ring_size;
1553 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1554 ret = rndis_filter_device_add(dev, &device_info);
1556 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1558 hv_set_drvdata(dev, NULL);
1561 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1563 /* hw_features computed in rndis_filter_device_add */
1564 net->features = net->hw_features |
1565 NETIF_F_HIGHDMA | NETIF_F_SG |
1566 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1567 net->vlan_features = net->features;
1569 /* RCU not necessary here, device not registered */
1570 nvdev = net_device_ctx->nvdev;
1571 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1572 netif_set_real_num_rx_queues(net, nvdev->num_chn);
1574 /* MTU range: 68 - 1500 or 65521 */
1575 net->min_mtu = NETVSC_MTU_MIN;
1576 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1577 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1579 net->max_mtu = ETH_DATA_LEN;
1581 ret = register_netdev(net);
1583 pr_err("Unable to register netdev.\n");
1584 rndis_filter_device_remove(dev, nvdev);
1591 static int netvsc_remove(struct hv_device *dev)
1593 struct net_device *net;
1594 struct net_device_context *ndev_ctx;
1596 net = hv_get_drvdata(dev);
1599 dev_err(&dev->device, "No net device to remove\n");
1603 ndev_ctx = netdev_priv(net);
1605 netif_device_detach(net);
1607 cancel_delayed_work_sync(&ndev_ctx->dwork);
1610 * Call to the vsc driver to let it know that the device is being
1611 * removed. Also blocks mtu and channel changes.
1614 rndis_filter_device_remove(dev, ndev_ctx->nvdev);
1617 unregister_netdev(net);
1619 hv_set_drvdata(dev, NULL);
1625 static const struct hv_vmbus_device_id id_table[] = {
1631 MODULE_DEVICE_TABLE(vmbus, id_table);
1633 /* The one and only one */
1634 static struct hv_driver netvsc_drv = {
1635 .name = KBUILD_MODNAME,
1636 .id_table = id_table,
1637 .probe = netvsc_probe,
1638 .remove = netvsc_remove,
1642 * On Hyper-V, every VF interface is matched with a corresponding
1643 * synthetic interface. The synthetic interface is presented first
1644 * to the guest. When the corresponding VF instance is registered,
1645 * we will take care of switching the data path.
1647 static int netvsc_netdev_event(struct notifier_block *this,
1648 unsigned long event, void *ptr)
1650 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1652 /* Skip our own events */
1653 if (event_dev->netdev_ops == &device_ops)
1656 /* Avoid non-Ethernet type devices */
1657 if (event_dev->type != ARPHRD_ETHER)
1660 /* Avoid Vlan dev with same MAC registering as VF */
1661 if (is_vlan_dev(event_dev))
1664 /* Avoid Bonding master dev with same MAC registering as VF */
1665 if ((event_dev->priv_flags & IFF_BONDING) &&
1666 (event_dev->flags & IFF_MASTER))
1670 case NETDEV_REGISTER:
1671 return netvsc_register_vf(event_dev);
1672 case NETDEV_UNREGISTER:
1673 return netvsc_unregister_vf(event_dev);
1675 return netvsc_vf_up(event_dev);
1677 return netvsc_vf_down(event_dev);
1683 static struct notifier_block netvsc_netdev_notifier = {
1684 .notifier_call = netvsc_netdev_event,
1687 static void __exit netvsc_drv_exit(void)
1689 unregister_netdevice_notifier(&netvsc_netdev_notifier);
1690 vmbus_driver_unregister(&netvsc_drv);
1693 static int __init netvsc_drv_init(void)
1697 if (ring_size < RING_SIZE_MIN) {
1698 ring_size = RING_SIZE_MIN;
1699 pr_info("Increased ring_size to %d (min allowed)\n",
1702 ret = vmbus_driver_register(&netvsc_drv);
1707 register_netdevice_notifier(&netvsc_netdev_notifier);
1711 MODULE_LICENSE("GPL");
1712 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1714 module_init(netvsc_drv_init);
1715 module_exit(netvsc_drv_exit);