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
50 #define NETVSC_MIN_TX_SECTIONS 10
51 #define NETVSC_DEFAULT_TX 192 /* ~1M */
52 #define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */
53 #define NETVSC_DEFAULT_RX 10485 /* Max ~16M */
55 #define LINKCHANGE_INT (2 * HZ)
56 #define VF_TAKEOVER_INT (HZ / 10)
58 static unsigned int ring_size __ro_after_init = 128;
59 module_param(ring_size, uint, S_IRUGO);
60 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
61 unsigned int netvsc_ring_bytes __ro_after_init;
62 struct reciprocal_value netvsc_ring_reciprocal __ro_after_init;
64 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
65 NETIF_MSG_LINK | NETIF_MSG_IFUP |
66 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
69 static int debug = -1;
70 module_param(debug, int, S_IRUGO);
71 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
73 static void netvsc_set_multicast_list(struct net_device *net)
75 struct net_device_context *net_device_ctx = netdev_priv(net);
76 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
78 rndis_filter_update(nvdev);
81 static int netvsc_open(struct net_device *net)
83 struct net_device_context *ndev_ctx = netdev_priv(net);
84 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
85 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
86 struct rndis_device *rdev;
89 netif_carrier_off(net);
91 /* Open up the device */
92 ret = rndis_filter_open(nvdev);
94 netdev_err(net, "unable to open device (ret %d).\n", ret);
98 netif_tx_wake_all_queues(net);
100 rdev = nvdev->extension;
102 if (!rdev->link_state)
103 netif_carrier_on(net);
106 /* Setting synthetic device up transparently sets
107 * slave as up. If open fails, then slave will be
108 * still be offline (and not used).
110 ret = dev_open(vf_netdev);
113 "unable to open slave: %s: %d\n",
114 vf_netdev->name, ret);
119 static int netvsc_close(struct net_device *net)
121 struct net_device_context *net_device_ctx = netdev_priv(net);
122 struct net_device *vf_netdev
123 = rtnl_dereference(net_device_ctx->vf_netdev);
124 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
126 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
127 struct vmbus_channel *chn;
129 netif_tx_disable(net);
131 /* No need to close rndis filter if it is removed already */
135 ret = rndis_filter_close(nvdev);
137 netdev_err(net, "unable to close device (ret %d).\n", ret);
141 /* Ensure pending bytes in ring are read */
144 for (i = 0; i < nvdev->num_chn; i++) {
145 chn = nvdev->chan_table[i].channel;
149 aread = hv_get_bytes_to_read(&chn->inbound);
153 aread = hv_get_bytes_to_read(&chn->outbound);
159 if (retry > retry_max || aread == 0)
169 netdev_err(net, "Ring buffer not empty after closing rndis\n");
175 dev_close(vf_netdev);
180 static inline void *init_ppi_data(struct rndis_message *msg,
181 u32 ppi_size, u32 pkt_type)
183 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
184 struct rndis_per_packet_info *ppi;
186 rndis_pkt->data_offset += ppi_size;
187 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
188 + rndis_pkt->per_pkt_info_len;
190 ppi->size = ppi_size;
191 ppi->type = pkt_type;
192 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
194 rndis_pkt->per_pkt_info_len += ppi_size;
199 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
200 * packets. We can use ethtool to change UDP hash level when necessary.
202 static inline u32 netvsc_get_hash(
204 const struct net_device_context *ndc)
206 struct flow_keys flow;
207 u32 hash, pkt_proto = 0;
208 static u32 hashrnd __read_mostly;
210 net_get_random_once(&hashrnd, sizeof(hashrnd));
212 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
215 switch (flow.basic.ip_proto) {
217 if (flow.basic.n_proto == htons(ETH_P_IP))
218 pkt_proto = HV_TCP4_L4HASH;
219 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
220 pkt_proto = HV_TCP6_L4HASH;
225 if (flow.basic.n_proto == htons(ETH_P_IP))
226 pkt_proto = HV_UDP4_L4HASH;
227 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
228 pkt_proto = HV_UDP6_L4HASH;
233 if (pkt_proto & ndc->l4_hash) {
234 return skb_get_hash(skb);
236 if (flow.basic.n_proto == htons(ETH_P_IP))
237 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
238 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
239 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
243 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
249 static inline int netvsc_get_tx_queue(struct net_device *ndev,
250 struct sk_buff *skb, int old_idx)
252 const struct net_device_context *ndc = netdev_priv(ndev);
253 struct sock *sk = skb->sk;
256 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
257 (VRSS_SEND_TAB_SIZE - 1)];
259 /* If queue index changed record the new value */
260 if (q_idx != old_idx &&
261 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
262 sk_tx_queue_set(sk, q_idx);
268 * Select queue for transmit.
270 * If a valid queue has already been assigned, then use that.
271 * Otherwise compute tx queue based on hash and the send table.
273 * This is basically similar to default (__netdev_pick_tx) with the added step
274 * of using the host send_table when no other queue has been assigned.
276 * TODO support XPS - but get_xps_queue not exported
278 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
280 int q_idx = sk_tx_queue_get(skb->sk);
282 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
283 /* If forwarding a packet, we use the recorded queue when
284 * available for better cache locality.
286 if (skb_rx_queue_recorded(skb))
287 q_idx = skb_get_rx_queue(skb);
289 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
295 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
297 select_queue_fallback_t fallback)
299 struct net_device_context *ndc = netdev_priv(ndev);
300 struct net_device *vf_netdev;
304 vf_netdev = rcu_dereference(ndc->vf_netdev);
306 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
307 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
309 txq = netvsc_pick_tx(ndev, skb);
313 while (unlikely(txq >= ndev->real_num_tx_queues))
314 txq -= ndev->real_num_tx_queues;
319 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
320 struct hv_page_buffer *pb)
324 /* Deal with compund pages by ignoring unused part
327 page += (offset >> PAGE_SHIFT);
328 offset &= ~PAGE_MASK;
333 bytes = PAGE_SIZE - offset;
336 pb[j].pfn = page_to_pfn(page);
337 pb[j].offset = offset;
343 if (offset == PAGE_SIZE && len) {
353 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
354 struct hv_netvsc_packet *packet,
355 struct hv_page_buffer *pb)
358 char *data = skb->data;
359 int frags = skb_shinfo(skb)->nr_frags;
362 /* The packet is laid out thus:
363 * 1. hdr: RNDIS header and PPI
365 * 3. skb fragment data
367 slots_used += fill_pg_buf(virt_to_page(hdr),
369 len, &pb[slots_used]);
371 packet->rmsg_size = len;
372 packet->rmsg_pgcnt = slots_used;
374 slots_used += fill_pg_buf(virt_to_page(data),
375 offset_in_page(data),
376 skb_headlen(skb), &pb[slots_used]);
378 for (i = 0; i < frags; i++) {
379 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
381 slots_used += fill_pg_buf(skb_frag_page(frag),
383 skb_frag_size(frag), &pb[slots_used]);
388 static int count_skb_frag_slots(struct sk_buff *skb)
390 int i, frags = skb_shinfo(skb)->nr_frags;
393 for (i = 0; i < frags; i++) {
394 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
395 unsigned long size = skb_frag_size(frag);
396 unsigned long offset = frag->page_offset;
398 /* Skip unused frames from start of page */
399 offset &= ~PAGE_MASK;
400 pages += PFN_UP(offset + size);
405 static int netvsc_get_slots(struct sk_buff *skb)
407 char *data = skb->data;
408 unsigned int offset = offset_in_page(data);
409 unsigned int len = skb_headlen(skb);
413 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
414 frag_slots = count_skb_frag_slots(skb);
415 return slots + frag_slots;
418 static u32 net_checksum_info(struct sk_buff *skb)
420 if (skb->protocol == htons(ETH_P_IP)) {
421 struct iphdr *ip = ip_hdr(skb);
423 if (ip->protocol == IPPROTO_TCP)
424 return TRANSPORT_INFO_IPV4_TCP;
425 else if (ip->protocol == IPPROTO_UDP)
426 return TRANSPORT_INFO_IPV4_UDP;
428 struct ipv6hdr *ip6 = ipv6_hdr(skb);
430 if (ip6->nexthdr == IPPROTO_TCP)
431 return TRANSPORT_INFO_IPV6_TCP;
432 else if (ip6->nexthdr == IPPROTO_UDP)
433 return TRANSPORT_INFO_IPV6_UDP;
436 return TRANSPORT_INFO_NOT_IP;
439 /* Send skb on the slave VF device. */
440 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
443 struct net_device_context *ndev_ctx = netdev_priv(net);
444 unsigned int len = skb->len;
447 skb->dev = vf_netdev;
448 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
450 rc = dev_queue_xmit(skb);
451 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
452 struct netvsc_vf_pcpu_stats *pcpu_stats
453 = this_cpu_ptr(ndev_ctx->vf_stats);
455 u64_stats_update_begin(&pcpu_stats->syncp);
456 pcpu_stats->tx_packets++;
457 pcpu_stats->tx_bytes += len;
458 u64_stats_update_end(&pcpu_stats->syncp);
460 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
466 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
468 struct net_device_context *net_device_ctx = netdev_priv(net);
469 struct hv_netvsc_packet *packet = NULL;
471 unsigned int num_data_pgs;
472 struct rndis_message *rndis_msg;
473 struct net_device *vf_netdev;
476 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
478 /* if VF is present and up then redirect packets
479 * already called with rcu_read_lock_bh
481 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
482 if (vf_netdev && netif_running(vf_netdev) &&
483 !netpoll_tx_running(net))
484 return netvsc_vf_xmit(net, vf_netdev, skb);
486 /* We will atmost need two pages to describe the rndis
487 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
488 * of pages in a single packet. If skb is scattered around
489 * more pages we try linearizing it.
492 num_data_pgs = netvsc_get_slots(skb) + 2;
494 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
495 ++net_device_ctx->eth_stats.tx_scattered;
497 if (skb_linearize(skb))
500 num_data_pgs = netvsc_get_slots(skb) + 2;
501 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
502 ++net_device_ctx->eth_stats.tx_too_big;
508 * Place the rndis header in the skb head room and
509 * the skb->cb will be used for hv_netvsc_packet
512 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
516 /* Use the skb control buffer for building up the packet */
517 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
518 FIELD_SIZEOF(struct sk_buff, cb));
519 packet = (struct hv_netvsc_packet *)skb->cb;
521 packet->q_idx = skb_get_queue_mapping(skb);
523 packet->total_data_buflen = skb->len;
524 packet->total_bytes = skb->len;
525 packet->total_packets = 1;
527 rndis_msg = (struct rndis_message *)skb->head;
529 /* Add the rndis header */
530 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
531 rndis_msg->msg_len = packet->total_data_buflen;
533 rndis_msg->msg.pkt = (struct rndis_packet) {
534 .data_offset = sizeof(struct rndis_packet),
535 .data_len = packet->total_data_buflen,
536 .per_pkt_info_offset = sizeof(struct rndis_packet),
539 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
541 hash = skb_get_hash_raw(skb);
542 if (hash != 0 && net->real_num_tx_queues > 1) {
545 rndis_msg_size += NDIS_HASH_PPI_SIZE;
546 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
551 if (skb_vlan_tag_present(skb)) {
552 struct ndis_pkt_8021q_info *vlan;
554 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
555 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
559 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
560 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
564 if (skb_is_gso(skb)) {
565 struct ndis_tcp_lso_info *lso_info;
567 rndis_msg_size += NDIS_LSO_PPI_SIZE;
568 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
569 TCP_LARGESEND_PKTINFO);
572 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
573 if (skb->protocol == htons(ETH_P_IP)) {
574 lso_info->lso_v2_transmit.ip_version =
575 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
576 ip_hdr(skb)->tot_len = 0;
577 ip_hdr(skb)->check = 0;
578 tcp_hdr(skb)->check =
579 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
580 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
582 lso_info->lso_v2_transmit.ip_version =
583 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
584 ipv6_hdr(skb)->payload_len = 0;
585 tcp_hdr(skb)->check =
586 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
587 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
589 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
590 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
591 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
592 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
593 struct ndis_tcp_ip_checksum_info *csum_info;
595 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
596 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
597 TCPIP_CHKSUM_PKTINFO);
599 csum_info->value = 0;
600 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
602 if (skb->protocol == htons(ETH_P_IP)) {
603 csum_info->transmit.is_ipv4 = 1;
605 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
606 csum_info->transmit.tcp_checksum = 1;
608 csum_info->transmit.udp_checksum = 1;
610 csum_info->transmit.is_ipv6 = 1;
612 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
613 csum_info->transmit.tcp_checksum = 1;
615 csum_info->transmit.udp_checksum = 1;
618 /* Can't do offload of this type of checksum */
619 if (skb_checksum_help(skb))
624 /* Start filling in the page buffers with the rndis hdr */
625 rndis_msg->msg_len += rndis_msg_size;
626 packet->total_data_buflen = rndis_msg->msg_len;
627 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
630 /* timestamp packet in software */
631 skb_tx_timestamp(skb);
633 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
634 if (likely(ret == 0))
637 if (ret == -EAGAIN) {
638 ++net_device_ctx->eth_stats.tx_busy;
639 return NETDEV_TX_BUSY;
643 ++net_device_ctx->eth_stats.tx_no_space;
646 dev_kfree_skb_any(skb);
647 net->stats.tx_dropped++;
652 ++net_device_ctx->eth_stats.tx_no_memory;
657 * netvsc_linkstatus_callback - Link up/down notification
659 void netvsc_linkstatus_callback(struct hv_device *device_obj,
660 struct rndis_message *resp)
662 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
663 struct net_device *net;
664 struct net_device_context *ndev_ctx;
665 struct netvsc_reconfig *event;
668 net = hv_get_drvdata(device_obj);
673 ndev_ctx = netdev_priv(net);
675 /* Update the physical link speed when changing to another vSwitch */
676 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
679 speed = *(u32 *)((void *)indicate
680 + indicate->status_buf_offset) / 10000;
681 ndev_ctx->speed = speed;
685 /* Handle these link change statuses below */
686 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
687 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
688 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
691 if (net->reg_state != NETREG_REGISTERED)
694 event = kzalloc(sizeof(*event), GFP_ATOMIC);
697 event->event = indicate->status;
699 spin_lock_irqsave(&ndev_ctx->lock, flags);
700 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
701 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
703 schedule_delayed_work(&ndev_ctx->dwork, 0);
706 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
707 struct napi_struct *napi,
708 const struct ndis_tcp_ip_checksum_info *csum_info,
709 const struct ndis_pkt_8021q_info *vlan,
710 void *data, u32 buflen)
714 skb = napi_alloc_skb(napi, buflen);
719 * Copy to skb. This copy is needed here since the memory pointed by
720 * hv_netvsc_packet cannot be deallocated
722 skb_put_data(skb, data, buflen);
724 skb->protocol = eth_type_trans(skb, net);
726 /* skb is already created with CHECKSUM_NONE */
727 skb_checksum_none_assert(skb);
730 * In Linux, the IP checksum is always checked.
731 * Do L4 checksum offload if enabled and present.
733 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
734 if (csum_info->receive.tcp_checksum_succeeded ||
735 csum_info->receive.udp_checksum_succeeded)
736 skb->ip_summed = CHECKSUM_UNNECESSARY;
740 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
742 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
750 * netvsc_recv_callback - Callback when we receive a packet from the
751 * "wire" on the specified device.
753 int netvsc_recv_callback(struct net_device *net,
754 struct vmbus_channel *channel,
756 const struct ndis_tcp_ip_checksum_info *csum_info,
757 const struct ndis_pkt_8021q_info *vlan)
759 struct net_device_context *net_device_ctx = netdev_priv(net);
760 struct netvsc_device *net_device;
761 u16 q_idx = channel->offermsg.offer.sub_channel_index;
762 struct netvsc_channel *nvchan;
764 struct netvsc_stats *rx_stats;
766 if (net->reg_state != NETREG_REGISTERED)
767 return NVSP_STAT_FAIL;
770 net_device = rcu_dereference(net_device_ctx->nvdev);
771 if (unlikely(!net_device))
774 nvchan = &net_device->chan_table[q_idx];
776 /* Allocate a skb - TODO direct I/O to pages? */
777 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
778 csum_info, vlan, data, len);
779 if (unlikely(!skb)) {
781 ++net->stats.rx_dropped;
783 return NVSP_STAT_FAIL;
786 skb_record_rx_queue(skb, q_idx);
789 * Even if injecting the packet, record the statistics
790 * on the synthetic device because modifying the VF device
791 * statistics will not work correctly.
793 rx_stats = &nvchan->rx_stats;
794 u64_stats_update_begin(&rx_stats->syncp);
796 rx_stats->bytes += len;
798 if (skb->pkt_type == PACKET_BROADCAST)
799 ++rx_stats->broadcast;
800 else if (skb->pkt_type == PACKET_MULTICAST)
801 ++rx_stats->multicast;
802 u64_stats_update_end(&rx_stats->syncp);
804 napi_gro_receive(&nvchan->napi, skb);
810 static void netvsc_get_drvinfo(struct net_device *net,
811 struct ethtool_drvinfo *info)
813 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
814 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
817 static void netvsc_get_channels(struct net_device *net,
818 struct ethtool_channels *channel)
820 struct net_device_context *net_device_ctx = netdev_priv(net);
821 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
824 channel->max_combined = nvdev->max_chn;
825 channel->combined_count = nvdev->num_chn;
829 static int netvsc_set_channels(struct net_device *net,
830 struct ethtool_channels *channels)
832 struct net_device_context *net_device_ctx = netdev_priv(net);
833 struct hv_device *dev = net_device_ctx->device_ctx;
834 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
835 unsigned int orig, count = channels->combined_count;
836 struct netvsc_device_info device_info;
840 /* We do not support separate count for rx, tx, or other */
842 channels->rx_count || channels->tx_count || channels->other_count)
845 if (!nvdev || nvdev->destroy)
848 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
851 if (count > nvdev->max_chn)
854 orig = nvdev->num_chn;
855 was_opened = rndis_filter_opened(nvdev);
857 rndis_filter_close(nvdev);
859 memset(&device_info, 0, sizeof(device_info));
860 device_info.num_chn = count;
861 device_info.send_sections = nvdev->send_section_cnt;
862 device_info.send_section_size = nvdev->send_section_size;
863 device_info.recv_sections = nvdev->recv_section_cnt;
864 device_info.recv_section_size = nvdev->recv_section_size;
866 rndis_filter_device_remove(dev, nvdev);
868 nvdev = rndis_filter_device_add(dev, &device_info);
870 ret = PTR_ERR(nvdev);
871 device_info.num_chn = orig;
872 nvdev = rndis_filter_device_add(dev, &device_info);
875 netdev_err(net, "restoring channel setting failed: %ld\n",
882 rndis_filter_open(nvdev);
884 /* We may have missed link change notifications */
885 net_device_ctx->last_reconfig = 0;
886 schedule_delayed_work(&net_device_ctx->dwork, 0);
892 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
894 struct ethtool_link_ksettings diff1 = *cmd;
895 struct ethtool_link_ksettings diff2 = {};
897 diff1.base.speed = 0;
898 diff1.base.duplex = 0;
899 /* advertising and cmd are usually set */
900 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
902 /* We set port to PORT_OTHER */
903 diff2.base.port = PORT_OTHER;
905 return !memcmp(&diff1, &diff2, sizeof(diff1));
908 static void netvsc_init_settings(struct net_device *dev)
910 struct net_device_context *ndc = netdev_priv(dev);
912 ndc->l4_hash = HV_DEFAULT_L4HASH;
914 ndc->speed = SPEED_UNKNOWN;
915 ndc->duplex = DUPLEX_FULL;
918 static int netvsc_get_link_ksettings(struct net_device *dev,
919 struct ethtool_link_ksettings *cmd)
921 struct net_device_context *ndc = netdev_priv(dev);
923 cmd->base.speed = ndc->speed;
924 cmd->base.duplex = ndc->duplex;
925 cmd->base.port = PORT_OTHER;
930 static int netvsc_set_link_ksettings(struct net_device *dev,
931 const struct ethtool_link_ksettings *cmd)
933 struct net_device_context *ndc = netdev_priv(dev);
936 speed = cmd->base.speed;
937 if (!ethtool_validate_speed(speed) ||
938 !ethtool_validate_duplex(cmd->base.duplex) ||
939 !netvsc_validate_ethtool_ss_cmd(cmd))
943 ndc->duplex = cmd->base.duplex;
948 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
950 struct net_device_context *ndevctx = netdev_priv(ndev);
951 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
952 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
953 struct hv_device *hdev = ndevctx->device_ctx;
954 int orig_mtu = ndev->mtu;
955 struct netvsc_device_info device_info;
959 if (!nvdev || nvdev->destroy)
962 /* Change MTU of underlying VF netdev first. */
964 ret = dev_set_mtu(vf_netdev, mtu);
969 netif_device_detach(ndev);
970 was_opened = rndis_filter_opened(nvdev);
972 rndis_filter_close(nvdev);
974 memset(&device_info, 0, sizeof(device_info));
975 device_info.num_chn = nvdev->num_chn;
976 device_info.send_sections = nvdev->send_section_cnt;
977 device_info.send_section_size = nvdev->send_section_size;
978 device_info.recv_sections = nvdev->recv_section_cnt;
979 device_info.recv_section_size = nvdev->recv_section_size;
981 rndis_filter_device_remove(hdev, nvdev);
985 nvdev = rndis_filter_device_add(hdev, &device_info);
987 ret = PTR_ERR(nvdev);
989 /* Attempt rollback to original MTU */
990 ndev->mtu = orig_mtu;
991 nvdev = rndis_filter_device_add(hdev, &device_info);
994 dev_set_mtu(vf_netdev, orig_mtu);
997 netdev_err(ndev, "restoring mtu failed: %ld\n",
1004 rndis_filter_open(nvdev);
1006 netif_device_attach(ndev);
1008 /* We may have missed link change notifications */
1009 schedule_delayed_work(&ndevctx->dwork, 0);
1014 static void netvsc_get_vf_stats(struct net_device *net,
1015 struct netvsc_vf_pcpu_stats *tot)
1017 struct net_device_context *ndev_ctx = netdev_priv(net);
1020 memset(tot, 0, sizeof(*tot));
1022 for_each_possible_cpu(i) {
1023 const struct netvsc_vf_pcpu_stats *stats
1024 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1025 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1029 start = u64_stats_fetch_begin_irq(&stats->syncp);
1030 rx_packets = stats->rx_packets;
1031 tx_packets = stats->tx_packets;
1032 rx_bytes = stats->rx_bytes;
1033 tx_bytes = stats->tx_bytes;
1034 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1036 tot->rx_packets += rx_packets;
1037 tot->tx_packets += tx_packets;
1038 tot->rx_bytes += rx_bytes;
1039 tot->tx_bytes += tx_bytes;
1040 tot->tx_dropped += stats->tx_dropped;
1044 static void netvsc_get_stats64(struct net_device *net,
1045 struct rtnl_link_stats64 *t)
1047 struct net_device_context *ndev_ctx = netdev_priv(net);
1048 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1049 struct netvsc_vf_pcpu_stats vf_tot;
1055 netdev_stats_to_stats64(t, &net->stats);
1057 netvsc_get_vf_stats(net, &vf_tot);
1058 t->rx_packets += vf_tot.rx_packets;
1059 t->tx_packets += vf_tot.tx_packets;
1060 t->rx_bytes += vf_tot.rx_bytes;
1061 t->tx_bytes += vf_tot.tx_bytes;
1062 t->tx_dropped += vf_tot.tx_dropped;
1064 for (i = 0; i < nvdev->num_chn; i++) {
1065 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1066 const struct netvsc_stats *stats;
1067 u64 packets, bytes, multicast;
1070 stats = &nvchan->tx_stats;
1072 start = u64_stats_fetch_begin_irq(&stats->syncp);
1073 packets = stats->packets;
1074 bytes = stats->bytes;
1075 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1077 t->tx_bytes += bytes;
1078 t->tx_packets += packets;
1080 stats = &nvchan->rx_stats;
1082 start = u64_stats_fetch_begin_irq(&stats->syncp);
1083 packets = stats->packets;
1084 bytes = stats->bytes;
1085 multicast = stats->multicast + stats->broadcast;
1086 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1088 t->rx_bytes += bytes;
1089 t->rx_packets += packets;
1090 t->multicast += multicast;
1094 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1096 struct net_device_context *ndc = netdev_priv(ndev);
1097 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1098 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1099 struct sockaddr *addr = p;
1102 err = eth_prepare_mac_addr_change(ndev, p);
1110 err = dev_set_mac_address(vf_netdev, addr);
1115 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1117 eth_commit_mac_addr_change(ndev, p);
1118 } else if (vf_netdev) {
1119 /* rollback change on VF */
1120 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1121 dev_set_mac_address(vf_netdev, addr);
1127 static const struct {
1128 char name[ETH_GSTRING_LEN];
1130 } netvsc_stats[] = {
1131 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1132 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1133 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1134 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1135 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1136 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1137 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1138 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1139 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1141 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1142 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1143 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1144 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1145 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1148 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1149 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1151 /* 4 statistics per queue (rx/tx packets/bytes) */
1152 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1154 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1156 struct net_device_context *ndc = netdev_priv(dev);
1157 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1162 switch (string_set) {
1164 return NETVSC_GLOBAL_STATS_LEN
1165 + NETVSC_VF_STATS_LEN
1166 + NETVSC_QUEUE_STATS_LEN(nvdev);
1172 static void netvsc_get_ethtool_stats(struct net_device *dev,
1173 struct ethtool_stats *stats, u64 *data)
1175 struct net_device_context *ndc = netdev_priv(dev);
1176 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1177 const void *nds = &ndc->eth_stats;
1178 const struct netvsc_stats *qstats;
1179 struct netvsc_vf_pcpu_stats sum;
1187 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1188 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1190 netvsc_get_vf_stats(dev, &sum);
1191 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1192 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1194 for (j = 0; j < nvdev->num_chn; j++) {
1195 qstats = &nvdev->chan_table[j].tx_stats;
1198 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1199 packets = qstats->packets;
1200 bytes = qstats->bytes;
1201 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1202 data[i++] = packets;
1205 qstats = &nvdev->chan_table[j].rx_stats;
1207 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1208 packets = qstats->packets;
1209 bytes = qstats->bytes;
1210 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1211 data[i++] = packets;
1216 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1218 struct net_device_context *ndc = netdev_priv(dev);
1219 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1226 switch (stringset) {
1228 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1229 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1230 p += ETH_GSTRING_LEN;
1233 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1234 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1235 p += ETH_GSTRING_LEN;
1238 for (i = 0; i < nvdev->num_chn; i++) {
1239 sprintf(p, "tx_queue_%u_packets", i);
1240 p += ETH_GSTRING_LEN;
1241 sprintf(p, "tx_queue_%u_bytes", i);
1242 p += ETH_GSTRING_LEN;
1243 sprintf(p, "rx_queue_%u_packets", i);
1244 p += ETH_GSTRING_LEN;
1245 sprintf(p, "rx_queue_%u_bytes", i);
1246 p += ETH_GSTRING_LEN;
1254 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1255 struct ethtool_rxnfc *info)
1257 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1259 info->data = RXH_IP_SRC | RXH_IP_DST;
1261 switch (info->flow_type) {
1263 if (ndc->l4_hash & HV_TCP4_L4HASH)
1264 info->data |= l4_flag;
1269 if (ndc->l4_hash & HV_TCP6_L4HASH)
1270 info->data |= l4_flag;
1275 if (ndc->l4_hash & HV_UDP4_L4HASH)
1276 info->data |= l4_flag;
1281 if (ndc->l4_hash & HV_UDP6_L4HASH)
1282 info->data |= l4_flag;
1298 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1301 struct net_device_context *ndc = netdev_priv(dev);
1302 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1307 switch (info->cmd) {
1308 case ETHTOOL_GRXRINGS:
1309 info->data = nvdev->num_chn;
1313 return netvsc_get_rss_hash_opts(ndc, info);
1318 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1319 struct ethtool_rxnfc *info)
1321 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1322 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1323 switch (info->flow_type) {
1325 ndc->l4_hash |= HV_TCP4_L4HASH;
1329 ndc->l4_hash |= HV_TCP6_L4HASH;
1333 ndc->l4_hash |= HV_UDP4_L4HASH;
1337 ndc->l4_hash |= HV_UDP6_L4HASH;
1347 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1348 switch (info->flow_type) {
1350 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1354 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1358 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1362 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1376 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1378 struct net_device_context *ndc = netdev_priv(ndev);
1380 if (info->cmd == ETHTOOL_SRXFH)
1381 return netvsc_set_rss_hash_opts(ndc, info);
1386 #ifdef CONFIG_NET_POLL_CONTROLLER
1387 static void netvsc_poll_controller(struct net_device *dev)
1389 struct net_device_context *ndc = netdev_priv(dev);
1390 struct netvsc_device *ndev;
1394 ndev = rcu_dereference(ndc->nvdev);
1396 for (i = 0; i < ndev->num_chn; i++) {
1397 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1399 napi_schedule(&nvchan->napi);
1406 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1408 return NETVSC_HASH_KEYLEN;
1411 static u32 netvsc_rss_indir_size(struct net_device *dev)
1416 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1419 struct net_device_context *ndc = netdev_priv(dev);
1420 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1421 struct rndis_device *rndis_dev;
1428 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1430 rndis_dev = ndev->extension;
1432 for (i = 0; i < ITAB_NUM; i++)
1433 indir[i] = rndis_dev->rx_table[i];
1437 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1442 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1443 const u8 *key, const u8 hfunc)
1445 struct net_device_context *ndc = netdev_priv(dev);
1446 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1447 struct rndis_device *rndis_dev;
1453 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1456 rndis_dev = ndev->extension;
1458 for (i = 0; i < ITAB_NUM; i++)
1459 if (indir[i] >= ndev->num_chn)
1462 for (i = 0; i < ITAB_NUM; i++)
1463 rndis_dev->rx_table[i] = indir[i];
1470 key = rndis_dev->rss_key;
1473 return rndis_filter_set_rss_param(rndis_dev, key);
1476 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1477 * It does have pre-allocated receive area which is divided into sections.
1479 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1480 struct ethtool_ringparam *ring)
1484 ring->rx_pending = nvdev->recv_section_cnt;
1485 ring->tx_pending = nvdev->send_section_cnt;
1487 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1488 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1490 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1492 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1493 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1494 / nvdev->send_section_size;
1497 static void netvsc_get_ringparam(struct net_device *ndev,
1498 struct ethtool_ringparam *ring)
1500 struct net_device_context *ndevctx = netdev_priv(ndev);
1501 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1506 __netvsc_get_ringparam(nvdev, ring);
1509 static int netvsc_set_ringparam(struct net_device *ndev,
1510 struct ethtool_ringparam *ring)
1512 struct net_device_context *ndevctx = netdev_priv(ndev);
1513 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1514 struct hv_device *hdev = ndevctx->device_ctx;
1515 struct netvsc_device_info device_info;
1516 struct ethtool_ringparam orig;
1521 if (!nvdev || nvdev->destroy)
1524 memset(&orig, 0, sizeof(orig));
1525 __netvsc_get_ringparam(nvdev, &orig);
1527 new_tx = clamp_t(u32, ring->tx_pending,
1528 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1529 new_rx = clamp_t(u32, ring->rx_pending,
1530 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1532 if (new_tx == orig.tx_pending &&
1533 new_rx == orig.rx_pending)
1534 return 0; /* no change */
1536 memset(&device_info, 0, sizeof(device_info));
1537 device_info.num_chn = nvdev->num_chn;
1538 device_info.send_sections = new_tx;
1539 device_info.send_section_size = nvdev->send_section_size;
1540 device_info.recv_sections = new_rx;
1541 device_info.recv_section_size = nvdev->recv_section_size;
1543 netif_device_detach(ndev);
1544 was_opened = rndis_filter_opened(nvdev);
1546 rndis_filter_close(nvdev);
1548 rndis_filter_device_remove(hdev, nvdev);
1550 nvdev = rndis_filter_device_add(hdev, &device_info);
1551 if (IS_ERR(nvdev)) {
1552 ret = PTR_ERR(nvdev);
1554 device_info.send_sections = orig.tx_pending;
1555 device_info.recv_sections = orig.rx_pending;
1556 nvdev = rndis_filter_device_add(hdev, &device_info);
1557 if (IS_ERR(nvdev)) {
1558 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1565 rndis_filter_open(nvdev);
1566 netif_device_attach(ndev);
1568 /* We may have missed link change notifications */
1569 ndevctx->last_reconfig = 0;
1570 schedule_delayed_work(&ndevctx->dwork, 0);
1575 static const struct ethtool_ops ethtool_ops = {
1576 .get_drvinfo = netvsc_get_drvinfo,
1577 .get_link = ethtool_op_get_link,
1578 .get_ethtool_stats = netvsc_get_ethtool_stats,
1579 .get_sset_count = netvsc_get_sset_count,
1580 .get_strings = netvsc_get_strings,
1581 .get_channels = netvsc_get_channels,
1582 .set_channels = netvsc_set_channels,
1583 .get_ts_info = ethtool_op_get_ts_info,
1584 .get_rxnfc = netvsc_get_rxnfc,
1585 .set_rxnfc = netvsc_set_rxnfc,
1586 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1587 .get_rxfh_indir_size = netvsc_rss_indir_size,
1588 .get_rxfh = netvsc_get_rxfh,
1589 .set_rxfh = netvsc_set_rxfh,
1590 .get_link_ksettings = netvsc_get_link_ksettings,
1591 .set_link_ksettings = netvsc_set_link_ksettings,
1592 .get_ringparam = netvsc_get_ringparam,
1593 .set_ringparam = netvsc_set_ringparam,
1596 static const struct net_device_ops device_ops = {
1597 .ndo_open = netvsc_open,
1598 .ndo_stop = netvsc_close,
1599 .ndo_start_xmit = netvsc_start_xmit,
1600 .ndo_set_rx_mode = netvsc_set_multicast_list,
1601 .ndo_change_mtu = netvsc_change_mtu,
1602 .ndo_validate_addr = eth_validate_addr,
1603 .ndo_set_mac_address = netvsc_set_mac_addr,
1604 .ndo_select_queue = netvsc_select_queue,
1605 .ndo_get_stats64 = netvsc_get_stats64,
1606 #ifdef CONFIG_NET_POLL_CONTROLLER
1607 .ndo_poll_controller = netvsc_poll_controller,
1612 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1613 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1614 * present send GARP packet to network peers with netif_notify_peers().
1616 static void netvsc_link_change(struct work_struct *w)
1618 struct net_device_context *ndev_ctx =
1619 container_of(w, struct net_device_context, dwork.work);
1620 struct hv_device *device_obj = ndev_ctx->device_ctx;
1621 struct net_device *net = hv_get_drvdata(device_obj);
1622 struct netvsc_device *net_device;
1623 struct rndis_device *rdev;
1624 struct netvsc_reconfig *event = NULL;
1625 bool notify = false, reschedule = false;
1626 unsigned long flags, next_reconfig, delay;
1628 /* if changes are happening, comeback later */
1629 if (!rtnl_trylock()) {
1630 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1634 net_device = rtnl_dereference(ndev_ctx->nvdev);
1638 rdev = net_device->extension;
1640 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1641 if (time_is_after_jiffies(next_reconfig)) {
1642 /* link_watch only sends one notification with current state
1643 * per second, avoid doing reconfig more frequently. Handle
1646 delay = next_reconfig - jiffies;
1647 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1648 schedule_delayed_work(&ndev_ctx->dwork, delay);
1651 ndev_ctx->last_reconfig = jiffies;
1653 spin_lock_irqsave(&ndev_ctx->lock, flags);
1654 if (!list_empty(&ndev_ctx->reconfig_events)) {
1655 event = list_first_entry(&ndev_ctx->reconfig_events,
1656 struct netvsc_reconfig, list);
1657 list_del(&event->list);
1658 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1660 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1665 switch (event->event) {
1666 /* Only the following events are possible due to the check in
1667 * netvsc_linkstatus_callback()
1669 case RNDIS_STATUS_MEDIA_CONNECT:
1670 if (rdev->link_state) {
1671 rdev->link_state = false;
1672 netif_carrier_on(net);
1673 netif_tx_wake_all_queues(net);
1679 case RNDIS_STATUS_MEDIA_DISCONNECT:
1680 if (!rdev->link_state) {
1681 rdev->link_state = true;
1682 netif_carrier_off(net);
1683 netif_tx_stop_all_queues(net);
1687 case RNDIS_STATUS_NETWORK_CHANGE:
1688 /* Only makes sense if carrier is present */
1689 if (!rdev->link_state) {
1690 rdev->link_state = true;
1691 netif_carrier_off(net);
1692 netif_tx_stop_all_queues(net);
1693 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1694 spin_lock_irqsave(&ndev_ctx->lock, flags);
1695 list_add(&event->list, &ndev_ctx->reconfig_events);
1696 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1705 netdev_notify_peers(net);
1707 /* link_watch only sends one notification with current state per
1708 * second, handle next reconfig event in 2 seconds.
1711 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1719 static struct net_device *get_netvsc_bymac(const u8 *mac)
1721 struct net_device *dev;
1725 for_each_netdev(&init_net, dev) {
1726 if (dev->netdev_ops != &device_ops)
1727 continue; /* not a netvsc device */
1729 if (ether_addr_equal(mac, dev->perm_addr))
1736 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1738 struct net_device *dev;
1742 for_each_netdev(&init_net, dev) {
1743 struct net_device_context *net_device_ctx;
1745 if (dev->netdev_ops != &device_ops)
1746 continue; /* not a netvsc device */
1748 net_device_ctx = netdev_priv(dev);
1749 if (!rtnl_dereference(net_device_ctx->nvdev))
1750 continue; /* device is removed */
1752 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1753 return dev; /* a match */
1759 /* Called when VF is injecting data into network stack.
1760 * Change the associated network device from VF to netvsc.
1761 * note: already called with rcu_read_lock
1763 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1765 struct sk_buff *skb = *pskb;
1766 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1767 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1768 struct netvsc_vf_pcpu_stats *pcpu_stats
1769 = this_cpu_ptr(ndev_ctx->vf_stats);
1773 u64_stats_update_begin(&pcpu_stats->syncp);
1774 pcpu_stats->rx_packets++;
1775 pcpu_stats->rx_bytes += skb->len;
1776 u64_stats_update_end(&pcpu_stats->syncp);
1778 return RX_HANDLER_ANOTHER;
1781 static int netvsc_vf_join(struct net_device *vf_netdev,
1782 struct net_device *ndev)
1784 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1787 ret = netdev_rx_handler_register(vf_netdev,
1788 netvsc_vf_handle_frame, ndev);
1790 netdev_err(vf_netdev,
1791 "can not register netvsc VF receive handler (err = %d)\n",
1793 goto rx_handler_failed;
1796 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
1798 netdev_err(vf_netdev,
1799 "can not set master device %s (err = %d)\n",
1801 goto upper_link_failed;
1804 /* set slave flag before open to prevent IPv6 addrconf */
1805 vf_netdev->flags |= IFF_SLAVE;
1807 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1809 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1811 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1815 netdev_rx_handler_unregister(vf_netdev);
1820 static void __netvsc_vf_setup(struct net_device *ndev,
1821 struct net_device *vf_netdev)
1825 /* Align MTU of VF with master */
1826 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1828 netdev_warn(vf_netdev,
1829 "unable to change mtu to %u\n", ndev->mtu);
1831 if (netif_running(ndev)) {
1832 ret = dev_open(vf_netdev);
1834 netdev_warn(vf_netdev,
1835 "unable to open: %d\n", ret);
1839 /* Setup VF as slave of the synthetic device.
1840 * Runs in workqueue to avoid recursion in netlink callbacks.
1842 static void netvsc_vf_setup(struct work_struct *w)
1844 struct net_device_context *ndev_ctx
1845 = container_of(w, struct net_device_context, vf_takeover.work);
1846 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1847 struct net_device *vf_netdev;
1849 if (!rtnl_trylock()) {
1850 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1854 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1856 __netvsc_vf_setup(ndev, vf_netdev);
1861 static int netvsc_register_vf(struct net_device *vf_netdev)
1863 struct net_device *ndev;
1864 struct net_device_context *net_device_ctx;
1865 struct netvsc_device *netvsc_dev;
1867 if (vf_netdev->addr_len != ETH_ALEN)
1871 * We will use the MAC address to locate the synthetic interface to
1872 * associate with the VF interface. If we don't find a matching
1873 * synthetic interface, move on.
1875 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1879 net_device_ctx = netdev_priv(ndev);
1880 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1881 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1884 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1887 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1889 dev_hold(vf_netdev);
1890 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1894 /* VF up/down change detected, schedule to change data path */
1895 static int netvsc_vf_changed(struct net_device *vf_netdev)
1897 struct net_device_context *net_device_ctx;
1898 struct netvsc_device *netvsc_dev;
1899 struct net_device *ndev;
1900 bool vf_is_up = netif_running(vf_netdev);
1902 ndev = get_netvsc_byref(vf_netdev);
1906 net_device_ctx = netdev_priv(ndev);
1907 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1911 netvsc_switch_datapath(ndev, vf_is_up);
1912 netdev_info(ndev, "Data path switched %s VF: %s\n",
1913 vf_is_up ? "to" : "from", vf_netdev->name);
1918 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1920 struct net_device *ndev;
1921 struct net_device_context *net_device_ctx;
1923 ndev = get_netvsc_byref(vf_netdev);
1927 net_device_ctx = netdev_priv(ndev);
1928 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1930 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1932 netdev_rx_handler_unregister(vf_netdev);
1933 netdev_upper_dev_unlink(vf_netdev, ndev);
1934 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1940 static int netvsc_probe(struct hv_device *dev,
1941 const struct hv_vmbus_device_id *dev_id)
1943 struct net_device *net = NULL;
1944 struct net_device_context *net_device_ctx;
1945 struct netvsc_device_info device_info;
1946 struct netvsc_device *nvdev;
1949 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1954 netif_carrier_off(net);
1956 netvsc_init_settings(net);
1958 net_device_ctx = netdev_priv(net);
1959 net_device_ctx->device_ctx = dev;
1960 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1961 if (netif_msg_probe(net_device_ctx))
1962 netdev_dbg(net, "netvsc msg_enable: %d\n",
1963 net_device_ctx->msg_enable);
1965 hv_set_drvdata(dev, net);
1967 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1969 spin_lock_init(&net_device_ctx->lock);
1970 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1971 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1973 net_device_ctx->vf_stats
1974 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1975 if (!net_device_ctx->vf_stats)
1978 net->netdev_ops = &device_ops;
1979 net->ethtool_ops = ðtool_ops;
1980 SET_NETDEV_DEV(net, &dev->device);
1982 /* We always need headroom for rndis header */
1983 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1985 /* Initialize the number of queues to be 1, we may change it if more
1986 * channels are offered later.
1988 netif_set_real_num_tx_queues(net, 1);
1989 netif_set_real_num_rx_queues(net, 1);
1991 /* Notify the netvsc driver of the new device */
1992 memset(&device_info, 0, sizeof(device_info));
1993 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1994 device_info.send_sections = NETVSC_DEFAULT_TX;
1995 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
1996 device_info.recv_sections = NETVSC_DEFAULT_RX;
1997 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
1999 nvdev = rndis_filter_device_add(dev, &device_info);
2000 if (IS_ERR(nvdev)) {
2001 ret = PTR_ERR(nvdev);
2002 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2006 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2008 /* hw_features computed in rndis_netdev_set_hwcaps() */
2009 net->features = net->hw_features |
2010 NETIF_F_HIGHDMA | NETIF_F_SG |
2011 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2012 net->vlan_features = net->features;
2014 netdev_lockdep_set_classes(net);
2016 /* MTU range: 68 - 1500 or 65521 */
2017 net->min_mtu = NETVSC_MTU_MIN;
2018 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2019 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2021 net->max_mtu = ETH_DATA_LEN;
2023 ret = register_netdev(net);
2025 pr_err("Unable to register netdev.\n");
2026 goto register_failed;
2032 rndis_filter_device_remove(dev, nvdev);
2034 free_percpu(net_device_ctx->vf_stats);
2036 hv_set_drvdata(dev, NULL);
2042 static int netvsc_remove(struct hv_device *dev)
2044 struct net_device_context *ndev_ctx;
2045 struct net_device *vf_netdev;
2046 struct net_device *net;
2048 net = hv_get_drvdata(dev);
2050 dev_err(&dev->device, "No net device to remove\n");
2054 ndev_ctx = netdev_priv(net);
2056 netif_device_detach(net);
2058 cancel_delayed_work_sync(&ndev_ctx->dwork);
2061 * Call to the vsc driver to let it know that the device is being
2062 * removed. Also blocks mtu and channel changes.
2065 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2067 netvsc_unregister_vf(vf_netdev);
2069 unregister_netdevice(net);
2071 rndis_filter_device_remove(dev,
2072 rtnl_dereference(ndev_ctx->nvdev));
2075 hv_set_drvdata(dev, NULL);
2077 free_percpu(ndev_ctx->vf_stats);
2082 static const struct hv_vmbus_device_id id_table[] = {
2088 MODULE_DEVICE_TABLE(vmbus, id_table);
2090 /* The one and only one */
2091 static struct hv_driver netvsc_drv = {
2092 .name = KBUILD_MODNAME,
2093 .id_table = id_table,
2094 .probe = netvsc_probe,
2095 .remove = netvsc_remove,
2099 * On Hyper-V, every VF interface is matched with a corresponding
2100 * synthetic interface. The synthetic interface is presented first
2101 * to the guest. When the corresponding VF instance is registered,
2102 * we will take care of switching the data path.
2104 static int netvsc_netdev_event(struct notifier_block *this,
2105 unsigned long event, void *ptr)
2107 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2109 /* Skip our own events */
2110 if (event_dev->netdev_ops == &device_ops)
2113 /* Avoid non-Ethernet type devices */
2114 if (event_dev->type != ARPHRD_ETHER)
2117 /* Avoid Vlan dev with same MAC registering as VF */
2118 if (is_vlan_dev(event_dev))
2121 /* Avoid Bonding master dev with same MAC registering as VF */
2122 if ((event_dev->priv_flags & IFF_BONDING) &&
2123 (event_dev->flags & IFF_MASTER))
2127 case NETDEV_REGISTER:
2128 return netvsc_register_vf(event_dev);
2129 case NETDEV_UNREGISTER:
2130 return netvsc_unregister_vf(event_dev);
2133 return netvsc_vf_changed(event_dev);
2139 static struct notifier_block netvsc_netdev_notifier = {
2140 .notifier_call = netvsc_netdev_event,
2143 static void __exit netvsc_drv_exit(void)
2145 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2146 vmbus_driver_unregister(&netvsc_drv);
2149 static int __init netvsc_drv_init(void)
2153 if (ring_size < RING_SIZE_MIN) {
2154 ring_size = RING_SIZE_MIN;
2155 pr_info("Increased ring_size to %u (min allowed)\n",
2158 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2159 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
2161 ret = vmbus_driver_register(&netvsc_drv);
2165 register_netdevice_notifier(&netvsc_netdev_notifier);
2169 MODULE_LICENSE("GPL");
2170 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2172 module_init(netvsc_drv_init);
2173 module_exit(netvsc_drv_exit);