]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
Use standard routines for interface name to index etc
authorStephen Hemminger <stephen.hemminger@vyatta.com>
Sun, 28 Nov 2010 18:35:28 +0000 (10:35 -0800)
committerStephen Hemminger <stephen.hemminger@vyatta.com>
Sun, 28 Nov 2010 18:35:28 +0000 (10:35 -0800)
Use the available libraries for mapping from interface index to name
or type. This should speed up display with lots of interfaces

ip/ip6tunnel.c
ip/iptunnel.c
ip/link_gre.c
ip/tunnel.c
ip/tunnel.h

index 203e4a3a920d815a0a8c5ba8bc84a90708e9dd3f..f0db5aa04c98b5bae354b2dd1d9009c6b55e3b09 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "utils.h"
 #include "tunnel.h"
+#include "ip_common.h"
 
 #define IP6_FLOWINFO_TCLASS    htonl(0x0FF00000)
 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
@@ -75,7 +76,7 @@ static void print_tunnel(struct ip6_tnl_parm *p)
        printf("%s: %s/ipv6 remote %s local %s",
               p->name, tnl_strproto(p->proto), remote, local);
        if (p->link) {
-               char *n = tnl_ioctl_get_ifname(p->link);
+               const char *n = ll_index_to_name(p->link);
                if (n)
                        printf(" dev %s", n);
        }
@@ -210,7 +211,7 @@ static int parse_args(int argc, char **argv, struct ip6_tnl_parm *p)
                argc--; argv++;
        }
        if (medium[0]) {
-               p->link = tnl_ioctl_get_ifindex(medium);
+               p->link = ll_name_to_index(medium);
                if (p->link == 0)
                        return -1;
        }
@@ -266,7 +267,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                char name[IFNAMSIZ];
-               int type;
+               int index, type;
                unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
                        rx_fifo, rx_frame,
                        tx_bytes, tx_packets, tx_errs, tx_drops,
@@ -288,7 +289,10 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
                        continue;
                if (p->name[0] && strcmp(p->name, name))
                        continue;
-               type = tnl_ioctl_get_iftype(name);
+               index = ll_name_to_index(name);
+               if (index == 0)
+                       continue;
+               type = ll_index_to_type(index);
                if (type == -1) {
                        fprintf(stderr, "Failed to get type of [%s]\n", name);
                        continue;
@@ -298,7 +302,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
                memset(&p1, 0, sizeof(p1));
                ip6_tnl_parm_init(&p1, 0);
                strcpy(p1.name, name);
-               p1.link = tnl_ioctl_get_ifindex(p1.name);
+               p1.link = ll_name_to_index(p1.name);
                if (p1.link == 0)
                        continue;
                if (tnl_get_ioctl(p1.name, &p1))
@@ -329,6 +333,7 @@ static int do_show(int argc, char **argv)
 {
         struct ip6_tnl_parm p;
 
+       ll_init_map(&rth);
        ip6_tnl_parm_init(&p, 0);
        p.proto = 0;  /* default to any */
 
index 3525fbb2f9e19e3e1b9021851a50a35919e5654b..2a5c1a1942d26843ad7e093293a430c6c6bec35f 100644 (file)
@@ -18,8 +18,8 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
-#include <linux/if.h>
-#include <linux/if_arp.h>
+#include <net/if.h>
+#include <net/if_arp.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
 
@@ -231,7 +231,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
        }
 
        if (medium[0]) {
-               p->link = tnl_ioctl_get_ifindex(medium);
+               p->link = if_nametoindex(medium);
                if (p->link == 0)
                        return -1;
        }
@@ -342,7 +342,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
        }
 
        if (p->link) {
-               char *n = tnl_ioctl_get_ifname(p->link);
+               const char *n = ll_index_to_name(p->link);
                if (n)
                        printf(" dev %s ", n);
        }
@@ -402,7 +402,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
        rx_fifo, rx_frame,
        tx_bytes, tx_packets, tx_errs, tx_drops,
        tx_fifo, tx_colls, tx_carrier, rx_multi;
-       int type;
        struct ip_tunnel_parm p1;
 
        char buf[512];
@@ -416,6 +415,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
        fgets(buf, sizeof(buf), fp);
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
+               int index, type;
                char *ptr;
                buf[sizeof(buf) - 1] = 0;
                if ((ptr = strchr(buf, ':')) == NULL ||
@@ -431,7 +431,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                        continue;
                if (p->name[0] && strcmp(p->name, name))
                        continue;
-               type = tnl_ioctl_get_iftype(name);
+               index = ll_name_to_index(name);
+               if (index == 0)
+                       continue;
+               type = ll_index_to_type(index);
                if (type == -1) {
                        fprintf(stderr, "Failed to get type of [%s]\n", name);
                        continue;
@@ -467,6 +470,7 @@ static int do_show(int argc, char **argv)
        int err;
        struct ip_tunnel_parm p;
 
+       ll_init_map(&rth);
        if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
                return -1;
 
index 9f8bde66da1f899d05a8dcf848b4faf9190fe14b..62baaf484eea5b8384babd3d2d47336af144735f 100644 (file)
@@ -206,7 +206,7 @@ get_failed:
                                saddr = get_addr32(*argv);
                } else if (!matches(*argv, "dev")) {
                        NEXT_ARG();
-                       link = tnl_ioctl_get_ifindex(*argv);
+                       link = if_nametoindex(*argv);
                        if (link == 0)
                                exit(-1);
                } else if (!matches(*argv, "ttl") ||
@@ -298,7 +298,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 
        if (tb[IFLA_GRE_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_GRE_LINK])) {
                unsigned link = *(__u32 *)RTA_DATA(tb[IFLA_GRE_LINK]);
-               char *n = tnl_ioctl_get_ifname(link);
+               const char *n = if_indextoname(link, s2);
 
                if (n)
                        fprintf(f, "dev %s ", n);
index 6efbd2dfcebfdd868c116f94a94c2fd8247c8498..b176d3f04e611feab98ceeb7be391a123f9b1f2e 100644 (file)
@@ -63,58 +63,6 @@ const char *tnl_strproto(__u8 proto)
        return buf;
 }
 
-int tnl_ioctl_get_ifindex(const char *dev)
-{
-       struct ifreq ifr;
-       int fd;
-       int err;
-
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-       fd = socket(preferred_family, SOCK_DGRAM, 0);
-       err = ioctl(fd, SIOCGIFINDEX, &ifr);
-       if (err) {
-               perror("ioctl");
-               return 0;
-       }
-       close(fd);
-       return ifr.ifr_ifindex;
-}
-
-int tnl_ioctl_get_iftype(const char *dev)
-{
-       struct ifreq ifr;
-       int fd;
-       int err;
-
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-       fd = socket(preferred_family, SOCK_DGRAM, 0);
-       err = ioctl(fd, SIOCGIFHWADDR, &ifr);
-       if (err) {
-               perror("ioctl");
-               return -1;
-       }
-       close(fd);
-       return ifr.ifr_addr.sa_family;
-}
-
-
-char * tnl_ioctl_get_ifname(int idx)
-{
-       static struct ifreq ifr;
-       int fd;
-       int err;
-
-       ifr.ifr_ifindex = idx;
-       fd = socket(preferred_family, SOCK_DGRAM, 0);
-       err = ioctl(fd, SIOCGIFNAME, &ifr);
-       if (err) {
-               perror("ioctl");
-               return NULL;
-       }
-       close(fd);
-       return ifr.ifr_name;
-}
-
 int tnl_get_ioctl(const char *basedev, void *p)
 {
        struct ifreq ifr;
@@ -126,7 +74,9 @@ int tnl_get_ioctl(const char *basedev, void *p)
        fd = socket(preferred_family, SOCK_DGRAM, 0);
        err = ioctl(fd, SIOCGETTUNNEL, &ifr);
        if (err)
-               perror("ioctl");
+               fprintf(stderr, "get tunnel %s failed: %s\n", basedev, 
+                       strerror(errno));
+
        close(fd);
        return err;
 }
@@ -145,7 +95,8 @@ int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
        fd = socket(preferred_family, SOCK_DGRAM, 0);
        err = ioctl(fd, cmd, &ifr);
        if (err)
-               perror("ioctl");
+               fprintf(stderr, "add tunnel %s failed: %s\n", ifr.ifr_name,
+                       strerror(errno));
        close(fd);
        return err;
 }
@@ -160,16 +111,19 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p)
                strncpy(ifr.ifr_name, name, IFNAMSIZ);
        else
                strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+
        ifr.ifr_ifru.ifru_data = p;
        fd = socket(preferred_family, SOCK_DGRAM, 0);
        err = ioctl(fd, SIOCDELTUNNEL, &ifr);
        if (err)
-               perror("ioctl");
+               fprintf(stderr, "delete tunnel %s failed: %s\n",
+                       ifr.ifr_name, strerror(errno));
        close(fd);
        return err;
 }
 
-static int tnl_gen_ioctl(int cmd, const char *name, void *p, int skiperr)
+static int tnl_gen_ioctl(int cmd, const char *name, 
+                        void *p, int skiperr)
 {
        struct ifreq ifr;
        int fd;
@@ -180,7 +134,8 @@ static int tnl_gen_ioctl(int cmd, const char *name, void *p, int skiperr)
        fd = socket(preferred_family, SOCK_DGRAM, 0);
        err = ioctl(fd, cmd, &ifr);
        if (err && errno != skiperr)
-               perror("ioctl");
+               fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
+                       cmd, strerror(errno));
        close(fd);
        return err;
 }
index ded226b4faf92a08f816ae1b5cf87c6c22ad2626..7e7fe135905c3559cf7d19a2ba62dbc10c33ece2 100644 (file)
@@ -25,9 +25,7 @@
 #include <linux/types.h>
 
 const char *tnl_strproto(__u8 proto);
-int tnl_ioctl_get_ifindex(const char *dev);
-int tnl_ioctl_get_iftype(const char *dev);
-char * tnl_ioctl_get_ifname(int idx);
+
 int tnl_get_ioctl(const char *basedev, void *p);
 int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p);
 int tnl_del_ioctl(const char *basedev, const char *name, void *p);