1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/init.h>
12 #include <linux/atomic.h>
13 #include <linux/ethtool.h>
14 #include <linux/module.h>
15 #include <linux/highmem.h>
16 #include <linux/device.h>
17 #include <linux/io.h>
18 #include <linux/delay.h>
19 #include <linux/netdevice.h>
20 #include <linux/inetdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/pci.h>
23 #include <linux/skbuff.h>
24 #include <linux/if_vlan.h>
25 #include <linux/in.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/netpoll.h>
29 #include <linux/bpf.h>
30
31 #include <net/arp.h>
32 #include <net/route.h>
33 #include <net/sock.h>
34 #include <net/pkt_sched.h>
35 #include <net/checksum.h>
36 #include <net/ip6_checksum.h>
37
38 #include "hyperv_net.h"
39
40 #define RING_SIZE_MIN 64
41
42 #define LINKCHANGE_INT (2 * HZ)
43 #define VF_TAKEOVER_INT (HZ / 10)
44
45 /* Macros to define the context of vf registration */
46 #define VF_REG_IN_PROBE 1
47 #define VF_REG_IN_NOTIFIER 2
48
49 static unsigned int ring_size __ro_after_init = 128;
50 module_param(ring_size, uint, 0444);
51 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of 4K pages)");
52 unsigned int netvsc_ring_bytes __ro_after_init;
53
54 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
55 NETIF_MSG_LINK | NETIF_MSG_IFUP |
56 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
57 NETIF_MSG_TX_ERR;
58
59 static int debug = -1;
60 module_param(debug, int, 0444);
61 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
62
63 static LIST_HEAD(netvsc_dev_list);
64
netvsc_change_rx_flags(struct net_device * net,int change)65 static void netvsc_change_rx_flags(struct net_device *net, int change)
66 {
67 struct net_device_context *ndev_ctx = netdev_priv(net);
68 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
69 int inc;
70
71 if (!vf_netdev)
72 return;
73
74 if (change & IFF_PROMISC) {
75 inc = (net->flags & IFF_PROMISC) ? 1 : -1;
76 dev_set_promiscuity(vf_netdev, inc);
77 }
78
79 if (change & IFF_ALLMULTI) {
80 inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
81 dev_set_allmulti(vf_netdev, inc);
82 }
83 }
84
netvsc_set_rx_mode(struct net_device * net)85 static void netvsc_set_rx_mode(struct net_device *net)
86 {
87 struct net_device_context *ndev_ctx = netdev_priv(net);
88 struct net_device *vf_netdev;
89 struct netvsc_device *nvdev;
90
91 rcu_read_lock();
92 vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
93 if (vf_netdev) {
94 dev_uc_sync(vf_netdev, net);
95 dev_mc_sync(vf_netdev, net);
96 }
97
98 nvdev = rcu_dereference(ndev_ctx->nvdev);
99 if (nvdev)
100 rndis_filter_update(nvdev);
101 rcu_read_unlock();
102 }
103
netvsc_tx_enable(struct netvsc_device * nvscdev,struct net_device * ndev)104 static void netvsc_tx_enable(struct netvsc_device *nvscdev,
105 struct net_device *ndev)
106 {
107 nvscdev->tx_disable = false;
108 virt_wmb(); /* ensure queue wake up mechanism is on */
109
110 netif_tx_wake_all_queues(ndev);
111 }
112
netvsc_open(struct net_device * net)113 static int netvsc_open(struct net_device *net)
114 {
115 struct net_device_context *ndev_ctx = netdev_priv(net);
116 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
117 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
118 struct rndis_device *rdev;
119 int ret = 0;
120
121 netif_carrier_off(net);
122
123 /* Open up the device */
124 ret = rndis_filter_open(nvdev);
125 if (ret != 0) {
126 netdev_err(net, "unable to open device (ret %d).\n", ret);
127 return ret;
128 }
129
130 rdev = nvdev->extension;
131 if (!rdev->link_state) {
132 netif_carrier_on(net);
133 netvsc_tx_enable(nvdev, net);
134 }
135
136 if (vf_netdev) {
137 /* Setting synthetic device up transparently sets
138 * slave as up. If open fails, then slave will be
139 * still be offline (and not used).
140 */
141 ret = dev_open(vf_netdev, NULL);
142 if (ret)
143 netdev_warn(net,
144 "unable to open slave: %s: %d\n",
145 vf_netdev->name, ret);
146 }
147 return 0;
148 }
149
netvsc_wait_until_empty(struct netvsc_device * nvdev)150 static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
151 {
152 unsigned int retry = 0;
153 int i;
154
155 /* Ensure pending bytes in ring are read */
156 for (;;) {
157 u32 aread = 0;
158
159 for (i = 0; i < nvdev->num_chn; i++) {
160 struct vmbus_channel *chn
161 = nvdev->chan_table[i].channel;
162
163 if (!chn)
164 continue;
165
166 /* make sure receive not running now */
167 napi_synchronize(&nvdev->chan_table[i].napi);
168
169 aread = hv_get_bytes_to_read(&chn->inbound);
170 if (aread)
171 break;
172
173 aread = hv_get_bytes_to_read(&chn->outbound);
174 if (aread)
175 break;
176 }
177
178 if (aread == 0)
179 return 0;
180
181 if (++retry > RETRY_MAX)
182 return -ETIMEDOUT;
183
184 usleep_range(RETRY_US_LO, RETRY_US_HI);
185 }
186 }
187
netvsc_tx_disable(struct netvsc_device * nvscdev,struct net_device * ndev)188 static void netvsc_tx_disable(struct netvsc_device *nvscdev,
189 struct net_device *ndev)
190 {
191 if (nvscdev) {
192 nvscdev->tx_disable = true;
193 virt_wmb(); /* ensure txq will not wake up after stop */
194 }
195
196 netif_tx_disable(ndev);
197 }
198
netvsc_close(struct net_device * net)199 static int netvsc_close(struct net_device *net)
200 {
201 struct net_device_context *net_device_ctx = netdev_priv(net);
202 struct net_device *vf_netdev
203 = rtnl_dereference(net_device_ctx->vf_netdev);
204 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
205 int ret;
206
207 netvsc_tx_disable(nvdev, net);
208
209 /* No need to close rndis filter if it is removed already */
210 if (!nvdev)
211 return 0;
212
213 ret = rndis_filter_close(nvdev);
214 if (ret != 0) {
215 netdev_err(net, "unable to close device (ret %d).\n", ret);
216 return ret;
217 }
218
219 ret = netvsc_wait_until_empty(nvdev);
220 if (ret)
221 netdev_err(net, "Ring buffer not empty after closing rndis\n");
222
223 if (vf_netdev)
224 dev_close(vf_netdev);
225
226 return ret;
227 }
228
init_ppi_data(struct rndis_message * msg,u32 ppi_size,u32 pkt_type)229 static inline void *init_ppi_data(struct rndis_message *msg,
230 u32 ppi_size, u32 pkt_type)
231 {
232 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
233 struct rndis_per_packet_info *ppi;
234
235 rndis_pkt->data_offset += ppi_size;
236 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
237 + rndis_pkt->per_pkt_info_len;
238
239 ppi->size = ppi_size;
240 ppi->type = pkt_type;
241 ppi->internal = 0;
242 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
243
244 rndis_pkt->per_pkt_info_len += ppi_size;
245
246 return ppi + 1;
247 }
248
netvsc_get_tx_queue(struct net_device * ndev,struct sk_buff * skb,int old_idx)249 static inline int netvsc_get_tx_queue(struct net_device *ndev,
250 struct sk_buff *skb, int old_idx)
251 {
252 const struct net_device_context *ndc = netdev_priv(ndev);
253 struct sock *sk = skb->sk;
254 int q_idx;
255
256 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
257 (VRSS_SEND_TAB_SIZE - 1)];
258
259 /* If queue index changed record the new value */
260 if (q_idx != old_idx &&
261 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
262 sk_tx_queue_set(sk, q_idx);
263
264 return q_idx;
265 }
266
267 /*
268 * Select queue for transmit.
269 *
270 * If a valid queue has already been assigned, then use that.
271 * Otherwise compute tx queue based on hash and the send table.
272 *
273 * This is basically similar to default (netdev_pick_tx) with the added step
274 * of using the host send_table when no other queue has been assigned.
275 *
276 * TODO support XPS - but get_xps_queue not exported
277 */
netvsc_pick_tx(struct net_device * ndev,struct sk_buff * skb)278 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
279 {
280 int q_idx = sk_tx_queue_get(skb->sk);
281
282 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
283 /* If forwarding a packet, we use the recorded queue when
284 * available for better cache locality.
285 */
286 if (skb_rx_queue_recorded(skb))
287 q_idx = skb_get_rx_queue(skb);
288 else
289 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
290 }
291
292 return q_idx;
293 }
294
netvsc_select_queue(struct net_device * ndev,struct sk_buff * skb,struct net_device * sb_dev)295 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
296 struct net_device *sb_dev)
297 {
298 struct net_device_context *ndc = netdev_priv(ndev);
299 struct net_device *vf_netdev;
300 u16 txq;
301
302 rcu_read_lock();
303 vf_netdev = rcu_dereference(ndc->vf_netdev);
304 if (vf_netdev) {
305 const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
306
307 if (vf_ops->ndo_select_queue)
308 txq = vf_ops->ndo_select_queue(vf_netdev, skb, sb_dev);
309 else
310 txq = netdev_pick_tx(vf_netdev, skb, NULL);
311
312 /* Record the queue selected by VF so that it can be
313 * used for common case where VF has more queues than
314 * the synthetic device.
315 */
316 qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
317 } else {
318 txq = netvsc_pick_tx(ndev, skb);
319 }
320 rcu_read_unlock();
321
322 while (txq >= ndev->real_num_tx_queues)
323 txq -= ndev->real_num_tx_queues;
324
325 return txq;
326 }
327
init_page_array(void * hdr,u32 len,struct sk_buff * skb,struct hv_netvsc_packet * packet,struct hv_page_buffer * pb)328 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
329 struct hv_netvsc_packet *packet,
330 struct hv_page_buffer *pb)
331 {
332 int frags = skb_shinfo(skb)->nr_frags;
333 int i;
334
335 /* The packet is laid out thus:
336 * 1. hdr: RNDIS header and PPI
337 * 2. skb linear data
338 * 3. skb fragment data
339 */
340
341 pb[0].offset = offset_in_hvpage(hdr);
342 pb[0].len = len;
343 pb[0].pfn = virt_to_hvpfn(hdr);
344 packet->rmsg_size = len;
345
346 pb[1].offset = offset_in_hvpage(skb->data);
347 pb[1].len = skb_headlen(skb);
348 pb[1].pfn = virt_to_hvpfn(skb->data);
349
350 for (i = 0; i < frags; i++) {
351 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
352 struct hv_page_buffer *cur_pb = &pb[i + 2];
353 u64 pfn = page_to_hvpfn(skb_frag_page(frag));
354 u32 offset = skb_frag_off(frag);
355
356 cur_pb->offset = offset_in_hvpage(offset);
357 cur_pb->len = skb_frag_size(frag);
358 cur_pb->pfn = pfn + (offset >> HV_HYP_PAGE_SHIFT);
359 }
360 return frags + 2;
361 }
362
count_skb_frag_slots(struct sk_buff * skb)363 static int count_skb_frag_slots(struct sk_buff *skb)
364 {
365 int i, frags = skb_shinfo(skb)->nr_frags;
366 int pages = 0;
367
368 for (i = 0; i < frags; i++) {
369 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
370 unsigned long size = skb_frag_size(frag);
371 unsigned long offset = skb_frag_off(frag);
372
373 /* Skip unused frames from start of page */
374 offset &= ~HV_HYP_PAGE_MASK;
375 pages += HVPFN_UP(offset + size);
376 }
377 return pages;
378 }
379
netvsc_get_slots(struct sk_buff * skb)380 static int netvsc_get_slots(struct sk_buff *skb)
381 {
382 char *data = skb->data;
383 unsigned int offset = offset_in_hvpage(data);
384 unsigned int len = skb_headlen(skb);
385 int slots;
386 int frag_slots;
387
388 slots = DIV_ROUND_UP(offset + len, HV_HYP_PAGE_SIZE);
389 frag_slots = count_skb_frag_slots(skb);
390 return slots + frag_slots;
391 }
392
net_checksum_info(struct sk_buff * skb)393 static u32 net_checksum_info(struct sk_buff *skb)
394 {
395 if (skb->protocol == htons(ETH_P_IP)) {
396 struct iphdr *ip = ip_hdr(skb);
397
398 if (ip->protocol == IPPROTO_TCP)
399 return TRANSPORT_INFO_IPV4_TCP;
400 else if (ip->protocol == IPPROTO_UDP)
401 return TRANSPORT_INFO_IPV4_UDP;
402 } else {
403 struct ipv6hdr *ip6 = ipv6_hdr(skb);
404
405 if (ip6->nexthdr == IPPROTO_TCP)
406 return TRANSPORT_INFO_IPV6_TCP;
407 else if (ip6->nexthdr == IPPROTO_UDP)
408 return TRANSPORT_INFO_IPV6_UDP;
409 }
410
411 return TRANSPORT_INFO_NOT_IP;
412 }
413
414 /* Send skb on the slave VF device. */
netvsc_vf_xmit(struct net_device * net,struct net_device * vf_netdev,struct sk_buff * skb)415 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
416 struct sk_buff *skb)
417 {
418 struct net_device_context *ndev_ctx = netdev_priv(net);
419 unsigned int len = skb->len;
420 int rc;
421
422 skb->dev = vf_netdev;
423 skb_record_rx_queue(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
424
425 rc = dev_queue_xmit(skb);
426 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
427 struct netvsc_vf_pcpu_stats *pcpu_stats
428 = this_cpu_ptr(ndev_ctx->vf_stats);
429
430 u64_stats_update_begin(&pcpu_stats->syncp);
431 pcpu_stats->tx_packets++;
432 pcpu_stats->tx_bytes += len;
433 u64_stats_update_end(&pcpu_stats->syncp);
434 } else {
435 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
436 }
437
438 return rc;
439 }
440
netvsc_xmit(struct sk_buff * skb,struct net_device * net,bool xdp_tx)441 static int netvsc_xmit(struct sk_buff *skb, struct net_device *net, bool xdp_tx)
442 {
443 struct net_device_context *net_device_ctx = netdev_priv(net);
444 struct hv_netvsc_packet *packet = NULL;
445 int ret;
446 unsigned int num_data_pgs;
447 struct rndis_message *rndis_msg;
448 struct net_device *vf_netdev;
449 u32 rndis_msg_size;
450 u32 hash;
451 struct hv_page_buffer pb[MAX_DATA_RANGES];
452
453 /* If VF is present and up then redirect packets to it.
454 * Skip the VF if it is marked down or has no carrier.
455 * If netpoll is in uses, then VF can not be used either.
456 */
457 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
458 if (vf_netdev && netif_running(vf_netdev) &&
459 netif_carrier_ok(vf_netdev) && !netpoll_tx_running(net) &&
460 net_device_ctx->data_path_is_vf)
461 return netvsc_vf_xmit(net, vf_netdev, skb);
462
463 /* We will atmost need two pages to describe the rndis
464 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
465 * of pages in a single packet. If skb is scattered around
466 * more pages we try linearizing it.
467 */
468
469 num_data_pgs = netvsc_get_slots(skb) + 2;
470
471 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
472 ++net_device_ctx->eth_stats.tx_scattered;
473
474 if (skb_linearize(skb))
475 goto no_memory;
476
477 num_data_pgs = netvsc_get_slots(skb) + 2;
478 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
479 ++net_device_ctx->eth_stats.tx_too_big;
480 goto drop;
481 }
482 }
483
484 /*
485 * Place the rndis header in the skb head room and
486 * the skb->cb will be used for hv_netvsc_packet
487 * structure.
488 */
489 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
490 if (ret)
491 goto no_memory;
492
493 /* Use the skb control buffer for building up the packet */
494 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
495 sizeof_field(struct sk_buff, cb));
496 packet = (struct hv_netvsc_packet *)skb->cb;
497
498 packet->q_idx = skb_get_queue_mapping(skb);
499
500 packet->total_data_buflen = skb->len;
501 packet->total_bytes = skb->len;
502 packet->total_packets = 1;
503
504 rndis_msg = (struct rndis_message *)skb->head;
505
506 /* Add the rndis header */
507 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
508 rndis_msg->msg_len = packet->total_data_buflen;
509
510 rndis_msg->msg.pkt = (struct rndis_packet) {
511 .data_offset = sizeof(struct rndis_packet),
512 .data_len = packet->total_data_buflen,
513 .per_pkt_info_offset = sizeof(struct rndis_packet),
514 };
515
516 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
517
518 hash = skb_get_hash_raw(skb);
519 if (hash != 0 && net->real_num_tx_queues > 1) {
520 u32 *hash_info;
521
522 rndis_msg_size += NDIS_HASH_PPI_SIZE;
523 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
524 NBL_HASH_VALUE);
525 *hash_info = hash;
526 }
527
528 /* When using AF_PACKET we need to drop VLAN header from
529 * the frame and update the SKB to allow the HOST OS
530 * to transmit the 802.1Q packet
531 */
532 if (skb->protocol == htons(ETH_P_8021Q)) {
533 u16 vlan_tci;
534
535 skb_reset_mac_header(skb);
536 if (eth_type_vlan(eth_hdr(skb)->h_proto)) {
537 if (unlikely(__skb_vlan_pop(skb, &vlan_tci) != 0)) {
538 ++net_device_ctx->eth_stats.vlan_error;
539 goto drop;
540 }
541
542 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
543 /* Update the NDIS header pkt lengths */
544 packet->total_data_buflen -= VLAN_HLEN;
545 packet->total_bytes -= VLAN_HLEN;
546 rndis_msg->msg_len = packet->total_data_buflen;
547 rndis_msg->msg.pkt.data_len = packet->total_data_buflen;
548 }
549 }
550
551 if (skb_vlan_tag_present(skb)) {
552 struct ndis_pkt_8021q_info *vlan;
553
554 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
555 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
556 IEEE_8021Q_INFO);
557
558 vlan->value = 0;
559 vlan->vlanid = skb_vlan_tag_get_id(skb);
560 vlan->cfi = skb_vlan_tag_get_cfi(skb);
561 vlan->pri = skb_vlan_tag_get_prio(skb);
562 }
563
564 if (skb_is_gso(skb)) {
565 struct ndis_tcp_lso_info *lso_info;
566
567 rndis_msg_size += NDIS_LSO_PPI_SIZE;
568 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
569 TCP_LARGESEND_PKTINFO);
570
571 lso_info->value = 0;
572 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
573 if (skb->protocol == htons(ETH_P_IP)) {
574 lso_info->lso_v2_transmit.ip_version =
575 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
576 ip_hdr(skb)->tot_len = 0;
577 ip_hdr(skb)->check = 0;
578 tcp_hdr(skb)->check =
579 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
580 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
581 } else {
582 lso_info->lso_v2_transmit.ip_version =
583 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
584 tcp_v6_gso_csum_prep(skb);
585 }
586 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
587 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
588 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
589 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
590 struct ndis_tcp_ip_checksum_info *csum_info;
591
592 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
593 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
594 TCPIP_CHKSUM_PKTINFO);
595
596 csum_info->value = 0;
597 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
598
599 if (skb->protocol == htons(ETH_P_IP)) {
600 csum_info->transmit.is_ipv4 = 1;
601
602 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
603 csum_info->transmit.tcp_checksum = 1;
604 else
605 csum_info->transmit.udp_checksum = 1;
606 } else {
607 csum_info->transmit.is_ipv6 = 1;
608
609 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
610 csum_info->transmit.tcp_checksum = 1;
611 else
612 csum_info->transmit.udp_checksum = 1;
613 }
614 } else {
615 /* Can't do offload of this type of checksum */
616 if (skb_checksum_help(skb))
617 goto drop;
618 }
619 }
620
621 /* Start filling in the page buffers with the rndis hdr */
622 rndis_msg->msg_len += rndis_msg_size;
623 packet->total_data_buflen = rndis_msg->msg_len;
624 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
625 skb, packet, pb);
626
627 /* timestamp packet in software */
628 skb_tx_timestamp(skb);
629
630 ret = netvsc_send(net, packet, rndis_msg, pb, skb, xdp_tx);
631 if (likely(ret == 0))
632 return NETDEV_TX_OK;
633
634 if (ret == -EAGAIN) {
635 ++net_device_ctx->eth_stats.tx_busy;
636 return NETDEV_TX_BUSY;
637 }
638
639 if (ret == -ENOSPC)
640 ++net_device_ctx->eth_stats.tx_no_space;
641
642 drop:
643 dev_kfree_skb_any(skb);
644 net->stats.tx_dropped++;
645
646 return NETDEV_TX_OK;
647
648 no_memory:
649 ++net_device_ctx->eth_stats.tx_no_memory;
650 goto drop;
651 }
652
netvsc_start_xmit(struct sk_buff * skb,struct net_device * ndev)653 static netdev_tx_t netvsc_start_xmit(struct sk_buff *skb,
654 struct net_device *ndev)
655 {
656 return netvsc_xmit(skb, ndev, false);
657 }
658
659 /*
660 * netvsc_linkstatus_callback - Link up/down notification
661 */
netvsc_linkstatus_callback(struct net_device * net,struct rndis_message * resp,void * data,u32 data_buflen)662 void netvsc_linkstatus_callback(struct net_device *net,
663 struct rndis_message *resp,
664 void *data, u32 data_buflen)
665 {
666 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
667 struct net_device_context *ndev_ctx = netdev_priv(net);
668 struct netvsc_reconfig *event;
669 unsigned long flags;
670
671 /* Ensure the packet is big enough to access its fields */
672 if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_indicate_status)) {
673 netdev_err(net, "invalid rndis_indicate_status packet, len: %u\n",
674 resp->msg_len);
675 return;
676 }
677
678 /* Copy the RNDIS indicate status into nvchan->recv_buf */
679 memcpy(indicate, data + RNDIS_HEADER_SIZE, sizeof(*indicate));
680
681 /* Update the physical link speed when changing to another vSwitch */
682 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
683 u32 speed;
684
685 /* Validate status_buf_offset and status_buflen.
686 *
687 * Certain (pre-Fe) implementations of Hyper-V's vSwitch didn't account
688 * for the status buffer field in resp->msg_len; perform the validation
689 * using data_buflen (>= resp->msg_len).
690 */
691 if (indicate->status_buflen < sizeof(speed) ||
692 indicate->status_buf_offset < sizeof(*indicate) ||
693 data_buflen - RNDIS_HEADER_SIZE < indicate->status_buf_offset ||
694 data_buflen - RNDIS_HEADER_SIZE - indicate->status_buf_offset
695 < indicate->status_buflen) {
696 netdev_err(net, "invalid rndis_indicate_status packet\n");
697 return;
698 }
699
700 speed = *(u32 *)(data + RNDIS_HEADER_SIZE + indicate->status_buf_offset) / 10000;
701 ndev_ctx->speed = speed;
702 return;
703 }
704
705 /* Handle these link change statuses below */
706 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
707 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
708 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
709 return;
710
711 if (net->reg_state != NETREG_REGISTERED)
712 return;
713
714 event = kzalloc(sizeof(*event), GFP_ATOMIC);
715 if (!event)
716 return;
717 event->event = indicate->status;
718
719 spin_lock_irqsave(&ndev_ctx->lock, flags);
720 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
721 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
722
723 schedule_delayed_work(&ndev_ctx->dwork, 0);
724 }
725
726 /* This function should only be called after skb_record_rx_queue() */
netvsc_xdp_xmit(struct sk_buff * skb,struct net_device * ndev)727 void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev)
728 {
729 int rc;
730
731 skb->queue_mapping = skb_get_rx_queue(skb);
732 __skb_push(skb, ETH_HLEN);
733
734 rc = netvsc_xmit(skb, ndev, true);
735
736 if (dev_xmit_complete(rc))
737 return;
738
739 dev_kfree_skb_any(skb);
740 ndev->stats.tx_dropped++;
741 }
742
netvsc_comp_ipcsum(struct sk_buff * skb)743 static void netvsc_comp_ipcsum(struct sk_buff *skb)
744 {
745 struct iphdr *iph = (struct iphdr *)skb->data;
746
747 iph->check = 0;
748 iph->check = ip_fast_csum(iph, iph->ihl);
749 }
750
netvsc_alloc_recv_skb(struct net_device * net,struct netvsc_channel * nvchan,struct xdp_buff * xdp)751 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
752 struct netvsc_channel *nvchan,
753 struct xdp_buff *xdp)
754 {
755 struct napi_struct *napi = &nvchan->napi;
756 const struct ndis_pkt_8021q_info *vlan = &nvchan->rsc.vlan;
757 const struct ndis_tcp_ip_checksum_info *csum_info =
758 &nvchan->rsc.csum_info;
759 const u32 *hash_info = &nvchan->rsc.hash_info;
760 u8 ppi_flags = nvchan->rsc.ppi_flags;
761 struct sk_buff *skb;
762 void *xbuf = xdp->data_hard_start;
763 int i;
764
765 if (xbuf) {
766 unsigned int hdroom = xdp->data - xdp->data_hard_start;
767 unsigned int xlen = xdp->data_end - xdp->data;
768 unsigned int frag_size = xdp->frame_sz;
769
770 skb = build_skb(xbuf, frag_size);
771
772 if (!skb) {
773 __free_page(virt_to_page(xbuf));
774 return NULL;
775 }
776
777 skb_reserve(skb, hdroom);
778 skb_put(skb, xlen);
779 skb->dev = napi->dev;
780 } else {
781 skb = napi_alloc_skb(napi, nvchan->rsc.pktlen);
782
783 if (!skb)
784 return NULL;
785
786 /* Copy to skb. This copy is needed here since the memory
787 * pointed by hv_netvsc_packet cannot be deallocated.
788 */
789 for (i = 0; i < nvchan->rsc.cnt; i++)
790 skb_put_data(skb, nvchan->rsc.data[i],
791 nvchan->rsc.len[i]);
792 }
793
794 skb->protocol = eth_type_trans(skb, net);
795
796 /* skb is already created with CHECKSUM_NONE */
797 skb_checksum_none_assert(skb);
798
799 /* Incoming packets may have IP header checksum verified by the host.
800 * They may not have IP header checksum computed after coalescing.
801 * We compute it here if the flags are set, because on Linux, the IP
802 * checksum is always checked.
803 */
804 if ((ppi_flags & NVSC_RSC_CSUM_INFO) && csum_info->receive.ip_checksum_value_invalid &&
805 csum_info->receive.ip_checksum_succeeded &&
806 skb->protocol == htons(ETH_P_IP)) {
807 /* Check that there is enough space to hold the IP header. */
808 if (skb_headlen(skb) < sizeof(struct iphdr)) {
809 kfree_skb(skb);
810 return NULL;
811 }
812 netvsc_comp_ipcsum(skb);
813 }
814
815 /* Do L4 checksum offload if enabled and present. */
816 if ((ppi_flags & NVSC_RSC_CSUM_INFO) && (net->features & NETIF_F_RXCSUM)) {
817 if (csum_info->receive.tcp_checksum_succeeded ||
818 csum_info->receive.udp_checksum_succeeded)
819 skb->ip_summed = CHECKSUM_UNNECESSARY;
820 }
821
822 if ((ppi_flags & NVSC_RSC_HASH_INFO) && (net->features & NETIF_F_RXHASH))
823 skb_set_hash(skb, *hash_info, PKT_HASH_TYPE_L4);
824
825 if (ppi_flags & NVSC_RSC_VLAN) {
826 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT) |
827 (vlan->cfi ? VLAN_CFI_MASK : 0);
828
829 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
830 vlan_tci);
831 }
832
833 return skb;
834 }
835
836 /*
837 * netvsc_recv_callback - Callback when we receive a packet from the
838 * "wire" on the specified device.
839 */
netvsc_recv_callback(struct net_device * net,struct netvsc_device * net_device,struct netvsc_channel * nvchan)840 int netvsc_recv_callback(struct net_device *net,
841 struct netvsc_device *net_device,
842 struct netvsc_channel *nvchan)
843 {
844 struct net_device_context *net_device_ctx = netdev_priv(net);
845 struct vmbus_channel *channel = nvchan->channel;
846 u16 q_idx = channel->offermsg.offer.sub_channel_index;
847 struct sk_buff *skb;
848 struct netvsc_stats_rx *rx_stats = &nvchan->rx_stats;
849 struct xdp_buff xdp;
850 u32 act;
851
852 if (net->reg_state != NETREG_REGISTERED)
853 return NVSP_STAT_FAIL;
854
855 act = netvsc_run_xdp(net, nvchan, &xdp);
856
857 if (act == XDP_REDIRECT)
858 return NVSP_STAT_SUCCESS;
859
860 if (act != XDP_PASS && act != XDP_TX) {
861 u64_stats_update_begin(&rx_stats->syncp);
862 rx_stats->xdp_drop++;
863 u64_stats_update_end(&rx_stats->syncp);
864
865 return NVSP_STAT_SUCCESS; /* consumed by XDP */
866 }
867
868 /* Allocate a skb - TODO direct I/O to pages? */
869 skb = netvsc_alloc_recv_skb(net, nvchan, &xdp);
870
871 if (unlikely(!skb)) {
872 ++net_device_ctx->eth_stats.rx_no_memory;
873 return NVSP_STAT_FAIL;
874 }
875
876 skb_record_rx_queue(skb, q_idx);
877
878 /*
879 * Even if injecting the packet, record the statistics
880 * on the synthetic device because modifying the VF device
881 * statistics will not work correctly.
882 */
883 u64_stats_update_begin(&rx_stats->syncp);
884 if (act == XDP_TX)
885 rx_stats->xdp_tx++;
886
887 rx_stats->packets++;
888 rx_stats->bytes += nvchan->rsc.pktlen;
889
890 if (skb->pkt_type == PACKET_BROADCAST)
891 ++rx_stats->broadcast;
892 else if (skb->pkt_type == PACKET_MULTICAST)
893 ++rx_stats->multicast;
894 u64_stats_update_end(&rx_stats->syncp);
895
896 if (act == XDP_TX) {
897 netvsc_xdp_xmit(skb, net);
898 return NVSP_STAT_SUCCESS;
899 }
900
901 napi_gro_receive(&nvchan->napi, skb);
902 return NVSP_STAT_SUCCESS;
903 }
904
netvsc_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)905 static void netvsc_get_drvinfo(struct net_device *net,
906 struct ethtool_drvinfo *info)
907 {
908 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
909 strscpy(info->fw_version, "N/A", sizeof(info->fw_version));
910 }
911
netvsc_get_channels(struct net_device * net,struct ethtool_channels * channel)912 static void netvsc_get_channels(struct net_device *net,
913 struct ethtool_channels *channel)
914 {
915 struct net_device_context *net_device_ctx = netdev_priv(net);
916 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
917
918 if (nvdev) {
919 channel->max_combined = nvdev->max_chn;
920 channel->combined_count = nvdev->num_chn;
921 }
922 }
923
924 /* Alloc struct netvsc_device_info, and initialize it from either existing
925 * struct netvsc_device, or from default values.
926 */
927 static
netvsc_devinfo_get(struct netvsc_device * nvdev)928 struct netvsc_device_info *netvsc_devinfo_get(struct netvsc_device *nvdev)
929 {
930 struct netvsc_device_info *dev_info;
931 struct bpf_prog *prog;
932
933 dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
934
935 if (!dev_info)
936 return NULL;
937
938 if (nvdev) {
939 ASSERT_RTNL();
940
941 dev_info->num_chn = nvdev->num_chn;
942 dev_info->send_sections = nvdev->send_section_cnt;
943 dev_info->send_section_size = nvdev->send_section_size;
944 dev_info->recv_sections = nvdev->recv_section_cnt;
945 dev_info->recv_section_size = nvdev->recv_section_size;
946
947 memcpy(dev_info->rss_key, nvdev->extension->rss_key,
948 NETVSC_HASH_KEYLEN);
949
950 prog = netvsc_xdp_get(nvdev);
951 if (prog) {
952 bpf_prog_inc(prog);
953 dev_info->bprog = prog;
954 }
955 } else {
956 dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
957 dev_info->send_sections = NETVSC_DEFAULT_TX;
958 dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
959 dev_info->recv_sections = NETVSC_DEFAULT_RX;
960 dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
961 }
962
963 return dev_info;
964 }
965
966 /* Free struct netvsc_device_info */
netvsc_devinfo_put(struct netvsc_device_info * dev_info)967 static void netvsc_devinfo_put(struct netvsc_device_info *dev_info)
968 {
969 if (dev_info->bprog) {
970 ASSERT_RTNL();
971 bpf_prog_put(dev_info->bprog);
972 }
973
974 kfree(dev_info);
975 }
976
netvsc_detach(struct net_device * ndev,struct netvsc_device * nvdev)977 static int netvsc_detach(struct net_device *ndev,
978 struct netvsc_device *nvdev)
979 {
980 struct net_device_context *ndev_ctx = netdev_priv(ndev);
981 struct hv_device *hdev = ndev_ctx->device_ctx;
982 int ret;
983
984 /* Don't try continuing to try and setup sub channels */
985 if (cancel_work_sync(&nvdev->subchan_work))
986 nvdev->num_chn = 1;
987
988 netvsc_xdp_set(ndev, NULL, NULL, nvdev);
989
990 /* If device was up (receiving) then shutdown */
991 if (netif_running(ndev)) {
992 netvsc_tx_disable(nvdev, ndev);
993
994 ret = rndis_filter_close(nvdev);
995 if (ret) {
996 netdev_err(ndev,
997 "unable to close device (ret %d).\n", ret);
998 return ret;
999 }
1000
1001 ret = netvsc_wait_until_empty(nvdev);
1002 if (ret) {
1003 netdev_err(ndev,
1004 "Ring buffer not empty after closing rndis\n");
1005 return ret;
1006 }
1007 }
1008
1009 netif_device_detach(ndev);
1010
1011 rndis_filter_device_remove(hdev, nvdev);
1012
1013 return 0;
1014 }
1015
netvsc_attach(struct net_device * ndev,struct netvsc_device_info * dev_info)1016 static int netvsc_attach(struct net_device *ndev,
1017 struct netvsc_device_info *dev_info)
1018 {
1019 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1020 struct hv_device *hdev = ndev_ctx->device_ctx;
1021 struct netvsc_device *nvdev;
1022 struct rndis_device *rdev;
1023 struct bpf_prog *prog;
1024 int ret = 0;
1025
1026 nvdev = rndis_filter_device_add(hdev, dev_info);
1027 if (IS_ERR(nvdev))
1028 return PTR_ERR(nvdev);
1029
1030 if (nvdev->num_chn > 1) {
1031 ret = rndis_set_subchannel(ndev, nvdev, dev_info);
1032
1033 /* if unavailable, just proceed with one queue */
1034 if (ret) {
1035 nvdev->max_chn = 1;
1036 nvdev->num_chn = 1;
1037 }
1038 }
1039
1040 prog = dev_info->bprog;
1041 if (prog) {
1042 bpf_prog_inc(prog);
1043 ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
1044 if (ret) {
1045 bpf_prog_put(prog);
1046 goto err1;
1047 }
1048 }
1049
1050 /* In any case device is now ready */
1051 nvdev->tx_disable = false;
1052 netif_device_attach(ndev);
1053
1054 /* Note: enable and attach happen when sub-channels setup */
1055 netif_carrier_off(ndev);
1056
1057 if (netif_running(ndev)) {
1058 ret = rndis_filter_open(nvdev);
1059 if (ret)
1060 goto err2;
1061
1062 rdev = nvdev->extension;
1063 if (!rdev->link_state)
1064 netif_carrier_on(ndev);
1065 }
1066
1067 return 0;
1068
1069 err2:
1070 netif_device_detach(ndev);
1071
1072 err1:
1073 rndis_filter_device_remove(hdev, nvdev);
1074
1075 return ret;
1076 }
1077
netvsc_set_channels(struct net_device * net,struct ethtool_channels * channels)1078 static int netvsc_set_channels(struct net_device *net,
1079 struct ethtool_channels *channels)
1080 {
1081 struct net_device_context *net_device_ctx = netdev_priv(net);
1082 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
1083 unsigned int orig, count = channels->combined_count;
1084 struct netvsc_device_info *device_info;
1085 int ret;
1086
1087 /* We do not support separate count for rx, tx, or other */
1088 if (count == 0 ||
1089 channels->rx_count || channels->tx_count || channels->other_count)
1090 return -EINVAL;
1091
1092 if (!nvdev || nvdev->destroy)
1093 return -ENODEV;
1094
1095 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1096 return -EINVAL;
1097
1098 if (count > nvdev->max_chn)
1099 return -EINVAL;
1100
1101 orig = nvdev->num_chn;
1102
1103 device_info = netvsc_devinfo_get(nvdev);
1104
1105 if (!device_info)
1106 return -ENOMEM;
1107
1108 device_info->num_chn = count;
1109
1110 ret = netvsc_detach(net, nvdev);
1111 if (ret)
1112 goto out;
1113
1114 ret = netvsc_attach(net, device_info);
1115 if (ret) {
1116 device_info->num_chn = orig;
1117 if (netvsc_attach(net, device_info))
1118 netdev_err(net, "restoring channel setting failed\n");
1119 }
1120
1121 out:
1122 netvsc_devinfo_put(device_info);
1123 return ret;
1124 }
1125
netvsc_init_settings(struct net_device * dev)1126 static void netvsc_init_settings(struct net_device *dev)
1127 {
1128 struct net_device_context *ndc = netdev_priv(dev);
1129
1130 ndc->l4_hash = HV_DEFAULT_L4HASH;
1131
1132 ndc->speed = SPEED_UNKNOWN;
1133 ndc->duplex = DUPLEX_FULL;
1134
1135 dev->features = NETIF_F_LRO;
1136 }
1137
netvsc_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)1138 static int netvsc_get_link_ksettings(struct net_device *dev,
1139 struct ethtool_link_ksettings *cmd)
1140 {
1141 struct net_device_context *ndc = netdev_priv(dev);
1142 struct net_device *vf_netdev;
1143
1144 vf_netdev = rtnl_dereference(ndc->vf_netdev);
1145
1146 if (vf_netdev)
1147 return __ethtool_get_link_ksettings(vf_netdev, cmd);
1148
1149 cmd->base.speed = ndc->speed;
1150 cmd->base.duplex = ndc->duplex;
1151 cmd->base.port = PORT_OTHER;
1152
1153 return 0;
1154 }
1155
netvsc_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)1156 static int netvsc_set_link_ksettings(struct net_device *dev,
1157 const struct ethtool_link_ksettings *cmd)
1158 {
1159 struct net_device_context *ndc = netdev_priv(dev);
1160 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1161
1162 if (vf_netdev) {
1163 if (!vf_netdev->ethtool_ops->set_link_ksettings)
1164 return -EOPNOTSUPP;
1165
1166 return vf_netdev->ethtool_ops->set_link_ksettings(vf_netdev,
1167 cmd);
1168 }
1169
1170 return ethtool_virtdev_set_link_ksettings(dev, cmd,
1171 &ndc->speed, &ndc->duplex);
1172 }
1173
netvsc_change_mtu(struct net_device * ndev,int mtu)1174 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
1175 {
1176 struct net_device_context *ndevctx = netdev_priv(ndev);
1177 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1178 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1179 int orig_mtu = ndev->mtu;
1180 struct netvsc_device_info *device_info;
1181 int ret = 0;
1182
1183 if (!nvdev || nvdev->destroy)
1184 return -ENODEV;
1185
1186 device_info = netvsc_devinfo_get(nvdev);
1187
1188 if (!device_info)
1189 return -ENOMEM;
1190
1191 /* Change MTU of underlying VF netdev first. */
1192 if (vf_netdev) {
1193 ret = dev_set_mtu(vf_netdev, mtu);
1194 if (ret)
1195 goto out;
1196 }
1197
1198 ret = netvsc_detach(ndev, nvdev);
1199 if (ret)
1200 goto rollback_vf;
1201
1202 ndev->mtu = mtu;
1203
1204 ret = netvsc_attach(ndev, device_info);
1205 if (!ret)
1206 goto out;
1207
1208 /* Attempt rollback to original MTU */
1209 ndev->mtu = orig_mtu;
1210
1211 if (netvsc_attach(ndev, device_info))
1212 netdev_err(ndev, "restoring mtu failed\n");
1213 rollback_vf:
1214 if (vf_netdev)
1215 dev_set_mtu(vf_netdev, orig_mtu);
1216
1217 out:
1218 netvsc_devinfo_put(device_info);
1219 return ret;
1220 }
1221
netvsc_get_vf_stats(struct net_device * net,struct netvsc_vf_pcpu_stats * tot)1222 static void netvsc_get_vf_stats(struct net_device *net,
1223 struct netvsc_vf_pcpu_stats *tot)
1224 {
1225 struct net_device_context *ndev_ctx = netdev_priv(net);
1226 int i;
1227
1228 memset(tot, 0, sizeof(*tot));
1229
1230 for_each_possible_cpu(i) {
1231 const struct netvsc_vf_pcpu_stats *stats
1232 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1233 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1234 unsigned int start;
1235
1236 do {
1237 start = u64_stats_fetch_begin(&stats->syncp);
1238 rx_packets = stats->rx_packets;
1239 tx_packets = stats->tx_packets;
1240 rx_bytes = stats->rx_bytes;
1241 tx_bytes = stats->tx_bytes;
1242 } while (u64_stats_fetch_retry(&stats->syncp, start));
1243
1244 tot->rx_packets += rx_packets;
1245 tot->tx_packets += tx_packets;
1246 tot->rx_bytes += rx_bytes;
1247 tot->tx_bytes += tx_bytes;
1248 tot->tx_dropped += stats->tx_dropped;
1249 }
1250 }
1251
netvsc_get_pcpu_stats(struct net_device * net,struct netvsc_ethtool_pcpu_stats * pcpu_tot)1252 static void netvsc_get_pcpu_stats(struct net_device *net,
1253 struct netvsc_ethtool_pcpu_stats *pcpu_tot)
1254 {
1255 struct net_device_context *ndev_ctx = netdev_priv(net);
1256 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1257 int i;
1258
1259 /* fetch percpu stats of vf */
1260 for_each_possible_cpu(i) {
1261 const struct netvsc_vf_pcpu_stats *stats =
1262 per_cpu_ptr(ndev_ctx->vf_stats, i);
1263 struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
1264 unsigned int start;
1265
1266 do {
1267 start = u64_stats_fetch_begin(&stats->syncp);
1268 this_tot->vf_rx_packets = stats->rx_packets;
1269 this_tot->vf_tx_packets = stats->tx_packets;
1270 this_tot->vf_rx_bytes = stats->rx_bytes;
1271 this_tot->vf_tx_bytes = stats->tx_bytes;
1272 } while (u64_stats_fetch_retry(&stats->syncp, start));
1273 this_tot->rx_packets = this_tot->vf_rx_packets;
1274 this_tot->tx_packets = this_tot->vf_tx_packets;
1275 this_tot->rx_bytes = this_tot->vf_rx_bytes;
1276 this_tot->tx_bytes = this_tot->vf_tx_bytes;
1277 }
1278
1279 /* fetch percpu stats of netvsc */
1280 for (i = 0; i < nvdev->num_chn; i++) {
1281 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1282 const struct netvsc_stats_tx *tx_stats;
1283 const struct netvsc_stats_rx *rx_stats;
1284 struct netvsc_ethtool_pcpu_stats *this_tot =
1285 &pcpu_tot[nvchan->channel->target_cpu];
1286 u64 packets, bytes;
1287 unsigned int start;
1288
1289 tx_stats = &nvchan->tx_stats;
1290 do {
1291 start = u64_stats_fetch_begin(&tx_stats->syncp);
1292 packets = tx_stats->packets;
1293 bytes = tx_stats->bytes;
1294 } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
1295
1296 this_tot->tx_bytes += bytes;
1297 this_tot->tx_packets += packets;
1298
1299 rx_stats = &nvchan->rx_stats;
1300 do {
1301 start = u64_stats_fetch_begin(&rx_stats->syncp);
1302 packets = rx_stats->packets;
1303 bytes = rx_stats->bytes;
1304 } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
1305
1306 this_tot->rx_bytes += bytes;
1307 this_tot->rx_packets += packets;
1308 }
1309 }
1310
netvsc_get_stats64(struct net_device * net,struct rtnl_link_stats64 * t)1311 static void netvsc_get_stats64(struct net_device *net,
1312 struct rtnl_link_stats64 *t)
1313 {
1314 struct net_device_context *ndev_ctx = netdev_priv(net);
1315 struct netvsc_device *nvdev;
1316 struct netvsc_vf_pcpu_stats vf_tot;
1317 int i;
1318
1319 rcu_read_lock();
1320
1321 nvdev = rcu_dereference(ndev_ctx->nvdev);
1322 if (!nvdev)
1323 goto out;
1324
1325 netdev_stats_to_stats64(t, &net->stats);
1326
1327 netvsc_get_vf_stats(net, &vf_tot);
1328 t->rx_packets += vf_tot.rx_packets;
1329 t->tx_packets += vf_tot.tx_packets;
1330 t->rx_bytes += vf_tot.rx_bytes;
1331 t->tx_bytes += vf_tot.tx_bytes;
1332 t->tx_dropped += vf_tot.tx_dropped;
1333
1334 for (i = 0; i < nvdev->num_chn; i++) {
1335 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1336 const struct netvsc_stats_tx *tx_stats;
1337 const struct netvsc_stats_rx *rx_stats;
1338 u64 packets, bytes, multicast;
1339 unsigned int start;
1340
1341 tx_stats = &nvchan->tx_stats;
1342 do {
1343 start = u64_stats_fetch_begin(&tx_stats->syncp);
1344 packets = tx_stats->packets;
1345 bytes = tx_stats->bytes;
1346 } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
1347
1348 t->tx_bytes += bytes;
1349 t->tx_packets += packets;
1350
1351 rx_stats = &nvchan->rx_stats;
1352 do {
1353 start = u64_stats_fetch_begin(&rx_stats->syncp);
1354 packets = rx_stats->packets;
1355 bytes = rx_stats->bytes;
1356 multicast = rx_stats->multicast + rx_stats->broadcast;
1357 } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
1358
1359 t->rx_bytes += bytes;
1360 t->rx_packets += packets;
1361 t->multicast += multicast;
1362 }
1363 out:
1364 rcu_read_unlock();
1365 }
1366
netvsc_set_mac_addr(struct net_device * ndev,void * p)1367 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1368 {
1369 struct net_device_context *ndc = netdev_priv(ndev);
1370 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1371 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1372 struct sockaddr *addr = p;
1373 int err;
1374
1375 err = eth_prepare_mac_addr_change(ndev, p);
1376 if (err)
1377 return err;
1378
1379 if (!nvdev)
1380 return -ENODEV;
1381
1382 if (vf_netdev) {
1383 err = dev_set_mac_address(vf_netdev, addr, NULL);
1384 if (err)
1385 return err;
1386 }
1387
1388 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1389 if (!err) {
1390 eth_commit_mac_addr_change(ndev, p);
1391 } else if (vf_netdev) {
1392 /* rollback change on VF */
1393 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1394 dev_set_mac_address(vf_netdev, addr, NULL);
1395 }
1396
1397 return err;
1398 }
1399
1400 static const struct {
1401 char name[ETH_GSTRING_LEN];
1402 u16 offset;
1403 } netvsc_stats[] = {
1404 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1405 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1406 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1407 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1408 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
1409 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1410 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1411 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1412 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1413 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1414 { "vlan_error", offsetof(struct netvsc_ethtool_stats, vlan_error) },
1415 }, pcpu_stats[] = {
1416 { "cpu%u_rx_packets",
1417 offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
1418 { "cpu%u_rx_bytes",
1419 offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
1420 { "cpu%u_tx_packets",
1421 offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
1422 { "cpu%u_tx_bytes",
1423 offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
1424 { "cpu%u_vf_rx_packets",
1425 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
1426 { "cpu%u_vf_rx_bytes",
1427 offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
1428 { "cpu%u_vf_tx_packets",
1429 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
1430 { "cpu%u_vf_tx_bytes",
1431 offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
1432 }, vf_stats[] = {
1433 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1434 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1435 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1436 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1437 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1438 };
1439
1440 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1441 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
1442
1443 /* statistics per queue (rx/tx packets/bytes) */
1444 #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
1445
1446 /* 8 statistics per queue (rx/tx packets/bytes, XDP actions) */
1447 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 8)
1448
netvsc_get_sset_count(struct net_device * dev,int string_set)1449 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1450 {
1451 struct net_device_context *ndc = netdev_priv(dev);
1452 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1453
1454 if (!nvdev)
1455 return -ENODEV;
1456
1457 switch (string_set) {
1458 case ETH_SS_STATS:
1459 return NETVSC_GLOBAL_STATS_LEN
1460 + NETVSC_VF_STATS_LEN
1461 + NETVSC_QUEUE_STATS_LEN(nvdev)
1462 + NETVSC_PCPU_STATS_LEN;
1463 default:
1464 return -EINVAL;
1465 }
1466 }
1467
netvsc_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)1468 static void netvsc_get_ethtool_stats(struct net_device *dev,
1469 struct ethtool_stats *stats, u64 *data)
1470 {
1471 struct net_device_context *ndc = netdev_priv(dev);
1472 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1473 const void *nds = &ndc->eth_stats;
1474 const struct netvsc_stats_tx *tx_stats;
1475 const struct netvsc_stats_rx *rx_stats;
1476 struct netvsc_vf_pcpu_stats sum;
1477 struct netvsc_ethtool_pcpu_stats *pcpu_sum;
1478 unsigned int start;
1479 u64 packets, bytes;
1480 u64 xdp_drop;
1481 u64 xdp_redirect;
1482 u64 xdp_tx;
1483 u64 xdp_xmit;
1484 int i, j, cpu;
1485
1486 if (!nvdev)
1487 return;
1488
1489 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1490 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1491
1492 netvsc_get_vf_stats(dev, &sum);
1493 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1494 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1495
1496 for (j = 0; j < nvdev->num_chn; j++) {
1497 tx_stats = &nvdev->chan_table[j].tx_stats;
1498
1499 do {
1500 start = u64_stats_fetch_begin(&tx_stats->syncp);
1501 packets = tx_stats->packets;
1502 bytes = tx_stats->bytes;
1503 xdp_xmit = tx_stats->xdp_xmit;
1504 } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
1505 data[i++] = packets;
1506 data[i++] = bytes;
1507 data[i++] = xdp_xmit;
1508
1509 rx_stats = &nvdev->chan_table[j].rx_stats;
1510 do {
1511 start = u64_stats_fetch_begin(&rx_stats->syncp);
1512 packets = rx_stats->packets;
1513 bytes = rx_stats->bytes;
1514 xdp_drop = rx_stats->xdp_drop;
1515 xdp_redirect = rx_stats->xdp_redirect;
1516 xdp_tx = rx_stats->xdp_tx;
1517 } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
1518 data[i++] = packets;
1519 data[i++] = bytes;
1520 data[i++] = xdp_drop;
1521 data[i++] = xdp_redirect;
1522 data[i++] = xdp_tx;
1523 }
1524
1525 pcpu_sum = kvmalloc_array(num_possible_cpus(),
1526 sizeof(struct netvsc_ethtool_pcpu_stats),
1527 GFP_KERNEL);
1528 if (!pcpu_sum)
1529 return;
1530
1531 netvsc_get_pcpu_stats(dev, pcpu_sum);
1532 for_each_present_cpu(cpu) {
1533 struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
1534
1535 for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
1536 data[i++] = *(u64 *)((void *)this_sum
1537 + pcpu_stats[j].offset);
1538 }
1539 kvfree(pcpu_sum);
1540 }
1541
netvsc_get_strings(struct net_device * dev,u32 stringset,u8 * data)1542 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1543 {
1544 struct net_device_context *ndc = netdev_priv(dev);
1545 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1546 u8 *p = data;
1547 int i, cpu;
1548
1549 if (!nvdev)
1550 return;
1551
1552 switch (stringset) {
1553 case ETH_SS_STATS:
1554 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1555 ethtool_sprintf(&p, netvsc_stats[i].name);
1556
1557 for (i = 0; i < ARRAY_SIZE(vf_stats); i++)
1558 ethtool_sprintf(&p, vf_stats[i].name);
1559
1560 for (i = 0; i < nvdev->num_chn; i++) {
1561 ethtool_sprintf(&p, "tx_queue_%u_packets", i);
1562 ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
1563 ethtool_sprintf(&p, "tx_queue_%u_xdp_xmit", i);
1564 ethtool_sprintf(&p, "rx_queue_%u_packets", i);
1565 ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
1566 ethtool_sprintf(&p, "rx_queue_%u_xdp_drop", i);
1567 ethtool_sprintf(&p, "rx_queue_%u_xdp_redirect", i);
1568 ethtool_sprintf(&p, "rx_queue_%u_xdp_tx", i);
1569 }
1570
1571 for_each_present_cpu(cpu) {
1572 for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++)
1573 ethtool_sprintf(&p, pcpu_stats[i].name, cpu);
1574 }
1575
1576 break;
1577 }
1578 }
1579
1580 static int
netvsc_get_rss_hash_opts(struct net_device_context * ndc,struct ethtool_rxnfc * info)1581 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1582 struct ethtool_rxnfc *info)
1583 {
1584 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1585
1586 info->data = RXH_IP_SRC | RXH_IP_DST;
1587
1588 switch (info->flow_type) {
1589 case TCP_V4_FLOW:
1590 if (ndc->l4_hash & HV_TCP4_L4HASH)
1591 info->data |= l4_flag;
1592
1593 break;
1594
1595 case TCP_V6_FLOW:
1596 if (ndc->l4_hash & HV_TCP6_L4HASH)
1597 info->data |= l4_flag;
1598
1599 break;
1600
1601 case UDP_V4_FLOW:
1602 if (ndc->l4_hash & HV_UDP4_L4HASH)
1603 info->data |= l4_flag;
1604
1605 break;
1606
1607 case UDP_V6_FLOW:
1608 if (ndc->l4_hash & HV_UDP6_L4HASH)
1609 info->data |= l4_flag;
1610
1611 break;
1612
1613 case IPV4_FLOW:
1614 case IPV6_FLOW:
1615 break;
1616 default:
1617 info->data = 0;
1618 break;
1619 }
1620
1621 return 0;
1622 }
1623
1624 static int
netvsc_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)1625 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1626 u32 *rules)
1627 {
1628 struct net_device_context *ndc = netdev_priv(dev);
1629 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1630
1631 if (!nvdev)
1632 return -ENODEV;
1633
1634 switch (info->cmd) {
1635 case ETHTOOL_GRXRINGS:
1636 info->data = nvdev->num_chn;
1637 return 0;
1638
1639 case ETHTOOL_GRXFH:
1640 return netvsc_get_rss_hash_opts(ndc, info);
1641 }
1642 return -EOPNOTSUPP;
1643 }
1644
netvsc_set_rss_hash_opts(struct net_device_context * ndc,struct ethtool_rxnfc * info)1645 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1646 struct ethtool_rxnfc *info)
1647 {
1648 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1649 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1650 switch (info->flow_type) {
1651 case TCP_V4_FLOW:
1652 ndc->l4_hash |= HV_TCP4_L4HASH;
1653 break;
1654
1655 case TCP_V6_FLOW:
1656 ndc->l4_hash |= HV_TCP6_L4HASH;
1657 break;
1658
1659 case UDP_V4_FLOW:
1660 ndc->l4_hash |= HV_UDP4_L4HASH;
1661 break;
1662
1663 case UDP_V6_FLOW:
1664 ndc->l4_hash |= HV_UDP6_L4HASH;
1665 break;
1666
1667 default:
1668 return -EOPNOTSUPP;
1669 }
1670
1671 return 0;
1672 }
1673
1674 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1675 switch (info->flow_type) {
1676 case TCP_V4_FLOW:
1677 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1678 break;
1679
1680 case TCP_V6_FLOW:
1681 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1682 break;
1683
1684 case UDP_V4_FLOW:
1685 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1686 break;
1687
1688 case UDP_V6_FLOW:
1689 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1690 break;
1691
1692 default:
1693 return -EOPNOTSUPP;
1694 }
1695
1696 return 0;
1697 }
1698
1699 return -EOPNOTSUPP;
1700 }
1701
1702 static int
netvsc_set_rxnfc(struct net_device * ndev,struct ethtool_rxnfc * info)1703 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1704 {
1705 struct net_device_context *ndc = netdev_priv(ndev);
1706
1707 if (info->cmd == ETHTOOL_SRXFH)
1708 return netvsc_set_rss_hash_opts(ndc, info);
1709
1710 return -EOPNOTSUPP;
1711 }
1712
netvsc_get_rxfh_key_size(struct net_device * dev)1713 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1714 {
1715 return NETVSC_HASH_KEYLEN;
1716 }
1717
netvsc_rss_indir_size(struct net_device * dev)1718 static u32 netvsc_rss_indir_size(struct net_device *dev)
1719 {
1720 struct net_device_context *ndc = netdev_priv(dev);
1721
1722 return ndc->rx_table_sz;
1723 }
1724
netvsc_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)1725 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1726 u8 *hfunc)
1727 {
1728 struct net_device_context *ndc = netdev_priv(dev);
1729 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1730 struct rndis_device *rndis_dev;
1731 int i;
1732
1733 if (!ndev)
1734 return -ENODEV;
1735
1736 if (hfunc)
1737 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1738
1739 rndis_dev = ndev->extension;
1740 if (indir) {
1741 for (i = 0; i < ndc->rx_table_sz; i++)
1742 indir[i] = ndc->rx_table[i];
1743 }
1744
1745 if (key)
1746 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1747
1748 return 0;
1749 }
1750
netvsc_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)1751 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1752 const u8 *key, const u8 hfunc)
1753 {
1754 struct net_device_context *ndc = netdev_priv(dev);
1755 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1756 struct rndis_device *rndis_dev;
1757 int i;
1758
1759 if (!ndev)
1760 return -ENODEV;
1761
1762 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1763 return -EOPNOTSUPP;
1764
1765 rndis_dev = ndev->extension;
1766 if (indir) {
1767 for (i = 0; i < ndc->rx_table_sz; i++)
1768 if (indir[i] >= ndev->num_chn)
1769 return -EINVAL;
1770
1771 for (i = 0; i < ndc->rx_table_sz; i++)
1772 ndc->rx_table[i] = indir[i];
1773 }
1774
1775 if (!key) {
1776 if (!indir)
1777 return 0;
1778
1779 key = rndis_dev->rss_key;
1780 }
1781
1782 return rndis_filter_set_rss_param(rndis_dev, key);
1783 }
1784
1785 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1786 * It does have pre-allocated receive area which is divided into sections.
1787 */
__netvsc_get_ringparam(struct netvsc_device * nvdev,struct ethtool_ringparam * ring)1788 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1789 struct ethtool_ringparam *ring)
1790 {
1791 u32 max_buf_size;
1792
1793 ring->rx_pending = nvdev->recv_section_cnt;
1794 ring->tx_pending = nvdev->send_section_cnt;
1795
1796 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1797 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1798 else
1799 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1800
1801 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1802 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1803 / nvdev->send_section_size;
1804 }
1805
netvsc_get_ringparam(struct net_device * ndev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)1806 static void netvsc_get_ringparam(struct net_device *ndev,
1807 struct ethtool_ringparam *ring,
1808 struct kernel_ethtool_ringparam *kernel_ring,
1809 struct netlink_ext_ack *extack)
1810 {
1811 struct net_device_context *ndevctx = netdev_priv(ndev);
1812 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1813
1814 if (!nvdev)
1815 return;
1816
1817 __netvsc_get_ringparam(nvdev, ring);
1818 }
1819
netvsc_set_ringparam(struct net_device * ndev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)1820 static int netvsc_set_ringparam(struct net_device *ndev,
1821 struct ethtool_ringparam *ring,
1822 struct kernel_ethtool_ringparam *kernel_ring,
1823 struct netlink_ext_ack *extack)
1824 {
1825 struct net_device_context *ndevctx = netdev_priv(ndev);
1826 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1827 struct netvsc_device_info *device_info;
1828 struct ethtool_ringparam orig;
1829 u32 new_tx, new_rx;
1830 int ret = 0;
1831
1832 if (!nvdev || nvdev->destroy)
1833 return -ENODEV;
1834
1835 memset(&orig, 0, sizeof(orig));
1836 __netvsc_get_ringparam(nvdev, &orig);
1837
1838 new_tx = clamp_t(u32, ring->tx_pending,
1839 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1840 new_rx = clamp_t(u32, ring->rx_pending,
1841 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1842
1843 if (new_tx == orig.tx_pending &&
1844 new_rx == orig.rx_pending)
1845 return 0; /* no change */
1846
1847 device_info = netvsc_devinfo_get(nvdev);
1848
1849 if (!device_info)
1850 return -ENOMEM;
1851
1852 device_info->send_sections = new_tx;
1853 device_info->recv_sections = new_rx;
1854
1855 ret = netvsc_detach(ndev, nvdev);
1856 if (ret)
1857 goto out;
1858
1859 ret = netvsc_attach(ndev, device_info);
1860 if (ret) {
1861 device_info->send_sections = orig.tx_pending;
1862 device_info->recv_sections = orig.rx_pending;
1863
1864 if (netvsc_attach(ndev, device_info))
1865 netdev_err(ndev, "restoring ringparam failed");
1866 }
1867
1868 out:
1869 netvsc_devinfo_put(device_info);
1870 return ret;
1871 }
1872
netvsc_fix_features(struct net_device * ndev,netdev_features_t features)1873 static netdev_features_t netvsc_fix_features(struct net_device *ndev,
1874 netdev_features_t features)
1875 {
1876 struct net_device_context *ndevctx = netdev_priv(ndev);
1877 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1878
1879 if (!nvdev || nvdev->destroy)
1880 return features;
1881
1882 if ((features & NETIF_F_LRO) && netvsc_xdp_get(nvdev)) {
1883 features ^= NETIF_F_LRO;
1884 netdev_info(ndev, "Skip LRO - unsupported with XDP\n");
1885 }
1886
1887 return features;
1888 }
1889
netvsc_set_features(struct net_device * ndev,netdev_features_t features)1890 static int netvsc_set_features(struct net_device *ndev,
1891 netdev_features_t features)
1892 {
1893 netdev_features_t change = features ^ ndev->features;
1894 struct net_device_context *ndevctx = netdev_priv(ndev);
1895 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1896 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1897 struct ndis_offload_params offloads;
1898 int ret = 0;
1899
1900 if (!nvdev || nvdev->destroy)
1901 return -ENODEV;
1902
1903 if (!(change & NETIF_F_LRO))
1904 goto syncvf;
1905
1906 memset(&offloads, 0, sizeof(struct ndis_offload_params));
1907
1908 if (features & NETIF_F_LRO) {
1909 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1910 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1911 } else {
1912 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1913 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1914 }
1915
1916 ret = rndis_filter_set_offload_params(ndev, nvdev, &offloads);
1917
1918 if (ret) {
1919 features ^= NETIF_F_LRO;
1920 ndev->features = features;
1921 }
1922
1923 syncvf:
1924 if (!vf_netdev)
1925 return ret;
1926
1927 vf_netdev->wanted_features = features;
1928 netdev_update_features(vf_netdev);
1929
1930 return ret;
1931 }
1932
netvsc_get_regs_len(struct net_device * netdev)1933 static int netvsc_get_regs_len(struct net_device *netdev)
1934 {
1935 return VRSS_SEND_TAB_SIZE * sizeof(u32);
1936 }
1937
netvsc_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)1938 static void netvsc_get_regs(struct net_device *netdev,
1939 struct ethtool_regs *regs, void *p)
1940 {
1941 struct net_device_context *ndc = netdev_priv(netdev);
1942 u32 *regs_buff = p;
1943
1944 /* increase the version, if buffer format is changed. */
1945 regs->version = 1;
1946
1947 memcpy(regs_buff, ndc->tx_table, VRSS_SEND_TAB_SIZE * sizeof(u32));
1948 }
1949
netvsc_get_msglevel(struct net_device * ndev)1950 static u32 netvsc_get_msglevel(struct net_device *ndev)
1951 {
1952 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1953
1954 return ndev_ctx->msg_enable;
1955 }
1956
netvsc_set_msglevel(struct net_device * ndev,u32 val)1957 static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
1958 {
1959 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1960
1961 ndev_ctx->msg_enable = val;
1962 }
1963
1964 static const struct ethtool_ops ethtool_ops = {
1965 .get_drvinfo = netvsc_get_drvinfo,
1966 .get_regs_len = netvsc_get_regs_len,
1967 .get_regs = netvsc_get_regs,
1968 .get_msglevel = netvsc_get_msglevel,
1969 .set_msglevel = netvsc_set_msglevel,
1970 .get_link = ethtool_op_get_link,
1971 .get_ethtool_stats = netvsc_get_ethtool_stats,
1972 .get_sset_count = netvsc_get_sset_count,
1973 .get_strings = netvsc_get_strings,
1974 .get_channels = netvsc_get_channels,
1975 .set_channels = netvsc_set_channels,
1976 .get_ts_info = ethtool_op_get_ts_info,
1977 .get_rxnfc = netvsc_get_rxnfc,
1978 .set_rxnfc = netvsc_set_rxnfc,
1979 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1980 .get_rxfh_indir_size = netvsc_rss_indir_size,
1981 .get_rxfh = netvsc_get_rxfh,
1982 .set_rxfh = netvsc_set_rxfh,
1983 .get_link_ksettings = netvsc_get_link_ksettings,
1984 .set_link_ksettings = netvsc_set_link_ksettings,
1985 .get_ringparam = netvsc_get_ringparam,
1986 .set_ringparam = netvsc_set_ringparam,
1987 };
1988
1989 static const struct net_device_ops device_ops = {
1990 .ndo_open = netvsc_open,
1991 .ndo_stop = netvsc_close,
1992 .ndo_start_xmit = netvsc_start_xmit,
1993 .ndo_change_rx_flags = netvsc_change_rx_flags,
1994 .ndo_set_rx_mode = netvsc_set_rx_mode,
1995 .ndo_fix_features = netvsc_fix_features,
1996 .ndo_set_features = netvsc_set_features,
1997 .ndo_change_mtu = netvsc_change_mtu,
1998 .ndo_validate_addr = eth_validate_addr,
1999 .ndo_set_mac_address = netvsc_set_mac_addr,
2000 .ndo_select_queue = netvsc_select_queue,
2001 .ndo_get_stats64 = netvsc_get_stats64,
2002 .ndo_bpf = netvsc_bpf,
2003 .ndo_xdp_xmit = netvsc_ndoxdp_xmit,
2004 };
2005
2006 /*
2007 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
2008 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
2009 * present send GARP packet to network peers with netif_notify_peers().
2010 */
netvsc_link_change(struct work_struct * w)2011 static void netvsc_link_change(struct work_struct *w)
2012 {
2013 struct net_device_context *ndev_ctx =
2014 container_of(w, struct net_device_context, dwork.work);
2015 struct hv_device *device_obj = ndev_ctx->device_ctx;
2016 struct net_device *net = hv_get_drvdata(device_obj);
2017 unsigned long flags, next_reconfig, delay;
2018 struct netvsc_reconfig *event = NULL;
2019 struct netvsc_device *net_device;
2020 struct rndis_device *rdev;
2021 bool reschedule = false;
2022
2023 /* if changes are happening, comeback later */
2024 if (!rtnl_trylock()) {
2025 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2026 return;
2027 }
2028
2029 net_device = rtnl_dereference(ndev_ctx->nvdev);
2030 if (!net_device)
2031 goto out_unlock;
2032
2033 rdev = net_device->extension;
2034
2035 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
2036 if (time_is_after_jiffies(next_reconfig)) {
2037 /* link_watch only sends one notification with current state
2038 * per second, avoid doing reconfig more frequently. Handle
2039 * wrap around.
2040 */
2041 delay = next_reconfig - jiffies;
2042 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
2043 schedule_delayed_work(&ndev_ctx->dwork, delay);
2044 goto out_unlock;
2045 }
2046 ndev_ctx->last_reconfig = jiffies;
2047
2048 spin_lock_irqsave(&ndev_ctx->lock, flags);
2049 if (!list_empty(&ndev_ctx->reconfig_events)) {
2050 event = list_first_entry(&ndev_ctx->reconfig_events,
2051 struct netvsc_reconfig, list);
2052 list_del(&event->list);
2053 reschedule = !list_empty(&ndev_ctx->reconfig_events);
2054 }
2055 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2056
2057 if (!event)
2058 goto out_unlock;
2059
2060 switch (event->event) {
2061 /* Only the following events are possible due to the check in
2062 * netvsc_linkstatus_callback()
2063 */
2064 case RNDIS_STATUS_MEDIA_CONNECT:
2065 if (rdev->link_state) {
2066 rdev->link_state = false;
2067 netif_carrier_on(net);
2068 netvsc_tx_enable(net_device, net);
2069 } else {
2070 __netdev_notify_peers(net);
2071 }
2072 kfree(event);
2073 break;
2074 case RNDIS_STATUS_MEDIA_DISCONNECT:
2075 if (!rdev->link_state) {
2076 rdev->link_state = true;
2077 netif_carrier_off(net);
2078 netvsc_tx_disable(net_device, net);
2079 }
2080 kfree(event);
2081 break;
2082 case RNDIS_STATUS_NETWORK_CHANGE:
2083 /* Only makes sense if carrier is present */
2084 if (!rdev->link_state) {
2085 rdev->link_state = true;
2086 netif_carrier_off(net);
2087 netvsc_tx_disable(net_device, net);
2088 event->event = RNDIS_STATUS_MEDIA_CONNECT;
2089 spin_lock_irqsave(&ndev_ctx->lock, flags);
2090 list_add(&event->list, &ndev_ctx->reconfig_events);
2091 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2092 reschedule = true;
2093 }
2094 break;
2095 }
2096
2097 rtnl_unlock();
2098
2099 /* link_watch only sends one notification with current state per
2100 * second, handle next reconfig event in 2 seconds.
2101 */
2102 if (reschedule)
2103 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2104
2105 return;
2106
2107 out_unlock:
2108 rtnl_unlock();
2109 }
2110
get_netvsc_byref(struct net_device * vf_netdev)2111 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
2112 {
2113 struct net_device_context *net_device_ctx;
2114 struct net_device *dev;
2115
2116 dev = netdev_master_upper_dev_get(vf_netdev);
2117 if (!dev || dev->netdev_ops != &device_ops)
2118 return NULL; /* not a netvsc device */
2119
2120 net_device_ctx = netdev_priv(dev);
2121 if (!rtnl_dereference(net_device_ctx->nvdev))
2122 return NULL; /* device is removed */
2123
2124 return dev;
2125 }
2126
2127 /* Called when VF is injecting data into network stack.
2128 * Change the associated network device from VF to netvsc.
2129 * note: already called with rcu_read_lock
2130 */
netvsc_vf_handle_frame(struct sk_buff ** pskb)2131 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
2132 {
2133 struct sk_buff *skb = *pskb;
2134 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
2135 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2136 struct netvsc_vf_pcpu_stats *pcpu_stats
2137 = this_cpu_ptr(ndev_ctx->vf_stats);
2138
2139 skb = skb_share_check(skb, GFP_ATOMIC);
2140 if (unlikely(!skb))
2141 return RX_HANDLER_CONSUMED;
2142
2143 *pskb = skb;
2144
2145 skb->dev = ndev;
2146
2147 u64_stats_update_begin(&pcpu_stats->syncp);
2148 pcpu_stats->rx_packets++;
2149 pcpu_stats->rx_bytes += skb->len;
2150 u64_stats_update_end(&pcpu_stats->syncp);
2151
2152 return RX_HANDLER_ANOTHER;
2153 }
2154
netvsc_vf_join(struct net_device * vf_netdev,struct net_device * ndev,int context)2155 static int netvsc_vf_join(struct net_device *vf_netdev,
2156 struct net_device *ndev, int context)
2157 {
2158 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2159 int ret;
2160
2161 ret = netdev_rx_handler_register(vf_netdev,
2162 netvsc_vf_handle_frame, ndev);
2163 if (ret != 0) {
2164 netdev_err(vf_netdev,
2165 "can not register netvsc VF receive handler (err = %d)\n",
2166 ret);
2167 goto rx_handler_failed;
2168 }
2169
2170 ret = netdev_master_upper_dev_link(vf_netdev, ndev,
2171 NULL, NULL, NULL);
2172 if (ret != 0) {
2173 netdev_err(vf_netdev,
2174 "can not set master device %s (err = %d)\n",
2175 ndev->name, ret);
2176 goto upper_link_failed;
2177 }
2178
2179 /* If this registration is called from probe context vf_takeover
2180 * is taken care of later in probe itself.
2181 */
2182 if (context == VF_REG_IN_NOTIFIER)
2183 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
2184
2185 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
2186
2187 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
2188 return 0;
2189
2190 upper_link_failed:
2191 netdev_rx_handler_unregister(vf_netdev);
2192 rx_handler_failed:
2193 return ret;
2194 }
2195
__netvsc_vf_setup(struct net_device * ndev,struct net_device * vf_netdev)2196 static void __netvsc_vf_setup(struct net_device *ndev,
2197 struct net_device *vf_netdev)
2198 {
2199 int ret;
2200
2201 /* Align MTU of VF with master */
2202 ret = dev_set_mtu(vf_netdev, ndev->mtu);
2203 if (ret)
2204 netdev_warn(vf_netdev,
2205 "unable to change mtu to %u\n", ndev->mtu);
2206
2207 /* set multicast etc flags on VF */
2208 dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE, NULL);
2209
2210 /* sync address list from ndev to VF */
2211 netif_addr_lock_bh(ndev);
2212 dev_uc_sync(vf_netdev, ndev);
2213 dev_mc_sync(vf_netdev, ndev);
2214 netif_addr_unlock_bh(ndev);
2215
2216 if (netif_running(ndev)) {
2217 ret = dev_open(vf_netdev, NULL);
2218 if (ret)
2219 netdev_warn(vf_netdev,
2220 "unable to open: %d\n", ret);
2221 }
2222 }
2223
2224 /* Setup VF as slave of the synthetic device.
2225 * Runs in workqueue to avoid recursion in netlink callbacks.
2226 */
netvsc_vf_setup(struct work_struct * w)2227 static void netvsc_vf_setup(struct work_struct *w)
2228 {
2229 struct net_device_context *ndev_ctx
2230 = container_of(w, struct net_device_context, vf_takeover.work);
2231 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2232 struct net_device *vf_netdev;
2233
2234 if (!rtnl_trylock()) {
2235 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
2236 return;
2237 }
2238
2239 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2240 if (vf_netdev)
2241 __netvsc_vf_setup(ndev, vf_netdev);
2242
2243 rtnl_unlock();
2244 }
2245
2246 /* Find netvsc by VF serial number.
2247 * The PCI hyperv controller records the serial number as the slot kobj name.
2248 */
get_netvsc_byslot(const struct net_device * vf_netdev)2249 static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
2250 {
2251 struct device *parent = vf_netdev->dev.parent;
2252 struct net_device_context *ndev_ctx;
2253 struct net_device *ndev;
2254 struct pci_dev *pdev;
2255 u32 serial;
2256
2257 if (!parent || !dev_is_pci(parent))
2258 return NULL; /* not a PCI device */
2259
2260 pdev = to_pci_dev(parent);
2261 if (!pdev->slot) {
2262 netdev_notice(vf_netdev, "no PCI slot information\n");
2263 return NULL;
2264 }
2265
2266 if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
2267 netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
2268 pci_slot_name(pdev->slot));
2269 return NULL;
2270 }
2271
2272 list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
2273 if (!ndev_ctx->vf_alloc)
2274 continue;
2275
2276 if (ndev_ctx->vf_serial != serial)
2277 continue;
2278
2279 ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2280 if (ndev->addr_len != vf_netdev->addr_len ||
2281 memcmp(ndev->perm_addr, vf_netdev->perm_addr,
2282 ndev->addr_len) != 0)
2283 continue;
2284
2285 return ndev;
2286
2287 }
2288
2289 /* Fallback path to check synthetic vf with help of mac addr.
2290 * Because this function can be called before vf_netdev is
2291 * initialized (NETDEV_POST_INIT) when its perm_addr has not been copied
2292 * from dev_addr, also try to match to its dev_addr.
2293 * Note: On Hyper-V and Azure, it's not possible to set a MAC address
2294 * on a VF that matches to the MAC of a unrelated NETVSC device.
2295 */
2296 list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
2297 ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2298 if (ether_addr_equal(vf_netdev->perm_addr, ndev->perm_addr) ||
2299 ether_addr_equal(vf_netdev->dev_addr, ndev->perm_addr))
2300 return ndev;
2301 }
2302
2303 netdev_notice(vf_netdev,
2304 "no netdev found for vf serial:%u\n", serial);
2305 return NULL;
2306 }
2307
netvsc_prepare_bonding(struct net_device * vf_netdev)2308 static int netvsc_prepare_bonding(struct net_device *vf_netdev)
2309 {
2310 struct net_device *ndev;
2311
2312 ndev = get_netvsc_byslot(vf_netdev);
2313 if (!ndev)
2314 return NOTIFY_DONE;
2315
2316 /* set slave flag before open to prevent IPv6 addrconf */
2317 vf_netdev->flags |= IFF_SLAVE;
2318 return NOTIFY_DONE;
2319 }
2320
netvsc_register_vf(struct net_device * vf_netdev,int context)2321 static int netvsc_register_vf(struct net_device *vf_netdev, int context)
2322 {
2323 struct net_device_context *net_device_ctx;
2324 struct netvsc_device *netvsc_dev;
2325 struct bpf_prog *prog;
2326 struct net_device *ndev;
2327 int ret;
2328
2329 if (vf_netdev->addr_len != ETH_ALEN)
2330 return NOTIFY_DONE;
2331
2332 ndev = get_netvsc_byslot(vf_netdev);
2333 if (!ndev)
2334 return NOTIFY_DONE;
2335
2336 net_device_ctx = netdev_priv(ndev);
2337 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2338 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
2339 return NOTIFY_DONE;
2340
2341 /* if synthetic interface is a different namespace,
2342 * then move the VF to that namespace; join will be
2343 * done again in that context.
2344 */
2345 if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
2346 ret = dev_change_net_namespace(vf_netdev,
2347 dev_net(ndev), "eth%d");
2348 if (ret)
2349 netdev_err(vf_netdev,
2350 "could not move to same namespace as %s: %d\n",
2351 ndev->name, ret);
2352 else
2353 netdev_info(vf_netdev,
2354 "VF moved to namespace with: %s\n",
2355 ndev->name);
2356 return NOTIFY_DONE;
2357 }
2358
2359 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
2360
2361 if (netvsc_vf_join(vf_netdev, ndev, context) != 0)
2362 return NOTIFY_DONE;
2363
2364 dev_hold(vf_netdev);
2365 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
2366
2367 if (ndev->needed_headroom < vf_netdev->needed_headroom)
2368 ndev->needed_headroom = vf_netdev->needed_headroom;
2369
2370 vf_netdev->wanted_features = ndev->features;
2371 netdev_update_features(vf_netdev);
2372
2373 prog = netvsc_xdp_get(netvsc_dev);
2374 netvsc_vf_setxdp(vf_netdev, prog);
2375
2376 return NOTIFY_OK;
2377 }
2378
2379 /* Change the data path when VF UP/DOWN/CHANGE are detected.
2380 *
2381 * Typically a UP or DOWN event is followed by a CHANGE event, so
2382 * net_device_ctx->data_path_is_vf is used to cache the current data path
2383 * to avoid the duplicate call of netvsc_switch_datapath() and the duplicate
2384 * message.
2385 *
2386 * During hibernation, if a VF NIC driver (e.g. mlx5) preserves the network
2387 * interface, there is only the CHANGE event and no UP or DOWN event.
2388 */
netvsc_vf_changed(struct net_device * vf_netdev,unsigned long event)2389 static int netvsc_vf_changed(struct net_device *vf_netdev, unsigned long event)
2390 {
2391 struct net_device_context *net_device_ctx;
2392 struct netvsc_device *netvsc_dev;
2393 struct net_device *ndev;
2394 bool vf_is_up = false;
2395 int ret;
2396
2397 if (event != NETDEV_GOING_DOWN)
2398 vf_is_up = netif_running(vf_netdev);
2399
2400 ndev = get_netvsc_byref(vf_netdev);
2401 if (!ndev)
2402 return NOTIFY_DONE;
2403
2404 net_device_ctx = netdev_priv(ndev);
2405 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2406 if (!netvsc_dev)
2407 return NOTIFY_DONE;
2408
2409 if (net_device_ctx->data_path_is_vf == vf_is_up)
2410 return NOTIFY_OK;
2411
2412 if (vf_is_up && !net_device_ctx->vf_alloc) {
2413 netdev_info(ndev, "Waiting for the VF association from host\n");
2414 wait_for_completion(&net_device_ctx->vf_add);
2415 }
2416
2417 ret = netvsc_switch_datapath(ndev, vf_is_up);
2418
2419 if (ret) {
2420 netdev_err(ndev,
2421 "Data path failed to switch %s VF: %s, err: %d\n",
2422 vf_is_up ? "to" : "from", vf_netdev->name, ret);
2423 return NOTIFY_DONE;
2424 } else {
2425 netdev_info(ndev, "Data path switched %s VF: %s\n",
2426 vf_is_up ? "to" : "from", vf_netdev->name);
2427 }
2428
2429 return NOTIFY_OK;
2430 }
2431
netvsc_unregister_vf(struct net_device * vf_netdev)2432 static int netvsc_unregister_vf(struct net_device *vf_netdev)
2433 {
2434 struct net_device *ndev;
2435 struct net_device_context *net_device_ctx;
2436
2437 ndev = get_netvsc_byref(vf_netdev);
2438 if (!ndev)
2439 return NOTIFY_DONE;
2440
2441 net_device_ctx = netdev_priv(ndev);
2442 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
2443
2444 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
2445
2446 netvsc_vf_setxdp(vf_netdev, NULL);
2447
2448 reinit_completion(&net_device_ctx->vf_add);
2449 netdev_rx_handler_unregister(vf_netdev);
2450 netdev_upper_dev_unlink(vf_netdev, ndev);
2451 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
2452 dev_put(vf_netdev);
2453
2454 ndev->needed_headroom = RNDIS_AND_PPI_SIZE;
2455
2456 return NOTIFY_OK;
2457 }
2458
check_dev_is_matching_vf(struct net_device * event_ndev)2459 static int check_dev_is_matching_vf(struct net_device *event_ndev)
2460 {
2461 /* Skip NetVSC interfaces */
2462 if (event_ndev->netdev_ops == &device_ops)
2463 return -ENODEV;
2464
2465 /* Avoid non-Ethernet type devices */
2466 if (event_ndev->type != ARPHRD_ETHER)
2467 return -ENODEV;
2468
2469 /* Avoid Vlan dev with same MAC registering as VF */
2470 if (is_vlan_dev(event_ndev))
2471 return -ENODEV;
2472
2473 /* Avoid Bonding master dev with same MAC registering as VF */
2474 if (netif_is_bond_master(event_ndev))
2475 return -ENODEV;
2476
2477 return 0;
2478 }
2479
netvsc_probe(struct hv_device * dev,const struct hv_vmbus_device_id * dev_id)2480 static int netvsc_probe(struct hv_device *dev,
2481 const struct hv_vmbus_device_id *dev_id)
2482 {
2483 struct net_device *net = NULL, *vf_netdev;
2484 struct net_device_context *net_device_ctx;
2485 struct netvsc_device_info *device_info = NULL;
2486 struct netvsc_device *nvdev;
2487 int ret = -ENOMEM;
2488
2489 net = alloc_etherdev_mq(sizeof(struct net_device_context),
2490 VRSS_CHANNEL_MAX);
2491 if (!net)
2492 goto no_net;
2493
2494 netif_carrier_off(net);
2495
2496 netvsc_init_settings(net);
2497
2498 net_device_ctx = netdev_priv(net);
2499 net_device_ctx->device_ctx = dev;
2500 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
2501 if (netif_msg_probe(net_device_ctx))
2502 netdev_dbg(net, "netvsc msg_enable: %d\n",
2503 net_device_ctx->msg_enable);
2504
2505 hv_set_drvdata(dev, net);
2506
2507 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
2508
2509 init_completion(&net_device_ctx->vf_add);
2510 spin_lock_init(&net_device_ctx->lock);
2511 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
2512 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
2513
2514 net_device_ctx->vf_stats
2515 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
2516 if (!net_device_ctx->vf_stats)
2517 goto no_stats;
2518
2519 net->netdev_ops = &device_ops;
2520 net->ethtool_ops = ðtool_ops;
2521 SET_NETDEV_DEV(net, &dev->device);
2522 dma_set_min_align_mask(&dev->device, HV_HYP_PAGE_SIZE - 1);
2523
2524 /* We always need headroom for rndis header */
2525 net->needed_headroom = RNDIS_AND_PPI_SIZE;
2526
2527 /* Initialize the number of queues to be 1, we may change it if more
2528 * channels are offered later.
2529 */
2530 netif_set_real_num_tx_queues(net, 1);
2531 netif_set_real_num_rx_queues(net, 1);
2532
2533 /* Notify the netvsc driver of the new device */
2534 device_info = netvsc_devinfo_get(NULL);
2535
2536 if (!device_info) {
2537 ret = -ENOMEM;
2538 goto devinfo_failed;
2539 }
2540
2541 /* We must get rtnl lock before scheduling nvdev->subchan_work,
2542 * otherwise netvsc_subchan_work() can get rtnl lock first and wait
2543 * all subchannels to show up, but that may not happen because
2544 * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2545 * -> ... -> device_add() -> ... -> __device_attach() can't get
2546 * the device lock, so all the subchannels can't be processed --
2547 * finally netvsc_subchan_work() hangs forever.
2548 *
2549 * The rtnl lock also needs to be held before rndis_filter_device_add()
2550 * which advertises nvsp_2_vsc_capability / sriov bit, and triggers
2551 * VF NIC offering and registering. If VF NIC finished register_netdev()
2552 * earlier it may cause name based config failure.
2553 */
2554 rtnl_lock();
2555
2556 nvdev = rndis_filter_device_add(dev, device_info);
2557 if (IS_ERR(nvdev)) {
2558 ret = PTR_ERR(nvdev);
2559 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2560 goto rndis_failed;
2561 }
2562
2563 eth_hw_addr_set(net, device_info->mac_adr);
2564
2565 if (nvdev->num_chn > 1)
2566 schedule_work(&nvdev->subchan_work);
2567
2568 /* hw_features computed in rndis_netdev_set_hwcaps() */
2569 net->features = net->hw_features |
2570 NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX |
2571 NETIF_F_HW_VLAN_CTAG_RX;
2572 net->vlan_features = net->features;
2573
2574 netdev_lockdep_set_classes(net);
2575
2576 net->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
2577 NETDEV_XDP_ACT_NDO_XMIT;
2578
2579 /* MTU range: 68 - 1500 or 65521 */
2580 net->min_mtu = NETVSC_MTU_MIN;
2581 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2582 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2583 else
2584 net->max_mtu = ETH_DATA_LEN;
2585
2586 nvdev->tx_disable = false;
2587
2588 ret = register_netdevice(net);
2589 if (ret != 0) {
2590 pr_err("Unable to register netdev.\n");
2591 goto register_failed;
2592 }
2593
2594 list_add(&net_device_ctx->list, &netvsc_dev_list);
2595
2596 /* When the hv_netvsc driver is unloaded and reloaded, the
2597 * NET_DEVICE_REGISTER for the vf device is replayed before probe
2598 * is complete. This is because register_netdevice_notifier() gets
2599 * registered before vmbus_driver_register() so that callback func
2600 * is set before probe and we don't miss events like NETDEV_POST_INIT
2601 * So, in this section we try to register the matching vf device that
2602 * is present as a netdevice, knowing that its register call is not
2603 * processed in the netvsc_netdev_notifier(as probing is progress and
2604 * get_netvsc_byslot fails).
2605 */
2606 for_each_netdev(dev_net(net), vf_netdev) {
2607 ret = check_dev_is_matching_vf(vf_netdev);
2608 if (ret != 0)
2609 continue;
2610
2611 if (net != get_netvsc_byslot(vf_netdev))
2612 continue;
2613
2614 netvsc_prepare_bonding(vf_netdev);
2615 netvsc_register_vf(vf_netdev, VF_REG_IN_PROBE);
2616 __netvsc_vf_setup(net, vf_netdev);
2617 break;
2618 }
2619 rtnl_unlock();
2620
2621 netvsc_devinfo_put(device_info);
2622 return 0;
2623
2624 register_failed:
2625 rndis_filter_device_remove(dev, nvdev);
2626 rndis_failed:
2627 rtnl_unlock();
2628 netvsc_devinfo_put(device_info);
2629 devinfo_failed:
2630 free_percpu(net_device_ctx->vf_stats);
2631 no_stats:
2632 hv_set_drvdata(dev, NULL);
2633 free_netdev(net);
2634 no_net:
2635 return ret;
2636 }
2637
netvsc_remove(struct hv_device * dev)2638 static void netvsc_remove(struct hv_device *dev)
2639 {
2640 struct net_device_context *ndev_ctx;
2641 struct net_device *vf_netdev, *net;
2642 struct netvsc_device *nvdev;
2643
2644 net = hv_get_drvdata(dev);
2645 if (net == NULL) {
2646 dev_err(&dev->device, "No net device to remove\n");
2647 return;
2648 }
2649
2650 ndev_ctx = netdev_priv(net);
2651
2652 cancel_delayed_work_sync(&ndev_ctx->dwork);
2653
2654 rtnl_lock();
2655 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2656 if (nvdev) {
2657 cancel_work_sync(&nvdev->subchan_work);
2658 netvsc_xdp_set(net, NULL, NULL, nvdev);
2659 }
2660
2661 /*
2662 * Call to the vsc driver to let it know that the device is being
2663 * removed. Also blocks mtu and channel changes.
2664 */
2665 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2666 if (vf_netdev)
2667 netvsc_unregister_vf(vf_netdev);
2668
2669 if (nvdev)
2670 rndis_filter_device_remove(dev, nvdev);
2671
2672 unregister_netdevice(net);
2673 list_del(&ndev_ctx->list);
2674
2675 rtnl_unlock();
2676
2677 hv_set_drvdata(dev, NULL);
2678
2679 free_percpu(ndev_ctx->vf_stats);
2680 free_netdev(net);
2681 }
2682
netvsc_suspend(struct hv_device * dev)2683 static int netvsc_suspend(struct hv_device *dev)
2684 {
2685 struct net_device_context *ndev_ctx;
2686 struct netvsc_device *nvdev;
2687 struct net_device *net;
2688 int ret;
2689
2690 net = hv_get_drvdata(dev);
2691
2692 ndev_ctx = netdev_priv(net);
2693 cancel_delayed_work_sync(&ndev_ctx->dwork);
2694
2695 rtnl_lock();
2696
2697 nvdev = rtnl_dereference(ndev_ctx->nvdev);
2698 if (nvdev == NULL) {
2699 ret = -ENODEV;
2700 goto out;
2701 }
2702
2703 /* Save the current config info */
2704 ndev_ctx->saved_netvsc_dev_info = netvsc_devinfo_get(nvdev);
2705 if (!ndev_ctx->saved_netvsc_dev_info) {
2706 ret = -ENOMEM;
2707 goto out;
2708 }
2709 ret = netvsc_detach(net, nvdev);
2710 out:
2711 rtnl_unlock();
2712
2713 return ret;
2714 }
2715
netvsc_resume(struct hv_device * dev)2716 static int netvsc_resume(struct hv_device *dev)
2717 {
2718 struct net_device *net = hv_get_drvdata(dev);
2719 struct net_device_context *net_device_ctx;
2720 struct netvsc_device_info *device_info;
2721 int ret;
2722
2723 rtnl_lock();
2724
2725 net_device_ctx = netdev_priv(net);
2726
2727 /* Reset the data path to the netvsc NIC before re-opening the vmbus
2728 * channel. Later netvsc_netdev_event() will switch the data path to
2729 * the VF upon the UP or CHANGE event.
2730 */
2731 net_device_ctx->data_path_is_vf = false;
2732 device_info = net_device_ctx->saved_netvsc_dev_info;
2733
2734 ret = netvsc_attach(net, device_info);
2735
2736 netvsc_devinfo_put(device_info);
2737 net_device_ctx->saved_netvsc_dev_info = NULL;
2738
2739 rtnl_unlock();
2740
2741 return ret;
2742 }
2743 static const struct hv_vmbus_device_id id_table[] = {
2744 /* Network guid */
2745 { HV_NIC_GUID, },
2746 { },
2747 };
2748
2749 MODULE_DEVICE_TABLE(vmbus, id_table);
2750
2751 /* The one and only one */
2752 static struct hv_driver netvsc_drv = {
2753 .name = KBUILD_MODNAME,
2754 .id_table = id_table,
2755 .probe = netvsc_probe,
2756 .remove = netvsc_remove,
2757 .suspend = netvsc_suspend,
2758 .resume = netvsc_resume,
2759 .driver = {
2760 .probe_type = PROBE_FORCE_SYNCHRONOUS,
2761 },
2762 };
2763
2764 /* Set VF's namespace same as the synthetic NIC */
netvsc_event_set_vf_ns(struct net_device * ndev)2765 static void netvsc_event_set_vf_ns(struct net_device *ndev)
2766 {
2767 struct net_device_context *ndev_ctx = netdev_priv(ndev);
2768 struct net_device *vf_netdev;
2769 int ret;
2770
2771 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2772 if (!vf_netdev)
2773 return;
2774
2775 if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
2776 ret = dev_change_net_namespace(vf_netdev, dev_net(ndev),
2777 "eth%d");
2778 if (ret)
2779 netdev_err(vf_netdev,
2780 "Cannot move to same namespace as %s: %d\n",
2781 ndev->name, ret);
2782 else
2783 netdev_info(vf_netdev,
2784 "Moved VF to namespace with: %s\n",
2785 ndev->name);
2786 }
2787 }
2788
2789 /*
2790 * On Hyper-V, every VF interface is matched with a corresponding
2791 * synthetic interface. The synthetic interface is presented first
2792 * to the guest. When the corresponding VF instance is registered,
2793 * we will take care of switching the data path.
2794 */
netvsc_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)2795 static int netvsc_netdev_event(struct notifier_block *this,
2796 unsigned long event, void *ptr)
2797 {
2798 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2799 int ret = 0;
2800
2801 if (event_dev->netdev_ops == &device_ops && event == NETDEV_REGISTER) {
2802 netvsc_event_set_vf_ns(event_dev);
2803 return NOTIFY_DONE;
2804 }
2805
2806 ret = check_dev_is_matching_vf(event_dev);
2807 if (ret != 0)
2808 return NOTIFY_DONE;
2809
2810 switch (event) {
2811 case NETDEV_POST_INIT:
2812 return netvsc_prepare_bonding(event_dev);
2813 case NETDEV_REGISTER:
2814 return netvsc_register_vf(event_dev, VF_REG_IN_NOTIFIER);
2815 case NETDEV_UNREGISTER:
2816 return netvsc_unregister_vf(event_dev);
2817 case NETDEV_UP:
2818 case NETDEV_DOWN:
2819 case NETDEV_CHANGE:
2820 case NETDEV_GOING_DOWN:
2821 return netvsc_vf_changed(event_dev, event);
2822 default:
2823 return NOTIFY_DONE;
2824 }
2825 }
2826
2827 static struct notifier_block netvsc_netdev_notifier = {
2828 .notifier_call = netvsc_netdev_event,
2829 };
2830
netvsc_drv_exit(void)2831 static void __exit netvsc_drv_exit(void)
2832 {
2833 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2834 vmbus_driver_unregister(&netvsc_drv);
2835 }
2836
netvsc_drv_init(void)2837 static int __init netvsc_drv_init(void)
2838 {
2839 int ret;
2840
2841 if (ring_size < RING_SIZE_MIN) {
2842 ring_size = RING_SIZE_MIN;
2843 pr_info("Increased ring_size to %u (min allowed)\n",
2844 ring_size);
2845 }
2846 netvsc_ring_bytes = VMBUS_RING_SIZE(ring_size * 4096);
2847
2848 register_netdevice_notifier(&netvsc_netdev_notifier);
2849
2850 ret = vmbus_driver_register(&netvsc_drv);
2851 if (ret)
2852 goto err_vmbus_reg;
2853
2854 return 0;
2855
2856 err_vmbus_reg:
2857 unregister_netdevice_notifier(&netvsc_netdev_notifier);
2858 return ret;
2859 }
2860
2861 MODULE_LICENSE("GPL");
2862 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2863
2864 module_init(netvsc_drv_init);
2865 module_exit(netvsc_drv_exit);
2866