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