mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-10-31 12:45:23 +08:00
Refactor egress pacer option parsing
This commit is contained in:
@@ -63,7 +63,7 @@ static void usage(const char *program)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int parse_u64(const char *value, unsigned long long maximum,
|
static int parse_u64(const char *value, unsigned long long maximum,
|
||||||
unsigned long long *result)
|
unsigned long long *result)
|
||||||
{
|
{
|
||||||
char *end = NULL;
|
char *end = NULL;
|
||||||
unsigned long long parsed;
|
unsigned long long parsed;
|
||||||
@@ -76,6 +76,65 @@ static int parse_u64(const char *value, unsigned long long maximum,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int parse_rate(const char *value)
|
||||||
|
{
|
||||||
|
unsigned long long parsed;
|
||||||
|
|
||||||
|
if (parse_u64(value, 100000000, &parsed) || parsed < 8) {
|
||||||
|
fprintf(stderr, "invalid rate in Kbit/s: %s\n", value);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
env.rate_kbps = parsed;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_queue_limit(const char *value)
|
||||||
|
{
|
||||||
|
unsigned long long parsed;
|
||||||
|
|
||||||
|
if (parse_u64(value, 65535, &parsed) || parsed == 0) {
|
||||||
|
fprintf(stderr, "invalid queue limit: %s\n", value);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
env.queue_limit = parsed;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_duration(const char *value)
|
||||||
|
{
|
||||||
|
unsigned long long parsed;
|
||||||
|
|
||||||
|
if (parse_u64(value, 86400, &parsed) || parsed == 0) {
|
||||||
|
fprintf(stderr, "invalid duration in seconds: %s\n", value);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
env.duration = parsed;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_option(int option, const char *program)
|
||||||
|
{
|
||||||
|
switch (option) {
|
||||||
|
case 'i':
|
||||||
|
env.interface = optarg;
|
||||||
|
return 0;
|
||||||
|
case 'r':
|
||||||
|
return parse_rate(optarg);
|
||||||
|
case 'q':
|
||||||
|
return parse_queue_limit(optarg);
|
||||||
|
case 'd':
|
||||||
|
return parse_duration(optarg);
|
||||||
|
case 'v':
|
||||||
|
env.verbose = true;
|
||||||
|
return 0;
|
||||||
|
case 'h':
|
||||||
|
usage(program);
|
||||||
|
exit(0);
|
||||||
|
default:
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int parse_args(int argc, char **argv)
|
static int parse_args(int argc, char **argv)
|
||||||
{
|
{
|
||||||
static const struct option options[] = {
|
static const struct option options[] = {
|
||||||
@@ -87,44 +146,12 @@ static int parse_args(int argc, char **argv)
|
|||||||
{ "help", no_argument, NULL, 'h' },
|
{ "help", no_argument, NULL, 'h' },
|
||||||
{},
|
{},
|
||||||
};
|
};
|
||||||
unsigned long long parsed;
|
int error, option;
|
||||||
int option;
|
|
||||||
|
|
||||||
while ((option = getopt_long(argc, argv, "i:r:q:d:vh", options, NULL)) != -1) {
|
while ((option = getopt_long(argc, argv, "i:r:q:d:vh", options, NULL)) != -1) {
|
||||||
switch (option) {
|
error = parse_option(option, argv[0]);
|
||||||
case 'i':
|
if (error)
|
||||||
env.interface = optarg;
|
return error;
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
if (parse_u64(optarg, 100000000, &parsed) || parsed < 8) {
|
|
||||||
fprintf(stderr, "invalid rate in Kbit/s: %s\n", optarg);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
env.rate_kbps = parsed;
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
if (parse_u64(optarg, 65535, &parsed) || parsed == 0) {
|
|
||||||
fprintf(stderr, "invalid queue limit: %s\n", optarg);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
env.queue_limit = parsed;
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
if (parse_u64(optarg, 86400, &parsed) || parsed == 0) {
|
|
||||||
fprintf(stderr, "invalid duration in seconds: %s\n", optarg);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
env.duration = parsed;
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
env.verbose = true;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(0);
|
|
||||||
default:
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!env.interface) {
|
if (!env.interface) {
|
||||||
|
|||||||
Reference in New Issue
Block a user