From: roopa Date: Fri, 9 Nov 2012 22:41:36 +0000 (-0800) Subject: Add hash support to route cache X-Git-Tag: libnl3_2_15~35 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a2207c7beb80050671d209650aaaeba429658e49;p=libnl Add hash support to route cache This patch adds keygen function to route 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 --- diff --git a/lib/route/route_obj.c b/lib/route/route_obj.c index 54df023..0ee9ca0 100644 --- a/lib/route/route_obj.c +++ b/lib/route/route_obj.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -289,6 +290,51 @@ static void route_dump_stats(struct nl_object *obj, struct nl_dump_params *p) } } +static void route_keygen(struct nl_object *obj, uint32_t *hashkey, + uint32_t table_sz) +{ + struct rtnl_route *route = (struct rtnl_route *) obj; + unsigned int rkey_sz; + struct nl_addr *addr = NULL; + struct route_hash_key { + uint8_t rt_family; + uint8_t rt_tos; + uint32_t rt_table; + char rt_addr[0]; + } __attribute__((packed)) *rkey; + char buf[INET6_ADDRSTRLEN+5]; + + if (route->rt_dst) + addr = route->rt_dst; + + rkey_sz = sizeof(*rkey); + if (addr) + rkey_sz += nl_addr_get_len(addr); + rkey = calloc(1, rkey_sz); + if (!rkey) { + NL_DBG(2, "Warning: calloc failed for %d bytes...\n", rkey_sz); + *hashkey = 0; + return; + } + rkey->rt_family = route->rt_family; + rkey->rt_tos = route->rt_tos; + rkey->rt_table = route->rt_table; + if (addr) + memcpy(rkey->rt_addr, nl_addr_get_binary_addr(addr), + nl_addr_get_len(addr)); + + *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz; + + NL_DBG(5, "route %p key (fam %d tos %d table %d addr %s) keysz %d " + "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos, + rkey->rt_table, nl_addr2str(addr, buf, sizeof(buf)), + rkey_sz, *hashkey); + + free(rkey); + + return; +} + static int route_compare(struct nl_object *_a, struct nl_object *_b, uint32_t attrs, int flags) { @@ -1151,6 +1197,7 @@ struct nl_object_ops route_obj_ops = { [NL_DUMP_STATS] = route_dump_stats, }, .oo_compare = route_compare, + .oo_keygen = route_keygen, .oo_attrs2str = route_attrs2str, .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS | ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),