1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/atomic.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/netdevice.h>
19 #include <linux/inetdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/pci.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_vlan.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/netpoll.h>
28 #include <linux/bpf.h>
31 #include <net/route.h>
33 #include <net/pkt_sched.h>
34 #include <net/checksum.h>
35 #include <net/ip6_checksum.h>
37 #include "hyperv_net.h"
39 #define RING_SIZE_MIN 64
40 #define RETRY_US_LO 5000
41 #define RETRY_US_HI 10000
42 #define RETRY_MAX 2000 /* >10 sec */
44 #define LINKCHANGE_INT (2 * HZ)
45 #define VF_TAKEOVER_INT (HZ / 10)
47 static unsigned int ring_size __ro_after_init = 128;
48 module_param(ring_size, uint, 0444);
49 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
50 unsigned int netvsc_ring_bytes __ro_after_init;
52 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
53 NETIF_MSG_LINK | NETIF_MSG_IFUP |
54 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
57 static int debug = -1;
58 module_param(debug, int, 0444);
59 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
61 static LIST_HEAD(netvsc_dev_list);
63 static void netvsc_change_rx_flags(struct net_device *net, int change)
65 struct net_device_context *ndev_ctx = netdev_priv(net);
66 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
72 if (change & IFF_PROMISC) {
73 inc = (net->flags & IFF_PROMISC) ? 1 : -1;
74 dev_set_promiscuity(vf_netdev, inc);
77 if (change & IFF_ALLMULTI) {
78 inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
79 dev_set_allmulti(vf_netdev, inc);
83 static void netvsc_set_rx_mode(struct net_device *net)
85 struct net_device_context *ndev_ctx = netdev_priv(net);
86 struct net_device *vf_netdev;
87 struct netvsc_device *nvdev;
90 vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
92 dev_uc_sync(vf_netdev, net);
93 dev_mc_sync(vf_netdev, net);
96 nvdev = rcu_dereference(ndev_ctx->nvdev);
98 rndis_filter_update(nvdev);
102 static void netvsc_tx_enable(struct netvsc_device *nvscdev,
103 struct net_device *ndev)
105 nvscdev->tx_disable = false;
106 virt_wmb(); /* ensure queue wake up mechanism is on */
108 netif_tx_wake_all_queues(ndev);
111 static int netvsc_open(struct net_device *net)
113 struct net_device_context *ndev_ctx = netdev_priv(net);
114 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
115 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
116 struct rndis_device *rdev;
119 netif_carrier_off(net);
121 /* Open up the device */
122 ret = rndis_filter_open(nvdev);
124 netdev_err(net, "unable to open device (ret %d).\n", ret);
128 rdev = nvdev->extension;
129 if (!rdev->link_state) {
130 netif_carrier_on(net);
131 netvsc_tx_enable(nvdev, net);
135 /* Setting synthetic device up transparently sets
136 * slave as up. If open fails, then slave will be
137 * still be offline (and not used).
139 ret = dev_open(vf_netdev, NULL);
142 "unable to open slave: %s: %d\n",
143 vf_netdev->name, ret);
148 static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
150 unsigned int retry = 0;
153 /* Ensure pending bytes in ring are read */
157 for (i = 0; i < nvdev->num_chn; i++) {
158 struct vmbus_channel *chn
159 = nvdev->chan_table[i].channel;
164 /* make sure receive not running now */
165 napi_synchronize(&nvdev->chan_table[i].napi);
167 aread = hv_get_bytes_to_read(&chn->inbound);
171 aread = hv_get_bytes_to_read(&chn->outbound);
179 if (++retry > RETRY_MAX)
182 usleep_range(RETRY_US_LO, RETRY_US_HI);
186 static void netvsc_tx_disable(struct netvsc_device *nvscdev,
187 struct net_device *ndev)
190 nvscdev->tx_disable = true;
191 virt_wmb(); /* ensure txq will not wake up after stop */
194 netif_tx_disable(ndev);
197 static int netvsc_close(struct net_device *net)
199 struct net_device_context *net_device_ctx = netdev_priv(net);
200 struct net_device *vf_netdev
201 = rtnl_dereference(net_device_ctx->vf_netdev);
202 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
205 netvsc_tx_disable(nvdev, net);
207 /* No need to close rndis filter if it is removed already */
211 ret = rndis_filter_close(nvdev);
213 netdev_err(net, "unable to close device (ret %d).\n", ret);
217 ret = netvsc_wait_until_empty(nvdev);
219 netdev_err(net, "Ring buffer not empty after closing rndis\n");
222 dev_close(vf_netdev);
227 static inline void *init_ppi_data(struct rndis_message *msg,
228 u32 ppi_size, u32 pkt_type)
230 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
231 struct rndis_per_packet_info *ppi;
233 rndis_pkt->data_offset += ppi_size;
234 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
235 + rndis_pkt->per_pkt_info_len;
237 ppi->size = ppi_size;
238 ppi->type = pkt_type;
240 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
242 rndis_pkt->per_pkt_info_len += ppi_size;
247 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
248 * packets. We can use ethtool to change UDP hash level when necessary.
250 static inline u32 netvsc_get_hash(
252 const struct net_device_context *ndc)
254 struct flow_keys flow;
255 u32 hash, pkt_proto = 0;
256 static u32 hashrnd __read_mostly;
258 net_get_random_once(&hashrnd, sizeof(hashrnd));
260 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
263 switch (flow.basic.ip_proto) {
265 if (flow.basic.n_proto == htons(ETH_P_IP))
266 pkt_proto = HV_TCP4_L4HASH;
267 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
268 pkt_proto = HV_TCP6_L4HASH;
273 if (flow.basic.n_proto == htons(ETH_P_IP))
274 pkt_proto = HV_UDP4_L4HASH;
275 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
276 pkt_proto = HV_UDP6_L4HASH;
281 if (pkt_proto & ndc->l4_hash) {
282 return skb_get_hash(skb);
284 if (flow.basic.n_proto == htons(ETH_P_IP))
285 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
286 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
287 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
291 __skb_set_sw_hash(skb, hash, false);
297 static inline int netvsc_get_tx_queue(struct net_device *ndev,
298 struct sk_buff *skb, int old_idx)
300 const struct net_device_context *ndc = netdev_priv(ndev);
301 struct sock *sk = skb->sk;
304 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
305 (VRSS_SEND_TAB_SIZE - 1)];
307 /* If queue index changed record the new value */
308 if (q_idx != old_idx &&
309 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
310 sk_tx_queue_set(sk, q_idx);
316 * Select queue for transmit.
318 * If a valid queue has already been assigned, then use that.
319 * Otherwise compute tx queue based on hash and the send table.
321 * This is basically similar to default (netdev_pick_tx) with the added step
322 * of using the host send_table when no other queue has been assigned.
324 * TODO support XPS - but get_xps_queue not exported
326 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
328 int q_idx = sk_tx_queue_get(skb->sk);
330 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
331 /* If forwarding a packet, we use the recorded queue when
332 * available for better cache locality.
334 if (skb_rx_queue_recorded(skb))
335 q_idx = skb_get_rx_queue(skb);
337 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
343 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
344 struct net_device *sb_dev)
346 struct net_device_context *ndc = netdev_priv(ndev);
347 struct net_device *vf_netdev;
351 vf_netdev = rcu_dereference(ndc->vf_netdev);
353 const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
355 if (vf_ops->ndo_select_queue)
356 txq = vf_ops->ndo_select_queue(vf_netdev, skb, sb_dev);
358 txq = netdev_pick_tx(vf_netdev, skb, NULL);
360 /* Record the queue selected by VF so that it can be
361 * used for common case where VF has more queues than
362 * the synthetic device.
364 qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
366 txq = netvsc_pick_tx(ndev, skb);
370 while (unlikely(txq >= ndev->real_num_tx_queues))
371 txq -= ndev->real_num_tx_queues;
376 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
377 struct hv_page_buffer *pb)
381 /* Deal with compound pages by ignoring unused part
384 page += (offset >> PAGE_SHIFT);
385 offset &= ~PAGE_MASK;
390 bytes = PAGE_SIZE - offset;
393 pb[j].pfn = page_to_pfn(page);
394 pb[j].offset = offset;
400 if (offset == PAGE_SIZE && len) {
410 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
411 struct hv_netvsc_packet *packet,
412 struct hv_page_buffer *pb)
415 char *data = skb->data;
416 int frags = skb_shinfo(skb)->nr_frags;
419 /* The packet is laid out thus:
420 * 1. hdr: RNDIS header and PPI
422 * 3. skb fragment data
424 slots_used += fill_pg_buf(virt_to_page(hdr),
426 len, &pb[slots_used]);
428 packet->rmsg_size = len;
429 packet->rmsg_pgcnt = slots_used;
431 slots_used += fill_pg_buf(virt_to_page(data),
432 offset_in_page(data),
433 skb_headlen(skb), &pb[slots_used]);
435 for (i = 0; i < frags; i++) {
436 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
438 slots_used += fill_pg_buf(skb_frag_page(frag),
440 skb_frag_size(frag), &pb[slots_used]);
445 static int count_skb_frag_slots(struct sk_buff *skb)
447 int i, frags = skb_shinfo(skb)->nr_frags;
450 for (i = 0; i < frags; i++) {
451 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
452 unsigned long size = skb_frag_size(frag);
453 unsigned long offset = skb_frag_off(frag);
455 /* Skip unused frames from start of page */
456 offset &= ~PAGE_MASK;
457 pages += PFN_UP(offset + size);
462 static int netvsc_get_slots(struct sk_buff *skb)
464 char *data = skb->data;
465 unsigned int offset = offset_in_page(data);
466 unsigned int len = skb_headlen(skb);
470 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
471 frag_slots = count_skb_frag_slots(skb);
472 return slots + frag_slots;
475 static u32 net_checksum_info(struct sk_buff *skb)
477 if (skb->protocol == htons(ETH_P_IP)) {
478 struct iphdr *ip = ip_hdr(skb);
480 if (ip->protocol == IPPROTO_TCP)
481 return TRANSPORT_INFO_IPV4_TCP;
482 else if (ip->protocol == IPPROTO_UDP)
483 return TRANSPORT_INFO_IPV4_UDP;
485 struct ipv6hdr *ip6 = ipv6_hdr(skb);
487 if (ip6->nexthdr == IPPROTO_TCP)
488 return TRANSPORT_INFO_IPV6_TCP;
489 else if (ip6->nexthdr == IPPROTO_UDP)
490 return TRANSPORT_INFO_IPV6_UDP;
493 return TRANSPORT_INFO_NOT_IP;
496 /* Send skb on the slave VF device. */
497 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
500 struct net_device_context *ndev_ctx = netdev_priv(net);
501 unsigned int len = skb->len;
504 skb->dev = vf_netdev;
505 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
507 rc = dev_queue_xmit(skb);
508 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
509 struct netvsc_vf_pcpu_stats *pcpu_stats
510 = this_cpu_ptr(ndev_ctx->vf_stats);
512 u64_stats_update_begin(&pcpu_stats->syncp);
513 pcpu_stats->tx_packets++;
514 pcpu_stats->tx_bytes += len;
515 u64_stats_update_end(&pcpu_stats->syncp);
517 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
523 static int netvsc_xmit(struct sk_buff *skb, struct net_device *net, bool xdp_tx)
525 struct net_device_context *net_device_ctx = netdev_priv(net);
526 struct hv_netvsc_packet *packet = NULL;
528 unsigned int num_data_pgs;
529 struct rndis_message *rndis_msg;
530 struct net_device *vf_netdev;
533 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
535 /* If VF is present and up then redirect packets to it.
536 * Skip the VF if it is marked down or has no carrier.
537 * If netpoll is in uses, then VF can not be used either.
539 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
540 if (vf_netdev && netif_running(vf_netdev) &&
541 netif_carrier_ok(vf_netdev) && !netpoll_tx_running(net))
542 return netvsc_vf_xmit(net, vf_netdev, skb);
544 /* We will atmost need two pages to describe the rndis
545 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
546 * of pages in a single packet. If skb is scattered around
547 * more pages we try linearizing it.
550 num_data_pgs = netvsc_get_slots(skb) + 2;
552 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
553 ++net_device_ctx->eth_stats.tx_scattered;
555 if (skb_linearize(skb))
558 num_data_pgs = netvsc_get_slots(skb) + 2;
559 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
560 ++net_device_ctx->eth_stats.tx_too_big;
566 * Place the rndis header in the skb head room and
567 * the skb->cb will be used for hv_netvsc_packet
570 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
574 /* Use the skb control buffer for building up the packet */
575 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
576 sizeof_field(struct sk_buff, cb));
577 packet = (struct hv_netvsc_packet *)skb->cb;
579 packet->q_idx = skb_get_queue_mapping(skb);
581 packet->total_data_buflen = skb->len;
582 packet->total_bytes = skb->len;
583 packet->total_packets = 1;
585 rndis_msg = (struct rndis_message *)skb->head;
587 /* Add the rndis header */
588 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
589 rndis_msg->msg_len = packet->total_data_buflen;
591 rndis_msg->msg.pkt = (struct rndis_packet) {
592 .data_offset = sizeof(struct rndis_packet),
593 .data_len = packet->total_data_buflen,
594 .per_pkt_info_offset = sizeof(struct rndis_packet),
597 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
599 hash = skb_get_hash_raw(skb);
600 if (hash != 0 && net->real_num_tx_queues > 1) {
603 rndis_msg_size += NDIS_HASH_PPI_SIZE;
604 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
609 /* When using AF_PACKET we need to drop VLAN header from
610 * the frame and update the SKB to allow the HOST OS
611 * to transmit the 802.1Q packet
613 if (skb->protocol == htons(ETH_P_8021Q)) {
616 skb_reset_mac_header(skb);
617 if (eth_type_vlan(eth_hdr(skb)->h_proto)) {
618 if (unlikely(__skb_vlan_pop(skb, &vlan_tci) != 0)) {
619 ++net_device_ctx->eth_stats.vlan_error;
623 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
624 /* Update the NDIS header pkt lengths */
625 packet->total_data_buflen -= VLAN_HLEN;
626 packet->total_bytes -= VLAN_HLEN;
627 rndis_msg->msg_len = packet->total_data_buflen;
628 rndis_msg->msg.pkt.data_len = packet->total_data_buflen;
632 if (skb_vlan_tag_present(skb)) {
633 struct ndis_pkt_8021q_info *vlan;
635 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
636 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
640 vlan->vlanid = skb_vlan_tag_get_id(skb);
641 vlan->cfi = skb_vlan_tag_get_cfi(skb);
642 vlan->pri = skb_vlan_tag_get_prio(skb);
645 if (skb_is_gso(skb)) {
646 struct ndis_tcp_lso_info *lso_info;
648 rndis_msg_size += NDIS_LSO_PPI_SIZE;
649 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
650 TCP_LARGESEND_PKTINFO);
653 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
654 if (skb->protocol == htons(ETH_P_IP)) {
655 lso_info->lso_v2_transmit.ip_version =
656 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
657 ip_hdr(skb)->tot_len = 0;
658 ip_hdr(skb)->check = 0;
659 tcp_hdr(skb)->check =
660 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
661 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
663 lso_info->lso_v2_transmit.ip_version =
664 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
665 tcp_v6_gso_csum_prep(skb);
667 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
668 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
669 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
670 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
671 struct ndis_tcp_ip_checksum_info *csum_info;
673 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
674 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
675 TCPIP_CHKSUM_PKTINFO);
677 csum_info->value = 0;
678 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
680 if (skb->protocol == htons(ETH_P_IP)) {
681 csum_info->transmit.is_ipv4 = 1;
683 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
684 csum_info->transmit.tcp_checksum = 1;
686 csum_info->transmit.udp_checksum = 1;
688 csum_info->transmit.is_ipv6 = 1;
690 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
691 csum_info->transmit.tcp_checksum = 1;
693 csum_info->transmit.udp_checksum = 1;
696 /* Can't do offload of this type of checksum */
697 if (skb_checksum_help(skb))
702 /* Start filling in the page buffers with the rndis hdr */
703 rndis_msg->msg_len += rndis_msg_size;
704 packet->total_data_buflen = rndis_msg->msg_len;
705 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
708 /* timestamp packet in software */
709 skb_tx_timestamp(skb);
711 ret = netvsc_send(net, packet, rndis_msg, pb, skb, xdp_tx);
712 if (likely(ret == 0))
715 if (ret == -EAGAIN) {
716 ++net_device_ctx->eth_stats.tx_busy;
717 return NETDEV_TX_BUSY;
721 ++net_device_ctx->eth_stats.tx_no_space;
724 dev_kfree_skb_any(skb);
725 net->stats.tx_dropped++;
730 ++net_device_ctx->eth_stats.tx_no_memory;
734 static netdev_tx_t netvsc_start_xmit(struct sk_buff *skb,
735 struct net_device *ndev)
737 return netvsc_xmit(skb, ndev, false);
741 * netvsc_linkstatus_callback - Link up/down notification
743 void netvsc_linkstatus_callback(struct net_device *net,
744 struct rndis_message *resp)
746 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
747 struct net_device_context *ndev_ctx = netdev_priv(net);
748 struct netvsc_reconfig *event;
751 /* Update the physical link speed when changing to another vSwitch */
752 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
755 speed = *(u32 *)((void *)indicate
756 + indicate->status_buf_offset) / 10000;
757 ndev_ctx->speed = speed;
761 /* Handle these link change statuses below */
762 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
763 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
764 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
767 if (net->reg_state != NETREG_REGISTERED)
770 event = kzalloc(sizeof(*event), GFP_ATOMIC);
773 event->event = indicate->status;
775 spin_lock_irqsave(&ndev_ctx->lock, flags);
776 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
777 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
779 schedule_delayed_work(&ndev_ctx->dwork, 0);
782 static void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev)
786 skb->queue_mapping = skb_get_rx_queue(skb);
787 __skb_push(skb, ETH_HLEN);
789 rc = netvsc_xmit(skb, ndev, true);
791 if (dev_xmit_complete(rc))
794 dev_kfree_skb_any(skb);
795 ndev->stats.tx_dropped++;
798 static void netvsc_comp_ipcsum(struct sk_buff *skb)
800 struct iphdr *iph = (struct iphdr *)skb->data;
803 iph->check = ip_fast_csum(iph, iph->ihl);
806 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
807 struct netvsc_channel *nvchan,
808 struct xdp_buff *xdp)
810 struct napi_struct *napi = &nvchan->napi;
811 const struct ndis_pkt_8021q_info *vlan = nvchan->rsc.vlan;
812 const struct ndis_tcp_ip_checksum_info *csum_info =
813 nvchan->rsc.csum_info;
814 const u32 *hash_info = nvchan->rsc.hash_info;
816 void *xbuf = xdp->data_hard_start;
820 unsigned int hdroom = xdp->data - xdp->data_hard_start;
821 unsigned int xlen = xdp->data_end - xdp->data;
822 unsigned int frag_size = xdp->frame_sz;
824 skb = build_skb(xbuf, frag_size);
827 __free_page(virt_to_page(xbuf));
831 skb_reserve(skb, hdroom);
833 skb->dev = napi->dev;
835 skb = napi_alloc_skb(napi, nvchan->rsc.pktlen);
840 /* Copy to skb. This copy is needed here since the memory
841 * pointed by hv_netvsc_packet cannot be deallocated.
843 for (i = 0; i < nvchan->rsc.cnt; i++)
844 skb_put_data(skb, nvchan->rsc.data[i],
848 skb->protocol = eth_type_trans(skb, net);
850 /* skb is already created with CHECKSUM_NONE */
851 skb_checksum_none_assert(skb);
853 /* Incoming packets may have IP header checksum verified by the host.
854 * They may not have IP header checksum computed after coalescing.
855 * We compute it here if the flags are set, because on Linux, the IP
856 * checksum is always checked.
858 if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
859 csum_info->receive.ip_checksum_succeeded &&
860 skb->protocol == htons(ETH_P_IP))
861 netvsc_comp_ipcsum(skb);
863 /* Do L4 checksum offload if enabled and present. */
864 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
865 if (csum_info->receive.tcp_checksum_succeeded ||
866 csum_info->receive.udp_checksum_succeeded)
867 skb->ip_summed = CHECKSUM_UNNECESSARY;
870 if (hash_info && (net->features & NETIF_F_RXHASH))
871 skb_set_hash(skb, *hash_info, PKT_HASH_TYPE_L4);
874 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT) |
875 (vlan->cfi ? VLAN_CFI_MASK : 0);
877 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
885 * netvsc_recv_callback - Callback when we receive a packet from the
886 * "wire" on the specified device.
888 int netvsc_recv_callback(struct net_device *net,
889 struct netvsc_device *net_device,
890 struct netvsc_channel *nvchan)
892 struct net_device_context *net_device_ctx = netdev_priv(net);
893 struct vmbus_channel *channel = nvchan->channel;
894 u16 q_idx = channel->offermsg.offer.sub_channel_index;
896 struct netvsc_stats *rx_stats = &nvchan->rx_stats;
900 if (net->reg_state != NETREG_REGISTERED)
901 return NVSP_STAT_FAIL;
903 act = netvsc_run_xdp(net, nvchan, &xdp);
905 if (act != XDP_PASS && act != XDP_TX) {
906 u64_stats_update_begin(&rx_stats->syncp);
907 rx_stats->xdp_drop++;
908 u64_stats_update_end(&rx_stats->syncp);
910 return NVSP_STAT_SUCCESS; /* consumed by XDP */
913 /* Allocate a skb - TODO direct I/O to pages? */
914 skb = netvsc_alloc_recv_skb(net, nvchan, &xdp);
916 if (unlikely(!skb)) {
917 ++net_device_ctx->eth_stats.rx_no_memory;
918 return NVSP_STAT_FAIL;
921 skb_record_rx_queue(skb, q_idx);
924 * Even if injecting the packet, record the statistics
925 * on the synthetic device because modifying the VF device
926 * statistics will not work correctly.
928 u64_stats_update_begin(&rx_stats->syncp);
930 rx_stats->bytes += nvchan->rsc.pktlen;
932 if (skb->pkt_type == PACKET_BROADCAST)
933 ++rx_stats->broadcast;
934 else if (skb->pkt_type == PACKET_MULTICAST)
935 ++rx_stats->multicast;
936 u64_stats_update_end(&rx_stats->syncp);
939 netvsc_xdp_xmit(skb, net);
940 return NVSP_STAT_SUCCESS;
943 napi_gro_receive(&nvchan->napi, skb);
944 return NVSP_STAT_SUCCESS;
947 static void netvsc_get_drvinfo(struct net_device *net,
948 struct ethtool_drvinfo *info)
950 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
951 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
954 static void netvsc_get_channels(struct net_device *net,
955 struct ethtool_channels *channel)
957 struct net_device_context *net_device_ctx = netdev_priv(net);
958 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
961 channel->max_combined = nvdev->max_chn;
962 channel->combined_count = nvdev->num_chn;
966 /* Alloc struct netvsc_device_info, and initialize it from either existing
967 * struct netvsc_device, or from default values.
970 struct netvsc_device_info *netvsc_devinfo_get(struct netvsc_device *nvdev)
972 struct netvsc_device_info *dev_info;
973 struct bpf_prog *prog;
975 dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
983 dev_info->num_chn = nvdev->num_chn;
984 dev_info->send_sections = nvdev->send_section_cnt;
985 dev_info->send_section_size = nvdev->send_section_size;
986 dev_info->recv_sections = nvdev->recv_section_cnt;
987 dev_info->recv_section_size = nvdev->recv_section_size;
989 memcpy(dev_info->rss_key, nvdev->extension->rss_key,
992 prog = netvsc_xdp_get(nvdev);
995 dev_info->bprog = prog;
998 dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
999 dev_info->send_sections = NETVSC_DEFAULT_TX;
1000 dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
1001 dev_info->recv_sections = NETVSC_DEFAULT_RX;
1002 dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
1008 /* Free struct netvsc_device_info */
1009 static void netvsc_devinfo_put(struct netvsc_device_info *dev_info)
1011 if (dev_info->bprog) {
1013 bpf_prog_put(dev_info->bprog);
1019 static int netvsc_detach(struct net_device *ndev,
1020 struct netvsc_device *nvdev)
1022 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1023 struct hv_device *hdev = ndev_ctx->device_ctx;
1026 /* Don't try continuing to try and setup sub channels */
1027 if (cancel_work_sync(&nvdev->subchan_work))
1030 netvsc_xdp_set(ndev, NULL, NULL, nvdev);
1032 /* If device was up (receiving) then shutdown */
1033 if (netif_running(ndev)) {
1034 netvsc_tx_disable(nvdev, ndev);
1036 ret = rndis_filter_close(nvdev);
1039 "unable to close device (ret %d).\n", ret);
1043 ret = netvsc_wait_until_empty(nvdev);
1046 "Ring buffer not empty after closing rndis\n");
1051 netif_device_detach(ndev);
1053 rndis_filter_device_remove(hdev, nvdev);
1058 static int netvsc_attach(struct net_device *ndev,
1059 struct netvsc_device_info *dev_info)
1061 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1062 struct hv_device *hdev = ndev_ctx->device_ctx;
1063 struct netvsc_device *nvdev;
1064 struct rndis_device *rdev;
1065 struct bpf_prog *prog;
1068 nvdev = rndis_filter_device_add(hdev, dev_info);
1070 return PTR_ERR(nvdev);
1072 if (nvdev->num_chn > 1) {
1073 ret = rndis_set_subchannel(ndev, nvdev, dev_info);
1075 /* if unavailable, just proceed with one queue */
1082 prog = dev_info->bprog;
1085 ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
1092 /* In any case device is now ready */
1093 nvdev->tx_disable = false;
1094 netif_device_attach(ndev);
1096 /* Note: enable and attach happen when sub-channels setup */
1097 netif_carrier_off(ndev);
1099 if (netif_running(ndev)) {
1100 ret = rndis_filter_open(nvdev);
1104 rdev = nvdev->extension;
1105 if (!rdev->link_state)
1106 netif_carrier_on(ndev);
1112 netif_device_detach(ndev);
1115 rndis_filter_device_remove(hdev, nvdev);
1120 static int netvsc_set_channels(struct net_device *net,
1121 struct ethtool_channels *channels)
1123 struct net_device_context *net_device_ctx = netdev_priv(net);
1124 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
1125 unsigned int orig, count = channels->combined_count;
1126 struct netvsc_device_info *device_info;
1129 /* We do not support separate count for rx, tx, or other */
1131 channels->rx_count || channels->tx_count || channels->other_count)
1134 if (!nvdev || nvdev->destroy)
1137 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1140 if (count > nvdev->max_chn)
1143 orig = nvdev->num_chn;
1145 device_info = netvsc_devinfo_get(nvdev);
1150 device_info->num_chn = count;
1152 ret = netvsc_detach(net, nvdev);
1156 ret = netvsc_attach(net, device_info);
1158 device_info->num_chn = orig;
1159 if (netvsc_attach(net, device_info))
1160 netdev_err(net, "restoring channel setting failed\n");
1164 netvsc_devinfo_put(device_info);
1168 static void netvsc_init_settings(struct net_device *dev)
1170 struct net_device_context *ndc = netdev_priv(dev);
1172 ndc->l4_hash = HV_DEFAULT_L4HASH;
1174 ndc->speed = SPEED_UNKNOWN;
1175 ndc->duplex = DUPLEX_FULL;
1177 dev->features = NETIF_F_LRO;
1180 static int netvsc_get_link_ksettings(struct net_device *dev,
1181 struct ethtool_link_ksettings *cmd)
1183 struct net_device_context *ndc = netdev_priv(dev);
1184 struct net_device *vf_netdev;
1186 vf_netdev = rtnl_dereference(ndc->vf_netdev);
1189 return __ethtool_get_link_ksettings(vf_netdev, cmd);
1191 cmd->base.speed = ndc->speed;
1192 cmd->base.duplex = ndc->duplex;
1193 cmd->base.port = PORT_OTHER;
1198 static int netvsc_set_link_ksettings(struct net_device *dev,
1199 const struct ethtool_link_ksettings *cmd)
1201 struct net_device_context *ndc = netdev_priv(dev);
1202 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1205 if (!vf_netdev->ethtool_ops->set_link_ksettings)
1208 return vf_netdev->ethtool_ops->set_link_ksettings(vf_netdev,
1212 return ethtool_virtdev_set_link_ksettings(dev, cmd,
1213 &ndc->speed, &ndc->duplex);
1216 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
1218 struct net_device_context *ndevctx = netdev_priv(ndev);
1219 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1220 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1221 int orig_mtu = ndev->mtu;
1222 struct netvsc_device_info *device_info;
1225 if (!nvdev || nvdev->destroy)
1228 device_info = netvsc_devinfo_get(nvdev);
1233 /* Change MTU of underlying VF netdev first. */
1235 ret = dev_set_mtu(vf_netdev, mtu);
1240 ret = netvsc_detach(ndev, nvdev);
1246 ret = netvsc_attach(ndev, device_info);
1250 /* Attempt rollback to original MTU */
1251 ndev->mtu = orig_mtu;
1253 if (netvsc_attach(ndev, device_info))
1254 netdev_err(ndev, "restoring mtu failed\n");
1257 dev_set_mtu(vf_netdev, orig_mtu);
1260 netvsc_devinfo_put(device_info);
1264 static void netvsc_get_vf_stats(struct net_device *net,
1265 struct netvsc_vf_pcpu_stats *tot)
1267 struct net_device_context *ndev_ctx = netdev_priv(net);
1270 memset(tot, 0, sizeof(*tot));
1272 for_each_possible_cpu(i) {
1273 const struct netvsc_vf_pcpu_stats *stats
1274 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1275 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1279 start = u64_stats_fetch_begin_irq(&stats->syncp);
1280 rx_packets = stats->rx_packets;
1281 tx_packets = stats->tx_packets;
1282 rx_bytes = stats->rx_bytes;
1283 tx_bytes = stats->tx_bytes;
1284 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1286 tot->rx_packets += rx_packets;
1287 tot->tx_packets += tx_packets;
1288 tot->rx_bytes += rx_bytes;
1289 tot->tx_bytes += tx_bytes;
1290 tot->tx_dropped += stats->tx_dropped;
1294 static void netvsc_get_pcpu_stats(struct net_device *net,
1295 struct netvsc_ethtool_pcpu_stats *pcpu_tot)
1297 struct net_device_context *ndev_ctx = netdev_priv(net);
1298 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1301 /* fetch percpu stats of vf */
1302 for_each_possible_cpu(i) {
1303 const struct netvsc_vf_pcpu_stats *stats =
1304 per_cpu_ptr(ndev_ctx->vf_stats, i);
1305 struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
1309 start = u64_stats_fetch_begin_irq(&stats->syncp);
1310 this_tot->vf_rx_packets = stats->rx_packets;
1311 this_tot->vf_tx_packets = stats->tx_packets;
1312 this_tot->vf_rx_bytes = stats->rx_bytes;
1313 this_tot->vf_tx_bytes = stats->tx_bytes;
1314 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1315 this_tot->rx_packets = this_tot->vf_rx_packets;
1316 this_tot->tx_packets = this_tot->vf_tx_packets;
1317 this_tot->rx_bytes = this_tot->vf_rx_bytes;
1318 this_tot->tx_bytes = this_tot->vf_tx_bytes;
1321 /* fetch percpu stats of netvsc */
1322 for (i = 0; i < nvdev->num_chn; i++) {
1323 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1324 const struct netvsc_stats *stats;
1325 struct netvsc_ethtool_pcpu_stats *this_tot =
1326 &pcpu_tot[nvchan->channel->target_cpu];
1330 stats = &nvchan->tx_stats;
1332 start = u64_stats_fetch_begin_irq(&stats->syncp);
1333 packets = stats->packets;
1334 bytes = stats->bytes;
1335 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1337 this_tot->tx_bytes += bytes;
1338 this_tot->tx_packets += packets;
1340 stats = &nvchan->rx_stats;
1342 start = u64_stats_fetch_begin_irq(&stats->syncp);
1343 packets = stats->packets;
1344 bytes = stats->bytes;
1345 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1347 this_tot->rx_bytes += bytes;
1348 this_tot->rx_packets += packets;
1352 static void netvsc_get_stats64(struct net_device *net,
1353 struct rtnl_link_stats64 *t)
1355 struct net_device_context *ndev_ctx = netdev_priv(net);
1356 struct netvsc_device *nvdev;
1357 struct netvsc_vf_pcpu_stats vf_tot;
1362 nvdev = rcu_dereference(ndev_ctx->nvdev);
1366 netdev_stats_to_stats64(t, &net->stats);
1368 netvsc_get_vf_stats(net, &vf_tot);
1369 t->rx_packets += vf_tot.rx_packets;
1370 t->tx_packets += vf_tot.tx_packets;
1371 t->rx_bytes += vf_tot.rx_bytes;
1372 t->tx_bytes += vf_tot.tx_bytes;
1373 t->tx_dropped += vf_tot.tx_dropped;
1375 for (i = 0; i < nvdev->num_chn; i++) {
1376 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1377 const struct netvsc_stats *stats;
1378 u64 packets, bytes, multicast;
1381 stats = &nvchan->tx_stats;
1383 start = u64_stats_fetch_begin_irq(&stats->syncp);
1384 packets = stats->packets;
1385 bytes = stats->bytes;
1386 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1388 t->tx_bytes += bytes;
1389 t->tx_packets += packets;
1391 stats = &nvchan->rx_stats;
1393 start = u64_stats_fetch_begin_irq(&stats->syncp);
1394 packets = stats->packets;
1395 bytes = stats->bytes;
1396 multicast = stats->multicast + stats->broadcast;
1397 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1399 t->rx_bytes += bytes;
1400 t->rx_packets += packets;
1401 t->multicast += multicast;
1407 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1409 struct net_device_context *ndc = netdev_priv(ndev);
1410 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1411 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1412 struct sockaddr *addr = p;
1415 err = eth_prepare_mac_addr_change(ndev, p);
1423 err = dev_set_mac_address(vf_netdev, addr, NULL);
1428 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1430 eth_commit_mac_addr_change(ndev, p);
1431 } else if (vf_netdev) {
1432 /* rollback change on VF */
1433 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1434 dev_set_mac_address(vf_netdev, addr, NULL);
1440 static const struct {
1441 char name[ETH_GSTRING_LEN];
1443 } netvsc_stats[] = {
1444 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1445 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1446 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1447 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1448 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1449 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1450 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1451 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1452 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1453 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1454 { "vlan_error", offsetof(struct netvsc_ethtool_stats, vlan_error) },
1456 { "cpu%u_rx_packets",
1457 offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
1459 offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
1460 { "cpu%u_tx_packets",
1461 offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
1463 offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
1464 { "cpu%u_vf_rx_packets",
1465 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
1466 { "cpu%u_vf_rx_bytes",
1467 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
1468 { "cpu%u_vf_tx_packets",
1469 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
1470 { "cpu%u_vf_tx_bytes",
1471 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
1473 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1474 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1475 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1476 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1477 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1480 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1481 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1483 /* statistics per queue (rx/tx packets/bytes) */
1484 #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
1486 /* 5 statistics per queue (rx/tx packets/bytes, rx xdp_drop) */
1487 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 5)
1489 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1491 struct net_device_context *ndc = netdev_priv(dev);
1492 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1497 switch (string_set) {
1499 return NETVSC_GLOBAL_STATS_LEN
1500 + NETVSC_VF_STATS_LEN
1501 + NETVSC_QUEUE_STATS_LEN(nvdev)
1502 + NETVSC_PCPU_STATS_LEN;
1508 static void netvsc_get_ethtool_stats(struct net_device *dev,
1509 struct ethtool_stats *stats, u64 *data)
1511 struct net_device_context *ndc = netdev_priv(dev);
1512 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1513 const void *nds = &ndc->eth_stats;
1514 const struct netvsc_stats *qstats;
1515 struct netvsc_vf_pcpu_stats sum;
1516 struct netvsc_ethtool_pcpu_stats *pcpu_sum;
1525 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1526 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1528 netvsc_get_vf_stats(dev, &sum);
1529 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1530 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1532 for (j = 0; j < nvdev->num_chn; j++) {
1533 qstats = &nvdev->chan_table[j].tx_stats;
1536 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1537 packets = qstats->packets;
1538 bytes = qstats->bytes;
1539 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1540 data[i++] = packets;
1543 qstats = &nvdev->chan_table[j].rx_stats;
1545 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1546 packets = qstats->packets;
1547 bytes = qstats->bytes;
1548 xdp_drop = qstats->xdp_drop;
1549 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1550 data[i++] = packets;
1552 data[i++] = xdp_drop;
1555 pcpu_sum = kvmalloc_array(num_possible_cpus(),
1556 sizeof(struct netvsc_ethtool_pcpu_stats),
1558 netvsc_get_pcpu_stats(dev, pcpu_sum);
1559 for_each_present_cpu(cpu) {
1560 struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
1562 for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
1563 data[i++] = *(u64 *)((void *)this_sum
1564 + pcpu_stats[j].offset);
1569 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1571 struct net_device_context *ndc = netdev_priv(dev);
1572 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1579 switch (stringset) {
1581 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1582 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1583 p += ETH_GSTRING_LEN;
1586 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1587 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1588 p += ETH_GSTRING_LEN;
1591 for (i = 0; i < nvdev->num_chn; i++) {
1592 sprintf(p, "tx_queue_%u_packets", i);
1593 p += ETH_GSTRING_LEN;
1594 sprintf(p, "tx_queue_%u_bytes", i);
1595 p += ETH_GSTRING_LEN;
1596 sprintf(p, "rx_queue_%u_packets", i);
1597 p += ETH_GSTRING_LEN;
1598 sprintf(p, "rx_queue_%u_bytes", i);
1599 p += ETH_GSTRING_LEN;
1600 sprintf(p, "rx_queue_%u_xdp_drop", i);
1601 p += ETH_GSTRING_LEN;
1604 for_each_present_cpu(cpu) {
1605 for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++) {
1606 sprintf(p, pcpu_stats[i].name, cpu);
1607 p += ETH_GSTRING_LEN;
1616 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1617 struct ethtool_rxnfc *info)
1619 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1621 info->data = RXH_IP_SRC | RXH_IP_DST;
1623 switch (info->flow_type) {
1625 if (ndc->l4_hash & HV_TCP4_L4HASH)
1626 info->data |= l4_flag;
1631 if (ndc->l4_hash & HV_TCP6_L4HASH)
1632 info->data |= l4_flag;
1637 if (ndc->l4_hash & HV_UDP4_L4HASH)
1638 info->data |= l4_flag;
1643 if (ndc->l4_hash & HV_UDP6_L4HASH)
1644 info->data |= l4_flag;
1660 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1663 struct net_device_context *ndc = netdev_priv(dev);
1664 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1669 switch (info->cmd) {
1670 case ETHTOOL_GRXRINGS:
1671 info->data = nvdev->num_chn;
1675 return netvsc_get_rss_hash_opts(ndc, info);
1680 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1681 struct ethtool_rxnfc *info)
1683 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1684 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1685 switch (info->flow_type) {
1687 ndc->l4_hash |= HV_TCP4_L4HASH;
1691 ndc->l4_hash |= HV_TCP6_L4HASH;
1695 ndc->l4_hash |= HV_UDP4_L4HASH;
1699 ndc->l4_hash |= HV_UDP6_L4HASH;
1709 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1710 switch (info->flow_type) {
1712 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1716 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1720 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1724 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1738 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1740 struct net_device_context *ndc = netdev_priv(ndev);
1742 if (info->cmd == ETHTOOL_SRXFH)
1743 return netvsc_set_rss_hash_opts(ndc, info);
1748 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1750 return NETVSC_HASH_KEYLEN;
1753 static u32 netvsc_rss_indir_size(struct net_device *dev)
1758 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1761 struct net_device_context *ndc = netdev_priv(dev);
1762 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1763 struct rndis_device *rndis_dev;
1770 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1772 rndis_dev = ndev->extension;
1774 for (i = 0; i < ITAB_NUM; i++)
1775 indir[i] = ndc->rx_table[i];
1779 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1784 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1785 const u8 *key, const u8 hfunc)
1787 struct net_device_context *ndc = netdev_priv(dev);
1788 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1789 struct rndis_device *rndis_dev;
1795 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1798 rndis_dev = ndev->extension;
1800 for (i = 0; i < ITAB_NUM; i++)
1801 if (indir[i] >= ndev->num_chn)
1804 for (i = 0; i < ITAB_NUM; i++)
1805 ndc->rx_table[i] = indir[i];
1812 key = rndis_dev->rss_key;
1815 return rndis_filter_set_rss_param(rndis_dev, key);
1818 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1819 * It does have pre-allocated receive area which is divided into sections.
1821 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1822 struct ethtool_ringparam *ring)
1826 ring->rx_pending = nvdev->recv_section_cnt;
1827 ring->tx_pending = nvdev->send_section_cnt;
1829 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1830 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1832 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1834 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1835 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1836 / nvdev->send_section_size;
1839 static void netvsc_get_ringparam(struct net_device *ndev,
1840 struct ethtool_ringparam *ring)
1842 struct net_device_context *ndevctx = netdev_priv(ndev);
1843 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1848 __netvsc_get_ringparam(nvdev, ring);
1851 static int netvsc_set_ringparam(struct net_device *ndev,
1852 struct ethtool_ringparam *ring)
1854 struct net_device_context *ndevctx = netdev_priv(ndev);
1855 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1856 struct netvsc_device_info *device_info;
1857 struct ethtool_ringparam orig;
1861 if (!nvdev || nvdev->destroy)
1864 memset(&orig, 0, sizeof(orig));
1865 __netvsc_get_ringparam(nvdev, &orig);
1867 new_tx = clamp_t(u32, ring->tx_pending,
1868 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1869 new_rx = clamp_t(u32, ring->rx_pending,
1870 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1872 if (new_tx == orig.tx_pending &&
1873 new_rx == orig.rx_pending)
1874 return 0; /* no change */
1876 device_info = netvsc_devinfo_get(nvdev);
1881 device_info->send_sections = new_tx;
1882 device_info->recv_sections = new_rx;
1884 ret = netvsc_detach(ndev, nvdev);
1888 ret = netvsc_attach(ndev, device_info);
1890 device_info->send_sections = orig.tx_pending;
1891 device_info->recv_sections = orig.rx_pending;
1893 if (netvsc_attach(ndev, device_info))
1894 netdev_err(ndev, "restoring ringparam failed");
1898 netvsc_devinfo_put(device_info);
1902 static netdev_features_t netvsc_fix_features(struct net_device *ndev,
1903 netdev_features_t features)
1905 struct net_device_context *ndevctx = netdev_priv(ndev);
1906 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1908 if (!nvdev || nvdev->destroy)
1911 if ((features & NETIF_F_LRO) && netvsc_xdp_get(nvdev)) {
1912 features ^= NETIF_F_LRO;
1913 netdev_info(ndev, "Skip LRO - unsupported with XDP\n");
1919 static int netvsc_set_features(struct net_device *ndev,
1920 netdev_features_t features)
1922 netdev_features_t change = features ^ ndev->features;
1923 struct net_device_context *ndevctx = netdev_priv(ndev);
1924 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1925 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1926 struct ndis_offload_params offloads;
1929 if (!nvdev || nvdev->destroy)
1932 if (!(change & NETIF_F_LRO))
1935 memset(&offloads, 0, sizeof(struct ndis_offload_params));
1937 if (features & NETIF_F_LRO) {
1938 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1939 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1941 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1942 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1945 ret = rndis_filter_set_offload_params(ndev, nvdev, &offloads);
1948 features ^= NETIF_F_LRO;
1949 ndev->features = features;
1956 vf_netdev->wanted_features = features;
1957 netdev_update_features(vf_netdev);
1962 static int netvsc_get_regs_len(struct net_device *netdev)
1964 return VRSS_SEND_TAB_SIZE * sizeof(u32);
1967 static void netvsc_get_regs(struct net_device *netdev,
1968 struct ethtool_regs *regs, void *p)
1970 struct net_device_context *ndc = netdev_priv(netdev);
1973 /* increase the version, if buffer format is changed. */
1976 memcpy(regs_buff, ndc->tx_table, VRSS_SEND_TAB_SIZE * sizeof(u32));
1979 static u32 netvsc_get_msglevel(struct net_device *ndev)
1981 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1983 return ndev_ctx->msg_enable;
1986 static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
1988 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1990 ndev_ctx->msg_enable = val;
1993 static const struct ethtool_ops ethtool_ops = {
1994 .get_drvinfo = netvsc_get_drvinfo,
1995 .get_regs_len = netvsc_get_regs_len,
1996 .get_regs = netvsc_get_regs,
1997 .get_msglevel = netvsc_get_msglevel,
1998 .set_msglevel = netvsc_set_msglevel,
1999 .get_link = ethtool_op_get_link,
2000 .get_ethtool_stats = netvsc_get_ethtool_stats,
2001 .get_sset_count = netvsc_get_sset_count,
2002 .get_strings = netvsc_get_strings,
2003 .get_channels = netvsc_get_channels,
2004 .set_channels = netvsc_set_channels,
2005 .get_ts_info = ethtool_op_get_ts_info,
2006 .get_rxnfc = netvsc_get_rxnfc,
2007 .set_rxnfc = netvsc_set_rxnfc,
2008 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
2009 .get_rxfh_indir_size = netvsc_rss_indir_size,
2010 .get_rxfh = netvsc_get_rxfh,
2011 .set_rxfh = netvsc_set_rxfh,
2012 .get_link_ksettings = netvsc_get_link_ksettings,
2013 .set_link_ksettings = netvsc_set_link_ksettings,
2014 .get_ringparam = netvsc_get_ringparam,
2015 .set_ringparam = netvsc_set_ringparam,
2018 static const struct net_device_ops device_ops = {
2019 .ndo_open = netvsc_open,
2020 .ndo_stop = netvsc_close,
2021 .ndo_start_xmit = netvsc_start_xmit,
2022 .ndo_change_rx_flags = netvsc_change_rx_flags,
2023 .ndo_set_rx_mode = netvsc_set_rx_mode,
2024 .ndo_fix_features = netvsc_fix_features,
2025 .ndo_set_features = netvsc_set_features,
2026 .ndo_change_mtu = netvsc_change_mtu,
2027 .ndo_validate_addr = eth_validate_addr,
2028 .ndo_set_mac_address = netvsc_set_mac_addr,
2029 .ndo_select_queue = netvsc_select_queue,
2030 .ndo_get_stats64 = netvsc_get_stats64,
2031 .ndo_bpf = netvsc_bpf,
2035 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
2036 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
2037 * present send GARP packet to network peers with netif_notify_peers().
2039 static void netvsc_link_change(struct work_struct *w)
2041 struct net_device_context *ndev_ctx =
2042 container_of(w, struct net_device_context, dwork.work);
2043 struct hv_device *device_obj = ndev_ctx->device_ctx;
2044 struct net_device *net = hv_get_drvdata(device_obj);
2045 struct netvsc_device *net_device;
2046 struct rndis_device *rdev;
2047 struct netvsc_reconfig *event = NULL;
2048 bool notify = false, reschedule = false;
2049 unsigned long flags, next_reconfig, delay;
2051 /* if changes are happening, comeback later */
2052 if (!rtnl_trylock()) {
2053 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2057 net_device = rtnl_dereference(ndev_ctx->nvdev);
2061 rdev = net_device->extension;
2063 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
2064 if (time_is_after_jiffies(next_reconfig)) {
2065 /* link_watch only sends one notification with current state
2066 * per second, avoid doing reconfig more frequently. Handle
2069 delay = next_reconfig - jiffies;
2070 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
2071 schedule_delayed_work(&ndev_ctx->dwork, delay);
2074 ndev_ctx->last_reconfig = jiffies;
2076 spin_lock_irqsave(&ndev_ctx->lock, flags);
2077 if (!list_empty(&ndev_ctx->reconfig_events)) {
2078 event = list_first_entry(&ndev_ctx->reconfig_events,
2079 struct netvsc_reconfig, list);
2080 list_del(&event->list);
2081 reschedule = !list_empty(&ndev_ctx->reconfig_events);
2083 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2088 switch (event->event) {
2089 /* Only the following events are possible due to the check in
2090 * netvsc_linkstatus_callback()
2092 case RNDIS_STATUS_MEDIA_CONNECT:
2093 if (rdev->link_state) {
2094 rdev->link_state = false;
2095 netif_carrier_on(net);
2096 netvsc_tx_enable(net_device, net);
2102 case RNDIS_STATUS_MEDIA_DISCONNECT:
2103 if (!rdev->link_state) {
2104 rdev->link_state = true;
2105 netif_carrier_off(net);
2106 netvsc_tx_disable(net_device, net);
2110 case RNDIS_STATUS_NETWORK_CHANGE:
2111 /* Only makes sense if carrier is present */
2112 if (!rdev->link_state) {
2113 rdev->link_state = true;
2114 netif_carrier_off(net);
2115 netvsc_tx_disable(net_device, net);
2116 event->event = RNDIS_STATUS_MEDIA_CONNECT;
2117 spin_lock_irqsave(&ndev_ctx->lock, flags);
2118 list_add(&event->list, &ndev_ctx->reconfig_events);
2119 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2128 netdev_notify_peers(net);
2130 /* link_watch only sends one notification with current state per
2131 * second, handle next reconfig event in 2 seconds.
2134 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2142 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
2144 struct net_device_context *net_device_ctx;
2145 struct net_device *dev;
2147 dev = netdev_master_upper_dev_get(vf_netdev);
2148 if (!dev || dev->netdev_ops != &device_ops)
2149 return NULL; /* not a netvsc device */
2151 net_device_ctx = netdev_priv(dev);
2152 if (!rtnl_dereference(net_device_ctx->nvdev))
2153 return NULL; /* device is removed */
2158 /* Called when VF is injecting data into network stack.
2159 * Change the associated network device from VF to netvsc.
2160 * note: already called with rcu_read_lock
2162 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
2164 struct sk_buff *skb = *pskb;
2165 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
2166 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2167 struct netvsc_vf_pcpu_stats *pcpu_stats
2168 = this_cpu_ptr(ndev_ctx->vf_stats);
2170 skb = skb_share_check(skb, GFP_ATOMIC);
2172 return RX_HANDLER_CONSUMED;
2178 u64_stats_update_begin(&pcpu_stats->syncp);
2179 pcpu_stats->rx_packets++;
2180 pcpu_stats->rx_bytes += skb->len;
2181 u64_stats_update_end(&pcpu_stats->syncp);
2183 return RX_HANDLER_ANOTHER;
2186 static int netvsc_vf_join(struct net_device *vf_netdev,
2187 struct net_device *ndev)
2189 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2192 ret = netdev_rx_handler_register(vf_netdev,
2193 netvsc_vf_handle_frame, ndev);
2195 netdev_err(vf_netdev,
2196 "can not register netvsc VF receive handler (err = %d)\n",
2198 goto rx_handler_failed;
2201 ret = netdev_master_upper_dev_link(vf_netdev, ndev,
2204 netdev_err(vf_netdev,
2205 "can not set master device %s (err = %d)\n",
2207 goto upper_link_failed;
2210 /* set slave flag before open to prevent IPv6 addrconf */
2211 vf_netdev->flags |= IFF_SLAVE;
2213 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
2215 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
2217 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
2221 netdev_rx_handler_unregister(vf_netdev);
2226 static void __netvsc_vf_setup(struct net_device *ndev,
2227 struct net_device *vf_netdev)
2231 /* Align MTU of VF with master */
2232 ret = dev_set_mtu(vf_netdev, ndev->mtu);
2234 netdev_warn(vf_netdev,
2235 "unable to change mtu to %u\n", ndev->mtu);
2237 /* set multicast etc flags on VF */
2238 dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE, NULL);
2240 /* sync address list from ndev to VF */
2241 netif_addr_lock_bh(ndev);
2242 dev_uc_sync(vf_netdev, ndev);
2243 dev_mc_sync(vf_netdev, ndev);
2244 netif_addr_unlock_bh(ndev);
2246 if (netif_running(ndev)) {
2247 ret = dev_open(vf_netdev, NULL);
2249 netdev_warn(vf_netdev,
2250 "unable to open: %d\n", ret);
2254 /* Setup VF as slave of the synthetic device.
2255 * Runs in workqueue to avoid recursion in netlink callbacks.
2257 static void netvsc_vf_setup(struct work_struct *w)
2259 struct net_device_context *ndev_ctx
2260 = container_of(w, struct net_device_context, vf_takeover.work);
2261 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2262 struct net_device *vf_netdev;
2264 if (!rtnl_trylock()) {
2265 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
2269 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2271 __netvsc_vf_setup(ndev, vf_netdev);
2276 /* Find netvsc by VF serial number.
2277 * The PCI hyperv controller records the serial number as the slot kobj name.
2279 static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
2281 struct device *parent = vf_netdev->dev.parent;
2282 struct net_device_context *ndev_ctx;
2283 struct pci_dev *pdev;
2286 if (!parent || !dev_is_pci(parent))
2287 return NULL; /* not a PCI device */
2289 pdev = to_pci_dev(parent);
2291 netdev_notice(vf_netdev, "no PCI slot information\n");
2295 if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
2296 netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
2297 pci_slot_name(pdev->slot));
2301 list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
2302 if (!ndev_ctx->vf_alloc)
2305 if (ndev_ctx->vf_serial == serial)
2306 return hv_get_drvdata(ndev_ctx->device_ctx);
2309 netdev_notice(vf_netdev,
2310 "no netdev found for vf serial:%u\n", serial);
2314 static int netvsc_register_vf(struct net_device *vf_netdev)
2316 struct net_device_context *net_device_ctx;
2317 struct netvsc_device *netvsc_dev;
2318 struct bpf_prog *prog;
2319 struct net_device *ndev;
2322 if (vf_netdev->addr_len != ETH_ALEN)
2325 ndev = get_netvsc_byslot(vf_netdev);
2329 net_device_ctx = netdev_priv(ndev);
2330 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2331 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
2334 /* if synthetic interface is a different namespace,
2335 * then move the VF to that namespace; join will be
2336 * done again in that context.
2338 if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
2339 ret = dev_change_net_namespace(vf_netdev,
2340 dev_net(ndev), "eth%d");
2342 netdev_err(vf_netdev,
2343 "could not move to same namespace as %s: %d\n",
2346 netdev_info(vf_netdev,
2347 "VF moved to namespace with: %s\n",
2352 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
2354 if (netvsc_vf_join(vf_netdev, ndev) != 0)
2357 dev_hold(vf_netdev);
2358 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
2360 vf_netdev->wanted_features = ndev->features;
2361 netdev_update_features(vf_netdev);
2363 prog = netvsc_xdp_get(netvsc_dev);
2364 netvsc_vf_setxdp(vf_netdev, prog);
2369 /* VF up/down change detected, schedule to change data path */
2370 static int netvsc_vf_changed(struct net_device *vf_netdev)
2372 struct net_device_context *net_device_ctx;
2373 struct netvsc_device *netvsc_dev;
2374 struct net_device *ndev;
2375 bool vf_is_up = netif_running(vf_netdev);
2377 ndev = get_netvsc_byref(vf_netdev);
2381 net_device_ctx = netdev_priv(ndev);
2382 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2386 netvsc_switch_datapath(ndev, vf_is_up);
2387 netdev_info(ndev, "Data path switched %s VF: %s\n",
2388 vf_is_up ? "to" : "from", vf_netdev->name);
2393 static int netvsc_unregister_vf(struct net_device *vf_netdev)
2395 struct net_device *ndev;
2396 struct net_device_context *net_device_ctx;
2398 ndev = get_netvsc_byref(vf_netdev);
2402 net_device_ctx = netdev_priv(ndev);
2403 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
2405 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
2407 netvsc_vf_setxdp(vf_netdev, NULL);
2409 netdev_rx_handler_unregister(vf_netdev);
2410 netdev_upper_dev_unlink(vf_netdev, ndev);
2411 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
2417 static int netvsc_probe(struct hv_device *dev,
2418 const struct hv_vmbus_device_id *dev_id)
2420 struct net_device *net = NULL;
2421 struct net_device_context *net_device_ctx;
2422 struct netvsc_device_info *device_info = NULL;
2423 struct netvsc_device *nvdev;
2426 net = alloc_etherdev_mq(sizeof(struct net_device_context),
2431 netif_carrier_off(net);
2433 netvsc_init_settings(net);
2435 net_device_ctx = netdev_priv(net);
2436 net_device_ctx->device_ctx = dev;
2437 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
2438 if (netif_msg_probe(net_device_ctx))
2439 netdev_dbg(net, "netvsc msg_enable: %d\n",
2440 net_device_ctx->msg_enable);
2442 hv_set_drvdata(dev, net);
2444 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
2446 spin_lock_init(&net_device_ctx->lock);
2447 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
2448 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
2450 net_device_ctx->vf_stats
2451 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
2452 if (!net_device_ctx->vf_stats)
2455 net->netdev_ops = &device_ops;
2456 net->ethtool_ops = ðtool_ops;
2457 SET_NETDEV_DEV(net, &dev->device);
2459 /* We always need headroom for rndis header */
2460 net->needed_headroom = RNDIS_AND_PPI_SIZE;
2462 /* Initialize the number of queues to be 1, we may change it if more
2463 * channels are offered later.
2465 netif_set_real_num_tx_queues(net, 1);
2466 netif_set_real_num_rx_queues(net, 1);
2468 /* Notify the netvsc driver of the new device */
2469 device_info = netvsc_devinfo_get(NULL);
2473 goto devinfo_failed;
2476 nvdev = rndis_filter_device_add(dev, device_info);
2477 if (IS_ERR(nvdev)) {
2478 ret = PTR_ERR(nvdev);
2479 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2483 memcpy(net->dev_addr, device_info->mac_adr, ETH_ALEN);
2485 /* We must get rtnl lock before scheduling nvdev->subchan_work,
2486 * otherwise netvsc_subchan_work() can get rtnl lock first and wait
2487 * all subchannels to show up, but that may not happen because
2488 * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2489 * -> ... -> device_add() -> ... -> __device_attach() can't get
2490 * the device lock, so all the subchannels can't be processed --
2491 * finally netvsc_subchan_work() hangs forever.
2495 if (nvdev->num_chn > 1)
2496 schedule_work(&nvdev->subchan_work);
2498 /* hw_features computed in rndis_netdev_set_hwcaps() */
2499 net->features = net->hw_features |
2500 NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX |
2501 NETIF_F_HW_VLAN_CTAG_RX;
2502 net->vlan_features = net->features;
2504 netdev_lockdep_set_classes(net);
2506 /* MTU range: 68 - 1500 or 65521 */
2507 net->min_mtu = NETVSC_MTU_MIN;
2508 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2509 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2511 net->max_mtu = ETH_DATA_LEN;
2513 nvdev->tx_disable = false;
2515 ret = register_netdevice(net);
2517 pr_err("Unable to register netdev.\n");
2518 goto register_failed;
2521 list_add(&net_device_ctx->list, &netvsc_dev_list);
2524 netvsc_devinfo_put(device_info);
2529 rndis_filter_device_remove(dev, nvdev);
2531 netvsc_devinfo_put(device_info);
2533 free_percpu(net_device_ctx->vf_stats);
2535 hv_set_drvdata(dev, NULL);
2541 static int netvsc_remove(struct hv_device *dev)
2543 struct net_device_context *ndev_ctx;
2544 struct net_device *vf_netdev, *net;
2545 struct netvsc_device *nvdev;
2547 net = hv_get_drvdata(dev);
2549 dev_err(&dev->device, "No net device to remove\n");
2553 ndev_ctx = netdev_priv(net);
2555 cancel_delayed_work_sync(&ndev_ctx->dwork);
2558 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2560 cancel_work_sync(&nvdev->subchan_work);
2561 netvsc_xdp_set(net, NULL, NULL, nvdev);
2565 * Call to the vsc driver to let it know that the device is being
2566 * removed. Also blocks mtu and channel changes.
2568 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2570 netvsc_unregister_vf(vf_netdev);
2573 rndis_filter_device_remove(dev, nvdev);
2575 unregister_netdevice(net);
2576 list_del(&ndev_ctx->list);
2580 hv_set_drvdata(dev, NULL);
2582 free_percpu(ndev_ctx->vf_stats);
2587 static int netvsc_suspend(struct hv_device *dev)
2589 struct net_device_context *ndev_ctx;
2590 struct net_device *vf_netdev, *net;
2591 struct netvsc_device *nvdev;
2594 net = hv_get_drvdata(dev);
2596 ndev_ctx = netdev_priv(net);
2597 cancel_delayed_work_sync(&ndev_ctx->dwork);
2601 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2602 if (nvdev == NULL) {
2607 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2609 netvsc_unregister_vf(vf_netdev);
2611 /* Save the current config info */
2612 ndev_ctx->saved_netvsc_dev_info = netvsc_devinfo_get(nvdev);
2614 ret = netvsc_detach(net, nvdev);
2621 static int netvsc_resume(struct hv_device *dev)
2623 struct net_device *net = hv_get_drvdata(dev);
2624 struct net_device_context *net_device_ctx;
2625 struct netvsc_device_info *device_info;
2630 net_device_ctx = netdev_priv(net);
2631 device_info = net_device_ctx->saved_netvsc_dev_info;
2633 ret = netvsc_attach(net, device_info);
2635 netvsc_devinfo_put(device_info);
2636 net_device_ctx->saved_netvsc_dev_info = NULL;
2642 static const struct hv_vmbus_device_id id_table[] = {
2648 MODULE_DEVICE_TABLE(vmbus, id_table);
2650 /* The one and only one */
2651 static struct hv_driver netvsc_drv = {
2652 .name = KBUILD_MODNAME,
2653 .id_table = id_table,
2654 .probe = netvsc_probe,
2655 .remove = netvsc_remove,
2656 .suspend = netvsc_suspend,
2657 .resume = netvsc_resume,
2659 .probe_type = PROBE_FORCE_SYNCHRONOUS,
2664 * On Hyper-V, every VF interface is matched with a corresponding
2665 * synthetic interface. The synthetic interface is presented first
2666 * to the guest. When the corresponding VF instance is registered,
2667 * we will take care of switching the data path.
2669 static int netvsc_netdev_event(struct notifier_block *this,
2670 unsigned long event, void *ptr)
2672 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2674 /* Skip our own events */
2675 if (event_dev->netdev_ops == &device_ops)
2678 /* Avoid non-Ethernet type devices */
2679 if (event_dev->type != ARPHRD_ETHER)
2682 /* Avoid Vlan dev with same MAC registering as VF */
2683 if (is_vlan_dev(event_dev))
2686 /* Avoid Bonding master dev with same MAC registering as VF */
2687 if ((event_dev->priv_flags & IFF_BONDING) &&
2688 (event_dev->flags & IFF_MASTER))
2692 case NETDEV_REGISTER:
2693 return netvsc_register_vf(event_dev);
2694 case NETDEV_UNREGISTER:
2695 return netvsc_unregister_vf(event_dev);
2698 return netvsc_vf_changed(event_dev);
2704 static struct notifier_block netvsc_netdev_notifier = {
2705 .notifier_call = netvsc_netdev_event,
2708 static void __exit netvsc_drv_exit(void)
2710 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2711 vmbus_driver_unregister(&netvsc_drv);
2714 static int __init netvsc_drv_init(void)
2718 if (ring_size < RING_SIZE_MIN) {
2719 ring_size = RING_SIZE_MIN;
2720 pr_info("Increased ring_size to %u (min allowed)\n",
2723 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2725 ret = vmbus_driver_register(&netvsc_drv);
2729 register_netdevice_notifier(&netvsc_netdev_notifier);
2733 MODULE_LICENSE("GPL");
2734 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2736 module_init(netvsc_drv_init);
2737 module_exit(netvsc_drv_exit);