blob: 3997c2d64427aa2d2fc4eb69f0ba68cfabbcc832 [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Hank Janssenfceaf242009-07-13 15:34:54 -070022#include <linux/init.h>
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070023#include <linux/atomic.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070024#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070027#include <linux/io.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
Haiyang Zhangc802db12013-05-28 06:15:56 +000033#include <linux/if_vlan.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070034#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
stephen hemminger27f5aa92017-07-24 10:57:29 -070036#include <linux/rtnetlink.h>
stephen hemminger0c195562017-08-01 19:58:53 -070037#include <linux/netpoll.h>
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080038#include <linux/reciprocal_div.h>
stephen hemminger27f5aa92017-07-24 10:57:29 -070039
Hank Janssenfceaf242009-07-13 15:34:54 -070040#include <net/arp.h>
41#include <net/route.h>
42#include <net/sock.h>
43#include <net/pkt_sched.h>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070044#include <net/checksum.h>
45#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070046
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070047#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070048
stephen hemminger8b532792017-08-09 17:46:11 -070049#define RING_SIZE_MIN 64
stephen hemminger8b532792017-08-09 17:46:11 -070050
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010051#define LINKCHANGE_INT (2 * HZ)
stephen hemminger6123c662017-08-09 17:46:03 -070052#define VF_TAKEOVER_INT (HZ / 10)
stephen hemmingera50af862016-12-06 13:43:54 -080053
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080054static unsigned int ring_size __ro_after_init = 128;
55module_param(ring_size, uint, S_IRUGO);
Stephen Hemminger450d7a42010-05-04 09:58:53 -070056MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080057unsigned int netvsc_ring_bytes __ro_after_init;
58struct reciprocal_value netvsc_ring_reciprocal __ro_after_init;
Hank Janssenfceaf242009-07-13 15:34:54 -070059
Simon Xiao3f300ff2015-04-28 01:05:17 -070060static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
61 NETIF_MSG_LINK | NETIF_MSG_IFUP |
62 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
63 NETIF_MSG_TX_ERR;
64
65static int debug = -1;
66module_param(debug, int, S_IRUGO);
67MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
68
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070069static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070070{
Wenqi Ma792df872012-04-19 00:39:37 +000071 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070072 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080073
stephen hemminger4f19c0d2017-06-07 15:53:49 -070074 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070075}
76
Hank Janssenfceaf242009-07-13 15:34:54 -070077static int netvsc_open(struct net_device *net)
78{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070079 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -070080 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070081 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080082 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070083 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070084
Haiyang Zhang891de742014-02-12 16:54:27 -080085 netif_carrier_off(net);
86
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070087 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020088 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070089 if (ret != 0) {
90 netdev_err(net, "unable to open device (ret %d).\n", ret);
91 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070092 }
93
Haiyang Zhang2de85302015-07-13 13:09:16 -070094 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070095
Haiyang Zhang891de742014-02-12 16:54:27 -080096 rdev = nvdev->extension;
stephen hemminger0c195562017-08-01 19:58:53 -070097
98 if (!rdev->link_state)
Haiyang Zhang891de742014-02-12 16:54:27 -080099 netif_carrier_on(net);
100
stephen hemminger0c195562017-08-01 19:58:53 -0700101 if (vf_netdev) {
102 /* Setting synthetic device up transparently sets
103 * slave as up. If open fails, then slave will be
104 * still be offline (and not used).
105 */
106 ret = dev_open(vf_netdev);
107 if (ret)
108 netdev_warn(net,
109 "unable to open slave: %s: %d\n",
110 vf_netdev->name, ret);
111 }
112 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700113}
114
Hank Janssenfceaf242009-07-13 15:34:54 -0700115static int netvsc_close(struct net_device *net)
116{
Hank Janssenfceaf242009-07-13 15:34:54 -0700117 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700118 struct net_device *vf_netdev
119 = rtnl_dereference(net_device_ctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700120 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700121 int ret = 0;
stephen hemminger40975962017-06-08 16:21:19 -0700122 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700123 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700124
Haiyang Zhang0a282532012-02-02 07:17:59 +0000125 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700126
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700127 /* No need to close rndis filter if it is removed already */
128 if (!nvdev)
129 goto out;
130
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200131 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700132 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700133 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700134 return ret;
135 }
136
137 /* Ensure pending bytes in ring are read */
138 while (true) {
139 aread = 0;
140 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800141 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700142 if (!chn)
143 continue;
144
stephen hemminger40975962017-06-08 16:21:19 -0700145 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700146 if (aread)
147 break;
148
stephen hemminger40975962017-06-08 16:21:19 -0700149 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700150 if (aread)
151 break;
152 }
153
154 retry++;
155 if (retry > retry_max || aread == 0)
156 break;
157
158 msleep(msec);
159
160 if (msec < 1000)
161 msec *= 2;
162 }
163
164 if (aread) {
165 netdev_err(net, "Ring buffer not empty after closing rndis\n");
166 ret = -ETIMEDOUT;
167 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700168
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700169out:
stephen hemminger0c195562017-08-01 19:58:53 -0700170 if (vf_netdev)
171 dev_close(vf_netdev);
172
Hank Janssenfceaf242009-07-13 15:34:54 -0700173 return ret;
174}
175
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800176static inline void *init_ppi_data(struct rndis_message *msg,
177 u32 ppi_size, u32 pkt_type)
KY Srinivasan8a002512014-03-08 19:23:14 -0800178{
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800179 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
KY Srinivasan8a002512014-03-08 19:23:14 -0800180 struct rndis_per_packet_info *ppi;
181
KY Srinivasan8a002512014-03-08 19:23:14 -0800182 rndis_pkt->data_offset += ppi_size;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800183 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
184 + rndis_pkt->per_pkt_info_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800185
186 ppi->size = ppi_size;
187 ppi->type = pkt_type;
188 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
189
190 rndis_pkt->per_pkt_info_len += ppi_size;
191
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800192 return ppi + 1;
KY Srinivasan8a002512014-03-08 19:23:14 -0800193}
194
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700195/* Azure hosts don't support non-TCP port numbers in hashing for fragmented
196 * packets. We can use ethtool to change UDP hash level when necessary.
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700197 */
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700198static inline u32 netvsc_get_hash(
199 struct sk_buff *skb,
200 const struct net_device_context *ndc)
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700201{
202 struct flow_keys flow;
Haiyang Zhang486e3982017-10-06 08:33:57 -0700203 u32 hash, pkt_proto = 0;
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700204 static u32 hashrnd __read_mostly;
205
206 net_get_random_once(&hashrnd, sizeof(hashrnd));
207
208 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
209 return 0;
210
Haiyang Zhang486e3982017-10-06 08:33:57 -0700211 switch (flow.basic.ip_proto) {
212 case IPPROTO_TCP:
213 if (flow.basic.n_proto == htons(ETH_P_IP))
214 pkt_proto = HV_TCP4_L4HASH;
215 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
216 pkt_proto = HV_TCP6_L4HASH;
217
218 break;
219
220 case IPPROTO_UDP:
221 if (flow.basic.n_proto == htons(ETH_P_IP))
222 pkt_proto = HV_UDP4_L4HASH;
223 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
224 pkt_proto = HV_UDP6_L4HASH;
225
226 break;
227 }
228
229 if (pkt_proto & ndc->l4_hash) {
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700230 return skb_get_hash(skb);
231 } else {
232 if (flow.basic.n_proto == htons(ETH_P_IP))
233 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
234 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
235 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
236 else
237 hash = 0;
238
239 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
240 }
241
242 return hash;
243}
244
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700245static inline int netvsc_get_tx_queue(struct net_device *ndev,
246 struct sk_buff *skb, int old_idx)
247{
248 const struct net_device_context *ndc = netdev_priv(ndev);
249 struct sock *sk = skb->sk;
250 int q_idx;
251
Haiyang Zhang39e91cf2017-10-13 12:28:04 -0700252 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
253 (VRSS_SEND_TAB_SIZE - 1)];
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700254
255 /* If queue index changed record the new value */
256 if (q_idx != old_idx &&
257 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
258 sk_tx_queue_set(sk, q_idx);
259
260 return q_idx;
261}
262
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800263/*
264 * Select queue for transmit.
265 *
266 * If a valid queue has already been assigned, then use that.
267 * Otherwise compute tx queue based on hash and the send table.
268 *
269 * This is basically similar to default (__netdev_pick_tx) with the added step
270 * of using the host send_table when no other queue has been assigned.
271 *
272 * TODO support XPS - but get_xps_queue not exported
273 */
stephen hemminger0c195562017-08-01 19:58:53 -0700274static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700275{
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700276 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700277
stephen hemminger0c195562017-08-01 19:58:53 -0700278 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700279 /* If forwarding a packet, we use the recorded queue when
280 * available for better cache locality.
281 */
282 if (skb_rx_queue_recorded(skb))
283 q_idx = skb_get_rx_queue(skb);
284 else
285 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800286 }
287
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700288 return q_idx;
289}
290
stephen hemminger0c195562017-08-01 19:58:53 -0700291static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
292 void *accel_priv,
293 select_queue_fallback_t fallback)
294{
295 struct net_device_context *ndc = netdev_priv(ndev);
296 struct net_device *vf_netdev;
297 u16 txq;
298
299 rcu_read_lock();
300 vf_netdev = rcu_dereference(ndc->vf_netdev);
301 if (vf_netdev) {
302 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
303 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
304 } else {
305 txq = netvsc_pick_tx(ndev, skb);
306 }
307 rcu_read_unlock();
308
309 while (unlikely(txq >= ndev->real_num_tx_queues))
310 txq -= ndev->real_num_tx_queues;
311
312 return txq;
313}
314
KY Srinivasan54a73572014-03-08 19:23:13 -0800315static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700316 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800317{
318 int j = 0;
319
320 /* Deal with compund pages by ignoring unused part
321 * of the page.
322 */
323 page += (offset >> PAGE_SHIFT);
324 offset &= ~PAGE_MASK;
325
326 while (len > 0) {
327 unsigned long bytes;
328
329 bytes = PAGE_SIZE - offset;
330 if (bytes > len)
331 bytes = len;
332 pb[j].pfn = page_to_pfn(page);
333 pb[j].offset = offset;
334 pb[j].len = bytes;
335
336 offset += bytes;
337 len -= bytes;
338
339 if (offset == PAGE_SIZE && len) {
340 page++;
341 offset = 0;
342 j++;
343 }
344 }
345
346 return j + 1;
347}
348
KY Srinivasan8a002512014-03-08 19:23:14 -0800349static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800350 struct hv_netvsc_packet *packet,
stephen hemminger02b6de02017-07-28 08:59:44 -0700351 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800352{
353 u32 slots_used = 0;
354 char *data = skb->data;
355 int frags = skb_shinfo(skb)->nr_frags;
356 int i;
357
358 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700359 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800360 * 2. skb linear data
361 * 3. skb fragment data
362 */
stephen hemmingerea5a32c2017-08-09 17:46:10 -0700363 slots_used += fill_pg_buf(virt_to_page(hdr),
364 offset_in_page(hdr),
365 len, &pb[slots_used]);
KY Srinivasan54a73572014-03-08 19:23:13 -0800366
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700367 packet->rmsg_size = len;
368 packet->rmsg_pgcnt = slots_used;
369
KY Srinivasan54a73572014-03-08 19:23:13 -0800370 slots_used += fill_pg_buf(virt_to_page(data),
371 offset_in_page(data),
372 skb_headlen(skb), &pb[slots_used]);
373
374 for (i = 0; i < frags; i++) {
375 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
376
377 slots_used += fill_pg_buf(skb_frag_page(frag),
378 frag->page_offset,
379 skb_frag_size(frag), &pb[slots_used]);
380 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800381 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800382}
383
stephen hemminger80d887d2017-07-24 21:03:19 -0700384static int count_skb_frag_slots(struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800385{
stephen hemminger80d887d2017-07-24 21:03:19 -0700386 int i, frags = skb_shinfo(skb)->nr_frags;
387 int pages = 0;
388
389 for (i = 0; i < frags; i++) {
390 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
391 unsigned long size = skb_frag_size(frag);
392 unsigned long offset = frag->page_offset;
393
394 /* Skip unused frames from start of page */
395 offset &= ~PAGE_MASK;
396 pages += PFN_UP(offset + size);
397 }
398 return pages;
399}
400
401static int netvsc_get_slots(struct sk_buff *skb)
402{
403 char *data = skb->data;
404 unsigned int offset = offset_in_page(data);
405 unsigned int len = skb_headlen(skb);
406 int slots;
407 int frag_slots;
408
409 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
410 frag_slots = count_skb_frag_slots(skb);
411 return slots + frag_slots;
KY Srinivasan54a73572014-03-08 19:23:13 -0800412}
413
stephen hemminger23312a32017-01-24 13:05:59 -0800414static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800415{
stephen hemminger23312a32017-01-24 13:05:59 -0800416 if (skb->protocol == htons(ETH_P_IP)) {
417 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800418
stephen hemminger23312a32017-01-24 13:05:59 -0800419 if (ip->protocol == IPPROTO_TCP)
420 return TRANSPORT_INFO_IPV4_TCP;
421 else if (ip->protocol == IPPROTO_UDP)
422 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800423 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800424 struct ipv6hdr *ip6 = ipv6_hdr(skb);
425
426 if (ip6->nexthdr == IPPROTO_TCP)
427 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700428 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800429 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800430 }
431
stephen hemminger23312a32017-01-24 13:05:59 -0800432 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800433}
434
stephen hemminger0c195562017-08-01 19:58:53 -0700435/* Send skb on the slave VF device. */
436static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
437 struct sk_buff *skb)
438{
439 struct net_device_context *ndev_ctx = netdev_priv(net);
440 unsigned int len = skb->len;
441 int rc;
442
443 skb->dev = vf_netdev;
444 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
445
446 rc = dev_queue_xmit(skb);
447 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
448 struct netvsc_vf_pcpu_stats *pcpu_stats
449 = this_cpu_ptr(ndev_ctx->vf_stats);
450
451 u64_stats_update_begin(&pcpu_stats->syncp);
452 pcpu_stats->tx_packets++;
453 pcpu_stats->tx_bytes += len;
454 u64_stats_update_end(&pcpu_stats->syncp);
455 } else {
456 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
457 }
458
459 return rc;
460}
461
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700462static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700463{
Hank Janssenfceaf242009-07-13 15:34:54 -0700464 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200465 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700466 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800467 unsigned int num_data_pgs;
468 struct rndis_message *rndis_msg;
stephen hemminger0c195562017-08-01 19:58:53 -0700469 struct net_device *vf_netdev;
KY Srinivasan8a002512014-03-08 19:23:14 -0800470 u32 rndis_msg_size;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700471 u32 hash;
stephen hemminger02b6de02017-07-28 08:59:44 -0700472 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
Hank Janssenfceaf242009-07-13 15:34:54 -0700473
stephen hemminger0c195562017-08-01 19:58:53 -0700474 /* if VF is present and up then redirect packets
475 * already called with rcu_read_lock_bh
476 */
477 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
478 if (vf_netdev && netif_running(vf_netdev) &&
479 !netpoll_tx_running(net))
480 return netvsc_vf_xmit(net, vf_netdev, skb);
481
stephen hemminger80d887d2017-07-24 21:03:19 -0700482 /* We will atmost need two pages to describe the rndis
483 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200484 * of pages in a single packet. If skb is scattered around
485 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800486 */
stephen hemminger80d887d2017-07-24 21:03:19 -0700487
488 num_data_pgs = netvsc_get_slots(skb) + 2;
489
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700490 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700491 ++net_device_ctx->eth_stats.tx_scattered;
492
493 if (skb_linearize(skb))
494 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700495
stephen hemminger80d887d2017-07-24 21:03:19 -0700496 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700497 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700498 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700499 goto drop;
500 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800501 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700502
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800503 /*
504 * Place the rndis header in the skb head room and
505 * the skb->cb will be used for hv_netvsc_packet
506 * structure.
507 */
508 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700509 if (ret)
510 goto no_memory;
511
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800512 /* Use the skb control buffer for building up the packet */
513 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
514 FIELD_SIZEOF(struct sk_buff, cb));
515 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700516
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700517 packet->q_idx = skb_get_queue_mapping(skb);
518
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800519 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800520 packet->total_bytes = skb->len;
521 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700522
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800523 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700524
KY Srinivasan8a002512014-03-08 19:23:14 -0800525 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800526 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
527 rndis_msg->msg_len = packet->total_data_buflen;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800528
529 rndis_msg->msg.pkt = (struct rndis_packet) {
530 .data_offset = sizeof(struct rndis_packet),
531 .data_len = packet->total_data_buflen,
532 .per_pkt_info_offset = sizeof(struct rndis_packet),
533 };
KY Srinivasan8a002512014-03-08 19:23:14 -0800534
535 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
536
Haiyang Zhang307f0992014-05-21 12:55:39 -0700537 hash = skb_get_hash_raw(skb);
538 if (hash != 0 && net->real_num_tx_queues > 1) {
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800539 u32 *hash_info;
540
Haiyang Zhang307f0992014-05-21 12:55:39 -0700541 rndis_msg_size += NDIS_HASH_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800542 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
543 NBL_HASH_VALUE);
544 *hash_info = hash;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700545 }
546
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700547 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800548 struct ndis_pkt_8021q_info *vlan;
549
550 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800551 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
552 IEEE_8021Q_INFO);
stephen hemminger00f50242017-08-09 17:46:09 -0700553
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800554 vlan->value = 0;
KY Srinivasan760d1e32015-12-01 16:43:19 -0800555 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
556 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800557 VLAN_PRIO_SHIFT;
558 }
559
stephen hemminger23312a32017-01-24 13:05:59 -0800560 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700561 struct ndis_tcp_lso_info *lso_info;
562
563 rndis_msg_size += NDIS_LSO_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800564 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
565 TCP_LARGESEND_PKTINFO);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700566
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800567 lso_info->value = 0;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700568 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800569 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700570 lso_info->lso_v2_transmit.ip_version =
571 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
572 ip_hdr(skb)->tot_len = 0;
573 ip_hdr(skb)->check = 0;
574 tcp_hdr(skb)->check =
575 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
576 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
577 } else {
578 lso_info->lso_v2_transmit.ip_version =
579 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
580 ipv6_hdr(skb)->payload_len = 0;
581 tcp_hdr(skb)->check =
582 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
583 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
584 }
stephen hemminger23312a32017-01-24 13:05:59 -0800585 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700586 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700587 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800588 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
589 struct ndis_tcp_ip_checksum_info *csum_info;
590
stephen hemmingerad19bc82016-10-11 14:03:07 -0700591 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800592 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
593 TCPIP_CHKSUM_PKTINFO);
stephen hemmingerad19bc82016-10-11 14:03:07 -0700594
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800595 csum_info->value = 0;
stephen hemminger23312a32017-01-24 13:05:59 -0800596 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
597
598 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700599 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800600
601 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
602 csum_info->transmit.tcp_checksum = 1;
603 else
604 csum_info->transmit.udp_checksum = 1;
605 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700606 csum_info->transmit.is_ipv6 = 1;
607
stephen hemminger23312a32017-01-24 13:05:59 -0800608 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
609 csum_info->transmit.tcp_checksum = 1;
610 else
611 csum_info->transmit.udp_checksum = 1;
612 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700613 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800614 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700615 if (skb_checksum_help(skb))
616 goto drop;
617 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700618 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800619
KY Srinivasan8a002512014-03-08 19:23:14 -0800620 /* Start filling in the page buffers with the rndis hdr */
621 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700622 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800623 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
stephen hemminger02b6de02017-07-28 08:59:44 -0700624 skb, packet, pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800625
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800626 /* timestamp packet in software */
627 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700628
stephen hemminger02b6de02017-07-28 08:59:44 -0700629 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800630 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700631 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700632
633 if (ret == -EAGAIN) {
634 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700635 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700636 }
637
638 if (ret == -ENOSPC)
639 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700640
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700641drop:
642 dev_kfree_skb_any(skb);
643 net->stats.tx_dropped++;
644
645 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700646
647no_memory:
648 ++net_device_ctx->eth_stats.tx_no_memory;
649 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700650}
stephen hemminger89bb42b2017-08-09 17:46:08 -0700651
Hank Janssen3e189512010-03-04 22:11:00 +0000652/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700653 * netvsc_linkstatus_callback - Link up/down notification
654 */
Stephen Hemminger79cf1ba2017-12-12 16:48:37 -0800655void netvsc_linkstatus_callback(struct net_device *net,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700656 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700657{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700658 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
Stephen Hemminger79cf1ba2017-12-12 16:48:37 -0800659 struct net_device_context *ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100660 struct netvsc_reconfig *event;
661 unsigned long flags;
662
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700663 /* Update the physical link speed when changing to another vSwitch */
664 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
665 u32 speed;
666
stephen hemminger89bb42b2017-08-09 17:46:08 -0700667 speed = *(u32 *)((void *)indicate
668 + indicate->status_buf_offset) / 10000;
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700669 ndev_ctx->speed = speed;
670 return;
671 }
672
673 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100674 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
675 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
676 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
677 return;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700678
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700679 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700680 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700681
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100682 event = kzalloc(sizeof(*event), GFP_ATOMIC);
683 if (!event)
684 return;
685 event->event = indicate->status;
686
687 spin_lock_irqsave(&ndev_ctx->lock, flags);
688 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
689 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
690
691 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700692}
693
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700694static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800695 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800696 const struct ndis_tcp_ip_checksum_info *csum_info,
697 const struct ndis_pkt_8021q_info *vlan,
698 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700699{
Hank Janssenfceaf242009-07-13 15:34:54 -0700700 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700701
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800702 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700703 if (!skb)
704 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700705
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700706 /*
707 * Copy to skb. This copy is needed here since the memory pointed by
708 * hv_netvsc_packet cannot be deallocated
709 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200710 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700711
712 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700713
714 /* skb is already created with CHECKSUM_NONE */
715 skb_checksum_none_assert(skb);
716
717 /*
718 * In Linux, the IP checksum is always checked.
719 * Do L4 checksum offload if enabled and present.
720 */
721 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
722 if (csum_info->receive.tcp_checksum_succeeded ||
723 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800724 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800725 }
726
stephen hemmingerdc54a082017-01-24 13:06:08 -0800727 if (vlan) {
728 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
729
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700730 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800731 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800732 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700733
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700734 return skb;
735}
736
737/*
738 * netvsc_recv_callback - Callback when we receive a packet from the
739 * "wire" on the specified device.
740 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800741int netvsc_recv_callback(struct net_device *net,
742 struct vmbus_channel *channel,
743 void *data, u32 len,
744 const struct ndis_tcp_ip_checksum_info *csum_info,
745 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700746{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200747 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700748 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800749 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700750 struct netvsc_channel *nvchan;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700751 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700752 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700753
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700754 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700755 return NVSP_STAT_FAIL;
756
stephen hemminger0719e722017-01-11 09:16:32 -0800757 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700758 net_device = rcu_dereference(net_device_ctx->nvdev);
759 if (unlikely(!net_device))
760 goto drop;
761
762 nvchan = &net_device->chan_table[q_idx];
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700763
764 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800765 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
766 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700767 if (unlikely(!skb)) {
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -0800768 ++net_device_ctx->eth_stats.rx_no_memory;
stephen hemminger545a8e72017-03-22 14:51:00 -0700769drop:
stephen hemminger0719e722017-01-11 09:16:32 -0800770 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700771 return NVSP_STAT_FAIL;
772 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700773
stephen hemminger0c195562017-08-01 19:58:53 -0700774 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700775
776 /*
777 * Even if injecting the packet, record the statistics
778 * on the synthetic device because modifying the VF device
779 * statistics will not work correctly.
780 */
stephen hemminger742fe542017-02-27 10:26:50 -0800781 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700782 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700783 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800784 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700785
786 if (skb->pkt_type == PACKET_BROADCAST)
787 ++rx_stats->broadcast;
788 else if (skb->pkt_type == PACKET_MULTICAST)
789 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700790 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800791
stephen hemminger742fe542017-02-27 10:26:50 -0800792 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800793 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700794
Hank Janssenfceaf242009-07-13 15:34:54 -0700795 return 0;
796}
797
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700798static void netvsc_get_drvinfo(struct net_device *net,
799 struct ethtool_drvinfo *info)
800{
Jiri Pirko7826d432013-01-06 00:44:26 +0000801 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000802 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700803}
804
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800805static void netvsc_get_channels(struct net_device *net,
806 struct ethtool_channels *channel)
807{
808 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700809 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800810
811 if (nvdev) {
812 channel->max_combined = nvdev->max_chn;
813 channel->combined_count = nvdev->num_chn;
814 }
815}
816
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700817static int netvsc_set_channels(struct net_device *net,
818 struct ethtool_channels *channels)
819{
820 struct net_device_context *net_device_ctx = netdev_priv(net);
821 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700822 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700823 unsigned int orig, count = channels->combined_count;
824 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700825 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700826 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800827
828 /* We do not support separate count for rx, tx, or other */
829 if (count == 0 ||
830 channels->rx_count || channels->tx_count || channels->other_count)
831 return -EINVAL;
832
stephen hemmingera0be4502017-03-22 14:51:01 -0700833 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700834 return -ENODEV;
835
stephen hemminger2b018882017-01-24 13:06:03 -0800836 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700837 return -EINVAL;
838
stephen hemminger2b018882017-01-24 13:06:03 -0800839 if (count > nvdev->max_chn)
840 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700841
stephen hemminger7ca45932017-07-24 10:57:28 -0700842 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700843 was_opened = rndis_filter_opened(nvdev);
844 if (was_opened)
845 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700846
stephen hemminger7ca45932017-07-24 10:57:28 -0700847 memset(&device_info, 0, sizeof(device_info));
848 device_info.num_chn = count;
stephen hemminger8b532792017-08-09 17:46:11 -0700849 device_info.send_sections = nvdev->send_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700850 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700851 device_info.recv_sections = nvdev->recv_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700852 device_info.recv_section_size = nvdev->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700853
854 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700855
stephen hemminger7ca45932017-07-24 10:57:28 -0700856 nvdev = rndis_filter_device_add(dev, &device_info);
Stephen Hemminger8195b132017-09-06 13:53:05 -0700857 if (IS_ERR(nvdev)) {
stephen hemmingerd6aac1f2017-07-28 08:59:41 -0700858 ret = PTR_ERR(nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700859 device_info.num_chn = orig;
stephen hemminger68d715f2017-08-09 17:46:06 -0700860 nvdev = rndis_filter_device_add(dev, &device_info);
861
862 if (IS_ERR(nvdev)) {
863 netdev_err(net, "restoring channel setting failed: %ld\n",
864 PTR_ERR(nvdev));
865 return ret;
866 }
stephen hemminger7ca45932017-07-24 10:57:28 -0700867 }
868
stephen hemmingerea383bf2017-07-19 11:53:15 -0700869 if (was_opened)
870 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700871
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200872 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700873 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200874 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700875
876 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700877}
878
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100879static bool
880netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800881{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100882 struct ethtool_link_ksettings diff1 = *cmd;
883 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800884
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100885 diff1.base.speed = 0;
886 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800887 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100888 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
889 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800890 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100891 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800892
893 return !memcmp(&diff1, &diff2, sizeof(diff1));
894}
895
896static void netvsc_init_settings(struct net_device *dev)
897{
898 struct net_device_context *ndc = netdev_priv(dev);
899
Haiyang Zhang486e3982017-10-06 08:33:57 -0700900 ndc->l4_hash = HV_DEFAULT_L4HASH;
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700901
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800902 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700903 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800904}
905
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100906static int netvsc_get_link_ksettings(struct net_device *dev,
907 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800908{
909 struct net_device_context *ndc = netdev_priv(dev);
910
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100911 cmd->base.speed = ndc->speed;
912 cmd->base.duplex = ndc->duplex;
913 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800914
915 return 0;
916}
917
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100918static int netvsc_set_link_ksettings(struct net_device *dev,
919 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800920{
921 struct net_device_context *ndc = netdev_priv(dev);
922 u32 speed;
923
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100924 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800925 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100926 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800927 !netvsc_validate_ethtool_ss_cmd(cmd))
928 return -EINVAL;
929
930 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100931 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800932
933 return 0;
934}
935
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800936static int netvsc_change_mtu(struct net_device *ndev, int mtu)
937{
938 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -0700939 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700940 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200941 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700942 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800943 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700944 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700945 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800946
stephen hemmingera0be4502017-03-22 14:51:01 -0700947 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800948 return -ENODEV;
949
stephen hemminger0c195562017-08-01 19:58:53 -0700950 /* Change MTU of underlying VF netdev first. */
951 if (vf_netdev) {
952 ret = dev_set_mtu(vf_netdev, mtu);
953 if (ret)
954 return ret;
955 }
956
stephen hemmingerea383bf2017-07-19 11:53:15 -0700957 netif_device_detach(ndev);
958 was_opened = rndis_filter_opened(nvdev);
959 if (was_opened)
960 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700961
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700962 memset(&device_info, 0, sizeof(device_info));
stephen hemminger2b018882017-01-24 13:06:03 -0800963 device_info.num_chn = nvdev->num_chn;
stephen hemminger8b532792017-08-09 17:46:11 -0700964 device_info.send_sections = nvdev->send_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700965 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700966 device_info.recv_sections = nvdev->recv_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700967 device_info.recv_section_size = nvdev->recv_section_size;
Dexuan Cui152669b2017-03-02 13:00:53 +0000968
Dexuan Cui152669b2017-03-02 13:00:53 +0000969 rndis_filter_device_remove(hdev, nvdev);
970
Dexuan Cui152669b2017-03-02 13:00:53 +0000971 ndev->mtu = mtu;
972
stephen hemminger9749fed2017-07-19 11:53:16 -0700973 nvdev = rndis_filter_device_add(hdev, &device_info);
974 if (IS_ERR(nvdev)) {
975 ret = PTR_ERR(nvdev);
976
977 /* Attempt rollback to original MTU */
978 ndev->mtu = orig_mtu;
stephen hemminger68d715f2017-08-09 17:46:06 -0700979 nvdev = rndis_filter_device_add(hdev, &device_info);
stephen hemminger0c195562017-08-01 19:58:53 -0700980
981 if (vf_netdev)
982 dev_set_mtu(vf_netdev, orig_mtu);
stephen hemminger68d715f2017-08-09 17:46:06 -0700983
984 if (IS_ERR(nvdev)) {
985 netdev_err(ndev, "restoring mtu failed: %ld\n",
986 PTR_ERR(nvdev));
987 return ret;
988 }
stephen hemminger9749fed2017-07-19 11:53:16 -0700989 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800990
stephen hemmingerea383bf2017-07-19 11:53:15 -0700991 if (was_opened)
992 rndis_filter_open(nvdev);
993
994 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700995
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200996 /* We may have missed link change notifications */
997 schedule_delayed_work(&ndevctx->dwork, 0);
998
stephen hemminger9749fed2017-07-19 11:53:16 -0700999 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001000}
1001
stephen hemminger0c195562017-08-01 19:58:53 -07001002static void netvsc_get_vf_stats(struct net_device *net,
1003 struct netvsc_vf_pcpu_stats *tot)
1004{
1005 struct net_device_context *ndev_ctx = netdev_priv(net);
1006 int i;
1007
1008 memset(tot, 0, sizeof(*tot));
1009
1010 for_each_possible_cpu(i) {
1011 const struct netvsc_vf_pcpu_stats *stats
1012 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1013 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1014 unsigned int start;
1015
1016 do {
1017 start = u64_stats_fetch_begin_irq(&stats->syncp);
1018 rx_packets = stats->rx_packets;
1019 tx_packets = stats->tx_packets;
1020 rx_bytes = stats->rx_bytes;
1021 tx_bytes = stats->tx_bytes;
1022 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1023
1024 tot->rx_packets += rx_packets;
1025 tot->tx_packets += tx_packets;
1026 tot->rx_bytes += rx_bytes;
1027 tot->tx_bytes += tx_bytes;
1028 tot->tx_dropped += stats->tx_dropped;
1029 }
1030}
1031
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001032static void netvsc_get_stats64(struct net_device *net,
1033 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001034{
1035 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -07001036 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001037 struct netvsc_vf_pcpu_stats vf_tot;
stephen hemminger89bb42b2017-08-09 17:46:08 -07001038 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001039
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001040 if (!nvdev)
1041 return;
1042
stephen hemminger0c195562017-08-01 19:58:53 -07001043 netdev_stats_to_stats64(t, &net->stats);
1044
1045 netvsc_get_vf_stats(net, &vf_tot);
1046 t->rx_packets += vf_tot.rx_packets;
1047 t->tx_packets += vf_tot.tx_packets;
1048 t->rx_bytes += vf_tot.rx_bytes;
1049 t->tx_bytes += vf_tot.tx_bytes;
1050 t->tx_dropped += vf_tot.tx_dropped;
1051
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001052 for (i = 0; i < nvdev->num_chn; i++) {
1053 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1054 const struct netvsc_stats *stats;
1055 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001056 unsigned int start;
1057
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001058 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001059 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001060 start = u64_stats_fetch_begin_irq(&stats->syncp);
1061 packets = stats->packets;
1062 bytes = stats->bytes;
1063 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001064
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001065 t->tx_bytes += bytes;
1066 t->tx_packets += packets;
1067
1068 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001069 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001070 start = u64_stats_fetch_begin_irq(&stats->syncp);
1071 packets = stats->packets;
1072 bytes = stats->bytes;
1073 multicast = stats->multicast + stats->broadcast;
1074 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001075
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001076 t->rx_bytes += bytes;
1077 t->rx_packets += packets;
1078 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001079 }
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001080}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001081
1082static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1083{
stephen hemminger867047c2017-07-28 08:59:42 -07001084 struct net_device_context *ndc = netdev_priv(ndev);
stephen hemminger16ba3262017-08-09 17:46:05 -07001085 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
stephen hemminger867047c2017-07-28 08:59:42 -07001086 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001087 struct sockaddr *addr = p;
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001088 int err;
1089
stephen hemminger16ba3262017-08-09 17:46:05 -07001090 err = eth_prepare_mac_addr_change(ndev, p);
1091 if (err)
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001092 return err;
1093
stephen hemminger867047c2017-07-28 08:59:42 -07001094 if (!nvdev)
1095 return -ENODEV;
1096
stephen hemminger16ba3262017-08-09 17:46:05 -07001097 if (vf_netdev) {
1098 err = dev_set_mac_address(vf_netdev, addr);
1099 if (err)
1100 return err;
1101 }
1102
stephen hemminger867047c2017-07-28 08:59:42 -07001103 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
stephen hemminger16ba3262017-08-09 17:46:05 -07001104 if (!err) {
1105 eth_commit_mac_addr_change(ndev, p);
1106 } else if (vf_netdev) {
1107 /* rollback change on VF */
1108 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1109 dev_set_mac_address(vf_netdev, addr);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001110 }
1111
1112 return err;
1113}
1114
Stephen Hemminger4323b472016-08-23 12:17:57 -07001115static const struct {
1116 char name[ETH_GSTRING_LEN];
1117 u16 offset;
1118} netvsc_stats[] = {
1119 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -08001120 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001121 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1122 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1123 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
stephen hemmingercad5c192017-08-09 17:46:12 -07001124 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1125 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -08001126 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
Simon Xiao09af87d2017-09-29 11:39:46 -07001127 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1128 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
stephen hemminger0c195562017-08-01 19:58:53 -07001129}, vf_stats[] = {
1130 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1131 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1132 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1133 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1134 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001135};
1136
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001137#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
stephen hemminger0c195562017-08-01 19:58:53 -07001138#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001139
1140/* 4 statistics per queue (rx/tx packets/bytes) */
1141#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1142
Stephen Hemminger4323b472016-08-23 12:17:57 -07001143static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1144{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001145 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -07001146 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001147
1148 if (!nvdev)
1149 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001150
Stephen Hemminger4323b472016-08-23 12:17:57 -07001151 switch (string_set) {
1152 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001153 return NETVSC_GLOBAL_STATS_LEN
1154 + NETVSC_VF_STATS_LEN
1155 + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001156 default:
1157 return -EINVAL;
1158 }
1159}
1160
1161static void netvsc_get_ethtool_stats(struct net_device *dev,
1162 struct ethtool_stats *stats, u64 *data)
1163{
1164 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001165 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001166 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001167 const struct netvsc_stats *qstats;
stephen hemminger0c195562017-08-01 19:58:53 -07001168 struct netvsc_vf_pcpu_stats sum;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001169 unsigned int start;
1170 u64 packets, bytes;
1171 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001172
stephen hemminger545a8e72017-03-22 14:51:00 -07001173 if (!nvdev)
1174 return;
1175
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001176 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001177 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001178
stephen hemminger0c195562017-08-01 19:58:53 -07001179 netvsc_get_vf_stats(dev, &sum);
1180 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1181 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1182
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001183 for (j = 0; j < nvdev->num_chn; j++) {
1184 qstats = &nvdev->chan_table[j].tx_stats;
1185
1186 do {
1187 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1188 packets = qstats->packets;
1189 bytes = qstats->bytes;
1190 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1191 data[i++] = packets;
1192 data[i++] = bytes;
1193
1194 qstats = &nvdev->chan_table[j].rx_stats;
1195 do {
1196 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1197 packets = qstats->packets;
1198 bytes = qstats->bytes;
1199 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1200 data[i++] = packets;
1201 data[i++] = bytes;
1202 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001203}
1204
1205static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1206{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001207 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001208 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001209 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001210 int i;
1211
stephen hemminger545a8e72017-03-22 14:51:00 -07001212 if (!nvdev)
1213 return;
1214
Stephen Hemminger4323b472016-08-23 12:17:57 -07001215 switch (stringset) {
1216 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001217 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1218 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1219 p += ETH_GSTRING_LEN;
1220 }
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001221
stephen hemminger0c195562017-08-01 19:58:53 -07001222 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1223 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1224 p += ETH_GSTRING_LEN;
1225 }
1226
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001227 for (i = 0; i < nvdev->num_chn; i++) {
1228 sprintf(p, "tx_queue_%u_packets", i);
1229 p += ETH_GSTRING_LEN;
1230 sprintf(p, "tx_queue_%u_bytes", i);
1231 p += ETH_GSTRING_LEN;
1232 sprintf(p, "rx_queue_%u_packets", i);
1233 p += ETH_GSTRING_LEN;
1234 sprintf(p, "rx_queue_%u_bytes", i);
1235 p += ETH_GSTRING_LEN;
1236 }
1237
Stephen Hemminger4323b472016-08-23 12:17:57 -07001238 break;
1239 }
1240}
1241
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001242static int
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001243netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1244 struct ethtool_rxnfc *info)
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001245{
Haiyang Zhang486e3982017-10-06 08:33:57 -07001246 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1247
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001248 info->data = RXH_IP_SRC | RXH_IP_DST;
1249
1250 switch (info->flow_type) {
1251 case TCP_V4_FLOW:
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001252 if (ndc->l4_hash & HV_TCP4_L4HASH)
1253 info->data |= l4_flag;
1254
1255 break;
1256
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001257 case TCP_V6_FLOW:
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001258 if (ndc->l4_hash & HV_TCP6_L4HASH)
1259 info->data |= l4_flag;
1260
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001261 break;
1262
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001263 case UDP_V4_FLOW:
Haiyang Zhang486e3982017-10-06 08:33:57 -07001264 if (ndc->l4_hash & HV_UDP4_L4HASH)
1265 info->data |= l4_flag;
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001266
1267 break;
1268
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001269 case UDP_V6_FLOW:
Haiyang Zhang486e3982017-10-06 08:33:57 -07001270 if (ndc->l4_hash & HV_UDP6_L4HASH)
1271 info->data |= l4_flag;
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001272
1273 break;
1274
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001275 case IPV4_FLOW:
1276 case IPV6_FLOW:
1277 break;
1278 default:
1279 info->data = 0;
1280 break;
1281 }
1282
1283 return 0;
1284}
1285
1286static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001287netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1288 u32 *rules)
1289{
1290 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001291 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001292
1293 if (!nvdev)
1294 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001295
1296 switch (info->cmd) {
1297 case ETHTOOL_GRXRINGS:
1298 info->data = nvdev->num_chn;
1299 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001300
1301 case ETHTOOL_GRXFH:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001302 return netvsc_get_rss_hash_opts(ndc, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001303 }
1304 return -EOPNOTSUPP;
1305}
1306
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001307static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1308 struct ethtool_rxnfc *info)
1309{
1310 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1311 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
Haiyang Zhang486e3982017-10-06 08:33:57 -07001312 switch (info->flow_type) {
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001313 case TCP_V4_FLOW:
1314 ndc->l4_hash |= HV_TCP4_L4HASH;
1315 break;
1316
1317 case TCP_V6_FLOW:
1318 ndc->l4_hash |= HV_TCP6_L4HASH;
1319 break;
1320
Haiyang Zhang486e3982017-10-06 08:33:57 -07001321 case UDP_V4_FLOW:
1322 ndc->l4_hash |= HV_UDP4_L4HASH;
1323 break;
1324
1325 case UDP_V6_FLOW:
1326 ndc->l4_hash |= HV_UDP6_L4HASH;
1327 break;
1328
1329 default:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001330 return -EOPNOTSUPP;
Haiyang Zhang486e3982017-10-06 08:33:57 -07001331 }
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001332
1333 return 0;
1334 }
1335
1336 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
Haiyang Zhang486e3982017-10-06 08:33:57 -07001337 switch (info->flow_type) {
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001338 case TCP_V4_FLOW:
1339 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1340 break;
1341
1342 case TCP_V6_FLOW:
1343 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1344 break;
1345
Haiyang Zhang486e3982017-10-06 08:33:57 -07001346 case UDP_V4_FLOW:
1347 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1348 break;
1349
1350 case UDP_V6_FLOW:
1351 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1352 break;
1353
1354 default:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001355 return -EOPNOTSUPP;
Haiyang Zhang486e3982017-10-06 08:33:57 -07001356 }
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001357
1358 return 0;
1359 }
1360
1361 return -EOPNOTSUPP;
1362}
1363
1364static int
1365netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1366{
1367 struct net_device_context *ndc = netdev_priv(ndev);
1368
1369 if (info->cmd == ETHTOOL_SRXFH)
1370 return netvsc_set_rss_hash_opts(ndc, info);
1371
1372 return -EOPNOTSUPP;
1373}
1374
Richard Weinberger316158f2014-07-09 16:23:59 +02001375#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001376static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001377{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001378 struct net_device_context *ndc = netdev_priv(dev);
1379 struct netvsc_device *ndev;
1380 int i;
1381
1382 rcu_read_lock();
1383 ndev = rcu_dereference(ndc->nvdev);
1384 if (ndev) {
1385 for (i = 0; i < ndev->num_chn; i++) {
1386 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1387
1388 napi_schedule(&nvchan->napi);
1389 }
1390 }
1391 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001392}
1393#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001394
stephen hemminger962f3fe2017-01-24 13:06:02 -08001395static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1396{
1397 return NETVSC_HASH_KEYLEN;
1398}
1399
1400static u32 netvsc_rss_indir_size(struct net_device *dev)
1401{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001402 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001403}
1404
1405static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1406 u8 *hfunc)
1407{
1408 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001409 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001410 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001411 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001412
stephen hemminger545a8e72017-03-22 14:51:00 -07001413 if (!ndev)
1414 return -ENODEV;
1415
stephen hemminger962f3fe2017-01-24 13:06:02 -08001416 if (hfunc)
1417 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1418
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001419 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001420 if (indir) {
1421 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhang473713002017-10-13 12:28:03 -07001422 indir[i] = rndis_dev->rx_table[i];
stephen hemmingerff4a4412017-01-24 13:06:04 -08001423 }
1424
stephen hemminger962f3fe2017-01-24 13:06:02 -08001425 if (key)
1426 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1427
1428 return 0;
1429}
1430
1431static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1432 const u8 *key, const u8 hfunc)
1433{
1434 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001435 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001436 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001437 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001438
stephen hemminger545a8e72017-03-22 14:51:00 -07001439 if (!ndev)
1440 return -ENODEV;
1441
stephen hemminger962f3fe2017-01-24 13:06:02 -08001442 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1443 return -EOPNOTSUPP;
1444
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001445 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001446 if (indir) {
1447 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhangdb3cd7a2017-09-01 14:30:07 -07001448 if (indir[i] >= ndev->num_chn)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001449 return -EINVAL;
1450
1451 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhang473713002017-10-13 12:28:03 -07001452 rndis_dev->rx_table[i] = indir[i];
stephen hemmingerff4a4412017-01-24 13:06:04 -08001453 }
1454
1455 if (!key) {
1456 if (!indir)
1457 return 0;
1458
1459 key = rndis_dev->rss_key;
1460 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001461
Haiyang Zhang715e2ec2017-09-01 14:30:04 -07001462 return rndis_filter_set_rss_param(rndis_dev, key);
stephen hemminger962f3fe2017-01-24 13:06:02 -08001463}
1464
stephen hemminger8b532792017-08-09 17:46:11 -07001465/* Hyper-V RNDIS protocol does not have ring in the HW sense.
1466 * It does have pre-allocated receive area which is divided into sections.
1467 */
1468static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1469 struct ethtool_ringparam *ring)
1470{
1471 u32 max_buf_size;
1472
1473 ring->rx_pending = nvdev->recv_section_cnt;
1474 ring->tx_pending = nvdev->send_section_cnt;
1475
1476 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1477 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1478 else
1479 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1480
1481 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1482 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1483 / nvdev->send_section_size;
1484}
1485
1486static void netvsc_get_ringparam(struct net_device *ndev,
1487 struct ethtool_ringparam *ring)
1488{
1489 struct net_device_context *ndevctx = netdev_priv(ndev);
1490 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1491
1492 if (!nvdev)
1493 return;
1494
1495 __netvsc_get_ringparam(nvdev, ring);
1496}
1497
1498static int netvsc_set_ringparam(struct net_device *ndev,
1499 struct ethtool_ringparam *ring)
1500{
1501 struct net_device_context *ndevctx = netdev_priv(ndev);
1502 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1503 struct hv_device *hdev = ndevctx->device_ctx;
1504 struct netvsc_device_info device_info;
1505 struct ethtool_ringparam orig;
1506 u32 new_tx, new_rx;
1507 bool was_opened;
1508 int ret = 0;
1509
1510 if (!nvdev || nvdev->destroy)
1511 return -ENODEV;
1512
1513 memset(&orig, 0, sizeof(orig));
1514 __netvsc_get_ringparam(nvdev, &orig);
1515
1516 new_tx = clamp_t(u32, ring->tx_pending,
1517 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1518 new_rx = clamp_t(u32, ring->rx_pending,
1519 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1520
1521 if (new_tx == orig.tx_pending &&
1522 new_rx == orig.rx_pending)
1523 return 0; /* no change */
1524
1525 memset(&device_info, 0, sizeof(device_info));
1526 device_info.num_chn = nvdev->num_chn;
stephen hemminger8b532792017-08-09 17:46:11 -07001527 device_info.send_sections = new_tx;
Alex Ng0ab09be2017-09-20 11:17:35 -07001528 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -07001529 device_info.recv_sections = new_rx;
Alex Ng0ab09be2017-09-20 11:17:35 -07001530 device_info.recv_section_size = nvdev->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -07001531
1532 netif_device_detach(ndev);
1533 was_opened = rndis_filter_opened(nvdev);
1534 if (was_opened)
1535 rndis_filter_close(nvdev);
1536
1537 rndis_filter_device_remove(hdev, nvdev);
1538
1539 nvdev = rndis_filter_device_add(hdev, &device_info);
1540 if (IS_ERR(nvdev)) {
1541 ret = PTR_ERR(nvdev);
1542
1543 device_info.send_sections = orig.tx_pending;
1544 device_info.recv_sections = orig.rx_pending;
1545 nvdev = rndis_filter_device_add(hdev, &device_info);
1546 if (IS_ERR(nvdev)) {
1547 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1548 PTR_ERR(nvdev));
1549 return ret;
1550 }
1551 }
1552
1553 if (was_opened)
1554 rndis_filter_open(nvdev);
1555 netif_device_attach(ndev);
1556
1557 /* We may have missed link change notifications */
1558 ndevctx->last_reconfig = 0;
1559 schedule_delayed_work(&ndevctx->dwork, 0);
1560
1561 return ret;
1562}
1563
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001564static const struct ethtool_ops ethtool_ops = {
1565 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001566 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001567 .get_ethtool_stats = netvsc_get_ethtool_stats,
1568 .get_sset_count = netvsc_get_sset_count,
1569 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001570 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001571 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001572 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001573 .get_rxnfc = netvsc_get_rxnfc,
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001574 .set_rxnfc = netvsc_set_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001575 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1576 .get_rxfh_indir_size = netvsc_rss_indir_size,
1577 .get_rxfh = netvsc_get_rxfh,
1578 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001579 .get_link_ksettings = netvsc_get_link_ksettings,
1580 .set_link_ksettings = netvsc_set_link_ksettings,
stephen hemminger8b532792017-08-09 17:46:11 -07001581 .get_ringparam = netvsc_get_ringparam,
1582 .set_ringparam = netvsc_set_ringparam,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001583};
1584
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001585static const struct net_device_ops device_ops = {
1586 .ndo_open = netvsc_open,
1587 .ndo_stop = netvsc_close,
1588 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001589 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001590 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001591 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001592 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001593 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001594 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001595#ifdef CONFIG_NET_POLL_CONTROLLER
1596 .ndo_poll_controller = netvsc_poll_controller,
1597#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001598};
1599
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001600/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001601 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1602 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1603 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001604 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001605static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001606{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001607 struct net_device_context *ndev_ctx =
1608 container_of(w, struct net_device_context, dwork.work);
1609 struct hv_device *device_obj = ndev_ctx->device_ctx;
1610 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001611 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001612 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001613 struct netvsc_reconfig *event = NULL;
1614 bool notify = false, reschedule = false;
1615 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001616
stephen hemminger9b4e9462017-08-24 16:49:16 -07001617 /* if changes are happening, comeback later */
1618 if (!rtnl_trylock()) {
1619 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1620 return;
1621 }
1622
stephen hemmingera0be4502017-03-22 14:51:01 -07001623 net_device = rtnl_dereference(ndev_ctx->nvdev);
1624 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001625 goto out_unlock;
1626
Haiyang Zhang891de742014-02-12 16:54:27 -08001627 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001628
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001629 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1630 if (time_is_after_jiffies(next_reconfig)) {
1631 /* link_watch only sends one notification with current state
1632 * per second, avoid doing reconfig more frequently. Handle
1633 * wrap around.
1634 */
1635 delay = next_reconfig - jiffies;
1636 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1637 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001638 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001639 }
1640 ndev_ctx->last_reconfig = jiffies;
1641
1642 spin_lock_irqsave(&ndev_ctx->lock, flags);
1643 if (!list_empty(&ndev_ctx->reconfig_events)) {
1644 event = list_first_entry(&ndev_ctx->reconfig_events,
1645 struct netvsc_reconfig, list);
1646 list_del(&event->list);
1647 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1648 }
1649 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1650
1651 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001652 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001653
1654 switch (event->event) {
1655 /* Only the following events are possible due to the check in
1656 * netvsc_linkstatus_callback()
1657 */
1658 case RNDIS_STATUS_MEDIA_CONNECT:
1659 if (rdev->link_state) {
1660 rdev->link_state = false;
stephen hemminger0c195562017-08-01 19:58:53 -07001661 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001662 netif_tx_wake_all_queues(net);
1663 } else {
1664 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001665 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001666 kfree(event);
1667 break;
1668 case RNDIS_STATUS_MEDIA_DISCONNECT:
1669 if (!rdev->link_state) {
1670 rdev->link_state = true;
1671 netif_carrier_off(net);
1672 netif_tx_stop_all_queues(net);
1673 }
1674 kfree(event);
1675 break;
1676 case RNDIS_STATUS_NETWORK_CHANGE:
1677 /* Only makes sense if carrier is present */
1678 if (!rdev->link_state) {
1679 rdev->link_state = true;
1680 netif_carrier_off(net);
1681 netif_tx_stop_all_queues(net);
1682 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1683 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001684 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001685 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1686 reschedule = true;
1687 }
1688 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001689 }
1690
1691 rtnl_unlock();
1692
1693 if (notify)
1694 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001695
1696 /* link_watch only sends one notification with current state per
1697 * second, handle next reconfig event in 2 seconds.
1698 */
1699 if (reschedule)
1700 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001701
1702 return;
1703
1704out_unlock:
1705 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001706}
1707
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001708static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001709{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001710 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001711
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001712 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001713
1714 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001715 if (dev->netdev_ops != &device_ops)
1716 continue; /* not a netvsc device */
1717
1718 if (ether_addr_equal(mac, dev->perm_addr))
1719 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001720 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001721
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001722 return NULL;
1723}
1724
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001725static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001726{
1727 struct net_device *dev;
1728
1729 ASSERT_RTNL();
1730
1731 for_each_netdev(&init_net, dev) {
1732 struct net_device_context *net_device_ctx;
1733
1734 if (dev->netdev_ops != &device_ops)
1735 continue; /* not a netvsc device */
1736
1737 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001738 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001739 continue; /* device is removed */
1740
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001741 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001742 return dev; /* a match */
1743 }
1744
1745 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001746}
1747
stephen hemminger0c195562017-08-01 19:58:53 -07001748/* Called when VF is injecting data into network stack.
1749 * Change the associated network device from VF to netvsc.
1750 * note: already called with rcu_read_lock
1751 */
1752static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1753{
1754 struct sk_buff *skb = *pskb;
1755 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1756 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1757 struct netvsc_vf_pcpu_stats *pcpu_stats
1758 = this_cpu_ptr(ndev_ctx->vf_stats);
1759
1760 skb->dev = ndev;
1761
1762 u64_stats_update_begin(&pcpu_stats->syncp);
1763 pcpu_stats->rx_packets++;
1764 pcpu_stats->rx_bytes += skb->len;
1765 u64_stats_update_end(&pcpu_stats->syncp);
1766
1767 return RX_HANDLER_ANOTHER;
1768}
1769
1770static int netvsc_vf_join(struct net_device *vf_netdev,
1771 struct net_device *ndev)
1772{
1773 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1774 int ret;
1775
1776 ret = netdev_rx_handler_register(vf_netdev,
1777 netvsc_vf_handle_frame, ndev);
1778 if (ret != 0) {
1779 netdev_err(vf_netdev,
1780 "can not register netvsc VF receive handler (err = %d)\n",
1781 ret);
1782 goto rx_handler_failed;
1783 }
1784
David Ahern42ab19e2017-10-04 17:48:47 -07001785 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
stephen hemminger0c195562017-08-01 19:58:53 -07001786 if (ret != 0) {
1787 netdev_err(vf_netdev,
1788 "can not set master device %s (err = %d)\n",
1789 ndev->name, ret);
1790 goto upper_link_failed;
1791 }
1792
1793 /* set slave flag before open to prevent IPv6 addrconf */
1794 vf_netdev->flags |= IFF_SLAVE;
1795
stephen hemminger6123c662017-08-09 17:46:03 -07001796 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1797
1798 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001799
1800 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1801 return 0;
1802
1803upper_link_failed:
1804 netdev_rx_handler_unregister(vf_netdev);
1805rx_handler_failed:
1806 return ret;
1807}
1808
1809static void __netvsc_vf_setup(struct net_device *ndev,
1810 struct net_device *vf_netdev)
1811{
1812 int ret;
1813
stephen hemminger0c195562017-08-01 19:58:53 -07001814 /* Align MTU of VF with master */
1815 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1816 if (ret)
1817 netdev_warn(vf_netdev,
1818 "unable to change mtu to %u\n", ndev->mtu);
1819
1820 if (netif_running(ndev)) {
1821 ret = dev_open(vf_netdev);
1822 if (ret)
1823 netdev_warn(vf_netdev,
1824 "unable to open: %d\n", ret);
1825 }
1826}
1827
1828/* Setup VF as slave of the synthetic device.
1829 * Runs in workqueue to avoid recursion in netlink callbacks.
1830 */
1831static void netvsc_vf_setup(struct work_struct *w)
1832{
1833 struct net_device_context *ndev_ctx
stephen hemminger6123c662017-08-09 17:46:03 -07001834 = container_of(w, struct net_device_context, vf_takeover.work);
stephen hemminger0c195562017-08-01 19:58:53 -07001835 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1836 struct net_device *vf_netdev;
1837
stephen hemmingerfb84af82017-08-04 12:14:00 -07001838 if (!rtnl_trylock()) {
stephen hemminger6123c662017-08-09 17:46:03 -07001839 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
stephen hemmingerfb84af82017-08-04 12:14:00 -07001840 return;
1841 }
1842
stephen hemminger0c195562017-08-01 19:58:53 -07001843 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1844 if (vf_netdev)
1845 __netvsc_vf_setup(ndev, vf_netdev);
1846
1847 rtnl_unlock();
1848}
1849
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001850static int netvsc_register_vf(struct net_device *vf_netdev)
1851{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001852 struct net_device *ndev;
1853 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001854 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001855
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001856 if (vf_netdev->addr_len != ETH_ALEN)
1857 return NOTIFY_DONE;
1858
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001859 /*
1860 * We will use the MAC address to locate the synthetic interface to
1861 * associate with the VF interface. If we don't find a matching
1862 * synthetic interface, move on.
1863 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001864 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001865 if (!ndev)
1866 return NOTIFY_DONE;
1867
1868 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001869 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001870 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001871 return NOTIFY_DONE;
1872
stephen hemminger0c195562017-08-01 19:58:53 -07001873 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1874 return NOTIFY_DONE;
1875
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001876 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
stephen hemminger0c195562017-08-01 19:58:53 -07001877
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001878 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001879 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001880 return NOTIFY_OK;
1881}
1882
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001883/* VF up/down change detected, schedule to change data path */
1884static int netvsc_vf_changed(struct net_device *vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001885{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001886 struct net_device_context *net_device_ctx;
stephen hemminger7b83f522017-08-07 11:30:00 -07001887 struct netvsc_device *netvsc_dev;
stephen hemminger0c195562017-08-01 19:58:53 -07001888 struct net_device *ndev;
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001889 bool vf_is_up = netif_running(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001890
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001891 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001892 if (!ndev)
1893 return NOTIFY_DONE;
1894
1895 net_device_ctx = netdev_priv(ndev);
stephen hemminger7b83f522017-08-07 11:30:00 -07001896 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1897 if (!netvsc_dev)
1898 return NOTIFY_DONE;
1899
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001900 netvsc_switch_datapath(ndev, vf_is_up);
1901 netdev_info(ndev, "Data path switched %s VF: %s\n",
1902 vf_is_up ? "to" : "from", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001903
1904 return NOTIFY_OK;
1905}
1906
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001907static int netvsc_unregister_vf(struct net_device *vf_netdev)
1908{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001909 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001910 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001911
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001912 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001913 if (!ndev)
1914 return NOTIFY_DONE;
1915
1916 net_device_ctx = netdev_priv(ndev);
stephen hemminger6123c662017-08-09 17:46:03 -07001917 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001918
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001919 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001920
Stephen Hemmingerec158f72017-08-31 16:16:13 -07001921 netdev_rx_handler_unregister(vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001922 netdev_upper_dev_unlink(vf_netdev, ndev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001923 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001924 dev_put(vf_netdev);
Stephen Hemmingerec158f72017-08-31 16:16:13 -07001925
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001926 return NOTIFY_OK;
1927}
1928
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001929static int netvsc_probe(struct hv_device *dev,
1930 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001931{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001932 struct net_device *net = NULL;
1933 struct net_device_context *net_device_ctx;
1934 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001935 struct netvsc_device *nvdev;
stephen hemminger0c195562017-08-01 19:58:53 -07001936 int ret = -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001937
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001938 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001939 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001940 if (!net)
stephen hemminger0c195562017-08-01 19:58:53 -07001941 goto no_net;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001942
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001943 netif_carrier_off(net);
1944
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001945 netvsc_init_settings(net);
1946
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001947 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001948 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001949 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1950 if (netif_msg_probe(net_device_ctx))
1951 netdev_dbg(net, "netvsc msg_enable: %d\n",
1952 net_device_ctx->msg_enable);
1953
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001954 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001955
Haiyang Zhang891de742014-02-12 16:54:27 -08001956 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001957
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001958 spin_lock_init(&net_device_ctx->lock);
1959 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
stephen hemminger6123c662017-08-09 17:46:03 -07001960 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
stephen hemminger0c195562017-08-01 19:58:53 -07001961
1962 net_device_ctx->vf_stats
1963 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1964 if (!net_device_ctx->vf_stats)
1965 goto no_stats;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001966
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001967 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001968 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001969 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001970
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001971 /* We always need headroom for rndis header */
1972 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1973
Haiyang Zhang6450f8f2017-09-22 15:31:38 -07001974 /* Initialize the number of queues to be 1, we may change it if more
1975 * channels are offered later.
1976 */
1977 netif_set_real_num_tx_queues(net, 1);
1978 netif_set_real_num_rx_queues(net, 1);
1979
Haiyang Zhang692e0842011-09-01 12:19:43 -07001980 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001981 memset(&device_info, 0, sizeof(device_info));
stephen hemminger3071ada2017-03-22 14:50:59 -07001982 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger8b532792017-08-09 17:46:11 -07001983 device_info.send_sections = NETVSC_DEFAULT_TX;
Alex Ng0ab09be2017-09-20 11:17:35 -07001984 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
stephen hemminger8b532792017-08-09 17:46:11 -07001985 device_info.recv_sections = NETVSC_DEFAULT_RX;
Alex Ng0ab09be2017-09-20 11:17:35 -07001986 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
stephen hemminger9749fed2017-07-19 11:53:16 -07001987
1988 nvdev = rndis_filter_device_add(dev, &device_info);
1989 if (IS_ERR(nvdev)) {
1990 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001991 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
stephen hemminger0c195562017-08-01 19:58:53 -07001992 goto rndis_failed;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001993 }
stephen hemminger0c195562017-08-01 19:58:53 -07001994
Haiyang Zhang692e0842011-09-01 12:19:43 -07001995 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1996
Vitaly Kuznetsovaefd80e2017-11-15 15:12:55 +01001997 /* hw_features computed in rndis_netdev_set_hwcaps() */
stephen hemminger23312a32017-01-24 13:05:59 -08001998 net->features = net->hw_features |
1999 NETIF_F_HIGHDMA | NETIF_F_SG |
2000 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2001 net->vlan_features = net->features;
2002
stephen hemminger9749fed2017-07-19 11:53:16 -07002003 netdev_lockdep_set_classes(net);
2004
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002005 /* MTU range: 68 - 1500 or 65521 */
2006 net->min_mtu = NETVSC_MTU_MIN;
2007 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2008 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2009 else
2010 net->max_mtu = ETH_DATA_LEN;
2011
Haiyang Zhanga68f9612013-12-20 16:52:31 -08002012 ret = register_netdev(net);
2013 if (ret != 0) {
2014 pr_err("Unable to register netdev.\n");
stephen hemminger0c195562017-08-01 19:58:53 -07002015 goto register_failed;
Haiyang Zhanga68f9612013-12-20 16:52:31 -08002016 }
2017
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002018 return ret;
stephen hemminger0c195562017-08-01 19:58:53 -07002019
2020register_failed:
2021 rndis_filter_device_remove(dev, nvdev);
2022rndis_failed:
2023 free_percpu(net_device_ctx->vf_stats);
2024no_stats:
2025 hv_set_drvdata(dev, NULL);
2026 free_netdev(net);
2027no_net:
2028 return ret;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002029}
2030
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002031static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002032{
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002033 struct net_device_context *ndev_ctx;
Stephen Hemmingerec158f72017-08-31 16:16:13 -07002034 struct net_device *vf_netdev;
2035 struct net_device *net;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07002036
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002037 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002038 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002039 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002040 return 0;
2041 }
2042
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002043 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002044
stephen hemmingera0be4502017-03-22 14:51:01 -07002045 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02002046
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002047 cancel_delayed_work_sync(&ndev_ctx->dwork);
2048
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002049 /*
2050 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07002051 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002052 */
stephen hemmingera0be4502017-03-22 14:51:01 -07002053 rtnl_lock();
Stephen Hemmingerec158f72017-08-31 16:16:13 -07002054 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2055 if (vf_netdev)
2056 netvsc_unregister_vf(vf_netdev);
2057
Stephen Hemminger8195b132017-09-06 13:53:05 -07002058 unregister_netdevice(net);
2059
stephen hemminger79e8cbe2017-07-19 11:53:13 -07002060 rndis_filter_device_remove(dev,
2061 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07002062 rtnl_unlock();
2063
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002064 hv_set_drvdata(dev, NULL);
2065
stephen hemminger0c195562017-08-01 19:58:53 -07002066 free_percpu(ndev_ctx->vf_stats);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08002067 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07002068 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002069}
2070
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002071static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002072 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08002073 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002074 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002075};
2076
2077MODULE_DEVICE_TABLE(vmbus, id_table);
2078
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002079/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002080static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00002081 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002082 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002083 .probe = netvsc_probe,
2084 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07002085};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002086
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002087/*
2088 * On Hyper-V, every VF interface is matched with a corresponding
2089 * synthetic interface. The synthetic interface is presented first
2090 * to the guest. When the corresponding VF instance is registered,
2091 * we will take care of switching the data path.
2092 */
2093static int netvsc_netdev_event(struct notifier_block *this,
2094 unsigned long event, void *ptr)
2095{
2096 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2097
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002098 /* Skip our own events */
2099 if (event_dev->netdev_ops == &device_ops)
2100 return NOTIFY_DONE;
2101
2102 /* Avoid non-Ethernet type devices */
2103 if (event_dev->type != ARPHRD_ETHER)
2104 return NOTIFY_DONE;
2105
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002106 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06002107 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002108 return NOTIFY_DONE;
2109
2110 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002111 if ((event_dev->priv_flags & IFF_BONDING) &&
2112 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07002113 return NOTIFY_DONE;
2114
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002115 switch (event) {
2116 case NETDEV_REGISTER:
2117 return netvsc_register_vf(event_dev);
2118 case NETDEV_UNREGISTER:
2119 return netvsc_unregister_vf(event_dev);
2120 case NETDEV_UP:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002121 case NETDEV_DOWN:
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07002122 return netvsc_vf_changed(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002123 default:
2124 return NOTIFY_DONE;
2125 }
2126}
2127
2128static struct notifier_block netvsc_netdev_notifier = {
2129 .notifier_call = netvsc_netdev_event,
2130};
2131
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002132static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07002133{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002134 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002135 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07002136}
2137
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002138static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002139{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002140 int ret;
2141
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00002142 if (ring_size < RING_SIZE_MIN) {
2143 ring_size = RING_SIZE_MIN;
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002144 pr_info("Increased ring_size to %u (min allowed)\n",
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00002145 ring_size);
2146 }
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002147 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2148 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002149
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002150 ret = vmbus_driver_register(&netvsc_drv);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002151 if (ret)
2152 return ret;
2153
2154 register_netdevice_notifier(&netvsc_netdev_notifier);
2155 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002156}
2157
Hank Janssen26c14cc2010-02-11 23:02:42 +00002158MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07002159MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07002160
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002161module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002162module_exit(netvsc_drv_exit);