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