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> 10*d5252f7dSBruce Richardson #include <sys/stat.h> 1199a2dd95SBruce Richardson #include <dlfcn.h> 1299a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 1399a2dd95SBruce Richardson 1499a2dd95SBruce Richardson /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */ 1599a2dd95SBruce Richardson #undef RTE_USE_LIBBSD 1699a2dd95SBruce Richardson #include <rte_string_fns.h> 1799a2dd95SBruce Richardson #include <rte_common.h> 1899a2dd95SBruce Richardson #include <rte_spinlock.h> 1999a2dd95SBruce Richardson #include <rte_log.h> 2099a2dd95SBruce Richardson 2199a2dd95SBruce Richardson #include "rte_telemetry.h" 2299a2dd95SBruce Richardson #include "telemetry_json.h" 2399a2dd95SBruce Richardson #include "telemetry_data.h" 2499a2dd95SBruce Richardson #include "telemetry_internal.h" 2599a2dd95SBruce Richardson 2699a2dd95SBruce Richardson #define MAX_CMD_LEN 56 2799a2dd95SBruce Richardson #define MAX_HELP_LEN 64 2899a2dd95SBruce Richardson #define MAX_OUTPUT_LEN (1024 * 16) 2999a2dd95SBruce Richardson #define MAX_CONNECTIONS 10 3099a2dd95SBruce Richardson 3199a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 3299a2dd95SBruce Richardson static void * 3399a2dd95SBruce Richardson client_handler(void *socket); 3499a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 3599a2dd95SBruce Richardson 3699a2dd95SBruce Richardson struct cmd_callback { 3799a2dd95SBruce Richardson char cmd[MAX_CMD_LEN]; 3899a2dd95SBruce Richardson telemetry_cb fn; 3999a2dd95SBruce Richardson char help[MAX_HELP_LEN]; 4099a2dd95SBruce Richardson }; 4199a2dd95SBruce Richardson 4299a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 4399a2dd95SBruce Richardson struct socket { 4499a2dd95SBruce Richardson int sock; 4599a2dd95SBruce Richardson char path[sizeof(((struct sockaddr_un *)0)->sun_path)]; 4699a2dd95SBruce Richardson handler fn; 4799a2dd95SBruce Richardson uint16_t *num_clients; 4899a2dd95SBruce Richardson }; 4999a2dd95SBruce Richardson static struct socket v2_socket; /* socket for v2 telemetry */ 5099a2dd95SBruce Richardson static struct socket v1_socket; /* socket for v1 telemetry */ 5199a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 5299a2dd95SBruce Richardson 5399a2dd95SBruce Richardson static const char *telemetry_version; /* save rte_version */ 5499a2dd95SBruce Richardson static const char *socket_dir; /* runtime directory */ 5599a2dd95SBruce Richardson static rte_cpuset_t *thread_cpuset; 5699a2dd95SBruce Richardson static rte_log_fn rte_log_ptr; 5799a2dd95SBruce Richardson static uint32_t logtype; 5899a2dd95SBruce Richardson 5999a2dd95SBruce Richardson #define TMTY_LOG(l, ...) \ 6099a2dd95SBruce Richardson rte_log_ptr(RTE_LOG_ ## l, logtype, "TELEMETRY: " __VA_ARGS__) 6199a2dd95SBruce Richardson 6299a2dd95SBruce Richardson /* list of command callbacks, with one command registered by default */ 6364307fadSDavid Marchand static struct cmd_callback *callbacks; 6499a2dd95SBruce Richardson static int num_callbacks; /* How many commands are registered */ 6599a2dd95SBruce Richardson /* Used when accessing or modifying list of command callbacks */ 6699a2dd95SBruce Richardson static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER; 6799a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 6899a2dd95SBruce Richardson static uint16_t v2_clients; 6999a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 7099a2dd95SBruce Richardson 7199a2dd95SBruce Richardson int 7299a2dd95SBruce Richardson rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help) 7399a2dd95SBruce Richardson { 7464307fadSDavid Marchand struct cmd_callback *new_callbacks; 7599a2dd95SBruce Richardson int i = 0; 7699a2dd95SBruce Richardson 7799a2dd95SBruce Richardson if (strlen(cmd) >= MAX_CMD_LEN || fn == NULL || cmd[0] != '/' 7899a2dd95SBruce Richardson || strlen(help) >= MAX_HELP_LEN) 7999a2dd95SBruce Richardson return -EINVAL; 8099a2dd95SBruce Richardson 8199a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 8264307fadSDavid Marchand new_callbacks = realloc(callbacks, sizeof(callbacks[0]) * (num_callbacks + 1)); 8364307fadSDavid Marchand if (new_callbacks == NULL) { 8464307fadSDavid Marchand rte_spinlock_unlock(&callback_sl); 8564307fadSDavid Marchand return -ENOMEM; 8664307fadSDavid Marchand } 8764307fadSDavid Marchand callbacks = new_callbacks; 8864307fadSDavid Marchand 8999a2dd95SBruce Richardson while (i < num_callbacks && strcmp(cmd, callbacks[i].cmd) > 0) 9099a2dd95SBruce Richardson i++; 9199a2dd95SBruce Richardson if (i != num_callbacks) 9299a2dd95SBruce Richardson /* Move elements to keep the list alphabetical */ 9399a2dd95SBruce Richardson memmove(callbacks + i + 1, callbacks + i, 9499a2dd95SBruce Richardson sizeof(struct cmd_callback) * (num_callbacks - i)); 9599a2dd95SBruce Richardson 9699a2dd95SBruce Richardson strlcpy(callbacks[i].cmd, cmd, MAX_CMD_LEN); 9799a2dd95SBruce Richardson callbacks[i].fn = fn; 9899a2dd95SBruce Richardson strlcpy(callbacks[i].help, help, MAX_HELP_LEN); 9999a2dd95SBruce Richardson num_callbacks++; 10099a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 10199a2dd95SBruce Richardson 10299a2dd95SBruce Richardson return 0; 10399a2dd95SBruce Richardson } 10499a2dd95SBruce Richardson 10599a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 10699a2dd95SBruce Richardson 10799a2dd95SBruce Richardson static int 10899a2dd95SBruce Richardson list_commands(const char *cmd __rte_unused, const char *params __rte_unused, 10999a2dd95SBruce Richardson struct rte_tel_data *d) 11099a2dd95SBruce Richardson { 11199a2dd95SBruce Richardson int i; 11299a2dd95SBruce Richardson 11399a2dd95SBruce Richardson rte_tel_data_start_array(d, RTE_TEL_STRING_VAL); 11404896027SCiara Power rte_spinlock_lock(&callback_sl); 11599a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 11699a2dd95SBruce Richardson rte_tel_data_add_array_string(d, callbacks[i].cmd); 11704896027SCiara Power rte_spinlock_unlock(&callback_sl); 11899a2dd95SBruce Richardson return 0; 11999a2dd95SBruce Richardson } 12099a2dd95SBruce Richardson 12199a2dd95SBruce Richardson static int 12299a2dd95SBruce Richardson json_info(const char *cmd __rte_unused, const char *params __rte_unused, 12399a2dd95SBruce Richardson struct rte_tel_data *d) 12499a2dd95SBruce Richardson { 12599a2dd95SBruce Richardson rte_tel_data_start_dict(d); 12699a2dd95SBruce Richardson rte_tel_data_add_dict_string(d, "version", telemetry_version); 12799a2dd95SBruce Richardson rte_tel_data_add_dict_int(d, "pid", getpid()); 12899a2dd95SBruce Richardson rte_tel_data_add_dict_int(d, "max_output_len", MAX_OUTPUT_LEN); 12999a2dd95SBruce Richardson return 0; 13099a2dd95SBruce Richardson } 13199a2dd95SBruce Richardson 13299a2dd95SBruce Richardson static int 13399a2dd95SBruce Richardson command_help(const char *cmd __rte_unused, const char *params, 13499a2dd95SBruce Richardson struct rte_tel_data *d) 13599a2dd95SBruce Richardson { 13699a2dd95SBruce Richardson int i; 13799a2dd95SBruce Richardson 13899a2dd95SBruce Richardson if (!params) 13999a2dd95SBruce Richardson return -1; 14099a2dd95SBruce Richardson rte_tel_data_start_dict(d); 14199a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 14299a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 14399a2dd95SBruce Richardson if (strcmp(params, callbacks[i].cmd) == 0) { 14499a2dd95SBruce Richardson rte_tel_data_add_dict_string(d, params, 14599a2dd95SBruce Richardson callbacks[i].help); 14699a2dd95SBruce Richardson break; 14799a2dd95SBruce Richardson } 14899a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 14999a2dd95SBruce Richardson if (i == num_callbacks) 15099a2dd95SBruce Richardson return -1; 15199a2dd95SBruce Richardson return 0; 15299a2dd95SBruce Richardson } 15399a2dd95SBruce Richardson 15499a2dd95SBruce Richardson static int 15599a2dd95SBruce Richardson container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len) 15699a2dd95SBruce Richardson { 15799a2dd95SBruce Richardson size_t used = 0; 15899a2dd95SBruce Richardson unsigned int i; 15999a2dd95SBruce Richardson 16099a2dd95SBruce Richardson if (d->type != RTE_TEL_ARRAY_U64 && d->type != RTE_TEL_ARRAY_INT 16199a2dd95SBruce Richardson && d->type != RTE_TEL_ARRAY_STRING) 16299a2dd95SBruce Richardson return snprintf(out_buf, buf_len, "null"); 16399a2dd95SBruce Richardson 16499a2dd95SBruce Richardson used = rte_tel_json_empty_array(out_buf, buf_len, 0); 16599a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_U64) 16699a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 16799a2dd95SBruce Richardson used = rte_tel_json_add_array_u64(out_buf, 16899a2dd95SBruce Richardson buf_len, used, 16999a2dd95SBruce Richardson d->data.array[i].u64val); 17099a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_INT) 17199a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 17299a2dd95SBruce Richardson used = rte_tel_json_add_array_int(out_buf, 17399a2dd95SBruce Richardson buf_len, used, 17499a2dd95SBruce Richardson d->data.array[i].ival); 17599a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_STRING) 17699a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 17799a2dd95SBruce Richardson used = rte_tel_json_add_array_string(out_buf, 17899a2dd95SBruce Richardson buf_len, used, 17999a2dd95SBruce Richardson d->data.array[i].sval); 18099a2dd95SBruce Richardson return used; 18199a2dd95SBruce Richardson } 18299a2dd95SBruce Richardson 18399a2dd95SBruce Richardson static void 18499a2dd95SBruce Richardson output_json(const char *cmd, const struct rte_tel_data *d, int s) 18599a2dd95SBruce Richardson { 18699a2dd95SBruce Richardson char out_buf[MAX_OUTPUT_LEN]; 18799a2dd95SBruce Richardson 18899a2dd95SBruce Richardson char *cb_data_buf; 18999a2dd95SBruce Richardson size_t buf_len, prefix_used, used = 0; 19099a2dd95SBruce Richardson unsigned int i; 19199a2dd95SBruce Richardson 19299a2dd95SBruce Richardson RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN + 19399a2dd95SBruce Richardson RTE_TEL_MAX_SINGLE_STRING_LEN + 10); 19499a2dd95SBruce Richardson switch (d->type) { 19599a2dd95SBruce Richardson case RTE_TEL_NULL: 19699a2dd95SBruce Richardson used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}", 19799a2dd95SBruce Richardson MAX_CMD_LEN, cmd ? cmd : "none"); 19899a2dd95SBruce Richardson break; 19999a2dd95SBruce Richardson case RTE_TEL_STRING: 20099a2dd95SBruce Richardson used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}", 20199a2dd95SBruce Richardson MAX_CMD_LEN, cmd, 20299a2dd95SBruce Richardson RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str); 20399a2dd95SBruce Richardson break; 20499a2dd95SBruce Richardson case RTE_TEL_DICT: 20599a2dd95SBruce Richardson prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", 20699a2dd95SBruce Richardson MAX_CMD_LEN, cmd); 20799a2dd95SBruce Richardson cb_data_buf = &out_buf[prefix_used]; 20899a2dd95SBruce Richardson buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ 20999a2dd95SBruce Richardson 21099a2dd95SBruce Richardson used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0); 21199a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) { 21299a2dd95SBruce Richardson const struct tel_dict_entry *v = &d->data.dict[i]; 21399a2dd95SBruce Richardson switch (v->type) { 21499a2dd95SBruce Richardson case RTE_TEL_STRING_VAL: 21599a2dd95SBruce Richardson used = rte_tel_json_add_obj_str(cb_data_buf, 21699a2dd95SBruce Richardson buf_len, used, 21799a2dd95SBruce Richardson v->name, v->value.sval); 21899a2dd95SBruce Richardson break; 21999a2dd95SBruce Richardson case RTE_TEL_INT_VAL: 22099a2dd95SBruce Richardson used = rte_tel_json_add_obj_int(cb_data_buf, 22199a2dd95SBruce Richardson buf_len, used, 22299a2dd95SBruce Richardson v->name, v->value.ival); 22399a2dd95SBruce Richardson break; 22499a2dd95SBruce Richardson case RTE_TEL_U64_VAL: 22599a2dd95SBruce Richardson used = rte_tel_json_add_obj_u64(cb_data_buf, 22699a2dd95SBruce Richardson buf_len, used, 22799a2dd95SBruce Richardson v->name, v->value.u64val); 22899a2dd95SBruce Richardson break; 22999a2dd95SBruce Richardson case RTE_TEL_CONTAINER: 23099a2dd95SBruce Richardson { 23199a2dd95SBruce Richardson char temp[buf_len]; 23299a2dd95SBruce Richardson const struct container *cont = 23399a2dd95SBruce Richardson &v->value.container; 23499a2dd95SBruce Richardson if (container_to_json(cont->data, 23599a2dd95SBruce Richardson temp, buf_len) != 0) 23699a2dd95SBruce Richardson used = rte_tel_json_add_obj_json( 23799a2dd95SBruce Richardson cb_data_buf, 23899a2dd95SBruce Richardson buf_len, used, 23999a2dd95SBruce Richardson v->name, temp); 24099a2dd95SBruce Richardson if (!cont->keep) 24199a2dd95SBruce Richardson rte_tel_data_free(cont->data); 24299a2dd95SBruce Richardson } 24399a2dd95SBruce Richardson } 24499a2dd95SBruce Richardson } 24599a2dd95SBruce Richardson used += prefix_used; 24699a2dd95SBruce Richardson used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); 24799a2dd95SBruce Richardson break; 24899a2dd95SBruce Richardson case RTE_TEL_ARRAY_STRING: 24999a2dd95SBruce Richardson case RTE_TEL_ARRAY_INT: 25099a2dd95SBruce Richardson case RTE_TEL_ARRAY_U64: 25199a2dd95SBruce Richardson case RTE_TEL_ARRAY_CONTAINER: 25299a2dd95SBruce Richardson prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", 25399a2dd95SBruce Richardson MAX_CMD_LEN, cmd); 25499a2dd95SBruce Richardson cb_data_buf = &out_buf[prefix_used]; 25599a2dd95SBruce Richardson buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ 25699a2dd95SBruce Richardson 25799a2dd95SBruce Richardson used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0); 25899a2dd95SBruce Richardson for (i = 0; i < d->data_len; i++) 25999a2dd95SBruce Richardson if (d->type == RTE_TEL_ARRAY_STRING) 26099a2dd95SBruce Richardson used = rte_tel_json_add_array_string( 26199a2dd95SBruce Richardson cb_data_buf, 26299a2dd95SBruce Richardson buf_len, used, 26399a2dd95SBruce Richardson d->data.array[i].sval); 26499a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_INT) 26599a2dd95SBruce Richardson used = rte_tel_json_add_array_int(cb_data_buf, 26699a2dd95SBruce Richardson buf_len, used, 26799a2dd95SBruce Richardson d->data.array[i].ival); 26899a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_U64) 26999a2dd95SBruce Richardson used = rte_tel_json_add_array_u64(cb_data_buf, 27099a2dd95SBruce Richardson buf_len, used, 27199a2dd95SBruce Richardson d->data.array[i].u64val); 27299a2dd95SBruce Richardson else if (d->type == RTE_TEL_ARRAY_CONTAINER) { 27399a2dd95SBruce Richardson char temp[buf_len]; 27499a2dd95SBruce Richardson const struct container *rec_data = 27599a2dd95SBruce Richardson &d->data.array[i].container; 27699a2dd95SBruce Richardson if (container_to_json(rec_data->data, 27799a2dd95SBruce Richardson temp, buf_len) != 0) 27899a2dd95SBruce Richardson used = rte_tel_json_add_array_json( 27999a2dd95SBruce Richardson cb_data_buf, 28099a2dd95SBruce Richardson buf_len, used, temp); 28199a2dd95SBruce Richardson if (!rec_data->keep) 28299a2dd95SBruce Richardson rte_tel_data_free(rec_data->data); 28399a2dd95SBruce Richardson } 28499a2dd95SBruce Richardson used += prefix_used; 28599a2dd95SBruce Richardson used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); 28699a2dd95SBruce Richardson break; 28799a2dd95SBruce Richardson } 28899a2dd95SBruce Richardson if (write(s, out_buf, used) < 0) 28999a2dd95SBruce Richardson perror("Error writing to socket"); 29099a2dd95SBruce Richardson } 29199a2dd95SBruce Richardson 29299a2dd95SBruce Richardson static void 29399a2dd95SBruce Richardson perform_command(telemetry_cb fn, const char *cmd, const char *param, int s) 29499a2dd95SBruce Richardson { 29599a2dd95SBruce Richardson struct rte_tel_data data; 29699a2dd95SBruce Richardson 29799a2dd95SBruce Richardson int ret = fn(cmd, param, &data); 29899a2dd95SBruce Richardson if (ret < 0) { 29999a2dd95SBruce Richardson char out_buf[MAX_CMD_LEN + 10]; 30099a2dd95SBruce Richardson int used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}", 30199a2dd95SBruce Richardson MAX_CMD_LEN, cmd ? cmd : "none"); 30299a2dd95SBruce Richardson if (write(s, out_buf, used) < 0) 30399a2dd95SBruce Richardson perror("Error writing to socket"); 30499a2dd95SBruce Richardson return; 30599a2dd95SBruce Richardson } 30699a2dd95SBruce Richardson output_json(cmd, &data, s); 30799a2dd95SBruce Richardson } 30899a2dd95SBruce Richardson 30999a2dd95SBruce Richardson static int 31099a2dd95SBruce Richardson unknown_command(const char *cmd __rte_unused, const char *params __rte_unused, 31199a2dd95SBruce Richardson struct rte_tel_data *d) 31299a2dd95SBruce Richardson { 31399a2dd95SBruce Richardson return d->type = RTE_TEL_NULL; 31499a2dd95SBruce Richardson } 31599a2dd95SBruce Richardson 31699a2dd95SBruce Richardson static void * 31799a2dd95SBruce Richardson client_handler(void *sock_id) 31899a2dd95SBruce Richardson { 31999a2dd95SBruce Richardson int s = (int)(uintptr_t)sock_id; 32099a2dd95SBruce Richardson char buffer[1024]; 32199a2dd95SBruce Richardson char info_str[1024]; 32299a2dd95SBruce Richardson snprintf(info_str, sizeof(info_str), 32399a2dd95SBruce Richardson "{\"version\":\"%s\",\"pid\":%d,\"max_output_len\":%d}", 32499a2dd95SBruce Richardson telemetry_version, getpid(), MAX_OUTPUT_LEN); 32599a2dd95SBruce Richardson if (write(s, info_str, strlen(info_str)) < 0) { 32699a2dd95SBruce Richardson close(s); 32799a2dd95SBruce Richardson return NULL; 32899a2dd95SBruce Richardson } 32999a2dd95SBruce Richardson 33099a2dd95SBruce Richardson /* receive data is not null terminated */ 33199a2dd95SBruce Richardson int bytes = read(s, buffer, sizeof(buffer) - 1); 33299a2dd95SBruce Richardson while (bytes > 0) { 33399a2dd95SBruce Richardson buffer[bytes] = 0; 33499a2dd95SBruce Richardson const char *cmd = strtok(buffer, ","); 33599a2dd95SBruce Richardson const char *param = strtok(NULL, "\0"); 33699a2dd95SBruce Richardson telemetry_cb fn = unknown_command; 33799a2dd95SBruce Richardson int i; 33899a2dd95SBruce Richardson 33999a2dd95SBruce Richardson if (cmd && strlen(cmd) < MAX_CMD_LEN) { 34099a2dd95SBruce Richardson rte_spinlock_lock(&callback_sl); 34199a2dd95SBruce Richardson for (i = 0; i < num_callbacks; i++) 34299a2dd95SBruce Richardson if (strcmp(cmd, callbacks[i].cmd) == 0) { 34399a2dd95SBruce Richardson fn = callbacks[i].fn; 34499a2dd95SBruce Richardson break; 34599a2dd95SBruce Richardson } 34699a2dd95SBruce Richardson rte_spinlock_unlock(&callback_sl); 34799a2dd95SBruce Richardson } 34899a2dd95SBruce Richardson perform_command(fn, cmd, param, s); 34999a2dd95SBruce Richardson 35099a2dd95SBruce Richardson bytes = read(s, buffer, sizeof(buffer) - 1); 35199a2dd95SBruce Richardson } 35299a2dd95SBruce Richardson close(s); 35399a2dd95SBruce Richardson __atomic_sub_fetch(&v2_clients, 1, __ATOMIC_RELAXED); 35499a2dd95SBruce Richardson return NULL; 35599a2dd95SBruce Richardson } 35699a2dd95SBruce Richardson 35799a2dd95SBruce Richardson static void * 35899a2dd95SBruce Richardson socket_listener(void *socket) 35999a2dd95SBruce Richardson { 36099a2dd95SBruce Richardson while (1) { 36199a2dd95SBruce Richardson pthread_t th; 362c53a5f3eSChengwen Feng int rc; 36399a2dd95SBruce Richardson struct socket *s = (struct socket *)socket; 36499a2dd95SBruce Richardson int s_accepted = accept(s->sock, NULL, NULL); 36599a2dd95SBruce Richardson if (s_accepted < 0) { 36699a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with accept, telemetry thread quitting\n"); 36799a2dd95SBruce Richardson return NULL; 36899a2dd95SBruce Richardson } 36999a2dd95SBruce Richardson if (s->num_clients != NULL) { 37099a2dd95SBruce Richardson uint16_t conns = __atomic_load_n(s->num_clients, 37199a2dd95SBruce Richardson __ATOMIC_RELAXED); 37299a2dd95SBruce Richardson if (conns >= MAX_CONNECTIONS) { 37399a2dd95SBruce Richardson close(s_accepted); 37499a2dd95SBruce Richardson continue; 37599a2dd95SBruce Richardson } 37699a2dd95SBruce Richardson __atomic_add_fetch(s->num_clients, 1, 37799a2dd95SBruce Richardson __ATOMIC_RELAXED); 37899a2dd95SBruce Richardson } 379c53a5f3eSChengwen Feng rc = pthread_create(&th, NULL, s->fn, 380c53a5f3eSChengwen Feng (void *)(uintptr_t)s_accepted); 381c53a5f3eSChengwen Feng if (rc != 0) { 382c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create client thread: %s\n", 383c53a5f3eSChengwen Feng strerror(rc)); 384c53a5f3eSChengwen Feng close(s_accepted); 385c53a5f3eSChengwen Feng if (s->num_clients != NULL) 386c53a5f3eSChengwen Feng __atomic_sub_fetch(s->num_clients, 1, 387c53a5f3eSChengwen Feng __ATOMIC_RELAXED); 388c53a5f3eSChengwen Feng continue; 389c53a5f3eSChengwen Feng } 39099a2dd95SBruce Richardson pthread_detach(th); 39199a2dd95SBruce Richardson } 39299a2dd95SBruce Richardson return NULL; 39399a2dd95SBruce Richardson } 39499a2dd95SBruce Richardson 39599a2dd95SBruce Richardson static inline char * 39699a2dd95SBruce Richardson get_socket_path(const char *runtime_dir, const int version) 39799a2dd95SBruce Richardson { 39899a2dd95SBruce Richardson static char path[PATH_MAX]; 39999a2dd95SBruce Richardson snprintf(path, sizeof(path), "%s/dpdk_telemetry.v%d", 40099a2dd95SBruce Richardson strlen(runtime_dir) ? runtime_dir : "/tmp", version); 40199a2dd95SBruce Richardson return path; 40299a2dd95SBruce Richardson } 40399a2dd95SBruce Richardson 40499a2dd95SBruce Richardson static void 40599a2dd95SBruce Richardson unlink_sockets(void) 40699a2dd95SBruce Richardson { 40799a2dd95SBruce Richardson if (v2_socket.path[0]) 40899a2dd95SBruce Richardson unlink(v2_socket.path); 40999a2dd95SBruce Richardson if (v1_socket.path[0]) 41099a2dd95SBruce Richardson unlink(v1_socket.path); 41199a2dd95SBruce Richardson } 41299a2dd95SBruce Richardson 41399a2dd95SBruce Richardson static int 41499a2dd95SBruce Richardson create_socket(char *path) 41599a2dd95SBruce Richardson { 41699a2dd95SBruce Richardson int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); 41799a2dd95SBruce Richardson if (sock < 0) { 41899a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket creation, %s\n", strerror(errno)); 41999a2dd95SBruce Richardson return -1; 42099a2dd95SBruce Richardson } 42199a2dd95SBruce Richardson 42299a2dd95SBruce Richardson struct sockaddr_un sun = {.sun_family = AF_UNIX}; 42399a2dd95SBruce Richardson strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 42499a2dd95SBruce Richardson unlink(sun.sun_path); 42599a2dd95SBruce Richardson if (bind(sock, (void *) &sun, sizeof(sun)) < 0) { 426*d5252f7dSBruce Richardson struct stat st; 427*d5252f7dSBruce Richardson 42899a2dd95SBruce Richardson TMTY_LOG(ERR, "Error binding socket: %s\n", strerror(errno)); 429*d5252f7dSBruce Richardson if (stat(socket_dir, &st) < 0 || !S_ISDIR(st.st_mode)) 430*d5252f7dSBruce Richardson TMTY_LOG(ERR, "Cannot access DPDK runtime directory: %s\n", socket_dir); 43199a2dd95SBruce Richardson sun.sun_path[0] = 0; 43299a2dd95SBruce Richardson goto error; 43399a2dd95SBruce Richardson } 43499a2dd95SBruce Richardson 43599a2dd95SBruce Richardson if (listen(sock, 1) < 0) { 43699a2dd95SBruce Richardson TMTY_LOG(ERR, "Error calling listen for socket: %s\n", strerror(errno)); 43799a2dd95SBruce Richardson goto error; 43899a2dd95SBruce Richardson } 43999a2dd95SBruce Richardson 44099a2dd95SBruce Richardson return sock; 44199a2dd95SBruce Richardson 44299a2dd95SBruce Richardson error: 44399a2dd95SBruce Richardson close(sock); 44499a2dd95SBruce Richardson unlink_sockets(); 44599a2dd95SBruce Richardson return -1; 44699a2dd95SBruce Richardson } 44799a2dd95SBruce Richardson 448027c931bSThomas Monjalon static void 449027c931bSThomas Monjalon set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused) 450027c931bSThomas Monjalon { 451027c931bSThomas Monjalon #if defined RTE_EXEC_ENV_LINUX && defined __GLIBC__ && defined __GLIBC_PREREQ 452027c931bSThomas Monjalon #if __GLIBC_PREREQ(2, 12) 453027c931bSThomas Monjalon pthread_setname_np(id, name); 454027c931bSThomas Monjalon #endif 455027c931bSThomas Monjalon #elif defined RTE_EXEC_ENV_FREEBSD 456027c931bSThomas Monjalon pthread_set_name_np(id, name); 457027c931bSThomas Monjalon #endif 458027c931bSThomas Monjalon } 459027c931bSThomas Monjalon 46099a2dd95SBruce Richardson static int 46199a2dd95SBruce Richardson telemetry_legacy_init(void) 46299a2dd95SBruce Richardson { 46399a2dd95SBruce Richardson pthread_t t_old; 464c53a5f3eSChengwen Feng int rc; 46599a2dd95SBruce Richardson 46699a2dd95SBruce Richardson if (num_legacy_callbacks == 1) { 46799a2dd95SBruce Richardson TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n"); 46899a2dd95SBruce Richardson return -1; 46999a2dd95SBruce Richardson } 47099a2dd95SBruce Richardson 47199a2dd95SBruce Richardson v1_socket.fn = legacy_client_handler; 47299a2dd95SBruce Richardson if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path), 47399a2dd95SBruce Richardson "%s/telemetry", socket_dir) >= sizeof(v1_socket.path)) { 47499a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket binding, path too long\n"); 47599a2dd95SBruce Richardson return -1; 47699a2dd95SBruce Richardson } 47799a2dd95SBruce Richardson v1_socket.sock = create_socket(v1_socket.path); 47899a2dd95SBruce Richardson if (v1_socket.sock < 0) 47999a2dd95SBruce Richardson return -1; 480c53a5f3eSChengwen Feng rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket); 481c53a5f3eSChengwen Feng if (rc != 0) { 482c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n", 483c53a5f3eSChengwen Feng strerror(rc)); 484c53a5f3eSChengwen Feng close(v1_socket.sock); 485c53a5f3eSChengwen Feng v1_socket.sock = -1; 486c53a5f3eSChengwen Feng unlink(v1_socket.path); 487c53a5f3eSChengwen Feng v1_socket.path[0] = '\0'; 488c53a5f3eSChengwen Feng return -1; 489c53a5f3eSChengwen Feng } 49099a2dd95SBruce Richardson pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset); 491027c931bSThomas Monjalon set_thread_name(t_old, "telemetry-v1"); 49299a2dd95SBruce Richardson TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n"); 49399a2dd95SBruce Richardson return 0; 49499a2dd95SBruce Richardson } 49599a2dd95SBruce Richardson 49699a2dd95SBruce Richardson static int 49799a2dd95SBruce Richardson telemetry_v2_init(void) 49899a2dd95SBruce Richardson { 49999a2dd95SBruce Richardson pthread_t t_new; 500c53a5f3eSChengwen Feng int rc; 50199a2dd95SBruce Richardson 50299a2dd95SBruce Richardson v2_socket.num_clients = &v2_clients; 50399a2dd95SBruce Richardson rte_telemetry_register_cmd("/", list_commands, 50499a2dd95SBruce Richardson "Returns list of available commands, Takes no parameters"); 50599a2dd95SBruce Richardson rte_telemetry_register_cmd("/info", json_info, 50699a2dd95SBruce Richardson "Returns DPDK Telemetry information. Takes no parameters"); 50799a2dd95SBruce Richardson rte_telemetry_register_cmd("/help", command_help, 50899a2dd95SBruce Richardson "Returns help text for a command. Parameters: string command"); 50999a2dd95SBruce Richardson v2_socket.fn = client_handler; 51099a2dd95SBruce Richardson if (strlcpy(v2_socket.path, get_socket_path(socket_dir, 2), 51199a2dd95SBruce Richardson sizeof(v2_socket.path)) >= sizeof(v2_socket.path)) { 51299a2dd95SBruce Richardson TMTY_LOG(ERR, "Error with socket binding, path too long\n"); 51399a2dd95SBruce Richardson return -1; 51499a2dd95SBruce Richardson } 51599a2dd95SBruce Richardson 51699a2dd95SBruce Richardson v2_socket.sock = create_socket(v2_socket.path); 51799a2dd95SBruce Richardson if (v2_socket.sock < 0) 51899a2dd95SBruce Richardson return -1; 519c53a5f3eSChengwen Feng rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket); 520c53a5f3eSChengwen Feng if (rc != 0) { 521c53a5f3eSChengwen Feng TMTY_LOG(ERR, "Error with create socket thread: %s\n", 522c53a5f3eSChengwen Feng strerror(rc)); 523c53a5f3eSChengwen Feng close(v2_socket.sock); 524c53a5f3eSChengwen Feng v2_socket.sock = -1; 525c53a5f3eSChengwen Feng unlink(v2_socket.path); 526c53a5f3eSChengwen Feng v2_socket.path[0] = '\0'; 527c53a5f3eSChengwen Feng return -1; 528c53a5f3eSChengwen Feng } 52999a2dd95SBruce Richardson pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset); 530027c931bSThomas Monjalon set_thread_name(t_new, "telemetry-v2"); 53199a2dd95SBruce Richardson atexit(unlink_sockets); 53299a2dd95SBruce Richardson 53399a2dd95SBruce Richardson return 0; 53499a2dd95SBruce Richardson } 53599a2dd95SBruce Richardson 53699a2dd95SBruce Richardson #endif /* !RTE_EXEC_ENV_WINDOWS */ 53799a2dd95SBruce Richardson 53899a2dd95SBruce Richardson int32_t 53999a2dd95SBruce Richardson rte_telemetry_init(const char *runtime_dir, const char *rte_version, rte_cpuset_t *cpuset, 54099a2dd95SBruce Richardson rte_log_fn log_fn, uint32_t registered_logtype) 54199a2dd95SBruce Richardson { 54299a2dd95SBruce Richardson telemetry_version = rte_version; 54399a2dd95SBruce Richardson socket_dir = runtime_dir; 54499a2dd95SBruce Richardson thread_cpuset = cpuset; 54599a2dd95SBruce Richardson rte_log_ptr = log_fn; 54699a2dd95SBruce Richardson logtype = registered_logtype; 54799a2dd95SBruce Richardson 54899a2dd95SBruce Richardson #ifndef RTE_EXEC_ENV_WINDOWS 54999a2dd95SBruce Richardson if (telemetry_v2_init() != 0) 55099a2dd95SBruce Richardson return -1; 55199a2dd95SBruce Richardson TMTY_LOG(DEBUG, "Telemetry initialized ok\n"); 55299a2dd95SBruce Richardson telemetry_legacy_init(); 55399a2dd95SBruce Richardson #endif /* RTE_EXEC_ENV_WINDOWS */ 55499a2dd95SBruce Richardson 55599a2dd95SBruce Richardson return 0; 55699a2dd95SBruce Richardson } 557