summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroopa <roopa@cumulusnetworks.com>2013-01-08 04:33:29 -0800
committerThomas Graf <tgraf@suug.ch>2013-01-11 13:52:50 +0100
commitb1ebda92410c57c09c7aff3e4d5ce3572d9fc1c1 (patch)
tree278021a70471809c8f2fdf7a3a80fd6f20174754
parent3540e44b158f0f45fc2364d99e70433fba176b21 (diff)
downloadlibnl-b1ebda92410c57c09c7aff3e4d5ce3572d9fc1c1.tar.gz
cache: Add new nl_cache_find api
This patch adds new cache find api nl_cache_find api was suggested by Thomas. Unlike nl_cache_search, this patch uses nl_object_match_filter() to look for an object match. Am not sure this matches what was decided on the list few weeks back. I will be happy to make any changes. Signed-off-by: Roopa Prabhu <roopa@cumulusnetworks.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
-rw-r--r--include/netlink/object.h1
-rw-r--r--lib/cache.c39
-rw-r--r--lib/object.c22
3 files changed, 62 insertions, 0 deletions
diff --git a/include/netlink/object.h b/include/netlink/object.h
index f25713e..a95feda 100644
--- a/include/netlink/object.h
+++ b/include/netlink/object.h
@@ -64,6 +64,7 @@ extern struct nl_cache * nl_object_get_cache(struct nl_object *);
extern const char * nl_object_get_type(const struct nl_object *);
extern int nl_object_get_msgtype(const struct nl_object *);
struct nl_object_ops * nl_object_get_ops(const struct nl_object *);
+uint32_t nl_object_get_id_attrs(struct nl_object *obj);
static inline void * nl_object_priv(struct nl_object *obj)
diff --git a/lib/cache.c b/lib/cache.c
index 42c0b5e..24ffa31 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -1023,6 +1023,45 @@ struct nl_object *nl_cache_search(struct nl_cache *cache,
}
/**
+ * Find object in cache
+ * @arg cache Cache
+ * @arg filter object acting as a filter
+ *
+ * Searches the cache for an object which matches the object filter.
+ * If the filter attributes matches the object type id attributes,
+ * and the cache supports hash lookups, a faster hashtable lookup
+ * is used to return the object. Else, function nl_object_match_filter() is
+ * used to determine if the objects match. If a matching object is
+ * found, the reference counter is incremented and the object is returned.
+ *
+ * Therefore, if an object is returned, the reference to the object
+ * must be returned by calling nl_object_put() after usage.
+ *
+ * @return Reference to object or NULL if not found.
+ */
+struct nl_object *nl_cache_find(struct nl_cache *cache,
+ struct nl_object *filter)
+{
+ struct nl_object *obj;
+
+ if (cache->c_ops == NULL)
+ BUG();
+
+ if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
+ && cache->hashtable)
+ return __cache_fast_lookup(cache, filter);
+
+ nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
+ if (nl_object_match_filter(obj, filter)) {
+ nl_object_get(obj);
+ return obj;
+ }
+ }
+
+ return NULL;
+}
+
+/**
* Mark all objects of a cache
* @arg cache Cache
*
diff --git a/lib/object.c b/lib/object.c
index 806bae6..7361194 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -506,6 +506,28 @@ struct nl_object_ops *nl_object_get_ops(const struct nl_object *obj)
return obj->ce_ops;
}
+/**
+ * Return object id attribute mask
+ * @arg obj object
+ *
+ * @return object id attribute mask
+ */
+uint32_t nl_object_get_id_attrs(struct nl_object *obj)
+{
+ struct nl_object_ops *ops = obj_ops(obj);
+ uint32_t id_attrs;
+
+ if (!ops)
+ return 0;
+
+ if (ops->oo_id_attrs_get)
+ id_attrs = ops->oo_id_attrs_get(obj);
+ else
+ id_attrs = ops->oo_id_attrs;
+
+ return id_attrs;
+}
+
/** @} */
/** @} */