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