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