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