LCOV - code coverage report
Current view: top level - lib/metrics - rte_metrics_telemetry.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 6 270 2.2 %
Date: 2026-11-01 19:11:22 Functions: 2 15 13.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 154 0.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2020 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <jansson.h>
       6                 :            : 
       7                 :            : #include <ethdev_driver.h>
       8                 :            : #include <rte_string_fns.h>
       9                 :            : #ifdef RTE_LIB_TELEMETRY
      10                 :            : #include <telemetry_internal.h>
      11                 :            : #endif
      12                 :            : 
      13                 :            : #include "rte_metrics.h"
      14                 :            : 
      15                 :            : enum rte_telemetry_stats_type {
      16                 :            :         PORT_STATS = 0,
      17                 :            :         GLOBAL_STATS = 1
      18                 :            : };
      19                 :            : 
      20                 :            : struct telemetry_encode_param {
      21                 :            :         enum rte_telemetry_stats_type type;
      22                 :            :         struct port_param {
      23                 :            :                 int num_metric_ids;
      24                 :            :                 uint32_t metric_ids[RTE_METRICS_MAX_METRICS];
      25                 :            :                 int num_port_ids;
      26                 :            :                 uint32_t port_ids[RTE_MAX_ETHPORTS];
      27                 :            :         } pp;
      28                 :            : };
      29                 :            : 
      30                 :            : static struct telemetry_metrics_data {
      31                 :            :         int reg_index[RTE_MAX_ETHPORTS];
      32                 :            :         int metrics_register_done;
      33                 :            : } tel_met_data;
      34                 :            : 
      35                 :            : int metrics_log_level;
      36                 :            : #define RTE_LOGTYPE_METRICS metrics_log_level
      37                 :            : 
      38                 :            : /* Logging Macros */
      39                 :            : #define METRICS_LOG(level, ...) \
      40                 :            :         RTE_LOG_LINE_PREFIX(level, METRICS, "%s(): ", __func__, __VA_ARGS__)
      41                 :            : 
      42                 :            : #define METRICS_LOG_ERR(...) \
      43                 :            :         METRICS_LOG(ERR, __VA_ARGS__)
      44                 :            : 
      45                 :            : #define METRICS_LOG_WARN(...) \
      46                 :            :         METRICS_LOG(WARNING, __VA_ARGS__)
      47                 :            : 
      48                 :            : static int32_t
      49                 :          0 : rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
      50                 :            : {
      51                 :            :         int ret,  num_xstats, i;
      52                 :            :         struct rte_eth_xstat_name *eth_xstats_names;
      53                 :            :         const char **xstats_names;
      54                 :            : 
      55                 :          0 :         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
      56         [ #  # ]:          0 :         if (num_xstats < 0) {
      57                 :          0 :                 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
      58                 :            :                                 port_id, num_xstats);
      59                 :          0 :                 return -EPERM;
      60                 :            :         }
      61                 :            : 
      62                 :          0 :         xstats_names = malloc(sizeof(*xstats_names) * num_xstats);
      63                 :          0 :         eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
      64                 :            :                         * num_xstats);
      65         [ #  # ]:          0 :         if (eth_xstats_names == NULL || xstats_names == NULL) {
      66                 :          0 :                 METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
      67                 :            :                 ret = -ENOMEM;
      68                 :          0 :                 goto free_xstats;
      69                 :            :         }
      70                 :            : 
      71         [ #  # ]:          0 :         if (rte_eth_xstats_get_names(port_id,
      72                 :            :                         eth_xstats_names, num_xstats) != num_xstats) {
      73                 :          0 :                 METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len %d failed",
      74                 :            :                                 port_id, num_xstats);
      75                 :            :                 ret = -EPERM;
      76                 :          0 :                 goto free_xstats;
      77                 :            :         }
      78                 :            : 
      79         [ #  # ]:          0 :         for (i = 0; i < num_xstats; i++)
      80                 :          0 :                 xstats_names[i] = eth_xstats_names[i].name;
      81                 :          0 :         ret = rte_metrics_reg_names(xstats_names, num_xstats);
      82         [ #  # ]:          0 :         if (ret < 0)
      83                 :          0 :                 METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
      84                 :            : 
      85                 :          0 : free_xstats:
      86                 :          0 :         free(eth_xstats_names);
      87                 :          0 :         free(xstats_names);
      88                 :          0 :         return ret;
      89                 :            : }
      90                 :            : 
      91                 :            : static int32_t
      92                 :          0 : rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
      93                 :            : {
      94                 :            :         struct driver_index {
      95                 :            :                 const void *dev_ops;
      96                 :            :                 int reg_index;
      97                 :          0 :         } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
      98                 :            :         int ret, nb_drv_idx = 0;
      99                 :            :         uint16_t d;
     100                 :            : 
     101                 :          0 :         rte_metrics_init(rte_socket_id());
     102         [ #  # ]:          0 :         RTE_ETH_FOREACH_DEV(d) {
     103                 :            :                 int i;
     104                 :            :                 /* Different device types have different numbers of stats, so
     105                 :            :                  * first check if the stats for this type of device have
     106                 :            :                  * already been registered
     107                 :            :                  */
     108         [ #  # ]:          0 :                 for (i = 0; i < nb_drv_idx; i++) {
     109         [ #  # ]:          0 :                         if (rte_eth_devices[d].dev_ops == drv_idx[i].dev_ops) {
     110                 :          0 :                                 reg_index_list[d] = drv_idx[i].reg_index;
     111                 :          0 :                                 break;
     112                 :            :                         }
     113                 :            :                 }
     114         [ #  # ]:          0 :                 if (i < nb_drv_idx)
     115                 :          0 :                         continue; /* we found a match, go to next port */
     116                 :            : 
     117                 :            :                 /* No match, register a new set of xstats for this port */
     118                 :          0 :                 ret = rte_metrics_tel_reg_port_ethdev_to_metrics(d);
     119         [ #  # ]:          0 :                 if (ret < 0) {
     120                 :          0 :                         METRICS_LOG_ERR("Failed to register ethdev to metrics");
     121                 :          0 :                         return ret;
     122                 :            :                 }
     123                 :          0 :                 reg_index_list[d] = ret;
     124                 :          0 :                 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[d].dev_ops;
     125                 :          0 :                 drv_idx[nb_drv_idx].reg_index = ret;
     126                 :          0 :                 nb_drv_idx++;
     127                 :            :         }
     128                 :          0 :         *metrics_register_done = 1;
     129                 :          0 :         return 0;
     130                 :            : }
     131                 :            : 
     132                 :            : static int32_t
     133                 :          0 : rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
     134                 :          0 : {
     135                 :            :         int ret, num_xstats, i;
     136                 :            :         struct rte_eth_xstat *eth_xstats;
     137                 :            : 
     138                 :          0 :         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
     139         [ #  # ]:          0 :         if (num_xstats < 0) {
     140                 :          0 :                 METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
     141                 :            :                                 num_xstats);
     142                 :          0 :                 return -EPERM;
     143                 :            :         }
     144                 :          0 :         eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
     145         [ #  # ]:          0 :         if (eth_xstats == NULL) {
     146                 :          0 :                 METRICS_LOG_ERR("Failed to malloc memory for xstats");
     147                 :          0 :                 return -ENOMEM;
     148                 :            :         }
     149                 :          0 :         ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
     150         [ #  # ]:          0 :         if (ret < 0 || ret > num_xstats) {
     151                 :          0 :                 free(eth_xstats);
     152                 :          0 :                 METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
     153                 :            :                                 port_id, num_xstats, ret);
     154                 :          0 :                 return -EPERM;
     155                 :            :         }
     156                 :            : 
     157                 :          0 :         uint64_t xstats_values[num_xstats];
     158         [ #  # ]:          0 :         for (i = 0; i < num_xstats; i++)
     159                 :          0 :                 xstats_values[i] = eth_xstats[i].value;
     160         [ #  # ]:          0 :         if (rte_metrics_update_values(port_id, reg_start_index, xstats_values,
     161                 :            :                         num_xstats) < 0) {
     162                 :          0 :                 METRICS_LOG_ERR("Could not update metrics values");
     163                 :          0 :                 free(eth_xstats);
     164                 :          0 :                 return -EPERM;
     165                 :            :         }
     166                 :          0 :         free(eth_xstats);
     167                 :          0 :         return 0;
     168                 :            : }
     169                 :            : 
     170                 :            : static int32_t
     171                 :          0 : rte_metrics_tel_format_port(uint32_t pid, json_t *ports,
     172                 :            :         uint32_t *metric_ids, int num_metric_ids)
     173                 :            : {
     174                 :            :         struct rte_metric_value *metrics = NULL;
     175                 :            :         struct rte_metric_name *names = NULL;
     176                 :            :         int num_metrics, i, ret = -EPERM; /* most error cases return EPERM */
     177                 :            :         json_t *port, *stats;
     178                 :            : 
     179                 :          0 :         num_metrics = rte_metrics_get_names(NULL, 0);
     180         [ #  # ]:          0 :         if (num_metrics < 0) {
     181                 :          0 :                 METRICS_LOG_ERR("Cannot get metrics count");
     182                 :          0 :                 return -EINVAL;
     183         [ #  # ]:          0 :         } else if (num_metrics == 0) {
     184                 :          0 :                 METRICS_LOG_ERR("No metrics to display (none have been registered)");
     185                 :          0 :                 return -EPERM;
     186                 :            :         }
     187                 :            : 
     188                 :          0 :         metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
     189                 :          0 :         names = malloc(sizeof(struct rte_metric_name) * num_metrics);
     190         [ #  # ]:          0 :         if (metrics == NULL || names == NULL) {
     191                 :          0 :                 METRICS_LOG_ERR("Cannot allocate memory");
     192                 :            :                 ret = -ENOMEM;
     193                 :          0 :                 goto fail;
     194                 :            :         }
     195                 :            : 
     196   [ #  #  #  # ]:          0 :         if (rte_metrics_get_names(names, num_metrics) != num_metrics ||
     197                 :          0 :                         rte_metrics_get_values(pid, metrics, num_metrics)
     198                 :            :                                 != num_metrics) {
     199                 :          0 :                 METRICS_LOG_ERR("Error getting metrics");
     200                 :          0 :                 goto fail;
     201                 :            :         }
     202                 :            : 
     203                 :          0 :         stats = json_array();
     204         [ #  # ]:          0 :         if (stats == NULL) {
     205                 :          0 :                 METRICS_LOG_ERR("Could not create stats JSON object");
     206                 :          0 :                 goto fail;
     207                 :            :         }
     208                 :            : 
     209         [ #  # ]:          0 :         for (i = 0; i < num_metrics; i++) {
     210                 :            :                 int32_t j;
     211         [ #  # ]:          0 :                 for (j = 0; j < num_metric_ids; j++)
     212         [ #  # ]:          0 :                         if (metrics[i].key == metric_ids[j])
     213                 :            :                                 break;
     214                 :            : 
     215         [ #  # ]:          0 :                 if (num_metric_ids > 0 && j == num_metric_ids)
     216                 :          0 :                         continue; /* can't find this id */
     217                 :            : 
     218                 :          0 :                 json_t *stat = json_pack("{s,s,s,I}",
     219                 :          0 :                                 "name", names[metrics[i].key].name,
     220                 :          0 :                                 "value", metrics[i].value);
     221   [ #  #  #  # ]:          0 :                 if (stat == NULL || json_array_append_new(stats, stat) < 0) {
     222                 :          0 :                         METRICS_LOG_ERR("Format stat with id: %u failed",
     223                 :            :                                         metrics[i].key);
     224                 :          0 :                         goto fail;
     225                 :            :                 }
     226                 :            :         }
     227                 :            : 
     228         [ #  # ]:          0 :         port = json_pack("{s,i,s,o}", "port", pid, "stats",
     229                 :          0 :                         json_array_size(stats) ? stats : json_null());
     230   [ #  #  #  # ]:          0 :         if (port == NULL || json_array_append_new(ports, port) < 0) {
     231                 :          0 :                 METRICS_LOG_ERR("Error creating port and adding to ports");
     232                 :          0 :                 goto fail;
     233                 :            :         }
     234                 :            : 
     235                 :          0 :         free(metrics);
     236                 :          0 :         free(names);
     237                 :          0 :         return 0;
     238                 :            : 
     239                 :          0 : fail:
     240                 :          0 :         free(metrics);
     241                 :          0 :         free(names);
     242                 :          0 :         return ret;
     243                 :            : }
     244                 :            : 
     245                 :            : static int32_t
     246                 :          0 : rte_metrics_tel_encode_json_format(struct telemetry_encode_param *ep,
     247                 :            :                 char **json_buffer)
     248                 :            : {
     249                 :            :         json_t *root, *ports;
     250                 :            :         int ret, i;
     251                 :            : 
     252                 :          0 :         ports = json_array();
     253         [ #  # ]:          0 :         if (ports == NULL) {
     254                 :          0 :                 METRICS_LOG_ERR("Could not create ports JSON array");
     255                 :          0 :                 return -EPERM;
     256                 :            :         }
     257                 :            : 
     258         [ #  # ]:          0 :         if (ep->type == PORT_STATS) {
     259         [ #  # ]:          0 :                 if (ep->pp.num_port_ids <= 0) {
     260                 :          0 :                         METRICS_LOG_ERR("Please provide port/metric ids");
     261                 :          0 :                         return -EINVAL;
     262                 :            :                 }
     263                 :            : 
     264         [ #  # ]:          0 :                 for (i = 0; i < ep->pp.num_port_ids; i++) {
     265                 :          0 :                         ret = rte_metrics_tel_format_port(ep->pp.port_ids[i],
     266                 :            :                                         ports, &ep->pp.metric_ids[0],
     267                 :            :                                         ep->pp.num_metric_ids);
     268         [ #  # ]:          0 :                         if (ret < 0) {
     269                 :          0 :                                 METRICS_LOG_ERR("Format port in JSON failed");
     270                 :          0 :                                 return ret;
     271                 :            :                         }
     272                 :            :                 }
     273         [ #  # ]:          0 :         } else if (ep->type == GLOBAL_STATS) {
     274                 :            :                 /* Request Global Metrics */
     275                 :          0 :                 ret = rte_metrics_tel_format_port(RTE_METRICS_GLOBAL,
     276                 :            :                                 ports, NULL, 0);
     277         [ #  # ]:          0 :                 if (ret < 0) {
     278                 :          0 :                         METRICS_LOG_ERR("Request Global Metrics Failed");
     279                 :          0 :                         return ret;
     280                 :            :                 }
     281                 :            :         } else {
     282                 :          0 :                 METRICS_LOG_ERR("Invalid metrics type in encode params");
     283                 :          0 :                 return -EINVAL;
     284                 :            :         }
     285                 :            : 
     286                 :          0 :         root = json_pack("{s,s,s,o}", "status_code", "Status OK: 200",
     287                 :            :                         "data", ports);
     288         [ #  # ]:          0 :         if (root == NULL) {
     289                 :          0 :                 METRICS_LOG_ERR("Root, Status or data field cannot be set");
     290                 :          0 :                 return -EPERM;
     291                 :            :         }
     292                 :            : 
     293                 :          0 :         *json_buffer = json_dumps(root, JSON_INDENT(2));
     294                 :          0 :         json_decref(root);
     295                 :          0 :         return 0;
     296                 :            : }
     297                 :            : 
     298                 :            : static int32_t
     299                 :          0 : rte_metrics_tel_get_ports_stats_json(struct telemetry_encode_param *ep,
     300                 :            :                 int *reg_index, char **json_buffer)
     301                 :            : {
     302                 :            :         int ret, i;
     303                 :            :         uint32_t port_id;
     304                 :            : 
     305         [ #  # ]:          0 :         for (i = 0; i < ep->pp.num_port_ids; i++) {
     306                 :          0 :                 port_id = ep->pp.port_ids[i];
     307         [ #  # ]:          0 :                 if (!rte_eth_dev_is_valid_port(port_id)) {
     308                 :          0 :                         METRICS_LOG_ERR("Port: %d invalid", port_id);
     309                 :          0 :                         return -EINVAL;
     310                 :            :                 }
     311                 :            : 
     312                 :          0 :                 ret = rte_metrics_tel_update_metrics_ethdev(port_id,
     313                 :          0 :                                 reg_index[i]);
     314         [ #  # ]:          0 :                 if (ret < 0) {
     315                 :          0 :                         METRICS_LOG_ERR("Failed to update ethdev metrics");
     316                 :          0 :                         return ret;
     317                 :            :                 }
     318                 :            :         }
     319                 :            : 
     320                 :          0 :         ret = rte_metrics_tel_encode_json_format(ep, json_buffer);
     321         [ #  # ]:          0 :         if (ret < 0) {
     322                 :          0 :                 METRICS_LOG_ERR("JSON encode function failed");
     323                 :          0 :                 return ret;
     324                 :            :         }
     325                 :            :         return 0;
     326                 :            : }
     327                 :            : 
     328                 :            : static int32_t
     329                 :          0 : rte_metrics_tel_get_port_stats_ids(struct telemetry_encode_param *ep)
     330                 :            : {
     331                 :            :         int p, num_port_ids = 0;
     332                 :            : 
     333         [ #  # ]:          0 :         RTE_ETH_FOREACH_DEV(p) {
     334                 :          0 :                 ep->pp.port_ids[num_port_ids] = p;
     335                 :          0 :                 num_port_ids++;
     336                 :            :         }
     337                 :            : 
     338         [ #  # ]:          0 :         if (!num_port_ids) {
     339                 :          0 :                 METRICS_LOG_ERR("No active ports");
     340                 :          0 :                 return -EINVAL;
     341                 :            :         }
     342                 :            : 
     343                 :          0 :         ep->pp.num_port_ids = num_port_ids;
     344                 :          0 :         ep->pp.num_metric_ids = 0;
     345                 :          0 :         ep->type = PORT_STATS;
     346                 :          0 :         return 0;
     347                 :            : }
     348                 :            : 
     349                 :            : static int32_t
     350                 :          0 : rte_metrics_tel_stat_names_to_ids(const char * const *stat_names,
     351                 :            :         uint32_t *stat_ids, int num_stat_names)
     352                 :            : {
     353                 :            :         struct rte_metric_name *names;
     354                 :            :         int num_metrics;
     355                 :            :         int i, j, nb_stat_ids = 0;
     356                 :            : 
     357                 :          0 :         num_metrics = rte_metrics_get_names(NULL, 0);
     358         [ #  # ]:          0 :         if (num_metrics <= 0) {
     359                 :          0 :                 METRICS_LOG_ERR("Error getting metrics count - no metrics may be registered");
     360                 :          0 :                 return -EPERM;
     361                 :            :         }
     362                 :            : 
     363                 :          0 :         names = malloc(sizeof(struct rte_metric_name) * num_metrics);
     364         [ #  # ]:          0 :         if (names == NULL) {
     365                 :          0 :                 METRICS_LOG_ERR("Cannot allocate memory for names");
     366                 :          0 :                 return -ENOMEM;
     367                 :            :         }
     368                 :            : 
     369         [ #  # ]:          0 :         if (rte_metrics_get_names(names, num_metrics) != num_metrics) {
     370                 :          0 :                 METRICS_LOG_ERR("Cannot get metrics names");
     371                 :          0 :                 free(names);
     372                 :          0 :                 return -EPERM;
     373                 :            :         }
     374                 :            : 
     375         [ #  # ]:          0 :         for (i = 0; i < num_stat_names; i++) {
     376         [ #  # ]:          0 :                 for (j = 0; j < num_metrics; j++) {
     377         [ #  # ]:          0 :                         if (strcmp(stat_names[i], names[j].name) == 0) {
     378                 :          0 :                                 stat_ids[nb_stat_ids++] = j;
     379                 :          0 :                                 break;
     380                 :            :                         }
     381                 :            :                 }
     382         [ #  # ]:          0 :                 if (j == num_metrics) {
     383                 :          0 :                         METRICS_LOG_WARN("Invalid stat name %s",
     384                 :            :                                         stat_names[i]);
     385                 :          0 :                         free(names);
     386                 :          0 :                         return -EINVAL;
     387                 :            :                 }
     388                 :            :         }
     389                 :            : 
     390                 :          0 :         free(names);
     391                 :          0 :         return 0;
     392                 :            : }
     393                 :            : 
     394                 :            : static int32_t
     395                 :          0 : rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
     396                 :          0 : {
     397                 :            :         int ret;
     398                 :          0 :         json_t *port_ids_json = json_object_get(data, "ports");
     399                 :          0 :         json_t *stat_names_json = json_object_get(data, "stats");
     400                 :          0 :         uint64_t num_stat_names = json_array_size(stat_names_json);
     401                 :          0 :         const char *stat_names[num_stat_names];
     402                 :            :         size_t index;
     403                 :            :         json_t *value;
     404                 :            : 
     405                 :            :         memset(ep, 0, sizeof(*ep));
     406                 :          0 :         ep->pp.num_port_ids = json_array_size(port_ids_json);
     407                 :          0 :         ep->pp.num_metric_ids = num_stat_names;
     408   [ #  #  #  #  :          0 :         if (!json_is_object(data) || !json_is_array(port_ids_json) ||
          #  #  #  #  #  
                      # ]
     409         [ #  # ]:          0 :                         !json_is_array(stat_names_json)) {
     410                 :          0 :                 METRICS_LOG_WARN("Invalid data provided for this command");
     411                 :          0 :                 return -EINVAL;
     412                 :            :         }
     413                 :            : 
     414   [ #  #  #  # ]:          0 :         json_array_foreach(port_ids_json, index, value) {
     415         [ #  # ]:          0 :                 if (!json_is_integer(value)) {
     416                 :          0 :                         METRICS_LOG_WARN("Port ID given is not valid");
     417                 :          0 :                         return -EINVAL;
     418                 :            :                 }
     419                 :          0 :                 ep->pp.port_ids[index] = json_integer_value(value);
     420         [ #  # ]:          0 :                 if (rte_eth_dev_is_valid_port(ep->pp.port_ids[index]) < 1)
     421                 :            :                         return -EINVAL;
     422                 :            :         }
     423   [ #  #  #  # ]:          0 :         json_array_foreach(stat_names_json, index, value) {
     424         [ #  # ]:          0 :                 if (!json_is_string(value)) {
     425                 :          0 :                         METRICS_LOG_WARN("Stat Name given is not a string");
     426                 :          0 :                         return -EINVAL;
     427                 :            :                 }
     428                 :          0 :                 stat_names[index] = json_string_value(value);
     429                 :            :         }
     430                 :            : 
     431                 :          0 :         ret = rte_metrics_tel_stat_names_to_ids(stat_names, ep->pp.metric_ids,
     432                 :            :                         num_stat_names);
     433         [ #  # ]:          0 :         if (ret < 0) {
     434                 :          0 :                 METRICS_LOG_ERR("Could not convert stat names to IDs");
     435                 :          0 :                 return ret;
     436                 :            :         }
     437                 :            : 
     438                 :          0 :         ep->type = PORT_STATS;
     439                 :          0 :         return 0;
     440                 :            : }
     441                 :            : 
     442                 :            : static int
     443                 :          0 : rte_metrics_tel_initial_metrics_setup(void)
     444                 :            : {
     445                 :            :         int ret;
     446                 :          0 :         rte_metrics_init(rte_socket_id());
     447                 :            : 
     448         [ #  # ]:          0 :         if (!tel_met_data.metrics_register_done) {
     449                 :          0 :                 ret = rte_metrics_tel_reg_all_ethdev(
     450                 :            :                         &tel_met_data.metrics_register_done,
     451                 :            :                         tel_met_data.reg_index);
     452                 :            :                 if (ret < 0)
     453                 :            :                         return ret;
     454                 :            :         }
     455                 :            :         return 0;
     456                 :            : }
     457                 :            : 
     458                 :            : static int
     459                 :          0 : handle_ports_all_stats_values(const char *cmd __rte_unused,
     460                 :            :                 const char *params __rte_unused,
     461                 :            :                 char *buffer, int buf_len)
     462                 :            : {
     463                 :            :         struct telemetry_encode_param ep;
     464                 :            :         int ret, used = 0;
     465                 :          0 :         char *json_buffer = NULL;
     466                 :            : 
     467                 :          0 :         ret = rte_metrics_tel_initial_metrics_setup();
     468         [ #  # ]:          0 :         if (ret < 0)
     469                 :            :                 return ret;
     470                 :            : 
     471                 :            :         memset(&ep, 0, sizeof(ep));
     472                 :          0 :         ret = rte_metrics_tel_get_port_stats_ids(&ep);
     473         [ #  # ]:          0 :         if (ret < 0)
     474                 :            :                 return ret;
     475                 :            : 
     476                 :          0 :         ret = rte_metrics_tel_get_ports_stats_json(&ep, tel_met_data.reg_index,
     477                 :            :                         &json_buffer);
     478         [ #  # ]:          0 :         if (ret < 0)
     479                 :            :                 return ret;
     480                 :            : 
     481                 :          0 :         used += strlcpy(buffer, json_buffer, buf_len);
     482                 :          0 :         return used;
     483                 :            : }
     484                 :            : 
     485                 :            : static int
     486                 :          0 : handle_global_stats_values(const char *cmd __rte_unused,
     487                 :            :                 const char *params __rte_unused,
     488                 :            :                 char *buffer, int buf_len)
     489                 :            : {
     490                 :          0 :         char *json_buffer = NULL;
     491                 :          0 :         struct telemetry_encode_param ep = { .type = GLOBAL_STATS };
     492                 :            :         int ret, used = 0;
     493                 :            : 
     494                 :          0 :         ret = rte_metrics_tel_initial_metrics_setup();
     495         [ #  # ]:          0 :         if (ret < 0)
     496                 :            :                 return ret;
     497                 :            : 
     498                 :          0 :         ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
     499         [ #  # ]:          0 :         if (ret < 0) {
     500                 :          0 :                 METRICS_LOG_ERR("JSON encode function failed");
     501                 :          0 :                 return ret;
     502                 :            :         }
     503                 :          0 :         used += strlcpy(buffer, json_buffer, buf_len);
     504                 :          0 :         return used;
     505                 :            : }
     506                 :            : 
     507                 :            : static int
     508                 :          0 : handle_ports_stats_values_by_name(const char *cmd __rte_unused,
     509                 :            :                 const char *params,
     510                 :            :                 char *buffer, int buf_len)
     511                 :            : {
     512                 :          0 :         char *json_buffer = NULL;
     513                 :            :         struct telemetry_encode_param ep;
     514                 :            :         int ret, used = 0;
     515                 :            :         json_t *data;
     516                 :            :         json_error_t error;
     517                 :            : 
     518                 :          0 :         ret = rte_metrics_tel_initial_metrics_setup();
     519         [ #  # ]:          0 :         if (ret < 0)
     520                 :            :                 return ret;
     521                 :            : 
     522                 :          0 :         data = json_loads(params, 0, &error);
     523         [ #  # ]:          0 :         if (!data) {
     524                 :          0 :                 METRICS_LOG_WARN("Could not load JSON object from data passed in : %s",
     525                 :            :                                 error.text);
     526                 :          0 :                 return -EPERM;
     527         [ #  # ]:          0 :         } else if (!json_is_object(data)) {
     528                 :          0 :                 METRICS_LOG_WARN("JSON Request data is not a JSON object");
     529                 :          0 :                 json_decref(data);
     530                 :          0 :                 return -EINVAL;
     531                 :            :         }
     532                 :            : 
     533                 :          0 :         ret = rte_metrics_tel_extract_data(&ep, data);
     534         [ #  # ]:          0 :         if (ret < 0) {
     535                 :          0 :                 METRICS_LOG_ERR("Extract data function failed");
     536                 :          0 :                 return ret;
     537                 :            :         }
     538                 :            : 
     539                 :          0 :         ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
     540         [ #  # ]:          0 :         if (ret < 0) {
     541                 :          0 :                 METRICS_LOG_ERR("JSON encode function failed");
     542                 :          0 :                 return ret;
     543                 :            :         }
     544                 :          0 :         used += strlcpy(buffer, json_buffer, buf_len);
     545                 :          0 :         return used;
     546                 :            : }
     547                 :            : 
     548         [ -  + ]:        305 : RTE_LOG_REGISTER_DEFAULT(metrics_log_level, ERR);
     549                 :            : 
     550                 :        305 : RTE_INIT(metrics_ctor)
     551                 :            : {
     552                 :            : #ifdef RTE_LIB_TELEMETRY
     553                 :        305 :         rte_telemetry_legacy_register("ports_all_stat_values", DATA_NOT_REQ,
     554                 :            :                         handle_ports_all_stats_values);
     555                 :        305 :         rte_telemetry_legacy_register("global_stat_values", DATA_NOT_REQ,
     556                 :            :                         handle_global_stats_values);
     557                 :        305 :         rte_telemetry_legacy_register("ports_stats_values_by_name", DATA_REQ,
     558                 :            :                         handle_ports_stats_values_by_name);
     559                 :            : #endif
     560                 :        305 : }

Generated by: LCOV version 1.14