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