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>
37 #include <net/route.h>
39 #include <net/pkt_sched.h>
41 #include "hyperv_net.h"
44 #define RING_SIZE_MIN 64
45 static int ring_size
= 128;
46 module_param(ring_size
, int, S_IRUGO
);
47 MODULE_PARM_DESC(ring_size
, "Ring buffer size (# of pages)");
49 static int max_num_vrss_chns
= 8;
51 static const u32 default_msg
= NETIF_MSG_DRV
| NETIF_MSG_PROBE
|
52 NETIF_MSG_LINK
| NETIF_MSG_IFUP
|
53 NETIF_MSG_IFDOWN
| NETIF_MSG_RX_ERR
|
56 static int debug
= -1;
57 module_param(debug
, int, S_IRUGO
);
58 MODULE_PARM_DESC(debug
, "Debug level (0=none,...,16=all)");
60 static void do_set_multicast(struct work_struct
*w
)
62 struct net_device_context
*ndevctx
=
63 container_of(w
, struct net_device_context
, work
);
64 struct netvsc_device
*nvdev
;
65 struct rndis_device
*rdev
;
67 nvdev
= hv_get_drvdata(ndevctx
->device_ctx
);
68 if (nvdev
== NULL
|| nvdev
->ndev
== NULL
)
71 rdev
= nvdev
->extension
;
75 if (nvdev
->ndev
->flags
& IFF_PROMISC
)
76 rndis_filter_set_packet_filter(rdev
,
77 NDIS_PACKET_TYPE_PROMISCUOUS
);
79 rndis_filter_set_packet_filter(rdev
,
80 NDIS_PACKET_TYPE_BROADCAST
|
81 NDIS_PACKET_TYPE_ALL_MULTICAST
|
82 NDIS_PACKET_TYPE_DIRECTED
);
85 static void netvsc_set_multicast_list(struct net_device
*net
)
87 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
89 schedule_work(&net_device_ctx
->work
);
92 static int netvsc_open(struct net_device
*net
)
94 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
95 struct hv_device
*device_obj
= net_device_ctx
->device_ctx
;
96 struct netvsc_device
*nvdev
;
97 struct rndis_device
*rdev
;
100 netif_carrier_off(net
);
102 /* Open up the device */
103 ret
= rndis_filter_open(device_obj
);
105 netdev_err(net
, "unable to open device (ret %d).\n", ret
);
109 netif_tx_wake_all_queues(net
);
111 nvdev
= hv_get_drvdata(device_obj
);
112 rdev
= nvdev
->extension
;
113 if (!rdev
->link_state
)
114 netif_carrier_on(net
);
119 static int netvsc_close(struct net_device
*net
)
121 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
122 struct hv_device
*device_obj
= net_device_ctx
->device_ctx
;
123 struct netvsc_device
*nvdev
= hv_get_drvdata(device_obj
);
125 u32 aread
, awrite
, i
, msec
= 10, retry
= 0, retry_max
= 20;
126 struct vmbus_channel
*chn
;
128 netif_tx_disable(net
);
130 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
131 cancel_work_sync(&net_device_ctx
->work
);
132 ret
= rndis_filter_close(device_obj
);
134 netdev_err(net
, "unable to close device (ret %d).\n", ret
);
138 /* Ensure pending bytes in ring are read */
141 for (i
= 0; i
< nvdev
->num_chn
; i
++) {
142 chn
= nvdev
->chn_table
[i
];
146 hv_get_ringbuffer_availbytes(&chn
->inbound
, &aread
,
152 hv_get_ringbuffer_availbytes(&chn
->outbound
, &aread
,
160 if (retry
> retry_max
|| aread
== 0)
170 netdev_err(net
, "Ring buffer not empty after closing rndis\n");
177 static void *init_ppi_data(struct rndis_message
*msg
, u32 ppi_size
,
180 struct rndis_packet
*rndis_pkt
;
181 struct rndis_per_packet_info
*ppi
;
183 rndis_pkt
= &msg
->msg
.pkt
;
184 rndis_pkt
->data_offset
+= ppi_size
;
186 ppi
= (struct rndis_per_packet_info
*)((void *)rndis_pkt
+
187 rndis_pkt
->per_pkt_info_offset
+ rndis_pkt
->per_pkt_info_len
);
189 ppi
->size
= ppi_size
;
190 ppi
->type
= pkt_type
;
191 ppi
->ppi_offset
= sizeof(struct rndis_per_packet_info
);
193 rndis_pkt
->per_pkt_info_len
+= ppi_size
;
207 /* Toeplitz hash function
208 * data: network byte order
209 * return: host byte order
211 static u32
comp_hash(u8
*key
, int klen
, void *data
, int dlen
)
220 subk
.ka
= ntohl(*(u32
*)key
);
222 for (i
= 0; i
< dlen
; i
++) {
223 subk
.kb
= key
[k_next
];
224 k_next
= (k_next
+ 1) % klen
;
225 dt
= ((u8
*)data
)[i
];
226 for (j
= 0; j
< 8; j
++) {
237 static bool netvsc_set_hash(u32
*hash
, struct sk_buff
*skb
)
239 struct flow_keys flow
;
242 if (!skb_flow_dissect_flow_keys(skb
, &flow
) ||
243 !(flow
.basic
.n_proto
== htons(ETH_P_IP
) ||
244 flow
.basic
.n_proto
== htons(ETH_P_IPV6
)))
247 if (flow
.basic
.ip_proto
== IPPROTO_TCP
)
252 *hash
= comp_hash(netvsc_hash_key
, HASH_KEYLEN
, &flow
, data_len
);
257 static u16
netvsc_select_queue(struct net_device
*ndev
, struct sk_buff
*skb
,
258 void *accel_priv
, select_queue_fallback_t fallback
)
260 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
261 struct hv_device
*hdev
= net_device_ctx
->device_ctx
;
262 struct netvsc_device
*nvsc_dev
= hv_get_drvdata(hdev
);
266 if (nvsc_dev
== NULL
|| ndev
->real_num_tx_queues
<= 1)
269 if (netvsc_set_hash(&hash
, skb
)) {
270 q_idx
= nvsc_dev
->send_table
[hash
% VRSS_SEND_TAB_SIZE
] %
271 ndev
->real_num_tx_queues
;
272 skb_set_hash(skb
, hash
, PKT_HASH_TYPE_L3
);
278 void netvsc_xmit_completion(void *context
)
280 struct hv_netvsc_packet
*packet
= (struct hv_netvsc_packet
*)context
;
281 struct sk_buff
*skb
= (struct sk_buff
*)
282 (unsigned long)packet
->send_completion_tid
;
285 dev_kfree_skb_any(skb
);
288 static u32
fill_pg_buf(struct page
*page
, u32 offset
, u32 len
,
289 struct hv_page_buffer
*pb
)
293 /* Deal with compund pages by ignoring unused part
296 page
+= (offset
>> PAGE_SHIFT
);
297 offset
&= ~PAGE_MASK
;
302 bytes
= PAGE_SIZE
- offset
;
305 pb
[j
].pfn
= page_to_pfn(page
);
306 pb
[j
].offset
= offset
;
312 if (offset
== PAGE_SIZE
&& len
) {
322 static u32
init_page_array(void *hdr
, u32 len
, struct sk_buff
*skb
,
323 struct hv_netvsc_packet
*packet
)
325 struct hv_page_buffer
*pb
= packet
->page_buf
;
327 char *data
= skb
->data
;
328 int frags
= skb_shinfo(skb
)->nr_frags
;
331 /* The packet is laid out thus:
332 * 1. hdr: RNDIS header and PPI
334 * 3. skb fragment data
337 slots_used
+= fill_pg_buf(virt_to_page(hdr
),
339 len
, &pb
[slots_used
]);
341 packet
->rmsg_size
= len
;
342 packet
->rmsg_pgcnt
= slots_used
;
344 slots_used
+= fill_pg_buf(virt_to_page(data
),
345 offset_in_page(data
),
346 skb_headlen(skb
), &pb
[slots_used
]);
348 for (i
= 0; i
< frags
; i
++) {
349 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
351 slots_used
+= fill_pg_buf(skb_frag_page(frag
),
353 skb_frag_size(frag
), &pb
[slots_used
]);
358 static int count_skb_frag_slots(struct sk_buff
*skb
)
360 int i
, frags
= skb_shinfo(skb
)->nr_frags
;
363 for (i
= 0; i
< frags
; i
++) {
364 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
365 unsigned long size
= skb_frag_size(frag
);
366 unsigned long offset
= frag
->page_offset
;
368 /* Skip unused frames from start of page */
369 offset
&= ~PAGE_MASK
;
370 pages
+= PFN_UP(offset
+ size
);
375 static int netvsc_get_slots(struct sk_buff
*skb
)
377 char *data
= skb
->data
;
378 unsigned int offset
= offset_in_page(data
);
379 unsigned int len
= skb_headlen(skb
);
383 slots
= DIV_ROUND_UP(offset
+ len
, PAGE_SIZE
);
384 frag_slots
= count_skb_frag_slots(skb
);
385 return slots
+ frag_slots
;
388 static u32
get_net_transport_info(struct sk_buff
*skb
, u32
*trans_off
)
390 u32 ret_val
= TRANSPORT_INFO_NOT_IP
;
392 if ((eth_hdr(skb
)->h_proto
!= htons(ETH_P_IP
)) &&
393 (eth_hdr(skb
)->h_proto
!= htons(ETH_P_IPV6
))) {
397 *trans_off
= skb_transport_offset(skb
);
399 if ((eth_hdr(skb
)->h_proto
== htons(ETH_P_IP
))) {
400 struct iphdr
*iphdr
= ip_hdr(skb
);
402 if (iphdr
->protocol
== IPPROTO_TCP
)
403 ret_val
= TRANSPORT_INFO_IPV4_TCP
;
404 else if (iphdr
->protocol
== IPPROTO_UDP
)
405 ret_val
= TRANSPORT_INFO_IPV4_UDP
;
407 if (ipv6_hdr(skb
)->nexthdr
== IPPROTO_TCP
)
408 ret_val
= TRANSPORT_INFO_IPV6_TCP
;
409 else if (ipv6_hdr(skb
)->nexthdr
== IPPROTO_UDP
)
410 ret_val
= TRANSPORT_INFO_IPV6_UDP
;
417 static int netvsc_start_xmit(struct sk_buff
*skb
, struct net_device
*net
)
419 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
420 struct hv_netvsc_packet
*packet
= NULL
;
422 unsigned int num_data_pgs
;
423 struct rndis_message
*rndis_msg
;
424 struct rndis_packet
*rndis_pkt
;
428 struct rndis_per_packet_info
*ppi
;
429 struct ndis_tcp_ip_checksum_info
*csum_info
;
430 struct ndis_tcp_lso_info
*lso_info
;
436 struct hv_page_buffer page_buf
[MAX_PAGE_BUFFER_COUNT
];
437 struct netvsc_stats
*tx_stats
= this_cpu_ptr(net_device_ctx
->tx_stats
);
439 /* We will atmost need two pages to describe the rndis
440 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
441 * of pages in a single packet. If skb is scattered around
442 * more pages we try linearizing it.
446 skb_length
= skb
->len
;
447 num_data_pgs
= netvsc_get_slots(skb
) + 2;
448 if (num_data_pgs
> MAX_PAGE_BUFFER_COUNT
&& linear
) {
449 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
450 num_data_pgs
, skb
->len
);
453 } else if (num_data_pgs
> MAX_PAGE_BUFFER_COUNT
) {
454 if (skb_linearize(skb
)) {
455 net_alert_ratelimited("failed to linearize skb\n");
463 pkt_sz
= sizeof(struct hv_netvsc_packet
) + RNDIS_AND_PPI_SIZE
;
465 ret
= skb_cow_head(skb
, pkt_sz
);
467 netdev_err(net
, "unable to alloc hv_netvsc_packet\n");
471 /* Use the headroom for building up the packet */
472 packet
= (struct hv_netvsc_packet
*)skb
->head
;
475 packet
->xmit_more
= skb
->xmit_more
;
477 packet
->vlan_tci
= skb
->vlan_tci
;
478 packet
->page_buf
= page_buf
;
480 packet
->q_idx
= skb_get_queue_mapping(skb
);
482 packet
->is_data_pkt
= true;
483 packet
->total_data_buflen
= skb
->len
;
485 packet
->rndis_msg
= (struct rndis_message
*)((unsigned long)packet
+
486 sizeof(struct hv_netvsc_packet
));
488 memset(packet
->rndis_msg
, 0, RNDIS_AND_PPI_SIZE
);
490 /* Set the completion routine */
491 packet
->send_completion
= netvsc_xmit_completion
;
492 packet
->send_completion_ctx
= packet
;
493 packet
->send_completion_tid
= (unsigned long)skb
;
495 isvlan
= packet
->vlan_tci
& VLAN_TAG_PRESENT
;
497 /* Add the rndis header */
498 rndis_msg
= packet
->rndis_msg
;
499 rndis_msg
->ndis_msg_type
= RNDIS_MSG_PACKET
;
500 rndis_msg
->msg_len
= packet
->total_data_buflen
;
501 rndis_pkt
= &rndis_msg
->msg
.pkt
;
502 rndis_pkt
->data_offset
= sizeof(struct rndis_packet
);
503 rndis_pkt
->data_len
= packet
->total_data_buflen
;
504 rndis_pkt
->per_pkt_info_offset
= sizeof(struct rndis_packet
);
506 rndis_msg_size
= RNDIS_MESSAGE_SIZE(struct rndis_packet
);
508 hash
= skb_get_hash_raw(skb
);
509 if (hash
!= 0 && net
->real_num_tx_queues
> 1) {
510 rndis_msg_size
+= NDIS_HASH_PPI_SIZE
;
511 ppi
= init_ppi_data(rndis_msg
, NDIS_HASH_PPI_SIZE
,
513 *(u32
*)((void *)ppi
+ ppi
->ppi_offset
) = hash
;
517 struct ndis_pkt_8021q_info
*vlan
;
519 rndis_msg_size
+= NDIS_VLAN_PPI_SIZE
;
520 ppi
= init_ppi_data(rndis_msg
, NDIS_VLAN_PPI_SIZE
,
522 vlan
= (struct ndis_pkt_8021q_info
*)((void *)ppi
+
524 vlan
->vlanid
= packet
->vlan_tci
& VLAN_VID_MASK
;
525 vlan
->pri
= (packet
->vlan_tci
& VLAN_PRIO_MASK
) >>
529 net_trans_info
= get_net_transport_info(skb
, &hdr_offset
);
530 if (net_trans_info
== TRANSPORT_INFO_NOT_IP
)
534 * Setup the sendside checksum offload only if this is not a
540 if ((skb
->ip_summed
== CHECKSUM_NONE
) ||
541 (skb
->ip_summed
== CHECKSUM_UNNECESSARY
))
544 rndis_msg_size
+= NDIS_CSUM_PPI_SIZE
;
545 ppi
= init_ppi_data(rndis_msg
, NDIS_CSUM_PPI_SIZE
,
546 TCPIP_CHKSUM_PKTINFO
);
548 csum_info
= (struct ndis_tcp_ip_checksum_info
*)((void *)ppi
+
551 if (net_trans_info
& (INFO_IPV4
<< 16))
552 csum_info
->transmit
.is_ipv4
= 1;
554 csum_info
->transmit
.is_ipv6
= 1;
556 if (net_trans_info
& INFO_TCP
) {
557 csum_info
->transmit
.tcp_checksum
= 1;
558 csum_info
->transmit
.tcp_header_offset
= hdr_offset
;
559 } else if (net_trans_info
& INFO_UDP
) {
560 /* UDP checksum offload is not supported on ws2008r2.
561 * Furthermore, on ws2012 and ws2012r2, there are some
562 * issues with udp checksum offload from Linux guests.
563 * (these are host issues).
564 * For now compute the checksum here.
569 ret
= skb_cow_head(skb
, 0);
574 udp_len
= ntohs(uh
->len
);
576 uh
->check
= csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
578 udp_len
, IPPROTO_UDP
,
579 csum_partial(uh
, udp_len
, 0));
581 uh
->check
= CSUM_MANGLED_0
;
583 csum_info
->transmit
.udp_checksum
= 0;
588 rndis_msg_size
+= NDIS_LSO_PPI_SIZE
;
589 ppi
= init_ppi_data(rndis_msg
, NDIS_LSO_PPI_SIZE
,
590 TCP_LARGESEND_PKTINFO
);
592 lso_info
= (struct ndis_tcp_lso_info
*)((void *)ppi
+
595 lso_info
->lso_v2_transmit
.type
= NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE
;
596 if (net_trans_info
& (INFO_IPV4
<< 16)) {
597 lso_info
->lso_v2_transmit
.ip_version
=
598 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4
;
599 ip_hdr(skb
)->tot_len
= 0;
600 ip_hdr(skb
)->check
= 0;
601 tcp_hdr(skb
)->check
=
602 ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
603 ip_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
605 lso_info
->lso_v2_transmit
.ip_version
=
606 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6
;
607 ipv6_hdr(skb
)->payload_len
= 0;
608 tcp_hdr(skb
)->check
=
609 ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
610 &ipv6_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
612 lso_info
->lso_v2_transmit
.tcp_header_offset
= hdr_offset
;
613 lso_info
->lso_v2_transmit
.mss
= skb_shinfo(skb
)->gso_size
;
616 /* Start filling in the page buffers with the rndis hdr */
617 rndis_msg
->msg_len
+= rndis_msg_size
;
618 packet
->total_data_buflen
= rndis_msg
->msg_len
;
619 packet
->page_buf_cnt
= init_page_array(rndis_msg
, rndis_msg_size
,
622 ret
= netvsc_send(net_device_ctx
->device_ctx
, packet
);
626 u64_stats_update_begin(&tx_stats
->syncp
);
628 tx_stats
->bytes
+= skb_length
;
629 u64_stats_update_end(&tx_stats
->syncp
);
631 if (ret
!= -EAGAIN
) {
632 dev_kfree_skb_any(skb
);
633 net
->stats
.tx_dropped
++;
637 return (ret
== -EAGAIN
) ? NETDEV_TX_BUSY
: NETDEV_TX_OK
;
641 * netvsc_linkstatus_callback - Link up/down notification
643 void netvsc_linkstatus_callback(struct hv_device
*device_obj
,
644 struct rndis_message
*resp
)
646 struct rndis_indicate_status
*indicate
= &resp
->msg
.indicate_status
;
647 struct net_device
*net
;
648 struct net_device_context
*ndev_ctx
;
649 struct netvsc_device
*net_device
;
650 struct rndis_device
*rdev
;
652 net_device
= hv_get_drvdata(device_obj
);
653 rdev
= net_device
->extension
;
655 switch (indicate
->status
) {
656 case RNDIS_STATUS_MEDIA_CONNECT
:
657 rdev
->link_state
= false;
659 case RNDIS_STATUS_MEDIA_DISCONNECT
:
660 rdev
->link_state
= true;
662 case RNDIS_STATUS_NETWORK_CHANGE
:
663 rdev
->link_change
= true;
669 net
= net_device
->ndev
;
671 if (!net
|| net
->reg_state
!= NETREG_REGISTERED
)
674 ndev_ctx
= netdev_priv(net
);
675 if (!rdev
->link_state
) {
676 schedule_delayed_work(&ndev_ctx
->dwork
, 0);
677 schedule_delayed_work(&ndev_ctx
->dwork
, msecs_to_jiffies(20));
679 schedule_delayed_work(&ndev_ctx
->dwork
, 0);
684 * netvsc_recv_callback - Callback when we receive a packet from the
685 * "wire" on the specified device.
687 int netvsc_recv_callback(struct hv_device
*device_obj
,
688 struct hv_netvsc_packet
*packet
,
689 struct ndis_tcp_ip_checksum_info
*csum_info
)
691 struct net_device
*net
;
692 struct net_device_context
*net_device_ctx
;
694 struct netvsc_stats
*rx_stats
;
696 net
= ((struct netvsc_device
*)hv_get_drvdata(device_obj
))->ndev
;
697 if (!net
|| net
->reg_state
!= NETREG_REGISTERED
) {
698 packet
->status
= NVSP_STAT_FAIL
;
701 net_device_ctx
= netdev_priv(net
);
702 rx_stats
= this_cpu_ptr(net_device_ctx
->rx_stats
);
704 /* Allocate a skb - TODO direct I/O to pages? */
705 skb
= netdev_alloc_skb_ip_align(net
, packet
->total_data_buflen
);
706 if (unlikely(!skb
)) {
707 ++net
->stats
.rx_dropped
;
708 packet
->status
= NVSP_STAT_FAIL
;
713 * Copy to skb. This copy is needed here since the memory pointed by
714 * hv_netvsc_packet cannot be deallocated
716 memcpy(skb_put(skb
, packet
->total_data_buflen
), packet
->data
,
717 packet
->total_data_buflen
);
719 skb
->protocol
= eth_type_trans(skb
, net
);
721 /* We only look at the IP checksum here.
722 * Should we be dropping the packet if checksum
723 * failed? How do we deal with other checksums - TCP/UDP?
725 if (csum_info
->receive
.ip_checksum_succeeded
)
726 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
728 skb
->ip_summed
= CHECKSUM_NONE
;
731 if (packet
->vlan_tci
& VLAN_TAG_PRESENT
)
732 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
),
735 skb_record_rx_queue(skb
, packet
->channel
->
736 offermsg
.offer
.sub_channel_index
);
738 u64_stats_update_begin(&rx_stats
->syncp
);
740 rx_stats
->bytes
+= packet
->total_data_buflen
;
741 u64_stats_update_end(&rx_stats
->syncp
);
744 * Pass the skb back up. Network stack will deallocate the skb when it
753 static void netvsc_get_drvinfo(struct net_device
*net
,
754 struct ethtool_drvinfo
*info
)
756 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
757 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
760 static void netvsc_get_channels(struct net_device
*net
,
761 struct ethtool_channels
*channel
)
763 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
764 struct hv_device
*dev
= net_device_ctx
->device_ctx
;
765 struct netvsc_device
*nvdev
= hv_get_drvdata(dev
);
768 channel
->max_combined
= nvdev
->max_chn
;
769 channel
->combined_count
= nvdev
->num_chn
;
773 static int netvsc_change_mtu(struct net_device
*ndev
, int mtu
)
775 struct net_device_context
*ndevctx
= netdev_priv(ndev
);
776 struct hv_device
*hdev
= ndevctx
->device_ctx
;
777 struct netvsc_device
*nvdev
= hv_get_drvdata(hdev
);
778 struct netvsc_device_info device_info
;
779 int limit
= ETH_DATA_LEN
;
782 if (nvdev
== NULL
|| nvdev
->destroy
)
785 if (nvdev
->nvsp_version
>= NVSP_PROTOCOL_VERSION_2
)
786 limit
= NETVSC_MTU
- ETH_HLEN
;
788 if (mtu
< NETVSC_MTU_MIN
|| mtu
> limit
)
791 ret
= netvsc_close(ndev
);
795 nvdev
->start_remove
= true;
796 rndis_filter_device_remove(hdev
);
800 ndevctx
->device_ctx
= hdev
;
801 hv_set_drvdata(hdev
, ndev
);
802 device_info
.ring_size
= ring_size
;
803 device_info
.max_num_vrss_chns
= max_num_vrss_chns
;
804 rndis_filter_device_add(hdev
, &device_info
);
812 static struct rtnl_link_stats64
*netvsc_get_stats64(struct net_device
*net
,
813 struct rtnl_link_stats64
*t
)
815 struct net_device_context
*ndev_ctx
= netdev_priv(net
);
818 for_each_possible_cpu(cpu
) {
819 struct netvsc_stats
*tx_stats
= per_cpu_ptr(ndev_ctx
->tx_stats
,
821 struct netvsc_stats
*rx_stats
= per_cpu_ptr(ndev_ctx
->rx_stats
,
823 u64 tx_packets
, tx_bytes
, rx_packets
, rx_bytes
;
827 start
= u64_stats_fetch_begin_irq(&tx_stats
->syncp
);
828 tx_packets
= tx_stats
->packets
;
829 tx_bytes
= tx_stats
->bytes
;
830 } while (u64_stats_fetch_retry_irq(&tx_stats
->syncp
, start
));
833 start
= u64_stats_fetch_begin_irq(&rx_stats
->syncp
);
834 rx_packets
= rx_stats
->packets
;
835 rx_bytes
= rx_stats
->bytes
;
836 } while (u64_stats_fetch_retry_irq(&rx_stats
->syncp
, start
));
838 t
->tx_bytes
+= tx_bytes
;
839 t
->tx_packets
+= tx_packets
;
840 t
->rx_bytes
+= rx_bytes
;
841 t
->rx_packets
+= rx_packets
;
844 t
->tx_dropped
= net
->stats
.tx_dropped
;
845 t
->tx_errors
= net
->stats
.tx_dropped
;
847 t
->rx_dropped
= net
->stats
.rx_dropped
;
848 t
->rx_errors
= net
->stats
.rx_errors
;
853 static int netvsc_set_mac_addr(struct net_device
*ndev
, void *p
)
855 struct net_device_context
*ndevctx
= netdev_priv(ndev
);
856 struct hv_device
*hdev
= ndevctx
->device_ctx
;
857 struct sockaddr
*addr
= p
;
858 char save_adr
[ETH_ALEN
];
859 unsigned char save_aatype
;
862 memcpy(save_adr
, ndev
->dev_addr
, ETH_ALEN
);
863 save_aatype
= ndev
->addr_assign_type
;
865 err
= eth_mac_addr(ndev
, p
);
869 err
= rndis_filter_set_device_mac(hdev
, addr
->sa_data
);
871 /* roll back to saved MAC */
872 memcpy(ndev
->dev_addr
, save_adr
, ETH_ALEN
);
873 ndev
->addr_assign_type
= save_aatype
;
879 #ifdef CONFIG_NET_POLL_CONTROLLER
880 static void netvsc_poll_controller(struct net_device
*net
)
882 /* As netvsc_start_xmit() works synchronous we don't have to
883 * trigger anything here.
888 static const struct ethtool_ops ethtool_ops
= {
889 .get_drvinfo
= netvsc_get_drvinfo
,
890 .get_link
= ethtool_op_get_link
,
891 .get_channels
= netvsc_get_channels
,
894 static const struct net_device_ops device_ops
= {
895 .ndo_open
= netvsc_open
,
896 .ndo_stop
= netvsc_close
,
897 .ndo_start_xmit
= netvsc_start_xmit
,
898 .ndo_set_rx_mode
= netvsc_set_multicast_list
,
899 .ndo_change_mtu
= netvsc_change_mtu
,
900 .ndo_validate_addr
= eth_validate_addr
,
901 .ndo_set_mac_address
= netvsc_set_mac_addr
,
902 .ndo_select_queue
= netvsc_select_queue
,
903 .ndo_get_stats64
= netvsc_get_stats64
,
904 #ifdef CONFIG_NET_POLL_CONTROLLER
905 .ndo_poll_controller
= netvsc_poll_controller
,
910 * Send GARP packet to network peers after migrations.
911 * After Quick Migration, the network is not immediately operational in the
912 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
913 * another netif_notify_peers() into a delayed work, otherwise GARP packet
914 * will not be sent after quick migration, and cause network disconnection.
915 * Also, we update the carrier status here.
917 static void netvsc_link_change(struct work_struct
*w
)
919 struct net_device_context
*ndev_ctx
;
920 struct net_device
*net
;
921 struct netvsc_device
*net_device
;
922 struct rndis_device
*rdev
;
923 bool notify
, refresh
= false;
924 char *argv
[] = { "/etc/init.d/network", "restart", NULL
};
925 char *envp
[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL
};
929 ndev_ctx
= container_of(w
, struct net_device_context
, dwork
.work
);
930 net_device
= hv_get_drvdata(ndev_ctx
->device_ctx
);
931 rdev
= net_device
->extension
;
932 net
= net_device
->ndev
;
934 if (rdev
->link_state
) {
935 netif_carrier_off(net
);
938 netif_carrier_on(net
);
940 if (rdev
->link_change
) {
941 rdev
->link_change
= false;
949 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
952 netdev_notify_peers(net
);
955 static void netvsc_free_netdev(struct net_device
*netdev
)
957 struct net_device_context
*net_device_ctx
= netdev_priv(netdev
);
959 free_percpu(net_device_ctx
->tx_stats
);
960 free_percpu(net_device_ctx
->rx_stats
);
964 static int netvsc_probe(struct hv_device
*dev
,
965 const struct hv_vmbus_device_id
*dev_id
)
967 struct net_device
*net
= NULL
;
968 struct net_device_context
*net_device_ctx
;
969 struct netvsc_device_info device_info
;
970 struct netvsc_device
*nvdev
;
972 u32 max_needed_headroom
;
974 net
= alloc_etherdev_mq(sizeof(struct net_device_context
),
979 max_needed_headroom
= sizeof(struct hv_netvsc_packet
) +
982 netif_carrier_off(net
);
984 net_device_ctx
= netdev_priv(net
);
985 net_device_ctx
->device_ctx
= dev
;
986 net_device_ctx
->msg_enable
= netif_msg_init(debug
, default_msg
);
987 if (netif_msg_probe(net_device_ctx
))
988 netdev_dbg(net
, "netvsc msg_enable: %d\n",
989 net_device_ctx
->msg_enable
);
991 net_device_ctx
->tx_stats
= netdev_alloc_pcpu_stats(struct netvsc_stats
);
992 if (!net_device_ctx
->tx_stats
) {
996 net_device_ctx
->rx_stats
= netdev_alloc_pcpu_stats(struct netvsc_stats
);
997 if (!net_device_ctx
->rx_stats
) {
998 free_percpu(net_device_ctx
->tx_stats
);
1003 hv_set_drvdata(dev
, net
);
1004 INIT_DELAYED_WORK(&net_device_ctx
->dwork
, netvsc_link_change
);
1005 INIT_WORK(&net_device_ctx
->work
, do_set_multicast
);
1007 net
->netdev_ops
= &device_ops
;
1009 net
->hw_features
= NETIF_F_RXCSUM
| NETIF_F_SG
| NETIF_F_IP_CSUM
|
1011 net
->features
= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_SG
| NETIF_F_RXCSUM
|
1012 NETIF_F_IP_CSUM
| NETIF_F_TSO
;
1014 net
->ethtool_ops
= ðtool_ops
;
1015 SET_NETDEV_DEV(net
, &dev
->device
);
1018 * Request additional head room in the skb.
1019 * We will use this space to build the rndis
1020 * heaser and other state we need to maintain.
1022 net
->needed_headroom
= max_needed_headroom
;
1024 /* Notify the netvsc driver of the new device */
1025 device_info
.ring_size
= ring_size
;
1026 device_info
.max_num_vrss_chns
= max_num_vrss_chns
;
1027 ret
= rndis_filter_device_add(dev
, &device_info
);
1029 netdev_err(net
, "unable to add netvsc device (ret %d)\n", ret
);
1030 netvsc_free_netdev(net
);
1031 hv_set_drvdata(dev
, NULL
);
1034 memcpy(net
->dev_addr
, device_info
.mac_adr
, ETH_ALEN
);
1036 nvdev
= hv_get_drvdata(dev
);
1037 netif_set_real_num_tx_queues(net
, nvdev
->num_chn
);
1038 netif_set_real_num_rx_queues(net
, nvdev
->num_chn
);
1040 ret
= register_netdev(net
);
1042 pr_err("Unable to register netdev.\n");
1043 rndis_filter_device_remove(dev
);
1044 netvsc_free_netdev(net
);
1046 schedule_delayed_work(&net_device_ctx
->dwork
, 0);
1052 static int netvsc_remove(struct hv_device
*dev
)
1054 struct net_device
*net
;
1055 struct net_device_context
*ndev_ctx
;
1056 struct netvsc_device
*net_device
;
1058 net_device
= hv_get_drvdata(dev
);
1059 net
= net_device
->ndev
;
1062 dev_err(&dev
->device
, "No net device to remove\n");
1066 net_device
->start_remove
= true;
1068 ndev_ctx
= netdev_priv(net
);
1069 cancel_delayed_work_sync(&ndev_ctx
->dwork
);
1070 cancel_work_sync(&ndev_ctx
->work
);
1072 /* Stop outbound asap */
1073 netif_tx_disable(net
);
1075 unregister_netdev(net
);
1078 * Call to the vsc driver to let it know that the device is being
1081 rndis_filter_device_remove(dev
);
1083 netvsc_free_netdev(net
);
1087 static const struct hv_vmbus_device_id id_table
[] = {
1093 MODULE_DEVICE_TABLE(vmbus
, id_table
);
1095 /* The one and only one */
1096 static struct hv_driver netvsc_drv
= {
1097 .name
= KBUILD_MODNAME
,
1098 .id_table
= id_table
,
1099 .probe
= netvsc_probe
,
1100 .remove
= netvsc_remove
,
1103 static void __exit
netvsc_drv_exit(void)
1105 vmbus_driver_unregister(&netvsc_drv
);
1108 static int __init
netvsc_drv_init(void)
1110 if (ring_size
< RING_SIZE_MIN
) {
1111 ring_size
= RING_SIZE_MIN
;
1112 pr_info("Increased ring_size to %d (min allowed)\n",
1115 return vmbus_driver_register(&netvsc_drv
);
1118 MODULE_LICENSE("GPL");
1119 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1121 module_init(netvsc_drv_init
);
1122 module_exit(netvsc_drv_exit
);