Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2020 Intel Corporation
3 : : */
4 : : #include <ctype.h>
5 : : #include <errno.h>
6 : : #ifndef RTE_EXEC_ENV_WINDOWS
7 : : #include <unistd.h>
8 : : #include <sys/socket.h>
9 : : #include <sys/un.h>
10 : : #include <pthread.h>
11 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
12 : :
13 : : /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
14 : : #undef RTE_USE_LIBBSD
15 : : #include <eal_export.h>
16 : : #include <rte_string_fns.h>
17 : : #include <rte_common.h>
18 : : #include <rte_spinlock.h>
19 : :
20 : : #include "telemetry_internal.h"
21 : :
22 : : #define MAX_LEN 128
23 : : #define BUF_SIZE 1024
24 : : #define CLIENTS_UNREG_ACTION "\"action\":2"
25 : : #define CLIENTS_CMD "\"command\":\"clients\""
26 : : #define CLIENTS_DATA "\"data\":{\"client_path\":\""
27 : : #define STATS_ACTION "\"action\":0"
28 : : #define DATA_REQ_LABEL "\"data\":"
29 : : #define TELEMETRY_LEGACY_MAX_CALLBACKS 4
30 : :
31 : :
32 : : static int
33 : : register_client(const char *cmd __rte_unused,
34 : : const char *params __rte_unused,
35 : : char *buffer, int buf_len);
36 : :
37 : : struct json_command {
38 : : char action[MAX_LEN];
39 : : char cmd[MAX_LEN];
40 : : char data[MAX_LEN];
41 : : telemetry_legacy_cb fn;
42 : :
43 : : };
44 : :
45 : : struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
46 : : {
47 : : .action = "\"action\":1",
48 : : .cmd = CLIENTS_CMD,
49 : : .data = CLIENTS_DATA,
50 : : .fn = register_client
51 : : }
52 : : };
53 : : int num_legacy_callbacks = 1;
54 : : static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
55 : :
56 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_telemetry_legacy_register)
57 : : int
58 : 828 : rte_telemetry_legacy_register(const char *cmd,
59 : : enum rte_telemetry_legacy_data_req data_req,
60 : : telemetry_legacy_cb fn)
61 : : {
62 [ + - ]: 828 : if (fn == NULL)
63 : : return -EINVAL;
64 [ + - ]: 828 : if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
65 : : return -ENOENT;
66 : :
67 : : rte_spinlock_lock(&callback_sl);
68 [ + + ]: 828 : strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
69 : 828 : snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
70 : : "\"command\":\"%s\"", cmd);
71 [ + + ]: 1932 : snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
72 : : data_req ? "%s{\"" : "%snull",
73 : : DATA_REQ_LABEL);
74 : 828 : callbacks[num_legacy_callbacks].fn = fn;
75 : 828 : num_legacy_callbacks++;
76 : : rte_spinlock_unlock(&callback_sl);
77 : :
78 : 828 : return 0;
79 : : }
80 : :
81 : : static int
82 : 0 : register_client(const char *cmd __rte_unused, const char *params,
83 : : char *buffer __rte_unused, int buf_len __rte_unused)
84 : : {
85 : : #ifndef RTE_EXEC_ENV_WINDOWS
86 : : pthread_t th;
87 : : char data[BUF_SIZE];
88 : : int fd;
89 : : int rc;
90 : : struct sockaddr_un addrs;
91 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
92 : :
93 [ # # ]: 0 : if (!strchr(params, ':')) {
94 : 0 : fprintf(stderr, "Invalid data\n");
95 : 0 : return -1;
96 : : }
97 : : #ifndef RTE_EXEC_ENV_WINDOWS
98 : : strlcpy(data, strchr(params, ':'), sizeof(data));
99 [ # # ]: 0 : memmove(data, &data[strlen(":\"")], strlen(data));
100 [ # # ]: 0 : if (!strchr(data, '\"')) {
101 : 0 : fprintf(stderr, "Invalid client data\n");
102 : 0 : return -1;
103 : : }
104 : 0 : *strchr(data, '\"') = 0;
105 [ # # ]: 0 : addrs.sun_family = AF_UNIX;
106 [ # # ]: 0 : if (strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path)) >= sizeof(addrs.sun_path)) {
107 : 0 : fprintf(stderr, "Client path too long\n");
108 : 0 : return -1;
109 : : }
110 : :
111 : 0 : fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
112 [ # # ]: 0 : if (fd < 0) {
113 : 0 : perror("Failed to open socket");
114 : 0 : return -1;
115 : : }
116 : :
117 [ # # ]: 0 : if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
118 : 0 : perror("\nClient connection error\n");
119 : 0 : close(fd);
120 : 0 : return -1;
121 : : }
122 : 0 : rc = pthread_create(&th, NULL, &legacy_client_handler,
123 : 0 : (void *)(uintptr_t)fd);
124 [ # # ]: 0 : if (rc != 0) {
125 : 0 : fprintf(stderr, "Failed to create legacy client thread: %s\n",
126 : : strerror(rc));
127 : 0 : close(fd);
128 : 0 : return -1;
129 : : }
130 : 0 : pthread_detach(th);
131 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
132 : 0 : return 0;
133 : : }
134 : :
135 : : #ifndef RTE_EXEC_ENV_WINDOWS
136 : :
137 : : static int
138 : 0 : send_error_response(int s, int err)
139 : : {
140 : : const char *desc;
141 : : char out_buf[100000];
142 : :
143 [ # # # # ]: 0 : switch (err) {
144 : : case -ENOMEM:
145 : : desc = "Memory Allocation Error";
146 : : break;
147 : 0 : case -EINVAL:
148 : : desc = "Invalid Argument 404";
149 : 0 : break;
150 : 0 : case -EPERM:
151 : : desc = "Unknown";
152 : 0 : break;
153 : : default:
154 : : /* Default case keeps behaviour of Telemetry library */
155 : : printf("\nInvalid error type: %d\n", err);
156 : 0 : return -EINVAL;
157 : : }
158 : : int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
159 : : "\"Status Error: %s\", \"data\": null}", desc);
160 [ # # ]: 0 : if (write(s, out_buf, used) < 0) {
161 : 0 : perror("Error writing to socket");
162 : 0 : return -1;
163 : : }
164 : : return 0;
165 : : }
166 : :
167 : : static void
168 : 0 : perform_command(telemetry_legacy_cb fn, const char *param, int s)
169 : : {
170 : : char out_buf[100000];
171 : : int ret, used = 0;
172 : :
173 : 0 : ret = fn("", param, out_buf, sizeof(out_buf));
174 [ # # ]: 0 : if (ret < 0) {
175 : 0 : ret = send_error_response(s, ret);
176 [ # # ]: 0 : if (ret < 0)
177 : : printf("\nCould not send error response\n");
178 : 0 : return;
179 : : }
180 : : used += ret;
181 [ # # ]: 0 : if (write(s, out_buf, used) < 0)
182 : 0 : perror("Error writing to socket");
183 : : }
184 : :
185 : : static int
186 : 0 : parse_client_request(char *buffer, int buf_len, int s)
187 : : {
188 : : int i;
189 : 0 : char *data = buffer + buf_len;
190 : : telemetry_legacy_cb fn = NULL;
191 : : const char *valid_sep = ",}";
192 [ # # # # ]: 0 : if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
193 : : return -EPERM;
194 : :
195 [ # # # # ]: 0 : if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
196 [ # # ]: 0 : && strstr(buffer, CLIENTS_DATA))
197 : : return 0;
198 : :
199 [ # # ]: 0 : for (i = 0; i < num_legacy_callbacks; i++) {
200 : 0 : char *action_ptr = strstr(buffer, callbacks[i].action);
201 : 0 : char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
202 : 0 : char *data_ptr = strstr(buffer, callbacks[i].data);
203 [ # # # # ]: 0 : if (!action_ptr || !cmd_ptr || !data_ptr)
204 : : continue;
205 : :
206 : 0 : char action_sep = action_ptr[strlen(callbacks[i].action)];
207 : 0 : char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
208 [ # # # # ]: 0 : if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
209 : : cmd_sep)))
210 : : return -EPERM;
211 : : char data_sep;
212 : :
213 [ # # ]: 0 : if (!strchr(data_ptr, '{'))
214 : 0 : data_sep = data_ptr[strlen(callbacks[i].data)];
215 : : else {
216 [ # # ]: 0 : if (!strchr(data_ptr, '}'))
217 : : return -EINVAL;
218 : : char *data_end = strchr(data_ptr, '}');
219 : 0 : data = data_ptr + strlen(DATA_REQ_LABEL);
220 : 0 : data_sep = data_end[1];
221 : 0 : data_end[1] = 0;
222 : : }
223 [ # # ]: 0 : if (!strchr(valid_sep, data_sep))
224 : : return -EPERM;
225 : 0 : fn = callbacks[i].fn;
226 : 0 : break;
227 : : }
228 : :
229 [ # # ]: 0 : if (!fn)
230 : : return -EINVAL;
231 : 0 : perform_command(fn, data, s);
232 : 0 : return 0;
233 : : }
234 : :
235 : : void *
236 : 3 : legacy_client_handler(void *sock_id)
237 : : {
238 : 3 : int s = (int)(uintptr_t)sock_id;
239 : : int ret;
240 : : char buffer_recv[BUF_SIZE];
241 : : /* receive data is not null terminated */
242 : 3 : int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
243 : :
244 [ - + ]: 3 : while (bytes > 0) {
245 : 0 : buffer_recv[bytes] = 0;
246 : : int i, j;
247 : : char buffer[BUF_SIZE];
248 [ # # ]: 0 : for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
249 : 0 : buffer[j] = buffer_recv[i];
250 : 0 : j += !isspace(buffer_recv[i]);
251 : : }
252 : 0 : buffer[j] = 0;
253 : 0 : ret = parse_client_request(buffer, j, s);
254 [ # # ]: 0 : if (ret < 0) {
255 : 0 : ret = send_error_response(s, ret);
256 [ # # ]: 0 : if (ret < 0)
257 : : printf("\nCould not send error response\n");
258 : : }
259 : 0 : bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
260 : : }
261 : 3 : close(s);
262 : 3 : return NULL;
263 : : }
264 : :
265 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
|