1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
7 #include <rte_ethdev.h>
8 #include <rte_string_fns.h>
9 #ifdef RTE_LIBRTE_TELEMETRY
10 #include <rte_telemetry_legacy.h>
13 #include "rte_metrics.h"
14 #include "rte_metrics_telemetry.h"
16 int metrics_log_level;
19 #define METRICS_LOG(level, fmt, args...) \
20 rte_log(RTE_LOG_ ##level, metrics_log_level, "%s(): "fmt "\n", \
23 #define METRICS_LOG_ERR(fmt, args...) \
24 METRICS_LOG(ERR, fmt, ## args)
26 #define METRICS_LOG_WARN(fmt, args...) \
27 METRICS_LOG(WARNING, fmt, ## args)
30 rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
32 int ret, num_xstats, i;
33 struct rte_eth_xstat_name *eth_xstats_names;
34 const char **xstats_names;
36 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
38 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
43 xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
44 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
46 if (eth_xstats_names == NULL || xstats_names == NULL) {
47 METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
52 if (rte_eth_xstats_get_names(port_id,
53 eth_xstats_names, num_xstats) != num_xstats) {
54 METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d failed",
60 for (i = 0; i < num_xstats; i++)
61 xstats_names[i] = eth_xstats_names[i].name;
62 ret = rte_metrics_reg_names(xstats_names, num_xstats);
64 METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
67 free(eth_xstats_names);
73 rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
78 } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
79 int ret, nb_drv_idx = 0;
82 rte_metrics_init(rte_socket_id());
83 RTE_ETH_FOREACH_DEV(d) {
85 /* Different device types have different numbers of stats, so
86 * first check if the stats for this type of device have
87 * already been registered
89 for (i = 0; i < nb_drv_idx; i++) {
90 if (rte_eth_devices[d].dev_ops == drv_idx[i].dev_ops) {
91 reg_index_list[d] = drv_idx[i].reg_index;
96 continue; /* we found a match, go to next port */
98 /* No match, register a new set of xstats for this port */
99 ret = rte_metrics_tel_reg_port_ethdev_to_metrics(d);
101 METRICS_LOG_ERR("Failed to register ethdev to metrics");
104 reg_index_list[d] = ret;
105 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[d].dev_ops;
106 drv_idx[nb_drv_idx].reg_index = ret;
109 *metrics_register_done = 1;
114 rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
116 int ret, num_xstats, i;
117 struct rte_eth_xstat *eth_xstats;
119 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
120 if (num_xstats < 0) {
121 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
125 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
126 if (eth_xstats == NULL) {
127 METRICS_LOG_ERR("Failed to malloc memory for xstats");
130 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
131 if (ret < 0 || ret > num_xstats) {
133 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
134 port_id, num_xstats, ret);
138 uint64_t xstats_values[num_xstats];
139 for (i = 0; i < num_xstats; i++)
140 xstats_values[i] = eth_xstats[i].value;
141 if (rte_metrics_update_values(port_id, reg_start_index, xstats_values,
143 METRICS_LOG_ERR("Could not update metrics values");
152 rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
153 uint32_t *metric_ids, int num_metric_ids)
155 struct rte_metric_value *metrics = NULL;
156 struct rte_metric_name *names = NULL;
157 int num_metrics, i, ret = -EPERM; /* most error cases return EPERM */
158 json_t *port, *stats;
160 num_metrics = rte_metrics_get_names(NULL, 0);
161 if (num_metrics < 0) {
162 METRICS_LOG_ERR("Cannot get metrics count");
164 } else if (num_metrics == 0) {
165 METRICS_LOG_ERR("No metrics to display (none have been registered)");
169 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
170 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
171 if (metrics == NULL || names == NULL) {
172 METRICS_LOG_ERR("Cannot allocate memory");
176 if (rte_metrics_get_names(names, num_metrics) != num_metrics ||
177 rte_metrics_get_values(pid, metrics, num_metrics)
179 METRICS_LOG_ERR("Error getting metrics");
183 stats = json_array();
185 METRICS_LOG_ERR("Could not create stats JSON object");
189 for (i = 0; i < num_metrics; i++) {
191 for (j = 0; j < num_metric_ids; j++)
192 if (metrics[i].key == metric_ids[j])
195 if (num_metric_ids > 0 && j == num_metric_ids)
196 continue; /* can't find this id */
198 json_t *stat = json_pack("{s,s,s,I}",
199 "name", names[metrics[i].key].name,
200 "value", metrics[i].value);
201 if (stat == NULL || json_array_append_new(stats, stat) < 0) {
202 METRICS_LOG_ERR("Format stat with id: %u failed",
208 port = json_pack("{s,i,s,o}", "port", pid, "stats",
209 json_array_size(stats) ? stats : json_null());
210 if (port == NULL || json_array_append_new(ports, port) < 0) {
211 METRICS_LOG_ERR("Error creating port and adding to ports");
226 rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
229 json_t *root, *ports;
232 ports = json_array();
234 METRICS_LOG_ERR("Could not create ports JSON array");
238 if (ep->type == PORT_STATS) {
239 if (ep->pp.num_port_ids <= 0) {
240 METRICS_LOG_ERR("Please provide port/metric ids");
244 for (i = 0; i < ep->pp.num_port_ids; i++) {
245 ret = rte_metrics_tel_format_port(ep->pp.port_ids[i],
246 ports, &ep->pp.metric_ids[0],
247 ep->pp.num_metric_ids);
249 METRICS_LOG_ERR("Format port in JSON failed");
253 } else if (ep->type == GLOBAL_STATS) {
254 /* Request Global Metrics */
255 ret = rte_metrics_tel_format_port(RTE_METRICS_GLOBAL,
258 METRICS_LOG_ERR("Request Global Metrics Failed");
262 METRICS_LOG_ERR("Invalid metrics type in encode params");
266 root = json_pack("{s,s,s,o}", "status_code", "Status OK: 200",
269 METRICS_LOG_ERR("Root, Status or data field cannot be set");
273 *json_buffer = json_dumps(root, JSON_INDENT(2));
279 rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
280 int *reg_index, char **json_buffer)
285 for (i = 0; i < ep->pp.num_port_ids; i++) {
286 port_id = ep->pp.port_ids[i];
287 if (!rte_eth_dev_is_valid_port(port_id)) {
288 METRICS_LOG_ERR("Port: %d invalid", port_id);
292 ret = rte_metrics_tel_update_metrics_ethdev(port_id,
295 METRICS_LOG_ERR("Failed to update ethdev metrics");
300 ret = rte_metrics_tel_encode_json_format(ep, json_buffer);
302 METRICS_LOG_ERR("JSON encode function failed");
309 rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
311 int p, num_port_ids = 0;
313 RTE_ETH_FOREACH_DEV(p) {
314 ep->pp.port_ids[num_port_ids] = p;
319 METRICS_LOG_ERR("No active ports");
323 ep->pp.num_port_ids = num_port_ids;
324 ep->pp.num_metric_ids = 0;
325 ep->type = PORT_STATS;
330 rte_metrics_tel_stat_names_to_ids(const char * const *stat_names,
331 uint32_t *stat_ids, int num_stat_names)
333 struct rte_metric_name *names;
335 int i, j, nb_stat_ids = 0;
337 num_metrics = rte_metrics_get_names(NULL, 0);
338 if (num_metrics <= 0) {
339 METRICS_LOG_ERR("Error getting metrics count - no metrics may be registered");
343 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
345 METRICS_LOG_ERR("Cannot allocate memory for names");
349 if (rte_metrics_get_names(names, num_metrics) != num_metrics) {
350 METRICS_LOG_ERR("Cannot get metrics names");
355 for (i = 0; i < num_stat_names; i++) {
356 for (j = 0; j < num_metrics; j++) {
357 if (strcmp(stat_names[i], names[j].name) == 0) {
358 stat_ids[nb_stat_ids++] = j;
362 if (j == num_metrics) {
363 METRICS_LOG_WARN("Invalid stat name %s\n",
375 rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
378 json_t *port_ids_json = json_object_get(data, "ports");
379 json_t *stat_names_json = json_object_get(data, "stats");
380 uint64_t num_stat_names = json_array_size(stat_names_json);
381 const char *stat_names[num_stat_names];
385 memset(ep, 0, sizeof(*ep));
386 ep->pp.num_port_ids = json_array_size(port_ids_json);
387 ep->pp.num_metric_ids = num_stat_names;
388 if (!json_is_object(data) || !json_is_array(port_ids_json) ||
389 !json_is_array(stat_names_json)) {
390 METRICS_LOG_WARN("Invalid data provided for this command");
394 json_array_foreach(port_ids_json, index, value) {
395 if (!json_is_integer(value)) {
396 METRICS_LOG_WARN("Port ID given is not valid");
399 ep->pp.port_ids[index] = json_integer_value(value);
400 if (rte_eth_dev_is_valid_port(ep->pp.port_ids[index]) < 1)
403 json_array_foreach(stat_names_json, index, value) {
404 if (!json_is_string(value)) {
405 METRICS_LOG_WARN("Stat Name given is not a string");
408 stat_names[index] = json_string_value(value);
411 ret = rte_metrics_tel_stat_names_to_ids(stat_names, ep->pp.metric_ids,
414 METRICS_LOG_ERR("Could not convert stat names to IDs");
418 ep->type = PORT_STATS;
423 rte_metrics_tel_initial_metrics_setup(void)
426 rte_metrics_init(rte_socket_id());
428 if (!tel_met_data.metrics_register_done) {
429 ret = rte_metrics_tel_reg_all_ethdev(
430 &tel_met_data.metrics_register_done,
431 tel_met_data.reg_index);
439 handle_ports_all_stats_values(const char *cmd __rte_unused,
440 const char *params __rte_unused,
441 char *buffer, int buf_len)
443 struct telemetry_encode_param ep;
445 char *json_buffer = NULL;
447 ret = rte_metrics_tel_initial_metrics_setup();
451 memset(&ep, 0, sizeof(ep));
452 ret = rte_metrics_tel_get_port_stats_ids(&ep);
456 ret = rte_metrics_tel_get_ports_stats_json(&ep, tel_met_data.reg_index,
461 used += strlcpy(buffer, json_buffer, buf_len);
466 handle_global_stats_values(const char *cmd __rte_unused,
467 const char *params __rte_unused,
468 char *buffer, int buf_len)
470 char *json_buffer = NULL;
471 struct telemetry_encode_param ep = { .type = GLOBAL_STATS };
474 ret = rte_metrics_tel_initial_metrics_setup();
478 ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
480 METRICS_LOG_ERR("JSON encode function failed");
483 used += strlcpy(buffer, json_buffer, buf_len);
488 handle_ports_stats_values_by_name(const char *cmd __rte_unused,
490 char *buffer, int buf_len)
492 char *json_buffer = NULL;
493 struct telemetry_encode_param ep;
498 ret = rte_metrics_tel_initial_metrics_setup();
502 data = json_loads(params, 0, &error);
504 METRICS_LOG_WARN("Could not load JSON object from data passed in : %s",
507 } else if (!json_is_object(data)) {
508 METRICS_LOG_WARN("JSON Request data is not a JSON object");
513 ret = rte_metrics_tel_extract_data(&ep, data);
515 METRICS_LOG_ERR("Extract data function failed");
519 ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
521 METRICS_LOG_ERR("JSON encode function failed");
524 used += strlcpy(buffer, json_buffer, buf_len);
528 RTE_LOG_REGISTER(metrics_log_level, lib.metrics, ERR);
530 RTE_INIT(metrics_ctor)
532 #ifdef RTE_LIBRTE_TELEMETRY
533 rte_telemetry_legacy_register("ports_all_stat_values", DATA_NOT_REQ,
534 handle_ports_all_stats_values);
535 rte_telemetry_legacy_register("global_stat_values", DATA_NOT_REQ,
536 handle_global_stats_values);
537 rte_telemetry_legacy_register("ports_stats_values_by_name", DATA_REQ,
538 handle_ports_stats_values_by_name);