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"
83 #define OPT_SOCKET_MEM "socket-mem"
85 #define RTE_EAL_BLACKLIST_SIZE 0x100
87 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
89 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
91 #define GET_BLACKLIST_FIELD(in, fd, lim, dlm) \
96 val = strtoul((in), &end, 16); \
97 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
99 (fd) = (typeof (fd))val; \
103 /* early configuration structure, when memory config is not mmapped */
104 static struct rte_mem_config early_mem_config;
106 /* define fd variable here, because file needs to be kept open for the
107 * duration of the program, as we hold a write lock on it in the primary proc */
108 static int mem_cfg_fd = -1;
110 static struct flock wr_lock = {
112 .l_whence = SEEK_SET,
113 .l_start = offsetof(struct rte_mem_config, memseg),
114 .l_len = sizeof(early_mem_config.memseg),
117 /* Address of global and public configuration */
118 static struct rte_config rte_config = {
119 .mem_config = &early_mem_config,
122 static struct rte_pci_addr eal_dev_blacklist[RTE_EAL_BLACKLIST_SIZE];
124 /* internal configuration (per-core) */
125 struct lcore_config lcore_config[RTE_MAX_LCORE];
127 /* internal configuration */
128 struct internal_config internal_config;
130 /* Return a pointer to the configuration structure */
132 rte_eal_get_configuration(void)
137 /* parse a sysfs (or other) file containing one integer value */
139 eal_parse_sysfs_value(const char *filename, unsigned long *val)
145 if ((f = fopen(filename, "r")) == NULL) {
146 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
151 if (fgets(buf, sizeof(buf), f) == NULL) {
152 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
157 *val = strtoul(buf, &end, 0);
158 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
159 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
169 /* create memory configuration in shared/mmap memory. Take out
170 * a write lock on the memsegs, so we can auto-detect primary/secondary.
171 * This means we never close the file while running (auto-close on exit).
172 * We also don't lock the whole file, so that in future we can use read-locks
173 * on other parts, e.g. memzones, to detect if there are running secondary
176 rte_eal_config_create(void)
178 void *rte_mem_cfg_addr;
181 const char *pathname = eal_runtime_config_path();
183 if (internal_config.no_shconf)
187 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
189 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
192 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
195 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
198 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
201 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
202 "process running?\n", pathname);
205 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
206 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
208 if (rte_mem_cfg_addr == MAP_FAILED){
209 rte_panic("Cannot mmap memory for rte_config\n");
211 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
212 memcpy(rte_config.mem_config, &early_mem_config,
213 sizeof(early_mem_config));
216 /* attach to an existing shared memory config */
218 rte_eal_config_attach(void)
220 void *rte_mem_cfg_addr;
221 const char *pathname = eal_runtime_config_path();
223 if (internal_config.no_shconf)
227 mem_cfg_fd = open(pathname, O_RDONLY);
229 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
232 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config), PROT_READ,
233 MAP_SHARED, mem_cfg_fd, 0);
235 if (rte_mem_cfg_addr == MAP_FAILED)
236 rte_panic("Cannot mmap memory for rte_config\n");
238 rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
241 /* Detect if we are a primary or a secondary process */
242 static enum rte_proc_type_t
243 eal_proc_type_detect(void)
245 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
246 const char *pathname = eal_runtime_config_path();
248 /* if we can open the file but not get a write-lock we are a secondary
249 * process. NOTE: if we get a file handle back, we keep that open
250 * and don't close it to prevent a race condition between multiple opens */
251 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
252 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
253 ptype = RTE_PROC_SECONDARY;
255 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
256 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
261 /* Sets up rte_config structure with the pointer to shared memory config.*/
263 rte_config_init(void)
265 /* set the magic in configuration structure */
266 rte_config.magic = RTE_MAGIC;
267 rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
268 eal_proc_type_detect() : /* for auto, detect the type */
269 internal_config.process_type; /* otherwise use what's already set */
271 switch (rte_config.process_type){
272 case RTE_PROC_PRIMARY:
273 rte_eal_config_create();
275 case RTE_PROC_SECONDARY:
276 rte_eal_config_attach();
279 case RTE_PROC_INVALID:
280 rte_panic("Invalid process type\n");
286 eal_usage(const char *prgname)
288 printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
289 "[--proc-type primary|secondary|auto] \n\n"
291 " -c COREMASK : A hexadecimal bitmask of cores to run on\n"
292 " -n NUM : Number of memory channels\n"
293 " -v : Display version information on startup\n"
294 " -b <domain:bus:devid.func>: to prevent EAL from using specified "
296 " (multiple -b options are allowed)\n"
297 " -m MB : memory to allocate (see also --"OPT_SOCKET_MEM")\n"
298 " -r NUM : force number of memory ranks (don't detect)\n"
299 " --"OPT_SOCKET_MEM" : memory to allocate on specific \n"
300 " sockets (use comma separated values)\n"
301 " --"OPT_HUGE_DIR" : directory where hugetlbfs is mounted\n"
302 " --"OPT_PROC_TYPE" : type of this process\n"
303 " --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
304 "\nEAL options for DEBUG use only:\n"
305 " --"OPT_NO_HUGE" : use malloc instead of hugetlbfs\n"
306 " --"OPT_NO_PCI" : disable pci\n"
307 " --"OPT_NO_HPET" : disable hpet\n"
308 " --"OPT_NO_SHCONF": no shared config (mmap'd files)\n\n",
313 * Parse the coremask given as argument (hexadecimal string) and fill
314 * the global configuration (core role and core count) with the parsed
318 eal_parse_coremask(const char *coremask)
320 struct rte_config *cfg = rte_eal_get_configuration();
323 unsigned long long cm;
326 /* parse hexadecimal string */
327 cm = strtoull(coremask, &end, 16);
328 if ((coremask[0] == '\0') || (end == NULL) || (*end != '\0') || (cm == 0))
331 RTE_LOG(DEBUG, EAL, "coremask set to %llx\n", cm);
332 /* set core role and core count */
333 for (i = 0; i < RTE_MAX_LCORE; i++) {
334 if ((1ULL << i) & cm) {
336 cfg->master_lcore = i;
337 cfg->lcore_role[i] = ROLE_RTE;
341 cfg->lcore_role[i] = ROLE_OFF;
348 eal_parse_socket_mem(char *socket_mem)
350 char * arg[RTE_MAX_NUMA_NODES];
353 uint64_t total_mem = 0;
355 len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
356 if (len == SOCKET_MEM_STRLEN) {
357 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
361 /* all other error cases will be caught later */
362 if (!isdigit(socket_mem[len-1]))
365 /* split the optarg into separate socket values */
366 arg_num = rte_strsplit(socket_mem, len,
367 arg, RTE_MAX_NUMA_NODES, ',');
369 /* if split failed, or 0 arguments */
373 internal_config.force_sockets = 1;
375 /* parse each defined socket option */
377 for (i = 0; i < arg_num; i++) {
379 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
381 /* check for invalid input */
383 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
385 internal_config.socket_mem[i] *= 1024ULL;
386 internal_config.socket_mem[i] *= 1024ULL;
387 total_mem += internal_config.socket_mem[i];
390 /* check if we have a positive amount of total memory */
397 static inline uint64_t
398 eal_get_hugepage_mem_size(void)
403 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
404 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
405 if (hpi->hugedir != NULL) {
406 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
407 size += hpi->hugepage_sz * hpi->num_pages[j];
415 static enum rte_proc_type_t
416 eal_parse_proc_type(const char *arg)
418 if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
419 return RTE_PROC_PRIMARY;
420 if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
421 return RTE_PROC_SECONDARY;
422 if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
423 return RTE_PROC_AUTO;
425 return RTE_PROC_INVALID;
429 eal_parse_blacklist(const char *input, struct rte_pci_addr *dev2bl)
431 GET_BLACKLIST_FIELD(input, dev2bl->domain, UINT16_MAX, ':');
432 GET_BLACKLIST_FIELD(input, dev2bl->bus, UINT8_MAX, ':');
433 GET_BLACKLIST_FIELD(input, dev2bl->devid, UINT8_MAX, '.');
434 GET_BLACKLIST_FIELD(input, dev2bl->function, UINT8_MAX, 0);
439 eal_parse_blacklist_opt(const char *optarg, size_t idx)
441 if (idx >= sizeof (eal_dev_blacklist) / sizeof (eal_dev_blacklist[0])) {
443 "%s - too many devices to blacklist...\n",
446 } else if (eal_parse_blacklist(optarg, eal_dev_blacklist + idx) != 0) {
448 "%s - invalid device to blacklist...\n",
458 /* Parse the argument given in the command line of the application */
460 eal_parse_args(int argc, char **argv)
466 ssize_t blacklist_index = 0;;
467 char *prgname = argv[0];
468 static struct option lgopts[] = {
469 {OPT_NO_HUGE, 0, 0, 0},
470 {OPT_NO_PCI, 0, 0, 0},
471 {OPT_NO_HPET, 0, 0, 0},
472 {OPT_HUGE_DIR, 1, 0, 0},
473 {OPT_NO_SHCONF, 0, 0, 0},
474 {OPT_PROC_TYPE, 1, 0, 0},
475 {OPT_FILE_PREFIX, 1, 0, 0},
476 {OPT_SOCKET_MEM, 1, 0, 0},
482 internal_config.memory = 0;
483 internal_config.force_nrank = 0;
484 internal_config.force_nchannel = 0;
485 internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
486 internal_config.hugepage_dir = NULL;
487 internal_config.force_sockets = 0;
488 #ifdef RTE_LIBEAL_USE_HPET
489 internal_config.no_hpet = 0;
491 internal_config.no_hpet = 1;
493 /* zero out the NUMA config */
494 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
495 internal_config.socket_mem[i] = 0;
497 while ((opt = getopt_long(argc, argvopt, "b:c:m:n:r:v",
498 lgopts, &option_index)) != EOF) {
503 if ((blacklist_index = eal_parse_blacklist_opt(optarg,
504 blacklist_index)) < 0) {
511 if (eal_parse_coremask(optarg) < 0) {
512 RTE_LOG(ERR, EAL, "invalid coremask\n");
520 internal_config.memory = atoi(optarg);
521 internal_config.memory *= 1024ULL;
522 internal_config.memory *= 1024ULL;
524 /* force number of channels */
526 internal_config.force_nchannel = atoi(optarg);
527 if (internal_config.force_nchannel == 0 ||
528 internal_config.force_nchannel > 4) {
529 RTE_LOG(ERR, EAL, "invalid channel number\n");
534 /* force number of ranks */
536 internal_config.force_nrank = atoi(optarg);
537 if (internal_config.force_nrank == 0 ||
538 internal_config.force_nrank > 16) {
539 RTE_LOG(ERR, EAL, "invalid rank number\n");
545 /* since message is explicitly requested by user, we
546 * write message at highest log level so it can always be seen
547 * even if info or warning messages are disabled */
548 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
553 if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
554 internal_config.no_hugetlbfs = 1;
556 else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
557 internal_config.no_pci = 1;
559 else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
560 internal_config.no_hpet = 1;
562 else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
563 internal_config.no_shconf = 1;
565 else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
566 internal_config.hugepage_dir = optarg;
568 else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
569 internal_config.process_type = eal_parse_proc_type(optarg);
571 else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
572 internal_config.hugefile_prefix = optarg;
574 else if (!strcmp(lgopts[option_index].name, OPT_SOCKET_MEM)) {
575 if (eal_parse_socket_mem(optarg) < 0) {
576 RTE_LOG(ERR, EAL, "invalid parameters for --"
577 OPT_SOCKET_MEM "\n");
592 RTE_LOG(ERR, EAL, "coremask not specified\n");
596 if (internal_config.process_type == RTE_PROC_AUTO){
597 internal_config.process_type = eal_proc_type_detect();
599 if (internal_config.process_type == RTE_PROC_INVALID){
600 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
604 if (internal_config.process_type == RTE_PROC_PRIMARY &&
605 internal_config.force_nchannel == 0) {
606 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
610 if (index(internal_config.hugefile_prefix,'%') != NULL){
611 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
615 if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
616 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
617 "at the same time\n");
621 /* --no-huge doesn't make sense with either -m or --socket-mem */
622 if (internal_config.no_hugetlbfs &&
623 (internal_config.memory > 0 ||
624 internal_config.force_sockets == 1)) {
625 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
626 "together with --no-huge!\n");
631 if (blacklist_index > 0)
632 rte_eal_pci_set_blacklist(eal_dev_blacklist, blacklist_index);
635 argv[optind-1] = prgname;
637 /* if no memory amounts were requested, this will result in 0 and
638 * will be overriden later, right after eal_hugepage_info_init() */
639 for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
640 internal_config.memory += internal_config.socket_mem[i];
643 optind = 0; /* reset getopt lib */
648 eal_check_mem_on_local_socket(void)
650 const struct rte_memseg *ms;
653 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
655 ms = rte_eal_get_physmem_layout();
657 for (i = 0; i < RTE_MAX_MEMSEG; i++)
658 if (ms[i].socket_id == socket_id &&
662 RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
663 "memory on local socket!\n");
666 /* Launch threads, called at application init(). */
668 rte_eal_init(int argc, char **argv)
673 thread_id = pthread_self();
675 if (rte_eal_log_early_init() < 0)
676 rte_panic("Cannot init early logs\n");
678 fctret = eal_parse_args(argc, argv);
682 if (eal_hugepage_info_init() < 0)
683 rte_panic("Cannot get hugepage information\n");
685 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
686 if (internal_config.no_hugetlbfs)
687 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
689 internal_config.memory = eal_get_hugepage_mem_size();
692 rte_srand(rte_rdtsc());
695 if (rte_eal_cpu_init() < 0)
696 rte_panic("Cannot detect lcores\n");
698 if (rte_eal_memory_init() < 0)
699 rte_panic("Cannot init memory\n");
701 if (rte_eal_memzone_init() < 0)
702 rte_panic("Cannot init memzone\n");
704 if (rte_eal_tailqs_init() < 0)
705 rte_panic("Cannot init tail queues for objects\n");
707 if (rte_eal_log_init() < 0)
708 rte_panic("Cannot init logs\n");
710 if (rte_eal_alarm_init() < 0)
711 rte_panic("Cannot init interrupt-handling thread\n");
713 if (rte_eal_intr_init() < 0)
714 rte_panic("Cannot init interrupt-handling thread\n");
716 if (rte_eal_hpet_init() < 0)
717 rte_panic("Cannot init HPET\n");
719 if (rte_eal_pci_init() < 0)
720 rte_panic("Cannot init PCI\n");
722 RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
723 rte_config.master_lcore, (int)thread_id);
725 eal_check_mem_on_local_socket();
727 RTE_LCORE_FOREACH_SLAVE(i) {
730 * create communication pipes between master thread
733 if (pipe(lcore_config[i].pipe_master2slave) < 0)
734 rte_panic("Cannot create pipe\n");
735 if (pipe(lcore_config[i].pipe_slave2master) < 0)
736 rte_panic("Cannot create pipe\n");
738 lcore_config[i].state = WAIT;
740 /* create a thread for each lcore */
741 ret = pthread_create(&lcore_config[i].thread_id, NULL,
742 eal_thread_loop, NULL);
744 rte_panic("Cannot create thread\n");
747 eal_thread_init_master(rte_config.master_lcore);
753 enum rte_lcore_role_t
754 rte_eal_lcore_role(unsigned lcore_id)
756 return (rte_config.lcore_role[lcore_id]);
760 rte_eal_process_type(void)
762 return (rte_config.process_type);