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