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 <netinet/in.h>
16 #include <sys/ioctl.h>
18 #include <rte_ethdev.h>
19 #include <rte_memcpy.h>
20 #include <rte_string_fns.h>
21 #include <rte_memzone.h>
22 #include <rte_devargs.h>
23 #include <rte_malloc.h>
24 #include <rte_kvargs.h>
25 #include <rte_atomic.h>
26 #include <rte_branch_prediction.h>
27 #include <rte_ether.h>
28 #include <ethdev_driver.h>
29 #include <rte_cycles.h>
30 #include <rte_errno.h>
31 #include <rte_memory.h>
34 #include <rte_bus_vmbus.h>
35 #include <rte_alarm.h>
43 #define HN_TX_OFFLOAD_CAPS (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
44 RTE_ETH_TX_OFFLOAD_TCP_CKSUM | \
45 RTE_ETH_TX_OFFLOAD_UDP_CKSUM | \
46 RTE_ETH_TX_OFFLOAD_TCP_TSO | \
47 RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
48 RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
50 #define HN_RX_OFFLOAD_CAPS (RTE_ETH_RX_OFFLOAD_CHECKSUM | \
51 RTE_ETH_RX_OFFLOAD_VLAN_STRIP | \
52 RTE_ETH_RX_OFFLOAD_RSS_HASH)
54 #define NETVSC_ARG_LATENCY "latency"
55 #define NETVSC_ARG_RXBREAK "rx_copybreak"
56 #define NETVSC_ARG_TXBREAK "tx_copybreak"
57 #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable"
59 /* The max number of retry when hot adding a VF device */
60 #define NETVSC_MAX_HOTADD_RETRY 10
62 struct hn_xstats_name_off {
63 char name[RTE_ETH_XSTATS_NAME_SIZE];
67 static const struct hn_xstats_name_off hn_stat_strings[] = {
68 { "good_packets", offsetof(struct hn_stats, packets) },
69 { "good_bytes", offsetof(struct hn_stats, bytes) },
70 { "errors", offsetof(struct hn_stats, errors) },
71 { "ring full", offsetof(struct hn_stats, ring_full) },
72 { "channel full", offsetof(struct hn_stats, channel_full) },
73 { "multicast_packets", offsetof(struct hn_stats, multicast) },
74 { "broadcast_packets", offsetof(struct hn_stats, broadcast) },
75 { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) },
76 { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) },
77 { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) },
78 { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) },
79 { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) },
80 { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) },
81 { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
82 { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) },
85 /* The default RSS key.
86 * This value is the same as MLX5 so that flows will be
87 * received on same path for both VF and synthetic NIC.
89 static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
90 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
91 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
92 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
93 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
94 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a,
97 static struct rte_eth_dev *
98 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
100 struct rte_eth_dev *eth_dev;
106 name = dev->device.name;
108 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
109 eth_dev = rte_eth_dev_allocate(name);
111 PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
115 if (private_data_size) {
116 eth_dev->data->dev_private =
117 rte_zmalloc_socket(name, private_data_size,
118 RTE_CACHE_LINE_SIZE, dev->device.numa_node);
119 if (!eth_dev->data->dev_private) {
120 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
121 rte_eth_dev_release_port(eth_dev);
126 eth_dev = rte_eth_dev_attach_secondary(name);
128 PMD_DRV_LOG(NOTICE, "can not attach secondary");
133 eth_dev->device = &dev->device;
135 /* interrupt is simulated */
136 rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_EXT);
137 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
138 eth_dev->intr_handle = dev->intr_handle;
144 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
146 /* free ether device */
147 rte_eth_dev_release_port(eth_dev);
149 eth_dev->device = NULL;
150 eth_dev->intr_handle = NULL;
153 static int hn_set_parameter(const char *key, const char *value, void *opaque)
155 struct hn_data *hv = opaque;
159 v = strtoul(value, &endp, 0);
160 if (*value == '\0' || *endp != '\0') {
161 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
165 if (!strcmp(key, NETVSC_ARG_LATENCY)) {
167 hv->latency = v * 1000;
168 PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency);
169 } else if (!strcmp(key, NETVSC_ARG_RXBREAK)) {
170 hv->rx_copybreak = v;
171 PMD_DRV_LOG(DEBUG, "rx copy break set to %u",
173 } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
174 hv->tx_copybreak = v;
175 PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
177 } else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) {
178 hv->rx_extmbuf_enable = v;
179 PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u",
180 hv->rx_extmbuf_enable);
186 /* Parse device arguments */
187 static int hn_parse_args(const struct rte_eth_dev *dev)
189 struct hn_data *hv = dev->data->dev_private;
190 struct rte_devargs *devargs = dev->device->devargs;
191 static const char * const valid_keys[] = {
195 NETVSC_ARG_RX_EXTMBUF_ENABLE,
198 struct rte_kvargs *kvlist;
204 PMD_INIT_LOG(DEBUG, "device args %s %s",
205 devargs->name, devargs->args);
207 kvlist = rte_kvargs_parse(devargs->args, valid_keys);
209 PMD_DRV_LOG(ERR, "invalid parameters");
213 ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
214 rte_kvargs_free(kvlist);
219 /* Update link status.
220 * Note: the DPDK definition of "wait_to_complete"
221 * means block this call until link is up.
222 * which is not worth supporting.
225 hn_dev_link_update(struct rte_eth_dev *dev,
226 int wait_to_complete __rte_unused)
228 struct hn_data *hv = dev->data->dev_private;
229 struct rte_eth_link link, old;
232 old = dev->data->dev_link;
234 error = hn_rndis_get_linkstatus(hv);
238 hn_rndis_get_linkspeed(hv);
240 link = (struct rte_eth_link) {
241 .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
242 .link_autoneg = RTE_ETH_LINK_SPEED_FIXED,
243 .link_speed = hv->link_speed / 10000,
246 if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
247 link.link_status = RTE_ETH_LINK_UP;
249 link.link_status = RTE_ETH_LINK_DOWN;
251 if (old.link_status == link.link_status)
254 PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
255 (link.link_status == RTE_ETH_LINK_UP) ? "up" : "down");
257 return rte_eth_linkstatus_set(dev, &link);
260 static int hn_dev_info_get(struct rte_eth_dev *dev,
261 struct rte_eth_dev_info *dev_info)
263 struct hn_data *hv = dev->data->dev_private;
266 dev_info->speed_capa = RTE_ETH_LINK_SPEED_10G;
267 dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
268 dev_info->max_rx_pktlen = HN_MAX_XFER_LEN;
269 dev_info->max_mac_addrs = 1;
271 dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
272 dev_info->flow_type_rss_offloads = hv->rss_offloads;
273 dev_info->reta_size = RTE_ETH_RSS_RETA_SIZE_128;
275 dev_info->max_rx_queues = hv->max_queues;
276 dev_info->max_tx_queues = hv->max_queues;
278 dev_info->tx_desc_lim.nb_min = 1;
279 dev_info->tx_desc_lim.nb_max = 4096;
281 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
284 /* fills in rx and tx offload capability */
285 rc = hn_rndis_get_offload(hv, dev_info);
289 /* merges the offload and queues of vf */
290 return hn_vf_info_get(hv, dev_info);
293 static int hn_rss_reta_update(struct rte_eth_dev *dev,
294 struct rte_eth_rss_reta_entry64 *reta_conf,
297 struct hn_data *hv = dev->data->dev_private;
301 PMD_INIT_FUNC_TRACE();
303 if (reta_size != NDIS_HASH_INDCNT) {
304 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
308 for (i = 0; i < NDIS_HASH_INDCNT; i++) {
309 uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
310 uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
311 uint64_t mask = (uint64_t)1 << shift;
313 if (reta_conf[idx].mask & mask)
314 hv->rss_ind[i] = reta_conf[idx].reta[shift];
317 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
320 "rss disable failed");
324 err = hn_rndis_conf_rss(hv, 0);
327 "reta reconfig failed");
331 return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
334 static int hn_rss_reta_query(struct rte_eth_dev *dev,
335 struct rte_eth_rss_reta_entry64 *reta_conf,
338 struct hn_data *hv = dev->data->dev_private;
341 PMD_INIT_FUNC_TRACE();
343 if (reta_size != NDIS_HASH_INDCNT) {
344 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
348 for (i = 0; i < NDIS_HASH_INDCNT; i++) {
349 uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
350 uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
351 uint64_t mask = (uint64_t)1 << shift;
353 if (reta_conf[idx].mask & mask)
354 reta_conf[idx].reta[shift] = hv->rss_ind[i];
359 static void hn_rss_hash_init(struct hn_data *hv,
360 const struct rte_eth_rss_conf *rss_conf)
362 /* Convert from DPDK RSS hash flags to NDIS hash flags */
363 hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
365 if (rss_conf->rss_hf & RTE_ETH_RSS_IPV4)
366 hv->rss_hash |= NDIS_HASH_IPV4;
367 if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
368 hv->rss_hash |= NDIS_HASH_TCP_IPV4;
369 if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6)
370 hv->rss_hash |= NDIS_HASH_IPV6;
371 if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_EX)
372 hv->rss_hash |= NDIS_HASH_IPV6_EX;
373 if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
374 hv->rss_hash |= NDIS_HASH_TCP_IPV6;
375 if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
376 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
378 memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
379 NDIS_HASH_KEYSIZE_TOEPLITZ);
382 static int hn_rss_hash_update(struct rte_eth_dev *dev,
383 struct rte_eth_rss_conf *rss_conf)
385 struct hn_data *hv = dev->data->dev_private;
388 PMD_INIT_FUNC_TRACE();
390 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
393 "rss disable failed");
397 hn_rss_hash_init(hv, rss_conf);
399 if (rss_conf->rss_hf != 0) {
400 err = hn_rndis_conf_rss(hv, 0);
403 "rss reconfig failed (RSS disabled)");
408 return hn_vf_rss_hash_update(dev, rss_conf);
411 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
412 struct rte_eth_rss_conf *rss_conf)
414 struct hn_data *hv = dev->data->dev_private;
416 PMD_INIT_FUNC_TRACE();
418 if (hv->ndis_ver < NDIS_VERSION_6_20) {
419 PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
423 rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
424 if (rss_conf->rss_key)
425 memcpy(rss_conf->rss_key, hv->rss_key,
426 NDIS_HASH_KEYSIZE_TOEPLITZ);
428 rss_conf->rss_hf = 0;
429 if (hv->rss_hash & NDIS_HASH_IPV4)
430 rss_conf->rss_hf |= RTE_ETH_RSS_IPV4;
432 if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
433 rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
435 if (hv->rss_hash & NDIS_HASH_IPV6)
436 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6;
438 if (hv->rss_hash & NDIS_HASH_IPV6_EX)
439 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_EX;
441 if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
442 rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
444 if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
445 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_TCP_EX;
451 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
453 struct hn_data *hv = dev->data->dev_private;
455 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
456 return hn_vf_promiscuous_enable(dev);
460 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
462 struct hn_data *hv = dev->data->dev_private;
465 filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
466 if (dev->data->all_multicast)
467 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
468 hn_rndis_set_rxfilter(hv, filter);
469 return hn_vf_promiscuous_disable(dev);
473 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
475 struct hn_data *hv = dev->data->dev_private;
477 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
478 NDIS_PACKET_TYPE_ALL_MULTICAST |
479 NDIS_PACKET_TYPE_BROADCAST);
480 return hn_vf_allmulticast_enable(dev);
484 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
486 struct hn_data *hv = dev->data->dev_private;
488 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
489 NDIS_PACKET_TYPE_BROADCAST);
490 return hn_vf_allmulticast_disable(dev);
494 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
495 struct rte_ether_addr *mc_addr_set,
498 /* No filtering on the synthetic path, but can do it on VF */
499 return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
502 /* Setup shared rx/tx queue data */
503 static int hn_subchan_configure(struct hn_data *hv,
506 struct vmbus_channel *primary = hn_primary_chan(hv);
508 unsigned int retry = 0;
511 "open %u subchannels", subchan);
513 /* Send create sub channels command */
514 err = hn_nvs_alloc_subchans(hv, &subchan);
518 while (subchan > 0) {
519 struct vmbus_channel *new_sc;
522 err = rte_vmbus_subchan_open(primary, &new_sc);
523 if (err == -ENOENT && ++retry < 1000) {
524 /* This can happen if not ready yet */
531 "open subchannel failed: %d", err);
535 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
538 chn_index = rte_vmbus_sub_channel_index(new_sc);
539 if (chn_index == 0 || chn_index > hv->max_queues) {
541 "Invalid subchannel offermsg channel %u",
546 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
547 hv->channels[chn_index] = new_sc;
554 static void netvsc_hotplug_retry(void *args)
557 struct hn_data *hv = args;
558 struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
559 struct rte_devargs *d = &hv->devargs;
565 struct rte_ether_addr eth_addr;
568 PMD_DRV_LOG(DEBUG, "%s: retry count %d",
569 __func__, hv->eal_hot_plug_retry);
571 if (hv->eal_hot_plug_retry++ > NETVSC_MAX_HOTADD_RETRY)
574 snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name);
577 PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, "
578 "retrying in 1 second", __func__, buf);
582 while ((dir = readdir(di))) {
583 /* Skip . and .. directories */
584 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
587 /* trying to get mac address if this is a network device*/
588 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
590 PMD_DRV_LOG(ERR, "Failed to create socket errno %d",
594 strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name));
595 ret = ioctl(s, SIOCGIFHWADDR, &req);
599 "Failed to send SIOCGIFHWADDR for device %s",
603 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
607 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
608 RTE_DIM(eth_addr.addr_bytes));
610 if (rte_is_same_ether_addr(ð_addr, dev->data->mac_addrs)) {
612 "Found matching MAC address, adding device %s network name %s",
613 d->name, dir->d_name);
614 ret = rte_eal_hotplug_add(d->bus->name, d->name,
618 "Failed to add PCI device %s",
623 /* When the code reaches here, we either have already added
624 * the device, or its MAC address did not match.
631 /* The device is still being initialized, retry after 1 second */
632 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv);
636 netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type,
639 struct hn_data *hv = arg;
640 struct rte_devargs *d = &hv->devargs;
643 PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s",
647 case RTE_DEV_EVENT_ADD:
648 /* if we already has a VF, don't check on hot add */
649 if (hv->vf_ctx.vf_state > vf_removed)
652 ret = rte_devargs_parse(d, device_name);
655 "devargs parsing failed ret=%d", ret);
659 if (!strcmp(d->bus->name, "pci")) {
660 /* Start the process of figuring out if this
661 * PCI device is a VF device
663 hv->eal_hot_plug_retry = 0;
664 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hv);
667 /* We will switch to VF on RDNIS configure message
677 static int hn_dev_configure(struct rte_eth_dev *dev)
679 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
680 struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
681 const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
682 const struct rte_eth_txmode *txmode = &dev_conf->txmode;
683 struct hn_data *hv = dev->data->dev_private;
684 uint64_t unsupported;
687 PMD_INIT_FUNC_TRACE();
689 if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)
690 dev_conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
692 unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
695 "unsupported TX offload: %#" PRIx64,
700 unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
703 "unsupported RX offload: %#" PRIx64,
708 hv->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
710 err = hn_rndis_conf_offload(hv, txmode->offloads,
714 "offload configure failed");
718 hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
719 dev->data->nb_tx_queues);
721 for (i = 0; i < NDIS_HASH_INDCNT; i++)
722 hv->rss_ind[i] = i % dev->data->nb_rx_queues;
724 hn_rss_hash_init(hv, rss_conf);
726 subchan = hv->num_queues - 1;
728 err = hn_subchan_configure(hv, subchan);
731 "subchannel configuration failed");
735 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
738 "rss disable failed");
742 if (rss_conf->rss_hf != 0) {
743 err = hn_rndis_conf_rss(hv, 0);
746 "initial RSS config failed");
752 return hn_vf_configure_locked(dev, dev_conf);
755 static int hn_dev_stats_get(struct rte_eth_dev *dev,
756 struct rte_eth_stats *stats)
760 hn_vf_stats_get(dev, stats);
762 for (i = 0; i < dev->data->nb_tx_queues; i++) {
763 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
768 stats->opackets += txq->stats.packets;
769 stats->obytes += txq->stats.bytes;
770 stats->oerrors += txq->stats.errors;
772 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
773 stats->q_opackets[i] = txq->stats.packets;
774 stats->q_obytes[i] = txq->stats.bytes;
778 for (i = 0; i < dev->data->nb_rx_queues; i++) {
779 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
784 stats->ipackets += rxq->stats.packets;
785 stats->ibytes += rxq->stats.bytes;
786 stats->ierrors += rxq->stats.errors;
787 stats->imissed += rxq->stats.ring_full;
789 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
790 stats->q_ipackets[i] = rxq->stats.packets;
791 stats->q_ibytes[i] = rxq->stats.bytes;
795 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
800 hn_dev_stats_reset(struct rte_eth_dev *dev)
804 PMD_INIT_FUNC_TRACE();
806 for (i = 0; i < dev->data->nb_tx_queues; i++) {
807 struct hn_tx_queue *txq = dev->data->tx_queues[i];
811 memset(&txq->stats, 0, sizeof(struct hn_stats));
814 for (i = 0; i < dev->data->nb_rx_queues; i++) {
815 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
820 memset(&rxq->stats, 0, sizeof(struct hn_stats));
827 hn_dev_xstats_reset(struct rte_eth_dev *dev)
831 ret = hn_dev_stats_reset(dev);
835 return hn_vf_xstats_reset(dev);
839 hn_dev_xstats_count(struct rte_eth_dev *dev)
843 count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
844 count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
846 ret = hn_vf_xstats_get_names(dev, NULL, 0);
854 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
855 struct rte_eth_xstat_name *xstats_names,
858 unsigned int i, t, count = 0;
862 return hn_dev_xstats_count(dev);
864 /* Note: limit checked in rte_eth_xstats_names() */
865 for (i = 0; i < dev->data->nb_tx_queues; i++) {
866 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
874 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
875 snprintf(xstats_names[count++].name,
876 RTE_ETH_XSTATS_NAME_SIZE,
877 "tx_q%u_%s", i, hn_stat_strings[t].name);
880 for (i = 0; i < dev->data->nb_rx_queues; i++) {
881 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
889 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
890 snprintf(xstats_names[count++].name,
891 RTE_ETH_XSTATS_NAME_SIZE,
893 hn_stat_strings[t].name);
896 ret = hn_vf_xstats_get_names(dev, xstats_names + count,
905 hn_dev_xstats_get(struct rte_eth_dev *dev,
906 struct rte_eth_xstat *xstats,
909 unsigned int i, t, count = 0;
910 const unsigned int nstats = hn_dev_xstats_count(dev);
914 PMD_INIT_FUNC_TRACE();
919 for (i = 0; i < dev->data->nb_tx_queues; i++) {
920 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
925 stats = (const char *)&txq->stats;
926 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
927 xstats[count].id = count;
928 xstats[count].value = *(const uint64_t *)
929 (stats + hn_stat_strings[t].offset);
933 for (i = 0; i < dev->data->nb_rx_queues; i++) {
934 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
939 stats = (const char *)&rxq->stats;
940 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
941 xstats[count].id = count;
942 xstats[count].value = *(const uint64_t *)
943 (stats + hn_stat_strings[t].offset);
947 ret = hn_vf_xstats_get(dev, xstats, count, n);
955 hn_dev_start(struct rte_eth_dev *dev)
957 struct hn_data *hv = dev->data->dev_private;
960 PMD_INIT_FUNC_TRACE();
962 /* Register to monitor hot plug events */
963 error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback,
966 PMD_DRV_LOG(ERR, "failed to register device event callback");
970 error = hn_rndis_set_rxfilter(hv,
971 NDIS_PACKET_TYPE_BROADCAST |
972 NDIS_PACKET_TYPE_ALL_MULTICAST |
973 NDIS_PACKET_TYPE_DIRECTED);
977 error = hn_vf_start(dev);
979 hn_rndis_set_rxfilter(hv, 0);
981 /* Initialize Link state */
983 hn_dev_link_update(dev, 0);
989 hn_dev_stop(struct rte_eth_dev *dev)
991 struct hn_data *hv = dev->data->dev_private;
993 PMD_INIT_FUNC_TRACE();
994 dev->data->dev_started = 0;
996 rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv);
997 hn_rndis_set_rxfilter(hv, 0);
998 return hn_vf_stop(dev);
1002 hn_dev_close(struct rte_eth_dev *dev)
1005 struct hn_data *hv = dev->data->dev_private;
1007 PMD_INIT_FUNC_TRACE();
1008 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1011 rte_eal_alarm_cancel(netvsc_hotplug_retry, &hv->devargs);
1013 ret = hn_vf_close(dev);
1014 hn_dev_free_queues(dev);
1019 static const struct eth_dev_ops hn_eth_dev_ops = {
1020 .dev_configure = hn_dev_configure,
1021 .dev_start = hn_dev_start,
1022 .dev_stop = hn_dev_stop,
1023 .dev_close = hn_dev_close,
1024 .dev_infos_get = hn_dev_info_get,
1025 .txq_info_get = hn_dev_tx_queue_info,
1026 .rxq_info_get = hn_dev_rx_queue_info,
1027 .dev_supported_ptypes_get = hn_vf_supported_ptypes,
1028 .promiscuous_enable = hn_dev_promiscuous_enable,
1029 .promiscuous_disable = hn_dev_promiscuous_disable,
1030 .allmulticast_enable = hn_dev_allmulticast_enable,
1031 .allmulticast_disable = hn_dev_allmulticast_disable,
1032 .set_mc_addr_list = hn_dev_mc_addr_list,
1033 .reta_update = hn_rss_reta_update,
1034 .reta_query = hn_rss_reta_query,
1035 .rss_hash_update = hn_rss_hash_update,
1036 .rss_hash_conf_get = hn_rss_hash_conf_get,
1037 .tx_queue_setup = hn_dev_tx_queue_setup,
1038 .tx_queue_release = hn_dev_tx_queue_release,
1039 .tx_done_cleanup = hn_dev_tx_done_cleanup,
1040 .rx_queue_setup = hn_dev_rx_queue_setup,
1041 .rx_queue_release = hn_dev_rx_queue_release,
1042 .link_update = hn_dev_link_update,
1043 .stats_get = hn_dev_stats_get,
1044 .stats_reset = hn_dev_stats_reset,
1045 .xstats_get = hn_dev_xstats_get,
1046 .xstats_get_names = hn_dev_xstats_get_names,
1047 .xstats_reset = hn_dev_xstats_reset,
1051 * Setup connection between PMD and kernel.
1054 hn_attach(struct hn_data *hv, unsigned int mtu)
1059 error = hn_nvs_attach(hv, mtu);
1064 error = hn_rndis_attach(hv);
1070 * Under certain conditions on certain versions of Hyper-V,
1071 * the RNDIS rxfilter is _not_ zero on the hypervisor side
1072 * after the successful RNDIS initialization.
1074 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
1083 hn_detach(struct hn_data *hv)
1086 hn_rndis_detach(hv);
1090 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
1092 struct hn_data *hv = eth_dev->data->dev_private;
1093 struct rte_device *device = eth_dev->device;
1094 struct rte_vmbus_device *vmbus;
1095 unsigned int rxr_cnt;
1098 PMD_INIT_FUNC_TRACE();
1100 vmbus = container_of(device, struct rte_vmbus_device, device);
1101 eth_dev->dev_ops = &hn_eth_dev_ops;
1102 eth_dev->rx_queue_count = hn_dev_rx_queue_count;
1103 eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
1104 eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
1105 eth_dev->tx_pkt_burst = &hn_xmit_pkts;
1106 eth_dev->rx_pkt_burst = &hn_recv_pkts;
1109 * for secondary processes, we don't initialize any further as primary
1110 * has already done this work.
1112 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1115 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1117 /* Since Hyper-V only supports one MAC address */
1118 eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
1119 sizeof(struct rte_ether_addr), 0);
1120 if (eth_dev->data->mac_addrs == NULL) {
1122 "Failed to allocate memory store MAC addresses");
1127 hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
1128 hv->chim_res = &vmbus->resource[HV_SEND_BUF_MAP];
1129 hv->port_id = eth_dev->data->port_id;
1130 hv->latency = HN_CHAN_LATENCY_NS;
1131 hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
1132 hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
1133 hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
1136 rte_rwlock_init(&hv->vf_lock);
1137 hv->vf_ctx.vf_vsc_switched = false;
1138 hv->vf_ctx.vf_vsp_reported = false;
1139 hv->vf_ctx.vf_attached = false;
1140 hv->vf_ctx.vf_state = vf_unknown;
1142 err = hn_parse_args(eth_dev);
1146 strlcpy(hv->owner.name, eth_dev->device->name,
1147 RTE_ETH_MAX_OWNER_NAME_LEN);
1148 err = rte_eth_dev_owner_new(&hv->owner.id);
1150 PMD_INIT_LOG(ERR, "Can not get owner id");
1154 /* Initialize primary channel input for control operations */
1155 err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
1159 rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1161 hv->primary = hn_rx_queue_alloc(hv, 0,
1162 eth_dev->device->numa_node);
1167 err = hn_attach(hv, RTE_ETHER_MTU);
1171 err = hn_chim_init(eth_dev);
1175 err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
1179 /* Multi queue requires later versions of windows server */
1180 if (hv->nvs_ver < NVS_VERSION_5)
1183 max_chan = rte_vmbus_max_channels(vmbus);
1184 PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1188 if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1191 hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1193 /* If VF was reported but not added, do it now */
1194 if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) {
1195 PMD_INIT_LOG(DEBUG, "Adding VF device");
1197 err = hn_vf_add(eth_dev, hv);
1203 PMD_INIT_LOG(NOTICE, "device init failed");
1205 hn_chim_uninit(eth_dev);
1211 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1213 struct hn_data *hv = eth_dev->data->dev_private;
1216 PMD_INIT_FUNC_TRACE();
1218 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1221 ret_stop = hn_dev_stop(eth_dev);
1222 hn_dev_close(eth_dev);
1225 hn_chim_uninit(eth_dev);
1226 rte_vmbus_chan_close(hv->primary->chan);
1227 rte_free(hv->primary);
1228 ret = rte_eth_dev_owner_delete(hv->owner.id);
1235 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1236 struct rte_vmbus_device *dev)
1238 struct rte_eth_dev *eth_dev;
1241 PMD_INIT_FUNC_TRACE();
1243 ret = rte_dev_event_monitor_start();
1245 PMD_DRV_LOG(ERR, "Failed to start device event monitoring");
1249 eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1253 ret = eth_hn_dev_init(eth_dev);
1255 eth_dev_vmbus_release(eth_dev);
1256 rte_dev_event_monitor_stop();
1258 rte_eth_dev_probing_finish(eth_dev);
1264 static int eth_hn_remove(struct rte_vmbus_device *dev)
1266 struct rte_eth_dev *eth_dev;
1269 PMD_INIT_FUNC_TRACE();
1271 eth_dev = rte_eth_dev_allocated(dev->device.name);
1273 return 0; /* port already released */
1275 ret = eth_hn_dev_uninit(eth_dev);
1279 eth_dev_vmbus_release(eth_dev);
1280 rte_dev_event_monitor_stop();
1284 /* Network device GUID */
1285 static const rte_uuid_t hn_net_ids[] = {
1286 /* f8615163-df3e-46c5-913f-f2d2f965ed0e */
1287 RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1291 static struct rte_vmbus_driver rte_netvsc_pmd = {
1292 .id_table = hn_net_ids,
1293 .probe = eth_hn_probe,
1294 .remove = eth_hn_remove,
1297 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1298 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1299 RTE_LOG_REGISTER_SUFFIX(hn_logtype_init, init, NOTICE);
1300 RTE_LOG_REGISTER_SUFFIX(hn_logtype_driver, driver, NOTICE);
1301 RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
1302 NETVSC_ARG_LATENCY "=<uint32> "
1303 NETVSC_ARG_RXBREAK "=<uint32> "
1304 NETVSC_ARG_TXBREAK "=<uint32> "
1305 NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");