2022ce68ebe215b29dfff7917772ac97686781e6
[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 #include "rte_telemetry_socket_tests.h"
22
23 #define BUF_SIZE 1024
24 #define ACTION_POST 1
25 #define SLEEP_TIME 10
26
27 #define SELFTEST_VALID_CLIENT "/var/run/dpdk/valid_client"
28 #define SELFTEST_INVALID_CLIENT "/var/run/dpdk/invalid_client"
29 #define SOCKET_TEST_CLIENT_PATH "/var/run/dpdk/client"
30
31 static telemetry_impl *static_telemetry;
32
33 struct telemetry_message_test {
34         const char *test_name;
35         int (*test_func_ptr)(struct telemetry_impl *telemetry, int fd);
36 };
37
38 struct json_data {
39         char *status_code;
40         const char *data;
41         int port;
42         char *stat_name;
43         int stat_value;
44 };
45
46 static void
47 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
48 {
49         snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
50 }
51
52 static int32_t
53 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
54         const char *json_string)
55 {
56         int ret;
57
58         if (telemetry == NULL) {
59                 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
60                 return -1;
61         }
62
63         if (telemetry->request_client == NULL) {
64                 TELEMETRY_LOG_ERR("No client has been chosen to write to");
65                 return -1;
66         }
67
68         if (json_string == NULL) {
69                 TELEMETRY_LOG_ERR("Invalid JSON string!");
70                 return -1;
71         }
72
73         ret = send(telemetry->request_client->fd,
74                         json_string, strlen(json_string), 0);
75         if (ret < 0) {
76                 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
77                                 telemetry->request_client->file_path);
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 int32_t
85 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
86         int error_type)
87 {
88         int ret;
89         const char *status_code, *json_buffer;
90         json_t *root;
91
92         if (error_type == -EPERM)
93                 status_code = "Status Error: Unknown";
94         else if (error_type == -EINVAL)
95                 status_code = "Status Error: Invalid Argument 404";
96         else if (error_type == -ENOMEM)
97                 status_code = "Status Error: Memory Allocation Error";
98         else {
99                 TELEMETRY_LOG_ERR("Invalid error type");
100                 return -EINVAL;
101         }
102
103         root = json_object();
104
105         if (root == NULL) {
106                 TELEMETRY_LOG_ERR("Could not create root JSON object");
107                 return -EPERM;
108         }
109
110         ret = json_object_set_new(root, "status_code", json_string(status_code));
111         if (ret < 0) {
112                 TELEMETRY_LOG_ERR("Status code field cannot be set");
113                 json_decref(root);
114                 return -EPERM;
115         }
116
117         ret = json_object_set_new(root, "data", json_null());
118         if (ret < 0) {
119                 TELEMETRY_LOG_ERR("Data field cannot be set");
120                 json_decref(root);
121                 return -EPERM;
122         }
123
124         json_buffer = json_dumps(root, 0);
125         json_decref(root);
126
127         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
128         if (ret < 0) {
129                 TELEMETRY_LOG_ERR("Could not write to socket");
130                 return -EPERM;
131         }
132
133         return 0;
134 }
135
136 int32_t
137 rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
138         struct telemetry_impl *telemetry)
139 {
140         int ret;
141         char *json_buffer = NULL;
142
143         if (telemetry == NULL) {
144                 TELEMETRY_LOG_ERR("Invalid telemetry argument");
145                 return -1;
146         }
147
148         ret = rte_metrics_tel_encode_json_format(ep, &json_buffer);
149         if (ret < 0) {
150                 TELEMETRY_LOG_ERR("JSON encode function failed");
151                 ret = rte_telemetry_send_error_response(telemetry, ret);
152                 if (ret < 0)
153                         TELEMETRY_LOG_ERR("Could not send error");
154                 return -1;
155         }
156
157         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
158         if (ret < 0) {
159                 TELEMETRY_LOG_ERR("Could not write to socket");
160                 return -1;
161         }
162
163         return 0;
164 }
165
166 int32_t
167 rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
168         struct telemetry_impl *telemetry)
169 {
170         int ret;
171         char *json_buffer = NULL;
172
173         if (telemetry == NULL) {
174                 TELEMETRY_LOG_ERR("Invalid telemetry argument");
175                 return -1;
176         }
177
178         if (ep == NULL) {
179                 TELEMETRY_LOG_ERR("Invalid encode param argument");
180                 goto einval_fail;
181         }
182
183         if (ep->pp.num_metric_ids < 0) {
184                 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
185                 goto einval_fail;
186         }
187
188         if (ep->pp.num_port_ids < 0) {
189                 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
190                 goto einval_fail;
191         }
192
193         ret = rte_metrics_tel_get_ports_stats_json(ep, telemetry->reg_index,
194                         &json_buffer);
195         if (ret < 0) {
196                 TELEMETRY_LOG_ERR("Function for get_ports_stats_json"
197                                 " failed");
198                 ret = rte_telemetry_send_error_response(telemetry, ret);
199                 if (ret < 0)
200                         TELEMETRY_LOG_ERR("Could not send error");
201                 return -1;
202         }
203
204         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
205         if (ret < 0) {
206                 TELEMETRY_LOG_ERR("Could not write to socket");
207                 return -1;
208         }
209
210         return 0;
211
212 einval_fail:
213         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
214         if (ret < 0)
215                 TELEMETRY_LOG_ERR("Could not send error");
216         return -1;
217 }
218
219 static int32_t
220 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
221 {
222         int ret;
223         int selftest = 0;
224
225         ret = rte_metrics_tel_reg_all_ethdev(
226                         &telemetry->metrics_register_done,
227                         telemetry->reg_index);
228         if (ret < 0) {
229                 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
230                 ret = rte_telemetry_send_error_response(telemetry, ret);
231                 if (ret < 0)
232                         TELEMETRY_LOG_ERR("Could not send error");
233                 return -1;
234         }
235
236         if (selftest) {
237                 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
238                                 telemetry->server_fd);
239                 if (ret < 0)
240                         return -1;
241
242                 ret = rte_telemetry_parser_test(telemetry);
243                 if (ret < 0) {
244                         TELEMETRY_LOG_ERR("Parser Tests Failed");
245                         return -1;
246                 }
247
248                 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
249         }
250
251         return 0;
252 }
253
254 static int32_t
255 rte_telemetry_read_client(struct telemetry_impl *telemetry)
256 {
257         char buf[BUF_SIZE];
258         int ret, buffer_read;
259
260         buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
261
262         if (buffer_read == -1) {
263                 TELEMETRY_LOG_ERR("Read error");
264                 return -1;
265         } else if (buffer_read == 0) {
266                 goto close_socket;
267         } else {
268                 buf[buffer_read] = '\0';
269                 ret = rte_telemetry_parse_client_message(telemetry, buf);
270                 if (ret < 0)
271                         TELEMETRY_LOG_WARN("Parse message failed");
272                 goto close_socket;
273         }
274
275 close_socket:
276         if (close(telemetry->accept_fd) < 0) {
277                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
278                 free(telemetry);
279                 return -EPERM;
280         }
281         telemetry->accept_fd = 0;
282
283         return 0;
284 }
285
286 static int32_t
287 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
288 {
289         int ret;
290
291         if (telemetry->accept_fd <= 0) {
292                 ret = listen(telemetry->server_fd, 1);
293                 if (ret < 0) {
294                         TELEMETRY_LOG_ERR("Listening error with server fd");
295                         return -1;
296                 }
297
298                 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
299                 if (telemetry->accept_fd >= 0 &&
300                         telemetry->metrics_register_done == 0) {
301                         ret = rte_telemetry_initial_accept(telemetry);
302                         if (ret < 0) {
303                                 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
304                                 return -1;
305                         }
306                 }
307         } else {
308                 ret = rte_telemetry_read_client(telemetry);
309                 if (ret < 0) {
310                         TELEMETRY_LOG_ERR("Failed to read socket buffer");
311                         return -1;
312                 }
313         }
314
315         return 0;
316 }
317
318 static int32_t
319 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
320 {
321         int ret;
322         telemetry_client *client;
323         char client_buf[BUF_SIZE];
324         int bytes;
325
326         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
327                 bytes = read(client->fd, client_buf, BUF_SIZE-1);
328
329                 if (bytes > 0) {
330                         client_buf[bytes] = '\0';
331                         telemetry->request_client = client;
332                         ret = rte_telemetry_parse(telemetry, client_buf);
333                         if (ret < 0) {
334                                 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
335                                                 ret);
336                                 return -1;
337                         }
338                 }
339         }
340
341         return 0;
342 }
343
344 static int32_t
345 rte_telemetry_run(void *userdata)
346 {
347         int ret;
348         struct telemetry_impl *telemetry = userdata;
349
350         if (telemetry == NULL) {
351                 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
352                 return -1;
353         }
354
355         ret = rte_telemetry_accept_new_client(telemetry);
356         if (ret < 0) {
357                 TELEMETRY_LOG_ERR("Accept and read new client failed");
358                 return -1;
359         }
360
361         ret = rte_telemetry_read_client_sockets(telemetry);
362         if (ret < 0) {
363                 TELEMETRY_LOG_ERR("Client socket read failed");
364                 return -1;
365         }
366
367         return 0;
368 }
369
370 static void
371 *rte_telemetry_run_thread_func(void *userdata)
372 {
373         int ret;
374         struct telemetry_impl *telemetry = userdata;
375
376         if (telemetry == NULL) {
377                 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
378                 pthread_exit(0);
379         }
380
381         while (telemetry->thread_status) {
382                 rte_telemetry_run(telemetry);
383                 ret = usleep(SLEEP_TIME);
384                 if (ret < 0)
385                         TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
386         }
387         pthread_exit(0);
388 }
389
390 static int32_t
391 rte_telemetry_set_socket_nonblock(int fd)
392 {
393         int flags;
394
395         if (fd < 0) {
396                 TELEMETRY_LOG_ERR("Invalid fd provided");
397                 return -1;
398         }
399
400         flags = fcntl(fd, F_GETFL, 0);
401         if (flags < 0)
402                 flags = 0;
403
404         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
405 }
406
407 static int32_t
408 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
409 {
410         int ret;
411         struct sockaddr_un addr;
412         char socket_path[BUF_SIZE];
413
414         if (telemetry == NULL)
415                 return -1;
416
417         telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
418         if (telemetry->server_fd == -1) {
419                 TELEMETRY_LOG_ERR("Failed to open socket");
420                 return -1;
421         }
422
423         ret  = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
424         if (ret < 0) {
425                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
426                 goto close_socket;
427         }
428
429         addr.sun_family = AF_UNIX;
430         rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
431         strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
432         unlink(socket_path);
433
434         if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
435                 sizeof(addr)) < 0) {
436                 TELEMETRY_LOG_ERR("Socket binding error");
437                 goto close_socket;
438         }
439
440         return 0;
441
442 close_socket:
443         if (close(telemetry->server_fd) < 0) {
444                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
445                 return -EPERM;
446         }
447
448         return -1;
449 }
450
451 int32_t
452 rte_telemetry_init(void)
453 {
454         int ret;
455         pthread_attr_t attr;
456         const char *telemetry_ctrl_thread = "telemetry";
457
458         if (static_telemetry) {
459                 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
460                 return -EALREADY;
461         }
462
463         static_telemetry = calloc(1, sizeof(struct telemetry_impl));
464         if (static_telemetry == NULL) {
465                 TELEMETRY_LOG_ERR("Memory could not be allocated");
466                 return -ENOMEM;
467         }
468
469         static_telemetry->socket_id = rte_socket_id();
470         rte_metrics_init(static_telemetry->socket_id);
471
472         ret = pthread_attr_init(&attr);
473         if (ret != 0) {
474                 TELEMETRY_LOG_ERR("Pthread attribute init failed");
475                 return -EPERM;
476         }
477
478         ret = rte_telemetry_create_socket(static_telemetry);
479         if (ret < 0) {
480                 ret = rte_telemetry_cleanup();
481                 if (ret < 0)
482                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
483                 return -EPERM;
484         }
485         TAILQ_INIT(&static_telemetry->client_list_head);
486
487         ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
488                 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
489                 (void *)static_telemetry);
490         static_telemetry->thread_status = 1;
491
492         if (ret < 0) {
493                 ret = rte_telemetry_cleanup();
494                 if (ret < 0)
495                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
496                 return -EPERM;
497         }
498
499         return 0;
500 }
501
502 static int32_t
503 rte_telemetry_client_cleanup(struct telemetry_client *client)
504 {
505         int ret;
506
507         ret = close(client->fd);
508         free(client->file_path);
509         free(client);
510
511         if (ret < 0) {
512                 TELEMETRY_LOG_ERR("Close client socket failed");
513                 return -EPERM;
514         }
515
516         return 0;
517 }
518
519 int32_t
520 rte_telemetry_cleanup(void)
521 {
522         int ret;
523         struct telemetry_impl *telemetry = static_telemetry;
524         telemetry_client *client, *temp_client;
525
526         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
527                 temp_client) {
528                 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
529                 ret = rte_telemetry_client_cleanup(client);
530                 if (ret < 0) {
531                         TELEMETRY_LOG_ERR("Client cleanup failed");
532                         return -EPERM;
533                 }
534         }
535
536         ret = close(telemetry->server_fd);
537         if (ret < 0) {
538                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
539                 free(telemetry);
540                 return -EPERM;
541         }
542
543         telemetry->thread_status = 0;
544         pthread_join(telemetry->thread_id, NULL);
545         free(telemetry);
546         static_telemetry = NULL;
547
548         return 0;
549 }
550
551 int32_t
552 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
553         const char *client_path)
554 {
555         int ret;
556         telemetry_client *client, *temp_client;
557
558         if (telemetry == NULL) {
559                 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
560                 return -ENODEV;
561         }
562
563         if (client_path == NULL) {
564                 TELEMETRY_LOG_ERR("Invalid client path");
565                 goto einval_fail;
566         }
567
568         if (TAILQ_EMPTY(&telemetry->client_list_head)) {
569                 TELEMETRY_LOG_ERR("There are no clients currently registered");
570                 return -EPERM;
571         }
572
573         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
574                         temp_client) {
575                 if (strcmp(client_path, client->file_path) == 0) {
576                         TAILQ_REMOVE(&telemetry->client_list_head, client,
577                                 client_list);
578                         ret = rte_telemetry_client_cleanup(client);
579
580                         if (ret < 0) {
581                                 TELEMETRY_LOG_ERR("Client cleanup failed");
582                                 return -EPERM;
583                         }
584
585                         return 0;
586                 }
587         }
588
589         TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
590         return -1;
591
592 einval_fail:
593         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
594         if (ret < 0)
595                 TELEMETRY_LOG_ERR("Could not send error");
596         return -EINVAL;
597 }
598
599 int32_t
600 rte_telemetry_register_client(struct telemetry_impl *telemetry,
601         const char *client_path)
602 {
603         int ret, fd;
604         struct sockaddr_un addrs;
605
606         if (telemetry == NULL) {
607                 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
608                 return -ENODEV;
609         }
610
611         if (client_path == NULL) {
612                 TELEMETRY_LOG_ERR("Invalid client path");
613                 return -EINVAL;
614         }
615
616         telemetry_client *client;
617         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
618                 if (strcmp(client_path, client->file_path) == 0) {
619                         TELEMETRY_LOG_WARN("'%s' already registered",
620                                         client_path);
621                         return -EINVAL;
622                 }
623         }
624
625         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
626         if (fd == -1) {
627                 TELEMETRY_LOG_ERR("Client socket error");
628                 return -EACCES;
629         }
630
631         ret = rte_telemetry_set_socket_nonblock(fd);
632         if (ret < 0) {
633                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
634                 return -EPERM;
635         }
636
637         addrs.sun_family = AF_UNIX;
638         strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
639         telemetry_client *new_client = malloc(sizeof(telemetry_client));
640         new_client->file_path = strdup(client_path);
641         new_client->fd = fd;
642
643         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
644                 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
645                                 client_path);
646                 ret = rte_telemetry_client_cleanup(new_client);
647                 if (ret < 0) {
648                         TELEMETRY_LOG_ERR("Client cleanup failed");
649                         return -EPERM;
650                 }
651                 return -EINVAL;
652         }
653
654         TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
655
656         return 0;
657 }
658
659 int32_t
660 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
661 {
662         int ret, action_int;
663         json_error_t error;
664         json_t *root = json_loads(buf, 0, &error);
665
666         if (root == NULL) {
667                 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
668                                 error.text);
669                 goto fail;
670         } else if (!json_is_object(root)) {
671                 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
672                 goto fail;
673         }
674
675         json_t *action = json_object_get(root, "action");
676         if (action == NULL) {
677                 TELEMETRY_LOG_WARN("Request does not have action field");
678                 goto fail;
679         } else if (!json_is_integer(action)) {
680                 TELEMETRY_LOG_WARN("Action value is not an integer");
681                 goto fail;
682         }
683
684         json_t *command = json_object_get(root, "command");
685         if (command == NULL) {
686                 TELEMETRY_LOG_WARN("Request does not have command field");
687                 goto fail;
688         } else if (!json_is_string(command)) {
689                 TELEMETRY_LOG_WARN("Command value is not a string");
690                 goto fail;
691         }
692
693         action_int = json_integer_value(action);
694         if (action_int != ACTION_POST) {
695                 TELEMETRY_LOG_WARN("Invalid action code");
696                 goto fail;
697         }
698
699         if (strcmp(json_string_value(command), "clients") != 0) {
700                 TELEMETRY_LOG_WARN("Invalid command");
701                 goto fail;
702         }
703
704         json_t *data = json_object_get(root, "data");
705         if (data == NULL) {
706                 TELEMETRY_LOG_WARN("Request does not have data field");
707                 goto fail;
708         }
709
710         json_t *client_path = json_object_get(data, "client_path");
711         if (client_path == NULL) {
712                 TELEMETRY_LOG_WARN("Request does not have client_path field");
713                 goto fail;
714         }
715
716         if (!json_is_string(client_path)) {
717                 TELEMETRY_LOG_WARN("Client_path value is not a string");
718                 goto fail;
719         }
720
721         ret = rte_telemetry_register_client(telemetry,
722                         json_string_value(client_path));
723         if (ret < 0) {
724                 TELEMETRY_LOG_ERR("Could not register client");
725                 telemetry->register_fail_count++;
726                 goto fail;
727         }
728
729         return 0;
730
731 fail:
732         TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
733         json_decref(root);
734         return -1;
735 }
736
737 static int32_t
738 rte_telemetry_dummy_client_socket(const char *valid_client_path)
739 {
740         int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
741         struct sockaddr_un addr = {0};
742
743         if (sockfd < 0) {
744                 TELEMETRY_LOG_ERR("Test socket creation failure");
745                 return -1;
746         }
747
748         addr.sun_family = AF_UNIX;
749         strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
750         unlink(valid_client_path);
751
752         if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
753                 TELEMETRY_LOG_ERR("Test socket binding failure");
754                 return -1;
755         }
756
757         if (listen(sockfd, 1) < 0) {
758                 TELEMETRY_LOG_ERR("Listen failure");
759                 return -1;
760         }
761
762         return sockfd;
763 }
764
765 int32_t
766 rte_telemetry_selftest(void)
767 {
768         const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
769         const char *valid_client_path = SELFTEST_VALID_CLIENT;
770         int ret, sockfd;
771
772         TELEMETRY_LOG_INFO("Selftest");
773
774         ret = rte_telemetry_init();
775         if (ret < 0) {
776                 TELEMETRY_LOG_ERR("Valid initialisation test failed");
777                 return -1;
778         }
779
780         TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
781
782         ret = rte_telemetry_init();
783         if (ret != -EALREADY) {
784                 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
785                 return -1;
786         }
787
788         TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
789
790         ret = rte_telemetry_unregister_client(static_telemetry,
791                         invalid_client_path);
792         if (ret != -EPERM) {
793                 TELEMETRY_LOG_ERR("Invalid unregister test failed");
794                 return -1;
795         }
796
797         TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
798
799         sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
800         if (sockfd < 0) {
801                 TELEMETRY_LOG_ERR("Test socket creation failed");
802                 return -1;
803         }
804
805         ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
806         if (ret != 0) {
807                 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
808                 return -1;
809         }
810
811         accept(sockfd, NULL, NULL);
812         TELEMETRY_LOG_INFO("Success - Valid register test passed");
813
814         ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
815         if (ret != -EINVAL) {
816                 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
817                 return -1;
818         }
819
820         TELEMETRY_LOG_INFO("Success - Invalid register test passed");
821
822         ret = rte_telemetry_unregister_client(static_telemetry,
823                 invalid_client_path);
824         if (ret != -1) {
825                 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
826                 return -1;
827         }
828
829         TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
830
831         ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
832         if (ret != 0) {
833                 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
834                 return -1;
835         }
836
837         TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
838
839         ret = rte_telemetry_cleanup();
840         if (ret < 0) {
841                 TELEMETRY_LOG_ERR("Cleanup test failed");
842                 return -1;
843         }
844
845         TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
846
847         return 0;
848 }
849
850 int32_t
851 rte_telemetry_socket_messaging_testing(int index, int socket)
852 {
853         struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
854         int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
855
856         if (telemetry == NULL) {
857                 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
858                 return -1;
859         }
860
861         telemetry->server_fd = socket;
862         telemetry->reg_index[0] = index;
863         TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
864         rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
865         TELEMETRY_LOG_INFO("Register valid client test");
866
867         ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
868                 recv_fd);
869         if (ret < 0) {
870                 TELEMETRY_LOG_ERR("Register valid client test failed!");
871                 free(telemetry);
872                 return -1;
873         }
874
875         TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
876
877         TELEMETRY_LOG_INFO("Register invalid/same client test");
878         ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
879                 &bad_recv_fd);
880         ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
881                 bad_send_fd, bad_recv_fd);
882         if (!ret) {
883                 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
884                 free(telemetry);
885                 return -1;
886         }
887
888         TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
889
890         ret = rte_telemetry_json_socket_message_test(telemetry, fd);
891         if (ret < 0) {
892                 free(telemetry);
893                 return -1;
894         }
895
896         free(telemetry);
897         return 0;
898 }
899
900 int32_t
901 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
902         int send_fd, int recv_fd)
903 {
904         int ret;
905         char good_req_string[BUF_SIZE];
906
907         snprintf(good_req_string, sizeof(good_req_string),
908         "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
909                 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
910
911         listen(recv_fd, 1);
912
913         ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
914         if (ret < 0) {
915                 TELEMETRY_LOG_ERR("Could not send message over socket");
916                 return -1;
917         }
918
919         rte_telemetry_run(telemetry);
920
921         if (telemetry->register_fail_count != 0)
922                 return -1;
923
924         *fd = accept(recv_fd, NULL, NULL);
925
926         return 0;
927 }
928
929 int32_t
930 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
931         int *recv_fd)
932 {
933         int ret;
934         const char *client_path = SOCKET_TEST_CLIENT_PATH;
935         char socket_path[BUF_SIZE];
936         struct sockaddr_un addr = {0};
937         struct sockaddr_un addrs = {0};
938         *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
939         *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
940
941         listen(telemetry->server_fd, 5);
942         addr.sun_family = AF_UNIX;
943         rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
944         strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
945
946         ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
947         if (ret < 0) {
948                 TELEMETRY_LOG_ERR("Could not connect socket");
949                 return -1;
950         }
951
952         telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
953
954         addrs.sun_family = AF_UNIX;
955         strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
956         unlink(client_path);
957
958         ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
959         if (ret < 0) {
960                 TELEMETRY_LOG_ERR("Could not bind socket");
961                 return -1;
962         }
963
964         return 0;
965 }
966
967 static int32_t
968 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
969 {
970         json_error_t error;
971         json_t *root = json_loads(buf, 0, &error);
972         int arraylen, i;
973         json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
974                *statsArrayObj;
975
976         stats = NULL;
977         port = NULL;
978         name = NULL;
979
980         if (buf == NULL) {
981                 TELEMETRY_LOG_ERR("JSON message is NULL");
982                 return -EINVAL;
983         }
984
985         if (root == NULL) {
986                 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
987                                 error.text);
988                 return -EPERM;
989         } else if (!json_is_object(root)) {
990                 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
991                 json_decref(root);
992                 return -EINVAL;
993         }
994
995         status = json_object_get(root, "status_code");
996         if (!status) {
997                 TELEMETRY_LOG_ERR("Request does not have status field");
998                 return -EINVAL;
999         } else if (!json_is_string(status)) {
1000                 TELEMETRY_LOG_ERR("Status value is not a string");
1001                 return -EINVAL;
1002         }
1003
1004         json_data_struct->status_code = strdup(json_string_value(status));
1005
1006         dataArray = json_object_get(root, "data");
1007         if (dataArray == NULL) {
1008                 TELEMETRY_LOG_ERR("Request does not have data field");
1009                 return -EINVAL;
1010         }
1011
1012         arraylen = json_array_size(dataArray);
1013         if (arraylen == 0) {
1014                 json_data_struct->data = "null";
1015                 return -EINVAL;
1016         }
1017
1018         for (i = 0; i < arraylen; i++) {
1019                 dataArrayObj = json_array_get(dataArray, i);
1020                 port = json_object_get(dataArrayObj, "port");
1021                 stats = json_object_get(dataArrayObj, "stats");
1022         }
1023
1024         if (port == NULL) {
1025                 TELEMETRY_LOG_ERR("Request does not have port field");
1026                 return -EINVAL;
1027         }
1028
1029         if (!json_is_integer(port)) {
1030                 TELEMETRY_LOG_ERR("Port value is not an integer");
1031                 return -EINVAL;
1032         }
1033
1034         json_data_struct->port = json_integer_value(port);
1035
1036         if (stats == NULL) {
1037                 TELEMETRY_LOG_ERR("Request does not have stats field");
1038                 return -EINVAL;
1039         }
1040
1041         arraylen = json_array_size(stats);
1042         for (i = 0; i < arraylen; i++) {
1043                 statsArrayObj = json_array_get(stats, i);
1044                 name = json_object_get(statsArrayObj, "name");
1045                 value = json_object_get(statsArrayObj, "value");
1046         }
1047
1048         if (name == NULL) {
1049                 TELEMETRY_LOG_ERR("Request does not have name field");
1050                 return -EINVAL;
1051         }
1052
1053         if (!json_is_string(name)) {
1054                 TELEMETRY_LOG_ERR("Stat name value is not a string");
1055                 return -EINVAL;
1056         }
1057
1058         json_data_struct->stat_name = strdup(json_string_value(name));
1059
1060         if (value == NULL) {
1061                 TELEMETRY_LOG_ERR("Request does not have value field");
1062                 return -EINVAL;
1063         }
1064
1065         if (!json_is_integer(value)) {
1066                 TELEMETRY_LOG_ERR("Stat value is not an integer");
1067                 return -EINVAL;
1068         }
1069
1070         json_data_struct->stat_value = json_integer_value(value);
1071
1072         return 0;
1073 }
1074
1075 static void
1076 rte_telemetry_free_test_data(struct json_data *data)
1077 {
1078         free(data->status_code);
1079         free(data->stat_name);
1080         free(data);
1081 }
1082
1083 int32_t
1084 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1085 {
1086         int ret;
1087         int port = 0;
1088         int value = 0;
1089         int fail_count = 0;
1090         int buffer_read = 0;
1091         char buf[BUF_SIZE];
1092         struct json_data *data_struct;
1093         errno = 0;
1094         const char *status = "Status OK: 200";
1095         const char *name = "rx_good_packets";
1096         const char *valid_json_message = "{\"action\":0,\"command\":"
1097         "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1098         ":[0],\"stats\":[\"rx_good_packets\"]}}";
1099
1100         ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1101         if (ret < 0) {
1102                 TELEMETRY_LOG_ERR("Could not send message over socket");
1103                 return -1;
1104         }
1105
1106         rte_telemetry_run(telemetry);
1107         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1108
1109         if (buffer_read == -1) {
1110                 TELEMETRY_LOG_ERR("Read error");
1111                 return -1;
1112         }
1113
1114         buf[buffer_read] = '\0';
1115         data_struct = calloc(1, sizeof(struct json_data));
1116         ret = rte_telemetry_stat_parse(buf, data_struct);
1117
1118         if (ret < 0) {
1119                 TELEMETRY_LOG_ERR("Could not parse stats");
1120                 fail_count++;
1121         }
1122
1123         if (strcmp(data_struct->status_code, status) != 0) {
1124                 TELEMETRY_LOG_ERR("Status code is invalid");
1125                 fail_count++;
1126         }
1127
1128         if (data_struct->port != port) {
1129                 TELEMETRY_LOG_ERR("Port is invalid");
1130                 fail_count++;
1131         }
1132
1133         if (strcmp(data_struct->stat_name, name) != 0) {
1134                 TELEMETRY_LOG_ERR("Stat name is invalid");
1135                 fail_count++;
1136         }
1137
1138         if (data_struct->stat_value != value) {
1139                 TELEMETRY_LOG_ERR("Stat value is invalid");
1140                 fail_count++;
1141         }
1142
1143         rte_telemetry_free_test_data(data_struct);
1144         if (fail_count > 0)
1145                 return -1;
1146
1147         TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1148
1149         return 0;
1150 }
1151
1152 int32_t
1153 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1154 {
1155         int ret;
1156         char buf[BUF_SIZE];
1157         int fail_count = 0;
1158         const char *invalid_json = "{]";
1159         const char *status = "Status Error: Unknown";
1160         const char *data = "null";
1161         struct json_data *data_struct;
1162         int buffer_read = 0;
1163         errno = 0;
1164
1165         ret = send(fd, invalid_json, strlen(invalid_json), 0);
1166         if (ret < 0) {
1167                 TELEMETRY_LOG_ERR("Could not send message over socket");
1168                 return -1;
1169         }
1170
1171         rte_telemetry_run(telemetry);
1172         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1173
1174         if (buffer_read == -1) {
1175                 TELEMETRY_LOG_ERR("Read error");
1176                 return -1;
1177         }
1178
1179         buf[buffer_read] = '\0';
1180
1181         data_struct = calloc(1, sizeof(struct json_data));
1182         ret = rte_telemetry_stat_parse(buf, data_struct);
1183
1184         if (ret < 0)
1185                 TELEMETRY_LOG_ERR("Could not parse stats");
1186
1187         if (strcmp(data_struct->status_code, status) != 0) {
1188                 TELEMETRY_LOG_ERR("Status code is invalid");
1189                 fail_count++;
1190         }
1191
1192         if (strcmp(data_struct->data, data) != 0) {
1193                 TELEMETRY_LOG_ERR("Data status is invalid");
1194                 fail_count++;
1195         }
1196
1197         rte_telemetry_free_test_data(data_struct);
1198         if (fail_count > 0)
1199                 return -1;
1200
1201         TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1202
1203         return 0;
1204 }
1205
1206 int32_t
1207 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1208 {
1209         int ret;
1210         char buf[BUF_SIZE];
1211         int fail_count = 0;
1212         const char *status = "Status Error: Invalid Argument 404";
1213         const char *data = "null";
1214         struct json_data *data_struct;
1215         const char *invalid_contents = "{\"action\":0,\"command\":"
1216         "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1217         ":[0],\"stats\":[\"some_invalid_param\","
1218         "\"another_invalid_param\"]}}";
1219         int buffer_read = 0;
1220         errno = 0;
1221
1222         ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1223         if (ret < 0) {
1224                 TELEMETRY_LOG_ERR("Could not send message over socket");
1225                 return -1;
1226         }
1227
1228         rte_telemetry_run(telemetry);
1229         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1230
1231         if (buffer_read == -1) {
1232                 TELEMETRY_LOG_ERR("Read error");
1233                 return -1;
1234         }
1235
1236         buf[buffer_read] = '\0';
1237         data_struct = calloc(1, sizeof(struct json_data));
1238         ret = rte_telemetry_stat_parse(buf, data_struct);
1239
1240         if (ret < 0)
1241                 TELEMETRY_LOG_ERR("Could not parse stats");
1242
1243         if (strcmp(data_struct->status_code, status) != 0) {
1244                 TELEMETRY_LOG_ERR("Status code is invalid");
1245                 fail_count++;
1246         }
1247
1248         if (strcmp(data_struct->data, data) != 0) {
1249                 TELEMETRY_LOG_ERR("Data status is invalid");
1250                 fail_count++;
1251         }
1252
1253         rte_telemetry_free_test_data(data_struct);
1254         if (fail_count > 0)
1255                 return -1;
1256
1257         TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1258
1259         return 0;
1260 }
1261
1262 int32_t
1263 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1264 {
1265         int ret;
1266         char buf[BUF_SIZE];
1267         int fail_count = 0;
1268         const char *status = "Status Error: Invalid Argument 404";
1269         const char *data = "null";
1270         struct json_data *data_struct;
1271         const char *empty_json  = "{}";
1272         int buffer_read = 0;
1273         errno = 0;
1274
1275         ret = (send(fd, empty_json, strlen(empty_json), 0));
1276         if (ret < 0) {
1277                 TELEMETRY_LOG_ERR("Could not send message over socket");
1278                 return -1;
1279         }
1280
1281         rte_telemetry_run(telemetry);
1282         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1283
1284         if (buffer_read == -1) {
1285                 TELEMETRY_LOG_ERR("Read error");
1286                 return -1;
1287         }
1288
1289         buf[buffer_read] = '\0';
1290         data_struct = calloc(1, sizeof(struct json_data));
1291         ret = rte_telemetry_stat_parse(buf, data_struct);
1292
1293         if (ret < 0)
1294                 TELEMETRY_LOG_ERR("Could not parse stats");
1295
1296         if (strcmp(data_struct->status_code, status) != 0) {
1297                 TELEMETRY_LOG_ERR("Status code is invalid");
1298                 fail_count++;
1299         }
1300
1301         if (strcmp(data_struct->data, data) != 0) {
1302                 TELEMETRY_LOG_ERR("Data status is invalid");
1303                 fail_count++;
1304         }
1305
1306         rte_telemetry_free_test_data(data_struct);
1307
1308         if (fail_count > 0)
1309                 return -1;
1310
1311         TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1312
1313         return 0;
1314 }
1315
1316 int32_t
1317 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1318 {
1319         uint16_t i;
1320         int ret, fail_count;
1321
1322         fail_count = 0;
1323         struct telemetry_message_test socket_json_tests[] = {
1324                 {.test_name = "Invalid JSON test",
1325                         .test_func_ptr = rte_telemetry_invalid_json_test},
1326                 {.test_name = "Valid JSON test",
1327                         .test_func_ptr = rte_telemetry_valid_json_test},
1328                 {.test_name = "JSON contents test",
1329                         .test_func_ptr = rte_telemetry_json_contents_test},
1330                 {.test_name = "JSON empty tests",
1331                         .test_func_ptr = rte_telemetry_json_empty_test}
1332                 };
1333
1334 #define NUM_TESTS RTE_DIM(socket_json_tests)
1335
1336         for (i = 0; i < NUM_TESTS; i++) {
1337                 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1338                 ret = (socket_json_tests[i].test_func_ptr)
1339                         (telemetry, fd);
1340                 if (ret < 0) {
1341                         TELEMETRY_LOG_ERR("%s failed",
1342                                         socket_json_tests[i].test_name);
1343                         fail_count++;
1344                 }
1345         }
1346
1347         if (fail_count > 0) {
1348                 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1349                                 fail_count);
1350                 return -1;
1351         }
1352
1353         TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1354
1355         return 0;
1356 }
1357
1358 int telemetry_log_level;
1359
1360 static struct rte_option option = {
1361         .name = "telemetry",
1362         .usage = "Enable telemetry backend",
1363         .cb = &rte_telemetry_init,
1364         .enabled = 0
1365 };
1366
1367 RTE_INIT(rte_telemetry_register)
1368 {
1369         telemetry_log_level = rte_log_register("lib.telemetry");
1370         if (telemetry_log_level >= 0)
1371                 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1372
1373         rte_option_register(&option);
1374 }