1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #ifndef RTE_EXEC_ENV_WINDOWS
8 #include <sys/socket.h>
12 #endif /* !RTE_EXEC_ENV_WINDOWS */
14 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
16 #include <rte_string_fns.h>
17 #include <rte_common.h>
18 #include <rte_spinlock.h>
21 #include "rte_telemetry.h"
22 #include "telemetry_json.h"
23 #include "telemetry_data.h"
24 #include "telemetry_internal.h"
26 #define MAX_CMD_LEN 56
27 #define MAX_HELP_LEN 64
28 #define MAX_OUTPUT_LEN (1024 * 16)
29 #define MAX_CONNECTIONS 10
31 #ifndef RTE_EXEC_ENV_WINDOWS
33 client_handler(void *socket);
34 #endif /* !RTE_EXEC_ENV_WINDOWS */
37 char cmd[MAX_CMD_LEN];
39 char help[MAX_HELP_LEN];
42 #ifndef RTE_EXEC_ENV_WINDOWS
45 char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
47 uint16_t *num_clients;
49 static struct socket v2_socket; /* socket for v2 telemetry */
50 static struct socket v1_socket; /* socket for v1 telemetry */
51 #endif /* !RTE_EXEC_ENV_WINDOWS */
53 static const char *telemetry_version; /* save rte_version */
54 static const char *socket_dir; /* runtime directory */
55 static rte_cpuset_t *thread_cpuset;
56 static rte_log_fn rte_log_ptr;
57 static uint32_t logtype;
59 #define TMTY_LOG(l, ...) \
60 rte_log_ptr(RTE_LOG_ ## l, logtype, "TELEMETRY: " __VA_ARGS__)
62 /* list of command callbacks, with one command registered by default */
63 static struct cmd_callback *callbacks;
64 static int num_callbacks; /* How many commands are registered */
65 /* Used when accessing or modifying list of command callbacks */
66 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
67 #ifndef RTE_EXEC_ENV_WINDOWS
68 static uint16_t v2_clients;
69 #endif /* !RTE_EXEC_ENV_WINDOWS */
72 rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help)
74 struct cmd_callback *new_callbacks;
77 if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/'
78 || strlen(help) >= MAX_HELP_LEN)
81 rte_spinlock_lock(&callback_sl);
82 new_callbacks = realloc(callbacks, sizeof(callbacks[0]) * (num_callbacks + 1));
83 if (new_callbacks == NULL) {
84 rte_spinlock_unlock(&callback_sl);
87 callbacks = new_callbacks;
89 while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0)
91 if (i != num_callbacks)
92 /* Move elements to keep the list alphabetical */
93 memmove(callbacks + i + 1, callbacks + i,
94 sizeof(struct cmd_callback) * (num_callbacks - i));
96 strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN);
98 strlcpy(callbacks[i].help, help, MAX_HELP_LEN);
100 rte_spinlock_unlock(&callback_sl);
105 #ifndef RTE_EXEC_ENV_WINDOWS
108 list_commands(const char *cmd __rte_unused, const char *params __rte_unused,
109 struct rte_tel_data *d)
113 rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
114 rte_spinlock_lock(&callback_sl);
115 for (i = 0; i < num_callbacks; i++)
116 rte_tel_data_add_array_string(d, callbacks[i].cmd);
117 rte_spinlock_unlock(&callback_sl);
122 json_info(const char *cmd __rte_unused, const char *params __rte_unused,
123 struct rte_tel_data *d)
125 rte_tel_data_start_dict(d);
126 rte_tel_data_add_dict_string(d, "version", telemetry_version);
127 rte_tel_data_add_dict_int(d, "pid", getpid());
128 rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN);
133 command_help(const char *cmd __rte_unused, const char *params,
134 struct rte_tel_data *d)
140 rte_tel_data_start_dict(d);
141 rte_spinlock_lock(&callback_sl);
142 for (i = 0; i < num_callbacks; i++)
143 if (strcmp(params, callbacks[i].cmd) == 0) {
144 rte_tel_data_add_dict_string(d, params,
148 rte_spinlock_unlock(&callback_sl);
149 if (i == num_callbacks)
155 container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len)
160 if (d->type != RTE_TEL_ARRAY_U64 && d->type != RTE_TEL_ARRAY_INT
161 && d->type != RTE_TEL_ARRAY_STRING)
162 return snprintf(out_buf, buf_len, "null");
164 used = rte_tel_json_empty_array(out_buf, buf_len, 0);
165 if (d->type == RTE_TEL_ARRAY_U64)
166 for (i = 0; i < d->data_len; i++)
167 used = rte_tel_json_add_array_u64(out_buf,
169 d->data.array[i].u64val);
170 if (d->type == RTE_TEL_ARRAY_INT)
171 for (i = 0; i < d->data_len; i++)
172 used = rte_tel_json_add_array_int(out_buf,
174 d->data.array[i].ival);
175 if (d->type == RTE_TEL_ARRAY_STRING)
176 for (i = 0; i < d->data_len; i++)
177 used = rte_tel_json_add_array_string(out_buf,
179 d->data.array[i].sval);
184 output_json(const char *cmd, const struct rte_tel_data *d, int s)
186 char out_buf[MAX_OUTPUT_LEN];
189 size_t buf_len, prefix_used, used = 0;
192 RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN +
193 RTE_TEL_MAX_SINGLE_STRING_LEN + 10);
196 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
197 MAX_CMD_LEN, cmd ? cmd : "none");
200 used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}",
202 RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str);
205 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
207 cb_data_buf = &out_buf[prefix_used];
208 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
210 used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0);
211 for (i = 0; i < d->data_len; i++) {
212 const struct tel_dict_entry *v = &d->data.dict[i];
214 case RTE_TEL_STRING_VAL:
215 used = rte_tel_json_add_obj_str(cb_data_buf,
217 v->name, v->value.sval);
219 case RTE_TEL_INT_VAL:
220 used = rte_tel_json_add_obj_int(cb_data_buf,
222 v->name, v->value.ival);
224 case RTE_TEL_U64_VAL:
225 used = rte_tel_json_add_obj_u64(cb_data_buf,
227 v->name, v->value.u64val);
229 case RTE_TEL_CONTAINER:
232 const struct container *cont =
234 if (container_to_json(cont->data,
236 used = rte_tel_json_add_obj_json(
241 rte_tel_data_free(cont->data);
246 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
248 case RTE_TEL_ARRAY_STRING:
249 case RTE_TEL_ARRAY_INT:
250 case RTE_TEL_ARRAY_U64:
251 case RTE_TEL_ARRAY_CONTAINER:
252 prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
254 cb_data_buf = &out_buf[prefix_used];
255 buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
257 used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0);
258 for (i = 0; i < d->data_len; i++)
259 if (d->type == RTE_TEL_ARRAY_STRING)
260 used = rte_tel_json_add_array_string(
263 d->data.array[i].sval);
264 else if (d->type == RTE_TEL_ARRAY_INT)
265 used = rte_tel_json_add_array_int(cb_data_buf,
267 d->data.array[i].ival);
268 else if (d->type == RTE_TEL_ARRAY_U64)
269 used = rte_tel_json_add_array_u64(cb_data_buf,
271 d->data.array[i].u64val);
272 else if (d->type == RTE_TEL_ARRAY_CONTAINER) {
274 const struct container *rec_data =
275 &d->data.array[i].container;
276 if (container_to_json(rec_data->data,
278 used = rte_tel_json_add_array_json(
280 buf_len, used, temp);
282 rte_tel_data_free(rec_data->data);
285 used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
288 if (write(s, out_buf, used) < 0)
289 perror("Error writing to socket");
293 perform_command(telemetry_cb fn, const char *cmd, const char *param, int s)
295 struct rte_tel_data data;
297 int ret = fn(cmd, param, &data);
299 char out_buf[MAX_CMD_LEN + 10];
300 int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
301 MAX_CMD_LEN, cmd ? cmd : "none");
302 if (write(s, out_buf, used) < 0)
303 perror("Error writing to socket");
306 output_json(cmd, &data, s);
310 unknown_command(const char *cmd __rte_unused, const char *params __rte_unused,
311 struct rte_tel_data *d)
313 return d->type = RTE_TEL_NULL;
317 client_handler(void *sock_id)
319 int s = (int)(uintptr_t)sock_id;
322 snprintf(info_str, sizeof(info_str),
323 "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}",
324 telemetry_version, getpid(), MAX_OUTPUT_LEN);
325 if (write(s, info_str, strlen(info_str)) < 0) {
330 /* receive data is not null terminated */
331 int bytes = read(s, buffer, sizeof(buffer) - 1);
334 const char *cmd = strtok(buffer, ",");
335 const char *param = strtok(NULL, "\0");
336 telemetry_cb fn = unknown_command;
339 if (cmd && strlen(cmd) < MAX_CMD_LEN) {
340 rte_spinlock_lock(&callback_sl);
341 for (i = 0; i < num_callbacks; i++)
342 if (strcmp(cmd, callbacks[i].cmd) == 0) {
343 fn = callbacks[i].fn;
346 rte_spinlock_unlock(&callback_sl);
348 perform_command(fn, cmd, param, s);
350 bytes = read(s, buffer, sizeof(buffer) - 1);
353 __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED);
358 socket_listener(void *socket)
363 struct socket *s = (struct socket *)socket;
364 int s_accepted = accept(s->sock, NULL, NULL);
365 if (s_accepted < 0) {
366 TMTY_LOG(ERR, "Error with accept, telemetry thread quitting\n");
369 if (s->num_clients != NULL) {
370 uint16_t conns = __atomic_load_n(s->num_clients,
372 if (conns >= MAX_CONNECTIONS) {
376 __atomic_add_fetch(s->num_clients, 1,
379 rc = pthread_create(&th, NULL, s->fn,
380 (void *)(uintptr_t)s_accepted);
382 TMTY_LOG(ERR, "Error with create client thread: %s\n",
385 if (s->num_clients != NULL)
386 __atomic_sub_fetch(s->num_clients, 1,
396 get_socket_path(const char *runtime_dir, const int version)
398 static char path[PATH_MAX];
399 snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d",
400 strlen(runtime_dir) ? runtime_dir : "/tmp", version);
407 if (v2_socket.path[0])
408 unlink(v2_socket.path);
409 if (v1_socket.path[0])
410 unlink(v1_socket.path);
414 create_socket(char *path)
416 int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
418 TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno));
422 struct sockaddr_un sun = {.sun_family = AF_UNIX};
423 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
424 unlink(sun.sun_path);
425 if (bind(sock, (void *) &sun, sizeof(sun)) < 0) {
428 TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno));
429 if (stat(socket_dir, &st) < 0 || !S_ISDIR(st.st_mode))
430 TMTY_LOG(ERR, "Cannot access DPDK runtime directory: %s\n", socket_dir);
435 if (listen(sock, 1) < 0) {
436 TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno));
449 set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused)
451 #if defined RTE_EXEC_ENV_LINUX && defined __GLIBC__ && defined __GLIBC_PREREQ
452 #if __GLIBC_PREREQ(2, 12)
453 pthread_setname_np(id, name);
455 #elif defined RTE_EXEC_ENV_FREEBSD
456 pthread_set_name_np(id, name);
461 telemetry_legacy_init(void)
466 if (num_legacy_callbacks == 1) {
467 TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
471 v1_socket.fn = legacy_client_handler;
472 if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path),
473 "%s/telemetry", socket_dir) >= sizeof(v1_socket.path)) {
474 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
477 v1_socket.sock = create_socket(v1_socket.path);
478 if (v1_socket.sock < 0)
480 rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
482 TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n",
484 close(v1_socket.sock);
486 unlink(v1_socket.path);
487 v1_socket.path[0] = '\0';
490 pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
491 set_thread_name(t_old, "telemetry-v1");
492 TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
497 telemetry_v2_init(void)
502 v2_socket.num_clients = &v2_clients;
503 rte_telemetry_register_cmd("/", list_commands,
504 "Returns list of available commands, Takes no parameters");
505 rte_telemetry_register_cmd("/info", json_info,
506 "Returns DPDK Telemetry information. Takes no parameters");
507 rte_telemetry_register_cmd("/help", command_help,
508 "Returns help text for a command. Parameters: string command");
509 v2_socket.fn = client_handler;
510 if (strlcpy(v2_socket.path, get_socket_path(socket_dir, 2),
511 sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) {
512 TMTY_LOG(ERR, "Error with socket binding, path too long\n");
516 v2_socket.sock = create_socket(v2_socket.path);
517 if (v2_socket.sock < 0)
519 rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
521 TMTY_LOG(ERR, "Error with create socket thread: %s\n",
523 close(v2_socket.sock);
525 unlink(v2_socket.path);
526 v2_socket.path[0] = '\0';
529 pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
530 set_thread_name(t_new, "telemetry-v2");
531 atexit(unlink_sockets);
536 #endif /* !RTE_EXEC_ENV_WINDOWS */
539 rte_telemetry_init(const char *runtime_dir, const char *rte_version, rte_cpuset_t *cpuset,
540 rte_log_fn log_fn, uint32_t registered_logtype)
542 telemetry_version = rte_version;
543 socket_dir = runtime_dir;
544 thread_cpuset = cpuset;
545 rte_log_ptr = log_fn;
546 logtype = registered_logtype;
548 #ifndef RTE_EXEC_ENV_WINDOWS
549 if (telemetry_v2_init() != 0)
551 TMTY_LOG(DEBUG, "Telemetry initialized ok\n");
552 telemetry_legacy_init();
553 #endif /* RTE_EXEC_ENV_WINDOWS */