1 /* 2 * Copyright (c) 2009, Microsoft Corporation. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program; if not, see <http://www.gnu.org/licenses/>. 15 * 16 * Authors: 17 * Haiyang Zhang <haiyangz@microsoft.com> 18 * Hank Janssen <hjanssen@microsoft.com> 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/skbuff.h> 33 #include <linux/if_vlan.h> 34 #include <linux/in.h> 35 #include <linux/slab.h> 36 #include <net/arp.h> 37 #include <net/route.h> 38 #include <net/sock.h> 39 #include <net/pkt_sched.h> 40 41 #include "hyperv_net.h" 42 43 #define RING_SIZE_MIN 64 44 #define LINKCHANGE_INT (2 * HZ) 45 46 static int ring_size = 128; 47 module_param(ring_size, int, S_IRUGO); 48 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); 49 50 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | 51 NETIF_MSG_LINK | NETIF_MSG_IFUP | 52 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | 53 NETIF_MSG_TX_ERR; 54 55 static int debug = -1; 56 module_param(debug, int, S_IRUGO); 57 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); 58 59 static void do_set_multicast(struct work_struct *w) 60 { 61 struct net_device_context *ndevctx = 62 container_of(w, struct net_device_context, work); 63 struct hv_device *device_obj = ndevctx->device_ctx; 64 struct net_device *ndev = hv_get_drvdata(device_obj); 65 struct netvsc_device *nvdev = ndevctx->nvdev; 66 struct rndis_device *rdev; 67 68 if (!nvdev) 69 return; 70 71 rdev = nvdev->extension; 72 if (rdev == NULL) 73 return; 74 75 if (ndev->flags & IFF_PROMISC) 76 rndis_filter_set_packet_filter(rdev, 77 NDIS_PACKET_TYPE_PROMISCUOUS); 78 else 79 rndis_filter_set_packet_filter(rdev, 80 NDIS_PACKET_TYPE_BROADCAST | 81 NDIS_PACKET_TYPE_ALL_MULTICAST | 82 NDIS_PACKET_TYPE_DIRECTED); 83 } 84 85 static void netvsc_set_multicast_list(struct net_device *net) 86 { 87 struct net_device_context *net_device_ctx = netdev_priv(net); 88 89 schedule_work(&net_device_ctx->work); 90 } 91 92 static int netvsc_open(struct net_device *net) 93 { 94 struct netvsc_device *nvdev = net_device_to_netvsc_device(net); 95 struct rndis_device *rdev; 96 int ret = 0; 97 98 netif_carrier_off(net); 99 100 /* Open up the device */ 101 ret = rndis_filter_open(nvdev); 102 if (ret != 0) { 103 netdev_err(net, "unable to open device (ret %d).\n", ret); 104 return ret; 105 } 106 107 netif_tx_wake_all_queues(net); 108 109 rdev = nvdev->extension; 110 if (!rdev->link_state) 111 netif_carrier_on(net); 112 113 return ret; 114 } 115 116 static int netvsc_close(struct net_device *net) 117 { 118 struct net_device_context *net_device_ctx = netdev_priv(net); 119 struct netvsc_device *nvdev = net_device_ctx->nvdev; 120 int ret; 121 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20; 122 struct vmbus_channel *chn; 123 124 netif_tx_disable(net); 125 126 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */ 127 cancel_work_sync(&net_device_ctx->work); 128 ret = rndis_filter_close(nvdev); 129 if (ret != 0) { 130 netdev_err(net, "unable to close device (ret %d).\n", ret); 131 return ret; 132 } 133 134 /* Ensure pending bytes in ring are read */ 135 while (true) { 136 aread = 0; 137 for (i = 0; i < nvdev->num_chn; i++) { 138 chn = nvdev->chan_table[i].channel; 139 if (!chn) 140 continue; 141 142 hv_get_ringbuffer_availbytes(&chn->inbound, &aread, 143 &awrite); 144 145 if (aread) 146 break; 147 148 hv_get_ringbuffer_availbytes(&chn->outbound, &aread, 149 &awrite); 150 151 if (aread) 152 break; 153 } 154 155 retry++; 156 if (retry > retry_max || aread == 0) 157 break; 158 159 msleep(msec); 160 161 if (msec < 1000) 162 msec *= 2; 163 } 164 165 if (aread) { 166 netdev_err(net, "Ring buffer not empty after closing rndis\n"); 167 ret = -ETIMEDOUT; 168 } 169 170 return ret; 171 } 172 173 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, 174 int pkt_type) 175 { 176 struct rndis_packet *rndis_pkt; 177 struct rndis_per_packet_info *ppi; 178 179 rndis_pkt = &msg->msg.pkt; 180 rndis_pkt->data_offset += ppi_size; 181 182 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + 183 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); 184 185 ppi->size = ppi_size; 186 ppi->type = pkt_type; 187 ppi->ppi_offset = sizeof(struct rndis_per_packet_info); 188 189 rndis_pkt->per_pkt_info_len += ppi_size; 190 191 return ppi; 192 } 193 194 /* 195 * Select queue for transmit. 196 * 197 * If a valid queue has already been assigned, then use that. 198 * Otherwise compute tx queue based on hash and the send table. 199 * 200 * This is basically similar to default (__netdev_pick_tx) with the added step 201 * of using the host send_table when no other queue has been assigned. 202 * 203 * TODO support XPS - but get_xps_queue not exported 204 */ 205 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, 206 void *accel_priv, select_queue_fallback_t fallback) 207 { 208 struct net_device_context *net_device_ctx = netdev_priv(ndev); 209 unsigned int num_tx_queues = ndev->real_num_tx_queues; 210 struct sock *sk = skb->sk; 211 int q_idx = sk_tx_queue_get(sk); 212 213 if (q_idx < 0 || skb->ooo_okay || q_idx >= num_tx_queues) { 214 u16 hash = __skb_tx_hash(ndev, skb, VRSS_SEND_TAB_SIZE); 215 int new_idx; 216 217 new_idx = net_device_ctx->tx_send_table[hash] % num_tx_queues; 218 219 if (q_idx != new_idx && sk && 220 sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) 221 sk_tx_queue_set(sk, new_idx); 222 223 q_idx = new_idx; 224 } 225 226 return q_idx; 227 } 228 229 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, 230 struct hv_page_buffer *pb) 231 { 232 int j = 0; 233 234 /* Deal with compund pages by ignoring unused part 235 * of the page. 236 */ 237 page += (offset >> PAGE_SHIFT); 238 offset &= ~PAGE_MASK; 239 240 while (len > 0) { 241 unsigned long bytes; 242 243 bytes = PAGE_SIZE - offset; 244 if (bytes > len) 245 bytes = len; 246 pb[j].pfn = page_to_pfn(page); 247 pb[j].offset = offset; 248 pb[j].len = bytes; 249 250 offset += bytes; 251 len -= bytes; 252 253 if (offset == PAGE_SIZE && len) { 254 page++; 255 offset = 0; 256 j++; 257 } 258 } 259 260 return j + 1; 261 } 262 263 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, 264 struct hv_netvsc_packet *packet, 265 struct hv_page_buffer **page_buf) 266 { 267 struct hv_page_buffer *pb = *page_buf; 268 u32 slots_used = 0; 269 char *data = skb->data; 270 int frags = skb_shinfo(skb)->nr_frags; 271 int i; 272 273 /* The packet is laid out thus: 274 * 1. hdr: RNDIS header and PPI 275 * 2. skb linear data 276 * 3. skb fragment data 277 */ 278 if (hdr != NULL) 279 slots_used += fill_pg_buf(virt_to_page(hdr), 280 offset_in_page(hdr), 281 len, &pb[slots_used]); 282 283 packet->rmsg_size = len; 284 packet->rmsg_pgcnt = slots_used; 285 286 slots_used += fill_pg_buf(virt_to_page(data), 287 offset_in_page(data), 288 skb_headlen(skb), &pb[slots_used]); 289 290 for (i = 0; i < frags; i++) { 291 skb_frag_t *frag = skb_shinfo(skb)->frags + i; 292 293 slots_used += fill_pg_buf(skb_frag_page(frag), 294 frag->page_offset, 295 skb_frag_size(frag), &pb[slots_used]); 296 } 297 return slots_used; 298 } 299 300 static int count_skb_frag_slots(struct sk_buff *skb) 301 { 302 int i, frags = skb_shinfo(skb)->nr_frags; 303 int pages = 0; 304 305 for (i = 0; i < frags; i++) { 306 skb_frag_t *frag = skb_shinfo(skb)->frags + i; 307 unsigned long size = skb_frag_size(frag); 308 unsigned long offset = frag->page_offset; 309 310 /* Skip unused frames from start of page */ 311 offset &= ~PAGE_MASK; 312 pages += PFN_UP(offset + size); 313 } 314 return pages; 315 } 316 317 static int netvsc_get_slots(struct sk_buff *skb) 318 { 319 char *data = skb->data; 320 unsigned int offset = offset_in_page(data); 321 unsigned int len = skb_headlen(skb); 322 int slots; 323 int frag_slots; 324 325 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); 326 frag_slots = count_skb_frag_slots(skb); 327 return slots + frag_slots; 328 } 329 330 static u32 net_checksum_info(struct sk_buff *skb) 331 { 332 if (skb->protocol == htons(ETH_P_IP)) { 333 struct iphdr *ip = ip_hdr(skb); 334 335 if (ip->protocol == IPPROTO_TCP) 336 return TRANSPORT_INFO_IPV4_TCP; 337 else if (ip->protocol == IPPROTO_UDP) 338 return TRANSPORT_INFO_IPV4_UDP; 339 } else { 340 struct ipv6hdr *ip6 = ipv6_hdr(skb); 341 342 if (ip6->nexthdr == IPPROTO_TCP) 343 return TRANSPORT_INFO_IPV6_TCP; 344 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP) 345 return TRANSPORT_INFO_IPV6_UDP; 346 } 347 348 return TRANSPORT_INFO_NOT_IP; 349 } 350 351 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) 352 { 353 struct net_device_context *net_device_ctx = netdev_priv(net); 354 struct hv_netvsc_packet *packet = NULL; 355 int ret; 356 unsigned int num_data_pgs; 357 struct rndis_message *rndis_msg; 358 struct rndis_packet *rndis_pkt; 359 u32 rndis_msg_size; 360 struct rndis_per_packet_info *ppi; 361 u32 hash; 362 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT]; 363 struct hv_page_buffer *pb = page_buf; 364 365 /* We will atmost need two pages to describe the rndis 366 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number 367 * of pages in a single packet. If skb is scattered around 368 * more pages we try linearizing it. 369 */ 370 371 num_data_pgs = netvsc_get_slots(skb) + 2; 372 373 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { 374 ++net_device_ctx->eth_stats.tx_scattered; 375 376 if (skb_linearize(skb)) 377 goto no_memory; 378 379 num_data_pgs = netvsc_get_slots(skb) + 2; 380 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { 381 ++net_device_ctx->eth_stats.tx_too_big; 382 goto drop; 383 } 384 } 385 386 /* 387 * Place the rndis header in the skb head room and 388 * the skb->cb will be used for hv_netvsc_packet 389 * structure. 390 */ 391 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); 392 if (ret) 393 goto no_memory; 394 395 /* Use the skb control buffer for building up the packet */ 396 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > 397 FIELD_SIZEOF(struct sk_buff, cb)); 398 packet = (struct hv_netvsc_packet *)skb->cb; 399 400 packet->q_idx = skb_get_queue_mapping(skb); 401 402 packet->total_data_buflen = skb->len; 403 packet->total_bytes = skb->len; 404 packet->total_packets = 1; 405 406 rndis_msg = (struct rndis_message *)skb->head; 407 408 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); 409 410 /* Add the rndis header */ 411 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; 412 rndis_msg->msg_len = packet->total_data_buflen; 413 rndis_pkt = &rndis_msg->msg.pkt; 414 rndis_pkt->data_offset = sizeof(struct rndis_packet); 415 rndis_pkt->data_len = packet->total_data_buflen; 416 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); 417 418 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); 419 420 hash = skb_get_hash_raw(skb); 421 if (hash != 0 && net->real_num_tx_queues > 1) { 422 rndis_msg_size += NDIS_HASH_PPI_SIZE; 423 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, 424 NBL_HASH_VALUE); 425 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; 426 } 427 428 if (skb_vlan_tag_present(skb)) { 429 struct ndis_pkt_8021q_info *vlan; 430 431 rndis_msg_size += NDIS_VLAN_PPI_SIZE; 432 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, 433 IEEE_8021Q_INFO); 434 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi + 435 ppi->ppi_offset); 436 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; 437 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> 438 VLAN_PRIO_SHIFT; 439 } 440 441 if (skb_is_gso(skb)) { 442 struct ndis_tcp_lso_info *lso_info; 443 444 rndis_msg_size += NDIS_LSO_PPI_SIZE; 445 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, 446 TCP_LARGESEND_PKTINFO); 447 448 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi + 449 ppi->ppi_offset); 450 451 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; 452 if (skb->protocol == htons(ETH_P_IP)) { 453 lso_info->lso_v2_transmit.ip_version = 454 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; 455 ip_hdr(skb)->tot_len = 0; 456 ip_hdr(skb)->check = 0; 457 tcp_hdr(skb)->check = 458 ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 459 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); 460 } else { 461 lso_info->lso_v2_transmit.ip_version = 462 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; 463 ipv6_hdr(skb)->payload_len = 0; 464 tcp_hdr(skb)->check = 465 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 466 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); 467 } 468 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); 469 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; 470 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 471 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { 472 struct ndis_tcp_ip_checksum_info *csum_info; 473 474 rndis_msg_size += NDIS_CSUM_PPI_SIZE; 475 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, 476 TCPIP_CHKSUM_PKTINFO); 477 478 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + 479 ppi->ppi_offset); 480 481 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); 482 483 if (skb->protocol == htons(ETH_P_IP)) { 484 csum_info->transmit.is_ipv4 = 1; 485 486 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 487 csum_info->transmit.tcp_checksum = 1; 488 else 489 csum_info->transmit.udp_checksum = 1; 490 } else { 491 csum_info->transmit.is_ipv6 = 1; 492 493 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) 494 csum_info->transmit.tcp_checksum = 1; 495 else 496 csum_info->transmit.udp_checksum = 1; 497 } 498 } else { 499 /* Can't do offload of this type of checksum */ 500 if (skb_checksum_help(skb)) 501 goto drop; 502 } 503 } 504 505 /* Start filling in the page buffers with the rndis hdr */ 506 rndis_msg->msg_len += rndis_msg_size; 507 packet->total_data_buflen = rndis_msg->msg_len; 508 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, 509 skb, packet, &pb); 510 511 /* timestamp packet in software */ 512 skb_tx_timestamp(skb); 513 ret = netvsc_send(net_device_ctx->device_ctx, packet, 514 rndis_msg, &pb, skb); 515 if (likely(ret == 0)) 516 return NETDEV_TX_OK; 517 518 if (ret == -EAGAIN) { 519 ++net_device_ctx->eth_stats.tx_busy; 520 return NETDEV_TX_BUSY; 521 } 522 523 if (ret == -ENOSPC) 524 ++net_device_ctx->eth_stats.tx_no_space; 525 526 drop: 527 dev_kfree_skb_any(skb); 528 net->stats.tx_dropped++; 529 530 return NETDEV_TX_OK; 531 532 no_memory: 533 ++net_device_ctx->eth_stats.tx_no_memory; 534 goto drop; 535 } 536 /* 537 * netvsc_linkstatus_callback - Link up/down notification 538 */ 539 void netvsc_linkstatus_callback(struct hv_device *device_obj, 540 struct rndis_message *resp) 541 { 542 struct rndis_indicate_status *indicate = &resp->msg.indicate_status; 543 struct net_device *net; 544 struct net_device_context *ndev_ctx; 545 struct netvsc_reconfig *event; 546 unsigned long flags; 547 548 net = hv_get_drvdata(device_obj); 549 550 if (!net) 551 return; 552 553 ndev_ctx = netdev_priv(net); 554 555 /* Update the physical link speed when changing to another vSwitch */ 556 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { 557 u32 speed; 558 559 speed = *(u32 *)((void *)indicate + indicate-> 560 status_buf_offset) / 10000; 561 ndev_ctx->speed = speed; 562 return; 563 } 564 565 /* Handle these link change statuses below */ 566 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && 567 indicate->status != RNDIS_STATUS_MEDIA_CONNECT && 568 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) 569 return; 570 571 if (net->reg_state != NETREG_REGISTERED) 572 return; 573 574 event = kzalloc(sizeof(*event), GFP_ATOMIC); 575 if (!event) 576 return; 577 event->event = indicate->status; 578 579 spin_lock_irqsave(&ndev_ctx->lock, flags); 580 list_add_tail(&event->list, &ndev_ctx->reconfig_events); 581 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 582 583 schedule_delayed_work(&ndev_ctx->dwork, 0); 584 } 585 586 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, 587 const struct ndis_tcp_ip_checksum_info *csum_info, 588 const struct ndis_pkt_8021q_info *vlan, 589 void *data, u32 buflen) 590 { 591 struct sk_buff *skb; 592 593 skb = netdev_alloc_skb_ip_align(net, buflen); 594 if (!skb) 595 return skb; 596 597 /* 598 * Copy to skb. This copy is needed here since the memory pointed by 599 * hv_netvsc_packet cannot be deallocated 600 */ 601 memcpy(skb_put(skb, buflen), data, buflen); 602 603 skb->protocol = eth_type_trans(skb, net); 604 605 /* skb is already created with CHECKSUM_NONE */ 606 skb_checksum_none_assert(skb); 607 608 /* 609 * In Linux, the IP checksum is always checked. 610 * Do L4 checksum offload if enabled and present. 611 */ 612 if (csum_info && (net->features & NETIF_F_RXCSUM)) { 613 if (csum_info->receive.tcp_checksum_succeeded || 614 csum_info->receive.udp_checksum_succeeded) 615 skb->ip_summed = CHECKSUM_UNNECESSARY; 616 } 617 618 if (vlan) { 619 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); 620 621 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 622 vlan_tci); 623 } 624 625 return skb; 626 } 627 628 /* 629 * netvsc_recv_callback - Callback when we receive a packet from the 630 * "wire" on the specified device. 631 */ 632 int netvsc_recv_callback(struct net_device *net, 633 struct vmbus_channel *channel, 634 void *data, u32 len, 635 const struct ndis_tcp_ip_checksum_info *csum_info, 636 const struct ndis_pkt_8021q_info *vlan) 637 { 638 struct net_device_context *net_device_ctx = netdev_priv(net); 639 struct netvsc_device *net_device = net_device_ctx->nvdev; 640 struct net_device *vf_netdev; 641 struct sk_buff *skb; 642 struct netvsc_stats *rx_stats; 643 u16 q_idx = channel->offermsg.offer.sub_channel_index; 644 645 646 if (net->reg_state != NETREG_REGISTERED) 647 return NVSP_STAT_FAIL; 648 649 /* 650 * If necessary, inject this packet into the VF interface. 651 * On Hyper-V, multicast and brodcast packets are only delivered 652 * to the synthetic interface (after subjecting these to 653 * policy filters on the host). Deliver these via the VF 654 * interface in the guest. 655 */ 656 rcu_read_lock(); 657 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev); 658 if (vf_netdev && (vf_netdev->flags & IFF_UP)) 659 net = vf_netdev; 660 661 /* Allocate a skb - TODO direct I/O to pages? */ 662 skb = netvsc_alloc_recv_skb(net, csum_info, vlan, data, len); 663 if (unlikely(!skb)) { 664 ++net->stats.rx_dropped; 665 rcu_read_unlock(); 666 return NVSP_STAT_FAIL; 667 } 668 669 if (net != vf_netdev) 670 skb_record_rx_queue(skb, q_idx); 671 672 /* 673 * Even if injecting the packet, record the statistics 674 * on the synthetic device because modifying the VF device 675 * statistics will not work correctly. 676 */ 677 rx_stats = &net_device->chan_table[q_idx].rx_stats; 678 u64_stats_update_begin(&rx_stats->syncp); 679 rx_stats->packets++; 680 rx_stats->bytes += len; 681 682 if (skb->pkt_type == PACKET_BROADCAST) 683 ++rx_stats->broadcast; 684 else if (skb->pkt_type == PACKET_MULTICAST) 685 ++rx_stats->multicast; 686 u64_stats_update_end(&rx_stats->syncp); 687 688 /* 689 * Pass the skb back up. Network stack will deallocate the skb when it 690 * is done. 691 * TODO - use NAPI? 692 */ 693 netif_receive_skb(skb); 694 rcu_read_unlock(); 695 696 return 0; 697 } 698 699 static void netvsc_get_drvinfo(struct net_device *net, 700 struct ethtool_drvinfo *info) 701 { 702 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 703 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); 704 } 705 706 static void netvsc_get_channels(struct net_device *net, 707 struct ethtool_channels *channel) 708 { 709 struct net_device_context *net_device_ctx = netdev_priv(net); 710 struct netvsc_device *nvdev = net_device_ctx->nvdev; 711 712 if (nvdev) { 713 channel->max_combined = nvdev->max_chn; 714 channel->combined_count = nvdev->num_chn; 715 } 716 } 717 718 static int netvsc_set_queues(struct net_device *net, struct hv_device *dev, 719 u32 num_chn) 720 { 721 struct netvsc_device_info device_info; 722 int ret; 723 724 memset(&device_info, 0, sizeof(device_info)); 725 device_info.num_chn = num_chn; 726 device_info.ring_size = ring_size; 727 device_info.max_num_vrss_chns = num_chn; 728 729 ret = rndis_filter_device_add(dev, &device_info); 730 if (ret) 731 return ret; 732 733 ret = netif_set_real_num_tx_queues(net, num_chn); 734 if (ret) 735 return ret; 736 737 ret = netif_set_real_num_rx_queues(net, num_chn); 738 739 return ret; 740 } 741 742 static int netvsc_set_channels(struct net_device *net, 743 struct ethtool_channels *channels) 744 { 745 struct net_device_context *net_device_ctx = netdev_priv(net); 746 struct hv_device *dev = net_device_ctx->device_ctx; 747 struct netvsc_device *nvdev = net_device_ctx->nvdev; 748 unsigned int count = channels->combined_count; 749 int ret; 750 751 /* We do not support separate count for rx, tx, or other */ 752 if (count == 0 || 753 channels->rx_count || channels->tx_count || channels->other_count) 754 return -EINVAL; 755 756 if (count > net->num_tx_queues || count > net->num_rx_queues) 757 return -EINVAL; 758 759 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy) 760 return -ENODEV; 761 762 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) 763 return -EINVAL; 764 765 if (count > nvdev->max_chn) 766 return -EINVAL; 767 768 ret = netvsc_close(net); 769 if (ret) 770 return ret; 771 772 net_device_ctx->start_remove = true; 773 rndis_filter_device_remove(dev, nvdev); 774 775 ret = netvsc_set_queues(net, dev, count); 776 if (ret == 0) 777 nvdev->num_chn = count; 778 else 779 netvsc_set_queues(net, dev, nvdev->num_chn); 780 781 netvsc_open(net); 782 net_device_ctx->start_remove = false; 783 784 /* We may have missed link change notifications */ 785 schedule_delayed_work(&net_device_ctx->dwork, 0); 786 787 return ret; 788 } 789 790 static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd) 791 { 792 struct ethtool_cmd diff1 = *cmd; 793 struct ethtool_cmd diff2 = {}; 794 795 ethtool_cmd_speed_set(&diff1, 0); 796 diff1.duplex = 0; 797 /* advertising and cmd are usually set */ 798 diff1.advertising = 0; 799 diff1.cmd = 0; 800 /* We set port to PORT_OTHER */ 801 diff2.port = PORT_OTHER; 802 803 return !memcmp(&diff1, &diff2, sizeof(diff1)); 804 } 805 806 static void netvsc_init_settings(struct net_device *dev) 807 { 808 struct net_device_context *ndc = netdev_priv(dev); 809 810 ndc->speed = SPEED_UNKNOWN; 811 ndc->duplex = DUPLEX_UNKNOWN; 812 } 813 814 static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 815 { 816 struct net_device_context *ndc = netdev_priv(dev); 817 818 ethtool_cmd_speed_set(cmd, ndc->speed); 819 cmd->duplex = ndc->duplex; 820 cmd->port = PORT_OTHER; 821 822 return 0; 823 } 824 825 static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 826 { 827 struct net_device_context *ndc = netdev_priv(dev); 828 u32 speed; 829 830 speed = ethtool_cmd_speed(cmd); 831 if (!ethtool_validate_speed(speed) || 832 !ethtool_validate_duplex(cmd->duplex) || 833 !netvsc_validate_ethtool_ss_cmd(cmd)) 834 return -EINVAL; 835 836 ndc->speed = speed; 837 ndc->duplex = cmd->duplex; 838 839 return 0; 840 } 841 842 static int netvsc_change_mtu(struct net_device *ndev, int mtu) 843 { 844 struct net_device_context *ndevctx = netdev_priv(ndev); 845 struct netvsc_device *nvdev = ndevctx->nvdev; 846 struct hv_device *hdev = ndevctx->device_ctx; 847 struct netvsc_device_info device_info; 848 int ret; 849 850 if (ndevctx->start_remove || !nvdev || nvdev->destroy) 851 return -ENODEV; 852 853 ret = netvsc_close(ndev); 854 if (ret) 855 goto out; 856 857 memset(&device_info, 0, sizeof(device_info)); 858 device_info.ring_size = ring_size; 859 device_info.num_chn = nvdev->num_chn; 860 device_info.max_num_vrss_chns = nvdev->num_chn; 861 862 ndevctx->start_remove = true; 863 rndis_filter_device_remove(hdev, nvdev); 864 865 /* 'nvdev' has been freed in rndis_filter_device_remove() -> 866 * netvsc_device_remove () -> free_netvsc_device(). 867 * We mustn't access it before it's re-created in 868 * rndis_filter_device_add() -> netvsc_device_add(). 869 */ 870 871 ndev->mtu = mtu; 872 873 rndis_filter_device_add(hdev, &device_info); 874 875 out: 876 netvsc_open(ndev); 877 ndevctx->start_remove = false; 878 879 /* We may have missed link change notifications */ 880 schedule_delayed_work(&ndevctx->dwork, 0); 881 882 return ret; 883 } 884 885 static void netvsc_get_stats64(struct net_device *net, 886 struct rtnl_link_stats64 *t) 887 { 888 struct net_device_context *ndev_ctx = netdev_priv(net); 889 struct netvsc_device *nvdev = ndev_ctx->nvdev; 890 int i; 891 892 if (!nvdev) 893 return; 894 895 for (i = 0; i < nvdev->num_chn; i++) { 896 const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; 897 const struct netvsc_stats *stats; 898 u64 packets, bytes, multicast; 899 unsigned int start; 900 901 stats = &nvchan->tx_stats; 902 do { 903 start = u64_stats_fetch_begin_irq(&stats->syncp); 904 packets = stats->packets; 905 bytes = stats->bytes; 906 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 907 908 t->tx_bytes += bytes; 909 t->tx_packets += packets; 910 911 stats = &nvchan->rx_stats; 912 do { 913 start = u64_stats_fetch_begin_irq(&stats->syncp); 914 packets = stats->packets; 915 bytes = stats->bytes; 916 multicast = stats->multicast + stats->broadcast; 917 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 918 919 t->rx_bytes += bytes; 920 t->rx_packets += packets; 921 t->multicast += multicast; 922 } 923 924 t->tx_dropped = net->stats.tx_dropped; 925 t->tx_errors = net->stats.tx_errors; 926 927 t->rx_dropped = net->stats.rx_dropped; 928 t->rx_errors = net->stats.rx_errors; 929 } 930 931 static int netvsc_set_mac_addr(struct net_device *ndev, void *p) 932 { 933 struct sockaddr *addr = p; 934 char save_adr[ETH_ALEN]; 935 unsigned char save_aatype; 936 int err; 937 938 memcpy(save_adr, ndev->dev_addr, ETH_ALEN); 939 save_aatype = ndev->addr_assign_type; 940 941 err = eth_mac_addr(ndev, p); 942 if (err != 0) 943 return err; 944 945 err = rndis_filter_set_device_mac(ndev, addr->sa_data); 946 if (err != 0) { 947 /* roll back to saved MAC */ 948 memcpy(ndev->dev_addr, save_adr, ETH_ALEN); 949 ndev->addr_assign_type = save_aatype; 950 } 951 952 return err; 953 } 954 955 static const struct { 956 char name[ETH_GSTRING_LEN]; 957 u16 offset; 958 } netvsc_stats[] = { 959 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, 960 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, 961 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, 962 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, 963 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, 964 }; 965 966 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) 967 968 /* 4 statistics per queue (rx/tx packets/bytes) */ 969 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) 970 971 static int netvsc_get_sset_count(struct net_device *dev, int string_set) 972 { 973 struct net_device_context *ndc = netdev_priv(dev); 974 struct netvsc_device *nvdev = ndc->nvdev; 975 976 switch (string_set) { 977 case ETH_SS_STATS: 978 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev); 979 default: 980 return -EINVAL; 981 } 982 } 983 984 static void netvsc_get_ethtool_stats(struct net_device *dev, 985 struct ethtool_stats *stats, u64 *data) 986 { 987 struct net_device_context *ndc = netdev_priv(dev); 988 struct netvsc_device *nvdev = ndc->nvdev; 989 const void *nds = &ndc->eth_stats; 990 const struct netvsc_stats *qstats; 991 unsigned int start; 992 u64 packets, bytes; 993 int i, j; 994 995 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) 996 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); 997 998 for (j = 0; j < nvdev->num_chn; j++) { 999 qstats = &nvdev->chan_table[j].tx_stats; 1000 1001 do { 1002 start = u64_stats_fetch_begin_irq(&qstats->syncp); 1003 packets = qstats->packets; 1004 bytes = qstats->bytes; 1005 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); 1006 data[i++] = packets; 1007 data[i++] = bytes; 1008 1009 qstats = &nvdev->chan_table[j].rx_stats; 1010 do { 1011 start = u64_stats_fetch_begin_irq(&qstats->syncp); 1012 packets = qstats->packets; 1013 bytes = qstats->bytes; 1014 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); 1015 data[i++] = packets; 1016 data[i++] = bytes; 1017 } 1018 } 1019 1020 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) 1021 { 1022 struct net_device_context *ndc = netdev_priv(dev); 1023 struct netvsc_device *nvdev = ndc->nvdev; 1024 u8 *p = data; 1025 int i; 1026 1027 switch (stringset) { 1028 case ETH_SS_STATS: 1029 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) 1030 memcpy(p + i * ETH_GSTRING_LEN, 1031 netvsc_stats[i].name, ETH_GSTRING_LEN); 1032 1033 p += i * ETH_GSTRING_LEN; 1034 for (i = 0; i < nvdev->num_chn; i++) { 1035 sprintf(p, "tx_queue_%u_packets", i); 1036 p += ETH_GSTRING_LEN; 1037 sprintf(p, "tx_queue_%u_bytes", i); 1038 p += ETH_GSTRING_LEN; 1039 sprintf(p, "rx_queue_%u_packets", i); 1040 p += ETH_GSTRING_LEN; 1041 sprintf(p, "rx_queue_%u_bytes", i); 1042 p += ETH_GSTRING_LEN; 1043 } 1044 1045 break; 1046 } 1047 } 1048 1049 static int 1050 netvsc_get_rss_hash_opts(struct netvsc_device *nvdev, 1051 struct ethtool_rxnfc *info) 1052 { 1053 info->data = RXH_IP_SRC | RXH_IP_DST; 1054 1055 switch (info->flow_type) { 1056 case TCP_V4_FLOW: 1057 case TCP_V6_FLOW: 1058 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 1059 /* fallthrough */ 1060 case UDP_V4_FLOW: 1061 case UDP_V6_FLOW: 1062 case IPV4_FLOW: 1063 case IPV6_FLOW: 1064 break; 1065 default: 1066 info->data = 0; 1067 break; 1068 } 1069 1070 return 0; 1071 } 1072 1073 static int 1074 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, 1075 u32 *rules) 1076 { 1077 struct net_device_context *ndc = netdev_priv(dev); 1078 struct netvsc_device *nvdev = ndc->nvdev; 1079 1080 switch (info->cmd) { 1081 case ETHTOOL_GRXRINGS: 1082 info->data = nvdev->num_chn; 1083 return 0; 1084 1085 case ETHTOOL_GRXFH: 1086 return netvsc_get_rss_hash_opts(nvdev, info); 1087 } 1088 return -EOPNOTSUPP; 1089 } 1090 1091 #ifdef CONFIG_NET_POLL_CONTROLLER 1092 static void netvsc_poll_controller(struct net_device *net) 1093 { 1094 /* As netvsc_start_xmit() works synchronous we don't have to 1095 * trigger anything here. 1096 */ 1097 } 1098 #endif 1099 1100 static u32 netvsc_get_rxfh_key_size(struct net_device *dev) 1101 { 1102 return NETVSC_HASH_KEYLEN; 1103 } 1104 1105 static u32 netvsc_rss_indir_size(struct net_device *dev) 1106 { 1107 return ITAB_NUM; 1108 } 1109 1110 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, 1111 u8 *hfunc) 1112 { 1113 struct net_device_context *ndc = netdev_priv(dev); 1114 struct netvsc_device *ndev = ndc->nvdev; 1115 struct rndis_device *rndis_dev = ndev->extension; 1116 int i; 1117 1118 if (hfunc) 1119 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ 1120 1121 if (indir) { 1122 for (i = 0; i < ITAB_NUM; i++) 1123 indir[i] = rndis_dev->ind_table[i]; 1124 } 1125 1126 if (key) 1127 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); 1128 1129 return 0; 1130 } 1131 1132 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, 1133 const u8 *key, const u8 hfunc) 1134 { 1135 struct net_device_context *ndc = netdev_priv(dev); 1136 struct netvsc_device *ndev = ndc->nvdev; 1137 struct rndis_device *rndis_dev = ndev->extension; 1138 int i; 1139 1140 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 1141 return -EOPNOTSUPP; 1142 1143 if (indir) { 1144 for (i = 0; i < ITAB_NUM; i++) 1145 if (indir[i] >= dev->num_rx_queues) 1146 return -EINVAL; 1147 1148 for (i = 0; i < ITAB_NUM; i++) 1149 rndis_dev->ind_table[i] = indir[i]; 1150 } 1151 1152 if (!key) { 1153 if (!indir) 1154 return 0; 1155 1156 key = rndis_dev->rss_key; 1157 } 1158 1159 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn); 1160 } 1161 1162 static const struct ethtool_ops ethtool_ops = { 1163 .get_drvinfo = netvsc_get_drvinfo, 1164 .get_link = ethtool_op_get_link, 1165 .get_ethtool_stats = netvsc_get_ethtool_stats, 1166 .get_sset_count = netvsc_get_sset_count, 1167 .get_strings = netvsc_get_strings, 1168 .get_channels = netvsc_get_channels, 1169 .set_channels = netvsc_set_channels, 1170 .get_ts_info = ethtool_op_get_ts_info, 1171 .get_settings = netvsc_get_settings, 1172 .set_settings = netvsc_set_settings, 1173 .get_rxnfc = netvsc_get_rxnfc, 1174 .get_rxfh_key_size = netvsc_get_rxfh_key_size, 1175 .get_rxfh_indir_size = netvsc_rss_indir_size, 1176 .get_rxfh = netvsc_get_rxfh, 1177 .set_rxfh = netvsc_set_rxfh, 1178 }; 1179 1180 static const struct net_device_ops device_ops = { 1181 .ndo_open = netvsc_open, 1182 .ndo_stop = netvsc_close, 1183 .ndo_start_xmit = netvsc_start_xmit, 1184 .ndo_set_rx_mode = netvsc_set_multicast_list, 1185 .ndo_change_mtu = netvsc_change_mtu, 1186 .ndo_validate_addr = eth_validate_addr, 1187 .ndo_set_mac_address = netvsc_set_mac_addr, 1188 .ndo_select_queue = netvsc_select_queue, 1189 .ndo_get_stats64 = netvsc_get_stats64, 1190 #ifdef CONFIG_NET_POLL_CONTROLLER 1191 .ndo_poll_controller = netvsc_poll_controller, 1192 #endif 1193 }; 1194 1195 /* 1196 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link 1197 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is 1198 * present send GARP packet to network peers with netif_notify_peers(). 1199 */ 1200 static void netvsc_link_change(struct work_struct *w) 1201 { 1202 struct net_device_context *ndev_ctx = 1203 container_of(w, struct net_device_context, dwork.work); 1204 struct hv_device *device_obj = ndev_ctx->device_ctx; 1205 struct net_device *net = hv_get_drvdata(device_obj); 1206 struct netvsc_device *net_device; 1207 struct rndis_device *rdev; 1208 struct netvsc_reconfig *event = NULL; 1209 bool notify = false, reschedule = false; 1210 unsigned long flags, next_reconfig, delay; 1211 1212 rtnl_lock(); 1213 if (ndev_ctx->start_remove) 1214 goto out_unlock; 1215 1216 net_device = ndev_ctx->nvdev; 1217 rdev = net_device->extension; 1218 1219 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; 1220 if (time_is_after_jiffies(next_reconfig)) { 1221 /* link_watch only sends one notification with current state 1222 * per second, avoid doing reconfig more frequently. Handle 1223 * wrap around. 1224 */ 1225 delay = next_reconfig - jiffies; 1226 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; 1227 schedule_delayed_work(&ndev_ctx->dwork, delay); 1228 goto out_unlock; 1229 } 1230 ndev_ctx->last_reconfig = jiffies; 1231 1232 spin_lock_irqsave(&ndev_ctx->lock, flags); 1233 if (!list_empty(&ndev_ctx->reconfig_events)) { 1234 event = list_first_entry(&ndev_ctx->reconfig_events, 1235 struct netvsc_reconfig, list); 1236 list_del(&event->list); 1237 reschedule = !list_empty(&ndev_ctx->reconfig_events); 1238 } 1239 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 1240 1241 if (!event) 1242 goto out_unlock; 1243 1244 switch (event->event) { 1245 /* Only the following events are possible due to the check in 1246 * netvsc_linkstatus_callback() 1247 */ 1248 case RNDIS_STATUS_MEDIA_CONNECT: 1249 if (rdev->link_state) { 1250 rdev->link_state = false; 1251 netif_carrier_on(net); 1252 netif_tx_wake_all_queues(net); 1253 } else { 1254 notify = true; 1255 } 1256 kfree(event); 1257 break; 1258 case RNDIS_STATUS_MEDIA_DISCONNECT: 1259 if (!rdev->link_state) { 1260 rdev->link_state = true; 1261 netif_carrier_off(net); 1262 netif_tx_stop_all_queues(net); 1263 } 1264 kfree(event); 1265 break; 1266 case RNDIS_STATUS_NETWORK_CHANGE: 1267 /* Only makes sense if carrier is present */ 1268 if (!rdev->link_state) { 1269 rdev->link_state = true; 1270 netif_carrier_off(net); 1271 netif_tx_stop_all_queues(net); 1272 event->event = RNDIS_STATUS_MEDIA_CONNECT; 1273 spin_lock_irqsave(&ndev_ctx->lock, flags); 1274 list_add(&event->list, &ndev_ctx->reconfig_events); 1275 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 1276 reschedule = true; 1277 } 1278 break; 1279 } 1280 1281 rtnl_unlock(); 1282 1283 if (notify) 1284 netdev_notify_peers(net); 1285 1286 /* link_watch only sends one notification with current state per 1287 * second, handle next reconfig event in 2 seconds. 1288 */ 1289 if (reschedule) 1290 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); 1291 1292 return; 1293 1294 out_unlock: 1295 rtnl_unlock(); 1296 } 1297 1298 static struct net_device *get_netvsc_bymac(const u8 *mac) 1299 { 1300 struct net_device *dev; 1301 1302 ASSERT_RTNL(); 1303 1304 for_each_netdev(&init_net, dev) { 1305 if (dev->netdev_ops != &device_ops) 1306 continue; /* not a netvsc device */ 1307 1308 if (ether_addr_equal(mac, dev->perm_addr)) 1309 return dev; 1310 } 1311 1312 return NULL; 1313 } 1314 1315 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) 1316 { 1317 struct net_device *dev; 1318 1319 ASSERT_RTNL(); 1320 1321 for_each_netdev(&init_net, dev) { 1322 struct net_device_context *net_device_ctx; 1323 1324 if (dev->netdev_ops != &device_ops) 1325 continue; /* not a netvsc device */ 1326 1327 net_device_ctx = netdev_priv(dev); 1328 if (net_device_ctx->nvdev == NULL) 1329 continue; /* device is removed */ 1330 1331 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) 1332 return dev; /* a match */ 1333 } 1334 1335 return NULL; 1336 } 1337 1338 static int netvsc_register_vf(struct net_device *vf_netdev) 1339 { 1340 struct net_device *ndev; 1341 struct net_device_context *net_device_ctx; 1342 struct netvsc_device *netvsc_dev; 1343 1344 if (vf_netdev->addr_len != ETH_ALEN) 1345 return NOTIFY_DONE; 1346 1347 /* 1348 * We will use the MAC address to locate the synthetic interface to 1349 * associate with the VF interface. If we don't find a matching 1350 * synthetic interface, move on. 1351 */ 1352 ndev = get_netvsc_bymac(vf_netdev->perm_addr); 1353 if (!ndev) 1354 return NOTIFY_DONE; 1355 1356 net_device_ctx = netdev_priv(ndev); 1357 netvsc_dev = net_device_ctx->nvdev; 1358 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) 1359 return NOTIFY_DONE; 1360 1361 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); 1362 /* 1363 * Take a reference on the module. 1364 */ 1365 try_module_get(THIS_MODULE); 1366 1367 dev_hold(vf_netdev); 1368 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); 1369 return NOTIFY_OK; 1370 } 1371 1372 static int netvsc_vf_up(struct net_device *vf_netdev) 1373 { 1374 struct net_device *ndev; 1375 struct netvsc_device *netvsc_dev; 1376 struct net_device_context *net_device_ctx; 1377 1378 ndev = get_netvsc_byref(vf_netdev); 1379 if (!ndev) 1380 return NOTIFY_DONE; 1381 1382 net_device_ctx = netdev_priv(ndev); 1383 netvsc_dev = net_device_ctx->nvdev; 1384 1385 netdev_info(ndev, "VF up: %s\n", vf_netdev->name); 1386 1387 /* 1388 * Open the device before switching data path. 1389 */ 1390 rndis_filter_open(netvsc_dev); 1391 1392 /* 1393 * notify the host to switch the data path. 1394 */ 1395 netvsc_switch_datapath(ndev, true); 1396 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name); 1397 1398 netif_carrier_off(ndev); 1399 1400 /* Now notify peers through VF device. */ 1401 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev); 1402 1403 return NOTIFY_OK; 1404 } 1405 1406 static int netvsc_vf_down(struct net_device *vf_netdev) 1407 { 1408 struct net_device *ndev; 1409 struct netvsc_device *netvsc_dev; 1410 struct net_device_context *net_device_ctx; 1411 1412 ndev = get_netvsc_byref(vf_netdev); 1413 if (!ndev) 1414 return NOTIFY_DONE; 1415 1416 net_device_ctx = netdev_priv(ndev); 1417 netvsc_dev = net_device_ctx->nvdev; 1418 1419 netdev_info(ndev, "VF down: %s\n", vf_netdev->name); 1420 netvsc_switch_datapath(ndev, false); 1421 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name); 1422 rndis_filter_close(netvsc_dev); 1423 netif_carrier_on(ndev); 1424 1425 /* Now notify peers through netvsc device. */ 1426 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev); 1427 1428 return NOTIFY_OK; 1429 } 1430 1431 static int netvsc_unregister_vf(struct net_device *vf_netdev) 1432 { 1433 struct net_device *ndev; 1434 struct net_device_context *net_device_ctx; 1435 1436 ndev = get_netvsc_byref(vf_netdev); 1437 if (!ndev) 1438 return NOTIFY_DONE; 1439 1440 net_device_ctx = netdev_priv(ndev); 1441 1442 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); 1443 1444 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); 1445 dev_put(vf_netdev); 1446 module_put(THIS_MODULE); 1447 return NOTIFY_OK; 1448 } 1449 1450 static int netvsc_probe(struct hv_device *dev, 1451 const struct hv_vmbus_device_id *dev_id) 1452 { 1453 struct net_device *net = NULL; 1454 struct net_device_context *net_device_ctx; 1455 struct netvsc_device_info device_info; 1456 struct netvsc_device *nvdev; 1457 int ret; 1458 1459 net = alloc_etherdev_mq(sizeof(struct net_device_context), 1460 VRSS_CHANNEL_MAX); 1461 if (!net) 1462 return -ENOMEM; 1463 1464 netif_carrier_off(net); 1465 1466 netvsc_init_settings(net); 1467 1468 net_device_ctx = netdev_priv(net); 1469 net_device_ctx->device_ctx = dev; 1470 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); 1471 if (netif_msg_probe(net_device_ctx)) 1472 netdev_dbg(net, "netvsc msg_enable: %d\n", 1473 net_device_ctx->msg_enable); 1474 1475 hv_set_drvdata(dev, net); 1476 1477 net_device_ctx->start_remove = false; 1478 1479 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); 1480 INIT_WORK(&net_device_ctx->work, do_set_multicast); 1481 1482 spin_lock_init(&net_device_ctx->lock); 1483 INIT_LIST_HEAD(&net_device_ctx->reconfig_events); 1484 1485 net->netdev_ops = &device_ops; 1486 net->ethtool_ops = ðtool_ops; 1487 SET_NETDEV_DEV(net, &dev->device); 1488 1489 /* We always need headroom for rndis header */ 1490 net->needed_headroom = RNDIS_AND_PPI_SIZE; 1491 1492 /* Notify the netvsc driver of the new device */ 1493 memset(&device_info, 0, sizeof(device_info)); 1494 device_info.ring_size = ring_size; 1495 device_info.max_num_vrss_chns = min_t(u32, VRSS_CHANNEL_DEFAULT, 1496 num_online_cpus()); 1497 ret = rndis_filter_device_add(dev, &device_info); 1498 if (ret != 0) { 1499 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); 1500 free_netdev(net); 1501 hv_set_drvdata(dev, NULL); 1502 return ret; 1503 } 1504 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); 1505 1506 /* hw_features computed in rndis_filter_device_add */ 1507 net->features = net->hw_features | 1508 NETIF_F_HIGHDMA | NETIF_F_SG | 1509 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; 1510 net->vlan_features = net->features; 1511 1512 nvdev = net_device_ctx->nvdev; 1513 netif_set_real_num_tx_queues(net, nvdev->num_chn); 1514 netif_set_real_num_rx_queues(net, nvdev->num_chn); 1515 1516 /* MTU range: 68 - 1500 or 65521 */ 1517 net->min_mtu = NETVSC_MTU_MIN; 1518 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) 1519 net->max_mtu = NETVSC_MTU - ETH_HLEN; 1520 else 1521 net->max_mtu = ETH_DATA_LEN; 1522 1523 ret = register_netdev(net); 1524 if (ret != 0) { 1525 pr_err("Unable to register netdev.\n"); 1526 rndis_filter_device_remove(dev, nvdev); 1527 free_netdev(net); 1528 } 1529 1530 return ret; 1531 } 1532 1533 static int netvsc_remove(struct hv_device *dev) 1534 { 1535 struct net_device *net; 1536 struct net_device_context *ndev_ctx; 1537 1538 net = hv_get_drvdata(dev); 1539 1540 if (net == NULL) { 1541 dev_err(&dev->device, "No net device to remove\n"); 1542 return 0; 1543 } 1544 1545 ndev_ctx = netdev_priv(net); 1546 1547 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels() 1548 * removing the device. 1549 */ 1550 rtnl_lock(); 1551 ndev_ctx->start_remove = true; 1552 rtnl_unlock(); 1553 1554 cancel_delayed_work_sync(&ndev_ctx->dwork); 1555 cancel_work_sync(&ndev_ctx->work); 1556 1557 /* Stop outbound asap */ 1558 netif_tx_disable(net); 1559 1560 unregister_netdev(net); 1561 1562 /* 1563 * Call to the vsc driver to let it know that the device is being 1564 * removed 1565 */ 1566 rndis_filter_device_remove(dev, ndev_ctx->nvdev); 1567 1568 hv_set_drvdata(dev, NULL); 1569 1570 free_netdev(net); 1571 return 0; 1572 } 1573 1574 static const struct hv_vmbus_device_id id_table[] = { 1575 /* Network guid */ 1576 { HV_NIC_GUID, }, 1577 { }, 1578 }; 1579 1580 MODULE_DEVICE_TABLE(vmbus, id_table); 1581 1582 /* The one and only one */ 1583 static struct hv_driver netvsc_drv = { 1584 .name = KBUILD_MODNAME, 1585 .id_table = id_table, 1586 .probe = netvsc_probe, 1587 .remove = netvsc_remove, 1588 }; 1589 1590 /* 1591 * On Hyper-V, every VF interface is matched with a corresponding 1592 * synthetic interface. The synthetic interface is presented first 1593 * to the guest. When the corresponding VF instance is registered, 1594 * we will take care of switching the data path. 1595 */ 1596 static int netvsc_netdev_event(struct notifier_block *this, 1597 unsigned long event, void *ptr) 1598 { 1599 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 1600 1601 /* Skip our own events */ 1602 if (event_dev->netdev_ops == &device_ops) 1603 return NOTIFY_DONE; 1604 1605 /* Avoid non-Ethernet type devices */ 1606 if (event_dev->type != ARPHRD_ETHER) 1607 return NOTIFY_DONE; 1608 1609 /* Avoid Vlan dev with same MAC registering as VF */ 1610 if (is_vlan_dev(event_dev)) 1611 return NOTIFY_DONE; 1612 1613 /* Avoid Bonding master dev with same MAC registering as VF */ 1614 if ((event_dev->priv_flags & IFF_BONDING) && 1615 (event_dev->flags & IFF_MASTER)) 1616 return NOTIFY_DONE; 1617 1618 switch (event) { 1619 case NETDEV_REGISTER: 1620 return netvsc_register_vf(event_dev); 1621 case NETDEV_UNREGISTER: 1622 return netvsc_unregister_vf(event_dev); 1623 case NETDEV_UP: 1624 return netvsc_vf_up(event_dev); 1625 case NETDEV_DOWN: 1626 return netvsc_vf_down(event_dev); 1627 default: 1628 return NOTIFY_DONE; 1629 } 1630 } 1631 1632 static struct notifier_block netvsc_netdev_notifier = { 1633 .notifier_call = netvsc_netdev_event, 1634 }; 1635 1636 static void __exit netvsc_drv_exit(void) 1637 { 1638 unregister_netdevice_notifier(&netvsc_netdev_notifier); 1639 vmbus_driver_unregister(&netvsc_drv); 1640 } 1641 1642 static int __init netvsc_drv_init(void) 1643 { 1644 int ret; 1645 1646 if (ring_size < RING_SIZE_MIN) { 1647 ring_size = RING_SIZE_MIN; 1648 pr_info("Increased ring_size to %d (min allowed)\n", 1649 ring_size); 1650 } 1651 ret = vmbus_driver_register(&netvsc_drv); 1652 1653 if (ret) 1654 return ret; 1655 1656 register_netdevice_notifier(&netvsc_netdev_notifier); 1657 return 0; 1658 } 1659 1660 MODULE_LICENSE("GPL"); 1661 MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); 1662 1663 module_init(netvsc_drv_init); 1664 module_exit(netvsc_drv_exit); 1665