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_write_to_socket(struct telemetry_impl *telemetry,
51 const char *json_string)
55 if (telemetry == NULL) {
56 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
60 if (telemetry->request_client == NULL) {
61 TELEMETRY_LOG_ERR("No client has been chosen to write to");
65 if (json_string == NULL) {
66 TELEMETRY_LOG_ERR("Invalid JSON string!");
70 ret = send(telemetry->request_client->fd,
71 json_string, strlen(json_string), 0);
73 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
74 telemetry->request_client->file_path);
82 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
86 const char *status_code, *json_buffer;
89 if (error_type == -EPERM)
90 status_code = "Status Error: Unknown";
91 else if (error_type == -EINVAL)
92 status_code = "Status Error: Invalid Argument 404";
93 else if (error_type == -ENOMEM)
94 status_code = "Status Error: Memory Allocation Error";
96 TELEMETRY_LOG_ERR("Invalid error type");
100 root = json_object();
103 TELEMETRY_LOG_ERR("Could not create root JSON object");
107 ret = json_object_set_new(root, "status_code", json_string(status_code));
109 TELEMETRY_LOG_ERR("Status code field cannot be set");
114 ret = json_object_set_new(root, "data", json_null());
116 TELEMETRY_LOG_ERR("Data field cannot be set");
121 json_buffer = json_dumps(root, JSON_INDENT(2));
124 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
126 TELEMETRY_LOG_ERR("Could not write to socket");
134 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
136 int ret, num_xstats, ret_val, i;
137 struct rte_eth_xstat *eth_xstats = NULL;
138 struct rte_eth_xstat_name *eth_xstats_names = NULL;
140 if (!rte_eth_dev_is_valid_port(port_id)) {
141 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
145 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
146 if (num_xstats < 0) {
147 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
148 port_id, num_xstats);
152 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
153 if (eth_xstats == NULL) {
154 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
158 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
159 const char *xstats_names[num_xstats];
160 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
161 if (ret < 0 || ret > num_xstats) {
162 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
163 port_id, num_xstats, ret);
168 if (eth_xstats_names == NULL) {
169 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
174 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
175 if (ret < 0 || ret > num_xstats) {
176 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
177 port_id, num_xstats, ret);
182 for (i = 0; i < num_xstats; i++)
183 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
185 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
187 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
196 free(eth_xstats_names);
201 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
205 RTE_ETH_FOREACH_DEV(pid) {
206 telemetry->reg_index = rte_telemetry_reg_ethdev_to_metrics(pid);
210 if (telemetry->reg_index < 0) {
211 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
215 telemetry->metrics_register_done = 1;
221 rte_telemetry_read_client(struct telemetry_impl *telemetry)
224 int ret, buffer_read;
226 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
228 if (buffer_read == -1) {
229 TELEMETRY_LOG_ERR("Read error");
231 } else if (buffer_read == 0) {
234 buf[buffer_read] = '\0';
235 ret = rte_telemetry_parse_client_message(telemetry, buf);
237 TELEMETRY_LOG_WARN("Parse message failed");
242 if (close(telemetry->accept_fd) < 0) {
243 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
247 telemetry->accept_fd = 0;
253 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
257 if (telemetry->accept_fd <= 0) {
258 ret = listen(telemetry->server_fd, 1);
260 TELEMETRY_LOG_ERR("Listening error with server fd");
264 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
265 if (telemetry->accept_fd >= 0 &&
266 telemetry->metrics_register_done == 0) {
267 ret = rte_telemetry_initial_accept(telemetry);
269 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
274 ret = rte_telemetry_read_client(telemetry);
276 TELEMETRY_LOG_ERR("Failed to read socket buffer");
285 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
288 telemetry_client *client;
289 char client_buf[BUF_SIZE];
292 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
293 bytes = read(client->fd, client_buf, BUF_SIZE-1);
296 client_buf[bytes] = '\0';
297 telemetry->request_client = client;
298 ret = rte_telemetry_parse(telemetry, client_buf);
300 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
311 rte_telemetry_run(void *userdata)
314 struct telemetry_impl *telemetry = userdata;
316 if (telemetry == NULL) {
317 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
321 ret = rte_telemetry_accept_new_client(telemetry);
323 TELEMETRY_LOG_ERR("Accept and read new client failed");
327 ret = rte_telemetry_read_client_sockets(telemetry);
329 TELEMETRY_LOG_ERR("Client socket read failed");
337 *rte_telemetry_run_thread_func(void *userdata)
340 struct telemetry_impl *telemetry = userdata;
342 if (telemetry == NULL) {
343 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
347 while (telemetry->thread_status) {
348 rte_telemetry_run(telemetry);
349 ret = usleep(SLEEP_TIME);
351 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
357 rte_telemetry_set_socket_nonblock(int fd)
362 TELEMETRY_LOG_ERR("Invalid fd provided");
366 flags = fcntl(fd, F_GETFL, 0);
370 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
374 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
377 struct sockaddr_un addr;
378 char socket_path[BUF_SIZE];
380 if (telemetry == NULL)
383 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
384 if (telemetry->server_fd == -1) {
385 TELEMETRY_LOG_ERR("Failed to open socket");
389 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
391 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
395 addr.sun_family = AF_UNIX;
396 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
397 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
400 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
402 TELEMETRY_LOG_ERR("Socket binding error");
409 if (close(telemetry->server_fd) < 0) {
410 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
417 int32_t __rte_experimental
422 const char *telemetry_ctrl_thread = "telemetry";
424 if (static_telemetry) {
425 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
429 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
430 if (static_telemetry == NULL) {
431 TELEMETRY_LOG_ERR("Memory could not be allocated");
435 static_telemetry->socket_id = rte_socket_id();
436 rte_metrics_init(static_telemetry->socket_id);
438 ret = pthread_attr_init(&attr);
440 TELEMETRY_LOG_ERR("Pthread attribute init failed");
444 ret = rte_telemetry_create_socket(static_telemetry);
446 ret = rte_telemetry_cleanup();
448 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
451 TAILQ_INIT(&static_telemetry->client_list_head);
453 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
454 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
455 (void *)static_telemetry);
456 static_telemetry->thread_status = 1;
459 ret = rte_telemetry_cleanup();
461 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
469 rte_telemetry_client_cleanup(struct telemetry_client *client)
473 ret = close(client->fd);
474 free(client->file_path);
478 TELEMETRY_LOG_ERR("Close client socket failed");
485 int32_t __rte_experimental
486 rte_telemetry_cleanup(void)
489 struct telemetry_impl *telemetry = static_telemetry;
490 telemetry_client *client, *temp_client;
492 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
494 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
495 ret = rte_telemetry_client_cleanup(client);
497 TELEMETRY_LOG_ERR("Client cleanup failed");
502 ret = close(telemetry->server_fd);
504 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
509 telemetry->thread_status = 0;
510 pthread_join(telemetry->thread_id, NULL);
512 static_telemetry = NULL;
518 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
519 const char *client_path)
522 telemetry_client *client, *temp_client;
524 if (telemetry == NULL) {
525 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
529 if (client_path == NULL) {
530 TELEMETRY_LOG_ERR("Invalid client path");
534 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
535 TELEMETRY_LOG_ERR("There are no clients currently registered");
539 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
541 if (strcmp(client_path, client->file_path) == 0) {
542 TAILQ_REMOVE(&telemetry->client_list_head, client,
544 ret = rte_telemetry_client_cleanup(client);
547 TELEMETRY_LOG_ERR("Client cleanup failed");
555 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
559 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
561 TELEMETRY_LOG_ERR("Could not send error");
566 rte_telemetry_register_client(struct telemetry_impl *telemetry,
567 const char *client_path)
570 struct sockaddr_un addrs;
572 if (telemetry == NULL) {
573 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
577 if (client_path == NULL) {
578 TELEMETRY_LOG_ERR("Invalid client path");
582 telemetry_client *client;
583 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
584 if (strcmp(client_path, client->file_path) == 0) {
585 TELEMETRY_LOG_WARN("'%s' already registered",
591 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
593 TELEMETRY_LOG_ERR("Client socket error");
597 ret = rte_telemetry_set_socket_nonblock(fd);
599 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
603 addrs.sun_family = AF_UNIX;
604 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
605 telemetry_client *new_client = malloc(sizeof(telemetry_client));
606 new_client->file_path = strdup(client_path);
609 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
610 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
612 ret = rte_telemetry_client_cleanup(new_client);
614 TELEMETRY_LOG_ERR("Client cleanup failed");
620 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
626 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
630 json_t *root = json_loads(buf, 0, &error);
633 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
636 } else if (!json_is_object(root)) {
637 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
641 json_t *action = json_object_get(root, "action");
642 if (action == NULL) {
643 TELEMETRY_LOG_WARN("Request does not have action field");
645 } else if (!json_is_integer(action)) {
646 TELEMETRY_LOG_WARN("Action value is not an integer");
650 json_t *command = json_object_get(root, "command");
651 if (command == NULL) {
652 TELEMETRY_LOG_WARN("Request does not have command field");
654 } else if (!json_is_string(command)) {
655 TELEMETRY_LOG_WARN("Command value is not a string");
659 action_int = json_integer_value(action);
660 if (action_int != ACTION_POST) {
661 TELEMETRY_LOG_WARN("Invalid action code");
665 if (strcmp(json_string_value(command), "clients") != 0) {
666 TELEMETRY_LOG_WARN("Invalid command");
670 json_t *data = json_object_get(root, "data");
672 TELEMETRY_LOG_WARN("Request does not have data field");
676 json_t *client_path = json_object_get(data, "client_path");
677 if (client_path == NULL) {
678 TELEMETRY_LOG_WARN("Request does not have client_path field");
682 if (!json_is_string(client_path)) {
683 TELEMETRY_LOG_WARN("Client_path value is not a string");
687 ret = rte_telemetry_register_client(telemetry,
688 json_string_value(client_path));
690 TELEMETRY_LOG_ERR("Could not register client");
691 telemetry->register_fail_count++;
698 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
703 int telemetry_log_level;
705 static struct rte_option option = {
706 .opt_str = "--telemetry",
707 .cb = &rte_telemetry_init,
711 RTE_INIT(rte_telemetry_register)
713 telemetry_log_level = rte_log_register("lib.telemetry");
714 if (telemetry_log_level >= 0)
715 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
717 rte_option_register(&option);