1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
6 #include <sys/socket.h>
10 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
12 #include <rte_string_fns.h>
13 #include <rte_common.h>
14 #include <rte_spinlock.h>
16 #include "rte_telemetry_legacy.h"
20 #define CLIENTS_UNREG_ACTION "\"action\":2"
21 #define CLIENTS_CMD "\"command\":\"clients\""
22 #define CLIENTS_DATA "\"data\":{\"client_path\":\""
23 #define STATS_ACTION "\"action\":0"
24 #define DATA_REQ_LABEL "\"data\":"
25 #define TELEMETRY_LEGACY_MAX_CALLBACKS 4
29 register_client(const char *cmd __rte_unused,
30 const char *params __rte_unused,
31 char *buffer, int buf_len);
37 telemetry_legacy_cb fn;
41 struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
43 .action = "\"action\":1",
49 int num_legacy_callbacks = 1;
50 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
53 rte_telemetry_legacy_register(const char *cmd,
54 enum rte_telemetry_legacy_data_req data_req,
55 telemetry_legacy_cb fn)
59 if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
62 rte_spinlock_lock(&callback_sl);
63 strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
64 snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
65 "\"command\":\"%s\"", cmd);
66 snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
67 data_req ? "%s{\"" : "%snull",
69 callbacks[num_legacy_callbacks].fn = fn;
70 num_legacy_callbacks++;
71 rte_spinlock_unlock(&callback_sl);
77 register_client(const char *cmd __rte_unused, const char *params,
78 char *buffer __rte_unused, int buf_len __rte_unused)
83 struct sockaddr_un addrs;
85 if (!strchr(params, ':')) {
86 fprintf(stderr, "Invalid data\n");
89 strlcpy(data, strchr(params, ':'), sizeof(data));
90 memcpy(data, &data[strlen(":\"")], strlen(data));
91 if (!strchr(data, '\"')) {
92 fprintf(stderr, "Invalid client data\n");
95 *strchr(data, '\"') = 0;
97 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
98 addrs.sun_family = AF_UNIX;
99 strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
101 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
102 perror("\nClient connection error\n");
106 pthread_create(&th, NULL, &legacy_client_handler,
107 (void *)(uintptr_t)fd);
112 send_error_response(int s, int err)
115 char out_buf[100000];
119 desc = "Memory Allocation Error";
122 desc = "Invalid Argument 404";
128 /* Default case keeps behaviour of Telemetry library */
129 printf("\nInvalid error type: %d\n", err);
132 int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
133 "\"Status Error: %s\", \"data\": null}", desc);
134 if (write(s, out_buf, used) < 0) {
135 perror("Error writing to socket");
142 perform_command(telemetry_legacy_cb fn, const char *param, int s)
144 char out_buf[100000];
147 ret = fn("", param, out_buf, sizeof(out_buf));
149 ret = send_error_response(s, ret);
151 printf("\nCould not send error response\n");
155 if (write(s, out_buf, used) < 0)
156 perror("Error writing to socket");
160 parse_client_request(char *buffer, int buf_len, int s)
163 char *data = buffer + buf_len;
164 telemetry_legacy_cb fn = NULL;
165 const char *valid_sep = ",}";
166 if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
169 if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
170 && strstr(buffer, CLIENTS_DATA))
173 for (i = 0; i < num_legacy_callbacks; i++) {
174 char *action_ptr = strstr(buffer, callbacks[i].action);
175 char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
176 char *data_ptr = strstr(buffer, callbacks[i].data);
177 if (!action_ptr || !cmd_ptr || !data_ptr)
180 char action_sep = action_ptr[strlen(callbacks[i].action)];
181 char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
182 if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
187 if (!strchr(data_ptr, '{'))
188 data_sep = data_ptr[strlen(callbacks[i].data)];
190 if (!strchr(data_ptr, '}'))
192 char *data_end = strchr(data_ptr, '}');
193 data = data_ptr + strlen(DATA_REQ_LABEL);
194 data_sep = data_end[1];
197 if (!strchr(valid_sep, data_sep))
199 fn = callbacks[i].fn;
205 perform_command(fn, data, s);
210 legacy_client_handler(void *sock_id)
212 int s = (int)(uintptr_t)sock_id;
214 char buffer_recv[BUF_SIZE];
215 /* receive data is not null terminated */
216 int bytes = read(s, buffer_recv, sizeof(buffer_recv));
219 buffer_recv[bytes] = 0;
221 char buffer[BUF_SIZE];
222 for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
223 buffer[j] = buffer_recv[i];
224 j += !isspace(buffer_recv[i]);
227 ret = parse_client_request(buffer, j, s);
229 ret = send_error_response(s, ret);
231 printf("\nCould not send error response\n");
233 bytes = read(s, buffer_recv, sizeof(buffer_recv));