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=%ld", _name, __FUNCTION__, __LINE__, (long)getpid())
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;
84 static int dpdk_shm_init(const char *name, size_t size, void **map);
85 static void dpdk_shm_cleanup(const char *name, size_t size, void *map);
87 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc);
88 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc);
89 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc);
90 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid);
91 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
92 enum DPDK_HELPER_STATUS status);
93 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
94 enum DPDK_HELPER_STATUS status);
95 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc);
96 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc);
97 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status);
99 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc) {
103 DPDK_HELPER_TRACE(phc->shm_name);
105 ssnprintf(phc->eal_config.coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
106 ssnprintf(phc->eal_config.memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
107 ssnprintf(phc->eal_config.process_type, DATA_MAX_NAME_LEN, "%s", "secondary");
108 ssnprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN, "%s",
109 DPDK_DEFAULT_RTE_CONFIG);
112 int dpdk_helper_eal_config_set(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
114 ERROR("Invalid argument (phc)");
118 DPDK_HELPER_TRACE(phc->shm_name);
121 ERROR("Invalid argument (ec)");
125 memcpy(&phc->eal_config, ec, sizeof(phc->eal_config));
130 int dpdk_helper_eal_config_get(dpdk_helper_ctx_t *phc, dpdk_eal_config_t *ec) {
132 ERROR("Invalid argument (phc)");
136 DPDK_HELPER_TRACE(phc->shm_name);
139 ERROR("Invalid argument (ec)");
143 memcpy(ec, &phc->eal_config, sizeof(*ec));
148 int dpdk_helper_eal_config_parse(dpdk_helper_ctx_t *phc, oconfig_item_t *ci) {
149 DPDK_HELPER_TRACE(phc->shm_name);
152 ERROR("Invalid argument (phc)");
157 ERROR("Invalid argument (ci)");
162 for (int i = 0; i < ci->children_num; i++) {
163 oconfig_item_t *child = ci->children + i;
164 if (strcasecmp("Coremask", child->key) == 0) {
165 status = cf_util_get_string_buffer(child, phc->eal_config.coremask,
166 sizeof(phc->eal_config.coremask));
167 DEBUG("dpdk_common: EAL:Coremask %s", phc->eal_config.coremask);
168 } 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 status = 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 status = 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);
188 ERROR("dpdk_common: Invalid '%s' configuration option", child->key);
193 ERROR("dpdk_common: Parsing EAL configuration failed");
201 static int dpdk_shm_init(const char *name, size_t size, void **map) {
202 DPDK_HELPER_TRACE(name);
204 char errbuf[ERR_BUF_SIZE];
206 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
208 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s", name,
209 sstrerror(errno, errbuf, sizeof(errbuf)));
214 int ret = ftruncate(fd, size);
216 WARNING("dpdk_shm_init: Failed to resize SHM:%s",
217 sstrerror(errno, errbuf, sizeof(errbuf)));
220 dpdk_shm_cleanup(name, size, NULL);
224 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
225 if (*map == MAP_FAILED) {
226 WARNING("dpdk_shm_init:Failed to mmap SHM:%s",
227 sstrerror(errno, errbuf, sizeof(errbuf)));
230 dpdk_shm_cleanup(name, size, NULL);
233 /* File descriptor no longer needed for shared memory operations */
235 memset(*map, 0, size);
240 static void dpdk_shm_cleanup(const char *name, size_t size, void *map) {
241 DPDK_HELPER_TRACE(name);
242 char errbuf[ERR_BUF_SIZE];
245 if (munmap(map, size))
246 ERROR("munmap failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
249 if (shm_unlink(name))
250 ERROR("shm_unlink failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
253 void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
255 return 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) {
271 dpdk_helper_ctx_t *phc = NULL;
272 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
273 char errbuf[ERR_BUF_SIZE];
276 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
281 ERROR("%s:Invalid argument(name)", __FUNCTION__);
285 DPDK_HELPER_TRACE(name);
287 /* Allocate dpdk_helper_ctx_t and
288 * initialize a POSIX SHared Memory (SHM) object.
290 int err = dpdk_shm_init(name, shm_size, (void **)&phc);
295 err = sem_init(&phc->sema_cmd_start, 1, 0);
297 ERROR("sema_cmd_start semaphore init failed: %s",
298 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",
307 sstrerror(errno, errbuf, sizeof(errbuf)));
308 sem_destroy(&phc->sema_cmd_start);
310 dpdk_shm_cleanup(name, shm_size, (void *)phc);
314 phc->shm_size = shm_size;
315 phc->shm_name = name;
317 dpdk_helper_config_default(phc);
324 int dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
326 ERROR("%s:Invalid argument(phc)", __FUNCTION__);
330 DPDK_HELPER_TRACE(phc->shm_name);
332 close(phc->pipes[1]);
334 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
335 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
338 sem_destroy(&phc->sema_cmd_start);
339 sem_destroy(&phc->sema_cmd_complete);
340 dpdk_shm_cleanup(phc->shm_name, phc->shm_size, (void *)phc);
345 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc) {
346 char errbuf[ERR_BUF_SIZE];
348 ERROR("Invalid argument(phc)");
352 DPDK_HELPER_TRACE(phc->shm_name);
354 phc->eal_initialized = 0;
355 phc->cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
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",
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)));
390 close(phc->pipes[1]);
391 DEBUG("%s:dpdk_helper_spawn: helper pid %lu", phc->shm_name,
393 } else if (pid == 0) {
394 /* Replace stdout with a pipe to collectd. */
395 close(phc->pipes[0]);
396 close(STDOUT_FILENO);
397 dup2(phc->pipes[1], STDOUT_FILENO);
398 DPDK_CHILD_TRACE(phc->shm_name);
399 dpdk_helper_worker(phc);
402 ERROR("dpdk_helper_start: Failed to fork helper process: %s",
403 sstrerror(errno, errbuf, sizeof(errbuf)));
410 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
411 enum DPDK_HELPER_STATUS status) {
412 DPDK_CHILD_LOG("%s:%s:%d %s\n", phc->shm_name, __FUNCTION__, __LINE__,
413 dpdk_helper_status_str(status));
415 close(phc->pipes[1]);
417 phc->status = status;
424 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
425 enum DPDK_HELPER_STATUS status) {
426 char errbuf[ERR_BUF_SIZE];
427 DPDK_HELPER_TRACE(phc->shm_name);
429 close(phc->pipes[1]);
431 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
432 phc->status = status;
433 DEBUG("%s:%s:%d %s", phc->shm_name, __FUNCTION__, __LINE__,
434 dpdk_helper_status_str(status));
436 int ret = dpdk_helper_command(phc, DPDK_CMD_QUIT, NULL, 0);
438 DEBUG("%s:%s:%d kill helper (pid=%lu)", phc->shm_name, __FUNCTION__,
439 __LINE__, (long)phc->pid);
441 int err = kill(phc->pid, SIGKILL);
443 ERROR("%s error sending kill to helper: %s", __FUNCTION__,
444 sstrerror(errno, errbuf, sizeof(errbuf)));
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)));
462 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc) {
463 phc->status = DPDK_HELPER_INITIALIZING_EAL;
464 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (start)\n",
465 phc->shm_name, __FUNCTION__, __LINE__);
467 char *argp[DPDK_EAL_ARGC * 2 + 1];
470 /* EAL config must be initialized */
471 assert(phc->eal_config.coremask[0] != 0);
472 assert(phc->eal_config.memory_channels[0] != 0);
473 assert(phc->eal_config.process_type[0] != 0);
474 assert(phc->eal_config.file_prefix[0] != 0);
476 argp[argc++] = "collectd-dpdk";
479 argp[argc++] = phc->eal_config.coremask;
482 argp[argc++] = phc->eal_config.memory_channels;
484 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
485 argp[argc++] = "--socket-mem";
486 argp[argc++] = phc->eal_config.socket_memory;
489 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
490 argp[argc++] = "--file-prefix";
491 argp[argc++] = phc->eal_config.file_prefix;
494 argp[argc++] = "--proc-type";
495 argp[argc++] = phc->eal_config.process_type;
497 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
499 int ret = rte_eal_init(argc, argp);
503 phc->eal_initialized = 0;
505 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
508 printf("dpdk_helper_eal_init: EAL arguments: ");
509 for (int i = 0; i < argc; i++) {
510 printf("%s ", argp[i]);
517 phc->eal_initialized = 1;
519 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
520 phc->shm_name, __FUNCTION__, __LINE__);
525 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
526 DPDK_CHILD_TRACE(phc->shm_name);
529 cdtime_t now = cdtime();
530 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
531 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
533 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
534 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
535 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
538 if (phc->cmd == DPDK_CMD_QUIT) {
539 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
540 __LINE__, (long)getpid());
542 } else if (ret == -1 && errno == ETIMEDOUT) {
543 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
544 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
545 " timeout, did collectd terminate?\n",
547 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
552 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
553 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
554 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
557 /* Parent PID change means collectd died so quit the helper process. */
558 if (ppid != getppid()) {
559 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
560 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
563 /* Checking for DPDK primary process. */
564 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
565 if (phc->eal_initialized) {
567 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
570 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
573 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
574 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
575 __FUNCTION__, __LINE__);
580 if (!phc->eal_initialized) {
581 int ret = dpdk_helper_eal_init(phc);
583 DPDK_CHILD_LOG("Error initializing EAL\n");
584 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
586 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
587 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
588 __FUNCTION__, __LINE__);
595 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
596 DPDK_CHILD_TRACE(phc->shm_name);
598 pid_t ppid = getppid();
601 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
602 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
603 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
605 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
607 phc->cmd_result = -1;
610 /* now kick collectd to get results */
611 int err = sem_post(&phc->sema_cmd_complete);
612 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
613 __FUNCTION__, __LINE__, (long)getpid());
615 char errbuf[ERR_BUF_SIZE];
616 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
618 sstrerror(errno, errbuf, sizeof(errbuf)));
623 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
624 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
625 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
634 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
636 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
637 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
638 case DPDK_HELPER_WAITING_ON_PRIMARY:
639 return "DPDK_HELPER_WAITING_ON_PRIMARY";
640 case DPDK_HELPER_INITIALIZING:
641 return "DPDK_HELPER_INITIALIZING";
642 case DPDK_HELPER_INITIALIZING_EAL:
643 return "DPDK_HELPER_INITIALIZING_EAL";
644 case DPDK_HELPER_GRACEFUL_QUIT:
645 return "DPDK_HELPER_GRACEFUL_QUIT";
646 case DPDK_HELPER_NOT_INITIALIZED:
647 return "DPDK_HELPER_NOT_INITIALIZED";
653 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
654 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
655 dpdk_helper_status_str(phc->status));
656 char errbuf[ERR_BUF_SIZE];
658 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
660 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
661 phc->status = DPDK_HELPER_INITIALIZING;
662 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
664 int err = dpdk_helper_spawn(phc);
666 ERROR("dpdkstat: error spawning helper %s",
667 sstrerror(errno, errbuf, sizeof(errbuf)));
672 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
674 phc->status = DPDK_HELPER_INITIALIZING;
675 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
677 int err = dpdk_helper_spawn(phc);
679 ERROR("dpdkstat: error spawning helper %s",
680 sstrerror(errno, errbuf, sizeof(errbuf)));
685 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
692 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
693 char buf[DPDK_MAX_BUFFER_SIZE];
694 char out[DPDK_MAX_BUFFER_SIZE];
696 /* non blocking check on helper logging pipe */
697 struct pollfd fds = {
698 .fd = phc->pipes[0], .events = POLLIN,
700 int data_avail = poll(&fds, 1, 0);
701 if (data_avail < 0) {
702 if (errno != EINTR || errno != EAGAIN) {
703 char errbuf[ERR_BUF_SIZE];
704 ERROR("%s: poll(2) failed: %s", phc->shm_name,
705 sstrerror(errno, errbuf, sizeof(errbuf)));
709 int nbytes = read(phc->pipes[0], buf, sizeof(buf));
712 sstrncpy(out, buf, nbytes);
713 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
717 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
718 cdtime_t cmd_wait_time) {
720 ERROR("Invalid argument(phc)");
724 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
725 (long)getpid(), cmd);
727 phc->cmd_wait_time = cmd_wait_time;
729 int ret = dpdk_helper_status_check(phc);
731 dpdk_helper_check_pipe(phc);
737 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
742 /* kick helper to process command */
743 int err = sem_post(&phc->sema_cmd_start);
745 char errbuf[ERR_BUF_SIZE];
746 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)",
747 sstrerror(errno, errbuf, sizeof(errbuf)));
752 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
753 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
757 if (phc->cmd != DPDK_CMD_QUIT) {
759 /* wait for helper to complete processing */
761 cdtime_t now = cdtime();
763 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
764 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
767 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
768 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
769 if (ret == -1 && errno == ETIMEDOUT) {
770 DPDK_HELPER_TRACE(phc->shm_name);
771 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
779 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
780 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
785 *result = phc->cmd_result;
789 dpdk_helper_check_pipe(phc);
791 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
792 phc->cmd, phc->cmd_result);
797 uint64_t strtoull_safe(const char *str, int *err) {
802 val = strtoull(str, &endptr, 16);
804 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
813 uint128_t str_to_uint128(const char *str, int len) {
814 uint128_t lcore_mask;
817 memset(&lcore_mask, 0, sizeof(lcore_mask));
819 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
820 ERROR("%s Value %s should be represened in hexadecimal format",
824 /* If str is <= 64 bit long ('0x' + 16 chars = 18 chars) then
825 * conversion is straightforward. Otherwise str is splitted into 64b long
828 lcore_mask.low = strtoull_safe(str, &err);
832 char low_str[DATA_MAX_NAME_LEN];
833 char high_str[DATA_MAX_NAME_LEN];
835 memset(high_str, 0, sizeof(high_str));
836 memset(low_str, 0, sizeof(low_str));
838 strncpy(high_str, str, len - 16);
839 strncpy(low_str, str + len - 16, 16);
841 lcore_mask.low = strtoull_safe(low_str, &err);
845 lcore_mask.high = strtoull_safe(high_str, &err);