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]);