4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 #include <sys/queue.h>
51 #include <rte_common.h>
52 #include <rte_debug.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_launch.h>
56 #include <rte_tailq.h>
58 #include <rte_per_lcore.h>
59 #include <rte_lcore.h>
61 #include <rte_random.h>
62 #include <rte_cycles.h>
63 #include <rte_string_fns.h>
64 #include <rte_cpuflags.h>
65 #include <rte_interrupts.h>
67 #include <rte_common.h>
68 #include <rte_version.h>
70 #include "eal_private.h"
71 #include "eal_thread.h"
72 #include "eal_internal_cfg.h"
73 #include "eal_filesystem.h"
74 #include "eal_hugepages.h"
76 #define OPT_HUGE_DIR "huge-dir"
77 #define OPT_PROC_TYPE "proc-type"
78 #define OPT_NO_SHCONF "no-shconf"
79 #define OPT_NO_HPET "no-hpet"
80 #define OPT_NO_PCI "no-pci"
81 #define OPT_NO_HUGE "no-huge"
82 #define OPT_FILE_PREFIX "file-prefix"
84 #define RTE_EAL_BLACKLIST_SIZE 0x100
86 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
88 #define GET_BLACKLIST_FIELD(in, fd, lim, dlm) \
93 val = strtoul((in), &end, 16); \
94 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
96 (fd) = (typeof (fd))val; \
100 /* early configuration structure, when memory config is not mmapped */
101 static struct rte_mem_config early_mem_config;
103 /* define fd variable here, because file needs to be kept open for the
104 * duration of the program, as we hold a write lock on it in the primary proc */
105 static int mem_cfg_fd = -1;
107 static struct flock wr_lock = {
109 .l_whence = SEEK_SET,
110 .l_start = offsetof(struct rte_mem_config, memseg),
111 .l_len = sizeof(early_mem_config.memseg),
114 /* Address of global and public configuration */
115 static struct rte_config rte_config = {
116 .mem_config = &early_mem_config,
119 static struct rte_pci_addr eal_dev_blacklist[RTE_EAL_BLACKLIST_SIZE];
121 /* internal configuration (per-core) */
122 struct lcore_config lcore_config[RTE_MAX_LCORE];
124 /* internal configuration */
125 struct internal_config internal_config;
127 /* Return a pointer to the configuration structure */
129 rte_eal_get_configuration(void)
134 /* parse a sysfs (or other) file containing one integer value */
136 eal_parse_sysfs_value(const char *filename, unsigned long *val)
142 if ((f = fopen(filename, "r")) == NULL) {
143 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
148 if (fgets(buf, sizeof(buf), f) == NULL) {
149 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
154 *val = strtoul(buf, &end, 0);
155 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
156 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
166 /* create memory configuration in shared/mmap memory. Take out
167 * a write lock on the memsegs, so we can auto-detect primary/secondary.
168 * This means we never close the file while running (auto-close on exit).
169 * We also don't lock the whole file, so that in future we can use read-locks
170 * on other parts, e.g. memzones, to detect if there are running secondary
173 rte_eal_config_create(void)
175 void *rte_mem_cfg_addr;
178 const char *pathname = eal_runtime_config_path();
180 if (internal_config.no_shconf)
184 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
186 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
189 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
192 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
195 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
198 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
199 "process running?\n", pathname);
202 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
203 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
205 if (rte_mem_cfg_addr == MAP_FAILED){
206 rte_panic("Cannot mmap memory for rte_config\n");
208 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
209 memcpy(rte_config.mem_config, &early_mem_config,
210 sizeof(early_mem_config));
213 /* attach to an existing shared memory config */
215 rte_eal_config_attach(void)
217 void *rte_mem_cfg_addr;
218 const char *pathname = eal_runtime_config_path();
220 if (internal_config.no_shconf)
224 mem_cfg_fd = open(pathname, O_RDONLY);
226 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
229 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config), PROT_READ,
230 MAP_SHARED, mem_cfg_fd, 0);
232 if (rte_mem_cfg_addr == MAP_FAILED)
233 rte_panic("Cannot mmap memory for rte_config\n");
235 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
238 /* Detect if we are a primary or a secondary process */
239 static enum rte_proc_type_t
240 eal_proc_type_detect(void)
242 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
243 const char *pathname = eal_runtime_config_path();
245 /* if we can open the file but not get a write-lock we are a secondary
246 * process. NOTE: if we get a file handle back, we keep that open
247 * and don't close it to prevent a race condition between multiple opens */
248 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
249 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
250 ptype = RTE_PROC_SECONDARY;
252 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
253 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
258 /* Sets up rte_config structure with the pointer to shared memory config.*/
260 rte_config_init(void)
262 /* set the magic in configuration structure */
263 rte_config.magic = RTE_MAGIC;
264 rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
265 eal_proc_type_detect() : /* for auto, detect the type */
266 internal_config.process_type; /* otherwise use what's already set */
268 switch (rte_config.process_type){
269 case RTE_PROC_PRIMARY:
270 rte_eal_config_create();
272 case RTE_PROC_SECONDARY:
273 rte_eal_config_attach();
276 case RTE_PROC_INVALID:
277 rte_panic("Invalid process type\n");
283 eal_usage(const char *prgname)
285 printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
286 "[--proc-type primary|secondary|auto] \n\n"
288 " -c COREMASK: A hexadecimal bitmask of cores to run on\n"
289 " -n NUM : Number of memory channels\n"
290 " -v : Display version information on startup\n"
291 " -b <domain:bus:devid.func>: to prevent EAL from using specified PCI device\n"
292 " (multiple -b options are alowed)\n"
293 " -m MB : memory to allocate (default = size of hugemem)\n"
294 " -r NUM : force number of memory ranks (don't detect)\n"
295 " --"OPT_HUGE_DIR" : directory where hugetlbfs is mounted\n"
296 " --"OPT_PROC_TYPE": type of this process\n"
297 " --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
298 "\nEAL options for DEBUG use only:\n"
299 " --"OPT_NO_HUGE" : use malloc instead of hugetlbfs\n"
300 " --"OPT_NO_PCI" : disable pci\n"
301 " --"OPT_NO_HPET" : disable hpet\n"
302 " --"OPT_NO_SHCONF": no shared config (mmap'd files)\n\n",
307 * Parse the coremask given as argument (hexadecimal string) and fill
308 * the global configuration (core role and core count) with the parsed
312 eal_parse_coremask(const char *coremask)
314 struct rte_config *cfg = rte_eal_get_configuration();
317 unsigned long long cm;
320 /* parse hexadecimal string */
321 cm = strtoull(coremask, &end, 16);
322 if ((coremask[0] == '\0') || (end == NULL) || (*end != '\0') || (cm == 0))
325 RTE_LOG(DEBUG, EAL, "coremask set to %llx\n", cm);
326 /* set core role and core count */
327 for (i = 0; i < RTE_MAX_LCORE; i++) {
328 if ((1ULL << i) & cm) {
330 cfg->master_lcore = i;
331 cfg->lcore_role[i] = ROLE_RTE;
335 cfg->lcore_role[i] = ROLE_OFF;
341 static inline uint64_t
342 eal_get_hugepage_mem_size(void)
347 for (i = 0; i < internal_config.num_hugepage_sizes; i++){
348 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
349 if (hpi->hugedir != NULL)
350 size += hpi->hugepage_sz * hpi->num_pages;
356 static enum rte_proc_type_t
357 eal_parse_proc_type(const char *arg)
359 if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
360 return RTE_PROC_PRIMARY;
361 if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
362 return RTE_PROC_SECONDARY;
363 if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
364 return RTE_PROC_AUTO;
366 return RTE_PROC_INVALID;
370 eal_parse_blacklist(const char *input, struct rte_pci_addr *dev2bl)
372 GET_BLACKLIST_FIELD(input, dev2bl->domain, UINT16_MAX, ':');
373 GET_BLACKLIST_FIELD(input, dev2bl->bus, UINT8_MAX, ':');
374 GET_BLACKLIST_FIELD(input, dev2bl->devid, UINT8_MAX, '.');
375 GET_BLACKLIST_FIELD(input, dev2bl->function, UINT8_MAX, 0);
380 eal_parse_blacklist_opt(const char *optarg, size_t idx)
382 if (idx >= sizeof (eal_dev_blacklist) / sizeof (eal_dev_blacklist[0])) {
384 "%s - too many devices to blacklist...\n",
387 } else if (eal_parse_blacklist(optarg, eal_dev_blacklist + idx) != 0) {
389 "%s - invalid device to blacklist...\n",
399 /* Parse the argument given in the command line of the application */
401 eal_parse_args(int argc, char **argv)
407 ssize_t blacklist_index = 0;;
408 char *prgname = argv[0];
409 static struct option lgopts[] = {
410 {OPT_NO_HUGE, 0, 0, 0},
411 {OPT_NO_PCI, 0, 0, 0},
412 {OPT_NO_HPET, 0, 0, 0},
413 {OPT_HUGE_DIR, 1, 0, 0},
414 {OPT_NO_SHCONF, 0, 0, 0},
415 {OPT_PROC_TYPE, 1, 0, 0},
416 {OPT_FILE_PREFIX, 1, 0, 0},
422 internal_config.memory = 0;
423 internal_config.force_nrank = 0;
424 internal_config.force_nchannel = 0;
425 internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
426 internal_config.hugepage_dir = NULL;
427 #ifdef RTE_LIBEAL_USE_HPET
428 internal_config.no_hpet = 0;
430 internal_config.no_hpet = 1;
433 while ((opt = getopt_long(argc, argvopt, "b:c:m:n:r:v",
434 lgopts, &option_index)) != EOF) {
439 if ((blacklist_index = eal_parse_blacklist_opt(optarg,
440 blacklist_index)) < 0) {
447 if (eal_parse_coremask(optarg) < 0) {
448 RTE_LOG(ERR, EAL, "invalid coremask\n");
456 internal_config.memory = atoi(optarg);
457 internal_config.memory *= 1024ULL;
458 internal_config.memory *= 1024ULL;
460 /* force number of channels */
462 internal_config.force_nchannel = atoi(optarg);
463 if (internal_config.force_nchannel == 0 ||
464 internal_config.force_nchannel > 4) {
465 RTE_LOG(ERR, EAL, "invalid channel number\n");
470 /* force number of ranks */
472 internal_config.force_nrank = atoi(optarg);
473 if (internal_config.force_nrank == 0 ||
474 internal_config.force_nrank > 16) {
475 RTE_LOG(ERR, EAL, "invalid rank number\n");
481 /* since message is explicitly requested by user, we
482 * write message at highest log level so it can always be seen
483 * even if info or warning messages are disabled */
484 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
489 if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
490 internal_config.no_hugetlbfs = 1;
492 else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
493 internal_config.no_pci = 1;
495 else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
496 internal_config.no_hpet = 1;
498 else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
499 internal_config.no_shconf = 1;
501 else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
502 internal_config.hugepage_dir = optarg;
504 else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
505 internal_config.process_type = eal_parse_proc_type(optarg);
507 else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
508 internal_config.hugefile_prefix = optarg;
520 RTE_LOG(ERR, EAL, "coremask not specified\n");
524 if (internal_config.process_type == RTE_PROC_AUTO){
525 internal_config.process_type = eal_proc_type_detect();
527 if (internal_config.process_type == RTE_PROC_INVALID){
528 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
532 if (internal_config.process_type == RTE_PROC_PRIMARY &&
533 internal_config.force_nchannel == 0) {
534 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
538 if (index(internal_config.hugefile_prefix,'%') != NULL){
539 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
544 if (blacklist_index > 0)
545 rte_eal_pci_set_blacklist(eal_dev_blacklist, blacklist_index);
548 argv[optind-1] = prgname;
551 optind = 0; /* reset getopt lib */
555 /* Launch threads, called at application init(). */
557 rte_eal_init(int argc, char **argv)
562 thread_id = pthread_self();
564 if (rte_eal_log_early_init() < 0)
565 rte_panic("Cannot init early logs\n");
567 fctret = eal_parse_args(argc, argv);
571 if (eal_hugepage_info_init() < 0)
572 rte_panic("Cannot get hugepage information\n");
574 if (internal_config.memory == 0) {
575 if (internal_config.no_hugetlbfs)
576 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
578 internal_config.memory = eal_get_hugepage_mem_size();
581 rte_srand(rte_rdtsc());
584 if (rte_eal_cpu_init() < 0)
585 rte_panic("Cannot detect lcores\n");
587 if (rte_eal_memory_init() < 0)
588 rte_panic("Cannot init memory\n");
590 if (rte_eal_memzone_init() < 0)
591 rte_panic("Cannot init memzone\n");
593 if (rte_eal_tailqs_init() < 0)
594 rte_panic("Cannot init tail queues for objects\n");
596 if (rte_eal_log_init() < 0)
597 rte_panic("Cannot init logs\n");
599 if (rte_eal_alarm_init() < 0)
600 rte_panic("Cannot init interrupt-handling thread\n");
602 if (rte_eal_intr_init() < 0)
603 rte_panic("Cannot init interrupt-handling thread\n");
605 if (rte_eal_hpet_init() < 0)
606 rte_panic("Cannot init HPET\n");
608 if (rte_eal_pci_init() < 0)
609 rte_panic("Cannot init PCI\n");
611 RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
612 rte_config.master_lcore, (int)thread_id);
614 RTE_LCORE_FOREACH_SLAVE(i) {
617 * create communication pipes between master thread
620 if (pipe(lcore_config[i].pipe_master2slave) < 0)
621 rte_panic("Cannot create pipe\n");
622 if (pipe(lcore_config[i].pipe_slave2master) < 0)
623 rte_panic("Cannot create pipe\n");
625 lcore_config[i].state = WAIT;
627 /* create a thread for each lcore */
628 ret = pthread_create(&lcore_config[i].thread_id, NULL,
629 eal_thread_loop, NULL);
631 rte_panic("Cannot create thread\n");
634 eal_thread_init_master(rte_config.master_lcore);
640 enum rte_lcore_role_t
641 rte_eal_lcore_role(unsigned lcore_id)
643 return (rte_config.lcore_role[lcore_id]);
647 rte_eal_process_type(void)
649 return (rte_config.process_type);