d03d8e3fa19e1af93f502b0ea6db4296912fa4ec
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 <stddef.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <sys/mman.h>
50 #include <sys/queue.h>
51 #include <sys/stat.h>
52 #if defined(RTE_ARCH_X86)
53 #include <sys/io.h>
54 #endif
55
56 #include <rte_common.h>
57 #include <rte_debug.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_launch.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_errno.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_log.h>
67 #include <rte_random.h>
68 #include <rte_cycles.h>
69 #include <rte_string_fns.h>
70 #include <rte_cpuflags.h>
71 #include <rte_interrupts.h>
72 #include <rte_bus.h>
73 #include <rte_pci.h>
74 #include <rte_dev.h>
75 #include <rte_devargs.h>
76 #include <rte_version.h>
77 #include <rte_atomic.h>
78 #include <malloc_heap.h>
79
80 #include "eal_private.h"
81 #include "eal_thread.h"
82 #include "eal_internal_cfg.h"
83 #include "eal_filesystem.h"
84 #include "eal_hugepages.h"
85 #include "eal_options.h"
86 #include "eal_vfio.h"
87
88 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
89
90 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
91
92 /* Allow the application to print its usage message too if set */
93 static rte_usage_hook_t rte_application_usage_hook = NULL;
94
95 /* early configuration structure, when memory config is not mmapped */
96 static struct rte_mem_config early_mem_config;
97
98 /* define fd variable here, because file needs to be kept open for the
99  * duration of the program, as we hold a write lock on it in the primary proc */
100 static int mem_cfg_fd = -1;
101
102 static struct flock wr_lock = {
103                 .l_type = F_WRLCK,
104                 .l_whence = SEEK_SET,
105                 .l_start = offsetof(struct rte_mem_config, memseg),
106                 .l_len = sizeof(early_mem_config.memseg),
107 };
108
109 /* Address of global and public configuration */
110 static struct rte_config rte_config = {
111                 .mem_config = &early_mem_config,
112 };
113
114 /* internal configuration (per-core) */
115 struct lcore_config lcore_config[RTE_MAX_LCORE];
116
117 /* internal configuration */
118 struct internal_config internal_config;
119
120 /* used by rte_rdtsc() */
121 int rte_cycles_vmware_tsc_map;
122
123 /* Return a pointer to the configuration structure */
124 struct rte_config *
125 rte_eal_get_configuration(void)
126 {
127         return &rte_config;
128 }
129
130 /* parse a sysfs (or other) file containing one integer value */
131 int
132 eal_parse_sysfs_value(const char *filename, unsigned long *val)
133 {
134         FILE *f;
135         char buf[BUFSIZ];
136         char *end = NULL;
137
138         if ((f = fopen(filename, "r")) == NULL) {
139                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
140                         __func__, filename);
141                 return -1;
142         }
143
144         if (fgets(buf, sizeof(buf), f) == NULL) {
145                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
146                         __func__, filename);
147                 fclose(f);
148                 return -1;
149         }
150         *val = strtoul(buf, &end, 0);
151         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
152                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
153                                 __func__, filename);
154                 fclose(f);
155                 return -1;
156         }
157         fclose(f);
158         return 0;
159 }
160
161
162 /* create memory configuration in shared/mmap memory. Take out
163  * a write lock on the memsegs, so we can auto-detect primary/secondary.
164  * This means we never close the file while running (auto-close on exit).
165  * We also don't lock the whole file, so that in future we can use read-locks
166  * on other parts, e.g. memzones, to detect if there are running secondary
167  * processes. */
168 static void
169 rte_eal_config_create(void)
170 {
171         void *rte_mem_cfg_addr;
172         int retval;
173
174         const char *pathname = eal_runtime_config_path();
175
176         if (internal_config.no_shconf)
177                 return;
178
179         /* map the config before hugepage address so that we don't waste a page */
180         if (internal_config.base_virtaddr != 0)
181                 rte_mem_cfg_addr = (void *)
182                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
183                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
184         else
185                 rte_mem_cfg_addr = NULL;
186
187         if (mem_cfg_fd < 0){
188                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
189                 if (mem_cfg_fd < 0)
190                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
191         }
192
193         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
194         if (retval < 0){
195                 close(mem_cfg_fd);
196                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
197         }
198
199         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
200         if (retval < 0){
201                 close(mem_cfg_fd);
202                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
203                                 "process running?\n", pathname);
204         }
205
206         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
207                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
208
209         if (rte_mem_cfg_addr == MAP_FAILED){
210                 rte_panic("Cannot mmap memory for rte_config\n");
211         }
212         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
213         rte_config.mem_config = rte_mem_cfg_addr;
214
215         /* store address of the config in the config itself so that secondary
216          * processes could later map the config into this exact location */
217         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
218
219 }
220
221 /* attach to an existing shared memory config */
222 static void
223 rte_eal_config_attach(void)
224 {
225         struct rte_mem_config *mem_config;
226
227         const char *pathname = eal_runtime_config_path();
228
229         if (internal_config.no_shconf)
230                 return;
231
232         if (mem_cfg_fd < 0){
233                 mem_cfg_fd = open(pathname, O_RDWR);
234                 if (mem_cfg_fd < 0)
235                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
236         }
237
238         /* map it as read-only first */
239         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
240                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
241         if (mem_config == MAP_FAILED)
242                 rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
243                           errno, strerror(errno));
244
245         rte_config.mem_config = mem_config;
246 }
247
248 /* reattach the shared config at exact memory location primary process has it */
249 static void
250 rte_eal_config_reattach(void)
251 {
252         struct rte_mem_config *mem_config;
253         void *rte_mem_cfg_addr;
254
255         if (internal_config.no_shconf)
256                 return;
257
258         /* save the address primary process has mapped shared config to */
259         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
260
261         /* unmap original config */
262         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
263
264         /* remap the config at proper address */
265         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
266                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
267                         mem_cfg_fd, 0);
268         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
269                 if (mem_config != MAP_FAILED)
270                         /* errno is stale, don't use */
271                         rte_panic("Cannot mmap memory for rte_config at [%p], got [%p]"
272                                   " - please use '--base-virtaddr' option\n",
273                                   rte_mem_cfg_addr, mem_config);
274                 else
275                         rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
276                                   errno, strerror(errno));
277         }
278         close(mem_cfg_fd);
279
280         rte_config.mem_config = mem_config;
281 }
282
283 /* Detect if we are a primary or a secondary process */
284 enum rte_proc_type_t
285 eal_proc_type_detect(void)
286 {
287         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
288         const char *pathname = eal_runtime_config_path();
289
290         /* if we can open the file but not get a write-lock we are a secondary
291          * process. NOTE: if we get a file handle back, we keep that open
292          * and don't close it to prevent a race condition between multiple opens */
293         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
294                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
295                 ptype = RTE_PROC_SECONDARY;
296
297         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
298                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
299
300         return ptype;
301 }
302
303 /* Sets up rte_config structure with the pointer to shared memory config.*/
304 static void
305 rte_config_init(void)
306 {
307         rte_config.process_type = internal_config.process_type;
308
309         switch (rte_config.process_type){
310         case RTE_PROC_PRIMARY:
311                 rte_eal_config_create();
312                 break;
313         case RTE_PROC_SECONDARY:
314                 rte_eal_config_attach();
315                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
316                 rte_eal_config_reattach();
317                 break;
318         case RTE_PROC_AUTO:
319         case RTE_PROC_INVALID:
320                 rte_panic("Invalid process type\n");
321         }
322 }
323
324 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
325 static void
326 eal_hugedirs_unlock(void)
327 {
328         int i;
329
330         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
331         {
332                 /* skip uninitialized */
333                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
334                         continue;
335                 /* unlock hugepage file */
336                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
337                 close(internal_config.hugepage_info[i].lock_descriptor);
338                 /* reset the field */
339                 internal_config.hugepage_info[i].lock_descriptor = -1;
340         }
341 }
342
343 /* display usage */
344 static void
345 eal_usage(const char *prgname)
346 {
347         printf("\nUsage: %s ", prgname);
348         eal_common_usage();
349         printf("EAL Linux options:\n"
350                "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
351                "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
352                "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
353                "  --"OPT_BASE_VIRTADDR"     Base virtual address\n"
354                "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
355                "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
356                "  --"OPT_XEN_DOM0"          Support running on Xen dom0 without hugetlbfs\n"
357                "\n");
358         /* Allow the application to print its usage message too if hook is set */
359         if ( rte_application_usage_hook ) {
360                 printf("===== Application Usage =====\n\n");
361                 rte_application_usage_hook(prgname);
362         }
363 }
364
365 /* Set a per-application usage message */
366 rte_usage_hook_t
367 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
368 {
369         rte_usage_hook_t        old_func;
370
371         /* Will be NULL on the first call to denote the last usage routine. */
372         old_func                                        = rte_application_usage_hook;
373         rte_application_usage_hook      = usage_func;
374
375         return old_func;
376 }
377
378 static int
379 eal_parse_socket_mem(char *socket_mem)
380 {
381         char * arg[RTE_MAX_NUMA_NODES];
382         char *end;
383         int arg_num, i, len;
384         uint64_t total_mem = 0;
385
386         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
387         if (len == SOCKET_MEM_STRLEN) {
388                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
389                 return -1;
390         }
391
392         /* all other error cases will be caught later */
393         if (!isdigit(socket_mem[len-1]))
394                 return -1;
395
396         /* split the optarg into separate socket values */
397         arg_num = rte_strsplit(socket_mem, len,
398                         arg, RTE_MAX_NUMA_NODES, ',');
399
400         /* if split failed, or 0 arguments */
401         if (arg_num <= 0)
402                 return -1;
403
404         internal_config.force_sockets = 1;
405
406         /* parse each defined socket option */
407         errno = 0;
408         for (i = 0; i < arg_num; i++) {
409                 end = NULL;
410                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
411
412                 /* check for invalid input */
413                 if ((errno != 0)  ||
414                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
415                         return -1;
416                 internal_config.socket_mem[i] *= 1024ULL;
417                 internal_config.socket_mem[i] *= 1024ULL;
418                 total_mem += internal_config.socket_mem[i];
419         }
420
421         /* check if we have a positive amount of total memory */
422         if (total_mem == 0)
423                 return -1;
424
425         return 0;
426 }
427
428 static int
429 eal_parse_base_virtaddr(const char *arg)
430 {
431         char *end;
432         uint64_t addr;
433
434         errno = 0;
435         addr = strtoull(arg, &end, 16);
436
437         /* check for errors */
438         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
439                 return -1;
440
441         /* make sure we don't exceed 32-bit boundary on 32-bit target */
442 #ifndef RTE_ARCH_64
443         if (addr >= UINTPTR_MAX)
444                 return -1;
445 #endif
446
447         /* align the addr on 16M boundary, 16MB is the minimum huge page
448          * size on IBM Power architecture. If the addr is aligned to 16MB,
449          * it can align to 2MB for x86. So this alignment can also be used
450          * on x86 */
451         internal_config.base_virtaddr =
452                 RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
453
454         return 0;
455 }
456
457 static int
458 eal_parse_vfio_intr(const char *mode)
459 {
460         unsigned i;
461         static struct {
462                 const char *name;
463                 enum rte_intr_mode value;
464         } map[] = {
465                 { "legacy", RTE_INTR_MODE_LEGACY },
466                 { "msi", RTE_INTR_MODE_MSI },
467                 { "msix", RTE_INTR_MODE_MSIX },
468         };
469
470         for (i = 0; i < RTE_DIM(map); i++) {
471                 if (!strcmp(mode, map[i].name)) {
472                         internal_config.vfio_intr_mode = map[i].value;
473                         return 0;
474                 }
475         }
476         return -1;
477 }
478
479 /* Parse the arguments for --log-level only */
480 static void
481 eal_log_level_parse(int argc, char **argv)
482 {
483         int opt;
484         char **argvopt;
485         int option_index;
486         const int old_optind = optind;
487         const int old_optopt = optopt;
488         char * const old_optarg = optarg;
489
490         argvopt = argv;
491         optind = 1;
492
493         while ((opt = getopt_long(argc, argvopt, eal_short_options,
494                                   eal_long_options, &option_index)) != EOF) {
495
496                 int ret;
497
498                 /* getopt is not happy, stop right now */
499                 if (opt == '?')
500                         break;
501
502                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
503                         eal_parse_common_option(opt, optarg, &internal_config) : 0;
504
505                 /* common parser is not happy */
506                 if (ret < 0)
507                         break;
508         }
509
510         /* restore getopt lib */
511         optind = old_optind;
512         optopt = old_optopt;
513         optarg = old_optarg;
514 }
515
516 /* Parse the argument given in the command line of the application */
517 static int
518 eal_parse_args(int argc, char **argv)
519 {
520         int opt, ret;
521         char **argvopt;
522         int option_index;
523         char *prgname = argv[0];
524         const int old_optind = optind;
525         const int old_optopt = optopt;
526         char * const old_optarg = optarg;
527
528         argvopt = argv;
529         optind = 1;
530
531         while ((opt = getopt_long(argc, argvopt, eal_short_options,
532                                   eal_long_options, &option_index)) != EOF) {
533
534                 /* getopt is not happy, stop right now */
535                 if (opt == '?') {
536                         eal_usage(prgname);
537                         ret = -1;
538                         goto out;
539                 }
540
541                 ret = eal_parse_common_option(opt, optarg, &internal_config);
542                 /* common parser is not happy */
543                 if (ret < 0) {
544                         eal_usage(prgname);
545                         ret = -1;
546                         goto out;
547                 }
548                 /* common parser handled this option */
549                 if (ret == 0)
550                         continue;
551
552                 switch (opt) {
553                 case 'h':
554                         eal_usage(prgname);
555                         exit(EXIT_SUCCESS);
556
557                 /* long options */
558                 case OPT_XEN_DOM0_NUM:
559 #ifdef RTE_LIBRTE_XEN_DOM0
560                         internal_config.xen_dom0_support = 1;
561 #else
562                         RTE_LOG(ERR, EAL, "Can't support DPDK app "
563                                 "running on Dom0, please configure"
564                                 " RTE_LIBRTE_XEN_DOM0=y\n");
565                         ret = -1;
566                         goto out;
567 #endif
568                         break;
569
570                 case OPT_HUGE_DIR_NUM:
571                         internal_config.hugepage_dir = optarg;
572                         break;
573
574                 case OPT_FILE_PREFIX_NUM:
575                         internal_config.hugefile_prefix = optarg;
576                         break;
577
578                 case OPT_SOCKET_MEM_NUM:
579                         if (eal_parse_socket_mem(optarg) < 0) {
580                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
581                                                 OPT_SOCKET_MEM "\n");
582                                 eal_usage(prgname);
583                                 ret = -1;
584                                 goto out;
585                         }
586                         break;
587
588                 case OPT_BASE_VIRTADDR_NUM:
589                         if (eal_parse_base_virtaddr(optarg) < 0) {
590                                 RTE_LOG(ERR, EAL, "invalid parameter for --"
591                                                 OPT_BASE_VIRTADDR "\n");
592                                 eal_usage(prgname);
593                                 ret = -1;
594                                 goto out;
595                         }
596                         break;
597
598                 case OPT_VFIO_INTR_NUM:
599                         if (eal_parse_vfio_intr(optarg) < 0) {
600                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
601                                                 OPT_VFIO_INTR "\n");
602                                 eal_usage(prgname);
603                                 ret = -1;
604                                 goto out;
605                         }
606                         break;
607
608                 case OPT_CREATE_UIO_DEV_NUM:
609                         internal_config.create_uio_dev = 1;
610                         break;
611
612                 default:
613                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
614                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
615                                         "on Linux\n", opt);
616                         } else if (opt >= OPT_LONG_MIN_NUM &&
617                                    opt < OPT_LONG_MAX_NUM) {
618                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
619                                         "on Linux\n",
620                                         eal_long_options[option_index].name);
621                         } else {
622                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
623                                         "on Linux\n", opt);
624                         }
625                         eal_usage(prgname);
626                         ret = -1;
627                         goto out;
628                 }
629         }
630
631         if (eal_adjust_config(&internal_config) != 0) {
632                 ret = -1;
633                 goto out;
634         }
635
636         /* sanity checks */
637         if (eal_check_common_options(&internal_config) != 0) {
638                 eal_usage(prgname);
639                 ret = -1;
640                 goto out;
641         }
642
643         /* --xen-dom0 doesn't make sense with --socket-mem */
644         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
645                 RTE_LOG(ERR, EAL, "Options --"OPT_SOCKET_MEM" cannot be specified "
646                         "together with --"OPT_XEN_DOM0"\n");
647                 eal_usage(prgname);
648                 ret = -1;
649                 goto out;
650         }
651
652         if (optind >= 0)
653                 argv[optind-1] = prgname;
654         ret = optind-1;
655
656 out:
657         /* restore getopt lib */
658         optind = old_optind;
659         optopt = old_optopt;
660         optarg = old_optarg;
661
662         return ret;
663 }
664
665 static void
666 eal_check_mem_on_local_socket(void)
667 {
668         const struct rte_memseg *ms;
669         int i, socket_id;
670
671         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
672
673         ms = rte_eal_get_physmem_layout();
674
675         for (i = 0; i < RTE_MAX_MEMSEG; i++)
676                 if (ms[i].socket_id == socket_id &&
677                                 ms[i].len > 0)
678                         return;
679
680         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
681                         "memory on local socket!\n");
682 }
683
684 static int
685 sync_func(__attribute__((unused)) void *arg)
686 {
687         return 0;
688 }
689
690 inline static void
691 rte_eal_mcfg_complete(void)
692 {
693         /* ALL shared mem_config related INIT DONE */
694         if (rte_config.process_type == RTE_PROC_PRIMARY)
695                 rte_config.mem_config->magic = RTE_MAGIC;
696 }
697
698 /*
699  * Request iopl privilege for all RPL, returns 0 on success
700  * iopl() call is mostly for the i386 architecture. For other architectures,
701  * return -1 to indicate IO privilege can't be changed in this way.
702  */
703 int
704 rte_eal_iopl_init(void)
705 {
706 #if defined(RTE_ARCH_X86)
707         if (iopl(3) != 0)
708                 return -1;
709 #endif
710         return 0;
711 }
712
713 #ifdef VFIO_PRESENT
714 static int rte_eal_vfio_setup(void)
715 {
716         int vfio_enabled = 0;
717
718         if (!internal_config.no_pci) {
719                 pci_vfio_enable();
720                 vfio_enabled |= pci_vfio_is_enabled();
721         }
722
723         if (vfio_enabled) {
724
725                 /* if we are primary process, create a thread to communicate with
726                  * secondary processes. the thread will use a socket to wait for
727                  * requests from secondary process to send open file descriptors,
728                  * because VFIO does not allow multiple open descriptors on a group or
729                  * VFIO container.
730                  */
731                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
732                                 vfio_mp_sync_setup() < 0)
733                         return -1;
734         }
735
736         return 0;
737 }
738 #endif
739
740 static void rte_eal_init_alert(const char *msg)
741 {
742         fprintf(stderr, "EAL: FATAL: %s\n", msg);
743         RTE_LOG(ERR, EAL, "%s\n", msg);
744 }
745
746 /* Launch threads, called at application init(). */
747 int
748 rte_eal_init(int argc, char **argv)
749 {
750         int i, fctret, ret;
751         pthread_t thread_id;
752         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
753         const char *logid;
754         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
755         char thread_name[RTE_MAX_THREAD_NAME_LEN];
756
757         /* checks if the machine is adequate */
758         if (!rte_cpu_is_supported()) {
759                 rte_eal_init_alert("unsupported cpu type.");
760                 rte_errno = ENOTSUP;
761                 return -1;
762         }
763
764         if (!rte_atomic32_test_and_set(&run_once)) {
765                 rte_eal_init_alert("already called initialization.");
766                 rte_errno = EALREADY;
767                 return -1;
768         }
769
770         logid = strrchr(argv[0], '/');
771         logid = strdup(logid ? logid + 1: argv[0]);
772
773         thread_id = pthread_self();
774
775         eal_reset_internal_config(&internal_config);
776
777         /* set log level as early as possible */
778         eal_log_level_parse(argc, argv);
779
780         if (rte_eal_cpu_init() < 0) {
781                 rte_eal_init_alert("Cannot detect lcores.");
782                 rte_errno = ENOTSUP;
783                 return -1;
784         }
785
786         fctret = eal_parse_args(argc, argv);
787         if (fctret < 0) {
788                 rte_eal_init_alert("Invalid 'command line' arguments.");
789                 rte_errno = EINVAL;
790                 rte_atomic32_clear(&run_once);
791                 return -1;
792         }
793
794         if (internal_config.no_hugetlbfs == 0 &&
795                         internal_config.process_type != RTE_PROC_SECONDARY &&
796                         internal_config.xen_dom0_support == 0 &&
797                         eal_hugepage_info_init() < 0) {
798                 rte_eal_init_alert("Cannot get hugepage information.");
799                 rte_errno = EACCES;
800                 rte_atomic32_clear(&run_once);
801                 return -1;
802         }
803
804         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
805                 if (internal_config.no_hugetlbfs)
806                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
807         }
808
809         if (internal_config.vmware_tsc_map == 1) {
810 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
811                 rte_cycles_vmware_tsc_map = 1;
812                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
813                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
814 #else
815                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
816                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
817 #endif
818         }
819
820         rte_srand(rte_rdtsc());
821
822         rte_config_init();
823
824         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
825                 rte_eal_init_alert("Cannot init logging.");
826                 rte_errno = ENOMEM;
827                 rte_atomic32_clear(&run_once);
828                 return -1;
829         }
830
831 #ifdef VFIO_PRESENT
832         if (rte_eal_vfio_setup() < 0) {
833                 rte_eal_init_alert("Cannot init VFIO\n");
834                 rte_errno = EAGAIN;
835                 rte_atomic32_clear(&run_once);
836                 return -1;
837         }
838 #endif
839
840         if (rte_eal_memory_init() < 0) {
841                 rte_eal_init_alert("Cannot init memory\n");
842                 rte_errno = ENOMEM;
843                 return -1;
844         }
845
846         /* the directories are locked during eal_hugepage_info_init */
847         eal_hugedirs_unlock();
848
849         if (rte_eal_memzone_init() < 0) {
850                 rte_eal_init_alert("Cannot init memzone\n");
851                 rte_errno = ENODEV;
852                 return -1;
853         }
854
855         if (rte_eal_tailqs_init() < 0) {
856                 rte_eal_init_alert("Cannot init tail queues for objects\n");
857                 rte_errno = EFAULT;
858                 return -1;
859         }
860
861         if (rte_eal_alarm_init() < 0) {
862                 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
863                 /* rte_eal_alarm_init sets rte_errno on failure. */
864                 return -1;
865         }
866
867         if (rte_eal_timer_init() < 0) {
868                 rte_eal_init_alert("Cannot init HPET or TSC timers\n");
869                 rte_errno = ENOTSUP;
870                 return -1;
871         }
872
873         eal_check_mem_on_local_socket();
874
875         if (eal_plugins_init() < 0)
876                 rte_eal_init_alert("Cannot init plugins\n");
877
878         eal_thread_init_master(rte_config.master_lcore);
879
880         ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
881
882         RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
883                 rte_config.master_lcore, (int)thread_id, cpuset,
884                 ret == 0 ? "" : "...");
885
886         if (rte_eal_intr_init() < 0) {
887                 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
888                 return -1;
889         }
890
891         if (rte_bus_scan()) {
892                 rte_eal_init_alert("Cannot scan the buses for devices\n");
893                 rte_errno = ENODEV;
894                 return -1;
895         }
896
897         RTE_LCORE_FOREACH_SLAVE(i) {
898
899                 /*
900                  * create communication pipes between master thread
901                  * and children
902                  */
903                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
904                         rte_panic("Cannot create pipe\n");
905                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
906                         rte_panic("Cannot create pipe\n");
907
908                 lcore_config[i].state = WAIT;
909
910                 /* create a thread for each lcore */
911                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
912                                      eal_thread_loop, NULL);
913                 if (ret != 0)
914                         rte_panic("Cannot create thread\n");
915
916                 /* Set thread_name for aid in debugging. */
917                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
918                         "lcore-slave-%d", i);
919                 ret = rte_thread_setname(lcore_config[i].thread_id,
920                                                 thread_name);
921                 if (ret != 0)
922                         RTE_LOG(DEBUG, EAL,
923                                 "Cannot set name for lcore thread\n");
924         }
925
926         /*
927          * Launch a dummy function on all slave lcores, so that master lcore
928          * knows they are all ready when this function returns.
929          */
930         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
931         rte_eal_mp_wait_lcore();
932
933         /* Probe all the buses and devices/drivers on them */
934         if (rte_bus_probe()) {
935                 rte_eal_init_alert("Cannot probe devices\n");
936                 rte_errno = ENOTSUP;
937                 return -1;
938         }
939
940         rte_eal_mcfg_complete();
941
942         return fctret;
943 }
944
945 /* get core role */
946 enum rte_lcore_role_t
947 rte_eal_lcore_role(unsigned lcore_id)
948 {
949         return rte_config.lcore_role[lcore_id];
950 }
951
952 enum rte_proc_type_t
953 rte_eal_process_type(void)
954 {
955         return rte_config.process_type;
956 }
957
958 int rte_eal_has_hugepages(void)
959 {
960         return ! internal_config.no_hugetlbfs;
961 }
962
963 int
964 rte_eal_check_module(const char *module_name)
965 {
966         char sysfs_mod_name[PATH_MAX];
967         struct stat st;
968         int n;
969
970         if (NULL == module_name)
971                 return -1;
972
973         /* Check if there is sysfs mounted */
974         if (stat("/sys/module", &st) != 0) {
975                 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
976                         errno, strerror(errno));
977                 return -1;
978         }
979
980         /* A module might be built-in, therefore try sysfs */
981         n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
982         if (n < 0 || n > PATH_MAX) {
983                 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
984                 return -1;
985         }
986
987         if (stat(sysfs_mod_name, &st) != 0) {
988                 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
989                         sysfs_mod_name, errno, strerror(errno));
990                 return 0;
991         }
992
993         /* Module has been found */
994         return 1;
995 }