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>
36 #include <linux/rtnetlink.h>
37 #include <linux/netpoll.h>
38 #include <linux/reciprocal_div.h>
41 #include <net/route.h>
43 #include <net/pkt_sched.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
47 #include "hyperv_net.h"
49 #define RING_SIZE_MIN 64
51 #define LINKCHANGE_INT (2 * HZ)
52 #define VF_TAKEOVER_INT (HZ / 10)
54 static unsigned int ring_size __ro_after_init = 128;
55 module_param(ring_size, uint, S_IRUGO);
56 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
57 unsigned int netvsc_ring_bytes __ro_after_init;
58 struct reciprocal_value netvsc_ring_reciprocal __ro_after_init;
60 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
61 NETIF_MSG_LINK | NETIF_MSG_IFUP |
62 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
65 static int debug = -1;
66 module_param(debug, int, S_IRUGO);
67 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
69 static void netvsc_set_multicast_list(struct net_device *net)
71 struct net_device_context *net_device_ctx = netdev_priv(net);
72 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
74 rndis_filter_update(nvdev);
77 static int netvsc_open(struct net_device *net)
79 struct net_device_context *ndev_ctx = netdev_priv(net);
80 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
81 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
82 struct rndis_device *rdev;
85 netif_carrier_off(net);
87 /* Open up the device */
88 ret = rndis_filter_open(nvdev);
90 netdev_err(net, "unable to open device (ret %d).\n", ret);
94 rdev = nvdev->extension;
95 if (!rdev->link_state) {
96 netif_carrier_on(net);
97 netif_tx_wake_all_queues(net);
101 /* Setting synthetic device up transparently sets
102 * slave as up. If open fails, then slave will be
103 * still be offline (and not used).
105 ret = dev_open(vf_netdev);
108 "unable to open slave: %s: %d\n",
109 vf_netdev->name, ret);
114 static int netvsc_close(struct net_device *net)
116 struct net_device_context *net_device_ctx = netdev_priv(net);
117 struct net_device *vf_netdev
118 = rtnl_dereference(net_device_ctx->vf_netdev);
119 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
121 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
122 struct vmbus_channel *chn;
124 netif_tx_disable(net);
126 /* No need to close rndis filter if it is removed already */
130 ret = rndis_filter_close(nvdev);
132 netdev_err(net, "unable to close device (ret %d).\n", ret);
136 /* Ensure pending bytes in ring are read */
139 for (i = 0; i < nvdev->num_chn; i++) {
140 chn = nvdev->chan_table[i].channel;
144 aread = hv_get_bytes_to_read(&chn->inbound);
148 aread = hv_get_bytes_to_read(&chn->outbound);
154 if (retry > retry_max || aread == 0)
164 netdev_err(net, "Ring buffer not empty after closing rndis\n");
170 dev_close(vf_netdev);
175 static inline void *init_ppi_data(struct rndis_message *msg,
176 u32 ppi_size, u32 pkt_type)
178 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
179 struct rndis_per_packet_info *ppi;
181 rndis_pkt->data_offset += ppi_size;
182 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
183 + rndis_pkt->per_pkt_info_len;
185 ppi->size = ppi_size;
186 ppi->type = pkt_type;
187 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
189 rndis_pkt->per_pkt_info_len += ppi_size;
194 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
195 * packets. We can use ethtool to change UDP hash level when necessary.
197 static inline u32 netvsc_get_hash(
199 const struct net_device_context *ndc)
201 struct flow_keys flow;
202 u32 hash, pkt_proto = 0;
203 static u32 hashrnd __read_mostly;
205 net_get_random_once(&hashrnd, sizeof(hashrnd));
207 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
210 switch (flow.basic.ip_proto) {
212 if (flow.basic.n_proto == htons(ETH_P_IP))
213 pkt_proto = HV_TCP4_L4HASH;
214 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
215 pkt_proto = HV_TCP6_L4HASH;
220 if (flow.basic.n_proto == htons(ETH_P_IP))
221 pkt_proto = HV_UDP4_L4HASH;
222 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
223 pkt_proto = HV_UDP6_L4HASH;
228 if (pkt_proto & ndc->l4_hash) {
229 return skb_get_hash(skb);
231 if (flow.basic.n_proto == htons(ETH_P_IP))
232 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
233 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
234 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
238 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
244 static inline int netvsc_get_tx_queue(struct net_device *ndev,
245 struct sk_buff *skb, int old_idx)
247 const struct net_device_context *ndc = netdev_priv(ndev);
248 struct sock *sk = skb->sk;
251 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
252 (VRSS_SEND_TAB_SIZE - 1)];
254 /* If queue index changed record the new value */
255 if (q_idx != old_idx &&
256 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
257 sk_tx_queue_set(sk, q_idx);
263 * Select queue for transmit.
265 * If a valid queue has already been assigned, then use that.
266 * Otherwise compute tx queue based on hash and the send table.
268 * This is basically similar to default (__netdev_pick_tx) with the added step
269 * of using the host send_table when no other queue has been assigned.
271 * TODO support XPS - but get_xps_queue not exported
273 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
275 int q_idx = sk_tx_queue_get(skb->sk);
277 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
278 /* If forwarding a packet, we use the recorded queue when
279 * available for better cache locality.
281 if (skb_rx_queue_recorded(skb))
282 q_idx = skb_get_rx_queue(skb);
284 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
290 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
292 select_queue_fallback_t fallback)
294 struct net_device_context *ndc = netdev_priv(ndev);
295 struct net_device *vf_netdev;
299 vf_netdev = rcu_dereference(ndc->vf_netdev);
301 const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
303 if (vf_ops->ndo_select_queue)
304 txq = vf_ops->ndo_select_queue(vf_netdev, skb,
305 accel_priv, fallback);
307 txq = fallback(vf_netdev, skb);
309 /* Record the queue selected by VF so that it can be
310 * used for common case where VF has more queues than
311 * the synthetic device.
313 qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
315 txq = netvsc_pick_tx(ndev, skb);
319 while (unlikely(txq >= ndev->real_num_tx_queues))
320 txq -= ndev->real_num_tx_queues;
325 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
326 struct hv_page_buffer *pb)
330 /* Deal with compund pages by ignoring unused part
333 page += (offset >> PAGE_SHIFT);
334 offset &= ~PAGE_MASK;
339 bytes = PAGE_SIZE - offset;
342 pb[j].pfn = page_to_pfn(page);
343 pb[j].offset = offset;
349 if (offset == PAGE_SIZE && len) {
359 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
360 struct hv_netvsc_packet *packet,
361 struct hv_page_buffer *pb)
364 char *data = skb->data;
365 int frags = skb_shinfo(skb)->nr_frags;
368 /* The packet is laid out thus:
369 * 1. hdr: RNDIS header and PPI
371 * 3. skb fragment data
373 slots_used += fill_pg_buf(virt_to_page(hdr),
375 len, &pb[slots_used]);
377 packet->rmsg_size = len;
378 packet->rmsg_pgcnt = slots_used;
380 slots_used += fill_pg_buf(virt_to_page(data),
381 offset_in_page(data),
382 skb_headlen(skb), &pb[slots_used]);
384 for (i = 0; i < frags; i++) {
385 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
387 slots_used += fill_pg_buf(skb_frag_page(frag),
389 skb_frag_size(frag), &pb[slots_used]);
394 static int count_skb_frag_slots(struct sk_buff *skb)
396 int i, frags = skb_shinfo(skb)->nr_frags;
399 for (i = 0; i < frags; i++) {
400 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
401 unsigned long size = skb_frag_size(frag);
402 unsigned long offset = frag->page_offset;
404 /* Skip unused frames from start of page */
405 offset &= ~PAGE_MASK;
406 pages += PFN_UP(offset + size);
411 static int netvsc_get_slots(struct sk_buff *skb)
413 char *data = skb->data;
414 unsigned int offset = offset_in_page(data);
415 unsigned int len = skb_headlen(skb);
419 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
420 frag_slots = count_skb_frag_slots(skb);
421 return slots + frag_slots;
424 static u32 net_checksum_info(struct sk_buff *skb)
426 if (skb->protocol == htons(ETH_P_IP)) {
427 struct iphdr *ip = ip_hdr(skb);
429 if (ip->protocol == IPPROTO_TCP)
430 return TRANSPORT_INFO_IPV4_TCP;
431 else if (ip->protocol == IPPROTO_UDP)
432 return TRANSPORT_INFO_IPV4_UDP;
434 struct ipv6hdr *ip6 = ipv6_hdr(skb);
436 if (ip6->nexthdr == IPPROTO_TCP)
437 return TRANSPORT_INFO_IPV6_TCP;
438 else if (ip6->nexthdr == IPPROTO_UDP)
439 return TRANSPORT_INFO_IPV6_UDP;
442 return TRANSPORT_INFO_NOT_IP;
445 /* Send skb on the slave VF device. */
446 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
449 struct net_device_context *ndev_ctx = netdev_priv(net);
450 unsigned int len = skb->len;
453 skb->dev = vf_netdev;
454 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
456 rc = dev_queue_xmit(skb);
457 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
458 struct netvsc_vf_pcpu_stats *pcpu_stats
459 = this_cpu_ptr(ndev_ctx->vf_stats);
461 u64_stats_update_begin(&pcpu_stats->syncp);
462 pcpu_stats->tx_packets++;
463 pcpu_stats->tx_bytes += len;
464 u64_stats_update_end(&pcpu_stats->syncp);
466 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
472 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
474 struct net_device_context *net_device_ctx = netdev_priv(net);
475 struct hv_netvsc_packet *packet = NULL;
477 unsigned int num_data_pgs;
478 struct rndis_message *rndis_msg;
479 struct net_device *vf_netdev;
482 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
484 /* if VF is present and up then redirect packets
485 * already called with rcu_read_lock_bh
487 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
488 if (vf_netdev && netif_running(vf_netdev) &&
489 !netpoll_tx_running(net))
490 return netvsc_vf_xmit(net, vf_netdev, skb);
492 /* We will atmost need two pages to describe the rndis
493 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
494 * of pages in a single packet. If skb is scattered around
495 * more pages we try linearizing it.
498 num_data_pgs = netvsc_get_slots(skb) + 2;
500 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
501 ++net_device_ctx->eth_stats.tx_scattered;
503 if (skb_linearize(skb))
506 num_data_pgs = netvsc_get_slots(skb) + 2;
507 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
508 ++net_device_ctx->eth_stats.tx_too_big;
514 * Place the rndis header in the skb head room and
515 * the skb->cb will be used for hv_netvsc_packet
518 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
522 /* Use the skb control buffer for building up the packet */
523 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
524 FIELD_SIZEOF(struct sk_buff, cb));
525 packet = (struct hv_netvsc_packet *)skb->cb;
527 packet->q_idx = skb_get_queue_mapping(skb);
529 packet->total_data_buflen = skb->len;
530 packet->total_bytes = skb->len;
531 packet->total_packets = 1;
533 rndis_msg = (struct rndis_message *)skb->head;
535 /* Add the rndis header */
536 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
537 rndis_msg->msg_len = packet->total_data_buflen;
539 rndis_msg->msg.pkt = (struct rndis_packet) {
540 .data_offset = sizeof(struct rndis_packet),
541 .data_len = packet->total_data_buflen,
542 .per_pkt_info_offset = sizeof(struct rndis_packet),
545 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
547 hash = skb_get_hash_raw(skb);
548 if (hash != 0 && net->real_num_tx_queues > 1) {
551 rndis_msg_size += NDIS_HASH_PPI_SIZE;
552 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
557 if (skb_vlan_tag_present(skb)) {
558 struct ndis_pkt_8021q_info *vlan;
560 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
561 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
565 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
566 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
570 if (skb_is_gso(skb)) {
571 struct ndis_tcp_lso_info *lso_info;
573 rndis_msg_size += NDIS_LSO_PPI_SIZE;
574 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
575 TCP_LARGESEND_PKTINFO);
578 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
579 if (skb->protocol == htons(ETH_P_IP)) {
580 lso_info->lso_v2_transmit.ip_version =
581 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
582 ip_hdr(skb)->tot_len = 0;
583 ip_hdr(skb)->check = 0;
584 tcp_hdr(skb)->check =
585 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
586 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
588 lso_info->lso_v2_transmit.ip_version =
589 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
590 ipv6_hdr(skb)->payload_len = 0;
591 tcp_hdr(skb)->check =
592 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
593 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
595 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
596 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
597 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
598 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
599 struct ndis_tcp_ip_checksum_info *csum_info;
601 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
602 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
603 TCPIP_CHKSUM_PKTINFO);
605 csum_info->value = 0;
606 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
608 if (skb->protocol == htons(ETH_P_IP)) {
609 csum_info->transmit.is_ipv4 = 1;
611 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
612 csum_info->transmit.tcp_checksum = 1;
614 csum_info->transmit.udp_checksum = 1;
616 csum_info->transmit.is_ipv6 = 1;
618 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
619 csum_info->transmit.tcp_checksum = 1;
621 csum_info->transmit.udp_checksum = 1;
624 /* Can't do offload of this type of checksum */
625 if (skb_checksum_help(skb))
630 /* Start filling in the page buffers with the rndis hdr */
631 rndis_msg->msg_len += rndis_msg_size;
632 packet->total_data_buflen = rndis_msg->msg_len;
633 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
636 /* timestamp packet in software */
637 skb_tx_timestamp(skb);
639 ret = netvsc_send(net, packet, rndis_msg, pb, skb);
640 if (likely(ret == 0))
643 if (ret == -EAGAIN) {
644 ++net_device_ctx->eth_stats.tx_busy;
645 return NETDEV_TX_BUSY;
649 ++net_device_ctx->eth_stats.tx_no_space;
652 dev_kfree_skb_any(skb);
653 net->stats.tx_dropped++;
658 ++net_device_ctx->eth_stats.tx_no_memory;
663 * netvsc_linkstatus_callback - Link up/down notification
665 void netvsc_linkstatus_callback(struct net_device *net,
666 struct rndis_message *resp)
668 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
669 struct net_device_context *ndev_ctx = netdev_priv(net);
670 struct netvsc_reconfig *event;
673 /* Update the physical link speed when changing to another vSwitch */
674 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
677 speed = *(u32 *)((void *)indicate
678 + indicate->status_buf_offset) / 10000;
679 ndev_ctx->speed = speed;
683 /* Handle these link change statuses below */
684 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
685 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
686 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
689 if (net->reg_state != NETREG_REGISTERED)
692 event = kzalloc(sizeof(*event), GFP_ATOMIC);
695 event->event = indicate->status;
697 spin_lock_irqsave(&ndev_ctx->lock, flags);
698 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
699 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
701 schedule_delayed_work(&ndev_ctx->dwork, 0);
704 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
705 struct napi_struct *napi,
706 const struct ndis_tcp_ip_checksum_info *csum_info,
707 const struct ndis_pkt_8021q_info *vlan,
708 void *data, u32 buflen)
712 skb = napi_alloc_skb(napi, buflen);
717 * Copy to skb. This copy is needed here since the memory pointed by
718 * hv_netvsc_packet cannot be deallocated
720 skb_put_data(skb, data, buflen);
722 skb->protocol = eth_type_trans(skb, net);
724 /* skb is already created with CHECKSUM_NONE */
725 skb_checksum_none_assert(skb);
728 * In Linux, the IP checksum is always checked.
729 * Do L4 checksum offload if enabled and present.
731 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
732 if (csum_info->receive.tcp_checksum_succeeded ||
733 csum_info->receive.udp_checksum_succeeded)
734 skb->ip_summed = CHECKSUM_UNNECESSARY;
738 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
740 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
748 * netvsc_recv_callback - Callback when we receive a packet from the
749 * "wire" on the specified device.
751 int netvsc_recv_callback(struct net_device *net,
752 struct netvsc_device *net_device,
753 struct vmbus_channel *channel,
755 const struct ndis_tcp_ip_checksum_info *csum_info,
756 const struct ndis_pkt_8021q_info *vlan)
758 struct net_device_context *net_device_ctx = netdev_priv(net);
759 u16 q_idx = channel->offermsg.offer.sub_channel_index;
760 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
762 struct netvsc_stats *rx_stats;
764 if (net->reg_state != NETREG_REGISTERED)
765 return NVSP_STAT_FAIL;
767 /* Allocate a skb - TODO direct I/O to pages? */
768 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
769 csum_info, vlan, data, len);
770 if (unlikely(!skb)) {
771 ++net_device_ctx->eth_stats.rx_no_memory;
773 return NVSP_STAT_FAIL;
776 skb_record_rx_queue(skb, q_idx);
779 * Even if injecting the packet, record the statistics
780 * on the synthetic device because modifying the VF device
781 * statistics will not work correctly.
783 rx_stats = &nvchan->rx_stats;
784 u64_stats_update_begin(&rx_stats->syncp);
786 rx_stats->bytes += len;
788 if (skb->pkt_type == PACKET_BROADCAST)
789 ++rx_stats->broadcast;
790 else if (skb->pkt_type == PACKET_MULTICAST)
791 ++rx_stats->multicast;
792 u64_stats_update_end(&rx_stats->syncp);
794 napi_gro_receive(&nvchan->napi, skb);
798 static void netvsc_get_drvinfo(struct net_device *net,
799 struct ethtool_drvinfo *info)
801 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
802 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
805 static void netvsc_get_channels(struct net_device *net,
806 struct ethtool_channels *channel)
808 struct net_device_context *net_device_ctx = netdev_priv(net);
809 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
812 channel->max_combined = nvdev->max_chn;
813 channel->combined_count = nvdev->num_chn;
817 static int netvsc_set_channels(struct net_device *net,
818 struct ethtool_channels *channels)
820 struct net_device_context *net_device_ctx = netdev_priv(net);
821 struct hv_device *dev = net_device_ctx->device_ctx;
822 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
823 unsigned int orig, count = channels->combined_count;
824 struct netvsc_device_info device_info;
828 /* We do not support separate count for rx, tx, or other */
830 channels->rx_count || channels->tx_count || channels->other_count)
833 if (!nvdev || nvdev->destroy)
836 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
839 if (count > nvdev->max_chn)
842 orig = nvdev->num_chn;
843 was_opened = rndis_filter_opened(nvdev);
845 rndis_filter_close(nvdev);
847 memset(&device_info, 0, sizeof(device_info));
848 device_info.num_chn = count;
849 device_info.send_sections = nvdev->send_section_cnt;
850 device_info.send_section_size = nvdev->send_section_size;
851 device_info.recv_sections = nvdev->recv_section_cnt;
852 device_info.recv_section_size = nvdev->recv_section_size;
854 rndis_filter_device_remove(dev, nvdev);
856 nvdev = rndis_filter_device_add(dev, &device_info);
858 ret = PTR_ERR(nvdev);
859 device_info.num_chn = orig;
860 nvdev = rndis_filter_device_add(dev, &device_info);
863 netdev_err(net, "restoring channel setting failed: %ld\n",
870 rndis_filter_open(nvdev);
872 /* We may have missed link change notifications */
873 net_device_ctx->last_reconfig = 0;
874 schedule_delayed_work(&net_device_ctx->dwork, 0);
880 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
882 struct ethtool_link_ksettings diff1 = *cmd;
883 struct ethtool_link_ksettings diff2 = {};
885 diff1.base.speed = 0;
886 diff1.base.duplex = 0;
887 /* advertising and cmd are usually set */
888 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
890 /* We set port to PORT_OTHER */
891 diff2.base.port = PORT_OTHER;
893 return !memcmp(&diff1, &diff2, sizeof(diff1));
896 static void netvsc_init_settings(struct net_device *dev)
898 struct net_device_context *ndc = netdev_priv(dev);
900 ndc->l4_hash = HV_DEFAULT_L4HASH;
902 ndc->speed = SPEED_UNKNOWN;
903 ndc->duplex = DUPLEX_FULL;
906 static int netvsc_get_link_ksettings(struct net_device *dev,
907 struct ethtool_link_ksettings *cmd)
909 struct net_device_context *ndc = netdev_priv(dev);
911 cmd->base.speed = ndc->speed;
912 cmd->base.duplex = ndc->duplex;
913 cmd->base.port = PORT_OTHER;
918 static int netvsc_set_link_ksettings(struct net_device *dev,
919 const struct ethtool_link_ksettings *cmd)
921 struct net_device_context *ndc = netdev_priv(dev);
924 speed = cmd->base.speed;
925 if (!ethtool_validate_speed(speed) ||
926 !ethtool_validate_duplex(cmd->base.duplex) ||
927 !netvsc_validate_ethtool_ss_cmd(cmd))
931 ndc->duplex = cmd->base.duplex;
936 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
938 struct net_device_context *ndevctx = netdev_priv(ndev);
939 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
940 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
941 struct hv_device *hdev = ndevctx->device_ctx;
942 int orig_mtu = ndev->mtu;
943 struct netvsc_device_info device_info;
947 if (!nvdev || nvdev->destroy)
950 /* Change MTU of underlying VF netdev first. */
952 ret = dev_set_mtu(vf_netdev, mtu);
957 netif_device_detach(ndev);
958 was_opened = rndis_filter_opened(nvdev);
960 rndis_filter_close(nvdev);
962 memset(&device_info, 0, sizeof(device_info));
963 device_info.num_chn = nvdev->num_chn;
964 device_info.send_sections = nvdev->send_section_cnt;
965 device_info.send_section_size = nvdev->send_section_size;
966 device_info.recv_sections = nvdev->recv_section_cnt;
967 device_info.recv_section_size = nvdev->recv_section_size;
969 rndis_filter_device_remove(hdev, nvdev);
973 nvdev = rndis_filter_device_add(hdev, &device_info);
975 ret = PTR_ERR(nvdev);
977 /* Attempt rollback to original MTU */
978 ndev->mtu = orig_mtu;
979 nvdev = rndis_filter_device_add(hdev, &device_info);
982 dev_set_mtu(vf_netdev, orig_mtu);
985 netdev_err(ndev, "restoring mtu failed: %ld\n",
992 rndis_filter_open(nvdev);
994 netif_device_attach(ndev);
996 /* We may have missed link change notifications */
997 schedule_delayed_work(&ndevctx->dwork, 0);
1002 static void netvsc_get_vf_stats(struct net_device *net,
1003 struct netvsc_vf_pcpu_stats *tot)
1005 struct net_device_context *ndev_ctx = netdev_priv(net);
1008 memset(tot, 0, sizeof(*tot));
1010 for_each_possible_cpu(i) {
1011 const struct netvsc_vf_pcpu_stats *stats
1012 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1013 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1017 start = u64_stats_fetch_begin_irq(&stats->syncp);
1018 rx_packets = stats->rx_packets;
1019 tx_packets = stats->tx_packets;
1020 rx_bytes = stats->rx_bytes;
1021 tx_bytes = stats->tx_bytes;
1022 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1024 tot->rx_packets += rx_packets;
1025 tot->tx_packets += tx_packets;
1026 tot->rx_bytes += rx_bytes;
1027 tot->tx_bytes += tx_bytes;
1028 tot->tx_dropped += stats->tx_dropped;
1032 static void netvsc_get_stats64(struct net_device *net,
1033 struct rtnl_link_stats64 *t)
1035 struct net_device_context *ndev_ctx = netdev_priv(net);
1036 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1037 struct netvsc_vf_pcpu_stats vf_tot;
1043 netdev_stats_to_stats64(t, &net->stats);
1045 netvsc_get_vf_stats(net, &vf_tot);
1046 t->rx_packets += vf_tot.rx_packets;
1047 t->tx_packets += vf_tot.tx_packets;
1048 t->rx_bytes += vf_tot.rx_bytes;
1049 t->tx_bytes += vf_tot.tx_bytes;
1050 t->tx_dropped += vf_tot.tx_dropped;
1052 for (i = 0; i < nvdev->num_chn; i++) {
1053 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1054 const struct netvsc_stats *stats;
1055 u64 packets, bytes, multicast;
1058 stats = &nvchan->tx_stats;
1060 start = u64_stats_fetch_begin_irq(&stats->syncp);
1061 packets = stats->packets;
1062 bytes = stats->bytes;
1063 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1065 t->tx_bytes += bytes;
1066 t->tx_packets += packets;
1068 stats = &nvchan->rx_stats;
1070 start = u64_stats_fetch_begin_irq(&stats->syncp);
1071 packets = stats->packets;
1072 bytes = stats->bytes;
1073 multicast = stats->multicast + stats->broadcast;
1074 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1076 t->rx_bytes += bytes;
1077 t->rx_packets += packets;
1078 t->multicast += multicast;
1082 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1084 struct net_device_context *ndc = netdev_priv(ndev);
1085 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1086 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1087 struct sockaddr *addr = p;
1090 err = eth_prepare_mac_addr_change(ndev, p);
1098 err = dev_set_mac_address(vf_netdev, addr);
1103 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1105 eth_commit_mac_addr_change(ndev, p);
1106 } else if (vf_netdev) {
1107 /* rollback change on VF */
1108 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1109 dev_set_mac_address(vf_netdev, addr);
1115 static const struct {
1116 char name[ETH_GSTRING_LEN];
1118 } netvsc_stats[] = {
1119 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1120 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1121 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1122 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1123 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1124 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1125 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1126 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1127 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1128 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1130 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1131 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1132 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1133 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1134 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1137 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1138 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1140 /* 4 statistics per queue (rx/tx packets/bytes) */
1141 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1143 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1145 struct net_device_context *ndc = netdev_priv(dev);
1146 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1151 switch (string_set) {
1153 return NETVSC_GLOBAL_STATS_LEN
1154 + NETVSC_VF_STATS_LEN
1155 + NETVSC_QUEUE_STATS_LEN(nvdev);
1161 static void netvsc_get_ethtool_stats(struct net_device *dev,
1162 struct ethtool_stats *stats, u64 *data)
1164 struct net_device_context *ndc = netdev_priv(dev);
1165 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1166 const void *nds = &ndc->eth_stats;
1167 const struct netvsc_stats *qstats;
1168 struct netvsc_vf_pcpu_stats sum;
1176 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1177 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1179 netvsc_get_vf_stats(dev, &sum);
1180 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1181 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1183 for (j = 0; j < nvdev->num_chn; j++) {
1184 qstats = &nvdev->chan_table[j].tx_stats;
1187 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1188 packets = qstats->packets;
1189 bytes = qstats->bytes;
1190 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1191 data[i++] = packets;
1194 qstats = &nvdev->chan_table[j].rx_stats;
1196 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1197 packets = qstats->packets;
1198 bytes = qstats->bytes;
1199 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1200 data[i++] = packets;
1205 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1207 struct net_device_context *ndc = netdev_priv(dev);
1208 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1215 switch (stringset) {
1217 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1218 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1219 p += ETH_GSTRING_LEN;
1222 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1223 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1224 p += ETH_GSTRING_LEN;
1227 for (i = 0; i < nvdev->num_chn; i++) {
1228 sprintf(p, "tx_queue_%u_packets", i);
1229 p += ETH_GSTRING_LEN;
1230 sprintf(p, "tx_queue_%u_bytes", i);
1231 p += ETH_GSTRING_LEN;
1232 sprintf(p, "rx_queue_%u_packets", i);
1233 p += ETH_GSTRING_LEN;
1234 sprintf(p, "rx_queue_%u_bytes", i);
1235 p += ETH_GSTRING_LEN;
1243 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1244 struct ethtool_rxnfc *info)
1246 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1248 info->data = RXH_IP_SRC | RXH_IP_DST;
1250 switch (info->flow_type) {
1252 if (ndc->l4_hash & HV_TCP4_L4HASH)
1253 info->data |= l4_flag;
1258 if (ndc->l4_hash & HV_TCP6_L4HASH)
1259 info->data |= l4_flag;
1264 if (ndc->l4_hash & HV_UDP4_L4HASH)
1265 info->data |= l4_flag;
1270 if (ndc->l4_hash & HV_UDP6_L4HASH)
1271 info->data |= l4_flag;
1287 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1290 struct net_device_context *ndc = netdev_priv(dev);
1291 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1296 switch (info->cmd) {
1297 case ETHTOOL_GRXRINGS:
1298 info->data = nvdev->num_chn;
1302 return netvsc_get_rss_hash_opts(ndc, info);
1307 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1308 struct ethtool_rxnfc *info)
1310 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1311 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1312 switch (info->flow_type) {
1314 ndc->l4_hash |= HV_TCP4_L4HASH;
1318 ndc->l4_hash |= HV_TCP6_L4HASH;
1322 ndc->l4_hash |= HV_UDP4_L4HASH;
1326 ndc->l4_hash |= HV_UDP6_L4HASH;
1336 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1337 switch (info->flow_type) {
1339 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1343 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1347 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1351 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1365 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1367 struct net_device_context *ndc = netdev_priv(ndev);
1369 if (info->cmd == ETHTOOL_SRXFH)
1370 return netvsc_set_rss_hash_opts(ndc, info);
1375 #ifdef CONFIG_NET_POLL_CONTROLLER
1376 static void netvsc_poll_controller(struct net_device *dev)
1378 struct net_device_context *ndc = netdev_priv(dev);
1379 struct netvsc_device *ndev;
1383 ndev = rcu_dereference(ndc->nvdev);
1385 for (i = 0; i < ndev->num_chn; i++) {
1386 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1388 napi_schedule(&nvchan->napi);
1395 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1397 return NETVSC_HASH_KEYLEN;
1400 static u32 netvsc_rss_indir_size(struct net_device *dev)
1405 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1408 struct net_device_context *ndc = netdev_priv(dev);
1409 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1410 struct rndis_device *rndis_dev;
1417 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1419 rndis_dev = ndev->extension;
1421 for (i = 0; i < ITAB_NUM; i++)
1422 indir[i] = rndis_dev->rx_table[i];
1426 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1431 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1432 const u8 *key, const u8 hfunc)
1434 struct net_device_context *ndc = netdev_priv(dev);
1435 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1436 struct rndis_device *rndis_dev;
1442 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1445 rndis_dev = ndev->extension;
1447 for (i = 0; i < ITAB_NUM; i++)
1448 if (indir[i] >= ndev->num_chn)
1451 for (i = 0; i < ITAB_NUM; i++)
1452 rndis_dev->rx_table[i] = indir[i];
1459 key = rndis_dev->rss_key;
1462 return rndis_filter_set_rss_param(rndis_dev, key);
1465 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1466 * It does have pre-allocated receive area which is divided into sections.
1468 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1469 struct ethtool_ringparam *ring)
1473 ring->rx_pending = nvdev->recv_section_cnt;
1474 ring->tx_pending = nvdev->send_section_cnt;
1476 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1477 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1479 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1481 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1482 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1483 / nvdev->send_section_size;
1486 static void netvsc_get_ringparam(struct net_device *ndev,
1487 struct ethtool_ringparam *ring)
1489 struct net_device_context *ndevctx = netdev_priv(ndev);
1490 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1495 __netvsc_get_ringparam(nvdev, ring);
1498 static int netvsc_set_ringparam(struct net_device *ndev,
1499 struct ethtool_ringparam *ring)
1501 struct net_device_context *ndevctx = netdev_priv(ndev);
1502 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1503 struct hv_device *hdev = ndevctx->device_ctx;
1504 struct netvsc_device_info device_info;
1505 struct ethtool_ringparam orig;
1510 if (!nvdev || nvdev->destroy)
1513 memset(&orig, 0, sizeof(orig));
1514 __netvsc_get_ringparam(nvdev, &orig);
1516 new_tx = clamp_t(u32, ring->tx_pending,
1517 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1518 new_rx = clamp_t(u32, ring->rx_pending,
1519 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1521 if (new_tx == orig.tx_pending &&
1522 new_rx == orig.rx_pending)
1523 return 0; /* no change */
1525 memset(&device_info, 0, sizeof(device_info));
1526 device_info.num_chn = nvdev->num_chn;
1527 device_info.send_sections = new_tx;
1528 device_info.send_section_size = nvdev->send_section_size;
1529 device_info.recv_sections = new_rx;
1530 device_info.recv_section_size = nvdev->recv_section_size;
1532 netif_device_detach(ndev);
1533 was_opened = rndis_filter_opened(nvdev);
1535 rndis_filter_close(nvdev);
1537 rndis_filter_device_remove(hdev, nvdev);
1539 nvdev = rndis_filter_device_add(hdev, &device_info);
1540 if (IS_ERR(nvdev)) {
1541 ret = PTR_ERR(nvdev);
1543 device_info.send_sections = orig.tx_pending;
1544 device_info.recv_sections = orig.rx_pending;
1545 nvdev = rndis_filter_device_add(hdev, &device_info);
1546 if (IS_ERR(nvdev)) {
1547 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1554 rndis_filter_open(nvdev);
1555 netif_device_attach(ndev);
1557 /* We may have missed link change notifications */
1558 ndevctx->last_reconfig = 0;
1559 schedule_delayed_work(&ndevctx->dwork, 0);
1564 static const struct ethtool_ops ethtool_ops = {
1565 .get_drvinfo = netvsc_get_drvinfo,
1566 .get_link = ethtool_op_get_link,
1567 .get_ethtool_stats = netvsc_get_ethtool_stats,
1568 .get_sset_count = netvsc_get_sset_count,
1569 .get_strings = netvsc_get_strings,
1570 .get_channels = netvsc_get_channels,
1571 .set_channels = netvsc_set_channels,
1572 .get_ts_info = ethtool_op_get_ts_info,
1573 .get_rxnfc = netvsc_get_rxnfc,
1574 .set_rxnfc = netvsc_set_rxnfc,
1575 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1576 .get_rxfh_indir_size = netvsc_rss_indir_size,
1577 .get_rxfh = netvsc_get_rxfh,
1578 .set_rxfh = netvsc_set_rxfh,
1579 .get_link_ksettings = netvsc_get_link_ksettings,
1580 .set_link_ksettings = netvsc_set_link_ksettings,
1581 .get_ringparam = netvsc_get_ringparam,
1582 .set_ringparam = netvsc_set_ringparam,
1585 static const struct net_device_ops device_ops = {
1586 .ndo_open = netvsc_open,
1587 .ndo_stop = netvsc_close,
1588 .ndo_start_xmit = netvsc_start_xmit,
1589 .ndo_set_rx_mode = netvsc_set_multicast_list,
1590 .ndo_change_mtu = netvsc_change_mtu,
1591 .ndo_validate_addr = eth_validate_addr,
1592 .ndo_set_mac_address = netvsc_set_mac_addr,
1593 .ndo_select_queue = netvsc_select_queue,
1594 .ndo_get_stats64 = netvsc_get_stats64,
1595 #ifdef CONFIG_NET_POLL_CONTROLLER
1596 .ndo_poll_controller = netvsc_poll_controller,
1601 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1602 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1603 * present send GARP packet to network peers with netif_notify_peers().
1605 static void netvsc_link_change(struct work_struct *w)
1607 struct net_device_context *ndev_ctx =
1608 container_of(w, struct net_device_context, dwork.work);
1609 struct hv_device *device_obj = ndev_ctx->device_ctx;
1610 struct net_device *net = hv_get_drvdata(device_obj);
1611 struct netvsc_device *net_device;
1612 struct rndis_device *rdev;
1613 struct netvsc_reconfig *event = NULL;
1614 bool notify = false, reschedule = false;
1615 unsigned long flags, next_reconfig, delay;
1617 /* if changes are happening, comeback later */
1618 if (!rtnl_trylock()) {
1619 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1623 net_device = rtnl_dereference(ndev_ctx->nvdev);
1627 rdev = net_device->extension;
1629 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1630 if (time_is_after_jiffies(next_reconfig)) {
1631 /* link_watch only sends one notification with current state
1632 * per second, avoid doing reconfig more frequently. Handle
1635 delay = next_reconfig - jiffies;
1636 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1637 schedule_delayed_work(&ndev_ctx->dwork, delay);
1640 ndev_ctx->last_reconfig = jiffies;
1642 spin_lock_irqsave(&ndev_ctx->lock, flags);
1643 if (!list_empty(&ndev_ctx->reconfig_events)) {
1644 event = list_first_entry(&ndev_ctx->reconfig_events,
1645 struct netvsc_reconfig, list);
1646 list_del(&event->list);
1647 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1649 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1654 switch (event->event) {
1655 /* Only the following events are possible due to the check in
1656 * netvsc_linkstatus_callback()
1658 case RNDIS_STATUS_MEDIA_CONNECT:
1659 if (rdev->link_state) {
1660 rdev->link_state = false;
1661 netif_carrier_on(net);
1662 netif_tx_wake_all_queues(net);
1668 case RNDIS_STATUS_MEDIA_DISCONNECT:
1669 if (!rdev->link_state) {
1670 rdev->link_state = true;
1671 netif_carrier_off(net);
1672 netif_tx_stop_all_queues(net);
1676 case RNDIS_STATUS_NETWORK_CHANGE:
1677 /* Only makes sense if carrier is present */
1678 if (!rdev->link_state) {
1679 rdev->link_state = true;
1680 netif_carrier_off(net);
1681 netif_tx_stop_all_queues(net);
1682 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1683 spin_lock_irqsave(&ndev_ctx->lock, flags);
1684 list_add(&event->list, &ndev_ctx->reconfig_events);
1685 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1694 netdev_notify_peers(net);
1696 /* link_watch only sends one notification with current state per
1697 * second, handle next reconfig event in 2 seconds.
1700 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1708 static struct net_device *get_netvsc_bymac(const u8 *mac)
1710 struct net_device *dev;
1714 for_each_netdev(&init_net, dev) {
1715 if (dev->netdev_ops != &device_ops)
1716 continue; /* not a netvsc device */
1718 if (ether_addr_equal(mac, dev->perm_addr))
1725 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1727 struct net_device *dev;
1731 for_each_netdev(&init_net, dev) {
1732 struct net_device_context *net_device_ctx;
1734 if (dev->netdev_ops != &device_ops)
1735 continue; /* not a netvsc device */
1737 net_device_ctx = netdev_priv(dev);
1738 if (!rtnl_dereference(net_device_ctx->nvdev))
1739 continue; /* device is removed */
1741 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1742 return dev; /* a match */
1748 /* Called when VF is injecting data into network stack.
1749 * Change the associated network device from VF to netvsc.
1750 * note: already called with rcu_read_lock
1752 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1754 struct sk_buff *skb = *pskb;
1755 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1756 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1757 struct netvsc_vf_pcpu_stats *pcpu_stats
1758 = this_cpu_ptr(ndev_ctx->vf_stats);
1762 u64_stats_update_begin(&pcpu_stats->syncp);
1763 pcpu_stats->rx_packets++;
1764 pcpu_stats->rx_bytes += skb->len;
1765 u64_stats_update_end(&pcpu_stats->syncp);
1767 return RX_HANDLER_ANOTHER;
1770 static int netvsc_vf_join(struct net_device *vf_netdev,
1771 struct net_device *ndev)
1773 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1776 ret = netdev_rx_handler_register(vf_netdev,
1777 netvsc_vf_handle_frame, ndev);
1779 netdev_err(vf_netdev,
1780 "can not register netvsc VF receive handler (err = %d)\n",
1782 goto rx_handler_failed;
1785 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
1787 netdev_err(vf_netdev,
1788 "can not set master device %s (err = %d)\n",
1790 goto upper_link_failed;
1793 /* set slave flag before open to prevent IPv6 addrconf */
1794 vf_netdev->flags |= IFF_SLAVE;
1796 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1798 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1800 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1804 netdev_rx_handler_unregister(vf_netdev);
1809 static void __netvsc_vf_setup(struct net_device *ndev,
1810 struct net_device *vf_netdev)
1814 /* Align MTU of VF with master */
1815 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1817 netdev_warn(vf_netdev,
1818 "unable to change mtu to %u\n", ndev->mtu);
1820 if (netif_running(ndev)) {
1821 ret = dev_open(vf_netdev);
1823 netdev_warn(vf_netdev,
1824 "unable to open: %d\n", ret);
1828 /* Setup VF as slave of the synthetic device.
1829 * Runs in workqueue to avoid recursion in netlink callbacks.
1831 static void netvsc_vf_setup(struct work_struct *w)
1833 struct net_device_context *ndev_ctx
1834 = container_of(w, struct net_device_context, vf_takeover.work);
1835 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1836 struct net_device *vf_netdev;
1838 if (!rtnl_trylock()) {
1839 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1843 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1845 __netvsc_vf_setup(ndev, vf_netdev);
1850 static int netvsc_register_vf(struct net_device *vf_netdev)
1852 struct net_device *ndev;
1853 struct net_device_context *net_device_ctx;
1854 struct netvsc_device *netvsc_dev;
1856 if (vf_netdev->addr_len != ETH_ALEN)
1860 * We will use the MAC address to locate the synthetic interface to
1861 * associate with the VF interface. If we don't find a matching
1862 * synthetic interface, move on.
1864 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1868 net_device_ctx = netdev_priv(ndev);
1869 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1870 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1873 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1876 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1878 dev_hold(vf_netdev);
1879 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1883 /* VF up/down change detected, schedule to change data path */
1884 static int netvsc_vf_changed(struct net_device *vf_netdev)
1886 struct net_device_context *net_device_ctx;
1887 struct netvsc_device *netvsc_dev;
1888 struct net_device *ndev;
1889 bool vf_is_up = netif_running(vf_netdev);
1891 ndev = get_netvsc_byref(vf_netdev);
1895 net_device_ctx = netdev_priv(ndev);
1896 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1900 netvsc_switch_datapath(ndev, vf_is_up);
1901 netdev_info(ndev, "Data path switched %s VF: %s\n",
1902 vf_is_up ? "to" : "from", vf_netdev->name);
1907 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1909 struct net_device *ndev;
1910 struct net_device_context *net_device_ctx;
1912 ndev = get_netvsc_byref(vf_netdev);
1916 net_device_ctx = netdev_priv(ndev);
1917 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1919 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1921 netdev_rx_handler_unregister(vf_netdev);
1922 netdev_upper_dev_unlink(vf_netdev, ndev);
1923 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1929 static int netvsc_probe(struct hv_device *dev,
1930 const struct hv_vmbus_device_id *dev_id)
1932 struct net_device *net = NULL;
1933 struct net_device_context *net_device_ctx;
1934 struct netvsc_device_info device_info;
1935 struct netvsc_device *nvdev;
1938 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1943 netif_carrier_off(net);
1945 netvsc_init_settings(net);
1947 net_device_ctx = netdev_priv(net);
1948 net_device_ctx->device_ctx = dev;
1949 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1950 if (netif_msg_probe(net_device_ctx))
1951 netdev_dbg(net, "netvsc msg_enable: %d\n",
1952 net_device_ctx->msg_enable);
1954 hv_set_drvdata(dev, net);
1956 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1958 spin_lock_init(&net_device_ctx->lock);
1959 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1960 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1962 net_device_ctx->vf_stats
1963 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1964 if (!net_device_ctx->vf_stats)
1967 net->netdev_ops = &device_ops;
1968 net->ethtool_ops = ðtool_ops;
1969 SET_NETDEV_DEV(net, &dev->device);
1971 /* We always need headroom for rndis header */
1972 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1974 /* Initialize the number of queues to be 1, we may change it if more
1975 * channels are offered later.
1977 netif_set_real_num_tx_queues(net, 1);
1978 netif_set_real_num_rx_queues(net, 1);
1980 /* Notify the netvsc driver of the new device */
1981 memset(&device_info, 0, sizeof(device_info));
1982 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1983 device_info.send_sections = NETVSC_DEFAULT_TX;
1984 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
1985 device_info.recv_sections = NETVSC_DEFAULT_RX;
1986 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
1988 nvdev = rndis_filter_device_add(dev, &device_info);
1989 if (IS_ERR(nvdev)) {
1990 ret = PTR_ERR(nvdev);
1991 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1995 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1997 /* hw_features computed in rndis_netdev_set_hwcaps() */
1998 net->features = net->hw_features |
1999 NETIF_F_HIGHDMA | NETIF_F_SG |
2000 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2001 net->vlan_features = net->features;
2003 netdev_lockdep_set_classes(net);
2005 /* MTU range: 68 - 1500 or 65521 */
2006 net->min_mtu = NETVSC_MTU_MIN;
2007 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2008 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2010 net->max_mtu = ETH_DATA_LEN;
2012 ret = register_netdev(net);
2014 pr_err("Unable to register netdev.\n");
2015 goto register_failed;
2021 rndis_filter_device_remove(dev, nvdev);
2023 free_percpu(net_device_ctx->vf_stats);
2025 hv_set_drvdata(dev, NULL);
2031 static int netvsc_remove(struct hv_device *dev)
2033 struct net_device_context *ndev_ctx;
2034 struct net_device *vf_netdev;
2035 struct net_device *net;
2037 net = hv_get_drvdata(dev);
2039 dev_err(&dev->device, "No net device to remove\n");
2043 ndev_ctx = netdev_priv(net);
2045 netif_device_detach(net);
2047 cancel_delayed_work_sync(&ndev_ctx->dwork);
2050 * Call to the vsc driver to let it know that the device is being
2051 * removed. Also blocks mtu and channel changes.
2054 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2056 netvsc_unregister_vf(vf_netdev);
2058 unregister_netdevice(net);
2060 rndis_filter_device_remove(dev,
2061 rtnl_dereference(ndev_ctx->nvdev));
2064 hv_set_drvdata(dev, NULL);
2066 free_percpu(ndev_ctx->vf_stats);
2071 static const struct hv_vmbus_device_id id_table[] = {
2077 MODULE_DEVICE_TABLE(vmbus, id_table);
2079 /* The one and only one */
2080 static struct hv_driver netvsc_drv = {
2081 .name = KBUILD_MODNAME,
2082 .id_table = id_table,
2083 .probe = netvsc_probe,
2084 .remove = netvsc_remove,
2088 * On Hyper-V, every VF interface is matched with a corresponding
2089 * synthetic interface. The synthetic interface is presented first
2090 * to the guest. When the corresponding VF instance is registered,
2091 * we will take care of switching the data path.
2093 static int netvsc_netdev_event(struct notifier_block *this,
2094 unsigned long event, void *ptr)
2096 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2098 /* Skip our own events */
2099 if (event_dev->netdev_ops == &device_ops)
2102 /* Avoid non-Ethernet type devices */
2103 if (event_dev->type != ARPHRD_ETHER)
2106 /* Avoid Vlan dev with same MAC registering as VF */
2107 if (is_vlan_dev(event_dev))
2110 /* Avoid Bonding master dev with same MAC registering as VF */
2111 if ((event_dev->priv_flags & IFF_BONDING) &&
2112 (event_dev->flags & IFF_MASTER))
2116 case NETDEV_REGISTER:
2117 return netvsc_register_vf(event_dev);
2118 case NETDEV_UNREGISTER:
2119 return netvsc_unregister_vf(event_dev);
2122 return netvsc_vf_changed(event_dev);
2128 static struct notifier_block netvsc_netdev_notifier = {
2129 .notifier_call = netvsc_netdev_event,
2132 static void __exit netvsc_drv_exit(void)
2134 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2135 vmbus_driver_unregister(&netvsc_drv);
2138 static int __init netvsc_drv_init(void)
2142 if (ring_size < RING_SIZE_MIN) {
2143 ring_size = RING_SIZE_MIN;
2144 pr_info("Increased ring_size to %u (min allowed)\n",
2147 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2148 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
2150 ret = vmbus_driver_register(&netvsc_drv);
2154 register_netdevice_notifier(&netvsc_netdev_notifier);
2158 MODULE_LICENSE("GPL");
2159 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2161 module_init(netvsc_drv_init);
2162 module_exit(netvsc_drv_exit);