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