2 * collectd - src/dpdkevents.c
5 * Copyright(c) 2017 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 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
29 * Kim-Marie Jones <kim-marie.jones@intel.com>
30 * Krzysztof Matczak <krzysztofx@intel.com>
38 #include "semaphore.h"
40 #include "utils_dpdk.h"
41 #include "utils_time.h"
43 #include <rte_config.h>
45 #include <rte_ethdev.h>
46 #include <rte_keepalive.h>
48 #define DPDK_EVENTS_PLUGIN "dpdkevents"
49 #define DPDK_EVENTS_NAME "dpdk_collectd_events"
50 #define ETH_LINK_NA 0xFF
52 #define INT64_BIT_SIZE 64
53 #define KEEPALIVE_PLUGIN_INSTANCE "keepalive"
54 #define RTE_KEEPALIVE_SHM_NAME "/dpdk_keepalive_shm_name"
56 typedef struct dpdk_keepalive_shm_s {
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;
62 typedef struct dpdk_ka_monitor_s {
67 typedef struct dpdk_link_status_config_s {
70 uint32_t enabled_port_mask;
71 char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
73 } dpdk_link_status_config_t;
75 typedef struct dpdk_keep_alive_config_s {
79 dpdk_keepalive_shm_t *shm;
80 char shm_name[DATA_MAX_NAME_LEN];
83 } dpdk_keep_alive_config_t;
85 typedef struct dpdk_events_config_s {
87 dpdk_link_status_config_t link_status;
88 dpdk_keep_alive_config_t keep_alive;
89 } dpdk_events_config_t;
91 typedef struct dpdk_link_info_s {
97 typedef struct dpdk_events_ctx_s {
98 dpdk_events_config_t config;
100 dpdk_link_info_t link_info[RTE_MAX_ETHPORTS];
101 dpdk_ka_monitor_t core_info[RTE_KEEPALIVE_MAXCORES];
104 #define DPDK_EVENTS_CTX_GET(a) ((dpdk_events_ctx_t *)dpdk_helper_priv_get(a))
106 #define DPDK_EVENTS_TRACE() \
107 DEBUG("%s:%s:%d pid=%u", DPDK_EVENTS_PLUGIN, __FUNCTION__, __LINE__, getpid())
109 static dpdk_helper_ctx_t *g_hc;
111 static int dpdk_event_keep_alive_shm_open(void) {
112 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
115 if (strlen(ec->config.keep_alive.shm_name)) {
116 shm_name = ec->config.keep_alive.shm_name;
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",
124 char errbuf[ERR_BUF_SIZE];
125 int fd = shm_open(shm_name, O_RDONLY, 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)));
133 if (ec->config.keep_alive.fd != -1) {
134 struct stat stat_old, stat_new;
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");
142 /* Check if inode number has changed. If yes, then create a new mapping */
143 if (stat_old.st_ino == stat_new.st_ino) {
148 if (munmap(ec->config.keep_alive.shm, sizeof(dpdk_keepalive_shm_t)) != 0) {
149 ERROR(DPDK_EVENTS_PLUGIN ": munmap KA monitor failed");
154 close(ec->config.keep_alive.fd);
155 ec->config.keep_alive.fd = -1;
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)));
166 ec->config.keep_alive.fd = fd;
171 static void dpdk_events_default_config(void) {
172 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
174 ec->config.interval = plugin_get_interval();
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;
182 for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
183 ec->config.link_status.port_name[i][0] = 0;
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;
198 static int dpdk_events_preinit(void) {
202 /* already initialized if config callback was called before init callback */
203 DEBUG("dpdk_events_preinit: helper already initialized.");
208 dpdk_helper_init(DPDK_EVENTS_NAME, sizeof(dpdk_events_ctx_t), &g_hc);
210 ERROR(DPDK_EVENTS_PLUGIN ": failed to initialize %s helper(error: %s)",
211 DPDK_EVENTS_NAME, strerror(ret));
215 dpdk_events_default_config();
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;
222 for (int i = 0; i < RTE_KEEPALIVE_MAXCORES; i++) {
223 ec->core_info[i].lcore_state = ETH_LINK_NA;
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;
233 DEBUG(DPDK_EVENTS_PLUGIN ": Subscribed for Link Status Events.");
235 for (int i = 0; i < ci->children_num; i++) {
236 oconfig_item_t *child = ci->children + i;
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);
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)))
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]);
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.");
278 for (int i = 0; i < ci->children_num; i++) {
279 oconfig_item_t *child = ci->children + i;
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);
310 static int dpdk_events_config(oconfig_item_t *ci) {
313 int ret = dpdk_events_preinit();
317 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
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);
329 ERROR(DPDK_EVENTS_PLUGIN ": The selected event \"%s\" is unknown.",
330 child->values[0].value.string);
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");
344 static int dpdk_helper_link_status_get(dpdk_helper_ctx_t *phc) {
345 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
347 /* get Link Status values from DPDK */
348 uint8_t nb_ports = rte_eth_dev_count();
350 DPDK_CHILD_LOG("dpdkevent-helper: No DPDK ports available. "
351 "Check bound devices to DPDK driver.\n");
354 ec->nb_ports = nb_ports > RTE_MAX_ETHPORTS ? RTE_MAX_ETHPORTS : nb_ports;
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");
374 /* this function is called from helper context */
375 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
377 DPDK_CHILD_LOG(DPDK_EVENTS_PLUGIN ": Invalid argument(phc)\n");
381 if (cmd != DPDK_CMD_GET_EVENTS) {
382 DPDK_CHILD_LOG(DPDK_EVENTS_PLUGIN ": Unknown command (cmd=%d)\n", cmd);
386 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
388 if (ec->config.link_status.enabled)
389 ret = dpdk_helper_link_status_get(phc);
394 static void dpdk_events_notification_dispatch(int severity,
395 const char *plugin_instance,
396 cdtime_t time, const char *msg) {
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);
405 static void dpdk_events_gauge_submit(const char *plugin_instance,
406 const char *type_instance, gauge_t value,
408 value_list_t vl = {.values = &(value_t){.gauge = value},
411 .plugin = DPDK_EVENTS_PLUGIN,
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);
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__,
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) {
431 DEBUG(DPDK_EVENTS_PLUGIN ": Dispatch PORT %d Link Status: %s", i,
432 ec->link_info[i].link_status ? "UP" : "DOWN");
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]);
439 ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
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);
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);
454 ec->link_info[i].status_updated = 0;
462 static void dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
463 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
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)))
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))))
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);
481 char core_name[DATA_MAX_NAME_LEN];
482 ssnprintf(core_name, sizeof(core_name), "lcore%u", i);
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();
490 if (ec->config.keep_alive.notify) {
491 char msg[DATA_MAX_NAME_LEN];
494 switch (ec->config.keep_alive.shm->core_state[i]) {
495 case RTE_KA_STATE_ALIVE:
497 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
499 case RTE_KA_STATE_MISSING:
500 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
503 case RTE_KA_STATE_DEAD:
504 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
507 case RTE_KA_STATE_UNUSED:
508 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
511 case RTE_KA_STATE_GONE:
512 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
515 case RTE_KA_STATE_DOZING:
516 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
519 case RTE_KA_STATE_SLEEP:
520 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
524 ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
528 dpdk_events_notification_dispatch(sev, KEEPALIVE_PLUGIN_INSTANCE,
529 ec->core_info[i].read_time, msg);
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);
539 static int dpdk_events_read(user_data_t *ud) {
543 ERROR(DPDK_EVENTS_PLUGIN ": plugin not initialized.");
547 dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
549 if (ec->config.link_status.enabled) {
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);
558 if (ec->config.keep_alive.enabled) {
559 int ret = dpdk_event_keep_alive_shm_open();
561 ERROR(DPDK_EVENTS_PLUGIN
562 ": %s : error %d in dpdk_event_keep_alive_shm_open()",
566 dpdk_events_keep_alive_dispatch(g_hc);
572 static int dpdk_events_init(void) {
575 int ret = dpdk_events_preinit();
582 static int dpdk_events_shutdown(void) {
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;
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");
598 ec->config.keep_alive.shm = MAP_FAILED;
602 ret = dpdk_helper_shutdown(g_hc);
605 ERROR(DPDK_EVENTS_PLUGIN ": failed to cleanup %s helper", DPDK_EVENTS_NAME);
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,
615 plugin_register_shutdown(DPDK_EVENTS_PLUGIN, dpdk_events_shutdown);