2 * collectd - src/utils_dpdk.c
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Maryam Tahhan <maryam.tahhan@intel.com>
27 * Harry van Haaren <harry.van.haaren@intel.com>
28 * Taras Chornyi <tarasx.chornyi@intel.com>
29 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
30 * Krzysztof Matczak <krzysztofx.matczak@intel.com>
36 #include <semaphore.h>
39 #include <rte_config.h>
41 #include <rte_ethdev.h>
44 #include "utils_dpdk.h"
46 #define DPDK_DEFAULT_RTE_CONFIG "/var/run/.rte_config"
47 #define DPDK_EAL_ARGC 10
48 // Complete trace should fit into 1024 chars
49 #define DPDK_MAX_BUFFER_SIZE 896
50 #define DPDK_CDM_DEFAULT_TIMEOUT 10000
52 enum DPDK_HELPER_STATUS {
53 DPDK_HELPER_NOT_INITIALIZED = 0,
54 DPDK_HELPER_INITIALIZING,
55 DPDK_HELPER_WAITING_ON_PRIMARY,
56 DPDK_HELPER_INITIALIZING_EAL,
57 DPDK_HELPER_ALIVE_SENDING_EVENTS,
58 DPDK_HELPER_GRACEFUL_QUIT,
61 #define DPDK_HELPER_TRACE(_name) \
62 DEBUG("%s:%s:%d pid=%ld", _name, __FUNCTION__, __LINE__, (long)getpid())
64 struct dpdk_helper_ctx_s {
66 dpdk_eal_config_t eal_config;
70 char shm_name[DATA_MAX_NAME_LEN];
73 sem_t sema_cmd_complete;
74 cdtime_t cmd_wait_time;
86 static int dpdk_shm_init(const char *name, size_t size, void **map);
87 static void dpdk_shm_cleanup(const char *name, size_t size, void *map);
89 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc);
90 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc);
91 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc);
92 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid);
93 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
94 enum DPDK_HELPER_STATUS status);
95 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
96 enum DPDK_HELPER_STATUS status);
97 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc);
98 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc);
99 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status);
101 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc) {
105 DPDK_HELPER_TRACE(phc->shm_name);
107 snprintf(phc->eal_config.coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
108 snprintf(phc->eal_config.memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
109 snprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN, "%s",
110 DPDK_DEFAULT_RTE_CONFIG);
113 int dpdk_helper_eal_config_set(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
115 ERROR("Invalid argument (phc)");
119 DPDK_HELPER_TRACE(phc->shm_name);
122 ERROR("Invalid argument (ec)");
126 memcpy(&phc->eal_config, ec, sizeof(phc->eal_config));
131 int dpdk_helper_eal_config_get(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
133 ERROR("Invalid argument (phc)");
137 DPDK_HELPER_TRACE(phc->shm_name);
140 ERROR("Invalid argument (ec)");
144 memcpy(ec, &phc->eal_config, sizeof(*ec));
149 int dpdk_helper_eal_config_parse(dpdk_helper_ctx_t *phc, oconfig_item_t *ci) {
150 DPDK_HELPER_TRACE(phc->shm_name);
153 ERROR("Invalid argument (phc)");
158 ERROR("Invalid argument (ci)");
163 for (int i = 0; i < ci->children_num; i++) {
164 oconfig_item_t *child = ci->children + i;
166 if (strcasecmp("Coremask", child->key) == 0) {
167 status = cf_util_get_string_buffer(child, phc->eal_config.coremask,
168 sizeof(phc->eal_config.coremask));
169 DEBUG("dpdk_common: EAL:Coremask %s", phc->eal_config.coremask);
170 } else if (strcasecmp("MemoryChannels", child->key) == 0) {
172 cf_util_get_string_buffer(child, phc->eal_config.memory_channels,
173 sizeof(phc->eal_config.memory_channels));
174 DEBUG("dpdk_common: EAL:Memory Channels %s",
175 phc->eal_config.memory_channels);
176 } else if (strcasecmp("SocketMemory", child->key) == 0) {
177 status = cf_util_get_string_buffer(child, phc->eal_config.socket_memory,
178 sizeof(phc->eal_config.socket_memory));
179 DEBUG("dpdk_common: EAL:Socket memory %s", phc->eal_config.socket_memory);
180 } else if (strcasecmp("FilePrefix", child->key) == 0) {
181 char prefix[DATA_MAX_NAME_LEN];
183 status = cf_util_get_string_buffer(child, prefix, sizeof(prefix));
185 snprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN,
186 "/var/run/.%s_config", prefix);
187 DEBUG("dpdk_common: EAL:File prefix %s", phc->eal_config.file_prefix);
189 } else if (strcasecmp("LogLevel", child->key) == 0) {
190 status = cf_util_get_string_buffer(child, phc->eal_config.log_level,
191 sizeof(phc->eal_config.log_level));
192 DEBUG("dpdk_common: EAL:LogLevel %s", phc->eal_config.log_level);
193 } else if (strcasecmp("RteDriverLibPath", child->key) == 0) {
194 status = cf_util_get_string_buffer(
195 child, phc->eal_config.rte_driver_lib_path,
196 sizeof(phc->eal_config.rte_driver_lib_path));
197 DEBUG("dpdk_common: EAL:RteDriverLibPath %s",
198 phc->eal_config.rte_driver_lib_path);
200 ERROR("dpdk_common: Invalid '%s' configuration option", child->key);
205 ERROR("dpdk_common: Parsing EAL configuration failed");
213 static int dpdk_shm_init(const char *name, size_t size, void **map) {
214 DPDK_HELPER_TRACE(name);
216 char errbuf[ERR_BUF_SIZE];
218 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
220 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s", name,
221 sstrerror(errno, errbuf, sizeof(errbuf)));
226 int ret = ftruncate(fd, size);
228 WARNING("dpdk_shm_init: Failed to resize SHM:%s",
229 sstrerror(errno, errbuf, sizeof(errbuf)));
232 dpdk_shm_cleanup(name, size, NULL);
236 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
237 if (*map == MAP_FAILED) {
238 WARNING("dpdk_shm_init:Failed to mmap SHM:%s",
239 sstrerror(errno, errbuf, sizeof(errbuf)));
242 dpdk_shm_cleanup(name, size, NULL);
245 /* File descriptor no longer needed for shared memory operations */
247 memset(*map, 0, size);
252 static void dpdk_shm_cleanup(const char *name, size_t size, void *map) {
253 DPDK_HELPER_TRACE(name);
254 char errbuf[ERR_BUF_SIZE];
257 * Call shm_unlink first, as 'name' might be no longer accessible after munmap
259 if (shm_unlink(name))
260 ERROR("shm_unlink failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
263 if (munmap(map, size))
264 ERROR("munmap failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
268 void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
270 return phc->priv_data;
275 int dpdk_helper_data_size_get(dpdk_helper_ctx_t *phc) {
277 DPDK_CHILD_LOG("Invalid argument(phc)\n");
281 return phc->shm_size - sizeof(dpdk_helper_ctx_t);
284 int dpdk_helper_init(const char *name, size_t data_size,
285 dpdk_helper_ctx_t **pphc) {
286 dpdk_helper_ctx_t *phc = NULL;
287 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
288 char errbuf[ERR_BUF_SIZE];
291 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
296 ERROR("%s:Invalid argument(name)", __FUNCTION__);
300 DPDK_HELPER_TRACE(name);
302 /* Allocate dpdk_helper_ctx_t and
303 * initialize a POSIX SHared Memory (SHM) object.
305 int err = dpdk_shm_init(name, shm_size, (void **)&phc);
310 err = sem_init(&phc->sema_cmd_start, 1, 0);
312 ERROR("sema_cmd_start semaphore init failed: %s",
313 sstrerror(errno, errbuf, sizeof(errbuf)));
315 dpdk_shm_cleanup(name, shm_size, (void *)phc);
319 err = sem_init(&phc->sema_cmd_complete, 1, 0);
321 ERROR("sema_cmd_complete semaphore init failed: %s",
322 sstrerror(errno, errbuf, sizeof(errbuf)));
323 sem_destroy(&phc->sema_cmd_start);
325 dpdk_shm_cleanup(name, shm_size, (void *)phc);
329 phc->shm_size = shm_size;
330 sstrncpy(phc->shm_name, name, sizeof(phc->shm_name));
332 dpdk_helper_config_default(phc);
339 void dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
343 DPDK_HELPER_TRACE(phc->shm_name);
345 close(phc->pipes[1]);
347 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
348 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
351 sem_destroy(&phc->sema_cmd_start);
352 sem_destroy(&phc->sema_cmd_complete);
353 dpdk_shm_cleanup(phc->shm_name, phc->shm_size, (void *)phc);
356 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc) {
357 char errbuf[ERR_BUF_SIZE];
359 ERROR("Invalid argument(phc)");
363 DPDK_HELPER_TRACE(phc->shm_name);
365 phc->eal_initialized = 0;
366 phc->cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
369 * Create a pipe for helper stdout back to collectd. This is necessary for
370 * logging EAL failures, as rte_eal_init() calls rte_panic().
373 DEBUG("dpdk_helper_spawn: collectd closing helper pipe %d", phc->pipes[1]);
375 DEBUG("dpdk_helper_spawn: collectd helper pipe %d, not closing",
379 if (pipe(phc->pipes) != 0) {
380 DEBUG("dpdk_helper_spawn: Could not create helper pipe: %s",
381 sstrerror(errno, errbuf, sizeof(errbuf)));
385 int pipe0_flags = fcntl(phc->pipes[0], F_GETFL, 0);
386 int pipe1_flags = fcntl(phc->pipes[1], F_GETFL, 0);
387 if (pipe0_flags == -1 || pipe1_flags == -1) {
388 WARNING("dpdk_helper_spawn: error setting up pipe flags: %s",
389 sstrerror(errno, errbuf, sizeof(errbuf)));
391 int pipe0_err = fcntl(phc->pipes[0], F_SETFL, pipe1_flags | O_NONBLOCK);
392 int pipe1_err = fcntl(phc->pipes[1], F_SETFL, pipe0_flags | O_NONBLOCK);
393 if (pipe0_err == -1 || pipe1_err == -1) {
394 WARNING("dpdk_helper_spawn: error setting up pipes: %s",
395 sstrerror(errno, errbuf, sizeof(errbuf)));
401 close(phc->pipes[1]);
402 DEBUG("%s:dpdk_helper_spawn: helper pid %lu", phc->shm_name,
404 } else if (pid == 0) {
405 /* Replace stdout with a pipe to collectd. */
406 close(phc->pipes[0]);
407 close(STDOUT_FILENO);
408 dup2(phc->pipes[1], STDOUT_FILENO);
409 DPDK_CHILD_TRACE(phc->shm_name);
410 dpdk_helper_worker(phc);
413 ERROR("dpdk_helper_start: Failed to fork helper process: %s",
414 sstrerror(errno, errbuf, sizeof(errbuf)));
421 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
422 enum DPDK_HELPER_STATUS status) {
423 DPDK_CHILD_LOG("%s:%s:%d %s\n", phc->shm_name, __FUNCTION__, __LINE__,
424 dpdk_helper_status_str(status));
426 close(phc->pipes[1]);
428 phc->status = status;
435 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
436 enum DPDK_HELPER_STATUS status) {
437 char errbuf[ERR_BUF_SIZE];
438 DPDK_HELPER_TRACE(phc->shm_name);
440 close(phc->pipes[1]);
442 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
443 phc->status = status;
444 DEBUG("%s:%s:%d %s", phc->shm_name, __FUNCTION__, __LINE__,
445 dpdk_helper_status_str(status));
447 int ret = dpdk_helper_command(phc, DPDK_CMD_QUIT, NULL, 0);
449 DEBUG("%s:%s:%d kill helper (pid=%lu)", phc->shm_name, __FUNCTION__,
450 __LINE__, (long)phc->pid);
452 int err = kill(phc->pid, SIGKILL);
454 ERROR("%s error sending kill to helper: %s", __FUNCTION__,
455 sstrerror(errno, errbuf, sizeof(errbuf)));
460 DEBUG("%s:%s:%d kill helper (pid=%lu)", phc->shm_name, __FUNCTION__,
461 __LINE__, (long)phc->pid);
463 int err = kill(phc->pid, SIGKILL);
465 ERROR("%s error sending kill to helper: %s", __FUNCTION__,
466 sstrerror(errno, errbuf, sizeof(errbuf)));
473 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc) {
474 phc->status = DPDK_HELPER_INITIALIZING_EAL;
475 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (start)\n",
476 phc->shm_name, __FUNCTION__, __LINE__);
478 char *argp[DPDK_EAL_ARGC * 2 + 1];
481 /* EAL config must be initialized */
482 assert(phc->eal_config.coremask[0] != 0);
483 assert(phc->eal_config.memory_channels[0] != 0);
484 assert(phc->eal_config.file_prefix[0] != 0);
486 argp[argc++] = "collectd-dpdk";
489 argp[argc++] = phc->eal_config.coremask;
492 argp[argc++] = phc->eal_config.memory_channels;
494 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
495 argp[argc++] = "--socket-mem";
496 argp[argc++] = phc->eal_config.socket_memory;
499 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
500 argp[argc++] = "--file-prefix";
501 argp[argc++] = phc->eal_config.file_prefix;
504 argp[argc++] = "--proc-type";
505 argp[argc++] = "secondary";
507 if (strcasecmp(phc->eal_config.log_level, "") != 0) {
508 argp[argc++] = "--log-level";
509 argp[argc++] = phc->eal_config.log_level;
511 if (strcasecmp(phc->eal_config.rte_driver_lib_path, "") != 0) {
513 argp[argc++] = phc->eal_config.rte_driver_lib_path;
516 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
518 int ret = rte_eal_init(argc, argp);
522 phc->eal_initialized = 0;
524 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
527 printf("dpdk_helper_eal_init: EAL arguments: ");
528 for (int i = 0; i < argc; i++) {
529 printf("%s ", argp[i]);
536 phc->eal_initialized = 1;
538 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
539 phc->shm_name, __FUNCTION__, __LINE__);
544 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
545 DPDK_CHILD_TRACE(phc->shm_name);
548 cdtime_t now = cdtime();
549 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
550 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
552 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
553 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
554 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
557 if (phc->cmd == DPDK_CMD_QUIT) {
558 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
559 __LINE__, (long)getpid());
561 } else if (ret == -1 && errno == ETIMEDOUT) {
562 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
563 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
564 " timeout, did collectd terminate?\n",
566 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
571 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
572 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
573 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
576 /* Parent PID change means collectd died so quit the helper process. */
577 if (ppid != getppid()) {
578 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
579 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
582 /* Checking for DPDK primary process. */
583 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
584 if (phc->eal_initialized) {
586 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
589 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
592 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
593 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
594 __FUNCTION__, __LINE__);
599 if (!phc->eal_initialized) {
600 int ret = dpdk_helper_eal_init(phc);
602 DPDK_CHILD_LOG("Error initializing EAL\n");
603 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
605 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
606 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
607 __FUNCTION__, __LINE__);
614 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
615 DPDK_CHILD_TRACE(phc->shm_name);
617 pid_t ppid = getppid();
620 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
621 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
622 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
624 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
626 phc->cmd_result = -1;
629 /* now kick collectd to get results */
630 int err = sem_post(&phc->sema_cmd_complete);
631 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
632 __FUNCTION__, __LINE__, (long)getpid());
634 char errbuf[ERR_BUF_SIZE];
635 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
637 sstrerror(errno, errbuf, sizeof(errbuf)));
642 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
643 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
644 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
653 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
655 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
656 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
657 case DPDK_HELPER_WAITING_ON_PRIMARY:
658 return "DPDK_HELPER_WAITING_ON_PRIMARY";
659 case DPDK_HELPER_INITIALIZING:
660 return "DPDK_HELPER_INITIALIZING";
661 case DPDK_HELPER_INITIALIZING_EAL:
662 return "DPDK_HELPER_INITIALIZING_EAL";
663 case DPDK_HELPER_GRACEFUL_QUIT:
664 return "DPDK_HELPER_GRACEFUL_QUIT";
665 case DPDK_HELPER_NOT_INITIALIZED:
666 return "DPDK_HELPER_NOT_INITIALIZED";
672 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
673 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
674 dpdk_helper_status_str(phc->status));
675 char errbuf[ERR_BUF_SIZE];
677 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
679 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
680 phc->status = DPDK_HELPER_INITIALIZING;
681 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
683 int err = dpdk_helper_spawn(phc);
685 ERROR("dpdkstat: error spawning helper %s",
686 sstrerror(errno, errbuf, sizeof(errbuf)));
691 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
693 phc->status = DPDK_HELPER_INITIALIZING;
694 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
696 int err = dpdk_helper_spawn(phc);
698 ERROR("dpdkstat: error spawning helper %s",
699 sstrerror(errno, errbuf, sizeof(errbuf)));
704 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
711 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
712 char buf[DPDK_MAX_BUFFER_SIZE];
713 char out[DPDK_MAX_BUFFER_SIZE];
715 /* non blocking check on helper logging pipe */
716 struct pollfd fds = {
717 .fd = phc->pipes[0], .events = POLLIN,
719 int data_avail = poll(&fds, 1, 0);
720 DEBUG("%s:dpdk_helper_check_pipe: poll data_avail=%d", phc->shm_name,
722 if (data_avail < 0) {
723 if (errno != EINTR || errno != EAGAIN) {
724 char errbuf[ERR_BUF_SIZE];
725 ERROR("%s: poll(2) failed: %s", phc->shm_name,
726 sstrerror(errno, errbuf, sizeof(errbuf)));
730 int nbytes = read(phc->pipes[0], buf, (sizeof(buf) - 1));
731 DEBUG("%s:dpdk_helper_check_pipe: read nbytes=%d", phc->shm_name, nbytes);
735 sstrncpy(out, buf, (nbytes + 1));
736 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
740 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
741 cdtime_t cmd_wait_time) {
743 ERROR("Invalid argument(phc)");
747 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
748 (long)getpid(), cmd);
750 phc->cmd_wait_time = cmd_wait_time;
752 int ret = dpdk_helper_status_check(phc);
754 dpdk_helper_check_pipe(phc);
760 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
765 /* kick helper to process command */
766 int err = sem_post(&phc->sema_cmd_start);
768 char errbuf[ERR_BUF_SIZE];
769 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)",
770 sstrerror(errno, errbuf, sizeof(errbuf)));
775 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
776 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
780 if (phc->cmd != DPDK_CMD_QUIT) {
782 /* wait for helper to complete processing */
784 cdtime_t now = cdtime();
786 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
787 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
790 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
791 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
792 if (ret == -1 && errno == ETIMEDOUT) {
793 DPDK_HELPER_TRACE(phc->shm_name);
794 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
802 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
803 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
808 *result = phc->cmd_result;
812 dpdk_helper_check_pipe(phc);
814 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
815 phc->cmd, phc->cmd_result);
820 uint64_t strtoull_safe(const char *str, int *err) {
825 val = strtoull(str, &endptr, 16);
827 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
836 uint128_t str_to_uint128(const char *str, int len) {
837 uint128_t lcore_mask;
840 memset(&lcore_mask, 0, sizeof(lcore_mask));
842 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
843 ERROR("%s Value %s should be represened in hexadecimal format",
847 /* If str is <= 64 bit long ('0x' + 16 chars = 18 chars) then
848 * conversion is straightforward. Otherwise str is splitted into 64b long
851 lcore_mask.low = strtoull_safe(str, &err);
855 char low_str[DATA_MAX_NAME_LEN];
856 char high_str[DATA_MAX_NAME_LEN];
858 memset(high_str, 0, sizeof(high_str));
859 memset(low_str, 0, sizeof(low_str));
861 strncpy(high_str, str, len - 16);
862 strncpy(low_str, str + len - 16, 16);
864 lcore_mask.low = strtoull_safe(low_str, &err);
868 lcore_mask.high = strtoull_safe(high_str, &err);
877 uint8_t dpdk_helper_eth_dev_count() {
878 uint8_t ports = rte_eth_dev_count();
881 "%s:%d: No DPDK ports available. Check bound devices to DPDK driver.\n",
882 __FUNCTION__, __LINE__);
886 if (ports > RTE_MAX_ETHPORTS) {
887 ERROR("%s:%d: Number of DPDK ports (%u) is greater than "
888 "RTE_MAX_ETHPORTS=%d. Ignoring extra ports\n",
889 __FUNCTION__, __LINE__, ports, RTE_MAX_ETHPORTS);
890 ports = RTE_MAX_ETHPORTS;