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
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is 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
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Maryam Tahhan <maryam.tahhan@intel.com>
27 * Harry van Haaren <harry.van.haaren@intel.com>
28 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
34 #include <semaphore.h>
37 #include <rte_config.h>
41 #include "utils_dpdk.h"
43 #define DPDK_DEFAULT_RTE_CONFIG "/var/run/.rte_config"
44 #define DPDK_EAL_ARGC 5
45 #define DPDK_MAX_BUFFER_SIZE (4096 * 4)
46 #define DPDK_CDM_DEFAULT_TIMEOUT 10000
48 enum DPDK_HELPER_STATUS {
49 DPDK_HELPER_NOT_INITIALIZED = 0,
50 DPDK_HELPER_INITIALIZING,
51 DPDK_HELPER_WAITING_ON_PRIMARY,
52 DPDK_HELPER_INITIALIZING_EAL,
53 DPDK_HELPER_ALIVE_SENDING_EVENTS,
54 DPDK_HELPER_GRACEFUL_QUIT,
57 #define DPDK_HELPER_TRACE(_name) \
58 DEBUG("%s:%s:%d pid=%lu", _name, __FUNCTION__, __LINE__, (long)getpid())
60 #define DPDK_HELPER_USE_PIPES
62 struct dpdk_helper_ctx_s {
64 dpdk_eal_config_t eal_config;
71 sem_t sema_cmd_complete;
72 cdtime_t cmd_wait_time;
75 #ifdef DPDK_HELPER_USE_PIPES
77 #endif /* DPDK_HELPER_USE_PIPES */
86 static int dpdk_shm_init(const char *name, size_t size, void **map);
87 static int 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 ssnprintf(phc->eal_config.coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
108 ssnprintf(phc->eal_config.memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
109 ssnprintf(phc->eal_config.process_type, DATA_MAX_NAME_LEN, "%s", "secondary");
110 ssnprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN, "%s",
111 DPDK_DEFAULT_RTE_CONFIG);
114 int dpdk_helper_eal_config_set(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
116 ERROR("Invalid argument (phc)");
120 DPDK_HELPER_TRACE(phc->shm_name);
123 ERROR("Invalid argument (ec)");
127 memcpy(&phc->eal_config, ec, sizeof(dpdk_eal_config_t));
132 int dpdk_helper_eal_config_get(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
134 ERROR("Invalid argument (phc)");
138 DPDK_HELPER_TRACE(phc->shm_name);
141 ERROR("Invalid argument (ec)");
145 memcpy(ec, &phc->eal_config, sizeof(dpdk_eal_config_t));
150 int dpdk_helper_eal_config_parse(dpdk_helper_ctx_t *phc, oconfig_item_t *ci) {
151 DPDK_HELPER_TRACE(phc->shm_name);
154 ERROR("Invalid argument (phc)");
159 ERROR("Invalid argument (ci)");
163 for (int i = 0; i < ci->children_num; i++) {
164 oconfig_item_t *child = ci->children + i;
165 if (strcasecmp("Coremask", child->key) == 0) {
166 cf_util_get_string_buffer(child, phc->eal_config.coremask,
167 sizeof(phc->eal_config.coremask));
168 DEBUG("dpdk_common: EAL:Coremask %s", phc->eal_config.coremask);
169 } else if (strcasecmp("MemoryChannels", child->key) == 0) {
170 cf_util_get_string_buffer(child, phc->eal_config.memory_channels,
171 sizeof(phc->eal_config.memory_channels));
172 DEBUG("dpdk_common: EAL:Memory Channels %s",
173 phc->eal_config.memory_channels);
174 } else if (strcasecmp("SocketMemory", child->key) == 0) {
175 cf_util_get_string_buffer(child, phc->eal_config.socket_memory,
176 sizeof(phc->eal_config.socket_memory));
177 DEBUG("dpdk_common: EAL:Socket memory %s", phc->eal_config.socket_memory);
178 } else if (strcasecmp("ProcessType", child->key) == 0) {
179 cf_util_get_string_buffer(child, phc->eal_config.process_type,
180 sizeof(phc->eal_config.process_type));
181 DEBUG("dpdk_common: EAL:Process type %s", phc->eal_config.process_type);
182 } else if ((strcasecmp("FilePrefix", child->key) == 0) &&
183 (child->values[0].type == OCONFIG_TYPE_STRING)) {
184 ssnprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN,
185 "/var/run/.%s_config", child->values[0].value.string);
186 DEBUG("dpdk_common: EAL:File prefix %s", phc->eal_config.file_prefix);
193 static int dpdk_shm_init(const char *name, size_t size, void **map) {
194 DPDK_HELPER_TRACE(name);
196 char errbuf[ERR_BUF_SIZE];
198 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
200 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s\n", name,
201 sstrerror(errno, errbuf, sizeof(errbuf)));
205 int ret = ftruncate(fd, size);
207 WARNING("dpdk_shm_init: Failed to resize SHM:%s\n",
208 sstrerror(errno, errbuf, sizeof(errbuf)));
212 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
213 if (*map == MAP_FAILED) {
214 WARNING("dpdk_shm_init:Failed to mmap SHM:%s\n",
215 sstrerror(errno, errbuf, sizeof(errbuf)));
219 * Close the file descriptor, the shared memory object still exists
220 * and can only be removed by calling shm_unlink().
224 memset(*map, 0, size);
235 static int dpdk_shm_cleanup(const char *name, size_t size, void *map) {
236 DPDK_HELPER_TRACE(name);
238 int ret = munmap(map, size);
240 ERROR("munmap returned %d\n", ret);
243 ret = shm_unlink(name);
245 ERROR("shm_unlink returned %d\n", ret);
251 inline void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
253 return (void *)phc->priv_data;
258 int dpdk_helper_data_size_get(dpdk_helper_ctx_t *phc) {
260 DPDK_CHILD_LOG("Invalid argument(phc)\n");
264 return (phc->shm_size - sizeof(dpdk_helper_ctx_t));
267 int dpdk_helper_init(const char *name, size_t data_size,
268 dpdk_helper_ctx_t **pphc) {
270 dpdk_helper_ctx_t *phc = NULL;
271 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
272 char errbuf[ERR_BUF_SIZE];
275 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
280 ERROR("%s:Invalid argument(name)", __FUNCTION__);
284 DPDK_HELPER_TRACE(name);
286 /* Allocate dpdk_helper_ctx_t and
287 * initialize a POSIX SHared Memory (SHM) object.
289 err = dpdk_shm_init(name, shm_size, (void **)&phc);
294 err = sem_init(&phc->sema_cmd_start, 1, 0);
296 ERROR("sema_cmd_start semaphore init failed: %s\n",
297 sstrerror(errno, errbuf, sizeof(errbuf)));
298 dpdk_shm_cleanup(name, shm_size, (void *)phc);
302 err = sem_init(&phc->sema_cmd_complete, 1, 0);
304 ERROR("sema_cmd_complete semaphore init failed: %s\n",
305 sstrerror(errno, errbuf, sizeof(errbuf)));
306 sem_destroy(&phc->sema_cmd_start);
307 dpdk_shm_cleanup(name, shm_size, (void *)phc);
311 phc->shm_size = shm_size;
312 phc->shm_name = name;
314 dpdk_helper_config_default(phc);
321 int dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
323 ERROR("%s:Invalid argument(phc)", __FUNCTION__);
327 DPDK_HELPER_TRACE(phc->shm_name);
329 #ifdef DPDK_HELPER_USE_PIPES
330 close(phc->pipes[1]);
333 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
334 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
337 sem_destroy(&phc->sema_cmd_start);
338 sem_destroy(&phc->sema_cmd_complete);
339 dpdk_shm_cleanup(phc->shm_name, phc->shm_size, (void *)phc);
344 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc) {
345 char errbuf[ERR_BUF_SIZE];
347 ERROR("Invalid argument(phc)");
351 DPDK_HELPER_TRACE(phc->shm_name);
353 phc->eal_initialized = 0;
354 phc->cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
356 #ifdef DPDK_HELPER_USE_PIPES
358 * Create a pipe for helper stdout back to collectd. This is necessary for
359 * logging EAL failures, as rte_eal_init() calls rte_panic().
362 DEBUG("dpdk_helper_spawn: collectd closing helper pipe %d", phc->pipes[1]);
364 DEBUG("dpdk_helper_spawn: collectd helper pipe %d, not closing",
368 if (pipe(phc->pipes) != 0) {
369 DEBUG("dpdk_helper_spawn: Could not create helper pipe: %s\n",
370 sstrerror(errno, errbuf, sizeof(errbuf)));
374 int pipe0_flags = fcntl(phc->pipes[0], F_GETFL, 0);
375 int pipe1_flags = fcntl(phc->pipes[1], F_GETFL, 0);
376 if (pipe0_flags == -1 || pipe1_flags == -1) {
377 WARNING("dpdk_helper_spawn: error setting up pipe flags: %s",
378 sstrerror(errno, errbuf, sizeof(errbuf)));
380 int pipe0_err = fcntl(phc->pipes[0], F_SETFL, pipe1_flags | O_NONBLOCK);
381 int pipe1_err = fcntl(phc->pipes[1], F_SETFL, pipe0_flags | O_NONBLOCK);
382 if (pipe0_err == -1 || pipe1_err == -1) {
383 WARNING("dpdk_helper_spawn: error setting up pipes: %s",
384 sstrerror(errno, errbuf, sizeof(errbuf)));
386 #endif /* DPDK_HELPER_USE_PIPES */
391 #ifdef DPDK_HELPER_USE_PIPES
392 close(phc->pipes[1]);
393 #endif /* DPDK_HELPER_USE_PIPES */
394 DEBUG("%s:dpdk_helper_spawn: helper pid %lu", phc->shm_name,
396 } else if (pid == 0) {
397 #ifdef DPDK_HELPER_USE_PIPES
398 /* Replace stdout with a pipe to collectd. */
399 close(phc->pipes[0]);
400 close(STDOUT_FILENO);
401 dup2(phc->pipes[1], STDOUT_FILENO);
402 #endif /* DPDK_HELPER_USE_PIPES */
403 DPDK_CHILD_TRACE(phc->shm_name);
404 dpdk_helper_worker(phc);
407 ERROR("dpdk_helper_start: Failed to fork helper process: %s\n",
408 sstrerror(errno, errbuf, sizeof(errbuf)));
415 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
416 enum DPDK_HELPER_STATUS status) {
417 DPDK_CHILD_LOG("%s:%s:%d %s\n", phc->shm_name, __FUNCTION__, __LINE__,
418 dpdk_helper_status_str(status));
420 #ifdef DPDK_HELPER_USE_PIPES
421 close(phc->pipes[1]);
422 #endif /* DPDK_HELPER_USE_PIPES */
424 phc->status = status;
431 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
432 enum DPDK_HELPER_STATUS status) {
433 char errbuf[ERR_BUF_SIZE];
434 DPDK_HELPER_TRACE(phc->shm_name);
436 #ifdef DPDK_HELPER_USE_PIPES
437 close(phc->pipes[1]);
438 #endif /* DPDK_HELPER_USE_PIPES */
440 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
441 phc->status = status;
442 DEBUG("%s:%s:%d %s", phc->shm_name, __FUNCTION__, __LINE__,
443 dpdk_helper_status_str(status));
445 int ret = dpdk_helper_command(phc, DPDK_CMD_QUIT, NULL, 0);
447 DEBUG("%s:%s:%d kill helper (pid=%lu)", phc->shm_name, __FUNCTION__,
448 __LINE__, (long)phc->pid);
450 int err = kill(phc->pid, SIGKILL);
452 ERROR("%s error sending kill to helper: %s\n", __FUNCTION__,
453 sstrerror(errno, errbuf, sizeof(errbuf)));
458 DEBUG("%s:%s:%d kill helper (pid=%lu)", phc->shm_name, __FUNCTION__,
459 __LINE__, (long)phc->pid);
461 int err = kill(phc->pid, SIGKILL);
463 ERROR("%s error sending kill to helper: %s\n", __FUNCTION__,
464 sstrerror(errno, errbuf, sizeof(errbuf)));
471 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc) {
472 phc->status = DPDK_HELPER_INITIALIZING_EAL;
473 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (start)\n",
474 phc->shm_name, __FUNCTION__, __LINE__);
476 char *argp[DPDK_EAL_ARGC * 2 + 1];
479 /* EAL config must be initialized */
480 assert(phc->eal_config.coremask[0] != 0);
481 assert(phc->eal_config.memory_channels[0] != 0);
482 assert(phc->eal_config.process_type[0] != 0);
483 assert(phc->eal_config.file_prefix[0] != 0);
485 argp[argc++] = "collectd-dpdk";
488 argp[argc++] = phc->eal_config.coremask;
491 argp[argc++] = phc->eal_config.memory_channels;
493 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
494 argp[argc++] = "--socket-mem";
495 argp[argc++] = phc->eal_config.socket_memory;
498 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
499 argp[argc++] = "--file-prefix";
500 argp[argc++] = phc->eal_config.file_prefix;
503 argp[argc++] = "--proc-type";
504 argp[argc++] = phc->eal_config.process_type;
506 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
508 int ret = rte_eal_init(argc, argp);
512 phc->eal_initialized = 0;
514 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
517 printf("dpdk_helper_eal_init: EAL arguments: ");
518 for (int i = 0; i < argc; i++) {
519 printf("%s ", argp[i]);
526 phc->eal_initialized = 1;
528 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
529 phc->shm_name, __FUNCTION__, __LINE__);
534 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
535 DPDK_CHILD_TRACE(phc->shm_name);
538 cdtime_t now = cdtime();
539 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
540 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
542 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
543 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
544 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
547 if (phc->cmd == DPDK_CMD_QUIT) {
548 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
549 __LINE__, (long)getpid());
551 } else if (ret == -1 && errno == ETIMEDOUT) {
552 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
553 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
554 " timeout, did collectd terminate?\n",
556 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
561 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
562 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
563 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
566 /* Parent PID change means collectd died so quit the helper process. */
567 if (ppid != getppid()) {
568 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
569 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
572 /* Checking for DPDK primary process. */
573 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
574 if (phc->eal_initialized) {
576 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
579 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
582 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
583 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
584 __FUNCTION__, __LINE__);
589 if (!phc->eal_initialized) {
590 int ret = dpdk_helper_eal_init(phc);
592 DPDK_CHILD_LOG("Error initializing EAL\n");
593 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
595 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
596 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
597 __FUNCTION__, __LINE__);
604 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
605 DPDK_CHILD_TRACE(phc->shm_name);
607 pid_t ppid = getppid();
610 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
611 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
612 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
614 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
616 phc->cmd_result = -1;
619 /* now kick collectd to get results */
620 int err = sem_post(&phc->sema_cmd_complete);
621 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
622 __FUNCTION__, __LINE__, (long)getpid());
624 char errbuf[ERR_BUF_SIZE];
625 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
627 sstrerror(errno, errbuf, sizeof(errbuf)));
632 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
633 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
634 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
643 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
645 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
646 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
647 case DPDK_HELPER_WAITING_ON_PRIMARY:
648 return "DPDK_HELPER_WAITING_ON_PRIMARY";
649 case DPDK_HELPER_INITIALIZING:
650 return "DPDK_HELPER_INITIALIZING";
651 case DPDK_HELPER_INITIALIZING_EAL:
652 return "DPDK_HELPER_INITIALIZING_EAL";
653 case DPDK_HELPER_GRACEFUL_QUIT:
654 return "DPDK_HELPER_GRACEFUL_QUIT";
655 case DPDK_HELPER_NOT_INITIALIZED:
656 return "DPDK_HELPER_NOT_INITIALIZED";
662 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
663 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
664 dpdk_helper_status_str(phc->status));
665 char errbuf[ERR_BUF_SIZE];
667 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
669 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
670 phc->status = DPDK_HELPER_INITIALIZING;
671 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
673 int err = dpdk_helper_spawn(phc);
675 ERROR("dpdkstat: error spawning helper %s",
676 sstrerror(errno, errbuf, sizeof(errbuf)));
681 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
683 phc->status = DPDK_HELPER_INITIALIZING;
684 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
686 int err = dpdk_helper_spawn(phc);
688 ERROR("dpdkstat: error spawning helper %s",
689 sstrerror(errno, errbuf, sizeof(errbuf)));
694 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
701 #ifdef DPDK_HELPER_USE_PIPES
702 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
703 char buf[DPDK_MAX_BUFFER_SIZE];
704 char out[DPDK_MAX_BUFFER_SIZE];
706 /* non blocking check on helper logging pipe */
707 struct pollfd fds = {
708 .fd = phc->pipes[0], .events = POLLIN,
710 int data_avail = poll(&fds, 1, 0);
711 if (data_avail < 0) {
712 if (errno != EINTR || errno != EAGAIN) {
713 char errbuf[ERR_BUF_SIZE];
714 ERROR("%s: poll(2) failed: %s", phc->shm_name,
715 sstrerror(errno, errbuf, sizeof(errbuf)));
719 int nbytes = read(phc->pipes[0], buf, sizeof(buf));
722 sstrncpy(out, buf, nbytes);
723 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
726 #endif /* DPDK_HELPER_USE_PIPES */
728 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
729 cdtime_t cmd_wait_time) {
731 ERROR("Invalid argument(phc)");
735 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
736 (long)getpid(), cmd);
738 phc->cmd_wait_time = cmd_wait_time;
742 ret = dpdk_helper_status_check(phc);
744 #ifdef DPDK_HELPER_USE_PIPES
745 dpdk_helper_check_pipe(phc);
746 #endif /* DPDK_HELPER_USE_PIPES */
752 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
757 /* kick helper to process command */
758 int err = sem_post(&phc->sema_cmd_start);
760 char errbuf[ERR_BUF_SIZE];
761 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)\n",
762 sstrerror(errno, errbuf, sizeof(errbuf)));
767 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
768 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
772 if (phc->cmd != DPDK_CMD_QUIT) {
774 /* wait for helper to complete processing */
776 cdtime_t now = cdtime();
778 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
779 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
782 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
783 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
784 if (ret == -1 && errno == ETIMEDOUT) {
785 DPDK_HELPER_TRACE(phc->shm_name);
786 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
794 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
795 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
800 *result = phc->cmd_result;
804 #ifdef DPDK_HELPER_USE_PIPES
805 dpdk_helper_check_pipe(phc);
806 #endif /* DPDK_HELPER_USE_PIPES */
808 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
809 phc->cmd, phc->cmd_result);
814 uint64_t strtoull_safe(const char *str, int *err) {
819 val = strtoull(str, &endptr, 16);
821 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
830 uint128_t str_to_uint128(const char *str, int len) {
831 uint128_t lcore_mask;
834 memset(&lcore_mask, 0, sizeof(uint128_t));
836 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
837 ERROR("%s Value %s should be represened in hexadecimal format",
843 lcore_mask.low = strtoull_safe(str, &err);
847 char low_str[DATA_MAX_NAME_LEN];
848 char high_str[DATA_MAX_NAME_LEN];
850 memset(high_str, 0, sizeof(high_str));
851 memset(low_str, 0, sizeof(low_str));
853 strncpy(high_str, str, len - 16);
854 strncpy(low_str, str + len - 16, 16);
856 lcore_mask.low = strtoull_safe(low_str, &err);
860 lcore_mask.high = strtoull_safe(high_str, &err);