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>
32 #include "common.h" /* auxiliary functions */
33 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
34 #include "utils_time.h"
37 #include <semaphore.h>
39 #include <sys/queue.h>
42 #include <rte_config.h>
44 #include <rte_ethdev.h>
45 #include <rte_common.h>
46 #include <rte_debug.h>
47 #include <rte_malloc.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_launch.h>
51 #include <rte_tailq.h>
52 #include <rte_lcore.h>
53 #include <rte_per_lcore.h>
54 #include <rte_debug.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_string_fns.h>
60 #define DPDK_DEFAULT_RTE_CONFIG "/var/run/.rte_config"
61 #define DPDK_MAX_ARGC 8
62 #define DPDKSTAT_MAX_BUFFER_SIZE (4096 * 4)
63 #define DPDK_SHM_NAME "dpdk_collectd_stats_shm"
64 #define ERR_BUF_SIZE 1024
69 enum DPDK_HELPER_ACTION {
70 DPDK_HELPER_ACTION_COUNT_STATS,
71 DPDK_HELPER_ACTION_SEND_STATS,
74 enum DPDK_HELPER_STATUS {
75 DPDK_HELPER_NOT_INITIALIZED = 0,
76 DPDK_HELPER_WAITING_ON_PRIMARY,
77 DPDK_HELPER_INITIALIZING_EAL,
78 DPDK_HELPER_ALIVE_SENDING_STATS,
79 DPDK_HELPER_GRACEFUL_QUIT,
82 struct dpdk_config_s {
83 /* General DPDK params */
84 char coremask[DATA_MAX_NAME_LEN];
85 char memory_channels[DATA_MAX_NAME_LEN];
86 char socket_memory[DATA_MAX_NAME_LEN];
87 char process_type[DATA_MAX_NAME_LEN];
88 char file_prefix[DATA_MAX_NAME_LEN];
90 uint32_t eal_initialized;
91 uint32_t enabled_port_mask;
92 char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
95 int collectd_reinit_shm;
97 sem_t sema_helper_get_stats;
98 sem_t sema_stats_in_shm;
100 enum DPDK_HELPER_STATUS helper_status;
101 enum DPDK_HELPER_ACTION helper_action;
105 cdtime_t port_read_time[RTE_MAX_ETHPORTS];
106 uint32_t num_stats_in_port[RTE_MAX_ETHPORTS];
107 struct rte_eth_link link_status[RTE_MAX_ETHPORTS];
108 struct rte_eth_xstats *xstats;
109 /* rte_eth_xstats from here on until the end of the SHM */
111 typedef struct dpdk_config_s dpdk_config_t;
113 static int g_configured;
114 static dpdk_config_t *g_configuration;
116 static void dpdk_config_init_default(void);
117 static int dpdk_config(oconfig_item_t *ci);
118 static int dpdk_helper_init_eal(void);
119 static int dpdk_helper_run(void);
120 static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action);
121 static int dpdk_init(void);
122 static int dpdk_read(user_data_t *ud);
123 static int dpdk_shm_cleanup(void);
124 static int dpdk_shm_init(size_t size);
126 /* Write the default configuration to the g_configuration instances */
127 static void dpdk_config_init_default(void) {
128 g_configuration->interval = plugin_get_interval();
129 if (g_configuration->interval == cf_get_default_interval())
130 WARNING("dpdkstat: No time interval was configured, default value %lu ms "
132 CDTIME_T_TO_MS(g_configuration->interval));
133 /* Default is all ports enabled */
134 g_configuration->enabled_port_mask = ~0;
135 g_configuration->eal_argc = DPDK_MAX_ARGC;
136 g_configuration->eal_initialized = 0;
137 ssnprintf(g_configuration->coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
138 ssnprintf(g_configuration->memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
139 ssnprintf(g_configuration->process_type, DATA_MAX_NAME_LEN, "%s",
141 ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN, "%s",
142 DPDK_DEFAULT_RTE_CONFIG);
144 for (int i = 0; i < RTE_MAX_ETHPORTS; i++)
145 g_configuration->port_name[i][0] = 0;
148 static int dpdk_config(oconfig_item_t *ci) {
149 int port_counter = 0;
150 char errbuf[ERR_BUF_SIZE];
151 /* Allocate g_configuration and
152 * initialize a POSIX SHared Memory (SHM) object.
154 int err = dpdk_shm_init(sizeof(dpdk_config_t));
156 DEBUG("dpdkstat: error in shm_init, %s",
157 sstrerror(errno, errbuf, sizeof(errbuf)));
161 /* Set defaults for config, overwritten by loop if config item exists */
162 dpdk_config_init_default();
164 for (int i = 0; i < ci->children_num; i++) {
165 oconfig_item_t *child = ci->children + i;
167 if (strcasecmp("Coremask", child->key) == 0) {
168 cf_util_get_string_buffer(child, g_configuration->coremask,
169 sizeof(g_configuration->coremask));
170 DEBUG("dpdkstat:COREMASK %s ", g_configuration->coremask);
171 } else if (strcasecmp("MemoryChannels", child->key) == 0) {
172 cf_util_get_string_buffer(child, g_configuration->memory_channels,
173 sizeof(g_configuration->memory_channels));
174 DEBUG("dpdkstat:Memory Channels %s ", g_configuration->memory_channels);
175 } else if (strcasecmp("SocketMemory", child->key) == 0) {
176 cf_util_get_string_buffer(child, g_configuration->socket_memory,
177 sizeof(g_configuration->memory_channels));
178 DEBUG("dpdkstat: socket mem %s ", g_configuration->socket_memory);
179 } else if (strcasecmp("ProcessType", child->key) == 0) {
180 cf_util_get_string_buffer(child, g_configuration->process_type,
181 sizeof(g_configuration->process_type));
182 DEBUG("dpdkstat: proc type %s ", g_configuration->process_type);
183 } else if ((strcasecmp("FilePrefix", child->key) == 0) &&
184 (child->values[0].type == OCONFIG_TYPE_STRING)) {
185 ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN,
186 "/var/run/.%s_config", child->values[0].value.string);
187 DEBUG("dpdkstat: file prefix %s ", g_configuration->file_prefix);
188 } else if ((strcasecmp("EnabledPortMask", child->key) == 0) &&
189 (child->values[0].type == OCONFIG_TYPE_NUMBER)) {
190 g_configuration->enabled_port_mask =
191 (uint32_t)child->values[0].value.number;
192 DEBUG("dpdkstat: Enabled Port Mask %u",
193 g_configuration->enabled_port_mask);
194 } else if (strcasecmp("PortName", child->key) == 0) {
195 cf_util_get_string_buffer(
196 child, g_configuration->port_name[port_counter],
197 sizeof(g_configuration->port_name[port_counter]));
198 DEBUG("dpdkstat: Port %d Name: %s ", port_counter,
199 g_configuration->port_name[port_counter]);
202 WARNING("dpdkstat: The config option \"%s\" is unknown.", child->key);
204 } /* End for (int i = 0; i < ci->children_num; i++)*/
205 g_configured = 1; /* Bypass configuration in dpdk_shm_init(). */
211 * Allocate g_configuration and initialize SHared Memory (SHM)
212 * for config and helper process
214 static int dpdk_shm_init(size_t size) {
216 * Check if SHM is already configured: when config items are provided, the
217 * config function initializes SHM. If there is no config, then init() will
223 char errbuf[ERR_BUF_SIZE];
225 /* Create and open a new object, or open an existing object. */
226 int fd = shm_open(DPDK_SHM_NAME, O_CREAT | O_TRUNC | O_RDWR, 0666);
228 WARNING("dpdkstat:Failed to open %s as SHM:%s", DPDK_SHM_NAME,
229 sstrerror(errno, errbuf, sizeof(errbuf)));
232 /* Set the size of the shared memory object. */
233 int ret = ftruncate(fd, size);
235 WARNING("dpdkstat:Failed to resize SHM:%s",
236 sstrerror(errno, errbuf, sizeof(errbuf)));
239 /* Map the shared memory object into this process' virtual address space. */
240 g_configuration = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
241 if (g_configuration == MAP_FAILED) {
242 WARNING("dpdkstat:Failed to mmap SHM:%s",
243 sstrerror(errno, errbuf, sizeof(errbuf)));
247 * Close the file descriptor, the shared memory object still exists
248 * and can only be removed by calling shm_unlink().
252 /* Initialize g_configuration. */
253 memset(g_configuration, 0, size);
255 /* Initialize the semaphores for SHM use */
256 int err = sem_init(&g_configuration->sema_helper_get_stats, 1, 0);
258 ERROR("dpdkstat semaphore init failed: %s",
259 sstrerror(errno, errbuf, sizeof(errbuf)));
262 err = sem_init(&g_configuration->sema_stats_in_shm, 1, 0);
264 ERROR("dpdkstat semaphore init failed: %s",
265 sstrerror(errno, errbuf, sizeof(errbuf)));
269 g_configuration->xstats = NULL;
276 /* Reset to zero, as it was set to MAP_FAILED aka: (void *)-1. Avoid
277 * an issue if collectd attempts to run this plugin failure.
283 static int dpdk_re_init_shm() {
284 dpdk_config_t temp_config;
285 memcpy(&temp_config, g_configuration, sizeof(dpdk_config_t));
286 DEBUG("dpdkstat: %s: ports %" PRIu32 ", xstats %" PRIu32, __func__,
287 temp_config.num_ports, temp_config.num_xstats);
289 size_t shm_xstats_size =
290 sizeof(dpdk_config_t) +
291 (sizeof(struct rte_eth_xstats) * g_configuration->num_xstats);
292 DEBUG("=== SHM new size for %" PRIu32 " xstats", g_configuration->num_xstats);
294 int err = dpdk_shm_cleanup();
296 ERROR("dpdkstat: Error in shm_cleanup in %s", __func__);
299 err = dpdk_shm_init(shm_xstats_size);
301 WARNING("dpdkstat: Error in shm_init in %s", __func__);
304 /* If the XML config() function has been run, don't re-initialize defaults */
306 dpdk_config_init_default();
308 memcpy(g_configuration, &temp_config, sizeof(dpdk_config_t));
309 g_configuration->collectd_reinit_shm = 0;
310 g_configuration->xstats = (struct rte_eth_xstats *)(g_configuration + 1);
314 static int dpdk_init(void) {
315 int err = dpdk_shm_init(sizeof(dpdk_config_t));
317 ERROR("dpdkstat: %s : error %d in shm_init()", __func__, err);
321 /* If the XML config() function has been run, dont re-initialize defaults */
323 dpdk_config_init_default();
329 static int dpdk_helper_stop(int reset) {
330 g_configuration->helper_status = DPDK_HELPER_GRACEFUL_QUIT;
332 g_configuration->eal_initialized = 0;
333 g_configuration->num_ports = 0;
334 g_configuration->xstats = NULL;
335 g_configuration->num_xstats = 0;
336 for (int i = 0; i < RTE_MAX_ETHPORTS; i++)
337 g_configuration->num_stats_in_port[i] = 0;
339 close(g_configuration->helper_pipes[1]);
340 int err = kill(g_configuration->helper_pid, SIGKILL);
342 char errbuf[ERR_BUF_SIZE];
343 WARNING("dpdkstat: error sending kill to helper: %s",
344 sstrerror(errno, errbuf, sizeof(errbuf)));
350 static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action) {
351 char errbuf[ERR_BUF_SIZE];
352 g_configuration->eal_initialized = 0;
353 g_configuration->helper_action = action;
355 * Create a pipe for helper stdout back to collectd. This is necessary for
356 * logging EAL failures, as rte_eal_init() calls rte_panic().
358 if (pipe(g_configuration->helper_pipes) != 0) {
359 DEBUG("dpdkstat: Could not create helper pipe: %s",
360 sstrerror(errno, errbuf, sizeof(errbuf)));
364 int pipe0_flags = fcntl(g_configuration->helper_pipes[0], F_GETFL, 0);
365 int pipe1_flags = fcntl(g_configuration->helper_pipes[1], F_GETFL, 0);
366 if (pipe0_flags == -1 || pipe1_flags == -1) {
367 WARNING("dpdkstat: Failed setting up pipe flags: %s",
368 sstrerror(errno, errbuf, sizeof(errbuf)));
370 int pipe0_err = fcntl(g_configuration->helper_pipes[0], F_SETFL,
371 pipe1_flags | O_NONBLOCK);
372 int pipe1_err = fcntl(g_configuration->helper_pipes[1], F_SETFL,
373 pipe0_flags | O_NONBLOCK);
374 if (pipe0_err == -1 || pipe1_err == -1) {
375 WARNING("dpdkstat: Failed setting up pipes: %s",
376 sstrerror(errno, errbuf, sizeof(errbuf)));
381 close(g_configuration->helper_pipes[1]);
382 g_configuration->helper_pid = pid;
383 DEBUG("dpdkstat: helper pid %lu", (long)g_configuration->helper_pid);
384 /* Kick helper once its alive to have it start processing */
385 sem_post(&g_configuration->sema_helper_get_stats);
386 } else if (pid == 0) {
387 /* Replace stdout with a pipe to collectd. */
388 close(g_configuration->helper_pipes[0]);
389 close(STDOUT_FILENO);
390 dup2(g_configuration->helper_pipes[1], STDOUT_FILENO);
394 ERROR("dpdkstat: Failed to fork helper process: %s",
395 sstrerror(errno, errbuf, sizeof(errbuf)));
402 * Initialize the DPDK EAL, if this returns, EAL is successfully initialized.
403 * On failure, the EAL prints an error message, and the helper process exits.
405 static int dpdk_helper_init_eal(void) {
406 g_configuration->helper_status = DPDK_HELPER_INITIALIZING_EAL;
407 char *argp[(g_configuration->eal_argc) + 1];
410 argp[i++] = "collectd-dpdk";
411 if (strcasecmp(g_configuration->coremask, "") != 0) {
413 argp[i++] = g_configuration->coremask;
415 if (strcasecmp(g_configuration->memory_channels, "") != 0) {
417 argp[i++] = g_configuration->memory_channels;
419 if (strcasecmp(g_configuration->socket_memory, "") != 0) {
420 argp[i++] = "--socket-mem";
421 argp[i++] = g_configuration->socket_memory;
423 if (strcasecmp(g_configuration->file_prefix, "") != 0 &&
424 strcasecmp(g_configuration->file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
425 argp[i++] = "--file-prefix";
426 argp[i++] = g_configuration->file_prefix;
428 if (strcasecmp(g_configuration->process_type, "") != 0) {
429 argp[i++] = "--proc-type";
430 argp[i++] = g_configuration->process_type;
432 g_configuration->eal_argc = i;
434 g_configuration->eal_initialized = 1;
435 int ret = rte_eal_init(g_configuration->eal_argc, argp);
437 g_configuration->eal_initialized = 0;
443 static int dpdk_helper_run(void) {
444 char errbuf[ERR_BUF_SIZE];
445 pid_t ppid = getppid();
446 g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY;
449 /* sem_timedwait() to avoid blocking forever */
451 cdtime_t now = cdtime();
452 cdtime_t safety_period = MS_TO_CDTIME_T(1500);
453 CDTIME_T_TO_TIMESPEC(now + safety_period + g_configuration->interval * 2,
455 int ret = sem_timedwait(&g_configuration->sema_helper_get_stats, &ts);
457 if (ret == -1 && errno == ETIMEDOUT) {
458 ERROR("dpdkstat-helper: sem timedwait()"
459 " timeout, did collectd terminate?");
460 dpdk_helper_stop(RESET);
462 /* Parent PID change means collectd died so quit the helper process. */
463 if (ppid != getppid()) {
464 WARNING("dpdkstat-helper: parent PID changed, quitting.");
465 dpdk_helper_stop(RESET);
468 /* Checking for DPDK primary process. */
469 if (!rte_eal_primary_proc_alive(g_configuration->file_prefix)) {
470 if (g_configuration->eal_initialized) {
471 WARNING("dpdkstat-helper: no primary alive but EAL initialized:"
473 dpdk_helper_stop(RESET);
475 g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY;
476 /* Back to start of while() - waiting for primary process */
480 if (!g_configuration->eal_initialized) {
481 /* Initialize EAL. */
482 int ret = dpdk_helper_init_eal();
484 WARNING("ERROR INITIALIZING EAL");
485 dpdk_helper_stop(RESET);
489 g_configuration->helper_status = DPDK_HELPER_ALIVE_SENDING_STATS;
491 uint8_t nb_ports = rte_eth_dev_count();
493 DEBUG("dpdkstat-helper: No DPDK ports available. "
494 "Check bound devices to DPDK driver.");
495 dpdk_helper_stop(RESET);
498 if (nb_ports > RTE_MAX_ETHPORTS)
499 nb_ports = RTE_MAX_ETHPORTS;
501 int len = 0, enabled_port_count = 0, num_xstats = 0;
502 for (uint8_t i = 0; i < nb_ports; i++) {
503 if (!(g_configuration->enabled_port_mask & (1 << i)))
506 if (g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) {
507 len = rte_eth_xstats_get(i, NULL, 0);
509 ERROR("dpdkstat-helper: Cannot get xstats count on port %" PRIu8, i);
513 g_configuration->num_stats_in_port[enabled_port_count] = len;
514 enabled_port_count++;
517 len = g_configuration->num_stats_in_port[enabled_port_count];
518 g_configuration->port_read_time[enabled_port_count] = cdtime();
519 ret = rte_eth_xstats_get(
520 i, g_configuration->xstats + num_xstats,
521 g_configuration->num_stats_in_port[enabled_port_count]);
522 if (ret < 0 || ret != len) {
523 DEBUG("dpdkstat-helper: Error reading xstats on port %" PRIu8
528 num_xstats += g_configuration->num_stats_in_port[enabled_port_count];
529 enabled_port_count++;
531 } /* for (nb_ports) */
533 if (g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) {
534 g_configuration->num_ports = enabled_port_count;
535 g_configuration->num_xstats = num_xstats;
536 DEBUG("dpdkstat-helper ports: %" PRIu32 ", num stats: %" PRIu32,
537 g_configuration->num_ports, g_configuration->num_xstats);
538 /* Exit, allowing collectd to re-init SHM to the right size */
539 g_configuration->collectd_reinit_shm = REINIT_SHM;
540 dpdk_helper_stop(NO_RESET);
542 /* Now kick collectd send thread to send the stats */
543 int err = sem_post(&g_configuration->sema_stats_in_shm);
545 WARNING("dpdkstat: error posting semaphore to helper %s",
546 sstrerror(errno, errbuf, sizeof(errbuf)));
547 dpdk_helper_stop(RESET);
554 static void dpdk_submit_xstats(const char *dev_name,
555 const struct rte_eth_xstats *xstats,
556 uint32_t counters, cdtime_t port_read_time) {
557 for (uint32_t j = 0; j < counters; j++) {
558 value_list_t dpdkstat_vl = VALUE_LIST_INIT;
561 dpdkstat_vl.values = &(value_t){.derive = (derive_t)xstats[j].value};
562 dpdkstat_vl.values_len = 1; /* Submit stats one at a time */
563 dpdkstat_vl.time = port_read_time;
564 sstrncpy(dpdkstat_vl.host, hostname_g, sizeof(dpdkstat_vl.host));
565 sstrncpy(dpdkstat_vl.plugin, "dpdkstat", sizeof(dpdkstat_vl.plugin));
566 sstrncpy(dpdkstat_vl.plugin_instance, dev_name,
567 sizeof(dpdkstat_vl.plugin_instance));
569 type_end = strrchr(xstats[j].name, '_');
571 if ((type_end != NULL) &&
572 (strncmp(xstats[j].name, "rx_", strlen("rx_")) == 0)) {
573 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
574 sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
575 } else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) {
576 sstrncpy(dpdkstat_vl.type, "if_rx_dropped", sizeof(dpdkstat_vl.type));
577 } else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) {
578 sstrncpy(dpdkstat_vl.type, "if_rx_octets", sizeof(dpdkstat_vl.type));
579 } else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) {
580 sstrncpy(dpdkstat_vl.type, "if_rx_packets", sizeof(dpdkstat_vl.type));
581 } else if (strncmp(type_end, "_placement", strlen("_placement")) == 0) {
582 sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
583 } else if (strncmp(type_end, "_buff", strlen("_buff")) == 0) {
584 sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
586 /* Does not fit obvious type: use a more generic one */
587 sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
590 } else if ((type_end != NULL) &&
591 (strncmp(xstats[j].name, "tx_", strlen("tx_"))) == 0) {
592 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
593 sstrncpy(dpdkstat_vl.type, "if_tx_errors", sizeof(dpdkstat_vl.type));
594 } else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) {
595 sstrncpy(dpdkstat_vl.type, "if_tx_dropped", sizeof(dpdkstat_vl.type));
596 } else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) {
597 sstrncpy(dpdkstat_vl.type, "if_tx_octets", sizeof(dpdkstat_vl.type));
598 } else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) {
599 sstrncpy(dpdkstat_vl.type, "if_tx_packets", sizeof(dpdkstat_vl.type));
601 /* Does not fit obvious type: use a more generic one */
602 sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
604 } else if ((type_end != NULL) &&
605 (strncmp(xstats[j].name, "flow_", strlen("flow_"))) == 0) {
607 if (strncmp(type_end, "_filters", strlen("_filters")) == 0) {
608 sstrncpy(dpdkstat_vl.type, "operations", sizeof(dpdkstat_vl.type));
609 } else if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
610 sstrncpy(dpdkstat_vl.type, "errors", sizeof(dpdkstat_vl.type));
611 } else if (strncmp(type_end, "_filters", strlen("_filters")) == 0) {
612 sstrncpy(dpdkstat_vl.type, "filter_result", sizeof(dpdkstat_vl.type));
614 } else if ((type_end != NULL) &&
615 (strncmp(xstats[j].name, "mac_", strlen("mac_"))) == 0) {
616 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
617 sstrncpy(dpdkstat_vl.type, "errors", sizeof(dpdkstat_vl.type));
620 /* Does not fit obvious type, or strrchr error:
621 * use a more generic type */
622 sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
625 sstrncpy(dpdkstat_vl.type_instance, xstats[j].name,
626 sizeof(dpdkstat_vl.type_instance));
627 plugin_dispatch_values(&dpdkstat_vl);
631 static int dpdk_read(user_data_t *ud) {
635 * Check if SHM flag is set to be re-initialized. AKA DPDK ports have been
636 * counted, so re-init SHM to be large enough to fit all the statistics.
638 if (g_configuration->collectd_reinit_shm) {
639 DEBUG("dpdkstat: read() now reinit SHM then launching send-thread");
644 * Check if DPDK proc is alive, and has already counted port / stats. This
645 * must be done in dpdk_read(), because the DPDK primary process may not be
646 * alive at dpdk_init() time.
648 if (g_configuration->helper_status == DPDK_HELPER_NOT_INITIALIZED ||
649 g_configuration->helper_status == DPDK_HELPER_GRACEFUL_QUIT) {
650 int action = DPDK_HELPER_ACTION_SEND_STATS;
651 if (g_configuration->num_xstats == 0)
652 action = DPDK_HELPER_ACTION_COUNT_STATS;
653 /* Spawn the helper thread to count stats or to read stats. */
654 int err = dpdk_helper_spawn(action);
656 char errbuf[ERR_BUF_SIZE];
657 ERROR("dpdkstat: error spawning helper %s",
658 sstrerror(errno, errbuf, sizeof(errbuf)));
663 pid_t ws = waitpid(g_configuration->helper_pid, NULL, WNOHANG);
665 * Conditions under which to respawn helper:
666 * waitpid() fails, helper process died (or quit), so respawn
668 _Bool respawn_helper = 0;
673 char buf[DPDKSTAT_MAX_BUFFER_SIZE];
674 char out[DPDKSTAT_MAX_BUFFER_SIZE];
676 /* non blocking check on helper logging pipe */
677 struct pollfd fds = {
678 .fd = g_configuration->helper_pipes[0], .events = POLLIN,
680 int data_avail = poll(&fds, 1, 0);
681 if (data_avail < 0) {
682 char errbuf[ERR_BUF_SIZE];
683 if (errno != EINTR || errno != EAGAIN)
684 ERROR("dpdkstats: poll(2) failed: %s",
685 sstrerror(errno, errbuf, sizeof(errbuf)));
688 int nbytes = read(g_configuration->helper_pipes[0], buf, sizeof(buf));
691 ssnprintf(out, nbytes, "%s", buf);
692 DEBUG("dpdkstat: helper-proc: %s", out);
695 if (respawn_helper) {
696 if (g_configuration->helper_pid)
697 dpdk_helper_stop(RESET);
698 dpdk_helper_spawn(DPDK_HELPER_ACTION_COUNT_STATS);
701 /* Kick helper process through SHM */
702 sem_post(&g_configuration->sema_helper_get_stats);
705 cdtime_t now = cdtime();
706 CDTIME_T_TO_TIMESPEC(now + g_configuration->interval, &ts);
707 ret = sem_timedwait(&g_configuration->sema_stats_in_shm, &ts);
709 if (errno == ETIMEDOUT)
711 "dpdkstat: timeout in collectd thread: is a DPDK Primary running? ");
715 /* Dispatch the stats.*/
716 uint32_t count = 0, port_num = 0;
718 for (uint32_t i = 0; i < g_configuration->num_ports; i++) {
720 cdtime_t port_read_time = g_configuration->port_read_time[i];
721 uint32_t counters_num = g_configuration->num_stats_in_port[i];
722 size_t ports_max = CHAR_BIT * sizeof(g_configuration->enabled_port_mask);
723 for (size_t j = port_num; j < ports_max; j++) {
724 if ((g_configuration->enabled_port_mask & (1 << j)) != 0)
729 if (g_configuration->port_name[i][0] != 0)
730 ssnprintf(dev_name, sizeof(dev_name), "%s",
731 g_configuration->port_name[i]);
733 ssnprintf(dev_name, sizeof(dev_name), "port.%" PRIu32, port_num);
734 struct rte_eth_xstats *xstats = g_configuration->xstats + count;
736 dpdk_submit_xstats(dev_name, xstats, counters_num, port_read_time);
737 count += counters_num;
739 } /* for each port */
743 static int dpdk_shm_cleanup(void) {
744 int ret = munmap(g_configuration, sizeof(dpdk_config_t));
747 ERROR("dpdkstat: munmap returned %d", ret);
750 ret = shm_unlink(DPDK_SHM_NAME);
752 ERROR("dpdkstat: shm_unlink returned %d", ret);
758 static int dpdk_shutdown(void) {
760 char errbuf[ERR_BUF_SIZE];
761 close(g_configuration->helper_pipes[1]);
762 int err = kill(g_configuration->helper_pid, SIGKILL);
764 ERROR("dpdkstat: error sending sigkill to helper %s",
765 sstrerror(errno, errbuf, sizeof(errbuf)));
768 err = dpdk_shm_cleanup();
770 ERROR("dpdkstat: error cleaning up SHM: %s",
771 sstrerror(errno, errbuf, sizeof(errbuf)));
778 void module_register(void) {
779 plugin_register_complex_config("dpdkstat", dpdk_config);
780 plugin_register_init("dpdkstat", dpdk_init);
781 plugin_register_complex_read(NULL, "dpdkstat", dpdk_read, 0, NULL);
782 plugin_register_shutdown("dpdkstat", dpdk_shutdown);