ipv4: fib table algorithm performance improvement

The FIB algorithim for IPV4 is set at compile time, but kernel goes through
the overhead of function call indirection at runtime. Save some
cycles by turning the indirect calls to direct calls to either
hash or trie code.

Signed-off-by: Stephen Hemminger <shemminger@vyatta.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Stephen Hemminger 2009-09-20 10:35:36 +00:00 committed by David S. Miller
parent 977750076d
commit 16c6cf8bb4
5 changed files with 48 additions and 56 deletions

View File

@ -144,18 +144,21 @@ struct fib_table {
struct hlist_node tb_hlist; struct hlist_node tb_hlist;
u32 tb_id; u32 tb_id;
int tb_default; int tb_default;
int (*tb_lookup)(struct fib_table *tb, const struct flowi *flp, struct fib_result *res);
int (*tb_insert)(struct fib_table *, struct fib_config *);
int (*tb_delete)(struct fib_table *, struct fib_config *);
int (*tb_dump)(struct fib_table *table, struct sk_buff *skb,
struct netlink_callback *cb);
int (*tb_flush)(struct fib_table *table);
void (*tb_select_default)(struct fib_table *table,
const struct flowi *flp, struct fib_result *res);
unsigned char tb_data[0]; unsigned char tb_data[0];
}; };
extern int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
struct fib_result *res);
extern int fib_table_insert(struct fib_table *, struct fib_config *);
extern int fib_table_delete(struct fib_table *, struct fib_config *);
extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
struct netlink_callback *cb);
extern int fib_table_flush(struct fib_table *table);
extern void fib_table_select_default(struct fib_table *table,
const struct flowi *flp,
struct fib_result *res);
#ifndef CONFIG_IP_MULTIPLE_TABLES #ifndef CONFIG_IP_MULTIPLE_TABLES
#define TABLE_LOCAL_INDEX 0 #define TABLE_LOCAL_INDEX 0
@ -182,11 +185,11 @@ static inline int fib_lookup(struct net *net, const struct flowi *flp,
struct fib_table *table; struct fib_table *table;
table = fib_get_table(net, RT_TABLE_LOCAL); table = fib_get_table(net, RT_TABLE_LOCAL);
if (!table->tb_lookup(table, flp, res)) if (!fib_table_lookup(table, flp, res))
return 0; return 0;
table = fib_get_table(net, RT_TABLE_MAIN); table = fib_get_table(net, RT_TABLE_MAIN);
if (!table->tb_lookup(table, flp, res)) if (!fib_table_lookup(table, flp, res))
return 0; return 0;
return -ENETUNREACH; return -ENETUNREACH;
} }

View File

@ -125,7 +125,7 @@ void fib_select_default(struct net *net,
#endif #endif
tb = fib_get_table(net, table); tb = fib_get_table(net, table);
if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
tb->tb_select_default(tb, flp, res); fib_table_select_default(tb, flp, res);
} }
static void fib_flush(struct net *net) static void fib_flush(struct net *net)
@ -139,7 +139,7 @@ static void fib_flush(struct net *net)
for (h = 0; h < FIB_TABLE_HASHSZ; h++) { for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
head = &net->ipv4.fib_table_hash[h]; head = &net->ipv4.fib_table_hash[h];
hlist_for_each_entry(tb, node, head, tb_hlist) hlist_for_each_entry(tb, node, head, tb_hlist)
flushed += tb->tb_flush(tb); flushed += fib_table_flush(tb);
} }
if (flushed) if (flushed)
@ -162,7 +162,7 @@ struct net_device * ip_dev_find(struct net *net, __be32 addr)
#endif #endif
local_table = fib_get_table(net, RT_TABLE_LOCAL); local_table = fib_get_table(net, RT_TABLE_LOCAL);
if (!local_table || local_table->tb_lookup(local_table, &fl, &res)) if (!local_table || fib_table_lookup(local_table, &fl, &res))
return NULL; return NULL;
if (res.type != RTN_LOCAL) if (res.type != RTN_LOCAL)
goto out; goto out;
@ -200,7 +200,7 @@ static inline unsigned __inet_dev_addr_type(struct net *net,
local_table = fib_get_table(net, RT_TABLE_LOCAL); local_table = fib_get_table(net, RT_TABLE_LOCAL);
if (local_table) { if (local_table) {
ret = RTN_UNICAST; ret = RTN_UNICAST;
if (!local_table->tb_lookup(local_table, &fl, &res)) { if (!fib_table_lookup(local_table, &fl, &res)) {
if (!dev || dev == res.fi->fib_dev) if (!dev || dev == res.fi->fib_dev)
ret = res.type; ret = res.type;
fib_res_put(&res); fib_res_put(&res);
@ -473,13 +473,13 @@ int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
if (cmd == SIOCDELRT) { if (cmd == SIOCDELRT) {
tb = fib_get_table(net, cfg.fc_table); tb = fib_get_table(net, cfg.fc_table);
if (tb) if (tb)
err = tb->tb_delete(tb, &cfg); err = fib_table_delete(tb, &cfg);
else else
err = -ESRCH; err = -ESRCH;
} else { } else {
tb = fib_new_table(net, cfg.fc_table); tb = fib_new_table(net, cfg.fc_table);
if (tb) if (tb)
err = tb->tb_insert(tb, &cfg); err = fib_table_insert(tb, &cfg);
else else
err = -ENOBUFS; err = -ENOBUFS;
} }
@ -594,7 +594,7 @@ static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *ar
goto errout; goto errout;
} }
err = tb->tb_delete(tb, &cfg); err = fib_table_delete(tb, &cfg);
errout: errout:
return err; return err;
} }
@ -616,7 +616,7 @@ static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *ar
goto errout; goto errout;
} }
err = tb->tb_insert(tb, &cfg); err = fib_table_insert(tb, &cfg);
errout: errout:
return err; return err;
} }
@ -647,7 +647,7 @@ static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
if (dumped) if (dumped)
memset(&cb->args[2], 0, sizeof(cb->args) - memset(&cb->args[2], 0, sizeof(cb->args) -
2 * sizeof(cb->args[0])); 2 * sizeof(cb->args[0]));
if (tb->tb_dump(tb, skb, cb) < 0) if (fib_table_dump(tb, skb, cb) < 0)
goto out; goto out;
dumped = 1; dumped = 1;
next: next:
@ -701,9 +701,9 @@ static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifad
cfg.fc_scope = RT_SCOPE_HOST; cfg.fc_scope = RT_SCOPE_HOST;
if (cmd == RTM_NEWROUTE) if (cmd == RTM_NEWROUTE)
tb->tb_insert(tb, &cfg); fib_table_insert(tb, &cfg);
else else
tb->tb_delete(tb, &cfg); fib_table_delete(tb, &cfg);
} }
void fib_add_ifaddr(struct in_ifaddr *ifa) void fib_add_ifaddr(struct in_ifaddr *ifa)
@ -832,7 +832,7 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
local_bh_disable(); local_bh_disable();
frn->tb_id = tb->tb_id; frn->tb_id = tb->tb_id;
frn->err = tb->tb_lookup(tb, &fl, &res); frn->err = fib_table_lookup(tb, &fl, &res);
if (!frn->err) { if (!frn->err) {
frn->prefixlen = res.prefixlen; frn->prefixlen = res.prefixlen;
@ -1009,7 +1009,7 @@ static void __net_exit ip_fib_net_exit(struct net *net)
head = &net->ipv4.fib_table_hash[i]; head = &net->ipv4.fib_table_hash[i];
hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) { hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
hlist_del(node); hlist_del(node);
tb->tb_flush(tb); fib_table_flush(tb);
kfree(tb); kfree(tb);
} }
} }

View File

@ -242,8 +242,8 @@ fn_new_zone(struct fn_hash *table, int z)
return fz; return fz;
} }
static int int fib_table_lookup(struct fib_table *tb,
fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res) const struct flowi *flp, struct fib_result *res)
{ {
int err; int err;
struct fn_zone *fz; struct fn_zone *fz;
@ -274,8 +274,8 @@ out:
return err; return err;
} }
static void void fib_table_select_default(struct fib_table *tb,
fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res) const struct flowi *flp, struct fib_result *res)
{ {
int order, last_idx; int order, last_idx;
struct hlist_node *node; struct hlist_node *node;
@ -366,7 +366,7 @@ static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
return NULL; return NULL;
} }
static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg) int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
{ {
struct fn_hash *table = (struct fn_hash *) tb->tb_data; struct fn_hash *table = (struct fn_hash *) tb->tb_data;
struct fib_node *new_f = NULL; struct fib_node *new_f = NULL;
@ -544,8 +544,7 @@ out:
return err; return err;
} }
int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
{ {
struct fn_hash *table = (struct fn_hash *)tb->tb_data; struct fn_hash *table = (struct fn_hash *)tb->tb_data;
struct fib_node *f; struct fib_node *f;
@ -662,7 +661,7 @@ static int fn_flush_list(struct fn_zone *fz, int idx)
return found; return found;
} }
static int fn_hash_flush(struct fib_table *tb) int fib_table_flush(struct fib_table *tb)
{ {
struct fn_hash *table = (struct fn_hash *) tb->tb_data; struct fn_hash *table = (struct fn_hash *) tb->tb_data;
struct fn_zone *fz; struct fn_zone *fz;
@ -743,7 +742,8 @@ fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
return skb->len; return skb->len;
} }
static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb) int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
struct netlink_callback *cb)
{ {
int m, s_m; int m, s_m;
struct fn_zone *fz; struct fn_zone *fz;
@ -787,12 +787,7 @@ struct fib_table *fib_hash_table(u32 id)
tb->tb_id = id; tb->tb_id = id;
tb->tb_default = -1; tb->tb_default = -1;
tb->tb_lookup = fn_hash_lookup;
tb->tb_insert = fn_hash_insert;
tb->tb_delete = fn_hash_delete;
tb->tb_flush = fn_hash_flush;
tb->tb_select_default = fn_hash_select_default;
tb->tb_dump = fn_hash_dump;
memset(tb->tb_data, 0, sizeof(struct fn_hash)); memset(tb->tb_data, 0, sizeof(struct fn_hash));
return tb; return tb;
} }

View File

@ -94,7 +94,7 @@ static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
if ((tbl = fib_get_table(rule->fr_net, rule->table)) == NULL) if ((tbl = fib_get_table(rule->fr_net, rule->table)) == NULL)
goto errout; goto errout;
err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); err = fib_table_lookup(tbl, flp, (struct fib_result *) arg->result);
if (err > 0) if (err > 0)
err = -EAGAIN; err = -EAGAIN;
errout: errout:

View File

@ -1174,7 +1174,7 @@ done:
/* /*
* Caller must hold RTNL. * Caller must hold RTNL.
*/ */
static int fn_trie_insert(struct fib_table *tb, struct fib_config *cfg) int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
struct fib_alias *fa, *new_fa; struct fib_alias *fa, *new_fa;
@ -1373,8 +1373,8 @@ static int check_leaf(struct trie *t, struct leaf *l,
return 1; return 1;
} }
static int fn_trie_lookup(struct fib_table *tb, const struct flowi *flp, int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
struct fib_result *res) struct fib_result *res)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
int ret; int ret;
@ -1595,7 +1595,7 @@ static void trie_leaf_remove(struct trie *t, struct leaf *l)
/* /*
* Caller must hold RTNL. * Caller must hold RTNL.
*/ */
static int fn_trie_delete(struct fib_table *tb, struct fib_config *cfg) int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
u32 key, mask; u32 key, mask;
@ -1786,7 +1786,7 @@ static struct leaf *trie_leafindex(struct trie *t, int index)
/* /*
* Caller must hold RTNL. * Caller must hold RTNL.
*/ */
static int fn_trie_flush(struct fib_table *tb) int fib_table_flush(struct fib_table *tb)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
struct leaf *l, *ll = NULL; struct leaf *l, *ll = NULL;
@ -1807,9 +1807,9 @@ static int fn_trie_flush(struct fib_table *tb)
return found; return found;
} }
static void fn_trie_select_default(struct fib_table *tb, void fib_table_select_default(struct fib_table *tb,
const struct flowi *flp, const struct flowi *flp,
struct fib_result *res) struct fib_result *res)
{ {
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
int order, last_idx; int order, last_idx;
@ -1952,8 +1952,8 @@ static int fn_trie_dump_leaf(struct leaf *l, struct fib_table *tb,
return skb->len; return skb->len;
} }
static int fn_trie_dump(struct fib_table *tb, struct sk_buff *skb, int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
struct netlink_callback *cb) struct netlink_callback *cb)
{ {
struct leaf *l; struct leaf *l;
struct trie *t = (struct trie *) tb->tb_data; struct trie *t = (struct trie *) tb->tb_data;
@ -2020,12 +2020,6 @@ struct fib_table *fib_hash_table(u32 id)
tb->tb_id = id; tb->tb_id = id;
tb->tb_default = -1; tb->tb_default = -1;
tb->tb_lookup = fn_trie_lookup;
tb->tb_insert = fn_trie_insert;
tb->tb_delete = fn_trie_delete;
tb->tb_flush = fn_trie_flush;
tb->tb_select_default = fn_trie_select_default;
tb->tb_dump = fn_trie_dump;
t = (struct trie *) tb->tb_data; t = (struct trie *) tb->tb_data;
memset(t, 0, sizeof(*t)); memset(t, 0, sizeof(*t));