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 /** Maximum number of telemetry callbacks. */
31 #define TELEMETRY_MAX_CALLBACKS 64
33 #ifndef RTE_EXEC_ENV_WINDOWS
35 client_handler(void *socket);
36 #endif /* !RTE_EXEC_ENV_WINDOWS */
39 char cmd[MAX_CMD_LEN];
41 char help[MAX_HELP_LEN];
44 #ifndef RTE_EXEC_ENV_WINDOWS
47 char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
49 uint16_t *num_clients;
51 static struct socket v2_socket; /* socket for v2 telemetry */
52 static struct socket v1_socket; /* socket for v1 telemetry */
53 #endif /* !RTE_EXEC_ENV_WINDOWS */
55 static const char *telemetry_version; /* save rte_version */
56 static const char *socket_dir; /* runtime directory */
57 static rte_cpuset_t *thread_cpuset;
58 static rte_log_fn rte_log_ptr;
59 static uint32_t logtype;
61 #define TMTY_LOG(l, ...) \
62 rte_log_ptr(RTE_LOG_ ## l, logtype, "TELEMETRY: " __VA_ARGS__)
64 /* list of command callbacks, with one command registered by default */
65 static struct cmd_callback callbacks[TELEMETRY_MAX_CALLBACKS];
66 static int num_callbacks; /* How many commands are registered */
67 /* Used when accessing or modifying list of command callbacks */
68 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
69 #ifndef RTE_EXEC_ENV_WINDOWS
70 static uint16_t v2_clients;
71 #endif /* !RTE_EXEC_ENV_WINDOWS */
74 rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help)
78 if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/'
79 || strlen(help) >= MAX_HELP_LEN)
81 if (num_callbacks >= TELEMETRY_MAX_CALLBACKS)
84 rte_spinlock_lock(&callback_sl);
85 while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0)
87 if (i != num_callbacks)
88 /* Move elements to keep the list alphabetical */
89 memmove(callbacks + i + 1, callbacks + i,
90 sizeof(struct cmd_callback) * (num_callbacks - i));
92 strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN);
94 strlcpy(callbacks[i].help, help, MAX_HELP_LEN);
96 rte_spinlock_unlock(&callback_sl);
101 #ifndef RTE_EXEC_ENV_WINDOWS
104 list_commands(const char *cmd __rte_unused, const char *params __rte_unused,
105 struct rte_tel_data *d)
109 rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
110 rte_spinlock_lock(&callback_sl);
111 for (i = 0; i < num_callbacks; i++)
112 rte_tel_data_add_array_string(d, callbacks[i].cmd);
113 rte_spinlock_unlock(&callback_sl);
118 json_info(const char *cmd __rte_unused, const char *params __rte_unused,
119 struct rte_tel_data *d)
121 rte_tel_data_start_dict(d);
122 rte_tel_data_add_dict_string(d, "version", telemetry_version);
123 rte_tel_data_add_dict_int(d, "pid", getpid());
124 rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN);
129 command_help(const char *cmd __rte_unused, const char *params,
130 struct rte_tel_data *d)
136 rte_tel_data_start_dict(d);
137 rte_spinlock_lock(&callback_sl);
138 for (i = 0; i < num_callbacks; i++)
139 if (strcmp(params, callbacks[i].cmd) == 0) {
140 rte_tel_data_add_dict_string(d, params,
144 rte_spinlock_unlock(&callback_sl);
145 if (i == num_callbacks)
151 container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len)
156 if (d->type != RTE_TEL_ARRAY_U64 && d->type != RTE_TEL_ARRAY_INT
157 && d->type != RTE_TEL_ARRAY_STRING)
158 return snprintf(out_buf, buf_len, "null");
160 used = rte_tel_json_empty_array(out_buf, buf_len, 0);
161 if (d->type == RTE_TEL_ARRAY_U64)
162 for (i = 0; i < d->data_len; i++)
163 used = rte_tel_json_add_array_u64(out_buf,
165 d->data.array[i].u64val);
166 if (d->type == RTE_TEL_ARRAY_INT)
167 for (i = 0; i < d->data_len; i++)
168 used = rte_tel_json_add_array_int(out_buf,
170 d->data.array[i].ival);
171 if (d->type == RTE_TEL_ARRAY_STRING)
172 for (i = 0; i < d->data_len; i++)
173 used = rte_tel_json_add_array_string(out_buf,
175 d->data.array[i].sval);
180 output_json(const char *cmd, const struct rte_tel_data *d, int s)
182 char out_buf[MAX_OUTPUT_LEN];
185 size_t buf_len, prefix_used, used = 0;
188 RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN +
189 RTE_TEL_MAX_SINGLE_STRING_LEN + 10);
192 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
193 MAX_CMD_LEN, cmd ? cmd : "none");
196 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}",
198 RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str);
201 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
203 cb_data_buf = &out_buf[prefix_used];
204 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
206 used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0);
207 for (i = 0; i < d->data_len; i++) {
208 const struct tel_dict_entry *v = &d->data.dict[i];
210 case RTE_TEL_STRING_VAL:
211 used = rte_tel_json_add_obj_str(cb_data_buf,
213 v->name, v->value.sval);
215 case RTE_TEL_INT_VAL:
216 used = rte_tel_json_add_obj_int(cb_data_buf,
218 v->name, v->value.ival);
220 case RTE_TEL_U64_VAL:
221 used = rte_tel_json_add_obj_u64(cb_data_buf,
223 v->name, v->value.u64val);
225 case RTE_TEL_CONTAINER:
228 const struct container *cont =
230 if (container_to_json(cont->data,
232 used = rte_tel_json_add_obj_json(
237 rte_tel_data_free(cont->data);
242 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
244 case RTE_TEL_ARRAY_STRING:
245 case RTE_TEL_ARRAY_INT:
246 case RTE_TEL_ARRAY_U64:
247 case RTE_TEL_ARRAY_CONTAINER:
248 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
250 cb_data_buf = &out_buf[prefix_used];
251 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
253 used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0);
254 for (i = 0; i < d->data_len; i++)
255 if (d->type == RTE_TEL_ARRAY_STRING)
256 used = rte_tel_json_add_array_string(
259 d->data.array[i].sval);
260 else if (d->type == RTE_TEL_ARRAY_INT)
261 used = rte_tel_json_add_array_int(cb_data_buf,
263 d->data.array[i].ival);
264 else if (d->type == RTE_TEL_ARRAY_U64)
265 used = rte_tel_json_add_array_u64(cb_data_buf,
267 d->data.array[i].u64val);
268 else if (d->type == RTE_TEL_ARRAY_CONTAINER) {
270 const struct container *rec_data =
271 &d->data.array[i].container;
272 if (container_to_json(rec_data->data,
274 used = rte_tel_json_add_array_json(
276 buf_len, used, temp);
278 rte_tel_data_free(rec_data->data);
281 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
284 if (write(s, out_buf, used) < 0)
285 perror("Error writing to socket");
289 perform_command(telemetry_cb fn, const char *cmd, const char *param, int s)
291 struct rte_tel_data data;
293 int ret = fn(cmd, param, &data);
295 char out_buf[MAX_CMD_LEN + 10];
296 int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
297 MAX_CMD_LEN, cmd ? cmd : "none");
298 if (write(s, out_buf, used) < 0)
299 perror("Error writing to socket");
302 output_json(cmd, &data, s);
306 unknown_command(const char *cmd __rte_unused, const char *params __rte_unused,
307 struct rte_tel_data *d)
309 return d->type = RTE_TEL_NULL;
313 client_handler(void *sock_id)
315 int s = (int)(uintptr_t)sock_id;
318 snprintf(info_str, sizeof(info_str),
319 "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}",
320 telemetry_version, getpid(), MAX_OUTPUT_LEN);
321 if (write(s, info_str, strlen(info_str)) < 0) {
326 /* receive data is not null terminated */
327 int bytes = read(s, buffer, sizeof(buffer) - 1);
330 const char *cmd = strtok(buffer, ",");
331 const char *param = strtok(NULL, "\0");
332 telemetry_cb fn = unknown_command;
335 if (cmd && strlen(cmd) < MAX_CMD_LEN) {
336 rte_spinlock_lock(&callback_sl);
337 for (i = 0; i < num_callbacks; i++)
338 if (strcmp(cmd, callbacks[i].cmd) == 0) {
339 fn = callbacks[i].fn;
342 rte_spinlock_unlock(&callback_sl);
344 perform_command(fn, cmd, param, s);
346 bytes = read(s, buffer, sizeof(buffer) - 1);
349 __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED);
354 socket_listener(void *socket)
359 struct socket *s = (struct socket *)socket;
360 int s_accepted = accept(s->sock, NULL, NULL);
361 if (s_accepted < 0) {
362 TMTY_LOG(ERR, "Error with accept, telemetry thread quitting\n");
365 if (s->num_clients != NULL) {
366 uint16_t conns = __atomic_load_n(s->num_clients,
368 if (conns >= MAX_CONNECTIONS) {
372 __atomic_add_fetch(s->num_clients, 1,
375 rc = pthread_create(&th, NULL, s->fn,
376 (void *)(uintptr_t)s_accepted);
378 TMTY_LOG(ERR, "Error with create client thread: %s\n",
381 if (s->num_clients != NULL)
382 __atomic_sub_fetch(s->num_clients, 1,
392 get_socket_path(const char *runtime_dir, const int version)
394 static char path[PATH_MAX];
395 snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d",
396 strlen(runtime_dir) ? runtime_dir : "/tmp", version);
403 if (v2_socket.path[0])
404 unlink(v2_socket.path);
405 if (v1_socket.path[0])
406 unlink(v1_socket.path);
410 create_socket(char *path)
412 int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
414 TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno));
418 struct sockaddr_un sun = {.sun_family = AF_UNIX};
419 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
420 unlink(sun.sun_path);
421 if (bind(sock, (void *) &sun, sizeof(sun)) < 0) {
422 TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno));
427 if (listen(sock, 1) < 0) {
428 TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno));
441 set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused)
443 #if defined RTE_EXEC_ENV_LINUX && defined __GLIBC__ && defined __GLIBC_PREREQ
444 #if __GLIBC_PREREQ(2, 12)
445 pthread_setname_np(id, name);
447 #elif defined RTE_EXEC_ENV_FREEBSD
448 pthread_set_name_np(id, name);
453 telemetry_legacy_init(void)
458 if (num_legacy_callbacks == 1) {
459 TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
463 v1_socket.fn = legacy_client_handler;
464 if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path),
465 "%s/telemetry", socket_dir) >= sizeof(v1_socket.path)) {
466 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
469 v1_socket.sock = create_socket(v1_socket.path);
470 if (v1_socket.sock < 0)
472 rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
474 TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n",
476 close(v1_socket.sock);
478 unlink(v1_socket.path);
479 v1_socket.path[0] = '\0';
482 pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
483 set_thread_name(t_old, "telemetry-v1");
484 TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
489 telemetry_v2_init(void)
494 v2_socket.num_clients = &v2_clients;
495 rte_telemetry_register_cmd("/", list_commands,
496 "Returns list of available commands, Takes no parameters");
497 rte_telemetry_register_cmd("/info", json_info,
498 "Returns DPDK Telemetry information. Takes no parameters");
499 rte_telemetry_register_cmd("/help", command_help,
500 "Returns help text for a command. Parameters: string command");
501 v2_socket.fn = client_handler;
502 if (strlcpy(v2_socket.path, get_socket_path(socket_dir, 2),
503 sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) {
504 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
508 v2_socket.sock = create_socket(v2_socket.path);
509 if (v2_socket.sock < 0)
511 rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
513 TMTY_LOG(ERR, "Error with create socket thread: %s\n",
515 close(v2_socket.sock);
517 unlink(v2_socket.path);
518 v2_socket.path[0] = '\0';
521 pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
522 set_thread_name(t_new, "telemetry-v2");
523 atexit(unlink_sockets);
528 #endif /* !RTE_EXEC_ENV_WINDOWS */
531 rte_telemetry_init(const char *runtime_dir, const char *rte_version, rte_cpuset_t *cpuset,
532 rte_log_fn log_fn, uint32_t registered_logtype)
534 telemetry_version = rte_version;
535 socket_dir = runtime_dir;
536 thread_cpuset = cpuset;
537 rte_log_ptr = log_fn;
538 logtype = registered_logtype;
540 #ifndef RTE_EXEC_ENV_WINDOWS
541 if (telemetry_v2_init() != 0)
543 TMTY_LOG(DEBUG, "Telemetry initialized ok\n");
544 telemetry_legacy_init();
545 #endif /* RTE_EXEC_ENV_WINDOWS */