]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
iplink: Use ll_index_to_name() instead of if_indextoname()
authorSerhey Popovych <serhe.popovych@gmail.com>
Thu, 18 Jan 2018 14:04:28 +0000 (16:04 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 19 Jan 2018 00:34:37 +0000 (16:34 -0800)
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 <serhe.popovych@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
bridge/fdb.c
bridge/link.c
ip/iplink_bond.c
ip/iplink_vxlan.c
ip/iproute_lwtunnel.c
ip/link_gre.c
ip/link_gre6.c
ip/link_ip6tnl.c
ip/link_iptnl.c
ip/link_vti.c
ip/link_vti6.c

index 376713b64e233c5a3cb69dd8adbf05abbc46fe82..4d55fb04b009a23165f4ffdbb8005bbee28f0f99 100644 (file)
@@ -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);
index e2371d0dbc12e10fae64f0d821cbec36e4d27be3..870ebe0504777bdede5d55c4cbfb91a6a048f955 100644 (file)
@@ -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) {
index 2b5cf4f6f4cfd12fc14c691d7175360762137458..f01fd8da52dae7a9b283c729bd4e8e6ff8ef4493 100644 (file)
@@ -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]) {
index 661eaa796acc45dcf1942fc150dacbbeb3b8201f..ad7ef1c0d136f65349825b79931ad122474e1bd3 100644 (file)
@@ -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]) {
index 740da7c6d884012c7a28e121b73eb3ab7f396851..a8d71714b7419f0da34ec454733a6969557a1fcf 100644 (file)
@@ -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) ?: "<unknown>");
+               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) ?: "<unknown>");
+               fprintf(fp, "oif %s ", ll_index_to_name(oif));
        }
 }
 
index 3c0b6d678afcfc950839e12f7defe5b45dd10f88..7463d7ca0866db0f6f701ab210f28d08ee2ab8d4 100644 (file)
@@ -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]) {
index 55bd1fbb8ddb075e3329ce454dfdb4a160c3eba0..7d38f47bb4162ff183f8c66e434157df837eba15 100644 (file)
@@ -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]) {
index bbc7878c36c6ba18e2c8a117d12930c60e1813fd..b22e7bc33cead1dc155d13722f35dc797792ddcb 100644 (file)
@@ -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]) {
index 24a0f0cd3841a1124c07874c94916e3a0a711cec..35ef5c06b8246aac2ffb7b3318624707804ad7b3 100644 (file)
@@ -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]) {
index 2b0fab27dbf46b037b3a162684ddeaee2397c23a..554cfcc43466913e292f1398ec50378d768463b9 100644 (file)
@@ -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] &&
index 74c246d3ed86bb2ef55f679d21980fe3dd24c879..a63f49fa8d0203898f105c9dc8cce6b67c11c72b 100644 (file)
@@ -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]) {