blob: 3219d2e8918f0b5028c0b548ab7eb73f4c8133c8 [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 hemminger27f5aa92017-07-24 10:57:29 -070038
Hank Janssenfceaf242009-07-13 15:34:54 -070039#include <net/arp.h>
40#include <net/route.h>
41#include <net/sock.h>
42#include <net/pkt_sched.h>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070043#include <net/checksum.h>
44#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070045
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070046#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070047
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000048#define RING_SIZE_MIN 64
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010049#define LINKCHANGE_INT (2 * HZ)
stephen hemminger6123c662017-08-09 17:46:03 -070050#define VF_TAKEOVER_INT (HZ / 10)
stephen hemmingera50af862016-12-06 13:43:54 -080051
Hank Janssen99c8da02010-10-12 10:45:23 -070052static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070053module_param(ring_size, int, S_IRUGO);
54MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070055
Simon Xiao3f300ff2015-04-28 01:05:17 -070056static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
57 NETIF_MSG_LINK | NETIF_MSG_IFUP |
58 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
59 NETIF_MSG_TX_ERR;
60
61static int debug = -1;
62module_param(debug, int, S_IRUGO);
63MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
64
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070065static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070066{
Wenqi Ma792df872012-04-19 00:39:37 +000067 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070068 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080069
stephen hemminger4f19c0d2017-06-07 15:53:49 -070070 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070071}
72
Hank Janssenfceaf242009-07-13 15:34:54 -070073static int netvsc_open(struct net_device *net)
74{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070075 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -070076 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070077 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080078 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070079 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070080
Haiyang Zhang891de742014-02-12 16:54:27 -080081 netif_carrier_off(net);
82
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070083 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020084 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070085 if (ret != 0) {
86 netdev_err(net, "unable to open device (ret %d).\n", ret);
87 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070088 }
89
Haiyang Zhang2de85302015-07-13 13:09:16 -070090 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070091
Haiyang Zhang891de742014-02-12 16:54:27 -080092 rdev = nvdev->extension;
stephen hemminger0c195562017-08-01 19:58:53 -070093
94 if (!rdev->link_state)
Haiyang Zhang891de742014-02-12 16:54:27 -080095 netif_carrier_on(net);
96
stephen hemminger0c195562017-08-01 19:58:53 -070097 if (vf_netdev) {
98 /* Setting synthetic device up transparently sets
99 * slave as up. If open fails, then slave will be
100 * still be offline (and not used).
101 */
102 ret = dev_open(vf_netdev);
103 if (ret)
104 netdev_warn(net,
105 "unable to open slave: %s: %d\n",
106 vf_netdev->name, ret);
107 }
108 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700109}
110
Hank Janssenfceaf242009-07-13 15:34:54 -0700111static int netvsc_close(struct net_device *net)
112{
Hank Janssenfceaf242009-07-13 15:34:54 -0700113 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700114 struct net_device *vf_netdev
115 = rtnl_dereference(net_device_ctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700116 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700117 int ret;
stephen hemminger40975962017-06-08 16:21:19 -0700118 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700119 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700120
Haiyang Zhang0a282532012-02-02 07:17:59 +0000121 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700122
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200123 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700124 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700125 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700126 return ret;
127 }
128
129 /* Ensure pending bytes in ring are read */
130 while (true) {
131 aread = 0;
132 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800133 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700134 if (!chn)
135 continue;
136
stephen hemminger40975962017-06-08 16:21:19 -0700137 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700138 if (aread)
139 break;
140
stephen hemminger40975962017-06-08 16:21:19 -0700141 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700142 if (aread)
143 break;
144 }
145
146 retry++;
147 if (retry > retry_max || aread == 0)
148 break;
149
150 msleep(msec);
151
152 if (msec < 1000)
153 msec *= 2;
154 }
155
156 if (aread) {
157 netdev_err(net, "Ring buffer not empty after closing rndis\n");
158 ret = -ETIMEDOUT;
159 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700160
stephen hemminger0c195562017-08-01 19:58:53 -0700161 if (vf_netdev)
162 dev_close(vf_netdev);
163
Hank Janssenfceaf242009-07-13 15:34:54 -0700164 return ret;
165}
166
KY Srinivasan8a002512014-03-08 19:23:14 -0800167static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700168 int pkt_type)
KY Srinivasan8a002512014-03-08 19:23:14 -0800169{
170 struct rndis_packet *rndis_pkt;
171 struct rndis_per_packet_info *ppi;
172
173 rndis_pkt = &msg->msg.pkt;
174 rndis_pkt->data_offset += ppi_size;
175
176 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
177 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
178
179 ppi->size = ppi_size;
180 ppi->type = pkt_type;
181 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
182
183 rndis_pkt->per_pkt_info_len += ppi_size;
184
185 return ppi;
186}
187
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700188/* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
189 * hash for non-TCP traffic with only IP numbers.
190 */
191static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
192{
193 struct flow_keys flow;
194 u32 hash;
195 static u32 hashrnd __read_mostly;
196
197 net_get_random_once(&hashrnd, sizeof(hashrnd));
198
199 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
200 return 0;
201
202 if (flow.basic.ip_proto == IPPROTO_TCP) {
203 return skb_get_hash(skb);
204 } else {
205 if (flow.basic.n_proto == htons(ETH_P_IP))
206 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
207 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
208 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
209 else
210 hash = 0;
211
212 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
213 }
214
215 return hash;
216}
217
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700218static inline int netvsc_get_tx_queue(struct net_device *ndev,
219 struct sk_buff *skb, int old_idx)
220{
221 const struct net_device_context *ndc = netdev_priv(ndev);
222 struct sock *sk = skb->sk;
223 int q_idx;
224
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700225 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700226 (VRSS_SEND_TAB_SIZE - 1)];
227
228 /* If queue index changed record the new value */
229 if (q_idx != old_idx &&
230 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
231 sk_tx_queue_set(sk, q_idx);
232
233 return q_idx;
234}
235
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800236/*
237 * Select queue for transmit.
238 *
239 * If a valid queue has already been assigned, then use that.
240 * Otherwise compute tx queue based on hash and the send table.
241 *
242 * This is basically similar to default (__netdev_pick_tx) with the added step
243 * of using the host send_table when no other queue has been assigned.
244 *
245 * TODO support XPS - but get_xps_queue not exported
246 */
stephen hemminger0c195562017-08-01 19:58:53 -0700247static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700248{
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700249 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700250
stephen hemminger0c195562017-08-01 19:58:53 -0700251 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700252 /* If forwarding a packet, we use the recorded queue when
253 * available for better cache locality.
254 */
255 if (skb_rx_queue_recorded(skb))
256 q_idx = skb_get_rx_queue(skb);
257 else
258 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800259 }
260
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700261 return q_idx;
262}
263
stephen hemminger0c195562017-08-01 19:58:53 -0700264static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
265 void *accel_priv,
266 select_queue_fallback_t fallback)
267{
268 struct net_device_context *ndc = netdev_priv(ndev);
269 struct net_device *vf_netdev;
270 u16 txq;
271
272 rcu_read_lock();
273 vf_netdev = rcu_dereference(ndc->vf_netdev);
274 if (vf_netdev) {
275 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
276 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
277 } else {
278 txq = netvsc_pick_tx(ndev, skb);
279 }
280 rcu_read_unlock();
281
282 while (unlikely(txq >= ndev->real_num_tx_queues))
283 txq -= ndev->real_num_tx_queues;
284
285 return txq;
286}
287
KY Srinivasan54a73572014-03-08 19:23:13 -0800288static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700289 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800290{
291 int j = 0;
292
293 /* Deal with compund pages by ignoring unused part
294 * of the page.
295 */
296 page += (offset >> PAGE_SHIFT);
297 offset &= ~PAGE_MASK;
298
299 while (len > 0) {
300 unsigned long bytes;
301
302 bytes = PAGE_SIZE - offset;
303 if (bytes > len)
304 bytes = len;
305 pb[j].pfn = page_to_pfn(page);
306 pb[j].offset = offset;
307 pb[j].len = bytes;
308
309 offset += bytes;
310 len -= bytes;
311
312 if (offset == PAGE_SIZE && len) {
313 page++;
314 offset = 0;
315 j++;
316 }
317 }
318
319 return j + 1;
320}
321
KY Srinivasan8a002512014-03-08 19:23:14 -0800322static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800323 struct hv_netvsc_packet *packet,
stephen hemminger02b6de02017-07-28 08:59:44 -0700324 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800325{
326 u32 slots_used = 0;
327 char *data = skb->data;
328 int frags = skb_shinfo(skb)->nr_frags;
329 int i;
330
331 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700332 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800333 * 2. skb linear data
334 * 3. skb fragment data
335 */
336 if (hdr != NULL)
337 slots_used += fill_pg_buf(virt_to_page(hdr),
338 offset_in_page(hdr),
339 len, &pb[slots_used]);
340
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700341 packet->rmsg_size = len;
342 packet->rmsg_pgcnt = slots_used;
343
KY Srinivasan54a73572014-03-08 19:23:13 -0800344 slots_used += fill_pg_buf(virt_to_page(data),
345 offset_in_page(data),
346 skb_headlen(skb), &pb[slots_used]);
347
348 for (i = 0; i < frags; i++) {
349 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
350
351 slots_used += fill_pg_buf(skb_frag_page(frag),
352 frag->page_offset,
353 skb_frag_size(frag), &pb[slots_used]);
354 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800355 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800356}
357
stephen hemminger80d887d2017-07-24 21:03:19 -0700358static int count_skb_frag_slots(struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800359{
stephen hemminger80d887d2017-07-24 21:03:19 -0700360 int i, frags = skb_shinfo(skb)->nr_frags;
361 int pages = 0;
362
363 for (i = 0; i < frags; i++) {
364 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
365 unsigned long size = skb_frag_size(frag);
366 unsigned long offset = frag->page_offset;
367
368 /* Skip unused frames from start of page */
369 offset &= ~PAGE_MASK;
370 pages += PFN_UP(offset + size);
371 }
372 return pages;
373}
374
375static int netvsc_get_slots(struct sk_buff *skb)
376{
377 char *data = skb->data;
378 unsigned int offset = offset_in_page(data);
379 unsigned int len = skb_headlen(skb);
380 int slots;
381 int frag_slots;
382
383 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
384 frag_slots = count_skb_frag_slots(skb);
385 return slots + frag_slots;
KY Srinivasan54a73572014-03-08 19:23:13 -0800386}
387
stephen hemminger23312a32017-01-24 13:05:59 -0800388static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800389{
stephen hemminger23312a32017-01-24 13:05:59 -0800390 if (skb->protocol == htons(ETH_P_IP)) {
391 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800392
stephen hemminger23312a32017-01-24 13:05:59 -0800393 if (ip->protocol == IPPROTO_TCP)
394 return TRANSPORT_INFO_IPV4_TCP;
395 else if (ip->protocol == IPPROTO_UDP)
396 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800397 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800398 struct ipv6hdr *ip6 = ipv6_hdr(skb);
399
400 if (ip6->nexthdr == IPPROTO_TCP)
401 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700402 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800403 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800404 }
405
stephen hemminger23312a32017-01-24 13:05:59 -0800406 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800407}
408
stephen hemminger0c195562017-08-01 19:58:53 -0700409/* Send skb on the slave VF device. */
410static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
411 struct sk_buff *skb)
412{
413 struct net_device_context *ndev_ctx = netdev_priv(net);
414 unsigned int len = skb->len;
415 int rc;
416
417 skb->dev = vf_netdev;
418 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
419
420 rc = dev_queue_xmit(skb);
421 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
422 struct netvsc_vf_pcpu_stats *pcpu_stats
423 = this_cpu_ptr(ndev_ctx->vf_stats);
424
425 u64_stats_update_begin(&pcpu_stats->syncp);
426 pcpu_stats->tx_packets++;
427 pcpu_stats->tx_bytes += len;
428 u64_stats_update_end(&pcpu_stats->syncp);
429 } else {
430 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
431 }
432
433 return rc;
434}
435
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700436static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700437{
Hank Janssenfceaf242009-07-13 15:34:54 -0700438 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200439 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700440 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800441 unsigned int num_data_pgs;
442 struct rndis_message *rndis_msg;
443 struct rndis_packet *rndis_pkt;
stephen hemminger0c195562017-08-01 19:58:53 -0700444 struct net_device *vf_netdev;
KY Srinivasan8a002512014-03-08 19:23:14 -0800445 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800446 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700447 u32 hash;
stephen hemminger02b6de02017-07-28 08:59:44 -0700448 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
Hank Janssenfceaf242009-07-13 15:34:54 -0700449
stephen hemminger0c195562017-08-01 19:58:53 -0700450 /* if VF is present and up then redirect packets
451 * already called with rcu_read_lock_bh
452 */
453 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
454 if (vf_netdev && netif_running(vf_netdev) &&
455 !netpoll_tx_running(net))
456 return netvsc_vf_xmit(net, vf_netdev, skb);
457
stephen hemminger80d887d2017-07-24 21:03:19 -0700458 /* We will atmost need two pages to describe the rndis
459 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200460 * of pages in a single packet. If skb is scattered around
461 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800462 */
stephen hemminger80d887d2017-07-24 21:03:19 -0700463
464 num_data_pgs = netvsc_get_slots(skb) + 2;
465
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700466 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700467 ++net_device_ctx->eth_stats.tx_scattered;
468
469 if (skb_linearize(skb))
470 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700471
stephen hemminger80d887d2017-07-24 21:03:19 -0700472 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700473 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700474 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700475 goto drop;
476 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800477 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700478
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800479 /*
480 * Place the rndis header in the skb head room and
481 * the skb->cb will be used for hv_netvsc_packet
482 * structure.
483 */
484 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700485 if (ret)
486 goto no_memory;
487
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800488 /* Use the skb control buffer for building up the packet */
489 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
490 FIELD_SIZEOF(struct sk_buff, cb));
491 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700492
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700493 packet->q_idx = skb_get_queue_mapping(skb);
494
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800495 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800496 packet->total_bytes = skb->len;
497 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700498
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800499 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700500
KY Srinivasan24476762015-12-01 16:43:06 -0800501 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700502
KY Srinivasan8a002512014-03-08 19:23:14 -0800503 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800504 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
505 rndis_msg->msg_len = packet->total_data_buflen;
506 rndis_pkt = &rndis_msg->msg.pkt;
507 rndis_pkt->data_offset = sizeof(struct rndis_packet);
508 rndis_pkt->data_len = packet->total_data_buflen;
509 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
510
511 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
512
Haiyang Zhang307f0992014-05-21 12:55:39 -0700513 hash = skb_get_hash_raw(skb);
514 if (hash != 0 && net->real_num_tx_queues > 1) {
515 rndis_msg_size += NDIS_HASH_PPI_SIZE;
516 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
517 NBL_HASH_VALUE);
518 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
519 }
520
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700521 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800522 struct ndis_pkt_8021q_info *vlan;
523
524 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
525 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
526 IEEE_8021Q_INFO);
527 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
528 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800529 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
530 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800531 VLAN_PRIO_SHIFT;
532 }
533
stephen hemminger23312a32017-01-24 13:05:59 -0800534 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700535 struct ndis_tcp_lso_info *lso_info;
536
537 rndis_msg_size += NDIS_LSO_PPI_SIZE;
538 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
539 TCP_LARGESEND_PKTINFO);
540
541 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
542 ppi->ppi_offset);
543
544 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800545 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700546 lso_info->lso_v2_transmit.ip_version =
547 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
548 ip_hdr(skb)->tot_len = 0;
549 ip_hdr(skb)->check = 0;
550 tcp_hdr(skb)->check =
551 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
552 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
553 } else {
554 lso_info->lso_v2_transmit.ip_version =
555 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
556 ipv6_hdr(skb)->payload_len = 0;
557 tcp_hdr(skb)->check =
558 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
559 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
560 }
stephen hemminger23312a32017-01-24 13:05:59 -0800561 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700562 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700563 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800564 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
565 struct ndis_tcp_ip_checksum_info *csum_info;
566
stephen hemmingerad19bc82016-10-11 14:03:07 -0700567 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
568 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
569 TCPIP_CHKSUM_PKTINFO);
570
571 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
572 ppi->ppi_offset);
573
stephen hemminger23312a32017-01-24 13:05:59 -0800574 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
575
576 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700577 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800578
579 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
580 csum_info->transmit.tcp_checksum = 1;
581 else
582 csum_info->transmit.udp_checksum = 1;
583 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700584 csum_info->transmit.is_ipv6 = 1;
585
stephen hemminger23312a32017-01-24 13:05:59 -0800586 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
587 csum_info->transmit.tcp_checksum = 1;
588 else
589 csum_info->transmit.udp_checksum = 1;
590 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700591 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800592 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700593 if (skb_checksum_help(skb))
594 goto drop;
595 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700596 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800597
KY Srinivasan8a002512014-03-08 19:23:14 -0800598 /* Start filling in the page buffers with the rndis hdr */
599 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700600 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800601 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
stephen hemminger02b6de02017-07-28 08:59:44 -0700602 skb, packet, pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800603
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800604 /* timestamp packet in software */
605 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700606
stephen hemminger02b6de02017-07-28 08:59:44 -0700607 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800608 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700609 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700610
611 if (ret == -EAGAIN) {
612 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700613 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700614 }
615
616 if (ret == -ENOSPC)
617 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700618
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700619drop:
620 dev_kfree_skb_any(skb);
621 net->stats.tx_dropped++;
622
623 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700624
625no_memory:
626 ++net_device_ctx->eth_stats.tx_no_memory;
627 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700628}
stephen hemminger89bb42b2017-08-09 17:46:08 -0700629
Hank Janssen3e189512010-03-04 22:11:00 +0000630/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700631 * netvsc_linkstatus_callback - Link up/down notification
632 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700633void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700634 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700635{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700636 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700637 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700638 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100639 struct netvsc_reconfig *event;
640 unsigned long flags;
641
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700642 net = hv_get_drvdata(device_obj);
643
644 if (!net)
645 return;
646
647 ndev_ctx = netdev_priv(net);
648
649 /* Update the physical link speed when changing to another vSwitch */
650 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
651 u32 speed;
652
stephen hemminger89bb42b2017-08-09 17:46:08 -0700653 speed = *(u32 *)((void *)indicate
654 + indicate->status_buf_offset) / 10000;
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700655 ndev_ctx->speed = speed;
656 return;
657 }
658
659 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100660 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
661 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
662 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
663 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700664
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700665 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700666 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700667
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100668 event = kzalloc(sizeof(*event), GFP_ATOMIC);
669 if (!event)
670 return;
671 event->event = indicate->status;
672
673 spin_lock_irqsave(&ndev_ctx->lock, flags);
674 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
675 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
676
677 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700678}
679
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700680static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800681 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800682 const struct ndis_tcp_ip_checksum_info *csum_info,
683 const struct ndis_pkt_8021q_info *vlan,
684 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700685{
Hank Janssenfceaf242009-07-13 15:34:54 -0700686 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700687
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800688 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700689 if (!skb)
690 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700691
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700692 /*
693 * Copy to skb. This copy is needed here since the memory pointed by
694 * hv_netvsc_packet cannot be deallocated
695 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200696 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700697
698 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700699
700 /* skb is already created with CHECKSUM_NONE */
701 skb_checksum_none_assert(skb);
702
703 /*
704 * In Linux, the IP checksum is always checked.
705 * Do L4 checksum offload if enabled and present.
706 */
707 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
708 if (csum_info->receive.tcp_checksum_succeeded ||
709 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800710 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800711 }
712
stephen hemmingerdc54a082017-01-24 13:06:08 -0800713 if (vlan) {
714 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
715
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700716 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800717 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800718 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700719
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700720 return skb;
721}
722
723/*
724 * netvsc_recv_callback - Callback when we receive a packet from the
725 * "wire" on the specified device.
726 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800727int netvsc_recv_callback(struct net_device *net,
728 struct vmbus_channel *channel,
729 void *data, u32 len,
730 const struct ndis_tcp_ip_checksum_info *csum_info,
731 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700732{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200733 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700734 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800735 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700736 struct netvsc_channel *nvchan;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700737 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700738 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700739
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700740 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700741 return NVSP_STAT_FAIL;
742
stephen hemminger0719e722017-01-11 09:16:32 -0800743 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700744 net_device = rcu_dereference(net_device_ctx->nvdev);
745 if (unlikely(!net_device))
746 goto drop;
747
748 nvchan = &net_device->chan_table[q_idx];
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700749
750 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800751 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
752 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700753 if (unlikely(!skb)) {
stephen hemminger545a8e72017-03-22 14:51:00 -0700754drop:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700755 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800756 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700757 return NVSP_STAT_FAIL;
758 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700759
stephen hemminger0c195562017-08-01 19:58:53 -0700760 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700761
762 /*
763 * Even if injecting the packet, record the statistics
764 * on the synthetic device because modifying the VF device
765 * statistics will not work correctly.
766 */
stephen hemminger742fe542017-02-27 10:26:50 -0800767 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700768 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700769 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800770 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700771
772 if (skb->pkt_type == PACKET_BROADCAST)
773 ++rx_stats->broadcast;
774 else if (skb->pkt_type == PACKET_MULTICAST)
775 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700776 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800777
stephen hemminger742fe542017-02-27 10:26:50 -0800778 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800779 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700780
Hank Janssenfceaf242009-07-13 15:34:54 -0700781 return 0;
782}
783
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700784static void netvsc_get_drvinfo(struct net_device *net,
785 struct ethtool_drvinfo *info)
786{
Jiri Pirko7826d432013-01-06 00:44:26 +0000787 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000788 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700789}
790
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800791static void netvsc_get_channels(struct net_device *net,
792 struct ethtool_channels *channel)
793{
794 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700795 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800796
797 if (nvdev) {
798 channel->max_combined = nvdev->max_chn;
799 channel->combined_count = nvdev->num_chn;
800 }
801}
802
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700803static int netvsc_set_channels(struct net_device *net,
804 struct ethtool_channels *channels)
805{
806 struct net_device_context *net_device_ctx = netdev_priv(net);
807 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700808 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700809 unsigned int orig, count = channels->combined_count;
810 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700811 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700812 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800813
814 /* We do not support separate count for rx, tx, or other */
815 if (count == 0 ||
816 channels->rx_count || channels->tx_count || channels->other_count)
817 return -EINVAL;
818
Arnd Bergmannb92b7d32017-06-22 00:16:37 +0200819 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
stephen hemminger2b018882017-01-24 13:06:03 -0800820 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700821
stephen hemmingera0be4502017-03-22 14:51:01 -0700822 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700823 return -ENODEV;
824
stephen hemminger2b018882017-01-24 13:06:03 -0800825 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700826 return -EINVAL;
827
stephen hemminger2b018882017-01-24 13:06:03 -0800828 if (count > nvdev->max_chn)
829 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700830
stephen hemminger7ca45932017-07-24 10:57:28 -0700831 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700832 was_opened = rndis_filter_opened(nvdev);
833 if (was_opened)
834 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700835
stephen hemminger2289f0a2017-01-24 13:06:10 -0800836 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700837
stephen hemminger7ca45932017-07-24 10:57:28 -0700838 memset(&device_info, 0, sizeof(device_info));
839 device_info.num_chn = count;
840 device_info.ring_size = ring_size;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700841
stephen hemminger7ca45932017-07-24 10:57:28 -0700842 nvdev = rndis_filter_device_add(dev, &device_info);
843 if (!IS_ERR(nvdev)) {
844 netif_set_real_num_tx_queues(net, nvdev->num_chn);
845 netif_set_real_num_rx_queues(net, nvdev->num_chn);
stephen hemminger7ca45932017-07-24 10:57:28 -0700846 } else {
stephen hemmingerd6aac1f2017-07-28 08:59:41 -0700847 ret = PTR_ERR(nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700848 device_info.num_chn = orig;
stephen hemminger68d715f2017-08-09 17:46:06 -0700849 nvdev = rndis_filter_device_add(dev, &device_info);
850
851 if (IS_ERR(nvdev)) {
852 netdev_err(net, "restoring channel setting failed: %ld\n",
853 PTR_ERR(nvdev));
854 return ret;
855 }
stephen hemminger7ca45932017-07-24 10:57:28 -0700856 }
857
stephen hemmingerea383bf2017-07-19 11:53:15 -0700858 if (was_opened)
859 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700860
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200861 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700862 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200863 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700864
865 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700866}
867
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100868static bool
869netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800870{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100871 struct ethtool_link_ksettings diff1 = *cmd;
872 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800873
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100874 diff1.base.speed = 0;
875 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800876 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100877 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
878 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800879 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100880 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800881
882 return !memcmp(&diff1, &diff2, sizeof(diff1));
883}
884
885static void netvsc_init_settings(struct net_device *dev)
886{
887 struct net_device_context *ndc = netdev_priv(dev);
888
889 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700890 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800891}
892
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100893static int netvsc_get_link_ksettings(struct net_device *dev,
894 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800895{
896 struct net_device_context *ndc = netdev_priv(dev);
897
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100898 cmd->base.speed = ndc->speed;
899 cmd->base.duplex = ndc->duplex;
900 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800901
902 return 0;
903}
904
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100905static int netvsc_set_link_ksettings(struct net_device *dev,
906 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800907{
908 struct net_device_context *ndc = netdev_priv(dev);
909 u32 speed;
910
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100911 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800912 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100913 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800914 !netvsc_validate_ethtool_ss_cmd(cmd))
915 return -EINVAL;
916
917 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100918 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800919
920 return 0;
921}
922
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800923static int netvsc_change_mtu(struct net_device *ndev, int mtu)
924{
925 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -0700926 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700927 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200928 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700929 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800930 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700931 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700932 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800933
stephen hemmingera0be4502017-03-22 14:51:01 -0700934 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800935 return -ENODEV;
936
stephen hemminger0c195562017-08-01 19:58:53 -0700937 /* Change MTU of underlying VF netdev first. */
938 if (vf_netdev) {
939 ret = dev_set_mtu(vf_netdev, mtu);
940 if (ret)
941 return ret;
942 }
943
stephen hemmingerea383bf2017-07-19 11:53:15 -0700944 netif_device_detach(ndev);
945 was_opened = rndis_filter_opened(nvdev);
946 if (was_opened)
947 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700948
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700949 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800950 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800951 device_info.num_chn = nvdev->num_chn;
Dexuan Cui152669b2017-03-02 13:00:53 +0000952
Dexuan Cui152669b2017-03-02 13:00:53 +0000953 rndis_filter_device_remove(hdev, nvdev);
954
Dexuan Cui152669b2017-03-02 13:00:53 +0000955 ndev->mtu = mtu;
956
stephen hemminger9749fed2017-07-19 11:53:16 -0700957 nvdev = rndis_filter_device_add(hdev, &device_info);
958 if (IS_ERR(nvdev)) {
959 ret = PTR_ERR(nvdev);
960
961 /* Attempt rollback to original MTU */
962 ndev->mtu = orig_mtu;
stephen hemminger68d715f2017-08-09 17:46:06 -0700963 nvdev = rndis_filter_device_add(hdev, &device_info);
stephen hemminger0c195562017-08-01 19:58:53 -0700964
965 if (vf_netdev)
966 dev_set_mtu(vf_netdev, orig_mtu);
stephen hemminger68d715f2017-08-09 17:46:06 -0700967
968 if (IS_ERR(nvdev)) {
969 netdev_err(ndev, "restoring mtu failed: %ld\n",
970 PTR_ERR(nvdev));
971 return ret;
972 }
stephen hemminger9749fed2017-07-19 11:53:16 -0700973 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800974
stephen hemmingerea383bf2017-07-19 11:53:15 -0700975 if (was_opened)
976 rndis_filter_open(nvdev);
977
978 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700979
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200980 /* We may have missed link change notifications */
981 schedule_delayed_work(&ndevctx->dwork, 0);
982
stephen hemminger9749fed2017-07-19 11:53:16 -0700983 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800984}
985
stephen hemminger0c195562017-08-01 19:58:53 -0700986static void netvsc_get_vf_stats(struct net_device *net,
987 struct netvsc_vf_pcpu_stats *tot)
988{
989 struct net_device_context *ndev_ctx = netdev_priv(net);
990 int i;
991
992 memset(tot, 0, sizeof(*tot));
993
994 for_each_possible_cpu(i) {
995 const struct netvsc_vf_pcpu_stats *stats
996 = per_cpu_ptr(ndev_ctx->vf_stats, i);
997 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
998 unsigned int start;
999
1000 do {
1001 start = u64_stats_fetch_begin_irq(&stats->syncp);
1002 rx_packets = stats->rx_packets;
1003 tx_packets = stats->tx_packets;
1004 rx_bytes = stats->rx_bytes;
1005 tx_bytes = stats->tx_bytes;
1006 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1007
1008 tot->rx_packets += rx_packets;
1009 tot->tx_packets += tx_packets;
1010 tot->rx_bytes += rx_bytes;
1011 tot->tx_bytes += tx_bytes;
1012 tot->tx_dropped += stats->tx_dropped;
1013 }
1014}
1015
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001016static void netvsc_get_stats64(struct net_device *net,
1017 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001018{
1019 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -07001020 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001021 struct netvsc_vf_pcpu_stats vf_tot;
stephen hemminger89bb42b2017-08-09 17:46:08 -07001022 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001023
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001024 if (!nvdev)
1025 return;
1026
stephen hemminger0c195562017-08-01 19:58:53 -07001027 netdev_stats_to_stats64(t, &net->stats);
1028
1029 netvsc_get_vf_stats(net, &vf_tot);
1030 t->rx_packets += vf_tot.rx_packets;
1031 t->tx_packets += vf_tot.tx_packets;
1032 t->rx_bytes += vf_tot.rx_bytes;
1033 t->tx_bytes += vf_tot.tx_bytes;
1034 t->tx_dropped += vf_tot.tx_dropped;
1035
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001036 for (i = 0; i < nvdev->num_chn; i++) {
1037 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1038 const struct netvsc_stats *stats;
1039 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001040 unsigned int start;
1041
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001042 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001043 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001044 start = u64_stats_fetch_begin_irq(&stats->syncp);
1045 packets = stats->packets;
1046 bytes = stats->bytes;
1047 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001048
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001049 t->tx_bytes += bytes;
1050 t->tx_packets += packets;
1051
1052 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001053 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001054 start = u64_stats_fetch_begin_irq(&stats->syncp);
1055 packets = stats->packets;
1056 bytes = stats->bytes;
1057 multicast = stats->multicast + stats->broadcast;
1058 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001059
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001060 t->rx_bytes += bytes;
1061 t->rx_packets += packets;
1062 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001063 }
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001064}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001065
1066static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1067{
stephen hemminger867047c2017-07-28 08:59:42 -07001068 struct net_device_context *ndc = netdev_priv(ndev);
stephen hemminger16ba3262017-08-09 17:46:05 -07001069 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
stephen hemminger867047c2017-07-28 08:59:42 -07001070 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001071 struct sockaddr *addr = p;
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001072 int err;
1073
stephen hemminger16ba3262017-08-09 17:46:05 -07001074 err = eth_prepare_mac_addr_change(ndev, p);
1075 if (err)
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001076 return err;
1077
stephen hemminger867047c2017-07-28 08:59:42 -07001078 if (!nvdev)
1079 return -ENODEV;
1080
stephen hemminger16ba3262017-08-09 17:46:05 -07001081 if (vf_netdev) {
1082 err = dev_set_mac_address(vf_netdev, addr);
1083 if (err)
1084 return err;
1085 }
1086
stephen hemminger867047c2017-07-28 08:59:42 -07001087 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
stephen hemminger16ba3262017-08-09 17:46:05 -07001088 if (!err) {
1089 eth_commit_mac_addr_change(ndev, p);
1090 } else if (vf_netdev) {
1091 /* rollback change on VF */
1092 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1093 dev_set_mac_address(vf_netdev, addr);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001094 }
1095
1096 return err;
1097}
1098
Stephen Hemminger4323b472016-08-23 12:17:57 -07001099static const struct {
1100 char name[ETH_GSTRING_LEN];
1101 u16 offset;
1102} netvsc_stats[] = {
1103 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1104 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1105 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1106 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1107 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
stephen hemminger0c195562017-08-01 19:58:53 -07001108}, vf_stats[] = {
1109 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1110 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1111 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1112 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1113 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001114};
1115
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001116#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
stephen hemminger0c195562017-08-01 19:58:53 -07001117#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001118
1119/* 4 statistics per queue (rx/tx packets/bytes) */
1120#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1121
Stephen Hemminger4323b472016-08-23 12:17:57 -07001122static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1123{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001124 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -07001125 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001126
1127 if (!nvdev)
1128 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001129
Stephen Hemminger4323b472016-08-23 12:17:57 -07001130 switch (string_set) {
1131 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001132 return NETVSC_GLOBAL_STATS_LEN
1133 + NETVSC_VF_STATS_LEN
1134 + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001135 default:
1136 return -EINVAL;
1137 }
1138}
1139
1140static void netvsc_get_ethtool_stats(struct net_device *dev,
1141 struct ethtool_stats *stats, u64 *data)
1142{
1143 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001144 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001145 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001146 const struct netvsc_stats *qstats;
stephen hemminger0c195562017-08-01 19:58:53 -07001147 struct netvsc_vf_pcpu_stats sum;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001148 unsigned int start;
1149 u64 packets, bytes;
1150 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001151
stephen hemminger545a8e72017-03-22 14:51:00 -07001152 if (!nvdev)
1153 return;
1154
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001155 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001156 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001157
stephen hemminger0c195562017-08-01 19:58:53 -07001158 netvsc_get_vf_stats(dev, &sum);
1159 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1160 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1161
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001162 for (j = 0; j < nvdev->num_chn; j++) {
1163 qstats = &nvdev->chan_table[j].tx_stats;
1164
1165 do {
1166 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1167 packets = qstats->packets;
1168 bytes = qstats->bytes;
1169 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1170 data[i++] = packets;
1171 data[i++] = bytes;
1172
1173 qstats = &nvdev->chan_table[j].rx_stats;
1174 do {
1175 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1176 packets = qstats->packets;
1177 bytes = qstats->bytes;
1178 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1179 data[i++] = packets;
1180 data[i++] = bytes;
1181 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001182}
1183
1184static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1185{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001186 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001187 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001188 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001189 int i;
1190
stephen hemminger545a8e72017-03-22 14:51:00 -07001191 if (!nvdev)
1192 return;
1193
Stephen Hemminger4323b472016-08-23 12:17:57 -07001194 switch (stringset) {
1195 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001196 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1197 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1198 p += ETH_GSTRING_LEN;
1199 }
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001200
stephen hemminger0c195562017-08-01 19:58:53 -07001201 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1202 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1203 p += ETH_GSTRING_LEN;
1204 }
1205
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001206 for (i = 0; i < nvdev->num_chn; i++) {
1207 sprintf(p, "tx_queue_%u_packets", i);
1208 p += ETH_GSTRING_LEN;
1209 sprintf(p, "tx_queue_%u_bytes", i);
1210 p += ETH_GSTRING_LEN;
1211 sprintf(p, "rx_queue_%u_packets", i);
1212 p += ETH_GSTRING_LEN;
1213 sprintf(p, "rx_queue_%u_bytes", i);
1214 p += ETH_GSTRING_LEN;
1215 }
1216
Stephen Hemminger4323b472016-08-23 12:17:57 -07001217 break;
1218 }
1219}
1220
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001221static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001222netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1223 struct ethtool_rxnfc *info)
1224{
1225 info->data = RXH_IP_SRC | RXH_IP_DST;
1226
1227 switch (info->flow_type) {
1228 case TCP_V4_FLOW:
1229 case TCP_V6_FLOW:
1230 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1231 /* fallthrough */
1232 case UDP_V4_FLOW:
1233 case UDP_V6_FLOW:
1234 case IPV4_FLOW:
1235 case IPV6_FLOW:
1236 break;
1237 default:
1238 info->data = 0;
1239 break;
1240 }
1241
1242 return 0;
1243}
1244
1245static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001246netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1247 u32 *rules)
1248{
1249 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001250 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001251
1252 if (!nvdev)
1253 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001254
1255 switch (info->cmd) {
1256 case ETHTOOL_GRXRINGS:
1257 info->data = nvdev->num_chn;
1258 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001259
1260 case ETHTOOL_GRXFH:
1261 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001262 }
1263 return -EOPNOTSUPP;
1264}
1265
Richard Weinberger316158f2014-07-09 16:23:59 +02001266#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001267static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001268{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001269 struct net_device_context *ndc = netdev_priv(dev);
1270 struct netvsc_device *ndev;
1271 int i;
1272
1273 rcu_read_lock();
1274 ndev = rcu_dereference(ndc->nvdev);
1275 if (ndev) {
1276 for (i = 0; i < ndev->num_chn; i++) {
1277 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1278
1279 napi_schedule(&nvchan->napi);
1280 }
1281 }
1282 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001283}
1284#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001285
stephen hemminger962f3fe2017-01-24 13:06:02 -08001286static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1287{
1288 return NETVSC_HASH_KEYLEN;
1289}
1290
1291static u32 netvsc_rss_indir_size(struct net_device *dev)
1292{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001293 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001294}
1295
1296static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1297 u8 *hfunc)
1298{
1299 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001300 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001301 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001302 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001303
stephen hemminger545a8e72017-03-22 14:51:00 -07001304 if (!ndev)
1305 return -ENODEV;
1306
stephen hemminger962f3fe2017-01-24 13:06:02 -08001307 if (hfunc)
1308 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1309
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001310 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001311 if (indir) {
1312 for (i = 0; i < ITAB_NUM; i++)
1313 indir[i] = rndis_dev->ind_table[i];
1314 }
1315
stephen hemminger962f3fe2017-01-24 13:06:02 -08001316 if (key)
1317 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1318
1319 return 0;
1320}
1321
1322static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1323 const u8 *key, const u8 hfunc)
1324{
1325 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001326 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001327 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001328 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001329
stephen hemminger545a8e72017-03-22 14:51:00 -07001330 if (!ndev)
1331 return -ENODEV;
1332
stephen hemminger962f3fe2017-01-24 13:06:02 -08001333 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1334 return -EOPNOTSUPP;
1335
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001336 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001337 if (indir) {
1338 for (i = 0; i < ITAB_NUM; i++)
Arnd Bergmannb92b7d32017-06-22 00:16:37 +02001339 if (indir[i] >= VRSS_CHANNEL_MAX)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001340 return -EINVAL;
1341
1342 for (i = 0; i < ITAB_NUM; i++)
1343 rndis_dev->ind_table[i] = indir[i];
1344 }
1345
1346 if (!key) {
1347 if (!indir)
1348 return 0;
1349
1350 key = rndis_dev->rss_key;
1351 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001352
1353 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1354}
1355
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001356static const struct ethtool_ops ethtool_ops = {
1357 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001358 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001359 .get_ethtool_stats = netvsc_get_ethtool_stats,
1360 .get_sset_count = netvsc_get_sset_count,
1361 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001362 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001363 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001364 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001365 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001366 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1367 .get_rxfh_indir_size = netvsc_rss_indir_size,
1368 .get_rxfh = netvsc_get_rxfh,
1369 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001370 .get_link_ksettings = netvsc_get_link_ksettings,
1371 .set_link_ksettings = netvsc_set_link_ksettings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001372};
1373
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001374static const struct net_device_ops device_ops = {
1375 .ndo_open = netvsc_open,
1376 .ndo_stop = netvsc_close,
1377 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001378 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001379 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001380 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001381 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001382 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001383 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001384#ifdef CONFIG_NET_POLL_CONTROLLER
1385 .ndo_poll_controller = netvsc_poll_controller,
1386#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001387};
1388
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001389/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001390 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1391 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1392 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001393 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001394static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001395{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001396 struct net_device_context *ndev_ctx =
1397 container_of(w, struct net_device_context, dwork.work);
1398 struct hv_device *device_obj = ndev_ctx->device_ctx;
1399 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001400 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001401 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001402 struct netvsc_reconfig *event = NULL;
1403 bool notify = false, reschedule = false;
1404 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001405
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001406 rtnl_lock();
stephen hemmingera0be4502017-03-22 14:51:01 -07001407 net_device = rtnl_dereference(ndev_ctx->nvdev);
1408 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001409 goto out_unlock;
1410
Haiyang Zhang891de742014-02-12 16:54:27 -08001411 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001412
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001413 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1414 if (time_is_after_jiffies(next_reconfig)) {
1415 /* link_watch only sends one notification with current state
1416 * per second, avoid doing reconfig more frequently. Handle
1417 * wrap around.
1418 */
1419 delay = next_reconfig - jiffies;
1420 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1421 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001422 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001423 }
1424 ndev_ctx->last_reconfig = jiffies;
1425
1426 spin_lock_irqsave(&ndev_ctx->lock, flags);
1427 if (!list_empty(&ndev_ctx->reconfig_events)) {
1428 event = list_first_entry(&ndev_ctx->reconfig_events,
1429 struct netvsc_reconfig, list);
1430 list_del(&event->list);
1431 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1432 }
1433 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1434
1435 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001436 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001437
1438 switch (event->event) {
1439 /* Only the following events are possible due to the check in
1440 * netvsc_linkstatus_callback()
1441 */
1442 case RNDIS_STATUS_MEDIA_CONNECT:
1443 if (rdev->link_state) {
1444 rdev->link_state = false;
stephen hemminger0c195562017-08-01 19:58:53 -07001445 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001446 netif_tx_wake_all_queues(net);
1447 } else {
1448 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001449 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001450 kfree(event);
1451 break;
1452 case RNDIS_STATUS_MEDIA_DISCONNECT:
1453 if (!rdev->link_state) {
1454 rdev->link_state = true;
1455 netif_carrier_off(net);
1456 netif_tx_stop_all_queues(net);
1457 }
1458 kfree(event);
1459 break;
1460 case RNDIS_STATUS_NETWORK_CHANGE:
1461 /* Only makes sense if carrier is present */
1462 if (!rdev->link_state) {
1463 rdev->link_state = true;
1464 netif_carrier_off(net);
1465 netif_tx_stop_all_queues(net);
1466 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1467 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001468 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001469 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1470 reschedule = true;
1471 }
1472 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001473 }
1474
1475 rtnl_unlock();
1476
1477 if (notify)
1478 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001479
1480 /* link_watch only sends one notification with current state per
1481 * second, handle next reconfig event in 2 seconds.
1482 */
1483 if (reschedule)
1484 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001485
1486 return;
1487
1488out_unlock:
1489 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001490}
1491
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001492static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001493{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001494 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001495
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001496 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001497
1498 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001499 if (dev->netdev_ops != &device_ops)
1500 continue; /* not a netvsc device */
1501
1502 if (ether_addr_equal(mac, dev->perm_addr))
1503 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001504 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001505
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001506 return NULL;
1507}
1508
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001509static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001510{
1511 struct net_device *dev;
1512
1513 ASSERT_RTNL();
1514
1515 for_each_netdev(&init_net, dev) {
1516 struct net_device_context *net_device_ctx;
1517
1518 if (dev->netdev_ops != &device_ops)
1519 continue; /* not a netvsc device */
1520
1521 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001522 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001523 continue; /* device is removed */
1524
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001525 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001526 return dev; /* a match */
1527 }
1528
1529 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001530}
1531
stephen hemminger0c195562017-08-01 19:58:53 -07001532/* Called when VF is injecting data into network stack.
1533 * Change the associated network device from VF to netvsc.
1534 * note: already called with rcu_read_lock
1535 */
1536static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1537{
1538 struct sk_buff *skb = *pskb;
1539 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1540 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1541 struct netvsc_vf_pcpu_stats *pcpu_stats
1542 = this_cpu_ptr(ndev_ctx->vf_stats);
1543
1544 skb->dev = ndev;
1545
1546 u64_stats_update_begin(&pcpu_stats->syncp);
1547 pcpu_stats->rx_packets++;
1548 pcpu_stats->rx_bytes += skb->len;
1549 u64_stats_update_end(&pcpu_stats->syncp);
1550
1551 return RX_HANDLER_ANOTHER;
1552}
1553
1554static int netvsc_vf_join(struct net_device *vf_netdev,
1555 struct net_device *ndev)
1556{
1557 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1558 int ret;
1559
1560 ret = netdev_rx_handler_register(vf_netdev,
1561 netvsc_vf_handle_frame, ndev);
1562 if (ret != 0) {
1563 netdev_err(vf_netdev,
1564 "can not register netvsc VF receive handler (err = %d)\n",
1565 ret);
1566 goto rx_handler_failed;
1567 }
1568
1569 ret = netdev_upper_dev_link(vf_netdev, ndev);
1570 if (ret != 0) {
1571 netdev_err(vf_netdev,
1572 "can not set master device %s (err = %d)\n",
1573 ndev->name, ret);
1574 goto upper_link_failed;
1575 }
1576
1577 /* set slave flag before open to prevent IPv6 addrconf */
1578 vf_netdev->flags |= IFF_SLAVE;
1579
stephen hemminger6123c662017-08-09 17:46:03 -07001580 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1581
1582 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001583
1584 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1585 return 0;
1586
1587upper_link_failed:
1588 netdev_rx_handler_unregister(vf_netdev);
1589rx_handler_failed:
1590 return ret;
1591}
1592
1593static void __netvsc_vf_setup(struct net_device *ndev,
1594 struct net_device *vf_netdev)
1595{
1596 int ret;
1597
stephen hemminger0c195562017-08-01 19:58:53 -07001598 /* Align MTU of VF with master */
1599 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1600 if (ret)
1601 netdev_warn(vf_netdev,
1602 "unable to change mtu to %u\n", ndev->mtu);
1603
1604 if (netif_running(ndev)) {
1605 ret = dev_open(vf_netdev);
1606 if (ret)
1607 netdev_warn(vf_netdev,
1608 "unable to open: %d\n", ret);
1609 }
1610}
1611
1612/* Setup VF as slave of the synthetic device.
1613 * Runs in workqueue to avoid recursion in netlink callbacks.
1614 */
1615static void netvsc_vf_setup(struct work_struct *w)
1616{
1617 struct net_device_context *ndev_ctx
stephen hemminger6123c662017-08-09 17:46:03 -07001618 = container_of(w, struct net_device_context, vf_takeover.work);
stephen hemminger0c195562017-08-01 19:58:53 -07001619 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1620 struct net_device *vf_netdev;
1621
stephen hemmingerfb84af82017-08-04 12:14:00 -07001622 if (!rtnl_trylock()) {
stephen hemminger6123c662017-08-09 17:46:03 -07001623 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
stephen hemmingerfb84af82017-08-04 12:14:00 -07001624 return;
1625 }
1626
stephen hemminger0c195562017-08-01 19:58:53 -07001627 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1628 if (vf_netdev)
1629 __netvsc_vf_setup(ndev, vf_netdev);
1630
1631 rtnl_unlock();
1632}
1633
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001634static int netvsc_register_vf(struct net_device *vf_netdev)
1635{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001636 struct net_device *ndev;
1637 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001638 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001639
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001640 if (vf_netdev->addr_len != ETH_ALEN)
1641 return NOTIFY_DONE;
1642
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001643 /*
1644 * We will use the MAC address to locate the synthetic interface to
1645 * associate with the VF interface. If we don't find a matching
1646 * synthetic interface, move on.
1647 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001648 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001649 if (!ndev)
1650 return NOTIFY_DONE;
1651
1652 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001653 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001654 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001655 return NOTIFY_DONE;
1656
stephen hemminger0c195562017-08-01 19:58:53 -07001657 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1658 return NOTIFY_DONE;
1659
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001660 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
stephen hemminger0c195562017-08-01 19:58:53 -07001661
1662 /* Prevent this module from being unloaded while VF is registered */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001663 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001664
1665 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001666 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001667 return NOTIFY_OK;
1668}
1669
stephen hemminger7b83f522017-08-07 11:30:00 -07001670static int netvsc_vf_up(struct net_device *vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001671{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001672 struct net_device_context *net_device_ctx;
stephen hemminger7b83f522017-08-07 11:30:00 -07001673 struct netvsc_device *netvsc_dev;
stephen hemminger0c195562017-08-01 19:58:53 -07001674 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001675
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001676 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001677 if (!ndev)
1678 return NOTIFY_DONE;
1679
1680 net_device_ctx = netdev_priv(ndev);
stephen hemminger7b83f522017-08-07 11:30:00 -07001681 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1682 if (!netvsc_dev)
1683 return NOTIFY_DONE;
1684
1685 /* Bump refcount when datapath is acvive - Why? */
1686 rndis_filter_open(netvsc_dev);
1687
1688 /* notify the host to switch the data path. */
1689 netvsc_switch_datapath(ndev, true);
1690 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1691
1692 return NOTIFY_OK;
1693}
1694
1695static int netvsc_vf_down(struct net_device *vf_netdev)
1696{
1697 struct net_device_context *net_device_ctx;
1698 struct netvsc_device *netvsc_dev;
1699 struct net_device *ndev;
1700
1701 ndev = get_netvsc_byref(vf_netdev);
1702 if (!ndev)
1703 return NOTIFY_DONE;
1704
1705 net_device_ctx = netdev_priv(ndev);
1706 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1707 if (!netvsc_dev)
1708 return NOTIFY_DONE;
1709
1710 netvsc_switch_datapath(ndev, false);
1711 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1712 rndis_filter_close(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001713
1714 return NOTIFY_OK;
1715}
1716
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001717static int netvsc_unregister_vf(struct net_device *vf_netdev)
1718{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001719 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001720 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001721
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001722 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001723 if (!ndev)
1724 return NOTIFY_DONE;
1725
1726 net_device_ctx = netdev_priv(ndev);
stephen hemminger6123c662017-08-09 17:46:03 -07001727 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001728
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001729 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001730
stephen hemminger0c195562017-08-01 19:58:53 -07001731 netdev_upper_dev_unlink(vf_netdev, ndev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001732 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001733 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001734 module_put(THIS_MODULE);
1735 return NOTIFY_OK;
1736}
1737
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001738static int netvsc_probe(struct hv_device *dev,
1739 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001740{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001741 struct net_device *net = NULL;
1742 struct net_device_context *net_device_ctx;
1743 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001744 struct netvsc_device *nvdev;
stephen hemminger0c195562017-08-01 19:58:53 -07001745 int ret = -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001746
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001747 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001748 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001749 if (!net)
stephen hemminger0c195562017-08-01 19:58:53 -07001750 goto no_net;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001751
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001752 netif_carrier_off(net);
1753
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001754 netvsc_init_settings(net);
1755
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001756 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001757 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001758 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1759 if (netif_msg_probe(net_device_ctx))
1760 netdev_dbg(net, "netvsc msg_enable: %d\n",
1761 net_device_ctx->msg_enable);
1762
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001763 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001764
Haiyang Zhang891de742014-02-12 16:54:27 -08001765 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001766
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001767 spin_lock_init(&net_device_ctx->lock);
1768 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
stephen hemminger6123c662017-08-09 17:46:03 -07001769 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
stephen hemminger0c195562017-08-01 19:58:53 -07001770
1771 net_device_ctx->vf_stats
1772 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1773 if (!net_device_ctx->vf_stats)
1774 goto no_stats;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001775
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001776 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001777 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001778 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001779
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001780 /* We always need headroom for rndis header */
1781 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1782
Haiyang Zhang692e0842011-09-01 12:19:43 -07001783 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001784 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001785 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001786 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger9749fed2017-07-19 11:53:16 -07001787
1788 nvdev = rndis_filter_device_add(dev, &device_info);
1789 if (IS_ERR(nvdev)) {
1790 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001791 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
stephen hemminger0c195562017-08-01 19:58:53 -07001792 goto rndis_failed;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001793 }
stephen hemminger0c195562017-08-01 19:58:53 -07001794
Haiyang Zhang692e0842011-09-01 12:19:43 -07001795 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1796
stephen hemminger23312a32017-01-24 13:05:59 -08001797 /* hw_features computed in rndis_filter_device_add */
1798 net->features = net->hw_features |
1799 NETIF_F_HIGHDMA | NETIF_F_SG |
1800 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1801 net->vlan_features = net->features;
1802
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001803 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1804 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001805
stephen hemminger9749fed2017-07-19 11:53:16 -07001806 netdev_lockdep_set_classes(net);
1807
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001808 /* MTU range: 68 - 1500 or 65521 */
1809 net->min_mtu = NETVSC_MTU_MIN;
1810 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1811 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1812 else
1813 net->max_mtu = ETH_DATA_LEN;
1814
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001815 ret = register_netdev(net);
1816 if (ret != 0) {
1817 pr_err("Unable to register netdev.\n");
stephen hemminger0c195562017-08-01 19:58:53 -07001818 goto register_failed;
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001819 }
1820
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001821 return ret;
stephen hemminger0c195562017-08-01 19:58:53 -07001822
1823register_failed:
1824 rndis_filter_device_remove(dev, nvdev);
1825rndis_failed:
1826 free_percpu(net_device_ctx->vf_stats);
1827no_stats:
1828 hv_set_drvdata(dev, NULL);
1829 free_netdev(net);
1830no_net:
1831 return ret;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001832}
1833
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001834static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001835{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001836 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001837 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001838
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001839 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001840
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001841 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001842 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001843 return 0;
1844 }
1845
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001846 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001847
stephen hemmingera0be4502017-03-22 14:51:01 -07001848 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001849
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001850 cancel_delayed_work_sync(&ndev_ctx->dwork);
1851
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001852 /*
1853 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07001854 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001855 */
stephen hemmingera0be4502017-03-22 14:51:01 -07001856 rtnl_lock();
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001857 rndis_filter_device_remove(dev,
1858 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07001859 rtnl_unlock();
1860
1861 unregister_netdev(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001862
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001863 hv_set_drvdata(dev, NULL);
1864
stephen hemminger0c195562017-08-01 19:58:53 -07001865 free_percpu(ndev_ctx->vf_stats);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001866 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001867 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001868}
1869
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001870static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001871 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001872 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001873 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001874};
1875
1876MODULE_DEVICE_TABLE(vmbus, id_table);
1877
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001878/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001879static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001880 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001881 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001882 .probe = netvsc_probe,
1883 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001884};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001885
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001886/*
1887 * On Hyper-V, every VF interface is matched with a corresponding
1888 * synthetic interface. The synthetic interface is presented first
1889 * to the guest. When the corresponding VF instance is registered,
1890 * we will take care of switching the data path.
1891 */
1892static int netvsc_netdev_event(struct notifier_block *this,
1893 unsigned long event, void *ptr)
1894{
1895 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1896
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001897 /* Skip our own events */
1898 if (event_dev->netdev_ops == &device_ops)
1899 return NOTIFY_DONE;
1900
1901 /* Avoid non-Ethernet type devices */
1902 if (event_dev->type != ARPHRD_ETHER)
1903 return NOTIFY_DONE;
1904
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001905 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06001906 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001907 return NOTIFY_DONE;
1908
1909 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001910 if ((event_dev->priv_flags & IFF_BONDING) &&
1911 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001912 return NOTIFY_DONE;
1913
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001914 switch (event) {
1915 case NETDEV_REGISTER:
1916 return netvsc_register_vf(event_dev);
1917 case NETDEV_UNREGISTER:
1918 return netvsc_unregister_vf(event_dev);
1919 case NETDEV_UP:
stephen hemminger7b83f522017-08-07 11:30:00 -07001920 return netvsc_vf_up(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001921 case NETDEV_DOWN:
stephen hemminger7b83f522017-08-07 11:30:00 -07001922 return netvsc_vf_down(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001923 default:
1924 return NOTIFY_DONE;
1925 }
1926}
1927
1928static struct notifier_block netvsc_netdev_notifier = {
1929 .notifier_call = netvsc_netdev_event,
1930};
1931
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001932static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001933{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001934 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001935 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001936}
1937
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001938static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001939{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001940 int ret;
1941
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001942 if (ring_size < RING_SIZE_MIN) {
1943 ring_size = RING_SIZE_MIN;
1944 pr_info("Increased ring_size to %d (min allowed)\n",
1945 ring_size);
1946 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001947 ret = vmbus_driver_register(&netvsc_drv);
1948
1949 if (ret)
1950 return ret;
1951
1952 register_netdevice_notifier(&netvsc_netdev_notifier);
1953 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001954}
1955
Hank Janssen26c14cc2010-02-11 23:02:42 +00001956MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001957MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001958
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001959module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001960module_exit(netvsc_drv_exit);