telemetry: add parser for client socket messages
[dpdk.git] / lib / librte_telemetry / rte_telemetry.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <pthread.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <jansson.h>
11
12 #include <rte_eal.h>
13 #include <rte_ethdev.h>
14 #include <rte_metrics.h>
15 #include <rte_option.h>
16 #include <rte_string_fns.h>
17
18 #include "rte_telemetry.h"
19 #include "rte_telemetry_internal.h"
20 #include "rte_telemetry_parser.h"
21
22 #define BUF_SIZE 1024
23 #define ACTION_POST 1
24 #define SLEEP_TIME 10
25
26 static telemetry_impl *static_telemetry;
27
28 static void
29 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
30 {
31         snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
32 }
33
34 int32_t
35 rte_telemetry_is_port_active(int port_id)
36 {
37         int ret;
38
39         ret = rte_eth_find_next(port_id);
40         if (ret == port_id)
41                 return 1;
42
43         TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
44                 port_id);
45
46         return 0;
47 }
48
49 int32_t
50 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
51         const char *json_string)
52 {
53         int ret;
54
55         if (telemetry == NULL) {
56                 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
57                 return -1;
58         }
59
60         if (telemetry->request_client == NULL) {
61                 TELEMETRY_LOG_ERR("No client has been chosen to write to");
62                 return -1;
63         }
64
65         if (json_string == NULL) {
66                 TELEMETRY_LOG_ERR("Invalid JSON string!");
67                 return -1;
68         }
69
70         ret = send(telemetry->request_client->fd,
71                         json_string, strlen(json_string), 0);
72         if (ret < 0) {
73                 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
74                                 telemetry->request_client->file_path);
75                 return -1;
76         }
77
78         return 0;
79 }
80
81 int32_t
82 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
83         int error_type)
84 {
85         int ret;
86         const char *status_code, *json_buffer;
87         json_t *root;
88
89         if (error_type == -EPERM)
90                 status_code = "Status Error: Unknown";
91         else if (error_type == -EINVAL)
92                 status_code = "Status Error: Invalid Argument 404";
93         else if (error_type == -ENOMEM)
94                 status_code = "Status Error: Memory Allocation Error";
95         else {
96                 TELEMETRY_LOG_ERR("Invalid error type");
97                 return -EINVAL;
98         }
99
100         root = json_object();
101
102         if (root == NULL) {
103                 TELEMETRY_LOG_ERR("Could not create root JSON object");
104                 return -EPERM;
105         }
106
107         ret = json_object_set_new(root, "status_code", json_string(status_code));
108         if (ret < 0) {
109                 TELEMETRY_LOG_ERR("Status code field cannot be set");
110                 json_decref(root);
111                 return -EPERM;
112         }
113
114         ret = json_object_set_new(root, "data", json_null());
115         if (ret < 0) {
116                 TELEMETRY_LOG_ERR("Data field cannot be set");
117                 json_decref(root);
118                 return -EPERM;
119         }
120
121         json_buffer = json_dumps(root, JSON_INDENT(2));
122         json_decref(root);
123
124         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
125         if (ret < 0) {
126                 TELEMETRY_LOG_ERR("Could not write to socket");
127                 return -EPERM;
128         }
129
130         return 0;
131 }
132
133 static int32_t
134 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
135 {
136         int ret, num_xstats, ret_val, i;
137         struct rte_eth_xstat *eth_xstats = NULL;
138         struct rte_eth_xstat_name *eth_xstats_names = NULL;
139
140         if (!rte_eth_dev_is_valid_port(port_id)) {
141                 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
142                 return -EINVAL;
143         }
144
145         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
146         if (num_xstats < 0) {
147                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
148                                 port_id, num_xstats);
149                 return -EPERM;
150         }
151
152         eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
153         if (eth_xstats == NULL) {
154                 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
155                 return -ENOMEM;
156         }
157
158         ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
159         const char *xstats_names[num_xstats];
160         eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
161         if (ret < 0 || ret > num_xstats) {
162                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
163                                 port_id, num_xstats, ret);
164                 ret_val = -EPERM;
165                 goto free_xstats;
166         }
167
168         if (eth_xstats_names == NULL) {
169                 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
170                 ret_val = -ENOMEM;
171                 goto free_xstats;
172         }
173
174         ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
175         if (ret < 0 || ret > num_xstats) {
176                 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
177                                 port_id, num_xstats, ret);
178                 ret_val = -EPERM;
179                 goto free_xstats;
180         }
181
182         for (i = 0; i < num_xstats; i++)
183                 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
184
185         ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
186         if (ret_val < 0) {
187                 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
188                 ret_val = -1;
189                 goto free_xstats;
190         }
191
192         goto free_xstats;
193
194 free_xstats:
195         free(eth_xstats);
196         free(eth_xstats_names);
197         return ret_val;
198 }
199
200 static int32_t
201 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
202 {
203         uint16_t pid;
204
205         RTE_ETH_FOREACH_DEV(pid) {
206                 telemetry->reg_index = rte_telemetry_reg_ethdev_to_metrics(pid);
207                 break;
208         }
209
210         if (telemetry->reg_index < 0) {
211                 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
212                 return -1;
213         }
214
215         telemetry->metrics_register_done = 1;
216
217         return 0;
218 }
219
220 static int32_t
221 rte_telemetry_read_client(struct telemetry_impl *telemetry)
222 {
223         char buf[BUF_SIZE];
224         int ret, buffer_read;
225
226         buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
227
228         if (buffer_read == -1) {
229                 TELEMETRY_LOG_ERR("Read error");
230                 return -1;
231         } else if (buffer_read == 0) {
232                 goto close_socket;
233         } else {
234                 buf[buffer_read] = '\0';
235                 ret = rte_telemetry_parse_client_message(telemetry, buf);
236                 if (ret < 0)
237                         TELEMETRY_LOG_WARN("Parse message failed");
238                 goto close_socket;
239         }
240
241 close_socket:
242         if (close(telemetry->accept_fd) < 0) {
243                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
244                 free(telemetry);
245                 return -EPERM;
246         }
247         telemetry->accept_fd = 0;
248
249         return 0;
250 }
251
252 static int32_t
253 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
254 {
255         int ret;
256
257         if (telemetry->accept_fd <= 0) {
258                 ret = listen(telemetry->server_fd, 1);
259                 if (ret < 0) {
260                         TELEMETRY_LOG_ERR("Listening error with server fd");
261                         return -1;
262                 }
263
264                 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
265                 if (telemetry->accept_fd >= 0 &&
266                         telemetry->metrics_register_done == 0) {
267                         ret = rte_telemetry_initial_accept(telemetry);
268                         if (ret < 0) {
269                                 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
270                                 return -1;
271                         }
272                 }
273         } else {
274                 ret = rte_telemetry_read_client(telemetry);
275                 if (ret < 0) {
276                         TELEMETRY_LOG_ERR("Failed to read socket buffer");
277                         return -1;
278                 }
279         }
280
281         return 0;
282 }
283
284 static int32_t
285 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
286 {
287         int ret;
288         telemetry_client *client;
289         char client_buf[BUF_SIZE];
290         int bytes;
291
292         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
293                 bytes = read(client->fd, client_buf, BUF_SIZE-1);
294
295                 if (bytes > 0) {
296                         client_buf[bytes] = '\0';
297                         telemetry->request_client = client;
298                         ret = rte_telemetry_parse(telemetry, client_buf);
299                         if (ret < 0) {
300                                 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
301                                                 ret);
302                                 return -1;
303                         }
304                 }
305         }
306
307         return 0;
308 }
309
310 static int32_t
311 rte_telemetry_run(void *userdata)
312 {
313         int ret;
314         struct telemetry_impl *telemetry = userdata;
315
316         if (telemetry == NULL) {
317                 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
318                 return -1;
319         }
320
321         ret = rte_telemetry_accept_new_client(telemetry);
322         if (ret < 0) {
323                 TELEMETRY_LOG_ERR("Accept and read new client failed");
324                 return -1;
325         }
326
327         ret = rte_telemetry_read_client_sockets(telemetry);
328         if (ret < 0) {
329                 TELEMETRY_LOG_ERR("Client socket read failed");
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 static void
337 *rte_telemetry_run_thread_func(void *userdata)
338 {
339         int ret;
340         struct telemetry_impl *telemetry = userdata;
341
342         if (telemetry == NULL) {
343                 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
344                 pthread_exit(0);
345         }
346
347         while (telemetry->thread_status) {
348                 rte_telemetry_run(telemetry);
349                 ret = usleep(SLEEP_TIME);
350                 if (ret < 0)
351                         TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
352         }
353         pthread_exit(0);
354 }
355
356 static int32_t
357 rte_telemetry_set_socket_nonblock(int fd)
358 {
359         int flags;
360
361         if (fd < 0) {
362                 TELEMETRY_LOG_ERR("Invalid fd provided");
363                 return -1;
364         }
365
366         flags = fcntl(fd, F_GETFL, 0);
367         if (flags < 0)
368                 flags = 0;
369
370         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
371 }
372
373 static int32_t
374 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
375 {
376         int ret;
377         struct sockaddr_un addr;
378         char socket_path[BUF_SIZE];
379
380         if (telemetry == NULL)
381                 return -1;
382
383         telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
384         if (telemetry->server_fd == -1) {
385                 TELEMETRY_LOG_ERR("Failed to open socket");
386                 return -1;
387         }
388
389         ret  = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
390         if (ret < 0) {
391                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
392                 goto close_socket;
393         }
394
395         addr.sun_family = AF_UNIX;
396         rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
397         strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
398         unlink(socket_path);
399
400         if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
401                 sizeof(addr)) < 0) {
402                 TELEMETRY_LOG_ERR("Socket binding error");
403                 goto close_socket;
404         }
405
406         return 0;
407
408 close_socket:
409         if (close(telemetry->server_fd) < 0) {
410                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
411                 return -EPERM;
412         }
413
414         return -1;
415 }
416
417 int32_t __rte_experimental
418 rte_telemetry_init()
419 {
420         int ret;
421         pthread_attr_t attr;
422         const char *telemetry_ctrl_thread = "telemetry";
423
424         if (static_telemetry) {
425                 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
426                 return -EALREADY;
427         }
428
429         static_telemetry = calloc(1, sizeof(struct telemetry_impl));
430         if (static_telemetry == NULL) {
431                 TELEMETRY_LOG_ERR("Memory could not be allocated");
432                 return -ENOMEM;
433         }
434
435         static_telemetry->socket_id = rte_socket_id();
436         rte_metrics_init(static_telemetry->socket_id);
437
438         ret = pthread_attr_init(&attr);
439         if (ret != 0) {
440                 TELEMETRY_LOG_ERR("Pthread attribute init failed");
441                 return -EPERM;
442         }
443
444         ret = rte_telemetry_create_socket(static_telemetry);
445         if (ret < 0) {
446                 ret = rte_telemetry_cleanup();
447                 if (ret < 0)
448                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
449                 return -EPERM;
450         }
451         TAILQ_INIT(&static_telemetry->client_list_head);
452
453         ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
454                 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
455                 (void *)static_telemetry);
456         static_telemetry->thread_status = 1;
457
458         if (ret < 0) {
459                 ret = rte_telemetry_cleanup();
460                 if (ret < 0)
461                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
462                 return -EPERM;
463         }
464
465         return 0;
466 }
467
468 static int32_t
469 rte_telemetry_client_cleanup(struct telemetry_client *client)
470 {
471         int ret;
472
473         ret = close(client->fd);
474         free(client->file_path);
475         free(client);
476
477         if (ret < 0) {
478                 TELEMETRY_LOG_ERR("Close client socket failed");
479                 return -EPERM;
480         }
481
482         return 0;
483 }
484
485 int32_t __rte_experimental
486 rte_telemetry_cleanup(void)
487 {
488         int ret;
489         struct telemetry_impl *telemetry = static_telemetry;
490         telemetry_client *client, *temp_client;
491
492         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
493                 temp_client) {
494                 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
495                 ret = rte_telemetry_client_cleanup(client);
496                 if (ret < 0) {
497                         TELEMETRY_LOG_ERR("Client cleanup failed");
498                         return -EPERM;
499                 }
500         }
501
502         ret = close(telemetry->server_fd);
503         if (ret < 0) {
504                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
505                 free(telemetry);
506                 return -EPERM;
507         }
508
509         telemetry->thread_status = 0;
510         pthread_join(telemetry->thread_id, NULL);
511         free(telemetry);
512         static_telemetry = NULL;
513
514         return 0;
515 }
516
517 int32_t
518 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
519         const char *client_path)
520 {
521         int ret;
522         telemetry_client *client, *temp_client;
523
524         if (telemetry == NULL) {
525                 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
526                 return -ENODEV;
527         }
528
529         if (client_path == NULL) {
530                 TELEMETRY_LOG_ERR("Invalid client path");
531                 goto einval_fail;
532         }
533
534         if (TAILQ_EMPTY(&telemetry->client_list_head)) {
535                 TELEMETRY_LOG_ERR("There are no clients currently registered");
536                 return -EPERM;
537         }
538
539         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
540                         temp_client) {
541                 if (strcmp(client_path, client->file_path) == 0) {
542                         TAILQ_REMOVE(&telemetry->client_list_head, client,
543                                 client_list);
544                         ret = rte_telemetry_client_cleanup(client);
545
546                         if (ret < 0) {
547                                 TELEMETRY_LOG_ERR("Client cleanup failed");
548                                 return -EPERM;
549                         }
550
551                         return 0;
552                 }
553         }
554
555         TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
556         return -1;
557
558 einval_fail:
559         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
560         if (ret < 0)
561                 TELEMETRY_LOG_ERR("Could not send error");
562         return -EINVAL;
563 }
564
565 int32_t
566 rte_telemetry_register_client(struct telemetry_impl *telemetry,
567         const char *client_path)
568 {
569         int ret, fd;
570         struct sockaddr_un addrs;
571
572         if (telemetry == NULL) {
573                 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
574                 return -ENODEV;
575         }
576
577         if (client_path == NULL) {
578                 TELEMETRY_LOG_ERR("Invalid client path");
579                 return -EINVAL;
580         }
581
582         telemetry_client *client;
583         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
584                 if (strcmp(client_path, client->file_path) == 0) {
585                         TELEMETRY_LOG_WARN("'%s' already registered",
586                                         client_path);
587                         return -EINVAL;
588                 }
589         }
590
591         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
592         if (fd == -1) {
593                 TELEMETRY_LOG_ERR("Client socket error");
594                 return -EACCES;
595         }
596
597         ret = rte_telemetry_set_socket_nonblock(fd);
598         if (ret < 0) {
599                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
600                 return -EPERM;
601         }
602
603         addrs.sun_family = AF_UNIX;
604         strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
605         telemetry_client *new_client = malloc(sizeof(telemetry_client));
606         new_client->file_path = strdup(client_path);
607         new_client->fd = fd;
608
609         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
610                 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
611                                 client_path);
612                 ret = rte_telemetry_client_cleanup(new_client);
613                 if (ret < 0) {
614                         TELEMETRY_LOG_ERR("Client cleanup failed");
615                         return -EPERM;
616                 }
617                 return -EINVAL;
618         }
619
620         TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
621
622         return 0;
623 }
624
625 int32_t
626 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
627 {
628         int ret, action_int;
629         json_error_t error;
630         json_t *root = json_loads(buf, 0, &error);
631
632         if (root == NULL) {
633                 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
634                                 error.text);
635                 goto fail;
636         } else if (!json_is_object(root)) {
637                 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
638                 goto fail;
639         }
640
641         json_t *action = json_object_get(root, "action");
642         if (action == NULL) {
643                 TELEMETRY_LOG_WARN("Request does not have action field");
644                 goto fail;
645         } else if (!json_is_integer(action)) {
646                 TELEMETRY_LOG_WARN("Action value is not an integer");
647                 goto fail;
648         }
649
650         json_t *command = json_object_get(root, "command");
651         if (command == NULL) {
652                 TELEMETRY_LOG_WARN("Request does not have command field");
653                 goto fail;
654         } else if (!json_is_string(command)) {
655                 TELEMETRY_LOG_WARN("Command value is not a string");
656                 goto fail;
657         }
658
659         action_int = json_integer_value(action);
660         if (action_int != ACTION_POST) {
661                 TELEMETRY_LOG_WARN("Invalid action code");
662                 goto fail;
663         }
664
665         if (strcmp(json_string_value(command), "clients") != 0) {
666                 TELEMETRY_LOG_WARN("Invalid command");
667                 goto fail;
668         }
669
670         json_t *data = json_object_get(root, "data");
671         if (data == NULL) {
672                 TELEMETRY_LOG_WARN("Request does not have data field");
673                 goto fail;
674         }
675
676         json_t *client_path = json_object_get(data, "client_path");
677         if (client_path == NULL) {
678                 TELEMETRY_LOG_WARN("Request does not have client_path field");
679                 goto fail;
680         }
681
682         if (!json_is_string(client_path)) {
683                 TELEMETRY_LOG_WARN("Client_path value is not a string");
684                 goto fail;
685         }
686
687         ret = rte_telemetry_register_client(telemetry,
688                         json_string_value(client_path));
689         if (ret < 0) {
690                 TELEMETRY_LOG_ERR("Could not register client");
691                 telemetry->register_fail_count++;
692                 goto fail;
693         }
694
695         return 0;
696
697 fail:
698         TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
699         json_decref(root);
700         return -1;
701 }
702
703 int telemetry_log_level;
704
705 static struct rte_option option = {
706         .opt_str = "--telemetry",
707         .cb = &rte_telemetry_init,
708         .enabled = 0
709 };
710
711 RTE_INIT(rte_telemetry_register)
712 {
713         telemetry_log_level = rte_log_register("lib.telemetry");
714         if (telemetry_log_level >= 0)
715                 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
716
717         rte_option_register(&option);
718 }