Use print_string() instead of fputs() and fprintf() to print encapsulation for !is_json_context().
Introduce and use tnl_encap_optstr() to format encapsulation option string according to tempate and given values to avoid code duplication and simplify it. Signed-off-by: Serhey Popovych <serhe.popov...@gmail.com> --- ip/link_gre.c | 54 +++++++++++++++++++++--------------------------------- ip/link_gre6.c | 46 ++++++++++++++++++++++------------------------ ip/link_ip6tnl.c | 40 +++++++++++++++++++--------------------- ip/link_iptnl.c | 52 ++++++++++++++++++++++------------------------------ ip/tunnel.c | 24 ++++++++++++++++++++++++ ip/tunnel.h | 1 + 6 files changed, 109 insertions(+), 108 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index 2ae2194..b70f73b 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -491,50 +491,38 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); + print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - - print_bool(PRINT_JSON, - "csum", - NULL, + print_bool(PRINT_JSON, "csum", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM); - - print_bool(PRINT_JSON, - "csum6", - NULL, + print_bool(PRINT_JSON, "csum6", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM6); - - print_bool(PRINT_JSON, - "remcsum", - NULL, + print_bool(PRINT_JSON, "remcsum", NULL, flags & TUNNEL_ENCAP_FLAG_REMCSUM); close_json_object(); } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); + int t; - fprintf(f, "encap-dport %u ", ntohs(dport)); + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("sport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("dport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum", t, -1)); - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("remcsum", t, -1)); } } } diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 9576354..41180bb 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -538,40 +538,38 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); + print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); print_bool(PRINT_JSON, "csum", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); + flags & TUNNEL_ENCAP_FLAG_CSUM); print_bool(PRINT_JSON, "csum6", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM6); + flags & TUNNEL_ENCAP_FLAG_CSUM6); print_bool(PRINT_JSON, "remcsum", NULL, - flags & TUNNEL_ENCAP_FLAG_REMCSUM); + flags & TUNNEL_ENCAP_FLAG_REMCSUM); + close_json_object(); } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); + int t; - fprintf(f, "encap-dport %u ", ntohs(dport)); + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("sport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("dport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum", t, -1)); - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("remcsum", t, -1)); } } } diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index 9594c3e..aa6f6fa 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -479,10 +479,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb } if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); + print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); print_bool(PRINT_JSON, "csum", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM); @@ -490,29 +487,30 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb flags & TUNNEL_ENCAP_FLAG_CSUM6); print_bool(PRINT_JSON, "remcsum", NULL, flags & TUNNEL_ENCAP_FLAG_REMCSUM); + close_json_object(); } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); + int t; - fprintf(f, "encap-dport %u ", ntohs(dport)); + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("sport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("dport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum", t, -1)); - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("remcsum", t, -1)); } } } diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index fcb0795..83a524f 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -510,46 +510,38 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ } if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); + print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, - "csum", - NULL, + print_bool(PRINT_JSON, "csum", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, - "csum6", - NULL, + print_bool(PRINT_JSON, "csum6", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM6); - print_bool(PRINT_JSON, - "remcsum", - NULL, + print_bool(PRINT_JSON, "remcsum", NULL, flags & TUNNEL_ENCAP_FLAG_REMCSUM); + close_json_object(); } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); + int t; - fprintf(f, "encap-dport %u ", ntohs(dport)); + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("sport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("dport", 1, t)); - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum", t, -1)); - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_optstr("remcsum", t, -1)); } } } diff --git a/ip/tunnel.c b/ip/tunnel.c index f860103..1305e12 100644 --- a/ip/tunnel.c +++ b/ip/tunnel.c @@ -200,6 +200,30 @@ __be32 tnl_parse_key(const char *name, const char *key) return htonl(uval); } +const char *tnl_encap_optstr(const char *name, int enabled, int port) +{ + static const char ne[][sizeof("no")] = { + [0] = "no", + [1] = "", + }; + static char buf[32]; + char b1[16]; + const char *val; + + if (!port) { + val = "auto"; + } else if (port < 0) { + val = ""; + } else { + snprintf(b1, sizeof(b1), "%u", port - 1); + val = b1; + } + + snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val); + return buf; +} + + /* tnl_print_stats - print tunnel statistics * * @buf - tunnel interface's line in /proc/net/dev, diff --git a/ip/tunnel.h b/ip/tunnel.h index 9a03c0d..7af3592 100644 --- a/ip/tunnel.h +++ b/ip/tunnel.h @@ -32,6 +32,7 @@ int tnl_prl_ioctl(int cmd, const char *name, void *p); int tnl_6rd_ioctl(int cmd, const char *name, void *p); int tnl_ioctl_get_6rd(const char *name, void *p); __be32 tnl_parse_key(const char *name, const char *key); +const char *tnl_encap_optstr(const char *name, int enabled, int port); void tnl_print_stats(const char *buf); #endif -- 1.7.10.4