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