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