199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2020 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 699a2dd95SBruce Richardson #include <unistd.h> 799a2dd95SBruce Richardson #include <pthread.h> 899a2dd95SBruce Richardson #include <sys/socket.h> 999a2dd95SBruce Richardson #include <sys/un.h> 1099a2dd95SBruce Richardson #include <dlfcn.h> 1199a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 1299a2dd95SBruce Richardson 1399a2dd95SBruce Richardson /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */ 1499a2dd95SBruce Richardson #undef RTE_USE_LIBBSD 1599a2dd95SBruce Richardson #include <rte_string_fns.h> 1699a2dd95SBruce Richardson #include <rte_common.h> 1799a2dd95SBruce Richardson #include <rte_spinlock.h> 1899a2dd95SBruce Richardson #include <rte_log.h> 1999a2dd95SBruce Richardson 2099a2dd95SBruce Richardson #include "rte_telemetry.h" 2199a2dd95SBruce Richardson #include "telemetry_json.h" 2299a2dd95SBruce Richardson #include "telemetry_data.h" 2399a2dd95SBruce Richardson #include "telemetry_internal.h" 2499a2dd95SBruce Richardson 2599a2dd95SBruce Richardson #define MAX_CMD_LEN 56 2699a2dd95SBruce Richardson #define MAX_HELP_LEN 64 2799a2dd95SBruce Richardson #define MAX_OUTPUT_LEN (1024 * 16) 2899a2dd95SBruce Richardson #define MAX_CONNECTIONS 10 2999a2dd95SBruce Richardson 3099a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 3199a2dd95SBruce Richardson static void * 3299a2dd95SBruce Richardson client_handler(void *socket); 3399a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 3499a2dd95SBruce Richardson 3599a2dd95SBruce Richardson struct cmd_callback { 3699a2dd95SBruce Richardson char cmd[MAX_CMD_LEN]; 3799a2dd95SBruce Richardson telemetry_cb fn; 3899a2dd95SBruce Richardson char help[MAX_HELP_LEN]; 3999a2dd95SBruce Richardson }; 4099a2dd95SBruce Richardson 4199a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 4299a2dd95SBruce Richardson struct socket { 4399a2dd95SBruce Richardson int sock; 4499a2dd95SBruce Richardson char path[sizeof(((struct sockaddr_un *)0)->sun_path)]; 4599a2dd95SBruce Richardson handler fn; 4699a2dd95SBruce Richardson uint16_t *num_clients; 4799a2dd95SBruce Richardson }; 4899a2dd95SBruce Richardson static struct socket v2_socket; /* socket for v2 telemetry */ 4999a2dd95SBruce Richardson static struct socket v1_socket; /* socket for v1 telemetry */ 5099a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 5199a2dd95SBruce Richardson 5299a2dd95SBruce Richardson static const char *telemetry_version; /* save rte_version */ 5399a2dd95SBruce Richardson static const char *socket_dir; /* runtime directory */ 5499a2dd95SBruce Richardson static rte_cpuset_t *thread_cpuset; 5599a2dd95SBruce Richardson static rte_log_fn rte_log_ptr; 5699a2dd95SBruce Richardson static uint32_t logtype; 5799a2dd95SBruce Richardson 5899a2dd95SBruce Richardson #define TMTY_LOG(l, ...) \ 5999a2dd95SBruce Richardson rte_log_ptr(RTE_LOG_ ## l, logtype, "TELEMETRY: " __VA_ARGS__) 6099a2dd95SBruce Richardson 6199a2dd95SBruce Richardson /* list of command callbacks, with one command registered by default */ 6299a2dd95SBruce Richardson static struct cmd_callback callbacks[TELEMETRY_MAX_CALLBACKS]; 6399a2dd95SBruce Richardson static int num_callbacks; /* How many commands are registered */ 6499a2dd95SBruce Richardson /* Used when accessing or modifying list of command callbacks */ 6599a2dd95SBruce Richardson static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER; 6699a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 6799a2dd95SBruce Richardson static uint16_t v2_clients; 6899a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 6999a2dd95SBruce Richardson 7099a2dd95SBruce Richardson int 7199a2dd95SBruce Richardson rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help) 7299a2dd95SBruce Richardson { 7399a2dd95SBruce Richardson int i = 0; 7499a2dd95SBruce Richardson 7599a2dd95SBruce Richardson if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/' 7699a2dd95SBruce Richardson || strlen(help) >= MAX_HELP_LEN) 7799a2dd95SBruce Richardson return -EINVAL; 7899a2dd95SBruce Richardson if (num_callbacks >= TELEMETRY_MAX_CALLBACKS) 7999a2dd95SBruce Richardson return -ENOENT; 8099a2dd95SBruce Richardson 8199a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 8299a2dd95SBruce Richardson while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0) 8399a2dd95SBruce Richardson i++; 8499a2dd95SBruce Richardson if (i != num_callbacks) 8599a2dd95SBruce Richardson /* Move elements to keep the list alphabetical */ 8699a2dd95SBruce Richardson memmove(callbacks + i + 1, callbacks + i, 8799a2dd95SBruce Richardson sizeof(struct cmd_callback) * (num_callbacks - i)); 8899a2dd95SBruce Richardson 8999a2dd95SBruce Richardson strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN); 9099a2dd95SBruce Richardson callbacks[i].fn = fn; 9199a2dd95SBruce Richardson strlcpy(callbacks[i].help, help, MAX_HELP_LEN); 9299a2dd95SBruce Richardson num_callbacks++; 9399a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 9499a2dd95SBruce Richardson 9599a2dd95SBruce Richardson return 0; 9699a2dd95SBruce Richardson } 9799a2dd95SBruce Richardson 9899a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 9999a2dd95SBruce Richardson 10099a2dd95SBruce Richardson static int 10199a2dd95SBruce Richardson list_commands(const char *cmd __rte_unused, const char *params __rte_unused, 10299a2dd95SBruce Richardson struct rte_tel_data *d) 10399a2dd95SBruce Richardson { 10499a2dd95SBruce Richardson int i; 10599a2dd95SBruce Richardson 10699a2dd95SBruce Richardson rte_tel_data_start_array(d, RTE_TEL_STRING_VAL); 10799a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 10899a2dd95SBruce Richardson rte_tel_data_add_array_string(d, callbacks[i].cmd); 10999a2dd95SBruce Richardson return 0; 11099a2dd95SBruce Richardson } 11199a2dd95SBruce Richardson 11299a2dd95SBruce Richardson static int 11399a2dd95SBruce Richardson json_info(const char *cmd __rte_unused, const char *params __rte_unused, 11499a2dd95SBruce Richardson struct rte_tel_data *d) 11599a2dd95SBruce Richardson { 11699a2dd95SBruce Richardson rte_tel_data_start_dict(d); 11799a2dd95SBruce Richardson rte_tel_data_add_dict_string(d, "version", telemetry_version); 11899a2dd95SBruce Richardson rte_tel_data_add_dict_int(d, "pid", getpid()); 11999a2dd95SBruce Richardson rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN); 12099a2dd95SBruce Richardson return 0; 12199a2dd95SBruce Richardson } 12299a2dd95SBruce Richardson 12399a2dd95SBruce Richardson static int 12499a2dd95SBruce Richardson command_help(const char *cmd __rte_unused, const char *params, 12599a2dd95SBruce Richardson struct rte_tel_data *d) 12699a2dd95SBruce Richardson { 12799a2dd95SBruce Richardson int i; 12899a2dd95SBruce Richardson 12999a2dd95SBruce Richardson if (!params) 13099a2dd95SBruce Richardson return -1; 13199a2dd95SBruce Richardson rte_tel_data_start_dict(d); 13299a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 13399a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 13499a2dd95SBruce Richardson if (strcmp(params, callbacks[i].cmd) == 0) { 13599a2dd95SBruce Richardson rte_tel_data_add_dict_string(d, params, 13699a2dd95SBruce Richardson callbacks[i].help); 13799a2dd95SBruce Richardson break; 13899a2dd95SBruce Richardson } 13999a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 14099a2dd95SBruce Richardson if (i == num_callbacks) 14199a2dd95SBruce Richardson return -1; 14299a2dd95SBruce Richardson return 0; 14399a2dd95SBruce Richardson } 14499a2dd95SBruce Richardson 14599a2dd95SBruce Richardson static int 14699a2dd95SBruce Richardson container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len) 14799a2dd95SBruce Richardson { 14899a2dd95SBruce Richardson size_t used = 0; 14999a2dd95SBruce Richardson unsigned int i; 15099a2dd95SBruce Richardson 15199a2dd95SBruce Richardson if (d->type != RTE_TEL_ARRAY_U64 && d->type != RTE_TEL_ARRAY_INT 15299a2dd95SBruce Richardson && d->type != RTE_TEL_ARRAY_STRING) 15399a2dd95SBruce Richardson return snprintf(out_buf, buf_len, "null"); 15499a2dd95SBruce Richardson 15599a2dd95SBruce Richardson used = rte_tel_json_empty_array(out_buf, buf_len, 0); 15699a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_U64) 15799a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 15899a2dd95SBruce Richardson used = rte_tel_json_add_array_u64(out_buf, 15999a2dd95SBruce Richardson buf_len, used, 16099a2dd95SBruce Richardson d->data.array[i].u64val); 16199a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_INT) 16299a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 16399a2dd95SBruce Richardson used = rte_tel_json_add_array_int(out_buf, 16499a2dd95SBruce Richardson buf_len, used, 16599a2dd95SBruce Richardson d->data.array[i].ival); 16699a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_STRING) 16799a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 16899a2dd95SBruce Richardson used = rte_tel_json_add_array_string(out_buf, 16999a2dd95SBruce Richardson buf_len, used, 17099a2dd95SBruce Richardson d->data.array[i].sval); 17199a2dd95SBruce Richardson return used; 17299a2dd95SBruce Richardson } 17399a2dd95SBruce Richardson 17499a2dd95SBruce Richardson static void 17599a2dd95SBruce Richardson output_json(const char *cmd, const struct rte_tel_data *d, int s) 17699a2dd95SBruce Richardson { 17799a2dd95SBruce Richardson char out_buf[MAX_OUTPUT_LEN]; 17899a2dd95SBruce Richardson 17999a2dd95SBruce Richardson char *cb_data_buf; 18099a2dd95SBruce Richardson size_t buf_len, prefix_used, used = 0; 18199a2dd95SBruce Richardson unsigned int i; 18299a2dd95SBruce Richardson 18399a2dd95SBruce Richardson RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN + 18499a2dd95SBruce Richardson RTE_TEL_MAX_SINGLE_STRING_LEN + 10); 18599a2dd95SBruce Richardson switch (d->type) { 18699a2dd95SBruce Richardson case RTE_TEL_NULL: 18799a2dd95SBruce Richardson used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}", 18899a2dd95SBruce Richardson MAX_CMD_LEN, cmd ? cmd : "none"); 18999a2dd95SBruce Richardson break; 19099a2dd95SBruce Richardson case RTE_TEL_STRING: 19199a2dd95SBruce Richardson used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}", 19299a2dd95SBruce Richardson MAX_CMD_LEN, cmd, 19399a2dd95SBruce Richardson RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str); 19499a2dd95SBruce Richardson break; 19599a2dd95SBruce Richardson case RTE_TEL_DICT: 19699a2dd95SBruce Richardson prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", 19799a2dd95SBruce Richardson MAX_CMD_LEN, cmd); 19899a2dd95SBruce Richardson cb_data_buf = &out_buf[prefix_used]; 19999a2dd95SBruce Richardson buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ 20099a2dd95SBruce Richardson 20199a2dd95SBruce Richardson used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0); 20299a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) { 20399a2dd95SBruce Richardson const struct tel_dict_entry *v = &d->data.dict[i]; 20499a2dd95SBruce Richardson switch (v->type) { 20599a2dd95SBruce Richardson case RTE_TEL_STRING_VAL: 20699a2dd95SBruce Richardson used = rte_tel_json_add_obj_str(cb_data_buf, 20799a2dd95SBruce Richardson buf_len, used, 20899a2dd95SBruce Richardson v->name, v->value.sval); 20999a2dd95SBruce Richardson break; 21099a2dd95SBruce Richardson case RTE_TEL_INT_VAL: 21199a2dd95SBruce Richardson used = rte_tel_json_add_obj_int(cb_data_buf, 21299a2dd95SBruce Richardson buf_len, used, 21399a2dd95SBruce Richardson v->name, v->value.ival); 21499a2dd95SBruce Richardson break; 21599a2dd95SBruce Richardson case RTE_TEL_U64_VAL: 21699a2dd95SBruce Richardson used = rte_tel_json_add_obj_u64(cb_data_buf, 21799a2dd95SBruce Richardson buf_len, used, 21899a2dd95SBruce Richardson v->name, v->value.u64val); 21999a2dd95SBruce Richardson break; 22099a2dd95SBruce Richardson case RTE_TEL_CONTAINER: 22199a2dd95SBruce Richardson { 22299a2dd95SBruce Richardson char temp[buf_len]; 22399a2dd95SBruce Richardson const struct container *cont = 22499a2dd95SBruce Richardson &v->value.container; 22599a2dd95SBruce Richardson if (container_to_json(cont->data, 22699a2dd95SBruce Richardson temp, buf_len) != 0) 22799a2dd95SBruce Richardson used = rte_tel_json_add_obj_json( 22899a2dd95SBruce Richardson cb_data_buf, 22999a2dd95SBruce Richardson buf_len, used, 23099a2dd95SBruce Richardson v->name, temp); 23199a2dd95SBruce Richardson if (!cont->keep) 23299a2dd95SBruce Richardson rte_tel_data_free(cont->data); 23399a2dd95SBruce Richardson } 23499a2dd95SBruce Richardson } 23599a2dd95SBruce Richardson } 23699a2dd95SBruce Richardson used += prefix_used; 23799a2dd95SBruce Richardson used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); 23899a2dd95SBruce Richardson break; 23999a2dd95SBruce Richardson case RTE_TEL_ARRAY_STRING: 24099a2dd95SBruce Richardson case RTE_TEL_ARRAY_INT: 24199a2dd95SBruce Richardson case RTE_TEL_ARRAY_U64: 24299a2dd95SBruce Richardson case RTE_TEL_ARRAY_CONTAINER: 24399a2dd95SBruce Richardson prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", 24499a2dd95SBruce Richardson MAX_CMD_LEN, cmd); 24599a2dd95SBruce Richardson cb_data_buf = &out_buf[prefix_used]; 24699a2dd95SBruce Richardson buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ 24799a2dd95SBruce Richardson 24899a2dd95SBruce Richardson used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0); 24999a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 25099a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_STRING) 25199a2dd95SBruce Richardson used = rte_tel_json_add_array_string( 25299a2dd95SBruce Richardson cb_data_buf, 25399a2dd95SBruce Richardson buf_len, used, 25499a2dd95SBruce Richardson d->data.array[i].sval); 25599a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_INT) 25699a2dd95SBruce Richardson used = rte_tel_json_add_array_int(cb_data_buf, 25799a2dd95SBruce Richardson buf_len, used, 25899a2dd95SBruce Richardson d->data.array[i].ival); 25999a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_U64) 26099a2dd95SBruce Richardson used = rte_tel_json_add_array_u64(cb_data_buf, 26199a2dd95SBruce Richardson buf_len, used, 26299a2dd95SBruce Richardson d->data.array[i].u64val); 26399a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_CONTAINER) { 26499a2dd95SBruce Richardson char temp[buf_len]; 26599a2dd95SBruce Richardson const struct container *rec_data = 26699a2dd95SBruce Richardson &d->data.array[i].container; 26799a2dd95SBruce Richardson if (container_to_json(rec_data->data, 26899a2dd95SBruce Richardson temp, buf_len) != 0) 26999a2dd95SBruce Richardson used = rte_tel_json_add_array_json( 27099a2dd95SBruce Richardson cb_data_buf, 27199a2dd95SBruce Richardson buf_len, used, temp); 27299a2dd95SBruce Richardson if (!rec_data->keep) 27399a2dd95SBruce Richardson rte_tel_data_free(rec_data->data); 27499a2dd95SBruce Richardson } 27599a2dd95SBruce Richardson used += prefix_used; 27699a2dd95SBruce Richardson used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); 27799a2dd95SBruce Richardson break; 27899a2dd95SBruce Richardson } 27999a2dd95SBruce Richardson if (write(s, out_buf, used) < 0) 28099a2dd95SBruce Richardson perror("Error writing to socket"); 28199a2dd95SBruce Richardson } 28299a2dd95SBruce Richardson 28399a2dd95SBruce Richardson static void 28499a2dd95SBruce Richardson perform_command(telemetry_cb fn, const char *cmd, const char *param, int s) 28599a2dd95SBruce Richardson { 28699a2dd95SBruce Richardson struct rte_tel_data data; 28799a2dd95SBruce Richardson 28899a2dd95SBruce Richardson int ret = fn(cmd, param, &data); 28999a2dd95SBruce Richardson if (ret < 0) { 29099a2dd95SBruce Richardson char out_buf[MAX_CMD_LEN + 10]; 29199a2dd95SBruce Richardson int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}", 29299a2dd95SBruce Richardson MAX_CMD_LEN, cmd ? cmd : "none"); 29399a2dd95SBruce Richardson if (write(s, out_buf, used) < 0) 29499a2dd95SBruce Richardson perror("Error writing to socket"); 29599a2dd95SBruce Richardson return; 29699a2dd95SBruce Richardson } 29799a2dd95SBruce Richardson output_json(cmd, &data, s); 29899a2dd95SBruce Richardson } 29999a2dd95SBruce Richardson 30099a2dd95SBruce Richardson static int 30199a2dd95SBruce Richardson unknown_command(const char *cmd __rte_unused, const char *params __rte_unused, 30299a2dd95SBruce Richardson struct rte_tel_data *d) 30399a2dd95SBruce Richardson { 30499a2dd95SBruce Richardson return d->type = RTE_TEL_NULL; 30599a2dd95SBruce Richardson } 30699a2dd95SBruce Richardson 30799a2dd95SBruce Richardson static void * 30899a2dd95SBruce Richardson client_handler(void *sock_id) 30999a2dd95SBruce Richardson { 31099a2dd95SBruce Richardson int s = (int)(uintptr_t)sock_id; 31199a2dd95SBruce Richardson char buffer[1024]; 31299a2dd95SBruce Richardson char info_str[1024]; 31399a2dd95SBruce Richardson snprintf(info_str, sizeof(info_str), 31499a2dd95SBruce Richardson "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}", 31599a2dd95SBruce Richardson telemetry_version, getpid(), MAX_OUTPUT_LEN); 31699a2dd95SBruce Richardson if (write(s, info_str, strlen(info_str)) < 0) { 31799a2dd95SBruce Richardson close(s); 31899a2dd95SBruce Richardson return NULL; 31999a2dd95SBruce Richardson } 32099a2dd95SBruce Richardson 32199a2dd95SBruce Richardson /* receive data is not null terminated */ 32299a2dd95SBruce Richardson int bytes = read(s, buffer, sizeof(buffer) - 1); 32399a2dd95SBruce Richardson while (bytes > 0) { 32499a2dd95SBruce Richardson buffer[bytes] = 0; 32599a2dd95SBruce Richardson const char *cmd = strtok(buffer, ","); 32699a2dd95SBruce Richardson const char *param = strtok(NULL, "\0"); 32799a2dd95SBruce Richardson telemetry_cb fn = unknown_command; 32899a2dd95SBruce Richardson int i; 32999a2dd95SBruce Richardson 33099a2dd95SBruce Richardson if (cmd && strlen(cmd) < MAX_CMD_LEN) { 33199a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 33299a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 33399a2dd95SBruce Richardson if (strcmp(cmd, callbacks[i].cmd) == 0) { 33499a2dd95SBruce Richardson fn = callbacks[i].fn; 33599a2dd95SBruce Richardson break; 33699a2dd95SBruce Richardson } 33799a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 33899a2dd95SBruce Richardson } 33999a2dd95SBruce Richardson perform_command(fn, cmd, param, s); 34099a2dd95SBruce Richardson 34199a2dd95SBruce Richardson bytes = read(s, buffer, sizeof(buffer) - 1); 34299a2dd95SBruce Richardson } 34399a2dd95SBruce Richardson close(s); 34499a2dd95SBruce Richardson __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED); 34599a2dd95SBruce Richardson return NULL; 34699a2dd95SBruce Richardson } 34799a2dd95SBruce Richardson 34899a2dd95SBruce Richardson static void * 34999a2dd95SBruce Richardson socket_listener(void *socket) 35099a2dd95SBruce Richardson { 35199a2dd95SBruce Richardson while (1) { 35299a2dd95SBruce Richardson pthread_t th; 353*c53a5f3eSChengwen Feng int rc; 35499a2dd95SBruce Richardson struct socket *s = (struct socket *)socket; 35599a2dd95SBruce Richardson int s_accepted = accept(s->sock, NULL, NULL); 35699a2dd95SBruce Richardson if (s_accepted < 0) { 35799a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with accept, telemetry thread quitting\n"); 35899a2dd95SBruce Richardson return NULL; 35999a2dd95SBruce Richardson } 36099a2dd95SBruce Richardson if (s->num_clients != NULL) { 36199a2dd95SBruce Richardson uint16_t conns = __atomic_load_n(s->num_clients, 36299a2dd95SBruce Richardson __ATOMIC_RELAXED); 36399a2dd95SBruce Richardson if (conns >= MAX_CONNECTIONS) { 36499a2dd95SBruce Richardson close(s_accepted); 36599a2dd95SBruce Richardson continue; 36699a2dd95SBruce Richardson } 36799a2dd95SBruce Richardson __atomic_add_fetch(s->num_clients, 1, 36899a2dd95SBruce Richardson __ATOMIC_RELAXED); 36999a2dd95SBruce Richardson } 370*c53a5f3eSChengwen Feng rc = pthread_create(&th, NULL, s->fn, 371*c53a5f3eSChengwen Feng (void *)(uintptr_t)s_accepted); 372*c53a5f3eSChengwen Feng if (rc != 0) { 373*c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create client thread: %s\n", 374*c53a5f3eSChengwen Feng strerror(rc)); 375*c53a5f3eSChengwen Feng close(s_accepted); 376*c53a5f3eSChengwen Feng if (s->num_clients != NULL) 377*c53a5f3eSChengwen Feng __atomic_sub_fetch(s->num_clients, 1, 378*c53a5f3eSChengwen Feng __ATOMIC_RELAXED); 379*c53a5f3eSChengwen Feng continue; 380*c53a5f3eSChengwen Feng } 38199a2dd95SBruce Richardson pthread_detach(th); 38299a2dd95SBruce Richardson } 38399a2dd95SBruce Richardson return NULL; 38499a2dd95SBruce Richardson } 38599a2dd95SBruce Richardson 38699a2dd95SBruce Richardson static inline char * 38799a2dd95SBruce Richardson get_socket_path(const char *runtime_dir, const int version) 38899a2dd95SBruce Richardson { 38999a2dd95SBruce Richardson static char path[PATH_MAX]; 39099a2dd95SBruce Richardson snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d", 39199a2dd95SBruce Richardson strlen(runtime_dir) ? runtime_dir : "/tmp", version); 39299a2dd95SBruce Richardson return path; 39399a2dd95SBruce Richardson } 39499a2dd95SBruce Richardson 39599a2dd95SBruce Richardson static void 39699a2dd95SBruce Richardson unlink_sockets(void) 39799a2dd95SBruce Richardson { 39899a2dd95SBruce Richardson if (v2_socket.path[0]) 39999a2dd95SBruce Richardson unlink(v2_socket.path); 40099a2dd95SBruce Richardson if (v1_socket.path[0]) 40199a2dd95SBruce Richardson unlink(v1_socket.path); 40299a2dd95SBruce Richardson } 40399a2dd95SBruce Richardson 40499a2dd95SBruce Richardson static int 40599a2dd95SBruce Richardson create_socket(char *path) 40699a2dd95SBruce Richardson { 40799a2dd95SBruce Richardson int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); 40899a2dd95SBruce Richardson if (sock < 0) { 40999a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno)); 41099a2dd95SBruce Richardson return -1; 41199a2dd95SBruce Richardson } 41299a2dd95SBruce Richardson 41399a2dd95SBruce Richardson struct sockaddr_un sun = {.sun_family = AF_UNIX}; 41499a2dd95SBruce Richardson strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 41599a2dd95SBruce Richardson unlink(sun.sun_path); 41699a2dd95SBruce Richardson if (bind(sock, (void *) &sun, sizeof(sun)) < 0) { 41799a2dd95SBruce Richardson TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno)); 41899a2dd95SBruce Richardson sun.sun_path[0] = 0; 41999a2dd95SBruce Richardson goto error; 42099a2dd95SBruce Richardson } 42199a2dd95SBruce Richardson 42299a2dd95SBruce Richardson if (listen(sock, 1) < 0) { 42399a2dd95SBruce Richardson TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno)); 42499a2dd95SBruce Richardson goto error; 42599a2dd95SBruce Richardson } 42699a2dd95SBruce Richardson 42799a2dd95SBruce Richardson return sock; 42899a2dd95SBruce Richardson 42999a2dd95SBruce Richardson error: 43099a2dd95SBruce Richardson close(sock); 43199a2dd95SBruce Richardson unlink_sockets(); 43299a2dd95SBruce Richardson return -1; 43399a2dd95SBruce Richardson } 43499a2dd95SBruce Richardson 43599a2dd95SBruce Richardson static int 43699a2dd95SBruce Richardson telemetry_legacy_init(void) 43799a2dd95SBruce Richardson { 43899a2dd95SBruce Richardson pthread_t t_old; 439*c53a5f3eSChengwen Feng int rc; 44099a2dd95SBruce Richardson 44199a2dd95SBruce Richardson if (num_legacy_callbacks == 1) { 44299a2dd95SBruce Richardson TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n"); 44399a2dd95SBruce Richardson return -1; 44499a2dd95SBruce Richardson } 44599a2dd95SBruce Richardson 44699a2dd95SBruce Richardson v1_socket.fn = legacy_client_handler; 44799a2dd95SBruce Richardson if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path), 44899a2dd95SBruce Richardson "%s/telemetry", socket_dir) >= sizeof(v1_socket.path)) { 44999a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket binding, path too long\n"); 45099a2dd95SBruce Richardson return -1; 45199a2dd95SBruce Richardson } 45299a2dd95SBruce Richardson v1_socket.sock = create_socket(v1_socket.path); 45399a2dd95SBruce Richardson if (v1_socket.sock < 0) 45499a2dd95SBruce Richardson return -1; 455*c53a5f3eSChengwen Feng rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket); 456*c53a5f3eSChengwen Feng if (rc != 0) { 457*c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n", 458*c53a5f3eSChengwen Feng strerror(rc)); 459*c53a5f3eSChengwen Feng close(v1_socket.sock); 460*c53a5f3eSChengwen Feng v1_socket.sock = -1; 461*c53a5f3eSChengwen Feng unlink(v1_socket.path); 462*c53a5f3eSChengwen Feng v1_socket.path[0] = '\0'; 463*c53a5f3eSChengwen Feng return -1; 464*c53a5f3eSChengwen Feng } 46599a2dd95SBruce Richardson pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset); 4665da7736fSChengwen Feng pthread_setname_np(t_old, "telemetry-v1"); 46799a2dd95SBruce Richardson TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n"); 46899a2dd95SBruce Richardson return 0; 46999a2dd95SBruce Richardson } 47099a2dd95SBruce Richardson 47199a2dd95SBruce Richardson static int 47299a2dd95SBruce Richardson telemetry_v2_init(void) 47399a2dd95SBruce Richardson { 47499a2dd95SBruce Richardson pthread_t t_new; 475*c53a5f3eSChengwen Feng int rc; 47699a2dd95SBruce Richardson 47799a2dd95SBruce Richardson v2_socket.num_clients = &v2_clients; 47899a2dd95SBruce Richardson rte_telemetry_register_cmd("/", list_commands, 47999a2dd95SBruce Richardson "Returns list of available commands, Takes no parameters"); 48099a2dd95SBruce Richardson rte_telemetry_register_cmd("/info", json_info, 48199a2dd95SBruce Richardson "Returns DPDK Telemetry information. Takes no parameters"); 48299a2dd95SBruce Richardson rte_telemetry_register_cmd("/help", command_help, 48399a2dd95SBruce Richardson "Returns help text for a command. Parameters: string command"); 48499a2dd95SBruce Richardson v2_socket.fn = client_handler; 48599a2dd95SBruce Richardson if (strlcpy(v2_socket.path, get_socket_path(socket_dir, 2), 48699a2dd95SBruce Richardson sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) { 48799a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket binding, path too long\n"); 48899a2dd95SBruce Richardson return -1; 48999a2dd95SBruce Richardson } 49099a2dd95SBruce Richardson 49199a2dd95SBruce Richardson v2_socket.sock = create_socket(v2_socket.path); 49299a2dd95SBruce Richardson if (v2_socket.sock < 0) 49399a2dd95SBruce Richardson return -1; 494*c53a5f3eSChengwen Feng rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket); 495*c53a5f3eSChengwen Feng if (rc != 0) { 496*c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create socket thread: %s\n", 497*c53a5f3eSChengwen Feng strerror(rc)); 498*c53a5f3eSChengwen Feng close(v2_socket.sock); 499*c53a5f3eSChengwen Feng v2_socket.sock = -1; 500*c53a5f3eSChengwen Feng unlink(v2_socket.path); 501*c53a5f3eSChengwen Feng v2_socket.path[0] = '\0'; 502*c53a5f3eSChengwen Feng return -1; 503*c53a5f3eSChengwen Feng } 50499a2dd95SBruce Richardson pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset); 5055da7736fSChengwen Feng pthread_setname_np(t_new, "telemetry-v2"); 50699a2dd95SBruce Richardson atexit(unlink_sockets); 50799a2dd95SBruce Richardson 50899a2dd95SBruce Richardson return 0; 50999a2dd95SBruce Richardson } 51099a2dd95SBruce Richardson 51199a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 51299a2dd95SBruce Richardson 51399a2dd95SBruce Richardson int32_t 51499a2dd95SBruce Richardson rte_telemetry_init(const char *runtime_dir, const char *rte_version, rte_cpuset_t *cpuset, 51599a2dd95SBruce Richardson rte_log_fn log_fn, uint32_t registered_logtype) 51699a2dd95SBruce Richardson { 51799a2dd95SBruce Richardson telemetry_version = rte_version; 51899a2dd95SBruce Richardson socket_dir = runtime_dir; 51999a2dd95SBruce Richardson thread_cpuset = cpuset; 52099a2dd95SBruce Richardson rte_log_ptr = log_fn; 52199a2dd95SBruce Richardson logtype = registered_logtype; 52299a2dd95SBruce Richardson 52399a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 52499a2dd95SBruce Richardson if (telemetry_v2_init() != 0) 52599a2dd95SBruce Richardson return -1; 52699a2dd95SBruce Richardson TMTY_LOG(DEBUG, "Telemetry initialized ok\n"); 52799a2dd95SBruce Richardson telemetry_legacy_init(); 52899a2dd95SBruce Richardson #endif /* RTE_EXEC_ENV_WINDOWS */ 52999a2dd95SBruce Richardson 53099a2dd95SBruce Richardson return 0; 53199a2dd95SBruce Richardson } 532