ethdev: remove name from extended statistic fetch
[dpdk.git] / drivers / net / virtio / virtio_ethdev.c
index a0ef0a1..a833740 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
@@ -59,7 +59,6 @@
 #include "virtqueue.h"
 #include "virtio_rxtx.h"
 
-
 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
 static int  virtio_dev_configure(struct rte_eth_dev *dev);
@@ -80,7 +79,10 @@ static void virtio_get_hwaddr(struct virtio_hw *hw);
 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
                                 struct rte_eth_stats *stats);
 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
-                                struct rte_eth_xstats *xstats, unsigned n);
+                                struct rte_eth_xstat *xstats, unsigned n);
+static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
+                                      struct rte_eth_xstat_name *xstats_names,
+                                      unsigned limit);
 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
@@ -261,12 +263,18 @@ virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
 }
 
 void
-virtio_dev_queue_release(struct virtqueue *vq) {
+virtio_dev_queue_release(struct virtqueue *vq)
+{
        struct virtio_hw *hw;
 
        if (vq) {
                hw = vq->hw;
-               hw->vtpci_ops->del_queue(hw, vq);
+               if (vq->configured)
+                       hw->vtpci_ops->del_queue(hw, vq);
+
+               rte_memzone_free(vq->mz);
+               if (vq->virtio_net_hdr_mz)
+                       rte_memzone_free(vq->virtio_net_hdr_mz);
 
                rte_free(vq->sw_ring);
                rte_free(vq);
@@ -286,6 +294,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
        unsigned int vq_size, size;
        struct virtio_hw *hw = dev->data->dev_private;
        struct virtqueue *vq = NULL;
+       const char *queue_names[] = {"rvq", "txq", "cvq"};
 
        PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
 
@@ -305,34 +314,29 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                return -EINVAL;
        }
 
-       if (queue_type == VTNET_RQ) {
-               snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
-                       dev->data->port_id, queue_idx);
-               vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
-                       vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
-               vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
-                       (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
-                       sizeof(vq->sw_ring[0]), RTE_CACHE_LINE_SIZE, socket_id);
-       } else if (queue_type == VTNET_TQ) {
-               snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
-                       dev->data->port_id, queue_idx);
-               vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
-                       vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
-       } else if (queue_type == VTNET_CQ) {
-               snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
-                       dev->data->port_id);
-               vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
-                       vq_size * sizeof(struct vq_desc_extra),
-                       RTE_CACHE_LINE_SIZE);
-       }
+       snprintf(vq_name, sizeof(vq_name), "port%d_%s%d",
+                dev->data->port_id, queue_names[queue_type], queue_idx);
+       vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
+                        vq_size * sizeof(struct vq_desc_extra),
+                        RTE_CACHE_LINE_SIZE);
        if (vq == NULL) {
                PMD_INIT_LOG(ERR, "Can not allocate virtqueue");
                return -ENOMEM;
        }
-       if (queue_type == VTNET_RQ && vq->sw_ring == NULL) {
-               PMD_INIT_LOG(ERR, "Can not allocate RX soft ring");
-               rte_free(vq);
-               return -ENOMEM;
+
+       if (queue_type == VTNET_RQ) {
+               size_t sz_sw;
+
+               sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
+                       sizeof(vq->sw_ring[0]);
+               vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring", sz_sw,
+                                                RTE_CACHE_LINE_SIZE,
+                                                socket_id);
+               if (!vq->sw_ring) {
+                       PMD_INIT_LOG(ERR, "Can not allocate RX soft ring");
+                       virtio_dev_queue_release(vq);
+                       return -ENOMEM;
+               }
        }
 
        vq->hw = hw;
@@ -358,7 +362,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                if (rte_errno == EEXIST)
                        mz = rte_memzone_lookup(vq_name);
                if (mz == NULL) {
-                       rte_free(vq);
+                       virtio_dev_queue_release(vq);
                        return -ENOMEM;
                }
        }
@@ -370,7 +374,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
         */
        if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
                PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
-               rte_free(vq);
+               virtio_dev_queue_release(vq);
                return -ENOMEM;
        }
 
@@ -402,7 +406,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                        if (rte_errno == EEXIST)
                                hdr_mz = rte_memzone_lookup(vq_name);
                        if (hdr_mz == NULL) {
-                               rte_free(vq);
+                               virtio_dev_queue_release(vq);
                                return -ENOMEM;
                        }
                }
@@ -436,7 +440,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                                vq->virtio_net_hdr_mz =
                                        rte_memzone_lookup(vq_name);
                        if (vq->virtio_net_hdr_mz == NULL) {
-                               rte_free(vq);
+                               virtio_dev_queue_release(vq);
                                return -ENOMEM;
                        }
                }
@@ -447,6 +451,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
 
        hw->vtpci_ops->setup_queue(hw, vq);
 
+       vq->configured = 1;
        *pvq = vq;
        return 0;
 }
@@ -491,7 +496,6 @@ static void
 virtio_dev_close(struct rte_eth_dev *dev)
 {
        struct virtio_hw *hw = dev->data->dev_private;
-       struct rte_pci_device *pci_dev = dev->pci_dev;
 
        PMD_INIT_LOG(DEBUG, "virtio_dev_close");
 
@@ -499,7 +503,7 @@ virtio_dev_close(struct rte_eth_dev *dev)
                virtio_dev_stop(dev);
 
        /* reset the NIC */
-       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
+       if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
                vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
        vtpci_reset(hw);
        virtio_dev_free_mbufs(dev);
@@ -614,6 +618,7 @@ static const struct eth_dev_ops virtio_eth_dev_ops = {
        .dev_infos_get           = virtio_dev_info_get,
        .stats_get               = virtio_dev_stats_get,
        .xstats_get              = virtio_dev_xstats_get,
+       .xstats_get_names        = virtio_dev_xstats_get_names,
        .stats_reset             = virtio_dev_stats_reset,
        .xstats_reset            = virtio_dev_stats_reset,
        .link_update             = virtio_dev_link_update,
@@ -707,8 +712,54 @@ virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
        stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
 }
 
+static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
+                                      struct rte_eth_xstat_name *xstats_names,
+                                      __rte_unused unsigned limit)
+{
+       unsigned i;
+       unsigned count = 0;
+       unsigned t;
+
+       unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_Q_XSTATS +
+               dev->data->nb_rx_queues * VIRTIO_NB_Q_XSTATS;
+
+       if (xstats_names == NULL) {
+               /* Note: limit checked in rte_eth_xstats_names() */
+
+               for (i = 0; i < dev->data->nb_rx_queues; i++) {
+                       struct virtqueue *rxvq = dev->data->rx_queues[i];
+                       if (rxvq == NULL)
+                               continue;
+                       for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
+                               snprintf(xstats_names[count].name,
+                                       sizeof(xstats_names[count].name),
+                                       "rx_q%u_%s", i,
+                                       rte_virtio_q_stat_strings[t].name);
+                               xstats_names[count].id = count;
+                               count++;
+                       }
+               }
+
+               for (i = 0; i < dev->data->nb_tx_queues; i++) {
+                       struct virtqueue *txvq = dev->data->tx_queues[i];
+                       if (txvq == NULL)
+                               continue;
+                       for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
+                               snprintf(xstats_names[count].name,
+                                       sizeof(xstats_names[count].name),
+                                       "tx_q%u_%s", i,
+                                       rte_virtio_q_stat_strings[t].name);
+                               xstats_names[count].id = count;
+                               count++;
+                       }
+               }
+               return count;
+       }
+       return nstats;
+}
+
 static int
-virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
+virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
                      unsigned n)
 {
        unsigned i;
@@ -729,9 +780,7 @@ virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
                unsigned t;
 
                for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
-                       snprintf(xstats[count].name, sizeof(xstats[count].name),
-                                "rx_q%u_%s", i,
-                                rte_virtio_q_stat_strings[t].name);
+                       xstats[count].id = count;
                        xstats[count].value = *(uint64_t *)(((char *)rxvq) +
                                rte_virtio_q_stat_strings[t].offset);
                        count++;
@@ -747,9 +796,7 @@ virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
                unsigned t;
 
                for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
-                       snprintf(xstats[count].name, sizeof(xstats[count].name),
-                                "tx_q%u_%s", i,
-                                rte_virtio_q_stat_strings[t].name);
+                       xstats[count].id = count;
                        xstats[count].value = *(uint64_t *)(((char *)txvq) +
                                rte_virtio_q_stat_strings[t].offset);
                        count++;
@@ -1034,9 +1081,10 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
        struct virtio_net_config *config;
        struct virtio_net_config local_config;
        struct rte_pci_device *pci_dev;
+       uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
        int ret;
 
-       RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
+       RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
 
        eth_dev->dev_ops = &virtio_eth_dev_ops;
        eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
@@ -1057,7 +1105,7 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 
        pci_dev = eth_dev->pci_dev;
 
-       ret = vtpci_init(pci_dev, hw);
+       ret = vtpci_init(pci_dev, hw, &dev_flags);
        if (ret)
                return ret;
 
@@ -1074,9 +1122,10 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 
        /* If host does not support status then disable LSC */
        if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
-               pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
+               dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
 
        rte_eth_copy_pci_info(eth_dev, pci_dev);
+       eth_dev->data->dev_flags = dev_flags;
 
        rx_func_get(eth_dev);
 
@@ -1155,7 +1204,7 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
                        pci_dev->id.device_id);
 
        /* Setup interrupt callback  */
-       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
+       if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
                rte_intr_callback_register(&pci_dev->intr_handle,
                                   virtio_interrupt_handler, eth_dev);
 
@@ -1190,7 +1239,7 @@ eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
        eth_dev->data->mac_addrs = NULL;
 
        /* reset interrupt callback  */
-       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
+       if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
                rte_intr_callback_unregister(&pci_dev->intr_handle,
                                                virtio_interrupt_handler,
                                                eth_dev);
@@ -1240,7 +1289,6 @@ virtio_dev_configure(struct rte_eth_dev *dev)
 {
        const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
        struct virtio_hw *hw = dev->data->dev_private;
-       struct rte_pci_device *pci_dev = dev->pci_dev;
 
        PMD_INIT_LOG(DEBUG, "configure");
 
@@ -1258,7 +1306,7 @@ virtio_dev_configure(struct rte_eth_dev *dev)
                return -ENOTSUP;
        }
 
-       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
+       if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
                if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
                        PMD_DRV_LOG(ERR, "failed to set config vector");
                        return -EBUSY;
@@ -1273,11 +1321,10 @@ virtio_dev_start(struct rte_eth_dev *dev)
 {
        uint16_t nb_queues, i;
        struct virtio_hw *hw = dev->data->dev_private;
-       struct rte_pci_device *pci_dev = dev->pci_dev;
 
        /* check if lsc interrupt feature is enabled */
        if (dev->data->dev_conf.intr_conf.lsc) {
-               if (!(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
+               if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
                        PMD_DRV_LOG(ERR, "link status not supported by host");
                        return -ENOTSUP;
                }