From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC 2/2] vxlan: make option printing more consistent
Date: Tue, 23 May 2023 09:59:32 -0700 [thread overview]
Message-ID: <20230523165932.8376-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20230523165932.8376-1-stephen@networkplumber.org>
Add new helper function print_bool_opt() which prints
with no prefix and use it for vxlan options.
Based on discussion around how to handle new localbypass option.
Initial version of this was from Ido Schimmel.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
include/json_print.h | 9 +++++
ip/iplink_vxlan.c | 80 ++++++++++++++++----------------------------
lib/json_print.c | 18 ++++++++++
3 files changed, 55 insertions(+), 52 deletions(-)
diff --git a/include/json_print.h b/include/json_print.h
index 91b34571ceb0..4d165a91c23a 100644
--- a/include/json_print.h
+++ b/include/json_print.h
@@ -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);
+
+static inline int print_bool_opt(enum output_type type,
+ const char *key, bool value)
+{
+ return print_color_bool_opt(type, COLOR_NONE, key, value);
+}
+
/* A backdoor to the size formatter. Please use print_size() instead. */
char *sprint_size(__u32 sz, char *buf);
diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c
index cb6745c74507..292e19cdb940 100644
--- a/ip/iplink_vxlan.c
+++ b/ip/iplink_vxlan.c
@@ -427,15 +427,13 @@ 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_COLLECT_METADATA])
+ print_bool_opt(PRINT_ANY, "external",
+ rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA]));
- if (tb[IFLA_VXLAN_VNIFILTER] &&
- rta_getattr_u8(tb[IFLA_VXLAN_VNIFILTER])) {
- print_bool(PRINT_ANY, "vnifilter", "vnifilter", true);
- }
+ if (tb[IFLA_VXLAN_VNIFILTER])
+ print_bool_opt(PRINT_ANY, "vnifilter",
+ rta_getattr_u8(tb[IFLA_VXLAN_VNIFILTER]));
if (tb[IFLA_VXLAN_ID] &&
RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) >= sizeof(__u32)) {
@@ -532,22 +530,24 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
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);
+ print_bool_opt(PRINT_ANY, "learning", learning);
}
- if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
- print_bool(PRINT_ANY, "proxy", "proxy ", true);
+ if (tb[IFLA_VXLAN_PROXY])
+ print_bool_opt(PRINT_ANY, "proxy",
+ rta_getattr_u8(tb[IFLA_VXLAN_PROXY]));
- if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
- print_bool(PRINT_ANY, "rsc", "rsc ", true);
+ if (tb[IFLA_VXLAN_RSC])
+ print_bool_opt(PRINT_ANY, "rsc",
+ rta_getattr_u8(tb[IFLA_VXLAN_RSC]));
- if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
- print_bool(PRINT_ANY, "l2miss", "l2miss ", true);
+ if (tb[IFLA_VXLAN_L2MISS])
+ print_bool_opt(PRINT_ANY, "l2miss",
+ rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]));
- if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
- print_bool(PRINT_ANY, "l3miss", "l3miss ", true);
+ if (tb[IFLA_VXLAN_L3MISS])
+ print_bool_opt(PRINT_ANY, "l3miss",
+ rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]));
if (tb[IFLA_VXLAN_TOS])
tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]);
@@ -604,51 +604,27 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
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);
- }
+ print_bool_opt(PRINT_ANY, "udp_csum", udp_csum);
}
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);
- }
+ print_bool_opt(PRINT_ANY, "udp_zero_csum6_tx", csum6);
}
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);
- }
+ print_bool_opt(PRINT_ANY, "udp_zero_csum6_rx", csum6);
}
- 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_REMCSUM_TX])
+ print_bool_opt(PRINT_ANY, "remcsum_tx",
+ rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]));
+ if (tb[IFLA_VXLAN_REMCSUM_RX])
+ print_bool_opt(PRINT_ANY, "remcsum_rx",
+ rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]));
if (tb[IFLA_VXLAN_GBP])
print_null(PRINT_ANY, "gbp", "gbp ", NULL);
if (tb[IFLA_VXLAN_GPE])
diff --git a/lib/json_print.c b/lib/json_print.c
index d7ee76b10de8..29959e7335c3 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -215,6 +215,24 @@ int print_color_bool(enum output_type type,
value ? "true" : "false");
}
+/* In JSON mode, acts like print_color_bool
+ * otherwise, prints key with no prefix if false
+ */
+int print_color_bool_opt(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ bool value)
+{
+ int ret = 0;
+
+ if (_IS_JSON_CONTEXT(type))
+ jsonw_bool_field(_jw, key, value);
+ else if (_IS_FP_CONTEXT(type))
+ 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,
--
2.39.2
next prev parent reply other threads:[~2023-05-23 16:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-23 16:59 [RFC 1/2] vxlan: use print_nll for gbp and gpe Stephen Hemminger
2023-05-23 16:59 ` Stephen Hemminger [this message]
2023-05-24 18:06 ` [RFC 2/2] vxlan: make option printing more consistent Andrea Claudi
2023-05-24 18:44 ` Stephen Hemminger
2023-05-24 21:08 ` Stephen Hemminger
2023-05-25 16:16 ` Andrea Claudi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230523165932.8376-2-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).