1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #ifndef RTE_EXEC_ENV_WINDOWS
7 #include <sys/socket.h>
10 #endif /* !RTE_EXEC_ENV_WINDOWS */
12 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
14 #include <rte_string_fns.h>
15 #include <rte_common.h>
16 #include <rte_spinlock.h>
18 #include "telemetry_internal.h"
22 #define CLIENTS_UNREG_ACTION "\"action\":2"
23 #define CLIENTS_CMD "\"command\":\"clients\""
24 #define CLIENTS_DATA "\"data\":{\"client_path\":\""
25 #define STATS_ACTION "\"action\":0"
26 #define DATA_REQ_LABEL "\"data\":"
27 #define TELEMETRY_LEGACY_MAX_CALLBACKS 4
31 register_client(const char *cmd __rte_unused,
32 const char *params __rte_unused,
33 char *buffer, int buf_len);
39 telemetry_legacy_cb fn;
43 struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
45 .action = "\"action\":1",
51 int num_legacy_callbacks = 1;
52 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
55 rte_telemetry_legacy_register(const char *cmd,
56 enum rte_telemetry_legacy_data_req data_req,
57 telemetry_legacy_cb fn)
61 if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
64 rte_spinlock_lock(&callback_sl);
65 strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
66 snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
67 "\"command\":\"%s\"", cmd);
68 snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
69 data_req ? "%s{\"" : "%snull",
71 callbacks[num_legacy_callbacks].fn = fn;
72 num_legacy_callbacks++;
73 rte_spinlock_unlock(&callback_sl);
79 register_client(const char *cmd __rte_unused, const char *params,
80 char *buffer __rte_unused, int buf_len __rte_unused)
82 #ifndef RTE_EXEC_ENV_WINDOWS
86 struct sockaddr_un addrs;
87 #endif /* !RTE_EXEC_ENV_WINDOWS */
89 if (!strchr(params, ':')) {
90 fprintf(stderr, "Invalid data\n");
93 #ifndef RTE_EXEC_ENV_WINDOWS
94 strlcpy(data, strchr(params, ':'), sizeof(data));
95 memcpy(data, &data[strlen(":\"")], strlen(data));
96 if (!strchr(data, '\"')) {
97 fprintf(stderr, "Invalid client data\n");
100 *strchr(data, '\"') = 0;
102 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
104 perror("Failed to open socket");
107 addrs.sun_family = AF_UNIX;
108 strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
110 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
111 perror("\nClient connection error\n");
115 pthread_create(&th, NULL, &legacy_client_handler,
116 (void *)(uintptr_t)fd);
117 #endif /* !RTE_EXEC_ENV_WINDOWS */
121 #ifndef RTE_EXEC_ENV_WINDOWS
124 send_error_response(int s, int err)
127 char out_buf[100000];
131 desc = "Memory Allocation Error";
134 desc = "Invalid Argument 404";
140 /* Default case keeps behaviour of Telemetry library */
141 printf("\nInvalid error type: %d\n", err);
144 int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
145 "\"Status Error: %s\", \"data\": null}", desc);
146 if (write(s, out_buf, used) < 0) {
147 perror("Error writing to socket");
154 perform_command(telemetry_legacy_cb fn, const char *param, int s)
156 char out_buf[100000];
159 ret = fn("", param, out_buf, sizeof(out_buf));
161 ret = send_error_response(s, ret);
163 printf("\nCould not send error response\n");
167 if (write(s, out_buf, used) < 0)
168 perror("Error writing to socket");
172 parse_client_request(char *buffer, int buf_len, int s)
175 char *data = buffer + buf_len;
176 telemetry_legacy_cb fn = NULL;
177 const char *valid_sep = ",}";
178 if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
181 if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
182 && strstr(buffer, CLIENTS_DATA))
185 for (i = 0; i < num_legacy_callbacks; i++) {
186 char *action_ptr = strstr(buffer, callbacks[i].action);
187 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
188 char *data_ptr = strstr(buffer, callbacks[i].data);
189 if (!action_ptr || !cmd_ptr || !data_ptr)
192 char action_sep = action_ptr[strlen(callbacks[i].action)];
193 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
194 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
199 if (!strchr(data_ptr, '{'))
200 data_sep = data_ptr[strlen(callbacks[i].data)];
202 if (!strchr(data_ptr, '}'))
204 char *data_end = strchr(data_ptr, '}');
205 data = data_ptr + strlen(DATA_REQ_LABEL);
206 data_sep = data_end[1];
209 if (!strchr(valid_sep, data_sep))
211 fn = callbacks[i].fn;
217 perform_command(fn, data, s);
222 legacy_client_handler(void *sock_id)
224 int s = (int)(uintptr_t)sock_id;
226 char buffer_recv[BUF_SIZE];
227 /* receive data is not null terminated */
228 int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
231 buffer_recv[bytes] = 0;
233 char buffer[BUF_SIZE];
234 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
235 buffer[j] = buffer_recv[i];
236 j += !isspace(buffer_recv[i]);
239 ret = parse_client_request(buffer, j, s);
241 ret = send_error_response(s, ret);
243 printf("\nCould not send error response\n");
245 bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
251 #endif /* !RTE_EXEC_ENV_WINDOWS */