telemetry: close socket on connection failure
[dpdk.git] / lib / librte_telemetry / telemetry_legacy.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8 #include <pthread.h>
9
10 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
11 #undef RTE_USE_LIBBSD
12 #include <rte_string_fns.h>
13 #include <rte_common.h>
14 #include <rte_spinlock.h>
15
16 #include "rte_telemetry_legacy.h"
17
18 #define MAX_LEN 128
19 #define BUF_SIZE 1024
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
26
27
28 static int
29 register_client(const char *cmd __rte_unused,
30                 const char *params __rte_unused,
31                 char *buffer, int buf_len);
32
33 struct json_command {
34         char action[MAX_LEN];
35         char cmd[MAX_LEN];
36         char data[MAX_LEN];
37         telemetry_legacy_cb fn;
38
39 };
40
41 struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
42                 {
43                         .action = "\"action\":1",
44                         .cmd = CLIENTS_CMD,
45                         .data = CLIENTS_DATA,
46                         .fn = register_client
47                 }
48 };
49 int num_legacy_callbacks = 1;
50 static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
51
52 int
53 rte_telemetry_legacy_register(const char *cmd,
54                 enum rte_telemetry_legacy_data_req data_req,
55                 telemetry_legacy_cb fn)
56 {
57         if (fn == NULL)
58                 return -EINVAL;
59         if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
60                 return -ENOENT;
61
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",
68                         DATA_REQ_LABEL);
69         callbacks[num_legacy_callbacks].fn = fn;
70         num_legacy_callbacks++;
71         rte_spinlock_unlock(&callback_sl);
72
73         return 0;
74 }
75
76 static int
77 register_client(const char *cmd __rte_unused, const char *params,
78                 char *buffer __rte_unused, int buf_len __rte_unused)
79 {
80         pthread_t th;
81         char data[BUF_SIZE];
82         int fd;
83         struct sockaddr_un addrs;
84
85         if (!strchr(params, ':')) {
86                 fprintf(stderr, "Invalid data\n");
87                 return -1;
88         }
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");
93                 return -1;
94         }
95         *strchr(data, '\"') = 0;
96
97         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
98         addrs.sun_family = AF_UNIX;
99         strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
100
101         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
102                 perror("\nClient connection error\n");
103                 close(fd);
104                 return -1;
105         }
106         pthread_create(&th, NULL, &legacy_client_handler,
107                         (void *)(uintptr_t)fd);
108         return 0;
109 }
110
111 static int
112 send_error_response(int s, int err)
113 {
114         const char *desc;
115         char out_buf[100000];
116
117         switch (err) {
118         case -ENOMEM:
119                 desc = "Memory Allocation Error";
120                 break;
121         case -EINVAL:
122                 desc = "Invalid Argument 404";
123                 break;
124         case -EPERM:
125                 desc = "Unknown";
126                 break;
127         default:
128                 /* Default case keeps behaviour of Telemetry library */
129                 printf("\nInvalid error type: %d\n", err);
130                 return -EINVAL;
131         }
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");
136                 return -1;
137         }
138         return 0;
139 }
140
141 static void
142 perform_command(telemetry_legacy_cb fn, const char *param, int s)
143 {
144         char out_buf[100000];
145         int ret, used = 0;
146
147         ret = fn("", param, out_buf, sizeof(out_buf));
148         if (ret < 0) {
149                 ret = send_error_response(s, ret);
150                 if (ret < 0)
151                         printf("\nCould not send error response\n");
152                 return;
153         }
154         used += ret;
155         if (write(s, out_buf, used) < 0)
156                 perror("Error writing to socket");
157 }
158
159 static int
160 parse_client_request(char *buffer, int buf_len, int s)
161 {
162         int i;
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] != '}')
167                 return -EPERM;
168
169         if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
170                         && strstr(buffer, CLIENTS_DATA))
171                 return 0;
172
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)
178                         continue;
179
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,
183                                 cmd_sep)))
184                         return -EPERM;
185                 char data_sep;
186
187                 if (!strchr(data_ptr, '{'))
188                         data_sep = data_ptr[strlen(callbacks[i].data)];
189                 else {
190                         if (!strchr(data_ptr, '}'))
191                                 return -EINVAL;
192                         char *data_end = strchr(data_ptr, '}');
193                         data = data_ptr + strlen(DATA_REQ_LABEL);
194                         data_sep = data_end[1];
195                         data_end[1] = 0;
196                 }
197                 if (!strchr(valid_sep, data_sep))
198                         return -EPERM;
199                 fn = callbacks[i].fn;
200                 break;
201         }
202
203         if (!fn)
204                 return -EINVAL;
205         perform_command(fn, data, s);
206         return 0;
207 }
208
209 void *
210 legacy_client_handler(void *sock_id)
211 {
212         int s = (int)(uintptr_t)sock_id;
213         int ret;
214         char buffer_recv[BUF_SIZE];
215         /* receive data is not null terminated */
216         int bytes = read(s, buffer_recv, sizeof(buffer_recv));
217
218         while (bytes > 0) {
219                 buffer_recv[bytes] = 0;
220                 int i, j;
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]);
225                 }
226                 buffer[j] = 0;
227                 ret = parse_client_request(buffer, j, s);
228                 if (ret < 0) {
229                         ret = send_error_response(s, ret);
230                         if (ret < 0)
231                                 printf("\nCould not send error response\n");
232                 }
233                 bytes = read(s, buffer_recv, sizeof(buffer_recv));
234         }
235         close(s);
236         return NULL;
237 }