1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #ifndef RTE_EXEC_ENV_WINDOWS
8 #include <sys/socket.h>
11 #endif /* !RTE_EXEC_ENV_WINDOWS */
13 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
15 #include <rte_string_fns.h>
16 #include <rte_common.h>
17 #include <rte_spinlock.h>
20 #include "rte_telemetry.h"
21 #include "telemetry_json.h"
22 #include "telemetry_data.h"
23 #include "telemetry_internal.h"
25 #define MAX_CMD_LEN 56
26 #define MAX_HELP_LEN 64
27 #define MAX_OUTPUT_LEN (1024 * 16)
28 #define MAX_CONNECTIONS 10
30 #ifndef RTE_EXEC_ENV_WINDOWS
32 client_handler(void *socket);
33 #endif /* !RTE_EXEC_ENV_WINDOWS */
36 char cmd[MAX_CMD_LEN];
38 char help[MAX_HELP_LEN];
41 #ifndef RTE_EXEC_ENV_WINDOWS
44 char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
46 uint16_t *num_clients;
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 */
52 static const char *telemetry_version; /* save rte_version */
53 static const char *socket_dir; /* runtime directory */
54 static rte_cpuset_t *thread_cpuset;
55 static rte_log_fn rte_log_ptr;
56 static uint32_t logtype;
58 #define TMTY_LOG(l, ...) \
59 rte_log_ptr(RTE_LOG_ ## l, logtype, "TELEMETRY: " __VA_ARGS__)
61 /* list of command callbacks, with one command registered by default */
62 static struct cmd_callback *callbacks;
63 static int num_callbacks; /* How many commands are registered */
64 /* Used when accessing or modifying list of command callbacks */
65 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
66 #ifndef RTE_EXEC_ENV_WINDOWS
67 static uint16_t v2_clients;
68 #endif /* !RTE_EXEC_ENV_WINDOWS */
71 rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help)
73 struct cmd_callback *new_callbacks;
76 if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/'
77 || strlen(help) >= MAX_HELP_LEN)
80 rte_spinlock_lock(&callback_sl);
81 new_callbacks = realloc(callbacks, sizeof(callbacks[0]) * (num_callbacks + 1));
82 if (new_callbacks == NULL) {
83 rte_spinlock_unlock(&callback_sl);
86 callbacks = new_callbacks;
88 while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0)
90 if (i != num_callbacks)
91 /* Move elements to keep the list alphabetical */
92 memmove(callbacks + i + 1, callbacks + i,
93 sizeof(struct cmd_callback) * (num_callbacks - i));
95 strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN);
97 strlcpy(callbacks[i].help, help, MAX_HELP_LEN);
99 rte_spinlock_unlock(&callback_sl);
104 #ifndef RTE_EXEC_ENV_WINDOWS
107 list_commands(const char *cmd __rte_unused, const char *params __rte_unused,
108 struct rte_tel_data *d)
112 rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
113 rte_spinlock_lock(&callback_sl);
114 for (i = 0; i < num_callbacks; i++)
115 rte_tel_data_add_array_string(d, callbacks[i].cmd);
116 rte_spinlock_unlock(&callback_sl);
121 json_info(const char *cmd __rte_unused, const char *params __rte_unused,
122 struct rte_tel_data *d)
124 rte_tel_data_start_dict(d);
125 rte_tel_data_add_dict_string(d, "version", telemetry_version);
126 rte_tel_data_add_dict_int(d, "pid", getpid());
127 rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN);
132 command_help(const char *cmd __rte_unused, const char *params,
133 struct rte_tel_data *d)
139 rte_tel_data_start_dict(d);
140 rte_spinlock_lock(&callback_sl);
141 for (i = 0; i < num_callbacks; i++)
142 if (strcmp(params, callbacks[i].cmd) == 0) {
143 rte_tel_data_add_dict_string(d, params,
147 rte_spinlock_unlock(&callback_sl);
148 if (i == num_callbacks)
154 container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len)
159 if (d->type != RTE_TEL_ARRAY_U64 && d->type != RTE_TEL_ARRAY_INT
160 && d->type != RTE_TEL_ARRAY_STRING)
161 return snprintf(out_buf, buf_len, "null");
163 used = rte_tel_json_empty_array(out_buf, buf_len, 0);
164 if (d->type == RTE_TEL_ARRAY_U64)
165 for (i = 0; i < d->data_len; i++)
166 used = rte_tel_json_add_array_u64(out_buf,
168 d->data.array[i].u64val);
169 if (d->type == RTE_TEL_ARRAY_INT)
170 for (i = 0; i < d->data_len; i++)
171 used = rte_tel_json_add_array_int(out_buf,
173 d->data.array[i].ival);
174 if (d->type == RTE_TEL_ARRAY_STRING)
175 for (i = 0; i < d->data_len; i++)
176 used = rte_tel_json_add_array_string(out_buf,
178 d->data.array[i].sval);
183 output_json(const char *cmd, const struct rte_tel_data *d, int s)
185 char out_buf[MAX_OUTPUT_LEN];
188 size_t buf_len, prefix_used, used = 0;
191 RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN +
192 RTE_TEL_MAX_SINGLE_STRING_LEN + 10);
195 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
196 MAX_CMD_LEN, cmd ? cmd : "none");
199 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}",
201 RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str);
204 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
206 cb_data_buf = &out_buf[prefix_used];
207 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
209 used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0);
210 for (i = 0; i < d->data_len; i++) {
211 const struct tel_dict_entry *v = &d->data.dict[i];
213 case RTE_TEL_STRING_VAL:
214 used = rte_tel_json_add_obj_str(cb_data_buf,
216 v->name, v->value.sval);
218 case RTE_TEL_INT_VAL:
219 used = rte_tel_json_add_obj_int(cb_data_buf,
221 v->name, v->value.ival);
223 case RTE_TEL_U64_VAL:
224 used = rte_tel_json_add_obj_u64(cb_data_buf,
226 v->name, v->value.u64val);
228 case RTE_TEL_CONTAINER:
231 const struct container *cont =
233 if (container_to_json(cont->data,
235 used = rte_tel_json_add_obj_json(
240 rte_tel_data_free(cont->data);
245 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
247 case RTE_TEL_ARRAY_STRING:
248 case RTE_TEL_ARRAY_INT:
249 case RTE_TEL_ARRAY_U64:
250 case RTE_TEL_ARRAY_CONTAINER:
251 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
253 cb_data_buf = &out_buf[prefix_used];
254 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
256 used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0);
257 for (i = 0; i < d->data_len; i++)
258 if (d->type == RTE_TEL_ARRAY_STRING)
259 used = rte_tel_json_add_array_string(
262 d->data.array[i].sval);
263 else if (d->type == RTE_TEL_ARRAY_INT)
264 used = rte_tel_json_add_array_int(cb_data_buf,
266 d->data.array[i].ival);
267 else if (d->type == RTE_TEL_ARRAY_U64)
268 used = rte_tel_json_add_array_u64(cb_data_buf,
270 d->data.array[i].u64val);
271 else if (d->type == RTE_TEL_ARRAY_CONTAINER) {
273 const struct container *rec_data =
274 &d->data.array[i].container;
275 if (container_to_json(rec_data->data,
277 used = rte_tel_json_add_array_json(
279 buf_len, used, temp);
281 rte_tel_data_free(rec_data->data);
284 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
287 if (write(s, out_buf, used) < 0)
288 perror("Error writing to socket");
292 perform_command(telemetry_cb fn, const char *cmd, const char *param, int s)
294 struct rte_tel_data data;
296 int ret = fn(cmd, param, &data);
298 char out_buf[MAX_CMD_LEN + 10];
299 int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
300 MAX_CMD_LEN, cmd ? cmd : "none");
301 if (write(s, out_buf, used) < 0)
302 perror("Error writing to socket");
305 output_json(cmd, &data, s);
309 unknown_command(const char *cmd __rte_unused, const char *params __rte_unused,
310 struct rte_tel_data *d)
312 return d->type = RTE_TEL_NULL;
316 client_handler(void *sock_id)
318 int s = (int)(uintptr_t)sock_id;
321 snprintf(info_str, sizeof(info_str),
322 "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}",
323 telemetry_version, getpid(), MAX_OUTPUT_LEN);
324 if (write(s, info_str, strlen(info_str)) < 0) {
329 /* receive data is not null terminated */
330 int bytes = read(s, buffer, sizeof(buffer) - 1);
333 const char *cmd = strtok(buffer, ",");
334 const char *param = strtok(NULL, "\0");
335 telemetry_cb fn = unknown_command;
338 if (cmd && strlen(cmd) < MAX_CMD_LEN) {
339 rte_spinlock_lock(&callback_sl);
340 for (i = 0; i < num_callbacks; i++)
341 if (strcmp(cmd, callbacks[i].cmd) == 0) {
342 fn = callbacks[i].fn;
345 rte_spinlock_unlock(&callback_sl);
347 perform_command(fn, cmd, param, s);
349 bytes = read(s, buffer, sizeof(buffer) - 1);
352 __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED);
357 socket_listener(void *socket)
362 struct socket *s = (struct socket *)socket;
363 int s_accepted = accept(s->sock, NULL, NULL);
364 if (s_accepted < 0) {
365 TMTY_LOG(ERR, "Error with accept, telemetry thread quitting\n");
368 if (s->num_clients != NULL) {
369 uint16_t conns = __atomic_load_n(s->num_clients,
371 if (conns >= MAX_CONNECTIONS) {
375 __atomic_add_fetch(s->num_clients, 1,
378 rc = pthread_create(&th, NULL, s->fn,
379 (void *)(uintptr_t)s_accepted);
381 TMTY_LOG(ERR, "Error with create client thread: %s\n",
384 if (s->num_clients != NULL)
385 __atomic_sub_fetch(s->num_clients, 1,
395 get_socket_path(const char *runtime_dir, const int version)
397 static char path[PATH_MAX];
398 snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d",
399 strlen(runtime_dir) ? runtime_dir : "/tmp", version);
406 if (v2_socket.path[0])
407 unlink(v2_socket.path);
408 if (v1_socket.path[0])
409 unlink(v1_socket.path);
413 create_socket(char *path)
415 int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
417 TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno));
421 struct sockaddr_un sun = {.sun_family = AF_UNIX};
422 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
423 unlink(sun.sun_path);
424 if (bind(sock, (void *) &sun, sizeof(sun)) < 0) {
425 TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno));
430 if (listen(sock, 1) < 0) {
431 TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno));
444 set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused)
446 #if defined RTE_EXEC_ENV_LINUX && defined __GLIBC__ && defined __GLIBC_PREREQ
447 #if __GLIBC_PREREQ(2, 12)
448 pthread_setname_np(id, name);
450 #elif defined RTE_EXEC_ENV_FREEBSD
451 pthread_set_name_np(id, name);
456 telemetry_legacy_init(void)
461 if (num_legacy_callbacks == 1) {
462 TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
466 v1_socket.fn = legacy_client_handler;
467 if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path),
468 "%s/telemetry", socket_dir) >= sizeof(v1_socket.path)) {
469 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
472 v1_socket.sock = create_socket(v1_socket.path);
473 if (v1_socket.sock < 0)
475 rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
477 TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n",
479 close(v1_socket.sock);
481 unlink(v1_socket.path);
482 v1_socket.path[0] = '\0';
485 pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
486 set_thread_name(t_old, "telemetry-v1");
487 TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
492 telemetry_v2_init(void)
497 v2_socket.num_clients = &v2_clients;
498 rte_telemetry_register_cmd("/", list_commands,
499 "Returns list of available commands, Takes no parameters");
500 rte_telemetry_register_cmd("/info", json_info,
501 "Returns DPDK Telemetry information. Takes no parameters");
502 rte_telemetry_register_cmd("/help", command_help,
503 "Returns help text for a command. Parameters: string command");
504 v2_socket.fn = client_handler;
505 if (strlcpy(v2_socket.path, get_socket_path(socket_dir, 2),
506 sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) {
507 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
511 v2_socket.sock = create_socket(v2_socket.path);
512 if (v2_socket.sock < 0)
514 rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
516 TMTY_LOG(ERR, "Error with create socket thread: %s\n",
518 close(v2_socket.sock);
520 unlink(v2_socket.path);
521 v2_socket.path[0] = '\0';
524 pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
525 set_thread_name(t_new, "telemetry-v2");
526 atexit(unlink_sockets);
531 #endif /* !RTE_EXEC_ENV_WINDOWS */
534 rte_telemetry_init(const char *runtime_dir, const char *rte_version, rte_cpuset_t *cpuset,
535 rte_log_fn log_fn, uint32_t registered_logtype)
537 telemetry_version = rte_version;
538 socket_dir = runtime_dir;
539 thread_cpuset = cpuset;
540 rte_log_ptr = log_fn;
541 logtype = registered_logtype;
543 #ifndef RTE_EXEC_ENV_WINDOWS
544 if (telemetry_v2_init() != 0)
546 TMTY_LOG(DEBUG, "Telemetry initialized ok\n");
547 telemetry_legacy_init();
548 #endif /* RTE_EXEC_ENV_WINDOWS */