remove version in all files
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
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 <getopt.h>
43 #include <fcntl.h>
44 #include <stddef.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <errno.h>
48 #include <sys/mman.h>
49 #include <sys/queue.h>
50
51 #include <rte_common.h>
52 #include <rte_debug.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_launch.h>
56 #include <rte_tailq.h>
57 #include <rte_eal.h>
58 #include <rte_per_lcore.h>
59 #include <rte_lcore.h>
60 #include <rte_log.h>
61 #include <rte_random.h>
62 #include <rte_cycles.h>
63 #include <rte_string_fns.h>
64 #include <rte_cpuflags.h>
65 #include <rte_interrupts.h>
66 #include <rte_pci.h>
67 #include <rte_common.h>
68 #include <rte_version.h>
69
70 #include "eal_private.h"
71 #include "eal_thread.h"
72 #include "eal_internal_cfg.h"
73 #include "eal_fs_paths.h"
74 #include "eal_hugepages.h"
75
76 #define OPT_HUGE_DIR    "huge-dir"
77 #define OPT_PROC_TYPE   "proc-type"
78 #define OPT_NO_SHCONF   "no-shconf"
79 #define OPT_NO_HPET     "no-hpet"
80 #define OPT_NO_PCI      "no-pci"
81 #define OPT_NO_HUGE     "no-huge"
82 #define OPT_FILE_PREFIX "file-prefix"
83
84 #define RTE_EAL_BLACKLIST_SIZE  0x100
85
86 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
87
88 #define GET_BLACKLIST_FIELD(in, fd, lim, dlm)                   \
89 {                                                               \
90         unsigned long val;                                      \
91         char *end;                                              \
92         errno = 0;                                              \
93         val = strtoul((in), &end, 16);                          \
94         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
95                 return (-EINVAL);                               \
96         (fd) = (typeof (fd))val;                                \
97         (in) = end + 1;                                         \
98 }
99
100 /* early configuration structure, when memory config is not mmapped */
101 static struct rte_mem_config early_mem_config;
102
103 /* define fd variable here, because file needs to be kept open for the
104  * duration of the program, as we hold a write lock on it in the primary proc */
105 static int mem_cfg_fd = -1;
106
107 static struct flock wr_lock = {
108                 .l_type = F_WRLCK,
109                 .l_whence = SEEK_SET,
110                 .l_start = offsetof(struct rte_mem_config, memseg),
111                 .l_len = sizeof(early_mem_config.memseg),
112 };
113
114 /* Address of global and public configuration */
115 static struct rte_config rte_config = {
116                 .mem_config = &early_mem_config,
117 };
118
119 static struct rte_pci_addr eal_dev_blacklist[RTE_EAL_BLACKLIST_SIZE];
120
121 /* internal configuration (per-core) */
122 struct lcore_config lcore_config[RTE_MAX_LCORE];
123
124 /* internal configuration */
125 struct internal_config internal_config;
126
127 /* Return a pointer to the configuration structure */
128 struct rte_config *
129 rte_eal_get_configuration(void)
130 {
131         return &rte_config;
132 }
133
134 /* create memory configuration in shared/mmap memory. Take out
135  * a write lock on the memsegs, so we can auto-detect primary/secondary.
136  * This means we never close the file while running (auto-close on exit).
137  * We also don't lock the whole file, so that in future we can use read-locks
138  * on other parts, e.g. memzones, to detect if there are running secondary
139  * processes. */
140 static void
141 rte_eal_config_create(void)
142 {
143         void *rte_mem_cfg_addr;
144         int retval;
145
146         const char *pathname = eal_runtime_config_path();
147
148         if (internal_config.no_shconf)
149                 return;
150
151         if (mem_cfg_fd < 0){
152                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
153                 if (mem_cfg_fd < 0)
154                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
155         }
156
157         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
158         if (retval < 0){
159                 close(mem_cfg_fd);
160                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
161         }
162
163         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
164         if (retval < 0){
165                 close(mem_cfg_fd);
166                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
167                                 "process running?\n", pathname);
168         }
169
170         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
171                            PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
172
173         if (rte_mem_cfg_addr == MAP_FAILED){
174                 rte_panic("Cannot mmap memory for rte_config\n");
175         }
176         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
177         memcpy(rte_config.mem_config, &early_mem_config,
178                         sizeof(early_mem_config));
179 }
180
181 /* attach to an existing shared memory config */
182 static void
183 rte_eal_config_attach(void)
184 {
185         void *rte_mem_cfg_addr;
186         const char *pathname = eal_runtime_config_path();
187
188         if (internal_config.no_shconf)
189                 return;
190
191         if (mem_cfg_fd < 0){
192                 mem_cfg_fd = open(pathname, O_RDONLY);
193                 if (mem_cfg_fd < 0)
194                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
195         }
196
197         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config), PROT_READ,
198                         MAP_SHARED, mem_cfg_fd, 0);
199         close(mem_cfg_fd);
200         if (rte_mem_cfg_addr == MAP_FAILED)
201                 rte_panic("Cannot mmap memory for rte_config\n");
202
203         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
204 }
205
206 /* Detect if we are a primary or a secondary process */
207 static enum rte_proc_type_t
208 eal_proc_type_detect(void)
209 {
210         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
211         const char *pathname = eal_runtime_config_path();
212
213         /* if we can open the file but not get a write-lock we are a secondary
214          * process. NOTE: if we get a file handle back, we keep that open
215          * and don't close it to prevent a race condition between multiple opens */
216         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
217                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
218                 ptype = RTE_PROC_SECONDARY;
219
220         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
221                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
222
223         return ptype;
224 }
225
226 /* Sets up rte_config structure with the pointer to shared memory config.*/
227 static void
228 rte_config_init(void)
229 {
230         /* set the magic in configuration structure */
231         rte_config.magic = RTE_MAGIC;
232         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
233                         eal_proc_type_detect() : /* for auto, detect the type */
234                         internal_config.process_type; /* otherwise use what's already set */
235
236         switch (rte_config.process_type){
237         case RTE_PROC_PRIMARY:
238                 rte_eal_config_create();
239                 break;
240         case RTE_PROC_SECONDARY:
241                 rte_eal_config_attach();
242                 break;
243         case RTE_PROC_AUTO:
244         case RTE_PROC_INVALID:
245                 rte_panic("Invalid process type\n");
246         }
247 }
248
249 /* display usage */
250 static void
251 eal_usage(const char *prgname)
252 {
253         printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
254                "[--proc-type primary|secondary|auto] \n\n"
255                "EAL options:\n"
256                "  -c COREMASK: A hexadecimal bitmask of cores to run on\n"
257                "  -n NUM     : Number of memory channels\n"
258                    "  -v         : Display version information on startup\n"
259                "  -b <domain:bus:devid.func>: to prevent EAL from using specified PCI device\n"
260                "               (multiple -b options are alowed)\n"
261                "  -m MB      : memory to allocate (default = size of hugemem)\n"
262                "  -r NUM     : force number of memory ranks (don't detect)\n"
263                "  --"OPT_HUGE_DIR" : directory where hugetlbfs is mounted\n"
264                "  --"OPT_PROC_TYPE": type of this process\n"
265                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
266                "\nEAL options for DEBUG use only:\n"
267                "  --"OPT_NO_HUGE"  : use malloc instead of hugetlbfs\n"
268                "  --"OPT_NO_PCI"   : disable pci\n"
269                "  --"OPT_NO_HPET"  : disable hpet\n"
270                "  --"OPT_NO_SHCONF": no shared config (mmap'd files)\n\n",
271                prgname);
272 }
273
274 /*
275  * Parse the coremask given as argument (hexadecimal string) and fill
276  * the global configuration (core role and core count) with the parsed
277  * value.
278  */
279 static int
280 eal_parse_coremask(const char *coremask)
281 {
282         struct rte_config *cfg = rte_eal_get_configuration();
283         unsigned i;
284         char *end = NULL;
285         unsigned long long cm;
286         unsigned count = 0;
287
288         /* parse hexadecimal string */
289         cm = strtoull(coremask, &end, 16);
290         if ((coremask[0] == '\0') || (end == NULL) || (*end != '\0') || (cm == 0))
291                 return -1;
292
293         RTE_LOG(DEBUG, EAL, "coremask set to %llx\n", cm);
294         /* set core role and core count */
295         for (i = 0; i < RTE_MAX_LCORE; i++) {
296                 if ((1ULL << i) & cm) {
297                         if (count == 0)
298                                 cfg->master_lcore = i;
299                         cfg->lcore_role[i] = ROLE_RTE;
300                         count++;
301                 }
302                 else {
303                         cfg->lcore_role[i] = ROLE_OFF;
304                 }
305         }
306         return 0;
307 }
308
309 static inline uint64_t
310 eal_get_hugepage_mem_size(void)
311 {
312         uint64_t size = 0;
313         unsigned i;
314
315         for (i = 0; i < internal_config.num_hugepage_sizes; i++){
316                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
317                 if (hpi->hugedir != NULL)
318                         size += hpi->hugepage_sz * hpi->num_pages;
319         }
320
321         return (size);
322 }
323
324 static enum rte_proc_type_t
325 eal_parse_proc_type(const char *arg)
326 {
327         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
328                 return RTE_PROC_PRIMARY;
329         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
330                 return RTE_PROC_SECONDARY;
331         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
332                 return RTE_PROC_AUTO;
333
334         return RTE_PROC_INVALID;
335 }
336
337 static int
338 eal_parse_blacklist(const char *input,  struct rte_pci_addr *dev2bl)
339 {
340         GET_BLACKLIST_FIELD(input, dev2bl->domain, UINT16_MAX, ':');
341         GET_BLACKLIST_FIELD(input, dev2bl->bus, UINT8_MAX, ':');
342         GET_BLACKLIST_FIELD(input, dev2bl->devid, UINT8_MAX, '.');
343         GET_BLACKLIST_FIELD(input, dev2bl->function, UINT8_MAX, 0);
344         return (0);
345 }
346
347 static ssize_t
348 eal_parse_blacklist_opt(const char *optarg, size_t idx)
349 {
350         if (idx >= sizeof (eal_dev_blacklist) / sizeof (eal_dev_blacklist[0])) {
351                 RTE_LOG(ERR, EAL,
352                     "%s - too many devices to blacklist...\n",
353                     optarg);
354                 return (-EINVAL);
355         } else if (eal_parse_blacklist(optarg, eal_dev_blacklist + idx) != 0) {
356                 RTE_LOG(ERR, EAL,
357                     "%s - invalid device to blacklist...\n",
358                     optarg);
359                 return (-EINVAL);
360         }
361
362         idx += 1;
363         return (idx);
364 }
365
366
367 /* Parse the argument given in the command line of the application */
368 static int
369 eal_parse_args(int argc, char **argv)
370 {
371         int opt, ret;
372         char **argvopt;
373         int option_index;
374         int coremask_ok = 0;
375         ssize_t blacklist_index = 0;;
376         char *prgname = argv[0];
377         static struct option lgopts[] = {
378                 {OPT_NO_HUGE, 0, 0, 0},
379                 {OPT_NO_PCI, 0, 0, 0},
380                 {OPT_NO_HPET, 0, 0, 0},
381                 {OPT_HUGE_DIR, 1, 0, 0},
382                 {OPT_NO_SHCONF, 0, 0, 0},
383                 {OPT_PROC_TYPE, 1, 0, 0},
384                 {OPT_FILE_PREFIX, 1, 0, 0},
385                 {0, 0, 0, 0}
386         };
387
388         argvopt = argv;
389
390         internal_config.memory = 0;
391         internal_config.force_nrank = 0;
392         internal_config.force_nchannel = 0;
393         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
394         internal_config.hugepage_dir = NULL;
395 #ifdef RTE_LIBEAL_USE_HPET
396         internal_config.no_hpet = 0;
397 #else
398         internal_config.no_hpet = 1;
399 #endif
400
401         while ((opt = getopt_long(argc, argvopt, "b:c:m:n:r:v",
402                                   lgopts, &option_index)) != EOF) {
403
404                 switch (opt) {
405                 /* blacklist */
406                 case 'b':
407                         if ((blacklist_index = eal_parse_blacklist_opt(optarg,
408                             blacklist_index)) < 0) {
409                                 eal_usage(prgname);
410                                 return (-1);
411                         }
412                         break;
413                 /* coremask */
414                 case 'c':
415                         if (eal_parse_coremask(optarg) < 0) {
416                                 RTE_LOG(ERR, EAL, "invalid coremask\n");
417                                 eal_usage(prgname);
418                                 return -1;
419                         }
420                         coremask_ok = 1;
421                         break;
422                 /* size of memory */
423                 case 'm':
424                         internal_config.memory = atoi(optarg);
425                         internal_config.memory *= 1024ULL;
426                         internal_config.memory *= 1024ULL;
427                         break;
428                 /* force number of channels */
429                 case 'n':
430                         internal_config.force_nchannel = atoi(optarg);
431                         if (internal_config.force_nchannel == 0 ||
432                             internal_config.force_nchannel > 4) {
433                                 RTE_LOG(ERR, EAL, "invalid channel number\n");
434                                 eal_usage(prgname);
435                                 return -1;
436                         }
437                         break;
438                 /* force number of ranks */
439                 case 'r':
440                         internal_config.force_nrank = atoi(optarg);
441                         if (internal_config.force_nrank == 0 ||
442                             internal_config.force_nrank > 16) {
443                                 RTE_LOG(ERR, EAL, "invalid rank number\n");
444                                 eal_usage(prgname);
445                                 return -1;
446                         }
447                         break;
448                 case 'v':
449                         /* since message is explicitly requested by user, we
450                          * write message at highest log level so it can always be seen
451                          * even if info or warning messages are disabled */
452                         RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
453                         break;
454
455                 /* long options */
456                 case 0:
457                         if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
458                                 internal_config.no_hugetlbfs = 1;
459                         }
460                         else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
461                                 internal_config.no_pci = 1;
462                         }
463                         else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
464                                 internal_config.no_hpet = 1;
465                         }
466                         else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
467                                 internal_config.no_shconf = 1;
468                         }
469                         else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
470                                 internal_config.hugepage_dir = optarg;
471                         }
472                         else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
473                                 internal_config.process_type = eal_parse_proc_type(optarg);
474                         }
475                         else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
476                                 internal_config.hugefile_prefix = optarg;
477                         }
478                         break;
479
480                 default:
481                         eal_usage(prgname);
482                         return -1;
483                 }
484         }
485
486         /* sanity checks */
487         if (!coremask_ok) {
488                 RTE_LOG(ERR, EAL, "coremask not specified\n");
489                 eal_usage(prgname);
490                 return -1;
491         }
492         if (internal_config.process_type == RTE_PROC_AUTO){
493                 internal_config.process_type = eal_proc_type_detect();
494         }
495         if (internal_config.process_type == RTE_PROC_INVALID){
496                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
497                 eal_usage(prgname);
498                 return -1;
499         }
500         if (internal_config.process_type == RTE_PROC_PRIMARY &&
501                         internal_config.force_nchannel == 0) {
502                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
503                 eal_usage(prgname);
504                 return -1;
505         }
506         if (index(internal_config.hugefile_prefix,'%') != NULL){
507                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
508                 eal_usage(prgname);
509                 return -1;
510         }
511
512         if (blacklist_index > 0)
513                 rte_eal_pci_set_blacklist(eal_dev_blacklist, blacklist_index);
514
515         if (optind >= 0)
516                 argv[optind-1] = prgname;
517
518         ret = optind-1;
519         optind = 0; /* reset getopt lib */
520         return ret;
521 }
522
523 /* Launch threads, called at application init(). */
524 int
525 rte_eal_init(int argc, char **argv)
526 {
527         int i, fctret, ret;
528         pthread_t thread_id;
529
530         thread_id = pthread_self();
531
532         if (rte_eal_log_early_init() < 0)
533                 rte_panic("Cannot init early logs\n");
534
535         fctret = eal_parse_args(argc, argv);
536         if (fctret < 0)
537                 exit(1);
538
539         if (eal_hugepage_info_init() < 0)
540                 rte_panic("Cannot get hugepage information\n");
541
542         if (internal_config.memory == 0) {
543                 if (internal_config.no_hugetlbfs)
544                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
545                 else
546                         internal_config.memory = eal_get_hugepage_mem_size();
547         }
548
549         rte_srand(rte_rdtsc());
550         rte_config_init();
551
552         if (rte_eal_cpu_init() < 0)
553                 rte_panic("Cannot detect lcores\n");
554
555         if (rte_eal_memory_init() < 0)
556                 rte_panic("Cannot init memory\n");
557
558         if (rte_eal_memzone_init() < 0)
559                 rte_panic("Cannot init memzone\n");
560
561         if (rte_eal_tailqs_init() < 0)
562                 rte_panic("Cannot init tail queues for objects\n");
563
564         if (rte_eal_log_init() < 0)
565                 rte_panic("Cannot init logs\n");
566
567         if (rte_eal_alarm_init() < 0)
568                 rte_panic("Cannot init interrupt-handling thread\n");
569
570         if (rte_eal_intr_init() < 0)
571                 rte_panic("Cannot init interrupt-handling thread\n");
572
573         if (rte_eal_hpet_init() < 0)
574                 rte_panic("Cannot init HPET\n");
575
576         if (rte_eal_pci_init() < 0)
577                 rte_panic("Cannot init PCI\n");
578
579         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
580                 rte_config.master_lcore, (int)thread_id);
581
582         RTE_LCORE_FOREACH_SLAVE(i) {
583
584                 /*
585                  * create communication pipes between master thread
586                  * and children
587                  */
588                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
589                         rte_panic("Cannot create pipe\n");
590                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
591                         rte_panic("Cannot create pipe\n");
592
593                 lcore_config[i].state = WAIT;
594
595                 /* create a thread for each lcore */
596                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
597                                      eal_thread_loop, NULL);
598                 if (ret != 0)
599                         rte_panic("Cannot create thread\n");
600         }
601
602         eal_thread_init_master(rte_config.master_lcore);
603
604         return fctret;
605 }
606
607 /* get core role */
608 enum rte_lcore_role_t
609 rte_eal_lcore_role(unsigned lcore_id)
610 {
611         return (rte_config.lcore_role[lcore_id]);
612 }
613
614 enum rte_proc_type_t
615 rte_eal_process_type(void)
616 {
617         return (rte_config.process_type);
618 }
619