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"
25 static telemetry_impl *static_telemetry;
28 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
30 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
34 rte_telemetry_is_port_active(int port_id)
38 ret = rte_eth_find_next(port_id);
42 TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
49 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
50 const char *json_string)
54 if (telemetry == NULL) {
55 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
59 if (telemetry->request_client == NULL) {
60 TELEMETRY_LOG_ERR("No client has been chosen to write to");
64 if (json_string == NULL) {
65 TELEMETRY_LOG_ERR("Invalid JSON string!");
69 ret = send(telemetry->request_client->fd,
70 json_string, strlen(json_string), 0);
72 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
73 telemetry->request_client->file_path);
81 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
85 const char *status_code, *json_buffer;
88 if (error_type == -EPERM)
89 status_code = "Status Error: Unknown";
90 else if (error_type == -EINVAL)
91 status_code = "Status Error: Invalid Argument 404";
92 else if (error_type == -ENOMEM)
93 status_code = "Status Error: Memory Allocation Error";
95 TELEMETRY_LOG_ERR("Invalid error type");
102 TELEMETRY_LOG_ERR("Could not create root JSON object");
106 ret = json_object_set_new(root, "status_code", json_string(status_code));
108 TELEMETRY_LOG_ERR("Status code field cannot be set");
113 ret = json_object_set_new(root, "data", json_null());
115 TELEMETRY_LOG_ERR("Data field cannot be set");
120 json_buffer = json_dumps(root, JSON_INDENT(2));
123 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
125 TELEMETRY_LOG_ERR("Could not write to socket");
133 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
135 int ret, num_xstats, ret_val, i;
136 struct rte_eth_xstat *eth_xstats = NULL;
137 struct rte_eth_xstat_name *eth_xstats_names = NULL;
139 if (!rte_eth_dev_is_valid_port(port_id)) {
140 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
144 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
145 if (num_xstats < 0) {
146 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
147 port_id, num_xstats);
151 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
152 if (eth_xstats == NULL) {
153 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
157 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
158 const char *xstats_names[num_xstats];
159 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
160 if (ret < 0 || ret > num_xstats) {
161 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
162 port_id, num_xstats, ret);
167 if (eth_xstats_names == NULL) {
168 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
173 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
174 if (ret < 0 || ret > num_xstats) {
175 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
176 port_id, num_xstats, ret);
181 for (i = 0; i < num_xstats; i++)
182 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
184 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
186 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
195 free(eth_xstats_names);
200 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
204 RTE_ETH_FOREACH_DEV(pid) {
205 telemetry->reg_index = rte_telemetry_reg_ethdev_to_metrics(pid);
209 if (telemetry->reg_index < 0) {
210 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
214 telemetry->metrics_register_done = 1;
220 rte_telemetry_read_client(struct telemetry_impl *telemetry)
223 int ret, buffer_read;
225 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
227 if (buffer_read == -1) {
228 TELEMETRY_LOG_ERR("Read error");
230 } else if (buffer_read == 0) {
233 buf[buffer_read] = '\0';
234 ret = rte_telemetry_parse_client_message(telemetry, buf);
236 TELEMETRY_LOG_WARN("Parse message failed");
241 if (close(telemetry->accept_fd) < 0) {
242 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
246 telemetry->accept_fd = 0;
252 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
256 if (telemetry->accept_fd <= 0) {
257 ret = listen(telemetry->server_fd, 1);
259 TELEMETRY_LOG_ERR("Listening error with server fd");
263 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
264 if (telemetry->accept_fd >= 0 &&
265 telemetry->metrics_register_done == 0) {
266 ret = rte_telemetry_initial_accept(telemetry);
268 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
273 ret = rte_telemetry_read_client(telemetry);
275 TELEMETRY_LOG_ERR("Failed to read socket buffer");
284 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
286 telemetry_client *client;
287 char client_buf[BUF_SIZE];
290 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
291 bytes = read(client->fd, client_buf, BUF_SIZE-1);
294 client_buf[bytes] = '\0';
295 telemetry->request_client = client;
303 rte_telemetry_run(void *userdata)
306 struct telemetry_impl *telemetry = userdata;
308 if (telemetry == NULL) {
309 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
313 ret = rte_telemetry_accept_new_client(telemetry);
315 TELEMETRY_LOG_ERR("Accept and read new client failed");
319 ret = rte_telemetry_read_client_sockets(telemetry);
321 TELEMETRY_LOG_ERR("Client socket read failed");
329 *rte_telemetry_run_thread_func(void *userdata)
332 struct telemetry_impl *telemetry = userdata;
334 if (telemetry == NULL) {
335 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
339 while (telemetry->thread_status) {
340 rte_telemetry_run(telemetry);
341 ret = usleep(SLEEP_TIME);
343 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
349 rte_telemetry_set_socket_nonblock(int fd)
354 TELEMETRY_LOG_ERR("Invalid fd provided");
358 flags = fcntl(fd, F_GETFL, 0);
362 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
366 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
369 struct sockaddr_un addr;
370 char socket_path[BUF_SIZE];
372 if (telemetry == NULL)
375 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
376 if (telemetry->server_fd == -1) {
377 TELEMETRY_LOG_ERR("Failed to open socket");
381 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
383 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
387 addr.sun_family = AF_UNIX;
388 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
389 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
392 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
394 TELEMETRY_LOG_ERR("Socket binding error");
401 if (close(telemetry->server_fd) < 0) {
402 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
409 int32_t __rte_experimental
414 const char *telemetry_ctrl_thread = "telemetry";
416 if (static_telemetry) {
417 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
421 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
422 if (static_telemetry == NULL) {
423 TELEMETRY_LOG_ERR("Memory could not be allocated");
427 static_telemetry->socket_id = rte_socket_id();
428 rte_metrics_init(static_telemetry->socket_id);
430 ret = pthread_attr_init(&attr);
432 TELEMETRY_LOG_ERR("Pthread attribute init failed");
436 ret = rte_telemetry_create_socket(static_telemetry);
438 ret = rte_telemetry_cleanup();
440 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
443 TAILQ_INIT(&static_telemetry->client_list_head);
445 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
446 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
447 (void *)static_telemetry);
448 static_telemetry->thread_status = 1;
451 ret = rte_telemetry_cleanup();
453 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
461 rte_telemetry_client_cleanup(struct telemetry_client *client)
465 ret = close(client->fd);
466 free(client->file_path);
470 TELEMETRY_LOG_ERR("Close client socket failed");
477 int32_t __rte_experimental
478 rte_telemetry_cleanup(void)
481 struct telemetry_impl *telemetry = static_telemetry;
482 telemetry_client *client, *temp_client;
484 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
486 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
487 ret = rte_telemetry_client_cleanup(client);
489 TELEMETRY_LOG_ERR("Client cleanup failed");
494 ret = close(telemetry->server_fd);
496 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
501 telemetry->thread_status = 0;
502 pthread_join(telemetry->thread_id, NULL);
504 static_telemetry = NULL;
510 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
511 const char *client_path)
514 telemetry_client *client, *temp_client;
516 if (telemetry == NULL) {
517 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
521 if (client_path == NULL) {
522 TELEMETRY_LOG_ERR("Invalid client path");
526 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
527 TELEMETRY_LOG_ERR("There are no clients currently registered");
531 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
533 if (strcmp(client_path, client->file_path) == 0) {
534 TAILQ_REMOVE(&telemetry->client_list_head, client,
536 ret = rte_telemetry_client_cleanup(client);
539 TELEMETRY_LOG_ERR("Client cleanup failed");
547 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
551 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
553 TELEMETRY_LOG_ERR("Could not send error");
558 rte_telemetry_register_client(struct telemetry_impl *telemetry,
559 const char *client_path)
562 struct sockaddr_un addrs;
564 if (telemetry == NULL) {
565 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
569 if (client_path == NULL) {
570 TELEMETRY_LOG_ERR("Invalid client path");
574 telemetry_client *client;
575 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
576 if (strcmp(client_path, client->file_path) == 0) {
577 TELEMETRY_LOG_WARN("'%s' already registered",
583 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
585 TELEMETRY_LOG_ERR("Client socket error");
589 ret = rte_telemetry_set_socket_nonblock(fd);
591 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
595 addrs.sun_family = AF_UNIX;
596 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
597 telemetry_client *new_client = malloc(sizeof(telemetry_client));
598 new_client->file_path = strdup(client_path);
601 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
602 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
604 ret = rte_telemetry_client_cleanup(new_client);
606 TELEMETRY_LOG_ERR("Client cleanup failed");
612 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
618 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
622 json_t *root = json_loads(buf, 0, &error);
625 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
628 } else if (!json_is_object(root)) {
629 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
633 json_t *action = json_object_get(root, "action");
634 if (action == NULL) {
635 TELEMETRY_LOG_WARN("Request does not have action field");
637 } else if (!json_is_integer(action)) {
638 TELEMETRY_LOG_WARN("Action value is not an integer");
642 json_t *command = json_object_get(root, "command");
643 if (command == NULL) {
644 TELEMETRY_LOG_WARN("Request does not have command field");
646 } else if (!json_is_string(command)) {
647 TELEMETRY_LOG_WARN("Command value is not a string");
651 action_int = json_integer_value(action);
652 if (action_int != ACTION_POST) {
653 TELEMETRY_LOG_WARN("Invalid action code");
657 if (strcmp(json_string_value(command), "clients") != 0) {
658 TELEMETRY_LOG_WARN("Invalid command");
662 json_t *data = json_object_get(root, "data");
664 TELEMETRY_LOG_WARN("Request does not have data field");
668 json_t *client_path = json_object_get(data, "client_path");
669 if (client_path == NULL) {
670 TELEMETRY_LOG_WARN("Request does not have client_path field");
674 if (!json_is_string(client_path)) {
675 TELEMETRY_LOG_WARN("Client_path value is not a string");
679 ret = rte_telemetry_register_client(telemetry,
680 json_string_value(client_path));
682 TELEMETRY_LOG_ERR("Could not register client");
683 telemetry->register_fail_count++;
690 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
695 int telemetry_log_level;
697 static struct rte_option option = {
698 .opt_str = "--telemetry",
699 .cb = &rte_telemetry_init,
703 RTE_INIT(rte_telemetry_register)
705 telemetry_log_level = rte_log_register("lib.telemetry");
706 if (telemetry_log_level >= 0)
707 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
709 rte_option_register(&option);