Introduced functions get_ipvlan_mode() and get_ipvlan_mode_name() to convert between name <-> IPVLAN_MODE_XXX. Next patch will add new mode and inplace code is becoming too large. Signed-off-by: Dmitry Skorodumov --- ip/iplink_ipvlan.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/ip/iplink_ipvlan.c b/ip/iplink_ipvlan.c index f29fa4f9..691fd6f3 100644 --- a/ip/iplink_ipvlan.c +++ b/ip/iplink_ipvlan.c @@ -25,6 +25,31 @@ static void print_explain(struct link_util *lu, FILE *f) lu->id); } +static int get_ipvlan_mode(const char *mode) +{ + if (strcmp(mode, "l2") == 0) + return IPVLAN_MODE_L2; + if (strcmp(mode, "l3") == 0) + return IPVLAN_MODE_L3; + if (strcmp(mode, "l3s") == 0) + return IPVLAN_MODE_L3S; + return -1; +} + +static const char *get_ipvlan_mode_name(__u16 mode) +{ + switch (mode) { + case IPVLAN_MODE_L2: + return "l2"; + case IPVLAN_MODE_L3: + return "l3"; + case IPVLAN_MODE_L3S: + return "l3s"; + default: + return "unknown"; + } +} + static int ipvlan_parse_opt(struct link_util *lu, int argc, char **argv, struct nlmsghdr *n) { @@ -33,21 +58,17 @@ static int ipvlan_parse_opt(struct link_util *lu, int argc, char **argv, while (argc > 0) { if (matches(*argv, "mode") == 0) { - __u16 mode = 0; + int mode; NEXT_ARG(); - if (strcmp(*argv, "l2") == 0) - mode = IPVLAN_MODE_L2; - else if (strcmp(*argv, "l3") == 0) - mode = IPVLAN_MODE_L3; - else if (strcmp(*argv, "l3s") == 0) - mode = IPVLAN_MODE_L3S; - else { - fprintf(stderr, "Error: argument of \"mode\" must be either \"l2\", \"l3\" or \"l3s\"\n"); + mode = get_ipvlan_mode(*argv); + if (mode < 0) { + fprintf(stderr, "Error: argument of \"mode\" must be either " + "\"l2\", \"l3\" or \"l3s\"\n"); return -1; } - addattr16(n, 1024, IFLA_IPVLAN_MODE, mode); + addattr16(n, 1024, IFLA_IPVLAN_MODE, (__u16)mode); } else if (matches(*argv, "private") == 0 && !mflag_given) { flags |= IPVLAN_F_PRIVATE; mflag_given = true; @@ -82,9 +103,7 @@ static void ipvlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_IPVLAN_MODE]) { if (RTA_PAYLOAD(tb[IFLA_IPVLAN_MODE]) == sizeof(__u16)) { __u16 mode = rta_getattr_u16(tb[IFLA_IPVLAN_MODE]); - const char *mode_str = mode == IPVLAN_MODE_L2 ? "l2" : - mode == IPVLAN_MODE_L3 ? "l3" : - mode == IPVLAN_MODE_L3S ? "l3s" : "unknown"; + const char *mode_str = get_ipvlan_mode_name(mode); print_string(PRINT_ANY, "mode", " mode %s ", mode_str); } -- 2.25.1