]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iplink_vlan.c
man: dcb-ets: Remove an unnecessary empty line
[mirror_iproute2.git] / ip / iplink_vlan.c
index 94b52ae55c73bbcaca1322af62aedb399aab3d5c..1426f2afca23ffe7ae14da9a589f82dd1eb3d770 100644 (file)
 #include "utils.h"
 #include "ip_common.h"
 
-static void explain(void)
+static void print_explain(FILE *f)
 {
-       fprintf(stderr,
-               "Usage: ... vlan [ protocol VLANPROTO ] id VLANID"
-               "                [ FLAG-LIST ]\n"
-               "                [ ingress-qos-map QOS-MAP ] [ egress-qos-map QOS-MAP ]\n"
+       fprintf(f,
+               "Usage: ... vlan id VLANID\n"
+               "               [ protocol VLANPROTO ]\n"
+               "               [ reorder_hdr { on | off } ]\n"
+               "               [ gvrp { on | off } ]\n"
+               "               [ mvrp { on | off } ]\n"
+               "               [ loose_binding { on | off } ]\n"
+               "               [ bridge_binding { on | off } ]\n"
+               "               [ ingress-qos-map QOS-MAP ]\n"
+               "               [ egress-qos-map QOS-MAP ]\n"
                "\n"
-               "VLANPROTO: [ 802.1Q / 802.1ad ]\n"
                "VLANID := 0-4095\n"
-               "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
-               "FLAG := [ reorder_hdr { on | off } ] [ gvrp { on | off } ] [ mvrp { on | off } ]\n"
-               "        [ loose_binding { on | off } ]\n"
+               "VLANPROTO: [ 802.1Q | 802.1ad ]\n"
                "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
                "QOS-MAPPING := FROM:TO\n"
        );
 }
 
+static void explain(void)
+{
+       print_explain(stderr);
+}
+
 static int on_off(const char *msg, const char *arg)
 {
        fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg, arg);
        return -1;
 }
 
-static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
-                             int attrtype)
+static int parse_qos_mapping(__u32 key, char *value, void *data)
 {
-       int argc = *argcp;
-       char **argv = *argvp;
-       struct ifla_vlan_qos_mapping m;
-       struct rtattr *tail;
+       struct nlmsghdr *n = data;
+       struct ifla_vlan_qos_mapping m = {
+               .from = key,
+       };
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, 1024, attrtype, NULL, 0);
+       if (get_u32(&m.to, value, 0))
+               return 1;
 
-       while (argc > 0) {
-               char *colon = strchr(*argv, ':');
+       return addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
+}
 
-               if (!colon)
-                       break;
-               *colon = '\0';
+static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
+                             int attrtype)
+{
+       struct rtattr *tail;
 
-               if (get_u32(&m.from, *argv, 0))
-                       return 1;
-               if (get_u32(&m.to, colon + 1, 0))
-                       return 1;
-               argc--, argv++;
+       tail = addattr_nest(n, 1024, attrtype);
 
-               addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
-       }
+       if (parse_mapping(argcp, argvp, false, &parse_qos_mapping, n))
+               return 1;
 
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *)tail;
-
-       *argcp = argc;
-       *argvp = argv;
+       addattr_nest_end(n, tail);
        return 0;
 }
 
@@ -128,6 +129,15 @@ static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
                                flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
                        else
                                return on_off("loose_binding", *argv);
+               } else if (matches(*argv, "bridge_binding") == 0) {
+                       NEXT_ARG();
+                       flags.mask |= VLAN_FLAG_BRIDGE_BINDING;
+                       if (strcmp(*argv, "on") == 0)
+                               flags.flags |= VLAN_FLAG_BRIDGE_BINDING;
+                       else if (strcmp(*argv, "off") == 0)
+                               flags.flags &= ~VLAN_FLAG_BRIDGE_BINDING;
+                       else
+                               return on_off("bridge_binding", *argv);
                } else if (matches(*argv, "ingress-qos-map") == 0) {
                        NEXT_ARG();
                        if (vlan_parse_qos_map(&argc, &argv, n,
@@ -157,42 +167,59 @@ static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
        return 0;
 }
 
-static void vlan_print_map(FILE *f, char *name, struct rtattr *attr)
+static void vlan_print_map(FILE *f,
+                          const char *name_json,
+                          const char *name_fp,
+                          struct rtattr *attr)
 {
        struct ifla_vlan_qos_mapping *m;
        struct rtattr *i;
        int rem;
 
-       fprintf(f, "\n      %s { ", name);
+       open_json_array(PRINT_JSON, name_json);
+       print_nl();
+       print_string(PRINT_FP, NULL, "      %s { ", name_fp);
 
        rem = RTA_PAYLOAD(attr);
        for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
                m = RTA_DATA(i);
-               fprintf(f, "%u:%u ", m->from, m->to);
+
+               if (is_json_context()) {
+                       open_json_object(NULL);
+                       print_uint(PRINT_JSON, "from", NULL, m->from);
+                       print_uint(PRINT_JSON, "to", NULL, m->to);
+                       close_json_object();
+               } else {
+                       fprintf(f, "%u:%u ", m->from, m->to);
+               }
        }
-       fprintf(f, "} ");
+
+       close_json_array(PRINT_JSON, NULL);
+       print_string(PRINT_FP, NULL, "%s ", "}");
 }
 
 static void vlan_print_flags(FILE *fp, __u32 flags)
 {
-       fprintf(fp, "<");
-#define _PF(f) if (flags & VLAN_FLAG_##f) { \
-                       flags &= ~ VLAN_FLAG_##f; \
-                       fprintf(fp, #f "%s", flags ? "," : ""); \
-               }
+       open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
+#define _PF(f) if (flags & VLAN_FLAG_##f) {                            \
+               flags &= ~VLAN_FLAG_##f;                                \
+               print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \
+       }
        _PF(REORDER_HDR);
        _PF(GVRP);
        _PF(MVRP);
        _PF(LOOSE_BINDING);
+       _PF(BRIDGE_BINDING);
 #undef _PF
        if (flags)
-               fprintf(fp, "%x", flags);
-       fprintf(fp, "> ");
+               print_hex(PRINT_ANY, NULL, "%x", flags);
+       close_json_array(PRINT_ANY, "> ");
 }
 
 static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
        struct ifla_vlan_flags *flags;
+
        SPRINT_BUF(b1);
 
        if (!tb)
@@ -206,13 +233,19 @@ static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                return;
 
        if (tb[IFLA_VLAN_PROTOCOL])
-               fprintf(f, "protocol %s ",
-                       ll_proto_n2a(rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]),
+               print_string(PRINT_ANY,
+                            "protocol",
+                            "protocol %s ",
+                            ll_proto_n2a(
+                                    rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]),
                                     b1, sizeof(b1)));
        else
-               fprintf(f, "protocol 802.1q ");
+               print_string(PRINT_ANY, "protocol", "protocol %s ", "802.1q");
 
-       fprintf(f, "id %u ", rta_getattr_u16(tb[IFLA_VLAN_ID]));
+       print_uint(PRINT_ANY,
+                  "id",
+                  "id %u ",
+                  rta_getattr_u16(tb[IFLA_VLAN_ID]));
 
        if (tb[IFLA_VLAN_FLAGS]) {
                if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
@@ -221,9 +254,21 @@ static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                vlan_print_flags(f, flags->flags);
        }
        if (tb[IFLA_VLAN_INGRESS_QOS])
-               vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]);
+               vlan_print_map(f,
+                              "ingress_qos",
+                              "ingress-qos-map",
+                              tb[IFLA_VLAN_INGRESS_QOS]);
        if (tb[IFLA_VLAN_EGRESS_QOS])
-               vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]);
+               vlan_print_map(f,
+                              "egress_qos",
+                              "egress-qos-map",
+                              tb[IFLA_VLAN_EGRESS_QOS]);
+}
+
+static void vlan_print_help(struct link_util *lu, int argc, char **argv,
+                           FILE *f)
+{
+       print_explain(f);
 }
 
 struct link_util vlan_link_util = {
@@ -231,4 +276,5 @@ struct link_util vlan_link_util = {
        .maxattr        = IFLA_VLAN_MAX,
        .parse_opt      = vlan_parse_opt,
        .print_opt      = vlan_print_opt,
+       .print_help     = vlan_print_help,
 };