]> git.ipfire.org Git - thirdparty/libnl.git/commitdiff
Add support for updating objects in the cache
authorroopa <roopa@cumulusnetworks.com>
Thu, 1 Nov 2012 21:13:30 +0000 (14:13 -0700)
committerThomas Graf <tgraf@redhat.com>
Mon, 5 Nov 2012 12:58:50 +0000 (13:58 +0100)
This patch adds support to update a cache object during cache_include instead
of the current approach of deleting the original object and adding a new one.
This operation is conditional on the object implementing the operation. If
the update is not successful, cache_include falls back to the existing cache
inclusion process of deleting and adding the object.

It adds a new object operation called oo_update. oo_update takes two objects
as arguments, first being the existing cache object that needs update, the
second argument being the new object. Currently it is left to the implementor
to use the msg type to decide wether to delete or add the new object attributes
to the old one. But the operation type or msg type can be easily made part of the
object arguments.

The motivation for this change is explained below in the context of including
support for AF_BRIDGE objects into the link cache.

libnl today deletes an object before it includes an identical object.
But for some objects like the AF_BRIDGE objects this does not work well.
link cache uses the ifindex as its key in object searches.
If link cache were to support AF_BRIDGE family objects, todays implementation,
- will replace the original link object with the bridge port link object
  for add notifications
- And a bridge port delete notification from kernel would delete the
link object from the cache leaving the cache without the link object
until the kernel sends another notification for that link

The bridge port link notification contains some base link object attributes
plus bridge specific protocol info attributes. In such cases we think an
operation to update the existing object in place in cache might be useful.

This can be made to work for AF_INET6 link objects too.

Signed-off-by: Roopa Prabhu <roopa@cumulusnetworks.com>
Reviewed-by: Nolan Leake <nolan@cumulusnetworks.com>
Reviewed-by: Shrijeet Mukherjee <shm@cumulusnetworks.com>
Reviewed-by: Wilson Kok <wkok@cumulusnetworks.com>
include/netlink/object-api.h
include/netlink/object.h
lib/cache.c
lib/object.c

index cfd5699b6a5ce61edab2eb6e297fcfbc34c6c381..ec2192b5a7230ee6d7ae95e5973f23801f0fda8f 100644 (file)
@@ -336,6 +336,18 @@ struct nl_object_ops
                            uint32_t, int);
 
 
+       /**
+        * update function
+        *
+        * Will be called when the object given by first argument
+        * needs to be updated with the contents of the second object
+        *
+        * The function must return 0 for success and error for failure
+        * to update. In case of failure its assumed that the original
+        * object is not touched
+        */
+       int   (*oo_update)(struct nl_object *, struct nl_object *);
+
        char *(*oo_attrs2str)(int, char *, size_t);
 
        /**
index bbda5f520a0f1ae2b776da928630eadecbe2a3d4..788ae487664e323f3cbf10546408057ff9868f79 100644 (file)
@@ -31,6 +31,8 @@ extern int                    nl_object_alloc_name(const char *,
                                                     struct nl_object **);
 extern void                    nl_object_free(struct nl_object *);
 extern struct nl_object *      nl_object_clone(struct nl_object *obj);
+extern int                     nl_object_update(struct nl_object *dst,
+                                                struct nl_object *src);
 extern void                    nl_object_get(struct nl_object *);
 extern void                    nl_object_put(struct nl_object *);
 extern int                     nl_object_shared(struct nl_object *);
index 45a1a27130f7e0fa4b41709c6ebd99a9c8d9871a..f73fedf8750cfac661c3a35f2f9c927222ab7a49 100644 (file)
@@ -642,6 +642,17 @@ static int cache_include(struct nl_cache *cache, struct nl_object *obj,
        case NL_ACT_DEL:
                old = nl_cache_search(cache, obj);
                if (old) {
+                       /*
+                        * Some objects types might support merging the new
+                        * object with the old existing cache object.
+                        * Handle them first.
+                        */
+                       if (nl_object_update(old, obj) == 0) {
+                               cb(cache, old, NL_ACT_CHANGE, data);
+                               nl_object_put(obj);
+                               return 0;
+                       }
+
                        nl_cache_remove(old);
                        if (type->mt_act == NL_ACT_DEL) {
                                if (cb)
index 585189e9187fe9f56a07aca4f615249c4aa9d910..d58f9a73e0e18f288a4fd756ca47ed65fc6b92d8 100644 (file)
@@ -138,6 +138,23 @@ struct nl_object *nl_object_clone(struct nl_object *obj)
        return new;
 }
 
+/**
+ * Merge a cacheable object
+ * @arg dst            object to be merged into
+ * @arg src            new object to be merged into dst
+ *
+ * @return 0 or a negative error code.
+ */
+int nl_object_update(struct nl_object *dst, struct nl_object *src)
+{
+       struct nl_object_ops *ops = obj_ops(dst);
+
+       if (ops->oo_update)
+               return ops->oo_update(dst, src);
+
+       return -NLE_OPNOTSUPP;
+}
+
 /**
  * Free a cacheable object
  * @arg obj            object to free