ll_map: disable interface name caching code

function                                             old     new   delta
find_by_index                                          -      26     +26
static.icache                                          4       -      -4
ll_idx_n2a                                            71      59     -12
static.ncache                                         16       -     -16
ll_index_to_flags                                     40      24     -16
xll_name_to_index                                    206     104    -102
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 0/3 up/down: 26/-150)          Total: -124 bytes

   text    data     bss     dec     hex filename
 734703    3040   14440  752183   b7a37 busybox_old
 734599    3040   14416  752055   b79b7 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-06-19 12:11:20 +00:00
parent 2d25491ed0
commit 08a61180ce
2 changed files with 51 additions and 40 deletions

View File

@ -11,9 +11,7 @@
* *
*/ */
//#include <sys/socket.h> /* socket() */
#include <net/if.h> /* struct ifreq and co. */ #include <net/if.h> /* struct ifreq and co. */
//#include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */
#include "libbb.h" #include "libbb.h"
#include "libnetlink.h" #include "libnetlink.h"
@ -31,6 +29,16 @@ struct idxmap {
static struct idxmap *idxmap[16]; static struct idxmap *idxmap[16];
static struct idxmap *find_by_index(int idx)
{
struct idxmap *im;
for (im = idxmap[idx & 0xF]; im; im = im->next)
if (im->index == idx)
return im;
return NULL;
}
int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{ {
int h; int h;
@ -44,7 +52,6 @@ int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi))) if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
return -1; return -1;
memset(tb, 0, sizeof(tb)); memset(tb, 0, sizeof(tb));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
if (tb[IFLA_IFNAME] == NULL) if (tb[IFLA_IFNAME] == NULL)
@ -54,15 +61,13 @@ int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next) for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
if (im->index == ifi->ifi_index) if (im->index == ifi->ifi_index)
break; goto found;
if (im == NULL) {
im = xmalloc(sizeof(*im)); im = xmalloc(sizeof(*im));
im->next = *imp; im->next = *imp;
im->index = ifi->ifi_index; im->index = ifi->ifi_index;
*imp = im; *imp = im;
} found:
im->type = ifi->ifi_type; im->type = ifi->ifi_type;
im->flags = ifi->ifi_flags; im->flags = ifi->ifi_flags;
if (tb[IFLA_ADDRESS]) { if (tb[IFLA_ADDRESS]) {
@ -85,8 +90,8 @@ const char *ll_idx_n2a(int idx, char *buf)
if (idx == 0) if (idx == 0)
return "*"; return "*";
for (im = idxmap[idx & 0xF]; im; im = im->next) im = find_by_index(idx);
if (im->index == idx) if (im)
return im->name; return im->name;
snprintf(buf, 16, "if%d", idx); snprintf(buf, 16, "if%d", idx);
return buf; return buf;
@ -100,17 +105,19 @@ const char *ll_index_to_name(int idx)
return ll_idx_n2a(idx, nbuf); return ll_idx_n2a(idx, nbuf);
} }
#ifdef UNUSED
int ll_index_to_type(int idx) int ll_index_to_type(int idx)
{ {
struct idxmap *im; struct idxmap *im;
if (idx == 0) if (idx == 0)
return -1; return -1;
for (im = idxmap[idx & 0xF]; im; im = im->next) im = find_by_index(idx);
if (im->index == idx) if (im)
return im->type; return im->type;
return -1; return -1;
} }
#endif
unsigned ll_index_to_flags(int idx) unsigned ll_index_to_flags(int idx)
{ {
@ -118,23 +125,24 @@ unsigned ll_index_to_flags(int idx)
if (idx == 0) if (idx == 0)
return 0; return 0;
im = find_by_index(idx);
for (im = idxmap[idx & 0xF]; im; im = im->next) if (im)
if (im->index == idx)
return im->flags; return im->flags;
return 0; return 0;
} }
// TODO: caching is not warranted - no users which repeatedly call it
int xll_name_to_index(const char * const name) int xll_name_to_index(const char * const name)
{ {
int ret = 0;
int sock_fd;
/* caching is not warranted - no users which repeatedly call it */
#ifdef UNUSED
static char ncache[16]; static char ncache[16];
static int icache; static int icache;
struct idxmap *im; struct idxmap *im;
int sock_fd;
int i; int i;
int ret = 0;
if (name == NULL) if (name == NULL)
goto out; goto out;
@ -159,10 +167,13 @@ int xll_name_to_index(const char * const name)
* we explicitely request it (check for dev_load() in net/core/dev.c). * we explicitely request it (check for dev_load() in net/core/dev.c).
* I can think of other similar scenario, but they are less common... * I can think of other similar scenario, but they are less common...
* Jean II */ * Jean II */
#endif
sock_fd = socket(AF_INET, SOCK_DGRAM, 0); sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd) { if (sock_fd) {
struct ifreq ifr; struct ifreq ifr;
int tmp; int tmp;
strncpy(ifr.ifr_name, name, IFNAMSIZ); strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_ifindex = -1; ifr.ifr_ifindex = -1;
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr); tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
@ -173,7 +184,7 @@ int xll_name_to_index(const char * const name)
* to the reader... Jean II */ * to the reader... Jean II */
ret = ifr.ifr_ifindex; ret = ifr.ifr_ifindex;
} }
out: /* out:*/
if (ret <= 0) if (ret <= 0)
bb_error_msg_and_die("cannot find device \"%s\"", name); bb_error_msg_and_die("cannot find device \"%s\"", name);
return ret; return ret;

View File

@ -2,12 +2,12 @@
#ifndef __LL_MAP_H__ #ifndef __LL_MAP_H__
#define __LL_MAP_H__ 1 #define __LL_MAP_H__ 1
extern int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg); int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);
extern int ll_init_map(struct rtnl_handle *rth); int ll_init_map(struct rtnl_handle *rth);
extern int xll_name_to_index(const char * const name); int xll_name_to_index(const char * const name);
extern const char *ll_index_to_name(int idx); const char *ll_index_to_name(int idx);
extern const char *ll_idx_n2a(int idx, char *buf); const char *ll_idx_n2a(int idx, char *buf);
extern int ll_index_to_type(int idx); /* int ll_index_to_type(int idx); */
extern unsigned ll_index_to_flags(int idx); unsigned ll_index_to_flags(int idx);
#endif /* __LL_MAP_H__ */ #endif /* __LL_MAP_H__ */