[PATCH 0/6] Add hash support to libnl caches

Roopa Prabhu roopa at cumulusnetworks.com
Fri Nov 2 19:14:30 EDT 2012


(patch files attached. Sorry about sending the patches as attachments. 
Patch emails from me are not reaching the list.)


 From ea9fdea2e612e4bc69484fa2484c94864423bbd6 Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 15:46:47 -0700
Subject: [PATCH 0/6] Add hash support to libnl caches
To: roopa at cumulusnetworks.com

This patch series adds hash lookup functionality for libnl caches.
In our tests we have found that the current linear search for libnl
cache objects does not scale well.

The current list representation continues to exist.
Hash support requires each object type to define a keygen operation
to generate the hash key. The hash is allocated at cache creation time.
If the cache object type does not support the keygen op, hash table is
not created for that cache.

This series adds keygen functions to link, neigh and route objects.

Note: Could not really verify if the documentation built correctly.
Can fix if there are problems.

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>

roopa (6):
   Add hash function
   Add nl hashtable structures and access functions
   Add hash support in cache mngr
   Add hash support to link cache
   Add hash support to neigh cache
   Add hash support to route cache

  include/netlink-types.h      |    2 +
  include/netlink/cache-api.h  |    3 +
  include/netlink/cache.h      |    2 +
  include/netlink/hash.h       |   69 ++++++
  include/netlink/hashtable.h  |   53 +++++
  include/netlink/object-api.h |    9 +
  include/netlink/object.h     |    2 +
  include/netlink/route/link.h |    2 +
  lib/Makefile.am              |    2 +-
  lib/cache.c                  |   86 +++++++-
  lib/hash.c                   |  482 
++++++++++++++++++++++++++++++++++++++++++
  lib/hashtable.c              |  141 ++++++++++++
  lib/object.c                 |   21 ++
  lib/route/link.c             |   34 +++
  lib/route/neigh.c            |   50 +++++
  lib/route/route_obj.c        |   55 +++++
  16 files changed, 1009 insertions(+), 4 deletions(-)
  create mode 100644 include/netlink/hash.h
  create mode 100644 include/netlink/hashtable.h
  create mode 100644 lib/hash.c
  create mode 100644 lib/hashtable.c

--
1.7.2.5
-------------- next part --------------
>From ea9fdea2e612e4bc69484fa2484c94864423bbd6 Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 15:46:47 -0700
Subject: [PATCH 0/6] Add hash support to libnl caches
To: roopa at cumulusnetworks.com

This patch series adds hash lookup functionality for libnl caches.
In our tests we have found that the current linear search for libnl
cache objects does not scale well.

The current list representation continues to exist.
Hash support requires each object type to define a keygen operation
to generate the hash key. The hash is allocated at cache creation time.
If the cache object type does not support the keygen op, hash table is
not created for that cache.

This series adds keygen functions to link, neigh and route objects.

Note: Could not really verify if the documentation built correctly.
Can fix if there are problems.

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>

roopa (6):
  Add hash function
  Add nl hashtable structures and access functions
  Add hash support in cache mngr
  Add hash support to link cache
  Add hash support to neigh cache
  Add hash support to route cache

 include/netlink-types.h      |    2 +
 include/netlink/cache-api.h  |    3 +
 include/netlink/cache.h      |    2 +
 include/netlink/hash.h       |   69 ++++++
 include/netlink/hashtable.h  |   53 +++++
 include/netlink/object-api.h |    9 +
 include/netlink/object.h     |    2 +
 include/netlink/route/link.h |    2 +
 lib/Makefile.am              |    2 +-
 lib/cache.c                  |   86 +++++++-
 lib/hash.c                   |  482 ++++++++++++++++++++++++++++++++++++++++++
 lib/hashtable.c              |  141 ++++++++++++
 lib/object.c                 |   21 ++
 lib/route/link.c             |   34 +++
 lib/route/neigh.c            |   50 +++++
 lib/route/route_obj.c        |   55 +++++
 16 files changed, 1009 insertions(+), 4 deletions(-)
 create mode 100644 include/netlink/hash.h
 create mode 100644 include/netlink/hashtable.h
 create mode 100644 lib/hash.c
 create mode 100644 lib/hashtable.c

-- 
1.7.2.5

-------------- next part --------------
>From d958cbf2e7692d34ac40b44a65c20b5ae425398f Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:47 -0700
Subject: [PATCH 1/6] Add hash function
To: roopa at cumulusnetworks.com

This patch adds a hash function for hashing libnl objects.

This hash function is from:
http://ccodearchive.net/info/hash.html

The original code was modified to remove unwanted dependencies,
unwanted code and fixes to header file locations

This hash function could be replaced with any other hash function
if preferred. If we choose to keep this hash function, we can resubmit
with some code cleanups.

One requirement with this hash function is, hashing over multiple fields of an
un-packed struct requires that the struct be zeroed, otherwise random padding
bytes will change the hash.

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/hash.h |   69 +++++++
 lib/Makefile.am        |    2 +-
 lib/hash.c             |  482 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 552 insertions(+), 1 deletions(-)
 create mode 100644 include/netlink/hash.h
 create mode 100644 lib/hash.c

diff --git a/include/netlink/hash.h b/include/netlink/hash.h
new file mode 100644
index 0000000..8ca1f5b
--- /dev/null
+++ b/include/netlink/hash.h
@@ -0,0 +1,69 @@
+/*
+ * This file was taken from http://ccodearchive.net/info/hash.html
+ * Changes to the original file include cleanups and removal of unwanted code
+ * and also code that depended on build_asert
+ */
+#ifndef CCAN_HASH_H
+#define CCAN_HASH_H
+#include <stdint.h>
+#include <stdlib.h>
+#include <endian.h>
+
+/* Stolen mostly from: lookup3.c, by Bob Jenkins, May 2006, Public Domain.
+ *
+ * http://burtleburtle.net/bob/c/lookup3.c
+ */
+
+#ifdef __LITTLE_ENDIAN
+#   define HAVE_LITTLE_ENDIAN 1
+#elif __BIG_ENDIAN
+#   define HAVE_BIG_ENDIAN 1
+#else
+#error Unknown endianness.  Failure in endian.h
+#endif
+
+/**
+ * hash - fast hash of an array for internal use
+ * @p: the array or pointer to first element
+ * @num: the number of elements to hash
+ * @base: the base number to roll into the hash (usually 0)
+ *
+ * The memory region pointed to by p is combined with the base to form
+ * a 32-bit hash.
+ *
+ * This hash will have different results on different machines, so is
+ * only useful for internal hashes (ie. not hashes sent across the
+ * network or saved to disk).
+ *
+ * It may also change with future versions: it could even detect at runtime
+ * what the fastest hash to use is.
+ *
+ * See also: hash64, hash_stable.
+ *
+ * Example:
+ *	#include <ccan/hash/hash.h>
+ *	#include <err.h>
+ *	#include <stdio.h>
+ *	#include <string.h>
+ *
+ *	// Simple demonstration: idential strings will have the same hash, but
+ *	// two different strings will probably not.
+ *	int main(int argc, char *argv[])
+ *	{
+ *		uint32_t hash1, hash2;
+ *
+ *		if (argc != 3)
+ *			err(1, "Usage: %s <string1> <string2>", argv[0]);
+ *
+ *		hash1 = hash(argv[1], strlen(argv[1]), 0);
+ *		hash2 = hash(argv[2], strlen(argv[2]), 0);
+ *		printf("Hash is %s\n", hash1 == hash2 ? "same" : "different");
+ *		return 0;
+ *	}
+ */
+#define hash(p, num, base) hash_any((p), (num)*sizeof(*(p)), (base))
+
+/* Our underlying operations. */
+uint32_t hash_any(const void *key, size_t length, uint32_t base);
+
+#endif /* HASH_H */
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9e5efd8..4d8d58d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -18,7 +18,7 @@ lib_LTLIBRARIES = \
 libnl_3_la_SOURCES = \
 	addr.c attr.c cache.c cache_mngr.c cache_mngt.c data.c \
 	error.c handlers.c msg.c nl.c object.c socket.c utils.c \
-	version.c
+	version.c hash.c
 
 libnl_genl_3_la_LIBADD  = libnl-3.la
 libnl_genl_3_la_SOURCES = \
diff --git a/lib/hash.c b/lib/hash.c
new file mode 100644
index 0000000..6fdf2b8
--- /dev/null
+++ b/lib/hash.c
@@ -0,0 +1,482 @@
+/*
+ * This code was taken from http://ccodearchive.net/info/hash.html
+ * The original file was modified to remove unwanted code
+ * and some changes to fit the current build environment
+ */
+/*
+-------------------------------------------------------------------------------
+lookup3.c, by Bob Jenkins, May 2006, Public Domain.
+
+These are functions for producing 32-bit hashes for hash table lookup.
+hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
+are externally useful functions.  Routines to test the hash are included
+if SELF_TEST is defined.  You can use this free for any purpose.  It's in
+the public domain.  It has no warranty.
+
+You probably want to use hashlittle().  hashlittle() and hashbig()
+hash byte arrays.  hashlittle() is is faster than hashbig() on
+little-endian machines.  Intel and AMD are little-endian machines.
+On second thought, you probably want hashlittle2(), which is identical to
+hashlittle() except it returns two 32-bit hashes for the price of one.
+You could implement hashbig2() if you wanted but I haven't bothered here.
+
+If you want to find a hash of, say, exactly 7 integers, do
+  a = i1;  b = i2;  c = i3;
+  mix(a,b,c);
+  a += i4; b += i5; c += i6;
+  mix(a,b,c);
+  a += i7;
+  final(a,b,c);
+then use c as the hash value.  If you have a variable length array of
+4-byte integers to hash, use hash_word().  If you have a byte array (like
+a character string), use hashlittle().  If you have several byte arrays, or
+a mix of things, see the comments above hashlittle().
+
+Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
+then mix those integers.  This is fast (you can do a lot more thorough
+mixing with 12*3 instructions on 3 integers than you can with 3 instructions
+on 1 byte), but shoehorning those bytes into integers efficiently is messy.
+-------------------------------------------------------------------------------
+*/
+#include <netlink/hash.h>
+
+#if HAVE_LITTLE_ENDIAN
+#define HASH_LITTLE_ENDIAN 1
+#define HASH_BIG_ENDIAN 0
+#elif HAVE_BIG_ENDIAN
+#define HASH_LITTLE_ENDIAN 0
+#define HASH_BIG_ENDIAN 1
+#else
+#error Unknown endian
+#endif
+
+#define hashsize(n) ((uint32_t)1<<(n))
+#define hashmask(n) (hashsize(n)-1)
+#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
+
+/*
+-------------------------------------------------------------------------------
+mix -- mix 3 32-bit values reversibly.
+
+This is reversible, so any information in (a,b,c) before mix() is
+still in (a,b,c) after mix().
+
+If four pairs of (a,b,c) inputs are run through mix(), or through
+mix() in reverse, there are at least 32 bits of the output that
+are sometimes the same for one pair and different for another pair.
+This was tested for:
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or
+  all zero plus a counter that starts at zero.
+
+Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
+satisfy this are
+    4  6  8 16 19  4
+    9 15  3 18 27 15
+   14  9  3  7 17  3
+Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
+for "differ" defined as + with a one-bit base and a two-bit delta.  I
+used http://burtleburtle.net/bob/hash/avalanche.html to choose
+the operations, constants, and arrangements of the variables.
+
+This does not achieve avalanche.  There are input bits of (a,b,c)
+that fail to affect some output bits of (a,b,c), especially of a.  The
+most thoroughly mixed value is c, but it doesn't really even achieve
+avalanche in c.
+
+This allows some parallelism.  Read-after-writes are good at doubling
+the number of bits affected, so the goal of mixing pulls in the opposite
+direction as the goal of parallelism.  I did what I could.  Rotates
+seem to cost as much as shifts on every machine I could lay my hands
+on, and rotates are much kinder to the top and bottom bits, so I used
+rotates.
+-------------------------------------------------------------------------------
+*/
+#define mix(a,b,c) \
+{ \
+  a -= c;  a ^= rot(c, 4);  c += b; \
+  b -= a;  b ^= rot(a, 6);  a += c; \
+  c -= b;  c ^= rot(b, 8);  b += a; \
+  a -= c;  a ^= rot(c,16);  c += b; \
+  b -= a;  b ^= rot(a,19);  a += c; \
+  c -= b;  c ^= rot(b, 4);  b += a; \
+}
+
+/*
+-------------------------------------------------------------------------------
+final -- final mixing of 3 32-bit values (a,b,c) into c
+
+Pairs of (a,b,c) values differing in only a few bits will usually
+produce values of c that look totally different.  This was tested for
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or
+  all zero plus a counter that starts at zero.
+
+These constants passed:
+ 14 11 25 16 4 14 24
+ 12 14 25 16 4 14 24
+and these came close:
+  4  8 15 26 3 22 24
+ 10  8 15 26 3 22 24
+ 11  8 15 26 3 22 24
+-------------------------------------------------------------------------------
+*/
+#define final(a,b,c) \
+{ \
+  c ^= b; c -= rot(b,14); \
+  a ^= c; a -= rot(c,11); \
+  b ^= a; b -= rot(a,25); \
+  c ^= b; c -= rot(b,16); \
+  a ^= c; a -= rot(c,4);  \
+  b ^= a; b -= rot(a,14); \
+  c ^= b; c -= rot(b,24); \
+}
+
+/*
+-------------------------------------------------------------------------------
+hashlittle() -- hash a variable-length key into a 32-bit value
+  k       : the key (the unaligned variable-length array of bytes)
+  length  : the length of the key, counting by bytes
+  val2    : IN: can be any 4-byte value OUT: second 32 bit hash.
+Returns a 32-bit value.  Every bit of the key affects every bit of
+the return value.  Two keys differing by one or two bits will have
+totally different hash values.  Note that the return value is better
+mixed than val2, so use that first.
+
+The best hash table sizes are powers of 2.  There is no need to do
+mod a prime (mod is sooo slow!).  If you need less than 32 bits,
+use a bitmask.  For example, if you need only 10 bits, do
+  h = (h & hashmask(10));
+In which case, the hash table should have hashsize(10) elements.
+
+If you are hashing n strings (uint8_t **)k, do it like this:
+  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
+
+By Bob Jenkins, 2006.  bob_jenkins at burtleburtle.net.  You may use this
+code any way you wish, private, educational, or commercial.  It's free.
+
+Use for hash table lookup, or anything where one collision in 2^^32 is
+acceptable.  Do NOT use for cryptographic purposes.
+-------------------------------------------------------------------------------
+*/
+
+static uint32_t hashlittle( const void *key, size_t length, uint32_t *val2 )
+{
+  uint32_t a,b,c;                                          /* internal state */
+  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
+
+  /* Set up the internal state */
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
+
+  u.ptr = key;
+  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
+    const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
+    const uint8_t  *k8;
+
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += k[0];
+      b += k[1];
+      c += k[2];
+      mix(a,b,c);
+      length -= 12;
+      k += 3;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    /*
+     * "k[2]&0xffffff" actually reads beyond the end of the string, but
+     * then masks off the part it's not allowed to read.  Because the
+     * string is aligned, the masked-off tail is in the same word as the
+     * rest of the string.  Every machine with memory protection I've seen
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
+     * still catch it and complain.  The masking trick does make the hash
+     * noticably faster for short strings (like English words).
+     *
+     * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR.
+     */
+#if 0
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
+    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
+    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
+    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
+    case 5 : b+=k[1]&0xff; a+=k[0]; break;
+    case 4 : a+=k[0]; break;
+    case 3 : a+=k[0]&0xffffff; break;
+    case 2 : a+=k[0]&0xffff; break;
+    case 1 : a+=k[0]&0xff; break;
+    case 0 : return c;              /* zero length strings require no mixing */
+    }
+
+#else /* make valgrind happy */
+
+    k8 = (const uint8_t *)k;
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
+    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
+    case 9 : c+=k8[8];                   /* fall through */
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
+    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
+    case 5 : b+=k8[4];                   /* fall through */
+    case 4 : a+=k[0]; break;
+    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
+    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
+    case 1 : a+=k8[0]; break;
+    case 0 : return c;
+    }
+
+#endif /* !valgrind */
+
+  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
+    const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
+    const uint8_t  *k8;
+
+    /*--------------- all but last block: aligned reads and different mixing */
+    while (length > 12)
+    {
+      a += k[0] + (((uint32_t)k[1])<<16);
+      b += k[2] + (((uint32_t)k[3])<<16);
+      c += k[4] + (((uint32_t)k[5])<<16);
+      mix(a,b,c);
+      length -= 12;
+      k += 6;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    k8 = (const uint8_t *)k;
+    switch(length)
+    {
+    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
+             b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
+    case 10: c+=k[4];
+             b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 9 : c+=k8[8];                      /* fall through */
+    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
+    case 6 : b+=k[2];
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 5 : b+=k8[4];                      /* fall through */
+    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
+    case 2 : a+=k[0];
+             break;
+    case 1 : a+=k8[0];
+             break;
+    case 0 : return c;                     /* zero length requires no mixing */
+    }
+
+  } else {                        /* need to read the key one byte at a time */
+    const uint8_t *k = (const uint8_t *)key;
+
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += k[0];
+      a += ((uint32_t)k[1])<<8;
+      a += ((uint32_t)k[2])<<16;
+      a += ((uint32_t)k[3])<<24;
+      b += k[4];
+      b += ((uint32_t)k[5])<<8;
+      b += ((uint32_t)k[6])<<16;
+      b += ((uint32_t)k[7])<<24;
+      c += k[8];
+      c += ((uint32_t)k[9])<<8;
+      c += ((uint32_t)k[10])<<16;
+      c += ((uint32_t)k[11])<<24;
+      mix(a,b,c);
+      length -= 12;
+      k += 12;
+    }
+
+    /*-------------------------------- last block: affect all 32 bits of (c) */
+    switch(length)                   /* all the case statements fall through */
+    {
+    case 12: c+=((uint32_t)k[11])<<24;
+    case 11: c+=((uint32_t)k[10])<<16;
+    case 10: c+=((uint32_t)k[9])<<8;
+    case 9 : c+=k[8];
+    case 8 : b+=((uint32_t)k[7])<<24;
+    case 7 : b+=((uint32_t)k[6])<<16;
+    case 6 : b+=((uint32_t)k[5])<<8;
+    case 5 : b+=k[4];
+    case 4 : a+=((uint32_t)k[3])<<24;
+    case 3 : a+=((uint32_t)k[2])<<16;
+    case 2 : a+=((uint32_t)k[1])<<8;
+    case 1 : a+=k[0];
+             break;
+    case 0 : return c;
+    }
+  }
+
+  final(a,b,c);
+  *val2 = b;
+  return c;
+}
+
+/*
+ * hashbig():
+ * This is the same as hash_word() on big-endian machines.  It is different
+ * from hashlittle() on all machines.  hashbig() takes advantage of
+ * big-endian byte ordering.
+ */
+static uint32_t hashbig( const void *key, size_t length, uint32_t *val2)
+{
+  uint32_t a,b,c;
+  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
+
+  /* Set up the internal state */
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2;
+
+  u.ptr = key;
+  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
+    const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
+    const uint8_t  *k8;
+
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += k[0];
+      b += k[1];
+      c += k[2];
+      mix(a,b,c);
+      length -= 12;
+      k += 3;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    /*
+     * "k[2]<<8" actually reads beyond the end of the string, but
+     * then shifts out the part it's not allowed to read.  Because the
+     * string is aligned, the illegal read is in the same word as the
+     * rest of the string.  Every machine with memory protection I've seen
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
+     * still catch it and complain.  The masking trick does make the hash
+     * noticably faster for short strings (like English words).
+     *
+     * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR.
+     */
+#if 0
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
+    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
+    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
+    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
+    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
+    case 4 : a+=k[0]; break;
+    case 3 : a+=k[0]&0xffffff00; break;
+    case 2 : a+=k[0]&0xffff0000; break;
+    case 1 : a+=k[0]&0xff000000; break;
+    case 0 : return c;              /* zero length strings require no mixing */
+    }
+
+#else  /* make valgrind happy */
+
+    k8 = (const uint8_t *)k;
+    switch(length)                   /* all the case statements fall through */
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
+    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
+    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
+    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
+    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
+    case 4 : a+=k[0]; break;
+    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
+    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
+    case 1 : a+=((uint32_t)k8[0])<<24; break;
+    case 0 : return c;
+    }
+
+#endif /* !VALGRIND */
+
+  } else {                        /* need to read the key one byte at a time */
+    const uint8_t *k = (const uint8_t *)key;
+
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += ((uint32_t)k[0])<<24;
+      a += ((uint32_t)k[1])<<16;
+      a += ((uint32_t)k[2])<<8;
+      a += ((uint32_t)k[3]);
+      b += ((uint32_t)k[4])<<24;
+      b += ((uint32_t)k[5])<<16;
+      b += ((uint32_t)k[6])<<8;
+      b += ((uint32_t)k[7]);
+      c += ((uint32_t)k[8])<<24;
+      c += ((uint32_t)k[9])<<16;
+      c += ((uint32_t)k[10])<<8;
+      c += ((uint32_t)k[11]);
+      mix(a,b,c);
+      length -= 12;
+      k += 12;
+    }
+
+    /*-------------------------------- last block: affect all 32 bits of (c) */
+    switch(length)                   /* all the case statements fall through */
+    {
+    case 12: c+=k[11];
+    case 11: c+=((uint32_t)k[10])<<8;
+    case 10: c+=((uint32_t)k[9])<<16;
+    case 9 : c+=((uint32_t)k[8])<<24;
+    case 8 : b+=k[7];
+    case 7 : b+=((uint32_t)k[6])<<8;
+    case 6 : b+=((uint32_t)k[5])<<16;
+    case 5 : b+=((uint32_t)k[4])<<24;
+    case 4 : a+=k[3];
+    case 3 : a+=((uint32_t)k[2])<<8;
+    case 2 : a+=((uint32_t)k[1])<<16;
+    case 1 : a+=((uint32_t)k[0])<<24;
+             break;
+    case 0 : return c;
+    }
+  }
+
+  final(a,b,c);
+  *val2 = b;
+  return c;
+}
+
+uint32_t hash_any(const void *key, size_t length, uint32_t base)
+{
+	if (HASH_BIG_ENDIAN)
+		return hashbig(key, length, &base);
+	else
+		return hashlittle(key, length, &base);
+}
-- 
1.7.2.5

-------------- next part --------------
>From 207457fbf457214fedbd58ca1fa159ce020f21f8 Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:47 -0700
Subject: [PATCH 2/6] Add nl hashtable structures and access functions
To: roopa at cumulusnetworks.com

This patch adds the required structures and access functions to create
and manage hashtables for netlink cache objects

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/hashtable.h  |   53 ++++++++++++++++
 include/netlink/object-api.h |    9 +++
 include/netlink/object.h     |    2 +
 lib/Makefile.am              |    2 +-
 lib/hashtable.c              |  141 ++++++++++++++++++++++++++++++++++++++++++
 lib/object.c                 |   21 ++++++
 6 files changed, 227 insertions(+), 1 deletions(-)
 create mode 100644 include/netlink/hashtable.h
 create mode 100644 lib/hashtable.c

diff --git a/include/netlink/hashtable.h b/include/netlink/hashtable.h
new file mode 100644
index 0000000..3cd7875
--- /dev/null
+++ b/include/netlink/hashtable.h
@@ -0,0 +1,53 @@
+/*
+ * netlink/hashtable.h       Netlink hashtable Utilities
+ *
+ *      This library is free software; you can redistribute it and/or
+ *      modify it under the terms of the GNU Lesser General Public
+ *      License as published by the Free Software Foundation version 2.1
+ *      of the License.
+ *
+ * Copyright (c) 2012 Cumulus Networks, Inc
+ */
+
+#ifndef NETLINK_HASHTABLE_H_
+#define NETLINK_HASHTABLE_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct nl_hash_node {
+    uint32_t			key;
+    uint32_t			key_size;
+    struct nl_object *		obj;
+    struct nl_hash_node *	next;
+} nl_hash_node_t;
+
+typedef struct nl_hash_table {
+    int 			size;
+    nl_hash_node_t **		nodes;
+} nl_hash_table_t;
+
+/* Default hash table size */
+#define NL_MAX_HASH_ENTRIES 16384
+#define NL_MAX_HASH_KEY 32
+
+/* Access Functions */
+extern nl_hash_table_t *	nl_hash_table_alloc(int size);
+extern void 			nl_hash_table_free(nl_hash_table_t *ht);
+
+extern int			nl_hash_table_add(nl_hash_table_t *ht,
+						  struct nl_object *obj);
+extern int			nl_hash_table_del(nl_hash_table_t *ht,
+						  struct nl_object *obj);
+
+extern struct nl_object *	nl_hash_table_lookup(nl_hash_table_t *ht,
+						     struct nl_object *obj);
+extern uint32_t 		nl_hash(void *k, size_t length,
+					uint32_t initval);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NETLINK_HASHTABLE_H_ */
diff --git a/include/netlink/object-api.h b/include/netlink/object-api.h
index 70a4ddd..0ed2a0c 100644
--- a/include/netlink/object-api.h
+++ b/include/netlink/object-api.h
@@ -335,6 +335,15 @@ struct nl_object_ops
 	int   (*oo_compare)(struct nl_object *, struct nl_object *,
 			    uint32_t, int);
 
+	/**
+	 * Hash Key generator function
+	 *
+	 * When called returns a hash key for the object being
+	 * referenced. This key will be used by higher level hash functions
+	 * to build association lists. Each object type gets to specify
+	 * it's own key formulation
+	 */
+	void   (*oo_keygen)(struct nl_object *, uint32_t *, uint32_t);
 
 	char *(*oo_attrs2str)(int, char *, size_t);
 };
diff --git a/include/netlink/object.h b/include/netlink/object.h
index bbda5f5..210a488 100644
--- a/include/netlink/object.h
+++ b/include/netlink/object.h
@@ -48,6 +48,8 @@ extern char *			nl_object_attrs2str(struct nl_object *,
 						    size_t);
 extern char *			nl_object_attr_list(struct nl_object *,
 						    char *, size_t);
+extern void			nl_object_keygen(struct nl_object *,
+						 uint32_t *, uint32_t);
 
 /* Marks */
 extern void			nl_object_mark(struct nl_object *);
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4d8d58d..4f6a003 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -18,7 +18,7 @@ lib_LTLIBRARIES = \
 libnl_3_la_SOURCES = \
 	addr.c attr.c cache.c cache_mngr.c cache_mngt.c data.c \
 	error.c handlers.c msg.c nl.c object.c socket.c utils.c \
-	version.c hash.c
+	version.c hash.c hashtable.c
 
 libnl_genl_3_la_LIBADD  = libnl-3.la
 libnl_genl_3_la_SOURCES = \
diff --git a/lib/hashtable.c b/lib/hashtable.c
new file mode 100644
index 0000000..59cd91e
--- /dev/null
+++ b/lib/hashtable.c
@@ -0,0 +1,141 @@
+/*
+ * netlink/hashtable.c      Netlink hashtable Utilities
+ *
+ *      This library is free software; you can redistribute it and/or
+ *      modify it under the terms of the GNU Lesser General Public
+ *      License as published by the Free Software Foundation version 2.1
+ *      of the License.
+ *
+ * Copyright (c) 2012 Cumulus Networks, Inc
+ */
+#include <string.h>
+#include <netlink-local.h>
+#include <netlink/object.h>
+#include <netlink/hash.h>
+#include <netlink/hashtable.h>
+
+nl_hash_table_t *nl_hash_table_alloc(int size)
+{
+	nl_hash_table_t *ht;
+
+	ht = calloc(1, sizeof (*ht));
+	if (!ht)
+		goto errout;
+
+	ht->nodes = calloc(size, sizeof (*ht->nodes));
+	if (!ht->nodes) {
+		free(ht);
+		goto errout;
+	}
+
+	ht->size = size;
+
+	return ht;
+errout:
+	return NULL;
+}
+
+void nl_hash_table_free(nl_hash_table_t *ht)
+{
+	int i;
+
+	for(i = 0; i < ht->size; i++) {
+	    nl_hash_node_t *node = ht->nodes[i];
+	    nl_hash_node_t *saved_node;
+
+	    while (node) {
+		   saved_node = node;
+		   node = node->next;
+		   free(saved_node);
+	    }
+	}
+
+	free(ht->nodes);
+	free(ht);
+}
+
+struct nl_object* nl_hash_table_lookup(nl_hash_table_t *ht,
+				       struct nl_object *obj)
+{
+	nl_hash_node_t *node;
+	uint32_t key_hash;
+
+	nl_object_keygen(obj, &key_hash, ht->size);
+	node = ht->nodes[key_hash];
+
+	while (node) {
+	       if (nl_object_identical(node->obj, obj))
+		   return node->obj;
+	       node = node->next;
+	}
+
+	return NULL;
+}
+
+int nl_hash_table_add(nl_hash_table_t *ht, struct nl_object *obj)
+{
+	nl_hash_node_t *node;
+	uint32_t key_hash;
+
+	nl_object_keygen(obj, &key_hash, ht->size);
+	node = ht->nodes[key_hash];
+
+	while (node) {
+	       if (nl_object_identical(node->obj, obj)) {
+		   NL_DBG(2, "Warning: Add to hashtable found duplicate...\n");
+		   return -NLE_EXIST;
+	       }
+	       node = node->next;
+	}
+
+	NL_DBG (5, "adding cache entry of obj %p in table %p, with hash 0x%x\n",
+		obj, ht, key_hash);
+
+	node = malloc(sizeof(nl_hash_node_t));
+	if (!node)
+		return -NLE_NOMEM;
+	nl_object_get(obj);
+	node->obj = obj;
+	node->key = key_hash;
+	node->key_size = sizeof(uint32_t);
+	node->next = ht->nodes[key_hash];
+	ht->nodes[key_hash] = node;
+
+	return 0;
+}
+
+int nl_hash_table_del(nl_hash_table_t *ht, struct nl_object *obj)
+{
+	nl_hash_node_t *node, *prev;
+	uint32_t key_hash;
+
+	nl_object_keygen(obj, &key_hash, ht->size);
+	prev = node = ht->nodes[key_hash];
+
+	while (node) {
+	       if (nl_object_identical(node->obj, obj)) {
+		   nl_object_put(obj);
+
+		   NL_DBG (5, "deleting cache entry of obj %p in table %p, with"
+			   " hash 0x%x\n", obj, ht, key_hash);
+
+	           if (node == ht->nodes[key_hash])
+		       ht->nodes[key_hash] = node->next;
+	           else
+		       prev->next = node->next;
+
+	           free(node);
+
+	           return 0;
+		}
+		prev = node;
+		node = node->next;
+	}
+
+	return -1;
+}
+
+uint32_t nl_hash(void *k, size_t length, uint32_t initval)
+{
+	return(hash(k, length, initval));
+}
diff --git a/lib/object.c b/lib/object.c
index 055a208..063c2e0 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -388,6 +388,27 @@ char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
 	return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
 }
 
+/**
+ * Generate object hash key
+ * @arg obj		the object
+ * @arg hashkey		destination buffer to be used for key stream
+ * @arg hashtbl_sz	hash table size
+ *
+ * @return hash key in destination buffer
+ */
+void nl_object_keygen(struct nl_object *obj, uint32_t *hashkey,
+		      uint32_t hashtbl_sz)
+{
+	struct nl_object_ops *ops = obj_ops(obj);
+
+	if (ops->oo_keygen)
+		ops->oo_keygen(obj, hashkey, hashtbl_sz);
+	else
+		*hashkey = 0;
+
+	return;
+}
+
 /** @} */
 
 /**
-- 
1.7.2.5

-------------- next part --------------
>From d5ffb0445c51cc85cce1c469b778eb4e71cd4aae Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:48 -0700
Subject: [PATCH 3/6] Add hash support in cache mngr
To: roopa at cumulusnetworks.com

This patch adds support to create, delete modify hash table for a 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-types.h     |    2 +
 include/netlink/cache-api.h |    3 ++
 include/netlink/cache.h     |    2 +
 lib/cache.c                 |   71 +++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/include/netlink-types.h b/include/netlink-types.h
index 10ef218..0cc781b 100644
--- a/include/netlink-types.h
+++ b/include/netlink-types.h
@@ -30,6 +30,7 @@
 struct nl_cache_ops;
 struct nl_sock;
 struct nl_object;
+struct nl_hash_table;
 
 struct nl_cb
 {
@@ -78,6 +79,7 @@ struct nl_cache
 	int			c_nitems;
 	int                     c_iarg1;
 	int                     c_iarg2;
+	struct nl_hash_table *	hashtable;
 	struct nl_cache_ops *   c_ops;
 };
 
diff --git a/include/netlink/cache-api.h b/include/netlink/cache-api.h
index 390cbea..aa5eeb6 100644
--- a/include/netlink/cache-api.h
+++ b/include/netlink/cache-api.h
@@ -186,6 +186,9 @@ struct nl_cache_ops
 	/** Netlink protocol */
 	int			co_protocol;
 
+	/** cache object hash size **/
+	int			co_hash_size;
+
 	/** Group definition */
 	struct nl_af_group *	co_groups;
 	
diff --git a/include/netlink/cache.h b/include/netlink/cache.h
index fd137e1..c919e6b 100644
--- a/include/netlink/cache.h
+++ b/include/netlink/cache.h
@@ -71,6 +71,8 @@ extern void			nl_cache_set_arg2(struct nl_cache *, int);
 extern int			nl_cache_is_empty(struct nl_cache *);
 extern struct nl_object *	nl_cache_search(struct nl_cache *,
 						struct nl_object *);
+extern struct nl_object *	nl_cache_lookup(struct nl_cache *,
+						struct nl_object *);
 extern void			nl_cache_mark_all(struct nl_cache *);
 
 /* Dumping */
diff --git a/lib/cache.c b/lib/cache.c
index 45a1a27..15b3e08 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -53,6 +53,7 @@
 #include <netlink/netlink.h>
 #include <netlink/cache.h>
 #include <netlink/object.h>
+#include <netlink/hashtable.h>
 #include <netlink/utils.h>
 
 /**
@@ -190,6 +191,22 @@ struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
 	nl_init_list_head(&cache->c_items);
 	cache->c_ops = ops;
 
+	/*
+	 * If object type provides a hash keygen
+	 * functions, allocate a hash table for the
+	 * cache objects for faster lookups
+	 */
+	if (ops->co_obj_ops->oo_keygen) {
+		int hashtable_size;
+
+		if (ops->co_hash_size)
+			hashtable_size = ops->co_hash_size;
+		else
+			hashtable_size = NL_MAX_HASH_ENTRIES;
+
+		cache->hashtable = nl_hash_table_alloc(hashtable_size);
+	}
+
 	NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
 
 	return cache;
@@ -362,6 +379,10 @@ void nl_cache_free(struct nl_cache *cache)
 		return;
 
 	nl_cache_clear(cache);
+
+	if (cache->hashtable)
+		nl_hash_table_free(cache->hashtable);
+
 	NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
 	free(cache);
 }
@@ -375,8 +396,18 @@ void nl_cache_free(struct nl_cache *cache)
 
 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
 {
+	int ret;
+
 	obj->ce_cache = cache;
 
+	if (cache->hashtable) {
+		ret = nl_hash_table_add(cache->hashtable, obj);
+		if (ret < 0) {
+			obj->ce_cache = NULL;
+			return ret;
+		}
+	}
+
 	nl_list_add_tail(&obj->ce_list, &cache->c_items);
 	cache->c_nitems++;
 
@@ -411,6 +442,7 @@ static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
 {
 	struct nl_object *new;
+	int ret = 0;
 
 	if (cache->c_ops->co_obj_ops != obj->ce_ops)
 		return -NLE_OBJ_MISMATCH;
@@ -424,7 +456,11 @@ int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
 		new = obj;
 	}
 
-	return __cache_add(cache, new);
+	ret = __cache_add(cache, new);
+	if (ret < 0)
+		nl_object_put(new);
+
+	return ret;
 }
 
 /**
@@ -474,11 +510,19 @@ int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
  */
 void nl_cache_remove(struct nl_object *obj)
 {
+	int ret;
 	struct nl_cache *cache = obj->ce_cache;
 
 	if (cache == NULL)
 		return;
 
+	if (cache->hashtable) {
+		ret = nl_hash_table_del(cache->hashtable, obj);
+		if (ret < 0)
+			NL_DBG(3, "Failed to delete %p from cache %p <%s>.\n",
+			       obj, cache, nl_cache_name(cache));
+	}
+
 	nl_list_del(&obj->ce_list);
 	obj->ce_cache = NULL;
 	nl_object_put(obj);
@@ -566,8 +610,13 @@ struct update_xdata {
 static int update_msg_parser(struct nl_msg *msg, void *arg)
 {
 	struct update_xdata *x = arg;
-	
-	return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
+	int ret = 0;
+
+	ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
+	if (ret == -NLE_EXIST)
+		return NL_SKIP;
+	else
+		return ret;
 }
 /** @endcond */
 
@@ -831,6 +880,19 @@ restart:
  * @name Utillities
  * @{
  */
+static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
+					     struct nl_object *needle)
+{
+	struct nl_object *obj;
+
+	obj = nl_hash_table_lookup(cache->hashtable, needle);
+	if (obj) {
+	    nl_object_get(obj);
+	    return obj;
+	}
+
+	return NULL;
+}
 
 /**
  * Search object in cache
@@ -852,6 +914,9 @@ struct nl_object *nl_cache_search(struct nl_cache *cache,
 {
 	struct nl_object *obj;
 
+	if (cache->hashtable)
+		return __cache_fast_lookup(cache, needle);
+
 	nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
 		if (nl_object_identical(obj, needle)) {
 			nl_object_get(obj);
-- 
1.7.2.5

-------------- next part --------------
>From 764d9dfe37e17e9f834b8e5f803413bc443bf41e Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:48 -0700
Subject: [PATCH 4/6] Add hash support to link cache
To: 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

-------------- next part --------------
>From 3a15fff02ecd879109d798c7138c80e0717a89b4 Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:48 -0700
Subject: [PATCH 5/6] Add hash support to neigh cache
To: roopa at cumulusnetworks.com

This patch adds keygen function to the neigh object

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>
---
 lib/route/neigh.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/lib/route/neigh.c b/lib/route/neigh.c
index bb61571..a9ab736 100644
--- a/lib/route/neigh.c
+++ b/lib/route/neigh.c
@@ -151,6 +151,7 @@
 #include <netlink-local.h>
 #include <netlink/netlink.h>
 #include <netlink/utils.h>
+#include <netlink/hashtable.h>
 #include <netlink/route/rtnl.h>
 #include <netlink/route/neighbour.h>
 #include <netlink/route/link.h>
@@ -197,6 +198,54 @@ 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;
+	struct neigh_hash_key *_key;
+	char *_key_b;
+	unsigned int _key_sz;
+	unsigned int sz_dst = 0;
+	void *dst = NULL;
+	struct neigh_hash_key {
+		uint32_t	n_family;
+		uint32_t	n_ifindex;
+		void 		*n_addr;
+	};
+
+	if (neigh->n_dst) {
+	    sz_dst = nl_addr_get_len(neigh->n_dst);
+	    dst = nl_addr_get_binary_addr(neigh->n_dst);
+	}
+
+	_key_sz = sizeof(struct neigh_hash_key) -
+			sizeof(void *) + sz_dst;
+
+	_key = calloc(1 , _key_sz);
+	if (!_key) {
+	    NL_DBG(2, "Warning: calloc failed for %d bytes...\n", _key_sz);
+	    *hashkey = 0;
+	}
+
+	_key_b = (char *) &(_key->n_addr);
+	_key->n_family = neigh->n_family;
+	_key->n_ifindex = neigh->n_ifindex;
+
+	NL_DBG(5, "neigh %p dst_a %p, size %d\n",
+	       neigh,
+	       &(_key->n_addr),
+	       _key_sz);
+
+	if (sz_dst)
+	    memcpy((void *)_key_b, dst, sz_dst);
+
+	*hashkey = nl_hash(_key, _key_sz, 0) % table_sz;
+
+	free(_key);
+
+	return;
+}
+
 static int neigh_compare(struct nl_object *_a, struct nl_object *_b,
 			uint32_t attrs, int flags)
 {
@@ -825,6 +874,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),
 };
-- 
1.7.2.5

-------------- next part --------------
>From a19d02dcc1a7e9fd0840d0619ac71b42fd3c76df Mon Sep 17 00:00:00 2001
From: roopa <roopa at cumulusnetworks.com>
Date: Thu, 25 Oct 2012 12:42:48 -0700
Subject: [PATCH 6/6] Add hash support to route cache
To: roopa at cumulusnetworks.com

This patch adds keygen function to route object

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>
---
 lib/route/route_obj.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/lib/route/route_obj.c b/lib/route/route_obj.c
index 54df023..c01c347 100644
--- a/lib/route/route_obj.c
+++ b/lib/route/route_obj.c
@@ -35,6 +35,7 @@
 #include <netlink/cache.h>
 #include <netlink/utils.h>
 #include <netlink/data.h>
+#include <netlink/hashtable.h>
 #include <netlink/route/rtnl.h>
 #include <netlink/route/route.h>
 #include <netlink/route/link.h>
@@ -289,6 +290,59 @@ 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;
+	struct route_hash_key *_key;
+	char *_key_b;
+	unsigned int _key_sz;
+	unsigned int sz_dst = 0;
+	void *dst = NULL;
+	struct route_hash_key {
+		uint8_t		rt_family;
+		uint8_t		rt_tos;
+		uint32_t	rt_table;
+		void 		*rt_addr;
+	};
+
+	if (route->rt_dst) {
+	    sz_dst = nl_addr_get_len(route->rt_dst);
+	    dst = nl_addr_get_binary_addr(route->rt_dst);
+	}
+
+	_key_sz = sizeof(struct route_hash_key) -
+			sizeof(void *) + sz_dst;
+
+	_key = calloc(1, _key_sz);
+
+	if (!_key) {
+	    NL_DBG(2, "Warning: calloc failed for %d bytes...\n", _key_sz);
+	    *hashkey = 0;
+	}
+
+	_key_b = (char *) &(_key->rt_addr);
+	_key->rt_family = route->rt_family;
+	_key->rt_tos = route->rt_tos;
+	_key->rt_table = route->rt_table;
+
+	NL_DBG(5, "route %p buffer %p, source %p, size %d\n",
+	       route,
+	       _key,
+	       nl_addr_get_binary_addr(route->rt_dst),
+	       _key_sz);
+
+
+	if (sz_dst)
+	    memcpy((void *)_key_b, dst, sz_dst);
+
+	*hashkey = nl_hash(_key, _key_sz, 0) % table_sz;
+
+	free(_key);
+
+	return;
+}
+
 static int route_compare(struct nl_object *_a, struct nl_object *_b,
 			uint32_t attrs, int flags)
 {
@@ -1151,6 +1205,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),
-- 
1.7.2.5



More information about the libnl mailing list