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