From patchwork Sat Oct 27 09:17:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Van Haaren, Harry" X-Patchwork-Id: 47513 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id DA2525681; Sat, 27 Oct 2018 11:19:46 +0200 (CEST) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by dpdk.org (Postfix) with ESMTP id 8320A4C8C for ; Sat, 27 Oct 2018 11:19:33 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Oct 2018 02:19:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,431,1534834800"; d="scan'208";a="84903877" Received: from silpixa00399779.ir.intel.com (HELO silpixa00399779.ger.corp.intel.com) ([10.237.223.188]) by orsmga008.jf.intel.com with ESMTP; 27 Oct 2018 02:19:30 -0700 From: Harry van Haaren To: dev@dpdk.org Cc: thomas@monjalon.net, bruce.richardson@intel.com, stephen@networkplumber.org, gaetan.rivet@6wind.com, shreyansh.jain@nxp.com, mattias.ronnblom@ericsson.com, Ciara Power , Brian Archbold , Kevin Laatz Date: Sat, 27 Oct 2018 10:17:42 +0100 Message-Id: <20181027091750.17254-5-harry.van.haaren@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181027091750.17254-1-harry.van.haaren@intel.com> References: <20181026235933.79779-1-harry.van.haaren@intel.com> <20181027091750.17254-1-harry.van.haaren@intel.com> Subject: [dpdk-dev] [PATCH v10 04/12] telemetry: add initial connection socket X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Ciara Power This patch adds the telemetry UNIX socket. It is used to allow connections from external clients. On the initial connection from a client, ethdev stats are registered in the metrics library, to allow for their retrieval at a later stage. Signed-off-by: Ciara Power Signed-off-by: Brian Archbold Signed-off-by: Kevin Laatz Acked-by: Harry van Haaren --- lib/librte_telemetry/rte_telemetry.c | 225 ++++++++++++++++++ lib/librte_telemetry/rte_telemetry_internal.h | 4 + 2 files changed, 229 insertions(+) diff --git a/lib/librte_telemetry/rte_telemetry.c b/lib/librte_telemetry/rte_telemetry.c index 7f4ad0342..e9b3330ca 100644 --- a/lib/librte_telemetry/rte_telemetry.c +++ b/lib/librte_telemetry/rte_telemetry.c @@ -3,23 +3,163 @@ */ #include +#include #include +#include +#include #include #include #include #include +#include #include "rte_telemetry.h" #include "rte_telemetry_internal.h" +#define BUF_SIZE 1024 #define SLEEP_TIME 10 static telemetry_impl *static_telemetry; +static void +rte_telemetry_get_runtime_dir(char *socket_path, size_t size) +{ + snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir()); +} + +int32_t +rte_telemetry_is_port_active(int port_id) +{ + int ret; + + ret = rte_eth_find_next(port_id); + if (ret == port_id) + return 1; + + TELEMETRY_LOG_ERR("port_id: %d is invalid, not active", + port_id); + return 0; +} + +static int32_t +rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id) +{ + int ret, num_xstats, ret_val, i; + struct rte_eth_xstat *eth_xstats = NULL; + struct rte_eth_xstat_name *eth_xstats_names = NULL; + + if (!rte_eth_dev_is_valid_port(port_id)) { + TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id); + return -EINVAL; + } + + num_xstats = rte_eth_xstats_get(port_id, NULL, 0); + if (num_xstats < 0) { + TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", + port_id, num_xstats); + return -EPERM; + } + + eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats); + if (eth_xstats == NULL) { + TELEMETRY_LOG_ERR("Failed to malloc memory for xstats"); + return -ENOMEM; + } + + ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats); + const char *xstats_names[num_xstats]; + eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats); + if (ret < 0 || ret > num_xstats) { + TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d", + port_id, num_xstats, ret); + ret_val = -EPERM; + goto free_xstats; + } + + if (eth_xstats_names == NULL) { + TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names"); + ret_val = -ENOMEM; + goto free_xstats; + } + + ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats); + if (ret < 0 || ret > num_xstats) { + TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d", + port_id, num_xstats, ret); + ret_val = -EPERM; + goto free_xstats; + } + + for (i = 0; i < num_xstats; i++) + xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name; + + ret_val = rte_metrics_reg_names(xstats_names, num_xstats); + if (ret_val < 0) { + TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered"); + ret_val = -1; + goto free_xstats; + } + + goto free_xstats; + +free_xstats: + free(eth_xstats); + free(eth_xstats_names); + return ret_val; +} + +static int32_t +rte_telemetry_initial_accept(struct telemetry_impl *telemetry) +{ + uint16_t pid; + + RTE_ETH_FOREACH_DEV(pid) { + telemetry->reg_index = + rte_telemetry_reg_ethdev_to_metrics(pid); + break; + } + + if (telemetry->reg_index < 0) { + TELEMETRY_LOG_ERR("Failed to register ethdev metrics"); + return -1; + } + + telemetry->metrics_register_done = 1; + + return 0; +} + +static int32_t +rte_telemetry_accept_new_client(struct telemetry_impl *telemetry) +{ + int ret; + + if (telemetry->accept_fd <= 0) { + ret = listen(telemetry->server_fd, 1); + if (ret < 0) { + TELEMETRY_LOG_ERR("Listening error with server fd"); + return -1; + } + telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL); + + if (telemetry->accept_fd >= 0 && + telemetry->metrics_register_done == 0) { + ret = rte_telemetry_initial_accept(telemetry); + if (ret < 0) { + TELEMETRY_LOG_ERR("Failed to run initial configurations/tests"); + return -1; + } + } + } + + return 0; +} + static int32_t rte_telemetry_run(void *userdata) { + int ret; struct telemetry_impl *telemetry = userdata; if (telemetry == NULL) { @@ -27,6 +167,12 @@ rte_telemetry_run(void *userdata) return -1; } + ret = rte_telemetry_accept_new_client(telemetry); + if (ret < 0) { + TELEMETRY_LOG_ERR("Accept and read new client failed"); + return -1; + } + return 0; } @@ -50,6 +196,67 @@ static void pthread_exit(0); } +static int32_t +rte_telemetry_set_socket_nonblock(int fd) +{ + int flags; + + if (fd < 0) { + TELEMETRY_LOG_ERR("Invalid fd provided"); + return -1; + } + + flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + flags = 0; + + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} + +static int32_t +rte_telemetry_create_socket(struct telemetry_impl *telemetry) +{ + int ret; + struct sockaddr_un addr; + char socket_path[BUF_SIZE]; + + if (telemetry == NULL) + return -1; + + telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + if (telemetry->server_fd == -1) { + TELEMETRY_LOG_ERR("Failed to open socket"); + return -1; + } + + ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd); + if (ret < 0) { + TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK"); + goto close_socket; + } + + addr.sun_family = AF_UNIX; + rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path)); + strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path)); + unlink(socket_path); + + if (bind(telemetry->server_fd, (struct sockaddr *)&addr, + sizeof(addr)) < 0) { + TELEMETRY_LOG_ERR("Socket binding error"); + goto close_socket; + } + + return 0; + +close_socket: + if (close(telemetry->server_fd) < 0) { + TELEMETRY_LOG_ERR("Close TELEMETRY socket failed"); + return -EPERM; + } + + return -1; +} + int32_t __rte_experimental rte_telemetry_init() { @@ -77,6 +284,14 @@ rte_telemetry_init() return -EPERM; } + ret = rte_telemetry_create_socket(static_telemetry); + if (ret < 0) { + ret = rte_telemetry_cleanup(); + if (ret < 0) + TELEMETRY_LOG_ERR("TELEMETRY cleanup failed"); + return -EPERM; + } + ret = rte_ctrl_thread_create(&static_telemetry->thread_id, telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func, (void *)static_telemetry); @@ -95,11 +310,21 @@ rte_telemetry_init() int32_t __rte_experimental rte_telemetry_cleanup(void) { + int ret; struct telemetry_impl *telemetry = static_telemetry; + + ret = close(telemetry->server_fd); + if (ret < 0) { + TELEMETRY_LOG_ERR("Close TELEMETRY socket failed"); + free(telemetry); + return -EPERM; + } + telemetry->thread_status = 0; pthread_join(telemetry->thread_id, NULL); free(telemetry); static_telemetry = NULL; + return 0; } diff --git a/lib/librte_telemetry/rte_telemetry_internal.h b/lib/librte_telemetry/rte_telemetry_internal.h index 4e810a84c..569d56ab8 100644 --- a/lib/librte_telemetry/rte_telemetry_internal.h +++ b/lib/librte_telemetry/rte_telemetry_internal.h @@ -24,9 +24,13 @@ extern int telemetry_log_level; TELEMETRY_LOG(INFO, fmt, ## args) typedef struct telemetry_impl { + int accept_fd; + int server_fd; pthread_t thread_id; int thread_status; uint32_t socket_id; + int reg_index; + int metrics_register_done; } telemetry_impl; #endif