Imported Upstream version 16.07-rc2
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 <errno.h>
50 #include <sys/mman.h>
51 #include <sys/queue.h>
52 #include <sys/stat.h>
53 #if defined(RTE_ARCH_X86)
54 #include <sys/io.h>
55 #endif
56
57 #include <rte_common.h>
58 #include <rte_debug.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_launch.h>
62 #include <rte_eal.h>
63 #include <rte_eal_memconfig.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_log.h>
67 #include <rte_random.h>
68 #include <rte_cycles.h>
69 #include <rte_string_fns.h>
70 #include <rte_cpuflags.h>
71 #include <rte_interrupts.h>
72 #include <rte_pci.h>
73 #include <rte_devargs.h>
74 #include <rte_common.h>
75 #include <rte_version.h>
76 #include <rte_atomic.h>
77 #include <malloc_heap.h>
78
79 #include "eal_private.h"
80 #include "eal_thread.h"
81 #include "eal_internal_cfg.h"
82 #include "eal_filesystem.h"
83 #include "eal_hugepages.h"
84 #include "eal_options.h"
85 #include "eal_vfio.h"
86
87 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
88
89 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
90
91 /* Allow the application to print its usage message too if set */
92 static rte_usage_hook_t rte_application_usage_hook = NULL;
93
94 /* early configuration structure, when memory config is not mmapped */
95 static struct rte_mem_config early_mem_config;
96
97 /* define fd variable here, because file needs to be kept open for the
98  * duration of the program, as we hold a write lock on it in the primary proc */
99 static int mem_cfg_fd = -1;
100
101 static struct flock wr_lock = {
102                 .l_type = F_WRLCK,
103                 .l_whence = SEEK_SET,
104                 .l_start = offsetof(struct rte_mem_config, memseg),
105                 .l_len = sizeof(early_mem_config.memseg),
106 };
107
108 /* Address of global and public configuration */
109 static struct rte_config rte_config = {
110                 .mem_config = &early_mem_config,
111 };
112
113 /* internal configuration (per-core) */
114 struct lcore_config lcore_config[RTE_MAX_LCORE];
115
116 /* internal configuration */
117 struct internal_config internal_config;
118
119 /* used by rte_rdtsc() */
120 int rte_cycles_vmware_tsc_map;
121
122 /* Return a pointer to the configuration structure */
123 struct rte_config *
124 rte_eal_get_configuration(void)
125 {
126         return &rte_config;
127 }
128
129 /* parse a sysfs (or other) file containing one integer value */
130 int
131 eal_parse_sysfs_value(const char *filename, unsigned long *val)
132 {
133         FILE *f;
134         char buf[BUFSIZ];
135         char *end = NULL;
136
137         if ((f = fopen(filename, "r")) == NULL) {
138                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
139                         __func__, filename);
140                 return -1;
141         }
142
143         if (fgets(buf, sizeof(buf), f) == NULL) {
144                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
145                         __func__, filename);
146                 fclose(f);
147                 return -1;
148         }
149         *val = strtoul(buf, &end, 0);
150         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
151                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
152                                 __func__, filename);
153                 fclose(f);
154                 return -1;
155         }
156         fclose(f);
157         return 0;
158 }
159
160
161 /* create memory configuration in shared/mmap memory. Take out
162  * a write lock on the memsegs, so we can auto-detect primary/secondary.
163  * This means we never close the file while running (auto-close on exit).
164  * We also don't lock the whole file, so that in future we can use read-locks
165  * on other parts, e.g. memzones, to detect if there are running secondary
166  * processes. */
167 static void
168 rte_eal_config_create(void)
169 {
170         void *rte_mem_cfg_addr;
171         int retval;
172
173         const char *pathname = eal_runtime_config_path();
174
175         if (internal_config.no_shconf)
176                 return;
177
178         /* map the config before hugepage address so that we don't waste a page */
179         if (internal_config.base_virtaddr != 0)
180                 rte_mem_cfg_addr = (void *)
181                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
182                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
183         else
184                 rte_mem_cfg_addr = NULL;
185
186         if (mem_cfg_fd < 0){
187                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
188                 if (mem_cfg_fd < 0)
189                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
190         }
191
192         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
193         if (retval < 0){
194                 close(mem_cfg_fd);
195                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
196         }
197
198         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
199         if (retval < 0){
200                 close(mem_cfg_fd);
201                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
202                                 "process running?\n", pathname);
203         }
204
205         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
206                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
207
208         if (rte_mem_cfg_addr == MAP_FAILED){
209                 rte_panic("Cannot mmap memory for rte_config\n");
210         }
211         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
212         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
213
214         /* store address of the config in the config itself so that secondary
215          * processes could later map the config into this exact location */
216         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
217
218 }
219
220 /* attach to an existing shared memory config */
221 static void
222 rte_eal_config_attach(void)
223 {
224         struct rte_mem_config *mem_config;
225
226         const char *pathname = eal_runtime_config_path();
227
228         if (internal_config.no_shconf)
229                 return;
230
231         if (mem_cfg_fd < 0){
232                 mem_cfg_fd = open(pathname, O_RDWR);
233                 if (mem_cfg_fd < 0)
234                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
235         }
236
237         /* map it as read-only first */
238         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
239                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
240         if (mem_config == MAP_FAILED)
241                 rte_panic("Cannot mmap memory for rte_config\n");
242
243         rte_config.mem_config = mem_config;
244 }
245
246 /* reattach the shared config at exact memory location primary process has it */
247 static void
248 rte_eal_config_reattach(void)
249 {
250         struct rte_mem_config *mem_config;
251         void *rte_mem_cfg_addr;
252
253         if (internal_config.no_shconf)
254                 return;
255
256         /* save the address primary process has mapped shared config to */
257         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
258
259         /* unmap original config */
260         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
261
262         /* remap the config at proper address */
263         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
264                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
265                         mem_cfg_fd, 0);
266         close(mem_cfg_fd);
267         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr)
268                 rte_panic("Cannot mmap memory for rte_config\n");
269
270         rte_config.mem_config = mem_config;
271 }
272
273 /* Detect if we are a primary or a secondary process */
274 enum rte_proc_type_t
275 eal_proc_type_detect(void)
276 {
277         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
278         const char *pathname = eal_runtime_config_path();
279
280         /* if we can open the file but not get a write-lock we are a secondary
281          * process. NOTE: if we get a file handle back, we keep that open
282          * and don't close it to prevent a race condition between multiple opens */
283         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
284                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
285                 ptype = RTE_PROC_SECONDARY;
286
287         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
288                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
289
290         return ptype;
291 }
292
293 /* Sets up rte_config structure with the pointer to shared memory config.*/
294 static void
295 rte_config_init(void)
296 {
297         rte_config.process_type = internal_config.process_type;
298
299         switch (rte_config.process_type){
300         case RTE_PROC_PRIMARY:
301                 rte_eal_config_create();
302                 break;
303         case RTE_PROC_SECONDARY:
304                 rte_eal_config_attach();
305                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
306                 rte_eal_config_reattach();
307                 break;
308         case RTE_PROC_AUTO:
309         case RTE_PROC_INVALID:
310                 rte_panic("Invalid process type\n");
311         }
312 }
313
314 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
315 static void
316 eal_hugedirs_unlock(void)
317 {
318         int i;
319
320         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
321         {
322                 /* skip uninitialized */
323                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
324                         continue;
325                 /* unlock hugepage file */
326                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
327                 close(internal_config.hugepage_info[i].lock_descriptor);
328                 /* reset the field */
329                 internal_config.hugepage_info[i].lock_descriptor = -1;
330         }
331 }
332
333 /* display usage */
334 static void
335 eal_usage(const char *prgname)
336 {
337         printf("\nUsage: %s ", prgname);
338         eal_common_usage();
339         printf("EAL Linux options:\n"
340                "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
341                "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
342                "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
343                "  --"OPT_BASE_VIRTADDR"     Base virtual address\n"
344                "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
345                "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
346                "  --"OPT_XEN_DOM0"          Support running on Xen dom0 without hugetlbfs\n"
347                "\n");
348         /* Allow the application to print its usage message too if hook is set */
349         if ( rte_application_usage_hook ) {
350                 printf("===== Application Usage =====\n\n");
351                 rte_application_usage_hook(prgname);
352         }
353 }
354
355 /* Set a per-application usage message */
356 rte_usage_hook_t
357 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
358 {
359         rte_usage_hook_t        old_func;
360
361         /* Will be NULL on the first call to denote the last usage routine. */
362         old_func                                        = rte_application_usage_hook;
363         rte_application_usage_hook      = usage_func;
364
365         return old_func;
366 }
367
368 static int
369 eal_parse_socket_mem(char *socket_mem)
370 {
371         char * arg[RTE_MAX_NUMA_NODES];
372         char *end;
373         int arg_num, i, len;
374         uint64_t total_mem = 0;
375
376         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
377         if (len == SOCKET_MEM_STRLEN) {
378                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
379                 return -1;
380         }
381
382         /* all other error cases will be caught later */
383         if (!isdigit(socket_mem[len-1]))
384                 return -1;
385
386         /* split the optarg into separate socket values */
387         arg_num = rte_strsplit(socket_mem, len,
388                         arg, RTE_MAX_NUMA_NODES, ',');
389
390         /* if split failed, or 0 arguments */
391         if (arg_num <= 0)
392                 return -1;
393
394         internal_config.force_sockets = 1;
395
396         /* parse each defined socket option */
397         errno = 0;
398         for (i = 0; i < arg_num; i++) {
399                 end = NULL;
400                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
401
402                 /* check for invalid input */
403                 if ((errno != 0)  ||
404                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
405                         return -1;
406                 internal_config.socket_mem[i] *= 1024ULL;
407                 internal_config.socket_mem[i] *= 1024ULL;
408                 total_mem += internal_config.socket_mem[i];
409         }
410
411         /* check if we have a positive amount of total memory */
412         if (total_mem == 0)
413                 return -1;
414
415         return 0;
416 }
417
418 static int
419 eal_parse_base_virtaddr(const char *arg)
420 {
421         char *end;
422         uint64_t addr;
423
424         errno = 0;
425         addr = strtoull(arg, &end, 16);
426
427         /* check for errors */
428         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
429                 return -1;
430
431         /* make sure we don't exceed 32-bit boundary on 32-bit target */
432 #ifndef RTE_ARCH_64
433         if (addr >= UINTPTR_MAX)
434                 return -1;
435 #endif
436
437         /* align the addr on 16M boundary, 16MB is the minimum huge page
438          * size on IBM Power architecture. If the addr is aligned to 16MB,
439          * it can align to 2MB for x86. So this alignment can also be used
440          * on x86 */
441         internal_config.base_virtaddr =
442                 RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
443
444         return 0;
445 }
446
447 static int
448 eal_parse_vfio_intr(const char *mode)
449 {
450         unsigned i;
451         static struct {
452                 const char *name;
453                 enum rte_intr_mode value;
454         } map[] = {
455                 { "legacy", RTE_INTR_MODE_LEGACY },
456                 { "msi", RTE_INTR_MODE_MSI },
457                 { "msix", RTE_INTR_MODE_MSIX },
458         };
459
460         for (i = 0; i < RTE_DIM(map); i++) {
461                 if (!strcmp(mode, map[i].name)) {
462                         internal_config.vfio_intr_mode = map[i].value;
463                         return 0;
464                 }
465         }
466         return -1;
467 }
468
469 /* Parse the arguments for --log-level only */
470 static void
471 eal_log_level_parse(int argc, char **argv)
472 {
473         int opt;
474         char **argvopt;
475         int option_index;
476         const int old_optind = optind;
477         const int old_optopt = optopt;
478         char * const old_optarg = optarg;
479
480         argvopt = argv;
481         optind = 1;
482
483         eal_reset_internal_config(&internal_config);
484
485         while ((opt = getopt_long(argc, argvopt, eal_short_options,
486                                   eal_long_options, &option_index)) != EOF) {
487
488                 int ret;
489
490                 /* getopt is not happy, stop right now */
491                 if (opt == '?')
492                         break;
493
494                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
495                         eal_parse_common_option(opt, optarg, &internal_config) : 0;
496
497                 /* common parser is not happy */
498                 if (ret < 0)
499                         break;
500         }
501
502         /* restore getopt lib */
503         optind = old_optind;
504         optopt = old_optopt;
505         optarg = old_optarg;
506 }
507
508 /* Parse the argument given in the command line of the application */
509 static int
510 eal_parse_args(int argc, char **argv)
511 {
512         int opt, ret;
513         char **argvopt;
514         int option_index;
515         char *prgname = argv[0];
516         const int old_optind = optind;
517         const int old_optopt = optopt;
518         char * const old_optarg = optarg;
519
520         argvopt = argv;
521         optind = 1;
522
523         while ((opt = getopt_long(argc, argvopt, eal_short_options,
524                                   eal_long_options, &option_index)) != EOF) {
525
526                 /* getopt is not happy, stop right now */
527                 if (opt == '?') {
528                         eal_usage(prgname);
529                         ret = -1;
530                         goto out;
531                 }
532
533                 ret = eal_parse_common_option(opt, optarg, &internal_config);
534                 /* common parser is not happy */
535                 if (ret < 0) {
536                         eal_usage(prgname);
537                         ret = -1;
538                         goto out;
539                 }
540                 /* common parser handled this option */
541                 if (ret == 0)
542                         continue;
543
544                 switch (opt) {
545                 case 'h':
546                         eal_usage(prgname);
547                         exit(EXIT_SUCCESS);
548
549                 /* long options */
550                 case OPT_XEN_DOM0_NUM:
551 #ifdef RTE_LIBRTE_XEN_DOM0
552                         internal_config.xen_dom0_support = 1;
553 #else
554                         RTE_LOG(ERR, EAL, "Can't support DPDK app "
555                                 "running on Dom0, please configure"
556                                 " RTE_LIBRTE_XEN_DOM0=y\n");
557                         ret = -1;
558                         goto out;
559 #endif
560                         break;
561
562                 case OPT_HUGE_DIR_NUM:
563                         internal_config.hugepage_dir = optarg;
564                         break;
565
566                 case OPT_FILE_PREFIX_NUM:
567                         internal_config.hugefile_prefix = optarg;
568                         break;
569
570                 case OPT_SOCKET_MEM_NUM:
571                         if (eal_parse_socket_mem(optarg) < 0) {
572                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
573                                                 OPT_SOCKET_MEM "\n");
574                                 eal_usage(prgname);
575                                 ret = -1;
576                                 goto out;
577                         }
578                         break;
579
580                 case OPT_BASE_VIRTADDR_NUM:
581                         if (eal_parse_base_virtaddr(optarg) < 0) {
582                                 RTE_LOG(ERR, EAL, "invalid parameter for --"
583                                                 OPT_BASE_VIRTADDR "\n");
584                                 eal_usage(prgname);
585                                 ret = -1;
586                                 goto out;
587                         }
588                         break;
589
590                 case OPT_VFIO_INTR_NUM:
591                         if (eal_parse_vfio_intr(optarg) < 0) {
592                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
593                                                 OPT_VFIO_INTR "\n");
594                                 eal_usage(prgname);
595                                 ret = -1;
596                                 goto out;
597                         }
598                         break;
599
600                 case OPT_CREATE_UIO_DEV_NUM:
601                         internal_config.create_uio_dev = 1;
602                         break;
603
604                 default:
605                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
606                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
607                                         "on Linux\n", opt);
608                         } else if (opt >= OPT_LONG_MIN_NUM &&
609                                    opt < OPT_LONG_MAX_NUM) {
610                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
611                                         "on Linux\n",
612                                         eal_long_options[option_index].name);
613                         } else {
614                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
615                                         "on Linux\n", opt);
616                         }
617                         eal_usage(prgname);
618                         ret = -1;
619                         goto out;
620                 }
621         }
622
623         if (eal_adjust_config(&internal_config) != 0) {
624                 ret = -1;
625                 goto out;
626         }
627
628         /* sanity checks */
629         if (eal_check_common_options(&internal_config) != 0) {
630                 eal_usage(prgname);
631                 ret = -1;
632                 goto out;
633         }
634
635         /* --xen-dom0 doesn't make sense with --socket-mem */
636         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
637                 RTE_LOG(ERR, EAL, "Options --"OPT_SOCKET_MEM" cannot be specified "
638                         "together with --"OPT_XEN_DOM0"\n");
639                 eal_usage(prgname);
640                 ret = -1;
641                 goto out;
642         }
643
644         if (optind >= 0)
645                 argv[optind-1] = prgname;
646         ret = optind-1;
647
648 out:
649         /* restore getopt lib */
650         optind = old_optind;
651         optopt = old_optopt;
652         optarg = old_optarg;
653
654         return ret;
655 }
656
657 static void
658 eal_check_mem_on_local_socket(void)
659 {
660         const struct rte_memseg *ms;
661         int i, socket_id;
662
663         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
664
665         ms = rte_eal_get_physmem_layout();
666
667         for (i = 0; i < RTE_MAX_MEMSEG; i++)
668                 if (ms[i].socket_id == socket_id &&
669                                 ms[i].len > 0)
670                         return;
671
672         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
673                         "memory on local socket!\n");
674 }
675
676 static int
677 sync_func(__attribute__((unused)) void *arg)
678 {
679         return 0;
680 }
681
682 inline static void
683 rte_eal_mcfg_complete(void)
684 {
685         /* ALL shared mem_config related INIT DONE */
686         if (rte_config.process_type == RTE_PROC_PRIMARY)
687                 rte_config.mem_config->magic = RTE_MAGIC;
688 }
689
690 /*
691  * Request iopl privilege for all RPL, returns 0 on success
692  * iopl() call is mostly for the i386 architecture. For other architectures,
693  * return -1 to indicate IO privilege can't be changed in this way.
694  */
695 int
696 rte_eal_iopl_init(void)
697 {
698 #if defined(RTE_ARCH_X86)
699         if (iopl(3) != 0)
700                 return -1;
701 #endif
702         return 0;
703 }
704
705 #ifdef VFIO_PRESENT
706 static int rte_eal_vfio_setup(void)
707 {
708         int vfio_enabled = 0;
709
710         if (!internal_config.no_pci) {
711                 pci_vfio_enable();
712                 vfio_enabled |= pci_vfio_is_enabled();
713         }
714
715         if (vfio_enabled) {
716
717                 /* if we are primary process, create a thread to communicate with
718                  * secondary processes. the thread will use a socket to wait for
719                  * requests from secondary process to send open file descriptors,
720                  * because VFIO does not allow multiple open descriptors on a group or
721                  * VFIO container.
722                  */
723                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
724                                 vfio_mp_sync_setup() < 0)
725                         return -1;
726         }
727
728         return 0;
729 }
730 #endif
731
732 /* Launch threads, called at application init(). */
733 int
734 rte_eal_init(int argc, char **argv)
735 {
736         int i, fctret, ret;
737         pthread_t thread_id;
738         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
739         const char *logid;
740         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
741         char thread_name[RTE_MAX_THREAD_NAME_LEN];
742
743         if (!rte_atomic32_test_and_set(&run_once))
744                 return -1;
745
746         logid = strrchr(argv[0], '/');
747         logid = strdup(logid ? logid + 1: argv[0]);
748
749         thread_id = pthread_self();
750
751         if (rte_eal_log_early_init() < 0)
752                 rte_panic("Cannot init early logs\n");
753
754         eal_log_level_parse(argc, argv);
755
756         /* set log level as early as possible */
757         rte_set_log_level(internal_config.log_level);
758
759         if (rte_eal_cpu_init() < 0)
760                 rte_panic("Cannot detect lcores\n");
761
762         fctret = eal_parse_args(argc, argv);
763         if (fctret < 0)
764                 exit(1);
765
766         if (internal_config.no_hugetlbfs == 0 &&
767                         internal_config.process_type != RTE_PROC_SECONDARY &&
768                         internal_config.xen_dom0_support == 0 &&
769                         eal_hugepage_info_init() < 0)
770                 rte_panic("Cannot get hugepage information\n");
771
772         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
773                 if (internal_config.no_hugetlbfs)
774                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
775         }
776
777         if (internal_config.vmware_tsc_map == 1) {
778 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
779                 rte_cycles_vmware_tsc_map = 1;
780                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
781                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
782 #else
783                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
784                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
785 #endif
786         }
787
788         rte_srand(rte_rdtsc());
789
790         rte_config_init();
791
792         if (rte_eal_pci_init() < 0)
793                 rte_panic("Cannot init PCI\n");
794
795 #ifdef VFIO_PRESENT
796         if (rte_eal_vfio_setup() < 0)
797                 rte_panic("Cannot init VFIO\n");
798 #endif
799
800 #ifdef RTE_LIBRTE_IVSHMEM
801         if (rte_eal_ivshmem_init() < 0)
802                 rte_panic("Cannot init IVSHMEM\n");
803 #endif
804
805         if (rte_eal_memory_init() < 0)
806                 rte_panic("Cannot init memory\n");
807
808         /* the directories are locked during eal_hugepage_info_init */
809         eal_hugedirs_unlock();
810
811         if (rte_eal_memzone_init() < 0)
812                 rte_panic("Cannot init memzone\n");
813
814         if (rte_eal_tailqs_init() < 0)
815                 rte_panic("Cannot init tail queues for objects\n");
816
817 #ifdef RTE_LIBRTE_IVSHMEM
818         if (rte_eal_ivshmem_obj_init() < 0)
819                 rte_panic("Cannot init IVSHMEM objects\n");
820 #endif
821
822         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
823                 rte_panic("Cannot init logs\n");
824
825         if (rte_eal_alarm_init() < 0)
826                 rte_panic("Cannot init interrupt-handling thread\n");
827
828         if (rte_eal_timer_init() < 0)
829                 rte_panic("Cannot init HPET or TSC timers\n");
830
831         eal_check_mem_on_local_socket();
832
833         if (eal_plugins_init() < 0)
834                 rte_panic("Cannot init plugins\n");
835
836         eal_thread_init_master(rte_config.master_lcore);
837
838         ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
839
840         RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
841                 rte_config.master_lcore, (int)thread_id, cpuset,
842                 ret == 0 ? "" : "...");
843
844         if (rte_eal_dev_init() < 0)
845                 rte_panic("Cannot init pmd devices\n");
846
847         if (rte_eal_intr_init() < 0)
848                 rte_panic("Cannot init interrupt-handling thread\n");
849
850         RTE_LCORE_FOREACH_SLAVE(i) {
851
852                 /*
853                  * create communication pipes between master thread
854                  * and children
855                  */
856                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
857                         rte_panic("Cannot create pipe\n");
858                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
859                         rte_panic("Cannot create pipe\n");
860
861                 lcore_config[i].state = WAIT;
862
863                 /* create a thread for each lcore */
864                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
865                                      eal_thread_loop, NULL);
866                 if (ret != 0)
867                         rte_panic("Cannot create thread\n");
868
869                 /* Set thread_name for aid in debugging. */
870                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
871                         "lcore-slave-%d", i);
872                 ret = rte_thread_setname(lcore_config[i].thread_id,
873                                                 thread_name);
874                 if (ret != 0)
875                         RTE_LOG(DEBUG, EAL,
876                                 "Cannot set name for lcore thread\n");
877         }
878
879         /*
880          * Launch a dummy function on all slave lcores, so that master lcore
881          * knows they are all ready when this function returns.
882          */
883         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
884         rte_eal_mp_wait_lcore();
885
886         /* Probe & Initialize PCI devices */
887         if (rte_eal_pci_probe())
888                 rte_panic("Cannot probe PCI\n");
889
890         rte_eal_mcfg_complete();
891
892         return fctret;
893 }
894
895 /* get core role */
896 enum rte_lcore_role_t
897 rte_eal_lcore_role(unsigned lcore_id)
898 {
899         return rte_config.lcore_role[lcore_id];
900 }
901
902 enum rte_proc_type_t
903 rte_eal_process_type(void)
904 {
905         return rte_config.process_type;
906 }
907
908 int rte_eal_has_hugepages(void)
909 {
910         return ! internal_config.no_hugetlbfs;
911 }
912
913 int
914 rte_eal_check_module(const char *module_name)
915 {
916         char sysfs_mod_name[PATH_MAX];
917         struct stat st;
918         int n;
919
920         if (NULL == module_name)
921                 return -1;
922
923         /* Check if there is sysfs mounted */
924         if (stat("/sys/module", &st) != 0) {
925                 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
926                         errno, strerror(errno));
927                 return -1;
928         }
929
930         /* A module might be built-in, therefore try sysfs */
931         n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
932         if (n < 0 || n > PATH_MAX) {
933                 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
934                 return -1;
935         }
936
937         if (stat(sysfs_mod_name, &st) != 0) {
938                 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
939                         sysfs_mod_name, errno, strerror(errno));
940                 return 0;
941         }
942
943         /* Module has been found */
944         return 1;
945 }