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