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 <linux/rtnetlink.h> 37 #include <linux/netpoll.h> 38 39 #include <net/arp.h> 40 #include <net/route.h> 41 #include <net/sock.h> 42 #include <net/pkt_sched.h> 43 #include <net/checksum.h> 44 #include <net/ip6_checksum.h> 45 46 #include "hyperv_net.h" 47 48 #define RING_SIZE_MIN 64 49 #define NETVSC_MIN_TX_SECTIONS 10 50 #define NETVSC_DEFAULT_TX 192 /* ~1M */ 51 #define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */ 52 #define NETVSC_DEFAULT_RX 2048 /* ~4M */ 53 54 #define LINKCHANGE_INT (2 * HZ) 55 #define VF_TAKEOVER_INT (HZ / 10) 56 57 static int ring_size = 128; 58 module_param(ring_size, int, S_IRUGO); 59 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); 60 61 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | 62 NETIF_MSG_LINK | NETIF_MSG_IFUP | 63 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | 64 NETIF_MSG_TX_ERR; 65 66 static int debug = -1; 67 module_param(debug, int, S_IRUGO); 68 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); 69 70 static void netvsc_set_multicast_list(struct net_device *net) 71 { 72 struct net_device_context *net_device_ctx = netdev_priv(net); 73 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); 74 75 rndis_filter_update(nvdev); 76 } 77 78 static int netvsc_open(struct net_device *net) 79 { 80 struct net_device_context *ndev_ctx = netdev_priv(net); 81 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); 82 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev); 83 struct rndis_device *rdev; 84 int ret = 0; 85 86 netif_carrier_off(net); 87 88 /* Open up the device */ 89 ret = rndis_filter_open(nvdev); 90 if (ret != 0) { 91 netdev_err(net, "unable to open device (ret %d).\n", ret); 92 return ret; 93 } 94 95 netif_tx_wake_all_queues(net); 96 97 rdev = nvdev->extension; 98 99 if (!rdev->link_state) 100 netif_carrier_on(net); 101 102 if (vf_netdev) { 103 /* Setting synthetic device up transparently sets 104 * slave as up. If open fails, then slave will be 105 * still be offline (and not used). 106 */ 107 ret = dev_open(vf_netdev); 108 if (ret) 109 netdev_warn(net, 110 "unable to open slave: %s: %d\n", 111 vf_netdev->name, ret); 112 } 113 return 0; 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 net_device *vf_netdev 120 = rtnl_dereference(net_device_ctx->vf_netdev); 121 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); 122 int ret = 0; 123 u32 aread, i, msec = 10, retry = 0, retry_max = 20; 124 struct vmbus_channel *chn; 125 126 netif_tx_disable(net); 127 128 /* No need to close rndis filter if it is removed already */ 129 if (!nvdev) 130 goto out; 131 132 ret = rndis_filter_close(nvdev); 133 if (ret != 0) { 134 netdev_err(net, "unable to close device (ret %d).\n", ret); 135 return ret; 136 } 137 138 /* Ensure pending bytes in ring are read */ 139 while (true) { 140 aread = 0; 141 for (i = 0; i < nvdev->num_chn; i++) { 142 chn = nvdev->chan_table[i].channel; 143 if (!chn) 144 continue; 145 146 aread = hv_get_bytes_to_read(&chn->inbound); 147 if (aread) 148 break; 149 150 aread = hv_get_bytes_to_read(&chn->outbound); 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 out: 171 if (vf_netdev) 172 dev_close(vf_netdev); 173 174 return ret; 175 } 176 177 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, 178 int pkt_type) 179 { 180 struct rndis_packet *rndis_pkt; 181 struct rndis_per_packet_info *ppi; 182 183 rndis_pkt = &msg->msg.pkt; 184 rndis_pkt->data_offset += ppi_size; 185 186 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + 187 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); 188 189 ppi->size = ppi_size; 190 ppi->type = pkt_type; 191 ppi->ppi_offset = sizeof(struct rndis_per_packet_info); 192 193 rndis_pkt->per_pkt_info_len += ppi_size; 194 195 return ppi; 196 } 197 198 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented 199 * packets. We can use ethtool to change UDP hash level when necessary. 200 */ 201 static inline u32 netvsc_get_hash( 202 struct sk_buff *skb, 203 const struct net_device_context *ndc) 204 { 205 struct flow_keys flow; 206 u32 hash; 207 static u32 hashrnd __read_mostly; 208 209 net_get_random_once(&hashrnd, sizeof(hashrnd)); 210 211 if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) 212 return 0; 213 214 if (flow.basic.ip_proto == IPPROTO_TCP || 215 (flow.basic.ip_proto == IPPROTO_UDP && 216 ((flow.basic.n_proto == htons(ETH_P_IP) && ndc->udp4_l4_hash) || 217 (flow.basic.n_proto == htons(ETH_P_IPV6) && 218 ndc->udp6_l4_hash)))) { 219 return skb_get_hash(skb); 220 } else { 221 if (flow.basic.n_proto == htons(ETH_P_IP)) 222 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd); 223 else if (flow.basic.n_proto == htons(ETH_P_IPV6)) 224 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd); 225 else 226 hash = 0; 227 228 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3); 229 } 230 231 return hash; 232 } 233 234 static inline int netvsc_get_tx_queue(struct net_device *ndev, 235 struct sk_buff *skb, int old_idx) 236 { 237 const struct net_device_context *ndc = netdev_priv(ndev); 238 struct sock *sk = skb->sk; 239 int q_idx; 240 241 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, ndc) & 242 (VRSS_SEND_TAB_SIZE - 1)]; 243 244 /* If queue index changed record the new value */ 245 if (q_idx != old_idx && 246 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) 247 sk_tx_queue_set(sk, q_idx); 248 249 return q_idx; 250 } 251 252 /* 253 * Select queue for transmit. 254 * 255 * If a valid queue has already been assigned, then use that. 256 * Otherwise compute tx queue based on hash and the send table. 257 * 258 * This is basically similar to default (__netdev_pick_tx) with the added step 259 * of using the host send_table when no other queue has been assigned. 260 * 261 * TODO support XPS - but get_xps_queue not exported 262 */ 263 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb) 264 { 265 int q_idx = sk_tx_queue_get(skb->sk); 266 267 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) { 268 /* If forwarding a packet, we use the recorded queue when 269 * available for better cache locality. 270 */ 271 if (skb_rx_queue_recorded(skb)) 272 q_idx = skb_get_rx_queue(skb); 273 else 274 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx); 275 } 276 277 return q_idx; 278 } 279 280 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, 281 void *accel_priv, 282 select_queue_fallback_t fallback) 283 { 284 struct net_device_context *ndc = netdev_priv(ndev); 285 struct net_device *vf_netdev; 286 u16 txq; 287 288 rcu_read_lock(); 289 vf_netdev = rcu_dereference(ndc->vf_netdev); 290 if (vf_netdev) { 291 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 292 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 293 } else { 294 txq = netvsc_pick_tx(ndev, skb); 295 } 296 rcu_read_unlock(); 297 298 while (unlikely(txq >= ndev->real_num_tx_queues)) 299 txq -= ndev->real_num_tx_queues; 300 301 return txq; 302 } 303 304 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, 305 struct hv_page_buffer *pb) 306 { 307 int j = 0; 308 309 /* Deal with compund pages by ignoring unused part 310 * of the page. 311 */ 312 page += (offset >> PAGE_SHIFT); 313 offset &= ~PAGE_MASK; 314 315 while (len > 0) { 316 unsigned long bytes; 317 318 bytes = PAGE_SIZE - offset; 319 if (bytes > len) 320 bytes = len; 321 pb[j].pfn = page_to_pfn(page); 322 pb[j].offset = offset; 323 pb[j].len = bytes; 324 325 offset += bytes; 326 len -= bytes; 327 328 if (offset == PAGE_SIZE && len) { 329 page++; 330 offset = 0; 331 j++; 332 } 333 } 334 335 return j + 1; 336 } 337 338 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, 339 struct hv_netvsc_packet *packet, 340 struct hv_page_buffer *pb) 341 { 342 u32 slots_used = 0; 343 char *data = skb->data; 344 int frags = skb_shinfo(skb)->nr_frags; 345 int i; 346 347 /* The packet is laid out thus: 348 * 1. hdr: RNDIS header and PPI 349 * 2. skb linear data 350 * 3. skb fragment data 351 */ 352 slots_used += fill_pg_buf(virt_to_page(hdr), 353 offset_in_page(hdr), 354 len, &pb[slots_used]); 355 356 packet->rmsg_size = len; 357 packet->rmsg_pgcnt = slots_used; 358 359 slots_used += fill_pg_buf(virt_to_page(data), 360 offset_in_page(data), 361 skb_headlen(skb), &pb[slots_used]); 362 363 for (i = 0; i < frags; i++) { 364 skb_frag_t *frag = skb_shinfo(skb)->frags + i; 365 366 slots_used += fill_pg_buf(skb_frag_page(frag), 367 frag->page_offset, 368 skb_frag_size(frag), &pb[slots_used]); 369 } 370 return slots_used; 371 } 372 373 static int count_skb_frag_slots(struct sk_buff *skb) 374 { 375 int i, frags = skb_shinfo(skb)->nr_frags; 376 int pages = 0; 377 378 for (i = 0; i < frags; i++) { 379 skb_frag_t *frag = skb_shinfo(skb)->frags + i; 380 unsigned long size = skb_frag_size(frag); 381 unsigned long offset = frag->page_offset; 382 383 /* Skip unused frames from start of page */ 384 offset &= ~PAGE_MASK; 385 pages += PFN_UP(offset + size); 386 } 387 return pages; 388 } 389 390 static int netvsc_get_slots(struct sk_buff *skb) 391 { 392 char *data = skb->data; 393 unsigned int offset = offset_in_page(data); 394 unsigned int len = skb_headlen(skb); 395 int slots; 396 int frag_slots; 397 398 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); 399 frag_slots = count_skb_frag_slots(skb); 400 return slots + frag_slots; 401 } 402 403 static u32 net_checksum_info(struct sk_buff *skb) 404 { 405 if (skb->protocol == htons(ETH_P_IP)) { 406 struct iphdr *ip = ip_hdr(skb); 407 408 if (ip->protocol == IPPROTO_TCP) 409 return TRANSPORT_INFO_IPV4_TCP; 410 else if (ip->protocol == IPPROTO_UDP) 411 return TRANSPORT_INFO_IPV4_UDP; 412 } else { 413 struct ipv6hdr *ip6 = ipv6_hdr(skb); 414 415 if (ip6->nexthdr == IPPROTO_TCP) 416 return TRANSPORT_INFO_IPV6_TCP; 417 else if (ip6->nexthdr == IPPROTO_UDP) 418 return TRANSPORT_INFO_IPV6_UDP; 419 } 420 421 return TRANSPORT_INFO_NOT_IP; 422 } 423 424 /* Send skb on the slave VF device. */ 425 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev, 426 struct sk_buff *skb) 427 { 428 struct net_device_context *ndev_ctx = netdev_priv(net); 429 unsigned int len = skb->len; 430 int rc; 431 432 skb->dev = vf_netdev; 433 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; 434 435 rc = dev_queue_xmit(skb); 436 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) { 437 struct netvsc_vf_pcpu_stats *pcpu_stats 438 = this_cpu_ptr(ndev_ctx->vf_stats); 439 440 u64_stats_update_begin(&pcpu_stats->syncp); 441 pcpu_stats->tx_packets++; 442 pcpu_stats->tx_bytes += len; 443 u64_stats_update_end(&pcpu_stats->syncp); 444 } else { 445 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped); 446 } 447 448 return rc; 449 } 450 451 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) 452 { 453 struct net_device_context *net_device_ctx = netdev_priv(net); 454 struct hv_netvsc_packet *packet = NULL; 455 int ret; 456 unsigned int num_data_pgs; 457 struct rndis_message *rndis_msg; 458 struct rndis_packet *rndis_pkt; 459 struct net_device *vf_netdev; 460 u32 rndis_msg_size; 461 struct rndis_per_packet_info *ppi; 462 u32 hash; 463 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT]; 464 465 /* if VF is present and up then redirect packets 466 * already called with rcu_read_lock_bh 467 */ 468 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev); 469 if (vf_netdev && netif_running(vf_netdev) && 470 !netpoll_tx_running(net)) 471 return netvsc_vf_xmit(net, vf_netdev, skb); 472 473 /* We will atmost need two pages to describe the rndis 474 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number 475 * of pages in a single packet. If skb is scattered around 476 * more pages we try linearizing it. 477 */ 478 479 num_data_pgs = netvsc_get_slots(skb) + 2; 480 481 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { 482 ++net_device_ctx->eth_stats.tx_scattered; 483 484 if (skb_linearize(skb)) 485 goto no_memory; 486 487 num_data_pgs = netvsc_get_slots(skb) + 2; 488 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { 489 ++net_device_ctx->eth_stats.tx_too_big; 490 goto drop; 491 } 492 } 493 494 /* 495 * Place the rndis header in the skb head room and 496 * the skb->cb will be used for hv_netvsc_packet 497 * structure. 498 */ 499 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); 500 if (ret) 501 goto no_memory; 502 503 /* Use the skb control buffer for building up the packet */ 504 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > 505 FIELD_SIZEOF(struct sk_buff, cb)); 506 packet = (struct hv_netvsc_packet *)skb->cb; 507 508 packet->q_idx = skb_get_queue_mapping(skb); 509 510 packet->total_data_buflen = skb->len; 511 packet->total_bytes = skb->len; 512 packet->total_packets = 1; 513 514 rndis_msg = (struct rndis_message *)skb->head; 515 516 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); 517 518 /* Add the rndis header */ 519 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; 520 rndis_msg->msg_len = packet->total_data_buflen; 521 rndis_pkt = &rndis_msg->msg.pkt; 522 rndis_pkt->data_offset = sizeof(struct rndis_packet); 523 rndis_pkt->data_len = packet->total_data_buflen; 524 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); 525 526 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); 527 528 hash = skb_get_hash_raw(skb); 529 if (hash != 0 && net->real_num_tx_queues > 1) { 530 rndis_msg_size += NDIS_HASH_PPI_SIZE; 531 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, 532 NBL_HASH_VALUE); 533 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; 534 } 535 536 if (skb_vlan_tag_present(skb)) { 537 struct ndis_pkt_8021q_info *vlan; 538 539 rndis_msg_size += NDIS_VLAN_PPI_SIZE; 540 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, 541 IEEE_8021Q_INFO); 542 543 vlan = (void *)ppi + ppi->ppi_offset; 544 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; 545 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> 546 VLAN_PRIO_SHIFT; 547 } 548 549 if (skb_is_gso(skb)) { 550 struct ndis_tcp_lso_info *lso_info; 551 552 rndis_msg_size += NDIS_LSO_PPI_SIZE; 553 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, 554 TCP_LARGESEND_PKTINFO); 555 556 lso_info = (void *)ppi + ppi->ppi_offset; 557 558 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; 559 if (skb->protocol == htons(ETH_P_IP)) { 560 lso_info->lso_v2_transmit.ip_version = 561 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; 562 ip_hdr(skb)->tot_len = 0; 563 ip_hdr(skb)->check = 0; 564 tcp_hdr(skb)->check = 565 ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 566 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); 567 } else { 568 lso_info->lso_v2_transmit.ip_version = 569 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; 570 ipv6_hdr(skb)->payload_len = 0; 571 tcp_hdr(skb)->check = 572 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 573 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); 574 } 575 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); 576 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; 577 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 578 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { 579 struct ndis_tcp_ip_checksum_info *csum_info; 580 581 rndis_msg_size += NDIS_CSUM_PPI_SIZE; 582 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, 583 TCPIP_CHKSUM_PKTINFO); 584 585 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + 586 ppi->ppi_offset); 587 588 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); 589 590 if (skb->protocol == htons(ETH_P_IP)) { 591 csum_info->transmit.is_ipv4 = 1; 592 593 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 594 csum_info->transmit.tcp_checksum = 1; 595 else 596 csum_info->transmit.udp_checksum = 1; 597 } else { 598 csum_info->transmit.is_ipv6 = 1; 599 600 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) 601 csum_info->transmit.tcp_checksum = 1; 602 else 603 csum_info->transmit.udp_checksum = 1; 604 } 605 } else { 606 /* Can't do offload of this type of checksum */ 607 if (skb_checksum_help(skb)) 608 goto drop; 609 } 610 } 611 612 /* Start filling in the page buffers with the rndis hdr */ 613 rndis_msg->msg_len += rndis_msg_size; 614 packet->total_data_buflen = rndis_msg->msg_len; 615 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, 616 skb, packet, pb); 617 618 /* timestamp packet in software */ 619 skb_tx_timestamp(skb); 620 621 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb); 622 if (likely(ret == 0)) 623 return NETDEV_TX_OK; 624 625 if (ret == -EAGAIN) { 626 ++net_device_ctx->eth_stats.tx_busy; 627 return NETDEV_TX_BUSY; 628 } 629 630 if (ret == -ENOSPC) 631 ++net_device_ctx->eth_stats.tx_no_space; 632 633 drop: 634 dev_kfree_skb_any(skb); 635 net->stats.tx_dropped++; 636 637 return NETDEV_TX_OK; 638 639 no_memory: 640 ++net_device_ctx->eth_stats.tx_no_memory; 641 goto drop; 642 } 643 644 /* 645 * netvsc_linkstatus_callback - Link up/down notification 646 */ 647 void netvsc_linkstatus_callback(struct hv_device *device_obj, 648 struct rndis_message *resp) 649 { 650 struct rndis_indicate_status *indicate = &resp->msg.indicate_status; 651 struct net_device *net; 652 struct net_device_context *ndev_ctx; 653 struct netvsc_reconfig *event; 654 unsigned long flags; 655 656 net = hv_get_drvdata(device_obj); 657 658 if (!net) 659 return; 660 661 ndev_ctx = netdev_priv(net); 662 663 /* Update the physical link speed when changing to another vSwitch */ 664 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { 665 u32 speed; 666 667 speed = *(u32 *)((void *)indicate 668 + indicate->status_buf_offset) / 10000; 669 ndev_ctx->speed = speed; 670 return; 671 } 672 673 /* Handle these link change statuses below */ 674 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && 675 indicate->status != RNDIS_STATUS_MEDIA_CONNECT && 676 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) 677 return; 678 679 if (net->reg_state != NETREG_REGISTERED) 680 return; 681 682 event = kzalloc(sizeof(*event), GFP_ATOMIC); 683 if (!event) 684 return; 685 event->event = indicate->status; 686 687 spin_lock_irqsave(&ndev_ctx->lock, flags); 688 list_add_tail(&event->list, &ndev_ctx->reconfig_events); 689 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 690 691 schedule_delayed_work(&ndev_ctx->dwork, 0); 692 } 693 694 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, 695 struct napi_struct *napi, 696 const struct ndis_tcp_ip_checksum_info *csum_info, 697 const struct ndis_pkt_8021q_info *vlan, 698 void *data, u32 buflen) 699 { 700 struct sk_buff *skb; 701 702 skb = napi_alloc_skb(napi, buflen); 703 if (!skb) 704 return skb; 705 706 /* 707 * Copy to skb. This copy is needed here since the memory pointed by 708 * hv_netvsc_packet cannot be deallocated 709 */ 710 skb_put_data(skb, data, buflen); 711 712 skb->protocol = eth_type_trans(skb, net); 713 714 /* skb is already created with CHECKSUM_NONE */ 715 skb_checksum_none_assert(skb); 716 717 /* 718 * In Linux, the IP checksum is always checked. 719 * Do L4 checksum offload if enabled and present. 720 */ 721 if (csum_info && (net->features & NETIF_F_RXCSUM)) { 722 if (csum_info->receive.tcp_checksum_succeeded || 723 csum_info->receive.udp_checksum_succeeded) 724 skb->ip_summed = CHECKSUM_UNNECESSARY; 725 } 726 727 if (vlan) { 728 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); 729 730 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 731 vlan_tci); 732 } 733 734 return skb; 735 } 736 737 /* 738 * netvsc_recv_callback - Callback when we receive a packet from the 739 * "wire" on the specified device. 740 */ 741 int netvsc_recv_callback(struct net_device *net, 742 struct vmbus_channel *channel, 743 void *data, u32 len, 744 const struct ndis_tcp_ip_checksum_info *csum_info, 745 const struct ndis_pkt_8021q_info *vlan) 746 { 747 struct net_device_context *net_device_ctx = netdev_priv(net); 748 struct netvsc_device *net_device; 749 u16 q_idx = channel->offermsg.offer.sub_channel_index; 750 struct netvsc_channel *nvchan; 751 struct sk_buff *skb; 752 struct netvsc_stats *rx_stats; 753 754 if (net->reg_state != NETREG_REGISTERED) 755 return NVSP_STAT_FAIL; 756 757 rcu_read_lock(); 758 net_device = rcu_dereference(net_device_ctx->nvdev); 759 if (unlikely(!net_device)) 760 goto drop; 761 762 nvchan = &net_device->chan_table[q_idx]; 763 764 /* Allocate a skb - TODO direct I/O to pages? */ 765 skb = netvsc_alloc_recv_skb(net, &nvchan->napi, 766 csum_info, vlan, data, len); 767 if (unlikely(!skb)) { 768 drop: 769 ++net->stats.rx_dropped; 770 rcu_read_unlock(); 771 return NVSP_STAT_FAIL; 772 } 773 774 skb_record_rx_queue(skb, q_idx); 775 776 /* 777 * Even if injecting the packet, record the statistics 778 * on the synthetic device because modifying the VF device 779 * statistics will not work correctly. 780 */ 781 rx_stats = &nvchan->rx_stats; 782 u64_stats_update_begin(&rx_stats->syncp); 783 rx_stats->packets++; 784 rx_stats->bytes += len; 785 786 if (skb->pkt_type == PACKET_BROADCAST) 787 ++rx_stats->broadcast; 788 else if (skb->pkt_type == PACKET_MULTICAST) 789 ++rx_stats->multicast; 790 u64_stats_update_end(&rx_stats->syncp); 791 792 napi_gro_receive(&nvchan->napi, skb); 793 rcu_read_unlock(); 794 795 return 0; 796 } 797 798 static void netvsc_get_drvinfo(struct net_device *net, 799 struct ethtool_drvinfo *info) 800 { 801 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 802 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); 803 } 804 805 static void netvsc_get_channels(struct net_device *net, 806 struct ethtool_channels *channel) 807 { 808 struct net_device_context *net_device_ctx = netdev_priv(net); 809 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); 810 811 if (nvdev) { 812 channel->max_combined = nvdev->max_chn; 813 channel->combined_count = nvdev->num_chn; 814 } 815 } 816 817 static int netvsc_set_channels(struct net_device *net, 818 struct ethtool_channels *channels) 819 { 820 struct net_device_context *net_device_ctx = netdev_priv(net); 821 struct hv_device *dev = net_device_ctx->device_ctx; 822 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); 823 unsigned int orig, count = channels->combined_count; 824 struct netvsc_device_info device_info; 825 bool was_opened; 826 int ret = 0; 827 828 /* We do not support separate count for rx, tx, or other */ 829 if (count == 0 || 830 channels->rx_count || channels->tx_count || channels->other_count) 831 return -EINVAL; 832 833 if (!nvdev || nvdev->destroy) 834 return -ENODEV; 835 836 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) 837 return -EINVAL; 838 839 if (count > nvdev->max_chn) 840 return -EINVAL; 841 842 orig = nvdev->num_chn; 843 was_opened = rndis_filter_opened(nvdev); 844 if (was_opened) 845 rndis_filter_close(nvdev); 846 847 memset(&device_info, 0, sizeof(device_info)); 848 device_info.num_chn = count; 849 device_info.ring_size = ring_size; 850 device_info.send_sections = nvdev->send_section_cnt; 851 device_info.recv_sections = nvdev->recv_section_cnt; 852 853 rndis_filter_device_remove(dev, nvdev); 854 855 nvdev = rndis_filter_device_add(dev, &device_info); 856 if (!IS_ERR(nvdev)) { 857 netif_set_real_num_tx_queues(net, nvdev->num_chn); 858 netif_set_real_num_rx_queues(net, nvdev->num_chn); 859 } else { 860 ret = PTR_ERR(nvdev); 861 device_info.num_chn = orig; 862 nvdev = rndis_filter_device_add(dev, &device_info); 863 864 if (IS_ERR(nvdev)) { 865 netdev_err(net, "restoring channel setting failed: %ld\n", 866 PTR_ERR(nvdev)); 867 return ret; 868 } 869 } 870 871 if (was_opened) 872 rndis_filter_open(nvdev); 873 874 /* We may have missed link change notifications */ 875 net_device_ctx->last_reconfig = 0; 876 schedule_delayed_work(&net_device_ctx->dwork, 0); 877 878 return ret; 879 } 880 881 static bool 882 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) 883 { 884 struct ethtool_link_ksettings diff1 = *cmd; 885 struct ethtool_link_ksettings diff2 = {}; 886 887 diff1.base.speed = 0; 888 diff1.base.duplex = 0; 889 /* advertising and cmd are usually set */ 890 ethtool_link_ksettings_zero_link_mode(&diff1, advertising); 891 diff1.base.cmd = 0; 892 /* We set port to PORT_OTHER */ 893 diff2.base.port = PORT_OTHER; 894 895 return !memcmp(&diff1, &diff2, sizeof(diff1)); 896 } 897 898 static void netvsc_init_settings(struct net_device *dev) 899 { 900 struct net_device_context *ndc = netdev_priv(dev); 901 902 ndc->udp4_l4_hash = true; 903 ndc->udp6_l4_hash = true; 904 905 ndc->speed = SPEED_UNKNOWN; 906 ndc->duplex = DUPLEX_FULL; 907 } 908 909 static int netvsc_get_link_ksettings(struct net_device *dev, 910 struct ethtool_link_ksettings *cmd) 911 { 912 struct net_device_context *ndc = netdev_priv(dev); 913 914 cmd->base.speed = ndc->speed; 915 cmd->base.duplex = ndc->duplex; 916 cmd->base.port = PORT_OTHER; 917 918 return 0; 919 } 920 921 static int netvsc_set_link_ksettings(struct net_device *dev, 922 const struct ethtool_link_ksettings *cmd) 923 { 924 struct net_device_context *ndc = netdev_priv(dev); 925 u32 speed; 926 927 speed = cmd->base.speed; 928 if (!ethtool_validate_speed(speed) || 929 !ethtool_validate_duplex(cmd->base.duplex) || 930 !netvsc_validate_ethtool_ss_cmd(cmd)) 931 return -EINVAL; 932 933 ndc->speed = speed; 934 ndc->duplex = cmd->base.duplex; 935 936 return 0; 937 } 938 939 static int netvsc_change_mtu(struct net_device *ndev, int mtu) 940 { 941 struct net_device_context *ndevctx = netdev_priv(ndev); 942 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev); 943 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); 944 struct hv_device *hdev = ndevctx->device_ctx; 945 int orig_mtu = ndev->mtu; 946 struct netvsc_device_info device_info; 947 bool was_opened; 948 int ret = 0; 949 950 if (!nvdev || nvdev->destroy) 951 return -ENODEV; 952 953 /* Change MTU of underlying VF netdev first. */ 954 if (vf_netdev) { 955 ret = dev_set_mtu(vf_netdev, mtu); 956 if (ret) 957 return ret; 958 } 959 960 netif_device_detach(ndev); 961 was_opened = rndis_filter_opened(nvdev); 962 if (was_opened) 963 rndis_filter_close(nvdev); 964 965 memset(&device_info, 0, sizeof(device_info)); 966 device_info.ring_size = ring_size; 967 device_info.num_chn = nvdev->num_chn; 968 device_info.send_sections = nvdev->send_section_cnt; 969 device_info.recv_sections = nvdev->recv_section_cnt; 970 971 rndis_filter_device_remove(hdev, nvdev); 972 973 ndev->mtu = mtu; 974 975 nvdev = rndis_filter_device_add(hdev, &device_info); 976 if (IS_ERR(nvdev)) { 977 ret = PTR_ERR(nvdev); 978 979 /* Attempt rollback to original MTU */ 980 ndev->mtu = orig_mtu; 981 nvdev = rndis_filter_device_add(hdev, &device_info); 982 983 if (vf_netdev) 984 dev_set_mtu(vf_netdev, orig_mtu); 985 986 if (IS_ERR(nvdev)) { 987 netdev_err(ndev, "restoring mtu failed: %ld\n", 988 PTR_ERR(nvdev)); 989 return ret; 990 } 991 } 992 993 if (was_opened) 994 rndis_filter_open(nvdev); 995 996 netif_device_attach(ndev); 997 998 /* We may have missed link change notifications */ 999 schedule_delayed_work(&ndevctx->dwork, 0); 1000 1001 return ret; 1002 } 1003 1004 static void netvsc_get_vf_stats(struct net_device *net, 1005 struct netvsc_vf_pcpu_stats *tot) 1006 { 1007 struct net_device_context *ndev_ctx = netdev_priv(net); 1008 int i; 1009 1010 memset(tot, 0, sizeof(*tot)); 1011 1012 for_each_possible_cpu(i) { 1013 const struct netvsc_vf_pcpu_stats *stats 1014 = per_cpu_ptr(ndev_ctx->vf_stats, i); 1015 u64 rx_packets, rx_bytes, tx_packets, tx_bytes; 1016 unsigned int start; 1017 1018 do { 1019 start = u64_stats_fetch_begin_irq(&stats->syncp); 1020 rx_packets = stats->rx_packets; 1021 tx_packets = stats->tx_packets; 1022 rx_bytes = stats->rx_bytes; 1023 tx_bytes = stats->tx_bytes; 1024 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 1025 1026 tot->rx_packets += rx_packets; 1027 tot->tx_packets += tx_packets; 1028 tot->rx_bytes += rx_bytes; 1029 tot->tx_bytes += tx_bytes; 1030 tot->tx_dropped += stats->tx_dropped; 1031 } 1032 } 1033 1034 static void netvsc_get_stats64(struct net_device *net, 1035 struct rtnl_link_stats64 *t) 1036 { 1037 struct net_device_context *ndev_ctx = netdev_priv(net); 1038 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); 1039 struct netvsc_vf_pcpu_stats vf_tot; 1040 int i; 1041 1042 if (!nvdev) 1043 return; 1044 1045 netdev_stats_to_stats64(t, &net->stats); 1046 1047 netvsc_get_vf_stats(net, &vf_tot); 1048 t->rx_packets += vf_tot.rx_packets; 1049 t->tx_packets += vf_tot.tx_packets; 1050 t->rx_bytes += vf_tot.rx_bytes; 1051 t->tx_bytes += vf_tot.tx_bytes; 1052 t->tx_dropped += vf_tot.tx_dropped; 1053 1054 for (i = 0; i < nvdev->num_chn; i++) { 1055 const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; 1056 const struct netvsc_stats *stats; 1057 u64 packets, bytes, multicast; 1058 unsigned int start; 1059 1060 stats = &nvchan->tx_stats; 1061 do { 1062 start = u64_stats_fetch_begin_irq(&stats->syncp); 1063 packets = stats->packets; 1064 bytes = stats->bytes; 1065 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 1066 1067 t->tx_bytes += bytes; 1068 t->tx_packets += packets; 1069 1070 stats = &nvchan->rx_stats; 1071 do { 1072 start = u64_stats_fetch_begin_irq(&stats->syncp); 1073 packets = stats->packets; 1074 bytes = stats->bytes; 1075 multicast = stats->multicast + stats->broadcast; 1076 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 1077 1078 t->rx_bytes += bytes; 1079 t->rx_packets += packets; 1080 t->multicast += multicast; 1081 } 1082 } 1083 1084 static int netvsc_set_mac_addr(struct net_device *ndev, void *p) 1085 { 1086 struct net_device_context *ndc = netdev_priv(ndev); 1087 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev); 1088 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); 1089 struct sockaddr *addr = p; 1090 int err; 1091 1092 err = eth_prepare_mac_addr_change(ndev, p); 1093 if (err) 1094 return err; 1095 1096 if (!nvdev) 1097 return -ENODEV; 1098 1099 if (vf_netdev) { 1100 err = dev_set_mac_address(vf_netdev, addr); 1101 if (err) 1102 return err; 1103 } 1104 1105 err = rndis_filter_set_device_mac(nvdev, addr->sa_data); 1106 if (!err) { 1107 eth_commit_mac_addr_change(ndev, p); 1108 } else if (vf_netdev) { 1109 /* rollback change on VF */ 1110 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN); 1111 dev_set_mac_address(vf_netdev, addr); 1112 } 1113 1114 return err; 1115 } 1116 1117 static const struct { 1118 char name[ETH_GSTRING_LEN]; 1119 u16 offset; 1120 } netvsc_stats[] = { 1121 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, 1122 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, 1123 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, 1124 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, 1125 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, 1126 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) }, 1127 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) }, 1128 }, vf_stats[] = { 1129 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) }, 1130 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) }, 1131 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) }, 1132 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) }, 1133 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) }, 1134 }; 1135 1136 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) 1137 #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats) 1138 1139 /* 4 statistics per queue (rx/tx packets/bytes) */ 1140 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) 1141 1142 static int netvsc_get_sset_count(struct net_device *dev, int string_set) 1143 { 1144 struct net_device_context *ndc = netdev_priv(dev); 1145 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); 1146 1147 if (!nvdev) 1148 return -ENODEV; 1149 1150 switch (string_set) { 1151 case ETH_SS_STATS: 1152 return NETVSC_GLOBAL_STATS_LEN 1153 + NETVSC_VF_STATS_LEN 1154 + NETVSC_QUEUE_STATS_LEN(nvdev); 1155 default: 1156 return -EINVAL; 1157 } 1158 } 1159 1160 static void netvsc_get_ethtool_stats(struct net_device *dev, 1161 struct ethtool_stats *stats, u64 *data) 1162 { 1163 struct net_device_context *ndc = netdev_priv(dev); 1164 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); 1165 const void *nds = &ndc->eth_stats; 1166 const struct netvsc_stats *qstats; 1167 struct netvsc_vf_pcpu_stats sum; 1168 unsigned int start; 1169 u64 packets, bytes; 1170 int i, j; 1171 1172 if (!nvdev) 1173 return; 1174 1175 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) 1176 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); 1177 1178 netvsc_get_vf_stats(dev, &sum); 1179 for (j = 0; j < NETVSC_VF_STATS_LEN; j++) 1180 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset); 1181 1182 for (j = 0; j < nvdev->num_chn; j++) { 1183 qstats = &nvdev->chan_table[j].tx_stats; 1184 1185 do { 1186 start = u64_stats_fetch_begin_irq(&qstats->syncp); 1187 packets = qstats->packets; 1188 bytes = qstats->bytes; 1189 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); 1190 data[i++] = packets; 1191 data[i++] = bytes; 1192 1193 qstats = &nvdev->chan_table[j].rx_stats; 1194 do { 1195 start = u64_stats_fetch_begin_irq(&qstats->syncp); 1196 packets = qstats->packets; 1197 bytes = qstats->bytes; 1198 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); 1199 data[i++] = packets; 1200 data[i++] = bytes; 1201 } 1202 } 1203 1204 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) 1205 { 1206 struct net_device_context *ndc = netdev_priv(dev); 1207 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); 1208 u8 *p = data; 1209 int i; 1210 1211 if (!nvdev) 1212 return; 1213 1214 switch (stringset) { 1215 case ETH_SS_STATS: 1216 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) { 1217 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN); 1218 p += ETH_GSTRING_LEN; 1219 } 1220 1221 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) { 1222 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN); 1223 p += ETH_GSTRING_LEN; 1224 } 1225 1226 for (i = 0; i < nvdev->num_chn; i++) { 1227 sprintf(p, "tx_queue_%u_packets", i); 1228 p += ETH_GSTRING_LEN; 1229 sprintf(p, "tx_queue_%u_bytes", i); 1230 p += ETH_GSTRING_LEN; 1231 sprintf(p, "rx_queue_%u_packets", i); 1232 p += ETH_GSTRING_LEN; 1233 sprintf(p, "rx_queue_%u_bytes", i); 1234 p += ETH_GSTRING_LEN; 1235 } 1236 1237 break; 1238 } 1239 } 1240 1241 static int 1242 netvsc_get_rss_hash_opts(struct net_device_context *ndc, 1243 struct ethtool_rxnfc *info) 1244 { 1245 info->data = RXH_IP_SRC | RXH_IP_DST; 1246 1247 switch (info->flow_type) { 1248 case TCP_V4_FLOW: 1249 case TCP_V6_FLOW: 1250 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 1251 break; 1252 1253 case UDP_V4_FLOW: 1254 if (ndc->udp4_l4_hash) 1255 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 1256 1257 break; 1258 1259 case UDP_V6_FLOW: 1260 if (ndc->udp6_l4_hash) 1261 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 1262 1263 break; 1264 1265 case IPV4_FLOW: 1266 case IPV6_FLOW: 1267 break; 1268 default: 1269 info->data = 0; 1270 break; 1271 } 1272 1273 return 0; 1274 } 1275 1276 static int 1277 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, 1278 u32 *rules) 1279 { 1280 struct net_device_context *ndc = netdev_priv(dev); 1281 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); 1282 1283 if (!nvdev) 1284 return -ENODEV; 1285 1286 switch (info->cmd) { 1287 case ETHTOOL_GRXRINGS: 1288 info->data = nvdev->num_chn; 1289 return 0; 1290 1291 case ETHTOOL_GRXFH: 1292 return netvsc_get_rss_hash_opts(ndc, info); 1293 } 1294 return -EOPNOTSUPP; 1295 } 1296 1297 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc, 1298 struct ethtool_rxnfc *info) 1299 { 1300 if (info->data == (RXH_IP_SRC | RXH_IP_DST | 1301 RXH_L4_B_0_1 | RXH_L4_B_2_3)) { 1302 if (info->flow_type == UDP_V4_FLOW) 1303 ndc->udp4_l4_hash = true; 1304 else if (info->flow_type == UDP_V6_FLOW) 1305 ndc->udp6_l4_hash = true; 1306 else 1307 return -EOPNOTSUPP; 1308 1309 return 0; 1310 } 1311 1312 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) { 1313 if (info->flow_type == UDP_V4_FLOW) 1314 ndc->udp4_l4_hash = false; 1315 else if (info->flow_type == UDP_V6_FLOW) 1316 ndc->udp6_l4_hash = false; 1317 else 1318 return -EOPNOTSUPP; 1319 1320 return 0; 1321 } 1322 1323 return -EOPNOTSUPP; 1324 } 1325 1326 static int 1327 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info) 1328 { 1329 struct net_device_context *ndc = netdev_priv(ndev); 1330 1331 if (info->cmd == ETHTOOL_SRXFH) 1332 return netvsc_set_rss_hash_opts(ndc, info); 1333 1334 return -EOPNOTSUPP; 1335 } 1336 1337 #ifdef CONFIG_NET_POLL_CONTROLLER 1338 static void netvsc_poll_controller(struct net_device *dev) 1339 { 1340 struct net_device_context *ndc = netdev_priv(dev); 1341 struct netvsc_device *ndev; 1342 int i; 1343 1344 rcu_read_lock(); 1345 ndev = rcu_dereference(ndc->nvdev); 1346 if (ndev) { 1347 for (i = 0; i < ndev->num_chn; i++) { 1348 struct netvsc_channel *nvchan = &ndev->chan_table[i]; 1349 1350 napi_schedule(&nvchan->napi); 1351 } 1352 } 1353 rcu_read_unlock(); 1354 } 1355 #endif 1356 1357 static u32 netvsc_get_rxfh_key_size(struct net_device *dev) 1358 { 1359 return NETVSC_HASH_KEYLEN; 1360 } 1361 1362 static u32 netvsc_rss_indir_size(struct net_device *dev) 1363 { 1364 return ITAB_NUM; 1365 } 1366 1367 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, 1368 u8 *hfunc) 1369 { 1370 struct net_device_context *ndc = netdev_priv(dev); 1371 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); 1372 struct rndis_device *rndis_dev; 1373 int i; 1374 1375 if (!ndev) 1376 return -ENODEV; 1377 1378 if (hfunc) 1379 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ 1380 1381 rndis_dev = ndev->extension; 1382 if (indir) { 1383 for (i = 0; i < ITAB_NUM; i++) 1384 indir[i] = rndis_dev->ind_table[i]; 1385 } 1386 1387 if (key) 1388 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); 1389 1390 return 0; 1391 } 1392 1393 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, 1394 const u8 *key, const u8 hfunc) 1395 { 1396 struct net_device_context *ndc = netdev_priv(dev); 1397 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); 1398 struct rndis_device *rndis_dev; 1399 int i; 1400 1401 if (!ndev) 1402 return -ENODEV; 1403 1404 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 1405 return -EOPNOTSUPP; 1406 1407 rndis_dev = ndev->extension; 1408 if (indir) { 1409 for (i = 0; i < ITAB_NUM; i++) 1410 if (indir[i] >= ndev->num_chn) 1411 return -EINVAL; 1412 1413 for (i = 0; i < ITAB_NUM; i++) 1414 rndis_dev->ind_table[i] = indir[i]; 1415 } 1416 1417 if (!key) { 1418 if (!indir) 1419 return 0; 1420 1421 key = rndis_dev->rss_key; 1422 } 1423 1424 return rndis_filter_set_rss_param(rndis_dev, key); 1425 } 1426 1427 /* Hyper-V RNDIS protocol does not have ring in the HW sense. 1428 * It does have pre-allocated receive area which is divided into sections. 1429 */ 1430 static void __netvsc_get_ringparam(struct netvsc_device *nvdev, 1431 struct ethtool_ringparam *ring) 1432 { 1433 u32 max_buf_size; 1434 1435 ring->rx_pending = nvdev->recv_section_cnt; 1436 ring->tx_pending = nvdev->send_section_cnt; 1437 1438 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2) 1439 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY; 1440 else 1441 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; 1442 1443 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size; 1444 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE 1445 / nvdev->send_section_size; 1446 } 1447 1448 static void netvsc_get_ringparam(struct net_device *ndev, 1449 struct ethtool_ringparam *ring) 1450 { 1451 struct net_device_context *ndevctx = netdev_priv(ndev); 1452 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); 1453 1454 if (!nvdev) 1455 return; 1456 1457 __netvsc_get_ringparam(nvdev, ring); 1458 } 1459 1460 static int netvsc_set_ringparam(struct net_device *ndev, 1461 struct ethtool_ringparam *ring) 1462 { 1463 struct net_device_context *ndevctx = netdev_priv(ndev); 1464 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); 1465 struct hv_device *hdev = ndevctx->device_ctx; 1466 struct netvsc_device_info device_info; 1467 struct ethtool_ringparam orig; 1468 u32 new_tx, new_rx; 1469 bool was_opened; 1470 int ret = 0; 1471 1472 if (!nvdev || nvdev->destroy) 1473 return -ENODEV; 1474 1475 memset(&orig, 0, sizeof(orig)); 1476 __netvsc_get_ringparam(nvdev, &orig); 1477 1478 new_tx = clamp_t(u32, ring->tx_pending, 1479 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending); 1480 new_rx = clamp_t(u32, ring->rx_pending, 1481 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending); 1482 1483 if (new_tx == orig.tx_pending && 1484 new_rx == orig.rx_pending) 1485 return 0; /* no change */ 1486 1487 memset(&device_info, 0, sizeof(device_info)); 1488 device_info.num_chn = nvdev->num_chn; 1489 device_info.ring_size = ring_size; 1490 device_info.send_sections = new_tx; 1491 device_info.recv_sections = new_rx; 1492 1493 netif_device_detach(ndev); 1494 was_opened = rndis_filter_opened(nvdev); 1495 if (was_opened) 1496 rndis_filter_close(nvdev); 1497 1498 rndis_filter_device_remove(hdev, nvdev); 1499 1500 nvdev = rndis_filter_device_add(hdev, &device_info); 1501 if (IS_ERR(nvdev)) { 1502 ret = PTR_ERR(nvdev); 1503 1504 device_info.send_sections = orig.tx_pending; 1505 device_info.recv_sections = orig.rx_pending; 1506 nvdev = rndis_filter_device_add(hdev, &device_info); 1507 if (IS_ERR(nvdev)) { 1508 netdev_err(ndev, "restoring ringparam failed: %ld\n", 1509 PTR_ERR(nvdev)); 1510 return ret; 1511 } 1512 } 1513 1514 if (was_opened) 1515 rndis_filter_open(nvdev); 1516 netif_device_attach(ndev); 1517 1518 /* We may have missed link change notifications */ 1519 ndevctx->last_reconfig = 0; 1520 schedule_delayed_work(&ndevctx->dwork, 0); 1521 1522 return ret; 1523 } 1524 1525 static const struct ethtool_ops ethtool_ops = { 1526 .get_drvinfo = netvsc_get_drvinfo, 1527 .get_link = ethtool_op_get_link, 1528 .get_ethtool_stats = netvsc_get_ethtool_stats, 1529 .get_sset_count = netvsc_get_sset_count, 1530 .get_strings = netvsc_get_strings, 1531 .get_channels = netvsc_get_channels, 1532 .set_channels = netvsc_set_channels, 1533 .get_ts_info = ethtool_op_get_ts_info, 1534 .get_rxnfc = netvsc_get_rxnfc, 1535 .set_rxnfc = netvsc_set_rxnfc, 1536 .get_rxfh_key_size = netvsc_get_rxfh_key_size, 1537 .get_rxfh_indir_size = netvsc_rss_indir_size, 1538 .get_rxfh = netvsc_get_rxfh, 1539 .set_rxfh = netvsc_set_rxfh, 1540 .get_link_ksettings = netvsc_get_link_ksettings, 1541 .set_link_ksettings = netvsc_set_link_ksettings, 1542 .get_ringparam = netvsc_get_ringparam, 1543 .set_ringparam = netvsc_set_ringparam, 1544 }; 1545 1546 static const struct net_device_ops device_ops = { 1547 .ndo_open = netvsc_open, 1548 .ndo_stop = netvsc_close, 1549 .ndo_start_xmit = netvsc_start_xmit, 1550 .ndo_set_rx_mode = netvsc_set_multicast_list, 1551 .ndo_change_mtu = netvsc_change_mtu, 1552 .ndo_validate_addr = eth_validate_addr, 1553 .ndo_set_mac_address = netvsc_set_mac_addr, 1554 .ndo_select_queue = netvsc_select_queue, 1555 .ndo_get_stats64 = netvsc_get_stats64, 1556 #ifdef CONFIG_NET_POLL_CONTROLLER 1557 .ndo_poll_controller = netvsc_poll_controller, 1558 #endif 1559 }; 1560 1561 /* 1562 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link 1563 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is 1564 * present send GARP packet to network peers with netif_notify_peers(). 1565 */ 1566 static void netvsc_link_change(struct work_struct *w) 1567 { 1568 struct net_device_context *ndev_ctx = 1569 container_of(w, struct net_device_context, dwork.work); 1570 struct hv_device *device_obj = ndev_ctx->device_ctx; 1571 struct net_device *net = hv_get_drvdata(device_obj); 1572 struct netvsc_device *net_device; 1573 struct rndis_device *rdev; 1574 struct netvsc_reconfig *event = NULL; 1575 bool notify = false, reschedule = false; 1576 unsigned long flags, next_reconfig, delay; 1577 1578 /* if changes are happening, comeback later */ 1579 if (!rtnl_trylock()) { 1580 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); 1581 return; 1582 } 1583 1584 net_device = rtnl_dereference(ndev_ctx->nvdev); 1585 if (!net_device) 1586 goto out_unlock; 1587 1588 rdev = net_device->extension; 1589 1590 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; 1591 if (time_is_after_jiffies(next_reconfig)) { 1592 /* link_watch only sends one notification with current state 1593 * per second, avoid doing reconfig more frequently. Handle 1594 * wrap around. 1595 */ 1596 delay = next_reconfig - jiffies; 1597 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; 1598 schedule_delayed_work(&ndev_ctx->dwork, delay); 1599 goto out_unlock; 1600 } 1601 ndev_ctx->last_reconfig = jiffies; 1602 1603 spin_lock_irqsave(&ndev_ctx->lock, flags); 1604 if (!list_empty(&ndev_ctx->reconfig_events)) { 1605 event = list_first_entry(&ndev_ctx->reconfig_events, 1606 struct netvsc_reconfig, list); 1607 list_del(&event->list); 1608 reschedule = !list_empty(&ndev_ctx->reconfig_events); 1609 } 1610 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 1611 1612 if (!event) 1613 goto out_unlock; 1614 1615 switch (event->event) { 1616 /* Only the following events are possible due to the check in 1617 * netvsc_linkstatus_callback() 1618 */ 1619 case RNDIS_STATUS_MEDIA_CONNECT: 1620 if (rdev->link_state) { 1621 rdev->link_state = false; 1622 netif_carrier_on(net); 1623 netif_tx_wake_all_queues(net); 1624 } else { 1625 notify = true; 1626 } 1627 kfree(event); 1628 break; 1629 case RNDIS_STATUS_MEDIA_DISCONNECT: 1630 if (!rdev->link_state) { 1631 rdev->link_state = true; 1632 netif_carrier_off(net); 1633 netif_tx_stop_all_queues(net); 1634 } 1635 kfree(event); 1636 break; 1637 case RNDIS_STATUS_NETWORK_CHANGE: 1638 /* Only makes sense if carrier is present */ 1639 if (!rdev->link_state) { 1640 rdev->link_state = true; 1641 netif_carrier_off(net); 1642 netif_tx_stop_all_queues(net); 1643 event->event = RNDIS_STATUS_MEDIA_CONNECT; 1644 spin_lock_irqsave(&ndev_ctx->lock, flags); 1645 list_add(&event->list, &ndev_ctx->reconfig_events); 1646 spin_unlock_irqrestore(&ndev_ctx->lock, flags); 1647 reschedule = true; 1648 } 1649 break; 1650 } 1651 1652 rtnl_unlock(); 1653 1654 if (notify) 1655 netdev_notify_peers(net); 1656 1657 /* link_watch only sends one notification with current state per 1658 * second, handle next reconfig event in 2 seconds. 1659 */ 1660 if (reschedule) 1661 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); 1662 1663 return; 1664 1665 out_unlock: 1666 rtnl_unlock(); 1667 } 1668 1669 static struct net_device *get_netvsc_bymac(const u8 *mac) 1670 { 1671 struct net_device *dev; 1672 1673 ASSERT_RTNL(); 1674 1675 for_each_netdev(&init_net, dev) { 1676 if (dev->netdev_ops != &device_ops) 1677 continue; /* not a netvsc device */ 1678 1679 if (ether_addr_equal(mac, dev->perm_addr)) 1680 return dev; 1681 } 1682 1683 return NULL; 1684 } 1685 1686 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) 1687 { 1688 struct net_device *dev; 1689 1690 ASSERT_RTNL(); 1691 1692 for_each_netdev(&init_net, dev) { 1693 struct net_device_context *net_device_ctx; 1694 1695 if (dev->netdev_ops != &device_ops) 1696 continue; /* not a netvsc device */ 1697 1698 net_device_ctx = netdev_priv(dev); 1699 if (!rtnl_dereference(net_device_ctx->nvdev)) 1700 continue; /* device is removed */ 1701 1702 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) 1703 return dev; /* a match */ 1704 } 1705 1706 return NULL; 1707 } 1708 1709 /* Called when VF is injecting data into network stack. 1710 * Change the associated network device from VF to netvsc. 1711 * note: already called with rcu_read_lock 1712 */ 1713 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb) 1714 { 1715 struct sk_buff *skb = *pskb; 1716 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data); 1717 struct net_device_context *ndev_ctx = netdev_priv(ndev); 1718 struct netvsc_vf_pcpu_stats *pcpu_stats 1719 = this_cpu_ptr(ndev_ctx->vf_stats); 1720 1721 skb->dev = ndev; 1722 1723 u64_stats_update_begin(&pcpu_stats->syncp); 1724 pcpu_stats->rx_packets++; 1725 pcpu_stats->rx_bytes += skb->len; 1726 u64_stats_update_end(&pcpu_stats->syncp); 1727 1728 return RX_HANDLER_ANOTHER; 1729 } 1730 1731 static int netvsc_vf_join(struct net_device *vf_netdev, 1732 struct net_device *ndev) 1733 { 1734 struct net_device_context *ndev_ctx = netdev_priv(ndev); 1735 int ret; 1736 1737 ret = netdev_rx_handler_register(vf_netdev, 1738 netvsc_vf_handle_frame, ndev); 1739 if (ret != 0) { 1740 netdev_err(vf_netdev, 1741 "can not register netvsc VF receive handler (err = %d)\n", 1742 ret); 1743 goto rx_handler_failed; 1744 } 1745 1746 ret = netdev_upper_dev_link(vf_netdev, ndev); 1747 if (ret != 0) { 1748 netdev_err(vf_netdev, 1749 "can not set master device %s (err = %d)\n", 1750 ndev->name, ret); 1751 goto upper_link_failed; 1752 } 1753 1754 /* set slave flag before open to prevent IPv6 addrconf */ 1755 vf_netdev->flags |= IFF_SLAVE; 1756 1757 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); 1758 1759 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev); 1760 1761 netdev_info(vf_netdev, "joined to %s\n", ndev->name); 1762 return 0; 1763 1764 upper_link_failed: 1765 netdev_rx_handler_unregister(vf_netdev); 1766 rx_handler_failed: 1767 return ret; 1768 } 1769 1770 static void __netvsc_vf_setup(struct net_device *ndev, 1771 struct net_device *vf_netdev) 1772 { 1773 int ret; 1774 1775 /* Align MTU of VF with master */ 1776 ret = dev_set_mtu(vf_netdev, ndev->mtu); 1777 if (ret) 1778 netdev_warn(vf_netdev, 1779 "unable to change mtu to %u\n", ndev->mtu); 1780 1781 if (netif_running(ndev)) { 1782 ret = dev_open(vf_netdev); 1783 if (ret) 1784 netdev_warn(vf_netdev, 1785 "unable to open: %d\n", ret); 1786 } 1787 } 1788 1789 /* Setup VF as slave of the synthetic device. 1790 * Runs in workqueue to avoid recursion in netlink callbacks. 1791 */ 1792 static void netvsc_vf_setup(struct work_struct *w) 1793 { 1794 struct net_device_context *ndev_ctx 1795 = container_of(w, struct net_device_context, vf_takeover.work); 1796 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx); 1797 struct net_device *vf_netdev; 1798 1799 if (!rtnl_trylock()) { 1800 schedule_delayed_work(&ndev_ctx->vf_takeover, 0); 1801 return; 1802 } 1803 1804 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); 1805 if (vf_netdev) 1806 __netvsc_vf_setup(ndev, vf_netdev); 1807 1808 rtnl_unlock(); 1809 } 1810 1811 static int netvsc_register_vf(struct net_device *vf_netdev) 1812 { 1813 struct net_device *ndev; 1814 struct net_device_context *net_device_ctx; 1815 struct netvsc_device *netvsc_dev; 1816 1817 if (vf_netdev->addr_len != ETH_ALEN) 1818 return NOTIFY_DONE; 1819 1820 /* 1821 * We will use the MAC address to locate the synthetic interface to 1822 * associate with the VF interface. If we don't find a matching 1823 * synthetic interface, move on. 1824 */ 1825 ndev = get_netvsc_bymac(vf_netdev->perm_addr); 1826 if (!ndev) 1827 return NOTIFY_DONE; 1828 1829 net_device_ctx = netdev_priv(ndev); 1830 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); 1831 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) 1832 return NOTIFY_DONE; 1833 1834 if (netvsc_vf_join(vf_netdev, ndev) != 0) 1835 return NOTIFY_DONE; 1836 1837 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); 1838 1839 dev_hold(vf_netdev); 1840 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); 1841 return NOTIFY_OK; 1842 } 1843 1844 /* VF up/down change detected, schedule to change data path */ 1845 static int netvsc_vf_changed(struct net_device *vf_netdev) 1846 { 1847 struct net_device_context *net_device_ctx; 1848 struct netvsc_device *netvsc_dev; 1849 struct net_device *ndev; 1850 bool vf_is_up = netif_running(vf_netdev); 1851 1852 ndev = get_netvsc_byref(vf_netdev); 1853 if (!ndev) 1854 return NOTIFY_DONE; 1855 1856 net_device_ctx = netdev_priv(ndev); 1857 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); 1858 if (!netvsc_dev) 1859 return NOTIFY_DONE; 1860 1861 netvsc_switch_datapath(ndev, vf_is_up); 1862 netdev_info(ndev, "Data path switched %s VF: %s\n", 1863 vf_is_up ? "to" : "from", vf_netdev->name); 1864 1865 return NOTIFY_OK; 1866 } 1867 1868 static int netvsc_unregister_vf(struct net_device *vf_netdev) 1869 { 1870 struct net_device *ndev; 1871 struct net_device_context *net_device_ctx; 1872 1873 ndev = get_netvsc_byref(vf_netdev); 1874 if (!ndev) 1875 return NOTIFY_DONE; 1876 1877 net_device_ctx = netdev_priv(ndev); 1878 cancel_delayed_work_sync(&net_device_ctx->vf_takeover); 1879 1880 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); 1881 1882 netdev_rx_handler_unregister(vf_netdev); 1883 netdev_upper_dev_unlink(vf_netdev, ndev); 1884 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); 1885 dev_put(vf_netdev); 1886 1887 return NOTIFY_OK; 1888 } 1889 1890 static int netvsc_probe(struct hv_device *dev, 1891 const struct hv_vmbus_device_id *dev_id) 1892 { 1893 struct net_device *net = NULL; 1894 struct net_device_context *net_device_ctx; 1895 struct netvsc_device_info device_info; 1896 struct netvsc_device *nvdev; 1897 int ret = -ENOMEM; 1898 1899 net = alloc_etherdev_mq(sizeof(struct net_device_context), 1900 VRSS_CHANNEL_MAX); 1901 if (!net) 1902 goto no_net; 1903 1904 netif_carrier_off(net); 1905 1906 netvsc_init_settings(net); 1907 1908 net_device_ctx = netdev_priv(net); 1909 net_device_ctx->device_ctx = dev; 1910 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); 1911 if (netif_msg_probe(net_device_ctx)) 1912 netdev_dbg(net, "netvsc msg_enable: %d\n", 1913 net_device_ctx->msg_enable); 1914 1915 hv_set_drvdata(dev, net); 1916 1917 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); 1918 1919 spin_lock_init(&net_device_ctx->lock); 1920 INIT_LIST_HEAD(&net_device_ctx->reconfig_events); 1921 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup); 1922 1923 net_device_ctx->vf_stats 1924 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats); 1925 if (!net_device_ctx->vf_stats) 1926 goto no_stats; 1927 1928 net->netdev_ops = &device_ops; 1929 net->ethtool_ops = ðtool_ops; 1930 SET_NETDEV_DEV(net, &dev->device); 1931 1932 /* We always need headroom for rndis header */ 1933 net->needed_headroom = RNDIS_AND_PPI_SIZE; 1934 1935 /* Notify the netvsc driver of the new device */ 1936 memset(&device_info, 0, sizeof(device_info)); 1937 device_info.ring_size = ring_size; 1938 device_info.num_chn = VRSS_CHANNEL_DEFAULT; 1939 device_info.send_sections = NETVSC_DEFAULT_TX; 1940 device_info.recv_sections = NETVSC_DEFAULT_RX; 1941 1942 nvdev = rndis_filter_device_add(dev, &device_info); 1943 if (IS_ERR(nvdev)) { 1944 ret = PTR_ERR(nvdev); 1945 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); 1946 goto rndis_failed; 1947 } 1948 1949 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); 1950 1951 /* hw_features computed in rndis_filter_device_add */ 1952 net->features = net->hw_features | 1953 NETIF_F_HIGHDMA | NETIF_F_SG | 1954 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; 1955 net->vlan_features = net->features; 1956 1957 netif_set_real_num_tx_queues(net, nvdev->num_chn); 1958 netif_set_real_num_rx_queues(net, nvdev->num_chn); 1959 1960 netdev_lockdep_set_classes(net); 1961 1962 /* MTU range: 68 - 1500 or 65521 */ 1963 net->min_mtu = NETVSC_MTU_MIN; 1964 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) 1965 net->max_mtu = NETVSC_MTU - ETH_HLEN; 1966 else 1967 net->max_mtu = ETH_DATA_LEN; 1968 1969 ret = register_netdev(net); 1970 if (ret != 0) { 1971 pr_err("Unable to register netdev.\n"); 1972 goto register_failed; 1973 } 1974 1975 return ret; 1976 1977 register_failed: 1978 rndis_filter_device_remove(dev, nvdev); 1979 rndis_failed: 1980 free_percpu(net_device_ctx->vf_stats); 1981 no_stats: 1982 hv_set_drvdata(dev, NULL); 1983 free_netdev(net); 1984 no_net: 1985 return ret; 1986 } 1987 1988 static int netvsc_remove(struct hv_device *dev) 1989 { 1990 struct net_device_context *ndev_ctx; 1991 struct net_device *vf_netdev; 1992 struct net_device *net; 1993 1994 net = hv_get_drvdata(dev); 1995 if (net == NULL) { 1996 dev_err(&dev->device, "No net device to remove\n"); 1997 return 0; 1998 } 1999 2000 ndev_ctx = netdev_priv(net); 2001 2002 netif_device_detach(net); 2003 2004 cancel_delayed_work_sync(&ndev_ctx->dwork); 2005 2006 /* 2007 * Call to the vsc driver to let it know that the device is being 2008 * removed. Also blocks mtu and channel changes. 2009 */ 2010 rtnl_lock(); 2011 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); 2012 if (vf_netdev) 2013 netvsc_unregister_vf(vf_netdev); 2014 2015 rndis_filter_device_remove(dev, 2016 rtnl_dereference(ndev_ctx->nvdev)); 2017 unregister_netdevice(net); 2018 rtnl_unlock(); 2019 2020 hv_set_drvdata(dev, NULL); 2021 2022 free_percpu(ndev_ctx->vf_stats); 2023 free_netdev(net); 2024 return 0; 2025 } 2026 2027 static const struct hv_vmbus_device_id id_table[] = { 2028 /* Network guid */ 2029 { HV_NIC_GUID, }, 2030 { }, 2031 }; 2032 2033 MODULE_DEVICE_TABLE(vmbus, id_table); 2034 2035 /* The one and only one */ 2036 static struct hv_driver netvsc_drv = { 2037 .name = KBUILD_MODNAME, 2038 .id_table = id_table, 2039 .probe = netvsc_probe, 2040 .remove = netvsc_remove, 2041 }; 2042 2043 /* 2044 * On Hyper-V, every VF interface is matched with a corresponding 2045 * synthetic interface. The synthetic interface is presented first 2046 * to the guest. When the corresponding VF instance is registered, 2047 * we will take care of switching the data path. 2048 */ 2049 static int netvsc_netdev_event(struct notifier_block *this, 2050 unsigned long event, void *ptr) 2051 { 2052 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 2053 2054 /* Skip our own events */ 2055 if (event_dev->netdev_ops == &device_ops) 2056 return NOTIFY_DONE; 2057 2058 /* Avoid non-Ethernet type devices */ 2059 if (event_dev->type != ARPHRD_ETHER) 2060 return NOTIFY_DONE; 2061 2062 /* Avoid Vlan dev with same MAC registering as VF */ 2063 if (is_vlan_dev(event_dev)) 2064 return NOTIFY_DONE; 2065 2066 /* Avoid Bonding master dev with same MAC registering as VF */ 2067 if ((event_dev->priv_flags & IFF_BONDING) && 2068 (event_dev->flags & IFF_MASTER)) 2069 return NOTIFY_DONE; 2070 2071 switch (event) { 2072 case NETDEV_REGISTER: 2073 return netvsc_register_vf(event_dev); 2074 case NETDEV_UNREGISTER: 2075 return netvsc_unregister_vf(event_dev); 2076 case NETDEV_UP: 2077 case NETDEV_DOWN: 2078 return netvsc_vf_changed(event_dev); 2079 default: 2080 return NOTIFY_DONE; 2081 } 2082 } 2083 2084 static struct notifier_block netvsc_netdev_notifier = { 2085 .notifier_call = netvsc_netdev_event, 2086 }; 2087 2088 static void __exit netvsc_drv_exit(void) 2089 { 2090 unregister_netdevice_notifier(&netvsc_netdev_notifier); 2091 vmbus_driver_unregister(&netvsc_drv); 2092 } 2093 2094 static int __init netvsc_drv_init(void) 2095 { 2096 int ret; 2097 2098 if (ring_size < RING_SIZE_MIN) { 2099 ring_size = RING_SIZE_MIN; 2100 pr_info("Increased ring_size to %d (min allowed)\n", 2101 ring_size); 2102 } 2103 ret = vmbus_driver_register(&netvsc_drv); 2104 2105 if (ret) 2106 return ret; 2107 2108 register_netdevice_notifier(&netvsc_netdev_notifier); 2109 return 0; 2110 } 2111 2112 MODULE_LICENSE("GPL"); 2113 MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); 2114 2115 module_init(netvsc_drv_init); 2116 module_exit(netvsc_drv_exit); 2117