blob: eb0023f55fe1b3f67c39313e47697f834ee373e3 [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 hemmingera50af862016-12-06 13:43:54 -080050
Hank Janssen99c8da02010-10-12 10:45:23 -070051static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070052module_param(ring_size, int, S_IRUGO);
53MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070054
Simon Xiao3f300ff2015-04-28 01:05:17 -070055static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
56 NETIF_MSG_LINK | NETIF_MSG_IFUP |
57 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
58 NETIF_MSG_TX_ERR;
59
60static int debug = -1;
61module_param(debug, int, S_IRUGO);
62MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
63
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070064static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070065{
Wenqi Ma792df872012-04-19 00:39:37 +000066 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070067 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080068
stephen hemminger4f19c0d2017-06-07 15:53:49 -070069 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070070}
71
Hank Janssenfceaf242009-07-13 15:34:54 -070072static int netvsc_open(struct net_device *net)
73{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070074 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -070075 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070076 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080077 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070078 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070079
Haiyang Zhang891de742014-02-12 16:54:27 -080080 netif_carrier_off(net);
81
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070082 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020083 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070084 if (ret != 0) {
85 netdev_err(net, "unable to open device (ret %d).\n", ret);
86 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070087 }
88
Haiyang Zhang2de85302015-07-13 13:09:16 -070089 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070090
Haiyang Zhang891de742014-02-12 16:54:27 -080091 rdev = nvdev->extension;
stephen hemminger0c195562017-08-01 19:58:53 -070092
93 if (!rdev->link_state)
Haiyang Zhang891de742014-02-12 16:54:27 -080094 netif_carrier_on(net);
95
stephen hemminger0c195562017-08-01 19:58:53 -070096 if (vf_netdev) {
97 /* Setting synthetic device up transparently sets
98 * slave as up. If open fails, then slave will be
99 * still be offline (and not used).
100 */
101 ret = dev_open(vf_netdev);
102 if (ret)
103 netdev_warn(net,
104 "unable to open slave: %s: %d\n",
105 vf_netdev->name, ret);
106 }
107 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700108}
109
Hank Janssenfceaf242009-07-13 15:34:54 -0700110static int netvsc_close(struct net_device *net)
111{
Hank Janssenfceaf242009-07-13 15:34:54 -0700112 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700113 struct net_device *vf_netdev
114 = rtnl_dereference(net_device_ctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700115 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700116 int ret;
stephen hemminger40975962017-06-08 16:21:19 -0700117 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700118 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700119
Haiyang Zhang0a282532012-02-02 07:17:59 +0000120 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700121
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200122 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700123 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700124 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700125 return ret;
126 }
127
128 /* Ensure pending bytes in ring are read */
129 while (true) {
130 aread = 0;
131 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800132 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700133 if (!chn)
134 continue;
135
stephen hemminger40975962017-06-08 16:21:19 -0700136 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700137 if (aread)
138 break;
139
stephen hemminger40975962017-06-08 16:21:19 -0700140 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700141 if (aread)
142 break;
143 }
144
145 retry++;
146 if (retry > retry_max || aread == 0)
147 break;
148
149 msleep(msec);
150
151 if (msec < 1000)
152 msec *= 2;
153 }
154
155 if (aread) {
156 netdev_err(net, "Ring buffer not empty after closing rndis\n");
157 ret = -ETIMEDOUT;
158 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700159
stephen hemminger0c195562017-08-01 19:58:53 -0700160 if (vf_netdev)
161 dev_close(vf_netdev);
162
Hank Janssenfceaf242009-07-13 15:34:54 -0700163 return ret;
164}
165
KY Srinivasan8a002512014-03-08 19:23:14 -0800166static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
167 int pkt_type)
168{
169 struct rndis_packet *rndis_pkt;
170 struct rndis_per_packet_info *ppi;
171
172 rndis_pkt = &msg->msg.pkt;
173 rndis_pkt->data_offset += ppi_size;
174
175 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
176 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
177
178 ppi->size = ppi_size;
179 ppi->type = pkt_type;
180 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
181
182 rndis_pkt->per_pkt_info_len += ppi_size;
183
184 return ppi;
185}
186
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700187/* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
188 * hash for non-TCP traffic with only IP numbers.
189 */
190static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
191{
192 struct flow_keys flow;
193 u32 hash;
194 static u32 hashrnd __read_mostly;
195
196 net_get_random_once(&hashrnd, sizeof(hashrnd));
197
198 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
199 return 0;
200
201 if (flow.basic.ip_proto == IPPROTO_TCP) {
202 return skb_get_hash(skb);
203 } else {
204 if (flow.basic.n_proto == htons(ETH_P_IP))
205 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
206 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
207 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
208 else
209 hash = 0;
210
211 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
212 }
213
214 return hash;
215}
216
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700217static inline int netvsc_get_tx_queue(struct net_device *ndev,
218 struct sk_buff *skb, int old_idx)
219{
220 const struct net_device_context *ndc = netdev_priv(ndev);
221 struct sock *sk = skb->sk;
222 int q_idx;
223
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700224 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700225 (VRSS_SEND_TAB_SIZE - 1)];
226
227 /* If queue index changed record the new value */
228 if (q_idx != old_idx &&
229 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
230 sk_tx_queue_set(sk, q_idx);
231
232 return q_idx;
233}
234
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800235/*
236 * Select queue for transmit.
237 *
238 * If a valid queue has already been assigned, then use that.
239 * Otherwise compute tx queue based on hash and the send table.
240 *
241 * This is basically similar to default (__netdev_pick_tx) with the added step
242 * of using the host send_table when no other queue has been assigned.
243 *
244 * TODO support XPS - but get_xps_queue not exported
245 */
stephen hemminger0c195562017-08-01 19:58:53 -0700246static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700247{
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700248 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700249
stephen hemminger0c195562017-08-01 19:58:53 -0700250 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700251 /* If forwarding a packet, we use the recorded queue when
252 * available for better cache locality.
253 */
254 if (skb_rx_queue_recorded(skb))
255 q_idx = skb_get_rx_queue(skb);
256 else
257 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800258 }
259
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700260 return q_idx;
261}
262
stephen hemminger0c195562017-08-01 19:58:53 -0700263static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
264 void *accel_priv,
265 select_queue_fallback_t fallback)
266{
267 struct net_device_context *ndc = netdev_priv(ndev);
268 struct net_device *vf_netdev;
269 u16 txq;
270
271 rcu_read_lock();
272 vf_netdev = rcu_dereference(ndc->vf_netdev);
273 if (vf_netdev) {
274 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
275 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
276 } else {
277 txq = netvsc_pick_tx(ndev, skb);
278 }
279 rcu_read_unlock();
280
281 while (unlikely(txq >= ndev->real_num_tx_queues))
282 txq -= ndev->real_num_tx_queues;
283
284 return txq;
285}
286
KY Srinivasan54a73572014-03-08 19:23:13 -0800287static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
288 struct hv_page_buffer *pb)
289{
290 int j = 0;
291
292 /* Deal with compund pages by ignoring unused part
293 * of the page.
294 */
295 page += (offset >> PAGE_SHIFT);
296 offset &= ~PAGE_MASK;
297
298 while (len > 0) {
299 unsigned long bytes;
300
301 bytes = PAGE_SIZE - offset;
302 if (bytes > len)
303 bytes = len;
304 pb[j].pfn = page_to_pfn(page);
305 pb[j].offset = offset;
306 pb[j].len = bytes;
307
308 offset += bytes;
309 len -= bytes;
310
311 if (offset == PAGE_SIZE && len) {
312 page++;
313 offset = 0;
314 j++;
315 }
316 }
317
318 return j + 1;
319}
320
KY Srinivasan8a002512014-03-08 19:23:14 -0800321static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800322 struct hv_netvsc_packet *packet,
stephen hemminger02b6de02017-07-28 08:59:44 -0700323 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800324{
325 u32 slots_used = 0;
326 char *data = skb->data;
327 int frags = skb_shinfo(skb)->nr_frags;
328 int i;
329
330 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700331 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800332 * 2. skb linear data
333 * 3. skb fragment data
334 */
335 if (hdr != NULL)
336 slots_used += fill_pg_buf(virt_to_page(hdr),
337 offset_in_page(hdr),
338 len, &pb[slots_used]);
339
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700340 packet->rmsg_size = len;
341 packet->rmsg_pgcnt = slots_used;
342
KY Srinivasan54a73572014-03-08 19:23:13 -0800343 slots_used += fill_pg_buf(virt_to_page(data),
344 offset_in_page(data),
345 skb_headlen(skb), &pb[slots_used]);
346
347 for (i = 0; i < frags; i++) {
348 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
349
350 slots_used += fill_pg_buf(skb_frag_page(frag),
351 frag->page_offset,
352 skb_frag_size(frag), &pb[slots_used]);
353 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800354 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800355}
356
stephen hemminger80d887d2017-07-24 21:03:19 -0700357static int count_skb_frag_slots(struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800358{
stephen hemminger80d887d2017-07-24 21:03:19 -0700359 int i, frags = skb_shinfo(skb)->nr_frags;
360 int pages = 0;
361
362 for (i = 0; i < frags; i++) {
363 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
364 unsigned long size = skb_frag_size(frag);
365 unsigned long offset = frag->page_offset;
366
367 /* Skip unused frames from start of page */
368 offset &= ~PAGE_MASK;
369 pages += PFN_UP(offset + size);
370 }
371 return pages;
372}
373
374static int netvsc_get_slots(struct sk_buff *skb)
375{
376 char *data = skb->data;
377 unsigned int offset = offset_in_page(data);
378 unsigned int len = skb_headlen(skb);
379 int slots;
380 int frag_slots;
381
382 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
383 frag_slots = count_skb_frag_slots(skb);
384 return slots + frag_slots;
KY Srinivasan54a73572014-03-08 19:23:13 -0800385}
386
stephen hemminger23312a32017-01-24 13:05:59 -0800387static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800388{
stephen hemminger23312a32017-01-24 13:05:59 -0800389 if (skb->protocol == htons(ETH_P_IP)) {
390 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800391
stephen hemminger23312a32017-01-24 13:05:59 -0800392 if (ip->protocol == IPPROTO_TCP)
393 return TRANSPORT_INFO_IPV4_TCP;
394 else if (ip->protocol == IPPROTO_UDP)
395 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800396 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800397 struct ipv6hdr *ip6 = ipv6_hdr(skb);
398
399 if (ip6->nexthdr == IPPROTO_TCP)
400 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700401 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800402 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800403 }
404
stephen hemminger23312a32017-01-24 13:05:59 -0800405 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800406}
407
stephen hemminger0c195562017-08-01 19:58:53 -0700408/* Send skb on the slave VF device. */
409static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
410 struct sk_buff *skb)
411{
412 struct net_device_context *ndev_ctx = netdev_priv(net);
413 unsigned int len = skb->len;
414 int rc;
415
416 skb->dev = vf_netdev;
417 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
418
419 rc = dev_queue_xmit(skb);
420 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
421 struct netvsc_vf_pcpu_stats *pcpu_stats
422 = this_cpu_ptr(ndev_ctx->vf_stats);
423
424 u64_stats_update_begin(&pcpu_stats->syncp);
425 pcpu_stats->tx_packets++;
426 pcpu_stats->tx_bytes += len;
427 u64_stats_update_end(&pcpu_stats->syncp);
428 } else {
429 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
430 }
431
432 return rc;
433}
434
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700435static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700436{
Hank Janssenfceaf242009-07-13 15:34:54 -0700437 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200438 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700439 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800440 unsigned int num_data_pgs;
441 struct rndis_message *rndis_msg;
442 struct rndis_packet *rndis_pkt;
stephen hemminger0c195562017-08-01 19:58:53 -0700443 struct net_device *vf_netdev;
KY Srinivasan8a002512014-03-08 19:23:14 -0800444 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800445 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700446 u32 hash;
stephen hemminger02b6de02017-07-28 08:59:44 -0700447 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
Hank Janssenfceaf242009-07-13 15:34:54 -0700448
stephen hemminger0c195562017-08-01 19:58:53 -0700449 /* if VF is present and up then redirect packets
450 * already called with rcu_read_lock_bh
451 */
452 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
453 if (vf_netdev && netif_running(vf_netdev) &&
454 !netpoll_tx_running(net))
455 return netvsc_vf_xmit(net, vf_netdev, skb);
456
stephen hemminger80d887d2017-07-24 21:03:19 -0700457 /* We will atmost need two pages to describe the rndis
458 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200459 * of pages in a single packet. If skb is scattered around
460 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800461 */
stephen hemminger80d887d2017-07-24 21:03:19 -0700462
463 num_data_pgs = netvsc_get_slots(skb) + 2;
464
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700465 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700466 ++net_device_ctx->eth_stats.tx_scattered;
467
468 if (skb_linearize(skb))
469 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700470
stephen hemminger80d887d2017-07-24 21:03:19 -0700471 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700472 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700473 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700474 goto drop;
475 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800476 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700477
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800478 /*
479 * Place the rndis header in the skb head room and
480 * the skb->cb will be used for hv_netvsc_packet
481 * structure.
482 */
483 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700484 if (ret)
485 goto no_memory;
486
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800487 /* Use the skb control buffer for building up the packet */
488 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
489 FIELD_SIZEOF(struct sk_buff, cb));
490 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700491
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700492 packet->q_idx = skb_get_queue_mapping(skb);
493
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800494 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800495 packet->total_bytes = skb->len;
496 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700497
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800498 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700499
KY Srinivasan24476762015-12-01 16:43:06 -0800500 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700501
KY Srinivasan8a002512014-03-08 19:23:14 -0800502 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800503 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
504 rndis_msg->msg_len = packet->total_data_buflen;
505 rndis_pkt = &rndis_msg->msg.pkt;
506 rndis_pkt->data_offset = sizeof(struct rndis_packet);
507 rndis_pkt->data_len = packet->total_data_buflen;
508 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
509
510 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
511
Haiyang Zhang307f0992014-05-21 12:55:39 -0700512 hash = skb_get_hash_raw(skb);
513 if (hash != 0 && net->real_num_tx_queues > 1) {
514 rndis_msg_size += NDIS_HASH_PPI_SIZE;
515 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
516 NBL_HASH_VALUE);
517 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
518 }
519
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700520 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800521 struct ndis_pkt_8021q_info *vlan;
522
523 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
524 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
525 IEEE_8021Q_INFO);
526 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
527 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800528 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
529 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800530 VLAN_PRIO_SHIFT;
531 }
532
stephen hemminger23312a32017-01-24 13:05:59 -0800533 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700534 struct ndis_tcp_lso_info *lso_info;
535
536 rndis_msg_size += NDIS_LSO_PPI_SIZE;
537 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
538 TCP_LARGESEND_PKTINFO);
539
540 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
541 ppi->ppi_offset);
542
543 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800544 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700545 lso_info->lso_v2_transmit.ip_version =
546 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
547 ip_hdr(skb)->tot_len = 0;
548 ip_hdr(skb)->check = 0;
549 tcp_hdr(skb)->check =
550 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
551 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
552 } else {
553 lso_info->lso_v2_transmit.ip_version =
554 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
555 ipv6_hdr(skb)->payload_len = 0;
556 tcp_hdr(skb)->check =
557 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
558 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
559 }
stephen hemminger23312a32017-01-24 13:05:59 -0800560 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700561 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700562 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800563 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
564 struct ndis_tcp_ip_checksum_info *csum_info;
565
stephen hemmingerad19bc82016-10-11 14:03:07 -0700566 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
567 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
568 TCPIP_CHKSUM_PKTINFO);
569
570 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
571 ppi->ppi_offset);
572
stephen hemminger23312a32017-01-24 13:05:59 -0800573 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
574
575 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700576 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800577
578 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
579 csum_info->transmit.tcp_checksum = 1;
580 else
581 csum_info->transmit.udp_checksum = 1;
582 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700583 csum_info->transmit.is_ipv6 = 1;
584
stephen hemminger23312a32017-01-24 13:05:59 -0800585 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
586 csum_info->transmit.tcp_checksum = 1;
587 else
588 csum_info->transmit.udp_checksum = 1;
589 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700590 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800591 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700592 if (skb_checksum_help(skb))
593 goto drop;
594 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700595 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800596
KY Srinivasan8a002512014-03-08 19:23:14 -0800597 /* Start filling in the page buffers with the rndis hdr */
598 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700599 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800600 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
stephen hemminger02b6de02017-07-28 08:59:44 -0700601 skb, packet, pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800602
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800603 /* timestamp packet in software */
604 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700605
stephen hemminger02b6de02017-07-28 08:59:44 -0700606 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800607 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700608 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700609
610 if (ret == -EAGAIN) {
611 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700612 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700613 }
614
615 if (ret == -ENOSPC)
616 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700617
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700618drop:
619 dev_kfree_skb_any(skb);
620 net->stats.tx_dropped++;
621
622 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700623
624no_memory:
625 ++net_device_ctx->eth_stats.tx_no_memory;
626 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700627}
Hank Janssen3e189512010-03-04 22:11:00 +0000628/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700629 * netvsc_linkstatus_callback - Link up/down notification
630 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700631void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700632 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700633{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700634 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700635 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700636 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100637 struct netvsc_reconfig *event;
638 unsigned long flags;
639
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700640 net = hv_get_drvdata(device_obj);
641
642 if (!net)
643 return;
644
645 ndev_ctx = netdev_priv(net);
646
647 /* Update the physical link speed when changing to another vSwitch */
648 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
649 u32 speed;
650
651 speed = *(u32 *)((void *)indicate + indicate->
652 status_buf_offset) / 10000;
653 ndev_ctx->speed = speed;
654 return;
655 }
656
657 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100658 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
659 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
660 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
661 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700662
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700663 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700664 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700665
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100666 event = kzalloc(sizeof(*event), GFP_ATOMIC);
667 if (!event)
668 return;
669 event->event = indicate->status;
670
671 spin_lock_irqsave(&ndev_ctx->lock, flags);
672 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
673 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
674
675 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700676}
677
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700678static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800679 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800680 const struct ndis_tcp_ip_checksum_info *csum_info,
681 const struct ndis_pkt_8021q_info *vlan,
682 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700683{
Hank Janssenfceaf242009-07-13 15:34:54 -0700684 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700685
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800686 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700687 if (!skb)
688 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700689
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700690 /*
691 * Copy to skb. This copy is needed here since the memory pointed by
692 * hv_netvsc_packet cannot be deallocated
693 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200694 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700695
696 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700697
698 /* skb is already created with CHECKSUM_NONE */
699 skb_checksum_none_assert(skb);
700
701 /*
702 * In Linux, the IP checksum is always checked.
703 * Do L4 checksum offload if enabled and present.
704 */
705 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
706 if (csum_info->receive.tcp_checksum_succeeded ||
707 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800708 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800709 }
710
stephen hemmingerdc54a082017-01-24 13:06:08 -0800711 if (vlan) {
712 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
713
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700714 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800715 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800716 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700717
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700718 return skb;
719}
720
721/*
722 * netvsc_recv_callback - Callback when we receive a packet from the
723 * "wire" on the specified device.
724 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800725int netvsc_recv_callback(struct net_device *net,
726 struct vmbus_channel *channel,
727 void *data, u32 len,
728 const struct ndis_tcp_ip_checksum_info *csum_info,
729 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700730{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200731 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700732 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800733 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700734 struct netvsc_channel *nvchan;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700735 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700736 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700737
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700738 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700739 return NVSP_STAT_FAIL;
740
stephen hemminger0719e722017-01-11 09:16:32 -0800741 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700742 net_device = rcu_dereference(net_device_ctx->nvdev);
743 if (unlikely(!net_device))
744 goto drop;
745
746 nvchan = &net_device->chan_table[q_idx];
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700747
748 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800749 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
750 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700751 if (unlikely(!skb)) {
stephen hemminger545a8e72017-03-22 14:51:00 -0700752drop:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700753 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800754 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700755 return NVSP_STAT_FAIL;
756 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700757
stephen hemminger0c195562017-08-01 19:58:53 -0700758 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700759
760 /*
761 * Even if injecting the packet, record the statistics
762 * on the synthetic device because modifying the VF device
763 * statistics will not work correctly.
764 */
stephen hemminger742fe542017-02-27 10:26:50 -0800765 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700766 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700767 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800768 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700769
770 if (skb->pkt_type == PACKET_BROADCAST)
771 ++rx_stats->broadcast;
772 else if (skb->pkt_type == PACKET_MULTICAST)
773 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700774 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800775
stephen hemminger742fe542017-02-27 10:26:50 -0800776 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800777 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700778
Hank Janssenfceaf242009-07-13 15:34:54 -0700779 return 0;
780}
781
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700782static void netvsc_get_drvinfo(struct net_device *net,
783 struct ethtool_drvinfo *info)
784{
Jiri Pirko7826d432013-01-06 00:44:26 +0000785 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000786 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700787}
788
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800789static void netvsc_get_channels(struct net_device *net,
790 struct ethtool_channels *channel)
791{
792 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700793 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800794
795 if (nvdev) {
796 channel->max_combined = nvdev->max_chn;
797 channel->combined_count = nvdev->num_chn;
798 }
799}
800
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700801static int netvsc_set_channels(struct net_device *net,
802 struct ethtool_channels *channels)
803{
804 struct net_device_context *net_device_ctx = netdev_priv(net);
805 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700806 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700807 unsigned int orig, count = channels->combined_count;
808 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700809 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700810 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800811
812 /* We do not support separate count for rx, tx, or other */
813 if (count == 0 ||
814 channels->rx_count || channels->tx_count || channels->other_count)
815 return -EINVAL;
816
Arnd Bergmannb92b7d32017-06-22 00:16:37 +0200817 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
stephen hemminger2b018882017-01-24 13:06:03 -0800818 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700819
stephen hemmingera0be4502017-03-22 14:51:01 -0700820 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700821 return -ENODEV;
822
stephen hemminger2b018882017-01-24 13:06:03 -0800823 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700824 return -EINVAL;
825
stephen hemminger2b018882017-01-24 13:06:03 -0800826 if (count > nvdev->max_chn)
827 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700828
stephen hemminger7ca45932017-07-24 10:57:28 -0700829 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700830 was_opened = rndis_filter_opened(nvdev);
831 if (was_opened)
832 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700833
stephen hemminger2289f0a2017-01-24 13:06:10 -0800834 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700835
stephen hemminger7ca45932017-07-24 10:57:28 -0700836 memset(&device_info, 0, sizeof(device_info));
837 device_info.num_chn = count;
838 device_info.ring_size = ring_size;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700839
stephen hemminger7ca45932017-07-24 10:57:28 -0700840 nvdev = rndis_filter_device_add(dev, &device_info);
841 if (!IS_ERR(nvdev)) {
842 netif_set_real_num_tx_queues(net, nvdev->num_chn);
843 netif_set_real_num_rx_queues(net, nvdev->num_chn);
stephen hemminger7ca45932017-07-24 10:57:28 -0700844 } else {
stephen hemmingerd6aac1f2017-07-28 08:59:41 -0700845 ret = PTR_ERR(nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700846 device_info.num_chn = orig;
stephen hemminger7ca45932017-07-24 10:57:28 -0700847 rndis_filter_device_add(dev, &device_info);
848 }
849
stephen hemmingerea383bf2017-07-19 11:53:15 -0700850 if (was_opened)
851 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700852
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200853 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700854 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200855 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700856
857 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700858}
859
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100860static bool
861netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800862{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100863 struct ethtool_link_ksettings diff1 = *cmd;
864 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800865
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100866 diff1.base.speed = 0;
867 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800868 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100869 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
870 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800871 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100872 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800873
874 return !memcmp(&diff1, &diff2, sizeof(diff1));
875}
876
877static void netvsc_init_settings(struct net_device *dev)
878{
879 struct net_device_context *ndc = netdev_priv(dev);
880
881 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700882 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800883}
884
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100885static int netvsc_get_link_ksettings(struct net_device *dev,
886 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800887{
888 struct net_device_context *ndc = netdev_priv(dev);
889
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100890 cmd->base.speed = ndc->speed;
891 cmd->base.duplex = ndc->duplex;
892 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800893
894 return 0;
895}
896
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100897static int netvsc_set_link_ksettings(struct net_device *dev,
898 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800899{
900 struct net_device_context *ndc = netdev_priv(dev);
901 u32 speed;
902
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100903 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800904 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100905 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800906 !netvsc_validate_ethtool_ss_cmd(cmd))
907 return -EINVAL;
908
909 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100910 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800911
912 return 0;
913}
914
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800915static int netvsc_change_mtu(struct net_device *ndev, int mtu)
916{
917 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -0700918 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700919 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200920 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700921 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800922 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700923 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700924 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800925
stephen hemmingera0be4502017-03-22 14:51:01 -0700926 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800927 return -ENODEV;
928
stephen hemminger0c195562017-08-01 19:58:53 -0700929 /* Change MTU of underlying VF netdev first. */
930 if (vf_netdev) {
931 ret = dev_set_mtu(vf_netdev, mtu);
932 if (ret)
933 return ret;
934 }
935
stephen hemmingerea383bf2017-07-19 11:53:15 -0700936 netif_device_detach(ndev);
937 was_opened = rndis_filter_opened(nvdev);
938 if (was_opened)
939 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700940
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700941 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800942 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800943 device_info.num_chn = nvdev->num_chn;
Dexuan Cui152669b2017-03-02 13:00:53 +0000944
Dexuan Cui152669b2017-03-02 13:00:53 +0000945 rndis_filter_device_remove(hdev, nvdev);
946
Dexuan Cui152669b2017-03-02 13:00:53 +0000947 ndev->mtu = mtu;
948
stephen hemminger9749fed2017-07-19 11:53:16 -0700949 nvdev = rndis_filter_device_add(hdev, &device_info);
950 if (IS_ERR(nvdev)) {
951 ret = PTR_ERR(nvdev);
952
953 /* Attempt rollback to original MTU */
954 ndev->mtu = orig_mtu;
955 rndis_filter_device_add(hdev, &device_info);
stephen hemminger0c195562017-08-01 19:58:53 -0700956
957 if (vf_netdev)
958 dev_set_mtu(vf_netdev, orig_mtu);
stephen hemminger9749fed2017-07-19 11:53:16 -0700959 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800960
stephen hemmingerea383bf2017-07-19 11:53:15 -0700961 if (was_opened)
962 rndis_filter_open(nvdev);
963
964 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700965
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200966 /* We may have missed link change notifications */
967 schedule_delayed_work(&ndevctx->dwork, 0);
968
stephen hemminger9749fed2017-07-19 11:53:16 -0700969 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800970}
971
stephen hemminger0c195562017-08-01 19:58:53 -0700972static void netvsc_get_vf_stats(struct net_device *net,
973 struct netvsc_vf_pcpu_stats *tot)
974{
975 struct net_device_context *ndev_ctx = netdev_priv(net);
976 int i;
977
978 memset(tot, 0, sizeof(*tot));
979
980 for_each_possible_cpu(i) {
981 const struct netvsc_vf_pcpu_stats *stats
982 = per_cpu_ptr(ndev_ctx->vf_stats, i);
983 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
984 unsigned int start;
985
986 do {
987 start = u64_stats_fetch_begin_irq(&stats->syncp);
988 rx_packets = stats->rx_packets;
989 tx_packets = stats->tx_packets;
990 rx_bytes = stats->rx_bytes;
991 tx_bytes = stats->tx_bytes;
992 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
993
994 tot->rx_packets += rx_packets;
995 tot->tx_packets += tx_packets;
996 tot->rx_bytes += rx_bytes;
997 tot->tx_bytes += tx_bytes;
998 tot->tx_dropped += stats->tx_dropped;
999 }
1000}
1001
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001002static void netvsc_get_stats64(struct net_device *net,
1003 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001004{
1005 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -07001006 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001007 struct netvsc_vf_pcpu_stats vf_tot;
1008 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001009
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001010 if (!nvdev)
1011 return;
1012
stephen hemminger0c195562017-08-01 19:58:53 -07001013 netdev_stats_to_stats64(t, &net->stats);
1014
1015 netvsc_get_vf_stats(net, &vf_tot);
1016 t->rx_packets += vf_tot.rx_packets;
1017 t->tx_packets += vf_tot.tx_packets;
1018 t->rx_bytes += vf_tot.rx_bytes;
1019 t->tx_bytes += vf_tot.tx_bytes;
1020 t->tx_dropped += vf_tot.tx_dropped;
1021
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001022 for (i = 0; i < nvdev->num_chn; i++) {
1023 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1024 const struct netvsc_stats *stats;
1025 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001026 unsigned int start;
1027
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001028 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001029 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001030 start = u64_stats_fetch_begin_irq(&stats->syncp);
1031 packets = stats->packets;
1032 bytes = stats->bytes;
1033 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001034
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001035 t->tx_bytes += bytes;
1036 t->tx_packets += packets;
1037
1038 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001039 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001040 start = u64_stats_fetch_begin_irq(&stats->syncp);
1041 packets = stats->packets;
1042 bytes = stats->bytes;
1043 multicast = stats->multicast + stats->broadcast;
1044 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001045
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001046 t->rx_bytes += bytes;
1047 t->rx_packets += packets;
1048 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001049 }
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001050}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001051
1052static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1053{
stephen hemminger867047c2017-07-28 08:59:42 -07001054 struct net_device_context *ndc = netdev_priv(ndev);
1055 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001056 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +00001057 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001058 unsigned char save_aatype;
1059 int err;
1060
1061 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
1062 save_aatype = ndev->addr_assign_type;
1063
1064 err = eth_mac_addr(ndev, p);
1065 if (err != 0)
1066 return err;
1067
stephen hemminger867047c2017-07-28 08:59:42 -07001068 if (!nvdev)
1069 return -ENODEV;
1070
1071 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001072 if (err != 0) {
1073 /* roll back to saved MAC */
1074 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
1075 ndev->addr_assign_type = save_aatype;
1076 }
1077
1078 return err;
1079}
1080
Stephen Hemminger4323b472016-08-23 12:17:57 -07001081static const struct {
1082 char name[ETH_GSTRING_LEN];
1083 u16 offset;
1084} netvsc_stats[] = {
1085 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1086 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1087 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1088 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1089 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
stephen hemminger0c195562017-08-01 19:58:53 -07001090}, vf_stats[] = {
1091 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1092 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1093 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1094 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1095 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001096};
1097
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001098#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
stephen hemminger0c195562017-08-01 19:58:53 -07001099#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001100
1101/* 4 statistics per queue (rx/tx packets/bytes) */
1102#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1103
Stephen Hemminger4323b472016-08-23 12:17:57 -07001104static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1105{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001106 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -07001107 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001108
1109 if (!nvdev)
1110 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001111
Stephen Hemminger4323b472016-08-23 12:17:57 -07001112 switch (string_set) {
1113 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001114 return NETVSC_GLOBAL_STATS_LEN
1115 + NETVSC_VF_STATS_LEN
1116 + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001117 default:
1118 return -EINVAL;
1119 }
1120}
1121
1122static void netvsc_get_ethtool_stats(struct net_device *dev,
1123 struct ethtool_stats *stats, u64 *data)
1124{
1125 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001126 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001127 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001128 const struct netvsc_stats *qstats;
stephen hemminger0c195562017-08-01 19:58:53 -07001129 struct netvsc_vf_pcpu_stats sum;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001130 unsigned int start;
1131 u64 packets, bytes;
1132 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001133
stephen hemminger545a8e72017-03-22 14:51:00 -07001134 if (!nvdev)
1135 return;
1136
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001137 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001138 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001139
stephen hemminger0c195562017-08-01 19:58:53 -07001140 netvsc_get_vf_stats(dev, &sum);
1141 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1142 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1143
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001144 for (j = 0; j < nvdev->num_chn; j++) {
1145 qstats = &nvdev->chan_table[j].tx_stats;
1146
1147 do {
1148 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1149 packets = qstats->packets;
1150 bytes = qstats->bytes;
1151 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1152 data[i++] = packets;
1153 data[i++] = bytes;
1154
1155 qstats = &nvdev->chan_table[j].rx_stats;
1156 do {
1157 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1158 packets = qstats->packets;
1159 bytes = qstats->bytes;
1160 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1161 data[i++] = packets;
1162 data[i++] = bytes;
1163 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001164}
1165
1166static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1167{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001168 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001169 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001170 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001171 int i;
1172
stephen hemminger545a8e72017-03-22 14:51:00 -07001173 if (!nvdev)
1174 return;
1175
Stephen Hemminger4323b472016-08-23 12:17:57 -07001176 switch (stringset) {
1177 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001178 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1179 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1180 p += ETH_GSTRING_LEN;
1181 }
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001182
stephen hemminger0c195562017-08-01 19:58:53 -07001183 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1184 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1185 p += ETH_GSTRING_LEN;
1186 }
1187
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001188 for (i = 0; i < nvdev->num_chn; i++) {
1189 sprintf(p, "tx_queue_%u_packets", i);
1190 p += ETH_GSTRING_LEN;
1191 sprintf(p, "tx_queue_%u_bytes", i);
1192 p += ETH_GSTRING_LEN;
1193 sprintf(p, "rx_queue_%u_packets", i);
1194 p += ETH_GSTRING_LEN;
1195 sprintf(p, "rx_queue_%u_bytes", i);
1196 p += ETH_GSTRING_LEN;
1197 }
1198
Stephen Hemminger4323b472016-08-23 12:17:57 -07001199 break;
1200 }
1201}
1202
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001203static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001204netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1205 struct ethtool_rxnfc *info)
1206{
1207 info->data = RXH_IP_SRC | RXH_IP_DST;
1208
1209 switch (info->flow_type) {
1210 case TCP_V4_FLOW:
1211 case TCP_V6_FLOW:
1212 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1213 /* fallthrough */
1214 case UDP_V4_FLOW:
1215 case UDP_V6_FLOW:
1216 case IPV4_FLOW:
1217 case IPV6_FLOW:
1218 break;
1219 default:
1220 info->data = 0;
1221 break;
1222 }
1223
1224 return 0;
1225}
1226
1227static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001228netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1229 u32 *rules)
1230{
1231 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001232 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001233
1234 if (!nvdev)
1235 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001236
1237 switch (info->cmd) {
1238 case ETHTOOL_GRXRINGS:
1239 info->data = nvdev->num_chn;
1240 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001241
1242 case ETHTOOL_GRXFH:
1243 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001244 }
1245 return -EOPNOTSUPP;
1246}
1247
Richard Weinberger316158f2014-07-09 16:23:59 +02001248#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001249static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001250{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001251 struct net_device_context *ndc = netdev_priv(dev);
1252 struct netvsc_device *ndev;
1253 int i;
1254
1255 rcu_read_lock();
1256 ndev = rcu_dereference(ndc->nvdev);
1257 if (ndev) {
1258 for (i = 0; i < ndev->num_chn; i++) {
1259 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1260
1261 napi_schedule(&nvchan->napi);
1262 }
1263 }
1264 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001265}
1266#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001267
stephen hemminger962f3fe2017-01-24 13:06:02 -08001268static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1269{
1270 return NETVSC_HASH_KEYLEN;
1271}
1272
1273static u32 netvsc_rss_indir_size(struct net_device *dev)
1274{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001275 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001276}
1277
1278static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1279 u8 *hfunc)
1280{
1281 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001282 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001283 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001284 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001285
stephen hemminger545a8e72017-03-22 14:51:00 -07001286 if (!ndev)
1287 return -ENODEV;
1288
stephen hemminger962f3fe2017-01-24 13:06:02 -08001289 if (hfunc)
1290 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1291
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001292 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001293 if (indir) {
1294 for (i = 0; i < ITAB_NUM; i++)
1295 indir[i] = rndis_dev->ind_table[i];
1296 }
1297
stephen hemminger962f3fe2017-01-24 13:06:02 -08001298 if (key)
1299 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1300
1301 return 0;
1302}
1303
1304static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1305 const u8 *key, const u8 hfunc)
1306{
1307 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001308 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001309 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001310 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001311
stephen hemminger545a8e72017-03-22 14:51:00 -07001312 if (!ndev)
1313 return -ENODEV;
1314
stephen hemminger962f3fe2017-01-24 13:06:02 -08001315 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1316 return -EOPNOTSUPP;
1317
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001318 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001319 if (indir) {
1320 for (i = 0; i < ITAB_NUM; i++)
Arnd Bergmannb92b7d32017-06-22 00:16:37 +02001321 if (indir[i] >= VRSS_CHANNEL_MAX)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001322 return -EINVAL;
1323
1324 for (i = 0; i < ITAB_NUM; i++)
1325 rndis_dev->ind_table[i] = indir[i];
1326 }
1327
1328 if (!key) {
1329 if (!indir)
1330 return 0;
1331
1332 key = rndis_dev->rss_key;
1333 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001334
1335 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1336}
1337
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001338static const struct ethtool_ops ethtool_ops = {
1339 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001340 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001341 .get_ethtool_stats = netvsc_get_ethtool_stats,
1342 .get_sset_count = netvsc_get_sset_count,
1343 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001344 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001345 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001346 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001347 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001348 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1349 .get_rxfh_indir_size = netvsc_rss_indir_size,
1350 .get_rxfh = netvsc_get_rxfh,
1351 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001352 .get_link_ksettings = netvsc_get_link_ksettings,
1353 .set_link_ksettings = netvsc_set_link_ksettings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001354};
1355
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001356static const struct net_device_ops device_ops = {
1357 .ndo_open = netvsc_open,
1358 .ndo_stop = netvsc_close,
1359 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001360 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001361 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001362 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001363 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001364 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001365 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001366#ifdef CONFIG_NET_POLL_CONTROLLER
1367 .ndo_poll_controller = netvsc_poll_controller,
1368#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001369};
1370
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001371/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001372 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1373 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1374 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001375 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001376static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001377{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001378 struct net_device_context *ndev_ctx =
1379 container_of(w, struct net_device_context, dwork.work);
1380 struct hv_device *device_obj = ndev_ctx->device_ctx;
1381 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001382 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001383 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001384 struct netvsc_reconfig *event = NULL;
1385 bool notify = false, reschedule = false;
1386 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001387
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001388 rtnl_lock();
stephen hemmingera0be4502017-03-22 14:51:01 -07001389 net_device = rtnl_dereference(ndev_ctx->nvdev);
1390 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001391 goto out_unlock;
1392
Haiyang Zhang891de742014-02-12 16:54:27 -08001393 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001394
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001395 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1396 if (time_is_after_jiffies(next_reconfig)) {
1397 /* link_watch only sends one notification with current state
1398 * per second, avoid doing reconfig more frequently. Handle
1399 * wrap around.
1400 */
1401 delay = next_reconfig - jiffies;
1402 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1403 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001404 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001405 }
1406 ndev_ctx->last_reconfig = jiffies;
1407
1408 spin_lock_irqsave(&ndev_ctx->lock, flags);
1409 if (!list_empty(&ndev_ctx->reconfig_events)) {
1410 event = list_first_entry(&ndev_ctx->reconfig_events,
1411 struct netvsc_reconfig, list);
1412 list_del(&event->list);
1413 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1414 }
1415 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1416
1417 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001418 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001419
1420 switch (event->event) {
1421 /* Only the following events are possible due to the check in
1422 * netvsc_linkstatus_callback()
1423 */
1424 case RNDIS_STATUS_MEDIA_CONNECT:
1425 if (rdev->link_state) {
1426 rdev->link_state = false;
stephen hemminger0c195562017-08-01 19:58:53 -07001427 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001428 netif_tx_wake_all_queues(net);
1429 } else {
1430 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001431 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001432 kfree(event);
1433 break;
1434 case RNDIS_STATUS_MEDIA_DISCONNECT:
1435 if (!rdev->link_state) {
1436 rdev->link_state = true;
1437 netif_carrier_off(net);
1438 netif_tx_stop_all_queues(net);
1439 }
1440 kfree(event);
1441 break;
1442 case RNDIS_STATUS_NETWORK_CHANGE:
1443 /* Only makes sense if carrier is present */
1444 if (!rdev->link_state) {
1445 rdev->link_state = true;
1446 netif_carrier_off(net);
1447 netif_tx_stop_all_queues(net);
1448 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1449 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001450 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001451 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1452 reschedule = true;
1453 }
1454 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001455 }
1456
1457 rtnl_unlock();
1458
1459 if (notify)
1460 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001461
1462 /* link_watch only sends one notification with current state per
1463 * second, handle next reconfig event in 2 seconds.
1464 */
1465 if (reschedule)
1466 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001467
1468 return;
1469
1470out_unlock:
1471 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001472}
1473
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001474static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001475{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001476 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001477
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001478 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001479
1480 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001481 if (dev->netdev_ops != &device_ops)
1482 continue; /* not a netvsc device */
1483
1484 if (ether_addr_equal(mac, dev->perm_addr))
1485 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001486 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001487
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001488 return NULL;
1489}
1490
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001491static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001492{
1493 struct net_device *dev;
1494
1495 ASSERT_RTNL();
1496
1497 for_each_netdev(&init_net, dev) {
1498 struct net_device_context *net_device_ctx;
1499
1500 if (dev->netdev_ops != &device_ops)
1501 continue; /* not a netvsc device */
1502
1503 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001504 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001505 continue; /* device is removed */
1506
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001507 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001508 return dev; /* a match */
1509 }
1510
1511 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001512}
1513
stephen hemminger0c195562017-08-01 19:58:53 -07001514/* Called when VF is injecting data into network stack.
1515 * Change the associated network device from VF to netvsc.
1516 * note: already called with rcu_read_lock
1517 */
1518static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1519{
1520 struct sk_buff *skb = *pskb;
1521 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1522 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1523 struct netvsc_vf_pcpu_stats *pcpu_stats
1524 = this_cpu_ptr(ndev_ctx->vf_stats);
1525
1526 skb->dev = ndev;
1527
1528 u64_stats_update_begin(&pcpu_stats->syncp);
1529 pcpu_stats->rx_packets++;
1530 pcpu_stats->rx_bytes += skb->len;
1531 u64_stats_update_end(&pcpu_stats->syncp);
1532
1533 return RX_HANDLER_ANOTHER;
1534}
1535
1536static int netvsc_vf_join(struct net_device *vf_netdev,
1537 struct net_device *ndev)
1538{
1539 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1540 int ret;
1541
1542 ret = netdev_rx_handler_register(vf_netdev,
1543 netvsc_vf_handle_frame, ndev);
1544 if (ret != 0) {
1545 netdev_err(vf_netdev,
1546 "can not register netvsc VF receive handler (err = %d)\n",
1547 ret);
1548 goto rx_handler_failed;
1549 }
1550
1551 ret = netdev_upper_dev_link(vf_netdev, ndev);
1552 if (ret != 0) {
1553 netdev_err(vf_netdev,
1554 "can not set master device %s (err = %d)\n",
1555 ndev->name, ret);
1556 goto upper_link_failed;
1557 }
1558
1559 /* set slave flag before open to prevent IPv6 addrconf */
1560 vf_netdev->flags |= IFF_SLAVE;
1561
1562 schedule_work(&ndev_ctx->vf_takeover);
1563
1564 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1565 return 0;
1566
1567upper_link_failed:
1568 netdev_rx_handler_unregister(vf_netdev);
1569rx_handler_failed:
1570 return ret;
1571}
1572
1573static void __netvsc_vf_setup(struct net_device *ndev,
1574 struct net_device *vf_netdev)
1575{
1576 int ret;
1577
1578 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1579
1580 /* Align MTU of VF with master */
1581 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1582 if (ret)
1583 netdev_warn(vf_netdev,
1584 "unable to change mtu to %u\n", ndev->mtu);
1585
1586 if (netif_running(ndev)) {
1587 ret = dev_open(vf_netdev);
1588 if (ret)
1589 netdev_warn(vf_netdev,
1590 "unable to open: %d\n", ret);
1591 }
1592}
1593
1594/* Setup VF as slave of the synthetic device.
1595 * Runs in workqueue to avoid recursion in netlink callbacks.
1596 */
1597static void netvsc_vf_setup(struct work_struct *w)
1598{
1599 struct net_device_context *ndev_ctx
1600 = container_of(w, struct net_device_context, vf_takeover);
1601 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1602 struct net_device *vf_netdev;
1603
stephen hemmingerfb84af82017-08-04 12:14:00 -07001604 if (!rtnl_trylock()) {
1605 schedule_work(w);
1606 return;
1607 }
1608
stephen hemminger0c195562017-08-01 19:58:53 -07001609 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1610 if (vf_netdev)
1611 __netvsc_vf_setup(ndev, vf_netdev);
1612
1613 rtnl_unlock();
1614}
1615
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001616static int netvsc_register_vf(struct net_device *vf_netdev)
1617{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001618 struct net_device *ndev;
1619 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001620 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001621
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001622 if (vf_netdev->addr_len != ETH_ALEN)
1623 return NOTIFY_DONE;
1624
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001625 /*
1626 * We will use the MAC address to locate the synthetic interface to
1627 * associate with the VF interface. If we don't find a matching
1628 * synthetic interface, move on.
1629 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001630 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001631 if (!ndev)
1632 return NOTIFY_DONE;
1633
1634 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001635 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001636 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001637 return NOTIFY_DONE;
1638
stephen hemminger0c195562017-08-01 19:58:53 -07001639 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1640 return NOTIFY_DONE;
1641
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001642 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
stephen hemminger0c195562017-08-01 19:58:53 -07001643
1644 /* Prevent this module from being unloaded while VF is registered */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001645 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001646
1647 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001648 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001649 return NOTIFY_OK;
1650}
1651
stephen hemminger7b83f522017-08-07 11:30:00 -07001652static int netvsc_vf_up(struct net_device *vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001653{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001654 struct net_device_context *net_device_ctx;
stephen hemminger7b83f522017-08-07 11:30:00 -07001655 struct netvsc_device *netvsc_dev;
stephen hemminger0c195562017-08-01 19:58:53 -07001656 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001657
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001658 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001659 if (!ndev)
1660 return NOTIFY_DONE;
1661
1662 net_device_ctx = netdev_priv(ndev);
stephen hemminger7b83f522017-08-07 11:30:00 -07001663 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1664 if (!netvsc_dev)
1665 return NOTIFY_DONE;
1666
1667 /* Bump refcount when datapath is acvive - Why? */
1668 rndis_filter_open(netvsc_dev);
1669
1670 /* notify the host to switch the data path. */
1671 netvsc_switch_datapath(ndev, true);
1672 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1673
1674 return NOTIFY_OK;
1675}
1676
1677static int netvsc_vf_down(struct net_device *vf_netdev)
1678{
1679 struct net_device_context *net_device_ctx;
1680 struct netvsc_device *netvsc_dev;
1681 struct net_device *ndev;
1682
1683 ndev = get_netvsc_byref(vf_netdev);
1684 if (!ndev)
1685 return NOTIFY_DONE;
1686
1687 net_device_ctx = netdev_priv(ndev);
1688 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1689 if (!netvsc_dev)
1690 return NOTIFY_DONE;
1691
1692 netvsc_switch_datapath(ndev, false);
1693 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1694 rndis_filter_close(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001695
1696 return NOTIFY_OK;
1697}
1698
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001699static int netvsc_unregister_vf(struct net_device *vf_netdev)
1700{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001701 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001702 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001703
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001704 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001705 if (!ndev)
1706 return NOTIFY_DONE;
1707
1708 net_device_ctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -07001709 cancel_work_sync(&net_device_ctx->vf_takeover);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001710
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001711 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001712
stephen hemminger0c195562017-08-01 19:58:53 -07001713 netdev_upper_dev_unlink(vf_netdev, ndev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001714 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001715 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001716 module_put(THIS_MODULE);
1717 return NOTIFY_OK;
1718}
1719
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001720static int netvsc_probe(struct hv_device *dev,
1721 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001722{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001723 struct net_device *net = NULL;
1724 struct net_device_context *net_device_ctx;
1725 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001726 struct netvsc_device *nvdev;
stephen hemminger0c195562017-08-01 19:58:53 -07001727 int ret = -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001728
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001729 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001730 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001731 if (!net)
stephen hemminger0c195562017-08-01 19:58:53 -07001732 goto no_net;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001733
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001734 netif_carrier_off(net);
1735
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001736 netvsc_init_settings(net);
1737
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001738 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001739 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001740 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1741 if (netif_msg_probe(net_device_ctx))
1742 netdev_dbg(net, "netvsc msg_enable: %d\n",
1743 net_device_ctx->msg_enable);
1744
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001745 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001746
Haiyang Zhang891de742014-02-12 16:54:27 -08001747 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001748
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001749 spin_lock_init(&net_device_ctx->lock);
1750 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
stephen hemminger0c195562017-08-01 19:58:53 -07001751 INIT_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
stephen hemminger0c195562017-08-01 19:58:53 -07001752
1753 net_device_ctx->vf_stats
1754 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1755 if (!net_device_ctx->vf_stats)
1756 goto no_stats;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001757
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001758 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001759 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001760 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001761
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001762 /* We always need headroom for rndis header */
1763 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1764
Haiyang Zhang692e0842011-09-01 12:19:43 -07001765 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001766 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001767 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001768 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger9749fed2017-07-19 11:53:16 -07001769
1770 nvdev = rndis_filter_device_add(dev, &device_info);
1771 if (IS_ERR(nvdev)) {
1772 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001773 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
stephen hemminger0c195562017-08-01 19:58:53 -07001774 goto rndis_failed;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001775 }
stephen hemminger0c195562017-08-01 19:58:53 -07001776
Haiyang Zhang692e0842011-09-01 12:19:43 -07001777 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1778
stephen hemminger23312a32017-01-24 13:05:59 -08001779 /* hw_features computed in rndis_filter_device_add */
1780 net->features = net->hw_features |
1781 NETIF_F_HIGHDMA | NETIF_F_SG |
1782 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1783 net->vlan_features = net->features;
1784
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001785 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1786 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001787
stephen hemminger9749fed2017-07-19 11:53:16 -07001788 netdev_lockdep_set_classes(net);
1789
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001790 /* MTU range: 68 - 1500 or 65521 */
1791 net->min_mtu = NETVSC_MTU_MIN;
1792 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1793 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1794 else
1795 net->max_mtu = ETH_DATA_LEN;
1796
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001797 ret = register_netdev(net);
1798 if (ret != 0) {
1799 pr_err("Unable to register netdev.\n");
stephen hemminger0c195562017-08-01 19:58:53 -07001800 goto register_failed;
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001801 }
1802
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001803 return ret;
stephen hemminger0c195562017-08-01 19:58:53 -07001804
1805register_failed:
1806 rndis_filter_device_remove(dev, nvdev);
1807rndis_failed:
1808 free_percpu(net_device_ctx->vf_stats);
1809no_stats:
1810 hv_set_drvdata(dev, NULL);
1811 free_netdev(net);
1812no_net:
1813 return ret;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001814}
1815
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001816static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001817{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001818 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001819 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001820
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001821 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001822
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001823 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001824 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001825 return 0;
1826 }
1827
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001828 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001829
stephen hemmingera0be4502017-03-22 14:51:01 -07001830 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001831
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001832 cancel_delayed_work_sync(&ndev_ctx->dwork);
1833
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001834 /*
1835 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07001836 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001837 */
stephen hemmingera0be4502017-03-22 14:51:01 -07001838 rtnl_lock();
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001839 rndis_filter_device_remove(dev,
1840 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07001841 rtnl_unlock();
1842
1843 unregister_netdev(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001844
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001845 hv_set_drvdata(dev, NULL);
1846
stephen hemminger0c195562017-08-01 19:58:53 -07001847 free_percpu(ndev_ctx->vf_stats);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001848 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001849 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001850}
1851
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001852static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001853 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001854 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001855 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001856};
1857
1858MODULE_DEVICE_TABLE(vmbus, id_table);
1859
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001860/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001861static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001862 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001863 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001864 .probe = netvsc_probe,
1865 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001866};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001867
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001868/*
1869 * On Hyper-V, every VF interface is matched with a corresponding
1870 * synthetic interface. The synthetic interface is presented first
1871 * to the guest. When the corresponding VF instance is registered,
1872 * we will take care of switching the data path.
1873 */
1874static int netvsc_netdev_event(struct notifier_block *this,
1875 unsigned long event, void *ptr)
1876{
1877 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1878
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001879 /* Skip our own events */
1880 if (event_dev->netdev_ops == &device_ops)
1881 return NOTIFY_DONE;
1882
1883 /* Avoid non-Ethernet type devices */
1884 if (event_dev->type != ARPHRD_ETHER)
1885 return NOTIFY_DONE;
1886
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001887 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06001888 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001889 return NOTIFY_DONE;
1890
1891 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001892 if ((event_dev->priv_flags & IFF_BONDING) &&
1893 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001894 return NOTIFY_DONE;
1895
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001896 switch (event) {
1897 case NETDEV_REGISTER:
1898 return netvsc_register_vf(event_dev);
1899 case NETDEV_UNREGISTER:
1900 return netvsc_unregister_vf(event_dev);
1901 case NETDEV_UP:
stephen hemminger7b83f522017-08-07 11:30:00 -07001902 return netvsc_vf_up(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001903 case NETDEV_DOWN:
stephen hemminger7b83f522017-08-07 11:30:00 -07001904 return netvsc_vf_down(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001905 default:
1906 return NOTIFY_DONE;
1907 }
1908}
1909
1910static struct notifier_block netvsc_netdev_notifier = {
1911 .notifier_call = netvsc_netdev_event,
1912};
1913
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001914static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001915{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001916 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001917 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001918}
1919
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001920static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001921{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001922 int ret;
1923
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001924 if (ring_size < RING_SIZE_MIN) {
1925 ring_size = RING_SIZE_MIN;
1926 pr_info("Increased ring_size to %d (min allowed)\n",
1927 ring_size);
1928 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001929 ret = vmbus_driver_register(&netvsc_drv);
1930
1931 if (ret)
1932 return ret;
1933
1934 register_netdevice_notifier(&netvsc_netdev_notifier);
1935 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001936}
1937
Hank Janssen26c14cc2010-02-11 23:02:42 +00001938MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001939MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001940
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001941module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001942module_exit(netvsc_drv_exit);