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[i]);
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)
661 struct driver_index {
664 } drv_idx[RTE_MAX_ETHPORTS];
670 RTE_ETH_FOREACH_DEV(pid) {
672 /* Different device types have different numbers of stats, so
673 * first check if the stats for this type of device have
674 * already been registered
676 for (i = 0; i < nb_drv_idx; i++) {
677 if (rte_eth_devices[pid].dev_ops == drv_idx[i].dev_ops) {
678 telemetry->reg_index[pid] = drv_idx[i].reg_index;
683 continue; /* we found a match, go to next port */
685 /* No match, register a new set of xstats for this port */
686 ret = rte_telemetry_reg_ethdev_to_metrics(pid);
688 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
691 telemetry->reg_index[pid] = ret;
692 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
693 drv_idx[nb_drv_idx].reg_index = ret;
697 telemetry->metrics_register_done = 1;
699 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
700 telemetry->server_fd);
704 ret = rte_telemetry_parser_test(telemetry);
706 TELEMETRY_LOG_ERR("Parser Tests Failed");
710 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
717 rte_telemetry_read_client(struct telemetry_impl *telemetry)
720 int ret, buffer_read;
722 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
724 if (buffer_read == -1) {
725 TELEMETRY_LOG_ERR("Read error");
727 } else if (buffer_read == 0) {
730 buf[buffer_read] = '\0';
731 ret = rte_telemetry_parse_client_message(telemetry, buf);
733 TELEMETRY_LOG_WARN("Parse message failed");
738 if (close(telemetry->accept_fd) < 0) {
739 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
743 telemetry->accept_fd = 0;
749 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
753 if (telemetry->accept_fd <= 0) {
754 ret = listen(telemetry->server_fd, 1);
756 TELEMETRY_LOG_ERR("Listening error with server fd");
760 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
761 if (telemetry->accept_fd >= 0 &&
762 telemetry->metrics_register_done == 0) {
763 ret = rte_telemetry_initial_accept(telemetry);
765 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
770 ret = rte_telemetry_read_client(telemetry);
772 TELEMETRY_LOG_ERR("Failed to read socket buffer");
781 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
784 telemetry_client *client;
785 char client_buf[BUF_SIZE];
788 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
789 bytes = read(client->fd, client_buf, BUF_SIZE-1);
792 client_buf[bytes] = '\0';
793 telemetry->request_client = client;
794 ret = rte_telemetry_parse(telemetry, client_buf);
796 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
807 rte_telemetry_run(void *userdata)
810 struct telemetry_impl *telemetry = userdata;
812 if (telemetry == NULL) {
813 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
817 ret = rte_telemetry_accept_new_client(telemetry);
819 TELEMETRY_LOG_ERR("Accept and read new client failed");
823 ret = rte_telemetry_read_client_sockets(telemetry);
825 TELEMETRY_LOG_ERR("Client socket read failed");
833 *rte_telemetry_run_thread_func(void *userdata)
836 struct telemetry_impl *telemetry = userdata;
838 if (telemetry == NULL) {
839 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
843 while (telemetry->thread_status) {
844 rte_telemetry_run(telemetry);
845 ret = usleep(SLEEP_TIME);
847 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
853 rte_telemetry_set_socket_nonblock(int fd)
858 TELEMETRY_LOG_ERR("Invalid fd provided");
862 flags = fcntl(fd, F_GETFL, 0);
866 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
870 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
873 struct sockaddr_un addr;
874 char socket_path[BUF_SIZE];
876 if (telemetry == NULL)
879 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
880 if (telemetry->server_fd == -1) {
881 TELEMETRY_LOG_ERR("Failed to open socket");
885 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
887 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
891 addr.sun_family = AF_UNIX;
892 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
893 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
896 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
898 TELEMETRY_LOG_ERR("Socket binding error");
905 if (close(telemetry->server_fd) < 0) {
906 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
913 int32_t __rte_experimental
918 const char *telemetry_ctrl_thread = "telemetry";
920 if (static_telemetry) {
921 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
925 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
926 if (static_telemetry == NULL) {
927 TELEMETRY_LOG_ERR("Memory could not be allocated");
931 static_telemetry->socket_id = rte_socket_id();
932 rte_metrics_init(static_telemetry->socket_id);
934 ret = pthread_attr_init(&attr);
936 TELEMETRY_LOG_ERR("Pthread attribute init failed");
940 ret = rte_telemetry_create_socket(static_telemetry);
942 ret = rte_telemetry_cleanup();
944 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
947 TAILQ_INIT(&static_telemetry->client_list_head);
949 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
950 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
951 (void *)static_telemetry);
952 static_telemetry->thread_status = 1;
955 ret = rte_telemetry_cleanup();
957 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
965 rte_telemetry_client_cleanup(struct telemetry_client *client)
969 ret = close(client->fd);
970 free(client->file_path);
974 TELEMETRY_LOG_ERR("Close client socket failed");
981 int32_t __rte_experimental
982 rte_telemetry_cleanup(void)
985 struct telemetry_impl *telemetry = static_telemetry;
986 telemetry_client *client, *temp_client;
988 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
990 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
991 ret = rte_telemetry_client_cleanup(client);
993 TELEMETRY_LOG_ERR("Client cleanup failed");
998 ret = close(telemetry->server_fd);
1000 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
1005 telemetry->thread_status = 0;
1006 pthread_join(telemetry->thread_id, NULL);
1008 static_telemetry = NULL;
1014 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
1015 const char *client_path)
1018 telemetry_client *client, *temp_client;
1020 if (telemetry == NULL) {
1021 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
1025 if (client_path == NULL) {
1026 TELEMETRY_LOG_ERR("Invalid client path");
1030 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
1031 TELEMETRY_LOG_ERR("There are no clients currently registered");
1035 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1037 if (strcmp(client_path, client->file_path) == 0) {
1038 TAILQ_REMOVE(&telemetry->client_list_head, client,
1040 ret = rte_telemetry_client_cleanup(client);
1043 TELEMETRY_LOG_ERR("Client cleanup failed");
1051 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
1055 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
1057 TELEMETRY_LOG_ERR("Could not send error");
1062 rte_telemetry_register_client(struct telemetry_impl *telemetry,
1063 const char *client_path)
1066 struct sockaddr_un addrs;
1068 if (telemetry == NULL) {
1069 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
1073 if (client_path == NULL) {
1074 TELEMETRY_LOG_ERR("Invalid client path");
1078 telemetry_client *client;
1079 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
1080 if (strcmp(client_path, client->file_path) == 0) {
1081 TELEMETRY_LOG_WARN("'%s' already registered",
1087 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1089 TELEMETRY_LOG_ERR("Client socket error");
1093 ret = rte_telemetry_set_socket_nonblock(fd);
1095 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
1099 addrs.sun_family = AF_UNIX;
1100 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1101 telemetry_client *new_client = malloc(sizeof(telemetry_client));
1102 new_client->file_path = strdup(client_path);
1103 new_client->fd = fd;
1105 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
1106 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
1108 ret = rte_telemetry_client_cleanup(new_client);
1110 TELEMETRY_LOG_ERR("Client cleanup failed");
1116 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
1122 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
1124 int ret, action_int;
1126 json_t *root = json_loads(buf, 0, &error);
1129 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
1132 } else if (!json_is_object(root)) {
1133 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
1137 json_t *action = json_object_get(root, "action");
1138 if (action == NULL) {
1139 TELEMETRY_LOG_WARN("Request does not have action field");
1141 } else if (!json_is_integer(action)) {
1142 TELEMETRY_LOG_WARN("Action value is not an integer");
1146 json_t *command = json_object_get(root, "command");
1147 if (command == NULL) {
1148 TELEMETRY_LOG_WARN("Request does not have command field");
1150 } else if (!json_is_string(command)) {
1151 TELEMETRY_LOG_WARN("Command value is not a string");
1155 action_int = json_integer_value(action);
1156 if (action_int != ACTION_POST) {
1157 TELEMETRY_LOG_WARN("Invalid action code");
1161 if (strcmp(json_string_value(command), "clients") != 0) {
1162 TELEMETRY_LOG_WARN("Invalid command");
1166 json_t *data = json_object_get(root, "data");
1168 TELEMETRY_LOG_WARN("Request does not have data field");
1172 json_t *client_path = json_object_get(data, "client_path");
1173 if (client_path == NULL) {
1174 TELEMETRY_LOG_WARN("Request does not have client_path field");
1178 if (!json_is_string(client_path)) {
1179 TELEMETRY_LOG_WARN("Client_path value is not a string");
1183 ret = rte_telemetry_register_client(telemetry,
1184 json_string_value(client_path));
1186 TELEMETRY_LOG_ERR("Could not register client");
1187 telemetry->register_fail_count++;
1194 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
1200 rte_telemetry_dummy_client_socket(const char *valid_client_path)
1202 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1203 struct sockaddr_un addr = {0};
1206 TELEMETRY_LOG_ERR("Test socket creation failure");
1210 addr.sun_family = AF_UNIX;
1211 strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
1212 unlink(valid_client_path);
1214 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1215 TELEMETRY_LOG_ERR("Test socket binding failure");
1219 if (listen(sockfd, 1) < 0) {
1220 TELEMETRY_LOG_ERR("Listen failure");
1227 int32_t __rte_experimental
1228 rte_telemetry_selftest(void)
1230 const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
1231 const char *valid_client_path = SELFTEST_VALID_CLIENT;
1234 TELEMETRY_LOG_INFO("Selftest");
1236 ret = rte_telemetry_init();
1238 TELEMETRY_LOG_ERR("Valid initialisation test failed");
1242 TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
1244 ret = rte_telemetry_init();
1245 if (ret != -EALREADY) {
1246 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
1250 TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
1252 ret = rte_telemetry_unregister_client(static_telemetry,
1253 invalid_client_path);
1254 if (ret != -EPERM) {
1255 TELEMETRY_LOG_ERR("Invalid unregister test failed");
1259 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1261 sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
1263 TELEMETRY_LOG_ERR("Test socket creation failed");
1267 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1269 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
1273 accept(sockfd, NULL, NULL);
1274 TELEMETRY_LOG_INFO("Success - Valid register test passed");
1276 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1277 if (ret != -EINVAL) {
1278 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
1282 TELEMETRY_LOG_INFO("Success - Invalid register test passed");
1284 ret = rte_telemetry_unregister_client(static_telemetry,
1285 invalid_client_path);
1287 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
1291 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1293 ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
1295 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
1299 TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
1301 ret = rte_telemetry_cleanup();
1303 TELEMETRY_LOG_ERR("Cleanup test failed");
1307 TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
1313 rte_telemetry_socket_messaging_testing(int index, int socket)
1315 struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
1316 int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
1318 if (telemetry == NULL) {
1319 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
1323 telemetry->server_fd = socket;
1324 telemetry->reg_index[0] = index;
1325 TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
1326 rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
1327 TELEMETRY_LOG_INFO("Register valid client test");
1329 ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
1332 TELEMETRY_LOG_ERR("Register valid client test failed!");
1337 TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
1339 TELEMETRY_LOG_INFO("Register invalid/same client test");
1340 ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
1342 ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
1343 bad_send_fd, bad_recv_fd);
1345 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
1350 TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
1352 ret = rte_telemetry_json_socket_message_test(telemetry, fd);
1363 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
1364 int send_fd, int recv_fd)
1367 char good_req_string[BUF_SIZE];
1369 snprintf(good_req_string, sizeof(good_req_string),
1370 "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
1371 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
1375 ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
1377 TELEMETRY_LOG_ERR("Could not send message over socket");
1381 rte_telemetry_run(telemetry);
1383 if (telemetry->register_fail_count != 0)
1386 *fd = accept(recv_fd, NULL, NULL);
1392 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
1396 const char *client_path = SOCKET_TEST_CLIENT_PATH;
1397 char socket_path[BUF_SIZE];
1398 struct sockaddr_un addr = {0};
1399 struct sockaddr_un addrs = {0};
1400 *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1401 *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1403 listen(telemetry->server_fd, 5);
1404 addr.sun_family = AF_UNIX;
1405 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
1406 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
1408 ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
1410 TELEMETRY_LOG_ERR("Could not connect socket");
1414 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
1416 addrs.sun_family = AF_UNIX;
1417 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1418 unlink(client_path);
1420 ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
1422 TELEMETRY_LOG_ERR("Could not bind socket");
1430 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
1433 json_t *root = json_loads(buf, 0, &error);
1435 json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
1443 TELEMETRY_LOG_ERR("JSON message is NULL");
1448 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
1451 } else if (!json_is_object(root)) {
1452 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1457 status = json_object_get(root, "status_code");
1459 TELEMETRY_LOG_ERR("Request does not have status field");
1461 } else if (!json_is_string(status)) {
1462 TELEMETRY_LOG_ERR("Status value is not a string");
1466 json_data_struct->status_code = strdup(json_string_value(status));
1468 dataArray = json_object_get(root, "data");
1469 if (dataArray == NULL) {
1470 TELEMETRY_LOG_ERR("Request does not have data field");
1474 arraylen = json_array_size(dataArray);
1475 if (arraylen == 0) {
1476 json_data_struct->data = "null";
1480 for (i = 0; i < arraylen; i++) {
1481 dataArrayObj = json_array_get(dataArray, i);
1482 port = json_object_get(dataArrayObj, "port");
1483 stats = json_object_get(dataArrayObj, "stats");
1487 TELEMETRY_LOG_ERR("Request does not have port field");
1491 if (!json_is_integer(port)) {
1492 TELEMETRY_LOG_ERR("Port value is not an integer");
1496 json_data_struct->port = json_integer_value(port);
1498 if (stats == NULL) {
1499 TELEMETRY_LOG_ERR("Request does not have stats field");
1503 arraylen = json_array_size(stats);
1504 for (i = 0; i < arraylen; i++) {
1505 statsArrayObj = json_array_get(stats, i);
1506 name = json_object_get(statsArrayObj, "name");
1507 value = json_object_get(statsArrayObj, "value");
1511 TELEMETRY_LOG_ERR("Request does not have name field");
1515 if (!json_is_string(name)) {
1516 TELEMETRY_LOG_ERR("Stat name value is not a string");
1520 json_data_struct->stat_name = strdup(json_string_value(name));
1522 if (value == NULL) {
1523 TELEMETRY_LOG_ERR("Request does not have value field");
1527 if (!json_is_integer(value)) {
1528 TELEMETRY_LOG_ERR("Stat value is not an integer");
1532 json_data_struct->stat_value = json_integer_value(value);
1538 rte_telemetry_free_test_data(struct json_data *data)
1540 free(data->status_code);
1541 free(data->stat_name);
1546 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1552 int buffer_read = 0;
1554 struct json_data *data_struct;
1556 const char *status = "Status OK: 200";
1557 const char *name = "rx_good_packets";
1558 const char *valid_json_message = "{\"action\":0,\"command\":"
1559 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1560 ":[0],\"stats\":[\"rx_good_packets\"]}}";
1562 ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1564 TELEMETRY_LOG_ERR("Could not send message over socket");
1568 rte_telemetry_run(telemetry);
1569 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1571 if (buffer_read == -1) {
1572 TELEMETRY_LOG_ERR("Read error");
1576 buf[buffer_read] = '\0';
1577 data_struct = calloc(1, sizeof(struct json_data));
1578 ret = rte_telemetry_stat_parse(buf, data_struct);
1581 TELEMETRY_LOG_ERR("Could not parse stats");
1585 if (strcmp(data_struct->status_code, status) != 0) {
1586 TELEMETRY_LOG_ERR("Status code is invalid");
1590 if (data_struct->port != port) {
1591 TELEMETRY_LOG_ERR("Port is invalid");
1595 if (strcmp(data_struct->stat_name, name) != 0) {
1596 TELEMETRY_LOG_ERR("Stat name is invalid");
1600 if (data_struct->stat_value != value) {
1601 TELEMETRY_LOG_ERR("Stat value is invalid");
1605 rte_telemetry_free_test_data(data_struct);
1609 TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1615 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1620 const char *invalid_json = "{]";
1621 const char *status = "Status Error: Unknown";
1622 const char *data = "null";
1623 struct json_data *data_struct;
1624 int buffer_read = 0;
1627 ret = send(fd, invalid_json, strlen(invalid_json), 0);
1629 TELEMETRY_LOG_ERR("Could not send message over socket");
1633 rte_telemetry_run(telemetry);
1634 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1636 if (buffer_read == -1) {
1637 TELEMETRY_LOG_ERR("Read error");
1641 buf[buffer_read] = '\0';
1643 data_struct = calloc(1, sizeof(struct json_data));
1644 ret = rte_telemetry_stat_parse(buf, data_struct);
1647 TELEMETRY_LOG_ERR("Could not parse stats");
1649 if (strcmp(data_struct->status_code, status) != 0) {
1650 TELEMETRY_LOG_ERR("Status code is invalid");
1654 if (strcmp(data_struct->data, data) != 0) {
1655 TELEMETRY_LOG_ERR("Data status is invalid");
1659 rte_telemetry_free_test_data(data_struct);
1663 TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1669 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1674 char *status = "Status Error: Invalid Argument 404";
1675 char *data = "null";
1676 struct json_data *data_struct;
1677 const char *invalid_contents = "{\"action\":0,\"command\":"
1678 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1679 ":[0],\"stats\":[\"some_invalid_param\","
1680 "\"another_invalid_param\"]}}";
1681 int buffer_read = 0;
1684 ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1686 TELEMETRY_LOG_ERR("Could not send message over socket");
1690 rte_telemetry_run(telemetry);
1691 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1693 if (buffer_read == -1) {
1694 TELEMETRY_LOG_ERR("Read error");
1698 buf[buffer_read] = '\0';
1699 data_struct = calloc(1, sizeof(struct json_data));
1700 ret = rte_telemetry_stat_parse(buf, data_struct);
1703 TELEMETRY_LOG_ERR("Could not parse stats");
1705 if (strcmp(data_struct->status_code, status) != 0) {
1706 TELEMETRY_LOG_ERR("Status code is invalid");
1710 if (strcmp(data_struct->data, data) != 0) {
1711 TELEMETRY_LOG_ERR("Data status is invalid");
1715 rte_telemetry_free_test_data(data_struct);
1719 TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1725 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1730 const char *status = "Status Error: Invalid Argument 404";
1731 char *data = "null";
1732 struct json_data *data_struct;
1733 const char *empty_json = "{}";
1734 int buffer_read = 0;
1737 ret = (send(fd, empty_json, strlen(empty_json), 0));
1739 TELEMETRY_LOG_ERR("Could not send message over socket");
1743 rte_telemetry_run(telemetry);
1744 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1746 if (buffer_read == -1) {
1747 TELEMETRY_LOG_ERR("Read error");
1751 buf[buffer_read] = '\0';
1752 data_struct = calloc(1, sizeof(struct json_data));
1753 ret = rte_telemetry_stat_parse(buf, data_struct);
1756 TELEMETRY_LOG_ERR("Could not parse stats");
1758 if (strcmp(data_struct->status_code, status) != 0) {
1759 TELEMETRY_LOG_ERR("Status code is invalid");
1763 if (strcmp(data_struct->data, data) != 0) {
1764 TELEMETRY_LOG_ERR("Data status is invalid");
1768 rte_telemetry_free_test_data(data_struct);
1773 TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1779 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1782 int ret, fail_count;
1785 struct telemetry_message_test socket_json_tests[] = {
1786 {.test_name = "Invalid JSON test",
1787 .test_func_ptr = rte_telemetry_invalid_json_test},
1788 {.test_name = "Valid JSON test",
1789 .test_func_ptr = rte_telemetry_valid_json_test},
1790 {.test_name = "JSON contents test",
1791 .test_func_ptr = rte_telemetry_json_contents_test},
1792 {.test_name = "JSON empty tests",
1793 .test_func_ptr = rte_telemetry_json_empty_test}
1796 #define NUM_TESTS RTE_DIM(socket_json_tests)
1798 for (i = 0; i < NUM_TESTS; i++) {
1799 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1800 ret = (socket_json_tests[i].test_func_ptr)
1803 TELEMETRY_LOG_ERR("%s failed",
1804 socket_json_tests[i].test_name);
1809 if (fail_count > 0) {
1810 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1815 TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1820 int telemetry_log_level;
1822 static struct rte_option option = {
1823 .opt_str = "--telemetry",
1824 .cb = &rte_telemetry_init,
1828 RTE_INIT(rte_telemetry_register)
1830 telemetry_log_level = rte_log_register("lib.telemetry");
1831 if (telemetry_log_level >= 0)
1832 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1834 rte_option_register(&option);