From: Mohsin Bashir Add parse_scope() and parse_rate() helpers in preparation for adding the group command which will need scope and rate parsing at various places in the code. This patch will help to avoid code duplication. Verify that the tool works as before: ./netshaper set dev eth2 handle scope queue id 0 bw-max 100kbit ./netshaper delete dev eth2 handle scope queue id 0 bw-max 100kbit Reviewed-by: Claude Assistant Signed-off-by: Jakub Kicinski Signed-off-by: Mohsin Bashir --- include/utils.h | 1 + netshaper/netshaper.c | 63 ++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/include/utils.h b/include/utils.h index e4e318e2..f0d45bad 100644 --- a/include/utils.h +++ b/include/utils.h @@ -260,6 +260,7 @@ unsigned int print_name_and_link(const char *fmt, __attribute__((format(printf, 1, 0))); +#define BITS_PER_BYTE 8 #define BIT(nr) (UINT64_C(1) << (nr)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) diff --git a/netshaper/netshaper.c b/netshaper/netshaper.c index 47fb805e..f90ceb11 100644 --- a/netshaper/netshaper.c +++ b/netshaper/netshaper.c @@ -47,6 +47,26 @@ static const char *net_shaper_scope_names[NET_SHAPER_SCOPE_MAX + 1] = { "node" }; +static int parse_scope(const char *str) +{ + for (int i = 1; i <= NET_SHAPER_SCOPE_MAX; i++) { + if (strcmp(str, net_shaper_scope_names[i]) == 0) + return i; + } + return -1; +} + +static int parse_rate(const char *str, __u64 *rate_bps) +{ + if (get_rate64(rate_bps, str)) { + fprintf(stderr, "Invalid rate value \"%s\"\n", str); + return -1; + } + /* get_rate64 returns bytes/sec, convert to bits/sec */ + *rate_bps *= BITS_PER_BYTE; + return 0; +} + static void print_netshaper_attrs(struct nlmsghdr *answer) { struct genlmsghdr *ghdr = NLMSG_DATA(answer); @@ -117,12 +137,8 @@ static int do_cmd(int argc, char **argv, int cmd) ifindex = ll_name_to_index(*argv); } else if (strcmp(*argv, "bw-max") == 0) { NEXT_ARG(); - if (get_rate64(&bw_max_bps, *argv)) { - fprintf(stderr, "Invalid bw-max value\n"); + if (parse_rate(*argv, &bw_max_bps)) return -1; - } - /* Convert Bps to bps */ - bw_max_bps *= 8; } else if (strcmp(*argv, "handle") == 0) { handle_present = true; NEXT_ARG(); @@ -134,34 +150,24 @@ static int do_cmd(int argc, char **argv, int cmd) } NEXT_ARG(); - if (strcmp(*argv, "netdev") == 0) { - handle_scope = NET_SHAPER_SCOPE_NETDEV; - /* For netdev scope, id is optional - check if next arg is "id" */ + handle_scope = parse_scope(*argv); + if (handle_scope < 0) { + fprintf(stderr, "Invalid scope \"%s\"\n", *argv); + return -1; + } + + if (handle_scope == NET_SHAPER_SCOPE_NETDEV) { + /* For netdev scope, id is optional */ if (argc > 1 && strcmp(argv[1], "id") == 0) { - NEXT_ARG(); /* move to "id" */ - NEXT_ARG(); /* move to id value */ + NEXT_ARG(); + NEXT_ARG(); if (get_unsigned(&handle_id, *argv, 10)) { fprintf(stderr, "Invalid handle id\n"); return -1; } } - } else if (strcmp(*argv, "queue") == 0) { - handle_scope = NET_SHAPER_SCOPE_QUEUE; - /* For queue scope, id is required */ - NEXT_ARG(); - if (strcmp(*argv, "id") != 0) { - fprintf(stderr, "What is \"%s\"\n", *argv); - usage(); - return -1; - } - NEXT_ARG(); - if (get_unsigned(&handle_id, *argv, 10)) { - fprintf(stderr, "Invalid handle id\n"); - return -1; - } - } else if (strcmp(*argv, "node") == 0) { - handle_scope = NET_SHAPER_SCOPE_NODE; - /* For node scope, id is required */ + } else { + /* For queue/node scope, id is required */ NEXT_ARG(); if (strcmp(*argv, "id") != 0) { fprintf(stderr, "What is \"%s\"\n", *argv); @@ -173,9 +179,6 @@ static int do_cmd(int argc, char **argv, int cmd) fprintf(stderr, "Invalid handle id\n"); return -1; } - } else { - fprintf(stderr, "Invalid scope\n"); - return -1; } } else { fprintf(stderr, "What is \"%s\"\n", *argv); -- 2.53.0-Meta