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