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
87 struct sockaddr_un addrs;
88 #endif /* !RTE_EXEC_ENV_WINDOWS */
90 if (!strchr(params, ':')) {
91 fprintf(stderr, "Invalid data\n");
94 #ifndef RTE_EXEC_ENV_WINDOWS
95 strlcpy(data, strchr(params, ':'), sizeof(data));
96 memcpy(data, &data[strlen(":\"")], strlen(data));
97 if (!strchr(data, '\"')) {
98 fprintf(stderr, "Invalid client data\n");
101 *strchr(data, '\"') = 0;
103 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
105 perror("Failed to open socket");
108 addrs.sun_family = AF_UNIX;
109 strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
111 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
112 perror("\nClient connection error\n");
116 rc = pthread_create(&th, NULL, &legacy_client_handler,
117 (void *)(uintptr_t)fd);
119 fprintf(stderr, "Failed to create legacy client thread: %s\n",
125 #endif /* !RTE_EXEC_ENV_WINDOWS */
129 #ifndef RTE_EXEC_ENV_WINDOWS
132 send_error_response(int s, int err)
135 char out_buf[100000];
139 desc = "Memory Allocation Error";
142 desc = "Invalid Argument 404";
148 /* Default case keeps behaviour of Telemetry library */
149 printf("\nInvalid error type: %d\n", err);
152 int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
153 "\"Status Error: %s\", \"data\": null}", desc);
154 if (write(s, out_buf, used) < 0) {
155 perror("Error writing to socket");
162 perform_command(telemetry_legacy_cb fn, const char *param, int s)
164 char out_buf[100000];
167 ret = fn("", param, out_buf, sizeof(out_buf));
169 ret = send_error_response(s, ret);
171 printf("\nCould not send error response\n");
175 if (write(s, out_buf, used) < 0)
176 perror("Error writing to socket");
180 parse_client_request(char *buffer, int buf_len, int s)
183 char *data = buffer + buf_len;
184 telemetry_legacy_cb fn = NULL;
185 const char *valid_sep = ",}";
186 if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
189 if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
190 && strstr(buffer, CLIENTS_DATA))
193 for (i = 0; i < num_legacy_callbacks; i++) {
194 char *action_ptr = strstr(buffer, callbacks[i].action);
195 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
196 char *data_ptr = strstr(buffer, callbacks[i].data);
197 if (!action_ptr || !cmd_ptr || !data_ptr)
200 char action_sep = action_ptr[strlen(callbacks[i].action)];
201 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
202 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
207 if (!strchr(data_ptr, '{'))
208 data_sep = data_ptr[strlen(callbacks[i].data)];
210 if (!strchr(data_ptr, '}'))
212 char *data_end = strchr(data_ptr, '}');
213 data = data_ptr + strlen(DATA_REQ_LABEL);
214 data_sep = data_end[1];
217 if (!strchr(valid_sep, data_sep))
219 fn = callbacks[i].fn;
225 perform_command(fn, data, s);
230 legacy_client_handler(void *sock_id)
232 int s = (int)(uintptr_t)sock_id;
234 char buffer_recv[BUF_SIZE];
235 /* receive data is not null terminated */
236 int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
239 buffer_recv[bytes] = 0;
241 char buffer[BUF_SIZE];
242 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
243 buffer[j] = buffer_recv[i];
244 j += !isspace(buffer_recv[i]);
247 ret = parse_client_request(buffer, j, s);
249 ret = send_error_response(s, ret);
251 printf("\nCould not send error response\n");
253 bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
259 #endif /* !RTE_EXEC_ENV_WINDOWS */