From patchwork Fri Jan 12 17:39:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 860087 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="Eaz/649M"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94K4wzGz9t3G for ; Sat, 13 Jan 2018 04:40:01 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934190AbeALRj7 (ORCPT ); Fri, 12 Jan 2018 12:39:59 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:44240 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933729AbeALRj4 (ORCPT ); Fri, 12 Jan 2018 12:39:56 -0500 Received: by mail-lf0-f66.google.com with SMTP id w23so6744400lfd.11 for ; Fri, 12 Jan 2018 09:39:56 -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=NdDq8Sxc1khGoRduoq+f3kNzsk25pNVC4l994YUp0i8=; b=Eaz/649MAwzFH6lJWWmCN1Dz2uOPsOH197PZUOodnMsF9W3pvvtdqJhQYULTRzO10O zORZzuz5IkHedAOjwIdC7ATszcJv3u2kaaLOHfRxRXYikuGh961WUQwb9TiDTTPv9HrZ y2Ean3VS+4kHs1KA9L5wFN+vAz5OLiW0iIuznJ1YWDdCOOBQrYHEfKuVSa1Mxwgfz+SM Gs6eaL8I2mkz0WGcfTi42HMxMtN43mT1RNXmy7unqji3YdqrOjjq34zCjCrGYji8Z6ud quMQaUdZu8f5WzOPzHwWX/jmh78sFw7uU3FK2lVfOBQWB4QbvcZorhp1xd7MIvdq/KW/ qq3A== 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=NdDq8Sxc1khGoRduoq+f3kNzsk25pNVC4l994YUp0i8=; b=JLEZ1tJQIBIEaDYb11Zn503o4ckeI3km9R6n5LNNuTAQFN0kltl1fs/5fIQ4+qberr V+RflxBLHBO39d2uHryKmSUjN1jqu/QRmD5u+S+3QVaO1JBIQOWE0bXKVXQSv27btkQV smUUpKsxUFyfhyGFP/Faas1aHBCZjQ7SMR8jUU2ZviFlf99b+bDrKFxHPVwwNGFruEKs o76Qtt5B1snQLFddjblOepG4Q8PfuyRekSJ9nV2tj7sO9jL+aKUb8/6yAMcgLMIWqJek SGWAW4LCS5s37EGS6/Gld2Mhz6sAfD3FL0pPfD/ETT4haFRmlG22noMPBzTFICtCQEIV nTQg== X-Gm-Message-State: AKGB3mJecnv1BpGeRctszbXMRU4Ly0SMXQuc/1TmgvJxws4Qlli9GDdB znTOePw4Zdgwk1OpRQMxDPMeZg== X-Google-Smtp-Source: ACJfBosS7t+SVaV9wQbplREgg65vR72At1ySvaEKkGtCo9Tz2/Gq5sZAagdefvrMEWQ+n5bLpUuZ6Q== X-Received: by 10.46.101.90 with SMTP id z87mr15969276ljb.112.1515778794997; Fri, 12 Jan 2018 09:39:54 -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.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:39:54 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 1/9] iplink: Use ll_index_to_name() instead of if_indextoname() Date: Fri, 12 Jan 2018 19:39:26 +0200 Message-Id: <1515778774-24173-2-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 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 | 4 ++-- bridge/link.c | 18 ++++++------------ ip/iplink_bond.c | 25 ++++--------------------- ip/iplink_vxlan.c | 8 ++------ ip/iproute_lwtunnel.c | 7 ++----- ip/link_gre.c | 12 +++++------- ip/link_gre6.c | 12 +++++------- ip/link_ip6tnl.c | 12 +++++------- ip/link_iptnl.c | 12 +++++------- ip/link_vti.c | 7 ++----- ip/link_vti6.c | 10 ++++------ 11 files changed, 42 insertions(+), 85 deletions(-) diff --git a/bridge/fdb.c b/bridge/fdb.c index 376713b..2cc0268 100644 --- a/bridge/fdb.c +++ b/bridge/fdb.c @@ -219,10 +219,10 @@ 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]; + const char *ifname; if (!tb[NDA_LINK_NETNSID] && - if_indextoname(ifindex, ifname)) { + (ifname = ll_index_to_name(ifindex))) { if (jw_global) jsonw_string_field(jw_global, "viaIf", ifname); diff --git a/bridge/link.c b/bridge/link.c index e2371d0..9c846c9 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,9 @@ 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 +143,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..45900c8 100644 --- a/ip/iplink_bond.c +++ b/ip/iplink_bond.c @@ -382,19 +382,9 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) 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); + const char *n = ll_index_to_name(ifindex); - if (n) - print_string(PRINT_ANY, - "active_slave", - "active_slave %s ", - n); - else - print_uint(PRINT_ANY, - "active_slave_index", - "active_slave %u ", - ifindex); + print_string(PRINT_ANY, "active_slave", "active_slave %s ", n); } if (tb[IFLA_BOND_MIIMON]) @@ -481,16 +471,9 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_BOND_PRIMARY] && (ifindex = rta_getattr_u32(tb[IFLA_BOND_PRIMARY]))) { - char buf[IFNAMSIZ]; - const char *n = if_indextoname(ifindex, buf); + const char *n = ll_index_to_name(ifindex); - if (n) - print_string(PRINT_ANY, "primary", "primary %s ", n); - else - print_uint(PRINT_ANY, - "primary_index", - "primary %u ", - ifindex); + print_string(PRINT_ANY, "primary", "primary %s ", n); } if (tb[IFLA_BOND_PRIMARY_RESELECT]) { diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c index 661eaa7..a6c964a 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -397,7 +397,6 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) unsigned int link; __u8 tos; __u32 maxaddr; - char s2[64]; if (!tb) return; @@ -469,12 +468,9 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_VXLAN_LINK] && (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) { - const char *n = if_indextoname(link, s2); + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } 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..d876bad 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -359,6 +359,7 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) char s2[64]; const char *local = "any"; const char *remote = "any"; + unsigned int link; unsigned int iflags = 0; unsigned int oflags = 0; @@ -380,14 +381,11 @@ 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])) { - unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); + if (tb[IFLA_GRE_LINK] && + (link = rta_getattr_u32(tb[IFLA_GRE_LINK]))) { + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_GRE_TTL]) { diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 55bd1fb..93f3a96 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -377,6 +377,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) char s2[64]; const char *local = "any"; const char *remote = "any"; + unsigned int link; unsigned int iflags = 0; unsigned int oflags = 0; unsigned int flags = 0; @@ -414,14 +415,11 @@ 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])) { - unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); + if (tb[IFLA_GRE_LINK] && + (link = rta_getattr_u32(tb[IFLA_GRE_LINK]))) { + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_GRE_TTL]) { diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index bbc7878..0cfb2c6 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -334,6 +334,7 @@ get_failed: static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { char s2[64]; + unsigned int link; int flags = 0; __u32 flowinfo = 0; @@ -377,14 +378,11 @@ 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])) { - unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); + if (tb[IFLA_IPTUN_LINK] && + (link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]))) { + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_IPTUN_TTL]) { diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 24a0f0c..3204e5e 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -364,6 +364,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"; + unsigned int link; __u16 prefixlen, type; if (!tb) @@ -407,14 +408,11 @@ 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])) { - unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); + if (tb[IFLA_IPTUN_LINK] && + (link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]))) { + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_int(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_IPTUN_TTL]) { diff --git a/ip/link_vti.c b/ip/link_vti.c index 2b0fab2..7ae2e3c 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -194,12 +194,9 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_VTI_LINK] && (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { - const char *n = if_indextoname(link, s2); + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_VTI_IKEY] && diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 74c246d..f7c40d3 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -190,13 +190,11 @@ 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] && + (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { + const char *n = ll_index_to_name(link); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + print_string(PRINT_ANY, "link", "dev %s ", n); } if (tb[IFLA_VTI_IKEY]) { From patchwork Fri Jan 12 17:39:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 860095 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="j0LHQZvV"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ9562Ytlz9t6m for ; Sat, 13 Jan 2018 04:40:42 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934151AbeALRkk (ORCPT ); Fri, 12 Jan 2018 12:40:40 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:37338 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932859AbeALRj6 (ORCPT ); Fri, 12 Jan 2018 12:39:58 -0500 Received: by mail-lf0-f65.google.com with SMTP id f3so6770449lfe.4 for ; Fri, 12 Jan 2018 09:39:57 -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=VoFKWqc3XEbYEGP1kYOhLEOdRS9IV9haXRMPc7wyw1g=; b=j0LHQZvVejFtAdwbPD8Yomuw0Ijt3rVmGfW+J8VR1OwzMzAfQU0wEqY9n/0TK2EgEY Rm9xkAwVn1ZIeb8G2e1LgN8jduvtLmfzKd6qMjwf1ZYAlpEPvsJQSzg40LsS7AgoVpX9 AnoM3MlRk20nl4Nf+fovGjH1NTSxrArauvjnp1/TrGp+IDn4JyS+gb25jPFC2Jdhcen+ ZhHTcnbXGJlg3Nz0+WEbaTD0bfCuPw+lG0Cpd6pyRyhpeHtgtMxiALx8Iphb+n6/MLay MB4OcWl2ldhBHxXhWM0WYzK20Umh5b8fl0L5MzGX4qfYNYtC5C24C3Yc15TriflXNnS8 /ocA== 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=VoFKWqc3XEbYEGP1kYOhLEOdRS9IV9haXRMPc7wyw1g=; b=SA/RDJRcf72P7Bk4ITWjyXYAoDbmBKbHVgR/vDouOtn27FQt43LZSjdoxcIfwii43Z k9MKkQzSdXMf6x+eEfqMWLv7snGJHJTNS+uZDnE3ON70DwLD9B5doB5jQfQR9OqVjHA1 TV93m6EEsmOANw3wnZJuaiMxZlvPDvzOwGIDVQXUoULTCqCnU1IuU6rB18F6nwZboGqv /BF+Ybkf4ysZzFVEf1oeM8//fa2eeHKnFR+5f9AhSqctJsRGmU2zFYtlBpf3e9PwpGWD 80gOJZ8OM192chbRSg/lJTn/q+uUJV4WTWEfgeb27DC1Za6M5hUWVfToW8MtNZLXLPH6 vRkg== X-Gm-Message-State: AKwxytcwpTTCdiz/G6a3tRZ6p5lFV9cKNbMgFEt1BzTwrKD1ZVzKlvbz CNNV8z8VXWbaNOGVcWI71bq8EQ== X-Google-Smtp-Source: ACJfBouUFLKiLUSk49Qhp6X2WNkvfhTL6R7t5zxNJy/REvFDfElS1c4UgbTc4C3ZBCTcZaVbC6g6mw== X-Received: by 10.25.125.133 with SMTP id y127mr12983579lfc.44.1515778796431; Fri, 12 Jan 2018 09:39:56 -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.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:39:55 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 2/9] ip/tunnel: Correct and unify ttl/hoplimit printing Date: Fri, 12 Jan 2018 19:39:27 +0200 Message-Id: <1515778774-24173-3-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 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 a6c964a..bac326d 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -395,6 +395,7 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; unsigned int link; + __u8 ttl = 0; __u8 tos; __u32 maxaddr; @@ -525,14 +526,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 d876bad..2f6be20 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 link; 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]); @@ -388,16 +389,12 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "link", "dev %s ", n); } - 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 93f3a96..9b08656 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -383,6 +383,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; @@ -422,14 +423,12 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "link", "dev %s ", n); } - 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 0cfb2c6..bd06e46 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -337,6 +337,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb unsigned int link; int flags = 0; __u32 flowinfo = 0; + __u8 ttl = 0; if (!tb) return; @@ -385,12 +386,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb print_string(PRINT_ANY, "link", "dev %s ", n); } - 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 3204e5e..e19cb21 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"; unsigned int link; __u16 prefixlen, type; + __u8 ttl = 0; if (!tb) return; @@ -415,16 +416,12 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ print_string(PRINT_ANY, "link", "dev %s ", n); } - 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 Fri Jan 12 17:39: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: 860088 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="b8y0c5g5"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94Q3yGxz9s4s for ; Sat, 13 Jan 2018 04:40:06 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964913AbeALRkD (ORCPT ); Fri, 12 Jan 2018 12:40:03 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:46198 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934126AbeALRj7 (ORCPT ); Fri, 12 Jan 2018 12:39:59 -0500 Received: by mail-lf0-f67.google.com with SMTP id a12so6738351lfe.13 for ; Fri, 12 Jan 2018 09:39:58 -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=+5MZiePmXul7LDWHDfNxK+9HRjBZ3H/tfp1rU+lglTw=; b=b8y0c5g5hescNA6KGtAAl/w+QFcRd6iKsJzpbSf5qMM2jwR9LcLbF6D/l9FSLT/un4 Y+bNM6tD8rPG6q0h5fmRNJTPgf+JsrK7PgcyrqIkXjTng/TBK+pe/5SGUMQInscvvKR+ Jx1+BkSK3n+vr9VI4mOMNYu39AXOGpQH1QWmHxYZBuVfa8bgHL9fGyg8CDtyYpjILyIz 0O6wc4Gzwfu8eJwhjxs+AulM8grl48kJVlhIWfzebfcNn/t356vxWyuKY++XyFWKG0H1 FwuCvXifIeyZZYG8WmeCjE3GsssthJ73JX4wVa8YtlaBnY7tfFXqp5fSFhfRkkfItA17 J1tA== 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=+5MZiePmXul7LDWHDfNxK+9HRjBZ3H/tfp1rU+lglTw=; b=EOqJO6wuSoQ92Fc7NmC5j6K2S4IXWQNGuXCH2PvXy2EBKk1lIyr/k1PsnM+0/ERrgc pojGlknKJmj3OQw+n+sRC8C73+urlKQqQwMovZDGOOS67gGdxXXjJbIPtlgmWB5dLW6I giIGcKMzC2itOp1yv3UEWuICgorFD40kCwWaC4C8MIj+IJhVgmB4A86l4bYtXdmeX/1y dBB94FrRs7u/TQhKuPrHCzeWtpLZZkOxo58uOWgKD0QRrGIZ2pzmI4FzW/IxcJj1366p W1Tzx7OoD6ymQFD8Lex6ki4YcoQ62q2MtyrXtiaMMDlMbIX1PQrM9pfmWVgE6Ewqi1z9 P4zw== X-Gm-Message-State: AKwxytcUFsi/dYSEZe+nRV20Xo1fc6kBz/9GgrkpfJqsYfAQDf7VGwer OZZ337d4iprL4eJc92jfXyK9bQ== X-Google-Smtp-Source: ACJfBota/cNC2arhu6GN6SqeSSuFDHeNbSlyjZCNWEI6ffu04WvTF7ZRNUYdw19pRH8bFd5lTX71Ng== X-Received: by 10.25.147.11 with SMTP id v11mr8801660lfd.93.1515778797604; Fri, 12 Jan 2018 09:39:57 -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.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:39:56 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 3/9] ip/tunnel: Simplify and unify tos printing Date: Fri, 12 Jan 2018 19:39:28 +0200 Message-Id: <1515778774-24173-4-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 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 bac326d..e292369 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -396,7 +396,7 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) __u32 vni; unsigned int link; __u8 ttl = 0; - __u8 tos; + __u8 tos = 0; __u32 maxaddr; if (!tb) @@ -514,16 +514,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 2f6be20..11a131f 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -363,6 +363,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]); @@ -396,21 +397,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 e19cb21..fd08bd9 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -367,6 +367,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ unsigned int link; __u16 prefixlen, type; __u8 ttl = 0; + __u8 tos = 0; if (!tb) return; @@ -423,20 +424,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 Fri Jan 12 17:39: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: 860094 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="tIHnepnp"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94y6w1Yz9t7R for ; Sat, 13 Jan 2018 04:40:34 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964984AbeALRkd (ORCPT ); Fri, 12 Jan 2018 12:40:33 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:41074 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933729AbeALRkA (ORCPT ); Fri, 12 Jan 2018 12:40:00 -0500 Received: by mail-lf0-f65.google.com with SMTP id h137so6763219lfe.8 for ; Fri, 12 Jan 2018 09:40:00 -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=WjqEFv0NaJcjT2FPiJwLXi0IGL35/3MoHxQIaMyJ7sU=; b=tIHnepnphgI92JHcYsEopvrJujmdz1qQ2dyUEJTsfeMZLOEY7iOd8ZsvTwj3JBRSsv aib8V+atQ5Mcuj1VBJWsuxTIlSNXmSCnTzwmiqx/GiiIWNc7FOeJbR+piPPiy4d0bDZ8 nQMVdhFzQ3XXruzk2CKIwc5p1vkngU1pqg3he7VZJuZX7BHlJs1Ixthyy0QL/Xe/5Vca 5zWvoR1p2F+E7aXh5gglM/IHb150wIHebfbdKcHtISCY5T8jz+GmsSyl3Aaj3sFwspim cIKLhiebx/YQls+MvAdzbgnh+wevXKyHj2wKF0YqLKP/W4W6+yZjANHxj1oK8ng9TK/6 jmWg== 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=WjqEFv0NaJcjT2FPiJwLXi0IGL35/3MoHxQIaMyJ7sU=; b=gqdQrTQsZZmneAk511nqxi/WN38K/dXWbQ9LzgB7PXRqOTR/EobQHfBfL9/1rCIZ4n TGjWKElFSmtEvslhcx9tnkR3WeqbqRKYDLoiZOMCGZlvNghgvY28S3GfuBML6qgM9Ctt yiQDGrFEvSDXCcriAPYpqLDLT4MBSuCgpeGKb6dpMvXS8M8in6LSfchHWmGurK6JU7/U DdxVow4ciTi1FcO35Et3Ig4Di0C9g9pHoP0+5PYJp9zOMKUL5vTkMlSLnUge0xY2qCiR p6pTKasUDlP8VLram5yYDjUDwx6Bjdv3HfNuFONcrxXkfOBQrB9fpmXaiC9gKXY+sX8z WKqQ== X-Gm-Message-State: AKwxytff2ULDmIR0CipBBTAEBbnAlrIVd5Tqq+YcOy3ZBZH/9BHec3ON 0qMqc291xPdeYU8CZhMDpxgifQ== X-Google-Smtp-Source: ACJfBotrWrbKkbhkxEqeNUBHXu9AbLlWe+zIJEFGBDbB2Q8ZSc+HIzh/8U1GlwLzEclV7QjCfC62Zg== X-Received: by 10.25.67.16 with SMTP id q16mr3118267lfa.26.1515778798988; Fri, 12 Jan 2018 09:39:58 -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.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:39:58 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 4/9] ip/tunnel: Use print_0xhex() instead of print_string() Date: Fri, 12 Jan 2018 19:39:29 +0200 Message-Id: <1515778774-24173-5-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 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 | 14 ++++---------- ip/link_vti6.c | 13 ++++--------- 6 files changed, 32 insertions(+), 49 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index 11a131f..2ae2194 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -442,9 +442,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 9b08656..9576354 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -496,18 +496,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 bd06e46..9594c3e 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -437,6 +437,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", @@ -446,19 +452,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 fd08bd9..fcb0795 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"; @@ -454,7 +453,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); @@ -481,6 +480,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]); @@ -544,16 +552,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 7ae2e3c..eebf542 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -168,6 +168,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; + __u32 fwmark; unsigned int link; char s2[IFNAMSIZ]; @@ -208,16 +209,9 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) (key = rta_getattr_u32(tb[IFLA_VTI_OKEY]))) print_0xhex(PRINT_ANY, "okey", "okey %#x ", ntohl(key)); - if (tb[IFLA_VTI_FWMARK]) { - __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); - } - } + if (tb[IFLA_VTI_FWMARK] && + (fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]))) + print_0xhex(PRINT_ANY, "fwmark", "fwmark 0x%x ", fwmark); } static void vti_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_vti6.c b/ip/link_vti6.c index f7c40d3..29a7062 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -168,6 +168,7 @@ 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; + __u32 fwmark; unsigned int link; char s2[64]; @@ -207,15 +208,9 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "okey", "okey %s ", s2); } - if (tb[IFLA_VTI_FWMARK]) { - __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); - } - } + if (tb[IFLA_VTI_FWMARK] && + (fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]))) + print_0xhex(PRINT_ANY, "fwmark", "fwmark 0x%x ", fwmark); } struct link_util vti6_link_util = { 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 From patchwork Fri Jan 12 17:39: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: 860089 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="iacWC655"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94R5RnRz9t3G for ; Sat, 13 Jan 2018 04:40:07 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964939AbeALRkF (ORCPT ); Fri, 12 Jan 2018 12:40:05 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:39177 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964885AbeALRkD (ORCPT ); Fri, 12 Jan 2018 12:40:03 -0500 Received: by mail-lf0-f68.google.com with SMTP id m8so6762799lfc.6 for ; Fri, 12 Jan 2018 09:40:02 -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=hcy39O1Iv2RwIyxs9qfopsifGsxpXVPwlnuDeBJM0J0=; b=iacWC655IhzijENWyQg4sVdiS1GBpuh5LquP6uwjpD1T+a+sobd3xR63jFtmJZLow1 ML1ISGwWBlvO6wBIJkYSs4dewlwiIH/TynByaUvRapllwLC+gFQMnWJPFObiA7pZI/1J 32/dEzFgOfyLKt3Mix+N6Ugb8Ksn/BeSmTLJEZA9J+BdOjHbNQ2Kp+W/7sabPMO1ojit 0y77WR88pu5M3i4e4j5yANA9u6lA8MVszxhQdbwssTy36WVyEFZFQ1jVztSid3lqd8/E C8rzERUb37qE48S0o+nj7eUnc/IXMDQNW0h/uSdo4y9QwQ9m7YCinZwzcb/o18FVNQE6 7nhw== 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=hcy39O1Iv2RwIyxs9qfopsifGsxpXVPwlnuDeBJM0J0=; b=KSLPamyDkmz6QdtL13vu7jIr7Vg8dwIsJvtoo52AC2ID673Oav9DVOGt3lxLSc54Ig 3/ZmU7/Apdz8tKZkSfLUUeKqGevJmQ70UmDvBISJLOCihJlLZH8Ef92CLIWXhsyVa2tt WDcSoizBaJ6VczsqzwacqmThLnmgekXiNJV0hKhWI2TmDDzsvmwzqI5AuB1zNFB+Hik4 JGt/D8fe9y9yYhybPDwnt2t+9Mm8+uXFWijIdNGC8onud1h1kSK/fs5j81ggFPLoPbZI YcGRqk8JvdpJS5y+gjCbKwMHOWup4geF42pnfe0E0DufpFXTufwCYskf09BEQfmo5H9N Jzbg== X-Gm-Message-State: AKwxytfpAxvwrgVqX7tEnRjR7UBi9sjYns1iuR7bzxaEl0jF6FWiG8ux mM2fDbQOei12vmwelIWbXP3ZFQ== X-Google-Smtp-Source: ACJfBotcOM8gtdPy87Z0eNjZ70YK915r7WluHYD1LZ+KF/xw5MOmiS5wtOBcfXHZA0ooQFJpR4RKLg== X-Received: by 10.25.29.15 with SMTP id d15mr13434460lfd.136.1515778801494; Fri, 12 Jan 2018 09:40:01 -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.40.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:40:00 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 6/9] gre/tunnel: Print erspan_index using print_uint() Date: Fri, 12 Jan 2018 19:39:31 +0200 Message-Id: <1515778774-24173-7-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 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 b70f73b..a7d1cd1 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -464,7 +464,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); } if (tb[IFLA_GRE_ENCAP_TYPE] && diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 41180bb..200846e 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -512,7 +512,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); } if (tb[IFLA_GRE_ENCAP_TYPE] && From patchwork Fri Jan 12 17:39: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: 860090 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="aMdRco0H"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94W0DRFz9t3H for ; Sat, 13 Jan 2018 04:40:11 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964958AbeALRkJ (ORCPT ); Fri, 12 Jan 2018 12:40:09 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:40316 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964919AbeALRkE (ORCPT ); Fri, 12 Jan 2018 12:40:04 -0500 Received: by mail-lf0-f68.google.com with SMTP id v74so4809339lfa.7 for ; Fri, 12 Jan 2018 09:40:03 -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=FhHW6WSiigV1MIOjJVP7GRTuf6AMrzDLZ2dtLcCqvW0=; b=aMdRco0HBQi3YQ6XAIdJCzVeLxe4yo7cWA7bKu2lRZPuLV9/TIfUxXH3YOUfiTj52z im70jjgtYIJzIE2ypGbVujOs0NqGuqt4ZJz/H1w4o7TrFSrg7oqR1yZlvVErdOmg3YKu K1FQN4ExdT5PI40CiJwBQ2uT1Yxes1bllJOyqD/1q03FiJM51p/pJ+nbd3g3dIZY0bDf MdgNCKWWqnK5Elkd1ZLOm8ZHPf249SiUjCnU1z/S+Mak0+0L+Xnd+z0fckWxmL7LyZul BJjnIh87D0QLad208u9aJKIz4Gt2Typpf0i929EPaDyGa+8opTDKxaWptSdk1Van5rY2 B/Lw== 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=FhHW6WSiigV1MIOjJVP7GRTuf6AMrzDLZ2dtLcCqvW0=; b=RGEIeCqtWrZN5rxIbxRZdjVMLWhW04UvcYKmUkzZPf0JSTKWNtn0+cBitpYchP5SWV pa6csljf08d2YKBMh7RlIjDSokMo6Lxl3yGhxWwfSsGa714Lv/DRGts/SmYw8QtMdAxM 522fSMzkEHSSxLH+G4U+hMZ3w6UKwdh6xeug3pFJB8Bo6P3cxCkkRmA3p7KdWZk2jtAu EZ88mZ/jc5IggSRsbh1SiUjFtDbd1Om/42WJFh37Z8JCbLDMBfMrqZw2hd8bsy26X6fg Bmf1i0/Wz+ib8uOVKl+0Xkbo791cPnLW5Tnr7DK5avi5sH50U33/KBRykNBTsS4nt/QA xRrQ== X-Gm-Message-State: AKGB3mLEVkEE0plYy4/kjpdtrqM+LWdNKIyVEMBs/FBPQpEAoQK7I0+v B+3yyBgt/co5IHQ/RDWPSZilvg== X-Google-Smtp-Source: ACJfBotNWIKwoM+9IYPrvPxovOK/qwsRQmk70Vzkob5JvROCsVqBXEbil5vam6jRMfNSp0FGd5iHrw== X-Received: by 10.25.87.139 with SMTP id l133mr13792516lfb.145.1515778802601; Fri, 12 Jan 2018 09:40:02 -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.40.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:40:01 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 7/9] ip/tunnel: Minor cleanups in print routines Date: Fri, 12 Jan 2018 19:39:32 +0200 Message-Id: <1515778774-24173-8-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 Print "unknown" parameter for "encap" type in PRINT_FP context using "%s " format specifier and benefit from complite time string merge. Unify encapsulation type check. Signed-off-by: Serhey Popovych --- ip/link_gre.c | 5 +++-- ip/link_gre6.c | 8 ++++---- ip/link_ip6tnl.c | 6 +++--- ip/link_iptnl.c | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ip/link_gre.c b/ip/link_gre.c index a7d1cd1..b4cde62 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -450,6 +450,8 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { + __u16 type; + if (!tb) return; @@ -469,8 +471,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } 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]); + (type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) { __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]); diff --git a/ip/link_gre6.c b/ip/link_gre6.c index 200846e..557151f 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -381,6 +381,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) unsigned int iflags = 0; unsigned int oflags = 0; unsigned int flags = 0; + __u16 type; __u32 flowinfo = 0; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; __u8 ttl = 0; @@ -518,15 +519,14 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } 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]); + (type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) { __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"); @@ -535,7 +535,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "type", "%s ", "gue"); break; default: - print_null(PRINT_ANY, "type", "unknown ", NULL); + print_null(PRINT_ANY, "type", "%s ", "unknown"); break; } diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index aa6f6fa..51c73dc 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]; unsigned int link; int flags = 0; + __u16 type; __u32 flowinfo = 0; __u8 ttl = 0; @@ -458,8 +459,7 @@ 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]); + (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]); @@ -474,7 +474,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb print_string(PRINT_ANY, "type", "%s ", "gue"); break; default: - print_null(PRINT_ANY, "type", "unknown ", NULL); + print_null(PRINT_ANY, "type", "%s ", "unknown"); break; } diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 83a524f..17d28ec 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -505,7 +505,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ print_string(PRINT_ANY, "type", "%s ", "gue"); break; default: - print_null(PRINT_ANY, "type", "unknown ", NULL); + print_null(PRINT_ANY, "type", "%s ", "unknown"); break; } From patchwork Fri Jan 12 17:39: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: 860091 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="Hnuv7p8z"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94b3RpCz9t5Y for ; Sat, 13 Jan 2018 04:40:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964944AbeALRkH (ORCPT ); Fri, 12 Jan 2018 12:40:07 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:46007 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964926AbeALRkF (ORCPT ); Fri, 12 Jan 2018 12:40:05 -0500 Received: by mail-lf0-f68.google.com with SMTP id y71so6750585lfd.12 for ; Fri, 12 Jan 2018 09:40:04 -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=dTSnVbja8CNtbsbIN5qeYTWtp+u+SGiyV8vNGQYBgNY=; b=Hnuv7p8zLzTdeyLADrTEjitdkjns/Ht2XOQPFXwg7WET4TAdlWpYVbPWDAZn90b5B3 60utSdtLIXQyh8dFLBCOeSgQ2P0tRPeAI1wcL+MwGT2eQ+sqQvWZhs7S0Wtl5ZGu5rFT FmNfp8+KdNxPBRu3ffqDq8ORdunYR5LqEGtnWfGnkjSg6btpcgDKwho5ybQcExmmBTzl zkNan0GuqRzAQVnJfU1JeiUhiZeVN2no/E8iGlYMYfrOIx01T6W4HH2+mmFPJGU5gGFq 6OMdUfTQBnDxglMh+gVP/OmANyG8fPgi2gTbD3FutvYO5soZj1b+3xzIwkH+yIMJH+bg g85A== 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=dTSnVbja8CNtbsbIN5qeYTWtp+u+SGiyV8vNGQYBgNY=; b=OlZHIwSr1nFBtBHkVAOEqvUT/f/tuslDZsNVG4rc9wE9tO4MmhfB1UmQW212Z+GsDo jBk2bjvc4/bP3KwwVAKO6P5DgZub3+TsTztxoPkGAjNkewZ9W7OZXk+KqSdPEh4nyg+r LXlwWqPsHjF80U0cySDJqoUI9WiiCgjWCwweOFJC6Rx82sNvwW4UejDu++aWjt36GDos JQUOJbhq7pWMcI7ZpX9xY+jNYvZ7d8hfSABdEaP15Huf3EmtmKSAnntfdhQ6LUZ48KpV ET9D7uz3H80tdircElwWSBzRpB/XdeGT8hxWDAv/UwQ4FAASxR+eZMUA7mCowp4e3VCO 0gdQ== X-Gm-Message-State: AKwxytdZr61uFAL/tqaIrIjnSZqfSoQasq35s45cjQjBFda6JmEmlimO BvEwXHl615cx9e+LAMDOyOgBRw== X-Google-Smtp-Source: ACJfBosadAyAISiQ2xsdDtGjW9qNvDaqMDSyN4t7vn96oP8P/PQ2LhnMLGLHU3OYp7LzP5k4Tvk9hA== X-Received: by 10.25.59.136 with SMTP id d8mr7429618lfl.141.1515778803868; Fri, 12 Jan 2018 09:40:03 -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.40.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:40:03 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 8/9] vti/tunnel: Unify ikey/okey printing Date: Fri, 12 Jan 2018 19:39:33 +0200 Message-Id: <1515778774-24173-9-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 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). Signed-off-by: Serhey Popovych --- ip/link_vti.c | 15 +++++++++------ ip/link_vti6.c | 7 +++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ip/link_vti.c b/ip/link_vti.c index eebf542..5fccf74 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -170,7 +170,7 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) __u32 key; __u32 fwmark; unsigned int link; - char s2[IFNAMSIZ]; + char s2[64]; if (!tb) return; @@ -201,13 +201,16 @@ 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)); - + (key = rta_getattr_u32(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); + } if (tb[IFLA_VTI_OKEY] && - (key = rta_getattr_u32(tb[IFLA_VTI_OKEY]))) - print_0xhex(PRINT_ANY, "okey", "okey %#x ", ntohl(key)); + (key = rta_getattr_u32(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); + } if (tb[IFLA_VTI_FWMARK] && (fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]))) diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 29a7062..a4ad650 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -168,6 +168,7 @@ 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; + __u32 key; __u32 fwmark; unsigned int link; char s2[64]; @@ -198,12 +199,14 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "link", "dev %s ", n); } - if (tb[IFLA_VTI_IKEY]) { + if (tb[IFLA_VTI_IKEY] && + (key = rta_getattr_u32(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); } - if (tb[IFLA_VTI_OKEY]) { + if (tb[IFLA_VTI_OKEY] && + (key = rta_getattr_u32(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); } From patchwork Fri Jan 12 17:39: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: 860092 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="Q1b3H1E9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zJ94d1WlBz9t5Y for ; Sat, 13 Jan 2018 04:40:17 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964967AbeALRkO (ORCPT ); Fri, 12 Jan 2018 12:40:14 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:35968 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964885AbeALRkH (ORCPT ); Fri, 12 Jan 2018 12:40:07 -0500 Received: by mail-lf0-f67.google.com with SMTP id e203so6786425lfg.3 for ; Fri, 12 Jan 2018 09:40:06 -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=jrs3pUX7M/Gz2H2HXrP39IZEEQd0TS1yCVlw89OEI8E=; b=Q1b3H1E9jbNq0AISQJJVJSJkJorWWu40NIQwQwA1EY5eOC2tlw5muH6/h1eCSGpX5e C8oMwpFwc2E9jE4IpPcn3UDoDP1iTTkVlj55/92xqSPrWZOY+UagS/CoA3g45Zar2rp+ fBJEb1kUaFMuekKCm3nvjvh3KMB/ilO9lvm3oDRBTtCczKSTkknUKMfFefWHFuw1HK1A 2Hu++32fjlBfZ7lEcXT1wUEbO/NcYU4ezi2/ofUQtnbZDx8FzMoV4C6nmeq1V82n3237 dt1+kBzMbYDev3EdtQ7mYO4NwJFvKdA7gHnsG43cuW59fT4/TCIp43XgefNk3CFDjwII h+ig== 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=jrs3pUX7M/Gz2H2HXrP39IZEEQd0TS1yCVlw89OEI8E=; b=CsEKHJYCHG0B5LHw0sJN4I7i0zoBGiPcOpH8TZr3Jbw9MR0bxjFaV29s0TmIJHuxaz Pfcl4D/Q9qtj2B1dlE4h+6WZPn64czV9wp99PhH8V0KdEyOOuBIykJspIxUAj4K3vHyQ TSjujat0VOEVNgokSGSHUaSp2RKRpTazvwiZ1J252l8Mk1ZBZY3eikYDRsQDaVBQp98+ 8KG//QUdPmkS7kFvjL4iCI2sPl5No17kYDBIGbwXruzK0bg80aqsNcFAC4+xHDh73hLd BmqiyFZsq+babmX0RYCmC1fk6xumcIPRxcQNwU8kkWR/mqx8IX9XC9AsaNiNycP6+Xmy 1KRw== X-Gm-Message-State: AKwxyteQpVfHfIwOAe3Yw5rNcRYA/z5wLnTvsNYusQ4uT9WT/DdD2LC/ Fffb94Usz95SZ6WNbKCYMBihHw== X-Google-Smtp-Source: ACJfBotk/Z3uKUcSYNFvudSVJSd2hOiWy6sCWH75imh9bAlQ2xGP/zqon4NokaGqPKMcSGFXhRGiQw== X-Received: by 10.25.157.84 with SMTP id g81mr14371898lfe.78.1515778805313; Fri, 12 Jan 2018 09:40:05 -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.40.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 09:40:04 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Subject: [PATCH iproute2 9/9] vti6/tunnel: Unify and simplify link type help functions Date: Fri, 12 Jan 2018 19:39:34 +0200 Message-Id: <1515778774-24173-10-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 Both of these two changes are missing for link_vti6.c: o commit 8b47135474cd (ip: link: Unify link type help functions a bit) o 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 a4ad650..b9fe83a 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); } @@ -216,9 +221,16 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_0xhex(PRINT_ANY, "fwmark", "fwmark 0x%x ", fwmark); } +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, };