ad4391404d37b4b65d58cb3f75443da18c7c1373
[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 #include <rte_dev.h>
79
80 #include "eal_private.h"
81 #include "eal_thread.h"
82 #include "eal_internal_cfg.h"
83 #include "eal_filesystem.h"
84 #include "eal_hugepages.h"
85
86 #define OPT_HUGE_DIR    "huge-dir"
87 #define OPT_PROC_TYPE   "proc-type"
88 #define OPT_NO_SHCONF   "no-shconf"
89 #define OPT_NO_HPET     "no-hpet"
90 #define OPT_VMWARE_TSC_MAP   "vmware-tsc-map"
91 #define OPT_NO_PCI      "no-pci"
92 #define OPT_NO_HUGE     "no-huge"
93 #define OPT_FILE_PREFIX "file-prefix"
94 #define OPT_SOCKET_MEM  "socket-mem"
95 #define OPT_USE_DEVICE  "use-device"
96 #define OPT_PCI_WHITELIST "pci-whitelist"
97 #define OPT_PCI_BLACKLIST "pci-blacklist"
98 #define OPT_VDEV        "vdev"
99 #define OPT_SYSLOG      "syslog"
100 #define OPT_BASE_VIRTADDR   "base-virtaddr"
101 #define OPT_XEN_DOM0    "xen-dom0"
102 #define OPT_CREATE_UIO_DEV "create-uio-dev"
103 #define OPT_VFIO_INTR    "vfio-intr"
104
105 #define RTE_EAL_BLACKLIST_SIZE  0x100
106
107 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
108
109 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
110
111 #define HIGHEST_RPL 3
112
113 #define BITS_PER_HEX 4
114
115 /* Allow the application to print its usage message too if set */
116 static rte_usage_hook_t rte_application_usage_hook = NULL;
117
118 TAILQ_HEAD(shared_driver_list, shared_driver);
119
120 /* Definition for shared object drivers. */
121 struct shared_driver {
122         TAILQ_ENTRY(shared_driver) next;
123
124         char    name[PATH_MAX];
125         void*   lib_handle;
126 };
127
128 /* List of external loadable drivers */
129 static struct shared_driver_list solib_list =
130 TAILQ_HEAD_INITIALIZER(solib_list);
131
132 /* early configuration structure, when memory config is not mmapped */
133 static struct rte_mem_config early_mem_config;
134
135 /* define fd variable here, because file needs to be kept open for the
136  * duration of the program, as we hold a write lock on it in the primary proc */
137 static int mem_cfg_fd = -1;
138
139 static struct flock wr_lock = {
140                 .l_type = F_WRLCK,
141                 .l_whence = SEEK_SET,
142                 .l_start = offsetof(struct rte_mem_config, memseg),
143                 .l_len = sizeof(early_mem_config.memseg),
144 };
145
146 /* Address of global and public configuration */
147 static struct rte_config rte_config = {
148                 .mem_config = &early_mem_config,
149 };
150
151 /* internal configuration (per-core) */
152 struct lcore_config lcore_config[RTE_MAX_LCORE];
153
154 /* internal configuration */
155 struct internal_config internal_config;
156
157 /* used by rte_rdtsc() */
158 int rte_cycles_vmware_tsc_map;
159
160 /* Return a pointer to the configuration structure */
161 struct rte_config *
162 rte_eal_get_configuration(void)
163 {
164         return &rte_config;
165 }
166
167 /* parse a sysfs (or other) file containing one integer value */
168 int
169 eal_parse_sysfs_value(const char *filename, unsigned long *val)
170 {
171         FILE *f;
172         char buf[BUFSIZ];
173         char *end = NULL;
174
175         if ((f = fopen(filename, "r")) == NULL) {
176                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
177                         __func__, filename);
178                 return -1;
179         }
180
181         if (fgets(buf, sizeof(buf), f) == NULL) {
182                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
183                         __func__, filename);
184                 fclose(f);
185                 return -1;
186         }
187         *val = strtoul(buf, &end, 0);
188         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
189                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
190                                 __func__, filename);
191                 fclose(f);
192                 return -1;
193         }
194         fclose(f);
195         return 0;
196 }
197
198
199 /* create memory configuration in shared/mmap memory. Take out
200  * a write lock on the memsegs, so we can auto-detect primary/secondary.
201  * This means we never close the file while running (auto-close on exit).
202  * We also don't lock the whole file, so that in future we can use read-locks
203  * on other parts, e.g. memzones, to detect if there are running secondary
204  * processes. */
205 static void
206 rte_eal_config_create(void)
207 {
208         void *rte_mem_cfg_addr;
209         int retval;
210
211         const char *pathname = eal_runtime_config_path();
212
213         if (internal_config.no_shconf)
214                 return;
215
216         /* map the config before hugepage address so that we don't waste a page */
217         if (internal_config.base_virtaddr != 0)
218                 rte_mem_cfg_addr = (void *)
219                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
220                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
221         else
222                 rte_mem_cfg_addr = NULL;
223
224         if (mem_cfg_fd < 0){
225                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
226                 if (mem_cfg_fd < 0)
227                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
228         }
229
230         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
231         if (retval < 0){
232                 close(mem_cfg_fd);
233                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
234         }
235
236         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
237         if (retval < 0){
238                 close(mem_cfg_fd);
239                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
240                                 "process running?\n", pathname);
241         }
242
243         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
244                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
245
246         if (rte_mem_cfg_addr == MAP_FAILED){
247                 rte_panic("Cannot mmap memory for rte_config\n");
248         }
249         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
250         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
251
252         /* store address of the config in the config itself so that secondary
253          * processes could later map the config into this exact location */
254         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
255
256 }
257
258 /* attach to an existing shared memory config */
259 static void
260 rte_eal_config_attach(void)
261 {
262         struct rte_mem_config *mem_config;
263
264         const char *pathname = eal_runtime_config_path();
265
266         if (internal_config.no_shconf)
267                 return;
268
269         if (mem_cfg_fd < 0){
270                 mem_cfg_fd = open(pathname, O_RDWR);
271                 if (mem_cfg_fd < 0)
272                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
273         }
274
275         /* map it as read-only first */
276         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
277                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
278         if (mem_config == MAP_FAILED)
279                 rte_panic("Cannot mmap memory for rte_config\n");
280
281         rte_config.mem_config = mem_config;
282 }
283
284 /* reattach the shared config at exact memory location primary process has it */
285 static void
286 rte_eal_config_reattach(void)
287 {
288         struct rte_mem_config *mem_config;
289         void *rte_mem_cfg_addr;
290
291         if (internal_config.no_shconf)
292                 return;
293
294         /* save the address primary process has mapped shared config to */
295         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
296
297         /* unmap original config */
298         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
299
300         /* remap the config at proper address */
301         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
302                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
303                         mem_cfg_fd, 0);
304         close(mem_cfg_fd);
305         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr)
306                 rte_panic("Cannot mmap memory for rte_config\n");
307
308         rte_config.mem_config = mem_config;
309 }
310
311 /* Detect if we are a primary or a secondary process */
312 static enum rte_proc_type_t
313 eal_proc_type_detect(void)
314 {
315         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
316         const char *pathname = eal_runtime_config_path();
317
318         /* if we can open the file but not get a write-lock we are a secondary
319          * process. NOTE: if we get a file handle back, we keep that open
320          * and don't close it to prevent a race condition between multiple opens */
321         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
322                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
323                 ptype = RTE_PROC_SECONDARY;
324
325         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
326                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
327
328         return ptype;
329 }
330
331 /* Sets up rte_config structure with the pointer to shared memory config.*/
332 static void
333 rte_config_init(void)
334 {
335         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
336                         eal_proc_type_detect() : /* for auto, detect the type */
337                         internal_config.process_type; /* otherwise use what's already set */
338
339         switch (rte_config.process_type){
340         case RTE_PROC_PRIMARY:
341                 rte_eal_config_create();
342                 break;
343         case RTE_PROC_SECONDARY:
344                 rte_eal_config_attach();
345                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
346                 rte_eal_config_reattach();
347                 break;
348         case RTE_PROC_AUTO:
349         case RTE_PROC_INVALID:
350                 rte_panic("Invalid process type\n");
351         }
352 }
353
354 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
355 static void
356 eal_hugedirs_unlock(void)
357 {
358         int i;
359
360         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
361         {
362                 /* skip uninitialized */
363                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
364                         continue;
365                 /* unlock hugepage file */
366                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
367                 close(internal_config.hugepage_info[i].lock_descriptor);
368                 /* reset the field */
369                 internal_config.hugepage_info[i].lock_descriptor = -1;
370         }
371 }
372
373 /* display usage */
374 static void
375 eal_usage(const char *prgname)
376 {
377         printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
378                "[--proc-type primary|secondary|auto] \n\n"
379                "EAL options:\n"
380                "  -c COREMASK  : A hexadecimal bitmask of cores to run on\n"
381                "  -n NUM       : Number of memory channels\n"
382                    "  -v           : Display version information on startup\n"
383                "  -d LIB.so    : add driver (can be used multiple times)\n"
384                "  -m MB        : memory to allocate (see also --"OPT_SOCKET_MEM")\n"
385                "  -r NUM       : force number of memory ranks (don't detect)\n"
386                "  --"OPT_XEN_DOM0" : support application running on Xen Domain0 "
387                            "without hugetlbfs\n"
388                "  --"OPT_SYSLOG"     : set syslog facility\n"
389                "  --"OPT_SOCKET_MEM" : memory to allocate on specific \n"
390                    "                 sockets (use comma separated values)\n"
391                "  --"OPT_HUGE_DIR"   : directory where hugetlbfs is mounted\n"
392                "  --"OPT_PROC_TYPE"  : type of this process\n"
393                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
394                "  --"OPT_PCI_BLACKLIST", -b: add a PCI device in black list.\n"
395                "               Prevent EAL from using this PCI device. The argument\n"
396                "               format is <domain:bus:devid.func>.\n"
397                "  --"OPT_PCI_WHITELIST", -w: add a PCI device in white list.\n"
398                "               Only use the specified PCI devices. The argument format\n"
399                "               is <[domain:]bus:devid.func>. This option can be present\n"
400                "               several times (once per device).\n"
401                "               [NOTE: PCI whitelist cannot be used with -b option]\n"
402                "  --"OPT_VDEV": add a virtual device.\n"
403                "               The argument format is <driver><id>[,key=val,...]\n"
404                "               (ex: --vdev=eth_pcap0,iface=eth2).\n"
405                "  --"OPT_VMWARE_TSC_MAP": use VMware TSC map instead of native RDTSC\n"
406                "  --"OPT_BASE_VIRTADDR": specify base virtual address\n"
407                "  --"OPT_VFIO_INTR": specify desired interrupt mode for VFIO "
408                            "(legacy|msi|msix)\n"
409                "  --"OPT_CREATE_UIO_DEV": create /dev/uioX (usually done by hotplug)\n"
410                "\nEAL options for DEBUG use only:\n"
411                "  --"OPT_NO_HUGE"  : use malloc instead of hugetlbfs\n"
412                "  --"OPT_NO_PCI"   : disable pci\n"
413                "  --"OPT_NO_HPET"  : disable hpet\n"
414                "  --"OPT_NO_SHCONF": no shared config (mmap'd files)\n"
415                "\n",
416                prgname);
417         /* Allow the application to print its usage message too if hook is set */
418         if ( rte_application_usage_hook ) {
419                 printf("===== Application Usage =====\n\n");
420                 rte_application_usage_hook(prgname);
421         }
422 }
423
424 /* Set a per-application usage message */
425 rte_usage_hook_t
426 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
427 {
428         rte_usage_hook_t        old_func;
429
430         /* Will be NULL on the first call to denote the last usage routine. */
431         old_func                                        = rte_application_usage_hook;
432         rte_application_usage_hook      = usage_func;
433
434         return old_func;
435 }
436
437 /*
438  * Parse the coremask given as argument (hexadecimal string) and fill
439  * the global configuration (core role and core count) with the parsed
440  * value.
441  */
442 static int xdigit2val(unsigned char c)
443 {
444         int val;
445         if(isdigit(c))
446                 val = c - '0';
447         else if(isupper(c))
448                 val = c - 'A' + 10;
449         else
450                 val = c - 'a' + 10;
451         return val;
452 }
453 static int
454 eal_parse_coremask(const char *coremask)
455 {
456         struct rte_config *cfg = rte_eal_get_configuration();
457         int i, j, idx = 0 ;
458         unsigned count = 0;
459         char c;
460         int val;
461
462         if (coremask == NULL)
463                 return -1;
464         /* Remove all blank characters ahead and after .
465          * Remove 0x/0X if exists.
466          */
467         while (isblank(*coremask))
468                 coremask++;
469         if (coremask[0] == '0' && ((coremask[1] == 'x')
470                 ||  (coremask[1] == 'X')) )
471                 coremask += 2;
472         i = strnlen(coremask, PATH_MAX);
473         while ((i > 0) && isblank(coremask[i - 1]))
474                 i--;
475         if (i == 0)
476                 return -1;
477
478         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
479                 c = coremask[i];
480                 if (isxdigit(c) == 0) {
481                         /* invalid characters */
482                         return (-1);
483                 }
484                 val = xdigit2val(c);
485                 for(j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++) {
486                         if((1 << j) & val) {
487                                 if (!lcore_config[idx].detected) {
488                                         RTE_LOG(ERR, EAL, "lcore %u "
489                                                 "unavailable\n", idx);
490                                         return -1;
491                                 }
492                                 cfg->lcore_role[idx] = ROLE_RTE;
493                                 if(count == 0)
494                                         cfg->master_lcore = idx;
495                                 count++;
496                         } else  {
497                                 cfg->lcore_role[idx] = ROLE_OFF;
498                         }
499                 }
500         }
501         for(; i >= 0; i--)
502                 if(coremask[i] != '0')
503                         return -1;
504         for(; idx < RTE_MAX_LCORE; idx++)
505                 cfg->lcore_role[idx] = ROLE_OFF;
506         if(count == 0)
507                 return -1;
508         /* Update the count of enabled logical cores of the EAL configuration */
509         cfg->lcore_count = count;
510         return 0;
511 }
512
513 static int
514 eal_parse_syslog(const char *facility)
515 {
516         int i;
517         static struct {
518                 const char *name;
519                 int value;
520         } map[] = {
521                 { "auth", LOG_AUTH },
522                 { "cron", LOG_CRON },
523                 { "daemon", LOG_DAEMON },
524                 { "ftp", LOG_FTP },
525                 { "kern", LOG_KERN },
526                 { "lpr", LOG_LPR },
527                 { "mail", LOG_MAIL },
528                 { "news", LOG_NEWS },
529                 { "syslog", LOG_SYSLOG },
530                 { "user", LOG_USER },
531                 { "uucp", LOG_UUCP },
532                 { "local0", LOG_LOCAL0 },
533                 { "local1", LOG_LOCAL1 },
534                 { "local2", LOG_LOCAL2 },
535                 { "local3", LOG_LOCAL3 },
536                 { "local4", LOG_LOCAL4 },
537                 { "local5", LOG_LOCAL5 },
538                 { "local6", LOG_LOCAL6 },
539                 { "local7", LOG_LOCAL7 },
540                 { NULL, 0 }
541         };
542
543         for (i = 0; map[i].name; i++) {
544                 if (!strcmp(facility, map[i].name)) {
545                         internal_config.syslog_facility = map[i].value;
546                         return 0;
547                 }
548         }
549         return -1;
550 }
551
552 static int
553 eal_parse_socket_mem(char *socket_mem)
554 {
555         char * arg[RTE_MAX_NUMA_NODES];
556         char *end;
557         int arg_num, i, len;
558         uint64_t total_mem = 0;
559
560         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
561         if (len == SOCKET_MEM_STRLEN) {
562                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
563                 return -1;
564         }
565
566         /* all other error cases will be caught later */
567         if (!isdigit(socket_mem[len-1]))
568                 return -1;
569
570         /* split the optarg into separate socket values */
571         arg_num = rte_strsplit(socket_mem, len,
572                         arg, RTE_MAX_NUMA_NODES, ',');
573
574         /* if split failed, or 0 arguments */
575         if (arg_num <= 0)
576                 return -1;
577
578         internal_config.force_sockets = 1;
579
580         /* parse each defined socket option */
581         errno = 0;
582         for (i = 0; i < arg_num; i++) {
583                 end = NULL;
584                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
585
586                 /* check for invalid input */
587                 if ((errno != 0)  ||
588                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
589                         return -1;
590                 internal_config.socket_mem[i] *= 1024ULL;
591                 internal_config.socket_mem[i] *= 1024ULL;
592                 total_mem += internal_config.socket_mem[i];
593         }
594
595         /* check if we have a positive amount of total memory */
596         if (total_mem == 0)
597                 return -1;
598
599         return 0;
600 }
601
602 static int
603 eal_parse_base_virtaddr(const char *arg)
604 {
605         char *end;
606         uint64_t addr;
607
608         errno = 0;
609         addr = strtoull(arg, &end, 16);
610
611         /* check for errors */
612         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
613                 return -1;
614
615         /* make sure we don't exceed 32-bit boundary on 32-bit target */
616 #ifndef RTE_ARCH_X86_64
617         if (addr >= UINTPTR_MAX)
618                 return -1;
619 #endif
620
621         /* align the addr on 2M boundary */
622         internal_config.base_virtaddr = RTE_PTR_ALIGN_CEIL((uintptr_t)addr,
623                                                            RTE_PGSIZE_2M);
624
625         return 0;
626 }
627
628 static int
629 eal_parse_vfio_intr(const char *mode)
630 {
631         unsigned i;
632         static struct {
633                 const char *name;
634                 enum rte_intr_mode value;
635         } map[] = {
636                 { "legacy", RTE_INTR_MODE_LEGACY },
637                 { "msi", RTE_INTR_MODE_MSI },
638                 { "msix", RTE_INTR_MODE_MSIX },
639         };
640
641         for (i = 0; i < RTE_DIM(map); i++) {
642                 if (!strcmp(mode, map[i].name)) {
643                         internal_config.vfio_intr_mode = map[i].value;
644                         return 0;
645                 }
646         }
647         return -1;
648 }
649
650 static inline size_t
651 eal_get_hugepage_mem_size(void)
652 {
653         uint64_t size = 0;
654         unsigned i, j;
655
656         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
657                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
658                 if (hpi->hugedir != NULL) {
659                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
660                                 size += hpi->hugepage_sz * hpi->num_pages[j];
661                         }
662                 }
663         }
664
665         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
666 }
667
668 static enum rte_proc_type_t
669 eal_parse_proc_type(const char *arg)
670 {
671         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
672                 return RTE_PROC_PRIMARY;
673         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
674                 return RTE_PROC_SECONDARY;
675         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
676                 return RTE_PROC_AUTO;
677
678         return RTE_PROC_INVALID;
679 }
680
681 /* Parse the argument given in the command line of the application */
682 static int
683 eal_parse_args(int argc, char **argv)
684 {
685         int opt, ret, i;
686         char **argvopt;
687         int option_index;
688         int coremask_ok = 0;
689         char *prgname = argv[0];
690         static struct option lgopts[] = {
691                 {OPT_NO_HUGE, 0, 0, 0},
692                 {OPT_NO_PCI, 0, 0, 0},
693                 {OPT_NO_HPET, 0, 0, 0},
694                 {OPT_VMWARE_TSC_MAP, 0, 0, 0},
695                 {OPT_HUGE_DIR, 1, 0, 0},
696                 {OPT_NO_SHCONF, 0, 0, 0},
697                 {OPT_PROC_TYPE, 1, 0, 0},
698                 {OPT_FILE_PREFIX, 1, 0, 0},
699                 {OPT_SOCKET_MEM, 1, 0, 0},
700                 {OPT_PCI_WHITELIST, 1, 0, 0},
701                 {OPT_PCI_BLACKLIST, 1, 0, 0},
702                 {OPT_VDEV, 1, 0, 0},
703                 {OPT_SYSLOG, 1, NULL, 0},
704                 {OPT_VFIO_INTR, 1, NULL, 0},
705                 {OPT_BASE_VIRTADDR, 1, 0, 0},
706                 {OPT_XEN_DOM0, 0, 0, 0},
707                 {OPT_CREATE_UIO_DEV, 1, NULL, 0},
708                 {0, 0, 0, 0}
709         };
710         struct shared_driver *solib;
711
712         argvopt = argv;
713
714         internal_config.memory = 0;
715         internal_config.force_nrank = 0;
716         internal_config.force_nchannel = 0;
717         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
718         internal_config.hugepage_dir = NULL;
719         internal_config.force_sockets = 0;
720         internal_config.syslog_facility = LOG_DAEMON;
721         internal_config.xen_dom0_support = 0;
722         /* if set to NONE, interrupt mode is determined automatically */
723         internal_config.vfio_intr_mode = RTE_INTR_MODE_NONE;
724 #ifdef RTE_LIBEAL_USE_HPET
725         internal_config.no_hpet = 0;
726 #else
727         internal_config.no_hpet = 1;
728 #endif
729         /* zero out the NUMA config */
730         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
731                 internal_config.socket_mem[i] = 0;
732
733         /* zero out hugedir descriptors */
734         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
735                 internal_config.hugepage_info[i].lock_descriptor = -1;
736
737         internal_config.vmware_tsc_map = 0;
738         internal_config.base_virtaddr = 0;
739
740         while ((opt = getopt_long(argc, argvopt, "b:w:c:d:m:n:r:v",
741                                   lgopts, &option_index)) != EOF) {
742
743                 switch (opt) {
744                 /* blacklist */
745                 case 'b':
746                         if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
747                                         optarg) < 0) {
748                                 eal_usage(prgname);
749                                 return (-1);
750                         }
751                         break;
752                 /* whitelist */
753                 case 'w':
754                         if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
755                                         optarg) < 0) {
756                                 eal_usage(prgname);
757                                 return -1;
758                         }
759                         break;
760                 /* coremask */
761                 case 'c':
762                         if (eal_parse_coremask(optarg) < 0) {
763                                 RTE_LOG(ERR, EAL, "invalid coremask\n");
764                                 eal_usage(prgname);
765                                 return -1;
766                         }
767                         coremask_ok = 1;
768                         break;
769                 /* force loading of external driver */
770                 case 'd':
771                         solib = malloc(sizeof(*solib));
772                         if (solib == NULL) {
773                                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
774                                 return -1;
775                         }
776                         memset(solib, 0, sizeof(*solib));
777                         strncpy(solib->name, optarg, PATH_MAX-1);
778                         solib->name[PATH_MAX-1] = 0;
779                         TAILQ_INSERT_TAIL(&solib_list, solib, next);
780                         break;
781                 /* size of memory */
782                 case 'm':
783                         internal_config.memory = atoi(optarg);
784                         internal_config.memory *= 1024ULL;
785                         internal_config.memory *= 1024ULL;
786                         break;
787                 /* force number of channels */
788                 case 'n':
789                         internal_config.force_nchannel = atoi(optarg);
790                         if (internal_config.force_nchannel == 0 ||
791                             internal_config.force_nchannel > 4) {
792                                 RTE_LOG(ERR, EAL, "invalid channel number\n");
793                                 eal_usage(prgname);
794                                 return -1;
795                         }
796                         break;
797                 /* force number of ranks */
798                 case 'r':
799                         internal_config.force_nrank = atoi(optarg);
800                         if (internal_config.force_nrank == 0 ||
801                             internal_config.force_nrank > 16) {
802                                 RTE_LOG(ERR, EAL, "invalid rank number\n");
803                                 eal_usage(prgname);
804                                 return -1;
805                         }
806                         break;
807                 case 'v':
808                         /* since message is explicitly requested by user, we
809                          * write message at highest log level so it can always be seen
810                          * even if info or warning messages are disabled */
811                         RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
812                         break;
813
814                 /* long options */
815                 case 0:
816                         if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
817                                 internal_config.no_hugetlbfs = 1;
818                         }
819                         if (!strcmp(lgopts[option_index].name, OPT_XEN_DOM0)) {
820                 #ifdef RTE_LIBRTE_XEN_DOM0
821                                 internal_config.xen_dom0_support = 1;
822                 #else
823                                 RTE_LOG(ERR, EAL, "Can't support DPDK app "
824                                         "running on Dom0, please configure"
825                                         " RTE_LIBRTE_XEN_DOM0=y\n");
826                                 return -1;
827                 #endif
828                         }
829                         else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
830                                 internal_config.no_pci = 1;
831                         }
832                         else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
833                                 internal_config.no_hpet = 1;
834                         }
835                         else if (!strcmp(lgopts[option_index].name, OPT_VMWARE_TSC_MAP)) {
836                                 internal_config.vmware_tsc_map = 1;
837                         }
838                         else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
839                                 internal_config.no_shconf = 1;
840                         }
841                         else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
842                                 internal_config.hugepage_dir = optarg;
843                         }
844                         else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
845                                 internal_config.process_type = eal_parse_proc_type(optarg);
846                         }
847                         else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
848                                 internal_config.hugefile_prefix = optarg;
849                         }
850                         else if (!strcmp(lgopts[option_index].name, OPT_SOCKET_MEM)) {
851                                 if (eal_parse_socket_mem(optarg) < 0) {
852                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
853                                                         OPT_SOCKET_MEM "\n");
854                                         eal_usage(prgname);
855                                         return -1;
856                                 }
857                         }
858                         else if (!strcmp(lgopts[option_index].name, OPT_USE_DEVICE)) {
859                                 printf("The --use-device option is deprecated, please use\n"
860                                         "--whitelist or --vdev instead.\n");
861                                 eal_usage(prgname);
862                                 return -1;
863                         }
864                         else if (!strcmp(lgopts[option_index].name, OPT_PCI_BLACKLIST)) {
865                                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
866                                                 optarg) < 0) {
867                                         eal_usage(prgname);
868                                         return -1;
869                                 }
870                         }
871                         else if (!strcmp(lgopts[option_index].name, OPT_PCI_WHITELIST)) {
872                                 if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
873                                                 optarg) < 0) {
874                                         eal_usage(prgname);
875                                         return -1;
876                                 }
877                         }
878                         else if (!strcmp(lgopts[option_index].name, OPT_VDEV)) {
879                                 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL,
880                                                 optarg) < 0) {
881                                         eal_usage(prgname);
882                                         return -1;
883                                 }
884                         }
885                         else if (!strcmp(lgopts[option_index].name, OPT_SYSLOG)) {
886                                 if (eal_parse_syslog(optarg) < 0) {
887                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
888                                                         OPT_SYSLOG "\n");
889                                         eal_usage(prgname);
890                                         return -1;
891                                 }
892                         }
893                         else if (!strcmp(lgopts[option_index].name, OPT_BASE_VIRTADDR)) {
894                                 if (eal_parse_base_virtaddr(optarg) < 0) {
895                                         RTE_LOG(ERR, EAL, "invalid parameter for --"
896                                                         OPT_BASE_VIRTADDR "\n");
897                                         eal_usage(prgname);
898                                         return -1;
899                                 }
900                         }
901                         else if (!strcmp(lgopts[option_index].name, OPT_VFIO_INTR)) {
902                                 if (eal_parse_vfio_intr(optarg) < 0) {
903                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
904                                                         OPT_VFIO_INTR "\n");
905                                         eal_usage(prgname);
906                                         return -1;
907                                 }
908                         }
909                         else if (!strcmp(lgopts[option_index].name, OPT_CREATE_UIO_DEV)) {
910                                 internal_config.create_uio_dev = 1;
911                         }
912                         break;
913
914                 default:
915                         eal_usage(prgname);
916                         return -1;
917                 }
918         }
919
920         /* sanity checks */
921         if (!coremask_ok) {
922                 RTE_LOG(ERR, EAL, "coremask not specified\n");
923                 eal_usage(prgname);
924                 return -1;
925         }
926         if (internal_config.process_type == RTE_PROC_AUTO){
927                 internal_config.process_type = eal_proc_type_detect();
928         }
929         if (internal_config.process_type == RTE_PROC_INVALID){
930                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
931                 eal_usage(prgname);
932                 return -1;
933         }
934         if (internal_config.process_type == RTE_PROC_PRIMARY &&
935                         internal_config.force_nchannel == 0) {
936                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
937                 eal_usage(prgname);
938                 return -1;
939         }
940         if (index(internal_config.hugefile_prefix,'%') != NULL){
941                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
942                 eal_usage(prgname);
943                 return -1;
944         }
945         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
946                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
947                                 "at the same time\n");
948                 eal_usage(prgname);
949                 return -1;
950         }
951         /* --no-huge doesn't make sense with either -m or --socket-mem */
952         if (internal_config.no_hugetlbfs &&
953                         (internal_config.memory > 0 ||
954                                         internal_config.force_sockets == 1)) {
955                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
956                                 "together with --no-huge!\n");
957                 eal_usage(prgname);
958                 return -1;
959         }
960         /* --xen-dom0 doesn't make sense with --socket-mem */
961         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
962                 RTE_LOG(ERR, EAL, "Options --socket-mem cannot be specified "
963                                         "together with --xen_dom0!\n");
964                 eal_usage(prgname);
965                 return -1;
966         }
967
968         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
969                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
970                 RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
971                         "[-w] options cannot be used at the same time\n");
972                 eal_usage(prgname);
973                 return -1;
974         }
975
976         if (optind >= 0)
977                 argv[optind-1] = prgname;
978
979         /* if no memory amounts were requested, this will result in 0 and
980          * will be overriden later, right after eal_hugepage_info_init() */
981         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
982                 internal_config.memory += internal_config.socket_mem[i];
983
984         ret = optind-1;
985         optind = 0; /* reset getopt lib */
986         return ret;
987 }
988
989 static void
990 eal_check_mem_on_local_socket(void)
991 {
992         const struct rte_memseg *ms;
993         int i, socket_id;
994
995         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
996
997         ms = rte_eal_get_physmem_layout();
998
999         for (i = 0; i < RTE_MAX_MEMSEG; i++)
1000                 if (ms[i].socket_id == socket_id &&
1001                                 ms[i].len > 0)
1002                         return;
1003
1004         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
1005                         "memory on local socket!\n");
1006 }
1007
1008 static int
1009 sync_func(__attribute__((unused)) void *arg)
1010 {
1011         return 0;
1012 }
1013
1014 inline static void
1015 rte_eal_mcfg_complete(void)
1016 {
1017         /* ALL shared mem_config related INIT DONE */
1018         if (rte_config.process_type == RTE_PROC_PRIMARY)
1019                 rte_config.mem_config->magic = RTE_MAGIC;
1020 }
1021
1022 /*
1023  * Request iopl privilege for all RPL, returns 0 on success
1024  */
1025 static int
1026 rte_eal_iopl_init(void)
1027 {
1028         return iopl(HIGHEST_RPL);
1029 }
1030
1031 /* Launch threads, called at application init(). */
1032 int
1033 rte_eal_init(int argc, char **argv)
1034 {
1035         int i, fctret, ret;
1036         pthread_t thread_id;
1037         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
1038         struct shared_driver *solib = NULL;
1039         const char *logid;
1040
1041         if (!rte_atomic32_test_and_set(&run_once))
1042                 return -1;
1043
1044         logid = strrchr(argv[0], '/');
1045         logid = strdup(logid ? logid + 1: argv[0]);
1046
1047         thread_id = pthread_self();
1048
1049         if (rte_eal_log_early_init() < 0)
1050                 rte_panic("Cannot init early logs\n");
1051
1052         if (rte_eal_cpu_init() < 0)
1053                 rte_panic("Cannot detect lcores\n");
1054
1055         fctret = eal_parse_args(argc, argv);
1056         if (fctret < 0)
1057                 exit(1);
1058
1059         if (internal_config.no_hugetlbfs == 0 &&
1060                         internal_config.process_type != RTE_PROC_SECONDARY &&
1061                         internal_config.xen_dom0_support == 0 &&
1062                         eal_hugepage_info_init() < 0)
1063                 rte_panic("Cannot get hugepage information\n");
1064
1065         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1066                 if (internal_config.no_hugetlbfs)
1067                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1068                 else
1069                         internal_config.memory = eal_get_hugepage_mem_size();
1070         }
1071
1072         if (internal_config.vmware_tsc_map == 1) {
1073 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1074                 rte_cycles_vmware_tsc_map = 1;
1075                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1076                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1077 #else
1078                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1079                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1080 #endif
1081         }
1082
1083         rte_srand(rte_rdtsc());
1084
1085         rte_config_init();
1086
1087         if (rte_eal_iopl_init() == 0)
1088                 rte_config.flags |= EAL_FLG_HIGH_IOPL;
1089
1090         if (rte_eal_pci_init() < 0)
1091                 rte_panic("Cannot init PCI\n");
1092
1093 #ifdef RTE_LIBRTE_IVSHMEM
1094         if (rte_eal_ivshmem_init() < 0)
1095                 rte_panic("Cannot init IVSHMEM\n");
1096 #endif
1097
1098         if (rte_eal_memory_init() < 0)
1099                 rte_panic("Cannot init memory\n");
1100
1101         /* the directories are locked during eal_hugepage_info_init */
1102         eal_hugedirs_unlock();
1103
1104         if (rte_eal_memzone_init() < 0)
1105                 rte_panic("Cannot init memzone\n");
1106
1107         if (rte_eal_tailqs_init() < 0)
1108                 rte_panic("Cannot init tail queues for objects\n");
1109
1110 #ifdef RTE_LIBRTE_IVSHMEM
1111         if (rte_eal_ivshmem_obj_init() < 0)
1112                 rte_panic("Cannot init IVSHMEM objects\n");
1113 #endif
1114
1115         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
1116                 rte_panic("Cannot init logs\n");
1117
1118         if (rte_eal_alarm_init() < 0)
1119                 rte_panic("Cannot init interrupt-handling thread\n");
1120
1121         if (rte_eal_intr_init() < 0)
1122                 rte_panic("Cannot init interrupt-handling thread\n");
1123
1124         if (rte_eal_timer_init() < 0)
1125                 rte_panic("Cannot init HPET or TSC timers\n");
1126
1127         eal_check_mem_on_local_socket();
1128
1129         rte_eal_mcfg_complete();
1130
1131         TAILQ_FOREACH(solib, &solib_list, next) {
1132                 RTE_LOG(INFO, EAL, "open shared lib %s\n", solib->name);
1133                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
1134                 if (solib->lib_handle == NULL)
1135                         RTE_LOG(WARNING, EAL, "%s\n", dlerror());
1136         }
1137
1138         eal_thread_init_master(rte_config.master_lcore);
1139
1140         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
1141                 rte_config.master_lcore, (int)thread_id);
1142
1143         if (rte_eal_dev_init(PMD_INIT_PRE_PCI_PROBE) < 0)
1144                 rte_panic("Cannot init pmd devices\n");
1145
1146         RTE_LCORE_FOREACH_SLAVE(i) {
1147
1148                 /*
1149                  * create communication pipes between master thread
1150                  * and children
1151                  */
1152                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1153                         rte_panic("Cannot create pipe\n");
1154                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1155                         rte_panic("Cannot create pipe\n");
1156
1157                 lcore_config[i].state = WAIT;
1158
1159                 /* create a thread for each lcore */
1160                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1161                                      eal_thread_loop, NULL);
1162                 if (ret != 0)
1163                         rte_panic("Cannot create thread\n");
1164         }
1165
1166         /*
1167          * Launch a dummy function on all slave lcores, so that master lcore
1168          * knows they are all ready when this function returns.
1169          */
1170         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1171         rte_eal_mp_wait_lcore();
1172
1173         /* Probe & Initialize PCI devices */
1174         if (rte_eal_pci_probe())
1175                         rte_panic("Cannot probe PCI\n");
1176
1177         /* Initialize any outstanding devices */
1178         if (rte_eal_dev_init(PMD_INIT_POST_PCI_PROBE) < 0)
1179                 rte_panic("Cannot init pmd devices\n");
1180
1181         return fctret;
1182 }
1183
1184 /* get core role */
1185 enum rte_lcore_role_t
1186 rte_eal_lcore_role(unsigned lcore_id)
1187 {
1188         return (rte_config.lcore_role[lcore_id]);
1189 }
1190
1191 enum rte_proc_type_t
1192 rte_eal_process_type(void)
1193 {
1194         return (rte_config.process_type);
1195 }
1196
1197 int rte_eal_has_hugepages(void)
1198 {
1199         return ! internal_config.no_hugetlbfs;
1200 }