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