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_parser_test.h"
22 #include "rte_telemetry_socket_tests.h"
28 #define SELFTEST_VALID_CLIENT "/var/run/dpdk/valid_client"
29 #define SELFTEST_INVALID_CLIENT "/var/run/dpdk/invalid_client"
30 #define SOCKET_TEST_CLIENT_PATH "/var/run/dpdk/client"
32 static telemetry_impl *static_telemetry;
34 struct telemetry_message_test {
36 int (*test_func_ptr)(struct telemetry_impl *telemetry, int fd);
48 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
50 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
54 rte_telemetry_is_port_active(int port_id)
58 ret = rte_eth_find_next(port_id);
62 TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
69 rte_telemetry_update_metrics_ethdev(struct telemetry_impl *telemetry,
70 uint16_t port_id, int reg_start_index)
72 int ret, num_xstats, i;
73 struct rte_eth_xstat *eth_xstats;
75 if (!rte_eth_dev_is_valid_port(port_id)) {
76 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
77 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
79 TELEMETRY_LOG_ERR("Could not send error");
83 ret = rte_telemetry_is_port_active(port_id);
85 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
87 TELEMETRY_LOG_ERR("Could not send error");
91 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
93 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
95 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
97 TELEMETRY_LOG_ERR("Could not send error");
101 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
102 if (eth_xstats == NULL) {
103 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
104 ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
106 TELEMETRY_LOG_ERR("Could not send error");
110 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
111 if (ret < 0 || ret > num_xstats) {
113 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
114 port_id, num_xstats, ret);
115 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
117 TELEMETRY_LOG_ERR("Could not send error");
121 uint64_t xstats_values[num_xstats];
122 for (i = 0; i < num_xstats; i++)
123 xstats_values[i] = eth_xstats[i].value;
125 ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
128 TELEMETRY_LOG_ERR("Could not update metrics values");
129 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
131 TELEMETRY_LOG_ERR("Could not send error");
141 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
142 const char *json_string)
146 if (telemetry == NULL) {
147 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
151 if (telemetry->request_client == NULL) {
152 TELEMETRY_LOG_ERR("No client has been chosen to write to");
156 if (json_string == NULL) {
157 TELEMETRY_LOG_ERR("Invalid JSON string!");
161 ret = send(telemetry->request_client->fd,
162 json_string, strlen(json_string), 0);
164 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
165 telemetry->request_client->file_path);
173 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
177 const char *status_code, *json_buffer;
180 if (error_type == -EPERM)
181 status_code = "Status Error: Unknown";
182 else if (error_type == -EINVAL)
183 status_code = "Status Error: Invalid Argument 404";
184 else if (error_type == -ENOMEM)
185 status_code = "Status Error: Memory Allocation Error";
187 TELEMETRY_LOG_ERR("Invalid error type");
191 root = json_object();
194 TELEMETRY_LOG_ERR("Could not create root JSON object");
198 ret = json_object_set_new(root, "status_code", json_string(status_code));
200 TELEMETRY_LOG_ERR("Status code field cannot be set");
205 ret = json_object_set_new(root, "data", json_null());
207 TELEMETRY_LOG_ERR("Data field cannot be set");
212 json_buffer = json_dumps(root, 0);
215 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
217 TELEMETRY_LOG_ERR("Could not write to socket");
225 rte_telemetry_get_metrics(struct telemetry_impl *telemetry, uint32_t port_id,
226 struct rte_metric_value *metrics, struct rte_metric_name *names,
231 if (num_metrics < 0) {
232 TELEMETRY_LOG_ERR("Invalid metrics count");
234 } else if (num_metrics == 0) {
235 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
239 if (metrics == NULL) {
240 TELEMETRY_LOG_ERR("Metrics must be initialised.");
245 TELEMETRY_LOG_ERR("Names must be initialised.");
249 ret = rte_metrics_get_names(names, num_metrics);
250 if (ret < 0 || ret > num_metrics) {
251 TELEMETRY_LOG_ERR("Cannot get metrics names");
255 num_values = rte_metrics_get_values(port_id, NULL, 0);
256 ret = rte_metrics_get_values(port_id, metrics, num_values);
257 if (ret < 0 || ret > num_values) {
258 TELEMETRY_LOG_ERR("Cannot get metrics values");
265 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
267 TELEMETRY_LOG_ERR("Could not send error");
271 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
273 TELEMETRY_LOG_ERR("Could not send error");
279 rte_telemetry_json_format_stat(struct telemetry_impl *telemetry, json_t *stats,
280 const char *metric_name, uint64_t metric_value)
283 json_t *stat = json_object();
286 TELEMETRY_LOG_ERR("Could not create stat JSON object");
290 ret = json_object_set_new(stat, "name", json_string(metric_name));
292 TELEMETRY_LOG_ERR("Stat Name field cannot be set");
296 ret = json_object_set_new(stat, "value", json_integer(metric_value));
298 TELEMETRY_LOG_ERR("Stat Value field cannot be set");
302 ret = json_array_append_new(stats, stat);
304 TELEMETRY_LOG_ERR("Stat cannot be added to stats json array");
311 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
313 TELEMETRY_LOG_ERR("Could not send error");
319 rte_telemetry_json_format_port(struct telemetry_impl *telemetry,
320 uint32_t port_id, json_t *ports, uint32_t *metric_ids,
321 uint32_t num_metric_ids)
323 struct rte_metric_value *metrics = 0;
324 struct rte_metric_name *names = 0;
325 int num_metrics, ret, err_ret;
326 json_t *port, *stats;
329 num_metrics = rte_metrics_get_names(NULL, 0);
330 if (num_metrics < 0) {
331 TELEMETRY_LOG_ERR("Cannot get metrics count");
333 } else if (num_metrics == 0) {
334 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
338 metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
339 names = malloc(sizeof(struct rte_metric_name) * num_metrics);
340 if (metrics == NULL || names == NULL) {
341 TELEMETRY_LOG_ERR("Cannot allocate memory");
345 err_ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
347 TELEMETRY_LOG_ERR("Could not send error");
351 ret = rte_telemetry_get_metrics(telemetry, port_id, metrics, names,
356 TELEMETRY_LOG_ERR("rte_telemetry_get_metrics failed");
360 port = json_object();
361 stats = json_array();
362 if (port == NULL || stats == NULL) {
363 TELEMETRY_LOG_ERR("Could not create port/stats JSON objects");
367 ret = json_object_set_new(port, "port", json_integer(port_id));
369 TELEMETRY_LOG_ERR("Port field cannot be set");
373 for (i = 0; i < num_metric_ids; i++) {
374 int metric_id = metric_ids[i];
375 int metric_index = -1;
376 int metric_name_key = -1;
378 uint64_t metric_value;
380 if (metric_id >= num_metrics) {
381 TELEMETRY_LOG_ERR("Metric_id: %d is not valid",
386 for (j = 0; j < num_metrics; j++) {
387 if (metrics[j].key == metric_id) {
388 metric_name_key = metrics[j].key;
394 const char *metric_name = names[metric_name_key].name;
395 metric_value = metrics[metric_index].value;
397 if (metric_name_key < 0 || metric_index < 0) {
398 TELEMETRY_LOG_ERR("Could not get metric name/index");
402 ret = rte_telemetry_json_format_stat(telemetry, stats,
403 metric_name, metric_value);
405 TELEMETRY_LOG_ERR("Format stat with id: %u failed",
413 if (json_array_size(stats) == 0)
414 ret = json_object_set_new(port, "stats", json_null());
416 ret = json_object_set_new(port, "stats", stats);
419 TELEMETRY_LOG_ERR("Stats object cannot be set");
423 ret = json_array_append_new(ports, port);
425 TELEMETRY_LOG_ERR("Port object cannot be added to ports array");
436 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
438 TELEMETRY_LOG_ERR("Could not send error");
444 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
446 TELEMETRY_LOG_ERR("Could not send error");
451 rte_telemetry_encode_json_format(struct telemetry_impl *telemetry,
452 uint32_t *port_ids, uint32_t num_port_ids, uint32_t *metric_ids,
453 uint32_t num_metric_ids, char **json_buffer)
456 json_t *root, *ports;
459 if (num_port_ids <= 0 || num_metric_ids <= 0) {
460 TELEMETRY_LOG_ERR("Please provide port and metric ids to query");
464 ports = json_array();
466 TELEMETRY_LOG_ERR("Could not create ports JSON array");
470 for (i = 0; i < num_port_ids; i++) {
471 if (!rte_eth_dev_is_valid_port(port_ids[i])) {
472 TELEMETRY_LOG_ERR("Port: %d invalid", port_ids[i]);
477 for (i = 0; i < num_port_ids; i++) {
478 ret = rte_telemetry_json_format_port(telemetry, port_ids[i],
479 ports, metric_ids, num_metric_ids);
481 TELEMETRY_LOG_ERR("Format port in JSON failed");
486 root = json_object();
488 TELEMETRY_LOG_ERR("Could not create root JSON object");
492 ret = json_object_set_new(root, "status_code",
493 json_string("Status OK: 200"));
495 TELEMETRY_LOG_ERR("Status code field cannot be set");
499 ret = json_object_set_new(root, "data", ports);
501 TELEMETRY_LOG_ERR("Data field cannot be set");
505 *json_buffer = json_dumps(root, JSON_INDENT(2));
510 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
512 TELEMETRY_LOG_ERR("Could not send error");
516 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
518 TELEMETRY_LOG_ERR("Could not send error");
523 rte_telemetry_send_ports_stats_values(uint32_t *metric_ids, int num_metric_ids,
524 uint32_t *port_ids, int num_port_ids, struct telemetry_impl *telemetry)
527 char *json_buffer = NULL;
529 if (telemetry == NULL) {
530 TELEMETRY_LOG_ERR("Invalid telemetry argument");
534 if (metric_ids == NULL) {
535 TELEMETRY_LOG_ERR("Invalid metric_ids array");
539 if (num_metric_ids < 0) {
540 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
544 if (port_ids == NULL) {
545 TELEMETRY_LOG_ERR("Invalid port_ids array");
549 if (num_port_ids < 0) {
550 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
554 for (i = 0; i < num_port_ids; i++) {
555 if (!rte_eth_dev_is_valid_port(port_ids[i])) {
556 TELEMETRY_LOG_ERR("Port: %d invalid", port_ids[i]);
560 ret = rte_telemetry_update_metrics_ethdev(telemetry,
561 port_ids[i], telemetry->reg_index);
563 TELEMETRY_LOG_ERR("Failed to update ethdev metrics");
568 ret = rte_telemetry_encode_json_format(telemetry, port_ids,
569 num_port_ids, metric_ids, num_metric_ids, &json_buffer);
571 TELEMETRY_LOG_ERR("JSON encode function failed");
575 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
577 TELEMETRY_LOG_ERR("Could not write to socket");
584 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
586 TELEMETRY_LOG_ERR("Could not send error");
592 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
594 int ret, num_xstats, ret_val, i;
595 struct rte_eth_xstat *eth_xstats = NULL;
596 struct rte_eth_xstat_name *eth_xstats_names = NULL;
598 if (!rte_eth_dev_is_valid_port(port_id)) {
599 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
603 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
604 if (num_xstats < 0) {
605 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
606 port_id, num_xstats);
610 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
611 if (eth_xstats == NULL) {
612 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
616 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
617 const char *xstats_names[num_xstats];
618 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
619 if (ret < 0 || ret > num_xstats) {
620 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
621 port_id, num_xstats, ret);
626 if (eth_xstats_names == NULL) {
627 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
632 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
633 if (ret < 0 || ret > num_xstats) {
634 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
635 port_id, num_xstats, ret);
640 for (i = 0; i < num_xstats; i++)
641 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
643 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
645 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
654 free(eth_xstats_names);
659 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
665 RTE_ETH_FOREACH_DEV(pid) {
666 telemetry->reg_index = rte_telemetry_reg_ethdev_to_metrics(pid);
670 if (telemetry->reg_index < 0) {
671 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
675 telemetry->metrics_register_done = 1;
677 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index,
678 telemetry->server_fd);
682 ret = rte_telemetry_parser_test(telemetry);
684 TELEMETRY_LOG_ERR("Parser Tests Failed");
688 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
695 rte_telemetry_read_client(struct telemetry_impl *telemetry)
698 int ret, buffer_read;
700 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
702 if (buffer_read == -1) {
703 TELEMETRY_LOG_ERR("Read error");
705 } else if (buffer_read == 0) {
708 buf[buffer_read] = '\0';
709 ret = rte_telemetry_parse_client_message(telemetry, buf);
711 TELEMETRY_LOG_WARN("Parse message failed");
716 if (close(telemetry->accept_fd) < 0) {
717 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
721 telemetry->accept_fd = 0;
727 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
731 if (telemetry->accept_fd <= 0) {
732 ret = listen(telemetry->server_fd, 1);
734 TELEMETRY_LOG_ERR("Listening error with server fd");
738 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
739 if (telemetry->accept_fd >= 0 &&
740 telemetry->metrics_register_done == 0) {
741 ret = rte_telemetry_initial_accept(telemetry);
743 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
748 ret = rte_telemetry_read_client(telemetry);
750 TELEMETRY_LOG_ERR("Failed to read socket buffer");
759 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
762 telemetry_client *client;
763 char client_buf[BUF_SIZE];
766 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
767 bytes = read(client->fd, client_buf, BUF_SIZE-1);
770 client_buf[bytes] = '\0';
771 telemetry->request_client = client;
772 ret = rte_telemetry_parse(telemetry, client_buf);
774 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
785 rte_telemetry_run(void *userdata)
788 struct telemetry_impl *telemetry = userdata;
790 if (telemetry == NULL) {
791 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
795 ret = rte_telemetry_accept_new_client(telemetry);
797 TELEMETRY_LOG_ERR("Accept and read new client failed");
801 ret = rte_telemetry_read_client_sockets(telemetry);
803 TELEMETRY_LOG_ERR("Client socket read failed");
811 *rte_telemetry_run_thread_func(void *userdata)
814 struct telemetry_impl *telemetry = userdata;
816 if (telemetry == NULL) {
817 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
821 while (telemetry->thread_status) {
822 rte_telemetry_run(telemetry);
823 ret = usleep(SLEEP_TIME);
825 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
831 rte_telemetry_set_socket_nonblock(int fd)
836 TELEMETRY_LOG_ERR("Invalid fd provided");
840 flags = fcntl(fd, F_GETFL, 0);
844 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
848 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
851 struct sockaddr_un addr;
852 char socket_path[BUF_SIZE];
854 if (telemetry == NULL)
857 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
858 if (telemetry->server_fd == -1) {
859 TELEMETRY_LOG_ERR("Failed to open socket");
863 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
865 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
869 addr.sun_family = AF_UNIX;
870 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
871 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
874 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
876 TELEMETRY_LOG_ERR("Socket binding error");
883 if (close(telemetry->server_fd) < 0) {
884 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
891 int32_t __rte_experimental
896 const char *telemetry_ctrl_thread = "telemetry";
898 if (static_telemetry) {
899 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
903 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
904 if (static_telemetry == NULL) {
905 TELEMETRY_LOG_ERR("Memory could not be allocated");
909 static_telemetry->socket_id = rte_socket_id();
910 rte_metrics_init(static_telemetry->socket_id);
912 ret = pthread_attr_init(&attr);
914 TELEMETRY_LOG_ERR("Pthread attribute init failed");
918 ret = rte_telemetry_create_socket(static_telemetry);
920 ret = rte_telemetry_cleanup();
922 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
925 TAILQ_INIT(&static_telemetry->client_list_head);
927 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
928 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
929 (void *)static_telemetry);
930 static_telemetry->thread_status = 1;
933 ret = rte_telemetry_cleanup();
935 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
943 rte_telemetry_client_cleanup(struct telemetry_client *client)
947 ret = close(client->fd);
948 free(client->file_path);
952 TELEMETRY_LOG_ERR("Close client socket failed");
959 int32_t __rte_experimental
960 rte_telemetry_cleanup(void)
963 struct telemetry_impl *telemetry = static_telemetry;
964 telemetry_client *client, *temp_client;
966 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
968 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
969 ret = rte_telemetry_client_cleanup(client);
971 TELEMETRY_LOG_ERR("Client cleanup failed");
976 ret = close(telemetry->server_fd);
978 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
983 telemetry->thread_status = 0;
984 pthread_join(telemetry->thread_id, NULL);
986 static_telemetry = NULL;
992 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
993 const char *client_path)
996 telemetry_client *client, *temp_client;
998 if (telemetry == NULL) {
999 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
1003 if (client_path == NULL) {
1004 TELEMETRY_LOG_ERR("Invalid client path");
1008 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
1009 TELEMETRY_LOG_ERR("There are no clients currently registered");
1013 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1015 if (strcmp(client_path, client->file_path) == 0) {
1016 TAILQ_REMOVE(&telemetry->client_list_head, client,
1018 ret = rte_telemetry_client_cleanup(client);
1021 TELEMETRY_LOG_ERR("Client cleanup failed");
1029 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
1033 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
1035 TELEMETRY_LOG_ERR("Could not send error");
1040 rte_telemetry_register_client(struct telemetry_impl *telemetry,
1041 const char *client_path)
1044 struct sockaddr_un addrs;
1046 if (telemetry == NULL) {
1047 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
1051 if (client_path == NULL) {
1052 TELEMETRY_LOG_ERR("Invalid client path");
1056 telemetry_client *client;
1057 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
1058 if (strcmp(client_path, client->file_path) == 0) {
1059 TELEMETRY_LOG_WARN("'%s' already registered",
1065 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1067 TELEMETRY_LOG_ERR("Client socket error");
1071 ret = rte_telemetry_set_socket_nonblock(fd);
1073 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
1077 addrs.sun_family = AF_UNIX;
1078 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1079 telemetry_client *new_client = malloc(sizeof(telemetry_client));
1080 new_client->file_path = strdup(client_path);
1081 new_client->fd = fd;
1083 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
1084 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
1086 ret = rte_telemetry_client_cleanup(new_client);
1088 TELEMETRY_LOG_ERR("Client cleanup failed");
1094 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
1100 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
1102 int ret, action_int;
1104 json_t *root = json_loads(buf, 0, &error);
1107 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
1110 } else if (!json_is_object(root)) {
1111 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
1115 json_t *action = json_object_get(root, "action");
1116 if (action == NULL) {
1117 TELEMETRY_LOG_WARN("Request does not have action field");
1119 } else if (!json_is_integer(action)) {
1120 TELEMETRY_LOG_WARN("Action value is not an integer");
1124 json_t *command = json_object_get(root, "command");
1125 if (command == NULL) {
1126 TELEMETRY_LOG_WARN("Request does not have command field");
1128 } else if (!json_is_string(command)) {
1129 TELEMETRY_LOG_WARN("Command value is not a string");
1133 action_int = json_integer_value(action);
1134 if (action_int != ACTION_POST) {
1135 TELEMETRY_LOG_WARN("Invalid action code");
1139 if (strcmp(json_string_value(command), "clients") != 0) {
1140 TELEMETRY_LOG_WARN("Invalid command");
1144 json_t *data = json_object_get(root, "data");
1146 TELEMETRY_LOG_WARN("Request does not have data field");
1150 json_t *client_path = json_object_get(data, "client_path");
1151 if (client_path == NULL) {
1152 TELEMETRY_LOG_WARN("Request does not have client_path field");
1156 if (!json_is_string(client_path)) {
1157 TELEMETRY_LOG_WARN("Client_path value is not a string");
1161 ret = rte_telemetry_register_client(telemetry,
1162 json_string_value(client_path));
1164 TELEMETRY_LOG_ERR("Could not register client");
1165 telemetry->register_fail_count++;
1172 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
1178 rte_telemetry_dummy_client_socket(const char *valid_client_path)
1180 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1181 struct sockaddr_un addr = {0};
1184 TELEMETRY_LOG_ERR("Test socket creation failure");
1188 addr.sun_family = AF_UNIX;
1189 strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
1190 unlink(valid_client_path);
1192 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1193 TELEMETRY_LOG_ERR("Test socket binding failure");
1197 if (listen(sockfd, 1) < 0) {
1198 TELEMETRY_LOG_ERR("Listen failure");
1205 int32_t __rte_experimental
1206 rte_telemetry_selftest(void)
1208 const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
1209 const char *valid_client_path = SELFTEST_VALID_CLIENT;
1212 TELEMETRY_LOG_INFO("Selftest");
1214 ret = rte_telemetry_init();
1216 TELEMETRY_LOG_ERR("Valid initialisation test failed");
1220 TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
1222 ret = rte_telemetry_init();
1223 if (ret != -EALREADY) {
1224 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
1228 TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
1230 ret = rte_telemetry_unregister_client(static_telemetry,
1231 invalid_client_path);
1232 if (ret != -EPERM) {
1233 TELEMETRY_LOG_ERR("Invalid unregister test failed");
1237 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1239 sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
1241 TELEMETRY_LOG_ERR("Test socket creation failed");
1245 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1247 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
1251 accept(sockfd, NULL, NULL);
1252 TELEMETRY_LOG_INFO("Success - Valid register test passed");
1254 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1255 if (ret != -EINVAL) {
1256 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
1260 TELEMETRY_LOG_INFO("Success - Invalid register test passed");
1262 ret = rte_telemetry_unregister_client(static_telemetry,
1263 invalid_client_path);
1265 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
1269 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1271 ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
1273 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
1277 TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
1279 ret = rte_telemetry_cleanup();
1281 TELEMETRY_LOG_ERR("Cleanup test failed");
1285 TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
1291 rte_telemetry_socket_messaging_testing(int index, int socket)
1293 struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
1294 int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
1296 if (telemetry == NULL) {
1297 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
1301 telemetry->server_fd = socket;
1302 telemetry->reg_index = index;
1303 TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
1304 rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
1305 TELEMETRY_LOG_INFO("Register valid client test");
1307 ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
1310 TELEMETRY_LOG_ERR("Register valid client test failed!");
1315 TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
1317 TELEMETRY_LOG_INFO("Register invalid/same client test");
1318 ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
1320 ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
1321 bad_send_fd, bad_recv_fd);
1323 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
1328 TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
1330 ret = rte_telemetry_json_socket_message_test(telemetry, fd);
1341 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
1342 int send_fd, int recv_fd)
1345 char good_req_string[BUF_SIZE];
1347 snprintf(good_req_string, sizeof(good_req_string),
1348 "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
1349 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
1353 ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
1355 TELEMETRY_LOG_ERR("Could not send message over socket");
1359 rte_telemetry_run(telemetry);
1361 if (telemetry->register_fail_count != 0)
1364 *fd = accept(recv_fd, NULL, NULL);
1370 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
1374 const char *client_path = SOCKET_TEST_CLIENT_PATH;
1375 char socket_path[BUF_SIZE];
1376 struct sockaddr_un addr = {0};
1377 struct sockaddr_un addrs = {0};
1378 *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1379 *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1381 listen(telemetry->server_fd, 5);
1382 addr.sun_family = AF_UNIX;
1383 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
1384 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
1386 ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
1388 TELEMETRY_LOG_ERR("Could not connect socket");
1392 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
1394 addrs.sun_family = AF_UNIX;
1395 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1396 unlink(client_path);
1398 ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
1400 TELEMETRY_LOG_ERR("Could not bind socket");
1408 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
1411 json_t *root = json_loads(buf, 0, &error);
1413 json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
1421 TELEMETRY_LOG_ERR("JSON message is NULL");
1426 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
1429 } else if (!json_is_object(root)) {
1430 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1435 status = json_object_get(root, "status_code");
1437 TELEMETRY_LOG_ERR("Request does not have status field");
1439 } else if (!json_is_string(status)) {
1440 TELEMETRY_LOG_ERR("Status value is not a string");
1444 json_data_struct->status_code = strdup(json_string_value(status));
1446 dataArray = json_object_get(root, "data");
1447 if (dataArray == NULL) {
1448 TELEMETRY_LOG_ERR("Request does not have data field");
1452 arraylen = json_array_size(dataArray);
1453 if (arraylen == 0) {
1454 json_data_struct->data = "null";
1458 for (i = 0; i < arraylen; i++) {
1459 dataArrayObj = json_array_get(dataArray, i);
1460 port = json_object_get(dataArrayObj, "port");
1461 stats = json_object_get(dataArrayObj, "stats");
1465 TELEMETRY_LOG_ERR("Request does not have port field");
1469 if (!json_is_integer(port)) {
1470 TELEMETRY_LOG_ERR("Port value is not an integer");
1474 json_data_struct->port = json_integer_value(port);
1476 if (stats == NULL) {
1477 TELEMETRY_LOG_ERR("Request does not have stats field");
1481 arraylen = json_array_size(stats);
1482 for (i = 0; i < arraylen; i++) {
1483 statsArrayObj = json_array_get(stats, i);
1484 name = json_object_get(statsArrayObj, "name");
1485 value = json_object_get(statsArrayObj, "value");
1489 TELEMETRY_LOG_ERR("Request does not have name field");
1493 if (!json_is_string(name)) {
1494 TELEMETRY_LOG_ERR("Stat name value is not a string");
1498 json_data_struct->stat_name = strdup(json_string_value(name));
1500 if (value == NULL) {
1501 TELEMETRY_LOG_ERR("Request does not have value field");
1505 if (!json_is_integer(value)) {
1506 TELEMETRY_LOG_ERR("Stat value is not an integer");
1510 json_data_struct->stat_value = json_integer_value(value);
1516 rte_telemetry_free_test_data(struct json_data *data)
1518 free(data->status_code);
1519 free(data->stat_name);
1524 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1530 int buffer_read = 0;
1532 struct json_data *data_struct;
1534 const char *status = "Status OK: 200";
1535 const char *name = "rx_good_packets";
1536 const char *valid_json_message = "{\"action\":0,\"command\":"
1537 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1538 ":[0],\"stats\":[\"rx_good_packets\"]}}";
1540 ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1542 TELEMETRY_LOG_ERR("Could not send message over socket");
1546 rte_telemetry_run(telemetry);
1547 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1549 if (buffer_read == -1) {
1550 TELEMETRY_LOG_ERR("Read error");
1554 buf[buffer_read] = '\0';
1555 data_struct = calloc(1, sizeof(struct json_data));
1556 ret = rte_telemetry_stat_parse(buf, data_struct);
1559 TELEMETRY_LOG_ERR("Could not parse stats");
1563 if (strcmp(data_struct->status_code, status) != 0) {
1564 TELEMETRY_LOG_ERR("Status code is invalid");
1568 if (data_struct->port != port) {
1569 TELEMETRY_LOG_ERR("Port is invalid");
1573 if (strcmp(data_struct->stat_name, name) != 0) {
1574 TELEMETRY_LOG_ERR("Stat name is invalid");
1578 if (data_struct->stat_value != value) {
1579 TELEMETRY_LOG_ERR("Stat value is invalid");
1583 rte_telemetry_free_test_data(data_struct);
1587 TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1593 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1598 const char *invalid_json = "{]";
1599 const char *status = "Status Error: Unknown";
1600 const char *data = "null";
1601 struct json_data *data_struct;
1602 int buffer_read = 0;
1605 ret = send(fd, invalid_json, strlen(invalid_json), 0);
1607 TELEMETRY_LOG_ERR("Could not send message over socket");
1611 rte_telemetry_run(telemetry);
1612 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1614 if (buffer_read == -1) {
1615 TELEMETRY_LOG_ERR("Read error");
1619 buf[buffer_read] = '\0';
1621 data_struct = calloc(1, sizeof(struct json_data));
1622 ret = rte_telemetry_stat_parse(buf, data_struct);
1625 TELEMETRY_LOG_ERR("Could not parse stats");
1627 if (strcmp(data_struct->status_code, status) != 0) {
1628 TELEMETRY_LOG_ERR("Status code is invalid");
1632 if (strcmp(data_struct->data, data) != 0) {
1633 TELEMETRY_LOG_ERR("Data status is invalid");
1637 rte_telemetry_free_test_data(data_struct);
1641 TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1647 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1652 char *status = "Status Error: Invalid Argument 404";
1653 char *data = "null";
1654 struct json_data *data_struct;
1655 const char *invalid_contents = "{\"action\":0,\"command\":"
1656 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1657 ":[0],\"stats\":[\"some_invalid_param\","
1658 "\"another_invalid_param\"]}}";
1659 int buffer_read = 0;
1662 ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1664 TELEMETRY_LOG_ERR("Could not send message over socket");
1668 rte_telemetry_run(telemetry);
1669 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1671 if (buffer_read == -1) {
1672 TELEMETRY_LOG_ERR("Read error");
1676 buf[buffer_read] = '\0';
1677 data_struct = calloc(1, sizeof(struct json_data));
1678 ret = rte_telemetry_stat_parse(buf, data_struct);
1681 TELEMETRY_LOG_ERR("Could not parse stats");
1683 if (strcmp(data_struct->status_code, status) != 0) {
1684 TELEMETRY_LOG_ERR("Status code is invalid");
1688 if (strcmp(data_struct->data, data) != 0) {
1689 TELEMETRY_LOG_ERR("Data status is invalid");
1693 rte_telemetry_free_test_data(data_struct);
1697 TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1703 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1708 const char *status = "Status Error: Invalid Argument 404";
1709 char *data = "null";
1710 struct json_data *data_struct;
1711 const char *empty_json = "{}";
1712 int buffer_read = 0;
1715 ret = (send(fd, empty_json, strlen(empty_json), 0));
1717 TELEMETRY_LOG_ERR("Could not send message over socket");
1721 rte_telemetry_run(telemetry);
1722 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1724 if (buffer_read == -1) {
1725 TELEMETRY_LOG_ERR("Read error");
1729 buf[buffer_read] = '\0';
1730 data_struct = calloc(1, sizeof(struct json_data));
1731 ret = rte_telemetry_stat_parse(buf, data_struct);
1734 TELEMETRY_LOG_ERR("Could not parse stats");
1736 if (strcmp(data_struct->status_code, status) != 0) {
1737 TELEMETRY_LOG_ERR("Status code is invalid");
1741 if (strcmp(data_struct->data, data) != 0) {
1742 TELEMETRY_LOG_ERR("Data status is invalid");
1746 rte_telemetry_free_test_data(data_struct);
1751 TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1757 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1760 int ret, fail_count;
1763 struct telemetry_message_test socket_json_tests[] = {
1764 {.test_name = "Invalid JSON test",
1765 .test_func_ptr = rte_telemetry_invalid_json_test},
1766 {.test_name = "Valid JSON test",
1767 .test_func_ptr = rte_telemetry_valid_json_test},
1768 {.test_name = "JSON contents test",
1769 .test_func_ptr = rte_telemetry_json_contents_test},
1770 {.test_name = "JSON empty tests",
1771 .test_func_ptr = rte_telemetry_json_empty_test}
1774 #define NUM_TESTS RTE_DIM(socket_json_tests)
1776 for (i = 0; i < NUM_TESTS; i++) {
1777 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1778 ret = (socket_json_tests[i].test_func_ptr)
1781 TELEMETRY_LOG_ERR("%s failed",
1782 socket_json_tests[i].test_name);
1787 if (fail_count > 0) {
1788 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1793 TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1798 int telemetry_log_level;
1800 static struct rte_option option = {
1801 .opt_str = "--telemetry",
1802 .cb = &rte_telemetry_init,
1806 RTE_INIT(rte_telemetry_register)
1808 telemetry_log_level = rte_log_register("lib.telemetry");
1809 if (telemetry_log_level >= 0)
1810 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1812 rte_option_register(&option);