2 * collectd - src/dpdkstat.c
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Maryam Tahhan <maryam.tahhan@intel.com>
27 * Harry van Haaren <harry.van.haaren@intel.com>
28 * Taras Chornyi <tarasx.chornyi@intel.com>
29 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
30 * Krzysztof Matczak <krzysztofx.matczak@intel.com>
36 #include "utils_dpdk.h"
38 #include <rte_config.h>
39 #include <rte_ethdev.h>
41 #define DPDK_STATS_PLUGIN "dpdkstat"
42 #define DPDK_STATS_NAME "dpdk_collectd_stats"
44 #define DPDK_STATS_TRACE() \
45 DEBUG("%s:%s:%d pid=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__, getpid())
47 struct dpdk_stats_config_s {
49 uint32_t enabled_port_mask;
50 char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
52 typedef struct dpdk_stats_config_s dpdk_stats_config_t;
54 #define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16)
56 #if RTE_VERSION < RTE_VERSION_16_07
57 #define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
58 #define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xstats[index].name
59 #define DPDK_STATS_CTX_GET_XSTAT_SIZE sizeof(struct rte_eth_xstats)
60 #define DPDK_STATS_CTX_INIT(ctx) \
62 ctx->xstats = (struct rte_eth_xstats *)&ctx->raw_data[0]; \
65 #define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
66 #define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xnames[index].name
67 #define DPDK_STATS_CTX_GET_XSTAT_SIZE \
68 (sizeof(struct rte_eth_xstat) + sizeof(struct rte_eth_xstat_name))
69 #define DPDK_STATS_CTX_INIT(ctx) \
71 ctx->xstats = (struct rte_eth_xstat *)&ctx->raw_data[0]; \
73 (struct rte_eth_xstat_name *)&ctx \
74 ->raw_data[ctx->stats_count * sizeof(struct rte_eth_xstat)]; \
78 struct dpdk_stats_ctx_s {
79 dpdk_stats_config_t config;
82 cdtime_t port_read_time[RTE_MAX_ETHPORTS];
83 uint32_t port_stats_count[RTE_MAX_ETHPORTS];
84 #if RTE_VERSION < RTE_VERSION_16_07
85 struct rte_eth_xstats *xstats;
87 struct rte_eth_xstat *xstats;
88 struct rte_eth_xstat_name *xnames;
92 typedef struct dpdk_stats_ctx_s dpdk_stats_ctx_t;
95 DPDK_STAT_STATE_OKAY = 0,
96 DPDK_STAT_STATE_CFG_ERR,
97 } dpdk_stat_cfg_status;
99 #define DPDK_STATS_CTX_GET(a) ((dpdk_stats_ctx_t *)dpdk_helper_priv_get(a))
101 dpdk_helper_ctx_t *g_hc = NULL;
102 static char g_shm_name[DATA_MAX_NAME_LEN] = DPDK_STATS_NAME;
103 static dpdk_stat_cfg_status g_state = DPDK_STAT_STATE_OKAY;
105 static int dpdk_stats_reinit_helper();
106 static void dpdk_stats_default_config(void) {
107 dpdk_stats_ctx_t *ec = DPDK_STATS_CTX_GET(g_hc);
109 ec->config.interval = plugin_get_interval();
110 for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
111 ec->config.port_name[i][0] = 0;
113 /* Enable all ports by default */
114 ec->config.enabled_port_mask = ~0;
117 static int dpdk_stats_preinit(void) {
121 /* already initialized if config callback was called before init callback */
122 DEBUG("dpdk_stats_preinit: helper already initialized");
126 int ret = dpdk_helper_init(g_shm_name, sizeof(dpdk_stats_ctx_t), &g_hc);
128 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
129 g_shm_name, STRERRNO);
133 dpdk_stats_default_config();
137 static int dpdk_stats_config(oconfig_item_t *ci) {
140 int ret = dpdk_stats_preinit();
142 g_state = DPDK_STAT_STATE_CFG_ERR;
146 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
148 for (int i = 0; i < ci->children_num; i++) {
149 oconfig_item_t *child = ci->children + i;
151 if (strcasecmp("EnabledPortMask", child->key) == 0)
152 ret = cf_util_get_int(child, (int *)&ctx->config.enabled_port_mask);
153 else if (strcasecmp("SharedMemObj", child->key) == 0) {
154 ret = cf_util_get_string_buffer(child, g_shm_name, sizeof(g_shm_name));
156 ret = dpdk_stats_reinit_helper();
157 } else if (strcasecmp("EAL", child->key) == 0)
158 ret = dpdk_helper_eal_config_parse(g_hc, child);
159 else if (strcasecmp("PortName", child->key) != 0) {
160 ERROR(DPDK_STATS_PLUGIN ": unrecognized configuration option %s",
166 g_state = DPDK_STAT_STATE_CFG_ERR;
171 DEBUG(DPDK_STATS_PLUGIN ": Enabled Port Mask 0x%X",
172 ctx->config.enabled_port_mask);
173 DEBUG(DPDK_STATS_PLUGIN ": Shared memory object %s", g_shm_name);
177 /* parse port names after EnabledPortMask was parsed */
178 for (int i = 0; i < ci->children_num; i++) {
179 oconfig_item_t *child = ci->children + i;
181 if (strcasecmp("PortName", child->key) == 0) {
183 while (!(ctx->config.enabled_port_mask & (1 << port_num)))
186 if (cf_util_get_string_buffer(child, ctx->config.port_name[port_num],
187 sizeof(ctx->config.port_name[port_num]))) {
188 g_state = DPDK_STAT_STATE_CFG_ERR;
192 DEBUG(DPDK_STATS_PLUGIN ": Port %d Name: %s", port_num,
193 ctx->config.port_name[port_num]);
202 static int dpdk_helper_stats_get(dpdk_helper_ctx_t *phc) {
206 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
208 /* get stats from DPDK */
209 for (uint8_t i = 0; i < ctx->ports_count; i++) {
210 if (!(ctx->config.enabled_port_mask & (1 << i)))
213 ctx->port_read_time[i] = cdtime();
214 /* Store available stats array length for port */
215 len = ctx->port_stats_count[i];
217 ret = rte_eth_xstats_get(i, &ctx->xstats[stats], len);
218 if (ret < 0 || ret > len) {
219 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
220 ": Error reading stats (port=%d; len=%d, ret=%d)\n",
222 ctx->port_stats_count[i] = 0;
225 #if RTE_VERSION >= RTE_VERSION_16_07
226 ret = rte_eth_xstats_get_names(i, &ctx->xnames[stats], len);
227 if (ret < 0 || ret > len) {
228 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
229 ": Error reading stat names (port=%d; len=%d ret=%d)\n",
231 ctx->port_stats_count[i] = 0;
235 ctx->port_stats_count[i] = ret;
236 stats += ctx->port_stats_count[i];
239 assert(stats <= ctx->stats_count);
243 static int dpdk_helper_stats_count_get(dpdk_helper_ctx_t *phc) {
244 uint8_t ports = dpdk_helper_eth_dev_count();
248 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
249 ctx->ports_count = ports;
253 for (int i = 0; i < ports; i++) {
254 if (!(ctx->config.enabled_port_mask & (1 << i)))
256 #if RTE_VERSION >= RTE_VERSION_16_07
257 len = rte_eth_xstats_get_names(i, NULL, 0);
259 len = rte_eth_xstats_get(i, NULL, 0);
262 DPDK_CHILD_LOG("%s: Cannot get stats count\n", DPDK_STATS_PLUGIN);
265 ctx->port_stats_count[i] = len;
269 DPDK_CHILD_LOG("%s:%s:%d stats_count=%d\n", DPDK_STATS_PLUGIN, __FUNCTION__,
270 __LINE__, stats_count);
275 static int dpdk_stats_get_size(dpdk_helper_ctx_t *phc) {
276 return dpdk_helper_data_size_get(phc) - sizeof(dpdk_stats_ctx_t);
279 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
280 /* this function is called from helper context */
283 DPDK_CHILD_LOG("%s: Invalid argument(phc)\n", DPDK_STATS_PLUGIN);
287 if (cmd != DPDK_CMD_GET_STATS) {
288 DPDK_CHILD_LOG("%s: Unknown command (cmd=%d)\n", DPDK_STATS_PLUGIN, cmd);
292 int stats_count = dpdk_helper_stats_count_get(phc);
293 if (stats_count < 0) {
297 DPDK_STATS_CTX_GET(phc)->stats_count = stats_count;
298 int stats_size = stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE;
300 if (dpdk_stats_get_size(phc) < stats_size) {
303 ":%s:%d not enough space for stats (available=%d, needed=%d)\n",
304 __FUNCTION__, __LINE__, (int)dpdk_stats_get_size(phc), stats_size);
308 return dpdk_helper_stats_get(phc);
311 static void dpdk_stats_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
312 const char *cnt_name) {
314 type_end = strrchr(cnt_name, '_');
316 if ((type_end != NULL) && (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
317 if (strstr(type_end, "bytes") != NULL) {
318 sstrncpy(cnt_type, "if_rx_octets", cnt_type_len);
319 } else if (strstr(type_end, "error") != NULL) {
320 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
321 } else if (strstr(type_end, "dropped") != NULL) {
322 sstrncpy(cnt_type, "if_rx_dropped", cnt_type_len);
323 } else if (strstr(type_end, "packets") != NULL) {
324 sstrncpy(cnt_type, "if_rx_packets", cnt_type_len);
325 } else if (strstr(type_end, "_placement") != NULL) {
326 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
327 } else if (strstr(type_end, "_buff") != NULL) {
328 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
330 /* Does not fit obvious type: use a more generic one */
331 sstrncpy(cnt_type, "derive", cnt_type_len);
334 } else if ((type_end != NULL) &&
335 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
336 if (strstr(type_end, "bytes") != NULL) {
337 sstrncpy(cnt_type, "if_tx_octets", cnt_type_len);
338 } else if (strstr(type_end, "error") != NULL) {
339 sstrncpy(cnt_type, "if_tx_errors", cnt_type_len);
340 } else if (strstr(type_end, "dropped") != NULL) {
341 sstrncpy(cnt_type, "if_tx_dropped", cnt_type_len);
342 } else if (strstr(type_end, "packets") != NULL) {
343 sstrncpy(cnt_type, "if_tx_packets", cnt_type_len);
345 /* Does not fit obvious type: use a more generic one */
346 sstrncpy(cnt_type, "derive", cnt_type_len);
348 } else if ((type_end != NULL) &&
349 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
351 if (strstr(type_end, "_filters") != NULL) {
352 sstrncpy(cnt_type, "operations", cnt_type_len);
353 } else if (strstr(type_end, "error") != NULL)
354 sstrncpy(cnt_type, "errors", cnt_type_len);
356 } else if ((type_end != NULL) &&
357 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
358 if (strstr(type_end, "error") != NULL) {
359 sstrncpy(cnt_type, "errors", cnt_type_len);
362 /* Does not fit obvious type, or strrchr error:
363 * use a more generic type */
364 sstrncpy(cnt_type, "derive", cnt_type_len);
368 static void dpdk_stats_counter_submit(const char *plugin_instance,
369 const char *cnt_name, derive_t value,
370 cdtime_t port_read_time) {
371 value_list_t vl = VALUE_LIST_INIT;
372 vl.values = &(value_t){.derive = value};
374 vl.time = port_read_time;
375 sstrncpy(vl.plugin, DPDK_STATS_PLUGIN, sizeof(vl.plugin));
376 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
377 dpdk_stats_resolve_cnt_type(vl.type, sizeof(vl.type), cnt_name);
378 sstrncpy(vl.type_instance, cnt_name, sizeof(vl.type_instance));
379 plugin_dispatch_values(&vl);
382 static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) {
383 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
385 /* dispatch stats values to collectd */
387 DEBUG("%s:%s:%d ports=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__,
392 for (int i = 0; i < ctx->ports_count; i++) {
393 if (!(ctx->config.enabled_port_mask & (1 << i)))
397 if (ctx->config.port_name[i][0] != 0) {
398 snprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
400 snprintf(dev_name, sizeof(dev_name), "port.%d", i);
403 DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i,
404 dev_name, ctx->port_stats_count[i]);
406 for (int j = 0; j < ctx->port_stats_count[i]; j++) {
407 const char *cnt_name = DPDK_STATS_XSTAT_GET_NAME(ctx, stats_count);
408 if (cnt_name == NULL)
409 WARNING("dpdkstat: Invalid counter name");
411 dpdk_stats_counter_submit(
413 (derive_t)DPDK_STATS_XSTAT_GET_VALUE(ctx, stats_count),
414 ctx->port_read_time[i]);
417 assert(stats_count <= ctx->stats_count);
424 static int dpdk_stats_reinit_helper() {
427 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
429 size_t data_size = sizeof(dpdk_stats_ctx_t) +
430 (ctx->stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE);
432 DEBUG("%s:%d helper reinit (new_size=%" PRIsz ")", __FUNCTION__, __LINE__,
435 dpdk_stats_ctx_t tmp_ctx;
436 dpdk_eal_config_t tmp_eal;
438 memcpy(&tmp_ctx, ctx, sizeof(dpdk_stats_ctx_t));
439 dpdk_helper_eal_config_get(g_hc, &tmp_eal);
441 dpdk_helper_shutdown(g_hc);
446 ret = dpdk_helper_init(g_shm_name, data_size, &g_hc);
448 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
449 g_shm_name, STRERRNO);
453 ctx = DPDK_STATS_CTX_GET(g_hc);
454 memcpy(ctx, &tmp_ctx, sizeof(dpdk_stats_ctx_t));
455 DPDK_STATS_CTX_INIT(ctx);
456 dpdk_helper_eal_config_set(g_hc, &tmp_eal);
461 static int dpdk_stats_read(user_data_t *ud) {
467 ERROR("dpdk stats plugin not initialized");
471 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
474 ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_STATS, &result,
475 ctx->config.interval);
480 if (result == -ENOBUFS) {
481 dpdk_stats_reinit_helper();
482 } else if (result == -ENODEV) {
483 dpdk_helper_shutdown(g_hc);
484 } else if (result == 0) {
485 dpdk_stats_counters_dispatch(g_hc);
491 static int dpdk_stats_shutdown(void) {
494 dpdk_helper_shutdown(g_hc);
500 static int dpdk_stats_init(void) {
504 if (g_state != DPDK_STAT_STATE_OKAY) {
505 dpdk_stats_shutdown();
509 ret = dpdk_stats_preinit();
517 void module_register(void) {
518 plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init);
519 plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config);
520 plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0,
522 plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown);