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 struct telemetry_encode_param *ep, char **json_buffer)
455 json_t *root, *ports;
458 uint32_t num_port_ids;
459 uint32_t num_metric_ids;
461 ports = json_array();
463 TELEMETRY_LOG_ERR("Could not create ports JSON array");
467 if (ep->type == PORT_STATS) {
468 num_port_ids = ep->pp.num_port_ids;
469 num_metric_ids = ep->pp.num_metric_ids;
471 if (num_port_ids <= 0 || num_metric_ids <= 0) {
472 TELEMETRY_LOG_ERR("Please provide port and metric ids to query");
476 for (i = 0; i < num_port_ids; i++) {
477 port_id = ep->pp.port_ids[i];
478 if (!rte_eth_dev_is_valid_port(port_id)) {
479 TELEMETRY_LOG_ERR("Port: %d invalid",
485 for (i = 0; i < num_port_ids; i++) {
486 port_id = ep->pp.port_ids[i];
487 ret = rte_telemetry_json_format_port(telemetry,
488 port_id, ports, &ep->pp.metric_ids[0],
491 TELEMETRY_LOG_ERR("Format port in JSON failed");
495 } else if (ep->type == GLOBAL_STATS) {
496 /* Request Global Metrics */
497 ret = rte_telemetry_json_format_port(telemetry,
499 ports, &ep->gp.metric_ids[0],
500 ep->gp.num_metric_ids);
502 TELEMETRY_LOG_ERR(" Request Global Metrics Failed");
506 TELEMETRY_LOG_ERR(" Invalid metrics type in encode params");
510 root = json_object();
512 TELEMETRY_LOG_ERR("Could not create root JSON object");
516 ret = json_object_set_new(root, "status_code",
517 json_string("Status OK: 200"));
519 TELEMETRY_LOG_ERR("Status code field cannot be set");
523 ret = json_object_set_new(root, "data", ports);
525 TELEMETRY_LOG_ERR("Data field cannot be set");
529 *json_buffer = json_dumps(root, JSON_INDENT(2));
534 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
536 TELEMETRY_LOG_ERR("Could not send error");
540 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
542 TELEMETRY_LOG_ERR("Could not send error");
547 rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
548 struct telemetry_impl *telemetry)
551 char *json_buffer = NULL;
553 if (telemetry == NULL) {
554 TELEMETRY_LOG_ERR("Invalid telemetry argument");
558 if (ep->gp.num_metric_ids < 0) {
559 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
563 ret = rte_telemetry_encode_json_format(telemetry, ep,
566 TELEMETRY_LOG_ERR("JSON encode function failed");
570 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
572 TELEMETRY_LOG_ERR("Could not write to socket");
579 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
581 TELEMETRY_LOG_ERR("Could not send error");
586 rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
587 struct telemetry_impl *telemetry)
590 char *json_buffer = NULL;
594 if (telemetry == NULL) {
595 TELEMETRY_LOG_ERR("Invalid telemetry argument");
600 TELEMETRY_LOG_ERR("Invalid encode param argument");
604 if (ep->pp.num_metric_ids < 0) {
605 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
609 if (ep->pp.num_port_ids < 0) {
610 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
614 for (i = 0; i < ep->pp.num_port_ids; i++) {
615 port_id = ep->pp.port_ids[i];
616 if (!rte_eth_dev_is_valid_port(port_id)) {
617 TELEMETRY_LOG_ERR("Port: %d invalid", port_id);
621 ret = rte_telemetry_update_metrics_ethdev(telemetry,
622 port_id, telemetry->reg_index[i]);
624 TELEMETRY_LOG_ERR("Failed to update ethdev metrics");
629 ret = rte_telemetry_encode_json_format(telemetry, ep, &json_buffer);
631 TELEMETRY_LOG_ERR("JSON encode function failed");
635 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
637 TELEMETRY_LOG_ERR("Could not write to socket");
644 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
646 TELEMETRY_LOG_ERR("Could not send error");
652 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
654 int ret, num_xstats, ret_val, i;
655 struct rte_eth_xstat *eth_xstats = NULL;
656 struct rte_eth_xstat_name *eth_xstats_names = NULL;
658 if (!rte_eth_dev_is_valid_port(port_id)) {
659 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
663 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
664 if (num_xstats < 0) {
665 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
666 port_id, num_xstats);
670 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
671 if (eth_xstats == NULL) {
672 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
676 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
677 const char *xstats_names[num_xstats];
678 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
679 if (ret < 0 || ret > num_xstats) {
680 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
681 port_id, num_xstats, ret);
686 if (eth_xstats_names == NULL) {
687 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
692 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
693 if (ret < 0 || ret > num_xstats) {
694 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
695 port_id, num_xstats, ret);
700 for (i = 0; i < num_xstats; i++)
701 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
703 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
705 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
714 free(eth_xstats_names);
719 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
721 struct driver_index {
724 } drv_idx[RTE_MAX_ETHPORTS];
730 RTE_ETH_FOREACH_DEV(pid) {
732 /* Different device types have different numbers of stats, so
733 * first check if the stats for this type of device have
734 * already been registered
736 for (i = 0; i < nb_drv_idx; i++) {
737 if (rte_eth_devices[pid].dev_ops == drv_idx[i].dev_ops) {
738 telemetry->reg_index[pid] = drv_idx[i].reg_index;
743 continue; /* we found a match, go to next port */
745 /* No match, register a new set of xstats for this port */
746 ret = rte_telemetry_reg_ethdev_to_metrics(pid);
748 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
751 telemetry->reg_index[pid] = ret;
752 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
753 drv_idx[nb_drv_idx].reg_index = ret;
757 telemetry->metrics_register_done = 1;
759 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
760 telemetry->server_fd);
764 ret = rte_telemetry_parser_test(telemetry);
766 TELEMETRY_LOG_ERR("Parser Tests Failed");
770 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
777 rte_telemetry_read_client(struct telemetry_impl *telemetry)
780 int ret, buffer_read;
782 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
784 if (buffer_read == -1) {
785 TELEMETRY_LOG_ERR("Read error");
787 } else if (buffer_read == 0) {
790 buf[buffer_read] = '\0';
791 ret = rte_telemetry_parse_client_message(telemetry, buf);
793 TELEMETRY_LOG_WARN("Parse message failed");
798 if (close(telemetry->accept_fd) < 0) {
799 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
803 telemetry->accept_fd = 0;
809 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
813 if (telemetry->accept_fd <= 0) {
814 ret = listen(telemetry->server_fd, 1);
816 TELEMETRY_LOG_ERR("Listening error with server fd");
820 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
821 if (telemetry->accept_fd >= 0 &&
822 telemetry->metrics_register_done == 0) {
823 ret = rte_telemetry_initial_accept(telemetry);
825 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
830 ret = rte_telemetry_read_client(telemetry);
832 TELEMETRY_LOG_ERR("Failed to read socket buffer");
841 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
844 telemetry_client *client;
845 char client_buf[BUF_SIZE];
848 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
849 bytes = read(client->fd, client_buf, BUF_SIZE-1);
852 client_buf[bytes] = '\0';
853 telemetry->request_client = client;
854 ret = rte_telemetry_parse(telemetry, client_buf);
856 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
867 rte_telemetry_run(void *userdata)
870 struct telemetry_impl *telemetry = userdata;
872 if (telemetry == NULL) {
873 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
877 ret = rte_telemetry_accept_new_client(telemetry);
879 TELEMETRY_LOG_ERR("Accept and read new client failed");
883 ret = rte_telemetry_read_client_sockets(telemetry);
885 TELEMETRY_LOG_ERR("Client socket read failed");
893 *rte_telemetry_run_thread_func(void *userdata)
896 struct telemetry_impl *telemetry = userdata;
898 if (telemetry == NULL) {
899 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
903 while (telemetry->thread_status) {
904 rte_telemetry_run(telemetry);
905 ret = usleep(SLEEP_TIME);
907 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
913 rte_telemetry_set_socket_nonblock(int fd)
918 TELEMETRY_LOG_ERR("Invalid fd provided");
922 flags = fcntl(fd, F_GETFL, 0);
926 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
930 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
933 struct sockaddr_un addr;
934 char socket_path[BUF_SIZE];
936 if (telemetry == NULL)
939 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
940 if (telemetry->server_fd == -1) {
941 TELEMETRY_LOG_ERR("Failed to open socket");
945 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
947 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
951 addr.sun_family = AF_UNIX;
952 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
953 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
956 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
958 TELEMETRY_LOG_ERR("Socket binding error");
965 if (close(telemetry->server_fd) < 0) {
966 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
973 int32_t __rte_experimental
978 const char *telemetry_ctrl_thread = "telemetry";
980 if (static_telemetry) {
981 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
985 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
986 if (static_telemetry == NULL) {
987 TELEMETRY_LOG_ERR("Memory could not be allocated");
991 static_telemetry->socket_id = rte_socket_id();
992 rte_metrics_init(static_telemetry->socket_id);
994 ret = pthread_attr_init(&attr);
996 TELEMETRY_LOG_ERR("Pthread attribute init failed");
1000 ret = rte_telemetry_create_socket(static_telemetry);
1002 ret = rte_telemetry_cleanup();
1004 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1007 TAILQ_INIT(&static_telemetry->client_list_head);
1009 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
1010 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
1011 (void *)static_telemetry);
1012 static_telemetry->thread_status = 1;
1015 ret = rte_telemetry_cleanup();
1017 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1025 rte_telemetry_client_cleanup(struct telemetry_client *client)
1029 ret = close(client->fd);
1030 free(client->file_path);
1034 TELEMETRY_LOG_ERR("Close client socket failed");
1041 int32_t __rte_experimental
1042 rte_telemetry_cleanup(void)
1045 struct telemetry_impl *telemetry = static_telemetry;
1046 telemetry_client *client, *temp_client;
1048 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1050 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
1051 ret = rte_telemetry_client_cleanup(client);
1053 TELEMETRY_LOG_ERR("Client cleanup failed");
1058 ret = close(telemetry->server_fd);
1060 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
1065 telemetry->thread_status = 0;
1066 pthread_join(telemetry->thread_id, NULL);
1068 static_telemetry = NULL;
1074 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
1075 const char *client_path)
1078 telemetry_client *client, *temp_client;
1080 if (telemetry == NULL) {
1081 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
1085 if (client_path == NULL) {
1086 TELEMETRY_LOG_ERR("Invalid client path");
1090 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
1091 TELEMETRY_LOG_ERR("There are no clients currently registered");
1095 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1097 if (strcmp(client_path, client->file_path) == 0) {
1098 TAILQ_REMOVE(&telemetry->client_list_head, client,
1100 ret = rte_telemetry_client_cleanup(client);
1103 TELEMETRY_LOG_ERR("Client cleanup failed");
1111 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
1115 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
1117 TELEMETRY_LOG_ERR("Could not send error");
1122 rte_telemetry_register_client(struct telemetry_impl *telemetry,
1123 const char *client_path)
1126 struct sockaddr_un addrs;
1128 if (telemetry == NULL) {
1129 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
1133 if (client_path == NULL) {
1134 TELEMETRY_LOG_ERR("Invalid client path");
1138 telemetry_client *client;
1139 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
1140 if (strcmp(client_path, client->file_path) == 0) {
1141 TELEMETRY_LOG_WARN("'%s' already registered",
1147 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1149 TELEMETRY_LOG_ERR("Client socket error");
1153 ret = rte_telemetry_set_socket_nonblock(fd);
1155 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
1159 addrs.sun_family = AF_UNIX;
1160 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1161 telemetry_client *new_client = malloc(sizeof(telemetry_client));
1162 new_client->file_path = strdup(client_path);
1163 new_client->fd = fd;
1165 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
1166 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
1168 ret = rte_telemetry_client_cleanup(new_client);
1170 TELEMETRY_LOG_ERR("Client cleanup failed");
1176 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
1182 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
1184 int ret, action_int;
1186 json_t *root = json_loads(buf, 0, &error);
1189 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
1192 } else if (!json_is_object(root)) {
1193 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
1197 json_t *action = json_object_get(root, "action");
1198 if (action == NULL) {
1199 TELEMETRY_LOG_WARN("Request does not have action field");
1201 } else if (!json_is_integer(action)) {
1202 TELEMETRY_LOG_WARN("Action value is not an integer");
1206 json_t *command = json_object_get(root, "command");
1207 if (command == NULL) {
1208 TELEMETRY_LOG_WARN("Request does not have command field");
1210 } else if (!json_is_string(command)) {
1211 TELEMETRY_LOG_WARN("Command value is not a string");
1215 action_int = json_integer_value(action);
1216 if (action_int != ACTION_POST) {
1217 TELEMETRY_LOG_WARN("Invalid action code");
1221 if (strcmp(json_string_value(command), "clients") != 0) {
1222 TELEMETRY_LOG_WARN("Invalid command");
1226 json_t *data = json_object_get(root, "data");
1228 TELEMETRY_LOG_WARN("Request does not have data field");
1232 json_t *client_path = json_object_get(data, "client_path");
1233 if (client_path == NULL) {
1234 TELEMETRY_LOG_WARN("Request does not have client_path field");
1238 if (!json_is_string(client_path)) {
1239 TELEMETRY_LOG_WARN("Client_path value is not a string");
1243 ret = rte_telemetry_register_client(telemetry,
1244 json_string_value(client_path));
1246 TELEMETRY_LOG_ERR("Could not register client");
1247 telemetry->register_fail_count++;
1254 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
1260 rte_telemetry_dummy_client_socket(const char *valid_client_path)
1262 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1263 struct sockaddr_un addr = {0};
1266 TELEMETRY_LOG_ERR("Test socket creation failure");
1270 addr.sun_family = AF_UNIX;
1271 strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
1272 unlink(valid_client_path);
1274 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1275 TELEMETRY_LOG_ERR("Test socket binding failure");
1279 if (listen(sockfd, 1) < 0) {
1280 TELEMETRY_LOG_ERR("Listen failure");
1287 int32_t __rte_experimental
1288 rte_telemetry_selftest(void)
1290 const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
1291 const char *valid_client_path = SELFTEST_VALID_CLIENT;
1294 TELEMETRY_LOG_INFO("Selftest");
1296 ret = rte_telemetry_init();
1298 TELEMETRY_LOG_ERR("Valid initialisation test failed");
1302 TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
1304 ret = rte_telemetry_init();
1305 if (ret != -EALREADY) {
1306 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
1310 TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
1312 ret = rte_telemetry_unregister_client(static_telemetry,
1313 invalid_client_path);
1314 if (ret != -EPERM) {
1315 TELEMETRY_LOG_ERR("Invalid unregister test failed");
1319 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1321 sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
1323 TELEMETRY_LOG_ERR("Test socket creation failed");
1327 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1329 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
1333 accept(sockfd, NULL, NULL);
1334 TELEMETRY_LOG_INFO("Success - Valid register test passed");
1336 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1337 if (ret != -EINVAL) {
1338 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
1342 TELEMETRY_LOG_INFO("Success - Invalid register test passed");
1344 ret = rte_telemetry_unregister_client(static_telemetry,
1345 invalid_client_path);
1347 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
1351 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1353 ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
1355 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
1359 TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
1361 ret = rte_telemetry_cleanup();
1363 TELEMETRY_LOG_ERR("Cleanup test failed");
1367 TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
1373 rte_telemetry_socket_messaging_testing(int index, int socket)
1375 struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
1376 int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
1378 if (telemetry == NULL) {
1379 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
1383 telemetry->server_fd = socket;
1384 telemetry->reg_index[0] = index;
1385 TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
1386 rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
1387 TELEMETRY_LOG_INFO("Register valid client test");
1389 ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
1392 TELEMETRY_LOG_ERR("Register valid client test failed!");
1397 TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
1399 TELEMETRY_LOG_INFO("Register invalid/same client test");
1400 ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
1402 ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
1403 bad_send_fd, bad_recv_fd);
1405 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
1410 TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
1412 ret = rte_telemetry_json_socket_message_test(telemetry, fd);
1423 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
1424 int send_fd, int recv_fd)
1427 char good_req_string[BUF_SIZE];
1429 snprintf(good_req_string, sizeof(good_req_string),
1430 "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
1431 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
1435 ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
1437 TELEMETRY_LOG_ERR("Could not send message over socket");
1441 rte_telemetry_run(telemetry);
1443 if (telemetry->register_fail_count != 0)
1446 *fd = accept(recv_fd, NULL, NULL);
1452 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
1456 const char *client_path = SOCKET_TEST_CLIENT_PATH;
1457 char socket_path[BUF_SIZE];
1458 struct sockaddr_un addr = {0};
1459 struct sockaddr_un addrs = {0};
1460 *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1461 *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1463 listen(telemetry->server_fd, 5);
1464 addr.sun_family = AF_UNIX;
1465 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
1466 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
1468 ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
1470 TELEMETRY_LOG_ERR("Could not connect socket");
1474 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
1476 addrs.sun_family = AF_UNIX;
1477 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1478 unlink(client_path);
1480 ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
1482 TELEMETRY_LOG_ERR("Could not bind socket");
1490 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
1493 json_t *root = json_loads(buf, 0, &error);
1495 json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
1503 TELEMETRY_LOG_ERR("JSON message is NULL");
1508 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
1511 } else if (!json_is_object(root)) {
1512 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1517 status = json_object_get(root, "status_code");
1519 TELEMETRY_LOG_ERR("Request does not have status field");
1521 } else if (!json_is_string(status)) {
1522 TELEMETRY_LOG_ERR("Status value is not a string");
1526 json_data_struct->status_code = strdup(json_string_value(status));
1528 dataArray = json_object_get(root, "data");
1529 if (dataArray == NULL) {
1530 TELEMETRY_LOG_ERR("Request does not have data field");
1534 arraylen = json_array_size(dataArray);
1535 if (arraylen == 0) {
1536 json_data_struct->data = "null";
1540 for (i = 0; i < arraylen; i++) {
1541 dataArrayObj = json_array_get(dataArray, i);
1542 port = json_object_get(dataArrayObj, "port");
1543 stats = json_object_get(dataArrayObj, "stats");
1547 TELEMETRY_LOG_ERR("Request does not have port field");
1551 if (!json_is_integer(port)) {
1552 TELEMETRY_LOG_ERR("Port value is not an integer");
1556 json_data_struct->port = json_integer_value(port);
1558 if (stats == NULL) {
1559 TELEMETRY_LOG_ERR("Request does not have stats field");
1563 arraylen = json_array_size(stats);
1564 for (i = 0; i < arraylen; i++) {
1565 statsArrayObj = json_array_get(stats, i);
1566 name = json_object_get(statsArrayObj, "name");
1567 value = json_object_get(statsArrayObj, "value");
1571 TELEMETRY_LOG_ERR("Request does not have name field");
1575 if (!json_is_string(name)) {
1576 TELEMETRY_LOG_ERR("Stat name value is not a string");
1580 json_data_struct->stat_name = strdup(json_string_value(name));
1582 if (value == NULL) {
1583 TELEMETRY_LOG_ERR("Request does not have value field");
1587 if (!json_is_integer(value)) {
1588 TELEMETRY_LOG_ERR("Stat value is not an integer");
1592 json_data_struct->stat_value = json_integer_value(value);
1598 rte_telemetry_free_test_data(struct json_data *data)
1600 free(data->status_code);
1601 free(data->stat_name);
1606 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1612 int buffer_read = 0;
1614 struct json_data *data_struct;
1616 const char *status = "Status OK: 200";
1617 const char *name = "rx_good_packets";
1618 const char *valid_json_message = "{\"action\":0,\"command\":"
1619 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1620 ":[0],\"stats\":[\"rx_good_packets\"]}}";
1622 ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1624 TELEMETRY_LOG_ERR("Could not send message over socket");
1628 rte_telemetry_run(telemetry);
1629 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1631 if (buffer_read == -1) {
1632 TELEMETRY_LOG_ERR("Read error");
1636 buf[buffer_read] = '\0';
1637 data_struct = calloc(1, sizeof(struct json_data));
1638 ret = rte_telemetry_stat_parse(buf, data_struct);
1641 TELEMETRY_LOG_ERR("Could not parse stats");
1645 if (strcmp(data_struct->status_code, status) != 0) {
1646 TELEMETRY_LOG_ERR("Status code is invalid");
1650 if (data_struct->port != port) {
1651 TELEMETRY_LOG_ERR("Port is invalid");
1655 if (strcmp(data_struct->stat_name, name) != 0) {
1656 TELEMETRY_LOG_ERR("Stat name is invalid");
1660 if (data_struct->stat_value != value) {
1661 TELEMETRY_LOG_ERR("Stat value is invalid");
1665 rte_telemetry_free_test_data(data_struct);
1669 TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1675 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1680 const char *invalid_json = "{]";
1681 const char *status = "Status Error: Unknown";
1682 const char *data = "null";
1683 struct json_data *data_struct;
1684 int buffer_read = 0;
1687 ret = send(fd, invalid_json, strlen(invalid_json), 0);
1689 TELEMETRY_LOG_ERR("Could not send message over socket");
1693 rte_telemetry_run(telemetry);
1694 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1696 if (buffer_read == -1) {
1697 TELEMETRY_LOG_ERR("Read error");
1701 buf[buffer_read] = '\0';
1703 data_struct = calloc(1, sizeof(struct json_data));
1704 ret = rte_telemetry_stat_parse(buf, data_struct);
1707 TELEMETRY_LOG_ERR("Could not parse stats");
1709 if (strcmp(data_struct->status_code, status) != 0) {
1710 TELEMETRY_LOG_ERR("Status code is invalid");
1714 if (strcmp(data_struct->data, data) != 0) {
1715 TELEMETRY_LOG_ERR("Data status is invalid");
1719 rte_telemetry_free_test_data(data_struct);
1723 TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1729 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1734 char *status = "Status Error: Invalid Argument 404";
1735 char *data = "null";
1736 struct json_data *data_struct;
1737 const char *invalid_contents = "{\"action\":0,\"command\":"
1738 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1739 ":[0],\"stats\":[\"some_invalid_param\","
1740 "\"another_invalid_param\"]}}";
1741 int buffer_read = 0;
1744 ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1746 TELEMETRY_LOG_ERR("Could not send message over socket");
1750 rte_telemetry_run(telemetry);
1751 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1753 if (buffer_read == -1) {
1754 TELEMETRY_LOG_ERR("Read error");
1758 buf[buffer_read] = '\0';
1759 data_struct = calloc(1, sizeof(struct json_data));
1760 ret = rte_telemetry_stat_parse(buf, data_struct);
1763 TELEMETRY_LOG_ERR("Could not parse stats");
1765 if (strcmp(data_struct->status_code, status) != 0) {
1766 TELEMETRY_LOG_ERR("Status code is invalid");
1770 if (strcmp(data_struct->data, data) != 0) {
1771 TELEMETRY_LOG_ERR("Data status is invalid");
1775 rte_telemetry_free_test_data(data_struct);
1779 TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1785 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1790 const char *status = "Status Error: Invalid Argument 404";
1791 char *data = "null";
1792 struct json_data *data_struct;
1793 const char *empty_json = "{}";
1794 int buffer_read = 0;
1797 ret = (send(fd, empty_json, strlen(empty_json), 0));
1799 TELEMETRY_LOG_ERR("Could not send message over socket");
1803 rte_telemetry_run(telemetry);
1804 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1806 if (buffer_read == -1) {
1807 TELEMETRY_LOG_ERR("Read error");
1811 buf[buffer_read] = '\0';
1812 data_struct = calloc(1, sizeof(struct json_data));
1813 ret = rte_telemetry_stat_parse(buf, data_struct);
1816 TELEMETRY_LOG_ERR("Could not parse stats");
1818 if (strcmp(data_struct->status_code, status) != 0) {
1819 TELEMETRY_LOG_ERR("Status code is invalid");
1823 if (strcmp(data_struct->data, data) != 0) {
1824 TELEMETRY_LOG_ERR("Data status is invalid");
1828 rte_telemetry_free_test_data(data_struct);
1833 TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1839 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1842 int ret, fail_count;
1845 struct telemetry_message_test socket_json_tests[] = {
1846 {.test_name = "Invalid JSON test",
1847 .test_func_ptr = rte_telemetry_invalid_json_test},
1848 {.test_name = "Valid JSON test",
1849 .test_func_ptr = rte_telemetry_valid_json_test},
1850 {.test_name = "JSON contents test",
1851 .test_func_ptr = rte_telemetry_json_contents_test},
1852 {.test_name = "JSON empty tests",
1853 .test_func_ptr = rte_telemetry_json_empty_test}
1856 #define NUM_TESTS RTE_DIM(socket_json_tests)
1858 for (i = 0; i < NUM_TESTS; i++) {
1859 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1860 ret = (socket_json_tests[i].test_func_ptr)
1863 TELEMETRY_LOG_ERR("%s failed",
1864 socket_json_tests[i].test_name);
1869 if (fail_count > 0) {
1870 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1875 TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1880 int telemetry_log_level;
1882 static struct rte_option option = {
1883 .name = "telemetry",
1884 .usage = "Enable telemetry backend",
1885 .cb = &rte_telemetry_init,
1889 RTE_INIT(rte_telemetry_register)
1891 telemetry_log_level = rte_log_register("lib.telemetry");
1892 if (telemetry_log_level >= 0)
1893 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1895 rte_option_register(&option);