]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
vxlan: make option printing more consistent
authorStephen Hemminger <stephen@networkplumber.org>
Tue, 23 May 2023 16:57:15 +0000 (09:57 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 30 May 2023 19:32:22 +0000 (12:32 -0700)
Add new helper function print_bool_opt() which prints
with no prefix and use it for vxlan options.

If the option matches the expected default value,
it is not printed if in non JSON mode unless the details
setting is repeated.

Use a table for the vxlan options. This will change
the order of the printing of options.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
include/json_print.h
ip/iplink_vxlan.c
lib/json_print.c

index 91b34571ceb00007ac35f5d171e7cecf65d7f93b..49d3cc14789cea9a71cd125b9477f3cf531a86d0 100644 (file)
@@ -101,6 +101,15 @@ static inline int print_rate(bool use_iec, enum output_type t,
        return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
 }
 
+int print_color_bool_opt(enum output_type type, enum color_attr color,
+                        const char *key, bool value, bool show);
+
+static inline int print_bool_opt(enum output_type type,
+                                const char *key, bool value, bool show)
+{
+       return print_color_bool_opt(type, COLOR_NONE, key, value, show);
+}
+
 /* A backdoor to the size formatter. Please use print_size() instead. */
 char *sprint_size(__u32 sz, char *buf);
 
index cb6745c745074b091587c0d8c03565dd3eb5ff12..3053cdb861d5112c2c522e7f5af869004863200e 100644 (file)
 
 #define VXLAN_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
 
+static const struct vxlan_bool_opt {
+       const char *key;
+       int type;
+       bool default_value;
+} vxlan_opts[] = {
+       { "external",   IFLA_VXLAN_COLLECT_METADATA,    false },
+       { "vnifilter",  IFLA_VXLAN_VNIFILTER,           false },
+       { "learning",   IFLA_VXLAN_LEARNING,            true },
+       { "proxy",      IFLA_VXLAN_PROXY,               false },
+       { "rsc",        IFLA_VXLAN_RSC,                 false },
+       { "l2miss",     IFLA_VXLAN_L2MISS,              false },
+       { "l3miss",     IFLA_VXLAN_L3MISS,              false },
+       { "udp_csum",   IFLA_VXLAN_UDP_CSUM,            true },
+       { "udp_zero_csum6_tx", IFLA_VXLAN_UDP_ZERO_CSUM6_TX, false },
+       { "udp_zero_csum6_rx", IFLA_VXLAN_UDP_ZERO_CSUM6_RX, false },
+       { "remcsum_tx", IFLA_VXLAN_REMCSUM_TX,          false },
+       { "remcsum_rx", IFLA_VXLAN_REMCSUM_RX,          false },
+};
+
 static void print_explain(FILE *f)
 {
        fprintf(f,
@@ -420,6 +439,7 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
 
 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
+       unsigned int i;
        __u8 ttl = 0;
        __u8 tos = 0;
        __u32 maxaddr;
@@ -427,16 +447,6 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
        if (!tb)
                return;
 
-       if (tb[IFLA_VXLAN_COLLECT_METADATA] &&
-           rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA])) {
-               print_bool(PRINT_ANY, "external", "external ", true);
-       }
-
-       if (tb[IFLA_VXLAN_VNIFILTER] &&
-           rta_getattr_u8(tb[IFLA_VXLAN_VNIFILTER])) {
-               print_bool(PRINT_ANY, "vnifilter", "vnifilter", true);
-       }
-
        if (tb[IFLA_VXLAN_ID] &&
            RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) >= sizeof(__u32)) {
                print_uint(PRINT_ANY, "id", "id %u ", rta_getattr_u32(tb[IFLA_VXLAN_ID]));
@@ -529,26 +539,6 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                           "dstport %u ",
                           rta_getattr_be16(tb[IFLA_VXLAN_PORT]));
 
-       if (tb[IFLA_VXLAN_LEARNING]) {
-               __u8 learning = rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]);
-
-               print_bool(PRINT_JSON, "learning", NULL, learning);
-               if (!learning)
-                       print_bool(PRINT_FP, NULL, "nolearning ", true);
-       }
-
-       if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
-               print_bool(PRINT_ANY, "proxy", "proxy ", true);
-
-       if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
-               print_bool(PRINT_ANY, "rsc", "rsc ", true);
-
-       if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
-               print_bool(PRINT_ANY, "l2miss", "l2miss ", true);
-
-       if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
-               print_bool(PRINT_ANY, "l3miss", "l3miss ", true);
-
        if (tb[IFLA_VXLAN_TOS])
                tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]);
        if (tos) {
@@ -601,58 +591,22 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
            ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
                print_uint(PRINT_ANY, "limit", "maxaddr %u ", maxaddr);
 
-       if (tb[IFLA_VXLAN_UDP_CSUM]) {
-               __u8 udp_csum = rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]);
-
-               if (is_json_context()) {
-                       print_bool(PRINT_ANY, "udp_csum", NULL, udp_csum);
-               } else {
-                       if (!udp_csum)
-                               fputs("no", f);
-                       fputs("udpcsum ", f);
-               }
-       }
-
-       if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
-               __u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]);
-
-               if (is_json_context()) {
-                       print_bool(PRINT_ANY,
-                                  "udp_zero_csum6_tx", NULL, csum6);
-               } else {
-                       if (!csum6)
-                               fputs("no", f);
-                       fputs("udp6zerocsumtx ", f);
-               }
-       }
-
-       if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
-               __u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]);
-
-               if (is_json_context()) {
-                       print_bool(PRINT_ANY,
-                                  "udp_zero_csum6_rx",
-                                  NULL,
-                                  csum6);
-               } else {
-                       if (!csum6)
-                               fputs("no", f);
-                       fputs("udp6zerocsumrx ", f);
-               }
-       }
-
-       if (tb[IFLA_VXLAN_REMCSUM_TX] &&
-           rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]))
-               print_bool(PRINT_ANY, "remcsum_tx", "remcsumtx ", true);
-
-       if (tb[IFLA_VXLAN_REMCSUM_RX] &&
-           rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]))
-               print_bool(PRINT_ANY, "remcsum_rx", "remcsumrx ", true);
-
        if (tb[IFLA_VXLAN_GBP])
                print_null(PRINT_ANY, "gbp", "gbp ", NULL);
        if (tb[IFLA_VXLAN_GPE])
                print_null(PRINT_ANY, "gpe", "gpe ", NULL);
+
+       for (i = 0; i < ARRAY_SIZE(vxlan_opts); i++) {
+               const struct vxlan_bool_opt *opt = &vxlan_opts[i];
+               __u8 val;
+
+               if (!tb[opt->type])
+                       continue;
+               val = rta_getattr_u8(tb[opt->type]);
+
+               print_bool_opt(PRINT_ANY, opt->key, val,
+                              val != opt->default_value || show_details > 1);
+       }
 }
 
 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
index d7ee76b10de889dca155bbbddd3dc3600726999a..602de027ca27b8867d3b6795aa26e4ffb0a171a6 100644 (file)
@@ -215,6 +215,25 @@ int print_color_bool(enum output_type type,
                                  value ? "true" : "false");
 }
 
+/* In JSON mode, acts like print_color_bool.
+ * Otherwise, will print key with prefix of "no" if false.
+ * The show flag is used to suppres printing in non-JSON mode
+ */
+int print_color_bool_opt(enum output_type type,
+                        enum color_attr color,
+                        const char *key,
+                        bool value, bool show)
+{
+       int ret = 0;
+
+       if (_IS_JSON_CONTEXT(type))
+               jsonw_bool_field(_jw, key, value);
+       else if (_IS_FP_CONTEXT(type) && show)
+               ret = color_fprintf(stdout, color, "%s%s ",
+                                   value ? "" : "no", key);
+       return ret;
+}
+
 int print_color_on_off(enum output_type type,
                       enum color_attr color,
                       const char *key,