From patchwork Fri Jan 12 17:39:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 860093 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="uH6JKo1p"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94q63Gpz9t6x for ; Sat, 13 Jan 2018 04:40:27 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964974AbeALRk0 (ORCPT ); Fri, 12 Jan 2018 12:40:26 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:40312 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964847AbeALRkC (ORCPT ); Fri, 12 Jan 2018 12:40:02 -0500 Received: by mail-lf0-f68.google.com with SMTP id v74so4809239lfa.7 for ; Fri, 12 Jan 2018 09:40:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=Xlpef4AvAoeXcCl7QPPzyVsUNlgqSta9YgeJ7OFoE+U=; b=uH6JKo1pDVDcxTX7VLkgdb7fRkzrxONfyYT30N+Wom/HQr//1KbXIBA6yo6JGt56hN FbnJ/DxKbjo1tX57zIQ543pCijDHHfhonZuM5HDc9PHXRzZctgVbPzLQ5ojLOnt07bJZ BlreNCl00CVilH/uhlTSXxlFSUaRo3rwsgwHuhRYT90FKucQByAMBfRos8MQEMn2aWX0 XfGHQp/EFpdC0csHsSNS8P9R08qgXLyTszh50QNQPBEIsK1ewnT2sF3AbZPnFnoPuktI j45qhUQcaW1vVMw3YFzC3QEU9WXZMn8iMzLdSys1ksFZvLCZ+dIUJa1aVlbcbSRnkvKf JtAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Xlpef4AvAoeXcCl7QPPzyVsUNlgqSta9YgeJ7OFoE+U=; b=Fo5AbIHJQGoTu8z6fqZhFyxC7CMhmDZxc8OZmplGeMt1cnA1HHXcaXgXXXIUTD11jJ g6IzU6W+NvndUjOVPjDEzMQcJQZW5aOV6vuC+aB/Yd59yYlySYtmYyCKHc2dpH6P3e+3 Bsb2BNYJ3ri2OYI/Mz6gM1qGOV9tlX1uo63clrFid9+70pQFolKNH6eDuU6AjB6VyKD6 +PTBvKV8io4bCSe2V1i88PvJt8W0Avtc8fe9mgyiNHC+yqHuq25IN9EnhzJkWPm5LE1f a/ppCweKKpS7EW3qZRa0K34cYi+7D2vJscRlVC9hHzsdV2ALqRB0o796uFEscrMEyCmJ AxKw== X-Gm-Message-State: AKwxyteZ3jb58I6ZvZc0SK3UASJnakMxFeN2v4Fn7c+CgHIty5M0nlGM P3CmVxmDpA/dTwbdJ4XE8im1dg== X-Google-Smtp-Source: ACJfBosArih6HMNlI196EucGi+8Jsmjs7/gMgDRrXzlAAEc2JcFuZ9eOvZxzLrdJMdKgh/8osQvsEA== X-Received: by 10.25.212.83 with SMTP id l80mr12487401lfg.83.1515778800128; Fri, 12 Jan 2018 09:40:00 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id c190sm3781968lfc.81.2018.01.12.09.39.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:39:59 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 5/9] ip/tunnel: Use print_string() and simplify encap option printing Date: Fri, 12 Jan 2018 19:39:30 +0200 Message-Id: <1515778774-24173-6-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1515778774-24173-1-git-send-email-serhe.popovych@gmail.com> References: <1515778774-24173-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- 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