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(child, phc->eal_config.rte_driver_lib_path,
195 sizeof(phc->eal_config.rte_driver_lib_path));
196 DEBUG("dpdk_common: EAL:RteDriverLibPath %s", phc->eal_config.rte_driver_lib_path);
198 ERROR("dpdk_common: Invalid '%s' configuration option", child->key);
203 ERROR("dpdk_common: Parsing EAL configuration failed");
211 static int dpdk_shm_init(const char *name, size_t size, void **map) {
212 DPDK_HELPER_TRACE(name);
214 char errbuf[ERR_BUF_SIZE];
216 int fd = shm_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
218 WARNING("dpdk_shm_init: Failed to open %s as SHM:%s", name,
219 sstrerror(errno, errbuf, sizeof(errbuf)));
224 int ret = ftruncate(fd, size);
226 WARNING("dpdk_shm_init: Failed to resize SHM:%s",
227 sstrerror(errno, errbuf, sizeof(errbuf)));
230 dpdk_shm_cleanup(name, size, NULL);
234 *map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
235 if (*map == MAP_FAILED) {
236 WARNING("dpdk_shm_init:Failed to mmap SHM:%s",
237 sstrerror(errno, errbuf, sizeof(errbuf)));
240 dpdk_shm_cleanup(name, size, NULL);
243 /* File descriptor no longer needed for shared memory operations */
245 memset(*map, 0, size);
250 static void dpdk_shm_cleanup(const char *name, size_t size, void *map) {
251 DPDK_HELPER_TRACE(name);
252 char errbuf[ERR_BUF_SIZE];
255 * Call shm_unlink first, as 'name' might be no longer accessible after munmap
257 if (shm_unlink(name))
258 ERROR("shm_unlink failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
261 if (munmap(map, size))
262 ERROR("munmap failure %s", sstrerror(errno, errbuf, sizeof(errbuf)));
266 void *dpdk_helper_priv_get(dpdk_helper_ctx_t *phc) {
268 return phc->priv_data;
273 int dpdk_helper_data_size_get(dpdk_helper_ctx_t *phc) {
275 DPDK_CHILD_LOG("Invalid argument(phc)\n");
279 return phc->shm_size - sizeof(dpdk_helper_ctx_t);
282 int dpdk_helper_init(const char *name, size_t data_size,
283 dpdk_helper_ctx_t **pphc) {
284 dpdk_helper_ctx_t *phc = NULL;
285 size_t shm_size = sizeof(dpdk_helper_ctx_t) + data_size;
286 char errbuf[ERR_BUF_SIZE];
289 ERROR("%s:Invalid argument(pphc)", __FUNCTION__);
294 ERROR("%s:Invalid argument(name)", __FUNCTION__);
298 DPDK_HELPER_TRACE(name);
300 /* Allocate dpdk_helper_ctx_t and
301 * initialize a POSIX SHared Memory (SHM) object.
303 int err = dpdk_shm_init(name, shm_size, (void **)&phc);
308 err = sem_init(&phc->sema_cmd_start, 1, 0);
310 ERROR("sema_cmd_start semaphore init failed: %s",
311 sstrerror(errno, errbuf, sizeof(errbuf)));
313 dpdk_shm_cleanup(name, shm_size, (void *)phc);
317 err = sem_init(&phc->sema_cmd_complete, 1, 0);
319 ERROR("sema_cmd_complete semaphore init failed: %s",
320 sstrerror(errno, errbuf, sizeof(errbuf)));
321 sem_destroy(&phc->sema_cmd_start);
323 dpdk_shm_cleanup(name, shm_size, (void *)phc);
327 phc->shm_size = shm_size;
328 sstrncpy(phc->shm_name, name, sizeof(phc->shm_name));
330 dpdk_helper_config_default(phc);
337 void dpdk_helper_shutdown(dpdk_helper_ctx_t *phc) {
341 DPDK_HELPER_TRACE(phc->shm_name);
343 close(phc->pipes[1]);
345 if (phc->status != DPDK_HELPER_NOT_INITIALIZED) {
346 dpdk_helper_exit_command(phc, DPDK_HELPER_GRACEFUL_QUIT);
349 sem_destroy(&phc->sema_cmd_start);
350 sem_destroy(&phc->sema_cmd_complete);
351 dpdk_shm_cleanup(phc->shm_name, phc->shm_size, (void *)phc);
354 static int dpdk_helper_spawn(dpdk_helper_ctx_t *phc) {
355 char errbuf[ERR_BUF_SIZE];
357 ERROR("Invalid argument(phc)");
361 DPDK_HELPER_TRACE(phc->shm_name);
363 phc->eal_initialized = 0;
364 phc->cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
367 * Create a pipe for helper stdout back to collectd. This is necessary for
368 * logging EAL failures, as rte_eal_init() calls rte_panic().
371 DEBUG("dpdk_helper_spawn: collectd closing helper pipe %d", phc->pipes[1]);
373 DEBUG("dpdk_helper_spawn: collectd helper pipe %d, not closing",
377 if (pipe(phc->pipes) != 0) {
378 DEBUG("dpdk_helper_spawn: Could not create helper pipe: %s",
379 sstrerror(errno, errbuf, sizeof(errbuf)));
383 int pipe0_flags = fcntl(phc->pipes[0], F_GETFL, 0);
384 int pipe1_flags = fcntl(phc->pipes[1], F_GETFL, 0);
385 if (pipe0_flags == -1 || pipe1_flags == -1) {
386 WARNING("dpdk_helper_spawn: error setting up pipe flags: %s",
387 sstrerror(errno, errbuf, sizeof(errbuf)));
389 int pipe0_err = fcntl(phc->pipes[0], F_SETFL, pipe1_flags | O_NONBLOCK);
390 int pipe1_err = fcntl(phc->pipes[1], F_SETFL, pipe0_flags | O_NONBLOCK);
391 if (pipe0_err == -1 || pipe1_err == -1) {
392 WARNING("dpdk_helper_spawn: error setting up pipes: %s",
393 sstrerror(errno, errbuf, sizeof(errbuf)));
399 close(phc->pipes[1]);
400 DEBUG("%s:dpdk_helper_spawn: helper pid %lu", phc->shm_name,
402 } else if (pid == 0) {
403 /* Replace stdout with a pipe to collectd. */
404 close(phc->pipes[0]);
405 close(STDOUT_FILENO);
406 dup2(phc->pipes[1], STDOUT_FILENO);
407 DPDK_CHILD_TRACE(phc->shm_name);
408 dpdk_helper_worker(phc);
411 ERROR("dpdk_helper_start: Failed to fork helper process: %s",
412 sstrerror(errno, errbuf, sizeof(errbuf)));
419 static int dpdk_helper_exit(dpdk_helper_ctx_t *phc,
420 enum DPDK_HELPER_STATUS status) {
421 DPDK_CHILD_LOG("%s:%s:%d %s\n", phc->shm_name, __FUNCTION__, __LINE__,
422 dpdk_helper_status_str(status));
424 close(phc->pipes[1]);
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 close(phc->pipes[1]);
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", __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", __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.file_prefix[0] != 0);
484 argp[argc++] = "collectd-dpdk";
487 argp[argc++] = phc->eal_config.coremask;
490 argp[argc++] = phc->eal_config.memory_channels;
492 if (strcasecmp(phc->eal_config.socket_memory, "") != 0) {
493 argp[argc++] = "--socket-mem";
494 argp[argc++] = phc->eal_config.socket_memory;
497 if (strcasecmp(phc->eal_config.file_prefix, DPDK_DEFAULT_RTE_CONFIG) != 0) {
498 argp[argc++] = "--file-prefix";
499 argp[argc++] = phc->eal_config.file_prefix;
502 argp[argc++] = "--proc-type";
503 argp[argc++] = "secondary";
505 if (strcasecmp(phc->eal_config.log_level, "") != 0) {
506 argp[argc++] = "--log-level";
507 argp[argc++] = phc->eal_config.log_level;
509 if (strcasecmp(phc->eal_config.rte_driver_lib_path, "") != 0) {
511 argp[argc++] = phc->eal_config.rte_driver_lib_path;
514 assert(argc <= (DPDK_EAL_ARGC * 2 + 1));
516 int ret = rte_eal_init(argc, argp);
520 phc->eal_initialized = 0;
522 DPDK_CHILD_LOG("dpdk_helper_eal_init: ERROR initializing EAL ret=%d\n",
525 printf("dpdk_helper_eal_init: EAL arguments: ");
526 for (int i = 0; i < argc; i++) {
527 printf("%s ", argp[i]);
534 phc->eal_initialized = 1;
536 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_INITIALIZING_EAL (done)\n",
537 phc->shm_name, __FUNCTION__, __LINE__);
542 static int dpdk_helper_cmd_wait(dpdk_helper_ctx_t *phc, pid_t ppid) {
543 DPDK_CHILD_TRACE(phc->shm_name);
546 cdtime_t now = cdtime();
547 cdtime_t cmd_wait_time = MS_TO_CDTIME_T(1500) + phc->cmd_wait_time * 2;
548 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
550 int ret = sem_timedwait(&phc->sema_cmd_start, &ts);
551 DPDK_CHILD_LOG("%s:%s:%d pid=%lu got sema_cmd_start (ret=%d, errno=%d)\n",
552 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), ret,
555 if (phc->cmd == DPDK_CMD_QUIT) {
556 DPDK_CHILD_LOG("%s:%s:%d pid=%lu exiting\n", phc->shm_name, __FUNCTION__,
557 __LINE__, (long)getpid());
559 } else if (ret == -1 && errno == ETIMEDOUT) {
560 if (phc->status == DPDK_HELPER_ALIVE_SENDING_EVENTS) {
561 DPDK_CHILD_LOG("%s:dpdk_helper_cmd_wait: sem timedwait()"
562 " timeout, did collectd terminate?\n",
564 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
569 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
570 DPDK_CHILD_LOG("%s:%s:%d pid=%lu wait sema_cmd_start (value=%d)\n",
571 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(), val);
574 /* Parent PID change means collectd died so quit the helper process. */
575 if (ppid != getppid()) {
576 DPDK_CHILD_LOG("dpdk_helper_cmd_wait: parent PID changed, quitting.\n");
577 dpdk_helper_exit(phc, DPDK_HELPER_GRACEFUL_QUIT);
580 /* Checking for DPDK primary process. */
581 if (!rte_eal_primary_proc_alive(phc->eal_config.file_prefix)) {
582 if (phc->eal_initialized) {
584 "%s:dpdk_helper_cmd_wait: no primary alive but EAL initialized:"
587 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
590 phc->status = DPDK_HELPER_WAITING_ON_PRIMARY;
591 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_WAITING_ON_PRIMARY\n", phc->shm_name,
592 __FUNCTION__, __LINE__);
597 if (!phc->eal_initialized) {
598 int ret = dpdk_helper_eal_init(phc);
600 DPDK_CHILD_LOG("Error initializing EAL\n");
601 dpdk_helper_exit(phc, DPDK_HELPER_NOT_INITIALIZED);
603 phc->status = DPDK_HELPER_ALIVE_SENDING_EVENTS;
604 DPDK_CHILD_LOG("%s:%s:%d DPDK_HELPER_ALIVE_SENDING_EVENTS\n", phc->shm_name,
605 __FUNCTION__, __LINE__);
612 static int dpdk_helper_worker(dpdk_helper_ctx_t *phc) {
613 DPDK_CHILD_TRACE(phc->shm_name);
615 pid_t ppid = getppid();
618 if (dpdk_helper_cmd_wait(phc, ppid) == 0) {
619 DPDK_CHILD_LOG("%s:%s:%d DPDK command handle (cmd=%d, pid=%lu)\n",
620 phc->shm_name, __FUNCTION__, __LINE__, phc->cmd,
622 phc->cmd_result = dpdk_helper_command_handler(phc, phc->cmd);
624 phc->cmd_result = -1;
627 /* now kick collectd to get results */
628 int err = sem_post(&phc->sema_cmd_complete);
629 DPDK_CHILD_LOG("%s:%s:%d post sema_cmd_complete (pid=%lu)\n", phc->shm_name,
630 __FUNCTION__, __LINE__, (long)getpid());
632 char errbuf[ERR_BUF_SIZE];
633 DPDK_CHILD_LOG("dpdk_helper_worker: error posting sema_cmd_complete "
635 sstrerror(errno, errbuf, sizeof(errbuf)));
640 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
641 DPDK_CHILD_LOG("%s:%s:%d pid=%lu sema_cmd_complete (value=%d)\n",
642 phc->shm_name, __FUNCTION__, __LINE__, (long)getpid(),
651 static const char *dpdk_helper_status_str(enum DPDK_HELPER_STATUS status) {
653 case DPDK_HELPER_ALIVE_SENDING_EVENTS:
654 return "DPDK_HELPER_ALIVE_SENDING_EVENTS";
655 case DPDK_HELPER_WAITING_ON_PRIMARY:
656 return "DPDK_HELPER_WAITING_ON_PRIMARY";
657 case DPDK_HELPER_INITIALIZING:
658 return "DPDK_HELPER_INITIALIZING";
659 case DPDK_HELPER_INITIALIZING_EAL:
660 return "DPDK_HELPER_INITIALIZING_EAL";
661 case DPDK_HELPER_GRACEFUL_QUIT:
662 return "DPDK_HELPER_GRACEFUL_QUIT";
663 case DPDK_HELPER_NOT_INITIALIZED:
664 return "DPDK_HELPER_NOT_INITIALIZED";
670 static int dpdk_helper_status_check(dpdk_helper_ctx_t *phc) {
671 DEBUG("%s:%s:%d pid=%u %s", phc->shm_name, __FUNCTION__, __LINE__, getpid(),
672 dpdk_helper_status_str(phc->status));
673 char errbuf[ERR_BUF_SIZE];
675 if (phc->status == DPDK_HELPER_GRACEFUL_QUIT) {
677 } else if (phc->status == DPDK_HELPER_NOT_INITIALIZED) {
678 phc->status = DPDK_HELPER_INITIALIZING;
679 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
681 int err = dpdk_helper_spawn(phc);
683 ERROR("dpdkstat: error spawning helper %s",
684 sstrerror(errno, errbuf, sizeof(errbuf)));
689 pid_t ws = waitpid(phc->pid, NULL, WNOHANG);
691 phc->status = DPDK_HELPER_INITIALIZING;
692 DEBUG("%s:%s:%d DPDK_HELPER_INITIALIZING", phc->shm_name, __FUNCTION__,
694 int err = dpdk_helper_spawn(phc);
696 ERROR("dpdkstat: error spawning helper %s",
697 sstrerror(errno, errbuf, sizeof(errbuf)));
702 if (phc->status == DPDK_HELPER_INITIALIZING_EAL) {
709 static void dpdk_helper_check_pipe(dpdk_helper_ctx_t *phc) {
710 char buf[DPDK_MAX_BUFFER_SIZE];
711 char out[DPDK_MAX_BUFFER_SIZE];
713 /* non blocking check on helper logging pipe */
714 struct pollfd fds = {
715 .fd = phc->pipes[0], .events = POLLIN,
717 int data_avail = poll(&fds, 1, 0);
718 DEBUG("%s:dpdk_helper_check_pipe: poll data_avail=%d", phc->shm_name, data_avail);
719 if (data_avail < 0) {
720 if (errno != EINTR || errno != EAGAIN) {
721 char errbuf[ERR_BUF_SIZE];
722 ERROR("%s: poll(2) failed: %s", phc->shm_name,
723 sstrerror(errno, errbuf, sizeof(errbuf)));
727 int nbytes = read(phc->pipes[0], buf, (sizeof(buf) - 1));
728 DEBUG("%s:dpdk_helper_check_pipe: read nbytes=%d", phc->shm_name, nbytes);
732 sstrncpy(out, buf, (nbytes + 1));
733 DEBUG("%s: helper process:\n%s", phc->shm_name, out);
737 int dpdk_helper_command(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd, int *result,
738 cdtime_t cmd_wait_time) {
740 ERROR("Invalid argument(phc)");
744 DEBUG("%s:%s:%d pid=%lu, cmd=%d", phc->shm_name, __FUNCTION__, __LINE__,
745 (long)getpid(), cmd);
747 phc->cmd_wait_time = cmd_wait_time;
749 int ret = dpdk_helper_status_check(phc);
751 dpdk_helper_check_pipe(phc);
757 DEBUG("%s: DPDK command execute (cmd=%d)", phc->shm_name, cmd);
762 /* kick helper to process command */
763 int err = sem_post(&phc->sema_cmd_start);
765 char errbuf[ERR_BUF_SIZE];
766 ERROR("dpdk_helper_worker: error posting sema_cmd_start semaphore (%s)",
767 sstrerror(errno, errbuf, sizeof(errbuf)));
772 if (sem_getvalue(&phc->sema_cmd_start, &val) == 0)
773 DEBUG("%s:dpdk_helper_command: post sema_cmd_start (value=%d)",
777 if (phc->cmd != DPDK_CMD_QUIT) {
779 /* wait for helper to complete processing */
781 cdtime_t now = cdtime();
783 if (phc->status != DPDK_HELPER_ALIVE_SENDING_EVENTS) {
784 cmd_wait_time = MS_TO_CDTIME_T(DPDK_CDM_DEFAULT_TIMEOUT);
787 ts = CDTIME_T_TO_TIMESPEC(now + cmd_wait_time);
788 ret = sem_timedwait(&phc->sema_cmd_complete, &ts);
789 if (ret == -1 && errno == ETIMEDOUT) {
790 DPDK_HELPER_TRACE(phc->shm_name);
791 DEBUG("%s:sema_cmd_start: timeout in collectd thread: is a DPDK Primary "
799 if (sem_getvalue(&phc->sema_cmd_complete, &val) == 0)
800 DEBUG("%s:dpdk_helper_command: wait sema_cmd_complete (value=%d)",
805 *result = phc->cmd_result;
809 dpdk_helper_check_pipe(phc);
811 DEBUG("%s: DPDK command complete (cmd=%d, result=%d)", phc->shm_name,
812 phc->cmd, phc->cmd_result);
817 uint64_t strtoull_safe(const char *str, int *err) {
822 val = strtoull(str, &endptr, 16);
824 ERROR("%s Failed to parse the value %s, endptr=%c", __FUNCTION__, str,
833 uint128_t str_to_uint128(const char *str, int len) {
834 uint128_t lcore_mask;
837 memset(&lcore_mask, 0, sizeof(lcore_mask));
839 if (len <= 2 || strncmp(str, "0x", 2) != 0) {
840 ERROR("%s Value %s should be represened in hexadecimal format",
844 /* If str is <= 64 bit long ('0x' + 16 chars = 18 chars) then
845 * conversion is straightforward. Otherwise str is splitted into 64b long
848 lcore_mask.low = strtoull_safe(str, &err);
852 char low_str[DATA_MAX_NAME_LEN];
853 char high_str[DATA_MAX_NAME_LEN];
855 memset(high_str, 0, sizeof(high_str));
856 memset(low_str, 0, sizeof(low_str));
858 strncpy(high_str, str, len - 16);
859 strncpy(low_str, str + len - 16, 16);
861 lcore_mask.low = strtoull_safe(low_str, &err);
865 lcore_mask.high = strtoull_safe(high_str, &err);
874 uint8_t dpdk_helper_eth_dev_count() {
875 uint8_t ports = rte_eth_dev_count();
878 "%s:%d: No DPDK ports available. Check bound devices to DPDK driver.\n",
879 __FUNCTION__, __LINE__);
883 if (ports > RTE_MAX_ETHPORTS) {
884 ERROR("%s:%d: Number of DPDK ports (%u) is greater than "
885 "RTE_MAX_ETHPORTS=%d. Ignoring extra ports\n",
886 __FUNCTION__, __LINE__, ports, RTE_MAX_ETHPORTS);
887 ports = RTE_MAX_ETHPORTS;