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