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