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