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