1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <sys/socket.h>
13 #include <rte_ethdev.h>
14 #include <rte_metrics.h>
15 #include <rte_option.h>
16 #include <rte_string_fns.h>
18 #include "rte_telemetry.h"
19 #include "rte_telemetry_internal.h"
20 #include "rte_telemetry_parser.h"
21 #include "rte_telemetry_socket_tests.h"
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"
31 static telemetry_impl *static_telemetry;
33 struct telemetry_message_test {
34 const char *test_name;
35 int (*test_func_ptr)(struct telemetry_impl *telemetry, int fd);
47 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
49 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
53 rte_telemetry_is_port_active(int port_id)
57 ret = rte_eth_find_next(port_id);
61 TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
68 rte_telemetry_update_metrics_ethdev(struct telemetry_impl *telemetry,
69 uint16_t port_id, int reg_start_index)
71 int ret, num_xstats, i;
72 struct rte_eth_xstat *eth_xstats;
74 if (!rte_eth_dev_is_valid_port(port_id)) {
75 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
76 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
78 TELEMETRY_LOG_ERR("Could not send error");
82 ret = rte_telemetry_is_port_active(port_id);
84 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
86 TELEMETRY_LOG_ERR("Could not send error");
90 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
92 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
94 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
96 TELEMETRY_LOG_ERR("Could not send error");
100 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
101 if (eth_xstats == NULL) {
102 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
103 ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
105 TELEMETRY_LOG_ERR("Could not send error");
109 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
110 if (ret < 0 || ret > num_xstats) {
112 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
113 port_id, num_xstats, ret);
114 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
116 TELEMETRY_LOG_ERR("Could not send error");
120 uint64_t xstats_values[num_xstats];
121 for (i = 0; i < num_xstats; i++)
122 xstats_values[i] = eth_xstats[i].value;
124 ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
127 TELEMETRY_LOG_ERR("Could not update metrics values");
128 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
130 TELEMETRY_LOG_ERR("Could not send error");
140 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
141 const char *json_string)
145 if (telemetry == NULL) {
146 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
150 if (telemetry->request_client == NULL) {
151 TELEMETRY_LOG_ERR("No client has been chosen to write to");
155 if (json_string == NULL) {
156 TELEMETRY_LOG_ERR("Invalid JSON string!");
160 ret = send(telemetry->request_client->fd,
161 json_string, strlen(json_string), 0);
163 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
164 telemetry->request_client->file_path);
172 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
176 const char *status_code, *json_buffer;
179 if (error_type == -EPERM)
180 status_code = "Status Error: Unknown";
181 else if (error_type == -EINVAL)
182 status_code = "Status Error: Invalid Argument 404";
183 else if (error_type == -ENOMEM)
184 status_code = "Status Error: Memory Allocation Error";
186 TELEMETRY_LOG_ERR("Invalid error type");
190 root = json_object();
193 TELEMETRY_LOG_ERR("Could not create root JSON object");
197 ret = json_object_set_new(root, "status_code", json_string(status_code));
199 TELEMETRY_LOG_ERR("Status code field cannot be set");
204 ret = json_object_set_new(root, "data", json_null());
206 TELEMETRY_LOG_ERR("Data field cannot be set");
211 json_buffer = json_dumps(root, 0);
214 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
216 TELEMETRY_LOG_ERR("Could not write to socket");
224 rte_telemetry_get_metrics(struct telemetry_impl *telemetry, uint32_t port_id,
225 struct rte_metric_value *metrics, struct rte_metric_name *names,
230 if (num_metrics < 0) {
231 TELEMETRY_LOG_ERR("Invalid metrics count");
233 } else if (num_metrics == 0) {
234 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
238 if (metrics == NULL) {
239 TELEMETRY_LOG_ERR("Metrics must be initialised.");
244 TELEMETRY_LOG_ERR("Names must be initialised.");
248 ret = rte_metrics_get_names(names, num_metrics);
249 if (ret < 0 || ret > num_metrics) {
250 TELEMETRY_LOG_ERR("Cannot get metrics names");
254 num_values = rte_metrics_get_values(port_id, NULL, 0);
255 ret = rte_metrics_get_values(port_id, metrics, num_values);
256 if (ret < 0 || ret > num_values) {
257 TELEMETRY_LOG_ERR("Cannot get metrics values");
264 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
266 TELEMETRY_LOG_ERR("Could not send error");
270 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
272 TELEMETRY_LOG_ERR("Could not send error");
278 rte_telemetry_json_format_stat(struct telemetry_impl *telemetry, json_t *stats,
279 const char *metric_name, uint64_t metric_value)
282 json_t *stat = json_object();
285 TELEMETRY_LOG_ERR("Could not create stat JSON object");
289 ret = json_object_set_new(stat, "name", json_string(metric_name));
291 TELEMETRY_LOG_ERR("Stat Name field cannot be set");
295 ret = json_object_set_new(stat, "value", json_integer(metric_value));
297 TELEMETRY_LOG_ERR("Stat Value field cannot be set");
301 ret = json_array_append_new(stats, stat);
303 TELEMETRY_LOG_ERR("Stat cannot be added to stats json array");
310 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
312 TELEMETRY_LOG_ERR("Could not send error");
318 rte_telemetry_json_format_port(struct telemetry_impl *telemetry,
319 uint32_t port_id, json_t *ports, uint32_t *metric_ids,
322 struct rte_metric_value *metrics = 0;
323 struct rte_metric_name *names = 0;
324 int num_metrics, ret, err_ret;
325 json_t *port, *stats;
328 num_metrics = rte_metrics_get_names(NULL, 0);
329 if (num_metrics < 0) {
330 TELEMETRY_LOG_ERR("Cannot get metrics count");
332 } else if (num_metrics == 0) {
333 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
337 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
338 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
339 if (metrics == NULL || names == NULL) {
340 TELEMETRY_LOG_ERR("Cannot allocate memory");
344 err_ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
346 TELEMETRY_LOG_ERR("Could not send error");
350 ret = rte_telemetry_get_metrics(telemetry, port_id, metrics, names,
355 TELEMETRY_LOG_ERR("rte_telemetry_get_metrics failed");
359 port = json_object();
360 stats = json_array();
361 if (port == NULL || stats == NULL) {
362 TELEMETRY_LOG_ERR("Could not create port/stats JSON objects");
366 ret = json_object_set_new(port, "port", json_integer(port_id));
368 TELEMETRY_LOG_ERR("Port field cannot be set");
372 for (i = 0; i < num_metric_ids; i++) {
373 int metric_id = metric_ids[i];
374 int metric_index = -1;
375 int metric_name_key = -1;
377 uint64_t metric_value;
379 if (metric_id >= num_metrics) {
380 TELEMETRY_LOG_ERR("Metric_id: %d is not valid",
385 for (j = 0; j < num_metrics; j++) {
386 if (metrics[j].key == metric_id) {
387 metric_name_key = metrics[j].key;
393 const char *metric_name = names[metric_name_key].name;
394 metric_value = metrics[metric_index].value;
396 if (metric_name_key < 0 || metric_index < 0) {
397 TELEMETRY_LOG_ERR("Could not get metric name/index");
401 ret = rte_telemetry_json_format_stat(telemetry, stats,
402 metric_name, metric_value);
404 TELEMETRY_LOG_ERR("Format stat with id: %u failed",
412 if (json_array_size(stats) == 0)
413 ret = json_object_set_new(port, "stats", json_null());
415 ret = json_object_set_new(port, "stats", stats);
418 TELEMETRY_LOG_ERR("Stats object cannot be set");
422 ret = json_array_append_new(ports, port);
424 TELEMETRY_LOG_ERR("Port object cannot be added to ports array");
435 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
437 TELEMETRY_LOG_ERR("Could not send error");
443 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
445 TELEMETRY_LOG_ERR("Could not send error");
450 rte_telemetry_encode_json_format(struct telemetry_impl *telemetry,
451 struct telemetry_encode_param *ep, char **json_buffer)
454 json_t *root, *ports;
460 ports = json_array();
462 TELEMETRY_LOG_ERR("Could not create ports JSON array");
466 if (ep->type == PORT_STATS) {
467 num_port_ids = ep->pp.num_port_ids;
468 num_metric_ids = ep->pp.num_metric_ids;
470 if (num_port_ids <= 0 || num_metric_ids <= 0) {
471 TELEMETRY_LOG_ERR("Please provide port and metric ids to query");
475 for (i = 0; i < num_port_ids; i++) {
476 port_id = ep->pp.port_ids[i];
477 if (!rte_eth_dev_is_valid_port(port_id)) {
478 TELEMETRY_LOG_ERR("Port: %d invalid",
484 for (i = 0; i < num_port_ids; i++) {
485 port_id = ep->pp.port_ids[i];
486 ret = rte_telemetry_json_format_port(telemetry,
487 port_id, ports, &ep->pp.metric_ids[0],
490 TELEMETRY_LOG_ERR("Format port in JSON failed");
494 } else if (ep->type == GLOBAL_STATS) {
495 /* Request Global Metrics */
496 ret = rte_telemetry_json_format_port(telemetry,
498 ports, &ep->gp.metric_ids[0],
499 ep->gp.num_metric_ids);
501 TELEMETRY_LOG_ERR(" Request Global Metrics Failed");
505 TELEMETRY_LOG_ERR(" Invalid metrics type in encode params");
509 root = json_object();
511 TELEMETRY_LOG_ERR("Could not create root JSON object");
515 ret = json_object_set_new(root, "status_code",
516 json_string("Status OK: 200"));
518 TELEMETRY_LOG_ERR("Status code field cannot be set");
522 ret = json_object_set_new(root, "data", ports);
524 TELEMETRY_LOG_ERR("Data field cannot be set");
528 *json_buffer = json_dumps(root, JSON_INDENT(2));
533 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
535 TELEMETRY_LOG_ERR("Could not send error");
539 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
541 TELEMETRY_LOG_ERR("Could not send error");
546 rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
547 struct telemetry_impl *telemetry)
550 char *json_buffer = NULL;
552 if (telemetry == NULL) {
553 TELEMETRY_LOG_ERR("Invalid telemetry argument");
557 if (ep->gp.num_metric_ids < 0) {
558 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
562 ret = rte_telemetry_encode_json_format(telemetry, ep,
565 TELEMETRY_LOG_ERR("JSON encode function failed");
569 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
571 TELEMETRY_LOG_ERR("Could not write to socket");
578 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
580 TELEMETRY_LOG_ERR("Could not send error");
585 rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
586 struct telemetry_impl *telemetry)
589 char *json_buffer = NULL;
593 if (telemetry == NULL) {
594 TELEMETRY_LOG_ERR("Invalid telemetry argument");
599 TELEMETRY_LOG_ERR("Invalid encode param argument");
603 if (ep->pp.num_metric_ids < 0) {
604 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
608 if (ep->pp.num_port_ids < 0) {
609 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
613 for (i = 0; i < ep->pp.num_port_ids; i++) {
614 port_id = ep->pp.port_ids[i];
615 if (!rte_eth_dev_is_valid_port(port_id)) {
616 TELEMETRY_LOG_ERR("Port: %d invalid", port_id);
620 ret = rte_telemetry_update_metrics_ethdev(telemetry,
621 port_id, telemetry->reg_index[i]);
623 TELEMETRY_LOG_ERR("Failed to update ethdev metrics");
628 ret = rte_telemetry_encode_json_format(telemetry, ep, &json_buffer);
630 TELEMETRY_LOG_ERR("JSON encode function failed");
634 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
636 TELEMETRY_LOG_ERR("Could not write to socket");
643 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
645 TELEMETRY_LOG_ERR("Could not send error");
651 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
653 int ret, num_xstats, ret_val, i;
654 struct rte_eth_xstat *eth_xstats = NULL;
655 struct rte_eth_xstat_name *eth_xstats_names = NULL;
657 if (!rte_eth_dev_is_valid_port(port_id)) {
658 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
662 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
663 if (num_xstats < 0) {
664 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
665 port_id, num_xstats);
669 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
670 if (eth_xstats == NULL) {
671 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
675 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
676 const char *xstats_names[num_xstats];
677 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
678 if (ret < 0 || ret > num_xstats) {
679 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
680 port_id, num_xstats, ret);
685 if (eth_xstats_names == NULL) {
686 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
691 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
692 if (ret < 0 || ret > num_xstats) {
693 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
694 port_id, num_xstats, ret);
699 for (i = 0; i < num_xstats; i++)
700 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
702 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
704 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
713 free(eth_xstats_names);
718 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
720 struct driver_index {
723 } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
729 RTE_ETH_FOREACH_DEV(pid) {
731 /* Different device types have different numbers of stats, so
732 * first check if the stats for this type of device have
733 * already been registered
735 for (i = 0; i < nb_drv_idx; i++) {
736 if (rte_eth_devices[pid].dev_ops == drv_idx[i].dev_ops) {
737 telemetry->reg_index[pid] = drv_idx[i].reg_index;
742 continue; /* we found a match, go to next port */
744 /* No match, register a new set of xstats for this port */
745 ret = rte_telemetry_reg_ethdev_to_metrics(pid);
747 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
750 telemetry->reg_index[pid] = ret;
751 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
752 drv_idx[nb_drv_idx].reg_index = ret;
756 telemetry->metrics_register_done = 1;
758 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
759 telemetry->server_fd);
763 ret = rte_telemetry_parser_test(telemetry);
765 TELEMETRY_LOG_ERR("Parser Tests Failed");
769 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
776 rte_telemetry_read_client(struct telemetry_impl *telemetry)
779 int ret, buffer_read;
781 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
783 if (buffer_read == -1) {
784 TELEMETRY_LOG_ERR("Read error");
786 } else if (buffer_read == 0) {
789 buf[buffer_read] = '\0';
790 ret = rte_telemetry_parse_client_message(telemetry, buf);
792 TELEMETRY_LOG_WARN("Parse message failed");
797 if (close(telemetry->accept_fd) < 0) {
798 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
802 telemetry->accept_fd = 0;
808 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
812 if (telemetry->accept_fd <= 0) {
813 ret = listen(telemetry->server_fd, 1);
815 TELEMETRY_LOG_ERR("Listening error with server fd");
819 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
820 if (telemetry->accept_fd >= 0 &&
821 telemetry->metrics_register_done == 0) {
822 ret = rte_telemetry_initial_accept(telemetry);
824 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
829 ret = rte_telemetry_read_client(telemetry);
831 TELEMETRY_LOG_ERR("Failed to read socket buffer");
840 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
843 telemetry_client *client;
844 char client_buf[BUF_SIZE];
847 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
848 bytes = read(client->fd, client_buf, BUF_SIZE-1);
851 client_buf[bytes] = '\0';
852 telemetry->request_client = client;
853 ret = rte_telemetry_parse(telemetry, client_buf);
855 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
866 rte_telemetry_run(void *userdata)
869 struct telemetry_impl *telemetry = userdata;
871 if (telemetry == NULL) {
872 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
876 ret = rte_telemetry_accept_new_client(telemetry);
878 TELEMETRY_LOG_ERR("Accept and read new client failed");
882 ret = rte_telemetry_read_client_sockets(telemetry);
884 TELEMETRY_LOG_ERR("Client socket read failed");
892 *rte_telemetry_run_thread_func(void *userdata)
895 struct telemetry_impl *telemetry = userdata;
897 if (telemetry == NULL) {
898 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
902 while (telemetry->thread_status) {
903 rte_telemetry_run(telemetry);
904 ret = usleep(SLEEP_TIME);
906 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
912 rte_telemetry_set_socket_nonblock(int fd)
917 TELEMETRY_LOG_ERR("Invalid fd provided");
921 flags = fcntl(fd, F_GETFL, 0);
925 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
929 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
932 struct sockaddr_un addr;
933 char socket_path[BUF_SIZE];
935 if (telemetry == NULL)
938 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
939 if (telemetry->server_fd == -1) {
940 TELEMETRY_LOG_ERR("Failed to open socket");
944 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
946 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
950 addr.sun_family = AF_UNIX;
951 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
952 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
955 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
957 TELEMETRY_LOG_ERR("Socket binding error");
964 if (close(telemetry->server_fd) < 0) {
965 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
973 rte_telemetry_init(void)
977 const char *telemetry_ctrl_thread = "telemetry";
979 if (static_telemetry) {
980 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
984 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
985 if (static_telemetry == NULL) {
986 TELEMETRY_LOG_ERR("Memory could not be allocated");
990 static_telemetry->socket_id = rte_socket_id();
991 rte_metrics_init(static_telemetry->socket_id);
993 ret = pthread_attr_init(&attr);
995 TELEMETRY_LOG_ERR("Pthread attribute init failed");
999 ret = rte_telemetry_create_socket(static_telemetry);
1001 ret = rte_telemetry_cleanup();
1003 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1006 TAILQ_INIT(&static_telemetry->client_list_head);
1008 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
1009 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
1010 (void *)static_telemetry);
1011 static_telemetry->thread_status = 1;
1014 ret = rte_telemetry_cleanup();
1016 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1024 rte_telemetry_client_cleanup(struct telemetry_client *client)
1028 ret = close(client->fd);
1029 free(client->file_path);
1033 TELEMETRY_LOG_ERR("Close client socket failed");
1041 rte_telemetry_cleanup(void)
1044 struct telemetry_impl *telemetry = static_telemetry;
1045 telemetry_client *client, *temp_client;
1047 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1049 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
1050 ret = rte_telemetry_client_cleanup(client);
1052 TELEMETRY_LOG_ERR("Client cleanup failed");
1057 ret = close(telemetry->server_fd);
1059 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
1064 telemetry->thread_status = 0;
1065 pthread_join(telemetry->thread_id, NULL);
1067 static_telemetry = NULL;
1073 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
1074 const char *client_path)
1077 telemetry_client *client, *temp_client;
1079 if (telemetry == NULL) {
1080 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
1084 if (client_path == NULL) {
1085 TELEMETRY_LOG_ERR("Invalid client path");
1089 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
1090 TELEMETRY_LOG_ERR("There are no clients currently registered");
1094 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1096 if (strcmp(client_path, client->file_path) == 0) {
1097 TAILQ_REMOVE(&telemetry->client_list_head, client,
1099 ret = rte_telemetry_client_cleanup(client);
1102 TELEMETRY_LOG_ERR("Client cleanup failed");
1110 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
1114 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
1116 TELEMETRY_LOG_ERR("Could not send error");
1121 rte_telemetry_register_client(struct telemetry_impl *telemetry,
1122 const char *client_path)
1125 struct sockaddr_un addrs;
1127 if (telemetry == NULL) {
1128 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
1132 if (client_path == NULL) {
1133 TELEMETRY_LOG_ERR("Invalid client path");
1137 telemetry_client *client;
1138 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
1139 if (strcmp(client_path, client->file_path) == 0) {
1140 TELEMETRY_LOG_WARN("'%s' already registered",
1146 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1148 TELEMETRY_LOG_ERR("Client socket error");
1152 ret = rte_telemetry_set_socket_nonblock(fd);
1154 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
1158 addrs.sun_family = AF_UNIX;
1159 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1160 telemetry_client *new_client = malloc(sizeof(telemetry_client));
1161 new_client->file_path = strdup(client_path);
1162 new_client->fd = fd;
1164 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
1165 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
1167 ret = rte_telemetry_client_cleanup(new_client);
1169 TELEMETRY_LOG_ERR("Client cleanup failed");
1175 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
1181 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
1183 int ret, action_int;
1185 json_t *root = json_loads(buf, 0, &error);
1188 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
1191 } else if (!json_is_object(root)) {
1192 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
1196 json_t *action = json_object_get(root, "action");
1197 if (action == NULL) {
1198 TELEMETRY_LOG_WARN("Request does not have action field");
1200 } else if (!json_is_integer(action)) {
1201 TELEMETRY_LOG_WARN("Action value is not an integer");
1205 json_t *command = json_object_get(root, "command");
1206 if (command == NULL) {
1207 TELEMETRY_LOG_WARN("Request does not have command field");
1209 } else if (!json_is_string(command)) {
1210 TELEMETRY_LOG_WARN("Command value is not a string");
1214 action_int = json_integer_value(action);
1215 if (action_int != ACTION_POST) {
1216 TELEMETRY_LOG_WARN("Invalid action code");
1220 if (strcmp(json_string_value(command), "clients") != 0) {
1221 TELEMETRY_LOG_WARN("Invalid command");
1225 json_t *data = json_object_get(root, "data");
1227 TELEMETRY_LOG_WARN("Request does not have data field");
1231 json_t *client_path = json_object_get(data, "client_path");
1232 if (client_path == NULL) {
1233 TELEMETRY_LOG_WARN("Request does not have client_path field");
1237 if (!json_is_string(client_path)) {
1238 TELEMETRY_LOG_WARN("Client_path value is not a string");
1242 ret = rte_telemetry_register_client(telemetry,
1243 json_string_value(client_path));
1245 TELEMETRY_LOG_ERR("Could not register client");
1246 telemetry->register_fail_count++;
1253 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
1259 rte_telemetry_dummy_client_socket(const char *valid_client_path)
1261 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1262 struct sockaddr_un addr = {0};
1265 TELEMETRY_LOG_ERR("Test socket creation failure");
1269 addr.sun_family = AF_UNIX;
1270 strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
1271 unlink(valid_client_path);
1273 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1274 TELEMETRY_LOG_ERR("Test socket binding failure");
1278 if (listen(sockfd, 1) < 0) {
1279 TELEMETRY_LOG_ERR("Listen failure");
1287 rte_telemetry_selftest(void)
1289 const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
1290 const char *valid_client_path = SELFTEST_VALID_CLIENT;
1293 TELEMETRY_LOG_INFO("Selftest");
1295 ret = rte_telemetry_init();
1297 TELEMETRY_LOG_ERR("Valid initialisation test failed");
1301 TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
1303 ret = rte_telemetry_init();
1304 if (ret != -EALREADY) {
1305 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
1309 TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
1311 ret = rte_telemetry_unregister_client(static_telemetry,
1312 invalid_client_path);
1313 if (ret != -EPERM) {
1314 TELEMETRY_LOG_ERR("Invalid unregister test failed");
1318 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1320 sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
1322 TELEMETRY_LOG_ERR("Test socket creation failed");
1326 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1328 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
1332 accept(sockfd, NULL, NULL);
1333 TELEMETRY_LOG_INFO("Success - Valid register test passed");
1335 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1336 if (ret != -EINVAL) {
1337 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
1341 TELEMETRY_LOG_INFO("Success - Invalid register test passed");
1343 ret = rte_telemetry_unregister_client(static_telemetry,
1344 invalid_client_path);
1346 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
1350 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1352 ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
1354 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
1358 TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
1360 ret = rte_telemetry_cleanup();
1362 TELEMETRY_LOG_ERR("Cleanup test failed");
1366 TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
1372 rte_telemetry_socket_messaging_testing(int index, int socket)
1374 struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
1375 int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
1377 if (telemetry == NULL) {
1378 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
1382 telemetry->server_fd = socket;
1383 telemetry->reg_index[0] = index;
1384 TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
1385 rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
1386 TELEMETRY_LOG_INFO("Register valid client test");
1388 ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
1391 TELEMETRY_LOG_ERR("Register valid client test failed!");
1396 TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
1398 TELEMETRY_LOG_INFO("Register invalid/same client test");
1399 ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
1401 ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
1402 bad_send_fd, bad_recv_fd);
1404 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
1409 TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
1411 ret = rte_telemetry_json_socket_message_test(telemetry, fd);
1422 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
1423 int send_fd, int recv_fd)
1426 char good_req_string[BUF_SIZE];
1428 snprintf(good_req_string, sizeof(good_req_string),
1429 "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
1430 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
1434 ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
1436 TELEMETRY_LOG_ERR("Could not send message over socket");
1440 rte_telemetry_run(telemetry);
1442 if (telemetry->register_fail_count != 0)
1445 *fd = accept(recv_fd, NULL, NULL);
1451 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
1455 const char *client_path = SOCKET_TEST_CLIENT_PATH;
1456 char socket_path[BUF_SIZE];
1457 struct sockaddr_un addr = {0};
1458 struct sockaddr_un addrs = {0};
1459 *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1460 *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1462 listen(telemetry->server_fd, 5);
1463 addr.sun_family = AF_UNIX;
1464 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
1465 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
1467 ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
1469 TELEMETRY_LOG_ERR("Could not connect socket");
1473 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
1475 addrs.sun_family = AF_UNIX;
1476 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1477 unlink(client_path);
1479 ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
1481 TELEMETRY_LOG_ERR("Could not bind socket");
1489 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
1492 json_t *root = json_loads(buf, 0, &error);
1494 json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
1502 TELEMETRY_LOG_ERR("JSON message is NULL");
1507 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
1510 } else if (!json_is_object(root)) {
1511 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1516 status = json_object_get(root, "status_code");
1518 TELEMETRY_LOG_ERR("Request does not have status field");
1520 } else if (!json_is_string(status)) {
1521 TELEMETRY_LOG_ERR("Status value is not a string");
1525 json_data_struct->status_code = strdup(json_string_value(status));
1527 dataArray = json_object_get(root, "data");
1528 if (dataArray == NULL) {
1529 TELEMETRY_LOG_ERR("Request does not have data field");
1533 arraylen = json_array_size(dataArray);
1534 if (arraylen == 0) {
1535 json_data_struct->data = "null";
1539 for (i = 0; i < arraylen; i++) {
1540 dataArrayObj = json_array_get(dataArray, i);
1541 port = json_object_get(dataArrayObj, "port");
1542 stats = json_object_get(dataArrayObj, "stats");
1546 TELEMETRY_LOG_ERR("Request does not have port field");
1550 if (!json_is_integer(port)) {
1551 TELEMETRY_LOG_ERR("Port value is not an integer");
1555 json_data_struct->port = json_integer_value(port);
1557 if (stats == NULL) {
1558 TELEMETRY_LOG_ERR("Request does not have stats field");
1562 arraylen = json_array_size(stats);
1563 for (i = 0; i < arraylen; i++) {
1564 statsArrayObj = json_array_get(stats, i);
1565 name = json_object_get(statsArrayObj, "name");
1566 value = json_object_get(statsArrayObj, "value");
1570 TELEMETRY_LOG_ERR("Request does not have name field");
1574 if (!json_is_string(name)) {
1575 TELEMETRY_LOG_ERR("Stat name value is not a string");
1579 json_data_struct->stat_name = strdup(json_string_value(name));
1581 if (value == NULL) {
1582 TELEMETRY_LOG_ERR("Request does not have value field");
1586 if (!json_is_integer(value)) {
1587 TELEMETRY_LOG_ERR("Stat value is not an integer");
1591 json_data_struct->stat_value = json_integer_value(value);
1597 rte_telemetry_free_test_data(struct json_data *data)
1599 free(data->status_code);
1600 free(data->stat_name);
1605 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1611 int buffer_read = 0;
1613 struct json_data *data_struct;
1615 const char *status = "Status OK: 200";
1616 const char *name = "rx_good_packets";
1617 const char *valid_json_message = "{\"action\":0,\"command\":"
1618 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1619 ":[0],\"stats\":[\"rx_good_packets\"]}}";
1621 ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1623 TELEMETRY_LOG_ERR("Could not send message over socket");
1627 rte_telemetry_run(telemetry);
1628 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1630 if (buffer_read == -1) {
1631 TELEMETRY_LOG_ERR("Read error");
1635 buf[buffer_read] = '\0';
1636 data_struct = calloc(1, sizeof(struct json_data));
1637 ret = rte_telemetry_stat_parse(buf, data_struct);
1640 TELEMETRY_LOG_ERR("Could not parse stats");
1644 if (strcmp(data_struct->status_code, status) != 0) {
1645 TELEMETRY_LOG_ERR("Status code is invalid");
1649 if (data_struct->port != port) {
1650 TELEMETRY_LOG_ERR("Port is invalid");
1654 if (strcmp(data_struct->stat_name, name) != 0) {
1655 TELEMETRY_LOG_ERR("Stat name is invalid");
1659 if (data_struct->stat_value != value) {
1660 TELEMETRY_LOG_ERR("Stat value is invalid");
1664 rte_telemetry_free_test_data(data_struct);
1668 TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1674 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1679 const char *invalid_json = "{]";
1680 const char *status = "Status Error: Unknown";
1681 const char *data = "null";
1682 struct json_data *data_struct;
1683 int buffer_read = 0;
1686 ret = send(fd, invalid_json, strlen(invalid_json), 0);
1688 TELEMETRY_LOG_ERR("Could not send message over socket");
1692 rte_telemetry_run(telemetry);
1693 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1695 if (buffer_read == -1) {
1696 TELEMETRY_LOG_ERR("Read error");
1700 buf[buffer_read] = '\0';
1702 data_struct = calloc(1, sizeof(struct json_data));
1703 ret = rte_telemetry_stat_parse(buf, data_struct);
1706 TELEMETRY_LOG_ERR("Could not parse stats");
1708 if (strcmp(data_struct->status_code, status) != 0) {
1709 TELEMETRY_LOG_ERR("Status code is invalid");
1713 if (strcmp(data_struct->data, data) != 0) {
1714 TELEMETRY_LOG_ERR("Data status is invalid");
1718 rte_telemetry_free_test_data(data_struct);
1722 TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1728 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1733 const char *status = "Status Error: Invalid Argument 404";
1734 const char *data = "null";
1735 struct json_data *data_struct;
1736 const char *invalid_contents = "{\"action\":0,\"command\":"
1737 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1738 ":[0],\"stats\":[\"some_invalid_param\","
1739 "\"another_invalid_param\"]}}";
1740 int buffer_read = 0;
1743 ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1745 TELEMETRY_LOG_ERR("Could not send message over socket");
1749 rte_telemetry_run(telemetry);
1750 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1752 if (buffer_read == -1) {
1753 TELEMETRY_LOG_ERR("Read error");
1757 buf[buffer_read] = '\0';
1758 data_struct = calloc(1, sizeof(struct json_data));
1759 ret = rte_telemetry_stat_parse(buf, data_struct);
1762 TELEMETRY_LOG_ERR("Could not parse stats");
1764 if (strcmp(data_struct->status_code, status) != 0) {
1765 TELEMETRY_LOG_ERR("Status code is invalid");
1769 if (strcmp(data_struct->data, data) != 0) {
1770 TELEMETRY_LOG_ERR("Data status is invalid");
1774 rte_telemetry_free_test_data(data_struct);
1778 TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1784 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1789 const char *status = "Status Error: Invalid Argument 404";
1790 const char *data = "null";
1791 struct json_data *data_struct;
1792 const char *empty_json = "{}";
1793 int buffer_read = 0;
1796 ret = (send(fd, empty_json, strlen(empty_json), 0));
1798 TELEMETRY_LOG_ERR("Could not send message over socket");
1802 rte_telemetry_run(telemetry);
1803 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1805 if (buffer_read == -1) {
1806 TELEMETRY_LOG_ERR("Read error");
1810 buf[buffer_read] = '\0';
1811 data_struct = calloc(1, sizeof(struct json_data));
1812 ret = rte_telemetry_stat_parse(buf, data_struct);
1815 TELEMETRY_LOG_ERR("Could not parse stats");
1817 if (strcmp(data_struct->status_code, status) != 0) {
1818 TELEMETRY_LOG_ERR("Status code is invalid");
1822 if (strcmp(data_struct->data, data) != 0) {
1823 TELEMETRY_LOG_ERR("Data status is invalid");
1827 rte_telemetry_free_test_data(data_struct);
1832 TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1838 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1841 int ret, fail_count;
1844 struct telemetry_message_test socket_json_tests[] = {
1845 {.test_name = "Invalid JSON test",
1846 .test_func_ptr = rte_telemetry_invalid_json_test},
1847 {.test_name = "Valid JSON test",
1848 .test_func_ptr = rte_telemetry_valid_json_test},
1849 {.test_name = "JSON contents test",
1850 .test_func_ptr = rte_telemetry_json_contents_test},
1851 {.test_name = "JSON empty tests",
1852 .test_func_ptr = rte_telemetry_json_empty_test}
1855 #define NUM_TESTS RTE_DIM(socket_json_tests)
1857 for (i = 0; i < NUM_TESTS; i++) {
1858 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1859 ret = (socket_json_tests[i].test_func_ptr)
1862 TELEMETRY_LOG_ERR("%s failed",
1863 socket_json_tests[i].test_name);
1868 if (fail_count > 0) {
1869 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1874 TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1879 int telemetry_log_level;
1881 static struct rte_option option = {
1882 .name = "telemetry",
1883 .usage = "Enable telemetry backend",
1884 .cb = &rte_telemetry_init,
1888 RTE_INIT(rte_telemetry_register)
1890 telemetry_log_level = rte_log_register("lib.telemetry");
1891 if (telemetry_log_level >= 0)
1892 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1894 rte_option_register(&option);