1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #include <rte_ethdev.h>
6 #include <rte_string_fns.h>
7 #ifdef RTE_LIB_TELEMETRY
8 #include <telemetry_internal.h>
11 #include "rte_metrics.h"
12 #include "rte_metrics_telemetry.h"
14 #ifdef RTE_HAS_JANSSON
16 struct telemetry_metrics_data tel_met_data;
18 int metrics_log_level;
21 #define METRICS_LOG(level, fmt, args...) \
22 rte_log(RTE_LOG_ ##level, metrics_log_level, "%s(): "fmt "\n", \
25 #define METRICS_LOG_ERR(fmt, args...) \
26 METRICS_LOG(ERR, fmt, ## args)
28 #define METRICS_LOG_WARN(fmt, args...) \
29 METRICS_LOG(WARNING, fmt, ## args)
32 rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
34 int ret, num_xstats, i;
35 struct rte_eth_xstat_name *eth_xstats_names;
36 const char **xstats_names;
38 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
40 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
45 xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
46 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
48 if (eth_xstats_names == NULL || xstats_names == NULL) {
49 METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
54 if (rte_eth_xstats_get_names(port_id,
55 eth_xstats_names, num_xstats) != num_xstats) {
56 METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d failed",
62 for (i = 0; i < num_xstats; i++)
63 xstats_names[i] = eth_xstats_names[i].name;
64 ret = rte_metrics_reg_names(xstats_names, num_xstats);
66 METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
69 free(eth_xstats_names);
75 rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
80 } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
81 int ret, nb_drv_idx = 0;
84 rte_metrics_init(rte_socket_id());
85 RTE_ETH_FOREACH_DEV(d) {
87 /* Different device types have different numbers of stats, so
88 * first check if the stats for this type of device have
89 * already been registered
91 for (i = 0; i < nb_drv_idx; i++) {
92 if (rte_eth_devices[d].dev_ops == drv_idx[i].dev_ops) {
93 reg_index_list[d] = drv_idx[i].reg_index;
98 continue; /* we found a match, go to next port */
100 /* No match, register a new set of xstats for this port */
101 ret = rte_metrics_tel_reg_port_ethdev_to_metrics(d);
103 METRICS_LOG_ERR("Failed to register ethdev to metrics");
106 reg_index_list[d] = ret;
107 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[d].dev_ops;
108 drv_idx[nb_drv_idx].reg_index = ret;
111 *metrics_register_done = 1;
116 rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
118 int ret, num_xstats, i;
119 struct rte_eth_xstat *eth_xstats;
121 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
122 if (num_xstats < 0) {
123 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
127 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
128 if (eth_xstats == NULL) {
129 METRICS_LOG_ERR("Failed to malloc memory for xstats");
132 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
133 if (ret < 0 || ret > num_xstats) {
135 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
136 port_id, num_xstats, ret);
140 uint64_t xstats_values[num_xstats];
141 for (i = 0; i < num_xstats; i++)
142 xstats_values[i] = eth_xstats[i].value;
143 if (rte_metrics_update_values(port_id, reg_start_index, xstats_values,
145 METRICS_LOG_ERR("Could not update metrics values");
154 rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
155 uint32_t *metric_ids, int num_metric_ids)
157 struct rte_metric_value *metrics = NULL;
158 struct rte_metric_name *names = NULL;
159 int num_metrics, i, ret = -EPERM; /* most error cases return EPERM */
160 json_t *port, *stats;
162 num_metrics = rte_metrics_get_names(NULL, 0);
163 if (num_metrics < 0) {
164 METRICS_LOG_ERR("Cannot get metrics count");
166 } else if (num_metrics == 0) {
167 METRICS_LOG_ERR("No metrics to display (none have been registered)");
171 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
172 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
173 if (metrics == NULL || names == NULL) {
174 METRICS_LOG_ERR("Cannot allocate memory");
179 if (rte_metrics_get_names(names, num_metrics) != num_metrics ||
180 rte_metrics_get_values(pid, metrics, num_metrics)
182 METRICS_LOG_ERR("Error getting metrics");
186 stats = json_array();
188 METRICS_LOG_ERR("Could not create stats JSON object");
192 for (i = 0; i < num_metrics; i++) {
194 for (j = 0; j < num_metric_ids; j++)
195 if (metrics[i].key == metric_ids[j])
198 if (num_metric_ids > 0 && j == num_metric_ids)
199 continue; /* can't find this id */
201 json_t *stat = json_pack("{s,s,s,I}",
202 "name", names[metrics[i].key].name,
203 "value", metrics[i].value);
204 if (stat == NULL || json_array_append_new(stats, stat) < 0) {
205 METRICS_LOG_ERR("Format stat with id: %u failed",
211 port = json_pack("{s,i,s,o}", "port", pid, "stats",
212 json_array_size(stats) ? stats : json_null());
213 if (port == NULL || json_array_append_new(ports, port) < 0) {
214 METRICS_LOG_ERR("Error creating port and adding to ports");
229 rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
232 json_t *root, *ports;
235 ports = json_array();
237 METRICS_LOG_ERR("Could not create ports JSON array");
241 if (ep->type == PORT_STATS) {
242 if (ep->pp.num_port_ids <= 0) {
243 METRICS_LOG_ERR("Please provide port/metric ids");
247 for (i = 0; i < ep->pp.num_port_ids; i++) {
248 ret = rte_metrics_tel_format_port(ep->pp.port_ids[i],
249 ports, &ep->pp.metric_ids[0],
250 ep->pp.num_metric_ids);
252 METRICS_LOG_ERR("Format port in JSON failed");
256 } else if (ep->type == GLOBAL_STATS) {
257 /* Request Global Metrics */
258 ret = rte_metrics_tel_format_port(RTE_METRICS_GLOBAL,
261 METRICS_LOG_ERR("Request Global Metrics Failed");
265 METRICS_LOG_ERR("Invalid metrics type in encode params");
269 root = json_pack("{s,s,s,o}", "status_code", "Status OK: 200",
272 METRICS_LOG_ERR("Root, Status or data field cannot be set");
276 *json_buffer = json_dumps(root, JSON_INDENT(2));
282 rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
283 int *reg_index, char **json_buffer)
288 for (i = 0; i < ep->pp.num_port_ids; i++) {
289 port_id = ep->pp.port_ids[i];
290 if (!rte_eth_dev_is_valid_port(port_id)) {
291 METRICS_LOG_ERR("Port: %d invalid", port_id);
295 ret = rte_metrics_tel_update_metrics_ethdev(port_id,
298 METRICS_LOG_ERR("Failed to update ethdev metrics");
303 ret = rte_metrics_tel_encode_json_format(ep, json_buffer);
305 METRICS_LOG_ERR("JSON encode function failed");
312 rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
314 int p, num_port_ids = 0;
316 RTE_ETH_FOREACH_DEV(p) {
317 ep->pp.port_ids[num_port_ids] = p;
322 METRICS_LOG_ERR("No active ports");
326 ep->pp.num_port_ids = num_port_ids;
327 ep->pp.num_metric_ids = 0;
328 ep->type = PORT_STATS;
333 rte_metrics_tel_stat_names_to_ids(const char * const *stat_names,
334 uint32_t *stat_ids, int num_stat_names)
336 struct rte_metric_name *names;
338 int i, j, nb_stat_ids = 0;
340 num_metrics = rte_metrics_get_names(NULL, 0);
341 if (num_metrics <= 0) {
342 METRICS_LOG_ERR("Error getting metrics count - no metrics may be registered");
346 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
348 METRICS_LOG_ERR("Cannot allocate memory for names");
352 if (rte_metrics_get_names(names, num_metrics) != num_metrics) {
353 METRICS_LOG_ERR("Cannot get metrics names");
358 for (i = 0; i < num_stat_names; i++) {
359 for (j = 0; j < num_metrics; j++) {
360 if (strcmp(stat_names[i], names[j].name) == 0) {
361 stat_ids[nb_stat_ids++] = j;
365 if (j == num_metrics) {
366 METRICS_LOG_WARN("Invalid stat name %s\n",
378 rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
381 json_t *port_ids_json = json_object_get(data, "ports");
382 json_t *stat_names_json = json_object_get(data, "stats");
383 uint64_t num_stat_names = json_array_size(stat_names_json);
384 const char *stat_names[num_stat_names];
388 memset(ep, 0, sizeof(*ep));
389 ep->pp.num_port_ids = json_array_size(port_ids_json);
390 ep->pp.num_metric_ids = num_stat_names;
391 if (!json_is_object(data) || !json_is_array(port_ids_json) ||
392 !json_is_array(stat_names_json)) {
393 METRICS_LOG_WARN("Invalid data provided for this command");
397 json_array_foreach(port_ids_json, index, value) {
398 if (!json_is_integer(value)) {
399 METRICS_LOG_WARN("Port ID given is not valid");
402 ep->pp.port_ids[index] = json_integer_value(value);
403 if (rte_eth_dev_is_valid_port(ep->pp.port_ids[index]) < 1)
406 json_array_foreach(stat_names_json, index, value) {
407 if (!json_is_string(value)) {
408 METRICS_LOG_WARN("Stat Name given is not a string");
411 stat_names[index] = json_string_value(value);
414 ret = rte_metrics_tel_stat_names_to_ids(stat_names, ep->pp.metric_ids,
417 METRICS_LOG_ERR("Could not convert stat names to IDs");
421 ep->type = PORT_STATS;
426 rte_metrics_tel_initial_metrics_setup(void)
429 rte_metrics_init(rte_socket_id());
431 if (!tel_met_data.metrics_register_done) {
432 ret = rte_metrics_tel_reg_all_ethdev(
433 &tel_met_data.metrics_register_done,
434 tel_met_data.reg_index);
442 handle_ports_all_stats_values(const char *cmd __rte_unused,
443 const char *params __rte_unused,
444 char *buffer, int buf_len)
446 struct telemetry_encode_param ep;
448 char *json_buffer = NULL;
450 ret = rte_metrics_tel_initial_metrics_setup();
454 memset(&ep, 0, sizeof(ep));
455 ret = rte_metrics_tel_get_port_stats_ids(&ep);
459 ret = rte_metrics_tel_get_ports_stats_json(&ep, tel_met_data.reg_index,
464 used += strlcpy(buffer, json_buffer, buf_len);
469 handle_global_stats_values(const char *cmd __rte_unused,
470 const char *params __rte_unused,
471 char *buffer, int buf_len)
473 char *json_buffer = NULL;
474 struct telemetry_encode_param ep = { .type = GLOBAL_STATS };
477 ret = rte_metrics_tel_initial_metrics_setup();
481 ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
483 METRICS_LOG_ERR("JSON encode function failed");
486 used += strlcpy(buffer, json_buffer, buf_len);
491 handle_ports_stats_values_by_name(const char *cmd __rte_unused,
493 char *buffer, int buf_len)
495 char *json_buffer = NULL;
496 struct telemetry_encode_param ep;
501 ret = rte_metrics_tel_initial_metrics_setup();
505 data = json_loads(params, 0, &error);
507 METRICS_LOG_WARN("Could not load JSON object from data passed in : %s",
510 } else if (!json_is_object(data)) {
511 METRICS_LOG_WARN("JSON Request data is not a JSON object");
516 ret = rte_metrics_tel_extract_data(&ep, data);
518 METRICS_LOG_ERR("Extract data function failed");
522 ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
524 METRICS_LOG_ERR("JSON encode function failed");
527 used += strlcpy(buffer, json_buffer, buf_len);
531 RTE_LOG_REGISTER_DEFAULT(metrics_log_level, ERR);
533 RTE_INIT(metrics_ctor)
535 #ifdef RTE_LIB_TELEMETRY
536 rte_telemetry_legacy_register("ports_all_stat_values", DATA_NOT_REQ,
537 handle_ports_all_stats_values);
538 rte_telemetry_legacy_register("global_stat_values", DATA_NOT_REQ,
539 handle_global_stats_values);
540 rte_telemetry_legacy_register("ports_stats_values_by_name", DATA_REQ,
541 handle_ports_stats_values_by_name);
545 #else /* !RTE_HAS_JANSSON */
548 rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
550 RTE_SET_USED(metrics_register_done);
551 RTE_SET_USED(reg_index_list);
557 rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
561 RTE_SET_USED(json_buffer);
567 rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
568 int *reg_index, char **json_buffer)
571 RTE_SET_USED(reg_index);
572 RTE_SET_USED(json_buffer);
578 rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
586 rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
595 rte_metrics_tel_get_global_stats(struct telemetry_encode_param *ep)
602 #endif /* !RTE_HAS_JANSSON */