ethdev: clear data when allocating device
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index 0d9d9c1..59038ad 100644 (file)
@@ -212,6 +212,7 @@ rte_eth_dev_allocate(const char *name)
 
        eth_dev = &rte_eth_devices[port_id];
        eth_dev->data = &rte_eth_dev_data[port_id];
+       memset(eth_dev->data, 0, sizeof(*eth_dev->data));
        snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
        eth_dev->data->port_id = port_id;
        eth_dev->attached = DEV_ATTACHED;
@@ -259,7 +260,6 @@ rte_eth_dev_pci_probe(struct rte_pci_driver *pci_drv,
        }
        eth_dev->pci_dev = pci_dev;
        eth_dev->driver = eth_drv;
-       eth_dev->data->rx_mbuf_alloc_failed = 0;
 
        /* init user callbacks */
        TAILQ_INIT(&(eth_dev->link_intr_cbs));
@@ -376,6 +376,9 @@ rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id)
                return -EINVAL;
        }
 
+       if (!nb_ports)
+               return -ENODEV;
+
        *port_id = RTE_MAX_ETHPORTS;
 
        for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
@@ -1343,8 +1346,10 @@ get_xstats_count(uint8_t port_id)
        } else
                count = 0;
        count += RTE_NB_STATS;
-       count += dev->data->nb_rx_queues * RTE_NB_RXQ_STATS;
-       count += dev->data->nb_tx_queues * RTE_NB_TXQ_STATS;
+       count += RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
+                RTE_NB_RXQ_STATS;
+       count += RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
+                RTE_NB_TXQ_STATS;
        return count;
 }
 
@@ -1358,6 +1363,7 @@ rte_eth_xstats_get_names(uint8_t port_id,
        int cnt_expected_entries;
        int cnt_driver_entries;
        uint32_t idx, id_queue;
+       uint16_t num_q;
 
        cnt_expected_entries = get_xstats_count(port_id);
        if (xstats_names == NULL || cnt_expected_entries < 0 ||
@@ -1374,7 +1380,8 @@ rte_eth_xstats_get_names(uint8_t port_id,
                        "%s", rte_stats_strings[idx].name);
                cnt_used_entries++;
        }
-       for (id_queue = 0; id_queue < dev->data->nb_rx_queues; id_queue++) {
+       num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       for (id_queue = 0; id_queue < num_q; id_queue++) {
                for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
                        snprintf(xstats_names[cnt_used_entries].name,
                                sizeof(xstats_names[0].name),
@@ -1384,7 +1391,8 @@ rte_eth_xstats_get_names(uint8_t port_id,
                }
 
        }
-       for (id_queue = 0; id_queue < dev->data->nb_tx_queues; id_queue++) {
+       num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       for (id_queue = 0; id_queue < num_q; id_queue++) {
                for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
                        snprintf(xstats_names[cnt_used_entries].name,
                                sizeof(xstats_names[0].name),
@@ -1420,14 +1428,18 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        unsigned count = 0, i, q;
        signed xcount = 0;
        uint64_t val, *stats_ptr;
+       uint16_t nb_rxqs, nb_txqs;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
 
+       nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+
        /* Return generic statistics */
-       count = RTE_NB_STATS + (dev->data->nb_rx_queues * RTE_NB_RXQ_STATS) +
-               (dev->data->nb_tx_queues * RTE_NB_TXQ_STATS);
+       count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
+               (nb_txqs * RTE_NB_TXQ_STATS);
 
        /* implemented by the driver */
        if (dev->dev_ops->xstats_get != NULL) {
@@ -1458,7 +1470,7 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        }
 
        /* per-rxq stats */
-       for (q = 0; q < dev->data->nb_rx_queues; q++) {
+       for (q = 0; q < nb_rxqs; q++) {
                for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
                        stats_ptr = RTE_PTR_ADD(&eth_stats,
                                        rte_rxq_stats_strings[i].offset +
@@ -1469,7 +1481,7 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        }
 
        /* per-txq stats */
-       for (q = 0; q < dev->data->nb_tx_queues; q++) {
+       for (q = 0; q < nb_txqs; q++) {
                for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
                        stats_ptr = RTE_PTR_ADD(&eth_stats,
                                        rte_txq_stats_strings[i].offset +
@@ -2449,14 +2461,15 @@ rte_eth_dev_callback_register(uint8_t port_id,
        }
 
        /* create a new callback. */
-       if (user_cb == NULL)
+       if (user_cb == NULL) {
                user_cb = rte_zmalloc("INTR_USER_CALLBACK",
                                        sizeof(struct rte_eth_dev_callback), 0);
-       if (user_cb != NULL) {
-               user_cb->cb_fn = cb_fn;
-               user_cb->cb_arg = cb_arg;
-               user_cb->event = event;
-               TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
+               if (user_cb != NULL) {
+                       user_cb->cb_fn = cb_fn;
+                       user_cb->cb_arg = cb_arg;
+                       user_cb->event = event;
+                       TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
+               }
        }
 
        rte_spinlock_unlock(&rte_eth_dev_cb_lock);