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