+int
+rte_eth_representor_id_get(const struct rte_eth_dev *ethdev,
+ enum rte_eth_representor_type type,
+ int controller, int pf, int representor_port,
+ uint16_t *repr_id)
+{
+ int ret, n, i, count;
+ struct rte_eth_representor_info *info = NULL;
+ size_t size;
+
+ if (type == RTE_ETH_REPRESENTOR_NONE)
+ return 0;
+ if (repr_id == NULL)
+ return -EINVAL;
+
+ /* Get PMD representor range info. */
+ ret = rte_eth_representor_info_get(ethdev->data->port_id, NULL);
+ if (ret == -ENOTSUP && type == RTE_ETH_REPRESENTOR_VF &&
+ controller == -1 && pf == -1) {
+ /* Direct mapping for legacy VF representor. */
+ *repr_id = representor_port;
+ return 0;
+ } else if (ret < 0) {
+ return ret;
+ }
+ n = ret;
+ size = sizeof(*info) + n * sizeof(info->ranges[0]);
+ info = calloc(1, size);
+ if (info == NULL)
+ return -ENOMEM;
+ ret = rte_eth_representor_info_get(ethdev->data->port_id, info);
+ if (ret < 0)
+ goto out;
+
+ /* Default controller and pf to caller. */
+ if (controller == -1)
+ controller = info->controller;
+ if (pf == -1)
+ pf = info->pf;
+
+ /* Locate representor ID. */
+ ret = -ENOENT;
+ for (i = 0; i < n; ++i) {
+ if (info->ranges[i].type != type)
+ continue;
+ if (info->ranges[i].controller != controller)
+ continue;
+ if (info->ranges[i].id_end < info->ranges[i].id_base) {
+ RTE_LOG(WARNING, EAL, "Port %hu invalid representor ID Range %u - %u, entry %d\n",
+ ethdev->data->port_id, info->ranges[i].id_base,
+ info->ranges[i].id_end, i);
+ continue;
+
+ }
+ count = info->ranges[i].id_end - info->ranges[i].id_base + 1;
+ switch (info->ranges[i].type) {
+ case RTE_ETH_REPRESENTOR_PF:
+ if (pf < info->ranges[i].pf ||
+ pf >= info->ranges[i].pf + count)
+ continue;
+ *repr_id = info->ranges[i].id_base +
+ (pf - info->ranges[i].pf);
+ ret = 0;
+ goto out;
+ case RTE_ETH_REPRESENTOR_VF:
+ if (info->ranges[i].pf != pf)
+ continue;
+ if (representor_port < info->ranges[i].vf ||
+ representor_port >= info->ranges[i].vf + count)
+ continue;
+ *repr_id = info->ranges[i].id_base +
+ (representor_port - info->ranges[i].vf);
+ ret = 0;
+ goto out;
+ case RTE_ETH_REPRESENTOR_SF:
+ if (info->ranges[i].pf != pf)
+ continue;
+ if (representor_port < info->ranges[i].sf ||
+ representor_port >= info->ranges[i].sf + count)
+ continue;
+ *repr_id = info->ranges[i].id_base +
+ (representor_port - info->ranges[i].sf);
+ ret = 0;
+ goto out;
+ default:
+ break;
+ }
+ }
+out:
+ free(info);
+ return ret;
+}
+
+static int
+eth_dev_handle_port_list(const char *cmd __rte_unused,
+ const char *params __rte_unused,
+ struct rte_tel_data *d)
+{
+ int port_id;
+
+ rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
+ RTE_ETH_FOREACH_DEV(port_id)
+ rte_tel_data_add_array_int(d, port_id);
+ return 0;
+}
+
+static void
+eth_dev_add_port_queue_stats(struct rte_tel_data *d, uint64_t *q_stats,
+ const char *stat_name)
+{
+ int q;
+ struct rte_tel_data *q_data = rte_tel_data_alloc();
+ rte_tel_data_start_array(q_data, RTE_TEL_U64_VAL);
+ for (q = 0; q < RTE_ETHDEV_QUEUE_STAT_CNTRS; q++)
+ rte_tel_data_add_array_u64(q_data, q_stats[q]);
+ rte_tel_data_add_dict_container(d, stat_name, q_data, 0);
+}
+
+#define ADD_DICT_STAT(stats, s) rte_tel_data_add_dict_u64(d, #s, stats.s)
+
+static int
+eth_dev_handle_port_stats(const char *cmd __rte_unused,
+ const char *params,
+ struct rte_tel_data *d)
+{
+ struct rte_eth_stats stats;
+ int port_id, ret;
+
+ if (params == NULL || strlen(params) == 0 || !isdigit(*params))
+ return -1;
+
+ port_id = atoi(params);
+ if (!rte_eth_dev_is_valid_port(port_id))
+ return -1;
+
+ ret = rte_eth_stats_get(port_id, &stats);
+ if (ret < 0)
+ return -1;
+
+ rte_tel_data_start_dict(d);
+ ADD_DICT_STAT(stats, ipackets);
+ ADD_DICT_STAT(stats, opackets);
+ ADD_DICT_STAT(stats, ibytes);
+ ADD_DICT_STAT(stats, obytes);
+ ADD_DICT_STAT(stats, imissed);
+ ADD_DICT_STAT(stats, ierrors);
+ ADD_DICT_STAT(stats, oerrors);
+ ADD_DICT_STAT(stats, rx_nombuf);
+ eth_dev_add_port_queue_stats(d, stats.q_ipackets, "q_ipackets");
+ eth_dev_add_port_queue_stats(d, stats.q_opackets, "q_opackets");
+ eth_dev_add_port_queue_stats(d, stats.q_ibytes, "q_ibytes");
+ eth_dev_add_port_queue_stats(d, stats.q_obytes, "q_obytes");
+ eth_dev_add_port_queue_stats(d, stats.q_errors, "q_errors");
+
+ return 0;
+}
+
+static int
+eth_dev_handle_port_xstats(const char *cmd __rte_unused,
+ const char *params,
+ struct rte_tel_data *d)
+{
+ struct rte_eth_xstat *eth_xstats;
+ struct rte_eth_xstat_name *xstat_names;
+ int port_id, num_xstats;
+ int i, ret;
+ char *end_param;
+
+ if (params == NULL || strlen(params) == 0 || !isdigit(*params))
+ return -1;
+
+ port_id = strtoul(params, &end_param, 0);
+ if (*end_param != '\0')
+ RTE_ETHDEV_LOG(NOTICE,
+ "Extra parameters passed to ethdev telemetry command, ignoring");
+ if (!rte_eth_dev_is_valid_port(port_id))
+ return -1;
+
+ num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
+ if (num_xstats < 0)
+ return -1;
+
+ /* use one malloc for both names and stats */
+ eth_xstats = malloc((sizeof(struct rte_eth_xstat) +
+ sizeof(struct rte_eth_xstat_name)) * num_xstats);
+ if (eth_xstats == NULL)
+ return -1;
+ xstat_names = (void *)ð_xstats[num_xstats];
+
+ ret = rte_eth_xstats_get_names(port_id, xstat_names, num_xstats);
+ if (ret < 0 || ret > num_xstats) {
+ free(eth_xstats);
+ return -1;
+ }
+
+ ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
+ if (ret < 0 || ret > num_xstats) {
+ free(eth_xstats);
+ return -1;
+ }
+
+ rte_tel_data_start_dict(d);
+ for (i = 0; i < num_xstats; i++)
+ rte_tel_data_add_dict_u64(d, xstat_names[i].name,
+ eth_xstats[i].value);
+ return 0;
+}
+
+static int
+eth_dev_handle_port_link_status(const char *cmd __rte_unused,
+ const char *params,
+ struct rte_tel_data *d)
+{
+ static const char *status_str = "status";
+ int ret, port_id;
+ struct rte_eth_link link;
+ char *end_param;
+
+ if (params == NULL || strlen(params) == 0 || !isdigit(*params))
+ return -1;
+
+ port_id = strtoul(params, &end_param, 0);
+ if (*end_param != '\0')
+ RTE_ETHDEV_LOG(NOTICE,
+ "Extra parameters passed to ethdev telemetry command, ignoring");
+ if (!rte_eth_dev_is_valid_port(port_id))
+ return -1;
+
+ ret = rte_eth_link_get_nowait(port_id, &link);
+ if (ret < 0)
+ return -1;
+
+ rte_tel_data_start_dict(d);
+ if (!link.link_status) {
+ rte_tel_data_add_dict_string(d, status_str, "DOWN");
+ return 0;
+ }
+ rte_tel_data_add_dict_string(d, status_str, "UP");
+ rte_tel_data_add_dict_u64(d, "speed", link.link_speed);
+ rte_tel_data_add_dict_string(d, "duplex",
+ (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
+ "full-duplex" : "half-duplex");
+ return 0;
+}
+
+int
+rte_eth_hairpin_queue_peer_update(uint16_t peer_port, uint16_t peer_queue,
+ struct rte_hairpin_peer_info *cur_info,
+ struct rte_hairpin_peer_info *peer_info,
+ uint32_t direction)
+{
+ struct rte_eth_dev *dev;
+
+ /* Current queue information is not mandatory. */
+ if (peer_info == NULL)
+ return -EINVAL;
+
+ /* No need to check the validity again. */
+ dev = &rte_eth_devices[peer_port];
+ RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_update,
+ -ENOTSUP);
+
+ return (*dev->dev_ops->hairpin_queue_peer_update)(dev, peer_queue,
+ cur_info, peer_info, direction);
+}
+
+int
+rte_eth_hairpin_queue_peer_bind(uint16_t cur_port, uint16_t cur_queue,
+ struct rte_hairpin_peer_info *peer_info,
+ uint32_t direction)
+{
+ struct rte_eth_dev *dev;
+
+ if (peer_info == NULL)
+ return -EINVAL;
+
+ /* No need to check the validity again. */
+ dev = &rte_eth_devices[cur_port];
+ RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_bind,
+ -ENOTSUP);
+
+ return (*dev->dev_ops->hairpin_queue_peer_bind)(dev, cur_queue,
+ peer_info, direction);
+}
+
+int
+rte_eth_hairpin_queue_peer_unbind(uint16_t cur_port, uint16_t cur_queue,
+ uint32_t direction)
+{
+ struct rte_eth_dev *dev;
+
+ /* No need to check the validity again. */
+ dev = &rte_eth_devices[cur_port];
+ RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_unbind,
+ -ENOTSUP);
+
+ return (*dev->dev_ops->hairpin_queue_peer_unbind)(dev, cur_queue,
+ direction);
+}
+
+int
+rte_eth_representor_info_get(uint16_t port_id,
+ struct rte_eth_representor_info *info)
+{
+ struct rte_eth_dev *dev;
+
+ RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+ dev = &rte_eth_devices[port_id];
+
+ RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->representor_info_get, -ENOTSUP);
+ return eth_err(port_id, (*dev->dev_ops->representor_info_get)(dev,
+ info));
+}
+
+RTE_LOG_REGISTER(rte_eth_dev_logtype, lib.ethdev, INFO);
+
+RTE_INIT(ethdev_init_telemetry)