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