4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 #include <sys/queue.h>
52 #include <rte_common.h>
53 #include <rte_debug.h>
54 #include <rte_memory.h>
55 #include <rte_memzone.h>
56 #include <rte_launch.h>
58 #include <rte_eal_memconfig.h>
59 #include <rte_errno.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
63 #include <rte_random.h>
64 #include <rte_cycles.h>
65 #include <rte_string_fns.h>
66 #include <rte_cpuflags.h>
67 #include <rte_interrupts.h>
71 #include <rte_devargs.h>
72 #include <rte_common.h>
73 #include <rte_version.h>
74 #include <rte_atomic.h>
75 #include <malloc_heap.h>
77 #include "eal_private.h"
78 #include "eal_thread.h"
79 #include "eal_internal_cfg.h"
80 #include "eal_filesystem.h"
81 #include "eal_hugepages.h"
82 #include "eal_options.h"
84 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
86 /* Allow the application to print its usage message too if set */
87 static rte_usage_hook_t rte_application_usage_hook = NULL;
88 /* early configuration structure, when memory config is not mmapped */
89 static struct rte_mem_config early_mem_config;
91 /* define fd variable here, because file needs to be kept open for the
92 * duration of the program, as we hold a write lock on it in the primary proc */
93 static int mem_cfg_fd = -1;
95 static struct flock wr_lock = {
98 .l_start = offsetof(struct rte_mem_config, memseg),
99 .l_len = sizeof(early_mem_config.memseg),
102 /* Address of global and public configuration */
103 static struct rte_config rte_config = {
104 .mem_config = &early_mem_config,
107 /* internal configuration (per-core) */
108 struct lcore_config lcore_config[RTE_MAX_LCORE];
110 /* internal configuration */
111 struct internal_config internal_config;
113 /* used by rte_rdtsc() */
114 int rte_cycles_vmware_tsc_map;
116 /* Return a pointer to the configuration structure */
118 rte_eal_get_configuration(void)
123 /* parse a sysfs (or other) file containing one integer value */
125 eal_parse_sysfs_value(const char *filename, unsigned long *val)
131 if ((f = fopen(filename, "r")) == NULL) {
132 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
137 if (fgets(buf, sizeof(buf), f) == NULL) {
138 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
143 *val = strtoul(buf, &end, 0);
144 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
145 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
155 /* create memory configuration in shared/mmap memory. Take out
156 * a write lock on the memsegs, so we can auto-detect primary/secondary.
157 * This means we never close the file while running (auto-close on exit).
158 * We also don't lock the whole file, so that in future we can use read-locks
159 * on other parts, e.g. memzones, to detect if there are running secondary
162 rte_eal_config_create(void)
164 void *rte_mem_cfg_addr;
167 const char *pathname = eal_runtime_config_path();
169 if (internal_config.no_shconf)
173 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
175 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
178 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
181 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
184 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
187 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
188 "process running?\n", pathname);
191 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
192 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
194 if (rte_mem_cfg_addr == MAP_FAILED){
195 rte_panic("Cannot mmap memory for rte_config\n");
197 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
198 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
201 /* attach to an existing shared memory config */
203 rte_eal_config_attach(void)
205 void *rte_mem_cfg_addr;
206 const char *pathname = eal_runtime_config_path();
208 if (internal_config.no_shconf)
212 mem_cfg_fd = open(pathname, O_RDWR);
214 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
217 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
218 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
220 if (rte_mem_cfg_addr == MAP_FAILED)
221 rte_panic("Cannot mmap memory for rte_config\n");
223 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
226 /* Detect if we are a primary or a secondary process */
228 eal_proc_type_detect(void)
230 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
231 const char *pathname = eal_runtime_config_path();
233 /* if we can open the file but not get a write-lock we are a secondary
234 * process. NOTE: if we get a file handle back, we keep that open
235 * and don't close it to prevent a race condition between multiple opens */
236 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
237 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
238 ptype = RTE_PROC_SECONDARY;
240 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
241 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
246 /* Sets up rte_config structure with the pointer to shared memory config.*/
248 rte_config_init(void)
250 rte_config.process_type = internal_config.process_type;
252 switch (rte_config.process_type){
253 case RTE_PROC_PRIMARY:
254 rte_eal_config_create();
256 case RTE_PROC_SECONDARY:
257 rte_eal_config_attach();
258 rte_eal_mcfg_wait_complete(rte_config.mem_config);
261 case RTE_PROC_INVALID:
262 rte_panic("Invalid process type\n");
268 eal_usage(const char *prgname)
270 printf("\nUsage: %s ", prgname);
272 /* Allow the application to print its usage message too if hook is set */
273 if ( rte_application_usage_hook ) {
274 printf("===== Application Usage =====\n\n");
275 rte_application_usage_hook(prgname);
279 /* Set a per-application usage message */
281 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
283 rte_usage_hook_t old_func;
285 /* Will be NULL on the first call to denote the last usage routine. */
286 old_func = rte_application_usage_hook;
287 rte_application_usage_hook = usage_func;
293 eal_get_hugepage_mem_size(void)
298 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
299 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
300 if (hpi->hugedir != NULL) {
301 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
302 size += hpi->hugepage_sz * hpi->num_pages[j];
307 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
310 /* Parse the arguments for --log-level only */
312 eal_log_level_parse(int argc, char **argv)
317 const int old_optind = optind;
318 const int old_optopt = optopt;
319 const int old_optreset = optreset;
320 char * const old_optarg = optarg;
326 eal_reset_internal_config(&internal_config);
328 while ((opt = getopt_long(argc, argvopt, eal_short_options,
329 eal_long_options, &option_index)) != EOF) {
333 /* getopt is not happy, stop right now */
337 ret = (opt == OPT_LOG_LEVEL_NUM) ?
338 eal_parse_common_option(opt, optarg, &internal_config) : 0;
340 /* common parser is not happy */
345 /* restore getopt lib */
348 optreset = old_optreset;
352 /* Parse the argument given in the command line of the application */
354 eal_parse_args(int argc, char **argv)
359 char *prgname = argv[0];
360 const int old_optind = optind;
361 const int old_optopt = optopt;
362 const int old_optreset = optreset;
363 char * const old_optarg = optarg;
369 while ((opt = getopt_long(argc, argvopt, eal_short_options,
370 eal_long_options, &option_index)) != EOF) {
372 /* getopt is not happy, stop right now */
379 ret = eal_parse_common_option(opt, optarg, &internal_config);
380 /* common parser is not happy */
386 /* common parser handled this option */
395 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
396 RTE_LOG(ERR, EAL, "Option %c is not supported "
397 "on FreeBSD\n", opt);
398 } else if (opt >= OPT_LONG_MIN_NUM &&
399 opt < OPT_LONG_MAX_NUM) {
400 RTE_LOG(ERR, EAL, "Option %s is not supported "
402 eal_long_options[option_index].name);
404 RTE_LOG(ERR, EAL, "Option %d is not supported "
405 "on FreeBSD\n", opt);
413 if (eal_adjust_config(&internal_config) != 0) {
419 if (eal_check_common_options(&internal_config) != 0) {
426 argv[optind-1] = prgname;
430 /* restore getopt lib */
433 optreset = old_optreset;
440 eal_check_mem_on_local_socket(void)
442 const struct rte_memseg *ms;
445 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
447 ms = rte_eal_get_physmem_layout();
449 for (i = 0; i < RTE_MAX_MEMSEG; i++)
450 if (ms[i].socket_id == socket_id &&
454 RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
455 "memory on local socket!\n");
459 sync_func(__attribute__((unused)) void *arg)
465 rte_eal_mcfg_complete(void)
467 /* ALL shared mem_config related INIT DONE */
468 if (rte_config.process_type == RTE_PROC_PRIMARY)
469 rte_config.mem_config->magic = RTE_MAGIC;
472 /* return non-zero if hugepages are enabled. */
473 int rte_eal_has_hugepages(void)
475 return !internal_config.no_hugetlbfs;
478 /* Abstraction for port I/0 privilege */
480 rte_eal_iopl_init(void)
484 fd = open("/dev/io", O_RDWR);
487 /* keep fd open for iopl */
491 static void rte_eal_init_alert(const char *msg)
493 fprintf(stderr, "EAL: FATAL: %s\n", msg);
494 RTE_LOG(ERR, EAL, "%s\n", msg);
497 /* Launch threads, called at application init(). */
499 rte_eal_init(int argc, char **argv)
503 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
504 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
505 char thread_name[RTE_MAX_THREAD_NAME_LEN];
507 /* checks if the machine is adequate */
508 if (!rte_cpu_is_supported()) {
509 rte_eal_init_alert("unsupported cpu type.");
514 if (!rte_atomic32_test_and_set(&run_once))
517 thread_id = pthread_self();
519 eal_log_level_parse(argc, argv);
521 /* set log level as early as possible */
522 rte_set_log_level(internal_config.log_level);
524 if (rte_eal_cpu_init() < 0) {
525 rte_eal_init_alert("Cannot detect lcores.");
530 fctret = eal_parse_args(argc, argv);
532 rte_eal_init_alert("Invalid 'command line' arguments.");
534 rte_atomic32_clear(&run_once);
538 if (internal_config.no_hugetlbfs == 0 &&
539 internal_config.process_type != RTE_PROC_SECONDARY &&
540 eal_hugepage_info_init() < 0) {
541 rte_eal_init_alert("Cannot get hugepage information.");
543 rte_atomic32_clear(&run_once);
547 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
548 if (internal_config.no_hugetlbfs)
549 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
551 internal_config.memory = eal_get_hugepage_mem_size();
554 if (internal_config.vmware_tsc_map == 1) {
555 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
556 rte_cycles_vmware_tsc_map = 1;
557 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
558 "you must have monitor_control.pseudo_perfctr = TRUE\n");
560 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
561 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
565 rte_srand(rte_rdtsc());
569 if (rte_eal_memory_init() < 0)
570 rte_panic("Cannot init memory\n");
572 if (rte_eal_memzone_init() < 0) {
573 rte_eal_init_alert("Cannot init memzone\n");
578 if (rte_eal_tailqs_init() < 0)
579 rte_panic("Cannot init tail queues for objects\n");
581 if (rte_eal_alarm_init() < 0)
582 rte_panic("Cannot init interrupt-handling thread\n");
584 if (rte_eal_intr_init() < 0)
585 rte_panic("Cannot init interrupt-handling thread\n");
587 if (rte_eal_timer_init() < 0)
588 rte_panic("Cannot init HPET or TSC timers\n");
590 if (rte_eal_pci_init() < 0)
591 rte_panic("Cannot init PCI\n");
593 eal_check_mem_on_local_socket();
595 if (eal_plugins_init() < 0)
596 rte_panic("Cannot init plugins\n");
598 eal_thread_init_master(rte_config.master_lcore);
600 ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
602 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
603 rte_config.master_lcore, thread_id, cpuset,
604 ret == 0 ? "" : "...");
607 rte_panic("Cannot scan the buses for devices\n");
609 RTE_LCORE_FOREACH_SLAVE(i) {
612 * create communication pipes between master thread
615 if (pipe(lcore_config[i].pipe_master2slave) < 0)
616 rte_panic("Cannot create pipe\n");
617 if (pipe(lcore_config[i].pipe_slave2master) < 0)
618 rte_panic("Cannot create pipe\n");
620 lcore_config[i].state = WAIT;
622 /* create a thread for each lcore */
623 ret = pthread_create(&lcore_config[i].thread_id, NULL,
624 eal_thread_loop, NULL);
626 rte_panic("Cannot create thread\n");
628 /* Set thread_name for aid in debugging. */
629 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
630 "lcore-slave-%d", i);
631 rte_thread_setname(lcore_config[i].thread_id, thread_name);
635 * Launch a dummy function on all slave lcores, so that master lcore
636 * knows they are all ready when this function returns.
638 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
639 rte_eal_mp_wait_lcore();
641 /* Probe all the buses and devices/drivers on them */
643 rte_panic("Cannot probe devices\n");
645 /* Probe & Initialize PCI devices */
646 if (rte_eal_pci_probe())
647 rte_panic("Cannot probe PCI\n");
649 if (rte_eal_dev_init() < 0)
650 rte_panic("Cannot init pmd devices\n");
652 rte_eal_mcfg_complete();
658 enum rte_lcore_role_t
659 rte_eal_lcore_role(unsigned lcore_id)
661 return rte_config.lcore_role[lcore_id];
665 rte_eal_process_type(void)
667 return rte_config.process_type;