eal: merge bsd and linux common options parsing
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 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 <stddef.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <errno.h>
49 #include <sys/mman.h>
50 #include <sys/queue.h>
51
52 #include <rte_common.h>
53 #include <rte_debug.h>
54 #include <rte_memory.h>
55 #include <rte_memzone.h>
56 #include <rte_launch.h>
57 #include <rte_tailq.h>
58 #include <rte_eal.h>
59 #include <rte_eal_memconfig.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
62 #include <rte_log.h>
63 #include <rte_random.h>
64 #include <rte_cycles.h>
65 #include <rte_string_fns.h>
66 #include <rte_cpuflags.h>
67 #include <rte_interrupts.h>
68 #include <rte_pci.h>
69 #include <rte_dev.h>
70 #include <rte_devargs.h>
71 #include <rte_common.h>
72 #include <rte_version.h>
73 #include <rte_atomic.h>
74 #include <malloc_heap.h>
75 #include <rte_eth_ring.h>
76
77 #include "eal_private.h"
78 #include "eal_thread.h"
79 #include "eal_internal_cfg.h"
80 #include "eal_filesystem.h"
81 #include "eal_hugepages.h"
82 #include "eal_options.h"
83
84 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
85
86 /* Allow the application to print its usage message too if set */
87 static rte_usage_hook_t rte_application_usage_hook = NULL;
88 /* early configuration structure, when memory config is not mmapped */
89 static struct rte_mem_config early_mem_config;
90
91 /* define fd variable here, because file needs to be kept open for the
92  * duration of the program, as we hold a write lock on it in the primary proc */
93 static int mem_cfg_fd = -1;
94
95 static struct flock wr_lock = {
96                 .l_type = F_WRLCK,
97                 .l_whence = SEEK_SET,
98                 .l_start = offsetof(struct rte_mem_config, memseg),
99                 .l_len = sizeof(early_mem_config.memseg),
100 };
101
102 /* Address of global and public configuration */
103 static struct rte_config rte_config = {
104                 .mem_config = &early_mem_config,
105 };
106
107 /* internal configuration (per-core) */
108 struct lcore_config lcore_config[RTE_MAX_LCORE];
109
110 /* internal configuration */
111 struct internal_config internal_config;
112
113 /* used by rte_rdtsc() */
114 int rte_cycles_vmware_tsc_map;
115
116 /* Return a pointer to the configuration structure */
117 struct rte_config *
118 rte_eal_get_configuration(void)
119 {
120         return &rte_config;
121 }
122
123 /* parse a sysfs (or other) file containing one integer value */
124 int
125 eal_parse_sysfs_value(const char *filename, unsigned long *val)
126 {
127         FILE *f;
128         char buf[BUFSIZ];
129         char *end = NULL;
130
131         if ((f = fopen(filename, "r")) == NULL) {
132                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
133                         __func__, filename);
134                 return -1;
135         }
136
137         if (fgets(buf, sizeof(buf), f) == NULL) {
138                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
139                         __func__, filename);
140                 fclose(f);
141                 return -1;
142         }
143         *val = strtoul(buf, &end, 0);
144         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
145                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
146                                 __func__, filename);
147                 fclose(f);
148                 return -1;
149         }
150         fclose(f);
151         return 0;
152 }
153
154
155 /* create memory configuration in shared/mmap memory. Take out
156  * a write lock on the memsegs, so we can auto-detect primary/secondary.
157  * This means we never close the file while running (auto-close on exit).
158  * We also don't lock the whole file, so that in future we can use read-locks
159  * on other parts, e.g. memzones, to detect if there are running secondary
160  * processes. */
161 static void
162 rte_eal_config_create(void)
163 {
164         void *rte_mem_cfg_addr;
165         int retval;
166
167         const char *pathname = eal_runtime_config_path();
168
169         if (internal_config.no_shconf)
170                 return;
171
172         if (mem_cfg_fd < 0){
173                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
174                 if (mem_cfg_fd < 0)
175                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
176         }
177
178         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
179         if (retval < 0){
180                 close(mem_cfg_fd);
181                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
182         }
183
184         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
185         if (retval < 0){
186                 close(mem_cfg_fd);
187                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
188                                 "process running?\n", pathname);
189         }
190
191         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
192                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
193
194         if (rte_mem_cfg_addr == MAP_FAILED){
195                 rte_panic("Cannot mmap memory for rte_config\n");
196         }
197         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
198         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
199 }
200
201 /* attach to an existing shared memory config */
202 static void
203 rte_eal_config_attach(void)
204 {
205         void *rte_mem_cfg_addr;
206         const char *pathname = eal_runtime_config_path();
207
208         if (internal_config.no_shconf)
209                 return;
210
211         if (mem_cfg_fd < 0){
212                 mem_cfg_fd = open(pathname, O_RDWR);
213                 if (mem_cfg_fd < 0)
214                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
215         }
216
217         rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
218                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
219         close(mem_cfg_fd);
220         if (rte_mem_cfg_addr == MAP_FAILED)
221                 rte_panic("Cannot mmap memory for rte_config\n");
222
223         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
224 }
225
226 /* Detect if we are a primary or a secondary process */
227 static enum rte_proc_type_t
228 eal_proc_type_detect(void)
229 {
230         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
231         const char *pathname = eal_runtime_config_path();
232
233         /* if we can open the file but not get a write-lock we are a secondary
234          * process. NOTE: if we get a file handle back, we keep that open
235          * and don't close it to prevent a race condition between multiple opens */
236         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
237                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
238                 ptype = RTE_PROC_SECONDARY;
239
240         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
241                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
242
243         return ptype;
244 }
245
246 /* Sets up rte_config structure with the pointer to shared memory config.*/
247 static void
248 rte_config_init(void)
249 {
250         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
251                         eal_proc_type_detect() : /* for auto, detect the type */
252                         internal_config.process_type; /* otherwise use what's already set */
253
254         switch (rte_config.process_type){
255         case RTE_PROC_PRIMARY:
256                 rte_eal_config_create();
257                 break;
258         case RTE_PROC_SECONDARY:
259                 rte_eal_config_attach();
260                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
261                 break;
262         case RTE_PROC_AUTO:
263         case RTE_PROC_INVALID:
264                 rte_panic("Invalid process type\n");
265         }
266 }
267
268 /* display usage */
269 static void
270 eal_usage(const char *prgname)
271 {
272         printf("\nUsage: %s ", prgname);
273         eal_common_usage();
274         /* Allow the application to print its usage message too if hook is set */
275         if ( rte_application_usage_hook ) {
276                 printf("===== Application Usage =====\n\n");
277                 rte_application_usage_hook(prgname);
278         }
279 }
280
281 /* Set a per-application usage message */
282 rte_usage_hook_t
283 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
284 {
285         rte_usage_hook_t        old_func;
286
287         /* Will be NULL on the first call to denote the last usage routine. */
288         old_func                                        = rte_application_usage_hook;
289         rte_application_usage_hook      = usage_func;
290
291         return old_func;
292 }
293
294 static inline size_t
295 eal_get_hugepage_mem_size(void)
296 {
297         uint64_t size = 0;
298         unsigned i, j;
299
300         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
301                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
302                 if (hpi->hugedir != NULL) {
303                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
304                                 size += hpi->hugepage_sz * hpi->num_pages[j];
305                         }
306                 }
307         }
308
309         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
310 }
311
312 /* Parse the argument given in the command line of the application */
313 static int
314 eal_parse_args(int argc, char **argv)
315 {
316         int opt, ret, i;
317         char **argvopt;
318         int option_index;
319         int coremask_ok = 0;
320         char *prgname = argv[0];
321
322         argvopt = argv;
323
324         internal_config.memory = 0;
325         internal_config.force_nrank = 0;
326         internal_config.force_nchannel = 0;
327         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
328         internal_config.hugepage_dir = NULL;
329         internal_config.force_sockets = 0;
330         internal_config.syslog_facility = LOG_DAEMON;
331         /* default value from build option */
332         internal_config.log_level = RTE_LOG_LEVEL;
333 #ifdef RTE_LIBEAL_USE_HPET
334         internal_config.no_hpet = 0;
335 #else
336         internal_config.no_hpet = 1;
337 #endif
338         /* zero out the NUMA config */
339         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
340                 internal_config.socket_mem[i] = 0;
341
342         /* zero out hugedir descriptors */
343         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
344                 internal_config.hugepage_info[i].lock_descriptor = 0;
345
346         internal_config.vmware_tsc_map = 0;
347
348         while ((opt = getopt_long(argc, argvopt, eal_short_options,
349                                   eal_long_options, &option_index)) != EOF) {
350
351                 int ret;
352
353                 /* getopt is not happy, stop right now */
354                 if (opt == '?')
355                         return -1;
356
357                 ret = eal_parse_common_option(opt, optarg, option_index,
358                                               &internal_config);
359                 /* common parser is not happy */
360                 if (ret < 0) {
361                         eal_usage(prgname);
362                         return -1;
363                 }
364                 /* common parser handled this option */
365                 if (ret == 0) {
366                         /* special case, note that the common parser accepted
367                          * the coremask option */
368                         if (opt == 'c')
369                                 coremask_ok = 1;
370                         continue;
371                 }
372
373                 switch (opt) {
374                 /* long options */
375                 case 0:
376                         {
377                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
378                                         "on FreeBSD\n",
379                                         eal_long_options[option_index].name);
380                                 eal_usage(prgname);
381                                 return -1;
382                         }
383                         break;
384
385                 default:
386                         if (isprint(opt)) {
387                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
388                                         "on FreeBSD\n", opt);
389                         } else {
390                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
391                                         "on FreeBSD\n", opt);
392                         }
393                         eal_usage(prgname);
394                         return -1;
395                 }
396         }
397
398         /* sanity checks */
399         if (!coremask_ok) {
400                 RTE_LOG(ERR, EAL, "coremask not specified\n");
401                 eal_usage(prgname);
402                 return -1;
403         }
404         if (internal_config.process_type == RTE_PROC_AUTO){
405                 internal_config.process_type = eal_proc_type_detect();
406         }
407         if (internal_config.process_type == RTE_PROC_INVALID){
408                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
409                 eal_usage(prgname);
410                 return -1;
411         }
412         if (internal_config.process_type == RTE_PROC_PRIMARY &&
413                         internal_config.force_nchannel == 0) {
414                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
415                 eal_usage(prgname);
416                 return -1;
417         }
418         if (index(internal_config.hugefile_prefix,'%') != NULL){
419                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
420                 eal_usage(prgname);
421                 return -1;
422         }
423         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
424                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
425                                 "at the same time\n");
426                 eal_usage(prgname);
427                 return -1;
428         }
429         /* --no-huge doesn't make sense with either -m or --socket-mem */
430         if (internal_config.no_hugetlbfs &&
431                         (internal_config.memory > 0 ||
432                                         internal_config.force_sockets == 1)) {
433                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
434                                 "together with --no-huge!\n");
435                 eal_usage(prgname);
436                 return -1;
437         }
438
439         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
440                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
441                 RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
442                         "[-w] options cannot be used at the same time\n");
443                 eal_usage(prgname);
444                 return -1;
445         }
446
447         if (optind >= 0)
448                 argv[optind-1] = prgname;
449
450         /* if no memory amounts were requested, this will result in 0 and
451          * will be overriden later, right after eal_hugepage_info_init() */
452         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
453                 internal_config.memory += internal_config.socket_mem[i];
454
455         ret = optind-1;
456         optind = 0; /* reset getopt lib */
457         return ret;
458 }
459
460 static void
461 eal_check_mem_on_local_socket(void)
462 {
463         const struct rte_memseg *ms;
464         int i, socket_id;
465
466         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
467
468         ms = rte_eal_get_physmem_layout();
469
470         for (i = 0; i < RTE_MAX_MEMSEG; i++)
471                 if (ms[i].socket_id == socket_id &&
472                                 ms[i].len > 0)
473                         return;
474
475         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
476                         "memory on local socket!\n");
477 }
478
479 static int
480 sync_func(__attribute__((unused)) void *arg)
481 {
482         return 0;
483 }
484
485 inline static void
486 rte_eal_mcfg_complete(void)
487 {
488         /* ALL shared mem_config related INIT DONE */
489         if (rte_config.process_type == RTE_PROC_PRIMARY)
490                 rte_config.mem_config->magic = RTE_MAGIC;
491 }
492
493 /* return non-zero if hugepages are enabled. */
494 int rte_eal_has_hugepages(void)
495 {
496         return !internal_config.no_hugetlbfs;
497 }
498
499 /* Abstraction for port I/0 privilege */
500 static int
501 rte_eal_iopl_init(void)
502 {
503         int fd = -1;
504         fd = open("/dev/io", O_RDWR);
505         if (fd < 0)
506                 return -1;
507         return 0;
508 }
509
510 /* Launch threads, called at application init(). */
511 int
512 rte_eal_init(int argc, char **argv)
513 {
514         int i, fctret, ret;
515         pthread_t thread_id;
516         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
517
518         if (!rte_atomic32_test_and_set(&run_once))
519                 return -1;
520
521         thread_id = pthread_self();
522
523         if (rte_eal_log_early_init() < 0)
524                 rte_panic("Cannot init early logs\n");
525
526         fctret = eal_parse_args(argc, argv);
527         if (fctret < 0)
528                 exit(1);
529
530         /* set log level as early as possible */
531         rte_set_log_level(internal_config.log_level);
532
533         if (internal_config.no_hugetlbfs == 0 &&
534                         internal_config.process_type != RTE_PROC_SECONDARY &&
535                         eal_hugepage_info_init() < 0)
536                 rte_panic("Cannot get hugepage information\n");
537
538         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
539                 if (internal_config.no_hugetlbfs)
540                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
541                 else
542                         internal_config.memory = eal_get_hugepage_mem_size();
543         }
544
545         if (internal_config.vmware_tsc_map == 1) {
546 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
547                 rte_cycles_vmware_tsc_map = 1;
548                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
549                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
550 #else
551                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
552                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
553 #endif
554         }
555
556         rte_srand(rte_rdtsc());
557
558         rte_config_init();
559
560         if (rte_eal_iopl_init() == 0)
561                 rte_config.flags |= EAL_FLG_HIGH_IOPL;
562
563         if (rte_eal_cpu_init() < 0)
564                 rte_panic("Cannot detect lcores\n");
565
566         if (rte_eal_memory_init() < 0)
567                 rte_panic("Cannot init memory\n");
568
569         if (rte_eal_memzone_init() < 0)
570                 rte_panic("Cannot init memzone\n");
571
572         if (rte_eal_tailqs_init() < 0)
573                 rte_panic("Cannot init tail queues for objects\n");
574
575 /*      if (rte_eal_log_init(argv[0], internal_config.syslog_facility) < 0)
576                 rte_panic("Cannot init logs\n");*/
577
578         if (rte_eal_alarm_init() < 0)
579                 rte_panic("Cannot init interrupt-handling thread\n");
580
581         if (rte_eal_intr_init() < 0)
582                 rte_panic("Cannot init interrupt-handling thread\n");
583
584         if (rte_eal_timer_init() < 0)
585                 rte_panic("Cannot init HPET or TSC timers\n");
586
587         if (rte_eal_pci_init() < 0)
588                 rte_panic("Cannot init PCI\n");
589
590         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%p)\n",
591                 rte_config.master_lcore, thread_id);
592
593         eal_check_mem_on_local_socket();
594
595         rte_eal_mcfg_complete();
596
597         if (rte_eal_dev_init(PMD_INIT_PRE_PCI_PROBE) < 0)
598                 rte_panic("Cannot init pmd devices\n");
599
600         RTE_LCORE_FOREACH_SLAVE(i) {
601
602                 /*
603                  * create communication pipes between master thread
604                  * and children
605                  */
606                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
607                         rte_panic("Cannot create pipe\n");
608                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
609                         rte_panic("Cannot create pipe\n");
610
611                 lcore_config[i].state = WAIT;
612
613                 /* create a thread for each lcore */
614                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
615                                      eal_thread_loop, NULL);
616                 if (ret != 0)
617                         rte_panic("Cannot create thread\n");
618         }
619
620         eal_thread_init_master(rte_config.master_lcore);
621
622         /*
623          * Launch a dummy function on all slave lcores, so that master lcore
624          * knows they are all ready when this function returns.
625          */
626         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
627         rte_eal_mp_wait_lcore();
628
629         /* Probe & Initialize PCI devices */
630         if (rte_eal_pci_probe())
631                         rte_panic("Cannot probe PCI\n");
632
633         /* Initialize any outstanding devices */
634         if (rte_eal_dev_init(PMD_INIT_POST_PCI_PROBE) < 0)
635                 rte_panic("Cannot init pmd devices\n");
636
637         return fctret;
638 }
639
640 /* get core role */
641 enum rte_lcore_role_t
642 rte_eal_lcore_role(unsigned lcore_id)
643 {
644         return (rte_config.lcore_role[lcore_id]);
645 }
646
647 enum rte_proc_type_t
648 rte_eal_process_type(void)
649 {
650         return (rte_config.process_type);
651 }
652