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",
124 #endif /* !RTE_EXEC_ENV_WINDOWS */
128 #ifndef RTE_EXEC_ENV_WINDOWS
131 send_error_response(int s, int err)
134 char out_buf[100000];
138 desc = "Memory Allocation Error";
141 desc = "Invalid Argument 404";
147 /* Default case keeps behaviour of Telemetry library */
148 printf("\nInvalid error type: %d\n", err);
151 int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
152 "\"Status Error: %s\", \"data\": null}", desc);
153 if (write(s, out_buf, used) < 0) {
154 perror("Error writing to socket");
161 perform_command(telemetry_legacy_cb fn, const char *param, int s)
163 char out_buf[100000];
166 ret = fn("", param, out_buf, sizeof(out_buf));
168 ret = send_error_response(s, ret);
170 printf("\nCould not send error response\n");
174 if (write(s, out_buf, used) < 0)
175 perror("Error writing to socket");
179 parse_client_request(char *buffer, int buf_len, int s)
182 char *data = buffer + buf_len;
183 telemetry_legacy_cb fn = NULL;
184 const char *valid_sep = ",}";
185 if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
188 if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
189 && strstr(buffer, CLIENTS_DATA))
192 for (i = 0; i < num_legacy_callbacks; i++) {
193 char *action_ptr = strstr(buffer, callbacks[i].action);
194 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
195 char *data_ptr = strstr(buffer, callbacks[i].data);
196 if (!action_ptr || !cmd_ptr || !data_ptr)
199 char action_sep = action_ptr[strlen(callbacks[i].action)];
200 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
201 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
206 if (!strchr(data_ptr, '{'))
207 data_sep = data_ptr[strlen(callbacks[i].data)];
209 if (!strchr(data_ptr, '}'))
211 char *data_end = strchr(data_ptr, '}');
212 data = data_ptr + strlen(DATA_REQ_LABEL);
213 data_sep = data_end[1];
216 if (!strchr(valid_sep, data_sep))
218 fn = callbacks[i].fn;
224 perform_command(fn, data, s);
229 legacy_client_handler(void *sock_id)
231 int s = (int)(uintptr_t)sock_id;
233 char buffer_recv[BUF_SIZE];
234 /* receive data is not null terminated */
235 int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
238 buffer_recv[bytes] = 0;
240 char buffer[BUF_SIZE];
241 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
242 buffer[j] = buffer_recv[i];
243 j += !isspace(buffer_recv[i]);
246 ret = parse_client_request(buffer, j, s);
248 ret = send_error_response(s, ret);
250 printf("\nCould not send error response\n");
252 bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
258 #endif /* !RTE_EXEC_ENV_WINDOWS */