ethdev: allow event registration for all ports
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index a2bf641..533bb5e 100644 (file)
@@ -176,7 +176,6 @@ eth_dev_get(uint16_t port_id)
 
        eth_dev->data = &rte_eth_dev_data[port_id];
        eth_dev->state = RTE_ETH_DEV_ATTACHED;
-       TAILQ_INIT(&(eth_dev->link_intr_cbs));
 
        eth_dev_last_created_port = port_id;
 
@@ -1613,6 +1612,45 @@ rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
        return -EINVAL;
 }
 
+/* retrieve basic stats names */
+static int
+rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,
+       struct rte_eth_xstat_name *xstats_names)
+{
+       int cnt_used_entries = 0;
+       uint32_t idx, id_queue;
+       uint16_t num_q;
+
+       for (idx = 0; idx < RTE_NB_STATS; idx++) {
+               snprintf(xstats_names[cnt_used_entries].name,
+                       sizeof(xstats_names[0].name),
+                       "%s", rte_stats_strings[idx].name);
+               cnt_used_entries++;
+       }
+       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),
+                               "rx_q%u%s",
+                               id_queue, rte_rxq_stats_strings[idx].name);
+                       cnt_used_entries++;
+               }
+
+       }
+       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),
+                               "tx_q%u%s",
+                               id_queue, rte_txq_stats_strings[idx].name);
+                       cnt_used_entries++;
+               }
+       }
+       return cnt_used_entries;
+}
+
 /* retrieve ethdev extended statistics names */
 int
 rte_eth_xstats_get_names_by_id(uint16_t port_id,
@@ -1621,7 +1659,9 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
 {
        struct rte_eth_xstat_name *xstats_names_copy;
        unsigned int no_basic_stat_requested = 1;
+       unsigned int no_ext_stat_requested = 1;
        unsigned int expected_entries;
+       unsigned int basic_count;
        struct rte_eth_dev *dev;
        unsigned int i;
        int ret;
@@ -1629,6 +1669,7 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
        dev = &rte_eth_devices[port_id];
 
+       basic_count = get_xstats_basic_count(dev);
        ret = get_xstats_count(port_id);
        if (ret < 0)
                return ret;
@@ -1646,7 +1687,6 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
                return -EINVAL;
 
        if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
-               unsigned int basic_count = get_xstats_basic_count(dev);
                uint64_t ids_copy[size];
 
                for (i = 0; i < size; i++) {
@@ -1685,8 +1725,22 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
                return -ENOMEM;
        }
 
+       if (ids) {
+               for (i = 0; i < size; i++) {
+                       if (ids[i] > basic_count) {
+                               no_ext_stat_requested = 0;
+                               break;
+                       }
+               }
+       }
+
        /* Fill xstats_names_copy structure */
-       rte_eth_xstats_get_names(port_id, xstats_names_copy, expected_entries);
+       if (ids && no_ext_stat_requested) {
+               rte_eth_basic_stats_get_names(dev, xstats_names_copy);
+       } else {
+               rte_eth_xstats_get_names(port_id, xstats_names_copy,
+                       expected_entries);
+       }
 
        /* Filter stats */
        for (i = 0; i < size; i++) {
@@ -1711,8 +1765,6 @@ rte_eth_xstats_get_names(uint16_t port_id,
        int cnt_used_entries;
        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 ||
@@ -1721,35 +1773,9 @@ rte_eth_xstats_get_names(uint16_t port_id,
 
        /* port_id checked in get_xstats_count() */
        dev = &rte_eth_devices[port_id];
-       cnt_used_entries = 0;
-
-       for (idx = 0; idx < RTE_NB_STATS; idx++) {
-               snprintf(xstats_names[cnt_used_entries].name,
-                       sizeof(xstats_names[0].name),
-                       "%s", rte_stats_strings[idx].name);
-               cnt_used_entries++;
-       }
-       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),
-                               "rx_q%u%s",
-                               id_queue, rte_rxq_stats_strings[idx].name);
-                       cnt_used_entries++;
-               }
 
-       }
-       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),
-                               "tx_q%u%s",
-                               id_queue, rte_txq_stats_strings[idx].name);
-                       cnt_used_entries++;
-               }
-       }
+       cnt_used_entries = rte_eth_basic_stats_get_names(
+               dev, xstats_names);
 
        if (dev->dev_ops->xstats_get_names != NULL) {
                /* If there are any driver-specific xstats, append them
@@ -1767,13 +1793,63 @@ rte_eth_xstats_get_names(uint16_t port_id,
        return cnt_used_entries;
 }
 
+
+static int
+rte_eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
+{
+       struct rte_eth_dev *dev;
+       struct rte_eth_stats eth_stats;
+       unsigned int count = 0, i, q;
+       uint64_t val, *stats_ptr;
+       uint16_t nb_rxqs, nb_txqs;
+
+       rte_eth_stats_get(port_id, &eth_stats);
+       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);
+
+       /* global stats */
+       for (i = 0; i < RTE_NB_STATS; i++) {
+               stats_ptr = RTE_PTR_ADD(&eth_stats,
+                                       rte_stats_strings[i].offset);
+               val = *stats_ptr;
+               xstats[count++].value = val;
+       }
+
+       /* per-rxq stats */
+       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 +
+                                       q * sizeof(uint64_t));
+                       val = *stats_ptr;
+                       xstats[count++].value = val;
+               }
+       }
+
+       /* per-txq stats */
+       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 +
+                                       q * sizeof(uint64_t));
+                       val = *stats_ptr;
+                       xstats[count++].value = val;
+               }
+       }
+       return count;
+}
+
 /* retrieve ethdev extended statistics */
 int
 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
                         uint64_t *values, unsigned int size)
 {
        unsigned int no_basic_stat_requested = 1;
+       unsigned int no_ext_stat_requested = 1;
        unsigned int num_xstats_filled;
+       unsigned int basic_count;
        uint16_t expected_entries;
        struct rte_eth_dev *dev;
        unsigned int i;
@@ -1783,6 +1859,7 @@ rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
        expected_entries = get_xstats_count(port_id);
        struct rte_eth_xstat xstats[expected_entries];
        dev = &rte_eth_devices[port_id];
+       basic_count = get_xstats_basic_count(dev);
 
        /* Return max number of stats if no ids given */
        if (!ids) {
@@ -1817,8 +1894,21 @@ rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
                                        values, size);
        }
 
+       if (ids) {
+               for (i = 0; i < size; i++) {
+                       if (ids[i] > basic_count) {
+                               no_ext_stat_requested = 0;
+                               break;
+                       }
+               }
+       }
+
        /* Fill the xstats structure */
-       ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
+       if (ids && no_ext_stat_requested)
+               ret = rte_eth_basic_stats_get(port_id, xstats);
+       else
+               ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
+
        if (ret < 0)
                return ret;
        num_xstats_filled = (unsigned int)ret;
@@ -1845,11 +1935,9 @@ int
 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
        unsigned int n)
 {
-       struct rte_eth_stats eth_stats;
        struct rte_eth_dev *dev;
-       unsigned int count = 0, i, q;
+       unsigned int count = 0, i;
        signed int xcount = 0;
-       uint64_t val, *stats_ptr;
        uint16_t nb_rxqs, nb_txqs;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
@@ -1880,38 +1968,7 @@ rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
                return count + xcount;
 
        /* now fill the xstats structure */
-       count = 0;
-       rte_eth_stats_get(port_id, &eth_stats);
-
-       /* global stats */
-       for (i = 0; i < RTE_NB_STATS; i++) {
-               stats_ptr = RTE_PTR_ADD(&eth_stats,
-                                       rte_stats_strings[i].offset);
-               val = *stats_ptr;
-               xstats[count++].value = val;
-       }
-
-       /* per-rxq stats */
-       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 +
-                                       q * sizeof(uint64_t));
-                       val = *stats_ptr;
-                       xstats[count++].value = val;
-               }
-       }
-
-       /* per-txq stats */
-       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 +
-                                       q * sizeof(uint64_t));
-                       val = *stats_ptr;
-                       xstats[count++].value = val;
-               }
-       }
+       count = rte_eth_basic_stats_get(port_id, xstats);
 
        for (i = 0; i < count; i++)
                xstats[i].id = i;
@@ -2792,6 +2849,14 @@ rte_eth_mirror_rule_reset(uint16_t port_id, uint8_t rule_id)
        return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
 }
 
+RTE_INIT(eth_dev_init_cb_lists)
+{
+       int i;
+
+       for (i = 0; i < RTE_MAX_ETHPORTS; i++)
+               TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
+}
+
 int
 rte_eth_dev_callback_register(uint16_t port_id,
                        enum rte_eth_event_type event,
@@ -2799,37 +2864,59 @@ rte_eth_dev_callback_register(uint16_t port_id,
 {
        struct rte_eth_dev *dev;
        struct rte_eth_dev_callback *user_cb;
+       uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
+       uint16_t last_port;
 
        if (!cb_fn)
                return -EINVAL;
 
-       RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
+       if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
+               RTE_LOG(ERR, EAL, "Invalid port_id=%d\n", port_id);
+               return -EINVAL;
+       }
+
+       if (port_id == RTE_ETH_ALL) {
+               next_port = 0;
+               last_port = RTE_MAX_ETHPORTS - 1;
+       } else {
+               next_port = last_port = port_id;
+       }
 
-       dev = &rte_eth_devices[port_id];
        rte_spinlock_lock(&rte_eth_dev_cb_lock);
 
-       TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
-               if (user_cb->cb_fn == cb_fn &&
-                       user_cb->cb_arg == cb_arg &&
-                       user_cb->event == event) {
-                       break;
+       do {
+               dev = &rte_eth_devices[next_port];
+
+               TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
+                       if (user_cb->cb_fn == cb_fn &&
+                               user_cb->cb_arg == cb_arg &&
+                               user_cb->event == event) {
+                               break;
+                       }
                }
-       }
 
-       /* create a new callback. */
-       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);
+               /* create a new callback. */
+               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);
+                       } else {
+                               rte_spinlock_unlock(&rte_eth_dev_cb_lock);
+                               rte_eth_dev_callback_unregister(port_id, event,
+                                                               cb_fn, cb_arg);
+                               return -ENOMEM;
+                       }
+
                }
-       }
+       } while (++next_port <= last_port);
 
        rte_spinlock_unlock(&rte_eth_dev_cb_lock);
-       return (user_cb == NULL) ? -ENOMEM : 0;
+       return 0;
 }
 
 int
@@ -2840,36 +2927,50 @@ rte_eth_dev_callback_unregister(uint16_t port_id,
        int ret;
        struct rte_eth_dev *dev;
        struct rte_eth_dev_callback *cb, *next;
+       uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
+       uint16_t last_port;
 
        if (!cb_fn)
                return -EINVAL;
 
-       RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
+       if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
+               RTE_LOG(ERR, EAL, "Invalid port_id=%d\n", port_id);
+               return -EINVAL;
+       }
+
+       if (port_id == RTE_ETH_ALL) {
+               next_port = 0;
+               last_port = RTE_MAX_ETHPORTS - 1;
+       } else {
+               next_port = last_port = port_id;
+       }
 
-       dev = &rte_eth_devices[port_id];
        rte_spinlock_lock(&rte_eth_dev_cb_lock);
 
-       ret = 0;
-       for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
+       do {
+               dev = &rte_eth_devices[next_port];
+               ret = 0;
+               for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
+                    cb = next) {
 
-               next = TAILQ_NEXT(cb, next);
+                       next = TAILQ_NEXT(cb, next);
 
-               if (cb->cb_fn != cb_fn || cb->event != event ||
-                               (cb->cb_arg != (void *)-1 &&
-                               cb->cb_arg != cb_arg))
-                       continue;
+                       if (cb->cb_fn != cb_fn || cb->event != event ||
+                           (cb->cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
+                               continue;
 
-               /*
-                * if this callback is not executing right now,
-                * then remove it.
-                */
-               if (cb->active == 0) {
-                       TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
-                       rte_free(cb);
-               } else {
-                       ret = -EAGAIN;
+                       /*
+                        * if this callback is not executing right now,
+                        * then remove it.
+                        */
+                       if (cb->active == 0) {
+                               TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
+                               rte_free(cb);
+                       } else {
+                               ret = -EAGAIN;
+                       }
                }
-       }
+       } while (++next_port <= last_port);
 
        rte_spinlock_unlock(&rte_eth_dev_cb_lock);
        return ret;
@@ -2877,7 +2978,7 @@ rte_eth_dev_callback_unregister(uint16_t port_id,
 
 int
 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
-       enum rte_eth_event_type event, void *cb_arg, void *ret_param)
+       enum rte_eth_event_type event, void *ret_param)
 {
        struct rte_eth_dev_callback *cb_lst;
        struct rte_eth_dev_callback dev_cb;
@@ -2889,8 +2990,6 @@ _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
                        continue;
                dev_cb = *cb_lst;
                cb_lst->active = 1;
-               if (cb_arg != NULL)
-                       dev_cb.cb_arg = cb_arg;
                if (ret_param != NULL)
                        dev_cb.ret_param = ret_param;