1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Microsoft Corporation
3 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
14 #include <net/if_arp.h>
15 #include <sys/ioctl.h>
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>
33 #include <rte_bus_vmbus.h>
34 #include <rte_alarm.h>
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)
49 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \
50 DEV_RX_OFFLOAD_VLAN_STRIP | \
51 DEV_RX_OFFLOAD_RSS_HASH)
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"
58 /* The max number of retry when hot adding a VF device */
59 #define NETVSC_MAX_HOTADD_RETRY 10
61 struct hn_xstats_name_off {
62 char name[RTE_ETH_XSTATS_NAME_SIZE];
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]) },
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.
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,
96 static struct rte_eth_dev *
97 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
99 struct rte_eth_dev *eth_dev;
105 name = dev->device.name;
107 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
108 eth_dev = rte_eth_dev_allocate(name);
110 PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
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);
125 eth_dev = rte_eth_dev_attach_secondary(name);
127 PMD_DRV_LOG(NOTICE, "can not attach secondary");
132 eth_dev->device = &dev->device;
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;
143 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
145 /* free ether device */
146 rte_eth_dev_release_port(eth_dev);
148 eth_dev->device = NULL;
149 eth_dev->intr_handle = NULL;
152 static int hn_set_parameter(const char *key, const char *value, void *opaque)
154 struct hn_data *hv = opaque;
158 v = strtoul(value, &endp, 0);
159 if (*value == '\0' || *endp != '\0') {
160 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
164 if (!strcmp(key, NETVSC_ARG_LATENCY)) {
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",
172 } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
173 hv->tx_copybreak = v;
174 PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
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);
185 /* Parse device arguments */
186 static int hn_parse_args(const struct rte_eth_dev *dev)
188 struct hn_data *hv = dev->data->dev_private;
189 struct rte_devargs *devargs = dev->device->devargs;
190 static const char * const valid_keys[] = {
194 NETVSC_ARG_RX_EXTMBUF_ENABLE,
197 struct rte_kvargs *kvlist;
203 PMD_INIT_LOG(DEBUG, "device args %s %s",
204 devargs->name, devargs->args);
206 kvlist = rte_kvargs_parse(devargs->args, valid_keys);
208 PMD_DRV_LOG(ERR, "invalid parameters");
212 ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
213 rte_kvargs_free(kvlist);
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.
224 hn_dev_link_update(struct rte_eth_dev *dev,
225 int wait_to_complete __rte_unused)
227 struct hn_data *hv = dev->data->dev_private;
228 struct rte_eth_link link, old;
231 old = dev->data->dev_link;
233 error = hn_rndis_get_linkstatus(hv);
237 hn_rndis_get_linkspeed(hv);
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,
245 if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
246 link.link_status = ETH_LINK_UP;
248 link.link_status = ETH_LINK_DOWN;
250 if (old.link_status == link.link_status)
253 PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
254 (link.link_status == ETH_LINK_UP) ? "up" : "down");
256 return rte_eth_linkstatus_set(dev, &link);
259 static int hn_dev_info_get(struct rte_eth_dev *dev,
260 struct rte_eth_dev_info *dev_info)
262 struct hn_data *hv = dev->data->dev_private;
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;
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;
274 dev_info->max_rx_queues = hv->max_queues;
275 dev_info->max_tx_queues = hv->max_queues;
277 dev_info->tx_desc_lim.nb_min = 1;
278 dev_info->tx_desc_lim.nb_max = 4096;
280 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
283 /* fills in rx and tx offload capability */
284 rc = hn_rndis_get_offload(hv, dev_info);
288 /* merges the offload and queues of vf */
289 return hn_vf_info_get(hv, dev_info);
292 static int hn_rss_reta_update(struct rte_eth_dev *dev,
293 struct rte_eth_rss_reta_entry64 *reta_conf,
296 struct hn_data *hv = dev->data->dev_private;
300 PMD_INIT_FUNC_TRACE();
302 if (reta_size != NDIS_HASH_INDCNT) {
303 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
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;
312 if (reta_conf[idx].mask & mask)
313 hv->rss_ind[i] = reta_conf[idx].reta[shift];
316 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
319 "rss disable failed");
323 err = hn_rndis_conf_rss(hv, 0);
326 "reta reconfig failed");
330 return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
333 static int hn_rss_reta_query(struct rte_eth_dev *dev,
334 struct rte_eth_rss_reta_entry64 *reta_conf,
337 struct hn_data *hv = dev->data->dev_private;
340 PMD_INIT_FUNC_TRACE();
342 if (reta_size != NDIS_HASH_INDCNT) {
343 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
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;
352 if (reta_conf[idx].mask & mask)
353 reta_conf[idx].reta[shift] = hv->rss_ind[i];
358 static void hn_rss_hash_init(struct hn_data *hv,
359 const struct rte_eth_rss_conf *rss_conf)
361 /* Convert from DPDK RSS hash flags to NDIS hash flags */
362 hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
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;
377 memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
378 NDIS_HASH_KEYSIZE_TOEPLITZ);
381 static int hn_rss_hash_update(struct rte_eth_dev *dev,
382 struct rte_eth_rss_conf *rss_conf)
384 struct hn_data *hv = dev->data->dev_private;
387 PMD_INIT_FUNC_TRACE();
389 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
392 "rss disable failed");
396 hn_rss_hash_init(hv, rss_conf);
398 if (rss_conf->rss_hf != 0) {
399 err = hn_rndis_conf_rss(hv, 0);
402 "rss reconfig failed (RSS disabled)");
407 return hn_vf_rss_hash_update(dev, rss_conf);
410 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
411 struct rte_eth_rss_conf *rss_conf)
413 struct hn_data *hv = dev->data->dev_private;
415 PMD_INIT_FUNC_TRACE();
417 if (hv->ndis_ver < NDIS_VERSION_6_20) {
418 PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
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);
427 rss_conf->rss_hf = 0;
428 if (hv->rss_hash & NDIS_HASH_IPV4)
429 rss_conf->rss_hf |= ETH_RSS_IPV4;
431 if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
432 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
434 if (hv->rss_hash & NDIS_HASH_IPV6)
435 rss_conf->rss_hf |= ETH_RSS_IPV6;
437 if (hv->rss_hash & NDIS_HASH_IPV6_EX)
438 rss_conf->rss_hf |= ETH_RSS_IPV6_EX;
440 if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
441 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
443 if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
444 rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX;
450 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
452 struct hn_data *hv = dev->data->dev_private;
454 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
455 return hn_vf_promiscuous_enable(dev);
459 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
461 struct hn_data *hv = dev->data->dev_private;
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);
472 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
474 struct hn_data *hv = dev->data->dev_private;
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);
483 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
485 struct hn_data *hv = dev->data->dev_private;
487 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
488 NDIS_PACKET_TYPE_BROADCAST);
489 return hn_vf_allmulticast_disable(dev);
493 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
494 struct rte_ether_addr *mc_addr_set,
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);
501 /* Setup shared rx/tx queue data */
502 static int hn_subchan_configure(struct hn_data *hv,
505 struct vmbus_channel *primary = hn_primary_chan(hv);
507 unsigned int retry = 0;
510 "open %u subchannels", subchan);
512 /* Send create sub channels command */
513 err = hn_nvs_alloc_subchans(hv, &subchan);
517 while (subchan > 0) {
518 struct vmbus_channel *new_sc;
521 err = rte_vmbus_subchan_open(primary, &new_sc);
522 if (err == -ENOENT && ++retry < 1000) {
523 /* This can happen if not ready yet */
530 "open subchannel failed: %d", err);
534 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
537 chn_index = rte_vmbus_sub_channel_index(new_sc);
538 if (chn_index == 0 || chn_index > hv->max_queues) {
540 "Invalid subchannel offermsg channel %u",
545 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
546 hv->channels[chn_index] = new_sc;
553 static void netvsc_hotplug_retry(void *args)
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;
564 struct rte_ether_addr eth_addr;
567 PMD_DRV_LOG(DEBUG, "%s: retry count %d\n",
568 __func__, hv->eal_hot_plug_retry);
570 if (hv->eal_hot_plug_retry++ > NETVSC_MAX_HOTADD_RETRY)
573 snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name);
576 PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, "
577 "retrying in 1 second\n", __func__, buf);
581 while ((dir = readdir(di))) {
582 /* Skip . and .. directories */
583 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
586 /* trying to get mac address if this is a network device*/
587 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
589 PMD_DRV_LOG(ERR, "Failed to create socket errno %d\n",
593 strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name));
594 ret = ioctl(s, SIOCGIFHWADDR, &req);
597 PMD_DRV_LOG(ERR, "Failed to send SIOCGIFHWADDR for "
598 "device %s\n", dir->d_name);
601 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
605 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
606 RTE_DIM(eth_addr.addr_bytes));
608 if (rte_is_same_ether_addr(ð_addr, dev->data->mac_addrs)) {
609 PMD_DRV_LOG(NOTICE, "Found matching MAC address, "
610 "adding device %s network name %s\n",
611 d->name, dir->d_name);
612 ret = rte_eal_hotplug_add(d->bus->name, d->name,
616 "Failed to add PCI device %s\n",
621 /* When the code reaches here, we either have already added
622 * the device, or its MAC address did not match.
629 /* The device is still being initialized, retry after 1 second */
630 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv);
634 netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type,
637 struct hn_data *hv = arg;
638 struct rte_devargs *d = &hv->devargs;
641 PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s\n",
645 case RTE_DEV_EVENT_ADD:
646 /* if we already has a VF, don't check on hot add */
647 if (hv->vf_ctx.vf_state > vf_removed)
650 ret = rte_devargs_parse(d, device_name);
653 "devargs parsing failed ret=%d\n", ret);
657 if (!strcmp(d->bus->name, "pci")) {
658 /* Start the process of figuring out if this
659 * PCI device is a VF device
661 hv->eal_hot_plug_retry = 0;
662 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv);
665 /* We will switch to VF on RDNIS configure message
675 static int hn_dev_configure(struct rte_eth_dev *dev)
677 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
678 struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
679 const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
680 const struct rte_eth_txmode *txmode = &dev_conf->txmode;
681 struct hn_data *hv = dev->data->dev_private;
682 uint64_t unsupported;
685 PMD_INIT_FUNC_TRACE();
687 if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
688 dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
690 unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
693 "unsupported TX offload: %#" PRIx64,
698 unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
701 "unsupported RX offload: %#" PRIx64,
706 hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
708 err = hn_rndis_conf_offload(hv, txmode->offloads,
712 "offload configure failed");
716 hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
717 dev->data->nb_tx_queues);
719 for (i = 0; i < NDIS_HASH_INDCNT; i++)
720 hv->rss_ind[i] = i % dev->data->nb_rx_queues;
722 hn_rss_hash_init(hv, rss_conf);
724 subchan = hv->num_queues - 1;
726 err = hn_subchan_configure(hv, subchan);
729 "subchannel configuration failed");
733 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
736 "rss disable failed");
740 if (rss_conf->rss_hf != 0) {
741 err = hn_rndis_conf_rss(hv, 0);
744 "initial RSS config failed");
750 return hn_vf_configure_locked(dev, dev_conf);
753 static int hn_dev_stats_get(struct rte_eth_dev *dev,
754 struct rte_eth_stats *stats)
758 hn_vf_stats_get(dev, stats);
760 for (i = 0; i < dev->data->nb_tx_queues; i++) {
761 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
766 stats->opackets += txq->stats.packets;
767 stats->obytes += txq->stats.bytes;
768 stats->oerrors += txq->stats.errors;
770 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
771 stats->q_opackets[i] = txq->stats.packets;
772 stats->q_obytes[i] = txq->stats.bytes;
776 for (i = 0; i < dev->data->nb_rx_queues; i++) {
777 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
782 stats->ipackets += rxq->stats.packets;
783 stats->ibytes += rxq->stats.bytes;
784 stats->ierrors += rxq->stats.errors;
785 stats->imissed += rxq->stats.ring_full;
787 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
788 stats->q_ipackets[i] = rxq->stats.packets;
789 stats->q_ibytes[i] = rxq->stats.bytes;
793 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
798 hn_dev_stats_reset(struct rte_eth_dev *dev)
802 PMD_INIT_FUNC_TRACE();
804 for (i = 0; i < dev->data->nb_tx_queues; i++) {
805 struct hn_tx_queue *txq = dev->data->tx_queues[i];
809 memset(&txq->stats, 0, sizeof(struct hn_stats));
812 for (i = 0; i < dev->data->nb_rx_queues; i++) {
813 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
818 memset(&rxq->stats, 0, sizeof(struct hn_stats));
825 hn_dev_xstats_reset(struct rte_eth_dev *dev)
829 ret = hn_dev_stats_reset(dev);
833 return hn_vf_xstats_reset(dev);
837 hn_dev_xstats_count(struct rte_eth_dev *dev)
841 count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
842 count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
844 ret = hn_vf_xstats_get_names(dev, NULL, 0);
852 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
853 struct rte_eth_xstat_name *xstats_names,
856 unsigned int i, t, count = 0;
860 return hn_dev_xstats_count(dev);
862 /* Note: limit checked in rte_eth_xstats_names() */
863 for (i = 0; i < dev->data->nb_tx_queues; i++) {
864 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
872 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
873 snprintf(xstats_names[count++].name,
874 RTE_ETH_XSTATS_NAME_SIZE,
875 "tx_q%u_%s", i, hn_stat_strings[t].name);
878 for (i = 0; i < dev->data->nb_rx_queues; i++) {
879 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
887 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
888 snprintf(xstats_names[count++].name,
889 RTE_ETH_XSTATS_NAME_SIZE,
891 hn_stat_strings[t].name);
894 ret = hn_vf_xstats_get_names(dev, xstats_names + count,
903 hn_dev_xstats_get(struct rte_eth_dev *dev,
904 struct rte_eth_xstat *xstats,
907 unsigned int i, t, count = 0;
908 const unsigned int nstats = hn_dev_xstats_count(dev);
912 PMD_INIT_FUNC_TRACE();
917 for (i = 0; i < dev->data->nb_tx_queues; i++) {
918 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
923 stats = (const char *)&txq->stats;
924 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
925 xstats[count].id = count;
926 xstats[count].value = *(const uint64_t *)
927 (stats + hn_stat_strings[t].offset);
931 for (i = 0; i < dev->data->nb_rx_queues; i++) {
932 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
937 stats = (const char *)&rxq->stats;
938 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
939 xstats[count].id = count;
940 xstats[count].value = *(const uint64_t *)
941 (stats + hn_stat_strings[t].offset);
945 ret = hn_vf_xstats_get(dev, xstats, count, n);
953 hn_dev_start(struct rte_eth_dev *dev)
955 struct hn_data *hv = dev->data->dev_private;
958 PMD_INIT_FUNC_TRACE();
960 /* Register to monitor hot plug events */
961 error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback,
964 PMD_DRV_LOG(ERR, "failed to register device event callback\n");
968 error = hn_rndis_set_rxfilter(hv,
969 NDIS_PACKET_TYPE_BROADCAST |
970 NDIS_PACKET_TYPE_ALL_MULTICAST |
971 NDIS_PACKET_TYPE_DIRECTED);
975 error = hn_vf_start(dev);
977 hn_rndis_set_rxfilter(hv, 0);
979 /* Initialize Link state */
981 hn_dev_link_update(dev, 0);
987 hn_dev_stop(struct rte_eth_dev *dev)
989 struct hn_data *hv = dev->data->dev_private;
991 PMD_INIT_FUNC_TRACE();
992 dev->data->dev_started = 0;
994 rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv);
995 hn_rndis_set_rxfilter(hv, 0);
996 return hn_vf_stop(dev);
1000 hn_dev_close(struct rte_eth_dev *dev)
1003 struct hn_data *hv = dev->data->dev_private;
1005 PMD_INIT_FUNC_TRACE();
1006 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1009 rte_eal_alarm_cancel(netvsc_hotplug_retry, &hv->devargs);
1011 ret = hn_vf_close(dev);
1012 hn_dev_free_queues(dev);
1017 static const struct eth_dev_ops hn_eth_dev_ops = {
1018 .dev_configure = hn_dev_configure,
1019 .dev_start = hn_dev_start,
1020 .dev_stop = hn_dev_stop,
1021 .dev_close = hn_dev_close,
1022 .dev_infos_get = hn_dev_info_get,
1023 .txq_info_get = hn_dev_tx_queue_info,
1024 .rxq_info_get = hn_dev_rx_queue_info,
1025 .dev_supported_ptypes_get = hn_vf_supported_ptypes,
1026 .promiscuous_enable = hn_dev_promiscuous_enable,
1027 .promiscuous_disable = hn_dev_promiscuous_disable,
1028 .allmulticast_enable = hn_dev_allmulticast_enable,
1029 .allmulticast_disable = hn_dev_allmulticast_disable,
1030 .set_mc_addr_list = hn_dev_mc_addr_list,
1031 .reta_update = hn_rss_reta_update,
1032 .reta_query = hn_rss_reta_query,
1033 .rss_hash_update = hn_rss_hash_update,
1034 .rss_hash_conf_get = hn_rss_hash_conf_get,
1035 .tx_queue_setup = hn_dev_tx_queue_setup,
1036 .tx_queue_release = hn_dev_tx_queue_release,
1037 .tx_done_cleanup = hn_dev_tx_done_cleanup,
1038 .rx_queue_setup = hn_dev_rx_queue_setup,
1039 .rx_queue_release = hn_dev_rx_queue_release,
1040 .link_update = hn_dev_link_update,
1041 .stats_get = hn_dev_stats_get,
1042 .stats_reset = hn_dev_stats_reset,
1043 .xstats_get = hn_dev_xstats_get,
1044 .xstats_get_names = hn_dev_xstats_get_names,
1045 .xstats_reset = hn_dev_xstats_reset,
1049 * Setup connection between PMD and kernel.
1052 hn_attach(struct hn_data *hv, unsigned int mtu)
1057 error = hn_nvs_attach(hv, mtu);
1062 error = hn_rndis_attach(hv);
1068 * Under certain conditions on certain versions of Hyper-V,
1069 * the RNDIS rxfilter is _not_ zero on the hypervisor side
1070 * after the successful RNDIS initialization.
1072 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
1081 hn_detach(struct hn_data *hv)
1084 hn_rndis_detach(hv);
1088 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
1090 struct hn_data *hv = eth_dev->data->dev_private;
1091 struct rte_device *device = eth_dev->device;
1092 struct rte_vmbus_device *vmbus;
1093 unsigned int rxr_cnt;
1096 PMD_INIT_FUNC_TRACE();
1098 vmbus = container_of(device, struct rte_vmbus_device, device);
1099 eth_dev->dev_ops = &hn_eth_dev_ops;
1100 eth_dev->rx_queue_count = hn_dev_rx_queue_count;
1101 eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
1102 eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
1103 eth_dev->tx_pkt_burst = &hn_xmit_pkts;
1104 eth_dev->rx_pkt_burst = &hn_recv_pkts;
1107 * for secondary processes, we don't initialize any further as primary
1108 * has already done this work.
1110 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1113 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1115 /* Since Hyper-V only supports one MAC address */
1116 eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
1117 sizeof(struct rte_ether_addr), 0);
1118 if (eth_dev->data->mac_addrs == NULL) {
1120 "Failed to allocate memory store MAC addresses");
1125 hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
1126 hv->chim_res = &vmbus->resource[HV_SEND_BUF_MAP];
1127 hv->port_id = eth_dev->data->port_id;
1128 hv->latency = HN_CHAN_LATENCY_NS;
1129 hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
1130 hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
1131 hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
1134 rte_rwlock_init(&hv->vf_lock);
1135 hv->vf_ctx.vf_vsc_switched = false;
1136 hv->vf_ctx.vf_vsp_reported = false;
1137 hv->vf_ctx.vf_attached = false;
1138 hv->vf_ctx.vf_state = vf_unknown;
1140 err = hn_parse_args(eth_dev);
1144 strlcpy(hv->owner.name, eth_dev->device->name,
1145 RTE_ETH_MAX_OWNER_NAME_LEN);
1146 err = rte_eth_dev_owner_new(&hv->owner.id);
1148 PMD_INIT_LOG(ERR, "Can not get owner id");
1152 /* Initialize primary channel input for control operations */
1153 err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
1157 rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1159 hv->primary = hn_rx_queue_alloc(hv, 0,
1160 eth_dev->device->numa_node);
1165 err = hn_attach(hv, RTE_ETHER_MTU);
1169 err = hn_chim_init(eth_dev);
1173 err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
1177 /* Multi queue requires later versions of windows server */
1178 if (hv->nvs_ver < NVS_VERSION_5)
1181 max_chan = rte_vmbus_max_channels(vmbus);
1182 PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1186 if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1189 hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1191 /* If VF was reported but not added, do it now */
1192 if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) {
1193 PMD_INIT_LOG(DEBUG, "Adding VF device");
1195 err = hn_vf_add(eth_dev, hv);
1201 PMD_INIT_LOG(NOTICE, "device init failed");
1203 hn_chim_uninit(eth_dev);
1209 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1211 struct hn_data *hv = eth_dev->data->dev_private;
1214 PMD_INIT_FUNC_TRACE();
1216 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1219 ret_stop = hn_dev_stop(eth_dev);
1220 hn_dev_close(eth_dev);
1223 hn_chim_uninit(eth_dev);
1224 rte_vmbus_chan_close(hv->primary->chan);
1225 rte_free(hv->primary);
1226 ret = rte_eth_dev_owner_delete(hv->owner.id);
1233 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1234 struct rte_vmbus_device *dev)
1236 struct rte_eth_dev *eth_dev;
1239 PMD_INIT_FUNC_TRACE();
1241 ret = rte_dev_event_monitor_start();
1243 PMD_DRV_LOG(ERR, "Failed to start device event monitoring\n");
1247 eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1251 ret = eth_hn_dev_init(eth_dev);
1253 eth_dev_vmbus_release(eth_dev);
1254 rte_dev_event_monitor_stop();
1256 rte_eth_dev_probing_finish(eth_dev);
1262 static int eth_hn_remove(struct rte_vmbus_device *dev)
1264 struct rte_eth_dev *eth_dev;
1267 PMD_INIT_FUNC_TRACE();
1269 eth_dev = rte_eth_dev_allocated(dev->device.name);
1271 return 0; /* port already released */
1273 ret = eth_hn_dev_uninit(eth_dev);
1277 eth_dev_vmbus_release(eth_dev);
1278 rte_dev_event_monitor_stop();
1282 /* Network device GUID */
1283 static const rte_uuid_t hn_net_ids[] = {
1284 /* f8615163-df3e-46c5-913f-f2d2f965ed0e */
1285 RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1289 static struct rte_vmbus_driver rte_netvsc_pmd = {
1290 .id_table = hn_net_ids,
1291 .probe = eth_hn_probe,
1292 .remove = eth_hn_remove,
1295 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1296 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1297 RTE_LOG_REGISTER(hn_logtype_init, pmd.net.netvsc.init, NOTICE);
1298 RTE_LOG_REGISTER(hn_logtype_driver, pmd.net.netvsc.driver, NOTICE);
1299 RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
1300 NETVSC_ARG_LATENCY "=<uint32> "
1301 NETVSC_ARG_RXBREAK "=<uint32> "
1302 NETVSC_ARG_TXBREAK "=<uint32> "
1303 NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");