telemetry: build stubs on Windows
[dpdk.git] / lib / librte_telemetry / telemetry.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #ifndef RTE_EXEC_ENV_WINDOWS
6 #include <unistd.h>
7 #include <pthread.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <dlfcn.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 <rte_string_fns.h>
16 #include <rte_common.h>
17 #include <rte_spinlock.h>
18 #include <rte_version.h>
19
20 #include "rte_telemetry.h"
21 #include "telemetry_json.h"
22 #include "telemetry_data.h"
23 #include "rte_telemetry_legacy.h"
24
25 #define MAX_CMD_LEN 56
26 #define MAX_HELP_LEN 64
27 #define MAX_OUTPUT_LEN (1024 * 16)
28 #define MAX_CONNECTIONS 10
29
30 #ifndef RTE_EXEC_ENV_WINDOWS
31 static void *
32 client_handler(void *socket);
33 #endif /* !RTE_EXEC_ENV_WINDOWS */
34
35 struct cmd_callback {
36         char cmd[MAX_CMD_LEN];
37         telemetry_cb fn;
38         char help[MAX_HELP_LEN];
39 };
40
41 #ifndef RTE_EXEC_ENV_WINDOWS
42 struct socket {
43         int sock;
44         char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
45         handler fn;
46         uint16_t *num_clients;
47 };
48 static struct socket v2_socket; /* socket for v2 telemetry */
49 static struct socket v1_socket; /* socket for v1 telemetry */
50 #endif /* !RTE_EXEC_ENV_WINDOWS */
51 static char telemetry_log_error[1024]; /* Will contain error on init failure */
52 /* list of command callbacks, with one command registered by default */
53 static struct cmd_callback callbacks[TELEMETRY_MAX_CALLBACKS];
54 static int num_callbacks; /* How many commands are registered */
55 /* Used when accessing or modifying list of command callbacks */
56 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
57 #ifndef RTE_EXEC_ENV_WINDOWS
58 static uint16_t v2_clients;
59 #endif /* !RTE_EXEC_ENV_WINDOWS */
60
61 int
62 rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help)
63 {
64         int i = 0;
65
66         if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/'
67                         || strlen(help) >= MAX_HELP_LEN)
68                 return -EINVAL;
69         if (num_callbacks >= TELEMETRY_MAX_CALLBACKS)
70                 return -ENOENT;
71
72         rte_spinlock_lock(&callback_sl);
73         while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0)
74                 i++;
75         if (i != num_callbacks)
76                 /* Move elements to keep the list alphabetical */
77                 memmove(callbacks + i + 1, callbacks + i,
78                         sizeof(struct cmd_callback) * (num_callbacks - i));
79
80         strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN);
81         callbacks[i].fn = fn;
82         strlcpy(callbacks[i].help, help, MAX_HELP_LEN);
83         num_callbacks++;
84         rte_spinlock_unlock(&callback_sl);
85
86         return 0;
87 }
88
89 #ifndef RTE_EXEC_ENV_WINDOWS
90
91 static int
92 list_commands(const char *cmd __rte_unused, const char *params __rte_unused,
93                 struct rte_tel_data *d)
94 {
95         int i;
96
97         rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
98         for (i = 0; i < num_callbacks; i++)
99                 rte_tel_data_add_array_string(d, callbacks[i].cmd);
100         return 0;
101 }
102
103 static int
104 json_info(const char *cmd __rte_unused, const char *params __rte_unused,
105                 struct rte_tel_data *d)
106 {
107         rte_tel_data_start_dict(d);
108         rte_tel_data_add_dict_string(d, "version", rte_version());
109         rte_tel_data_add_dict_int(d, "pid", getpid());
110         rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN);
111         return 0;
112 }
113
114 static int
115 command_help(const char *cmd __rte_unused, const char *params,
116                 struct rte_tel_data *d)
117 {
118         int i;
119
120         if (!params)
121                 return -1;
122         rte_tel_data_start_dict(d);
123         rte_spinlock_lock(&callback_sl);
124         for (i = 0; i < num_callbacks; i++)
125                 if (strcmp(params, callbacks[i].cmd) == 0) {
126                         rte_tel_data_add_dict_string(d, params,
127                                         callbacks[i].help);
128                         break;
129                 }
130         rte_spinlock_unlock(&callback_sl);
131         if (i == num_callbacks)
132                 return -1;
133         return 0;
134 }
135
136 static void
137 output_json(const char *cmd, const struct rte_tel_data *d, int s)
138 {
139         char out_buf[MAX_OUTPUT_LEN];
140
141         char *cb_data_buf;
142         size_t buf_len, prefix_used, used = 0;
143         unsigned int i;
144
145         RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN +
146                         RTE_TEL_MAX_SINGLE_STRING_LEN + 10);
147         switch (d->type) {
148         case RTE_TEL_NULL:
149                 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
150                                 MAX_CMD_LEN, cmd ? cmd : "none");
151                 break;
152         case RTE_TEL_STRING:
153                 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}",
154                                 MAX_CMD_LEN, cmd,
155                                 RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str);
156                 break;
157         case RTE_TEL_DICT:
158                 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
159                                 MAX_CMD_LEN, cmd);
160                 cb_data_buf = &out_buf[prefix_used];
161                 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
162
163                 used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0);
164                 for (i = 0; i < d->data_len; i++) {
165                         const struct tel_dict_entry *v = &d->data.dict[i];
166                         switch (v->type) {
167                         case RTE_TEL_STRING_VAL:
168                                 used = rte_tel_json_add_obj_str(cb_data_buf,
169                                                 buf_len, used,
170                                                 v->name, v->value.sval);
171                                 break;
172                         case RTE_TEL_INT_VAL:
173                                 used = rte_tel_json_add_obj_int(cb_data_buf,
174                                                 buf_len, used,
175                                                 v->name, v->value.ival);
176                                 break;
177                         case RTE_TEL_U64_VAL:
178                                 used = rte_tel_json_add_obj_u64(cb_data_buf,
179                                                 buf_len, used,
180                                                 v->name, v->value.u64val);
181                                 break;
182                         }
183                 }
184                 used += prefix_used;
185                 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
186                 break;
187         case RTE_TEL_ARRAY_STRING:
188         case RTE_TEL_ARRAY_INT:
189         case RTE_TEL_ARRAY_U64:
190                 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
191                                 MAX_CMD_LEN, cmd);
192                 cb_data_buf = &out_buf[prefix_used];
193                 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
194
195                 used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0);
196                 for (i = 0; i < d->data_len; i++)
197                         if (d->type == RTE_TEL_ARRAY_STRING)
198                                 used = rte_tel_json_add_array_string(
199                                                 cb_data_buf,
200                                                 buf_len, used,
201                                                 d->data.array[i].sval);
202                         else if (d->type == RTE_TEL_ARRAY_INT)
203                                 used = rte_tel_json_add_array_int(cb_data_buf,
204                                                 buf_len, used,
205                                                 d->data.array[i].ival);
206                         else if (d->type == RTE_TEL_ARRAY_U64)
207                                 used = rte_tel_json_add_array_u64(cb_data_buf,
208                                                 buf_len, used,
209                                                 d->data.array[i].u64val);
210                 used += prefix_used;
211                 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
212                 break;
213         }
214         if (write(s, out_buf, used) < 0)
215                 perror("Error writing to socket");
216 }
217
218 static void
219 perform_command(telemetry_cb fn, const char *cmd, const char *param, int s)
220 {
221         struct rte_tel_data data;
222
223         int ret = fn(cmd, param, &data);
224         if (ret < 0) {
225                 char out_buf[MAX_CMD_LEN + 10];
226                 int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
227                                 MAX_CMD_LEN, cmd ? cmd : "none");
228                 if (write(s, out_buf, used) < 0)
229                         perror("Error writing to socket");
230                 return;
231         }
232         output_json(cmd, &data, s);
233 }
234
235 static int
236 unknown_command(const char *cmd __rte_unused, const char *params __rte_unused,
237                 struct rte_tel_data *d)
238 {
239         return d->type = RTE_TEL_NULL;
240 }
241
242 static void *
243 client_handler(void *sock_id)
244 {
245         int s = (int)(uintptr_t)sock_id;
246         char buffer[1024];
247         char info_str[1024];
248         snprintf(info_str, sizeof(info_str),
249                         "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}",
250                         rte_version(), getpid(), MAX_OUTPUT_LEN);
251         if (write(s, info_str, strlen(info_str)) < 0) {
252                 close(s);
253                 return NULL;
254         }
255
256         /* receive data is not null terminated */
257         int bytes = read(s, buffer, sizeof(buffer) - 1);
258         while (bytes > 0) {
259                 buffer[bytes] = 0;
260                 const char *cmd = strtok(buffer, ",");
261                 const char *param = strtok(NULL, ",");
262                 telemetry_cb fn = unknown_command;
263                 int i;
264
265                 if (cmd && strlen(cmd) < MAX_CMD_LEN) {
266                         rte_spinlock_lock(&callback_sl);
267                         for (i = 0; i < num_callbacks; i++)
268                                 if (strcmp(cmd, callbacks[i].cmd) == 0) {
269                                         fn = callbacks[i].fn;
270                                         break;
271                                 }
272                         rte_spinlock_unlock(&callback_sl);
273                 }
274                 perform_command(fn, cmd, param, s);
275
276                 bytes = read(s, buffer, sizeof(buffer) - 1);
277         }
278         close(s);
279         __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED);
280         return NULL;
281 }
282
283 static void *
284 socket_listener(void *socket)
285 {
286         while (1) {
287                 pthread_t th;
288                 struct socket *s = (struct socket *)socket;
289                 int s_accepted = accept(s->sock, NULL, NULL);
290                 if (s_accepted < 0) {
291                         snprintf(telemetry_log_error,
292                                 sizeof(telemetry_log_error),
293                                 "Error with accept, telemetry thread quitting");
294                         return NULL;
295                 }
296                 if (s->num_clients != NULL) {
297                         uint16_t conns = __atomic_load_n(s->num_clients,
298                                         __ATOMIC_RELAXED);
299                         if (conns >= MAX_CONNECTIONS) {
300                                 close(s_accepted);
301                                 continue;
302                         }
303                         __atomic_add_fetch(s->num_clients, 1,
304                                         __ATOMIC_RELAXED);
305                 }
306                 pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
307                 pthread_detach(th);
308         }
309         return NULL;
310 }
311
312 static inline char *
313 get_socket_path(const char *runtime_dir, const int version)
314 {
315         static char path[PATH_MAX];
316         snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d",
317                         strlen(runtime_dir) ? runtime_dir : "/tmp", version);
318         return path;
319 }
320
321 static void
322 unlink_sockets(void)
323 {
324         if (v2_socket.path[0])
325                 unlink(v2_socket.path);
326         if (v1_socket.path[0])
327                 unlink(v1_socket.path);
328 }
329
330 static int
331 create_socket(char *path)
332 {
333         int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
334         if (sock < 0) {
335                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
336                                 "Error with socket creation, %s",
337                                 strerror(errno));
338                 return -1;
339         }
340
341         struct sockaddr_un sun = {.sun_family = AF_UNIX};
342         strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
343         unlink(sun.sun_path);
344         if (bind(sock, (void *) &sun, sizeof(sun)) < 0) {
345                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
346                                 "Error binding socket: %s",
347                                 strerror(errno));
348                 sun.sun_path[0] = 0;
349                 goto error;
350         }
351
352         if (listen(sock, 1) < 0) {
353                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
354                                 "Error calling listen for socket: %s",
355                                 strerror(errno));
356                 goto error;
357         }
358
359         return sock;
360
361 error:
362         close(sock);
363         unlink_sockets();
364         return -1;
365 }
366
367 static int
368 telemetry_legacy_init(const char *runtime_dir, rte_cpuset_t *cpuset)
369 {
370         pthread_t t_old;
371
372         if (num_legacy_callbacks == 1) {
373                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
374                          "No legacy callbacks, legacy socket not created");
375                 return -1;
376         }
377
378         v1_socket.fn = legacy_client_handler;
379         if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path),
380                         "%s/telemetry", runtime_dir)
381                         >= sizeof(v1_socket.path)) {
382                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
383                                 "Error with socket binding, path too long");
384                 return -1;
385         }
386         v1_socket.sock = create_socket(v1_socket.path);
387         if (v1_socket.sock < 0)
388                 return -1;
389         pthread_create(&t_old, NULL, socket_listener, &v1_socket);
390         pthread_setaffinity_np(t_old, sizeof(*cpuset), cpuset);
391
392         return 0;
393 }
394
395 static int
396 telemetry_v2_init(const char *runtime_dir, rte_cpuset_t *cpuset)
397 {
398         pthread_t t_new;
399
400         v2_socket.num_clients = &v2_clients;
401         rte_telemetry_register_cmd("/", list_commands,
402                         "Returns list of available commands, Takes no parameters");
403         rte_telemetry_register_cmd("/info", json_info,
404                         "Returns DPDK Telemetry information. Takes no parameters");
405         rte_telemetry_register_cmd("/help", command_help,
406                         "Returns help text for a command. Parameters: string command");
407         v2_socket.fn = client_handler;
408         if (strlcpy(v2_socket.path, get_socket_path(runtime_dir, 2),
409                         sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) {
410                 snprintf(telemetry_log_error, sizeof(telemetry_log_error),
411                                 "Error with socket binding, path too long");
412                 return -1;
413         }
414
415         v2_socket.sock = create_socket(v2_socket.path);
416         if (v2_socket.sock < 0)
417                 return -1;
418         pthread_create(&t_new, NULL, socket_listener, &v2_socket);
419         pthread_setaffinity_np(t_new, sizeof(*cpuset), cpuset);
420         atexit(unlink_sockets);
421
422         return 0;
423 }
424
425 #endif /* !RTE_EXEC_ENV_WINDOWS */
426
427 int32_t
428 rte_telemetry_init(const char *runtime_dir, rte_cpuset_t *cpuset,
429                 const char **err_str)
430 {
431 #ifndef RTE_EXEC_ENV_WINDOWS
432         if (telemetry_v2_init(runtime_dir, cpuset) != 0) {
433                 *err_str = telemetry_log_error;
434                 return -1;
435         }
436         if (telemetry_legacy_init(runtime_dir, cpuset) != 0) {
437                 *err_str = telemetry_log_error;
438         }
439 #else /* RTE_EXEC_ENV_WINDOWS */
440         RTE_SET_USED(runtime_dir);
441         RTE_SET_USED(cpuset);
442         RTE_SET_USED(err_str);
443
444         snprintf(telemetry_log_error, sizeof(telemetry_log_error),
445                 "DPDK Telemetry is not supported on Windows.");
446 #endif /* RTE_EXEC_ENV_WINDOWS */
447
448         return 0;
449 }