[PATCH v2] cache pickup: Avoid duplicates during cache pickup

roopa at cumulusnetworks.com roopa at cumulusnetworks.com
Thu Dec 20 21:32:00 EST 2012


From: roopa <roopa at cumulusnetworks.com>

This patch adds search and replace/update functionality
to cache pickup_cb. This change is required to avoid
duplicates getting into the cache during pickup. Also
helps updating existing objects during cache pickup.
We have seen this in cases of ipv6 equal cost multipath
routes

changes v1 to v2:
	- Updated documentation

Signed-off-by: Roopa Prabhu <roopa at cumulusnetworks.com>
---
 lib/cache.c |   26 +++++++++++++++++++++++---
 1 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/lib/cache.c b/lib/cache.c
index 36c45e1..883aa51 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -696,7 +696,21 @@ static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
 
 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
 {
-	return nl_cache_add((struct nl_cache *) p->pp_arg, c);
+	struct nl_cache *cache = (struct nl_cache *)p->pp_arg;
+	struct nl_object *old;
+
+	old = nl_cache_search(cache, c);
+	if (old) {
+		if (nl_object_update(old, c) == 0) {
+			nl_object_put(old);
+			return 0;
+		}
+
+		nl_cache_remove(old);
+		nl_object_put(old);
+	}
+
+	return nl_cache_add(cache, c);
 }
 
 /**
@@ -705,7 +719,10 @@ static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
  * @arg cache		Cache to put items into.
  *
  * Waits for netlink messages to arrive, parses them and puts them into
- * the specified cache.
+ * the specified cache. If an old object with same key attributes is
+ * present in the cache, it is replaced with the new object.
+ * If the old object type supports an update operation, an update is
+ * attempted before a replace.
  *
  * @return 0 on success or a negative error code.
  */
@@ -889,7 +906,10 @@ errout:
  * @arg msg		netlink message
  *
  * Parses a netlink message by calling the cache specific message parser
- * and adds the new element to the cache.
+ * and adds the new element to the cache. If an old object with same key
+ * attributes is present in the cache, it is replaced with the new object.
+ * If the old object type supports an update operation, an update is
+ * attempted before a replace.
  *
  * @return 0 or a negative error code.
  */
-- 
1.7.2.5




More information about the libnl mailing list