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