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