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 netif_tx_wake_all_queues(net);
96 rdev = nvdev->extension;
98 if (!rdev->link_state)
99 netif_carrier_on(net);
102 /* Setting synthetic device up transparently sets
103 * slave as up. If open fails, then slave will be
104 * still be offline (and not used).
106 ret = dev_open(vf_netdev);
109 "unable to open slave: %s: %d\n",
110 vf_netdev->name, ret);
115 static int netvsc_close(struct net_device *net)
117 struct net_device_context *net_device_ctx = netdev_priv(net);
118 struct net_device *vf_netdev
119 = rtnl_dereference(net_device_ctx->vf_netdev);
120 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
122 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
123 struct vmbus_channel *chn;
125 netif_tx_disable(net);
127 /* No need to close rndis filter if it is removed already */
131 ret = rndis_filter_close(nvdev);
133 netdev_err(net, "unable to close device (ret %d).\n", ret);
137 /* Ensure pending bytes in ring are read */
140 for (i = 0; i < nvdev->num_chn; i++) {
141 chn = nvdev->chan_table[i].channel;
145 aread = hv_get_bytes_to_read(&chn->inbound);
149 aread = hv_get_bytes_to_read(&chn->outbound);
155 if (retry > retry_max || aread == 0)
165 netdev_err(net, "Ring buffer not empty after closing rndis\n");
171 dev_close(vf_netdev);
176 static inline void *init_ppi_data(struct rndis_message *msg,
177 u32 ppi_size, u32 pkt_type)
179 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
180 struct rndis_per_packet_info *ppi;
182 rndis_pkt->data_offset += ppi_size;
183 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
184 + rndis_pkt->per_pkt_info_len;
186 ppi->size = ppi_size;
187 ppi->type = pkt_type;
188 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
190 rndis_pkt->per_pkt_info_len += ppi_size;
195 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
196 * packets. We can use ethtool to change UDP hash level when necessary.
198 static inline u32 netvsc_get_hash(
200 const struct net_device_context *ndc)
202 struct flow_keys flow;
203 u32 hash, pkt_proto = 0;
204 static u32 hashrnd __read_mostly;
206 net_get_random_once(&hashrnd, sizeof(hashrnd));
208 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
211 switch (flow.basic.ip_proto) {
213 if (flow.basic.n_proto == htons(ETH_P_IP))
214 pkt_proto = HV_TCP4_L4HASH;
215 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
216 pkt_proto = HV_TCP6_L4HASH;
221 if (flow.basic.n_proto == htons(ETH_P_IP))
222 pkt_proto = HV_UDP4_L4HASH;
223 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
224 pkt_proto = HV_UDP6_L4HASH;
229 if (pkt_proto & ndc->l4_hash) {
230 return skb_get_hash(skb);
232 if (flow.basic.n_proto == htons(ETH_P_IP))
233 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
234 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
235 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
239 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
245 static inline int netvsc_get_tx_queue(struct net_device *ndev,
246 struct sk_buff *skb, int old_idx)
248 const struct net_device_context *ndc = netdev_priv(ndev);
249 struct sock *sk = skb->sk;
252 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
253 (VRSS_SEND_TAB_SIZE - 1)];
255 /* If queue index changed record the new value */
256 if (q_idx != old_idx &&
257 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
258 sk_tx_queue_set(sk, q_idx);
264 * Select queue for transmit.
266 * If a valid queue has already been assigned, then use that.
267 * Otherwise compute tx queue based on hash and the send table.
269 * This is basically similar to default (__netdev_pick_tx) with the added step
270 * of using the host send_table when no other queue has been assigned.
272 * TODO support XPS - but get_xps_queue not exported
274 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
276 int q_idx = sk_tx_queue_get(skb->sk);
278 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
279 /* If forwarding a packet, we use the recorded queue when
280 * available for better cache locality.
282 if (skb_rx_queue_recorded(skb))
283 q_idx = skb_get_rx_queue(skb);
285 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
291 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
293 select_queue_fallback_t fallback)
295 struct net_device_context *ndc = netdev_priv(ndev);
296 struct net_device *vf_netdev;
300 vf_netdev = rcu_dereference(ndc->vf_netdev);
302 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
303 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
305 txq = netvsc_pick_tx(ndev, skb);
309 while (unlikely(txq >= ndev->real_num_tx_queues))
310 txq -= ndev->real_num_tx_queues;
315 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
316 struct hv_page_buffer *pb)
320 /* Deal with compund pages by ignoring unused part
323 page += (offset >> PAGE_SHIFT);
324 offset &= ~PAGE_MASK;
329 bytes = PAGE_SIZE - offset;
332 pb[j].pfn = page_to_pfn(page);
333 pb[j].offset = offset;
339 if (offset == PAGE_SIZE && len) {
349 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
350 struct hv_netvsc_packet *packet,
351 struct hv_page_buffer *pb)
354 char *data = skb->data;
355 int frags = skb_shinfo(skb)->nr_frags;
358 /* The packet is laid out thus:
359 * 1. hdr: RNDIS header and PPI
361 * 3. skb fragment data
363 slots_used += fill_pg_buf(virt_to_page(hdr),
365 len, &pb[slots_used]);
367 packet->rmsg_size = len;
368 packet->rmsg_pgcnt = slots_used;
370 slots_used += fill_pg_buf(virt_to_page(data),
371 offset_in_page(data),
372 skb_headlen(skb), &pb[slots_used]);
374 for (i = 0; i < frags; i++) {
375 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
377 slots_used += fill_pg_buf(skb_frag_page(frag),
379 skb_frag_size(frag), &pb[slots_used]);
384 static int count_skb_frag_slots(struct sk_buff *skb)
386 int i, frags = skb_shinfo(skb)->nr_frags;
389 for (i = 0; i < frags; i++) {
390 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
391 unsigned long size = skb_frag_size(frag);
392 unsigned long offset = frag->page_offset;
394 /* Skip unused frames from start of page */
395 offset &= ~PAGE_MASK;
396 pages += PFN_UP(offset + size);
401 static int netvsc_get_slots(struct sk_buff *skb)
403 char *data = skb->data;
404 unsigned int offset = offset_in_page(data);
405 unsigned int len = skb_headlen(skb);
409 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
410 frag_slots = count_skb_frag_slots(skb);
411 return slots + frag_slots;
414 static u32 net_checksum_info(struct sk_buff *skb)
416 if (skb->protocol == htons(ETH_P_IP)) {
417 struct iphdr *ip = ip_hdr(skb);
419 if (ip->protocol == IPPROTO_TCP)
420 return TRANSPORT_INFO_IPV4_TCP;
421 else if (ip->protocol == IPPROTO_UDP)
422 return TRANSPORT_INFO_IPV4_UDP;
424 struct ipv6hdr *ip6 = ipv6_hdr(skb);
426 if (ip6->nexthdr == IPPROTO_TCP)
427 return TRANSPORT_INFO_IPV6_TCP;
428 else if (ip6->nexthdr == IPPROTO_UDP)
429 return TRANSPORT_INFO_IPV6_UDP;
432 return TRANSPORT_INFO_NOT_IP;
435 /* Send skb on the slave VF device. */
436 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
439 struct net_device_context *ndev_ctx = netdev_priv(net);
440 unsigned int len = skb->len;
443 skb->dev = vf_netdev;
444 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
446 rc = dev_queue_xmit(skb);
447 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
448 struct netvsc_vf_pcpu_stats *pcpu_stats
449 = this_cpu_ptr(ndev_ctx->vf_stats);
451 u64_stats_update_begin(&pcpu_stats->syncp);
452 pcpu_stats->tx_packets++;
453 pcpu_stats->tx_bytes += len;
454 u64_stats_update_end(&pcpu_stats->syncp);
456 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
462 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
464 struct net_device_context *net_device_ctx = netdev_priv(net);
465 struct hv_netvsc_packet *packet = NULL;
467 unsigned int num_data_pgs;
468 struct rndis_message *rndis_msg;
469 struct net_device *vf_netdev;
472 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
474 /* if VF is present and up then redirect packets
475 * already called with rcu_read_lock_bh
477 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
478 if (vf_netdev && netif_running(vf_netdev) &&
479 !netpoll_tx_running(net))
480 return netvsc_vf_xmit(net, vf_netdev, skb);
482 /* We will atmost need two pages to describe the rndis
483 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
484 * of pages in a single packet. If skb is scattered around
485 * more pages we try linearizing it.
488 num_data_pgs = netvsc_get_slots(skb) + 2;
490 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
491 ++net_device_ctx->eth_stats.tx_scattered;
493 if (skb_linearize(skb))
496 num_data_pgs = netvsc_get_slots(skb) + 2;
497 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
498 ++net_device_ctx->eth_stats.tx_too_big;
504 * Place the rndis header in the skb head room and
505 * the skb->cb will be used for hv_netvsc_packet
508 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
512 /* Use the skb control buffer for building up the packet */
513 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
514 FIELD_SIZEOF(struct sk_buff, cb));
515 packet = (struct hv_netvsc_packet *)skb->cb;
517 packet->q_idx = skb_get_queue_mapping(skb);
519 packet->total_data_buflen = skb->len;
520 packet->total_bytes = skb->len;
521 packet->total_packets = 1;
523 rndis_msg = (struct rndis_message *)skb->head;
525 /* Add the rndis header */
526 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
527 rndis_msg->msg_len = packet->total_data_buflen;
529 rndis_msg->msg.pkt = (struct rndis_packet) {
530 .data_offset = sizeof(struct rndis_packet),
531 .data_len = packet->total_data_buflen,
532 .per_pkt_info_offset = sizeof(struct rndis_packet),
535 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
537 hash = skb_get_hash_raw(skb);
538 if (hash != 0 && net->real_num_tx_queues > 1) {
541 rndis_msg_size += NDIS_HASH_PPI_SIZE;
542 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
547 if (skb_vlan_tag_present(skb)) {
548 struct ndis_pkt_8021q_info *vlan;
550 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
551 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
555 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
556 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
560 if (skb_is_gso(skb)) {
561 struct ndis_tcp_lso_info *lso_info;
563 rndis_msg_size += NDIS_LSO_PPI_SIZE;
564 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
565 TCP_LARGESEND_PKTINFO);
568 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
569 if (skb->protocol == htons(ETH_P_IP)) {
570 lso_info->lso_v2_transmit.ip_version =
571 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
572 ip_hdr(skb)->tot_len = 0;
573 ip_hdr(skb)->check = 0;
574 tcp_hdr(skb)->check =
575 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
576 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
578 lso_info->lso_v2_transmit.ip_version =
579 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
580 ipv6_hdr(skb)->payload_len = 0;
581 tcp_hdr(skb)->check =
582 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
583 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
585 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
586 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
587 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
588 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
589 struct ndis_tcp_ip_checksum_info *csum_info;
591 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
592 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
593 TCPIP_CHKSUM_PKTINFO);
595 csum_info->value = 0;
596 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
598 if (skb->protocol == htons(ETH_P_IP)) {
599 csum_info->transmit.is_ipv4 = 1;
601 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
602 csum_info->transmit.tcp_checksum = 1;
604 csum_info->transmit.udp_checksum = 1;
606 csum_info->transmit.is_ipv6 = 1;
608 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
609 csum_info->transmit.tcp_checksum = 1;
611 csum_info->transmit.udp_checksum = 1;
614 /* Can't do offload of this type of checksum */
615 if (skb_checksum_help(skb))
620 /* Start filling in the page buffers with the rndis hdr */
621 rndis_msg->msg_len += rndis_msg_size;
622 packet->total_data_buflen = rndis_msg->msg_len;
623 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
626 /* timestamp packet in software */
627 skb_tx_timestamp(skb);
629 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
630 if (likely(ret == 0))
633 if (ret == -EAGAIN) {
634 ++net_device_ctx->eth_stats.tx_busy;
635 return NETDEV_TX_BUSY;
639 ++net_device_ctx->eth_stats.tx_no_space;
642 dev_kfree_skb_any(skb);
643 net->stats.tx_dropped++;
648 ++net_device_ctx->eth_stats.tx_no_memory;
653 * netvsc_linkstatus_callback - Link up/down notification
655 void netvsc_linkstatus_callback(struct hv_device *device_obj,
656 struct rndis_message *resp)
658 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
659 struct net_device *net;
660 struct net_device_context *ndev_ctx;
661 struct netvsc_reconfig *event;
664 net = hv_get_drvdata(device_obj);
669 ndev_ctx = netdev_priv(net);
671 /* Update the physical link speed when changing to another vSwitch */
672 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
675 speed = *(u32 *)((void *)indicate
676 + indicate->status_buf_offset) / 10000;
677 ndev_ctx->speed = speed;
681 /* Handle these link change statuses below */
682 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
683 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
684 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
687 if (net->reg_state != NETREG_REGISTERED)
690 event = kzalloc(sizeof(*event), GFP_ATOMIC);
693 event->event = indicate->status;
695 spin_lock_irqsave(&ndev_ctx->lock, flags);
696 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
697 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
699 schedule_delayed_work(&ndev_ctx->dwork, 0);
702 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
703 struct napi_struct *napi,
704 const struct ndis_tcp_ip_checksum_info *csum_info,
705 const struct ndis_pkt_8021q_info *vlan,
706 void *data, u32 buflen)
710 skb = napi_alloc_skb(napi, buflen);
715 * Copy to skb. This copy is needed here since the memory pointed by
716 * hv_netvsc_packet cannot be deallocated
718 skb_put_data(skb, data, buflen);
720 skb->protocol = eth_type_trans(skb, net);
722 /* skb is already created with CHECKSUM_NONE */
723 skb_checksum_none_assert(skb);
726 * In Linux, the IP checksum is always checked.
727 * Do L4 checksum offload if enabled and present.
729 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
730 if (csum_info->receive.tcp_checksum_succeeded ||
731 csum_info->receive.udp_checksum_succeeded)
732 skb->ip_summed = CHECKSUM_UNNECESSARY;
736 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
738 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
746 * netvsc_recv_callback - Callback when we receive a packet from the
747 * "wire" on the specified device.
749 int netvsc_recv_callback(struct net_device *net,
750 struct vmbus_channel *channel,
752 const struct ndis_tcp_ip_checksum_info *csum_info,
753 const struct ndis_pkt_8021q_info *vlan)
755 struct net_device_context *net_device_ctx = netdev_priv(net);
756 struct netvsc_device *net_device;
757 u16 q_idx = channel->offermsg.offer.sub_channel_index;
758 struct netvsc_channel *nvchan;
760 struct netvsc_stats *rx_stats;
762 if (net->reg_state != NETREG_REGISTERED)
763 return NVSP_STAT_FAIL;
766 net_device = rcu_dereference(net_device_ctx->nvdev);
767 if (unlikely(!net_device))
770 nvchan = &net_device->chan_table[q_idx];
772 /* Allocate a skb - TODO direct I/O to pages? */
773 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
774 csum_info, vlan, data, len);
775 if (unlikely(!skb)) {
776 ++net_device_ctx->eth_stats.rx_no_memory;
779 return NVSP_STAT_FAIL;
782 skb_record_rx_queue(skb, q_idx);
785 * Even if injecting the packet, record the statistics
786 * on the synthetic device because modifying the VF device
787 * statistics will not work correctly.
789 rx_stats = &nvchan->rx_stats;
790 u64_stats_update_begin(&rx_stats->syncp);
792 rx_stats->bytes += len;
794 if (skb->pkt_type == PACKET_BROADCAST)
795 ++rx_stats->broadcast;
796 else if (skb->pkt_type == PACKET_MULTICAST)
797 ++rx_stats->multicast;
798 u64_stats_update_end(&rx_stats->syncp);
800 napi_gro_receive(&nvchan->napi, skb);
806 static void netvsc_get_drvinfo(struct net_device *net,
807 struct ethtool_drvinfo *info)
809 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
810 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
813 static void netvsc_get_channels(struct net_device *net,
814 struct ethtool_channels *channel)
816 struct net_device_context *net_device_ctx = netdev_priv(net);
817 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
820 channel->max_combined = nvdev->max_chn;
821 channel->combined_count = nvdev->num_chn;
825 static int netvsc_set_channels(struct net_device *net,
826 struct ethtool_channels *channels)
828 struct net_device_context *net_device_ctx = netdev_priv(net);
829 struct hv_device *dev = net_device_ctx->device_ctx;
830 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
831 unsigned int orig, count = channels->combined_count;
832 struct netvsc_device_info device_info;
836 /* We do not support separate count for rx, tx, or other */
838 channels->rx_count || channels->tx_count || channels->other_count)
841 if (!nvdev || nvdev->destroy)
844 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
847 if (count > nvdev->max_chn)
850 orig = nvdev->num_chn;
851 was_opened = rndis_filter_opened(nvdev);
853 rndis_filter_close(nvdev);
855 memset(&device_info, 0, sizeof(device_info));
856 device_info.num_chn = count;
857 device_info.send_sections = nvdev->send_section_cnt;
858 device_info.send_section_size = nvdev->send_section_size;
859 device_info.recv_sections = nvdev->recv_section_cnt;
860 device_info.recv_section_size = nvdev->recv_section_size;
862 rndis_filter_device_remove(dev, nvdev);
864 nvdev = rndis_filter_device_add(dev, &device_info);
866 ret = PTR_ERR(nvdev);
867 device_info.num_chn = orig;
868 nvdev = rndis_filter_device_add(dev, &device_info);
871 netdev_err(net, "restoring channel setting failed: %ld\n",
878 rndis_filter_open(nvdev);
880 /* We may have missed link change notifications */
881 net_device_ctx->last_reconfig = 0;
882 schedule_delayed_work(&net_device_ctx->dwork, 0);
888 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
890 struct ethtool_link_ksettings diff1 = *cmd;
891 struct ethtool_link_ksettings diff2 = {};
893 diff1.base.speed = 0;
894 diff1.base.duplex = 0;
895 /* advertising and cmd are usually set */
896 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
898 /* We set port to PORT_OTHER */
899 diff2.base.port = PORT_OTHER;
901 return !memcmp(&diff1, &diff2, sizeof(diff1));
904 static void netvsc_init_settings(struct net_device *dev)
906 struct net_device_context *ndc = netdev_priv(dev);
908 ndc->l4_hash = HV_DEFAULT_L4HASH;
910 ndc->speed = SPEED_UNKNOWN;
911 ndc->duplex = DUPLEX_FULL;
914 static int netvsc_get_link_ksettings(struct net_device *dev,
915 struct ethtool_link_ksettings *cmd)
917 struct net_device_context *ndc = netdev_priv(dev);
919 cmd->base.speed = ndc->speed;
920 cmd->base.duplex = ndc->duplex;
921 cmd->base.port = PORT_OTHER;
926 static int netvsc_set_link_ksettings(struct net_device *dev,
927 const struct ethtool_link_ksettings *cmd)
929 struct net_device_context *ndc = netdev_priv(dev);
932 speed = cmd->base.speed;
933 if (!ethtool_validate_speed(speed) ||
934 !ethtool_validate_duplex(cmd->base.duplex) ||
935 !netvsc_validate_ethtool_ss_cmd(cmd))
939 ndc->duplex = cmd->base.duplex;
944 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
946 struct net_device_context *ndevctx = netdev_priv(ndev);
947 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
948 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
949 struct hv_device *hdev = ndevctx->device_ctx;
950 int orig_mtu = ndev->mtu;
951 struct netvsc_device_info device_info;
955 if (!nvdev || nvdev->destroy)
958 /* Change MTU of underlying VF netdev first. */
960 ret = dev_set_mtu(vf_netdev, mtu);
965 netif_device_detach(ndev);
966 was_opened = rndis_filter_opened(nvdev);
968 rndis_filter_close(nvdev);
970 memset(&device_info, 0, sizeof(device_info));
971 device_info.num_chn = nvdev->num_chn;
972 device_info.send_sections = nvdev->send_section_cnt;
973 device_info.send_section_size = nvdev->send_section_size;
974 device_info.recv_sections = nvdev->recv_section_cnt;
975 device_info.recv_section_size = nvdev->recv_section_size;
977 rndis_filter_device_remove(hdev, nvdev);
981 nvdev = rndis_filter_device_add(hdev, &device_info);
983 ret = PTR_ERR(nvdev);
985 /* Attempt rollback to original MTU */
986 ndev->mtu = orig_mtu;
987 nvdev = rndis_filter_device_add(hdev, &device_info);
990 dev_set_mtu(vf_netdev, orig_mtu);
993 netdev_err(ndev, "restoring mtu failed: %ld\n",
1000 rndis_filter_open(nvdev);
1002 netif_device_attach(ndev);
1004 /* We may have missed link change notifications */
1005 schedule_delayed_work(&ndevctx->dwork, 0);
1010 static void netvsc_get_vf_stats(struct net_device *net,
1011 struct netvsc_vf_pcpu_stats *tot)
1013 struct net_device_context *ndev_ctx = netdev_priv(net);
1016 memset(tot, 0, sizeof(*tot));
1018 for_each_possible_cpu(i) {
1019 const struct netvsc_vf_pcpu_stats *stats
1020 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1021 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1025 start = u64_stats_fetch_begin_irq(&stats->syncp);
1026 rx_packets = stats->rx_packets;
1027 tx_packets = stats->tx_packets;
1028 rx_bytes = stats->rx_bytes;
1029 tx_bytes = stats->tx_bytes;
1030 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1032 tot->rx_packets += rx_packets;
1033 tot->tx_packets += tx_packets;
1034 tot->rx_bytes += rx_bytes;
1035 tot->tx_bytes += tx_bytes;
1036 tot->tx_dropped += stats->tx_dropped;
1040 static void netvsc_get_stats64(struct net_device *net,
1041 struct rtnl_link_stats64 *t)
1043 struct net_device_context *ndev_ctx = netdev_priv(net);
1044 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1045 struct netvsc_vf_pcpu_stats vf_tot;
1051 netdev_stats_to_stats64(t, &net->stats);
1053 netvsc_get_vf_stats(net, &vf_tot);
1054 t->rx_packets += vf_tot.rx_packets;
1055 t->tx_packets += vf_tot.tx_packets;
1056 t->rx_bytes += vf_tot.rx_bytes;
1057 t->tx_bytes += vf_tot.tx_bytes;
1058 t->tx_dropped += vf_tot.tx_dropped;
1060 for (i = 0; i < nvdev->num_chn; i++) {
1061 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1062 const struct netvsc_stats *stats;
1063 u64 packets, bytes, multicast;
1066 stats = &nvchan->tx_stats;
1068 start = u64_stats_fetch_begin_irq(&stats->syncp);
1069 packets = stats->packets;
1070 bytes = stats->bytes;
1071 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1073 t->tx_bytes += bytes;
1074 t->tx_packets += packets;
1076 stats = &nvchan->rx_stats;
1078 start = u64_stats_fetch_begin_irq(&stats->syncp);
1079 packets = stats->packets;
1080 bytes = stats->bytes;
1081 multicast = stats->multicast + stats->broadcast;
1082 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1084 t->rx_bytes += bytes;
1085 t->rx_packets += packets;
1086 t->multicast += multicast;
1090 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1092 struct net_device_context *ndc = netdev_priv(ndev);
1093 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1094 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1095 struct sockaddr *addr = p;
1098 err = eth_prepare_mac_addr_change(ndev, p);
1106 err = dev_set_mac_address(vf_netdev, addr);
1111 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1113 eth_commit_mac_addr_change(ndev, p);
1114 } else if (vf_netdev) {
1115 /* rollback change on VF */
1116 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1117 dev_set_mac_address(vf_netdev, addr);
1123 static const struct {
1124 char name[ETH_GSTRING_LEN];
1126 } netvsc_stats[] = {
1127 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1128 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1129 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1130 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1131 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1132 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1133 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1134 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1135 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1136 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1138 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1139 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1140 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1141 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1142 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1145 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1146 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1148 /* 4 statistics per queue (rx/tx packets/bytes) */
1149 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1151 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1153 struct net_device_context *ndc = netdev_priv(dev);
1154 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1159 switch (string_set) {
1161 return NETVSC_GLOBAL_STATS_LEN
1162 + NETVSC_VF_STATS_LEN
1163 + NETVSC_QUEUE_STATS_LEN(nvdev);
1169 static void netvsc_get_ethtool_stats(struct net_device *dev,
1170 struct ethtool_stats *stats, u64 *data)
1172 struct net_device_context *ndc = netdev_priv(dev);
1173 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1174 const void *nds = &ndc->eth_stats;
1175 const struct netvsc_stats *qstats;
1176 struct netvsc_vf_pcpu_stats sum;
1184 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1185 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1187 netvsc_get_vf_stats(dev, &sum);
1188 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1189 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1191 for (j = 0; j < nvdev->num_chn; j++) {
1192 qstats = &nvdev->chan_table[j].tx_stats;
1195 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1196 packets = qstats->packets;
1197 bytes = qstats->bytes;
1198 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1199 data[i++] = packets;
1202 qstats = &nvdev->chan_table[j].rx_stats;
1204 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1205 packets = qstats->packets;
1206 bytes = qstats->bytes;
1207 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1208 data[i++] = packets;
1213 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1215 struct net_device_context *ndc = netdev_priv(dev);
1216 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1223 switch (stringset) {
1225 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1226 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1227 p += ETH_GSTRING_LEN;
1230 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1231 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1232 p += ETH_GSTRING_LEN;
1235 for (i = 0; i < nvdev->num_chn; i++) {
1236 sprintf(p, "tx_queue_%u_packets", i);
1237 p += ETH_GSTRING_LEN;
1238 sprintf(p, "tx_queue_%u_bytes", i);
1239 p += ETH_GSTRING_LEN;
1240 sprintf(p, "rx_queue_%u_packets", i);
1241 p += ETH_GSTRING_LEN;
1242 sprintf(p, "rx_queue_%u_bytes", i);
1243 p += ETH_GSTRING_LEN;
1251 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1252 struct ethtool_rxnfc *info)
1254 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1256 info->data = RXH_IP_SRC | RXH_IP_DST;
1258 switch (info->flow_type) {
1260 if (ndc->l4_hash & HV_TCP4_L4HASH)
1261 info->data |= l4_flag;
1266 if (ndc->l4_hash & HV_TCP6_L4HASH)
1267 info->data |= l4_flag;
1272 if (ndc->l4_hash & HV_UDP4_L4HASH)
1273 info->data |= l4_flag;
1278 if (ndc->l4_hash & HV_UDP6_L4HASH)
1279 info->data |= l4_flag;
1295 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1298 struct net_device_context *ndc = netdev_priv(dev);
1299 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1304 switch (info->cmd) {
1305 case ETHTOOL_GRXRINGS:
1306 info->data = nvdev->num_chn;
1310 return netvsc_get_rss_hash_opts(ndc, info);
1315 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1316 struct ethtool_rxnfc *info)
1318 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1319 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1320 switch (info->flow_type) {
1322 ndc->l4_hash |= HV_TCP4_L4HASH;
1326 ndc->l4_hash |= HV_TCP6_L4HASH;
1330 ndc->l4_hash |= HV_UDP4_L4HASH;
1334 ndc->l4_hash |= HV_UDP6_L4HASH;
1344 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1345 switch (info->flow_type) {
1347 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1351 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1355 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1359 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1373 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1375 struct net_device_context *ndc = netdev_priv(ndev);
1377 if (info->cmd == ETHTOOL_SRXFH)
1378 return netvsc_set_rss_hash_opts(ndc, info);
1383 #ifdef CONFIG_NET_POLL_CONTROLLER
1384 static void netvsc_poll_controller(struct net_device *dev)
1386 struct net_device_context *ndc = netdev_priv(dev);
1387 struct netvsc_device *ndev;
1391 ndev = rcu_dereference(ndc->nvdev);
1393 for (i = 0; i < ndev->num_chn; i++) {
1394 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1396 napi_schedule(&nvchan->napi);
1403 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1405 return NETVSC_HASH_KEYLEN;
1408 static u32 netvsc_rss_indir_size(struct net_device *dev)
1413 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1416 struct net_device_context *ndc = netdev_priv(dev);
1417 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1418 struct rndis_device *rndis_dev;
1425 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1427 rndis_dev = ndev->extension;
1429 for (i = 0; i < ITAB_NUM; i++)
1430 indir[i] = rndis_dev->rx_table[i];
1434 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1439 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1440 const u8 *key, const u8 hfunc)
1442 struct net_device_context *ndc = netdev_priv(dev);
1443 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1444 struct rndis_device *rndis_dev;
1450 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1453 rndis_dev = ndev->extension;
1455 for (i = 0; i < ITAB_NUM; i++)
1456 if (indir[i] >= ndev->num_chn)
1459 for (i = 0; i < ITAB_NUM; i++)
1460 rndis_dev->rx_table[i] = indir[i];
1467 key = rndis_dev->rss_key;
1470 return rndis_filter_set_rss_param(rndis_dev, key);
1473 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1474 * It does have pre-allocated receive area which is divided into sections.
1476 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1477 struct ethtool_ringparam *ring)
1481 ring->rx_pending = nvdev->recv_section_cnt;
1482 ring->tx_pending = nvdev->send_section_cnt;
1484 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1485 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1487 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1489 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1490 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1491 / nvdev->send_section_size;
1494 static void netvsc_get_ringparam(struct net_device *ndev,
1495 struct ethtool_ringparam *ring)
1497 struct net_device_context *ndevctx = netdev_priv(ndev);
1498 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1503 __netvsc_get_ringparam(nvdev, ring);
1506 static int netvsc_set_ringparam(struct net_device *ndev,
1507 struct ethtool_ringparam *ring)
1509 struct net_device_context *ndevctx = netdev_priv(ndev);
1510 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1511 struct hv_device *hdev = ndevctx->device_ctx;
1512 struct netvsc_device_info device_info;
1513 struct ethtool_ringparam orig;
1518 if (!nvdev || nvdev->destroy)
1521 memset(&orig, 0, sizeof(orig));
1522 __netvsc_get_ringparam(nvdev, &orig);
1524 new_tx = clamp_t(u32, ring->tx_pending,
1525 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1526 new_rx = clamp_t(u32, ring->rx_pending,
1527 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1529 if (new_tx == orig.tx_pending &&
1530 new_rx == orig.rx_pending)
1531 return 0; /* no change */
1533 memset(&device_info, 0, sizeof(device_info));
1534 device_info.num_chn = nvdev->num_chn;
1535 device_info.send_sections = new_tx;
1536 device_info.send_section_size = nvdev->send_section_size;
1537 device_info.recv_sections = new_rx;
1538 device_info.recv_section_size = nvdev->recv_section_size;
1540 netif_device_detach(ndev);
1541 was_opened = rndis_filter_opened(nvdev);
1543 rndis_filter_close(nvdev);
1545 rndis_filter_device_remove(hdev, nvdev);
1547 nvdev = rndis_filter_device_add(hdev, &device_info);
1548 if (IS_ERR(nvdev)) {
1549 ret = PTR_ERR(nvdev);
1551 device_info.send_sections = orig.tx_pending;
1552 device_info.recv_sections = orig.rx_pending;
1553 nvdev = rndis_filter_device_add(hdev, &device_info);
1554 if (IS_ERR(nvdev)) {
1555 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1562 rndis_filter_open(nvdev);
1563 netif_device_attach(ndev);
1565 /* We may have missed link change notifications */
1566 ndevctx->last_reconfig = 0;
1567 schedule_delayed_work(&ndevctx->dwork, 0);
1572 static const struct ethtool_ops ethtool_ops = {
1573 .get_drvinfo = netvsc_get_drvinfo,
1574 .get_link = ethtool_op_get_link,
1575 .get_ethtool_stats = netvsc_get_ethtool_stats,
1576 .get_sset_count = netvsc_get_sset_count,
1577 .get_strings = netvsc_get_strings,
1578 .get_channels = netvsc_get_channels,
1579 .set_channels = netvsc_set_channels,
1580 .get_ts_info = ethtool_op_get_ts_info,
1581 .get_rxnfc = netvsc_get_rxnfc,
1582 .set_rxnfc = netvsc_set_rxnfc,
1583 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1584 .get_rxfh_indir_size = netvsc_rss_indir_size,
1585 .get_rxfh = netvsc_get_rxfh,
1586 .set_rxfh = netvsc_set_rxfh,
1587 .get_link_ksettings = netvsc_get_link_ksettings,
1588 .set_link_ksettings = netvsc_set_link_ksettings,
1589 .get_ringparam = netvsc_get_ringparam,
1590 .set_ringparam = netvsc_set_ringparam,
1593 static const struct net_device_ops device_ops = {
1594 .ndo_open = netvsc_open,
1595 .ndo_stop = netvsc_close,
1596 .ndo_start_xmit = netvsc_start_xmit,
1597 .ndo_set_rx_mode = netvsc_set_multicast_list,
1598 .ndo_change_mtu = netvsc_change_mtu,
1599 .ndo_validate_addr = eth_validate_addr,
1600 .ndo_set_mac_address = netvsc_set_mac_addr,
1601 .ndo_select_queue = netvsc_select_queue,
1602 .ndo_get_stats64 = netvsc_get_stats64,
1603 #ifdef CONFIG_NET_POLL_CONTROLLER
1604 .ndo_poll_controller = netvsc_poll_controller,
1609 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1610 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1611 * present send GARP packet to network peers with netif_notify_peers().
1613 static void netvsc_link_change(struct work_struct *w)
1615 struct net_device_context *ndev_ctx =
1616 container_of(w, struct net_device_context, dwork.work);
1617 struct hv_device *device_obj = ndev_ctx->device_ctx;
1618 struct net_device *net = hv_get_drvdata(device_obj);
1619 struct netvsc_device *net_device;
1620 struct rndis_device *rdev;
1621 struct netvsc_reconfig *event = NULL;
1622 bool notify = false, reschedule = false;
1623 unsigned long flags, next_reconfig, delay;
1625 /* if changes are happening, comeback later */
1626 if (!rtnl_trylock()) {
1627 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1631 net_device = rtnl_dereference(ndev_ctx->nvdev);
1635 rdev = net_device->extension;
1637 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1638 if (time_is_after_jiffies(next_reconfig)) {
1639 /* link_watch only sends one notification with current state
1640 * per second, avoid doing reconfig more frequently. Handle
1643 delay = next_reconfig - jiffies;
1644 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1645 schedule_delayed_work(&ndev_ctx->dwork, delay);
1648 ndev_ctx->last_reconfig = jiffies;
1650 spin_lock_irqsave(&ndev_ctx->lock, flags);
1651 if (!list_empty(&ndev_ctx->reconfig_events)) {
1652 event = list_first_entry(&ndev_ctx->reconfig_events,
1653 struct netvsc_reconfig, list);
1654 list_del(&event->list);
1655 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1657 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1662 switch (event->event) {
1663 /* Only the following events are possible due to the check in
1664 * netvsc_linkstatus_callback()
1666 case RNDIS_STATUS_MEDIA_CONNECT:
1667 if (rdev->link_state) {
1668 rdev->link_state = false;
1669 netif_carrier_on(net);
1670 netif_tx_wake_all_queues(net);
1676 case RNDIS_STATUS_MEDIA_DISCONNECT:
1677 if (!rdev->link_state) {
1678 rdev->link_state = true;
1679 netif_carrier_off(net);
1680 netif_tx_stop_all_queues(net);
1684 case RNDIS_STATUS_NETWORK_CHANGE:
1685 /* Only makes sense if carrier is present */
1686 if (!rdev->link_state) {
1687 rdev->link_state = true;
1688 netif_carrier_off(net);
1689 netif_tx_stop_all_queues(net);
1690 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1691 spin_lock_irqsave(&ndev_ctx->lock, flags);
1692 list_add(&event->list, &ndev_ctx->reconfig_events);
1693 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1702 netdev_notify_peers(net);
1704 /* link_watch only sends one notification with current state per
1705 * second, handle next reconfig event in 2 seconds.
1708 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1716 static struct net_device *get_netvsc_bymac(const u8 *mac)
1718 struct net_device *dev;
1722 for_each_netdev(&init_net, dev) {
1723 if (dev->netdev_ops != &device_ops)
1724 continue; /* not a netvsc device */
1726 if (ether_addr_equal(mac, dev->perm_addr))
1733 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1735 struct net_device *dev;
1739 for_each_netdev(&init_net, dev) {
1740 struct net_device_context *net_device_ctx;
1742 if (dev->netdev_ops != &device_ops)
1743 continue; /* not a netvsc device */
1745 net_device_ctx = netdev_priv(dev);
1746 if (!rtnl_dereference(net_device_ctx->nvdev))
1747 continue; /* device is removed */
1749 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1750 return dev; /* a match */
1756 /* Called when VF is injecting data into network stack.
1757 * Change the associated network device from VF to netvsc.
1758 * note: already called with rcu_read_lock
1760 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1762 struct sk_buff *skb = *pskb;
1763 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1764 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1765 struct netvsc_vf_pcpu_stats *pcpu_stats
1766 = this_cpu_ptr(ndev_ctx->vf_stats);
1770 u64_stats_update_begin(&pcpu_stats->syncp);
1771 pcpu_stats->rx_packets++;
1772 pcpu_stats->rx_bytes += skb->len;
1773 u64_stats_update_end(&pcpu_stats->syncp);
1775 return RX_HANDLER_ANOTHER;
1778 static int netvsc_vf_join(struct net_device *vf_netdev,
1779 struct net_device *ndev)
1781 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1784 ret = netdev_rx_handler_register(vf_netdev,
1785 netvsc_vf_handle_frame, ndev);
1787 netdev_err(vf_netdev,
1788 "can not register netvsc VF receive handler (err = %d)\n",
1790 goto rx_handler_failed;
1793 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
1795 netdev_err(vf_netdev,
1796 "can not set master device %s (err = %d)\n",
1798 goto upper_link_failed;
1801 /* set slave flag before open to prevent IPv6 addrconf */
1802 vf_netdev->flags |= IFF_SLAVE;
1804 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1806 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1808 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1812 netdev_rx_handler_unregister(vf_netdev);
1817 static void __netvsc_vf_setup(struct net_device *ndev,
1818 struct net_device *vf_netdev)
1822 /* Align MTU of VF with master */
1823 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1825 netdev_warn(vf_netdev,
1826 "unable to change mtu to %u\n", ndev->mtu);
1828 if (netif_running(ndev)) {
1829 ret = dev_open(vf_netdev);
1831 netdev_warn(vf_netdev,
1832 "unable to open: %d\n", ret);
1836 /* Setup VF as slave of the synthetic device.
1837 * Runs in workqueue to avoid recursion in netlink callbacks.
1839 static void netvsc_vf_setup(struct work_struct *w)
1841 struct net_device_context *ndev_ctx
1842 = container_of(w, struct net_device_context, vf_takeover.work);
1843 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1844 struct net_device *vf_netdev;
1846 if (!rtnl_trylock()) {
1847 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1851 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1853 __netvsc_vf_setup(ndev, vf_netdev);
1858 static int netvsc_register_vf(struct net_device *vf_netdev)
1860 struct net_device *ndev;
1861 struct net_device_context *net_device_ctx;
1862 struct netvsc_device *netvsc_dev;
1864 if (vf_netdev->addr_len != ETH_ALEN)
1868 * We will use the MAC address to locate the synthetic interface to
1869 * associate with the VF interface. If we don't find a matching
1870 * synthetic interface, move on.
1872 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1876 net_device_ctx = netdev_priv(ndev);
1877 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1878 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1881 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1884 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1886 dev_hold(vf_netdev);
1887 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1891 /* VF up/down change detected, schedule to change data path */
1892 static int netvsc_vf_changed(struct net_device *vf_netdev)
1894 struct net_device_context *net_device_ctx;
1895 struct netvsc_device *netvsc_dev;
1896 struct net_device *ndev;
1897 bool vf_is_up = netif_running(vf_netdev);
1899 ndev = get_netvsc_byref(vf_netdev);
1903 net_device_ctx = netdev_priv(ndev);
1904 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1908 netvsc_switch_datapath(ndev, vf_is_up);
1909 netdev_info(ndev, "Data path switched %s VF: %s\n",
1910 vf_is_up ? "to" : "from", vf_netdev->name);
1915 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1917 struct net_device *ndev;
1918 struct net_device_context *net_device_ctx;
1920 ndev = get_netvsc_byref(vf_netdev);
1924 net_device_ctx = netdev_priv(ndev);
1925 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1927 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1929 netdev_rx_handler_unregister(vf_netdev);
1930 netdev_upper_dev_unlink(vf_netdev, ndev);
1931 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1937 static int netvsc_probe(struct hv_device *dev,
1938 const struct hv_vmbus_device_id *dev_id)
1940 struct net_device *net = NULL;
1941 struct net_device_context *net_device_ctx;
1942 struct netvsc_device_info device_info;
1943 struct netvsc_device *nvdev;
1946 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1951 netif_carrier_off(net);
1953 netvsc_init_settings(net);
1955 net_device_ctx = netdev_priv(net);
1956 net_device_ctx->device_ctx = dev;
1957 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1958 if (netif_msg_probe(net_device_ctx))
1959 netdev_dbg(net, "netvsc msg_enable: %d\n",
1960 net_device_ctx->msg_enable);
1962 hv_set_drvdata(dev, net);
1964 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1966 spin_lock_init(&net_device_ctx->lock);
1967 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1968 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1970 net_device_ctx->vf_stats
1971 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1972 if (!net_device_ctx->vf_stats)
1975 net->netdev_ops = &device_ops;
1976 net->ethtool_ops = ðtool_ops;
1977 SET_NETDEV_DEV(net, &dev->device);
1979 /* We always need headroom for rndis header */
1980 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1982 /* Initialize the number of queues to be 1, we may change it if more
1983 * channels are offered later.
1985 netif_set_real_num_tx_queues(net, 1);
1986 netif_set_real_num_rx_queues(net, 1);
1988 /* Notify the netvsc driver of the new device */
1989 memset(&device_info, 0, sizeof(device_info));
1990 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1991 device_info.send_sections = NETVSC_DEFAULT_TX;
1992 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
1993 device_info.recv_sections = NETVSC_DEFAULT_RX;
1994 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
1996 nvdev = rndis_filter_device_add(dev, &device_info);
1997 if (IS_ERR(nvdev)) {
1998 ret = PTR_ERR(nvdev);
1999 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2003 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2005 /* hw_features computed in rndis_netdev_set_hwcaps() */
2006 net->features = net->hw_features |
2007 NETIF_F_HIGHDMA | NETIF_F_SG |
2008 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2009 net->vlan_features = net->features;
2011 netdev_lockdep_set_classes(net);
2013 /* MTU range: 68 - 1500 or 65521 */
2014 net->min_mtu = NETVSC_MTU_MIN;
2015 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2016 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2018 net->max_mtu = ETH_DATA_LEN;
2020 ret = register_netdev(net);
2022 pr_err("Unable to register netdev.\n");
2023 goto register_failed;
2029 rndis_filter_device_remove(dev, nvdev);
2031 free_percpu(net_device_ctx->vf_stats);
2033 hv_set_drvdata(dev, NULL);
2039 static int netvsc_remove(struct hv_device *dev)
2041 struct net_device_context *ndev_ctx;
2042 struct net_device *vf_netdev;
2043 struct net_device *net;
2045 net = hv_get_drvdata(dev);
2047 dev_err(&dev->device, "No net device to remove\n");
2051 ndev_ctx = netdev_priv(net);
2053 netif_device_detach(net);
2055 cancel_delayed_work_sync(&ndev_ctx->dwork);
2058 * Call to the vsc driver to let it know that the device is being
2059 * removed. Also blocks mtu and channel changes.
2062 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2064 netvsc_unregister_vf(vf_netdev);
2066 unregister_netdevice(net);
2068 rndis_filter_device_remove(dev,
2069 rtnl_dereference(ndev_ctx->nvdev));
2072 hv_set_drvdata(dev, NULL);
2074 free_percpu(ndev_ctx->vf_stats);
2079 static const struct hv_vmbus_device_id id_table[] = {
2085 MODULE_DEVICE_TABLE(vmbus, id_table);
2087 /* The one and only one */
2088 static struct hv_driver netvsc_drv = {
2089 .name = KBUILD_MODNAME,
2090 .id_table = id_table,
2091 .probe = netvsc_probe,
2092 .remove = netvsc_remove,
2096 * On Hyper-V, every VF interface is matched with a corresponding
2097 * synthetic interface. The synthetic interface is presented first
2098 * to the guest. When the corresponding VF instance is registered,
2099 * we will take care of switching the data path.
2101 static int netvsc_netdev_event(struct notifier_block *this,
2102 unsigned long event, void *ptr)
2104 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2106 /* Skip our own events */
2107 if (event_dev->netdev_ops == &device_ops)
2110 /* Avoid non-Ethernet type devices */
2111 if (event_dev->type != ARPHRD_ETHER)
2114 /* Avoid Vlan dev with same MAC registering as VF */
2115 if (is_vlan_dev(event_dev))
2118 /* Avoid Bonding master dev with same MAC registering as VF */
2119 if ((event_dev->priv_flags & IFF_BONDING) &&
2120 (event_dev->flags & IFF_MASTER))
2124 case NETDEV_REGISTER:
2125 return netvsc_register_vf(event_dev);
2126 case NETDEV_UNREGISTER:
2127 return netvsc_unregister_vf(event_dev);
2130 return netvsc_vf_changed(event_dev);
2136 static struct notifier_block netvsc_netdev_notifier = {
2137 .notifier_call = netvsc_netdev_event,
2140 static void __exit netvsc_drv_exit(void)
2142 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2143 vmbus_driver_unregister(&netvsc_drv);
2146 static int __init netvsc_drv_init(void)
2150 if (ring_size < RING_SIZE_MIN) {
2151 ring_size = RING_SIZE_MIN;
2152 pr_info("Increased ring_size to %u (min allowed)\n",
2155 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2156 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
2158 ret = vmbus_driver_register(&netvsc_drv);
2162 register_netdevice_notifier(&netvsc_netdev_notifier);
2166 MODULE_LICENSE("GPL");
2167 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2169 module_init(netvsc_drv_init);
2170 module_exit(netvsc_drv_exit);