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