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