1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
7 #include <rte_ethdev.h>
8 #include <rte_string_fns.h>
10 #include "rte_metrics.h"
11 #include "rte_metrics_telemetry.h"
13 int metrics_log_level;
16 #define METRICS_LOG(level, fmt, args...) \
17 rte_log(RTE_LOG_ ##level, metrics_log_level, "%s(): "fmt "\n", \
20 #define METRICS_LOG_ERR(fmt, args...) \
21 METRICS_LOG(ERR, fmt, ## args)
23 #define METRICS_LOG_WARN(fmt, args...) \
24 METRICS_LOG(WARNING, fmt, ## args)
27 rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
29 int ret, num_xstats, i;
30 struct rte_eth_xstat_name *eth_xstats_names;
31 const char **xstats_names;
33 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
35 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
40 xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
41 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
43 if (eth_xstats_names == NULL || xstats_names == NULL) {
44 METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
49 if (rte_eth_xstats_get_names(port_id,
50 eth_xstats_names, num_xstats) != num_xstats) {
51 METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d failed",
57 for (i = 0; i < num_xstats; i++)
58 xstats_names[i] = eth_xstats_names[i].name;
59 ret = rte_metrics_reg_names(xstats_names, num_xstats);
61 METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
64 free(eth_xstats_names);
70 rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
75 } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
76 int ret, nb_drv_idx = 0;
79 RTE_ETH_FOREACH_DEV(d) {
81 /* Different device types have different numbers of stats, so
82 * first check if the stats for this type of device have
83 * already been registered
85 for (i = 0; i < nb_drv_idx; i++) {
86 if (rte_eth_devices[d].dev_ops == drv_idx[i].dev_ops) {
87 reg_index_list[d] = drv_idx[i].reg_index;
92 continue; /* we found a match, go to next port */
94 /* No match, register a new set of xstats for this port */
95 ret = rte_metrics_tel_reg_port_ethdev_to_metrics(d);
97 METRICS_LOG_ERR("Failed to register ethdev to metrics");
100 reg_index_list[d] = ret;
101 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[d].dev_ops;
102 drv_idx[nb_drv_idx].reg_index = ret;
105 *metrics_register_done = 1;
110 rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
112 int ret, num_xstats, i;
113 struct rte_eth_xstat *eth_xstats;
115 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
116 if (num_xstats < 0) {
117 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
121 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
122 if (eth_xstats == NULL) {
123 METRICS_LOG_ERR("Failed to malloc memory for xstats");
126 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
127 if (ret < 0 || ret > num_xstats) {
129 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
130 port_id, num_xstats, ret);
134 uint64_t xstats_values[num_xstats];
135 for (i = 0; i < num_xstats; i++)
136 xstats_values[i] = eth_xstats[i].value;
137 if (rte_metrics_update_values(port_id, reg_start_index, xstats_values,
139 METRICS_LOG_ERR("Could not update metrics values");
148 rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
149 uint32_t *metric_ids, int num_metric_ids)
151 struct rte_metric_value *metrics = NULL;
152 struct rte_metric_name *names = NULL;
153 int num_metrics, i, ret = -EPERM; /* most error cases return EPERM */
154 json_t *port, *stats;
156 num_metrics = rte_metrics_get_names(NULL, 0);
157 if (num_metrics < 0) {
158 METRICS_LOG_ERR("Cannot get metrics count");
160 } else if (num_metrics == 0) {
161 METRICS_LOG_ERR("No metrics to display (none have been registered)");
165 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
166 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
167 if (metrics == NULL || names == NULL) {
168 METRICS_LOG_ERR("Cannot allocate memory");
172 if (rte_metrics_get_names(names, num_metrics) != num_metrics ||
173 rte_metrics_get_values(pid, metrics, num_metrics)
175 METRICS_LOG_ERR("Error getting metrics");
179 stats = json_array();
181 METRICS_LOG_ERR("Could not create stats JSON object");
185 for (i = 0; i < num_metrics; i++) {
187 for (j = 0; j < num_metric_ids; j++)
188 if (metrics[i].key == metric_ids[j])
191 if (num_metric_ids > 0 && j == num_metric_ids)
192 continue; /* can't find this id */
194 json_t *stat = json_pack("{s,s,s,I}",
195 "name", names[metrics[i].key].name,
196 "value", metrics[i].value);
197 if (stat == NULL || json_array_append_new(stats, stat) < 0) {
198 METRICS_LOG_ERR("Format stat with id: %u failed",
204 port = json_pack("{s,i,s,o}", "port", pid, "stats",
205 json_array_size(stats) ? stats : json_null());
206 if (port == NULL || json_array_append_new(ports, port) < 0) {
207 METRICS_LOG_ERR("Error creating port and adding to ports");
222 rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
225 json_t *root, *ports;
228 ports = json_array();
230 METRICS_LOG_ERR("Could not create ports JSON array");
234 if (ep->type == PORT_STATS) {
235 if (ep->pp.num_port_ids <= 0) {
236 METRICS_LOG_ERR("Please provide port/metric ids");
240 for (i = 0; i < ep->pp.num_port_ids; i++) {
241 ret = rte_metrics_tel_format_port(ep->pp.port_ids[i],
242 ports, &ep->pp.metric_ids[0],
243 ep->pp.num_metric_ids);
245 METRICS_LOG_ERR("Format port in JSON failed");
249 } else if (ep->type == GLOBAL_STATS) {
250 /* Request Global Metrics */
251 ret = rte_metrics_tel_format_port(RTE_METRICS_GLOBAL,
254 METRICS_LOG_ERR("Request Global Metrics Failed");
258 METRICS_LOG_ERR("Invalid metrics type in encode params");
262 root = json_pack("{s,s,s,o}", "status_code", "Status OK: 200",
265 METRICS_LOG_ERR("Root, Status or data field cannot be set");
269 *json_buffer = json_dumps(root, JSON_INDENT(2));
275 rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
276 int *reg_index, char **json_buffer)
281 for (i = 0; i < ep->pp.num_port_ids; i++) {
282 port_id = ep->pp.port_ids[i];
283 if (!rte_eth_dev_is_valid_port(port_id)) {
284 METRICS_LOG_ERR("Port: %d invalid", port_id);
288 ret = rte_metrics_tel_update_metrics_ethdev(port_id,
291 METRICS_LOG_ERR("Failed to update ethdev metrics");
296 ret = rte_metrics_tel_encode_json_format(ep, json_buffer);
298 METRICS_LOG_ERR("JSON encode function failed");
305 rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
307 int p, num_port_ids = 0;
309 RTE_ETH_FOREACH_DEV(p) {
310 ep->pp.port_ids[num_port_ids] = p;
315 METRICS_LOG_ERR("No active ports");
319 ep->pp.num_port_ids = num_port_ids;
320 ep->pp.num_metric_ids = 0;
321 ep->type = PORT_STATS;
326 rte_metrics_tel_stat_names_to_ids(const char * const *stat_names,
327 uint32_t *stat_ids, int num_stat_names)
329 struct rte_metric_name *names;
331 int i, j, nb_stat_ids = 0;
333 num_metrics = rte_metrics_get_names(NULL, 0);
334 if (num_metrics <= 0) {
335 METRICS_LOG_ERR("Error getting metrics count - no metrics may be registered");
339 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
341 METRICS_LOG_ERR("Cannot allocate memory for names");
345 if (rte_metrics_get_names(names, num_metrics) != num_metrics) {
346 METRICS_LOG_ERR("Cannot get metrics names");
351 for (i = 0; i < num_stat_names; i++) {
352 for (j = 0; j < num_metrics; j++) {
353 if (strcmp(stat_names[i], names[j].name) == 0) {
354 stat_ids[nb_stat_ids++] = j;
358 if (j == num_metrics) {
359 METRICS_LOG_WARN("Invalid stat name %s\n",
371 rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
374 json_t *port_ids_json = json_object_get(data, "ports");
375 json_t *stat_names_json = json_object_get(data, "stats");
376 uint64_t num_stat_names = json_array_size(stat_names_json);
377 const char *stat_names[num_stat_names];
381 memset(ep, 0, sizeof(*ep));
382 ep->pp.num_port_ids = json_array_size(port_ids_json);
383 ep->pp.num_metric_ids = num_stat_names;
384 if (!json_is_object(data) || !json_is_array(port_ids_json) ||
385 !json_is_array(stat_names_json)) {
386 METRICS_LOG_WARN("Invalid data provided for this command");
390 json_array_foreach(port_ids_json, index, value) {
391 if (!json_is_integer(value)) {
392 METRICS_LOG_WARN("Port ID given is not valid");
395 ep->pp.port_ids[index] = json_integer_value(value);
396 if (rte_eth_dev_is_valid_port(ep->pp.port_ids[index]) < 1)
399 json_array_foreach(stat_names_json, index, value) {
400 if (!json_is_string(value)) {
401 METRICS_LOG_WARN("Stat Name given is not a string");
404 stat_names[index] = json_string_value(value);
407 ret = rte_metrics_tel_stat_names_to_ids(stat_names, ep->pp.metric_ids,
410 METRICS_LOG_ERR("Could not convert stat names to IDs");
414 ep->type = PORT_STATS;
418 RTE_INIT(metrics_ctor)
420 metrics_log_level = rte_log_register("lib.metrics");
421 if (metrics_log_level >= 0)
422 rte_log_set_level(metrics_log_level, RTE_LOG_ERR);