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_is_port_active(int port_id)
31 ret = rte_eth_find_next(port_id);
35 METRICS_LOG_ERR("port_id: %d is invalid, not active",
42 rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
44 int ret, num_xstats, ret_val, i;
45 struct rte_eth_xstat *eth_xstats = NULL;
46 struct rte_eth_xstat_name *eth_xstats_names = NULL;
48 if (!rte_eth_dev_is_valid_port(port_id)) {
49 METRICS_LOG_ERR("port_id: %d is invalid", port_id);
53 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
55 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
60 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
61 if (eth_xstats == NULL) {
62 METRICS_LOG_ERR("Failed to malloc memory for xstats");
66 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
67 const char *xstats_names[num_xstats];
68 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
70 if (ret < 0 || ret > num_xstats) {
71 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
72 port_id, num_xstats, ret);
77 if (eth_xstats_names == NULL) {
78 METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
83 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
84 if (ret < 0 || ret > num_xstats) {
85 METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
86 port_id, num_xstats, ret);
91 for (i = 0; i < num_xstats; i++)
92 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
94 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
96 METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
105 free(eth_xstats_names);
110 rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
112 struct driver_index {
115 } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
120 RTE_ETH_FOREACH_DEV(pid) {
122 /* Different device types have different numbers of stats, so
123 * first check if the stats for this type of device have
124 * already been registered
126 for (i = 0; i < nb_drv_idx; i++) {
127 if (rte_eth_devices[pid].dev_ops ==
128 drv_idx[i].dev_ops) {
129 reg_index_list[pid] = drv_idx[i].reg_index;
134 continue; /* we found a match, go to next port */
136 /* No match, register a new set of xstats for this port */
137 ret = rte_metrics_tel_reg_port_ethdev_to_metrics(pid);
139 METRICS_LOG_ERR("Failed to register ethdev metrics");
142 reg_index_list[pid] = ret;
143 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
144 drv_idx[nb_drv_idx].reg_index = ret;
148 *metrics_register_done = 1;
153 rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
155 int ret, num_xstats, i;
156 struct rte_eth_xstat *eth_xstats;
158 if (!rte_eth_dev_is_valid_port(port_id)) {
159 METRICS_LOG_ERR("port_id: %d is invalid", port_id);
163 ret = rte_metrics_tel_is_port_active(port_id);
167 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
168 if (num_xstats < 0) {
169 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
174 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
175 if (eth_xstats == NULL) {
176 METRICS_LOG_ERR("Failed to malloc memory for xstats");
180 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
181 if (ret < 0 || ret > num_xstats) {
183 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
184 port_id, num_xstats, ret);
188 uint64_t xstats_values[num_xstats];
189 for (i = 0; i < num_xstats; i++)
190 xstats_values[i] = eth_xstats[i].value;
192 ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
195 METRICS_LOG_ERR("Could not update metrics values");
205 rte_metrics_tel_get_metrics(uint32_t port_id, struct rte_metric_value
206 *metrics, struct rte_metric_name *names, int num_metrics)
210 if (num_metrics < 0) {
211 METRICS_LOG_ERR("Invalid metrics count");
213 } else if (num_metrics == 0) {
214 METRICS_LOG_ERR("No metrics to display (none have been registered)");
218 if (metrics == NULL) {
219 METRICS_LOG_ERR("Metrics must be initialised.");
224 METRICS_LOG_ERR("Names must be initialised.");
228 ret = rte_metrics_get_names(names, num_metrics);
229 if (ret < 0 || ret > num_metrics) {
230 METRICS_LOG_ERR("Cannot get metrics names");
234 num_values = rte_metrics_get_values(port_id, NULL, 0);
235 ret = rte_metrics_get_values(port_id, metrics, num_values);
236 if (ret < 0 || ret > num_values) {
237 METRICS_LOG_ERR("Cannot get metrics values");
245 rte_metrics_tel_json_format_stat(json_t *stats, const char *metric_name,
246 uint64_t metric_value)
249 json_t *stat = json_object();
252 METRICS_LOG_ERR("Could not create stat JSON object");
256 ret = json_object_set_new(stat, "name", json_string(metric_name));
258 METRICS_LOG_ERR("Stat Name field cannot be set");
262 ret = json_object_set_new(stat, "value", json_integer(metric_value));
264 METRICS_LOG_ERR("Stat Value field cannot be set");
268 ret = json_array_append_new(stats, stat);
270 METRICS_LOG_ERR("Stat cannot be added to stats json array");
278 rte_metrics_tel_json_format_port(uint32_t port_id, json_t *ports,
279 uint32_t *metric_ids, int num_metric_ids)
281 struct rte_metric_value *metrics = 0;
282 struct rte_metric_name *names = 0;
283 int num_metrics, ret;
284 json_t *port, *stats;
287 num_metrics = rte_metrics_get_names(NULL, 0);
288 if (num_metrics < 0) {
289 METRICS_LOG_ERR("Cannot get metrics count");
291 } else if (num_metrics == 0) {
292 METRICS_LOG_ERR("No metrics to display (none have been registered)");
296 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
297 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
298 if (metrics == NULL || names == NULL) {
299 METRICS_LOG_ERR("Cannot allocate memory");
305 ret = rte_metrics_tel_get_metrics(port_id, metrics, names,
310 METRICS_LOG_ERR("rte_metrics_tel_get_metrics failed");
314 port = json_object();
315 stats = json_array();
316 if (port == NULL || stats == NULL) {
317 METRICS_LOG_ERR("Could not create port/stats JSON objects");
321 ret = json_object_set_new(port, "port", json_integer(port_id));
323 METRICS_LOG_ERR("Port field cannot be set");
327 for (i = 0; i < num_metric_ids; i++) {
328 int metric_id = metric_ids[i];
329 int metric_index = -1;
330 int metric_name_key = -1;
332 uint64_t metric_value;
334 if (metric_id >= num_metrics) {
335 METRICS_LOG_ERR("Metric_id: %d is not valid",
340 for (j = 0; j < num_metrics; j++) {
341 if (metrics[j].key == metric_id) {
342 metric_name_key = metrics[j].key;
348 const char *metric_name = names[metric_name_key].name;
349 metric_value = metrics[metric_index].value;
351 if (metric_name_key < 0 || metric_index < 0) {
352 METRICS_LOG_ERR("Could not get metric name/index");
356 ret = rte_metrics_tel_json_format_stat(stats, metric_name,
359 METRICS_LOG_ERR("Format stat with id: %u failed",
367 if (json_array_size(stats) == 0)
368 ret = json_object_set_new(port, "stats", json_null());
370 ret = json_object_set_new(port, "stats", stats);
373 METRICS_LOG_ERR("Stats object cannot be set");
377 ret = json_array_append_new(ports, port);
379 METRICS_LOG_ERR("Port object cannot be added to ports array");
399 rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
403 json_t *root, *ports;
409 ports = json_array();
411 METRICS_LOG_ERR("Could not create ports JSON array");
415 if (ep->type == PORT_STATS) {
416 num_port_ids = ep->pp.num_port_ids;
417 num_metric_ids = ep->pp.num_metric_ids;
419 if (num_port_ids <= 0 || num_metric_ids <= 0) {
420 METRICS_LOG_ERR("Please provide port and metric ids to query");
424 for (i = 0; i < num_port_ids; i++) {
425 port_id = ep->pp.port_ids[i];
426 if (!rte_eth_dev_is_valid_port(port_id)) {
427 METRICS_LOG_ERR("Port: %d invalid",
433 for (i = 0; i < num_port_ids; i++) {
434 port_id = ep->pp.port_ids[i];
435 ret = rte_metrics_tel_json_format_port(port_id,
436 ports, &ep->pp.metric_ids[0],
439 METRICS_LOG_ERR("Format port in JSON failed");
443 } else if (ep->type == GLOBAL_STATS) {
444 /* Request Global Metrics */
445 ret = rte_metrics_tel_json_format_port(RTE_METRICS_GLOBAL,
446 ports, &ep->gp.metric_ids[0],
447 ep->gp.num_metric_ids);
449 METRICS_LOG_ERR(" Request Global Metrics Failed");
453 METRICS_LOG_ERR(" Invalid metrics type in encode params");
457 root = json_object();
459 METRICS_LOG_ERR("Could not create root JSON object");
463 ret = json_object_set_new(root, "status_code",
464 json_string("Status OK: 200"));
466 METRICS_LOG_ERR("Status code field cannot be set");
470 ret = json_object_set_new(root, "data", ports);
472 METRICS_LOG_ERR("Data field cannot be set");
476 *json_buffer = json_dumps(root, JSON_INDENT(2));
482 rte_metrics_tel_get_global_stats(struct telemetry_encode_param *ep)
484 int num_metrics, ret, i;
485 struct rte_metric_value *values;
487 num_metrics = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
488 if (num_metrics < 0) {
489 METRICS_LOG_ERR("Cannot get metrics count");
491 } else if (num_metrics == 0) {
492 METRICS_LOG_ERR("No metrics to display (none have been registered)");
496 values = malloc(sizeof(struct rte_metric_value) * num_metrics);
497 if (values == NULL) {
498 METRICS_LOG_ERR("Cannot allocate memory");
502 ret = rte_metrics_get_values(RTE_METRICS_GLOBAL, values, num_metrics);
504 METRICS_LOG_ERR("Could not get stat values");
508 for (i = 0; i < num_metrics; i++)
509 ep->gp.metric_ids[i] = values[i].key;
511 ep->gp.num_metric_ids = num_metrics;
512 ep->type = GLOBAL_STATS;
518 rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
519 int *reg_index, char **json_buffer)
524 for (i = 0; i < ep->pp.num_port_ids; i++) {
525 port_id = ep->pp.port_ids[i];
526 if (!rte_eth_dev_is_valid_port(port_id)) {
527 METRICS_LOG_ERR("Port: %d invalid", port_id);
531 ret = rte_metrics_tel_update_metrics_ethdev(port_id,
534 METRICS_LOG_ERR("Failed to update ethdev metrics");
539 ret = rte_metrics_tel_encode_json_format(ep, json_buffer);
541 METRICS_LOG_ERR("JSON encode function failed");
548 rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
550 int ret, num_metrics, i, p;
551 struct rte_metric_value *values;
552 uint64_t num_port_ids = 0;
554 num_metrics = rte_metrics_get_values(0, NULL, 0);
555 if (num_metrics < 0) {
556 METRICS_LOG_ERR("Cannot get metrics count");
558 } else if (num_metrics == 0) {
559 METRICS_LOG_ERR("No metrics to display (none have been registered)");
563 values = malloc(sizeof(struct rte_metric_value) * num_metrics);
564 if (values == NULL) {
565 METRICS_LOG_ERR("Cannot allocate memory");
569 RTE_ETH_FOREACH_DEV(p) {
570 ep->pp.port_ids[num_port_ids] = p;
575 METRICS_LOG_ERR("No active ports");
579 ret = rte_metrics_get_values(ep->pp.port_ids[0], values, num_metrics);
581 METRICS_LOG_ERR("Could not get stat values");
584 for (i = 0; i < num_metrics; i++)
585 ep->pp.metric_ids[i] = values[i].key;
587 ep->pp.num_port_ids = num_port_ids;
588 ep->pp.num_metric_ids = num_metrics;
589 ep->type = PORT_STATS;
598 rte_metrics_tel_stat_names_to_ids(const char * const *stat_names,
599 uint32_t *stat_ids, uint64_t num_stat_names)
601 struct rte_metric_name *names;
602 int ret, num_metrics;
605 if (stat_names == NULL) {
606 METRICS_LOG_WARN("Invalid stat_names argument");
610 if (num_stat_names <= 0) {
611 METRICS_LOG_WARN("Invalid num_stat_names argument");
615 num_metrics = rte_metrics_get_names(NULL, 0);
616 if (num_metrics < 0) {
617 METRICS_LOG_ERR("Cannot get metrics count");
619 } else if (num_metrics == 0) {
620 METRICS_LOG_WARN("No metrics have been registered");
624 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
626 METRICS_LOG_ERR("Cannot allocate memory for names");
630 ret = rte_metrics_get_names(names, num_metrics);
631 if (ret < 0 || ret > num_metrics) {
632 METRICS_LOG_ERR("Cannot get metrics names");
638 for (i = 0; i < (uint32_t)num_stat_names; i++) {
640 for (j = 0; j < (uint32_t)num_metrics; j++) {
641 if (strcmp(stat_names[i], names[j].name) == 0) {
649 if (k != num_stat_names) {
650 METRICS_LOG_WARN("Invalid stat names provided");
660 rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
663 json_t *port_ids_json = json_object_get(data, "ports");
664 json_t *stat_names_json = json_object_get(data, "stats");
665 uint64_t num_stat_names = json_array_size(stat_names_json);
666 const char *stat_names[num_stat_names];
670 memset(ep, 0, sizeof(*ep));
671 ep->pp.num_port_ids = json_array_size(port_ids_json);
672 ep->pp.num_metric_ids = num_stat_names;
673 if (!json_is_object(data)) {
674 METRICS_LOG_WARN("Invalid data provided for this command");
678 if (!json_is_array(port_ids_json) ||
679 !json_is_array(stat_names_json)) {
680 METRICS_LOG_WARN("Invalid input data array(s)");
684 json_array_foreach(port_ids_json, index, value) {
685 if (!json_is_integer(value)) {
686 METRICS_LOG_WARN("Port ID given is not valid");
689 ep->pp.port_ids[index] = json_integer_value(value);
690 ret = rte_metrics_tel_is_port_active(ep->pp.port_ids[index]);
695 json_array_foreach(stat_names_json, index, value) {
696 if (!json_is_string(value)) {
697 METRICS_LOG_WARN("Stat Name given is not a string");
700 stat_names[index] = json_string_value(value);
703 ret = rte_metrics_tel_stat_names_to_ids(stat_names, ep->pp.metric_ids,
706 METRICS_LOG_ERR("Could not convert stat names to IDs");
710 ep->type = PORT_STATS;
714 RTE_INIT(metrics_ctor)
716 metrics_log_level = rte_log_register("lib.metrics");
717 if (metrics_log_level >= 0)
718 rte_log_set_level(metrics_log_level, RTE_LOG_ERR);