1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <sys/socket.h>
12 #include <rte_ethdev.h>
13 #include <rte_metrics.h>
14 #include <rte_option.h>
15 #include <rte_string_fns.h>
17 #include "rte_telemetry.h"
18 #include "rte_telemetry_internal.h"
23 static telemetry_impl *static_telemetry;
26 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
28 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
32 rte_telemetry_is_port_active(int port_id)
36 ret = rte_eth_find_next(port_id);
40 TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
46 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
48 int ret, num_xstats, ret_val, i;
49 struct rte_eth_xstat *eth_xstats = NULL;
50 struct rte_eth_xstat_name *eth_xstats_names = NULL;
52 if (!rte_eth_dev_is_valid_port(port_id)) {
53 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
57 num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
59 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
64 eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
65 if (eth_xstats == NULL) {
66 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
70 ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
71 const char *xstats_names[num_xstats];
72 eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
73 if (ret < 0 || ret > num_xstats) {
74 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
75 port_id, num_xstats, ret);
80 if (eth_xstats_names == NULL) {
81 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
86 ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
87 if (ret < 0 || ret > num_xstats) {
88 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
89 port_id, num_xstats, ret);
94 for (i = 0; i < num_xstats; i++)
95 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
97 ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
99 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
108 free(eth_xstats_names);
113 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
117 RTE_ETH_FOREACH_DEV(pid) {
118 telemetry->reg_index =
119 rte_telemetry_reg_ethdev_to_metrics(pid);
123 if (telemetry->reg_index < 0) {
124 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
128 telemetry->metrics_register_done = 1;
134 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
138 if (telemetry->accept_fd <= 0) {
139 ret = listen(telemetry->server_fd, 1);
141 TELEMETRY_LOG_ERR("Listening error with server fd");
144 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
146 if (telemetry->accept_fd >= 0 &&
147 telemetry->metrics_register_done == 0) {
148 ret = rte_telemetry_initial_accept(telemetry);
150 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
160 rte_telemetry_run(void *userdata)
163 struct telemetry_impl *telemetry = userdata;
165 if (telemetry == NULL) {
166 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
170 ret = rte_telemetry_accept_new_client(telemetry);
172 TELEMETRY_LOG_ERR("Accept and read new client failed");
180 *rte_telemetry_run_thread_func(void *userdata)
183 struct telemetry_impl *telemetry = userdata;
185 if (telemetry == NULL) {
186 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
190 while (telemetry->thread_status) {
191 rte_telemetry_run(telemetry);
192 ret = usleep(SLEEP_TIME);
194 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
200 rte_telemetry_set_socket_nonblock(int fd)
205 TELEMETRY_LOG_ERR("Invalid fd provided");
209 flags = fcntl(fd, F_GETFL, 0);
213 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
217 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
220 struct sockaddr_un addr;
221 char socket_path[BUF_SIZE];
223 if (telemetry == NULL)
226 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
227 if (telemetry->server_fd == -1) {
228 TELEMETRY_LOG_ERR("Failed to open socket");
232 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
234 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
238 addr.sun_family = AF_UNIX;
239 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
240 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
243 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
245 TELEMETRY_LOG_ERR("Socket binding error");
252 if (close(telemetry->server_fd) < 0) {
253 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
260 int32_t __rte_experimental
265 const char *telemetry_ctrl_thread = "telemetry";
267 if (static_telemetry) {
268 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
272 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
273 if (static_telemetry == NULL) {
274 TELEMETRY_LOG_ERR("Memory could not be allocated");
278 static_telemetry->socket_id = rte_socket_id();
279 rte_metrics_init(static_telemetry->socket_id);
281 ret = pthread_attr_init(&attr);
283 TELEMETRY_LOG_ERR("Pthread attribute init failed");
287 ret = rte_telemetry_create_socket(static_telemetry);
289 ret = rte_telemetry_cleanup();
291 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
295 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
296 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
297 (void *)static_telemetry);
298 static_telemetry->thread_status = 1;
301 ret = rte_telemetry_cleanup();
303 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
310 int32_t __rte_experimental
311 rte_telemetry_cleanup(void)
314 struct telemetry_impl *telemetry = static_telemetry;
316 ret = close(telemetry->server_fd);
318 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
323 telemetry->thread_status = 0;
324 pthread_join(telemetry->thread_id, NULL);
326 static_telemetry = NULL;
331 int telemetry_log_level;
333 static struct rte_option option = {
334 .opt_str = "--telemetry",
335 .cb = &rte_telemetry_init,
339 RTE_INIT(rte_telemetry_register)
341 telemetry_log_level = rte_log_register("lib.telemetry");
342 if (telemetry_log_level >= 0)
343 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
345 rte_option_register(&option);