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