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, 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 net_device *net,
656 struct rndis_message *resp)
658 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
659 struct net_device_context *ndev_ctx = netdev_priv(net);
660 struct netvsc_reconfig *event;
663 /* Update the physical link speed when changing to another vSwitch */
664 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
667 speed = *(u32 *)((void *)indicate
668 + indicate->status_buf_offset) / 10000;
669 ndev_ctx->speed = speed;
673 /* Handle these link change statuses below */
674 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
675 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
676 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
679 if (net->reg_state != NETREG_REGISTERED)
682 event = kzalloc(sizeof(*event), GFP_ATOMIC);
685 event->event = indicate->status;
687 spin_lock_irqsave(&ndev_ctx->lock, flags);
688 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
689 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
691 schedule_delayed_work(&ndev_ctx->dwork, 0);
694 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
695 struct napi_struct *napi,
696 const struct ndis_tcp_ip_checksum_info *csum_info,
697 const struct ndis_pkt_8021q_info *vlan,
698 void *data, u32 buflen)
702 skb = napi_alloc_skb(napi, buflen);
707 * Copy to skb. This copy is needed here since the memory pointed by
708 * hv_netvsc_packet cannot be deallocated
710 skb_put_data(skb, data, buflen);
712 skb->protocol = eth_type_trans(skb, net);
714 /* skb is already created with CHECKSUM_NONE */
715 skb_checksum_none_assert(skb);
718 * In Linux, the IP checksum is always checked.
719 * Do L4 checksum offload if enabled and present.
721 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
722 if (csum_info->receive.tcp_checksum_succeeded ||
723 csum_info->receive.udp_checksum_succeeded)
724 skb->ip_summed = CHECKSUM_UNNECESSARY;
728 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
730 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
738 * netvsc_recv_callback - Callback when we receive a packet from the
739 * "wire" on the specified device.
741 int netvsc_recv_callback(struct net_device *net,
742 struct netvsc_device *net_device,
743 struct vmbus_channel *channel,
745 const struct ndis_tcp_ip_checksum_info *csum_info,
746 const struct ndis_pkt_8021q_info *vlan)
748 struct net_device_context *net_device_ctx = netdev_priv(net);
749 u16 q_idx = channel->offermsg.offer.sub_channel_index;
750 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
752 struct netvsc_stats *rx_stats;
754 if (net->reg_state != NETREG_REGISTERED)
755 return NVSP_STAT_FAIL;
757 /* Allocate a skb - TODO direct I/O to pages? */
758 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
759 csum_info, vlan, data, len);
760 if (unlikely(!skb)) {
761 ++net_device_ctx->eth_stats.rx_no_memory;
763 return NVSP_STAT_FAIL;
766 skb_record_rx_queue(skb, q_idx);
769 * Even if injecting the packet, record the statistics
770 * on the synthetic device because modifying the VF device
771 * statistics will not work correctly.
773 rx_stats = &nvchan->rx_stats;
774 u64_stats_update_begin(&rx_stats->syncp);
776 rx_stats->bytes += len;
778 if (skb->pkt_type == PACKET_BROADCAST)
779 ++rx_stats->broadcast;
780 else if (skb->pkt_type == PACKET_MULTICAST)
781 ++rx_stats->multicast;
782 u64_stats_update_end(&rx_stats->syncp);
784 napi_gro_receive(&nvchan->napi, skb);
788 static void netvsc_get_drvinfo(struct net_device *net,
789 struct ethtool_drvinfo *info)
791 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
792 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
795 static void netvsc_get_channels(struct net_device *net,
796 struct ethtool_channels *channel)
798 struct net_device_context *net_device_ctx = netdev_priv(net);
799 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
802 channel->max_combined = nvdev->max_chn;
803 channel->combined_count = nvdev->num_chn;
807 static int netvsc_set_channels(struct net_device *net,
808 struct ethtool_channels *channels)
810 struct net_device_context *net_device_ctx = netdev_priv(net);
811 struct hv_device *dev = net_device_ctx->device_ctx;
812 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
813 unsigned int orig, count = channels->combined_count;
814 struct netvsc_device_info device_info;
818 /* We do not support separate count for rx, tx, or other */
820 channels->rx_count || channels->tx_count || channels->other_count)
823 if (!nvdev || nvdev->destroy)
826 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
829 if (count > nvdev->max_chn)
832 orig = nvdev->num_chn;
833 was_opened = rndis_filter_opened(nvdev);
835 rndis_filter_close(nvdev);
837 memset(&device_info, 0, sizeof(device_info));
838 device_info.num_chn = count;
839 device_info.send_sections = nvdev->send_section_cnt;
840 device_info.send_section_size = nvdev->send_section_size;
841 device_info.recv_sections = nvdev->recv_section_cnt;
842 device_info.recv_section_size = nvdev->recv_section_size;
844 rndis_filter_device_remove(dev, nvdev);
846 nvdev = rndis_filter_device_add(dev, &device_info);
848 ret = PTR_ERR(nvdev);
849 device_info.num_chn = orig;
850 nvdev = rndis_filter_device_add(dev, &device_info);
853 netdev_err(net, "restoring channel setting failed: %ld\n",
860 rndis_filter_open(nvdev);
862 /* We may have missed link change notifications */
863 net_device_ctx->last_reconfig = 0;
864 schedule_delayed_work(&net_device_ctx->dwork, 0);
870 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
872 struct ethtool_link_ksettings diff1 = *cmd;
873 struct ethtool_link_ksettings diff2 = {};
875 diff1.base.speed = 0;
876 diff1.base.duplex = 0;
877 /* advertising and cmd are usually set */
878 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
880 /* We set port to PORT_OTHER */
881 diff2.base.port = PORT_OTHER;
883 return !memcmp(&diff1, &diff2, sizeof(diff1));
886 static void netvsc_init_settings(struct net_device *dev)
888 struct net_device_context *ndc = netdev_priv(dev);
890 ndc->l4_hash = HV_DEFAULT_L4HASH;
892 ndc->speed = SPEED_UNKNOWN;
893 ndc->duplex = DUPLEX_FULL;
896 static int netvsc_get_link_ksettings(struct net_device *dev,
897 struct ethtool_link_ksettings *cmd)
899 struct net_device_context *ndc = netdev_priv(dev);
901 cmd->base.speed = ndc->speed;
902 cmd->base.duplex = ndc->duplex;
903 cmd->base.port = PORT_OTHER;
908 static int netvsc_set_link_ksettings(struct net_device *dev,
909 const struct ethtool_link_ksettings *cmd)
911 struct net_device_context *ndc = netdev_priv(dev);
914 speed = cmd->base.speed;
915 if (!ethtool_validate_speed(speed) ||
916 !ethtool_validate_duplex(cmd->base.duplex) ||
917 !netvsc_validate_ethtool_ss_cmd(cmd))
921 ndc->duplex = cmd->base.duplex;
926 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
928 struct net_device_context *ndevctx = netdev_priv(ndev);
929 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
930 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
931 struct hv_device *hdev = ndevctx->device_ctx;
932 int orig_mtu = ndev->mtu;
933 struct netvsc_device_info device_info;
937 if (!nvdev || nvdev->destroy)
940 /* Change MTU of underlying VF netdev first. */
942 ret = dev_set_mtu(vf_netdev, mtu);
947 netif_device_detach(ndev);
948 was_opened = rndis_filter_opened(nvdev);
950 rndis_filter_close(nvdev);
952 memset(&device_info, 0, sizeof(device_info));
953 device_info.num_chn = nvdev->num_chn;
954 device_info.send_sections = nvdev->send_section_cnt;
955 device_info.send_section_size = nvdev->send_section_size;
956 device_info.recv_sections = nvdev->recv_section_cnt;
957 device_info.recv_section_size = nvdev->recv_section_size;
959 rndis_filter_device_remove(hdev, nvdev);
963 nvdev = rndis_filter_device_add(hdev, &device_info);
965 ret = PTR_ERR(nvdev);
967 /* Attempt rollback to original MTU */
968 ndev->mtu = orig_mtu;
969 nvdev = rndis_filter_device_add(hdev, &device_info);
972 dev_set_mtu(vf_netdev, orig_mtu);
975 netdev_err(ndev, "restoring mtu failed: %ld\n",
982 rndis_filter_open(nvdev);
984 netif_device_attach(ndev);
986 /* We may have missed link change notifications */
987 schedule_delayed_work(&ndevctx->dwork, 0);
992 static void netvsc_get_vf_stats(struct net_device *net,
993 struct netvsc_vf_pcpu_stats *tot)
995 struct net_device_context *ndev_ctx = netdev_priv(net);
998 memset(tot, 0, sizeof(*tot));
1000 for_each_possible_cpu(i) {
1001 const struct netvsc_vf_pcpu_stats *stats
1002 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1003 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1007 start = u64_stats_fetch_begin_irq(&stats->syncp);
1008 rx_packets = stats->rx_packets;
1009 tx_packets = stats->tx_packets;
1010 rx_bytes = stats->rx_bytes;
1011 tx_bytes = stats->tx_bytes;
1012 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1014 tot->rx_packets += rx_packets;
1015 tot->tx_packets += tx_packets;
1016 tot->rx_bytes += rx_bytes;
1017 tot->tx_bytes += tx_bytes;
1018 tot->tx_dropped += stats->tx_dropped;
1022 static void netvsc_get_stats64(struct net_device *net,
1023 struct rtnl_link_stats64 *t)
1025 struct net_device_context *ndev_ctx = netdev_priv(net);
1026 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1027 struct netvsc_vf_pcpu_stats vf_tot;
1033 netdev_stats_to_stats64(t, &net->stats);
1035 netvsc_get_vf_stats(net, &vf_tot);
1036 t->rx_packets += vf_tot.rx_packets;
1037 t->tx_packets += vf_tot.tx_packets;
1038 t->rx_bytes += vf_tot.rx_bytes;
1039 t->tx_bytes += vf_tot.tx_bytes;
1040 t->tx_dropped += vf_tot.tx_dropped;
1042 for (i = 0; i < nvdev->num_chn; i++) {
1043 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1044 const struct netvsc_stats *stats;
1045 u64 packets, bytes, multicast;
1048 stats = &nvchan->tx_stats;
1050 start = u64_stats_fetch_begin_irq(&stats->syncp);
1051 packets = stats->packets;
1052 bytes = stats->bytes;
1053 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1055 t->tx_bytes += bytes;
1056 t->tx_packets += packets;
1058 stats = &nvchan->rx_stats;
1060 start = u64_stats_fetch_begin_irq(&stats->syncp);
1061 packets = stats->packets;
1062 bytes = stats->bytes;
1063 multicast = stats->multicast + stats->broadcast;
1064 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1066 t->rx_bytes += bytes;
1067 t->rx_packets += packets;
1068 t->multicast += multicast;
1072 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1074 struct net_device_context *ndc = netdev_priv(ndev);
1075 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1076 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1077 struct sockaddr *addr = p;
1080 err = eth_prepare_mac_addr_change(ndev, p);
1088 err = dev_set_mac_address(vf_netdev, addr);
1093 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1095 eth_commit_mac_addr_change(ndev, p);
1096 } else if (vf_netdev) {
1097 /* rollback change on VF */
1098 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1099 dev_set_mac_address(vf_netdev, addr);
1105 static const struct {
1106 char name[ETH_GSTRING_LEN];
1108 } netvsc_stats[] = {
1109 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1110 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1111 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1112 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1113 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1114 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1115 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1116 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1117 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1118 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1120 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1121 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1122 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1123 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1124 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1127 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1128 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1130 /* 4 statistics per queue (rx/tx packets/bytes) */
1131 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1133 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1135 struct net_device_context *ndc = netdev_priv(dev);
1136 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1141 switch (string_set) {
1143 return NETVSC_GLOBAL_STATS_LEN
1144 + NETVSC_VF_STATS_LEN
1145 + NETVSC_QUEUE_STATS_LEN(nvdev);
1151 static void netvsc_get_ethtool_stats(struct net_device *dev,
1152 struct ethtool_stats *stats, u64 *data)
1154 struct net_device_context *ndc = netdev_priv(dev);
1155 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1156 const void *nds = &ndc->eth_stats;
1157 const struct netvsc_stats *qstats;
1158 struct netvsc_vf_pcpu_stats sum;
1166 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1167 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1169 netvsc_get_vf_stats(dev, &sum);
1170 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1171 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1173 for (j = 0; j < nvdev->num_chn; j++) {
1174 qstats = &nvdev->chan_table[j].tx_stats;
1177 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1178 packets = qstats->packets;
1179 bytes = qstats->bytes;
1180 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1181 data[i++] = packets;
1184 qstats = &nvdev->chan_table[j].rx_stats;
1186 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1187 packets = qstats->packets;
1188 bytes = qstats->bytes;
1189 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1190 data[i++] = packets;
1195 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1197 struct net_device_context *ndc = netdev_priv(dev);
1198 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1205 switch (stringset) {
1207 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1208 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1209 p += ETH_GSTRING_LEN;
1212 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1213 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1214 p += ETH_GSTRING_LEN;
1217 for (i = 0; i < nvdev->num_chn; i++) {
1218 sprintf(p, "tx_queue_%u_packets", i);
1219 p += ETH_GSTRING_LEN;
1220 sprintf(p, "tx_queue_%u_bytes", i);
1221 p += ETH_GSTRING_LEN;
1222 sprintf(p, "rx_queue_%u_packets", i);
1223 p += ETH_GSTRING_LEN;
1224 sprintf(p, "rx_queue_%u_bytes", i);
1225 p += ETH_GSTRING_LEN;
1233 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1234 struct ethtool_rxnfc *info)
1236 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1238 info->data = RXH_IP_SRC | RXH_IP_DST;
1240 switch (info->flow_type) {
1242 if (ndc->l4_hash & HV_TCP4_L4HASH)
1243 info->data |= l4_flag;
1248 if (ndc->l4_hash & HV_TCP6_L4HASH)
1249 info->data |= l4_flag;
1254 if (ndc->l4_hash & HV_UDP4_L4HASH)
1255 info->data |= l4_flag;
1260 if (ndc->l4_hash & HV_UDP6_L4HASH)
1261 info->data |= l4_flag;
1277 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1280 struct net_device_context *ndc = netdev_priv(dev);
1281 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1286 switch (info->cmd) {
1287 case ETHTOOL_GRXRINGS:
1288 info->data = nvdev->num_chn;
1292 return netvsc_get_rss_hash_opts(ndc, info);
1297 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1298 struct ethtool_rxnfc *info)
1300 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1301 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1302 switch (info->flow_type) {
1304 ndc->l4_hash |= HV_TCP4_L4HASH;
1308 ndc->l4_hash |= HV_TCP6_L4HASH;
1312 ndc->l4_hash |= HV_UDP4_L4HASH;
1316 ndc->l4_hash |= HV_UDP6_L4HASH;
1326 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1327 switch (info->flow_type) {
1329 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1333 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1337 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1341 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1355 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1357 struct net_device_context *ndc = netdev_priv(ndev);
1359 if (info->cmd == ETHTOOL_SRXFH)
1360 return netvsc_set_rss_hash_opts(ndc, info);
1365 #ifdef CONFIG_NET_POLL_CONTROLLER
1366 static void netvsc_poll_controller(struct net_device *dev)
1368 struct net_device_context *ndc = netdev_priv(dev);
1369 struct netvsc_device *ndev;
1373 ndev = rcu_dereference(ndc->nvdev);
1375 for (i = 0; i < ndev->num_chn; i++) {
1376 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1378 napi_schedule(&nvchan->napi);
1385 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1387 return NETVSC_HASH_KEYLEN;
1390 static u32 netvsc_rss_indir_size(struct net_device *dev)
1395 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1398 struct net_device_context *ndc = netdev_priv(dev);
1399 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1400 struct rndis_device *rndis_dev;
1407 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1409 rndis_dev = ndev->extension;
1411 for (i = 0; i < ITAB_NUM; i++)
1412 indir[i] = rndis_dev->rx_table[i];
1416 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1421 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1422 const u8 *key, const u8 hfunc)
1424 struct net_device_context *ndc = netdev_priv(dev);
1425 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1426 struct rndis_device *rndis_dev;
1432 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1435 rndis_dev = ndev->extension;
1437 for (i = 0; i < ITAB_NUM; i++)
1438 if (indir[i] >= ndev->num_chn)
1441 for (i = 0; i < ITAB_NUM; i++)
1442 rndis_dev->rx_table[i] = indir[i];
1449 key = rndis_dev->rss_key;
1452 return rndis_filter_set_rss_param(rndis_dev, key);
1455 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1456 * It does have pre-allocated receive area which is divided into sections.
1458 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1459 struct ethtool_ringparam *ring)
1463 ring->rx_pending = nvdev->recv_section_cnt;
1464 ring->tx_pending = nvdev->send_section_cnt;
1466 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1467 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1469 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1471 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1472 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1473 / nvdev->send_section_size;
1476 static void netvsc_get_ringparam(struct net_device *ndev,
1477 struct ethtool_ringparam *ring)
1479 struct net_device_context *ndevctx = netdev_priv(ndev);
1480 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1485 __netvsc_get_ringparam(nvdev, ring);
1488 static int netvsc_set_ringparam(struct net_device *ndev,
1489 struct ethtool_ringparam *ring)
1491 struct net_device_context *ndevctx = netdev_priv(ndev);
1492 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1493 struct hv_device *hdev = ndevctx->device_ctx;
1494 struct netvsc_device_info device_info;
1495 struct ethtool_ringparam orig;
1500 if (!nvdev || nvdev->destroy)
1503 memset(&orig, 0, sizeof(orig));
1504 __netvsc_get_ringparam(nvdev, &orig);
1506 new_tx = clamp_t(u32, ring->tx_pending,
1507 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1508 new_rx = clamp_t(u32, ring->rx_pending,
1509 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1511 if (new_tx == orig.tx_pending &&
1512 new_rx == orig.rx_pending)
1513 return 0; /* no change */
1515 memset(&device_info, 0, sizeof(device_info));
1516 device_info.num_chn = nvdev->num_chn;
1517 device_info.send_sections = new_tx;
1518 device_info.send_section_size = nvdev->send_section_size;
1519 device_info.recv_sections = new_rx;
1520 device_info.recv_section_size = nvdev->recv_section_size;
1522 netif_device_detach(ndev);
1523 was_opened = rndis_filter_opened(nvdev);
1525 rndis_filter_close(nvdev);
1527 rndis_filter_device_remove(hdev, nvdev);
1529 nvdev = rndis_filter_device_add(hdev, &device_info);
1530 if (IS_ERR(nvdev)) {
1531 ret = PTR_ERR(nvdev);
1533 device_info.send_sections = orig.tx_pending;
1534 device_info.recv_sections = orig.rx_pending;
1535 nvdev = rndis_filter_device_add(hdev, &device_info);
1536 if (IS_ERR(nvdev)) {
1537 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1544 rndis_filter_open(nvdev);
1545 netif_device_attach(ndev);
1547 /* We may have missed link change notifications */
1548 ndevctx->last_reconfig = 0;
1549 schedule_delayed_work(&ndevctx->dwork, 0);
1554 static const struct ethtool_ops ethtool_ops = {
1555 .get_drvinfo = netvsc_get_drvinfo,
1556 .get_link = ethtool_op_get_link,
1557 .get_ethtool_stats = netvsc_get_ethtool_stats,
1558 .get_sset_count = netvsc_get_sset_count,
1559 .get_strings = netvsc_get_strings,
1560 .get_channels = netvsc_get_channels,
1561 .set_channels = netvsc_set_channels,
1562 .get_ts_info = ethtool_op_get_ts_info,
1563 .get_rxnfc = netvsc_get_rxnfc,
1564 .set_rxnfc = netvsc_set_rxnfc,
1565 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1566 .get_rxfh_indir_size = netvsc_rss_indir_size,
1567 .get_rxfh = netvsc_get_rxfh,
1568 .set_rxfh = netvsc_set_rxfh,
1569 .get_link_ksettings = netvsc_get_link_ksettings,
1570 .set_link_ksettings = netvsc_set_link_ksettings,
1571 .get_ringparam = netvsc_get_ringparam,
1572 .set_ringparam = netvsc_set_ringparam,
1575 static const struct net_device_ops device_ops = {
1576 .ndo_open = netvsc_open,
1577 .ndo_stop = netvsc_close,
1578 .ndo_start_xmit = netvsc_start_xmit,
1579 .ndo_set_rx_mode = netvsc_set_multicast_list,
1580 .ndo_change_mtu = netvsc_change_mtu,
1581 .ndo_validate_addr = eth_validate_addr,
1582 .ndo_set_mac_address = netvsc_set_mac_addr,
1583 .ndo_select_queue = netvsc_select_queue,
1584 .ndo_get_stats64 = netvsc_get_stats64,
1585 #ifdef CONFIG_NET_POLL_CONTROLLER
1586 .ndo_poll_controller = netvsc_poll_controller,
1591 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1592 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1593 * present send GARP packet to network peers with netif_notify_peers().
1595 static void netvsc_link_change(struct work_struct *w)
1597 struct net_device_context *ndev_ctx =
1598 container_of(w, struct net_device_context, dwork.work);
1599 struct hv_device *device_obj = ndev_ctx->device_ctx;
1600 struct net_device *net = hv_get_drvdata(device_obj);
1601 struct netvsc_device *net_device;
1602 struct rndis_device *rdev;
1603 struct netvsc_reconfig *event = NULL;
1604 bool notify = false, reschedule = false;
1605 unsigned long flags, next_reconfig, delay;
1607 /* if changes are happening, comeback later */
1608 if (!rtnl_trylock()) {
1609 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1613 net_device = rtnl_dereference(ndev_ctx->nvdev);
1617 rdev = net_device->extension;
1619 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1620 if (time_is_after_jiffies(next_reconfig)) {
1621 /* link_watch only sends one notification with current state
1622 * per second, avoid doing reconfig more frequently. Handle
1625 delay = next_reconfig - jiffies;
1626 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1627 schedule_delayed_work(&ndev_ctx->dwork, delay);
1630 ndev_ctx->last_reconfig = jiffies;
1632 spin_lock_irqsave(&ndev_ctx->lock, flags);
1633 if (!list_empty(&ndev_ctx->reconfig_events)) {
1634 event = list_first_entry(&ndev_ctx->reconfig_events,
1635 struct netvsc_reconfig, list);
1636 list_del(&event->list);
1637 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1639 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1644 switch (event->event) {
1645 /* Only the following events are possible due to the check in
1646 * netvsc_linkstatus_callback()
1648 case RNDIS_STATUS_MEDIA_CONNECT:
1649 if (rdev->link_state) {
1650 rdev->link_state = false;
1651 netif_carrier_on(net);
1652 netif_tx_wake_all_queues(net);
1658 case RNDIS_STATUS_MEDIA_DISCONNECT:
1659 if (!rdev->link_state) {
1660 rdev->link_state = true;
1661 netif_carrier_off(net);
1662 netif_tx_stop_all_queues(net);
1666 case RNDIS_STATUS_NETWORK_CHANGE:
1667 /* Only makes sense if carrier is present */
1668 if (!rdev->link_state) {
1669 rdev->link_state = true;
1670 netif_carrier_off(net);
1671 netif_tx_stop_all_queues(net);
1672 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1673 spin_lock_irqsave(&ndev_ctx->lock, flags);
1674 list_add(&event->list, &ndev_ctx->reconfig_events);
1675 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1684 netdev_notify_peers(net);
1686 /* link_watch only sends one notification with current state per
1687 * second, handle next reconfig event in 2 seconds.
1690 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1698 static struct net_device *get_netvsc_bymac(const u8 *mac)
1700 struct net_device *dev;
1704 for_each_netdev(&init_net, dev) {
1705 if (dev->netdev_ops != &device_ops)
1706 continue; /* not a netvsc device */
1708 if (ether_addr_equal(mac, dev->perm_addr))
1715 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1717 struct net_device *dev;
1721 for_each_netdev(&init_net, dev) {
1722 struct net_device_context *net_device_ctx;
1724 if (dev->netdev_ops != &device_ops)
1725 continue; /* not a netvsc device */
1727 net_device_ctx = netdev_priv(dev);
1728 if (!rtnl_dereference(net_device_ctx->nvdev))
1729 continue; /* device is removed */
1731 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1732 return dev; /* a match */
1738 /* Called when VF is injecting data into network stack.
1739 * Change the associated network device from VF to netvsc.
1740 * note: already called with rcu_read_lock
1742 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1744 struct sk_buff *skb = *pskb;
1745 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1746 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1747 struct netvsc_vf_pcpu_stats *pcpu_stats
1748 = this_cpu_ptr(ndev_ctx->vf_stats);
1752 u64_stats_update_begin(&pcpu_stats->syncp);
1753 pcpu_stats->rx_packets++;
1754 pcpu_stats->rx_bytes += skb->len;
1755 u64_stats_update_end(&pcpu_stats->syncp);
1757 return RX_HANDLER_ANOTHER;
1760 static int netvsc_vf_join(struct net_device *vf_netdev,
1761 struct net_device *ndev)
1763 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1766 ret = netdev_rx_handler_register(vf_netdev,
1767 netvsc_vf_handle_frame, ndev);
1769 netdev_err(vf_netdev,
1770 "can not register netvsc VF receive handler (err = %d)\n",
1772 goto rx_handler_failed;
1775 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
1777 netdev_err(vf_netdev,
1778 "can not set master device %s (err = %d)\n",
1780 goto upper_link_failed;
1783 /* set slave flag before open to prevent IPv6 addrconf */
1784 vf_netdev->flags |= IFF_SLAVE;
1786 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1788 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1790 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1794 netdev_rx_handler_unregister(vf_netdev);
1799 static void __netvsc_vf_setup(struct net_device *ndev,
1800 struct net_device *vf_netdev)
1804 /* Align MTU of VF with master */
1805 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1807 netdev_warn(vf_netdev,
1808 "unable to change mtu to %u\n", ndev->mtu);
1810 if (netif_running(ndev)) {
1811 ret = dev_open(vf_netdev);
1813 netdev_warn(vf_netdev,
1814 "unable to open: %d\n", ret);
1818 /* Setup VF as slave of the synthetic device.
1819 * Runs in workqueue to avoid recursion in netlink callbacks.
1821 static void netvsc_vf_setup(struct work_struct *w)
1823 struct net_device_context *ndev_ctx
1824 = container_of(w, struct net_device_context, vf_takeover.work);
1825 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1826 struct net_device *vf_netdev;
1828 if (!rtnl_trylock()) {
1829 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1833 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1835 __netvsc_vf_setup(ndev, vf_netdev);
1840 static int netvsc_register_vf(struct net_device *vf_netdev)
1842 struct net_device *ndev;
1843 struct net_device_context *net_device_ctx;
1844 struct netvsc_device *netvsc_dev;
1846 if (vf_netdev->addr_len != ETH_ALEN)
1850 * We will use the MAC address to locate the synthetic interface to
1851 * associate with the VF interface. If we don't find a matching
1852 * synthetic interface, move on.
1854 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1858 net_device_ctx = netdev_priv(ndev);
1859 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1860 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1863 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1866 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1868 dev_hold(vf_netdev);
1869 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1873 /* VF up/down change detected, schedule to change data path */
1874 static int netvsc_vf_changed(struct net_device *vf_netdev)
1876 struct net_device_context *net_device_ctx;
1877 struct netvsc_device *netvsc_dev;
1878 struct net_device *ndev;
1879 bool vf_is_up = netif_running(vf_netdev);
1881 ndev = get_netvsc_byref(vf_netdev);
1885 net_device_ctx = netdev_priv(ndev);
1886 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1890 netvsc_switch_datapath(ndev, vf_is_up);
1891 netdev_info(ndev, "Data path switched %s VF: %s\n",
1892 vf_is_up ? "to" : "from", vf_netdev->name);
1897 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1899 struct net_device *ndev;
1900 struct net_device_context *net_device_ctx;
1902 ndev = get_netvsc_byref(vf_netdev);
1906 net_device_ctx = netdev_priv(ndev);
1907 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1909 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1911 netdev_rx_handler_unregister(vf_netdev);
1912 netdev_upper_dev_unlink(vf_netdev, ndev);
1913 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1919 static int netvsc_probe(struct hv_device *dev,
1920 const struct hv_vmbus_device_id *dev_id)
1922 struct net_device *net = NULL;
1923 struct net_device_context *net_device_ctx;
1924 struct netvsc_device_info device_info;
1925 struct netvsc_device *nvdev;
1928 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1933 netif_carrier_off(net);
1935 netvsc_init_settings(net);
1937 net_device_ctx = netdev_priv(net);
1938 net_device_ctx->device_ctx = dev;
1939 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1940 if (netif_msg_probe(net_device_ctx))
1941 netdev_dbg(net, "netvsc msg_enable: %d\n",
1942 net_device_ctx->msg_enable);
1944 hv_set_drvdata(dev, net);
1946 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1948 spin_lock_init(&net_device_ctx->lock);
1949 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1950 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1952 net_device_ctx->vf_stats
1953 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1954 if (!net_device_ctx->vf_stats)
1957 net->netdev_ops = &device_ops;
1958 net->ethtool_ops = ðtool_ops;
1959 SET_NETDEV_DEV(net, &dev->device);
1961 /* We always need headroom for rndis header */
1962 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1964 /* Initialize the number of queues to be 1, we may change it if more
1965 * channels are offered later.
1967 netif_set_real_num_tx_queues(net, 1);
1968 netif_set_real_num_rx_queues(net, 1);
1970 /* Notify the netvsc driver of the new device */
1971 memset(&device_info, 0, sizeof(device_info));
1972 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1973 device_info.send_sections = NETVSC_DEFAULT_TX;
1974 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
1975 device_info.recv_sections = NETVSC_DEFAULT_RX;
1976 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
1978 nvdev = rndis_filter_device_add(dev, &device_info);
1979 if (IS_ERR(nvdev)) {
1980 ret = PTR_ERR(nvdev);
1981 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1985 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1987 /* hw_features computed in rndis_netdev_set_hwcaps() */
1988 net->features = net->hw_features |
1989 NETIF_F_HIGHDMA | NETIF_F_SG |
1990 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1991 net->vlan_features = net->features;
1993 netdev_lockdep_set_classes(net);
1995 /* MTU range: 68 - 1500 or 65521 */
1996 net->min_mtu = NETVSC_MTU_MIN;
1997 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1998 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2000 net->max_mtu = ETH_DATA_LEN;
2002 ret = register_netdev(net);
2004 pr_err("Unable to register netdev.\n");
2005 goto register_failed;
2011 rndis_filter_device_remove(dev, nvdev);
2013 free_percpu(net_device_ctx->vf_stats);
2015 hv_set_drvdata(dev, NULL);
2021 static int netvsc_remove(struct hv_device *dev)
2023 struct net_device_context *ndev_ctx;
2024 struct net_device *vf_netdev;
2025 struct net_device *net;
2027 net = hv_get_drvdata(dev);
2029 dev_err(&dev->device, "No net device to remove\n");
2033 ndev_ctx = netdev_priv(net);
2035 netif_device_detach(net);
2037 cancel_delayed_work_sync(&ndev_ctx->dwork);
2040 * Call to the vsc driver to let it know that the device is being
2041 * removed. Also blocks mtu and channel changes.
2044 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2046 netvsc_unregister_vf(vf_netdev);
2048 unregister_netdevice(net);
2050 rndis_filter_device_remove(dev,
2051 rtnl_dereference(ndev_ctx->nvdev));
2054 hv_set_drvdata(dev, NULL);
2056 free_percpu(ndev_ctx->vf_stats);
2061 static const struct hv_vmbus_device_id id_table[] = {
2067 MODULE_DEVICE_TABLE(vmbus, id_table);
2069 /* The one and only one */
2070 static struct hv_driver netvsc_drv = {
2071 .name = KBUILD_MODNAME,
2072 .id_table = id_table,
2073 .probe = netvsc_probe,
2074 .remove = netvsc_remove,
2078 * On Hyper-V, every VF interface is matched with a corresponding
2079 * synthetic interface. The synthetic interface is presented first
2080 * to the guest. When the corresponding VF instance is registered,
2081 * we will take care of switching the data path.
2083 static int netvsc_netdev_event(struct notifier_block *this,
2084 unsigned long event, void *ptr)
2086 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2088 /* Skip our own events */
2089 if (event_dev->netdev_ops == &device_ops)
2092 /* Avoid non-Ethernet type devices */
2093 if (event_dev->type != ARPHRD_ETHER)
2096 /* Avoid Vlan dev with same MAC registering as VF */
2097 if (is_vlan_dev(event_dev))
2100 /* Avoid Bonding master dev with same MAC registering as VF */
2101 if ((event_dev->priv_flags & IFF_BONDING) &&
2102 (event_dev->flags & IFF_MASTER))
2106 case NETDEV_REGISTER:
2107 return netvsc_register_vf(event_dev);
2108 case NETDEV_UNREGISTER:
2109 return netvsc_unregister_vf(event_dev);
2112 return netvsc_vf_changed(event_dev);
2118 static struct notifier_block netvsc_netdev_notifier = {
2119 .notifier_call = netvsc_netdev_event,
2122 static void __exit netvsc_drv_exit(void)
2124 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2125 vmbus_driver_unregister(&netvsc_drv);
2128 static int __init netvsc_drv_init(void)
2132 if (ring_size < RING_SIZE_MIN) {
2133 ring_size = RING_SIZE_MIN;
2134 pr_info("Increased ring_size to %u (min allowed)\n",
2137 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2138 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
2140 ret = vmbus_driver_register(&netvsc_drv);
2144 register_netdevice_notifier(&netvsc_netdev_notifier);
2148 MODULE_LICENSE("GPL");
2149 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2151 module_init(netvsc_drv_init);
2152 module_exit(netvsc_drv_exit);