eal: set iopl only when needed
[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  *   Copyright(c) 2012-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 <fcntl.h>
46 #include <dlfcn.h>
47 #include <stddef.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <errno.h>
51 #include <sys/mman.h>
52 #include <sys/queue.h>
53 #include <sys/io.h>
54
55 #include <rte_common.h>
56 #include <rte_debug.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_launch.h>
60 #include <rte_tailq.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_log.h>
66 #include <rte_random.h>
67 #include <rte_cycles.h>
68 #include <rte_string_fns.h>
69 #include <rte_cpuflags.h>
70 #include <rte_interrupts.h>
71 #include <rte_pci.h>
72 #include <rte_devargs.h>
73 #include <rte_common.h>
74 #include <rte_version.h>
75 #include <rte_atomic.h>
76 #include <malloc_heap.h>
77 #include <rte_eth_ring.h>
78
79 #include "eal_private.h"
80 #include "eal_thread.h"
81 #include "eal_internal_cfg.h"
82 #include "eal_filesystem.h"
83 #include "eal_hugepages.h"
84 #include "eal_options.h"
85
86 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
87
88 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
89
90 /* Allow the application to print its usage message too if set */
91 static rte_usage_hook_t rte_application_usage_hook = NULL;
92
93 TAILQ_HEAD(shared_driver_list, shared_driver);
94
95 /* Definition for shared object drivers. */
96 struct shared_driver {
97         TAILQ_ENTRY(shared_driver) next;
98
99         char    name[PATH_MAX];
100         void*   lib_handle;
101 };
102
103 /* List of external loadable drivers */
104 static struct shared_driver_list solib_list =
105 TAILQ_HEAD_INITIALIZER(solib_list);
106
107 /* early configuration structure, when memory config is not mmapped */
108 static struct rte_mem_config early_mem_config;
109
110 /* define fd variable here, because file needs to be kept open for the
111  * duration of the program, as we hold a write lock on it in the primary proc */
112 static int mem_cfg_fd = -1;
113
114 static struct flock wr_lock = {
115                 .l_type = F_WRLCK,
116                 .l_whence = SEEK_SET,
117                 .l_start = offsetof(struct rte_mem_config, memseg),
118                 .l_len = sizeof(early_mem_config.memseg),
119 };
120
121 /* Address of global and public configuration */
122 static struct rte_config rte_config = {
123                 .mem_config = &early_mem_config,
124 };
125
126 /* internal configuration (per-core) */
127 struct lcore_config lcore_config[RTE_MAX_LCORE];
128
129 /* internal configuration */
130 struct internal_config internal_config;
131
132 /* used by rte_rdtsc() */
133 int rte_cycles_vmware_tsc_map;
134
135 /* Return a pointer to the configuration structure */
136 struct rte_config *
137 rte_eal_get_configuration(void)
138 {
139         return &rte_config;
140 }
141
142 /* parse a sysfs (or other) file containing one integer value */
143 int
144 eal_parse_sysfs_value(const char *filename, unsigned long *val)
145 {
146         FILE *f;
147         char buf[BUFSIZ];
148         char *end = NULL;
149
150         if ((f = fopen(filename, "r")) == NULL) {
151                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
152                         __func__, filename);
153                 return -1;
154         }
155
156         if (fgets(buf, sizeof(buf), f) == NULL) {
157                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
158                         __func__, filename);
159                 fclose(f);
160                 return -1;
161         }
162         *val = strtoul(buf, &end, 0);
163         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
164                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
165                                 __func__, filename);
166                 fclose(f);
167                 return -1;
168         }
169         fclose(f);
170         return 0;
171 }
172
173
174 /* create memory configuration in shared/mmap memory. Take out
175  * a write lock on the memsegs, so we can auto-detect primary/secondary.
176  * This means we never close the file while running (auto-close on exit).
177  * We also don't lock the whole file, so that in future we can use read-locks
178  * on other parts, e.g. memzones, to detect if there are running secondary
179  * processes. */
180 static void
181 rte_eal_config_create(void)
182 {
183         void *rte_mem_cfg_addr;
184         int retval;
185
186         const char *pathname = eal_runtime_config_path();
187
188         if (internal_config.no_shconf)
189                 return;
190
191         /* map the config before hugepage address so that we don't waste a page */
192         if (internal_config.base_virtaddr != 0)
193                 rte_mem_cfg_addr = (void *)
194                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
195                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
196         else
197                 rte_mem_cfg_addr = NULL;
198
199         if (mem_cfg_fd < 0){
200                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
201                 if (mem_cfg_fd < 0)
202                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
203         }
204
205         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
206         if (retval < 0){
207                 close(mem_cfg_fd);
208                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
209         }
210
211         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
212         if (retval < 0){
213                 close(mem_cfg_fd);
214                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
215                                 "process running?\n", pathname);
216         }
217
218         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
219                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
220
221         if (rte_mem_cfg_addr == MAP_FAILED){
222                 rte_panic("Cannot mmap memory for rte_config\n");
223         }
224         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
225         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
226
227         /* store address of the config in the config itself so that secondary
228          * processes could later map the config into this exact location */
229         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
230
231 }
232
233 /* attach to an existing shared memory config */
234 static void
235 rte_eal_config_attach(void)
236 {
237         struct rte_mem_config *mem_config;
238
239         const char *pathname = eal_runtime_config_path();
240
241         if (internal_config.no_shconf)
242                 return;
243
244         if (mem_cfg_fd < 0){
245                 mem_cfg_fd = open(pathname, O_RDWR);
246                 if (mem_cfg_fd < 0)
247                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
248         }
249
250         /* map it as read-only first */
251         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
252                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
253         if (mem_config == MAP_FAILED)
254                 rte_panic("Cannot mmap memory for rte_config\n");
255
256         rte_config.mem_config = mem_config;
257 }
258
259 /* reattach the shared config at exact memory location primary process has it */
260 static void
261 rte_eal_config_reattach(void)
262 {
263         struct rte_mem_config *mem_config;
264         void *rte_mem_cfg_addr;
265
266         if (internal_config.no_shconf)
267                 return;
268
269         /* save the address primary process has mapped shared config to */
270         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
271
272         /* unmap original config */
273         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
274
275         /* remap the config at proper address */
276         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
277                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
278                         mem_cfg_fd, 0);
279         close(mem_cfg_fd);
280         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr)
281                 rte_panic("Cannot mmap memory for rte_config\n");
282
283         rte_config.mem_config = mem_config;
284 }
285
286 /* Detect if we are a primary or a secondary process */
287 static enum rte_proc_type_t
288 eal_proc_type_detect(void)
289 {
290         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
291         const char *pathname = eal_runtime_config_path();
292
293         /* if we can open the file but not get a write-lock we are a secondary
294          * process. NOTE: if we get a file handle back, we keep that open
295          * and don't close it to prevent a race condition between multiple opens */
296         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
297                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
298                 ptype = RTE_PROC_SECONDARY;
299
300         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
301                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
302
303         return ptype;
304 }
305
306 /* Sets up rte_config structure with the pointer to shared memory config.*/
307 static void
308 rte_config_init(void)
309 {
310         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
311                         eal_proc_type_detect() : /* for auto, detect the type */
312                         internal_config.process_type; /* otherwise use what's already set */
313
314         switch (rte_config.process_type){
315         case RTE_PROC_PRIMARY:
316                 rte_eal_config_create();
317                 break;
318         case RTE_PROC_SECONDARY:
319                 rte_eal_config_attach();
320                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
321                 rte_eal_config_reattach();
322                 break;
323         case RTE_PROC_AUTO:
324         case RTE_PROC_INVALID:
325                 rte_panic("Invalid process type\n");
326         }
327 }
328
329 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
330 static void
331 eal_hugedirs_unlock(void)
332 {
333         int i;
334
335         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
336         {
337                 /* skip uninitialized */
338                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
339                         continue;
340                 /* unlock hugepage file */
341                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
342                 close(internal_config.hugepage_info[i].lock_descriptor);
343                 /* reset the field */
344                 internal_config.hugepage_info[i].lock_descriptor = -1;
345         }
346 }
347
348 /* display usage */
349 static void
350 eal_usage(const char *prgname)
351 {
352         printf("\nUsage: %s ", prgname);
353         eal_common_usage();
354         printf("EAL Linux options:\n"
355                "  -d LIB.so    : add driver (can be used multiple times)\n"
356                "  --"OPT_XEN_DOM0" : support application running on Xen Domain0 "
357                            "without hugetlbfs\n"
358                "  --"OPT_SOCKET_MEM" : memory to allocate on specific\n"
359                    "                 sockets (use comma separated values)\n"
360                "  --"OPT_HUGE_DIR"   : directory where hugetlbfs is mounted\n"
361                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
362                "  --"OPT_BASE_VIRTADDR": specify base virtual address\n"
363                "  --"OPT_VFIO_INTR": specify desired interrupt mode for VFIO "
364                            "(legacy|msi|msix)\n"
365                "  --"OPT_CREATE_UIO_DEV": create /dev/uioX (usually done by hotplug)\n"
366                "\n");
367         /* Allow the application to print its usage message too if hook is set */
368         if ( rte_application_usage_hook ) {
369                 printf("===== Application Usage =====\n\n");
370                 rte_application_usage_hook(prgname);
371         }
372 }
373
374 /* Set a per-application usage message */
375 rte_usage_hook_t
376 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
377 {
378         rte_usage_hook_t        old_func;
379
380         /* Will be NULL on the first call to denote the last usage routine. */
381         old_func                                        = rte_application_usage_hook;
382         rte_application_usage_hook      = usage_func;
383
384         return old_func;
385 }
386
387 static int
388 eal_parse_socket_mem(char *socket_mem)
389 {
390         char * arg[RTE_MAX_NUMA_NODES];
391         char *end;
392         int arg_num, i, len;
393         uint64_t total_mem = 0;
394
395         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
396         if (len == SOCKET_MEM_STRLEN) {
397                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
398                 return -1;
399         }
400
401         /* all other error cases will be caught later */
402         if (!isdigit(socket_mem[len-1]))
403                 return -1;
404
405         /* split the optarg into separate socket values */
406         arg_num = rte_strsplit(socket_mem, len,
407                         arg, RTE_MAX_NUMA_NODES, ',');
408
409         /* if split failed, or 0 arguments */
410         if (arg_num <= 0)
411                 return -1;
412
413         internal_config.force_sockets = 1;
414
415         /* parse each defined socket option */
416         errno = 0;
417         for (i = 0; i < arg_num; i++) {
418                 end = NULL;
419                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
420
421                 /* check for invalid input */
422                 if ((errno != 0)  ||
423                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
424                         return -1;
425                 internal_config.socket_mem[i] *= 1024ULL;
426                 internal_config.socket_mem[i] *= 1024ULL;
427                 total_mem += internal_config.socket_mem[i];
428         }
429
430         /* check if we have a positive amount of total memory */
431         if (total_mem == 0)
432                 return -1;
433
434         return 0;
435 }
436
437 static int
438 eal_parse_base_virtaddr(const char *arg)
439 {
440         char *end;
441         uint64_t addr;
442
443         errno = 0;
444         addr = strtoull(arg, &end, 16);
445
446         /* check for errors */
447         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
448                 return -1;
449
450         /* make sure we don't exceed 32-bit boundary on 32-bit target */
451 #ifndef RTE_ARCH_X86_64
452         if (addr >= UINTPTR_MAX)
453                 return -1;
454 #endif
455
456         /* align the addr on 2M boundary */
457         internal_config.base_virtaddr = RTE_PTR_ALIGN_CEIL((uintptr_t)addr,
458                                                            RTE_PGSIZE_2M);
459
460         return 0;
461 }
462
463 static int
464 eal_parse_vfio_intr(const char *mode)
465 {
466         unsigned i;
467         static struct {
468                 const char *name;
469                 enum rte_intr_mode value;
470         } map[] = {
471                 { "legacy", RTE_INTR_MODE_LEGACY },
472                 { "msi", RTE_INTR_MODE_MSI },
473                 { "msix", RTE_INTR_MODE_MSIX },
474         };
475
476         for (i = 0; i < RTE_DIM(map); i++) {
477                 if (!strcmp(mode, map[i].name)) {
478                         internal_config.vfio_intr_mode = map[i].value;
479                         return 0;
480                 }
481         }
482         return -1;
483 }
484
485 static inline size_t
486 eal_get_hugepage_mem_size(void)
487 {
488         uint64_t size = 0;
489         unsigned i, j;
490
491         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
492                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
493                 if (hpi->hugedir != NULL) {
494                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
495                                 size += hpi->hugepage_sz * hpi->num_pages[j];
496                         }
497                 }
498         }
499
500         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
501 }
502
503 /* Parse the argument given in the command line of the application */
504 static int
505 eal_parse_args(int argc, char **argv)
506 {
507         int opt, ret, i;
508         char **argvopt;
509         int option_index;
510         int coremask_ok = 0;
511         char *prgname = argv[0];
512         struct shared_driver *solib;
513
514         argvopt = argv;
515
516         internal_config.memory = 0;
517         internal_config.force_nrank = 0;
518         internal_config.force_nchannel = 0;
519         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
520         internal_config.hugepage_dir = NULL;
521         internal_config.force_sockets = 0;
522         internal_config.syslog_facility = LOG_DAEMON;
523         /* default value from build option */
524         internal_config.log_level = RTE_LOG_LEVEL;
525         internal_config.xen_dom0_support = 0;
526         /* if set to NONE, interrupt mode is determined automatically */
527         internal_config.vfio_intr_mode = RTE_INTR_MODE_NONE;
528 #ifdef RTE_LIBEAL_USE_HPET
529         internal_config.no_hpet = 0;
530 #else
531         internal_config.no_hpet = 1;
532 #endif
533         /* zero out the NUMA config */
534         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
535                 internal_config.socket_mem[i] = 0;
536
537         /* zero out hugedir descriptors */
538         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
539                 internal_config.hugepage_info[i].lock_descriptor = -1;
540
541         internal_config.vmware_tsc_map = 0;
542         internal_config.base_virtaddr = 0;
543
544         while ((opt = getopt_long(argc, argvopt, eal_short_options,
545                                   eal_long_options, &option_index)) != EOF) {
546
547                 int ret;
548
549                 /* getopt is not happy, stop right now */
550                 if (opt == '?')
551                         return -1;
552
553                 ret = eal_parse_common_option(opt, optarg, &internal_config);
554                 /* common parser is not happy */
555                 if (ret < 0) {
556                         eal_usage(prgname);
557                         return -1;
558                 }
559                 /* common parser handled this option */
560                 if (ret == 0) {
561                         /* special case, note that the common parser accepted
562                          * the coremask option */
563                         if (opt == 'c')
564                                 coremask_ok = 1;
565                         continue;
566                 }
567
568                 switch (opt) {
569                 /* force loading of external driver */
570                 case 'd':
571                         solib = malloc(sizeof(*solib));
572                         if (solib == NULL) {
573                                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
574                                 return -1;
575                         }
576                         memset(solib, 0, sizeof(*solib));
577                         strncpy(solib->name, optarg, PATH_MAX-1);
578                         solib->name[PATH_MAX-1] = 0;
579                         TAILQ_INSERT_TAIL(&solib_list, solib, next);
580                         break;
581
582                 /* long options */
583                 case OPT_XEN_DOM0_NUM:
584 #ifdef RTE_LIBRTE_XEN_DOM0
585                         internal_config.xen_dom0_support = 1;
586 #else
587                         RTE_LOG(ERR, EAL, "Can't support DPDK app "
588                                 "running on Dom0, please configure"
589                                 " RTE_LIBRTE_XEN_DOM0=y\n");
590                         return -1;
591 #endif
592                         break;
593
594                 case OPT_HUGE_DIR_NUM:
595                         internal_config.hugepage_dir = optarg;
596                         break;
597
598                 case OPT_FILE_PREFIX_NUM:
599                         internal_config.hugefile_prefix = optarg;
600                         break;
601
602                 case OPT_SOCKET_MEM_NUM:
603                         if (eal_parse_socket_mem(optarg) < 0) {
604                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
605                                                 OPT_SOCKET_MEM "\n");
606                                 eal_usage(prgname);
607                                 return -1;
608                         }
609                         break;
610
611                 case OPT_BASE_VIRTADDR_NUM:
612                         if (eal_parse_base_virtaddr(optarg) < 0) {
613                                 RTE_LOG(ERR, EAL, "invalid parameter for --"
614                                                 OPT_BASE_VIRTADDR "\n");
615                                 eal_usage(prgname);
616                                 return -1;
617                         }
618                         break;
619
620                 case OPT_VFIO_INTR_NUM:
621                         if (eal_parse_vfio_intr(optarg) < 0) {
622                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
623                                                 OPT_VFIO_INTR "\n");
624                                 eal_usage(prgname);
625                                 return -1;
626                         }
627                         break;
628
629                 case OPT_CREATE_UIO_DEV_NUM:
630                         internal_config.create_uio_dev = 1;
631                         break;
632
633                 default:
634                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
635                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
636                                         "on Linux\n", opt);
637                         } else if (opt >= OPT_LONG_MIN_NUM &&
638                                    opt < OPT_LONG_MAX_NUM) {
639                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
640                                         "on Linux\n",
641                                         eal_long_options[option_index].name);
642                         } else {
643                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
644                                         "on Linux\n", opt);
645                         }
646                         eal_usage(prgname);
647                         return -1;
648                 }
649         }
650
651         /* sanity checks */
652         if (!coremask_ok) {
653                 RTE_LOG(ERR, EAL, "coremask not specified\n");
654                 eal_usage(prgname);
655                 return -1;
656         }
657         if (internal_config.process_type == RTE_PROC_AUTO){
658                 internal_config.process_type = eal_proc_type_detect();
659         }
660         if (internal_config.process_type == RTE_PROC_INVALID){
661                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
662                 eal_usage(prgname);
663                 return -1;
664         }
665         if (internal_config.process_type == RTE_PROC_PRIMARY &&
666                         internal_config.force_nchannel == 0) {
667                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
668                 eal_usage(prgname);
669                 return -1;
670         }
671         if (index(internal_config.hugefile_prefix,'%') != NULL){
672                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
673                 eal_usage(prgname);
674                 return -1;
675         }
676         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
677                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
678                                 "at the same time\n");
679                 eal_usage(prgname);
680                 return -1;
681         }
682         /* --no-huge doesn't make sense with either -m or --socket-mem */
683         if (internal_config.no_hugetlbfs &&
684                         (internal_config.memory > 0 ||
685                                         internal_config.force_sockets == 1)) {
686                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
687                                 "together with --no-huge!\n");
688                 eal_usage(prgname);
689                 return -1;
690         }
691         /* --xen-dom0 doesn't make sense with --socket-mem */
692         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
693                 RTE_LOG(ERR, EAL, "Options --socket-mem cannot be specified "
694                                         "together with --xen_dom0!\n");
695                 eal_usage(prgname);
696                 return -1;
697         }
698
699         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
700                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
701                 RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
702                         "[-w] options cannot be used at the same time\n");
703                 eal_usage(prgname);
704                 return -1;
705         }
706
707         if (optind >= 0)
708                 argv[optind-1] = prgname;
709
710         /* if no memory amounts were requested, this will result in 0 and
711          * will be overriden later, right after eal_hugepage_info_init() */
712         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
713                 internal_config.memory += internal_config.socket_mem[i];
714
715         ret = optind-1;
716         optind = 0; /* reset getopt lib */
717         return ret;
718 }
719
720 static void
721 eal_check_mem_on_local_socket(void)
722 {
723         const struct rte_memseg *ms;
724         int i, socket_id;
725
726         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
727
728         ms = rte_eal_get_physmem_layout();
729
730         for (i = 0; i < RTE_MAX_MEMSEG; i++)
731                 if (ms[i].socket_id == socket_id &&
732                                 ms[i].len > 0)
733                         return;
734
735         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
736                         "memory on local socket!\n");
737 }
738
739 static int
740 sync_func(__attribute__((unused)) void *arg)
741 {
742         return 0;
743 }
744
745 inline static void
746 rte_eal_mcfg_complete(void)
747 {
748         /* ALL shared mem_config related INIT DONE */
749         if (rte_config.process_type == RTE_PROC_PRIMARY)
750                 rte_config.mem_config->magic = RTE_MAGIC;
751 }
752
753 /*
754  * Request iopl privilege for all RPL, returns 0 on success
755  */
756 int
757 rte_eal_iopl_init(void)
758 {
759         if (iopl(3) != 0)
760                 return -1;
761         return 0;
762 }
763
764 /* Launch threads, called at application init(). */
765 int
766 rte_eal_init(int argc, char **argv)
767 {
768         int i, fctret, ret;
769         pthread_t thread_id;
770         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
771         struct shared_driver *solib = NULL;
772         const char *logid;
773
774         if (!rte_atomic32_test_and_set(&run_once))
775                 return -1;
776
777         logid = strrchr(argv[0], '/');
778         logid = strdup(logid ? logid + 1: argv[0]);
779
780         thread_id = pthread_self();
781
782         if (rte_eal_log_early_init() < 0)
783                 rte_panic("Cannot init early logs\n");
784
785         if (rte_eal_cpu_init() < 0)
786                 rte_panic("Cannot detect lcores\n");
787
788         fctret = eal_parse_args(argc, argv);
789         if (fctret < 0)
790                 exit(1);
791
792         /* set log level as early as possible */
793         rte_set_log_level(internal_config.log_level);
794
795         if (internal_config.no_hugetlbfs == 0 &&
796                         internal_config.process_type != RTE_PROC_SECONDARY &&
797                         internal_config.xen_dom0_support == 0 &&
798                         eal_hugepage_info_init() < 0)
799                 rte_panic("Cannot get hugepage information\n");
800
801         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
802                 if (internal_config.no_hugetlbfs)
803                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
804                 else
805                         internal_config.memory = eal_get_hugepage_mem_size();
806         }
807
808         if (internal_config.vmware_tsc_map == 1) {
809 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
810                 rte_cycles_vmware_tsc_map = 1;
811                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
812                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
813 #else
814                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
815                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
816 #endif
817         }
818
819         rte_srand(rte_rdtsc());
820
821         rte_config_init();
822
823         if (rte_eal_pci_init() < 0)
824                 rte_panic("Cannot init PCI\n");
825
826 #ifdef RTE_LIBRTE_IVSHMEM
827         if (rte_eal_ivshmem_init() < 0)
828                 rte_panic("Cannot init IVSHMEM\n");
829 #endif
830
831         if (rte_eal_memory_init() < 0)
832                 rte_panic("Cannot init memory\n");
833
834         /* the directories are locked during eal_hugepage_info_init */
835         eal_hugedirs_unlock();
836
837         if (rte_eal_memzone_init() < 0)
838                 rte_panic("Cannot init memzone\n");
839
840         if (rte_eal_tailqs_init() < 0)
841                 rte_panic("Cannot init tail queues for objects\n");
842
843 #ifdef RTE_LIBRTE_IVSHMEM
844         if (rte_eal_ivshmem_obj_init() < 0)
845                 rte_panic("Cannot init IVSHMEM objects\n");
846 #endif
847
848         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
849                 rte_panic("Cannot init logs\n");
850
851         if (rte_eal_alarm_init() < 0)
852                 rte_panic("Cannot init interrupt-handling thread\n");
853
854         if (rte_eal_intr_init() < 0)
855                 rte_panic("Cannot init interrupt-handling thread\n");
856
857         if (rte_eal_timer_init() < 0)
858                 rte_panic("Cannot init HPET or TSC timers\n");
859
860         eal_check_mem_on_local_socket();
861
862         rte_eal_mcfg_complete();
863
864         TAILQ_FOREACH(solib, &solib_list, next) {
865                 RTE_LOG(INFO, EAL, "open shared lib %s\n", solib->name);
866                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
867                 if (solib->lib_handle == NULL)
868                         RTE_LOG(WARNING, EAL, "%s\n", dlerror());
869         }
870
871         eal_thread_init_master(rte_config.master_lcore);
872
873         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
874                 rte_config.master_lcore, (int)thread_id);
875
876         if (rte_eal_dev_init() < 0)
877                 rte_panic("Cannot init pmd devices\n");
878
879         RTE_LCORE_FOREACH_SLAVE(i) {
880
881                 /*
882                  * create communication pipes between master thread
883                  * and children
884                  */
885                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
886                         rte_panic("Cannot create pipe\n");
887                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
888                         rte_panic("Cannot create pipe\n");
889
890                 lcore_config[i].state = WAIT;
891
892                 /* create a thread for each lcore */
893                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
894                                      eal_thread_loop, NULL);
895                 if (ret != 0)
896                         rte_panic("Cannot create thread\n");
897         }
898
899         /*
900          * Launch a dummy function on all slave lcores, so that master lcore
901          * knows they are all ready when this function returns.
902          */
903         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
904         rte_eal_mp_wait_lcore();
905
906         /* Probe & Initialize PCI devices */
907         if (rte_eal_pci_probe())
908                 rte_panic("Cannot probe PCI\n");
909
910         return fctret;
911 }
912
913 /* get core role */
914 enum rte_lcore_role_t
915 rte_eal_lcore_role(unsigned lcore_id)
916 {
917         return (rte_config.lcore_role[lcore_id]);
918 }
919
920 enum rte_proc_type_t
921 rte_eal_process_type(void)
922 {
923         return (rte_config.process_type);
924 }
925
926 int rte_eal_has_hugepages(void)
927 {
928         return ! internal_config.no_hugetlbfs;
929 }