1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2018 Microsoft Corporation 3 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc. 4 * All rights reserved. 5 */ 6 7 #include <stdint.h> 8 #include <string.h> 9 #include <stdio.h> 10 #include <errno.h> 11 #include <unistd.h> 12 #include <dirent.h> 13 #include <net/if.h> 14 #include <net/if_arp.h> 15 #include <sys/ioctl.h> 16 17 #include <rte_ethdev.h> 18 #include <rte_memcpy.h> 19 #include <rte_string_fns.h> 20 #include <rte_memzone.h> 21 #include <rte_devargs.h> 22 #include <rte_malloc.h> 23 #include <rte_kvargs.h> 24 #include <rte_atomic.h> 25 #include <rte_branch_prediction.h> 26 #include <rte_ether.h> 27 #include <ethdev_driver.h> 28 #include <rte_cycles.h> 29 #include <rte_errno.h> 30 #include <rte_memory.h> 31 #include <rte_eal.h> 32 #include <rte_dev.h> 33 #include <rte_bus_vmbus.h> 34 #include <rte_alarm.h> 35 36 #include "hn_logs.h" 37 #include "hn_var.h" 38 #include "hn_rndis.h" 39 #include "hn_nvs.h" 40 #include "ndis.h" 41 42 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \ 43 DEV_TX_OFFLOAD_TCP_CKSUM | \ 44 DEV_TX_OFFLOAD_UDP_CKSUM | \ 45 DEV_TX_OFFLOAD_TCP_TSO | \ 46 DEV_TX_OFFLOAD_MULTI_SEGS | \ 47 DEV_TX_OFFLOAD_VLAN_INSERT) 48 49 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \ 50 DEV_RX_OFFLOAD_VLAN_STRIP | \ 51 DEV_RX_OFFLOAD_RSS_HASH) 52 53 #define NETVSC_ARG_LATENCY "latency" 54 #define NETVSC_ARG_RXBREAK "rx_copybreak" 55 #define NETVSC_ARG_TXBREAK "tx_copybreak" 56 #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable" 57 58 /* The max number of retry when hot adding a VF device */ 59 #define NETVSC_MAX_HOTADD_RETRY 10 60 61 struct hn_xstats_name_off { 62 char name[RTE_ETH_XSTATS_NAME_SIZE]; 63 unsigned int offset; 64 }; 65 66 static const struct hn_xstats_name_off hn_stat_strings[] = { 67 { "good_packets", offsetof(struct hn_stats, packets) }, 68 { "good_bytes", offsetof(struct hn_stats, bytes) }, 69 { "errors", offsetof(struct hn_stats, errors) }, 70 { "ring full", offsetof(struct hn_stats, ring_full) }, 71 { "channel full", offsetof(struct hn_stats, channel_full) }, 72 { "multicast_packets", offsetof(struct hn_stats, multicast) }, 73 { "broadcast_packets", offsetof(struct hn_stats, broadcast) }, 74 { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) }, 75 { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) }, 76 { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) }, 77 { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) }, 78 { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) }, 79 { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) }, 80 { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) }, 81 { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) }, 82 }; 83 84 /* The default RSS key. 85 * This value is the same as MLX5 so that flows will be 86 * received on same path for both VF and synthetic NIC. 87 */ 88 static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = { 89 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7, 90 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94, 91 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1, 92 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59, 93 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a, 94 }; 95 96 static struct rte_eth_dev * 97 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size) 98 { 99 struct rte_eth_dev *eth_dev; 100 const char *name; 101 102 if (!dev) 103 return NULL; 104 105 name = dev->device.name; 106 107 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 108 eth_dev = rte_eth_dev_allocate(name); 109 if (!eth_dev) { 110 PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev"); 111 return NULL; 112 } 113 114 if (private_data_size) { 115 eth_dev->data->dev_private = 116 rte_zmalloc_socket(name, private_data_size, 117 RTE_CACHE_LINE_SIZE, dev->device.numa_node); 118 if (!eth_dev->data->dev_private) { 119 PMD_DRV_LOG(NOTICE, "can not allocate driver data"); 120 rte_eth_dev_release_port(eth_dev); 121 return NULL; 122 } 123 } 124 } else { 125 eth_dev = rte_eth_dev_attach_secondary(name); 126 if (!eth_dev) { 127 PMD_DRV_LOG(NOTICE, "can not attach secondary"); 128 return NULL; 129 } 130 } 131 132 eth_dev->device = &dev->device; 133 134 /* interrupt is simulated */ 135 dev->intr_handle.type = RTE_INTR_HANDLE_EXT; 136 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC; 137 eth_dev->intr_handle = &dev->intr_handle; 138 139 return eth_dev; 140 } 141 142 static void 143 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev) 144 { 145 /* free ether device */ 146 rte_eth_dev_release_port(eth_dev); 147 148 eth_dev->device = NULL; 149 eth_dev->intr_handle = NULL; 150 } 151 152 static int hn_set_parameter(const char *key, const char *value, void *opaque) 153 { 154 struct hn_data *hv = opaque; 155 char *endp = NULL; 156 unsigned long v; 157 158 v = strtoul(value, &endp, 0); 159 if (*value == '\0' || *endp != '\0') { 160 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value); 161 return -EINVAL; 162 } 163 164 if (!strcmp(key, NETVSC_ARG_LATENCY)) { 165 /* usec to nsec */ 166 hv->latency = v * 1000; 167 PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency); 168 } else if (!strcmp(key, NETVSC_ARG_RXBREAK)) { 169 hv->rx_copybreak = v; 170 PMD_DRV_LOG(DEBUG, "rx copy break set to %u", 171 hv->rx_copybreak); 172 } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) { 173 hv->tx_copybreak = v; 174 PMD_DRV_LOG(DEBUG, "tx copy break set to %u", 175 hv->tx_copybreak); 176 } else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) { 177 hv->rx_extmbuf_enable = v; 178 PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u", 179 hv->rx_extmbuf_enable); 180 } 181 182 return 0; 183 } 184 185 /* Parse device arguments */ 186 static int hn_parse_args(const struct rte_eth_dev *dev) 187 { 188 struct hn_data *hv = dev->data->dev_private; 189 struct rte_devargs *devargs = dev->device->devargs; 190 static const char * const valid_keys[] = { 191 NETVSC_ARG_LATENCY, 192 NETVSC_ARG_RXBREAK, 193 NETVSC_ARG_TXBREAK, 194 NETVSC_ARG_RX_EXTMBUF_ENABLE, 195 NULL 196 }; 197 struct rte_kvargs *kvlist; 198 int ret; 199 200 if (!devargs) 201 return 0; 202 203 PMD_INIT_LOG(DEBUG, "device args %s %s", 204 devargs->name, devargs->args); 205 206 kvlist = rte_kvargs_parse(devargs->args, valid_keys); 207 if (!kvlist) { 208 PMD_DRV_LOG(ERR, "invalid parameters"); 209 return -EINVAL; 210 } 211 212 ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv); 213 rte_kvargs_free(kvlist); 214 215 return ret; 216 } 217 218 /* Update link status. 219 * Note: the DPDK definition of "wait_to_complete" 220 * means block this call until link is up. 221 * which is not worth supporting. 222 */ 223 int 224 hn_dev_link_update(struct rte_eth_dev *dev, 225 int wait_to_complete __rte_unused) 226 { 227 struct hn_data *hv = dev->data->dev_private; 228 struct rte_eth_link link, old; 229 int error; 230 231 old = dev->data->dev_link; 232 233 error = hn_rndis_get_linkstatus(hv); 234 if (error) 235 return error; 236 237 hn_rndis_get_linkspeed(hv); 238 239 link = (struct rte_eth_link) { 240 .link_duplex = ETH_LINK_FULL_DUPLEX, 241 .link_autoneg = ETH_LINK_SPEED_FIXED, 242 .link_speed = hv->link_speed / 10000, 243 }; 244 245 if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED) 246 link.link_status = ETH_LINK_UP; 247 else 248 link.link_status = ETH_LINK_DOWN; 249 250 if (old.link_status == link.link_status) 251 return 0; 252 253 PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id, 254 (link.link_status == ETH_LINK_UP) ? "up" : "down"); 255 256 return rte_eth_linkstatus_set(dev, &link); 257 } 258 259 static int hn_dev_info_get(struct rte_eth_dev *dev, 260 struct rte_eth_dev_info *dev_info) 261 { 262 struct hn_data *hv = dev->data->dev_private; 263 int rc; 264 265 dev_info->speed_capa = ETH_LINK_SPEED_10G; 266 dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE; 267 dev_info->max_rx_pktlen = HN_MAX_XFER_LEN; 268 dev_info->max_mac_addrs = 1; 269 270 dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ; 271 dev_info->flow_type_rss_offloads = hv->rss_offloads; 272 dev_info->reta_size = ETH_RSS_RETA_SIZE_128; 273 274 dev_info->max_rx_queues = hv->max_queues; 275 dev_info->max_tx_queues = hv->max_queues; 276 277 dev_info->tx_desc_lim.nb_min = 1; 278 dev_info->tx_desc_lim.nb_max = 4096; 279 280 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 281 return 0; 282 283 /* fills in rx and tx offload capability */ 284 rc = hn_rndis_get_offload(hv, dev_info); 285 if (rc != 0) 286 return rc; 287 288 /* merges the offload and queues of vf */ 289 return hn_vf_info_get(hv, dev_info); 290 } 291 292 static int hn_rss_reta_update(struct rte_eth_dev *dev, 293 struct rte_eth_rss_reta_entry64 *reta_conf, 294 uint16_t reta_size) 295 { 296 struct hn_data *hv = dev->data->dev_private; 297 unsigned int i; 298 int err; 299 300 PMD_INIT_FUNC_TRACE(); 301 302 if (reta_size != NDIS_HASH_INDCNT) { 303 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS"); 304 return -EINVAL; 305 } 306 307 for (i = 0; i < NDIS_HASH_INDCNT; i++) { 308 uint16_t idx = i / RTE_RETA_GROUP_SIZE; 309 uint16_t shift = i % RTE_RETA_GROUP_SIZE; 310 uint64_t mask = (uint64_t)1 << shift; 311 312 if (reta_conf[idx].mask & mask) 313 hv->rss_ind[i] = reta_conf[idx].reta[shift]; 314 } 315 316 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE); 317 if (err) { 318 PMD_DRV_LOG(NOTICE, 319 "rss disable failed"); 320 return err; 321 } 322 323 err = hn_rndis_conf_rss(hv, 0); 324 if (err) { 325 PMD_DRV_LOG(NOTICE, 326 "reta reconfig failed"); 327 return err; 328 } 329 330 return hn_vf_reta_hash_update(dev, reta_conf, reta_size); 331 } 332 333 static int hn_rss_reta_query(struct rte_eth_dev *dev, 334 struct rte_eth_rss_reta_entry64 *reta_conf, 335 uint16_t reta_size) 336 { 337 struct hn_data *hv = dev->data->dev_private; 338 unsigned int i; 339 340 PMD_INIT_FUNC_TRACE(); 341 342 if (reta_size != NDIS_HASH_INDCNT) { 343 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS"); 344 return -EINVAL; 345 } 346 347 for (i = 0; i < NDIS_HASH_INDCNT; i++) { 348 uint16_t idx = i / RTE_RETA_GROUP_SIZE; 349 uint16_t shift = i % RTE_RETA_GROUP_SIZE; 350 uint64_t mask = (uint64_t)1 << shift; 351 352 if (reta_conf[idx].mask & mask) 353 reta_conf[idx].reta[shift] = hv->rss_ind[i]; 354 } 355 return 0; 356 } 357 358 static void hn_rss_hash_init(struct hn_data *hv, 359 const struct rte_eth_rss_conf *rss_conf) 360 { 361 /* Convert from DPDK RSS hash flags to NDIS hash flags */ 362 hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ; 363 364 if (rss_conf->rss_hf & ETH_RSS_IPV4) 365 hv->rss_hash |= NDIS_HASH_IPV4; 366 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) 367 hv->rss_hash |= NDIS_HASH_TCP_IPV4; 368 if (rss_conf->rss_hf & ETH_RSS_IPV6) 369 hv->rss_hash |= NDIS_HASH_IPV6; 370 if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) 371 hv->rss_hash |= NDIS_HASH_IPV6_EX; 372 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) 373 hv->rss_hash |= NDIS_HASH_TCP_IPV6; 374 if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) 375 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX; 376 377 memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key, 378 NDIS_HASH_KEYSIZE_TOEPLITZ); 379 } 380 381 static int hn_rss_hash_update(struct rte_eth_dev *dev, 382 struct rte_eth_rss_conf *rss_conf) 383 { 384 struct hn_data *hv = dev->data->dev_private; 385 int err; 386 387 PMD_INIT_FUNC_TRACE(); 388 389 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE); 390 if (err) { 391 PMD_DRV_LOG(NOTICE, 392 "rss disable failed"); 393 return err; 394 } 395 396 hn_rss_hash_init(hv, rss_conf); 397 398 if (rss_conf->rss_hf != 0) { 399 err = hn_rndis_conf_rss(hv, 0); 400 if (err) { 401 PMD_DRV_LOG(NOTICE, 402 "rss reconfig failed (RSS disabled)"); 403 return err; 404 } 405 } 406 407 return hn_vf_rss_hash_update(dev, rss_conf); 408 } 409 410 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev, 411 struct rte_eth_rss_conf *rss_conf) 412 { 413 struct hn_data *hv = dev->data->dev_private; 414 415 PMD_INIT_FUNC_TRACE(); 416 417 if (hv->ndis_ver < NDIS_VERSION_6_20) { 418 PMD_DRV_LOG(DEBUG, "RSS not supported on this host"); 419 return -EOPNOTSUPP; 420 } 421 422 rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ; 423 if (rss_conf->rss_key) 424 memcpy(rss_conf->rss_key, hv->rss_key, 425 NDIS_HASH_KEYSIZE_TOEPLITZ); 426 427 rss_conf->rss_hf = 0; 428 if (hv->rss_hash & NDIS_HASH_IPV4) 429 rss_conf->rss_hf |= ETH_RSS_IPV4; 430 431 if (hv->rss_hash & NDIS_HASH_TCP_IPV4) 432 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP; 433 434 if (hv->rss_hash & NDIS_HASH_IPV6) 435 rss_conf->rss_hf |= ETH_RSS_IPV6; 436 437 if (hv->rss_hash & NDIS_HASH_IPV6_EX) 438 rss_conf->rss_hf |= ETH_RSS_IPV6_EX; 439 440 if (hv->rss_hash & NDIS_HASH_TCP_IPV6) 441 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP; 442 443 if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX) 444 rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX; 445 446 return 0; 447 } 448 449 static int 450 hn_dev_promiscuous_enable(struct rte_eth_dev *dev) 451 { 452 struct hn_data *hv = dev->data->dev_private; 453 454 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS); 455 return hn_vf_promiscuous_enable(dev); 456 } 457 458 static int 459 hn_dev_promiscuous_disable(struct rte_eth_dev *dev) 460 { 461 struct hn_data *hv = dev->data->dev_private; 462 uint32_t filter; 463 464 filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST; 465 if (dev->data->all_multicast) 466 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST; 467 hn_rndis_set_rxfilter(hv, filter); 468 return hn_vf_promiscuous_disable(dev); 469 } 470 471 static int 472 hn_dev_allmulticast_enable(struct rte_eth_dev *dev) 473 { 474 struct hn_data *hv = dev->data->dev_private; 475 476 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED | 477 NDIS_PACKET_TYPE_ALL_MULTICAST | 478 NDIS_PACKET_TYPE_BROADCAST); 479 return hn_vf_allmulticast_enable(dev); 480 } 481 482 static int 483 hn_dev_allmulticast_disable(struct rte_eth_dev *dev) 484 { 485 struct hn_data *hv = dev->data->dev_private; 486 487 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED | 488 NDIS_PACKET_TYPE_BROADCAST); 489 return hn_vf_allmulticast_disable(dev); 490 } 491 492 static int 493 hn_dev_mc_addr_list(struct rte_eth_dev *dev, 494 struct rte_ether_addr *mc_addr_set, 495 uint32_t nb_mc_addr) 496 { 497 /* No filtering on the synthetic path, but can do it on VF */ 498 return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr); 499 } 500 501 /* Setup shared rx/tx queue data */ 502 static int hn_subchan_configure(struct hn_data *hv, 503 uint32_t subchan) 504 { 505 struct vmbus_channel *primary = hn_primary_chan(hv); 506 int err; 507 unsigned int retry = 0; 508 509 PMD_DRV_LOG(DEBUG, 510 "open %u subchannels", subchan); 511 512 /* Send create sub channels command */ 513 err = hn_nvs_alloc_subchans(hv, &subchan); 514 if (err) 515 return err; 516 517 while (subchan > 0) { 518 struct vmbus_channel *new_sc; 519 uint16_t chn_index; 520 521 err = rte_vmbus_subchan_open(primary, &new_sc); 522 if (err == -ENOENT && ++retry < 1000) { 523 /* This can happen if not ready yet */ 524 rte_delay_ms(10); 525 continue; 526 } 527 528 if (err) { 529 PMD_DRV_LOG(ERR, 530 "open subchannel failed: %d", err); 531 return err; 532 } 533 534 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency); 535 536 retry = 0; 537 chn_index = rte_vmbus_sub_channel_index(new_sc); 538 if (chn_index == 0 || chn_index > hv->max_queues) { 539 PMD_DRV_LOG(ERR, 540 "Invalid subchannel offermsg channel %u", 541 chn_index); 542 return -EIO; 543 } 544 545 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index); 546 hv->channels[chn_index] = new_sc; 547 --subchan; 548 } 549 550 return err; 551 } 552 553 static void netvsc_hotplug_retry(void *args) 554 { 555 int ret; 556 struct hn_data *hv = args; 557 struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id]; 558 struct rte_devargs *d = &hv->devargs; 559 char buf[256]; 560 561 DIR *di; 562 struct dirent *dir; 563 struct ifreq req; 564 struct rte_ether_addr eth_addr; 565 int s; 566 567 PMD_DRV_LOG(DEBUG, "%s: retry count %d", 568 __func__, hv->eal_hot_plug_retry); 569 570 if (hv->eal_hot_plug_retry++ > NETVSC_MAX_HOTADD_RETRY) 571 return; 572 573 snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name); 574 di = opendir(buf); 575 if (!di) { 576 PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, " 577 "retrying in 1 second", __func__, buf); 578 goto retry; 579 } 580 581 while ((dir = readdir(di))) { 582 /* Skip . and .. directories */ 583 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) 584 continue; 585 586 /* trying to get mac address if this is a network device*/ 587 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 588 if (s == -1) { 589 PMD_DRV_LOG(ERR, "Failed to create socket errno %d", 590 errno); 591 break; 592 } 593 strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name)); 594 ret = ioctl(s, SIOCGIFHWADDR, &req); 595 close(s); 596 if (ret == -1) { 597 PMD_DRV_LOG(ERR, 598 "Failed to send SIOCGIFHWADDR for device %s", 599 dir->d_name); 600 break; 601 } 602 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) { 603 closedir(di); 604 return; 605 } 606 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data, 607 RTE_DIM(eth_addr.addr_bytes)); 608 609 if (rte_is_same_ether_addr(ð_addr, dev->data->mac_addrs)) { 610 PMD_DRV_LOG(NOTICE, 611 "Found matching MAC address, adding device %s network name %s", 612 d->name, dir->d_name); 613 ret = rte_eal_hotplug_add(d->bus->name, d->name, 614 d->args); 615 if (ret) { 616 PMD_DRV_LOG(ERR, 617 "Failed to add PCI device %s", 618 d->name); 619 break; 620 } 621 } 622 /* When the code reaches here, we either have already added 623 * the device, or its MAC address did not match. 624 */ 625 closedir(di); 626 return; 627 } 628 closedir(di); 629 retry: 630 /* The device is still being initialized, retry after 1 second */ 631 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv); 632 } 633 634 static void 635 netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type, 636 void *arg) 637 { 638 struct hn_data *hv = arg; 639 struct rte_devargs *d = &hv->devargs; 640 int ret; 641 642 PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s", 643 type, device_name); 644 645 switch (type) { 646 case RTE_DEV_EVENT_ADD: 647 /* if we already has a VF, don't check on hot add */ 648 if (hv->vf_ctx.vf_state > vf_removed) 649 break; 650 651 ret = rte_devargs_parse(d, device_name); 652 if (ret) { 653 PMD_DRV_LOG(ERR, 654 "devargs parsing failed ret=%d", ret); 655 return; 656 } 657 658 if (!strcmp(d->bus->name, "pci")) { 659 /* Start the process of figuring out if this 660 * PCI device is a VF device 661 */ 662 hv->eal_hot_plug_retry = 0; 663 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv); 664 } 665 666 /* We will switch to VF on RDNIS configure message 667 * sent from VSP 668 */ 669 670 break; 671 default: 672 break; 673 } 674 } 675 676 static int hn_dev_configure(struct rte_eth_dev *dev) 677 { 678 struct rte_eth_conf *dev_conf = &dev->data->dev_conf; 679 struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf; 680 const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; 681 const struct rte_eth_txmode *txmode = &dev_conf->txmode; 682 struct hn_data *hv = dev->data->dev_private; 683 uint64_t unsupported; 684 int i, err, subchan; 685 686 PMD_INIT_FUNC_TRACE(); 687 688 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) 689 dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH; 690 691 unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS; 692 if (unsupported) { 693 PMD_DRV_LOG(NOTICE, 694 "unsupported TX offload: %#" PRIx64, 695 unsupported); 696 return -EINVAL; 697 } 698 699 unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS; 700 if (unsupported) { 701 PMD_DRV_LOG(NOTICE, 702 "unsupported RX offload: %#" PRIx64, 703 rxmode->offloads); 704 return -EINVAL; 705 } 706 707 hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP); 708 709 err = hn_rndis_conf_offload(hv, txmode->offloads, 710 rxmode->offloads); 711 if (err) { 712 PMD_DRV_LOG(NOTICE, 713 "offload configure failed"); 714 return err; 715 } 716 717 hv->num_queues = RTE_MAX(dev->data->nb_rx_queues, 718 dev->data->nb_tx_queues); 719 720 for (i = 0; i < NDIS_HASH_INDCNT; i++) 721 hv->rss_ind[i] = i % dev->data->nb_rx_queues; 722 723 hn_rss_hash_init(hv, rss_conf); 724 725 subchan = hv->num_queues - 1; 726 if (subchan > 0) { 727 err = hn_subchan_configure(hv, subchan); 728 if (err) { 729 PMD_DRV_LOG(NOTICE, 730 "subchannel configuration failed"); 731 return err; 732 } 733 734 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE); 735 if (err) { 736 PMD_DRV_LOG(NOTICE, 737 "rss disable failed"); 738 return err; 739 } 740 741 if (rss_conf->rss_hf != 0) { 742 err = hn_rndis_conf_rss(hv, 0); 743 if (err) { 744 PMD_DRV_LOG(NOTICE, 745 "initial RSS config failed"); 746 return err; 747 } 748 } 749 } 750 751 return hn_vf_configure_locked(dev, dev_conf); 752 } 753 754 static int hn_dev_stats_get(struct rte_eth_dev *dev, 755 struct rte_eth_stats *stats) 756 { 757 unsigned int i; 758 759 hn_vf_stats_get(dev, stats); 760 761 for (i = 0; i < dev->data->nb_tx_queues; i++) { 762 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 763 764 if (!txq) 765 continue; 766 767 stats->opackets += txq->stats.packets; 768 stats->obytes += txq->stats.bytes; 769 stats->oerrors += txq->stats.errors; 770 771 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 772 stats->q_opackets[i] = txq->stats.packets; 773 stats->q_obytes[i] = txq->stats.bytes; 774 } 775 } 776 777 for (i = 0; i < dev->data->nb_rx_queues; i++) { 778 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 779 780 if (!rxq) 781 continue; 782 783 stats->ipackets += rxq->stats.packets; 784 stats->ibytes += rxq->stats.bytes; 785 stats->ierrors += rxq->stats.errors; 786 stats->imissed += rxq->stats.ring_full; 787 788 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 789 stats->q_ipackets[i] = rxq->stats.packets; 790 stats->q_ibytes[i] = rxq->stats.bytes; 791 } 792 } 793 794 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 795 return 0; 796 } 797 798 static int 799 hn_dev_stats_reset(struct rte_eth_dev *dev) 800 { 801 unsigned int i; 802 803 PMD_INIT_FUNC_TRACE(); 804 805 for (i = 0; i < dev->data->nb_tx_queues; i++) { 806 struct hn_tx_queue *txq = dev->data->tx_queues[i]; 807 808 if (!txq) 809 continue; 810 memset(&txq->stats, 0, sizeof(struct hn_stats)); 811 } 812 813 for (i = 0; i < dev->data->nb_rx_queues; i++) { 814 struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 815 816 if (!rxq) 817 continue; 818 819 memset(&rxq->stats, 0, sizeof(struct hn_stats)); 820 } 821 822 return 0; 823 } 824 825 static int 826 hn_dev_xstats_reset(struct rte_eth_dev *dev) 827 { 828 int ret; 829 830 ret = hn_dev_stats_reset(dev); 831 if (ret != 0) 832 return 0; 833 834 return hn_vf_xstats_reset(dev); 835 } 836 837 static int 838 hn_dev_xstats_count(struct rte_eth_dev *dev) 839 { 840 int ret, count; 841 842 count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings); 843 count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings); 844 845 ret = hn_vf_xstats_get_names(dev, NULL, 0); 846 if (ret < 0) 847 return ret; 848 849 return count + ret; 850 } 851 852 static int 853 hn_dev_xstats_get_names(struct rte_eth_dev *dev, 854 struct rte_eth_xstat_name *xstats_names, 855 unsigned int limit) 856 { 857 unsigned int i, t, count = 0; 858 int ret; 859 860 if (!xstats_names) 861 return hn_dev_xstats_count(dev); 862 863 /* Note: limit checked in rte_eth_xstats_names() */ 864 for (i = 0; i < dev->data->nb_tx_queues; i++) { 865 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 866 867 if (!txq) 868 continue; 869 870 if (count >= limit) 871 break; 872 873 for (t = 0; t < RTE_DIM(hn_stat_strings); t++) 874 snprintf(xstats_names[count++].name, 875 RTE_ETH_XSTATS_NAME_SIZE, 876 "tx_q%u_%s", i, hn_stat_strings[t].name); 877 } 878 879 for (i = 0; i < dev->data->nb_rx_queues; i++) { 880 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 881 882 if (!rxq) 883 continue; 884 885 if (count >= limit) 886 break; 887 888 for (t = 0; t < RTE_DIM(hn_stat_strings); t++) 889 snprintf(xstats_names[count++].name, 890 RTE_ETH_XSTATS_NAME_SIZE, 891 "rx_q%u_%s", i, 892 hn_stat_strings[t].name); 893 } 894 895 ret = hn_vf_xstats_get_names(dev, xstats_names + count, 896 limit - count); 897 if (ret < 0) 898 return ret; 899 900 return count + ret; 901 } 902 903 static int 904 hn_dev_xstats_get(struct rte_eth_dev *dev, 905 struct rte_eth_xstat *xstats, 906 unsigned int n) 907 { 908 unsigned int i, t, count = 0; 909 const unsigned int nstats = hn_dev_xstats_count(dev); 910 const char *stats; 911 int ret; 912 913 PMD_INIT_FUNC_TRACE(); 914 915 if (n < nstats) 916 return nstats; 917 918 for (i = 0; i < dev->data->nb_tx_queues; i++) { 919 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 920 921 if (!txq) 922 continue; 923 924 stats = (const char *)&txq->stats; 925 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) { 926 xstats[count].id = count; 927 xstats[count].value = *(const uint64_t *) 928 (stats + hn_stat_strings[t].offset); 929 } 930 } 931 932 for (i = 0; i < dev->data->nb_rx_queues; i++) { 933 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 934 935 if (!rxq) 936 continue; 937 938 stats = (const char *)&rxq->stats; 939 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) { 940 xstats[count].id = count; 941 xstats[count].value = *(const uint64_t *) 942 (stats + hn_stat_strings[t].offset); 943 } 944 } 945 946 ret = hn_vf_xstats_get(dev, xstats, count, n); 947 if (ret < 0) 948 return ret; 949 950 return count + ret; 951 } 952 953 static int 954 hn_dev_start(struct rte_eth_dev *dev) 955 { 956 struct hn_data *hv = dev->data->dev_private; 957 int error; 958 959 PMD_INIT_FUNC_TRACE(); 960 961 /* Register to monitor hot plug events */ 962 error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback, 963 hv); 964 if (error) { 965 PMD_DRV_LOG(ERR, "failed to register device event callback"); 966 return error; 967 } 968 969 error = hn_rndis_set_rxfilter(hv, 970 NDIS_PACKET_TYPE_BROADCAST | 971 NDIS_PACKET_TYPE_ALL_MULTICAST | 972 NDIS_PACKET_TYPE_DIRECTED); 973 if (error) 974 return error; 975 976 error = hn_vf_start(dev); 977 if (error) 978 hn_rndis_set_rxfilter(hv, 0); 979 980 /* Initialize Link state */ 981 if (error == 0) 982 hn_dev_link_update(dev, 0); 983 984 return error; 985 } 986 987 static int 988 hn_dev_stop(struct rte_eth_dev *dev) 989 { 990 struct hn_data *hv = dev->data->dev_private; 991 992 PMD_INIT_FUNC_TRACE(); 993 dev->data->dev_started = 0; 994 995 rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv); 996 hn_rndis_set_rxfilter(hv, 0); 997 return hn_vf_stop(dev); 998 } 999 1000 static int 1001 hn_dev_close(struct rte_eth_dev *dev) 1002 { 1003 int ret; 1004 struct hn_data *hv = dev->data->dev_private; 1005 1006 PMD_INIT_FUNC_TRACE(); 1007 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1008 return 0; 1009 1010 rte_eal_alarm_cancel(netvsc_hotplug_retry, &hv->devargs); 1011 1012 ret = hn_vf_close(dev); 1013 hn_dev_free_queues(dev); 1014 1015 return ret; 1016 } 1017 1018 static const struct eth_dev_ops hn_eth_dev_ops = { 1019 .dev_configure = hn_dev_configure, 1020 .dev_start = hn_dev_start, 1021 .dev_stop = hn_dev_stop, 1022 .dev_close = hn_dev_close, 1023 .dev_infos_get = hn_dev_info_get, 1024 .txq_info_get = hn_dev_tx_queue_info, 1025 .rxq_info_get = hn_dev_rx_queue_info, 1026 .dev_supported_ptypes_get = hn_vf_supported_ptypes, 1027 .promiscuous_enable = hn_dev_promiscuous_enable, 1028 .promiscuous_disable = hn_dev_promiscuous_disable, 1029 .allmulticast_enable = hn_dev_allmulticast_enable, 1030 .allmulticast_disable = hn_dev_allmulticast_disable, 1031 .set_mc_addr_list = hn_dev_mc_addr_list, 1032 .reta_update = hn_rss_reta_update, 1033 .reta_query = hn_rss_reta_query, 1034 .rss_hash_update = hn_rss_hash_update, 1035 .rss_hash_conf_get = hn_rss_hash_conf_get, 1036 .tx_queue_setup = hn_dev_tx_queue_setup, 1037 .tx_queue_release = hn_dev_tx_queue_release, 1038 .tx_done_cleanup = hn_dev_tx_done_cleanup, 1039 .rx_queue_setup = hn_dev_rx_queue_setup, 1040 .rx_queue_release = hn_dev_rx_queue_release, 1041 .link_update = hn_dev_link_update, 1042 .stats_get = hn_dev_stats_get, 1043 .stats_reset = hn_dev_stats_reset, 1044 .xstats_get = hn_dev_xstats_get, 1045 .xstats_get_names = hn_dev_xstats_get_names, 1046 .xstats_reset = hn_dev_xstats_reset, 1047 }; 1048 1049 /* 1050 * Setup connection between PMD and kernel. 1051 */ 1052 static int 1053 hn_attach(struct hn_data *hv, unsigned int mtu) 1054 { 1055 int error; 1056 1057 /* Attach NVS */ 1058 error = hn_nvs_attach(hv, mtu); 1059 if (error) 1060 goto failed_nvs; 1061 1062 /* Attach RNDIS */ 1063 error = hn_rndis_attach(hv); 1064 if (error) 1065 goto failed_rndis; 1066 1067 /* 1068 * NOTE: 1069 * Under certain conditions on certain versions of Hyper-V, 1070 * the RNDIS rxfilter is _not_ zero on the hypervisor side 1071 * after the successful RNDIS initialization. 1072 */ 1073 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE); 1074 return 0; 1075 failed_rndis: 1076 hn_nvs_detach(hv); 1077 failed_nvs: 1078 return error; 1079 } 1080 1081 static void 1082 hn_detach(struct hn_data *hv) 1083 { 1084 hn_nvs_detach(hv); 1085 hn_rndis_detach(hv); 1086 } 1087 1088 static int 1089 eth_hn_dev_init(struct rte_eth_dev *eth_dev) 1090 { 1091 struct hn_data *hv = eth_dev->data->dev_private; 1092 struct rte_device *device = eth_dev->device; 1093 struct rte_vmbus_device *vmbus; 1094 unsigned int rxr_cnt; 1095 int err, max_chan; 1096 1097 PMD_INIT_FUNC_TRACE(); 1098 1099 vmbus = container_of(device, struct rte_vmbus_device, device); 1100 eth_dev->dev_ops = &hn_eth_dev_ops; 1101 eth_dev->rx_queue_count = hn_dev_rx_queue_count; 1102 eth_dev->rx_descriptor_status = hn_dev_rx_queue_status; 1103 eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status; 1104 eth_dev->tx_pkt_burst = &hn_xmit_pkts; 1105 eth_dev->rx_pkt_burst = &hn_recv_pkts; 1106 1107 /* 1108 * for secondary processes, we don't initialize any further as primary 1109 * has already done this work. 1110 */ 1111 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1112 return 0; 1113 1114 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS; 1115 1116 /* Since Hyper-V only supports one MAC address */ 1117 eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS, 1118 sizeof(struct rte_ether_addr), 0); 1119 if (eth_dev->data->mac_addrs == NULL) { 1120 PMD_INIT_LOG(ERR, 1121 "Failed to allocate memory store MAC addresses"); 1122 return -ENOMEM; 1123 } 1124 1125 hv->vmbus = vmbus; 1126 hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP]; 1127 hv->chim_res = &vmbus->resource[HV_SEND_BUF_MAP]; 1128 hv->port_id = eth_dev->data->port_id; 1129 hv->latency = HN_CHAN_LATENCY_NS; 1130 hv->rx_copybreak = HN_RXCOPY_THRESHOLD; 1131 hv->tx_copybreak = HN_TXCOPY_THRESHOLD; 1132 hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE; 1133 hv->max_queues = 1; 1134 1135 rte_rwlock_init(&hv->vf_lock); 1136 hv->vf_ctx.vf_vsc_switched = false; 1137 hv->vf_ctx.vf_vsp_reported = false; 1138 hv->vf_ctx.vf_attached = false; 1139 hv->vf_ctx.vf_state = vf_unknown; 1140 1141 err = hn_parse_args(eth_dev); 1142 if (err) 1143 return err; 1144 1145 strlcpy(hv->owner.name, eth_dev->device->name, 1146 RTE_ETH_MAX_OWNER_NAME_LEN); 1147 err = rte_eth_dev_owner_new(&hv->owner.id); 1148 if (err) { 1149 PMD_INIT_LOG(ERR, "Can not get owner id"); 1150 return err; 1151 } 1152 1153 /* Initialize primary channel input for control operations */ 1154 err = rte_vmbus_chan_open(vmbus, &hv->channels[0]); 1155 if (err) 1156 return err; 1157 1158 rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency); 1159 1160 hv->primary = hn_rx_queue_alloc(hv, 0, 1161 eth_dev->device->numa_node); 1162 1163 if (!hv->primary) 1164 return -ENOMEM; 1165 1166 err = hn_attach(hv, RTE_ETHER_MTU); 1167 if (err) 1168 goto failed; 1169 1170 err = hn_chim_init(eth_dev); 1171 if (err) 1172 goto failed; 1173 1174 err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes); 1175 if (err) 1176 goto failed; 1177 1178 /* Multi queue requires later versions of windows server */ 1179 if (hv->nvs_ver < NVS_VERSION_5) 1180 return 0; 1181 1182 max_chan = rte_vmbus_max_channels(vmbus); 1183 PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan); 1184 if (max_chan <= 0) 1185 goto failed; 1186 1187 if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0) 1188 rxr_cnt = 1; 1189 1190 hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan); 1191 1192 /* If VF was reported but not added, do it now */ 1193 if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) { 1194 PMD_INIT_LOG(DEBUG, "Adding VF device"); 1195 1196 err = hn_vf_add(eth_dev, hv); 1197 } 1198 1199 return 0; 1200 1201 failed: 1202 PMD_INIT_LOG(NOTICE, "device init failed"); 1203 1204 hn_chim_uninit(eth_dev); 1205 hn_detach(hv); 1206 return err; 1207 } 1208 1209 static int 1210 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev) 1211 { 1212 struct hn_data *hv = eth_dev->data->dev_private; 1213 int ret, ret_stop; 1214 1215 PMD_INIT_FUNC_TRACE(); 1216 1217 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1218 return 0; 1219 1220 ret_stop = hn_dev_stop(eth_dev); 1221 hn_dev_close(eth_dev); 1222 1223 hn_detach(hv); 1224 hn_chim_uninit(eth_dev); 1225 rte_vmbus_chan_close(hv->primary->chan); 1226 rte_free(hv->primary); 1227 ret = rte_eth_dev_owner_delete(hv->owner.id); 1228 if (ret != 0) 1229 return ret; 1230 1231 return ret_stop; 1232 } 1233 1234 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused, 1235 struct rte_vmbus_device *dev) 1236 { 1237 struct rte_eth_dev *eth_dev; 1238 int ret; 1239 1240 PMD_INIT_FUNC_TRACE(); 1241 1242 ret = rte_dev_event_monitor_start(); 1243 if (ret) { 1244 PMD_DRV_LOG(ERR, "Failed to start device event monitoring"); 1245 return ret; 1246 } 1247 1248 eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data)); 1249 if (!eth_dev) 1250 return -ENOMEM; 1251 1252 ret = eth_hn_dev_init(eth_dev); 1253 if (ret) { 1254 eth_dev_vmbus_release(eth_dev); 1255 rte_dev_event_monitor_stop(); 1256 } else { 1257 rte_eth_dev_probing_finish(eth_dev); 1258 } 1259 1260 return ret; 1261 } 1262 1263 static int eth_hn_remove(struct rte_vmbus_device *dev) 1264 { 1265 struct rte_eth_dev *eth_dev; 1266 int ret; 1267 1268 PMD_INIT_FUNC_TRACE(); 1269 1270 eth_dev = rte_eth_dev_allocated(dev->device.name); 1271 if (!eth_dev) 1272 return 0; /* port already released */ 1273 1274 ret = eth_hn_dev_uninit(eth_dev); 1275 if (ret) 1276 return ret; 1277 1278 eth_dev_vmbus_release(eth_dev); 1279 rte_dev_event_monitor_stop(); 1280 return 0; 1281 } 1282 1283 /* Network device GUID */ 1284 static const rte_uuid_t hn_net_ids[] = { 1285 /* f8615163-df3e-46c5-913f-f2d2f965ed0e */ 1286 RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL), 1287 { 0 } 1288 }; 1289 1290 static struct rte_vmbus_driver rte_netvsc_pmd = { 1291 .id_table = hn_net_ids, 1292 .probe = eth_hn_probe, 1293 .remove = eth_hn_remove, 1294 }; 1295 1296 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd); 1297 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic"); 1298 RTE_LOG_REGISTER(hn_logtype_init, pmd.net.netvsc.init, NOTICE); 1299 RTE_LOG_REGISTER(hn_logtype_driver, pmd.net.netvsc.driver, NOTICE); 1300 RTE_PMD_REGISTER_PARAM_STRING(net_netvsc, 1301 NETVSC_ARG_LATENCY "=<uint32> " 1302 NETVSC_ARG_RXBREAK "=<uint32> " 1303 NETVSC_ARG_TXBREAK "=<uint32> " 1304 NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>"); 1305