eal: revert link bonding specific initialization
[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 #include <sys/io.h>
54
55 #include <rte_common.h>
56 #include <rte_debug.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_launch.h>
60 #include <rte_tailq.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_log.h>
66 #include <rte_random.h>
67 #include <rte_cycles.h>
68 #include <rte_string_fns.h>
69 #include <rte_cpuflags.h>
70 #include <rte_interrupts.h>
71 #include <rte_pci.h>
72 #include <rte_devargs.h>
73 #include <rte_common.h>
74 #include <rte_version.h>
75 #include <rte_atomic.h>
76 #include <malloc_heap.h>
77 #include <rte_eth_ring.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 #define HIGHEST_RPL 3
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 static 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 == RTE_PROC_AUTO) ?
313                         eal_proc_type_detect() : /* for auto, detect the type */
314                         internal_config.process_type; /* otherwise use what's already set */
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                "  -d LIB.so    : add driver (can be used multiple times)\n"
358                "  --"OPT_XEN_DOM0" : support application running on Xen Domain0 "
359                            "without hugetlbfs\n"
360                "  --"OPT_SOCKET_MEM" : memory to allocate on specific\n"
361                    "                 sockets (use comma separated values)\n"
362                "  --"OPT_HUGE_DIR"   : directory where hugetlbfs is mounted\n"
363                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
364                "  --"OPT_BASE_VIRTADDR": specify base virtual address\n"
365                "  --"OPT_VFIO_INTR": specify desired interrupt mode for VFIO "
366                            "(legacy|msi|msix)\n"
367                "  --"OPT_CREATE_UIO_DEV": create /dev/uioX (usually done by hotplug)\n"
368                "\n");
369         /* Allow the application to print its usage message too if hook is set */
370         if ( rte_application_usage_hook ) {
371                 printf("===== Application Usage =====\n\n");
372                 rte_application_usage_hook(prgname);
373         }
374 }
375
376 /* Set a per-application usage message */
377 rte_usage_hook_t
378 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
379 {
380         rte_usage_hook_t        old_func;
381
382         /* Will be NULL on the first call to denote the last usage routine. */
383         old_func                                        = rte_application_usage_hook;
384         rte_application_usage_hook      = usage_func;
385
386         return old_func;
387 }
388
389 static int
390 eal_parse_socket_mem(char *socket_mem)
391 {
392         char * arg[RTE_MAX_NUMA_NODES];
393         char *end;
394         int arg_num, i, len;
395         uint64_t total_mem = 0;
396
397         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
398         if (len == SOCKET_MEM_STRLEN) {
399                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
400                 return -1;
401         }
402
403         /* all other error cases will be caught later */
404         if (!isdigit(socket_mem[len-1]))
405                 return -1;
406
407         /* split the optarg into separate socket values */
408         arg_num = rte_strsplit(socket_mem, len,
409                         arg, RTE_MAX_NUMA_NODES, ',');
410
411         /* if split failed, or 0 arguments */
412         if (arg_num <= 0)
413                 return -1;
414
415         internal_config.force_sockets = 1;
416
417         /* parse each defined socket option */
418         errno = 0;
419         for (i = 0; i < arg_num; i++) {
420                 end = NULL;
421                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
422
423                 /* check for invalid input */
424                 if ((errno != 0)  ||
425                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
426                         return -1;
427                 internal_config.socket_mem[i] *= 1024ULL;
428                 internal_config.socket_mem[i] *= 1024ULL;
429                 total_mem += internal_config.socket_mem[i];
430         }
431
432         /* check if we have a positive amount of total memory */
433         if (total_mem == 0)
434                 return -1;
435
436         return 0;
437 }
438
439 static int
440 eal_parse_base_virtaddr(const char *arg)
441 {
442         char *end;
443         uint64_t addr;
444
445         errno = 0;
446         addr = strtoull(arg, &end, 16);
447
448         /* check for errors */
449         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
450                 return -1;
451
452         /* make sure we don't exceed 32-bit boundary on 32-bit target */
453 #ifndef RTE_ARCH_X86_64
454         if (addr >= UINTPTR_MAX)
455                 return -1;
456 #endif
457
458         /* align the addr on 2M boundary */
459         internal_config.base_virtaddr = RTE_PTR_ALIGN_CEIL((uintptr_t)addr,
460                                                            RTE_PGSIZE_2M);
461
462         return 0;
463 }
464
465 static int
466 eal_parse_vfio_intr(const char *mode)
467 {
468         unsigned i;
469         static struct {
470                 const char *name;
471                 enum rte_intr_mode value;
472         } map[] = {
473                 { "legacy", RTE_INTR_MODE_LEGACY },
474                 { "msi", RTE_INTR_MODE_MSI },
475                 { "msix", RTE_INTR_MODE_MSIX },
476         };
477
478         for (i = 0; i < RTE_DIM(map); i++) {
479                 if (!strcmp(mode, map[i].name)) {
480                         internal_config.vfio_intr_mode = map[i].value;
481                         return 0;
482                 }
483         }
484         return -1;
485 }
486
487 static inline size_t
488 eal_get_hugepage_mem_size(void)
489 {
490         uint64_t size = 0;
491         unsigned i, j;
492
493         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
494                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
495                 if (hpi->hugedir != NULL) {
496                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
497                                 size += hpi->hugepage_sz * hpi->num_pages[j];
498                         }
499                 }
500         }
501
502         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
503 }
504
505 /* Parse the argument given in the command line of the application */
506 static int
507 eal_parse_args(int argc, char **argv)
508 {
509         int opt, ret, i;
510         char **argvopt;
511         int option_index;
512         int coremask_ok = 0;
513         char *prgname = argv[0];
514         struct shared_driver *solib;
515
516         argvopt = argv;
517
518         internal_config.memory = 0;
519         internal_config.force_nrank = 0;
520         internal_config.force_nchannel = 0;
521         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
522         internal_config.hugepage_dir = NULL;
523         internal_config.force_sockets = 0;
524         internal_config.syslog_facility = LOG_DAEMON;
525         /* default value from build option */
526         internal_config.log_level = RTE_LOG_LEVEL;
527         internal_config.xen_dom0_support = 0;
528         /* if set to NONE, interrupt mode is determined automatically */
529         internal_config.vfio_intr_mode = RTE_INTR_MODE_NONE;
530 #ifdef RTE_LIBEAL_USE_HPET
531         internal_config.no_hpet = 0;
532 #else
533         internal_config.no_hpet = 1;
534 #endif
535         /* zero out the NUMA config */
536         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
537                 internal_config.socket_mem[i] = 0;
538
539         /* zero out hugedir descriptors */
540         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
541                 internal_config.hugepage_info[i].lock_descriptor = -1;
542
543         internal_config.vmware_tsc_map = 0;
544         internal_config.base_virtaddr = 0;
545
546         while ((opt = getopt_long(argc, argvopt, eal_short_options,
547                                   eal_long_options, &option_index)) != EOF) {
548
549                 int ret;
550
551                 /* getopt is not happy, stop right now */
552                 if (opt == '?')
553                         return -1;
554
555                 ret = eal_parse_common_option(opt, optarg, &internal_config);
556                 /* common parser is not happy */
557                 if (ret < 0) {
558                         eal_usage(prgname);
559                         return -1;
560                 }
561                 /* common parser handled this option */
562                 if (ret == 0) {
563                         /* special case, note that the common parser accepted
564                          * the coremask option */
565                         if (opt == 'c')
566                                 coremask_ok = 1;
567                         continue;
568                 }
569
570                 switch (opt) {
571                 /* force loading of external driver */
572                 case 'd':
573                         solib = malloc(sizeof(*solib));
574                         if (solib == NULL) {
575                                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
576                                 return -1;
577                         }
578                         memset(solib, 0, sizeof(*solib));
579                         strncpy(solib->name, optarg, PATH_MAX-1);
580                         solib->name[PATH_MAX-1] = 0;
581                         TAILQ_INSERT_TAIL(&solib_list, solib, next);
582                         break;
583
584                 /* long options */
585                 case OPT_XEN_DOM0_NUM:
586 #ifdef RTE_LIBRTE_XEN_DOM0
587                         internal_config.xen_dom0_support = 1;
588 #else
589                         RTE_LOG(ERR, EAL, "Can't support DPDK app "
590                                 "running on Dom0, please configure"
591                                 " RTE_LIBRTE_XEN_DOM0=y\n");
592                         return -1;
593 #endif
594                         break;
595
596                 case OPT_HUGE_DIR_NUM:
597                         internal_config.hugepage_dir = optarg;
598                         break;
599
600                 case OPT_FILE_PREFIX_NUM:
601                         internal_config.hugefile_prefix = optarg;
602                         break;
603
604                 case OPT_SOCKET_MEM_NUM:
605                         if (eal_parse_socket_mem(optarg) < 0) {
606                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
607                                                 OPT_SOCKET_MEM "\n");
608                                 eal_usage(prgname);
609                                 return -1;
610                         }
611                         break;
612
613                 case OPT_BASE_VIRTADDR_NUM:
614                         if (eal_parse_base_virtaddr(optarg) < 0) {
615                                 RTE_LOG(ERR, EAL, "invalid parameter for --"
616                                                 OPT_BASE_VIRTADDR "\n");
617                                 eal_usage(prgname);
618                                 return -1;
619                         }
620                         break;
621
622                 case OPT_VFIO_INTR_NUM:
623                         if (eal_parse_vfio_intr(optarg) < 0) {
624                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
625                                                 OPT_VFIO_INTR "\n");
626                                 eal_usage(prgname);
627                                 return -1;
628                         }
629                         break;
630
631                 case OPT_CREATE_UIO_DEV_NUM:
632                         internal_config.create_uio_dev = 1;
633                         break;
634
635                 default:
636                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
637                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
638                                         "on Linux\n", opt);
639                         } else if (opt >= OPT_LONG_MIN_NUM &&
640                                    opt < OPT_LONG_MAX_NUM) {
641                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
642                                         "on Linux\n",
643                                         eal_long_options[option_index].name);
644                         } else {
645                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
646                                         "on Linux\n", opt);
647                         }
648                         eal_usage(prgname);
649                         return -1;
650                 }
651         }
652
653         /* sanity checks */
654         if (!coremask_ok) {
655                 RTE_LOG(ERR, EAL, "coremask not specified\n");
656                 eal_usage(prgname);
657                 return -1;
658         }
659         if (internal_config.process_type == RTE_PROC_AUTO){
660                 internal_config.process_type = eal_proc_type_detect();
661         }
662         if (internal_config.process_type == RTE_PROC_INVALID){
663                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
664                 eal_usage(prgname);
665                 return -1;
666         }
667         if (internal_config.process_type == RTE_PROC_PRIMARY &&
668                         internal_config.force_nchannel == 0) {
669                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
670                 eal_usage(prgname);
671                 return -1;
672         }
673         if (index(internal_config.hugefile_prefix,'%') != NULL){
674                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
675                 eal_usage(prgname);
676                 return -1;
677         }
678         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
679                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
680                                 "at the same time\n");
681                 eal_usage(prgname);
682                 return -1;
683         }
684         /* --no-huge doesn't make sense with either -m or --socket-mem */
685         if (internal_config.no_hugetlbfs &&
686                         (internal_config.memory > 0 ||
687                                         internal_config.force_sockets == 1)) {
688                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
689                                 "together with --no-huge!\n");
690                 eal_usage(prgname);
691                 return -1;
692         }
693         /* --xen-dom0 doesn't make sense with --socket-mem */
694         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
695                 RTE_LOG(ERR, EAL, "Options --socket-mem cannot be specified "
696                                         "together with --xen_dom0!\n");
697                 eal_usage(prgname);
698                 return -1;
699         }
700
701         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
702                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
703                 RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
704                         "[-w] options cannot be used at the same time\n");
705                 eal_usage(prgname);
706                 return -1;
707         }
708
709         if (optind >= 0)
710                 argv[optind-1] = prgname;
711
712         /* if no memory amounts were requested, this will result in 0 and
713          * will be overriden later, right after eal_hugepage_info_init() */
714         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
715                 internal_config.memory += internal_config.socket_mem[i];
716
717         ret = optind-1;
718         optind = 0; /* reset getopt lib */
719         return ret;
720 }
721
722 static void
723 eal_check_mem_on_local_socket(void)
724 {
725         const struct rte_memseg *ms;
726         int i, socket_id;
727
728         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
729
730         ms = rte_eal_get_physmem_layout();
731
732         for (i = 0; i < RTE_MAX_MEMSEG; i++)
733                 if (ms[i].socket_id == socket_id &&
734                                 ms[i].len > 0)
735                         return;
736
737         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
738                         "memory on local socket!\n");
739 }
740
741 static int
742 sync_func(__attribute__((unused)) void *arg)
743 {
744         return 0;
745 }
746
747 inline static void
748 rte_eal_mcfg_complete(void)
749 {
750         /* ALL shared mem_config related INIT DONE */
751         if (rte_config.process_type == RTE_PROC_PRIMARY)
752                 rte_config.mem_config->magic = RTE_MAGIC;
753 }
754
755 /*
756  * Request iopl privilege for all RPL, returns 0 on success
757  */
758 static int
759 rte_eal_iopl_init(void)
760 {
761         return iopl(HIGHEST_RPL);
762 }
763
764 /* Launch threads, called at application init(). */
765 int
766 rte_eal_init(int argc, char **argv)
767 {
768         int i, fctret, ret;
769         pthread_t thread_id;
770         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
771         struct shared_driver *solib = NULL;
772         const char *logid;
773
774         if (!rte_atomic32_test_and_set(&run_once))
775                 return -1;
776
777         logid = strrchr(argv[0], '/');
778         logid = strdup(logid ? logid + 1: argv[0]);
779
780         thread_id = pthread_self();
781
782         if (rte_eal_log_early_init() < 0)
783                 rte_panic("Cannot init early logs\n");
784
785         if (rte_eal_cpu_init() < 0)
786                 rte_panic("Cannot detect lcores\n");
787
788         fctret = eal_parse_args(argc, argv);
789         if (fctret < 0)
790                 exit(1);
791
792         /* set log level as early as possible */
793         rte_set_log_level(internal_config.log_level);
794
795         if (internal_config.no_hugetlbfs == 0 &&
796                         internal_config.process_type != RTE_PROC_SECONDARY &&
797                         internal_config.xen_dom0_support == 0 &&
798                         eal_hugepage_info_init() < 0)
799                 rte_panic("Cannot get hugepage information\n");
800
801         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
802                 if (internal_config.no_hugetlbfs)
803                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
804                 else
805                         internal_config.memory = eal_get_hugepage_mem_size();
806         }
807
808         if (internal_config.vmware_tsc_map == 1) {
809 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
810                 rte_cycles_vmware_tsc_map = 1;
811                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
812                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
813 #else
814                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
815                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
816 #endif
817         }
818
819         rte_srand(rte_rdtsc());
820
821         rte_config_init();
822
823         if (rte_eal_iopl_init() == 0)
824                 rte_config.flags |= EAL_FLG_HIGH_IOPL;
825
826         if (rte_eal_pci_init() < 0)
827                 rte_panic("Cannot init PCI\n");
828
829 #ifdef RTE_LIBRTE_IVSHMEM
830         if (rte_eal_ivshmem_init() < 0)
831                 rte_panic("Cannot init IVSHMEM\n");
832 #endif
833
834         if (rte_eal_memory_init() < 0)
835                 rte_panic("Cannot init memory\n");
836
837         /* the directories are locked during eal_hugepage_info_init */
838         eal_hugedirs_unlock();
839
840         if (rte_eal_memzone_init() < 0)
841                 rte_panic("Cannot init memzone\n");
842
843         if (rte_eal_tailqs_init() < 0)
844                 rte_panic("Cannot init tail queues for objects\n");
845
846 #ifdef RTE_LIBRTE_IVSHMEM
847         if (rte_eal_ivshmem_obj_init() < 0)
848                 rte_panic("Cannot init IVSHMEM objects\n");
849 #endif
850
851         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
852                 rte_panic("Cannot init logs\n");
853
854         if (rte_eal_alarm_init() < 0)
855                 rte_panic("Cannot init interrupt-handling thread\n");
856
857         if (rte_eal_intr_init() < 0)
858                 rte_panic("Cannot init interrupt-handling thread\n");
859
860         if (rte_eal_timer_init() < 0)
861                 rte_panic("Cannot init HPET or TSC timers\n");
862
863         eal_check_mem_on_local_socket();
864
865         rte_eal_mcfg_complete();
866
867         TAILQ_FOREACH(solib, &solib_list, next) {
868                 RTE_LOG(INFO, EAL, "open shared lib %s\n", solib->name);
869                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
870                 if (solib->lib_handle == NULL)
871                         RTE_LOG(WARNING, EAL, "%s\n", dlerror());
872         }
873
874         eal_thread_init_master(rte_config.master_lcore);
875
876         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
877                 rte_config.master_lcore, (int)thread_id);
878
879         if (rte_eal_dev_init() < 0)
880                 rte_panic("Cannot init pmd devices\n");
881
882         RTE_LCORE_FOREACH_SLAVE(i) {
883
884                 /*
885                  * create communication pipes between master thread
886                  * and children
887                  */
888                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
889                         rte_panic("Cannot create pipe\n");
890                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
891                         rte_panic("Cannot create pipe\n");
892
893                 lcore_config[i].state = WAIT;
894
895                 /* create a thread for each lcore */
896                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
897                                      eal_thread_loop, NULL);
898                 if (ret != 0)
899                         rte_panic("Cannot create thread\n");
900         }
901
902         /*
903          * Launch a dummy function on all slave lcores, so that master lcore
904          * knows they are all ready when this function returns.
905          */
906         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
907         rte_eal_mp_wait_lcore();
908
909         /* Probe & Initialize PCI devices */
910         if (rte_eal_pci_probe())
911                 rte_panic("Cannot probe PCI\n");
912
913         return fctret;
914 }
915
916 /* get core role */
917 enum rte_lcore_role_t
918 rte_eal_lcore_role(unsigned lcore_id)
919 {
920         return (rte_config.lcore_role[lcore_id]);
921 }
922
923 enum rte_proc_type_t
924 rte_eal_process_type(void)
925 {
926         return (rte_config.process_type);
927 }
928
929 int rte_eal_has_hugepages(void)
930 {
931         return ! internal_config.no_hugetlbfs;
932 }