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