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 char errbuf[ERR_BUF_SIZE];
129 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
130 g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
134 dpdk_stats_default_config();
138 static int dpdk_stats_config(oconfig_item_t *ci) {
141 int ret = dpdk_stats_preinit();
143 g_state = DPDK_STAT_STATE_CFG_ERR;
147 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
149 for (int i = 0; i < ci->children_num; i++) {
150 oconfig_item_t *child = ci->children + i;
152 if (strcasecmp("EnabledPortMask", child->key) == 0)
153 ret = cf_util_get_int(child, (int *)&ctx->config.enabled_port_mask);
154 else if (strcasecmp("SharedMemObj", child->key) == 0) {
155 ret = cf_util_get_string_buffer(child, g_shm_name, sizeof(g_shm_name));
157 ret = dpdk_stats_reinit_helper();
158 } else if (strcasecmp("EAL", child->key) == 0)
159 ret = dpdk_helper_eal_config_parse(g_hc, child);
160 else if (strcasecmp("PortName", child->key) != 0) {
161 ERROR(DPDK_STATS_PLUGIN ": unrecognized configuration option %s",
167 g_state = DPDK_STAT_STATE_CFG_ERR;
172 DEBUG(DPDK_STATS_PLUGIN ": Enabled Port Mask 0x%X",
173 ctx->config.enabled_port_mask);
174 DEBUG(DPDK_STATS_PLUGIN ": Shared memory object %s", g_shm_name);
178 /* parse port names after EnabledPortMask was parsed */
179 for (int i = 0; i < ci->children_num; i++) {
180 oconfig_item_t *child = ci->children + i;
182 if (strcasecmp("PortName", child->key) == 0) {
184 while (!(ctx->config.enabled_port_mask & (1 << port_num)))
187 if (cf_util_get_string_buffer(child, ctx->config.port_name[port_num],
188 sizeof(ctx->config.port_name[port_num]))) {
189 g_state = DPDK_STAT_STATE_CFG_ERR;
193 DEBUG(DPDK_STATS_PLUGIN ": Port %d Name: %s", port_num,
194 ctx->config.port_name[port_num]);
203 static int dpdk_helper_stats_get(dpdk_helper_ctx_t *phc) {
207 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
209 /* get stats from DPDK */
210 for (uint8_t i = 0; i < ctx->ports_count; i++) {
211 if (!(ctx->config.enabled_port_mask & (1 << i)))
214 ctx->port_read_time[i] = cdtime();
215 /* Store available stats array length for port */
216 len = ctx->port_stats_count[i];
218 ret = rte_eth_xstats_get(i, &ctx->xstats[stats], len);
219 if (ret < 0 || ret > len) {
220 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
221 ": Error reading stats (port=%d; len=%d, ret=%d)\n",
223 ctx->port_stats_count[i] = 0;
226 #if RTE_VERSION >= RTE_VERSION_16_07
227 ret = rte_eth_xstats_get_names(i, &ctx->xnames[stats], len);
228 if (ret < 0 || ret > len) {
229 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
230 ": Error reading stat names (port=%d; len=%d ret=%d)\n",
232 ctx->port_stats_count[i] = 0;
236 ctx->port_stats_count[i] = ret;
237 stats += ctx->port_stats_count[i];
240 assert(stats <= ctx->stats_count);
244 static int dpdk_helper_stats_count_get(dpdk_helper_ctx_t *phc) {
245 uint8_t ports = dpdk_helper_eth_dev_count();
249 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
250 ctx->ports_count = ports;
254 for (int i = 0; i < ports; i++) {
255 if (!(ctx->config.enabled_port_mask & (1 << i)))
257 #if RTE_VERSION >= RTE_VERSION_16_07
258 len = rte_eth_xstats_get_names(i, NULL, 0);
260 len = rte_eth_xstats_get(i, NULL, 0);
263 DPDK_CHILD_LOG("%s: Cannot get stats count\n", DPDK_STATS_PLUGIN);
266 ctx->port_stats_count[i] = len;
270 DPDK_CHILD_LOG("%s:%s:%d stats_count=%d\n", DPDK_STATS_PLUGIN, __FUNCTION__,
271 __LINE__, stats_count);
276 static int dpdk_stats_get_size(dpdk_helper_ctx_t *phc) {
277 return dpdk_helper_data_size_get(phc) - sizeof(dpdk_stats_ctx_t);
280 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
281 /* this function is called from helper context */
284 DPDK_CHILD_LOG("%s: Invalid argument(phc)\n", DPDK_STATS_PLUGIN);
288 if (cmd != DPDK_CMD_GET_STATS) {
289 DPDK_CHILD_LOG("%s: Unknown command (cmd=%d)\n", DPDK_STATS_PLUGIN, cmd);
293 int stats_count = dpdk_helper_stats_count_get(phc);
294 if (stats_count < 0) {
298 DPDK_STATS_CTX_GET(phc)->stats_count = stats_count;
299 int stats_size = stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE;
301 if (dpdk_stats_get_size(phc) < stats_size) {
304 ":%s:%d not enough space for stats (available=%d, needed=%d)\n",
305 __FUNCTION__, __LINE__, (int)dpdk_stats_get_size(phc), stats_size);
309 return dpdk_helper_stats_get(phc);
312 static void dpdk_stats_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
313 const char *cnt_name) {
315 type_end = strrchr(cnt_name, '_');
317 if ((type_end != NULL) && (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
318 if (strstr(type_end, "bytes") != NULL) {
319 sstrncpy(cnt_type, "if_rx_octets", cnt_type_len);
320 } else if (strstr(type_end, "error") != NULL) {
321 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
322 } else if (strstr(type_end, "dropped") != NULL) {
323 sstrncpy(cnt_type, "if_rx_dropped", cnt_type_len);
324 } else if (strstr(type_end, "packets") != NULL) {
325 sstrncpy(cnt_type, "if_rx_packets", cnt_type_len);
326 } else if (strstr(type_end, "_placement") != NULL) {
327 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
328 } else if (strstr(type_end, "_buff") != NULL) {
329 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
331 /* Does not fit obvious type: use a more generic one */
332 sstrncpy(cnt_type, "derive", cnt_type_len);
335 } else if ((type_end != NULL) &&
336 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
337 if (strstr(type_end, "bytes") != NULL) {
338 sstrncpy(cnt_type, "if_tx_octets", cnt_type_len);
339 } else if (strstr(type_end, "error") != NULL) {
340 sstrncpy(cnt_type, "if_tx_errors", cnt_type_len);
341 } else if (strstr(type_end, "dropped") != NULL) {
342 sstrncpy(cnt_type, "if_tx_dropped", cnt_type_len);
343 } else if (strstr(type_end, "packets") != NULL) {
344 sstrncpy(cnt_type, "if_tx_packets", cnt_type_len);
346 /* Does not fit obvious type: use a more generic one */
347 sstrncpy(cnt_type, "derive", cnt_type_len);
349 } else if ((type_end != NULL) &&
350 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
352 if (strstr(type_end, "_filters") != NULL) {
353 sstrncpy(cnt_type, "operations", cnt_type_len);
354 } else if (strstr(type_end, "error") != NULL)
355 sstrncpy(cnt_type, "errors", cnt_type_len);
357 } else if ((type_end != NULL) &&
358 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
359 if (strstr(type_end, "error") != NULL) {
360 sstrncpy(cnt_type, "errors", cnt_type_len);
363 /* Does not fit obvious type, or strrchr error:
364 * use a more generic type */
365 sstrncpy(cnt_type, "derive", cnt_type_len);
369 static void dpdk_stats_counter_submit(const char *plugin_instance,
370 const char *cnt_name, derive_t value,
371 cdtime_t port_read_time) {
372 value_list_t vl = VALUE_LIST_INIT;
373 vl.values = &(value_t){.derive = value};
375 vl.time = port_read_time;
376 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
377 sstrncpy(vl.plugin, DPDK_STATS_PLUGIN, sizeof(vl.plugin));
378 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
379 dpdk_stats_resolve_cnt_type(vl.type, sizeof(vl.type), cnt_name);
380 sstrncpy(vl.type_instance, cnt_name, sizeof(vl.type_instance));
381 plugin_dispatch_values(&vl);
384 static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) {
385 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
387 /* dispatch stats values to collectd */
389 DEBUG("%s:%s:%d ports=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__,
394 for (int i = 0; i < ctx->ports_count; i++) {
395 if (!(ctx->config.enabled_port_mask & (1 << i)))
399 if (ctx->config.port_name[i][0] != 0) {
400 snprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
402 snprintf(dev_name, sizeof(dev_name), "port.%d", i);
405 DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i,
406 dev_name, ctx->port_stats_count[i]);
408 for (int j = 0; j < ctx->port_stats_count[i]; j++) {
409 const char *cnt_name = DPDK_STATS_XSTAT_GET_NAME(ctx, stats_count);
410 if (cnt_name == NULL)
411 WARNING("dpdkstat: Invalid counter name");
413 dpdk_stats_counter_submit(
415 (derive_t)DPDK_STATS_XSTAT_GET_VALUE(ctx, stats_count),
416 ctx->port_read_time[i]);
419 assert(stats_count <= ctx->stats_count);
426 static int dpdk_stats_reinit_helper() {
429 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
431 size_t data_size = sizeof(dpdk_stats_ctx_t) +
432 (ctx->stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE);
434 DEBUG("%s:%d helper reinit (new_size=%zu)", __FUNCTION__, __LINE__,
437 dpdk_stats_ctx_t tmp_ctx;
438 dpdk_eal_config_t tmp_eal;
440 memcpy(&tmp_ctx, ctx, sizeof(dpdk_stats_ctx_t));
441 dpdk_helper_eal_config_get(g_hc, &tmp_eal);
443 dpdk_helper_shutdown(g_hc);
448 ret = dpdk_helper_init(g_shm_name, data_size, &g_hc);
450 char errbuf[ERR_BUF_SIZE];
451 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
452 g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
456 ctx = DPDK_STATS_CTX_GET(g_hc);
457 memcpy(ctx, &tmp_ctx, sizeof(dpdk_stats_ctx_t));
458 DPDK_STATS_CTX_INIT(ctx);
459 dpdk_helper_eal_config_set(g_hc, &tmp_eal);
464 static int dpdk_stats_read(user_data_t *ud) {
470 ERROR("dpdk stats plugin not initialized");
474 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
477 ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_STATS, &result,
478 ctx->config.interval);
483 if (result == -ENOBUFS) {
484 dpdk_stats_reinit_helper();
485 } else if (result == -ENODEV) {
486 dpdk_helper_shutdown(g_hc);
487 } else if (result == 0) {
488 dpdk_stats_counters_dispatch(g_hc);
494 static int dpdk_stats_shutdown(void) {
497 dpdk_helper_shutdown(g_hc);
503 static int dpdk_stats_init(void) {
507 if (g_state != DPDK_STAT_STATE_OKAY) {
508 dpdk_stats_shutdown();
512 ret = dpdk_stats_preinit();
520 void module_register(void) {
521 plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init);
522 plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config);
523 plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0,
525 plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown);