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