From patchwork Thu Jan 18 14:04:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862925 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="qA/IoP0U"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm4S2vx7z9s7F for ; Fri, 19 Jan 2018 01:07:36 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932564AbeAROHe (ORCPT ); Thu, 18 Jan 2018 09:07:34 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:46848 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756254AbeAROFj (ORCPT ); Thu, 18 Jan 2018 09:05:39 -0500 Received: by mail-lf0-f66.google.com with SMTP id q194so15335353lfe.13 for ; Thu, 18 Jan 2018 06:05:38 -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=KSZwpsoXnHv7R1lGZwoMs2V5q4/7sySgHB1V+iIL+2c=; b=qA/IoP0UAH4QOvhoOjvkLs+V0F8io6ANTMLlXlbwS7Gm0Ffi6nhTongnYUJK9/vFxc ubFBmwac7tUwpMzzstapkaUVkpC85UXNLxsixFGX6L+ntmT30V8xKE69ojzGvZNAD4PF b2FpSJe2O/gvnvkUoYM070TMfSx6HEQTUII621+TCsdQ3fW3AsQR1fvDYeEuiKejUzbT Sb4oRw8/HAY2QPsNX7DwsYIrv1xv5Rz+Iojb6uQ+ACiClIUGIpBxDxCh7T2sCaxlR5u/ ZAjB4sU52wKceEFKbfTclf+NfrNKsNVNtXSGMRRFFNUM2lg1uLb3Z9BJZ/iyj7TnQS4C Vvxw== 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=KSZwpsoXnHv7R1lGZwoMs2V5q4/7sySgHB1V+iIL+2c=; b=BwbrkQ7XRUexRXClcYVeW8UwwAAeFPWYQwMdURWNP5e1xiITTi7RfSoNC4velic/2g 5fE7dNwItdriorET+VITnAKy4Yij1MY483HonHctm2hrKwbu/+WEIWIjr4LLFu3F9mnf LRRX/7xwNsiHdr5rqCNvpZJXksHJyxyjuq8RZKal1INMADNqXhh1yTHlFIrZYeMs/tIy uPcD7jLezCuJBXBl2dK1zK6qV13I2ODcTICaz6N4Z7AR8Ce0z3DgaPbPH/FdGN8GqRiY gH+EsjpLajPlYaKyF3InxpJyZIlMykrbUjNjL+/doWlRSwi8UxZmLIiBizH1dAVToUHs 8tLA== X-Gm-Message-State: AKwxytcKb2+3th1A5c56Vz+hMkdwNjqr/6fQuaMVB280DmRBt+u6Syr5 29znHBDH8GcQMYllz2b98xFUrw== X-Google-Smtp-Source: ACJfBouKQNhOEW6fewwJxAYQQvJ+lOORykE7S5IWTyv5DS2DQvr27wH4ps8s+VgCN7r9eux80KVkHA== X-Received: by 10.25.83.198 with SMTP id h67mr22447098lfl.135.1516284337196; Thu, 18 Jan 2018 06:05:37 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:36 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 1/9] iplink: Use ll_index_to_name() instead of if_indextoname() Date: Thu, 18 Jan 2018 16:04:28 +0200 Message-Id: <1516284276-10942-2-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 There are two reasons for switching to cached variant: 1) ll_index_to_name() may return result from cache, eliminating expensive ioctl() to the kernel. Note that most of the code already switched from plain if_indextoname() to ll_index_to_name() to cached variant in print path because in most cases cache populated. 2) It always return name in the form "if%d", even if entry is not in cache and ioctl() fails. This drops "link_index" from JSON output. Signed-off-by: Serhey Popovych --- bridge/fdb.c | 5 ++--- bridge/link.c | 19 +++++++------------ ip/iplink_bond.c | 38 ++++++++++++++------------------------ ip/iplink_vxlan.c | 15 ++++++--------- ip/iproute_lwtunnel.c | 7 ++----- ip/link_gre.c | 11 +++++------ ip/link_gre6.c | 11 +++++------ ip/link_ip6tnl.c | 11 +++++------ ip/link_iptnl.c | 11 +++++------ ip/link_vti.c | 14 ++++++-------- ip/link_vti6.c | 13 ++++++------- 11 files changed, 63 insertions(+), 92 deletions(-) diff --git a/bridge/fdb.c b/bridge/fdb.c index 376713b..4d55fb0 100644 --- a/bridge/fdb.c +++ b/bridge/fdb.c @@ -219,10 +219,9 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) unsigned int ifindex = rta_getattr_u32(tb[NDA_IFINDEX]); if (ifindex) { - char ifname[IF_NAMESIZE]; + if (!tb[NDA_LINK_NETNSID]) { + const char *ifname = ll_index_to_name(ifindex); - if (!tb[NDA_LINK_NETNSID] && - if_indextoname(ifindex, ifname)) { if (jw_global) jsonw_string_field(jw_global, "viaIf", ifname); diff --git a/bridge/link.c b/bridge/link.c index e2371d0..870ebe0 100644 --- a/bridge/link.c +++ b/bridge/link.c @@ -26,8 +26,6 @@ static const char *port_states[] = { [BR_STATE_BLOCKING] = "blocking", }; -extern char *if_indextoname(unsigned int __ifindex, char *__ifname); - static void print_link_flags(FILE *fp, unsigned int flags) { fprintf(fp, "<"); @@ -104,7 +102,6 @@ int print_linkinfo(const struct sockaddr_nl *who, int len = n->nlmsg_len; struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr *tb[IFLA_MAX+1]; - char b1[IFNAMSIZ]; len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) { @@ -135,14 +132,10 @@ int print_linkinfo(const struct sockaddr_nl *who, print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); if (tb[IFLA_LINK]) { - SPRINT_BUF(b1); int iflink = rta_getattr_u32(tb[IFLA_LINK]); - if (iflink == 0) - fprintf(fp, "@NONE: "); - else - fprintf(fp, "@%s: ", - if_indextoname(iflink, b1)); + fprintf(fp, "@%s: ", + iflink ? ll_index_to_name(iflink) : "NONE"); } else fprintf(fp, ": "); @@ -151,9 +144,11 @@ int print_linkinfo(const struct sockaddr_nl *who, if (tb[IFLA_MTU]) fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU])); - if (tb[IFLA_MASTER]) - fprintf(fp, "master %s ", - if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1)); + if (tb[IFLA_MASTER]) { + int master = rta_getattr_u32(tb[IFLA_MASTER]); + + fprintf(fp, "master %s ", ll_index_to_name(master)); + } if (tb[IFLA_PROTINFO]) { if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) { diff --git a/ip/iplink_bond.c b/ip/iplink_bond.c index 2b5cf4f..f01fd8d 100644 --- a/ip/iplink_bond.c +++ b/ip/iplink_bond.c @@ -369,8 +369,6 @@ static int bond_parse_opt(struct link_util *lu, int argc, char **argv, static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { - unsigned int ifindex; - if (!tb) return; @@ -380,21 +378,16 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "mode", "mode %s ", mode); } - if (tb[IFLA_BOND_ACTIVE_SLAVE] && - (ifindex = rta_getattr_u32(tb[IFLA_BOND_ACTIVE_SLAVE]))) { - char buf[IFNAMSIZ]; - const char *n = if_indextoname(ifindex, buf); + if (tb[IFLA_BOND_ACTIVE_SLAVE]) { + unsigned int ifindex = + rta_getattr_u32(tb[IFLA_BOND_ACTIVE_SLAVE]); - if (n) + if (ifindex) { print_string(PRINT_ANY, "active_slave", "active_slave %s ", - n); - else - print_uint(PRINT_ANY, - "active_slave_index", - "active_slave %u ", - ifindex); + ll_index_to_name(ifindex)); + } } if (tb[IFLA_BOND_MIIMON]) @@ -479,18 +472,15 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) arp_all_targets); } - if (tb[IFLA_BOND_PRIMARY] && - (ifindex = rta_getattr_u32(tb[IFLA_BOND_PRIMARY]))) { - char buf[IFNAMSIZ]; - const char *n = if_indextoname(ifindex, buf); + if (tb[IFLA_BOND_PRIMARY]) { + unsigned int ifindex = rta_getattr_u32(tb[IFLA_BOND_PRIMARY]); - if (n) - print_string(PRINT_ANY, "primary", "primary %s ", n); - else - print_uint(PRINT_ANY, - "primary_index", - "primary %u ", - ifindex); + if (ifindex) { + print_string(PRINT_ANY, + "primary", + "primary %s ", + ll_index_to_name(ifindex)); + } } if (tb[IFLA_BOND_PRIMARY_RESELECT]) { diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c index 661eaa7..ad7ef1c 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -394,10 +394,8 @@ 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[]) { __u32 vni; - unsigned int link; __u8 tos; __u32 maxaddr; - char s2[64]; if (!tb) return; @@ -467,14 +465,13 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) &addr)); } - if (tb[IFLA_VXLAN_LINK] && - (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VXLAN_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_VXLAN_PORT_RANGE]) { diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c index 740da7c..a8d7171 100644 --- a/ip/iproute_lwtunnel.c +++ b/ip/iproute_lwtunnel.c @@ -196,7 +196,6 @@ static int read_action_type(const char *name) static void print_encap_seg6local(FILE *fp, struct rtattr *encap) { struct rtattr *tb[SEG6_LOCAL_MAX + 1]; - char ifbuf[IFNAMSIZ]; int action; parse_rtattr_nested(tb, SEG6_LOCAL_MAX, encap); @@ -229,15 +228,13 @@ static void print_encap_seg6local(FILE *fp, struct rtattr *encap) if (tb[SEG6_LOCAL_IIF]) { int iif = rta_getattr_u32(tb[SEG6_LOCAL_IIF]); - fprintf(fp, "iif %s ", - if_indextoname(iif, ifbuf) ?: ""); + fprintf(fp, "iif %s ", ll_index_to_name(iif)); } if (tb[SEG6_LOCAL_OIF]) { int oif = rta_getattr_u32(tb[SEG6_LOCAL_OIF]); - fprintf(fp, "oif %s ", - if_indextoname(oif, ifbuf) ?: ""); + fprintf(fp, "oif %s ", ll_index_to_name(oif)); } } diff --git a/ip/link_gre.c b/ip/link_gre.c index 3c0b6d6..7463d7c 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -380,14 +380,13 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { + if (tb[IFLA_GRE_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_GRE_TTL]) { diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 55bd1fb..7d38f47 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -414,14 +414,13 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { + if (tb[IFLA_GRE_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_GRE_TTL]) { diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index bbc7878..b22e7bc 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -377,14 +377,13 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL])); } - if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { + if (tb[IFLA_IPTUN_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_IPTUN_TTL]) { diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 24a0f0c..35ef5c0 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -407,14 +407,13 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { + if (tb[IFLA_IPTUN_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_int(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_IPTUN_TTL]) { diff --git a/ip/link_vti.c b/ip/link_vti.c index 2b0fab2..554cfcc 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -168,7 +168,6 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) const char *local = "any"; const char *remote = "any"; __u32 key; - unsigned int link; char s2[IFNAMSIZ]; if (!tb) @@ -192,14 +191,13 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_VTI_LINK] && - (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VTI_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_VTI_IKEY] && diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 74c246d..a63f49f 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -168,7 +168,6 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) const char *remote = "any"; struct in6_addr saddr; struct in6_addr daddr; - unsigned int link; char s2[64]; if (!tb) @@ -190,13 +189,13 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_VTI_LINK] && (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VTI_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_VTI_IKEY]) { From patchwork Thu Jan 18 14:04:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862923 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="T11qLdVy"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm4F2fc5z9s7F for ; Fri, 19 Jan 2018 01:07:25 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756478AbeAROFn (ORCPT ); Thu, 18 Jan 2018 09:05:43 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:44037 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756451AbeAROFk (ORCPT ); Thu, 18 Jan 2018 09:05:40 -0500 Received: by mail-lf0-f65.google.com with SMTP id w23so26772571lfd.11 for ; Thu, 18 Jan 2018 06:05:39 -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=zBmmeowo1HvYJC24A7xk0X9QMTwBlO3/HT1BtR22PLQ=; b=T11qLdVyIdxp1/+DhO5w57FbUdjUHbpUp3P6nupjz0YVE6/kHzThD4JhefDB+/WgXE 9rxSBpPNyQkJKI/8YIc0dMSlnJHqKGFQAYlqDiDlieCBa4lPHvwjYSlVr9KOHmiFtxOJ dF3BTeKxCRwtzaOCewry3FII/EMVQEZbImBxPS3p8QUYw7htuB3JDwq0OEMs3dUaQLQi cokfddzL+xKeDuwJvhgO2CzEJaKT808uYKLKJfcw+b+IVpE/3GDeRSB8J2Kz0e2BNBTt 4YHx9stfFNyuR/9zrPyTL3vSMTuFZ55K0cP9TXUUjdTTKMauLZTUzzTyy9iAE1pDux7V 6Ejg== 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=zBmmeowo1HvYJC24A7xk0X9QMTwBlO3/HT1BtR22PLQ=; b=JuuRuBavFpXzjnh9QjUAHD8e1r7hkEKLy3l5SvQHYqeaH3MaMM9Qw6jP+QZQEzXDP9 df1kU1QRYN4gzVm1kIAqaho+rFscLt0Y2WUdC3utj8nH39JvjhiTGVtMDFlhjozDu0xn gevvCHFYpy3ebMgB/YnNRzRSr2zRscswX71yPmxB7+Dtyty2ciSpgH8T8SYnuLy3QHly SQa7Meo/KKCVc7cn15Kf4fNwI3ZBjOTiSpn1Vr0adnBfwoRTB06DwRpzQaH/tY9HYvni oXR6S5Ry0+ShcHCBf5SPwxDhQvmPFQc11vR2X7Thfw0YHxaASqxZLnQJfiwzPkR3OkQ0 TdTA== X-Gm-Message-State: AKwxytcUw6Yz8aHFGXa+DFjPF2Ehoh94NJho6rTMla08jf8WRIZeSBmp P0EB7aUX2R6Z3X8SnCWEtAyaoA== X-Google-Smtp-Source: ACJfBotZ6SuIJOU3Md79kR1ScbNPvLXo7o47BbGDmV7mJW2aqtRirtJsl3EUesCnw3d7mRIrbrNhdw== X-Received: by 10.25.33.70 with SMTP id h67mr9918644lfh.128.1516284338409; Thu, 18 Jan 2018 06:05:38 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:37 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 2/9] ip/tunnel: Correct and unify ttl/hoplimit printing Date: Thu, 18 Jan 2018 16:04:29 +0200 Message-Id: <1516284276-10942-3-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 Both ttl/hoplimit is from 1 to 255. Zero has special meaning: use encapsulated packet value. In ip-link(8) -d output this looks like "ttl/hoplimit inherit". In JSON we have "int" type for ttl and therefore values from 0 (inherit) to 255. To do the best in handling ttl/hoplimit we need to accept both cases: missing attribute in netlink dump and zero value for "inherit"ed case. Last one is broken since JSON output introduction for gre/iptnl versions and was never true for gre6/ip6tnl. For all tunnels, except ip6tnl change JSON type from "int" to "uint" to reflect true nature of the ttl. Signed-off-by: Serhey Popovych --- ip/ip6tunnel.c | 5 ++++- ip/iplink_geneve.c | 13 +++++++------ ip/iplink_vxlan.c | 15 +++++++-------- ip/iproute_lwtunnel.c | 4 ++-- ip/iptunnel.c | 2 +- ip/link_gre.c | 15 ++++++--------- ip/link_gre6.c | 15 +++++++-------- ip/link_ip6tnl.c | 13 +++++++------ ip/link_iptnl.c | 15 ++++++--------- 9 files changed, 47 insertions(+), 50 deletions(-) diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c index b8db49c..783e28a 100644 --- a/ip/ip6tunnel.c +++ b/ip/ip6tunnel.c @@ -92,7 +92,10 @@ static void print_tunnel(struct ip6_tnl_parm2 *p) else printf(" encaplimit %u", p->encap_limit); - printf(" hoplimit %u", p->hop_limit); + if (p->hop_limit) + printf(" hoplimit %u", p->hop_limit); + else + printf(" hoplimit inherit"); if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS) printf(" tclass inherit"); diff --git a/ip/iplink_geneve.c b/ip/iplink_geneve.c index f0f1d1c..5a0afd4 100644 --- a/ip/iplink_geneve.c +++ b/ip/iplink_geneve.c @@ -227,6 +227,7 @@ static int geneve_parse_opt(struct link_util *lu, int argc, char **argv, static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; + __u8 ttl = 0; __u8 tos; if (!tb) @@ -262,12 +263,12 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_GENEVE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - } + if (tb[IFLA_GENEVE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); if (tb[IFLA_GENEVE_TOS] && (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) { diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c index ad7ef1c..b9c335d 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -394,6 +394,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[]) { __u32 vni; + __u8 ttl = 0; __u8 tos; __u32 maxaddr; @@ -526,14 +527,12 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_VXLAN_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } + if (tb[IFLA_VXLAN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); if (tb[IFLA_VXLAN_LABEL]) { __u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]); diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c index a8d7171..a1d36ba 100644 --- a/ip/iproute_lwtunnel.c +++ b/ip/iproute_lwtunnel.c @@ -271,7 +271,7 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap) rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_DST])); if (tb[LWTUNNEL_IP_TTL]) - fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL])); + fprintf(fp, "ttl %u ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL])); if (tb[LWTUNNEL_IP_TOS]) fprintf(fp, "tos %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TOS])); @@ -326,7 +326,7 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap) rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_DST])); if (tb[LWTUNNEL_IP6_HOPLIMIT]) - fprintf(fp, "hoplimit %d ", + fprintf(fp, "hoplimit %u ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT])); if (tb[LWTUNNEL_IP6_TC]) diff --git a/ip/iptunnel.c b/ip/iptunnel.c index ce610f8..0aa3b33 100644 --- a/ip/iptunnel.c +++ b/ip/iptunnel.c @@ -326,7 +326,7 @@ static void print_tunnel(struct ip_tunnel_parm *p) } if (p->iph.ttl) - printf(" ttl %d", p->iph.ttl); + printf(" ttl %u", p->iph.ttl); else printf(" ttl inherit"); diff --git a/ip/link_gre.c b/ip/link_gre.c index 7463d7c..1cf7f4e 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -361,6 +361,7 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) const char *remote = "any"; unsigned int iflags = 0; unsigned int oflags = 0; + __u8 ttl = 0; if (tb[IFLA_GRE_REMOTE]) { unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); @@ -389,16 +390,12 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_GRE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } else { + if (tb[IFLA_GRE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - } if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 7d38f47..37bc544 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -382,6 +382,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) unsigned int flags = 0; __u32 flowinfo = 0; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; + __u8 ttl = 0; if (!tb) return; @@ -423,14 +424,12 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_GRE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "hoplimit %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } + if (tb[IFLA_GRE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl); + else + print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit"); if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) { print_bool(PRINT_ANY, diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index b22e7bc..605bc61 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -336,6 +336,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb char s2[64]; int flags = 0; __u32 flowinfo = 0; + __u8 ttl = 0; if (!tb) return; @@ -386,12 +387,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb } } - if (tb[IFLA_IPTUN_TTL]) { - print_uint(PRINT_ANY, - "ttl", - "hoplimit %u ", - rta_getattr_u8(tb[IFLA_IPTUN_TTL])); - } + if (tb[IFLA_IPTUN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl); + else + print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit"); if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) { print_bool(PRINT_ANY, diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 35ef5c0..93e1a1b 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -365,6 +365,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ const char *local = "any"; const char *remote = "any"; __u16 prefixlen, type; + __u8 ttl = 0; if (!tb) return; @@ -416,16 +417,12 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ } } - if (tb[IFLA_IPTUN_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } else { + if (tb[IFLA_IPTUN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - } if (tb[IFLA_IPTUN_TOS]) { int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); From patchwork Thu Jan 18 14:04: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: 862920 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="teoxvwlf"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm3Y3SG2z9s7g for ; Fri, 19 Jan 2018 01:06:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756498AbeAROFo (ORCPT ); Thu, 18 Jan 2018 09:05:44 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:45117 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756443AbeAROFl (ORCPT ); Thu, 18 Jan 2018 09:05:41 -0500 Received: by mail-lf0-f68.google.com with SMTP id x196so6799049lfd.12 for ; Thu, 18 Jan 2018 06:05:40 -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=Gv5ERUB+pCHDsoxczMtcQ4lZsgeOoiRDnEFCYQSE0os=; b=teoxvwlfwhANRvd4ZWxm6s+WhW1CoCL6OlYmBr3WUrwxGHdV+wf7+GGQ1kg08apVHD qzJ7+yLxOsPTSqXwJlDoOTh+GdxWv5e04ggV0V43SWvBMBOy2aeo5JCVWngrD1G/sTOK /1mA2plRe0CcGRYPKUB/AtLXJtDnZKfSwK1Lv/ysXjM8PbuDMZt9LqgPwLI60GiJ6HmX GUrEY3ClNbMzB3jpX8LTqV/QNbBGQS623qCw/iwYKbIPoqourgG3cNAzXU5beAgWMtBZ xl8dgytBAHLlIZY2D97x27X322LBVaLc6wOUtUBlaoqz751KuOBP+q/Z8QkuGrlu0ODV /IvA== 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=Gv5ERUB+pCHDsoxczMtcQ4lZsgeOoiRDnEFCYQSE0os=; b=m0xUv/D+ya4gjopad7OwOdLhPdu7UzuOQk9yuxqvvmADJQERk+r7qDHQRNWqPIWLA7 8MXvepbwsDo8iX2LqqjkIDTBLbja/wckV/0pxMPLwWlGvvghDoVxFvQOWBoM49bZPFyY coajlcUDKFVCqZNGROcOK4hPIBUbg4u1SRPN++FyLGpgl6hfL1XFSd+1ltc0Uf0ROq2S lVleGtlv284+SE5aa4jqb6U6FRv4GTieYM5BYWVoCFonZdrpXQGdWD9ZliA7Kgb2wS2w rNNggTPHzJVyR9oCZFGpv8SwCFZAGPTxCmeHYbpBXbQNrHR2Uv6MuKVnoAPU18Z0bfes lc1Q== X-Gm-Message-State: AKwxytd2ucGlzltp4o3IACbWcfNeCRuZT13070xIMzHQbrdN8P59d/dG B5alTDHCaKjEc0M1nfOIWlb9/Q== X-Google-Smtp-Source: ACJfBotlER71xC++Lt2e7zY/luFHzUDqNYfrgj5Gxn+oSGoT8lNF46kVcP4MxT8w9e8BVTUY4zJoYg== X-Received: by 10.25.90.200 with SMTP id y69mr17206996lfk.138.1516284339607; Thu, 18 Jan 2018 06:05:39 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:38 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 3/9] ip/tunnel: Simplify and unify tos printing Date: Thu, 18 Jan 2018 16:04:30 +0200 Message-Id: <1516284276-10942-4-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 For ip tunnels tos can be 0 when not configured, 1 when inherited from encapsulated packet and rest specifying diffserv (rfc2474) or tos (rfc1349) bits. It is stored in packet tos/diffserv field and returned in tos netlink attribute to userspace. Simplify and unify tos printing by using print_0xhex() and print_string() instead of fprintf() to output values. Signed-off-by: Serhey Popovych --- ip/iplink_geneve.c | 23 ++++++++--------------- ip/iplink_vxlan.c | 19 ++++++++----------- ip/link_gre.c | 23 ++++++++--------------- ip/link_iptnl.c | 22 ++++++++-------------- 4 files changed, 32 insertions(+), 55 deletions(-) diff --git a/ip/iplink_geneve.c b/ip/iplink_geneve.c index 5a0afd4..2d0a041 100644 --- a/ip/iplink_geneve.c +++ b/ip/iplink_geneve.c @@ -228,7 +228,7 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; __u8 ttl = 0; - __u8 tos; + __u8 tos = 0; if (!tb) return; @@ -270,20 +270,13 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) else print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - if (tb[IFLA_GENEVE_TOS] && - (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) { - if (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - if (tos == 1) { - print_string(PRINT_FP, - "tos", - "tos %s ", - "inherit"); - } else { - fprintf(f, "tos %#x ", tos); - } - } + if (tb[IFLA_GENEVE_TOS]) + tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } if (tb[IFLA_GENEVE_LABEL]) { diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c index b9c335d..88b5662 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -395,7 +395,7 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; __u8 ttl = 0; - __u8 tos; + __u8 tos = 0; __u32 maxaddr; if (!tb) @@ -515,16 +515,13 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) 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 (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - if (tos == 1) - fprintf(f, "tos %s ", "inherit"); - else - fprintf(f, "tos %#x ", tos); - } + if (tb[IFLA_VXLAN_TOS]) + tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } if (tb[IFLA_VXLAN_TTL]) diff --git a/ip/link_gre.c b/ip/link_gre.c index 1cf7f4e..490d7db 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -362,6 +362,7 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) unsigned int iflags = 0; unsigned int oflags = 0; __u8 ttl = 0; + __u8 tos = 0; if (tb[IFLA_GRE_REMOTE]) { unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); @@ -397,21 +398,13 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) else print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { - int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); - - if (is_json_context()) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", tos); - print_string(PRINT_JSON, "tos", NULL, b1); - } else { - fputs("tos ", f); - if (tos == 1) - fputs("inherit ", f); - else - fprintf(f, "0x%x ", tos); - } + if (tb[IFLA_GRE_TOS]) + tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } if (tb[IFLA_GRE_PMTUDISC]) { diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 93e1a1b..7fda580 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -366,6 +366,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ const char *remote = "any"; __u16 prefixlen, type; __u8 ttl = 0; + __u8 tos = 0; if (!tb) return; @@ -424,20 +425,13 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ else print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - if (tb[IFLA_IPTUN_TOS]) { - int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); - - if (tos) { - if (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - fputs("tos ", f); - if (tos == 1) - fputs("inherit ", f); - else - fprintf(f, "0x%x ", tos); - } - } + if (tb[IFLA_IPTUN_TOS]) + tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC])) From patchwork Thu Jan 18 14:04:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862924 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="Yn8LOdvN"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm4K6ZP2z9s7F for ; Fri, 19 Jan 2018 01:07:29 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932398AbeAROH2 (ORCPT ); Thu, 18 Jan 2018 09:07:28 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:43991 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756462AbeAROFm (ORCPT ); Thu, 18 Jan 2018 09:05:42 -0500 Received: by mail-lf0-f68.google.com with SMTP id o89so22684461lfg.10 for ; Thu, 18 Jan 2018 06:05:41 -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=WCbaucAqJRLacsZwxnbqnpVmO16lHV013GdPJtD7GAQ=; b=Yn8LOdvNdJFLrsVzu4gSeGWmcIfkr4kM0P6OkUfdDJohpaI+cO792rh3Ek3Biq0Hfp y1YKODkh3hjH4x8sWo44S5ONKVOJ6dWkEo8KRhIqebKFiExWtZYr+YhrBMi+FTDXPaer M7eQQFCPYjjzrXidQdaozc7DBq5XFZmjwBbcE/Qs5D0BLWwRxu/XHbeebM331I9wj919 soFGlqxxrAcuQ0bVwQCIzaKJLCxjchgKqD71qIjkZf4pFf6s38nh8gJkouAW56pKg+ow hZQ5EB8Tb8zV+qMCx5vMS18MlbN8Dhf5WtehM2EzsdQTT6m8TzZGPa7yDi5FGn9cglsy byFw== 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=WCbaucAqJRLacsZwxnbqnpVmO16lHV013GdPJtD7GAQ=; b=br4SjoA1eWEQXRfAMkCSbzMOW0lfeGK6qdlHeoyIHl8osUqCuA9EcMeyAqW7PHWebf bmcwjytDV3Z+y88+c4HUl4jttQ5stt417QoczbD/+da00qdoAmjiKaGhIHqJdft/kDcv bUtmpEwTCTIClPrabNsrFE7SMqHhwANLoCCi+HFyzR6q3TPy334QfJ7XTz7lKXF9HBiG 8yAgJYe2nChpOFP3Kc3MC4mM6An6cDjopN11aI2/ZWdrOkV8l1VOCWKThdwNAyvyASs0 qfaJ3BTDD5Y4oFQ8zQyQqTapIFd3vr0ZY+8VsSGh7Yxnb/wybKdTTh9kwICqJX65zd8p 47/g== X-Gm-Message-State: AKwxytcPdz2Ma4dQktd0FU/HRDxWFSlSGOrc+3RtvgOuBfChAY66YCXN BNnBH3Tumj7764fEGrFXPqpgaA== X-Google-Smtp-Source: ACJfBoujxBJEpdFR24ThTr2qAYgB4RQngkCF4m8nBjGhTDJQ1vi18hv6jpxTMIdgVFvBPKZJQYK3Sg== X-Received: by 10.25.204.81 with SMTP id c78mr14922095lfg.39.1516284340705; Thu, 18 Jan 2018 06:05:40 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:39 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 4/9] ip/tunnel: Use print_0xhex() instead of print_string() Date: Thu, 18 Jan 2018 16:04:31 +0200 Message-Id: <1516284276-10942-5-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 No need for custom SPRINT_BUF() and snprintf() 0x%x value to this buffer: we can use print_0xhex() instead of print_string(). In link_iptnl.c use s2 instead of s1 buffer and remove s1. While there adjust fwmark option print order in iptnl and ip6tnl to get it match each other. Signed-off-by: Serhey Popovych --- ip/link_gre.c | 5 ++--- ip/link_gre6.c | 9 ++++----- ip/link_ip6tnl.c | 18 ++++++++---------- ip/link_iptnl.c | 22 ++++++++++------------ ip/link_vti.c | 6 ++---- ip/link_vti6.c | 5 ++--- 6 files changed, 28 insertions(+), 37 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index 490d7db..2462828 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -443,9 +443,8 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 37bc544..7dcd70d 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -497,18 +497,17 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (oflags & GRE_CSUM) print_bool(PRINT_ANY, "ocsum", "ocsum ", true); - if (flags & IP6_TNL_F_USE_ORIG_FWMARK) + if (flags & IP6_TNL_F_USE_ORIG_FWMARK) { print_bool(PRINT_ANY, "ip6_tnl_f_use_orig_fwmark", "fwmark inherit ", true); - else if (tb[IFLA_GRE_FWMARK]) { + } else if (tb[IFLA_GRE_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index 605bc61..00bf00a 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -438,6 +438,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb if (flags & IP6_TNL_F_MIP6_DEV) print_bool(PRINT_ANY, "ip6_tnl_f_mip6_dev", "mip6 ", true); + if (flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) + print_bool(PRINT_ANY, + "ip6_tnl_f_allow_local_remote", + "allow-localremote ", + true); + if (flags & IP6_TNL_F_USE_ORIG_FWMARK) { print_bool(PRINT_ANY, "ip6_tnl_f_use_orig_fwmark", @@ -447,19 +453,11 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]); if (fwmark) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", fwmark); - print_string(PRINT_ANY, "fwmark", "fwmark %s ", b1); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } - if (flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) - print_bool(PRINT_ANY, - "ip6_tnl_f_allow_local_remote", - "allow-localremote ", - true); - if (tb[IFLA_IPTUN_ENCAP_TYPE] && rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]); diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 7fda580..00a8366 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -360,7 +360,6 @@ get_failed: static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { - char s1[1024]; char s2[64]; const char *local = "any"; const char *remote = "any"; @@ -455,7 +454,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ const char *prefix = inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]), - s1, sizeof(s1)); + s2, sizeof(s2)); if (is_json_context()) { print_string(PRINT_JSON, "prefix", NULL, prefix); @@ -482,6 +481,15 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ } } + if (tb[IFLA_IPTUN_FWMARK]) { + __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]); + + if (fwmark) { + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); + } + } + if (tb[IFLA_IPTUN_ENCAP_TYPE] && (type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) { __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); @@ -545,16 +553,6 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ fputs("noencap-remcsum ", f); } } - - if (tb[IFLA_IPTUN_FWMARK]) { - __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]); - - if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); - } - } } static void iptunnel_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_vti.c b/ip/link_vti.c index 554cfcc..baaaf2f 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -213,10 +213,8 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) __u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]); if (fwmark) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", fwmark); - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } diff --git a/ip/link_vti6.c b/ip/link_vti6.c index a63f49f..952dbb0 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -212,9 +212,8 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) __u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } From patchwork Thu Jan 18 14:04:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862921 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="I3lJPFAB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm3g5YHcz9s7v for ; Fri, 19 Jan 2018 01:06:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932562AbeAROGv (ORCPT ); Thu, 18 Jan 2018 09:06:51 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34913 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756479AbeAROFo (ORCPT ); Thu, 18 Jan 2018 09:05:44 -0500 Received: by mail-lf0-f66.google.com with SMTP id a204so18515230lfa.2 for ; Thu, 18 Jan 2018 06:05:43 -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=1RQo4sGTCiz5HyemdM0gbC6nD/0PP2XMRsPt+AJYwvs=; b=I3lJPFABQmK2W5OzlSkpund55HZHGMWynzWxR92U5KzZESVLMq9mKl52/zd+siGjqy 21TdvmjfuC57REVleOfv4x1KuOluEz5P8fMNrZIDNlkTbAJs7ytvGLHA7kp/EwaVFO9z 8SA2tuzMJJMFpAfdXwi8CPpXMqfmrGQQRC36KkClox7LzcNp8hWAbQdr1yRBnpwNc7+r tqQ1iDIt5Ux2Pb1/LIoZ/t7jPYbpLnw9Nk0nTUrgt6bFUEMWAGNBnTd7pJfQCTRXDjS/ cyB37Uj6o1m1wQMtBbsQbEfUnBZwyMBPXe48j4jO7yKhEQS2ksp71x72+suq26aG+Lv6 XQtQ== 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=1RQo4sGTCiz5HyemdM0gbC6nD/0PP2XMRsPt+AJYwvs=; b=pcQZ7NTW1fOKch5NV4FH12UMp/jiDlx+nevkqGyMBiZyjcsDKWIOQpoyxEu+PBcE4O C7nG1+29+RWh1VQqAfKFaVk2DcSGjtqzHrF2ssWRbtrBq5s7wFpBUIP+ivg5yYO+bNRy NukqStep8UcOslmtyLXlu5bEk0CtXIastmao7Wr7vvhmfUjJA4buRs70UJ8ZG98fuwrU UH9aC9SaLd07QhN8JiU5ptjsCOz5W5yzNcvlmc4N5b6Grir/1wV/rRzdB8c9HKCXKArC 6sqqvKtJ39vIh/4zRsGdWcy4AexF4tBnFYiOJQDkdQrufvvSTLAf/Zyk8U83uut7p7D+ wlfA== X-Gm-Message-State: AKwxyteIk3ZaOE5sYZqtpA4METYqgp3gSBb9wsSB3YFJ/MoXKvQFFBG3 SRPjE1ZHucYHzgCYtfvf1acbew== X-Google-Smtp-Source: ACJfBov8rgAkerc6rEzN/MTOb2ED/DPqz7xbhLiLn86JDzbdNOZwcBSd/d4T51f6n9IJFHBq+laQ3w== X-Received: by 10.25.229.146 with SMTP id i18mr16500041lfk.66.1516284342105; Thu, 18 Jan 2018 06:05:42 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:41 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 5/9] ip/tunnel: Abstract tunnel encapsulation options printing Date: Thu, 18 Jan 2018 16:04:32 +0200 Message-Id: <1516284276-10942-6-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 Get rid of code duplications and consolidate encapsulation options printing in single function - tnl_print_encap(). Introduce and use tnl_encap_str() to format encapsulation option string according to tempate and given values to avoid code duplication and simplify it. Use print_string() instead of fputs() and fprintf() to print encapsulation for !is_json_context(). Print "unknown" parameter for "encap" type in PRINT_FP context using "%s " format specifier and benefit from complite time string merge. Signed-off-by: Serhey Popovych --- ip/link_gre.c | 75 +++----------------------------------------- ip/link_gre6.c | 64 +++----------------------------------- ip/link_ip6tnl.c | 63 +++---------------------------------- ip/link_iptnl.c | 70 ++++------------------------------------- ip/tunnel.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ip/tunnel.h | 5 +++ 6 files changed, 117 insertions(+), 251 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index 2462828..d403d24 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -468,76 +468,11 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) fprintf(f, "erspan_index %u ", erspan_idx); } - if (tb[IFLA_GRE_ENCAP_TYPE] && - rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { - __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); - __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); - - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "%s ", "unknown"); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - - print_bool(PRINT_JSON, - "csum", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - - print_bool(PRINT_JSON, - "csum6", - NULL, - 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)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_GRE_ENCAP_TYPE, + IFLA_GRE_ENCAP_FLAGS, + IFLA_GRE_ENCAP_SPORT, + IFLA_GRE_ENCAP_DPORT); } static void gre_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 7dcd70d..a159b54 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -516,65 +516,11 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) fprintf(f, "erspan_index %u ", erspan_idx); } - if (tb[IFLA_GRE_ENCAP_TYPE] && - rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { - __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); - __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); - - open_json_object("encap"); - - print_string(PRINT_FP, NULL, "encap ", NULL); - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "unknown ", NULL); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, "csum", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, "csum6", NULL, - 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)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_GRE_ENCAP_TYPE, + IFLA_GRE_ENCAP_FLAGS, + IFLA_GRE_ENCAP_SPORT, + IFLA_GRE_ENCAP_DPORT); } static void gre_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index 00bf00a..8f5c9bd 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -458,64 +458,11 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb } } - if (tb[IFLA_IPTUN_ENCAP_TYPE] && - rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { - __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]); - __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "unknown ", NULL); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, "csum", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, "csum6", NULL, - 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)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_IPTUN_ENCAP_TYPE, + IFLA_IPTUN_ENCAP_FLAGS, + IFLA_IPTUN_ENCAP_SPORT, + IFLA_IPTUN_ENCAP_DPORT); } static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 00a8366..ce3855c 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -363,7 +363,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ char s2[64]; const char *local = "any"; const char *remote = "any"; - __u16 prefixlen, type; + __u16 prefixlen; __u8 ttl = 0; __u8 tos = 0; @@ -490,69 +490,11 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ } } - if (tb[IFLA_IPTUN_ENCAP_TYPE] && - (type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) { - __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "unknown ", NULL); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, - "csum", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, - "csum6", - NULL, - 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)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_IPTUN_ENCAP_TYPE, + IFLA_IPTUN_ENCAP_FLAGS, + IFLA_IPTUN_ENCAP_SPORT, + IFLA_IPTUN_ENCAP_DPORT); } static void iptunnel_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/tunnel.c b/ip/tunnel.c index f860103..42ae70e 100644 --- a/ip/tunnel.c +++ b/ip/tunnel.c @@ -36,6 +36,7 @@ #include "utils.h" #include "tunnel.h" +#include "json_print.h" const char *tnl_strproto(__u8 proto) { @@ -200,6 +201,96 @@ __be32 tnl_parse_key(const char *name, const char *key) return htonl(uval); } +static const char *tnl_encap_str(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; +} + +void tnl_print_encap(struct rtattr *tb[], + int encap_type, int encap_flags, + int encap_sport, int encap_dport) +{ + __u16 type, flags, sport, dport; + + if (!tb[encap_type]) + return; + + type = rta_getattr_u16(tb[encap_type]); + if (type == TUNNEL_ENCAP_NONE) + return; + + flags = rta_getattr_u16(tb[encap_flags]); + sport = rta_getattr_u16(tb[encap_sport]); + dport = rta_getattr_u16(tb[encap_dport]); + + open_json_object("encap"); + print_string(PRINT_FP, NULL, "encap ", NULL); + + switch (type) { + case TUNNEL_ENCAP_FOU: + print_string(PRINT_ANY, "type", "%s ", "fou"); + break; + case TUNNEL_ENCAP_GUE: + print_string(PRINT_ANY, "type", "%s ", "gue"); + break; + default: + print_null(PRINT_ANY, "type", "%s ", "unknown"); + break; + } + + if (is_json_context()) { + 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); + print_bool(PRINT_JSON, "csum6", NULL, + flags & TUNNEL_ENCAP_FLAG_CSUM6); + print_bool(PRINT_JSON, "remcsum", NULL, + flags & TUNNEL_ENCAP_FLAG_REMCSUM); + close_json_object(); + } else { + int t; + + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("sport", 1, t)); + + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("dport", 1, t)); + + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("csum", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("remcsum", t, -1)); + } +} + /* 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..a5c537c 100644 --- a/ip/tunnel.h +++ b/ip/tunnel.h @@ -23,6 +23,8 @@ #include +struct rtattr; + const char *tnl_strproto(__u8 proto); int tnl_get_ioctl(const char *basedev, void *p); @@ -32,6 +34,9 @@ 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); +void tnl_print_encap(struct rtattr *tb[], + int encap_type, int encap_flags, + int encap_sport, int encap_dport); void tnl_print_stats(const char *buf); #endif From patchwork Thu Jan 18 14:04:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862922 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="hQFart2/"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm3p0lD6z9s7v for ; Fri, 19 Jan 2018 01:07:02 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932552AbeAROGu (ORCPT ); Thu, 18 Jan 2018 09:06:50 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:41763 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756483AbeAROFo (ORCPT ); Thu, 18 Jan 2018 09:05:44 -0500 Received: by mail-lf0-f65.google.com with SMTP id f136so5341868lff.8 for ; Thu, 18 Jan 2018 06:05:44 -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=3jDIwnY/wa14spCCEibEyo2JIgnmED4I7Hjf5UOW18w=; b=hQFart2/Qw5D7MjjOyT56Xak3n/OXwbmxkVlnZiFBqqB9DHhQpwUwDxNeRUhTBoZjL MDq2gxfAvsRYegPZ3d9DMnVC+UE2n3p/dFXeYSDfPEOl2reZxXD9ifaf6E1ymUedLFhs D9efbT3jaC65v2uj/B2uLHy/aLr9EZhuChE7ZNr9BBetjnnIm4qB4TUPTePpcSGUi7uN bjsaO43Y6aWp/vHLRUlMsTSihA2ljks1F9rQMCJaXcgX6QZlb6LALrq3ZirN/vXE7IRY kQN71skIIfu0qpqZhw9/299xMJgMGF1JQSyq57HMjDhFqxysRIEcc3ivK+Wvq3Xobta7 igBg== 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=3jDIwnY/wa14spCCEibEyo2JIgnmED4I7Hjf5UOW18w=; b=P1mNCEFR7bm8b6gAViZA+avPQ4m9BouqV328SDEMbDAPGpQrTNib0abFskZv1KPEVj bIbK26HYJ2poqyJYiGOLGfA0fnqlJvjleZUN+Q9wkMHz3bExSIWe7QVAWJkp8Wf0OqLn 7cqjSA0mITEJx14okPZu3SlNwGILIxbrfBpsu+xarIrsZh8RStf6oiVcL63g2JZxqkQc EitWHe0VX/GRutQDnFje7lRkB+HGdtMrxcjPjFNnDMQPVaP4X5XmRmw9AMabaV2C8hKj 8o2WvWUiZrRERMwh+OLeEQG7DntK2fAehMo9M4Mnqb6ub7QS1+Ir6dVxIKnhW3mSac3H AY7w== X-Gm-Message-State: AKwxytcUZtXuw4pur7nXiibxT+VYhW8n0gMhS34JNRsHb2418MhEop02 SX9ZuzRhOZwsh0+NNmyaBYtLmA== X-Google-Smtp-Source: ACJfBovFBOMH8ualHPrLJfCNX3xz5SOOU6y3nXM93PPGiPLFDpDx+8qAxsI2xWfJLISM1cC9xx+TOg== X-Received: by 10.25.125.133 with SMTP id y127mr23233314lfc.44.1516284343253; Thu, 18 Jan 2018 06:05:43 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:42 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 6/9] gre/tunnel: Print erspan_index using print_uint() Date: Thu, 18 Jan 2018 16:04:33 +0200 Message-Id: <1516284276-10942-7-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 One is missing in JSON output because fprintf() is used instead of print_uint(). Signed-off-by: Serhey Popovych --- ip/link_gre.c | 3 ++- ip/link_gre6.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index d403d24..6254e88 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -465,7 +465,8 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_GRE_ERSPAN_INDEX]) { __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); - fprintf(f, "erspan_index %u ", erspan_idx); + print_uint(PRINT_ANY, + "erspan_index", "erspan_index %u ", erspan_idx); } tnl_print_encap(tb, diff --git a/ip/link_gre6.c b/ip/link_gre6.c index a159b54..29ca3d1 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -513,7 +513,9 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_GRE_ERSPAN_INDEX]) { __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); - fprintf(f, "erspan_index %u ", erspan_idx); + + print_uint(PRINT_ANY, + "erspan_index", "erspan_index %u ", erspan_idx); } tnl_print_encap(tb, From patchwork Thu Jan 18 14:04:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862917 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="Kll7Ehge"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm2R5tvzz9s7v for ; Fri, 19 Jan 2018 01:05:51 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932425AbeAROFu (ORCPT ); Thu, 18 Jan 2018 09:05:50 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:44005 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756501AbeAROFq (ORCPT ); Thu, 18 Jan 2018 09:05:46 -0500 Received: by mail-lf0-f67.google.com with SMTP id o89so22684697lfg.10 for ; Thu, 18 Jan 2018 06:05:45 -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=8tHBOwEgNWH09JklTln1w+xcu+YXuIF4Gianyno/wXs=; b=Kll7EhgeJFQbCk1zLM8zjaWiTyAWwV7LAqGsktaeRYTk6LTjAattpb7bFylhdd2vDQ iXZtvUzOGduEwyHpiFkKnaj/1WMV0POD0SuWL3PQ61dC46cLcJvUo8Fb21TMMgCk3Ypu eFwzyCT8UeO03qlEVT+Dg2jUV4Ll6lpDcYtiwDry6JPjf5G4zYNOoHlmWll+Bq1bt8na s9gEc8f0bcd5VDROy/7PW1fOX1iGL6vOb8sfkgCkDXYRSePwcFW2gv0E40EizDkJOCsD ZEZ6sq42OFg3KFxNAqZIVixhmCO9RMb62pdaA6rlJ84O1k4Tr5lipJ23EIPEbnfsa1cn /7dA== 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=8tHBOwEgNWH09JklTln1w+xcu+YXuIF4Gianyno/wXs=; b=HVFDA+V4PwFihmk80qo8GkfHhho7hjH0RN2XzPRVX5PIx4R9t7fgz+1SVAsQSpLoP8 nmA0WFvSYF1e6zkahEYGrfcMqtDJfz4JYc3v9GO7ixK4tqzQfsy7aQF/PdJgHY36Bk5q hwtrSdH2qC6+BPN8xJopZhpJhUG5EF0m9Fyn9oTW/fGcam7MISBdkMi8ZWaW7e1QqaUJ tyul1RUvaFNg74GXT9KXKQEdWu8jA3XgorReoRhkKSZb5f7S9TIxxWPZzYzsAWJsOqGl hEHPER6ryZU4y9SUvPtH7OmcAzqtPcYFrV+CHwGSlnkyoY40FTz2xGI1vGYBN3/kFtZq vMgw== X-Gm-Message-State: AKwxyteHhOnq4pyV0wh81Ms/DyhzWCt0M2ZP3N9wLoKN5MHs1xZF6Kcv kx2IjLuFPtDeZpWCEy7o4YZwnA== X-Google-Smtp-Source: ACJfBosMbGxwGJn4AfEeYFmMNqzoGVpNiSc8lAaRRrn/+R72vgn6nDYrInhMhBta+Zb6Y3rQjp1gng== X-Received: by 10.46.93.210 with SMTP id v79mr8946660lje.80.1516284344417; Thu, 18 Jan 2018 06:05:44 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:43 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 7/9] vti/tunnel: Unify ikey/okey printing Date: Thu, 18 Jan 2018 16:04:34 +0200 Message-Id: <1516284276-10942-8-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 For vti6 tunnel we print [io]key in dotted-quad notation (ipv4 address) while in vti we do that in hex format. For vti tunnel we print [io]key only if value is not zero while for vti6 we miss such check. Unify vti and vti6 tunnel [io]key output. While here enlarge s2 buffer to the same size as in rest of tunnel support code (64 bytes) and check return from inet_ntop(). Signed-off-by: Serhey Popovych --- ip/link_vti.c | 22 ++++++++++++++-------- ip/link_vti6.c | 14 ++++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ip/link_vti.c b/ip/link_vti.c index baaaf2f..1439e53 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -167,8 +167,7 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { const char *local = "any"; const char *remote = "any"; - __u32 key; - char s2[IFNAMSIZ]; + char s2[64]; if (!tb) return; @@ -200,14 +199,21 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_VTI_IKEY] && - (key = rta_getattr_u32(tb[IFLA_VTI_IKEY]))) - print_0xhex(PRINT_ANY, "ikey", "ikey %#x ", ntohl(key)); + if (tb[IFLA_VTI_IKEY]) { + struct rtattr *rta = tb[IFLA_VTI_IKEY]; + __u32 key = rta_getattr_u32(rta); + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "ikey", "ikey %s ", s2); + } + + if (tb[IFLA_VTI_OKEY]) { + struct rtattr *rta = tb[IFLA_VTI_OKEY]; + __u32 key = rta_getattr_u32(rta); - if (tb[IFLA_VTI_OKEY] && - (key = rta_getattr_u32(tb[IFLA_VTI_OKEY]))) - print_0xhex(PRINT_ANY, "okey", "okey %#x ", ntohl(key)); + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "okey", "okey %s ", s2); + } if (tb[IFLA_VTI_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]); diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 952dbb0..6b61e3d 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -199,13 +199,19 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } if (tb[IFLA_VTI_IKEY]) { - inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2)); - print_string(PRINT_ANY, "ikey", "ikey %s ", s2); + struct rtattr *rta = tb[IFLA_VTI_IKEY]; + __u32 key = rta_getattr_u32(rta); + + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "ikey", "ikey %s ", s2); } if (tb[IFLA_VTI_OKEY]) { - inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2)); - print_string(PRINT_ANY, "okey", "okey %s ", s2); + struct rtattr *rta = tb[IFLA_VTI_OKEY]; + __u32 key = rta_getattr_u32(rta); + + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "okey", "okey %s ", s2); } if (tb[IFLA_VTI_FWMARK]) { From patchwork Thu Jan 18 14:04:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862918 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="TmyhvNN5"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm2c2ttwz9sDB for ; Fri, 19 Jan 2018 01:06:00 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932411AbeAROFt (ORCPT ); Thu, 18 Jan 2018 09:05:49 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:33284 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932326AbeAROFr (ORCPT ); Thu, 18 Jan 2018 09:05:47 -0500 Received: by mail-lf0-f66.google.com with SMTP id t139so12029679lff.0 for ; Thu, 18 Jan 2018 06:05:46 -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=Atf2T5ofRL6bJM6b/8mKvXEmADxgKuBrkSdG4xc8pJU=; b=TmyhvNN5qD2fLZ89v48gwZ54NTrhTYM0rAZB3Thl0SCsJfg+0EBbv8TYXIoyboYekL D1n3j2zsPXXjBRu8GmkuX9ruMUqfjTcgLpkFsqtXPC01f8D6k6f7VBmMvLgpNjKPeQ7e 1UThjqwrqh35P02b6278M794m2lyfx6bHUbu3s8tBwjSN6NVngYN+uCZcobnYHpfmgUk JhjLDqCbXiFxAu5Bm3CB8ibfr4fpEckZzcSlS9qfEaoRdY6MiWCq8uLe+xau8CphP8hE C8eB2lGXbgwhbteD6APqJccycpt180MLL5mgKTg+c2GtUhmzKYi9sl8vhm/Pbjblcy80 Nt8g== 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=Atf2T5ofRL6bJM6b/8mKvXEmADxgKuBrkSdG4xc8pJU=; b=rvbQ3FXyE+KpbnrpavEQbeb/XMc1i5551a4CIFgJcK1ODMI1SqbUzfFVqsJ4rMDT2i X0er7uGCpNpU2h8dPFeYJewnITE2OSNQVkGDK7BfMsuMxfDRxYHI67YJjcQLvOtCHGD7 8n4w0uudCSG1nl7jQ6jbfXNzIRqPyfO5BU/Fl1Hniy8M5uxaSI/bfheDzgQxgFAw8zUq zz2OobKrFzI522gbdP074JhAM8eKU0J1DhRYJ+viuLYEEhchQlY05MP4SkVGWc5maD9X fRxl4akrWe+n+9Rilvfs3ZDiOa8FJz49Dxp4KKm42r8c4Xkdw6kUdZiDiHXxMy/Fkh9l zKwA== X-Gm-Message-State: AKwxyteZBQ1Ap5Z5D61Uq0l3dbSlET7zWxh5MctYU3FQsGe55UDVQQQa CiODLAKqgPY9c3illT681Mg/9A== X-Google-Smtp-Source: ACJfBoupyn3ovn0poIjFouWOANZFwQE2Hlv0TZZ2EjeSbTrJWLhR6zu1j8YLAkFPZEdzwd0c1jiAYA== X-Received: by 10.46.17.135 with SMTP id 7mr11782448ljr.87.1516284345709; Thu, 18 Jan 2018 06:05:45 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:44 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 8/9] vti6/tunnel: Unify and simplify link type help functions Date: Thu, 18 Jan 2018 16:04:35 +0200 Message-Id: <1516284276-10942-9-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 Both of these two changes are missing for link_vti6.c: commit 8b47135474cd ("ip: link: Unify link type help functions a bit") commit 561e650eff67 ("ip link: Shortify printing the usage of link type") Replay them on link_vti6.c to bring link type help functions inline with other tunneling code. Signed-off-by: Serhey Popovych --- ip/link_vti6.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 6b61e3d..2a86d59 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -24,20 +24,25 @@ #include "ip_common.h" #include "tunnel.h" +static void print_usage(FILE *f) +{ + fprintf(f, + "Usage: ... vti6 [ remote ADDR ]\n" + " [ local ADDR ]\n" + " [ [i|o]key KEY ]\n" + " [ dev PHYS_DEV ]\n" + " [ fwmark MARK ]\n" + "\n" + "Where: ADDR := { IPV6_ADDRESS }\n" + " KEY := { DOTTED_QUAD | NUMBER }\n" + " MARK := { 0x0..0xffffffff }\n" + ); +} static void usage(void) __attribute__((noreturn)); static void usage(void) { - fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n"); - fprintf(stderr, " type { vti6 } [ remote ADDR ] [ local ADDR ]\n"); - fprintf(stderr, " [ [i|o]key KEY ]\n"); - fprintf(stderr, " [ dev PHYS_DEV ]\n"); - fprintf(stderr, " [ fwmark MARK ]\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Where: NAME := STRING\n"); - fprintf(stderr, " ADDR := { IPV6_ADDRESS }\n"); - fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); - fprintf(stderr, " MARK := { 0x0..0xffffffff }\n"); + print_usage(stderr); exit(-1); } @@ -224,9 +229,16 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } +static void vti6_print_help(struct link_util *lu, int argc, char **argv, + FILE *f) +{ + print_usage(f); +} + struct link_util vti6_link_util = { .id = "vti6", .maxattr = IFLA_VTI_MAX, .parse_opt = vti6_parse_opt, .print_opt = vti6_print_opt, + .print_help = vti6_print_help, }; From patchwork Thu Jan 18 14:04:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 862919 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="Zz7GYyK7"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMm3174Fgz9s7F for ; Fri, 19 Jan 2018 01:06:21 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932508AbeAROGS (ORCPT ); Thu, 18 Jan 2018 09:06:18 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34332 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932337AbeAROFs (ORCPT ); Thu, 18 Jan 2018 09:05:48 -0500 Received: by mail-lf0-f66.google.com with SMTP id k19so6565172lfj.1 for ; Thu, 18 Jan 2018 06:05:47 -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=ruGTDgtmTHKU+TuY0YFeOxcYtUANqqnMo7cWnqbS0ds=; b=Zz7GYyK7VUROVvx2hHWKs+1zX0jR/Ki7nNV2fchr0stHQh+2jIIgM76+nitBr/VH/2 tURH/GoNIaV4xyj+668xJ10dEraD1/0mJ/A34TwLJQXOKDaWIpxG7VJgI3gdZ4Es2Cx/ 6RvIftSHh6/5c3i+OmsuIxda4qqEW6AT8tCN5csGGYHLa0VQgABfzZ8kyEP6Nv5CfsZU vQWtKs4e6b8ie/qp8DdoCxKbaBeLIS2gl0Am7qdlqzEV164f/EazX7wbjZfenHBHSZR9 aOB7U6sv30MiElQkueU8B9Fmg0FLWCL8JXM7ip7HWZjP2DkEwsFC5mnlGPuJF0onYU0H ffPQ== 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=ruGTDgtmTHKU+TuY0YFeOxcYtUANqqnMo7cWnqbS0ds=; b=Iw5gR7Y4vEOg9M2IjhlT4suTZE1jbFUtC8+M27uzzWyf4dh8ozhXrLUn8Yf4I3027s xaFGvgKQYptyVEUR4ISG7gIAHEQ6JxjuyCxqU5hrIdmKx2pnewk7q7rp14iXpkDZA43I whfnpSJHZRB00fYdcKtcm7Ki2dWP9pLfYuRu0FPzR85w67QOtQ+ZC2yqjm1vFHmQeuG7 iZ/AIflHJobyBWvwhTmSJqAY6uz9Y0vCp/Vc+e8VIf9/AWzMljokF3rB9KPJWbVtsSz2 I82cV4KMh2078a+aqQlhdYF3fZE9haY3psu8KEfyVFgWHeEvuMwHU9XvADwMA1DjmYOV RUXg== X-Gm-Message-State: AKwxyteUd5mW/29+n01Ca7Gr81c8my1zgJ+MkrFcJBNoFNyzxgMWTRIF 0CWu3yrLznsY9Ztf44otw2Pb6Q== X-Google-Smtp-Source: ACJfBosHzldL+D6PXO1MQF+q7Umdgwop4SNsQOTqM4SbPqWWLPPq6aAcX9HIrZe8xFibxebLRebkpw== X-Received: by 10.25.67.17 with SMTP id q17mr1163958lfa.114.1516284346777; Thu, 18 Jan 2018 06:05:46 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id r21sm1344748lja.8.2018.01.18.06.05.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 06:05:46 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 v2 9/9] tunnel: Return constant string without copying it Date: Thu, 18 Jan 2018 16:04:36 +0200 Message-Id: <1516284276-10942-10-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1516284276-10942-1-git-send-email-serhe.popovych@gmail.com> References: <1516284276-10942-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 We return constant string from tnl_strproto(), no need to copy it to temporary buffer and then return such buffer as const: return constant string instead. Signed-off-by: Serhey Popovych --- ip/tunnel.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/ip/tunnel.c b/ip/tunnel.c index 42ae70e..0414804 100644 --- a/ip/tunnel.c +++ b/ip/tunnel.c @@ -40,33 +40,22 @@ const char *tnl_strproto(__u8 proto) { - static char buf[16]; - switch (proto) { case IPPROTO_IPIP: - strcpy(buf, "ip"); - break; + return "ip"; case IPPROTO_GRE: - strcpy(buf, "gre"); - break; + return "gre"; case IPPROTO_IPV6: - strcpy(buf, "ipv6"); - break; + return "ipv6"; case IPPROTO_ESP: - strcpy(buf, "esp"); - break; + return "esp"; case IPPROTO_MPLS: - strcpy(buf, "mpls"); - break; + return "mpls"; case 0: - strcpy(buf, "any"); - break; + return "any"; default: - strcpy(buf, "unknown"); - break; + return "unknown"; } - - return buf; } int tnl_get_ioctl(const char *basedev, void *p)