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