Commit ea71beac authored by Stephen Hemminger's avatar Stephen Hemminger

Use standard routines for interface name to index etc

Use the available libraries for mapping from interface index to name
or type. This should speed up display with lots of interfaces
parent 82408fc1
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "utils.h" #include "utils.h"
#include "tunnel.h" #include "tunnel.h"
#include "ip_common.h"
#define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
#define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
...@@ -75,7 +76,7 @@ static void print_tunnel(struct ip6_tnl_parm *p) ...@@ -75,7 +76,7 @@ static void print_tunnel(struct ip6_tnl_parm *p)
printf("%s: %s/ipv6 remote %s local %s", printf("%s: %s/ipv6 remote %s local %s",
p->name, tnl_strproto(p->proto), remote, local); p->name, tnl_strproto(p->proto), remote, local);
if (p->link) { if (p->link) {
char *n = tnl_ioctl_get_ifname(p->link); const char *n = ll_index_to_name(p->link);
if (n) if (n)
printf(" dev %s", n); printf(" dev %s", n);
} }
...@@ -210,7 +211,7 @@ static int parse_args(int argc, char **argv, struct ip6_tnl_parm *p) ...@@ -210,7 +211,7 @@ static int parse_args(int argc, char **argv, struct ip6_tnl_parm *p)
argc--; argv++; argc--; argv++;
} }
if (medium[0]) { if (medium[0]) {
p->link = tnl_ioctl_get_ifindex(medium); p->link = ll_name_to_index(medium);
if (p->link == 0) if (p->link == 0)
return -1; return -1;
} }
...@@ -266,7 +267,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) ...@@ -266,7 +267,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
char name[IFNAMSIZ]; char name[IFNAMSIZ];
int type; int index, type;
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
rx_fifo, rx_frame, rx_fifo, rx_frame,
tx_bytes, tx_packets, tx_errs, tx_drops, tx_bytes, tx_packets, tx_errs, tx_drops,
...@@ -288,7 +289,10 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) ...@@ -288,7 +289,10 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
continue; continue;
if (p->name[0] && strcmp(p->name, name)) if (p->name[0] && strcmp(p->name, name))
continue; 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) { if (type == -1) {
fprintf(stderr, "Failed to get type of [%s]\n", name); fprintf(stderr, "Failed to get type of [%s]\n", name);
continue; continue;
...@@ -298,7 +302,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) ...@@ -298,7 +302,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
memset(&p1, 0, sizeof(p1)); memset(&p1, 0, sizeof(p1));
ip6_tnl_parm_init(&p1, 0); ip6_tnl_parm_init(&p1, 0);
strcpy(p1.name, name); strcpy(p1.name, name);
p1.link = tnl_ioctl_get_ifindex(p1.name); p1.link = ll_name_to_index(p1.name);
if (p1.link == 0) if (p1.link == 0)
continue; continue;
if (tnl_get_ioctl(p1.name, &p1)) if (tnl_get_ioctl(p1.name, &p1))
...@@ -329,6 +333,7 @@ static int do_show(int argc, char **argv) ...@@ -329,6 +333,7 @@ static int do_show(int argc, char **argv)
{ {
struct ip6_tnl_parm p; struct ip6_tnl_parm p;
ll_init_map(&rth);
ip6_tnl_parm_init(&p, 0); ip6_tnl_parm_init(&p, 0);
p.proto = 0; /* default to any */ p.proto = 0; /* default to any */
......
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/if.h> #include <net/if.h>
#include <linux/if_arp.h> #include <net/if_arp.h>
#include <linux/ip.h> #include <linux/ip.h>
#include <linux/if_tunnel.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) ...@@ -231,7 +231,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
} }
if (medium[0]) { if (medium[0]) {
p->link = tnl_ioctl_get_ifindex(medium); p->link = if_nametoindex(medium);
if (p->link == 0) if (p->link == 0)
return -1; return -1;
} }
...@@ -342,7 +342,7 @@ static void print_tunnel(struct ip_tunnel_parm *p) ...@@ -342,7 +342,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
} }
if (p->link) { if (p->link) {
char *n = tnl_ioctl_get_ifname(p->link); const char *n = ll_index_to_name(p->link);
if (n) if (n)
printf(" dev %s ", n); printf(" dev %s ", n);
} }
...@@ -402,7 +402,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) ...@@ -402,7 +402,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
rx_fifo, rx_frame, rx_fifo, rx_frame,
tx_bytes, tx_packets, tx_errs, tx_drops, tx_bytes, tx_packets, tx_errs, tx_drops,
tx_fifo, tx_colls, tx_carrier, rx_multi; tx_fifo, tx_colls, tx_carrier, rx_multi;
int type;
struct ip_tunnel_parm p1; struct ip_tunnel_parm p1;
char buf[512]; char buf[512];
...@@ -416,6 +415,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) ...@@ -416,6 +415,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp);
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
int index, type;
char *ptr; char *ptr;
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
if ((ptr = strchr(buf, ':')) == NULL || if ((ptr = strchr(buf, ':')) == NULL ||
...@@ -431,7 +431,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) ...@@ -431,7 +431,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
continue; continue;
if (p->name[0] && strcmp(p->name, name)) if (p->name[0] && strcmp(p->name, name))
continue; 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) { if (type == -1) {
fprintf(stderr, "Failed to get type of [%s]\n", name); fprintf(stderr, "Failed to get type of [%s]\n", name);
continue; continue;
...@@ -467,6 +470,7 @@ static int do_show(int argc, char **argv) ...@@ -467,6 +470,7 @@ static int do_show(int argc, char **argv)
int err; int err;
struct ip_tunnel_parm p; struct ip_tunnel_parm p;
ll_init_map(&rth);
if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
return -1; return -1;
......
...@@ -206,7 +206,7 @@ get_failed: ...@@ -206,7 +206,7 @@ get_failed:
saddr = get_addr32(*argv); saddr = get_addr32(*argv);
} else if (!matches(*argv, "dev")) { } else if (!matches(*argv, "dev")) {
NEXT_ARG(); NEXT_ARG();
link = tnl_ioctl_get_ifindex(*argv); link = if_nametoindex(*argv);
if (link == 0) if (link == 0)
exit(-1); exit(-1);
} else if (!matches(*argv, "ttl") || } else if (!matches(*argv, "ttl") ||
...@@ -298,7 +298,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) ...@@ -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])) { if (tb[IFLA_GRE_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_GRE_LINK])) {
unsigned 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) if (n)
fprintf(f, "dev %s ", n); fprintf(f, "dev %s ", n);
......
...@@ -63,58 +63,6 @@ const char *tnl_strproto(__u8 proto) ...@@ -63,58 +63,6 @@ const char *tnl_strproto(__u8 proto)
return buf; 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) int tnl_get_ioctl(const char *basedev, void *p)
{ {
struct ifreq ifr; struct ifreq ifr;
...@@ -126,7 +74,9 @@ int tnl_get_ioctl(const char *basedev, void *p) ...@@ -126,7 +74,9 @@ int tnl_get_ioctl(const char *basedev, void *p)
fd = socket(preferred_family, SOCK_DGRAM, 0); fd = socket(preferred_family, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGETTUNNEL, &ifr); err = ioctl(fd, SIOCGETTUNNEL, &ifr);
if (err) if (err)
perror("ioctl"); fprintf(stderr, "get tunnel %s failed: %s\n", basedev,
strerror(errno));
close(fd); close(fd);
return err; return err;
} }
...@@ -145,7 +95,8 @@ int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) ...@@ -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); fd = socket(preferred_family, SOCK_DGRAM, 0);
err = ioctl(fd, cmd, &ifr); err = ioctl(fd, cmd, &ifr);
if (err) if (err)
perror("ioctl"); fprintf(stderr, "add tunnel %s failed: %s\n", ifr.ifr_name,
strerror(errno));
close(fd); close(fd);
return err; return err;
} }
...@@ -160,16 +111,19 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p) ...@@ -160,16 +111,19 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p)
strncpy(ifr.ifr_name, name, IFNAMSIZ); strncpy(ifr.ifr_name, name, IFNAMSIZ);
else else
strncpy(ifr.ifr_name, basedev, IFNAMSIZ); strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = p; ifr.ifr_ifru.ifru_data = p;
fd = socket(preferred_family, SOCK_DGRAM, 0); fd = socket(preferred_family, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCDELTUNNEL, &ifr); err = ioctl(fd, SIOCDELTUNNEL, &ifr);
if (err) if (err)
perror("ioctl"); fprintf(stderr, "delete tunnel %s failed: %s\n",
ifr.ifr_name, strerror(errno));
close(fd); close(fd);
return err; 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; struct ifreq ifr;
int fd; int fd;
...@@ -180,7 +134,8 @@ static int tnl_gen_ioctl(int cmd, const char *name, void *p, int skiperr) ...@@ -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); fd = socket(preferred_family, SOCK_DGRAM, 0);
err = ioctl(fd, cmd, &ifr); err = ioctl(fd, cmd, &ifr);
if (err && errno != skiperr) if (err && errno != skiperr)
perror("ioctl"); fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
cmd, strerror(errno));
close(fd); close(fd);
return err; return err;
} }
......
...@@ -25,9 +25,7 @@ ...@@ -25,9 +25,7 @@
#include <linux/types.h> #include <linux/types.h>
const char *tnl_strproto(__u8 proto); 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_get_ioctl(const char *basedev, void *p);
int tnl_add_ioctl(int cmd, const char *basedev, const char *name, 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); int tnl_del_ioctl(const char *basedev, const char *name, void *p);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment