f-stack/dpdk/lib/librte_eal/common/rte_option.c

96 lines
1.8 KiB
C
Raw Normal View History

2019-06-25 11:12:58 +00:00
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2018 Intel Corporation.
*/
2020-06-18 16:55:50 +00:00
#include <getopt.h>
2019-06-25 11:12:58 +00:00
#include <unistd.h>
#include <string.h>
#include <rte_eal.h>
#include <rte_option.h>
#include "eal_private.h"
2020-06-18 16:55:50 +00:00
#include "eal_internal_cfg.h" /* Necessary for eal_options.h */
#include "eal_options.h"
2019-06-25 11:12:58 +00:00
TAILQ_HEAD(rte_option_list, rte_option);
struct rte_option_list rte_option_list =
TAILQ_HEAD_INITIALIZER(rte_option_list);
int
rte_option_parse(const char *opt)
{
2020-06-18 16:55:50 +00:00
struct rte_option *option;
if (strlen(opt) <= 2 ||
strncmp(opt, "--", 2))
return -1;
2019-06-25 11:12:58 +00:00
/* Check if the option is registered */
TAILQ_FOREACH(option, &rte_option_list, next) {
2020-06-18 16:55:50 +00:00
if (strcmp(&opt[2], option->name) == 0) {
2019-06-25 11:12:58 +00:00
option->enabled = 1;
return 0;
}
}
return -1;
}
2020-06-18 16:55:50 +00:00
int
2019-06-25 11:12:58 +00:00
rte_option_register(struct rte_option *opt)
{
2020-06-18 16:55:50 +00:00
struct rte_option *option;
const struct option *gopt;
gopt = &eal_long_options[0];
while (gopt->name != NULL) {
if (strcmp(gopt->name, opt->name) == 0) {
RTE_LOG(ERR, EAL, "Option %s is already a common EAL option.\n",
opt->name);
return -1;
}
gopt++;
}
2019-06-25 11:12:58 +00:00
TAILQ_FOREACH(option, &rte_option_list, next) {
2020-06-18 16:55:50 +00:00
if (strcmp(opt->name, option->name) == 0) {
2019-06-26 10:17:41 +00:00
RTE_LOG(ERR, EAL, "Option %s has already been registered.\n",
2020-06-18 16:55:50 +00:00
opt->name);
return -1;
2019-06-26 10:17:41 +00:00
}
2019-06-25 11:12:58 +00:00
}
TAILQ_INSERT_HEAD(&rte_option_list, opt, next);
2020-06-18 16:55:50 +00:00
return 0;
2019-06-25 11:12:58 +00:00
}
void
rte_option_init(void)
{
2020-06-18 16:55:50 +00:00
struct rte_option *option;
2019-06-25 11:12:58 +00:00
TAILQ_FOREACH(option, &rte_option_list, next) {
if (option->enabled)
option->cb();
}
}
2020-06-18 16:55:50 +00:00
void
rte_option_usage(void)
{
struct rte_option *option;
int opt_count = 0;
TAILQ_FOREACH(option, &rte_option_list, next)
opt_count += 1;
if (opt_count == 0)
return;
printf("EAL dynamic options:\n");
TAILQ_FOREACH(option, &rte_option_list, next)
printf(" --%-*s %s\n", 17, option->name, option->usage);
printf("\n");
}