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"
26 static telemetry_impl *static_telemetry;
29 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
31 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
35 rte_telemetry_is_port_active(int port_id)
39 ret = rte_eth_find_next(port_id);
43 TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
50 rte_telemetry_update_metrics_ethdev(struct telemetry_impl *telemetry,
51 uint16_t port_id, int reg_start_index)
53 int ret, num_xstats, i;
54 struct rte_eth_xstat *eth_xstats;
56 if (!rte_eth_dev_is_valid_port(port_id)) {
57 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
58 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
60 TELEMETRY_LOG_ERR("Could not send error");
64 ret = rte_telemetry_is_port_active(port_id);
66 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
68 TELEMETRY_LOG_ERR("Could not send error");
72 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
74 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
76 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
78 TELEMETRY_LOG_ERR("Could not send error");
82 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
83 if (eth_xstats == NULL) {
84 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
85 ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
87 TELEMETRY_LOG_ERR("Could not send error");
91 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
92 if (ret < 0 || ret > num_xstats) {
94 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
95 port_id, num_xstats, ret);
96 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
98 TELEMETRY_LOG_ERR("Could not send error");
102 uint64_t xstats_values[num_xstats];
103 for (i = 0; i < num_xstats; i++)
104 xstats_values[i] = eth_xstats[i].value;
106 ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
109 TELEMETRY_LOG_ERR("Could not update metrics values");
110 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
112 TELEMETRY_LOG_ERR("Could not send error");
122 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
123 const char *json_string)
127 if (telemetry == NULL) {
128 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
132 if (telemetry->request_client == NULL) {
133 TELEMETRY_LOG_ERR("No client has been chosen to write to");
137 if (json_string == NULL) {
138 TELEMETRY_LOG_ERR("Invalid JSON string!");
142 ret = send(telemetry->request_client->fd,
143 json_string, strlen(json_string), 0);
145 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
146 telemetry->request_client->file_path);
154 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
158 const char *status_code, *json_buffer;
161 if (error_type == -EPERM)
162 status_code = "Status Error: Unknown";
163 else if (error_type == -EINVAL)
164 status_code = "Status Error: Invalid Argument 404";
165 else if (error_type == -ENOMEM)
166 status_code = "Status Error: Memory Allocation Error";
168 TELEMETRY_LOG_ERR("Invalid error type");
172 root = json_object();
175 TELEMETRY_LOG_ERR("Could not create root JSON object");
179 ret = json_object_set_new(root, "status_code", json_string(status_code));
181 TELEMETRY_LOG_ERR("Status code field cannot be set");
186 ret = json_object_set_new(root, "data", json_null());
188 TELEMETRY_LOG_ERR("Data field cannot be set");
193 json_buffer = json_dumps(root, JSON_INDENT(2));
196 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
198 TELEMETRY_LOG_ERR("Could not write to socket");
206 rte_telemetry_send_ports_stats_values(uint32_t *metric_ids, int num_metric_ids,
207 uint32_t *port_ids, int num_port_ids, struct telemetry_impl *telemetry)
210 char *json_buffer = NULL;
212 if (telemetry == NULL) {
213 TELEMETRY_LOG_ERR("Invalid telemetry argument");
217 if (metric_ids == NULL) {
218 TELEMETRY_LOG_ERR("Invalid metric_ids array");
222 if (num_metric_ids < 0) {
223 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
227 if (port_ids == NULL) {
228 TELEMETRY_LOG_ERR("Invalid port_ids array");
232 if (num_port_ids < 0) {
233 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
237 for (i = 0; i < num_port_ids; i++) {
238 if (!rte_eth_dev_is_valid_port(port_ids[i])) {
239 TELEMETRY_LOG_ERR("Port: %d invalid", port_ids[i]);
243 ret = rte_telemetry_update_metrics_ethdev(telemetry,
244 port_ids[i], telemetry->reg_index);
246 TELEMETRY_LOG_ERR("Failed to update ethdev metrics");
251 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
253 TELEMETRY_LOG_ERR("Could not write to socket");
260 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
262 TELEMETRY_LOG_ERR("Could not send error");
268 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
270 int ret, num_xstats, ret_val, i;
271 struct rte_eth_xstat *eth_xstats = NULL;
272 struct rte_eth_xstat_name *eth_xstats_names = NULL;
274 if (!rte_eth_dev_is_valid_port(port_id)) {
275 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
279 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
280 if (num_xstats < 0) {
281 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
282 port_id, num_xstats);
286 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
287 if (eth_xstats == NULL) {
288 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
292 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
293 const char *xstats_names[num_xstats];
294 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
295 if (ret < 0 || ret > num_xstats) {
296 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
297 port_id, num_xstats, ret);
302 if (eth_xstats_names == NULL) {
303 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
308 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
309 if (ret < 0 || ret > num_xstats) {
310 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
311 port_id, num_xstats, ret);
316 for (i = 0; i < num_xstats; i++)
317 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
319 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
321 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
330 free(eth_xstats_names);
335 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
339 RTE_ETH_FOREACH_DEV(pid) {
340 telemetry->reg_index = rte_telemetry_reg_ethdev_to_metrics(pid);
344 if (telemetry->reg_index < 0) {
345 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
349 telemetry->metrics_register_done = 1;
355 rte_telemetry_read_client(struct telemetry_impl *telemetry)
358 int ret, buffer_read;
360 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
362 if (buffer_read == -1) {
363 TELEMETRY_LOG_ERR("Read error");
365 } else if (buffer_read == 0) {
368 buf[buffer_read] = '\0';
369 ret = rte_telemetry_parse_client_message(telemetry, buf);
371 TELEMETRY_LOG_WARN("Parse message failed");
376 if (close(telemetry->accept_fd) < 0) {
377 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
381 telemetry->accept_fd = 0;
387 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
391 if (telemetry->accept_fd <= 0) {
392 ret = listen(telemetry->server_fd, 1);
394 TELEMETRY_LOG_ERR("Listening error with server fd");
398 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
399 if (telemetry->accept_fd >= 0 &&
400 telemetry->metrics_register_done == 0) {
401 ret = rte_telemetry_initial_accept(telemetry);
403 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
408 ret = rte_telemetry_read_client(telemetry);
410 TELEMETRY_LOG_ERR("Failed to read socket buffer");
419 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
422 telemetry_client *client;
423 char client_buf[BUF_SIZE];
426 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
427 bytes = read(client->fd, client_buf, BUF_SIZE-1);
430 client_buf[bytes] = '\0';
431 telemetry->request_client = client;
432 ret = rte_telemetry_parse(telemetry, client_buf);
434 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
445 rte_telemetry_run(void *userdata)
448 struct telemetry_impl *telemetry = userdata;
450 if (telemetry == NULL) {
451 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
455 ret = rte_telemetry_accept_new_client(telemetry);
457 TELEMETRY_LOG_ERR("Accept and read new client failed");
461 ret = rte_telemetry_read_client_sockets(telemetry);
463 TELEMETRY_LOG_ERR("Client socket read failed");
471 *rte_telemetry_run_thread_func(void *userdata)
474 struct telemetry_impl *telemetry = userdata;
476 if (telemetry == NULL) {
477 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
481 while (telemetry->thread_status) {
482 rte_telemetry_run(telemetry);
483 ret = usleep(SLEEP_TIME);
485 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
491 rte_telemetry_set_socket_nonblock(int fd)
496 TELEMETRY_LOG_ERR("Invalid fd provided");
500 flags = fcntl(fd, F_GETFL, 0);
504 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
508 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
511 struct sockaddr_un addr;
512 char socket_path[BUF_SIZE];
514 if (telemetry == NULL)
517 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
518 if (telemetry->server_fd == -1) {
519 TELEMETRY_LOG_ERR("Failed to open socket");
523 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
525 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
529 addr.sun_family = AF_UNIX;
530 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
531 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
534 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
536 TELEMETRY_LOG_ERR("Socket binding error");
543 if (close(telemetry->server_fd) < 0) {
544 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
551 int32_t __rte_experimental
556 const char *telemetry_ctrl_thread = "telemetry";
558 if (static_telemetry) {
559 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
563 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
564 if (static_telemetry == NULL) {
565 TELEMETRY_LOG_ERR("Memory could not be allocated");
569 static_telemetry->socket_id = rte_socket_id();
570 rte_metrics_init(static_telemetry->socket_id);
572 ret = pthread_attr_init(&attr);
574 TELEMETRY_LOG_ERR("Pthread attribute init failed");
578 ret = rte_telemetry_create_socket(static_telemetry);
580 ret = rte_telemetry_cleanup();
582 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
585 TAILQ_INIT(&static_telemetry->client_list_head);
587 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
588 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
589 (void *)static_telemetry);
590 static_telemetry->thread_status = 1;
593 ret = rte_telemetry_cleanup();
595 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
603 rte_telemetry_client_cleanup(struct telemetry_client *client)
607 ret = close(client->fd);
608 free(client->file_path);
612 TELEMETRY_LOG_ERR("Close client socket failed");
619 int32_t __rte_experimental
620 rte_telemetry_cleanup(void)
623 struct telemetry_impl *telemetry = static_telemetry;
624 telemetry_client *client, *temp_client;
626 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
628 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
629 ret = rte_telemetry_client_cleanup(client);
631 TELEMETRY_LOG_ERR("Client cleanup failed");
636 ret = close(telemetry->server_fd);
638 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
643 telemetry->thread_status = 0;
644 pthread_join(telemetry->thread_id, NULL);
646 static_telemetry = NULL;
652 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
653 const char *client_path)
656 telemetry_client *client, *temp_client;
658 if (telemetry == NULL) {
659 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
663 if (client_path == NULL) {
664 TELEMETRY_LOG_ERR("Invalid client path");
668 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
669 TELEMETRY_LOG_ERR("There are no clients currently registered");
673 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
675 if (strcmp(client_path, client->file_path) == 0) {
676 TAILQ_REMOVE(&telemetry->client_list_head, client,
678 ret = rte_telemetry_client_cleanup(client);
681 TELEMETRY_LOG_ERR("Client cleanup failed");
689 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
693 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
695 TELEMETRY_LOG_ERR("Could not send error");
700 rte_telemetry_register_client(struct telemetry_impl *telemetry,
701 const char *client_path)
704 struct sockaddr_un addrs;
706 if (telemetry == NULL) {
707 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
711 if (client_path == NULL) {
712 TELEMETRY_LOG_ERR("Invalid client path");
716 telemetry_client *client;
717 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
718 if (strcmp(client_path, client->file_path) == 0) {
719 TELEMETRY_LOG_WARN("'%s' already registered",
725 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
727 TELEMETRY_LOG_ERR("Client socket error");
731 ret = rte_telemetry_set_socket_nonblock(fd);
733 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
737 addrs.sun_family = AF_UNIX;
738 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
739 telemetry_client *new_client = malloc(sizeof(telemetry_client));
740 new_client->file_path = strdup(client_path);
743 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
744 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
746 ret = rte_telemetry_client_cleanup(new_client);
748 TELEMETRY_LOG_ERR("Client cleanup failed");
754 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
760 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
764 json_t *root = json_loads(buf, 0, &error);
767 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
770 } else if (!json_is_object(root)) {
771 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
775 json_t *action = json_object_get(root, "action");
776 if (action == NULL) {
777 TELEMETRY_LOG_WARN("Request does not have action field");
779 } else if (!json_is_integer(action)) {
780 TELEMETRY_LOG_WARN("Action value is not an integer");
784 json_t *command = json_object_get(root, "command");
785 if (command == NULL) {
786 TELEMETRY_LOG_WARN("Request does not have command field");
788 } else if (!json_is_string(command)) {
789 TELEMETRY_LOG_WARN("Command value is not a string");
793 action_int = json_integer_value(action);
794 if (action_int != ACTION_POST) {
795 TELEMETRY_LOG_WARN("Invalid action code");
799 if (strcmp(json_string_value(command), "clients") != 0) {
800 TELEMETRY_LOG_WARN("Invalid command");
804 json_t *data = json_object_get(root, "data");
806 TELEMETRY_LOG_WARN("Request does not have data field");
810 json_t *client_path = json_object_get(data, "client_path");
811 if (client_path == NULL) {
812 TELEMETRY_LOG_WARN("Request does not have client_path field");
816 if (!json_is_string(client_path)) {
817 TELEMETRY_LOG_WARN("Client_path value is not a string");
821 ret = rte_telemetry_register_client(telemetry,
822 json_string_value(client_path));
824 TELEMETRY_LOG_ERR("Could not register client");
825 telemetry->register_fail_count++;
832 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
837 int telemetry_log_level;
839 static struct rte_option option = {
840 .opt_str = "--telemetry",
841 .cb = &rte_telemetry_init,
845 RTE_INIT(rte_telemetry_register)
847 telemetry_log_level = rte_log_register("lib.telemetry");
848 if (telemetry_log_level >= 0)
849 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
851 rte_option_register(&option);