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>
43 #include "utils_dpdk.h"
45 #define DPDK_DEFAULT_RTE_CONFIG "/var/run/.rte_config"
46 #define DPDK_EAL_ARGC 5
47 #define DPDK_MAX_BUFFER_SIZE (4096 * 4)
48 #define DPDK_CDM_DEFAULT_TIMEOUT 10000
50 enum DPDK_HELPER_STATUS {
51 DPDK_HELPER_NOT_INITIALIZED = 0,
52 DPDK_HELPER_INITIALIZING,
53 DPDK_HELPER_WAITING_ON_PRIMARY,
54 DPDK_HELPER_INITIALIZING_EAL,
55 DPDK_HELPER_ALIVE_SENDING_EVENTS,
56 DPDK_HELPER_GRACEFUL_QUIT,
59 #define DPDK_HELPER_TRACE(_name) \
60 DEBUG("%s:%s:%d pid=%lu", _name, __FUNCTION__, __LINE__, (long)getpid())
62 #define DPDK_HELPER_USE_PIPES
64 struct dpdk_helper_ctx_s {
66 dpdk_eal_config_t eal_config;
73 sem_t sema_cmd_complete;
74 cdtime_t cmd_wait_time;
77 #ifdef DPDK_HELPER_USE_PIPES
79 #endif /* DPDK_HELPER_USE_PIPES */
88 static int dpdk_shm_init(const char *name, size_t size, void **map);
89 static int dpdk_shm_cleanup(const char *name, size_t size, void *map);
91 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc);
92 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc);
93 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc);
94 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid);
95 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
96 enum DPDK_HELPER_STATUS status);
97 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
98 enum DPDK_HELPER_STATUS status);
99 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc);
100 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc);
101 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status);
103 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc) {
107 DPDK_HELPER_TRACE(phc->shm_name);
109 ssnprintf(phc->eal_config.coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
110 ssnprintf(phc->eal_config.memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
111 ssnprintf(phc->eal_config.process_type, DATA_MAX_NAME_LEN, "%s", "secondary");
112 ssnprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN, "%s",
113 DPDK_DEFAULT_RTE_CONFIG);
116 int dpdk_helper_eal_config_set(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
118 ERROR("Invalid argument (phc)");
122 DPDK_HELPER_TRACE(phc->shm_name);
125 ERROR("Invalid argument (ec)");
129 memcpy(&phc->eal_config, ec, sizeof(dpdk_eal_config_t));
134 int dpdk_helper_eal_config_get(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
136 ERROR("Invalid argument (phc)");
140 DPDK_HELPER_TRACE(phc->shm_name);
143 ERROR("Invalid argument (ec)");
147 memcpy(ec, &phc->eal_config, sizeof(dpdk_eal_config_t));
152 int dpdk_helper_eal_config_parse(dpdk_helper_ctx_t *phc, oconfig_item_t *ci) {
153 DPDK_HELPER_TRACE(phc->shm_name);
156 ERROR("Invalid argument (phc)");
161 ERROR("Invalid argument (ci)");
165 for (int i = 0; i < ci->children_num; i++) {
166 oconfig_item_t *child = ci->children + i;
167 if (strcasecmp("Coremask", child->key) == 0) {
168 cf_util_get_string_buffer(child, phc->eal_config.coremask,
169 sizeof(phc->eal_config.coremask));
170 DEBUG("dpdk_common: EAL:Coremask %s", phc->eal_config.coremask);
171 } 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 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("ProcessType", child->key) == 0) {
181 cf_util_get_string_buffer(child, phc->eal_config.process_type,
182 sizeof(phc->eal_config.process_type));
183 DEBUG("dpdk_common: EAL:Process type %s", phc->eal_config.process_type);
184 } else if ((strcasecmp("FilePrefix", child->key) == 0) &&
185 (child->values[0].type == OCONFIG_TYPE_STRING)) {
186 ssnprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN,
187 "/var/run/.%s_config", child->values[0].value.string);
188 DEBUG("dpdk_common: EAL:File prefix %s", phc->eal_config.file_prefix);
195 static int dpdk_shm_init(const char *name, size_t size, void **map) {
196 DPDK_HELPER_TRACE(name);
198 char errbuf[ERR_BUF_SIZE];
200 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
202 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s\n", name,
203 sstrerror(errno, errbuf, sizeof(errbuf)));
207 int ret = ftruncate(fd, size);
209 WARNING("dpdk_shm_init: Failed to resize SHM:%s\n",
210 sstrerror(errno, errbuf, sizeof(errbuf)));
214 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
215 if (*map == MAP_FAILED) {
216 WARNING("dpdk_shm_init:Failed to mmap SHM:%s\n",
217 sstrerror(errno, errbuf, sizeof(errbuf)));
221 * Close the file descriptor, the shared memory object still exists
222 * and can only be removed by calling shm_unlink().
226 memset(*map, 0, size);
237 static int dpdk_shm_cleanup(const char *name, size_t size, void *map) {
238 DPDK_HELPER_TRACE(name);
240 int ret = munmap(map, size);
242 ERROR("munmap returned %d\n", ret);
245 ret = shm_unlink(name);
247 ERROR("shm_unlink returned %d\n", ret);
253 inline void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
255 return (void *)phc->priv_data;
260 int dpdk_helper_data_size_get(dpdk_helper_ctx_t *phc) {
262 DPDK_CHILD_LOG("Invalid argument(phc)\n");
266 return (phc->shm_size - sizeof(dpdk_helper_ctx_t));
269 int dpdk_helper_init(const char *name, size_t data_size,
270 dpdk_helper_ctx_t **pphc) {
272 dpdk_helper_ctx_t *phc = NULL;
273 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
274 char errbuf[ERR_BUF_SIZE];
277 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
282 ERROR("%s:Invalid argument(name)", __FUNCTION__);
286 DPDK_HELPER_TRACE(name);
288 /* Allocate dpdk_helper_ctx_t and
289 * initialize a POSIX SHared Memory (SHM) object.
291 err = dpdk_shm_init(name, shm_size, (void **)&phc);
296 err = sem_init(&phc->sema_cmd_start, 1, 0);
298 ERROR("sema_cmd_start semaphore init failed: %s\n",
299 sstrerror(errno, errbuf, sizeof(errbuf)));
300 dpdk_shm_cleanup(name, shm_size, (void *)phc);
304 err = sem_init(&phc->sema_cmd_complete, 1, 0);
306 ERROR("sema_cmd_complete semaphore init failed: %s\n",
307 sstrerror(errno, errbuf, sizeof(errbuf)));
308 sem_destroy(&phc->sema_cmd_start);
309 dpdk_shm_cleanup(name, shm_size, (void *)phc);
313 phc->shm_size = shm_size;
314 phc->shm_name = name;
316 dpdk_helper_config_default(phc);
323 int dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
325 ERROR("%s:Invalid argument(phc)", __FUNCTION__);
329 DPDK_HELPER_TRACE(phc->shm_name);
331 #ifdef DPDK_HELPER_USE_PIPES
332 close(phc->pipes[1]);
335 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
336 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
339 sem_destroy(&phc->sema_cmd_start);
340 sem_destroy(&phc->sema_cmd_complete);
341 dpdk_shm_cleanup(phc->shm_name, phc->shm_size, (void *)phc);
346 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc) {
347 char errbuf[ERR_BUF_SIZE];
349 ERROR("Invalid argument(phc)");
353 DPDK_HELPER_TRACE(phc->shm_name);
355 phc->eal_initialized = 0;
356 phc->cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
358 #ifdef DPDK_HELPER_USE_PIPES
360 * Create a pipe for helper stdout back to collectd. This is necessary for
361 * logging EAL failures, as rte_eal_init() calls rte_panic().
364 DEBUG("dpdk_helper_spawn: collectd closing helper pipe %d", phc->pipes[1]);
366 DEBUG("dpdk_helper_spawn: collectd helper pipe %d, not closing",
370 if (pipe(phc->pipes) != 0) {
371 DEBUG("dpdk_helper_spawn: Could not create helper pipe: %s\n",
372 sstrerror(errno, errbuf, sizeof(errbuf)));
376 int pipe0_flags = fcntl(phc->pipes[0], F_GETFL, 0);
377 int pipe1_flags = fcntl(phc->pipes[1], F_GETFL, 0);
378 if (pipe0_flags == -1 || pipe1_flags == -1) {
379 WARNING("dpdk_helper_spawn: error setting up pipe flags: %s",
380 sstrerror(errno, errbuf, sizeof(errbuf)));
382 int pipe0_err = fcntl(phc->pipes[0], F_SETFL, pipe1_flags | O_NONBLOCK);
383 int pipe1_err = fcntl(phc->pipes[1], F_SETFL, pipe0_flags | O_NONBLOCK);
384 if (pipe0_err == -1 || pipe1_err == -1) {
385 WARNING("dpdk_helper_spawn: error setting up pipes: %s",
386 sstrerror(errno, errbuf, sizeof(errbuf)));
388 #endif /* DPDK_HELPER_USE_PIPES */
393 #ifdef DPDK_HELPER_USE_PIPES
394 close(phc->pipes[1]);
395 #endif /* DPDK_HELPER_USE_PIPES */
396 DEBUG("%s:dpdk_helper_spawn: helper pid %lu", phc->shm_name,
398 } else if (pid == 0) {
399 #ifdef DPDK_HELPER_USE_PIPES
400 /* Replace stdout with a pipe to collectd. */
401 close(phc->pipes[0]);
402 close(STDOUT_FILENO);
403 dup2(phc->pipes[1], STDOUT_FILENO);
404 #endif /* DPDK_HELPER_USE_PIPES */
405 DPDK_CHILD_TRACE(phc->shm_name);
406 dpdk_helper_worker(phc);
409 ERROR("dpdk_helper_start: Failed to fork helper process: %s\n",
410 sstrerror(errno, errbuf, sizeof(errbuf)));
417 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
418 enum DPDK_HELPER_STATUS status) {
419 DPDK_CHILD_LOG("%s:%s:%d %s\n", phc->shm_name, __FUNCTION__, __LINE__,
420 dpdk_helper_status_str(status));
422 #ifdef DPDK_HELPER_USE_PIPES
423 close(phc->pipes[1]);
424 #endif /* DPDK_HELPER_USE_PIPES */
426 phc->status = status;
433 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
434 enum DPDK_HELPER_STATUS status) {
435 char errbuf[ERR_BUF_SIZE];
436 DPDK_HELPER_TRACE(phc->shm_name);
438 #ifdef DPDK_HELPER_USE_PIPES
439 close(phc->pipes[1]);
440 #endif /* DPDK_HELPER_USE_PIPES */
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\n", __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\n", __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.process_type[0] != 0);
485 assert(phc->eal_config.file_prefix[0] != 0);
487 argp[argc++] = "collectd-dpdk";
490 argp[argc++] = phc->eal_config.coremask;
493 argp[argc++] = phc->eal_config.memory_channels;
495 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
496 argp[argc++] = "--socket-mem";
497 argp[argc++] = phc->eal_config.socket_memory;
500 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
501 argp[argc++] = "--file-prefix";
502 argp[argc++] = phc->eal_config.file_prefix;
505 argp[argc++] = "--proc-type";
506 argp[argc++] = phc->eal_config.process_type;
508 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
510 int ret = rte_eal_init(argc, argp);
514 phc->eal_initialized = 0;
516 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
519 printf("dpdk_helper_eal_init: EAL arguments: ");
520 for (int i = 0; i < argc; i++) {
521 printf("%s ", argp[i]);
528 phc->eal_initialized = 1;
530 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
531 phc->shm_name, __FUNCTION__, __LINE__);
536 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
537 DPDK_CHILD_TRACE(phc->shm_name);
540 cdtime_t now = cdtime();
541 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
542 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
544 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
545 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
546 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
549 if (phc->cmd == DPDK_CMD_QUIT) {
550 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
551 __LINE__, (long)getpid());
553 } else if (ret == -1 && errno == ETIMEDOUT) {
554 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
555 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
556 " timeout, did collectd terminate?\n",
558 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
563 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
564 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
565 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
568 /* Parent PID change means collectd died so quit the helper process. */
569 if (ppid != getppid()) {
570 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
571 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
574 /* Checking for DPDK primary process. */
575 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
576 if (phc->eal_initialized) {
578 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
581 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
584 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
585 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
586 __FUNCTION__, __LINE__);
591 if (!phc->eal_initialized) {
592 int ret = dpdk_helper_eal_init(phc);
594 DPDK_CHILD_LOG("Error initializing EAL\n");
595 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
597 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
598 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
599 __FUNCTION__, __LINE__);
606 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
607 DPDK_CHILD_TRACE(phc->shm_name);
609 pid_t ppid = getppid();
612 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
613 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
614 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
616 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
618 phc->cmd_result = -1;
621 /* now kick collectd to get results */
622 int err = sem_post(&phc->sema_cmd_complete);
623 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
624 __FUNCTION__, __LINE__, (long)getpid());
626 char errbuf[ERR_BUF_SIZE];
627 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
629 sstrerror(errno, errbuf, sizeof(errbuf)));
634 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
635 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
636 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
645 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
647 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
648 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
649 case DPDK_HELPER_WAITING_ON_PRIMARY:
650 return "DPDK_HELPER_WAITING_ON_PRIMARY";
651 case DPDK_HELPER_INITIALIZING:
652 return "DPDK_HELPER_INITIALIZING";
653 case DPDK_HELPER_INITIALIZING_EAL:
654 return "DPDK_HELPER_INITIALIZING_EAL";
655 case DPDK_HELPER_GRACEFUL_QUIT:
656 return "DPDK_HELPER_GRACEFUL_QUIT";
657 case DPDK_HELPER_NOT_INITIALIZED:
658 return "DPDK_HELPER_NOT_INITIALIZED";
664 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
665 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
666 dpdk_helper_status_str(phc->status));
667 char errbuf[ERR_BUF_SIZE];
669 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
671 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
672 phc->status = DPDK_HELPER_INITIALIZING;
673 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
675 int err = dpdk_helper_spawn(phc);
677 ERROR("dpdkstat: error spawning helper %s",
678 sstrerror(errno, errbuf, sizeof(errbuf)));
683 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
685 phc->status = DPDK_HELPER_INITIALIZING;
686 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
688 int err = dpdk_helper_spawn(phc);
690 ERROR("dpdkstat: error spawning helper %s",
691 sstrerror(errno, errbuf, sizeof(errbuf)));
696 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
703 #ifdef DPDK_HELPER_USE_PIPES
704 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
705 char buf[DPDK_MAX_BUFFER_SIZE];
706 char out[DPDK_MAX_BUFFER_SIZE];
708 /* non blocking check on helper logging pipe */
709 struct pollfd fds = {
710 .fd = phc->pipes[0], .events = POLLIN,
712 int data_avail = poll(&fds, 1, 0);
713 if (data_avail < 0) {
714 if (errno != EINTR || errno != EAGAIN) {
715 char errbuf[ERR_BUF_SIZE];
716 ERROR("%s: poll(2) failed: %s", phc->shm_name,
717 sstrerror(errno, errbuf, sizeof(errbuf)));
721 int nbytes = read(phc->pipes[0], buf, sizeof(buf));
724 sstrncpy(out, buf, nbytes);
725 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
728 #endif /* DPDK_HELPER_USE_PIPES */
730 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
731 cdtime_t cmd_wait_time) {
733 ERROR("Invalid argument(phc)");
737 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
738 (long)getpid(), cmd);
740 phc->cmd_wait_time = cmd_wait_time;
744 ret = dpdk_helper_status_check(phc);
746 #ifdef DPDK_HELPER_USE_PIPES
747 dpdk_helper_check_pipe(phc);
748 #endif /* DPDK_HELPER_USE_PIPES */
754 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
759 /* kick helper to process command */
760 int err = sem_post(&phc->sema_cmd_start);
762 char errbuf[ERR_BUF_SIZE];
763 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)\n",
764 sstrerror(errno, errbuf, sizeof(errbuf)));
769 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
770 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
774 if (phc->cmd != DPDK_CMD_QUIT) {
776 /* wait for helper to complete processing */
778 cdtime_t now = cdtime();
780 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
781 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
784 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
785 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
786 if (ret == -1 && errno == ETIMEDOUT) {
787 DPDK_HELPER_TRACE(phc->shm_name);
788 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
796 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
797 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
802 *result = phc->cmd_result;
806 #ifdef DPDK_HELPER_USE_PIPES
807 dpdk_helper_check_pipe(phc);
808 #endif /* DPDK_HELPER_USE_PIPES */
810 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
811 phc->cmd, phc->cmd_result);
816 uint64_t strtoull_safe(const char *str, int *err) {
821 val = strtoull(str, &endptr, 16);
823 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
832 uint128_t str_to_uint128(const char *str, int len) {
833 uint128_t lcore_mask;
836 memset(&lcore_mask, 0, sizeof(uint128_t));
838 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
839 ERROR("%s Value %s should be represened in hexadecimal format",
845 lcore_mask.low = strtoull_safe(str, &err);
849 char low_str[DATA_MAX_NAME_LEN];
850 char high_str[DATA_MAX_NAME_LEN];
852 memset(high_str, 0, sizeof(high_str));
853 memset(low_str, 0, sizeof(low_str));
855 strncpy(high_str, str, len - 16);
856 strncpy(low_str, str + len - 16, 16);
858 lcore_mask.low = strtoull_safe(low_str, &err);
862 lcore_mask.high = strtoull_safe(high_str, &err);