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