dpdkevents: detect primary DPDK application (re)start
[collectd.git] / src / dpdkevents.c
1 /*
2  * collectd - src/dpdkevents.c
3  * MIT License
4  *
5  * Copyright(c) 2017 Intel Corporation. All rights reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all 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  *   Serhiy Pshyk <serhiyx.pshyk@intel.com>
29  *   Kim-Marie Jones <kim-marie.jones@intel.com>
30  *   Krzysztof Matczak <krzysztofx@intel.com>
31  */
32
33 #include "collectd.h"
34
35 #include "common.h"
36 #include "plugin.h"
37
38 #include "semaphore.h"
39 #include "sys/mman.h"
40 #include "utils_dpdk.h"
41 #include "utils_time.h"
42
43 #include <rte_config.h>
44 #include <rte_eal.h>
45 #include <rte_ethdev.h>
46 #include <rte_keepalive.h>
47
48 #define DPDK_EVENTS_PLUGIN "dpdkevents"
49 #define DPDK_EVENTS_NAME "dpdk_collectd_events"
50 #define ETH_LINK_NA 0xFF
51
52 #define INT64_BIT_SIZE 64
53 #define KEEPALIVE_PLUGIN_INSTANCE "keepalive"
54 #define RTE_KEEPALIVE_SHM_NAME "/dpdk_keepalive_shm_name"
55
56 typedef struct dpdk_keepalive_shm_s {
57   sem_t core_died;
58   enum rte_keepalive_state core_state[RTE_KEEPALIVE_MAXCORES];
59   uint64_t core_last_seen_times[RTE_KEEPALIVE_MAXCORES];
60 } dpdk_keepalive_shm_t;
61
62 typedef struct dpdk_ka_monitor_s {
63   cdtime_t read_time;
64   int lcore_state;
65 } dpdk_ka_monitor_t;
66
67 typedef struct dpdk_link_status_config_s {
68   int enabled;
69   int send_updated;
70   uint32_t enabled_port_mask;
71   char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
72   int notify;
73 } dpdk_link_status_config_t;
74
75 typedef struct dpdk_keep_alive_config_s {
76   int enabled;
77   int send_updated;
78   uint128_t lcore_mask;
79   dpdk_keepalive_shm_t *shm;
80   char shm_name[DATA_MAX_NAME_LEN];
81   int notify;
82   int fd;
83 } dpdk_keep_alive_config_t;
84
85 typedef struct dpdk_events_config_s {
86   cdtime_t interval;
87   dpdk_link_status_config_t link_status;
88   dpdk_keep_alive_config_t keep_alive;
89 } dpdk_events_config_t;
90
91 typedef struct dpdk_link_info_s {
92   cdtime_t read_time;
93   int status_updated;
94   int link_status;
95 } dpdk_link_info_t;
96
97 typedef struct dpdk_events_ctx_s {
98   dpdk_events_config_t config;
99   uint32_t nb_ports;
100   dpdk_link_info_t link_info[RTE_MAX_ETHPORTS];
101   dpdk_ka_monitor_t core_info[RTE_KEEPALIVE_MAXCORES];
102 } dpdk_events_ctx_t;
103
104 #define DPDK_EVENTS_CTX_GET(a) ((dpdk_events_ctx_t *)dpdk_helper_priv_get(a))
105
106 #define DPDK_EVENTS_TRACE()                                                    \
107   DEBUG("%s:%s:%d pid=%u", DPDK_EVENTS_PLUGIN, __FUNCTION__, __LINE__, getpid())
108
109 static dpdk_helper_ctx_t *g_hc;
110
111 static int dpdk_event_keep_alive_shm_open(void) {
112   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
113   char *shm_name;
114
115   if (strlen(ec->config.keep_alive.shm_name)) {
116     shm_name = ec->config.keep_alive.shm_name;
117   } else {
118     shm_name = RTE_KEEPALIVE_SHM_NAME;
119     WARNING(DPDK_EVENTS_PLUGIN ": Keep alive shared memory identifier is not "
120                                "specified, using default one: %s",
121             shm_name);
122   }
123
124   char errbuf[ERR_BUF_SIZE];
125   int fd = shm_open(shm_name, O_RDONLY, 0);
126   if (fd < 0) {
127     ERROR(DPDK_EVENTS_PLUGIN ": Failed to open %s as SHM:%s. Is DPDK KA "
128                              "primary application running?",
129           shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
130     return errno;
131   }
132
133   if (ec->config.keep_alive.fd != -1) {
134     struct stat stat_old, stat_new;
135
136     if (fstat(ec->config.keep_alive.fd, &stat_old) || fstat(fd, &stat_new)) {
137       ERROR(DPDK_EVENTS_PLUGIN ": failed to get information about a file");
138       close(fd);
139       return -1;
140     }
141
142     /* Check if inode number has changed. If yes, then create a new mapping */
143     if (stat_old.st_ino == stat_new.st_ino) {
144       close(fd);
145       return 0;
146     }
147
148     if (munmap(ec->config.keep_alive.shm, sizeof(dpdk_keepalive_shm_t)) != 0) {
149       ERROR(DPDK_EVENTS_PLUGIN ": munmap KA monitor failed");
150       close(fd);
151       return -1;
152     }
153
154     close(ec->config.keep_alive.fd);
155     ec->config.keep_alive.fd = -1;
156   }
157
158   ec->config.keep_alive.shm = (dpdk_keepalive_shm_t *)mmap(
159       0, sizeof(*(ec->config.keep_alive.shm)), PROT_READ, MAP_SHARED, fd, 0);
160   if (ec->config.keep_alive.shm == MAP_FAILED) {
161     ERROR(DPDK_EVENTS_PLUGIN ": Failed to mmap KA SHM:%s",
162           sstrerror(errno, errbuf, sizeof(errbuf)));
163     close(fd);
164     return errno;
165   }
166   ec->config.keep_alive.fd = fd;
167
168   return 0;
169 }
170
171 static void dpdk_events_default_config(void) {
172   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
173
174   ec->config.interval = plugin_get_interval();
175
176   /* Link Status */
177   ec->config.link_status.enabled = 0;
178   ec->config.link_status.enabled_port_mask = ~0;
179   ec->config.link_status.send_updated = 1;
180   ec->config.link_status.notify = 0;
181
182   for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
183     ec->config.link_status.port_name[i][0] = 0;
184   }
185
186   /* Keep Alive */
187   ec->config.keep_alive.enabled = 0;
188   ec->config.keep_alive.send_updated = 1;
189   ec->config.keep_alive.notify = 0;
190   memset(&ec->config.keep_alive.lcore_mask, 0,
191          sizeof(ec->config.keep_alive.lcore_mask));
192   memset(&ec->config.keep_alive.shm_name, 0,
193          sizeof(ec->config.keep_alive.shm_name));
194   ec->config.keep_alive.shm = MAP_FAILED;
195   ec->config.keep_alive.fd = -1;
196 }
197
198 static int dpdk_events_preinit(void) {
199   DPDK_EVENTS_TRACE();
200
201   if (g_hc != NULL) {
202     /* already initialized if config callback was called before init callback */
203     DEBUG("dpdk_events_preinit: helper already initialized.");
204     return 0;
205   }
206
207   int ret =
208       dpdk_helper_init(DPDK_EVENTS_NAME, sizeof(dpdk_events_ctx_t), &g_hc);
209   if (ret != 0) {
210     ERROR(DPDK_EVENTS_PLUGIN ": failed to initialize %s helper(error: %s)",
211           DPDK_EVENTS_NAME, strerror(ret));
212     return ret;
213   }
214
215   dpdk_events_default_config();
216
217   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
218   for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
219     ec->link_info[i].link_status = ETH_LINK_NA;
220   }
221
222   for (int i = 0; i < RTE_KEEPALIVE_MAXCORES; i++) {
223     ec->core_info[i].lcore_state = ETH_LINK_NA;
224   }
225
226   return ret;
227 }
228
229 static int dpdk_events_link_status_config(dpdk_events_ctx_t *ec,
230                                           oconfig_item_t *ci) {
231   ec->config.link_status.enabled = 1;
232
233   DEBUG(DPDK_EVENTS_PLUGIN ": Subscribed for Link Status Events.");
234
235   for (int i = 0; i < ci->children_num; i++) {
236     oconfig_item_t *child = ci->children + i;
237
238     if (strcasecmp("EnabledPortMask", child->key) == 0) {
239       ec->config.link_status.enabled_port_mask =
240           (uint32_t)child->values[0].value.number;
241       DEBUG(DPDK_EVENTS_PLUGIN ": LinkStatus:Enabled Port Mask 0x%X",
242             ec->config.link_status.enabled_port_mask);
243     } else if (strcasecmp("SendEventsOnUpdate", child->key) == 0) {
244       ec->config.link_status.send_updated = child->values[0].value.boolean;
245       DEBUG(DPDK_EVENTS_PLUGIN ": LinkStatus:SendEventsOnUpdate %d",
246             (int)child->values[0].value.boolean);
247     } else if (strcasecmp("SendNotification", child->key) == 0) {
248       ec->config.link_status.notify = child->values[0].value.boolean;
249       DEBUG(DPDK_EVENTS_PLUGIN ": LinkStatus:SendNotification %d",
250             (int)child->values[0].value.boolean);
251     }
252   }
253
254   int port_num = 0;
255
256   /* parse port names after EnabledPortMask was parsed */
257   for (int i = 0; i < ci->children_num; i++) {
258     oconfig_item_t *child = ci->children + i;
259     if (strcasecmp("PortName", child->key) == 0) {
260       while (!(ec->config.link_status.enabled_port_mask & (1 << port_num)))
261         port_num++;
262       ssnprintf(ec->config.link_status.port_name[port_num], DATA_MAX_NAME_LEN,
263                 "%s", child->values[0].value.string);
264       DEBUG(DPDK_EVENTS_PLUGIN ": LinkStatus:Port %d Name: %s", port_num,
265             ec->config.link_status.port_name[port_num]);
266       port_num++;
267     }
268   }
269
270   return 0;
271 }
272
273 static int dpdk_events_keep_alive_config(dpdk_events_ctx_t *ec,
274                                          oconfig_item_t *ci) {
275   ec->config.keep_alive.enabled = 1;
276   DEBUG(DPDK_EVENTS_PLUGIN ": Subscribed for Keep Alive Events.");
277
278   for (int i = 0; i < ci->children_num; i++) {
279     oconfig_item_t *child = ci->children + i;
280
281     if (strcasecmp("SendEventsOnUpdate", child->key) == 0) {
282       ec->config.keep_alive.send_updated = child->values[0].value.boolean;
283       DEBUG(DPDK_EVENTS_PLUGIN ": KeepAlive:SendEventsOnUpdate %d",
284             (int)child->values[0].value.boolean);
285     } else if (strcasecmp("LCoreMask", child->key) == 0) {
286       char lcore_mask[DATA_MAX_NAME_LEN];
287       ssnprintf(lcore_mask, sizeof(lcore_mask), "%s",
288                 child->values[0].value.string);
289       ec->config.keep_alive.lcore_mask =
290           str_to_uint128(lcore_mask, strlen(lcore_mask));
291       DEBUG(DPDK_EVENTS_PLUGIN ": KeepAlive:LCoreMask 0x%" PRIX64 "%" PRIX64 "",
292             ec->config.keep_alive.lcore_mask.high,
293             ec->config.keep_alive.lcore_mask.low);
294     } else if (strcasecmp("KeepAliveShmName", child->key) == 0) {
295       ssnprintf(ec->config.keep_alive.shm_name,
296                 sizeof(ec->config.keep_alive.shm_name), "%s",
297                 child->values[0].value.string);
298       DEBUG(DPDK_EVENTS_PLUGIN ": KeepAlive:KeepAliveShmName %s",
299             ec->config.keep_alive.shm_name);
300     } else if (strcasecmp("SendNotification", child->key) == 0) {
301       ec->config.keep_alive.notify = child->values[0].value.boolean;
302       DEBUG(DPDK_EVENTS_PLUGIN ": KeepAlive:SendNotification %d",
303             (int)child->values[0].value.boolean);
304     }
305   }
306
307   return 0;
308 }
309
310 static int dpdk_events_config(oconfig_item_t *ci) {
311   DPDK_EVENTS_TRACE();
312
313   int ret = dpdk_events_preinit();
314   if (ret)
315     return ret;
316
317   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
318
319   for (int i = 0; i < ci->children_num; i++) {
320     oconfig_item_t *child = ci->children + i;
321     if (strcasecmp("EAL", child->key) == 0) {
322       dpdk_helper_eal_config_parse(g_hc, child);
323     } else if (strcasecmp("Event", child->key) == 0) {
324       if (strcasecmp(child->values[0].value.string, "link_status") == 0) {
325         dpdk_events_link_status_config(ec, child);
326       } else if (strcasecmp(child->values[0].value.string, "keep_alive") == 0) {
327         dpdk_events_keep_alive_config(ec, child);
328       } else {
329         ERROR(DPDK_EVENTS_PLUGIN ": The selected event \"%s\" is unknown.",
330               child->values[0].value.string);
331       }
332     }
333   }
334
335   if (!ec->config.keep_alive.enabled && !ec->config.link_status.enabled) {
336     ERROR(DPDK_EVENTS_PLUGIN ": At least one type of events should be "
337                              "configured for collecting. Plugin misconfigured");
338     return -1;
339   }
340
341   return ret;
342 }
343
344 static int dpdk_helper_link_status_get(dpdk_helper_ctx_t *phc) {
345   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
346
347   /* get Link Status values from DPDK */
348   uint8_t nb_ports = rte_eth_dev_count();
349   if (nb_ports == 0) {
350     DPDK_CHILD_LOG("dpdkevent-helper: No DPDK ports available. "
351                    "Check bound devices to DPDK driver.\n");
352     return -ENODEV;
353   }
354   ec->nb_ports = nb_ports > RTE_MAX_ETHPORTS ? RTE_MAX_ETHPORTS : nb_ports;
355
356   for (int i = 0; i < ec->nb_ports; i++) {
357     if (ec->config.link_status.enabled_port_mask & (1 << i)) {
358       struct rte_eth_link link;
359       ec->link_info[i].read_time = cdtime();
360       rte_eth_link_get_nowait(i, &link);
361       if ((link.link_status == ETH_LINK_NA) ||
362           (link.link_status != ec->link_info[i].link_status)) {
363         ec->link_info[i].link_status = link.link_status;
364         ec->link_info[i].status_updated = 1;
365         DPDK_CHILD_LOG(" === PORT %d Link Status: %s\n", i,
366                        link.link_status ? "UP" : "DOWN");
367       }
368     }
369   }
370
371   return 0;
372 }
373
374 /* this function is called from helper context */
375 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
376   if (phc == NULL) {
377     DPDK_CHILD_LOG(DPDK_EVENTS_PLUGIN ": Invalid argument(phc)\n");
378     return -EINVAL;
379   }
380
381   if (cmd != DPDK_CMD_GET_EVENTS) {
382     DPDK_CHILD_LOG(DPDK_EVENTS_PLUGIN ": Unknown command (cmd=%d)\n", cmd);
383     return -EINVAL;
384   }
385
386   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
387   int ret = 0;
388   if (ec->config.link_status.enabled)
389     ret = dpdk_helper_link_status_get(phc);
390
391   return ret;
392 }
393
394 static void dpdk_events_notification_dispatch(int severity,
395                                               const char *plugin_instance,
396                                               cdtime_t time, const char *msg) {
397   notification_t n = {
398       .severity = severity, .time = time, .plugin = DPDK_EVENTS_PLUGIN};
399   sstrncpy(n.host, hostname_g, sizeof(n.host));
400   sstrncpy(n.plugin_instance, plugin_instance, sizeof(n.plugin_instance));
401   sstrncpy(n.message, msg, sizeof(n.message));
402   plugin_dispatch_notification(&n);
403 }
404
405 static void dpdk_events_gauge_submit(const char *plugin_instance,
406                                      const char *type_instance, gauge_t value,
407                                      cdtime_t time) {
408   value_list_t vl = {.values = &(value_t){.gauge = value},
409                      .values_len = 1,
410                      .time = time,
411                      .plugin = DPDK_EVENTS_PLUGIN,
412                      .type = "gauge",
413                      .meta = NULL};
414   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
415   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
416   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
417   plugin_dispatch_values(&vl);
418 }
419
420 static int dpdk_events_link_status_dispatch(dpdk_helper_ctx_t *phc) {
421   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
422   DEBUG(DPDK_EVENTS_PLUGIN ": %s:%d ports=%u", __FUNCTION__, __LINE__,
423         ec->nb_ports);
424
425   /* dispatch Link Status values to collectd */
426   for (int i = 0; i < ec->nb_ports; i++) {
427     if (ec->config.link_status.enabled_port_mask & (1 << i)) {
428       if (!ec->config.link_status.send_updated ||
429           ec->link_info[i].status_updated) {
430
431         DEBUG(DPDK_EVENTS_PLUGIN ": Dispatch PORT %d Link Status: %s", i,
432               ec->link_info[i].link_status ? "UP" : "DOWN");
433
434         char dev_name[DATA_MAX_NAME_LEN];
435         if (ec->config.link_status.port_name[i][0] != 0) {
436           ssnprintf(dev_name, sizeof(dev_name), "%s",
437                     ec->config.link_status.port_name[i]);
438         } else {
439           ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
440         }
441
442         if (ec->config.link_status.notify) {
443           int sev = ec->link_info[i].link_status ? NOTIF_OKAY : NOTIF_WARNING;
444           char msg[DATA_MAX_NAME_LEN];
445           ssnprintf(msg, sizeof(msg), "Link Status: %s",
446                     ec->link_info[i].link_status ? "UP" : "DOWN");
447           dpdk_events_notification_dispatch(sev, dev_name,
448                                             ec->link_info[i].read_time, msg);
449         } else {
450           dpdk_events_gauge_submit(dev_name, "link_status",
451                                    (gauge_t)ec->link_info[i].link_status,
452                                    ec->link_info[i].read_time);
453         }
454         ec->link_info[i].status_updated = 0;
455       }
456     }
457   }
458
459   return 0;
460 }
461
462 static void dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
463   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
464
465   /* dispatch Keep Alive values to collectd */
466   for (int i = 0; i < RTE_KEEPALIVE_MAXCORES; i++) {
467     if (i < INT64_BIT_SIZE) {
468       if (!(ec->config.keep_alive.lcore_mask.low & ((uint64_t)1 << i)))
469         continue;
470     } else if (i >= INT64_BIT_SIZE && i < INT64_BIT_SIZE * 2) {
471       if (!(ec->config.keep_alive.lcore_mask.high &
472             ((uint64_t)1 << (i - INT64_BIT_SIZE))))
473         continue;
474     } else {
475       WARNING(DPDK_EVENTS_PLUGIN
476               ": %s:%d Core id %u is out of 0 to %u range, skipping",
477               __FUNCTION__, __LINE__, i, INT64_BIT_SIZE * 2);
478       continue;
479     }
480
481     char core_name[DATA_MAX_NAME_LEN];
482     ssnprintf(core_name, sizeof(core_name), "lcore%u", i);
483
484     if (!ec->config.keep_alive.send_updated ||
485         (ec->core_info[i].lcore_state !=
486          ec->config.keep_alive.shm->core_state[i])) {
487       ec->core_info[i].lcore_state = ec->config.keep_alive.shm->core_state[i];
488       ec->core_info[i].read_time = cdtime();
489
490       if (ec->config.keep_alive.notify) {
491         char msg[DATA_MAX_NAME_LEN];
492         int sev;
493
494         switch (ec->config.keep_alive.shm->core_state[i]) {
495         case RTE_KA_STATE_ALIVE:
496           sev = NOTIF_OKAY;
497           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
498           break;
499         case RTE_KA_STATE_MISSING:
500           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
501           sev = NOTIF_WARNING;
502           break;
503         case RTE_KA_STATE_DEAD:
504           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
505           sev = NOTIF_FAILURE;
506           break;
507         case RTE_KA_STATE_UNUSED:
508           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
509           sev = NOTIF_OKAY;
510           break;
511         case RTE_KA_STATE_GONE:
512           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
513           sev = NOTIF_FAILURE;
514           break;
515         case RTE_KA_STATE_DOZING:
516           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
517           sev = NOTIF_OKAY;
518           break;
519         case RTE_KA_STATE_SLEEP:
520           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
521           sev = NOTIF_OKAY;
522           break;
523         default:
524           ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
525           sev = NOTIF_FAILURE;
526         }
527
528         dpdk_events_notification_dispatch(sev, KEEPALIVE_PLUGIN_INSTANCE,
529                                           ec->core_info[i].read_time, msg);
530       } else {
531         dpdk_events_gauge_submit(KEEPALIVE_PLUGIN_INSTANCE, core_name,
532                                  ec->config.keep_alive.shm->core_state[i],
533                                  ec->core_info[i].read_time);
534       }
535     }
536   }
537 }
538
539 static int dpdk_events_read(user_data_t *ud) {
540   DPDK_EVENTS_TRACE();
541
542   if (g_hc == NULL) {
543     ERROR(DPDK_EVENTS_PLUGIN ": plugin not initialized.");
544     return -1;
545   }
546
547   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
548
549   if (ec->config.link_status.enabled) {
550     int cmd_res = 0;
551     int ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_EVENTS, &cmd_res,
552                                   ec->config.interval);
553     if (cmd_res == 0 && ret == 0) {
554       dpdk_events_link_status_dispatch(g_hc);
555     }
556   }
557
558   if (ec->config.keep_alive.enabled) {
559     int ret = dpdk_event_keep_alive_shm_open();
560     if (ret) {
561       ERROR(DPDK_EVENTS_PLUGIN
562             ": %s : error %d in dpdk_event_keep_alive_shm_open()",
563             __FUNCTION__, ret);
564       return ret;
565     }
566     dpdk_events_keep_alive_dispatch(g_hc);
567   }
568
569   return 0;
570 }
571
572 static int dpdk_events_init(void) {
573   DPDK_EVENTS_TRACE();
574
575   int ret = dpdk_events_preinit();
576   if (ret)
577     return ret;
578
579   return 0;
580 }
581
582 static int dpdk_events_shutdown(void) {
583   DPDK_EVENTS_TRACE();
584   int ret;
585
586   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
587   if (ec->config.keep_alive.enabled) {
588     if (ec->config.keep_alive.fd != -1) {
589       close(ec->config.keep_alive.fd);
590       ec->config.keep_alive.fd = -1;
591     }
592
593     if (ec->config.keep_alive.shm != MAP_FAILED) {
594       if (munmap(ec->config.keep_alive.shm, sizeof(dpdk_keepalive_shm_t))) {
595         ERROR(DPDK_EVENTS_PLUGIN ": munmap KA monitor failed");
596         return -1;
597       }
598       ec->config.keep_alive.shm = MAP_FAILED;
599     }
600   }
601
602   ret = dpdk_helper_shutdown(g_hc);
603   g_hc = NULL;
604   if (ret)
605     ERROR(DPDK_EVENTS_PLUGIN ": failed to cleanup %s helper", DPDK_EVENTS_NAME);
606
607   return ret;
608 }
609
610 void module_register(void) {
611   plugin_register_init(DPDK_EVENTS_PLUGIN, dpdk_events_init);
612   plugin_register_complex_config(DPDK_EVENTS_PLUGIN, dpdk_events_config);
613   plugin_register_complex_read(NULL, DPDK_EVENTS_PLUGIN, dpdk_events_read, 0,
614                                NULL);
615   plugin_register_shutdown(DPDK_EVENTS_PLUGIN, dpdk_events_shutdown);
616 }