eal: move plugin loading from linuxapp to common
[dpdk.git] / lib / librte_eal / common / eal_common_options.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
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 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <errno.h>
41 #include <getopt.h>
42 #include <dlfcn.h>
43
44 #include <rte_eal.h>
45 #include <rte_log.h>
46 #include <rte_lcore.h>
47 #include <rte_version.h>
48 #include <rte_devargs.h>
49 #include <rte_memcpy.h>
50
51 #include "eal_internal_cfg.h"
52 #include "eal_options.h"
53 #include "eal_filesystem.h"
54
55 #define BITS_PER_HEX 4
56
57 const char
58 eal_short_options[] =
59         "b:" /* pci-blacklist */
60         "c:" /* coremask */
61         "d:" /* driver */
62         "h"  /* help */
63         "l:" /* corelist */
64         "m:" /* memory size */
65         "n:" /* memory channels */
66         "r:" /* memory ranks */
67         "v"  /* version */
68         "w:" /* pci-whitelist */
69         ;
70
71 const struct option
72 eal_long_options[] = {
73         {OPT_BASE_VIRTADDR,     1, NULL, OPT_BASE_VIRTADDR_NUM    },
74         {OPT_CREATE_UIO_DEV,    0, NULL, OPT_CREATE_UIO_DEV_NUM   },
75         {OPT_FILE_PREFIX,       1, NULL, OPT_FILE_PREFIX_NUM      },
76         {OPT_HELP,              0, NULL, OPT_HELP_NUM             },
77         {OPT_HUGE_DIR,          1, NULL, OPT_HUGE_DIR_NUM         },
78         {OPT_HUGE_UNLINK,       0, NULL, OPT_HUGE_UNLINK_NUM      },
79         {OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
80         {OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
81         {OPT_MASTER_LCORE,      1, NULL, OPT_MASTER_LCORE_NUM     },
82         {OPT_NO_HPET,           0, NULL, OPT_NO_HPET_NUM          },
83         {OPT_NO_HUGE,           0, NULL, OPT_NO_HUGE_NUM          },
84         {OPT_NO_PCI,            0, NULL, OPT_NO_PCI_NUM           },
85         {OPT_NO_SHCONF,         0, NULL, OPT_NO_SHCONF_NUM        },
86         {OPT_PCI_BLACKLIST,     1, NULL, OPT_PCI_BLACKLIST_NUM    },
87         {OPT_PCI_WHITELIST,     1, NULL, OPT_PCI_WHITELIST_NUM    },
88         {OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
89         {OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
90         {OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
91         {OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
92         {OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
93         {OPT_VMWARE_TSC_MAP,    0, NULL, OPT_VMWARE_TSC_MAP_NUM   },
94         {OPT_XEN_DOM0,          0, NULL, OPT_XEN_DOM0_NUM         },
95         {0,                     0, NULL, 0                        }
96 };
97
98 TAILQ_HEAD(shared_driver_list, shared_driver);
99
100 /* Definition for shared object drivers. */
101 struct shared_driver {
102         TAILQ_ENTRY(shared_driver) next;
103
104         char    name[PATH_MAX];
105         void*   lib_handle;
106 };
107
108 /* List of external loadable drivers */
109 static struct shared_driver_list solib_list =
110 TAILQ_HEAD_INITIALIZER(solib_list);
111
112 static int master_lcore_parsed;
113 static int mem_parsed;
114
115 void
116 eal_reset_internal_config(struct internal_config *internal_cfg)
117 {
118         int i;
119
120         internal_cfg->memory = 0;
121         internal_cfg->force_nrank = 0;
122         internal_cfg->force_nchannel = 0;
123         internal_cfg->hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
124         internal_cfg->hugepage_dir = NULL;
125         internal_cfg->force_sockets = 0;
126         /* zero out the NUMA config */
127         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
128                 internal_cfg->socket_mem[i] = 0;
129         /* zero out hugedir descriptors */
130         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
131                 internal_cfg->hugepage_info[i].lock_descriptor = -1;
132         internal_cfg->base_virtaddr = 0;
133
134         internal_cfg->syslog_facility = LOG_DAEMON;
135         /* default value from build option */
136         internal_cfg->log_level = RTE_LOG_LEVEL;
137
138         internal_cfg->xen_dom0_support = 0;
139
140         /* if set to NONE, interrupt mode is determined automatically */
141         internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
142
143 #ifdef RTE_LIBEAL_USE_HPET
144         internal_cfg->no_hpet = 0;
145 #else
146         internal_cfg->no_hpet = 1;
147 #endif
148         internal_cfg->vmware_tsc_map = 0;
149         internal_cfg->create_uio_dev = 0;
150 }
151
152 static int
153 eal_plugin_add(const char *path)
154 {
155         struct shared_driver *solib;
156
157         solib = malloc(sizeof(*solib));
158         if (solib == NULL) {
159                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
160                 return -1;
161         }
162         memset(solib, 0, sizeof(*solib));
163         strncpy(solib->name, path, PATH_MAX-1);
164         solib->name[PATH_MAX-1] = 0;
165         TAILQ_INSERT_TAIL(&solib_list, solib, next);
166
167         return 0;
168 }
169
170 int
171 eal_plugins_init(void)
172 {
173         struct shared_driver *solib = NULL;
174
175         TAILQ_FOREACH(solib, &solib_list, next) {
176                 RTE_LOG(DEBUG, EAL, "open shared lib %s\n", solib->name);
177                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
178                 if (solib->lib_handle == NULL) {
179                         RTE_LOG(ERR, EAL, "%s\n", dlerror());
180                         return -1;
181                 }
182         }
183         return 0;
184 }
185
186 /*
187  * Parse the coremask given as argument (hexadecimal string) and fill
188  * the global configuration (core role and core count) with the parsed
189  * value.
190  */
191 static int xdigit2val(unsigned char c)
192 {
193         int val;
194
195         if (isdigit(c))
196                 val = c - '0';
197         else if (isupper(c))
198                 val = c - 'A' + 10;
199         else
200                 val = c - 'a' + 10;
201         return val;
202 }
203
204 static int
205 eal_parse_coremask(const char *coremask)
206 {
207         struct rte_config *cfg = rte_eal_get_configuration();
208         int i, j, idx = 0;
209         unsigned count = 0;
210         char c;
211         int val;
212
213         if (coremask == NULL)
214                 return -1;
215         /* Remove all blank characters ahead and after .
216          * Remove 0x/0X if exists.
217          */
218         while (isblank(*coremask))
219                 coremask++;
220         if (coremask[0] == '0' && ((coremask[1] == 'x')
221                 || (coremask[1] == 'X')))
222                 coremask += 2;
223         i = strlen(coremask);
224         while ((i > 0) && isblank(coremask[i - 1]))
225                 i--;
226         if (i == 0)
227                 return -1;
228
229         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
230                 c = coremask[i];
231                 if (isxdigit(c) == 0) {
232                         /* invalid characters */
233                         return -1;
234                 }
235                 val = xdigit2val(c);
236                 for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++)
237                 {
238                         if ((1 << j) & val) {
239                                 if (!lcore_config[idx].detected) {
240                                         RTE_LOG(ERR, EAL, "lcore %u "
241                                                 "unavailable\n", idx);
242                                         return -1;
243                                 }
244                                 cfg->lcore_role[idx] = ROLE_RTE;
245                                 lcore_config[idx].core_index = count;
246                                 count++;
247                         } else {
248                                 cfg->lcore_role[idx] = ROLE_OFF;
249                                 lcore_config[idx].core_index = -1;
250                         }
251                 }
252         }
253         for (; i >= 0; i--)
254                 if (coremask[i] != '0')
255                         return -1;
256         for (; idx < RTE_MAX_LCORE; idx++) {
257                 cfg->lcore_role[idx] = ROLE_OFF;
258                 lcore_config[idx].core_index = -1;
259         }
260         if (count == 0)
261                 return -1;
262         /* Update the count of enabled logical cores of the EAL configuration */
263         cfg->lcore_count = count;
264         return 0;
265 }
266
267 static int
268 eal_parse_corelist(const char *corelist)
269 {
270         struct rte_config *cfg = rte_eal_get_configuration();
271         int i, idx = 0;
272         unsigned count = 0;
273         char *end = NULL;
274         int min, max;
275
276         if (corelist == NULL)
277                 return -1;
278
279         /* Remove all blank characters ahead and after */
280         while (isblank(*corelist))
281                 corelist++;
282         i = strlen(corelist);
283         while ((i > 0) && isblank(corelist[i - 1]))
284                 i--;
285
286         /* Reset config */
287         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
288                 cfg->lcore_role[idx] = ROLE_OFF;
289                 lcore_config[idx].core_index = -1;
290         }
291
292         /* Get list of cores */
293         min = RTE_MAX_LCORE;
294         do {
295                 while (isblank(*corelist))
296                         corelist++;
297                 if (*corelist == '\0')
298                         return -1;
299                 errno = 0;
300                 idx = strtoul(corelist, &end, 10);
301                 if (errno || end == NULL)
302                         return -1;
303                 while (isblank(*end))
304                         end++;
305                 if (*end == '-') {
306                         min = idx;
307                 } else if ((*end == ',') || (*end == '\0')) {
308                         max = idx;
309                         if (min == RTE_MAX_LCORE)
310                                 min = idx;
311                         for (idx = min; idx <= max; idx++) {
312                                 if (cfg->lcore_role[idx] != ROLE_RTE) {
313                                         cfg->lcore_role[idx] = ROLE_RTE;
314                                         lcore_config[idx].core_index = count;
315                                         count++;
316                                 }
317                         }
318                         min = RTE_MAX_LCORE;
319                 } else
320                         return -1;
321                 corelist = end + 1;
322         } while (*end != '\0');
323
324         if (count == 0)
325                 return -1;
326
327         /* Update the count of enabled logical cores of the EAL configuration */
328         cfg->lcore_count = count;
329
330         return 0;
331 }
332
333 /* Changes the lcore id of the master thread */
334 static int
335 eal_parse_master_lcore(const char *arg)
336 {
337         char *parsing_end;
338         struct rte_config *cfg = rte_eal_get_configuration();
339
340         errno = 0;
341         cfg->master_lcore = (uint32_t) strtol(arg, &parsing_end, 0);
342         if (errno || parsing_end[0] != 0)
343                 return -1;
344         if (cfg->master_lcore >= RTE_MAX_LCORE)
345                 return -1;
346         master_lcore_parsed = 1;
347         return 0;
348 }
349
350 /*
351  * Parse elem, the elem could be single number/range or '(' ')' group
352  * 1) A single number elem, it's just a simple digit. e.g. 9
353  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
354  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
355  *    Within group elem, '-' used for a range separator;
356  *                       ',' used for a single number.
357  */
358 static int
359 eal_parse_set(const char *input, uint16_t set[], unsigned num)
360 {
361         unsigned idx;
362         const char *str = input;
363         char *end = NULL;
364         unsigned min, max;
365
366         memset(set, 0, num * sizeof(uint16_t));
367
368         while (isblank(*str))
369                 str++;
370
371         /* only digit or left bracket is qualify for start point */
372         if ((!isdigit(*str) && *str != '(') || *str == '\0')
373                 return -1;
374
375         /* process single number or single range of number */
376         if (*str != '(') {
377                 errno = 0;
378                 idx = strtoul(str, &end, 10);
379                 if (errno || end == NULL || idx >= num)
380                         return -1;
381                 else {
382                         while (isblank(*end))
383                                 end++;
384
385                         min = idx;
386                         max = idx;
387                         if (*end == '-') {
388                                 /* process single <number>-<number> */
389                                 end++;
390                                 while (isblank(*end))
391                                         end++;
392                                 if (!isdigit(*end))
393                                         return -1;
394
395                                 errno = 0;
396                                 idx = strtoul(end, &end, 10);
397                                 if (errno || end == NULL || idx >= num)
398                                         return -1;
399                                 max = idx;
400                                 while (isblank(*end))
401                                         end++;
402                                 if (*end != ',' && *end != '\0')
403                                         return -1;
404                         }
405
406                         if (*end != ',' && *end != '\0' &&
407                             *end != '@')
408                                 return -1;
409
410                         for (idx = RTE_MIN(min, max);
411                              idx <= RTE_MAX(min, max); idx++)
412                                 set[idx] = 1;
413
414                         return end - input;
415                 }
416         }
417
418         /* process set within bracket */
419         str++;
420         while (isblank(*str))
421                 str++;
422         if (*str == '\0')
423                 return -1;
424
425         min = RTE_MAX_LCORE;
426         do {
427
428                 /* go ahead to the first digit */
429                 while (isblank(*str))
430                         str++;
431                 if (!isdigit(*str))
432                         return -1;
433
434                 /* get the digit value */
435                 errno = 0;
436                 idx = strtoul(str, &end, 10);
437                 if (errno || end == NULL || idx >= num)
438                         return -1;
439
440                 /* go ahead to separator '-',',' and ')' */
441                 while (isblank(*end))
442                         end++;
443                 if (*end == '-') {
444                         if (min == RTE_MAX_LCORE)
445                                 min = idx;
446                         else /* avoid continuous '-' */
447                                 return -1;
448                 } else if ((*end == ',') || (*end == ')')) {
449                         max = idx;
450                         if (min == RTE_MAX_LCORE)
451                                 min = idx;
452                         for (idx = RTE_MIN(min, max);
453                              idx <= RTE_MAX(min, max); idx++)
454                                 set[idx] = 1;
455
456                         min = RTE_MAX_LCORE;
457                 } else
458                         return -1;
459
460                 str = end + 1;
461         } while (*end != '\0' && *end != ')');
462
463         return str - input;
464 }
465
466 /* convert from set array to cpuset bitmap */
467 static int
468 convert_to_cpuset(rte_cpuset_t *cpusetp,
469               uint16_t *set, unsigned num)
470 {
471         unsigned idx;
472
473         CPU_ZERO(cpusetp);
474
475         for (idx = 0; idx < num; idx++) {
476                 if (!set[idx])
477                         continue;
478
479                 if (!lcore_config[idx].detected) {
480                         RTE_LOG(ERR, EAL, "core %u "
481                                 "unavailable\n", idx);
482                         return -1;
483                 }
484
485                 CPU_SET(idx, cpusetp);
486         }
487
488         return 0;
489 }
490
491 /*
492  * The format pattern: --lcores='<lcores[@cpus]>[<,lcores[@cpus]>...]'
493  * lcores, cpus could be a single digit/range or a group.
494  * '(' and ')' are necessary if it's a group.
495  * If not supply '@cpus', the value of cpus uses the same as lcores.
496  * e.g. '1,2@(5-7),(3-5)@(0,2),(0,6),7-8' means start 9 EAL thread as below
497  *   lcore 0 runs on cpuset 0x41 (cpu 0,6)
498  *   lcore 1 runs on cpuset 0x2 (cpu 1)
499  *   lcore 2 runs on cpuset 0xe0 (cpu 5,6,7)
500  *   lcore 3,4,5 runs on cpuset 0x5 (cpu 0,2)
501  *   lcore 6 runs on cpuset 0x41 (cpu 0,6)
502  *   lcore 7 runs on cpuset 0x80 (cpu 7)
503  *   lcore 8 runs on cpuset 0x100 (cpu 8)
504  */
505 static int
506 eal_parse_lcores(const char *lcores)
507 {
508         struct rte_config *cfg = rte_eal_get_configuration();
509         static uint16_t set[RTE_MAX_LCORE];
510         unsigned idx = 0;
511         int i;
512         unsigned count = 0;
513         const char *lcore_start = NULL;
514         const char *end = NULL;
515         int offset;
516         rte_cpuset_t cpuset;
517         int lflags = 0;
518         int ret = -1;
519
520         if (lcores == NULL)
521                 return -1;
522
523         /* Remove all blank characters ahead and after */
524         while (isblank(*lcores))
525                 lcores++;
526         i = strlen(lcores);
527         while ((i > 0) && isblank(lcores[i - 1]))
528                 i--;
529
530         CPU_ZERO(&cpuset);
531
532         /* Reset lcore config */
533         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
534                 cfg->lcore_role[idx] = ROLE_OFF;
535                 lcore_config[idx].core_index = -1;
536                 CPU_ZERO(&lcore_config[idx].cpuset);
537         }
538
539         /* Get list of cores */
540         do {
541                 while (isblank(*lcores))
542                         lcores++;
543                 if (*lcores == '\0')
544                         goto err;
545
546                 /* record lcore_set start point */
547                 lcore_start = lcores;
548
549                 /* go across a complete bracket */
550                 if (*lcore_start == '(') {
551                         lcores += strcspn(lcores, ")");
552                         if (*lcores++ == '\0')
553                                 goto err;
554                 }
555
556                 /* scan the separator '@', ','(next) or '\0'(finish) */
557                 lcores += strcspn(lcores, "@,");
558
559                 if (*lcores == '@') {
560                         /* explicit assign cpu_set */
561                         offset = eal_parse_set(lcores + 1, set, RTE_DIM(set));
562                         if (offset < 0)
563                                 goto err;
564
565                         /* prepare cpu_set and update the end cursor */
566                         if (0 > convert_to_cpuset(&cpuset,
567                                                   set, RTE_DIM(set)))
568                                 goto err;
569                         end = lcores + 1 + offset;
570                 } else { /* ',' or '\0' */
571                         /* haven't given cpu_set, current loop done */
572                         end = lcores;
573
574                         /* go back to check <number>-<number> */
575                         offset = strcspn(lcore_start, "(-");
576                         if (offset < (end - lcore_start) &&
577                             *(lcore_start + offset) != '(')
578                                 lflags = 1;
579                 }
580
581                 if (*end != ',' && *end != '\0')
582                         goto err;
583
584                 /* parse lcore_set from start point */
585                 if (0 > eal_parse_set(lcore_start, set, RTE_DIM(set)))
586                         goto err;
587
588                 /* without '@', by default using lcore_set as cpu_set */
589                 if (*lcores != '@' &&
590                     0 > convert_to_cpuset(&cpuset, set, RTE_DIM(set)))
591                         goto err;
592
593                 /* start to update lcore_set */
594                 for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
595                         if (!set[idx])
596                                 continue;
597
598                         if (cfg->lcore_role[idx] != ROLE_RTE) {
599                                 lcore_config[idx].core_index = count;
600                                 cfg->lcore_role[idx] = ROLE_RTE;
601                                 count++;
602                         }
603
604                         if (lflags) {
605                                 CPU_ZERO(&cpuset);
606                                 CPU_SET(idx, &cpuset);
607                         }
608                         rte_memcpy(&lcore_config[idx].cpuset, &cpuset,
609                                    sizeof(rte_cpuset_t));
610                 }
611
612                 lcores = end + 1;
613         } while (*end != '\0');
614
615         if (count == 0)
616                 goto err;
617
618         cfg->lcore_count = count;
619         ret = 0;
620
621 err:
622
623         return ret;
624 }
625
626 static int
627 eal_parse_syslog(const char *facility, struct internal_config *conf)
628 {
629         int i;
630         static struct {
631                 const char *name;
632                 int value;
633         } map[] = {
634                 { "auth", LOG_AUTH },
635                 { "cron", LOG_CRON },
636                 { "daemon", LOG_DAEMON },
637                 { "ftp", LOG_FTP },
638                 { "kern", LOG_KERN },
639                 { "lpr", LOG_LPR },
640                 { "mail", LOG_MAIL },
641                 { "news", LOG_NEWS },
642                 { "syslog", LOG_SYSLOG },
643                 { "user", LOG_USER },
644                 { "uucp", LOG_UUCP },
645                 { "local0", LOG_LOCAL0 },
646                 { "local1", LOG_LOCAL1 },
647                 { "local2", LOG_LOCAL2 },
648                 { "local3", LOG_LOCAL3 },
649                 { "local4", LOG_LOCAL4 },
650                 { "local5", LOG_LOCAL5 },
651                 { "local6", LOG_LOCAL6 },
652                 { "local7", LOG_LOCAL7 },
653                 { NULL, 0 }
654         };
655
656         for (i = 0; map[i].name; i++) {
657                 if (!strcmp(facility, map[i].name)) {
658                         conf->syslog_facility = map[i].value;
659                         return 0;
660                 }
661         }
662         return -1;
663 }
664
665 static int
666 eal_parse_log_level(const char *level, uint32_t *log_level)
667 {
668         char *end;
669         unsigned long tmp;
670
671         errno = 0;
672         tmp = strtoul(level, &end, 0);
673
674         /* check for errors */
675         if ((errno != 0) || (level[0] == '\0') ||
676             end == NULL || (*end != '\0'))
677                 return -1;
678
679         /* log_level is a uint32_t */
680         if (tmp >= UINT32_MAX)
681                 return -1;
682
683         *log_level = tmp;
684         return 0;
685 }
686
687 static enum rte_proc_type_t
688 eal_parse_proc_type(const char *arg)
689 {
690         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
691                 return RTE_PROC_PRIMARY;
692         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
693                 return RTE_PROC_SECONDARY;
694         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
695                 return RTE_PROC_AUTO;
696
697         return RTE_PROC_INVALID;
698 }
699
700 int
701 eal_parse_common_option(int opt, const char *optarg,
702                         struct internal_config *conf)
703 {
704         switch (opt) {
705         /* blacklist */
706         case 'b':
707                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
708                                 optarg) < 0) {
709                         return -1;
710                 }
711                 break;
712         /* whitelist */
713         case 'w':
714                 if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
715                                 optarg) < 0) {
716                         return -1;
717                 }
718                 break;
719         /* coremask */
720         case 'c':
721                 if (eal_parse_coremask(optarg) < 0) {
722                         RTE_LOG(ERR, EAL, "invalid coremask\n");
723                         return -1;
724                 }
725                 break;
726         /* corelist */
727         case 'l':
728                 if (eal_parse_corelist(optarg) < 0) {
729                         RTE_LOG(ERR, EAL, "invalid core list\n");
730                         return -1;
731                 }
732                 break;
733         /* size of memory */
734         case 'm':
735                 conf->memory = atoi(optarg);
736                 conf->memory *= 1024ULL;
737                 conf->memory *= 1024ULL;
738                 mem_parsed = 1;
739                 break;
740         /* force number of channels */
741         case 'n':
742                 conf->force_nchannel = atoi(optarg);
743                 if (conf->force_nchannel == 0 ||
744                     conf->force_nchannel > 4) {
745                         RTE_LOG(ERR, EAL, "invalid channel number\n");
746                         return -1;
747                 }
748                 break;
749         /* force number of ranks */
750         case 'r':
751                 conf->force_nrank = atoi(optarg);
752                 if (conf->force_nrank == 0 ||
753                     conf->force_nrank > 16) {
754                         RTE_LOG(ERR, EAL, "invalid rank number\n");
755                         return -1;
756                 }
757                 break;
758         /* force loading of external driver */
759         case 'd':
760                 if (eal_plugin_add(optarg) == -1)
761                         return -1;
762                 break;
763         case 'v':
764                 /* since message is explicitly requested by user, we
765                  * write message at highest log level so it can always
766                  * be seen
767                  * even if info or warning messages are disabled */
768                 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
769                 break;
770
771         /* long options */
772         case OPT_HUGE_UNLINK_NUM:
773                 conf->hugepage_unlink = 1;
774                 break;
775
776         case OPT_NO_HUGE_NUM:
777                 conf->no_hugetlbfs = 1;
778                 break;
779
780         case OPT_NO_PCI_NUM:
781                 conf->no_pci = 1;
782                 break;
783
784         case OPT_NO_HPET_NUM:
785                 conf->no_hpet = 1;
786                 break;
787
788         case OPT_VMWARE_TSC_MAP_NUM:
789                 conf->vmware_tsc_map = 1;
790                 break;
791
792         case OPT_NO_SHCONF_NUM:
793                 conf->no_shconf = 1;
794                 break;
795
796         case OPT_PROC_TYPE_NUM:
797                 conf->process_type = eal_parse_proc_type(optarg);
798                 break;
799
800         case OPT_MASTER_LCORE_NUM:
801                 if (eal_parse_master_lcore(optarg) < 0) {
802                         RTE_LOG(ERR, EAL, "invalid parameter for --"
803                                         OPT_MASTER_LCORE "\n");
804                         return -1;
805                 }
806                 break;
807
808         case OPT_VDEV_NUM:
809                 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL,
810                                 optarg) < 0) {
811                         return -1;
812                 }
813                 break;
814
815         case OPT_SYSLOG_NUM:
816                 if (eal_parse_syslog(optarg, conf) < 0) {
817                         RTE_LOG(ERR, EAL, "invalid parameters for --"
818                                         OPT_SYSLOG "\n");
819                         return -1;
820                 }
821                 break;
822
823         case OPT_LOG_LEVEL_NUM: {
824                 uint32_t log;
825
826                 if (eal_parse_log_level(optarg, &log) < 0) {
827                         RTE_LOG(ERR, EAL,
828                                 "invalid parameters for --"
829                                 OPT_LOG_LEVEL "\n");
830                         return -1;
831                 }
832                 conf->log_level = log;
833                 break;
834         }
835         case OPT_LCORES_NUM:
836                 if (eal_parse_lcores(optarg) < 0) {
837                         RTE_LOG(ERR, EAL, "invalid parameter for --"
838                                 OPT_LCORES "\n");
839                         return -1;
840                 }
841                 break;
842
843         /* don't know what to do, leave this to caller */
844         default:
845                 return 1;
846
847         }
848
849         return 0;
850 }
851
852 int
853 eal_adjust_config(struct internal_config *internal_cfg)
854 {
855         int i;
856         struct rte_config *cfg = rte_eal_get_configuration();
857
858         if (internal_config.process_type == RTE_PROC_AUTO)
859                 internal_config.process_type = eal_proc_type_detect();
860
861         /* default master lcore is the first one */
862         if (!master_lcore_parsed)
863                 cfg->master_lcore = rte_get_next_lcore(-1, 0, 0);
864
865         /* if no memory amounts were requested, this will result in 0 and
866          * will be overridden later, right after eal_hugepage_info_init() */
867         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
868                 internal_cfg->memory += internal_cfg->socket_mem[i];
869
870         return 0;
871 }
872
873 int
874 eal_check_common_options(struct internal_config *internal_cfg)
875 {
876         struct rte_config *cfg = rte_eal_get_configuration();
877
878         if (cfg->lcore_role[cfg->master_lcore] != ROLE_RTE) {
879                 RTE_LOG(ERR, EAL, "Master lcore is not enabled for DPDK\n");
880                 return -1;
881         }
882
883         if (internal_cfg->process_type == RTE_PROC_INVALID) {
884                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
885                 return -1;
886         }
887         if (index(internal_cfg->hugefile_prefix, '%') != NULL) {
888                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in --"OPT_FILE_PREFIX" "
889                         "option\n");
890                 return -1;
891         }
892         if (mem_parsed && internal_cfg->force_sockets == 1) {
893                 RTE_LOG(ERR, EAL, "Options -m and --"OPT_SOCKET_MEM" cannot "
894                         "be specified at the same time\n");
895                 return -1;
896         }
897         if (internal_cfg->no_hugetlbfs && internal_cfg->force_sockets == 1) {
898                 RTE_LOG(ERR, EAL, "Option --"OPT_SOCKET_MEM" cannot "
899                         "be specified together with --"OPT_NO_HUGE"\n");
900                 return -1;
901         }
902
903         if (internal_cfg->no_hugetlbfs && internal_cfg->hugepage_unlink) {
904                 RTE_LOG(ERR, EAL, "Option --"OPT_HUGE_UNLINK" cannot "
905                         "be specified together with --"OPT_NO_HUGE"\n");
906                 return -1;
907         }
908
909         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
910                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
911                 RTE_LOG(ERR, EAL, "Options blacklist (-b) and whitelist (-w) "
912                         "cannot be used at the same time\n");
913                 return -1;
914         }
915
916         return 0;
917 }
918
919 void
920 eal_common_usage(void)
921 {
922         printf("[options]\n\n"
923                "EAL common options:\n"
924                "  -c COREMASK         Hexadecimal bitmask of cores to run on\n"
925                "  -l CORELIST         List of cores to run on\n"
926                "                      The argument format is <c1>[-c2][,c3[-c4],...]\n"
927                "                      where c1, c2, etc are core indexes between 0 and %d\n"
928                "  --"OPT_LCORES" COREMAP    Map lcore set to physical cpu set\n"
929                "                      The argument format is\n"
930                "                            '<lcores[@cpus]>[<,lcores[@cpus]>...]'\n"
931                "                      lcores and cpus list are grouped by '(' and ')'\n"
932                "                      Within the group, '-' is used for range separator,\n"
933                "                      ',' is used for single number separator.\n"
934                "                      '( )' can be omitted for single element group,\n"
935                "                      '@' can be omitted if cpus and lcores have the same value\n"
936                "  --"OPT_MASTER_LCORE" ID   Core ID that is used as master\n"
937                "  -n CHANNELS         Number of memory channels\n"
938                "  -m MB               Memory to allocate (see also --"OPT_SOCKET_MEM")\n"
939                "  -r RANKS            Force number of memory ranks (don't detect)\n"
940                "  -b, --"OPT_PCI_BLACKLIST" Add a PCI device in black list.\n"
941                "                      Prevent EAL from using this PCI device. The argument\n"
942                "                      format is <domain:bus:devid.func>.\n"
943                "  -w, --"OPT_PCI_WHITELIST" Add a PCI device in white list.\n"
944                "                      Only use the specified PCI devices. The argument format\n"
945                "                      is <[domain:]bus:devid.func>. This option can be present\n"
946                "                      several times (once per device).\n"
947                "                      [NOTE: PCI whitelist cannot be used with -b option]\n"
948                "  --"OPT_VDEV"              Add a virtual device.\n"
949                "                      The argument format is <driver><id>[,key=val,...]\n"
950                "                      (ex: --vdev=eth_pcap0,iface=eth2).\n"
951                "  -d LIB.so           Add driver (can be used multiple times)\n"
952                "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
953                "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
954                "  --"OPT_SYSLOG"            Set syslog facility\n"
955                "  --"OPT_LOG_LEVEL"         Set default log level\n"
956                "  -v                  Display version information on startup\n"
957                "  -h, --help          This help\n"
958                "\nEAL options for DEBUG use only:\n"
959                "  --"OPT_HUGE_UNLINK"       Unlink hugepage files after init\n"
960                "  --"OPT_NO_HUGE"           Use malloc instead of hugetlbfs\n"
961                "  --"OPT_NO_PCI"            Disable PCI\n"
962                "  --"OPT_NO_HPET"           Disable HPET\n"
963                "  --"OPT_NO_SHCONF"         No shared config (mmap'd files)\n"
964                "\n", RTE_MAX_LCORE);
965 }