]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
bridge: Deduplicate vlan show functions
authorBenjamin Poirier <bpoirier@cumulusnetworks.com>
Mon, 16 Dec 2019 06:43:43 +0000 (15:43 +0900)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 17 Dec 2019 04:49:28 +0000 (20:49 -0800)
print_vlan() and print_vlan_tunnel() are almost identical copies, save for
a missing newline in the latter which leads to broken output of "vlan
tunnelshow" in normal mode.

Fixes: c7c1a1ef51ae ("bridge: colorize output and use JSON print library")
Signed-off-by: Benjamin Poirier <bpoirier@cumulusnetworks.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
bridge/vlan.c
testsuite/tests/bridge/vlan/tunnelshow.t

index 428eeee3c5aef2ebb8a70185c00d47654db345b7..19283bca7c98648d534d860318aedacf92d1d6c6 100644 (file)
 #include "utils.h"
 
 static unsigned int filter_index, filter_vlan;
-static int show_vlan_tunnel_info = 0;
+
+enum vlan_show_subject {
+       VLAN_SHOW_VLAN,
+       VLAN_SHOW_TUNNELINFO,
+};
 
 static void usage(void)
 {
@@ -278,7 +282,7 @@ static void print_range(const char *name, __u32 start, __u32 id)
 
 }
 
-static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
+static void print_vlan_tunnel_info(struct rtattr *tb, int ifindex)
 {
        struct rtattr *i, *list = tb;
        int rem = RTA_PAYLOAD(list);
@@ -347,52 +351,9 @@ static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
                close_vlan_port();
 }
 
-static int print_vlan_tunnel(struct nlmsghdr *n, void *arg)
-{
-       struct ifinfomsg *ifm = NLMSG_DATA(n);
-       struct rtattr *tb[IFLA_MAX+1];
-       int len = n->nlmsg_len;
-       FILE *fp = arg;
-
-       if (n->nlmsg_type != RTM_NEWLINK) {
-               fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n",
-                       n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
-               return 0;
-       }
-
-       len -= NLMSG_LENGTH(sizeof(*ifm));
-       if (len < 0) {
-               fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
-               return -1;
-       }
-
-       if (ifm->ifi_family != AF_BRIDGE)
-               return 0;
-
-       if (filter_index && filter_index != ifm->ifi_index)
-               return 0;
-
-       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len);
-
-       /* if AF_SPEC isn't there, vlan table is not preset for this port */
-       if (!tb[IFLA_AF_SPEC]) {
-               if (!filter_vlan && !is_json_context()) {
-                       color_fprintf(fp, COLOR_IFNAME, "%s",
-                                     ll_index_to_name(ifm->ifi_index));
-                       fprintf(fp, "\tNone\n");
-               }
-               return 0;
-       }
-
-       print_vlan_tunnel_info(fp, tb[IFLA_AF_SPEC], ifm->ifi_index);
-
-       fflush(fp);
-       return 0;
-}
-
 static int print_vlan(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = arg;
+       enum vlan_show_subject *subject = arg;
        struct ifinfomsg *ifm = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr *tb[IFLA_MAX+1];
@@ -420,17 +381,24 @@ static int print_vlan(struct nlmsghdr *n, void *arg)
        /* if AF_SPEC isn't there, vlan table is not preset for this port */
        if (!tb[IFLA_AF_SPEC]) {
                if (!filter_vlan && !is_json_context()) {
-                       color_fprintf(fp, COLOR_IFNAME, "%s",
+                       color_fprintf(stdout, COLOR_IFNAME, "%s",
                                      ll_index_to_name(ifm->ifi_index));
-                       fprintf(fp, "\tNone\n");
+                       fprintf(stdout, "\tNone\n");
                }
                return 0;
        }
 
-       print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index);
+       switch (*subject) {
+       case VLAN_SHOW_VLAN:
+               print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index);
+               break;
+       case VLAN_SHOW_TUNNELINFO:
+               print_vlan_tunnel_info(tb[IFLA_AF_SPEC], ifm->ifi_index);
+               break;
+       }
        print_string(PRINT_FP, NULL, "%s", _SL_);
 
-       fflush(fp);
+       fflush(stdout);
        return 0;
 }
 
@@ -543,7 +511,7 @@ static int print_vlan_stats(struct nlmsghdr *n, void *arg)
        return 0;
 }
 
-static int vlan_show(int argc, char **argv)
+static int vlan_show(int argc, char **argv, int subject)
 {
        char *filter_dev = NULL;
        int ret = 0;
@@ -581,17 +549,13 @@ static int vlan_show(int argc, char **argv)
                }
 
                if (!is_json_context()) {
-                       if (show_vlan_tunnel_info)
-                               printf("port\tvlan ids\ttunnel id\n");
-                       else
-                               printf("port\tvlan ids\n");
+                       printf("port\tvlan ids");
+                       if (subject == VLAN_SHOW_TUNNELINFO)
+                               printf("\ttunnel id");
+                       printf("\n");
                }
 
-               if (show_vlan_tunnel_info)
-                       ret = rtnl_dump_filter(&rth, print_vlan_tunnel,
-                                              stdout);
-               else
-                       ret = rtnl_dump_filter(&rth, print_vlan, stdout);
+               ret = rtnl_dump_filter(&rth, print_vlan, &subject);
                if (ret < 0) {
                        fprintf(stderr, "Dump ternminated\n");
                        exit(1);
@@ -677,15 +641,14 @@ int do_vlan(int argc, char **argv)
                if (matches(*argv, "show") == 0 ||
                    matches(*argv, "lst") == 0 ||
                    matches(*argv, "list") == 0)
-                       return vlan_show(argc-1, argv+1);
+                       return vlan_show(argc-1, argv+1, VLAN_SHOW_VLAN);
                if (matches(*argv, "tunnelshow") == 0) {
-                       show_vlan_tunnel_info = 1;
-                       return vlan_show(argc-1, argv+1);
+                       return vlan_show(argc-1, argv+1, VLAN_SHOW_TUNNELINFO);
                }
                if (matches(*argv, "help") == 0)
                        usage();
        } else {
-               return vlan_show(0, NULL);
+               return vlan_show(0, NULL, VLAN_SHOW_VLAN);
        }
 
        fprintf(stderr, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv);
index 1583abb92f47229902689d8242299ac36049ee55..b2141e7c5ea4cf1f9f60329255b8ee7af0df0cac 100755 (executable)
@@ -16,9 +16,16 @@ ts_ip "$0" "Enslave $VX_DEV under $BR_DEV" \
 ts_ip "$0" "Set vlan_tunnel on $VX_DEV" \
        link set dev $VX_DEV type bridge_slave vlan_tunnel on
 
+ts_bridge "$0" "Add single vlan" vlan add dev $VX_DEV vid 1000
+ts_bridge "$0" "Add single tunnel" \
+       vlan add dev $VX_DEV vid 1000 tunnel_info id 1000
+ts_bridge "$0" "Add vlan range" vlan add dev $VX_DEV vid 1010-1020
+ts_bridge "$0" "Add tunnel range" \
+       vlan add dev $VX_DEV vid 1010-1020 tunnel_info id 1010-1020
 ts_bridge "$0" "Add single vlan" vlan add dev $VX_DEV vid 1030
 ts_bridge "$0" "Add tunnel with vni > 16k" \
        vlan add dev $VX_DEV vid 1030 tunnel_info id 65556
 
 ts_bridge "$0" "Show tunnel info" vlan tunnelshow dev $VX_DEV
 test_on "1030\s+65556"
+test_lines_count 5