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