Interface name and number of ports enabled can now be customised
[collectd.git] / src / dpdkstat.c
1 /*-
2  * collectd - src/dpdkstat.c
3  * MIT License
4  *
5  * Copyright(c) 2016 Intel Corporation. All rights reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is furnished to do
12  * so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
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
23  * SOFTWARE.
24  *
25  * Authors:
26  *   Maryam Tahhan <maryam.tahhan@intel.com>
27  *   Harry van Haaren <harry.van.haaren@intel.com>
28  */
29
30 #include "collectd.h"
31 #include "common.h" /* auxiliary functions */
32 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
33 #include "utils_time.h"
34
35 #include <getopt.h>
36 #include <semaphore.h>
37 #include <sys/mman.h>
38 #include <sys/queue.h>
39 #include <poll.h>
40
41 #include <rte_config.h>
42 #include <rte_eal.h>
43 #include <rte_ethdev.h>
44 #include <rte_common.h>
45 #include <rte_debug.h>
46 #include <rte_malloc.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_launch.h>
50 #include <rte_tailq.h>
51 #include <rte_lcore.h>
52 #include <rte_per_lcore.h>
53 #include <rte_debug.h>
54 #include <rte_log.h>
55 #include <rte_atomic.h>
56 #include <rte_branch_prediction.h>
57 #include <rte_string_fns.h>
58
59 #define DPDKSTAT_MAX_BUFFER_SIZE (4096*4)
60 #define DPDK_SHM_NAME "dpdk_collectd_stats_shm"
61 #define REINIT_SHM 1
62 #define RESET 1
63 #define NO_RESET 0
64
65 enum DPDK_HELPER_ACTION {
66   DPDK_HELPER_ACTION_COUNT_STATS,
67   DPDK_HELPER_ACTION_SEND_STATS,
68 };
69
70 enum DPDK_HELPER_STATUS {
71   DPDK_HELPER_NOT_INITIALIZED = 0,
72   DPDK_HELPER_WAITING_ON_PRIMARY,
73   DPDK_HELPER_INITIALIZING_EAL,
74   DPDK_HELPER_ALIVE_SENDING_STATS,
75   DPDK_HELPER_GRACEFUL_QUIT,
76 };
77
78 struct dpdk_config_s {
79   /* General DPDK params */
80   char coremask[DATA_MAX_NAME_LEN];
81   char memory_channels[DATA_MAX_NAME_LEN];
82   char socket_memory[DATA_MAX_NAME_LEN];
83   char process_type[DATA_MAX_NAME_LEN];
84   char file_prefix[DATA_MAX_NAME_LEN];
85   cdtime_t interval;
86   uint32_t eal_initialized;
87   uint32_t enabled_port_mask;
88   char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
89   uint32_t eal_argc;
90   /* Helper info */
91   int   collectd_reinit_shm;
92   pid_t helper_pid;
93   sem_t sema_helper_get_stats;
94   sem_t sema_stats_in_shm;
95   int   helper_pipes[2];
96   enum DPDK_HELPER_STATUS helper_status;
97   enum DPDK_HELPER_ACTION helper_action;
98   /* xstats info */
99   uint32_t num_ports;
100   uint32_t num_xstats;
101   cdtime_t port_read_time[RTE_MAX_ETHPORTS];
102   uint32_t num_stats_in_port[RTE_MAX_ETHPORTS];
103   struct rte_eth_link link_status[RTE_MAX_ETHPORTS];
104   struct rte_eth_xstats xstats;
105   /* rte_eth_xstats from here on until the end of the SHM */
106 };
107 typedef struct dpdk_config_s dpdk_config_t;
108
109 static int g_configured;
110 static dpdk_config_t *g_configuration;
111
112 static void dpdk_config_init_default(void);
113 static int dpdk_config(oconfig_item_t *ci);
114 static int dpdk_helper_init_eal(void);
115 static int dpdk_helper_run(void);
116 static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action);
117 static int dpdk_init(void);
118 static int dpdk_read(user_data_t *ud);
119 static int dpdk_shm_cleanup(void);
120 static int dpdk_shm_init(size_t size);
121
122 /* Write the default configuration to the g_configuration instances */
123 static void dpdk_config_init_default(void)
124 {
125     int i;
126
127     g_configuration->interval = plugin_get_interval();
128     WARNING("dpdkstat: No time interval was configured, default value %lu ms is set\n",
129              CDTIME_T_TO_MS(g_configuration->interval));
130     /* Default is all ports enabled */
131     g_configuration->enabled_port_mask = ~0;
132     g_configuration->eal_argc = 2;
133     g_configuration->eal_initialized = 0;
134     ssnprintf(g_configuration->coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
135     ssnprintf(g_configuration->memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
136     ssnprintf(g_configuration->process_type, DATA_MAX_NAME_LEN, "%s", "secondary");
137     ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN, "%s",
138              "/var/run/.rte_config");
139
140     for (i = 0; i < RTE_MAX_ETHPORTS; i++)
141       g_configuration->port_name[i][0] = 0;
142 }
143
144 static int dpdk_config(oconfig_item_t *ci)
145 {
146   int i = 0, port_counter = 0;
147
148   /* Initialize a POSIX SHared Memory (SHM) object. */
149   int err = dpdk_shm_init(sizeof(dpdk_config_t));
150   if (err) {
151     DEBUG("dpdkstat: error in shm_init, %s", strerror(errno));
152     return -1;
153   }
154
155   /* Set defaults for config, overwritten by loop if config item exists */
156   dpdk_config_init_default();
157
158   for (i = 0; i < ci->children_num; i++) {
159     oconfig_item_t *child = ci->children + i;
160
161     if (strcasecmp("Interval", child->key) == 0) {
162       g_configuration->interval =
163             DOUBLE_TO_CDTIME_T (child->values[0].value.number);
164       DEBUG("dpdkstat: Plugin Read Interval %lu milliseconds\n",
165             CDTIME_T_TO_MS(g_configuration->interval));
166     } else if (strcasecmp("Coremask", child->key) == 0) {
167       ssnprintf(g_configuration->coremask, DATA_MAX_NAME_LEN, "%s",
168                child->values[0].value.string);
169       DEBUG("dpdkstat:COREMASK %s \n", g_configuration->coremask);
170       g_configuration->eal_argc+=1;
171     } else if (strcasecmp("MemoryChannels", child->key) == 0) {
172       ssnprintf(g_configuration->memory_channels, DATA_MAX_NAME_LEN, "%s",
173                child->values[0].value.string);
174       DEBUG("dpdkstat:Memory Channels %s \n", g_configuration->memory_channels);
175       g_configuration->eal_argc+=1;
176     } else if (strcasecmp("SocketMemory", child->key) == 0) {
177       ssnprintf(g_configuration->socket_memory, DATA_MAX_NAME_LEN, "%s",
178                child->values[0].value.string);
179       DEBUG("dpdkstat: socket mem %s \n", g_configuration->socket_memory);
180       g_configuration->eal_argc+=1;
181     } else if (strcasecmp("ProcessType", child->key) == 0) {
182       ssnprintf(g_configuration->process_type, DATA_MAX_NAME_LEN, "%s",
183                child->values[0].value.string);
184       DEBUG("dpdkstat: proc type %s \n", g_configuration->process_type);
185       g_configuration->eal_argc+=1;
186     } else if (strcasecmp("FilePrefix", child->key) == 0) {
187       ssnprintf(g_configuration->file_prefix, DATA_MAX_NAME_LEN, "/var/run/.%s_config",
188                child->values[0].value.string);
189       DEBUG("dpdkstat: file prefix %s \n", g_configuration->file_prefix);
190       if (strcasecmp(g_configuration->file_prefix, "/var/run/.rte_config") != 0) {
191         g_configuration->eal_argc+=1;
192       }
193     } else if (strcasecmp("EnabledPortMask", child->key) == 0) {
194       g_configuration->enabled_port_mask = (uint32_t)child->values[0].value.number;
195       DEBUG("dpdkstat: Enabled Port Mask %u\n", g_configuration->enabled_port_mask);
196     } else if (strcasecmp("PortName", child->key) == 0) {
197       ssnprintf(g_configuration->port_name[port_counter], DATA_MAX_NAME_LEN, "%s",
198                child->values[0].value.string);
199       DEBUG("dpdkstat: Port %d Name: %s \n", port_counter,
200                g_configuration->port_name[port_counter]);
201       port_counter++;
202     } else {
203       WARNING ("dpdkstat: The config option \"%s\" is unknown.",
204                child->key);
205     }
206   } /* End for (i = 0; i < ci->children_num; i++)*/
207   g_configured = 1; /* Bypass configuration in dpdk_shm_init(). */
208
209   return 0;
210 }
211
212 /* Initialize SHared Memory (SHM) for config and helper process */
213 static int dpdk_shm_init(size_t size)
214 {
215   /*
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
218    * just return.
219    */
220   if(g_configuration)
221     return 0;
222
223   /* Create and open a new object, or open an existing object. */
224   int fd = shm_open(DPDK_SHM_NAME, O_CREAT | O_TRUNC | O_RDWR, 0666);
225   if (fd < 0) {
226     WARNING("dpdkstat:Failed to open %s as SHM:%s\n", DPDK_SHM_NAME,
227             strerror(errno));
228     goto fail;
229   }
230   /* Set the size of the shared memory object. */
231   int ret = ftruncate(fd, size);
232   if (ret != 0) {
233     WARNING("dpdkstat:Failed to resize SHM:%s\n", strerror(errno));
234     goto fail_close;
235   }
236   /* Map the shared memory object into this process' virtual address space. */
237   g_configuration = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
238   if (g_configuration == MAP_FAILED) {
239     WARNING("dpdkstat:Failed to mmap SHM:%s\n", strerror(errno));
240     goto fail_close;
241   }
242   /*
243    * Close the file descriptor, the shared memory object still exists
244    * and can only be removed by calling shm_unlink().
245    */
246   close(fd);
247
248   /* Initialize g_configuration. */
249   memset(g_configuration, 0, size);
250
251   /* Initialize the semaphores for SHM use */
252   int err = sem_init(&g_configuration->sema_helper_get_stats, 1, 0);
253   if(err) {
254     ERROR("dpdkstat semaphore init failed: %s\n", strerror(errno));
255     goto fail_close;
256   }
257   err = sem_init(&g_configuration->sema_stats_in_shm, 1, 0);
258   if(err) {
259     ERROR("dpdkstat semaphore init failed: %s\n", strerror(errno));
260     goto fail_close;
261   }
262
263   return 0;
264
265 fail_close:
266   close(fd);
267 fail:
268   /* Reset to zero, as it was set to MAP_FAILED aka: (void *)-1. Avoid
269    * an issue if collectd attempts to run this plugin failure.
270    */
271   g_configuration = 0;
272   return -1;
273 }
274
275 static int dpdk_re_init_shm()
276 {
277   dpdk_config_t temp_config;
278   memcpy(&temp_config, g_configuration, sizeof(dpdk_config_t));
279   DEBUG("dpdkstat: %s: ports %d, xstats %d\n", __func__, temp_config.num_ports,
280         temp_config.num_xstats);
281
282   int shm_xstats_size = sizeof(dpdk_config_t) + (sizeof(struct rte_eth_xstats) *
283                         g_configuration->num_xstats);
284   DEBUG("=== SHM new size for %d xstats\n", g_configuration->num_xstats);
285
286   int err = dpdk_shm_cleanup();
287   if (err)
288     ERROR("dpdkstat: Error in shm_cleanup in %s\n", __func__);
289
290   err = dpdk_shm_init(shm_xstats_size);
291   if (err)
292     ERROR("dpdkstat: Error in shm_init in %s\n", __func__);
293
294   /* If the XML config() function has been run, dont re-initialize defaults */
295   if(!g_configured)
296     dpdk_config_init_default();
297
298   memcpy(g_configuration, &temp_config, sizeof(dpdk_config_t));
299   g_configuration->collectd_reinit_shm = 0;
300
301   return 0;
302 }
303
304 static int dpdk_init (void)
305 {
306   int err = dpdk_shm_init(sizeof(dpdk_config_t));
307   if (err)
308     ERROR("dpdkstat: %s : error %d in shm_init()", __func__, err);
309
310   /* If the XML config() function has been run, dont re-initialize defaults */
311   if(!g_configured) {
312     dpdk_config_init_default();
313   }
314
315   plugin_register_complex_read (NULL, "dpdkstat", dpdk_read,
316                                 g_configuration->interval, NULL);
317   return 0;
318 }
319
320 static int dpdk_helper_exit(int reset)
321 {
322   g_configuration->helper_status = DPDK_HELPER_GRACEFUL_QUIT;
323   if (reset) {
324     g_configuration->eal_initialized = 0;
325     g_configuration->num_ports = 0;
326     memset(&g_configuration->xstats, 0, g_configuration->num_xstats* sizeof(struct rte_eth_xstats));
327     g_configuration->num_xstats = 0;
328     int i = 0;
329     for (; i < RTE_MAX_ETHPORTS; i++)
330       g_configuration->num_stats_in_port[i] = 0;
331   }
332   close(g_configuration->helper_pipes[1]);
333   int err = kill(g_configuration->helper_pid, SIGKILL);
334   if (err) {
335     ERROR("dpdkstat: error sending kill to helper: %s\n", strerror(errno));
336   }
337
338   return 0;
339 }
340
341 static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action)
342 {
343   g_configuration->eal_initialized = 0;
344   g_configuration->helper_action = action;
345   /*
346    * Create a pipe for helper stdout back to collectd. This is necessary for
347    * logging EAL failures, as rte_eal_init() calls rte_panic().
348    */
349   if (g_configuration->helper_pipes[1]) {
350     DEBUG("dpdkstat: collectd closing helper pipe %d\n",
351           g_configuration->helper_pipes[1]);
352   } else {
353     DEBUG("dpdkstat: collectd helper pipe %d, not closing\n",
354           g_configuration->helper_pipes[1]);
355   }
356   if(pipe(g_configuration->helper_pipes) != 0) {
357     DEBUG("dpdkstat: Could not create helper pipe: %s\n", strerror(errno));
358     return -1;
359   }
360
361   int pipe0_flags = fcntl(g_configuration->helper_pipes[0], F_GETFL, 0);
362   int pipe1_flags = fcntl(g_configuration->helper_pipes[1], F_GETFL, 0);
363   if (pipe0_flags == -1 || pipe1_flags == -1) {
364     ERROR("dpdkstat: error setting up pipe flags: %s\n", strerror(errno));
365   }
366   int  pipe0_err = fcntl(g_configuration->helper_pipes[0], F_SETFL, pipe1_flags
367                          | O_NONBLOCK);
368   int  pipe1_err = fcntl(g_configuration->helper_pipes[1], F_SETFL, pipe0_flags
369                          | O_NONBLOCK);
370   if (pipe0_err == -1 || pipe1_err == -1) {
371     ERROR("dpdkstat: error setting up pipes: %s\n", strerror(errno));
372   }
373
374   pid_t pid = fork();
375   if (pid > 0) {
376     close(g_configuration->helper_pipes[1]);
377     g_configuration->helper_pid = pid;
378     DEBUG("dpdkstat: helper pid %u\n", g_configuration->helper_pid);
379     /* Kick helper once its alive to have it start processing */
380     sem_post(&g_configuration->sema_helper_get_stats);
381   } else if (pid == 0) {
382     /* Replace stdout with a pipe to collectd. */
383     close(g_configuration->helper_pipes[0]);
384     close(STDOUT_FILENO);
385     dup2(g_configuration->helper_pipes[1], STDOUT_FILENO);
386     dpdk_helper_run();
387   } else {
388     ERROR("dpdkstat: Failed to fork helper process: %s\n", strerror(errno));
389     return -1;
390   }
391   return 0;
392 }
393
394 /*
395  * Initialize the DPDK EAL, if this returns, EAL is successfully initialized.
396  * On failure, the EAL prints an error message, and the helper process exits.
397  */
398 static int dpdk_helper_init_eal(void)
399 {
400   g_configuration->helper_status = DPDK_HELPER_INITIALIZING_EAL;
401   char *argp[(g_configuration->eal_argc) + 1];
402   int i = 0;
403
404   argp[i++] = "collectd-dpdk";
405   if (strcasecmp(g_configuration->coremask, "") != 0) {
406     argp[i++] = "-c";
407     argp[i++] = g_configuration->coremask;
408   }
409   if (strcasecmp(g_configuration->memory_channels, "") != 0) {
410     argp[i++] = "-n";
411     argp[i++] = g_configuration->memory_channels;
412   }
413   if (strcasecmp(g_configuration->socket_memory, "") != 0) {
414     argp[i++] = "--socket-mem";
415     argp[i++] = g_configuration->socket_memory;
416   }
417   if (strcasecmp(g_configuration->file_prefix, "") != 0 &&
418      strcasecmp(g_configuration->file_prefix, "/var/run/.rte_config") != 0) {
419     argp[i++] = "--file-prefix";
420     argp[i++] = g_configuration->file_prefix;
421   }
422   if (strcasecmp(g_configuration->process_type, "") != 0) {
423     argp[i++] = "--proc-type";
424     argp[i++] = g_configuration->process_type;
425   }
426   g_configuration->eal_argc = i;
427
428   g_configuration->eal_initialized = 1;
429   int ret = rte_eal_init(g_configuration->eal_argc, argp);
430   if (ret < 0) {
431     g_configuration->eal_initialized = 0;
432     printf("dpdkstat: ERROR initializing EAL ret = %d\n", ret);
433     printf("dpdkstat: EAL arguments: ");
434     for (i=0; i< g_configuration->eal_argc; i++) {
435       printf("%s ", argp[i]);
436     }
437     printf("\n");
438     return ret;
439   }
440   return 0;
441 }
442
443 static int dpdk_helper_run (void)
444 {
445   pid_t ppid = getppid();
446   g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY;
447
448    while (1) {
449     /* sem_timedwait() to avoid blocking forever */
450     struct timespec ts;
451     cdtime_t now = cdtime();
452     cdtime_t half_sec = MS_TO_CDTIME_T(1500);
453     CDTIME_T_TO_TIMESPEC(now + half_sec + g_configuration->interval *2, &ts);
454     int ret = sem_timedwait(&g_configuration->sema_helper_get_stats, &ts);
455
456     if (ret == -1 && errno == ETIMEDOUT) {
457       ERROR("dpdkstat-helper: sem timedwait()"
458              " timeout, did collectd terminate?\n");
459       dpdk_helper_exit(RESET);
460     }
461     /* Parent PID change means collectd died so quit the helper process. */
462     if (ppid != getppid()) {
463       WARNING("dpdkstat-helper: parent PID changed, quitting.\n");
464       dpdk_helper_exit(RESET);
465     }
466
467     /* Checking for DPDK primary process. */
468     if (!rte_eal_primary_proc_alive(g_configuration->file_prefix)) {
469       if(g_configuration->eal_initialized) {
470         WARNING("dpdkstat-helper: no primary alive but EAL initialized:"
471               " quitting.\n");
472         dpdk_helper_exit(RESET);
473       }
474       g_configuration->helper_status = DPDK_HELPER_WAITING_ON_PRIMARY;
475       /* Back to start of while() - waiting for primary process */
476       continue;
477     }
478
479     if (!g_configuration->eal_initialized) {
480       /* Initialize EAL. */
481       int ret = dpdk_helper_init_eal();
482       if(ret != 0) {
483         WARNING("ERROR INITIALIZING EAL\n");
484         dpdk_helper_exit(RESET);
485       }
486     }
487
488     g_configuration->helper_status = DPDK_HELPER_ALIVE_SENDING_STATS;
489
490     uint8_t nb_ports = rte_eth_dev_count();
491     if (nb_ports == 0) {
492       DEBUG("dpdkstat-helper: No DPDK ports available. "
493               "Check bound devices to DPDK driver.\n");
494       return 0;
495     }
496
497     if (nb_ports > RTE_MAX_ETHPORTS)
498       nb_ports = RTE_MAX_ETHPORTS;
499
500     int len = 0, enabled_port_count = 0, num_xstats = 0, i = 0;
501     for (; i < nb_ports; i++) {
502       if (g_configuration->enabled_port_mask & (1 << i)) {
503         if(g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) {
504           len = rte_eth_xstats_get(i, NULL, 0);
505           if (len < 0) {
506             ERROR("dpdkstat-helper: Cannot get xstats count\n");
507             return -1;
508           }
509           num_xstats += len;
510           g_configuration->num_stats_in_port[enabled_port_count] = len;
511           enabled_port_count++;
512           continue;
513         } else {
514           len = g_configuration->num_stats_in_port[enabled_port_count];
515           g_configuration->port_read_time[enabled_port_count] = cdtime();
516           ret = rte_eth_xstats_get(i, &g_configuration->xstats + num_xstats,
517                                    g_configuration->num_stats_in_port[enabled_port_count]);
518           if (ret < 0 || ret != len) {
519             DEBUG("dpdkstat-helper: Error reading xstats on port %d len = %d\n",
520                   i, len);
521             return -1;
522           }
523           num_xstats += g_configuration->num_stats_in_port[enabled_port_count];
524           enabled_port_count++;
525         }
526       } /* if (enabled_port_mask) */
527     } /* for (nb_ports) */
528
529     if (g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) {
530       g_configuration->num_ports  = enabled_port_count;
531       g_configuration->num_xstats = num_xstats;
532       DEBUG("dpdkstat-helper ports: %d, num stats: %d\n",
533             g_configuration->num_ports, g_configuration->num_xstats);
534       /* Exit, allowing collectd to re-init SHM to the right size */
535       g_configuration->collectd_reinit_shm = REINIT_SHM;
536       dpdk_helper_exit(NO_RESET);
537     }
538     /* Now kick collectd send thread to send the stats */
539     int err = sem_post(&g_configuration->sema_stats_in_shm);
540     if (err)
541       ERROR("dpdkstat: error posting semaphore to helper %s\n", strerror(errno));
542   } /* while(1) */
543
544   return 0;
545 }
546
547 static int dpdk_read (user_data_t *ud)
548 {
549   int ret = 0;
550
551   /*
552    * Check if SHM flag is set to be re-initialized. AKA DPDK ports have been
553    * counted, so re-init SHM to be large enough to fit all the statistics.
554    */
555   if (g_configuration->collectd_reinit_shm) {
556     DEBUG("dpdkstat: read() now reinit SHM then launching send-thread\n");
557     dpdk_re_init_shm();
558   }
559
560   /*
561    * Check if DPDK proc is alive, and has already counted port / stats. This
562    * must be done in dpdk_read(), because the DPDK primary process may not be
563    * alive at dpdk_init() time.
564    */
565   if (g_configuration->helper_status == DPDK_HELPER_NOT_INITIALIZED ||
566      g_configuration->helper_status == DPDK_HELPER_GRACEFUL_QUIT) {
567       int action = DPDK_HELPER_ACTION_SEND_STATS;
568       if(g_configuration->num_xstats == 0)
569         action = DPDK_HELPER_ACTION_COUNT_STATS;
570       /* Spawn the helper thread to count stats or to read stats. */
571       int err = dpdk_helper_spawn(action);
572       if (err) {
573         ERROR("dpdkstat: error spawning helper %s\n", strerror(errno));
574         return -1;
575       }
576     }
577
578   int exit_status;
579   pid_t ws = waitpid(g_configuration->helper_pid, &exit_status, WNOHANG);
580   /*
581    * Conditions under which to respawn helper:
582    *  waitpid() fails, helper process died (or quit), so respawn
583    */
584   int respawn_helper = 0;
585   if (ws != 0) {
586     respawn_helper = 1;
587   }
588
589   char buf[DPDKSTAT_MAX_BUFFER_SIZE];
590   char out[DPDKSTAT_MAX_BUFFER_SIZE];
591
592   /* non blocking check on helper logging pipe */
593   struct pollfd fds;
594   fds.fd = g_configuration->helper_pipes[0];
595   fds.events = POLLIN;
596   int data_avail = poll(&fds, 1, 0);
597   while (data_avail) {
598     int nbytes = read(g_configuration->helper_pipes[0], buf, sizeof(buf));
599     if (nbytes <= 0)
600       break;
601     ssnprintf( out, nbytes, "%s", buf);
602     DEBUG("dpdkstat: helper-proc: %s\n", out);
603   }
604
605   if (respawn_helper) {
606     if (g_configuration->helper_pid)
607       dpdk_helper_exit(RESET);
608     dpdk_helper_spawn(DPDK_HELPER_ACTION_COUNT_STATS);
609   }
610
611   struct timespec helper_kick_time;
612   clock_gettime(CLOCK_REALTIME, &helper_kick_time);
613   /* Kick helper process through SHM */
614   sem_post(&g_configuration->sema_helper_get_stats);
615
616   struct timespec ts;
617   cdtime_t now = cdtime();
618   CDTIME_T_TO_TIMESPEC(now + g_configuration->interval, &ts);
619   ret = sem_timedwait(&g_configuration->sema_stats_in_shm, &ts);
620   if (ret == -1 && errno == ETIMEDOUT) {
621     DEBUG("dpdkstat: timeout in collectd thread: is a DPDK Primary running? \n");
622     return 0;
623   }
624
625   /* Dispatch the stats.*/
626   int count = 0, i = 0, port_num = 0;
627
628   for (; i < g_configuration->num_ports; i++) {
629     cdtime_t time = g_configuration->port_read_time[i];
630     char dev_name[64];
631     int len = g_configuration->num_stats_in_port[i];
632
633     while(!(g_configuration->enabled_port_mask & (1 << port_num)))
634       port_num++;
635
636     if (g_configuration->port_name[i][0] != 0)
637       ssnprintf(dev_name, sizeof(dev_name), "%s", g_configuration->port_name[i]);
638     else
639       ssnprintf(dev_name, sizeof(dev_name), "port.%d", port_num);
640     struct rte_eth_xstats *xstats = (&g_configuration->xstats);
641     xstats += count; /* pointer arithmetic to jump to each stats struct */
642     int j = 0;
643     for (; j < len; j++) {
644       value_t dpdkstat_values[1];
645       value_list_t dpdkstat_vl = VALUE_LIST_INIT;
646
647       dpdkstat_values[0].counter = xstats[j].value;
648       dpdkstat_vl.values = dpdkstat_values;
649       dpdkstat_vl.values_len = 1; /* Submit stats one at a time */
650       dpdkstat_vl.time = time;
651       sstrncpy (dpdkstat_vl.host, hostname_g, sizeof (dpdkstat_vl.host));
652       sstrncpy (dpdkstat_vl.plugin, "dpdkstat", sizeof (dpdkstat_vl.plugin));
653       sstrncpy (dpdkstat_vl.plugin_instance, dev_name,
654                 sizeof (dpdkstat_vl.plugin_instance));
655       sstrncpy (dpdkstat_vl.type, "counter",
656                 sizeof (dpdkstat_vl.type));
657       sstrncpy (dpdkstat_vl.type_instance, xstats[j].name,
658                 sizeof (dpdkstat_vl.type_instance));
659       plugin_dispatch_values (&dpdkstat_vl);
660     }
661     count += len;
662     port_num++;
663   } /* for each port */
664   return 0;
665 }
666
667 static int dpdk_shm_cleanup(void)
668 {
669   int ret = munmap(g_configuration, sizeof(dpdk_config_t));
670   g_configuration = 0;
671   if (ret) {
672     WARNING("dpdkstat: munmap returned %d\n", ret);
673     return ret;
674   }
675   ret = shm_unlink(DPDK_SHM_NAME);
676   if (ret) {
677     WARNING("dpdkstat: shm_unlink returned %d\n", ret);
678     return ret;
679   }
680   return 0;
681 }
682
683 static int dpdk_shutdown (void)
684 {
685   int ret = 0;
686   close(g_configuration->helper_pipes[1]);
687   int err = kill(g_configuration->helper_pid, SIGKILL);
688   if (err) {
689     ERROR("dpdkstat: error sending sigkill to helper %s\n", strerror(errno));
690     ret = -1;
691   }
692   err = dpdk_shm_cleanup();
693   if (err) {
694     ERROR("dpdkstat: error cleaning up SHM: %s\n", strerror(errno));
695     ret = -1;
696   }
697
698   return ret;
699 }
700
701 void module_register (void)
702 {
703   plugin_register_complex_config ("dpdkstat", dpdk_config);
704   plugin_register_init ("dpdkstat", dpdk_init);
705   plugin_register_shutdown ("dpdkstat", dpdk_shutdown);
706 }