From 6f24cf12caae88e1a09f0516e00188966a232290 Mon Sep 17 00:00:00 2001 From: roopa Date: Fri, 9 Nov 2012 14:41:35 -0800 Subject: Add hash support to neigh cache This patch adds keygen function to the neigh object Signed-off-by: Shrijeet Mukherjee Signed-off-by: Nolan Leake Signed-off-by: Roopa Prabhu Reviewed-by: Wilson Kok Signed-off-by: Thomas Graf --- lib/route/neigh.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/route/neigh.c b/lib/route/neigh.c index bb61571..1986ef5 100644 --- a/lib/route/neigh.c +++ b/lib/route/neigh.c @@ -151,6 +151,7 @@ #include #include #include +#include #include #include #include @@ -197,6 +198,50 @@ static int neigh_clone(struct nl_object *_dst, struct nl_object *_src) return 0; } +static void neigh_keygen(struct nl_object *obj, uint32_t *hashkey, + uint32_t table_sz) +{ + struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj; + unsigned int nkey_sz; + struct nl_addr *addr = NULL; + struct neigh_hash_key { + uint32_t n_family; + uint32_t n_ifindex; + char n_addr[0]; + } __attribute__((packed)) *nkey; + char buf[INET6_ADDRSTRLEN+5]; + + if (neigh->n_dst) + addr = neigh->n_dst; + + nkey_sz = sizeof(*nkey); + if (addr) + nkey_sz += nl_addr_get_len(addr); + + nkey = calloc(1, nkey_sz); + if (!nkey) { + *hashkey = 0; + return; + } + nkey->n_family = neigh->n_family; + nkey->n_ifindex = neigh->n_ifindex; + if (addr) + memcpy(nkey->n_addr, + nl_addr_get_binary_addr(addr), + nl_addr_get_len(addr)); + + *hashkey = nl_hash(nkey, nkey_sz, 0) % table_sz; + + NL_DBG(5, "neigh %p key (fam %d dev %d addr %s) keysz %d hash 0x%x\n", + neigh, nkey->n_family, nkey->n_ifindex, + nl_addr2str(addr, buf, sizeof(buf)), + nkey_sz, *hashkey); + + free(nkey); + + return; +} + static int neigh_compare(struct nl_object *_a, struct nl_object *_b, uint32_t attrs, int flags) { @@ -825,6 +870,7 @@ static struct nl_object_ops neigh_obj_ops = { [NL_DUMP_STATS] = neigh_dump_stats, }, .oo_compare = neigh_compare, + .oo_keygen = neigh_keygen, .oo_attrs2str = neigh_attrs2str, .oo_id_attrs = (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY), }; -- cgit v1.2.1