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 5
48 #define DPDK_MAX_BUFFER_SIZE (4096 * 4)
49 #define DPDK_CDM_DEFAULT_TIMEOUT 10000
51 enum DPDK_HELPER_STATUS {
52 DPDK_HELPER_NOT_INITIALIZED = 0,
53 DPDK_HELPER_INITIALIZING,
54 DPDK_HELPER_WAITING_ON_PRIMARY,
55 DPDK_HELPER_INITIALIZING_EAL,
56 DPDK_HELPER_ALIVE_SENDING_EVENTS,
57 DPDK_HELPER_GRACEFUL_QUIT,
60 #define DPDK_HELPER_TRACE(_name) \
61 DEBUG("%s:%s:%d pid=%ld", _name, __FUNCTION__, __LINE__, (long)getpid())
63 struct dpdk_helper_ctx_s {
65 dpdk_eal_config_t eal_config;
69 char shm_name[DATA_MAX_NAME_LEN];
72 sem_t sema_cmd_complete;
73 cdtime_t cmd_wait_time;
85 static int dpdk_shm_init(const char *name, size_t size, void **map);
86 static void dpdk_shm_cleanup(const char *name, size_t size, void *map);
88 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc);
89 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc);
90 static int dpdk_helper_eal_init(dpdk_helper_ctx_t *phc);
91 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid);
92 static int dpdk_helper_exit_command(dpdk_helper_ctx_t *phc,
93 enum DPDK_HELPER_STATUS status);
94 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
95 enum DPDK_HELPER_STATUS status);
96 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc);
97 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc);
98 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status);
100 static void dpdk_helper_config_default(dpdk_helper_ctx_t *phc) {
104 DPDK_HELPER_TRACE(phc->shm_name);
106 snprintf(phc->eal_config.coremask, DATA_MAX_NAME_LEN, "%s", "0xf");
107 snprintf(phc->eal_config.memory_channels, DATA_MAX_NAME_LEN, "%s", "1");
108 snprintf(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;
165 if (strcasecmp("Coremask", child->key) == 0) {
166 status = 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) {
171 cf_util_get_string_buffer(child, phc->eal_config.memory_channels,
172 sizeof(phc->eal_config.memory_channels));
173 DEBUG("dpdk_common: EAL:Memory Channels %s",
174 phc->eal_config.memory_channels);
175 } else if (strcasecmp("SocketMemory", child->key) == 0) {
176 status = cf_util_get_string_buffer(child, phc->eal_config.socket_memory,
177 sizeof(phc->eal_config.socket_memory));
178 DEBUG("dpdk_common: EAL:Socket memory %s", phc->eal_config.socket_memory);
179 } else if (strcasecmp("FilePrefix", child->key) == 0) {
180 char prefix[DATA_MAX_NAME_LEN];
182 status = cf_util_get_string_buffer(child, prefix, sizeof(prefix));
184 snprintf(phc->eal_config.file_prefix, DATA_MAX_NAME_LEN,
185 "/var/run/.%s_config", prefix);
186 DEBUG("dpdk_common: EAL:File prefix %s", phc->eal_config.file_prefix);
189 ERROR("dpdk_common: Invalid '%s' configuration option", child->key);
194 ERROR("dpdk_common: Parsing EAL configuration failed");
202 static int dpdk_shm_init(const char *name, size_t size, void **map) {
203 DPDK_HELPER_TRACE(name);
205 char errbuf[ERR_BUF_SIZE];
207 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
209 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s", name,
210 sstrerror(errno, errbuf, sizeof(errbuf)));
215 int ret = ftruncate(fd, size);
217 WARNING("dpdk_shm_init: Failed to resize SHM:%s",
218 sstrerror(errno, errbuf, sizeof(errbuf)));
221 dpdk_shm_cleanup(name, size, NULL);
225 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
226 if (*map == MAP_FAILED) {
227 WARNING("dpdk_shm_init:Failed to mmap SHM:%s",
228 sstrerror(errno, errbuf, sizeof(errbuf)));
231 dpdk_shm_cleanup(name, size, NULL);
234 /* File descriptor no longer needed for shared memory operations */
236 memset(*map, 0, size);
241 static void dpdk_shm_cleanup(const char *name, size_t size, void *map) {
242 DPDK_HELPER_TRACE(name);
243 char errbuf[ERR_BUF_SIZE];
246 * Call shm_unlink first, as 'name' might be no longer accessible after munmap
248 if (shm_unlink(name))
249 ERROR("shm_unlink failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
252 if (munmap(map, size))
253 ERROR("munmap failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
257 void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
259 return phc->priv_data;
264 int dpdk_helper_data_size_get(dpdk_helper_ctx_t *phc) {
266 DPDK_CHILD_LOG("Invalid argument(phc)\n");
270 return phc->shm_size - sizeof(dpdk_helper_ctx_t);
273 int dpdk_helper_init(const char *name, size_t data_size,
274 dpdk_helper_ctx_t **pphc) {
275 dpdk_helper_ctx_t *phc = NULL;
276 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
277 char errbuf[ERR_BUF_SIZE];
280 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
285 ERROR("%s:Invalid argument(name)", __FUNCTION__);
289 DPDK_HELPER_TRACE(name);
291 /* Allocate dpdk_helper_ctx_t and
292 * initialize a POSIX SHared Memory (SHM) object.
294 int err = dpdk_shm_init(name, shm_size, (void **)&phc);
299 err = sem_init(&phc->sema_cmd_start, 1, 0);
301 ERROR("sema_cmd_start semaphore init failed: %s",
302 sstrerror(errno, errbuf, sizeof(errbuf)));
304 dpdk_shm_cleanup(name, shm_size, (void *)phc);
308 err = sem_init(&phc->sema_cmd_complete, 1, 0);
310 ERROR("sema_cmd_complete semaphore init failed: %s",
311 sstrerror(errno, errbuf, sizeof(errbuf)));
312 sem_destroy(&phc->sema_cmd_start);
314 dpdk_shm_cleanup(name, shm_size, (void *)phc);
318 phc->shm_size = shm_size;
319 sstrncpy(phc->shm_name, name, sizeof(phc->shm_name));
321 dpdk_helper_config_default(phc);
328 void dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
332 DPDK_HELPER_TRACE(phc->shm_name);
334 close(phc->pipes[1]);
336 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
337 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
340 sem_destroy(&phc->sema_cmd_start);
341 sem_destroy(&phc->sema_cmd_complete);
342 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.file_prefix[0] != 0);
475 argp[argc++] = "collectd-dpdk";
478 argp[argc++] = phc->eal_config.coremask;
481 argp[argc++] = phc->eal_config.memory_channels;
483 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
484 argp[argc++] = "--socket-mem";
485 argp[argc++] = phc->eal_config.socket_memory;
488 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
489 argp[argc++] = "--file-prefix";
490 argp[argc++] = phc->eal_config.file_prefix;
493 argp[argc++] = "--proc-type";
494 argp[argc++] = "secondary";
496 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
498 int ret = rte_eal_init(argc, argp);
502 phc->eal_initialized = 0;
504 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
507 printf("dpdk_helper_eal_init: EAL arguments: ");
508 for (int i = 0; i < argc; i++) {
509 printf("%s ", argp[i]);
516 phc->eal_initialized = 1;
518 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
519 phc->shm_name, __FUNCTION__, __LINE__);
524 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
525 DPDK_CHILD_TRACE(phc->shm_name);
528 cdtime_t now = cdtime();
529 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
530 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
532 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
533 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
534 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
537 if (phc->cmd == DPDK_CMD_QUIT) {
538 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
539 __LINE__, (long)getpid());
541 } else if (ret == -1 && errno == ETIMEDOUT) {
542 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
543 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
544 " timeout, did collectd terminate?\n",
546 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
551 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
552 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
553 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
556 /* Parent PID change means collectd died so quit the helper process. */
557 if (ppid != getppid()) {
558 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
559 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
562 /* Checking for DPDK primary process. */
563 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
564 if (phc->eal_initialized) {
566 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
569 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
572 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
573 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
574 __FUNCTION__, __LINE__);
579 if (!phc->eal_initialized) {
580 int ret = dpdk_helper_eal_init(phc);
582 DPDK_CHILD_LOG("Error initializing EAL\n");
583 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
585 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
586 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
587 __FUNCTION__, __LINE__);
594 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
595 DPDK_CHILD_TRACE(phc->shm_name);
597 pid_t ppid = getppid();
600 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
601 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
602 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
604 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
606 phc->cmd_result = -1;
609 /* now kick collectd to get results */
610 int err = sem_post(&phc->sema_cmd_complete);
611 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
612 __FUNCTION__, __LINE__, (long)getpid());
614 char errbuf[ERR_BUF_SIZE];
615 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
617 sstrerror(errno, errbuf, sizeof(errbuf)));
622 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
623 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
624 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
633 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
635 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
636 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
637 case DPDK_HELPER_WAITING_ON_PRIMARY:
638 return "DPDK_HELPER_WAITING_ON_PRIMARY";
639 case DPDK_HELPER_INITIALIZING:
640 return "DPDK_HELPER_INITIALIZING";
641 case DPDK_HELPER_INITIALIZING_EAL:
642 return "DPDK_HELPER_INITIALIZING_EAL";
643 case DPDK_HELPER_GRACEFUL_QUIT:
644 return "DPDK_HELPER_GRACEFUL_QUIT";
645 case DPDK_HELPER_NOT_INITIALIZED:
646 return "DPDK_HELPER_NOT_INITIALIZED";
652 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
653 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
654 dpdk_helper_status_str(phc->status));
655 char errbuf[ERR_BUF_SIZE];
657 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
659 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
660 phc->status = DPDK_HELPER_INITIALIZING;
661 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
663 int err = dpdk_helper_spawn(phc);
665 ERROR("dpdkstat: error spawning helper %s",
666 sstrerror(errno, errbuf, sizeof(errbuf)));
671 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
673 phc->status = DPDK_HELPER_INITIALIZING;
674 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
676 int err = dpdk_helper_spawn(phc);
678 ERROR("dpdkstat: error spawning helper %s",
679 sstrerror(errno, errbuf, sizeof(errbuf)));
684 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
691 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
692 char buf[DPDK_MAX_BUFFER_SIZE];
693 char out[DPDK_MAX_BUFFER_SIZE];
695 /* non blocking check on helper logging pipe */
696 struct pollfd fds = {
697 .fd = phc->pipes[0], .events = POLLIN,
699 int data_avail = poll(&fds, 1, 0);
700 if (data_avail < 0) {
701 if (errno != EINTR || errno != EAGAIN) {
702 char errbuf[ERR_BUF_SIZE];
703 ERROR("%s: poll(2) failed: %s", phc->shm_name,
704 sstrerror(errno, errbuf, sizeof(errbuf)));
708 int nbytes = read(phc->pipes[0], buf, sizeof(buf));
711 sstrncpy(out, buf, nbytes);
712 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
716 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
717 cdtime_t cmd_wait_time) {
719 ERROR("Invalid argument(phc)");
723 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
724 (long)getpid(), cmd);
726 phc->cmd_wait_time = cmd_wait_time;
728 int ret = dpdk_helper_status_check(phc);
730 dpdk_helper_check_pipe(phc);
736 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
741 /* kick helper to process command */
742 int err = sem_post(&phc->sema_cmd_start);
744 char errbuf[ERR_BUF_SIZE];
745 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)",
746 sstrerror(errno, errbuf, sizeof(errbuf)));
751 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
752 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
756 if (phc->cmd != DPDK_CMD_QUIT) {
758 /* wait for helper to complete processing */
760 cdtime_t now = cdtime();
762 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
763 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
766 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
767 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
768 if (ret == -1 && errno == ETIMEDOUT) {
769 DPDK_HELPER_TRACE(phc->shm_name);
770 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
778 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
779 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
784 *result = phc->cmd_result;
788 dpdk_helper_check_pipe(phc);
790 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
791 phc->cmd, phc->cmd_result);
796 uint64_t strtoull_safe(const char *str, int *err) {
801 val = strtoull(str, &endptr, 16);
803 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
812 uint128_t str_to_uint128(const char *str, int len) {
813 uint128_t lcore_mask;
816 memset(&lcore_mask, 0, sizeof(lcore_mask));
818 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
819 ERROR("%s Value %s should be represened in hexadecimal format",
823 /* If str is <= 64 bit long ('0x' + 16 chars = 18 chars) then
824 * conversion is straightforward. Otherwise str is splitted into 64b long
827 lcore_mask.low = strtoull_safe(str, &err);
831 char low_str[DATA_MAX_NAME_LEN];
832 char high_str[DATA_MAX_NAME_LEN];
834 memset(high_str, 0, sizeof(high_str));
835 memset(low_str, 0, sizeof(low_str));
837 strncpy(high_str, str, len - 16);
838 strncpy(low_str, str + len - 16, 16);
840 lcore_mask.low = strtoull_safe(low_str, &err);
844 lcore_mask.high = strtoull_safe(high_str, &err);
853 uint8_t dpdk_helper_eth_dev_count() {
854 uint8_t ports = rte_eth_dev_count();
857 "%s:%d: No DPDK ports available. Check bound devices to DPDK driver.\n",
858 __FUNCTION__, __LINE__);
862 if (ports > RTE_MAX_ETHPORTS) {
863 ERROR("%s:%d: Number of DPDK ports (%u) is greater than "
864 "RTE_MAX_ETHPORTS=%d. Ignoring extra ports\n",
865 __FUNCTION__, __LINE__, ports, RTE_MAX_ETHPORTS);
866 ports = RTE_MAX_ETHPORTS;