busybox/networking/libiproute/ll_map.c
Denys Vlasenko d5f1b1bbe0 *: add FAST_FUNC to function ptrs where it makes sense
function                                             old     new   delta
evalcommand                                         1195    1209     +14
testcmd                                                -      10     +10
printfcmd                                              -      10     +10
echocmd                                                -      10     +10
func_exec                                            270     276      +6
echo_dg                                              104     109      +5
store_nlmsg                                           85      89      +4
pseudo_exec_argv                                     195     198      +3
dotcmd                                               287     290      +3
machtime_stream                                       29      31      +2
discard_stream                                        24      26      +2
argstr                                              1299    1301      +2
killcmd                                              108     109      +1
evalfor                                              226     227      +1
daytime_stream                                        43      44      +1
run_list                                            2544    2543      -1
lookupvar                                             62      61      -1
ipaddr_modify                                       1310    1309      -1
...
parse_stream                                        2254    2245      -9
evalpipe                                             356     347      -9
collect_if                                           210     197     -13
read_opt                                             869     851     -18
handle_dollar                                        681     658     -23
print_addrinfo                                      1342    1303     -39
iterate_on_dir                                       156      59     -97
print_route                                         1709    1609    -100
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 12/130 up/down: 74/-767)       Total: -693 bytes
   text    data     bss     dec     hex filename
 841748     467    7872  850087   cf8a7 busybox_old
 841061     467    7872  849400   cf5f8 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2009-06-05 12:06:05 +02:00

201 lines
4.2 KiB
C

/* vi: set sw=4 ts=4: */
/*
* ll_map.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*
*/
#include <net/if.h> /* struct ifreq and co. */
#include "libbb.h"
#include "libnetlink.h"
#include "ll_map.h"
struct idxmap {
struct idxmap *next;
int index;
int type;
int alen;
unsigned flags;
unsigned char addr[8];
char name[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 FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
struct nlmsghdr *n,
void *arg UNUSED_PARAM)
{
int h;
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct idxmap *im, **imp;
struct rtattr *tb[IFLA_MAX+1];
if (n->nlmsg_type != RTM_NEWLINK)
return 0;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
return -1;
memset(tb, 0, sizeof(tb));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
if (tb[IFLA_IFNAME] == NULL)
return 0;
h = ifi->ifi_index & 0xF;
for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
if (im->index == ifi->ifi_index)
goto found;
im = xmalloc(sizeof(*im));
im->next = *imp;
im->index = ifi->ifi_index;
*imp = im;
found:
im->type = ifi->ifi_type;
im->flags = ifi->ifi_flags;
if (tb[IFLA_ADDRESS]) {
int alen;
im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
if (alen > (int)sizeof(im->addr))
alen = sizeof(im->addr);
memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
} else {
im->alen = 0;
memset(im->addr, 0, sizeof(im->addr));
}
strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
return 0;
}
const char FAST_FUNC *ll_idx_n2a(int idx, char *buf)
{
struct idxmap *im;
if (idx == 0)
return "*";
im = find_by_index(idx);
if (im)
return im->name;
snprintf(buf, 16, "if%d", idx);
return buf;
}
const char FAST_FUNC *ll_index_to_name(int idx)
{
static char nbuf[16];
return ll_idx_n2a(idx, nbuf);
}
#ifdef UNUSED
int ll_index_to_type(int idx)
{
struct idxmap *im;
if (idx == 0)
return -1;
im = find_by_index(idx);
if (im)
return im->type;
return -1;
}
#endif
unsigned FAST_FUNC ll_index_to_flags(int idx)
{
struct idxmap *im;
if (idx == 0)
return 0;
im = find_by_index(idx);
if (im)
return im->flags;
return 0;
}
int FAST_FUNC xll_name_to_index(const char *name)
{
int ret = 0;
int sock_fd;
/* caching is not warranted - no users which repeatedly call it */
#ifdef UNUSED
static char ncache[16];
static int icache;
struct idxmap *im;
int i;
if (name == NULL)
goto out;
if (icache && strcmp(name, ncache) == 0) {
ret = icache;
goto out;
}
for (i = 0; i < 16; i++) {
for (im = idxmap[i]; im; im = im->next) {
if (strcmp(im->name, name) == 0) {
icache = im->index;
strcpy(ncache, name);
ret = im->index;
goto out;
}
}
}
/* We have not found the interface in our cache, but the kernel
* may still know about it. One reason is that we may be using
* module on-demand loading, which means that the kernel will
* load the module and make the interface exist only when
* 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...
* Jean II */
#endif
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd >= 0) {
struct ifreq ifr;
int tmp;
strncpy_IFNAMSIZ(ifr.ifr_name, name);
ifr.ifr_ifindex = -1;
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
close(sock_fd);
if (tmp >= 0)
/* In theory, we should redump the interface list
* to update our cache, this is left as an exercise
* to the reader... Jean II */
ret = ifr.ifr_ifindex;
}
/* out:*/
if (ret <= 0)
bb_error_msg_and_die("cannot find device \"%s\"", name);
return ret;
}
int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
{
xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
return 0;
}