doc: whitespace changes in licenses
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <pthread.h>
41 #include <syslog.h>
42 #include <getopt.h>
43 #include <sys/file.h>
44 #include <stddef.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <errno.h>
48 #include <sys/mman.h>
49 #include <sys/queue.h>
50
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>
57 #include <rte_eal.h>
58 #include <rte_eal_memconfig.h>
59 #include <rte_per_lcore.h>
60 #include <rte_lcore.h>
61 #include <rte_log.h>
62 #include <rte_random.h>
63 #include <rte_cycles.h>
64 #include <rte_string_fns.h>
65 #include <rte_cpuflags.h>
66 #include <rte_interrupts.h>
67 #include <rte_pci.h>
68 #include <rte_common.h>
69 #include <rte_version.h>
70 #include <rte_atomic.h>
71 #include <malloc_heap.h>
72
73 #include "eal_private.h"
74 #include "eal_thread.h"
75 #include "eal_internal_cfg.h"
76 #include "eal_filesystem.h"
77 #include "eal_hugepages.h"
78
79 #define OPT_HUGE_DIR    "huge-dir"
80 #define OPT_PROC_TYPE   "proc-type"
81 #define OPT_NO_SHCONF   "no-shconf"
82 #define OPT_NO_HPET     "no-hpet"
83 #define OPT_VMWARE_TSC_MAP   "vmware-tsc-map"
84 #define OPT_NO_PCI      "no-pci"
85 #define OPT_NO_HUGE     "no-huge"
86 #define OPT_FILE_PREFIX "file-prefix"
87 #define OPT_SOCKET_MEM  "socket-mem"
88 #define OPT_SYSLOG      "syslog"
89
90 #define RTE_EAL_BLACKLIST_SIZE  0x100
91
92 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
93
94 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
95
96 #define GET_BLACKLIST_FIELD(in, fd, lim, dlm)                   \
97 {                                                               \
98         unsigned long val;                                      \
99         char *end;                                              \
100         errno = 0;                                              \
101         val = strtoul((in), &end, 16);                          \
102         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
103                 return (-EINVAL);                               \
104         (fd) = (typeof (fd))val;                                \
105         (in) = end + 1;                                         \
106 }
107
108 /* Allow the application to print its usage message too if set */
109 static rte_usage_hook_t rte_application_usage_hook = NULL;
110 /* early configuration structure, when memory config is not mmapped */
111 static struct rte_mem_config early_mem_config;
112
113 /* define fd variable here, because file needs to be kept open for the
114  * duration of the program, as we hold a write lock on it in the primary proc */
115 static int mem_cfg_fd = -1;
116
117 static struct flock wr_lock = {
118                 .l_type = F_WRLCK,
119                 .l_whence = SEEK_SET,
120                 .l_start = offsetof(struct rte_mem_config, memseg),
121                 .l_len = sizeof(early_mem_config.memseg),
122 };
123
124 /* Address of global and public configuration */
125 static struct rte_config rte_config = {
126                 .mem_config = &early_mem_config,
127 };
128
129 static struct rte_pci_addr eal_dev_blacklist[RTE_EAL_BLACKLIST_SIZE];
130
131 /* internal configuration (per-core) */
132 struct lcore_config lcore_config[RTE_MAX_LCORE];
133
134 /* internal configuration */
135 struct internal_config internal_config;
136
137 /* used by rte_rdtsc() */
138 int rte_cycles_vmware_tsc_map;
139
140 /* Return a pointer to the configuration structure */
141 struct rte_config *
142 rte_eal_get_configuration(void)
143 {
144         return &rte_config;
145 }
146
147 /* parse a sysfs (or other) file containing one integer value */
148 int
149 eal_parse_sysfs_value(const char *filename, unsigned long *val)
150 {
151         FILE *f;
152         char buf[BUFSIZ];
153         char *end = NULL;
154
155         if ((f = fopen(filename, "r")) == NULL) {
156                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
157                         __func__, filename);
158                 return -1;
159         }
160
161         if (fgets(buf, sizeof(buf), f) == NULL) {
162                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
163                         __func__, filename);
164                 fclose(f);
165                 return -1;
166         }
167         *val = strtoul(buf, &end, 0);
168         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
169                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
170                                 __func__, filename);
171                 fclose(f);
172                 return -1;
173         }
174         fclose(f);
175         return 0;
176 }
177
178
179 /* create memory configuration in shared/mmap memory. Take out
180  * a write lock on the memsegs, so we can auto-detect primary/secondary.
181  * This means we never close the file while running (auto-close on exit).
182  * We also don't lock the whole file, so that in future we can use read-locks
183  * on other parts, e.g. memzones, to detect if there are running secondary
184  * processes. */
185 static void
186 rte_eal_config_create(void)
187 {
188         void *rte_mem_cfg_addr;
189         int retval;
190
191         const char *pathname = eal_runtime_config_path();
192
193         if (internal_config.no_shconf)
194                 return;
195
196         if (mem_cfg_fd < 0){
197                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
198                 if (mem_cfg_fd < 0)
199                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
200         }
201
202         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
203         if (retval < 0){
204                 close(mem_cfg_fd);
205                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
206         }
207
208         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
209         if (retval < 0){
210                 close(mem_cfg_fd);
211                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
212                                 "process running?\n", pathname);
213         }
214
215         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
216                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
217
218         if (rte_mem_cfg_addr == MAP_FAILED){
219                 rte_panic("Cannot mmap memory for rte_config\n");
220         }
221         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
222         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
223 }
224
225 /* attach to an existing shared memory config */
226 static void
227 rte_eal_config_attach(void)
228 {
229         void *rte_mem_cfg_addr;
230         const char *pathname = eal_runtime_config_path();
231
232         if (internal_config.no_shconf)
233                 return;
234
235         if (mem_cfg_fd < 0){
236                 mem_cfg_fd = open(pathname, O_RDWR);
237                 if (mem_cfg_fd < 0)
238                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
239         }
240
241         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config), 
242                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
243         close(mem_cfg_fd);
244         if (rte_mem_cfg_addr == MAP_FAILED)
245                 rte_panic("Cannot mmap memory for rte_config\n");
246
247         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
248 }
249
250 /* Detect if we are a primary or a secondary process */
251 static enum rte_proc_type_t
252 eal_proc_type_detect(void)
253 {
254         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
255         const char *pathname = eal_runtime_config_path();
256
257         /* if we can open the file but not get a write-lock we are a secondary
258          * process. NOTE: if we get a file handle back, we keep that open
259          * and don't close it to prevent a race condition between multiple opens */
260         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
261                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
262                 ptype = RTE_PROC_SECONDARY;
263
264         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
265                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
266
267         return ptype;
268 }
269
270 /* Sets up rte_config structure with the pointer to shared memory config.*/
271 static void
272 rte_config_init(void)
273 {
274         /* set the magic in configuration structure */
275         rte_config.magic = RTE_MAGIC;
276         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
277                         eal_proc_type_detect() : /* for auto, detect the type */
278                         internal_config.process_type; /* otherwise use what's already set */
279
280         switch (rte_config.process_type){
281         case RTE_PROC_PRIMARY:
282                 rte_eal_config_create();
283                 break;
284         case RTE_PROC_SECONDARY:
285                 rte_eal_config_attach();
286                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
287                 break;
288         case RTE_PROC_AUTO:
289         case RTE_PROC_INVALID:
290                 rte_panic("Invalid process type\n");
291         }
292 }
293
294 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
295 static void
296 eal_hugedirs_unlock(void)
297 {
298         int i;
299
300         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
301         {
302                 /* skip uninitialized */
303                 if (internal_config.hugepage_info[i].lock_descriptor == 0)
304                         continue;
305                 /* unlock hugepage file */
306                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
307                 close(internal_config.hugepage_info[i].lock_descriptor);
308                 /* reset the field */
309                 internal_config.hugepage_info[i].lock_descriptor = 0;
310         }
311 }
312
313 /* display usage */
314 static void
315 eal_usage(const char *prgname)
316 {
317         printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
318                "[--proc-type primary|secondary|auto] \n\n"
319                "EAL options:\n"
320                "  -c COREMASK  : A hexadecimal bitmask of cores to run on\n"
321                "  -n NUM       : Number of memory channels\n"
322                    "  -v           : Display version information on startup\n"
323                "  -b <domain:bus:devid.func>: to prevent EAL from using specified "
324            "PCI device\n"
325                "                 (multiple -b options are allowed)\n"
326                "  -m MB        : memory to allocate (see also --"OPT_SOCKET_MEM")\n"
327                "  -r NUM       : force number of memory ranks (don't detect)\n"
328                "  --"OPT_SYSLOG"     : set syslog facility\n"
329                "  --"OPT_SOCKET_MEM" : memory to allocate on specific \n"
330                    "                 sockets (use comma separated values)\n"
331                "  --"OPT_HUGE_DIR"   : directory where hugetlbfs is mounted\n"
332                "  --"OPT_PROC_TYPE"  : type of this process\n"
333                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
334                "  --"OPT_VMWARE_TSC_MAP": use VMware TSC map instead of "
335                            "native RDTSC\n"
336                "\nEAL options for DEBUG use only:\n"
337                "  --"OPT_NO_HUGE"  : use malloc instead of hugetlbfs\n"
338                "  --"OPT_NO_PCI"   : disable pci\n"
339                "  --"OPT_NO_HPET"  : disable hpet\n"
340                "  --"OPT_NO_SHCONF": no shared config (mmap'd files)\n\n",
341                prgname);
342         /* Allow the application to print its usage message too if hook is set */
343         if ( rte_application_usage_hook ) {
344                 printf("===== Application Usage =====\n\n");
345                 rte_application_usage_hook(prgname);
346         }
347 }
348
349 /* Set a per-application usage message */
350 rte_usage_hook_t
351 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
352 {
353         rte_usage_hook_t        old_func;
354
355         /* Will be NULL on the first call to denote the last usage routine. */
356         old_func                                        = rte_application_usage_hook;
357         rte_application_usage_hook      = usage_func;
358
359         return old_func;
360 }
361
362 /*
363  * Parse the coremask given as argument (hexadecimal string) and fill
364  * the global configuration (core role and core count) with the parsed
365  * value.
366  */
367 static int
368 eal_parse_coremask(const char *coremask)
369 {
370         struct rte_config *cfg = rte_eal_get_configuration();
371         unsigned i;
372         char *end = NULL;
373         unsigned long long cm;
374         unsigned count = 0;
375
376         /* parse hexadecimal string */
377         cm = strtoull(coremask, &end, 16);
378         if ((coremask[0] == '\0') || (end == NULL) || (*end != '\0') || (cm == 0))
379                 return -1;
380
381         RTE_LOG(DEBUG, EAL, "coremask set to %llx\n", cm);
382         /* set core role and core count */
383         for (i = 0; i < RTE_MAX_LCORE; i++) {
384                 if ((1ULL << i) & cm) {
385                         if (count == 0)
386                                 cfg->master_lcore = i;
387                         cfg->lcore_role[i] = ROLE_RTE;
388                         count++;
389                 }
390                 else {
391                         cfg->lcore_role[i] = ROLE_OFF;
392                 }
393         }
394         return 0;
395 }
396
397 static int
398 eal_parse_syslog(const char *facility)
399 {
400         int i;
401         static struct {
402                 const char *name;
403                 int value;
404         } map[] = {
405                 { "auth", LOG_AUTH },
406                 { "cron", LOG_CRON },
407                 { "daemon", LOG_DAEMON },
408                 { "ftp", LOG_FTP },
409                 { "kern", LOG_KERN },
410                 { "lpr", LOG_LPR },
411                 { "mail", LOG_MAIL },
412                 { "news", LOG_NEWS },
413                 { "syslog", LOG_SYSLOG },
414                 { "user", LOG_USER },
415                 { "uucp", LOG_UUCP },
416                 { "local0", LOG_LOCAL0 },
417                 { "local1", LOG_LOCAL1 },
418                 { "local2", LOG_LOCAL2 },
419                 { "local3", LOG_LOCAL3 },
420                 { "local4", LOG_LOCAL4 },
421                 { "local5", LOG_LOCAL5 },
422                 { "local6", LOG_LOCAL6 },
423                 { "local7", LOG_LOCAL7 },
424                 { NULL, 0 }
425         };
426
427         for (i = 0; map[i].name; i++) {
428                 if (!strcmp(facility, map[i].name)) {
429                         internal_config.syslog_facility = map[i].value;
430                         return 0;
431                 }
432         }
433         return -1;
434 }
435
436 static int
437 eal_parse_socket_mem(char *socket_mem)
438 {
439         char * arg[RTE_MAX_NUMA_NODES];
440         char *end;
441         int arg_num, i, len;
442         uint64_t total_mem = 0;
443
444         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
445         if (len == SOCKET_MEM_STRLEN) {
446                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
447                 return -1;
448         }
449
450         /* all other error cases will be caught later */
451         if (!isdigit(socket_mem[len-1]))
452                 return -1;
453
454         /* split the optarg into separate socket values */
455         arg_num = rte_strsplit(socket_mem, len,
456                         arg, RTE_MAX_NUMA_NODES, ',');
457
458         /* if split failed, or 0 arguments */
459         if (arg_num <= 0)
460                 return -1;
461
462         internal_config.force_sockets = 1;
463
464         /* parse each defined socket option */
465         errno = 0;
466         for (i = 0; i < arg_num; i++) {
467                 end = NULL;
468                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
469
470                 /* check for invalid input */
471                 if ((errno != 0)  ||
472                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
473                         return -1;
474                 internal_config.socket_mem[i] *= 1024ULL;
475                 internal_config.socket_mem[i] *= 1024ULL;
476                 total_mem += internal_config.socket_mem[i];
477         }
478
479         /* check if we have a positive amount of total memory */
480         if (total_mem == 0)
481                 return -1;
482
483         return 0;
484 }
485
486 static inline size_t
487 eal_get_hugepage_mem_size(void)
488 {
489         uint64_t size = 0;
490         unsigned i, j;
491
492         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
493                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
494                 if (hpi->hugedir != NULL) {
495                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
496                                 size += hpi->hugepage_sz * hpi->num_pages[j];
497                         }
498                 }
499         }
500
501         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
502 }
503
504 static enum rte_proc_type_t
505 eal_parse_proc_type(const char *arg)
506 {
507         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
508                 return RTE_PROC_PRIMARY;
509         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
510                 return RTE_PROC_SECONDARY;
511         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
512                 return RTE_PROC_AUTO;
513
514         return RTE_PROC_INVALID;
515 }
516
517 static int
518 eal_parse_blacklist(const char *input,  struct rte_pci_addr *dev2bl)
519 {
520         GET_BLACKLIST_FIELD(input, dev2bl->domain, UINT16_MAX, ':');
521         GET_BLACKLIST_FIELD(input, dev2bl->bus, UINT8_MAX, ':');
522         GET_BLACKLIST_FIELD(input, dev2bl->devid, UINT8_MAX, '.');
523         GET_BLACKLIST_FIELD(input, dev2bl->function, UINT8_MAX, 0);
524         return (0);
525 }
526
527 static ssize_t
528 eal_parse_blacklist_opt(const char *optarg, size_t idx)
529 {
530         if (idx >= sizeof (eal_dev_blacklist) / sizeof (eal_dev_blacklist[0])) {
531                 RTE_LOG(ERR, EAL,
532                     "%s - too many devices to blacklist...\n",
533                     optarg);
534                 return (-EINVAL);
535         } else if (eal_parse_blacklist(optarg, eal_dev_blacklist + idx) != 0) {
536                 RTE_LOG(ERR, EAL,
537                     "%s - invalid device to blacklist...\n",
538                     optarg);
539                 return (-EINVAL);
540         }
541
542         idx += 1;
543         return (idx);
544 }
545
546
547 /* Parse the argument given in the command line of the application */
548 static int
549 eal_parse_args(int argc, char **argv)
550 {
551         int opt, ret, i;
552         char **argvopt;
553         int option_index;
554         int coremask_ok = 0;
555         ssize_t blacklist_index = 0;;
556         char *prgname = argv[0];
557         static struct option lgopts[] = {
558                 {OPT_NO_HUGE, 0, 0, 0},
559                 {OPT_NO_PCI, 0, 0, 0},
560                 {OPT_NO_HPET, 0, 0, 0},
561                 {OPT_VMWARE_TSC_MAP, 0, 0, 0},
562                 {OPT_HUGE_DIR, 1, 0, 0},
563                 {OPT_NO_SHCONF, 0, 0, 0},
564                 {OPT_PROC_TYPE, 1, 0, 0},
565                 {OPT_FILE_PREFIX, 1, 0, 0},
566                 {OPT_SOCKET_MEM, 1, 0, 0},
567                 {OPT_SYSLOG, 1, NULL, 0},
568                 {0, 0, 0, 0}
569         };
570
571         argvopt = argv;
572
573         internal_config.memory = 0;
574         internal_config.force_nrank = 0;
575         internal_config.force_nchannel = 0;
576         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
577         internal_config.hugepage_dir = NULL;
578         internal_config.force_sockets = 0;
579         internal_config.syslog_facility = LOG_DAEMON;
580 #ifdef RTE_LIBEAL_USE_HPET
581         internal_config.no_hpet = 0;
582 #else
583         internal_config.no_hpet = 1;
584 #endif
585         /* zero out the NUMA config */
586         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
587                 internal_config.socket_mem[i] = 0;
588
589         /* zero out hugedir descriptors */
590         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
591                 internal_config.hugepage_info[i].lock_descriptor = 0;
592
593         internal_config.vmware_tsc_map = 0;
594
595         while ((opt = getopt_long(argc, argvopt, "b:c:m:n:r:v",
596                                   lgopts, &option_index)) != EOF) {
597
598                 switch (opt) {
599                 /* blacklist */
600                 case 'b':
601                         if ((blacklist_index = eal_parse_blacklist_opt(optarg,
602                             blacklist_index)) < 0) {
603                                 eal_usage(prgname);
604                                 return (-1);
605                         }
606                         break;
607                 /* coremask */
608                 case 'c':
609                         if (eal_parse_coremask(optarg) < 0) {
610                                 RTE_LOG(ERR, EAL, "invalid coremask\n");
611                                 eal_usage(prgname);
612                                 return -1;
613                         }
614                         coremask_ok = 1;
615                         break;
616                 /* size of memory */
617                 case 'm':
618                         internal_config.memory = atoi(optarg);
619                         internal_config.memory *= 1024ULL;
620                         internal_config.memory *= 1024ULL;
621                         break;
622                 /* force number of channels */
623                 case 'n':
624                         internal_config.force_nchannel = atoi(optarg);
625                         if (internal_config.force_nchannel == 0 ||
626                             internal_config.force_nchannel > 4) {
627                                 RTE_LOG(ERR, EAL, "invalid channel number\n");
628                                 eal_usage(prgname);
629                                 return -1;
630                         }
631                         break;
632                 /* force number of ranks */
633                 case 'r':
634                         internal_config.force_nrank = atoi(optarg);
635                         if (internal_config.force_nrank == 0 ||
636                             internal_config.force_nrank > 16) {
637                                 RTE_LOG(ERR, EAL, "invalid rank number\n");
638                                 eal_usage(prgname);
639                                 return -1;
640                         }
641                         break;
642                 case 'v':
643                         /* since message is explicitly requested by user, we
644                          * write message at highest log level so it can always be seen
645                          * even if info or warning messages are disabled */
646                         RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
647                         break;
648
649                 /* long options */
650                 case 0:
651                         if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
652                                 internal_config.no_hugetlbfs = 1;
653                         }
654                         else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
655                                 internal_config.no_pci = 1;
656                         }
657                         else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
658                                 internal_config.no_hpet = 1;
659                         }
660                         else if (!strcmp(lgopts[option_index].name, OPT_VMWARE_TSC_MAP)) {
661                                 internal_config.vmware_tsc_map = 1;
662                         }
663                         else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
664                                 internal_config.no_shconf = 1;
665                         }
666                         else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
667                                 internal_config.hugepage_dir = optarg;
668                         }
669                         else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
670                                 internal_config.process_type = eal_parse_proc_type(optarg);
671                         }
672                         else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
673                                 internal_config.hugefile_prefix = optarg;
674                         }
675                         else if (!strcmp(lgopts[option_index].name, OPT_SOCKET_MEM)) {
676                                 if (eal_parse_socket_mem(optarg) < 0) {
677                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
678                                                         OPT_SOCKET_MEM "\n");
679                                         eal_usage(prgname);
680                                         return -1;
681                                 }
682                         }
683                         else if (!strcmp(lgopts[option_index].name, OPT_SYSLOG)) {
684                                 if (eal_parse_syslog(optarg) < 0) {
685                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
686                                                         OPT_SYSLOG "\n");
687                                         eal_usage(prgname);
688                                         return -1;
689                                 }
690                         }
691                         break;
692
693                 default:
694                         eal_usage(prgname);
695                         return -1;
696                 }
697         }
698
699         /* sanity checks */
700         if (!coremask_ok) {
701                 RTE_LOG(ERR, EAL, "coremask not specified\n");
702                 eal_usage(prgname);
703                 return -1;
704         }
705         if (internal_config.process_type == RTE_PROC_AUTO){
706                 internal_config.process_type = eal_proc_type_detect();
707         }
708         if (internal_config.process_type == RTE_PROC_INVALID){
709                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
710                 eal_usage(prgname);
711                 return -1;
712         }
713         if (internal_config.process_type == RTE_PROC_PRIMARY &&
714                         internal_config.force_nchannel == 0) {
715                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
716                 eal_usage(prgname);
717                 return -1;
718         }
719         if (index(internal_config.hugefile_prefix,'%') != NULL){
720                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
721                 eal_usage(prgname);
722                 return -1;
723         }
724         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
725                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
726                                 "at the same time\n");
727                 eal_usage(prgname);
728                 return -1;
729         }
730         /* --no-huge doesn't make sense with either -m or --socket-mem */
731         if (internal_config.no_hugetlbfs &&
732                         (internal_config.memory > 0 ||
733                                         internal_config.force_sockets == 1)) {
734                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
735                                 "together with --no-huge!\n");
736                 eal_usage(prgname);
737                 return -1;
738         }
739
740         if (blacklist_index > 0)
741                 rte_eal_pci_set_blacklist(eal_dev_blacklist, blacklist_index);
742
743         if (optind >= 0)
744                 argv[optind-1] = prgname;
745
746         /* if no memory amounts were requested, this will result in 0 and
747          * will be overriden later, right after eal_hugepage_info_init() */
748         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
749                 internal_config.memory += internal_config.socket_mem[i];
750
751         ret = optind-1;
752         optind = 0; /* reset getopt lib */
753         return ret;
754 }
755
756 static void
757 eal_check_mem_on_local_socket(void)
758 {
759         const struct rte_memseg *ms;
760         int i, socket_id;
761
762         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
763
764         ms = rte_eal_get_physmem_layout();
765
766         for (i = 0; i < RTE_MAX_MEMSEG; i++)
767                 if (ms[i].socket_id == socket_id &&
768                                 ms[i].len > 0)
769                         return;
770
771         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
772                         "memory on local socket!\n");
773 }
774
775 static int
776 sync_func(__attribute__((unused)) void *arg)
777 {
778         return 0;
779 }
780
781 inline static void 
782 rte_eal_mcfg_complete(void)
783 {
784         /* ALL shared mem_config related INIT DONE */
785         if (rte_config.process_type == RTE_PROC_PRIMARY)
786                 rte_config.mem_config->magic = RTE_MAGIC;
787 }
788
789 /* Launch threads, called at application init(). */
790 int
791 rte_eal_init(int argc, char **argv)
792 {
793         int i, fctret, ret;
794         pthread_t thread_id;
795         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
796
797         if (!rte_atomic32_test_and_set(&run_once))
798                 return -1;
799
800         thread_id = pthread_self();
801
802         if (rte_eal_log_early_init() < 0)
803                 rte_panic("Cannot init early logs\n");
804
805         fctret = eal_parse_args(argc, argv);
806         if (fctret < 0)
807                 exit(1);
808
809         if (internal_config.no_hugetlbfs == 0 &&
810                         internal_config.process_type != RTE_PROC_SECONDARY &&
811                         eal_hugepage_info_init() < 0)
812                 rte_panic("Cannot get hugepage information\n");
813
814         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
815                 if (internal_config.no_hugetlbfs)
816                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
817                 else
818                         internal_config.memory = eal_get_hugepage_mem_size();
819         }
820
821         if (internal_config.vmware_tsc_map == 1) {
822 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
823                 rte_cycles_vmware_tsc_map = 1;
824                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
825                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
826 #else
827                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
828                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
829 #endif
830         }
831
832         rte_srand(rte_rdtsc());
833
834         rte_config_init();
835         
836         if (rte_eal_cpu_init() < 0)
837                 rte_panic("Cannot detect lcores\n");
838
839         if (rte_eal_memory_init() < 0)
840                 rte_panic("Cannot init memory\n");
841
842         /* the directories are locked during eal_hugepage_info_init */
843         eal_hugedirs_unlock();
844         
845         if (rte_eal_memzone_init() < 0)
846                 rte_panic("Cannot init memzone\n");
847
848         if (rte_eal_tailqs_init() < 0)
849                 rte_panic("Cannot init tail queues for objects\n");
850
851         if (rte_eal_log_init(argv[0], internal_config.syslog_facility) < 0)
852                 rte_panic("Cannot init logs\n");
853
854         if (rte_eal_alarm_init() < 0)
855                 rte_panic("Cannot init interrupt-handling thread\n");
856
857         if (rte_eal_intr_init() < 0)
858                 rte_panic("Cannot init interrupt-handling thread\n");
859
860         if (rte_eal_timer_init() < 0)
861                 rte_panic("Cannot init HPET or TSC timers\n");
862
863         if (rte_eal_pci_init() < 0)
864                 rte_panic("Cannot init PCI\n");
865
866         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
867                 rte_config.master_lcore, (int)thread_id);
868
869         eal_check_mem_on_local_socket();
870
871         rte_eal_mcfg_complete();
872
873         RTE_LCORE_FOREACH_SLAVE(i) {
874
875                 /*
876                  * create communication pipes between master thread
877                  * and children
878                  */
879                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
880                         rte_panic("Cannot create pipe\n");
881                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
882                         rte_panic("Cannot create pipe\n");
883
884                 lcore_config[i].state = WAIT;
885
886                 /* create a thread for each lcore */
887                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
888                                      eal_thread_loop, NULL);
889                 if (ret != 0)
890                         rte_panic("Cannot create thread\n");
891         }
892
893         eal_thread_init_master(rte_config.master_lcore);
894
895         /*
896          * Launch a dummy function on all slave lcores, so that master lcore
897          * knows they are all ready when this function returns.
898          */
899         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
900         rte_eal_mp_wait_lcore();
901
902         return fctret;
903 }
904
905 /* get core role */
906 enum rte_lcore_role_t
907 rte_eal_lcore_role(unsigned lcore_id)
908 {
909         return (rte_config.lcore_role[lcore_id]);
910 }
911
912 enum rte_proc_type_t
913 rte_eal_process_type(void)
914 {
915         return (rte_config.process_type);
916 }
917