X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdpdkstat.c;h=6b057f214e3276412dd56a3aa402048c6bb6d369;hb=77ca1a45bab2f6adf9301723d0db68e5813a6d98;hp=e5aafdeb822930cb34373272c2fdc6e8558945e6;hpb=b9c8aeaa70b15792a10a694fcb8e4304a8330715;p=collectd.git diff --git a/src/dpdkstat.c b/src/dpdkstat.c index e5aafdeb..59ab9760 100644 --- a/src/dpdkstat.c +++ b/src/dpdkstat.c @@ -1,18 +1,18 @@ -/*- +/* * collectd - src/dpdkstat.c * MIT License * * Copyright(c) 2016 Intel Corporation. All rights reserved. * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,661 +25,499 @@ * Authors: * Maryam Tahhan * Harry van Haaren + * Taras Chornyi + * Serhiy Pshyk + * Krzysztof Matczak */ #include "collectd.h" -#include "common.h" /* auxiliary functions */ -#include "plugin.h" /* plugin_register_*, plugin_dispatch_values */ -#include "utils_time.h" -#include -#include -#include -#include -#include +#include "common.h" +#include "utils_dpdk.h" #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define DPDKSTAT_MAX_BUFFER_SIZE (4096*4) -#define DPDK_SHM_NAME "dpdk_collectd_stats_shm" -#define REINIT_SHM 1 -#define RESET 1 -#define NO_RESET 0 - -enum DPDK_HELPER_ACTION { - DPDK_HELPER_ACTION_COUNT_STATS, - DPDK_HELPER_ACTION_SEND_STATS, -}; -enum DPDK_HELPER_STATUS { - DPDK_HELPER_NOT_INITIALIZED = 0, - DPDK_HELPER_WAITING_ON_PRIMARY, - DPDK_HELPER_INITIALIZING_EAL, - DPDK_HELPER_ALIVE_SENDING_STATS, - DPDK_HELPER_GRACEFUL_QUIT, -}; +#define DPDK_STATS_PLUGIN "dpdkstat" +#define DPDK_STATS_NAME "dpdk_collectd_stats" -struct dpdk_config_s { - /* General DPDK params */ - char coremask[DATA_MAX_NAME_LEN]; - char memory_channels[DATA_MAX_NAME_LEN]; - char socket_memory[DATA_MAX_NAME_LEN]; - char process_type[DATA_MAX_NAME_LEN]; - char file_prefix[DATA_MAX_NAME_LEN]; +#define DPDK_STATS_TRACE() \ + DEBUG("%s:%s:%d pid=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__, getpid()) + +struct dpdk_stats_config_s { cdtime_t interval; - uint32_t eal_initialized; uint32_t enabled_port_mask; - uint32_t eal_argc; - /* Helper info */ - int collectd_reinit_shm; - pid_t helper_pid; - sem_t sema_helper_get_stats; - sem_t sema_stats_in_shm; - int helper_pipes[2]; - enum DPDK_HELPER_STATUS helper_status; - enum DPDK_HELPER_ACTION helper_action; - /* xstats info */ - uint32_t num_ports; - uint32_t num_xstats; + char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN]; +}; +typedef struct dpdk_stats_config_s dpdk_stats_config_t; + +#define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16) + +#if RTE_VERSION < RTE_VERSION_16_07 +#define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value +#define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xstats[index].name +#define DPDK_STATS_CTX_GET_XSTAT_SIZE sizeof(struct rte_eth_xstats) +#define DPDK_STATS_CTX_INIT(ctx) \ + do { \ + ctx->xstats = (struct rte_eth_xstats *)&ctx->raw_data[0]; \ + } while (0) +#else +#define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value +#define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xnames[index].name +#define DPDK_STATS_CTX_GET_XSTAT_SIZE \ + (sizeof(struct rte_eth_xstat) + sizeof(struct rte_eth_xstat_name)) +#define DPDK_STATS_CTX_INIT(ctx) \ + do { \ + ctx->xstats = (struct rte_eth_xstat *)&ctx->raw_data[0]; \ + ctx->xnames = \ + (struct rte_eth_xstat_name *)&ctx \ + ->raw_data[ctx->stats_count * sizeof(struct rte_eth_xstat)]; \ + } while (0) +#endif + +struct dpdk_stats_ctx_s { + dpdk_stats_config_t config; + uint32_t stats_count; + uint32_t ports_count; cdtime_t port_read_time[RTE_MAX_ETHPORTS]; - uint32_t num_stats_in_port[RTE_MAX_ETHPORTS]; - struct rte_eth_link link_status[RTE_MAX_ETHPORTS]; - struct rte_eth_xstats xstats; - /* rte_eth_xstats from here on until the end of the SHM */ + uint32_t port_stats_count[RTE_MAX_ETHPORTS]; +#if RTE_VERSION < RTE_VERSION_16_07 + struct rte_eth_xstats *xstats; +#else + struct rte_eth_xstat *xstats; + struct rte_eth_xstat_name *xnames; +#endif + char raw_data[]; }; -typedef struct dpdk_config_s dpdk_config_t; - -static int g_configured; -static dpdk_config_t *g_configuration; - -static void dpdk_config_init_default(void); -static int dpdk_config(oconfig_item_t *ci); -static int dpdk_helper_init_eal(void); -static int dpdk_helper_run(void); -static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action); -static int dpdk_init(void); -static int dpdk_read(user_data_t *ud); -static int dpdk_shm_cleanup(void); -static int dpdk_shm_init(size_t size); - -/* Write the default configuration to the g_configuration instances */ -static void dpdk_config_init_default(void) -{ - g_configuration->interval = plugin_get_interval(); - WARNING("dpdkstat: No time interval was configured, default value %lu ms is set\n", - CDTIME_T_TO_MS(g_configuration->interval)); - g_configuration->enabled_port_mask = 0; - g_configuration->eal_argc = 2; - g_configuration->eal_initialized = 0; - ssnprintf(g_configuration->coremask, DATA_MAX_NAME_LEN, "%s", "0xf"); - ssnprintf(g_configuration->memory_channels, DATA_MAX_NAME_LEN, "%s", "1"); - ssnprintf(g_configuration->process_type, DATA_MAX_NAME_LEN, "%s", "secondary"); - ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN, "%s", - "/var/run/.rte_config"); -} +typedef struct dpdk_stats_ctx_s dpdk_stats_ctx_t; -static int dpdk_config(oconfig_item_t *ci) -{ - int i = 0; +typedef enum { + DPDK_STAT_STATE_OKAY = 0, + DPDK_STAT_STATE_CFG_ERR, +} dpdk_stat_cfg_status; - /* Initialize a POSIX SHared Memory (SHM) object. */ - int err = dpdk_shm_init(sizeof(dpdk_config_t)); - if (err) { - DEBUG("dpdkstat: error in shm_init, %s", strerror(errno)); - return -1; - } +#define DPDK_STATS_CTX_GET(a) ((dpdk_stats_ctx_t *)dpdk_helper_priv_get(a)) - /* Set defaults for config, overwritten by loop if config item exists */ - dpdk_config_init_default(); +dpdk_helper_ctx_t *g_hc = NULL; +static char g_shm_name[DATA_MAX_NAME_LEN] = DPDK_STATS_NAME; +static dpdk_stat_cfg_status g_state = DPDK_STAT_STATE_OKAY; - for (i = 0; i < ci->children_num; i++) { - oconfig_item_t *child = ci->children + i; +static int dpdk_stats_reinit_helper(); +static void dpdk_stats_default_config(void) { + dpdk_stats_ctx_t *ec = DPDK_STATS_CTX_GET(g_hc); - if (strcasecmp("Interval", child->key) == 0) { - g_configuration->interval = - DOUBLE_TO_CDTIME_T (child->values[0].value.number); - DEBUG("dpdkstat: Plugin Read Interval %lu milliseconds\n", - CDTIME_T_TO_MS(g_configuration->interval)); - } else if (strcasecmp("Coremask", child->key) == 0) { - ssnprintf(g_configuration->coremask, DATA_MAX_NAME_LEN, "%s", - child->values[0].value.string); - DEBUG("dpdkstat:COREMASK %s \n", g_configuration->coremask); - g_configuration->eal_argc+=1; - } else if (strcasecmp("MemoryChannels", child->key) == 0) { - ssnprintf(g_configuration->memory_channels, DATA_MAX_NAME_LEN, "%s", - child->values[0].value.string); - DEBUG("dpdkstat:Memory Channels %s \n", g_configuration->memory_channels); - g_configuration->eal_argc+=1; - } else if (strcasecmp("SocketMemory", child->key) == 0) { - ssnprintf(g_configuration->socket_memory, DATA_MAX_NAME_LEN, "%s", - child->values[0].value.string); - DEBUG("dpdkstat: socket mem %s \n", g_configuration->socket_memory); - g_configuration->eal_argc+=1; - } else if (strcasecmp("ProcessType", child->key) == 0) { - ssnprintf(g_configuration->process_type, DATA_MAX_NAME_LEN, "%s", - child->values[0].value.string); - DEBUG("dpdkstat: proc type %s \n", g_configuration->process_type); - g_configuration->eal_argc+=1; - } else if (strcasecmp("FilePrefix", child->key) == 0) { - ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN, "/var/run/.%s_config", - child->values[0].value.string); - DEBUG("dpdkstat: file prefix %s \n", g_configuration->file_prefix); - if (strcasecmp(g_configuration->file_prefix, "/var/run/.rte_config") != 0) { - g_configuration->eal_argc+=1; - } - } else { - WARNING ("dpdkstat: The config option \"%s\" is unknown.", - child->key); - } - } /* End for (i = 0; i < ci->children_num; i++)*/ - g_configured = 1; /* Bypass configuration in dpdk_shm_init(). */ - - return 0; + ec->config.interval = plugin_get_interval(); + for (int i = 0; i < RTE_MAX_ETHPORTS; i++) { + ec->config.port_name[i][0] = 0; + } + /* Enable all ports by default */ + ec->config.enabled_port_mask = ~0; } -/* Initialize SHared Memory (SHM) for config and helper process */ -static int dpdk_shm_init(size_t size) -{ - /* - * Check if SHM is already configured: when config items are provided, the - * config function initializes SHM. If there is no config, then init() will - * just return. - */ - if(g_configuration) - return 0; +static int dpdk_stats_preinit(void) { + DPDK_STATS_TRACE(); - /* Create and open a new object, or open an existing object. */ - int fd = shm_open(DPDK_SHM_NAME, O_CREAT | O_TRUNC | O_RDWR, 0666); - if (fd < 0) { - WARNING("dpdkstat:Failed to open %s as SHM:%s\n", DPDK_SHM_NAME, - strerror(errno)); - goto fail; + if (g_hc != NULL) { + /* already initialized if config callback was called before init callback */ + DEBUG("dpdk_stats_preinit: helper already initialized"); + return 0; } - /* Set the size of the shared memory object. */ - int ret = ftruncate(fd, size); + + int ret = dpdk_helper_init(g_shm_name, sizeof(dpdk_stats_ctx_t), &g_hc); if (ret != 0) { - WARNING("dpdkstat:Failed to resize SHM:%s\n", strerror(errno)); - goto fail_close; - } - /* Map the shared memory object into this process' virtual address space. */ - g_configuration = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (g_configuration == MAP_FAILED) { - WARNING("dpdkstat:Failed to mmap SHM:%s\n", strerror(errno)); - goto fail_close; + ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN, + g_shm_name, STRERRNO); + return ret; } - /* - * Close the file descriptor, the shared memory object still exists - * and can only be removed by calling shm_unlink(). - */ - close(fd); - - /* Initialize g_configuration. */ - memset(g_configuration, 0, size); - - /* Initialize the semaphores for SHM use */ - int err = sem_init(&g_configuration->sema_helper_get_stats, 1, 0); - if(err) { - ERROR("dpdkstat semaphore init failed: %s\n", strerror(errno)); - goto fail_close; + + dpdk_stats_default_config(); + return ret; +} + +static int dpdk_stats_config(oconfig_item_t *ci) { + DPDK_STATS_TRACE(); + + int ret = dpdk_stats_preinit(); + if (ret) { + g_state = DPDK_STAT_STATE_CFG_ERR; + return 0; } - err = sem_init(&g_configuration->sema_stats_in_shm, 1, 0); - if(err) { - ERROR("dpdkstat semaphore init failed: %s\n", strerror(errno)); - goto fail_close; + + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc); + + for (int i = 0; i < ci->children_num; i++) { + oconfig_item_t *child = ci->children + i; + + if (strcasecmp("EnabledPortMask", child->key) == 0) + ret = cf_util_get_int(child, (int *)&ctx->config.enabled_port_mask); + else if (strcasecmp("SharedMemObj", child->key) == 0) { + ret = cf_util_get_string_buffer(child, g_shm_name, sizeof(g_shm_name)); + if (ret == 0) + ret = dpdk_stats_reinit_helper(); + } else if (strcasecmp("EAL", child->key) == 0) + ret = dpdk_helper_eal_config_parse(g_hc, child); + else if (strcasecmp("PortName", child->key) != 0) { + ERROR(DPDK_STATS_PLUGIN ": unrecognized configuration option %s", + child->key); + ret = -1; + } + + if (ret != 0) { + g_state = DPDK_STAT_STATE_CFG_ERR; + return 0; + } } - return 0; + DEBUG(DPDK_STATS_PLUGIN ": Enabled Port Mask 0x%X", + ctx->config.enabled_port_mask); + DEBUG(DPDK_STATS_PLUGIN ": Shared memory object %s", g_shm_name); -fail_close: - close(fd); -fail: - /* Reset to zero, as it was set to MAP_FAILED aka: (void *)-1. Avoid - * an issue if collectd attempts to run this plugin failure. - */ - g_configuration = 0; - return -1; -} + int port_num = 0; -static int dpdk_re_init_shm() -{ - dpdk_config_t temp_config; - memcpy(&temp_config, g_configuration, sizeof(dpdk_config_t)); - DEBUG("dpdkstat: %s: ports %d, xstats %d\n", __func__, temp_config.num_ports, - temp_config.num_xstats); + /* parse port names after EnabledPortMask was parsed */ + for (int i = 0; i < ci->children_num; i++) { + oconfig_item_t *child = ci->children + i; - int shm_xstats_size = sizeof(dpdk_config_t) + (sizeof(struct rte_eth_xstats) * - g_configuration->num_xstats); - DEBUG("=== SHM new size for %d xstats\n", g_configuration->num_xstats); + if (strcasecmp("PortName", child->key) == 0) { - int err = dpdk_shm_cleanup(); - if (err) - ERROR("dpdkstat: Error in shm_cleanup in %s\n", __func__); + while (!(ctx->config.enabled_port_mask & (1 << port_num))) + port_num++; - err = dpdk_shm_init(shm_xstats_size); - if (err) - ERROR("dpdkstat: Error in shm_init in %s\n", __func__); + if (cf_util_get_string_buffer(child, ctx->config.port_name[port_num], + sizeof(ctx->config.port_name[port_num]))) { + g_state = DPDK_STAT_STATE_CFG_ERR; + return 0; + } - /* If the XML config() function has been run, dont re-initialize defaults */ - if(!g_configured) - dpdk_config_init_default(); + DEBUG(DPDK_STATS_PLUGIN ": Port %d Name: %s", port_num, + ctx->config.port_name[port_num]); - memcpy(g_configuration, &temp_config, sizeof(dpdk_config_t)); - g_configuration->collectd_reinit_shm = 0; + port_num++; + } + } return 0; } -static int dpdk_init (void) -{ - int err = dpdk_shm_init(sizeof(dpdk_config_t)); - if (err) - ERROR("dpdkstat: %s : error %d in shm_init()", __func__, err); +static int dpdk_helper_stats_get(dpdk_helper_ctx_t *phc) { + int len = 0; + int ret = 0; + int stats = 0; + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc); - /* If the XML config() function has been run, dont re-initialize defaults */ - if(!g_configured) { - dpdk_config_init_default(); + /* get stats from DPDK */ + for (uint8_t i = 0; i < ctx->ports_count; i++) { + if (!(ctx->config.enabled_port_mask & (1 << i))) + continue; + + ctx->port_read_time[i] = cdtime(); + /* Store available stats array length for port */ + len = ctx->port_stats_count[i]; + + ret = rte_eth_xstats_get(i, &ctx->xstats[stats], len); + if (ret < 0 || ret > len) { + DPDK_CHILD_LOG(DPDK_STATS_PLUGIN + ": Error reading stats (port=%d; len=%d, ret=%d)\n", + i, len, ret); + ctx->port_stats_count[i] = 0; + return -1; + } +#if RTE_VERSION >= RTE_VERSION_16_07 + ret = rte_eth_xstats_get_names(i, &ctx->xnames[stats], len); + if (ret < 0 || ret > len) { + DPDK_CHILD_LOG(DPDK_STATS_PLUGIN + ": Error reading stat names (port=%d; len=%d ret=%d)\n", + i, len, ret); + ctx->port_stats_count[i] = 0; + return -1; + } +#endif + ctx->port_stats_count[i] = ret; + stats += ctx->port_stats_count[i]; } - plugin_register_complex_read (NULL, "dpdkstat", dpdk_read, - g_configuration->interval, NULL); + assert(stats <= ctx->stats_count); return 0; } -static int dpdk_helper_exit(int reset) -{ - g_configuration->helper_status = DPDK_HELPER_GRACEFUL_QUIT; - if (reset) { - g_configuration->eal_initialized = 0; - g_configuration->num_ports = 0; - memset(&g_configuration->xstats, 0, g_configuration->num_xstats* sizeof(struct rte_eth_xstats)); - g_configuration->num_xstats = 0; - int i = 0; - for (; i < RTE_MAX_ETHPORTS; i++) - g_configuration->num_stats_in_port[i] = 0; - } - close(g_configuration->helper_pipes[1]); - int err = kill(g_configuration->helper_pid, SIGKILL); - if (err) { - ERROR("dpdkstat: error sending kill to helper: %s\n", strerror(errno)); +static int dpdk_helper_stats_count_get(dpdk_helper_ctx_t *phc) { + uint8_t ports = dpdk_helper_eth_dev_count(); + if (ports == 0) + return -ENODEV; + + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc); + ctx->ports_count = ports; + + int len = 0; + int stats_count = 0; + for (int i = 0; i < ports; i++) { + if (!(ctx->config.enabled_port_mask & (1 << i))) + continue; +#if RTE_VERSION >= RTE_VERSION_16_07 + len = rte_eth_xstats_get_names(i, NULL, 0); +#else + len = rte_eth_xstats_get(i, NULL, 0); +#endif + if (len < 0) { + DPDK_CHILD_LOG("%s: Cannot get stats count\n", DPDK_STATS_PLUGIN); + return -1; + } + ctx->port_stats_count[i] = len; + stats_count += len; } - return 0; + DPDK_CHILD_LOG("%s:%s:%d stats_count=%d\n", DPDK_STATS_PLUGIN, __FUNCTION__, + __LINE__, stats_count); + + return stats_count; } -static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action) -{ - g_configuration->eal_initialized = 0; - g_configuration->helper_action = action; - /* - * Create a pipe for helper stdout back to collectd. This is necessary for - * logging EAL failures, as rte_eal_init() calls rte_panic(). - */ - if (g_configuration->helper_pipes[1]) { - DEBUG("dpdkstat: collectd closing helper pipe %d\n", - g_configuration->helper_pipes[1]); - } else { - DEBUG("dpdkstat: collectd helper pipe %d, not closing\n", - g_configuration->helper_pipes[1]); - } - if(pipe(g_configuration->helper_pipes) != 0) { - DEBUG("dpdkstat: Could not create helper pipe: %s\n", strerror(errno)); - return -1; +static int dpdk_stats_get_size(dpdk_helper_ctx_t *phc) { + return dpdk_helper_data_size_get(phc) - sizeof(dpdk_stats_ctx_t); +} + +int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) { + /* this function is called from helper context */ + + if (phc == NULL) { + DPDK_CHILD_LOG("%s: Invalid argument(phc)\n", DPDK_STATS_PLUGIN); + return -EINVAL; } - int pipe0_flags = fcntl(g_configuration->helper_pipes[0], F_GETFL, 0); - int pipe1_flags = fcntl(g_configuration->helper_pipes[1], F_GETFL, 0); - if (pipe0_flags == -1 || pipe1_flags == -1) { - ERROR("dpdkstat: error setting up pipe flags: %s\n", strerror(errno)); + if (cmd != DPDK_CMD_GET_STATS) { + DPDK_CHILD_LOG("%s: Unknown command (cmd=%d)\n", DPDK_STATS_PLUGIN, cmd); + return -EINVAL; } - int pipe0_err = fcntl(g_configuration->helper_pipes[0], F_SETFL, pipe1_flags - | O_NONBLOCK); - int pipe1_err = fcntl(g_configuration->helper_pipes[1], F_SETFL, pipe0_flags - | O_NONBLOCK); - if (pipe0_err == -1 || pipe1_err == -1) { - ERROR("dpdkstat: error setting up pipes: %s\n", strerror(errno)); + + int stats_count = dpdk_helper_stats_count_get(phc); + if (stats_count < 0) { + return stats_count; } - pid_t pid = fork(); - if (pid > 0) { - close(g_configuration->helper_pipes[1]); - g_configuration->helper_pid = pid; - DEBUG("dpdkstat: helper pid %u\n", g_configuration->helper_pid); - /* Kick helper once its alive to have it start processing */ - sem_post(&g_configuration->sema_helper_get_stats); - } else if (pid == 0) { - /* Replace stdout with a pipe to collectd. */ - close(g_configuration->helper_pipes[0]); - close(STDOUT_FILENO); - dup2(g_configuration->helper_pipes[1], STDOUT_FILENO); - dpdk_helper_run(); - } else { - ERROR("dpdkstat: Failed to fork helper process: %s\n", strerror(errno)); - return -1; + DPDK_STATS_CTX_GET(phc)->stats_count = stats_count; + int stats_size = stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE; + + if (dpdk_stats_get_size(phc) < stats_size) { + DPDK_CHILD_LOG( + DPDK_STATS_PLUGIN + ":%s:%d not enough space for stats (available=%d, needed=%d)\n", + __FUNCTION__, __LINE__, (int)dpdk_stats_get_size(phc), stats_size); + return -ENOBUFS; } - return 0; + + return dpdk_helper_stats_get(phc); } -/* - * Initialize the DPDK EAL, if this returns, EAL is successfully initialized. - * On failure, the EAL prints an error message, and the helper process exits. - */ -static int dpdk_helper_init_eal(void) -{ - g_configuration->helper_status = DPDK_HELPER_INITIALIZING_EAL; - char *argp[(g_configuration->eal_argc) + 1]; - int i = 0; - - argp[i++] = "collectd-dpdk"; - if (strcasecmp(g_configuration->coremask, "") != 0) { - argp[i++] = "-c"; - argp[i++] = g_configuration->coremask; - } - if (strcasecmp(g_configuration->memory_channels, "") != 0) { - argp[i++] = "-n"; - argp[i++] = g_configuration->memory_channels; - } - if (strcasecmp(g_configuration->socket_memory, "") != 0) { - argp[i++] = "--socket-mem"; - argp[i++] = g_configuration->socket_memory; - } - if (strcasecmp(g_configuration->file_prefix, "") != 0 && - strcasecmp(g_configuration->file_prefix, "/var/run/.rte_config") != 0) { - argp[i++] = "--file-prefix"; - argp[i++] = g_configuration->file_prefix; - } - if (strcasecmp(g_configuration->process_type, "") != 0) { - argp[i++] = "--proc-type"; - argp[i++] = g_configuration->process_type; - } - g_configuration->eal_argc = i; - - g_configuration->eal_initialized = 1; - int ret = rte_eal_init(g_configuration->eal_argc, argp); - if (ret < 0) { - g_configuration->eal_initialized = 0; - printf("dpdkstat: ERROR initializing EAL ret = %d\n", ret); - printf("dpdkstat: EAL arguments: "); - for (i=0; i< g_configuration->eal_argc; i++) { - printf("%s ", argp[i]); +static void dpdk_stats_resolve_cnt_type(char *cnt_type, size_t cnt_type_len, + const char *cnt_name) { + char *type_end; + type_end = strrchr(cnt_name, '_'); + + if ((type_end != NULL) && (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) { + if (strstr(type_end, "bytes") != NULL) { + sstrncpy(cnt_type, "if_rx_octets", cnt_type_len); + } else if (strstr(type_end, "error") != NULL) { + sstrncpy(cnt_type, "if_rx_errors", cnt_type_len); + } else if (strstr(type_end, "dropped") != NULL) { + sstrncpy(cnt_type, "if_rx_dropped", cnt_type_len); + } else if (strstr(type_end, "packets") != NULL) { + sstrncpy(cnt_type, "if_rx_packets", cnt_type_len); + } else if (strstr(type_end, "_placement") != NULL) { + sstrncpy(cnt_type, "if_rx_errors", cnt_type_len); + } else if (strstr(type_end, "_buff") != NULL) { + sstrncpy(cnt_type, "if_rx_errors", cnt_type_len); + } else { + /* Does not fit obvious type: use a more generic one */ + sstrncpy(cnt_type, "derive", cnt_type_len); } - printf("\n"); - return ret; - } - return 0; -} -static int dpdk_helper_run (void) -{ - pid_t ppid = getppid(); - g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY; - - while (1) { - /* sem_timedwait() to avoid blocking forever */ - struct timespec ts; - cdtime_t now = cdtime(); - cdtime_t half_sec = MS_TO_CDTIME_T(1500); - CDTIME_T_TO_TIMESPEC(now + half_sec + g_configuration->interval *2, &ts); - int ret = sem_timedwait(&g_configuration->sema_helper_get_stats, &ts); - - if (ret == -1 && errno == ETIMEDOUT) { - ERROR("dpdkstat-helper: sem timedwait()" - " timeout, did collectd terminate?\n"); - dpdk_helper_exit(RESET); + } else if ((type_end != NULL) && + (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) { + if (strstr(type_end, "bytes") != NULL) { + sstrncpy(cnt_type, "if_tx_octets", cnt_type_len); + } else if (strstr(type_end, "error") != NULL) { + sstrncpy(cnt_type, "if_tx_errors", cnt_type_len); + } else if (strstr(type_end, "dropped") != NULL) { + sstrncpy(cnt_type, "if_tx_dropped", cnt_type_len); + } else if (strstr(type_end, "packets") != NULL) { + sstrncpy(cnt_type, "if_tx_packets", cnt_type_len); + } else { + /* Does not fit obvious type: use a more generic one */ + sstrncpy(cnt_type, "derive", cnt_type_len); } - /* Parent PID change means collectd died so quit the helper process. */ - if (ppid != getppid()) { - WARNING("dpdkstat-helper: parent PID changed, quitting.\n"); - dpdk_helper_exit(RESET); + } else if ((type_end != NULL) && + (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) { + + if (strstr(type_end, "_filters") != NULL) { + sstrncpy(cnt_type, "operations", cnt_type_len); + } else if (strstr(type_end, "error") != NULL) + sstrncpy(cnt_type, "errors", cnt_type_len); + + } else if ((type_end != NULL) && + (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) { + if (strstr(type_end, "error") != NULL) { + sstrncpy(cnt_type, "errors", cnt_type_len); } + } else { + /* Does not fit obvious type, or strrchr error: + * use a more generic type */ + sstrncpy(cnt_type, "derive", cnt_type_len); + } +} - /* Checking for DPDK primary process. */ - if (!rte_eal_primary_proc_alive(g_configuration->file_prefix)) { - if(g_configuration->eal_initialized) { - WARNING("dpdkstat-helper: no primary alive but EAL initialized:" - " quitting.\n"); - dpdk_helper_exit(RESET); - } - g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY; - /* Back to start of while() - waiting for primary process */ - continue; - } +static void dpdk_stats_counter_submit(const char *plugin_instance, + const char *cnt_name, derive_t value, + cdtime_t port_read_time) { + value_list_t vl = VALUE_LIST_INIT; + vl.values = &(value_t){.derive = value}; + vl.values_len = 1; + vl.time = port_read_time; + sstrncpy(vl.plugin, DPDK_STATS_PLUGIN, sizeof(vl.plugin)); + sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance)); + dpdk_stats_resolve_cnt_type(vl.type, sizeof(vl.type), cnt_name); + sstrncpy(vl.type_instance, cnt_name, sizeof(vl.type_instance)); + plugin_dispatch_values(&vl); +} - if (!g_configuration->eal_initialized) { - /* Initialize EAL. */ - int ret = dpdk_helper_init_eal(); - if(ret != 0) { - WARNING("ERROR INITIALIZING EAL\n"); - dpdk_helper_exit(RESET); - } - } +static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) { + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc); - g_configuration->helper_status = DPDK_HELPER_ALIVE_SENDING_STATS; + /* dispatch stats values to collectd */ - uint8_t nb_ports = rte_eth_dev_count(); - if (nb_ports == 0) { - DEBUG("dpdkstat-helper: No DPDK ports available. " - "Check bound devices to DPDK driver.\n"); - return 0; + DEBUG("%s:%s:%d ports=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__, + ctx->ports_count); + + int stats_count = 0; + + for (int i = 0; i < ctx->ports_count; i++) { + if (!(ctx->config.enabled_port_mask & (1 << i))) + continue; + + char dev_name[64]; + if (ctx->config.port_name[i][0] != 0) { + snprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]); + } else { + snprintf(dev_name, sizeof(dev_name), "port.%d", i); } - if (nb_ports > RTE_MAX_ETHPORTS) - nb_ports = RTE_MAX_ETHPORTS; - /* If no port mask was specified enable all ports*/ - if (g_configuration->enabled_port_mask == 0) - g_configuration->enabled_port_mask = 0xffff; - - int len = 0, enabled_port_count = 0, num_xstats = 0, i = 0; - for (; i < nb_ports; i++) { - if (g_configuration->enabled_port_mask & (1 << i)) { - if(g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) { - len = rte_eth_xstats_get(i, NULL, 0); - if (len < 0) { - ERROR("dpdkstat-helper: Cannot get xstats count\n"); - return -1; - } - num_xstats += len; - g_configuration->num_stats_in_port[enabled_port_count] = len; - enabled_port_count++; - continue; - } else { - len = g_configuration->num_stats_in_port[enabled_port_count]; - g_configuration->port_read_time[enabled_port_count] = cdtime(); - ret = rte_eth_xstats_get(i, &g_configuration->xstats + num_xstats, - g_configuration->num_stats_in_port[enabled_port_count]); - if (ret < 0 || ret != len) { - DEBUG("dpdkstat-helper: Error reading xstats on port %d len = %d\n", - i, len); - return -1; - } - num_xstats += g_configuration->num_stats_in_port[enabled_port_count]; - enabled_port_count++; - } - } /* if (enabled_port_mask) */ - } /* for (nb_ports) */ - - if (g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) { - g_configuration->num_ports = enabled_port_count; - g_configuration->num_xstats = num_xstats; - DEBUG("dpdkstat-helper ports: %d, num stats: %d\n", - g_configuration->num_ports, g_configuration->num_xstats); - /* Exit, allowing collectd to re-init SHM to the right size */ - g_configuration->collectd_reinit_shm = REINIT_SHM; - dpdk_helper_exit(NO_RESET); + DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i, + dev_name, ctx->port_stats_count[i]); + + for (int j = 0; j < ctx->port_stats_count[i]; j++) { + const char *cnt_name = DPDK_STATS_XSTAT_GET_NAME(ctx, stats_count); + if (cnt_name == NULL) + WARNING("dpdkstat: Invalid counter name"); + else + dpdk_stats_counter_submit( + dev_name, cnt_name, + (derive_t)DPDK_STATS_XSTAT_GET_VALUE(ctx, stats_count), + ctx->port_read_time[i]); + stats_count++; + + assert(stats_count <= ctx->stats_count); } - /* Now kick collectd send thread to send the stats */ - int err = sem_post(&g_configuration->sema_stats_in_shm); - if (err) - ERROR("dpdkstat: error posting semaphore to helper %s\n", strerror(errno)); - } /* while(1) */ + } return 0; } -static int dpdk_read (user_data_t *ud) -{ - int ret = 0; +static int dpdk_stats_reinit_helper() { + DPDK_STATS_TRACE(); - /* - * Check if SHM flag is set to be re-initialized. AKA DPDK ports have been - * counted, so re-init SHM to be large enough to fit all the statistics. - */ - if (g_configuration->collectd_reinit_shm) { - DEBUG("dpdkstat: read() now reinit SHM then launching send-thread\n"); - dpdk_re_init_shm(); - } + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc); - /* - * Check if DPDK proc is alive, and has already counted port / stats. This - * must be done in dpdk_read(), because the DPDK primary process may not be - * alive at dpdk_init() time. - */ - if (g_configuration->helper_status == DPDK_HELPER_NOT_INITIALIZED || - g_configuration->helper_status == DPDK_HELPER_GRACEFUL_QUIT) { - int action = DPDK_HELPER_ACTION_SEND_STATS; - if(g_configuration->num_xstats == 0) - action = DPDK_HELPER_ACTION_COUNT_STATS; - /* Spawn the helper thread to count stats or to read stats. */ - int err = dpdk_helper_spawn(action); - if (err) { - ERROR("dpdkstat: error spawning helper %s\n", strerror(errno)); - return -1; - } - } + size_t data_size = sizeof(dpdk_stats_ctx_t) + + (ctx->stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE); - int exit_status; - pid_t ws = waitpid(g_configuration->helper_pid, &exit_status, WNOHANG); - /* - * Conditions under which to respawn helper: - * waitpid() fails, helper process died (or quit), so respawn - */ - int respawn_helper = 0; - if (ws != 0) { - respawn_helper = 1; - } + DEBUG("%s:%d helper reinit (new_size=%" PRIsz ")", __FUNCTION__, __LINE__, + data_size); - char buf[DPDKSTAT_MAX_BUFFER_SIZE]; - char out[DPDKSTAT_MAX_BUFFER_SIZE]; - - /* non blocking check on helper logging pipe */ - struct pollfd fds; - fds.fd = g_configuration->helper_pipes[0]; - fds.events = POLLIN; - int data_avail = poll(&fds, 1, 0); - while (data_avail) { - int nbytes = read(g_configuration->helper_pipes[0], buf, sizeof(buf)); - if (nbytes <= 0) - break; - ssnprintf( out, nbytes, "%s", buf); - DEBUG("dpdkstat: helper-proc: %s\n", out); + dpdk_stats_ctx_t tmp_ctx; + dpdk_eal_config_t tmp_eal; + + memcpy(&tmp_ctx, ctx, sizeof(dpdk_stats_ctx_t)); + dpdk_helper_eal_config_get(g_hc, &tmp_eal); + + dpdk_helper_shutdown(g_hc); + + g_hc = NULL; + + int ret; + ret = dpdk_helper_init(g_shm_name, data_size, &g_hc); + if (ret != 0) { + ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN, + g_shm_name, STRERRNO); + return ret; } - if (respawn_helper) { - if (g_configuration->helper_pid) - dpdk_helper_exit(RESET); - dpdk_helper_spawn(DPDK_HELPER_ACTION_COUNT_STATS); + ctx = DPDK_STATS_CTX_GET(g_hc); + memcpy(ctx, &tmp_ctx, sizeof(dpdk_stats_ctx_t)); + DPDK_STATS_CTX_INIT(ctx); + dpdk_helper_eal_config_set(g_hc, &tmp_eal); + + return ret; +} + +static int dpdk_stats_read(user_data_t *ud) { + DPDK_STATS_TRACE(); + + int ret = 0; + + if (g_hc == NULL) { + ERROR("dpdk stats plugin not initialized"); + return -EINVAL; } - struct timespec helper_kick_time; - clock_gettime(CLOCK_REALTIME, &helper_kick_time); - /* Kick helper process through SHM */ - sem_post(&g_configuration->sema_helper_get_stats); - - struct timespec ts; - cdtime_t now = cdtime(); - CDTIME_T_TO_TIMESPEC(now + g_configuration->interval, &ts); - ret = sem_timedwait(&g_configuration->sema_stats_in_shm, &ts); - if (ret == -1 && errno == ETIMEDOUT) { - DEBUG("dpdkstat: timeout in collectd thread: is a DPDK Primary running? \n"); + dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc); + + int result = 0; + ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_STATS, &result, + ctx->config.interval); + if (ret != 0) { return 0; } - /* Dispatch the stats.*/ - int count = 0, i = 0; + if (result == -ENOBUFS) { + dpdk_stats_reinit_helper(); + } else if (result == -ENODEV) { + dpdk_helper_shutdown(g_hc); + } else if (result == 0) { + dpdk_stats_counters_dispatch(g_hc); + } - for (; i < g_configuration->num_ports; i++) { - cdtime_t time = g_configuration->port_read_time[i]; - char dev_name[64]; - int len = g_configuration->num_stats_in_port[i]; - ssnprintf(dev_name, sizeof(dev_name), "port.%d", i); - struct rte_eth_xstats *xstats = (&g_configuration->xstats); - xstats += count; /* pointer arithmetic to jump to each stats struct */ - int j = 0; - for (; j < len; j++) { - value_t dpdkstat_values[1]; - value_list_t dpdkstat_vl = VALUE_LIST_INIT; - - dpdkstat_values[0].counter = xstats[j].value; - dpdkstat_vl.values = dpdkstat_values; - dpdkstat_vl.values_len = 1; /* Submit stats one at a time */ - dpdkstat_vl.time = time; - sstrncpy (dpdkstat_vl.host, hostname_g, sizeof (dpdkstat_vl.host)); - sstrncpy (dpdkstat_vl.plugin, "dpdkstat", sizeof (dpdkstat_vl.plugin)); - sstrncpy (dpdkstat_vl.plugin_instance, dev_name, - sizeof (dpdkstat_vl.plugin_instance)); - sstrncpy (dpdkstat_vl.type, "counter", - sizeof (dpdkstat_vl.type)); - sstrncpy (dpdkstat_vl.type_instance, xstats[j].name, - sizeof (dpdkstat_vl.type_instance)); - plugin_dispatch_values (&dpdkstat_vl); - } - count += len; - } /* for each port */ return 0; } -static int dpdk_shm_cleanup(void) -{ - int ret = munmap(g_configuration, sizeof(dpdk_config_t)); - g_configuration = 0; - if (ret) { - WARNING("dpdkstat: munmap returned %d\n", ret); - return ret; - } - ret = shm_unlink(DPDK_SHM_NAME); - if (ret) { - WARNING("dpdkstat: shm_unlink returned %d\n", ret); - return ret; - } +static int dpdk_stats_shutdown(void) { + DPDK_STATS_TRACE(); + + dpdk_helper_shutdown(g_hc); + g_hc = NULL; + return 0; } -static int dpdk_shutdown (void) -{ +static int dpdk_stats_init(void) { + DPDK_STATS_TRACE(); int ret = 0; - close(g_configuration->helper_pipes[1]); - int err = kill(g_configuration->helper_pid, SIGKILL); - if (err) { - ERROR("dpdkstat: error sending sigkill to helper %s\n", strerror(errno)); - ret = -1; + + if (g_state != DPDK_STAT_STATE_OKAY) { + dpdk_stats_shutdown(); + return -1; } - err = dpdk_shm_cleanup(); - if (err) { - ERROR("dpdkstat: error cleaning up SHM: %s\n", strerror(errno)); - ret = -1; + + ret = dpdk_stats_preinit(); + if (ret != 0) { + return ret; } - return ret; + return 0; } -void module_register (void) -{ - plugin_register_complex_config ("dpdkstat", dpdk_config); - plugin_register_init ("dpdkstat", dpdk_init); - plugin_register_shutdown ("dpdkstat", dpdk_shutdown); +void module_register(void) { + plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init); + plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config); + plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0, + NULL); + plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown); }