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