[PATCH 4/6] Add hash support to link cache

roopa at cumulusnetworks.com roopa at cumulusnetworks.com
Mon Nov 5 00:13:04 EST 2012


From: roopa <roopa at cumulusnetworks.com>

This patch adds hash size and a keygen function
for the link cache

Signed-off-by: Shrijeet Mukherjee <shm at cumulusnetworks.com>
Signed-off-by: Nolan Leake <nolan at cumulusnetworks.com>
Signed-off-by: Roopa Prabhu <roopa at cumulusnetworks.com>
Reviewed-by: Wilson Kok <wkok at cumulusnetworks.com>
---
 include/netlink/route/link.h |    2 ++
 lib/route/link.c             |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h
index 8268b13..0d43fe9 100644
--- a/include/netlink/route/link.h
+++ b/include/netlink/route/link.h
@@ -21,6 +21,8 @@
 extern "C" {
 #endif
 
+#define LINK_OBJ_HASH_SIZE 2048
+
 /**
  * @struct rtnl_link link.h "netlink/route/link.h"
  * @brief Link object
diff --git a/lib/route/link.c b/lib/route/link.c
index 3f9d9dc..d6e787c 100644
--- a/lib/route/link.c
+++ b/lib/route/link.c
@@ -23,6 +23,7 @@
 #include <netlink/attr.h>
 #include <netlink/utils.h>
 #include <netlink/object.h>
+#include <netlink/hashtable.h>
 #include <netlink/route/rtnl.h>
 #include <netlink/route/link.h>
 #include <netlink/route/link/api.h>
@@ -799,6 +800,37 @@ static int link_handle_event(struct nl_object *a, struct rtnl_link_event_cb *cb)
 }
 #endif
 
+
+static void link_keygen(struct nl_object *obj, uint32_t *hashkey,
+        uint32_t table_sz)
+{
+	struct rtnl_link *link = (struct rtnl_link *) obj;
+	struct link_hash_key *_key;
+	unsigned int _key_sz;
+	struct link_hash_key {
+		uint32_t	l_index;
+	};
+
+	_key_sz = sizeof(struct link_hash_key);
+
+	_key = calloc(1 , _key_sz);
+	if (!_key) {
+	    NL_DBG(2, "Warning: calloc failed for %d bytes...\n", _key_sz);
+	    *hashkey = 0;
+	}
+
+	_key->l_index = link->l_index;
+
+	NL_DBG(5, "link %p buffer %p, size %d\n",
+	       link, _key, _key_sz);
+
+	*hashkey = nl_hash(_key, _key_sz, 0) % table_sz;
+
+	free(_key);
+
+	return;
+}
+
 static int link_compare(struct nl_object *_a, struct nl_object *_b,
 			uint32_t attrs, int flags)
 {
@@ -2500,6 +2532,7 @@ static struct nl_object_ops link_obj_ops = {
 	    [NL_DUMP_STATS]	= link_dump_stats,
 	},
 	.oo_compare		= link_compare,
+	.oo_keygen		= link_keygen,
 	.oo_attrs2str		= link_attrs2str,
 	.oo_id_attrs		= LINK_ATTR_IFINDEX,
 };
@@ -2525,6 +2558,7 @@ static struct nl_cache_ops rtnl_link_ops = {
 	.co_msg_parser		= link_msg_parser,
 	.co_event_filter	= link_event_filter,
 	.co_obj_ops		= &link_obj_ops,
+	.co_hash_size		= LINK_OBJ_HASH_SIZE
 };
 
 static void __init link_init(void)
-- 
1.7.2.5




More information about the libnl mailing list