LCOV - code coverage report
Current view: top level - lib/telemetry - telemetry_legacy.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 15 94 16.0 %
Date: 2025-05-01 17:49:45 Functions: 2 6 33.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 68 10.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2020 Intel Corporation
       3                 :            :  */
       4                 :            : #include <ctype.h>
       5                 :            : #include <errno.h>
       6                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
       7                 :            : #include <unistd.h>
       8                 :            : #include <sys/socket.h>
       9                 :            : #include <sys/un.h>
      10                 :            : #include <pthread.h>
      11                 :            : #endif /* !RTE_EXEC_ENV_WINDOWS */
      12                 :            : 
      13                 :            : /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
      14                 :            : #undef RTE_USE_LIBBSD
      15                 :            : #include <eal_export.h>
      16                 :            : #include <rte_string_fns.h>
      17                 :            : #include <rte_common.h>
      18                 :            : #include <rte_spinlock.h>
      19                 :            : 
      20                 :            : #include "telemetry_internal.h"
      21                 :            : 
      22                 :            : #define MAX_LEN 128
      23                 :            : #define BUF_SIZE 1024
      24                 :            : #define CLIENTS_UNREG_ACTION "\"action\":2"
      25                 :            : #define CLIENTS_CMD "\"command\":\"clients\""
      26                 :            : #define CLIENTS_DATA "\"data\":{\"client_path\":\""
      27                 :            : #define STATS_ACTION "\"action\":0"
      28                 :            : #define DATA_REQ_LABEL "\"data\":"
      29                 :            : #define TELEMETRY_LEGACY_MAX_CALLBACKS 4
      30                 :            : 
      31                 :            : 
      32                 :            : static int
      33                 :            : register_client(const char *cmd __rte_unused,
      34                 :            :                 const char *params __rte_unused,
      35                 :            :                 char *buffer, int buf_len);
      36                 :            : 
      37                 :            : struct json_command {
      38                 :            :         char action[MAX_LEN];
      39                 :            :         char cmd[MAX_LEN];
      40                 :            :         char data[MAX_LEN];
      41                 :            :         telemetry_legacy_cb fn;
      42                 :            : 
      43                 :            : };
      44                 :            : 
      45                 :            : struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
      46                 :            :                 {
      47                 :            :                         .action = "\"action\":1",
      48                 :            :                         .cmd = CLIENTS_CMD,
      49                 :            :                         .data = CLIENTS_DATA,
      50                 :            :                         .fn = register_client
      51                 :            :                 }
      52                 :            : };
      53                 :            : int num_legacy_callbacks = 1;
      54                 :            : static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
      55                 :            : 
      56                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_telemetry_legacy_register)
      57                 :            : int
      58                 :        756 : rte_telemetry_legacy_register(const char *cmd,
      59                 :            :                 enum rte_telemetry_legacy_data_req data_req,
      60                 :            :                 telemetry_legacy_cb fn)
      61                 :            : {
      62         [ +  - ]:        756 :         if (fn == NULL)
      63                 :            :                 return -EINVAL;
      64         [ +  - ]:        756 :         if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
      65                 :            :                 return -ENOENT;
      66                 :            : 
      67                 :            :         rte_spinlock_lock(&callback_sl);
      68         [ +  + ]:        756 :         strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
      69                 :        756 :         snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
      70                 :            :                         "\"command\":\"%s\"", cmd);
      71         [ +  + ]:       1764 :         snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
      72                 :            :                         data_req ? "%s{\"" : "%snull",
      73                 :            :                         DATA_REQ_LABEL);
      74                 :        756 :         callbacks[num_legacy_callbacks].fn = fn;
      75                 :        756 :         num_legacy_callbacks++;
      76                 :            :         rte_spinlock_unlock(&callback_sl);
      77                 :            : 
      78                 :        756 :         return 0;
      79                 :            : }
      80                 :            : 
      81                 :            : static int
      82                 :          0 : register_client(const char *cmd __rte_unused, const char *params,
      83                 :            :                 char *buffer __rte_unused, int buf_len __rte_unused)
      84                 :            : {
      85                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      86                 :            :         pthread_t th;
      87                 :            :         char data[BUF_SIZE];
      88                 :            :         int fd;
      89                 :            :         int rc;
      90                 :            :         struct sockaddr_un addrs;
      91                 :            : #endif /* !RTE_EXEC_ENV_WINDOWS */
      92                 :            : 
      93         [ #  # ]:          0 :         if (!strchr(params, ':')) {
      94                 :          0 :                 fprintf(stderr, "Invalid data\n");
      95                 :          0 :                 return -1;
      96                 :            :         }
      97                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      98                 :            :         strlcpy(data, strchr(params, ':'), sizeof(data));
      99         [ #  # ]:          0 :         memmove(data, &data[strlen(":\"")], strlen(data));
     100         [ #  # ]:          0 :         if (!strchr(data, '\"')) {
     101                 :          0 :                 fprintf(stderr, "Invalid client data\n");
     102                 :          0 :                 return -1;
     103                 :            :         }
     104                 :          0 :         *strchr(data, '\"') = 0;
     105                 :            : 
     106                 :          0 :         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
     107         [ #  # ]:          0 :         if (fd < 0) {
     108                 :          0 :                 perror("Failed to open socket");
     109                 :          0 :                 return -1;
     110                 :            :         }
     111                 :          0 :         addrs.sun_family = AF_UNIX;
     112                 :            :         strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
     113                 :            : 
     114         [ #  # ]:          0 :         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
     115                 :          0 :                 perror("\nClient connection error\n");
     116                 :          0 :                 close(fd);
     117                 :          0 :                 return -1;
     118                 :            :         }
     119                 :          0 :         rc = pthread_create(&th, NULL, &legacy_client_handler,
     120                 :          0 :                                 (void *)(uintptr_t)fd);
     121         [ #  # ]:          0 :         if (rc != 0) {
     122                 :          0 :                 fprintf(stderr, "Failed to create legacy client thread: %s\n",
     123                 :            :                         strerror(rc));
     124                 :          0 :                 close(fd);
     125                 :          0 :                 return -1;
     126                 :            :         }
     127                 :          0 :         pthread_detach(th);
     128                 :            : #endif /* !RTE_EXEC_ENV_WINDOWS */
     129                 :          0 :         return 0;
     130                 :            : }
     131                 :            : 
     132                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
     133                 :            : 
     134                 :            : static int
     135                 :          0 : send_error_response(int s, int err)
     136                 :            : {
     137                 :            :         const char *desc;
     138                 :            :         char out_buf[100000];
     139                 :            : 
     140   [ #  #  #  # ]:          0 :         switch (err) {
     141                 :            :         case -ENOMEM:
     142                 :            :                 desc = "Memory Allocation Error";
     143                 :            :                 break;
     144                 :          0 :         case -EINVAL:
     145                 :            :                 desc = "Invalid Argument 404";
     146                 :          0 :                 break;
     147                 :          0 :         case -EPERM:
     148                 :            :                 desc = "Unknown";
     149                 :          0 :                 break;
     150                 :            :         default:
     151                 :            :                 /* Default case keeps behaviour of Telemetry library */
     152                 :            :                 printf("\nInvalid error type: %d\n", err);
     153                 :          0 :                 return -EINVAL;
     154                 :            :         }
     155                 :            :         int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
     156                 :            :                         "\"Status Error: %s\", \"data\": null}", desc);
     157         [ #  # ]:          0 :         if (write(s, out_buf, used) < 0) {
     158                 :          0 :                 perror("Error writing to socket");
     159                 :          0 :                 return -1;
     160                 :            :         }
     161                 :            :         return 0;
     162                 :            : }
     163                 :            : 
     164                 :            : static void
     165                 :          0 : perform_command(telemetry_legacy_cb fn, const char *param, int s)
     166                 :            : {
     167                 :            :         char out_buf[100000];
     168                 :            :         int ret, used = 0;
     169                 :            : 
     170                 :          0 :         ret = fn("", param, out_buf, sizeof(out_buf));
     171         [ #  # ]:          0 :         if (ret < 0) {
     172                 :          0 :                 ret = send_error_response(s, ret);
     173         [ #  # ]:          0 :                 if (ret < 0)
     174                 :            :                         printf("\nCould not send error response\n");
     175                 :          0 :                 return;
     176                 :            :         }
     177                 :            :         used += ret;
     178         [ #  # ]:          0 :         if (write(s, out_buf, used) < 0)
     179                 :          0 :                 perror("Error writing to socket");
     180                 :            : }
     181                 :            : 
     182                 :            : static int
     183                 :          0 : parse_client_request(char *buffer, int buf_len, int s)
     184                 :            : {
     185                 :            :         int i;
     186                 :          0 :         char *data = buffer + buf_len;
     187                 :            :         telemetry_legacy_cb fn = NULL;
     188                 :            :         const char *valid_sep = ",}";
     189   [ #  #  #  # ]:          0 :         if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
     190                 :            :                 return -EPERM;
     191                 :            : 
     192   [ #  #  #  # ]:          0 :         if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
     193         [ #  # ]:          0 :                         && strstr(buffer, CLIENTS_DATA))
     194                 :            :                 return 0;
     195                 :            : 
     196         [ #  # ]:          0 :         for (i = 0; i < num_legacy_callbacks; i++) {
     197                 :          0 :                 char *action_ptr = strstr(buffer, callbacks[i].action);
     198                 :          0 :                 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
     199                 :          0 :                 char *data_ptr = strstr(buffer, callbacks[i].data);
     200   [ #  #  #  # ]:          0 :                 if (!action_ptr || !cmd_ptr || !data_ptr)
     201                 :            :                         continue;
     202                 :            : 
     203                 :          0 :                 char action_sep = action_ptr[strlen(callbacks[i].action)];
     204                 :          0 :                 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
     205   [ #  #  #  # ]:          0 :                 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
     206                 :            :                                 cmd_sep)))
     207                 :            :                         return -EPERM;
     208                 :            :                 char data_sep;
     209                 :            : 
     210         [ #  # ]:          0 :                 if (!strchr(data_ptr, '{'))
     211                 :          0 :                         data_sep = data_ptr[strlen(callbacks[i].data)];
     212                 :            :                 else {
     213         [ #  # ]:          0 :                         if (!strchr(data_ptr, '}'))
     214                 :            :                                 return -EINVAL;
     215                 :            :                         char *data_end = strchr(data_ptr, '}');
     216                 :          0 :                         data = data_ptr + strlen(DATA_REQ_LABEL);
     217                 :          0 :                         data_sep = data_end[1];
     218                 :          0 :                         data_end[1] = 0;
     219                 :            :                 }
     220         [ #  # ]:          0 :                 if (!strchr(valid_sep, data_sep))
     221                 :            :                         return -EPERM;
     222                 :          0 :                 fn = callbacks[i].fn;
     223                 :          0 :                 break;
     224                 :            :         }
     225                 :            : 
     226         [ #  # ]:          0 :         if (!fn)
     227                 :            :                 return -EINVAL;
     228                 :          0 :         perform_command(fn, data, s);
     229                 :          0 :         return 0;
     230                 :            : }
     231                 :            : 
     232                 :            : void *
     233                 :          4 : legacy_client_handler(void *sock_id)
     234                 :            : {
     235                 :          4 :         int s = (int)(uintptr_t)sock_id;
     236                 :            :         int ret;
     237                 :            :         char buffer_recv[BUF_SIZE];
     238                 :            :         /* receive data is not null terminated */
     239                 :          4 :         int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
     240                 :            : 
     241         [ -  + ]:          4 :         while (bytes > 0) {
     242                 :          0 :                 buffer_recv[bytes] = 0;
     243                 :            :                 int i, j;
     244                 :            :                 char buffer[BUF_SIZE];
     245         [ #  # ]:          0 :                 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
     246                 :          0 :                         buffer[j] = buffer_recv[i];
     247                 :          0 :                         j += !isspace(buffer_recv[i]);
     248                 :            :                 }
     249                 :          0 :                 buffer[j] = 0;
     250                 :          0 :                 ret = parse_client_request(buffer, j, s);
     251         [ #  # ]:          0 :                 if (ret < 0) {
     252                 :          0 :                         ret = send_error_response(s, ret);
     253         [ #  # ]:          0 :                         if (ret < 0)
     254                 :            :                                 printf("\nCould not send error response\n");
     255                 :            :                 }
     256                 :          0 :                 bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
     257                 :            :         }
     258                 :          4 :         close(s);
     259                 :          4 :         return NULL;
     260                 :            : }
     261                 :            : 
     262                 :            : #endif /* !RTE_EXEC_ENV_WINDOWS */

Generated by: LCOV version 1.14