22#include <netlink-private/netlink.h>
23#include <netlink/netlink.h>
24#include <netlink/cache.h>
25#include <netlink/utils.h>
27static struct nl_cache_ops *cache_ops;
28static NL_RW_LOCK(cache_ops_lock);
35static struct nl_cache_ops *__nl_cache_ops_lookup(
const char *name)
37 struct nl_cache_ops *ops;
39 for (ops = cache_ops; ops; ops = ops->co_next)
40 if (!strcmp(ops->co_name, name))
75 struct nl_cache_ops *ops;
77 nl_read_lock(&cache_ops_lock);
78 ops = __nl_cache_ops_lookup(name);
79 nl_read_unlock(&cache_ops_lock);
95 struct nl_cache_ops *ops;
97 nl_write_lock(&cache_ops_lock);
98 if ((ops = __nl_cache_ops_lookup(name)))
100 nl_write_unlock(&cache_ops_lock);
105static struct nl_cache_ops *__cache_ops_associate(
int protocol,
int msgtype)
108 struct nl_cache_ops *ops;
110 for (ops = cache_ops; ops; ops = ops->co_next) {
111 if (ops->co_protocol != protocol)
114 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
115 if (ops->co_msgtypes[i].mt_id == msgtype)
136 struct nl_cache_ops *ops;
138 nl_read_lock(&cache_ops_lock);
139 ops = __cache_ops_associate(protocol, msgtype);
140 nl_read_unlock(&cache_ops_lock);
160 struct nl_cache_ops *ops;
162 nl_write_lock(&cache_ops_lock);
163 if ((ops = __cache_ops_associate(protocol, msgtype)))
165 nl_write_unlock(&cache_ops_lock);
187 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
188 if (ops->co_msgtypes[i].mt_id == msgtype)
189 return &ops->co_msgtypes[i];
195static struct nl_cache_ops *cache_ops_lookup_for_obj(
struct nl_object_ops *obj_ops)
197 struct nl_cache_ops *ops;
199 for (ops = cache_ops; ops; ops = ops->co_next)
200 if (ops->co_obj_ops == obj_ops)
214 struct nl_cache_ops *ops;
216 nl_read_lock(&cache_ops_lock);
217 for (ops = cache_ops; ops; ops = ops->co_next)
219 nl_read_unlock(&cache_ops_lock);
232 nl_write_lock(&cache_ops_lock);
233 ops->co_flags |= flags;
234 nl_write_unlock(&cache_ops_lock);
248 if (!ops->co_name || !ops->co_obj_ops)
252 BUG_ON (ops->co_obj_ops->oo_keygen && !ops->co_obj_ops->oo_compare);
254 nl_write_lock(&cache_ops_lock);
255 if (__nl_cache_ops_lookup(ops->co_name)) {
256 nl_write_unlock(&cache_ops_lock);
261 ops->co_next = cache_ops;
263 nl_write_unlock(&cache_ops_lock);
265 NL_DBG(1,
"Registered cache operations %s\n", ops->co_name);
283 struct nl_cache_ops *t, **tp;
286 nl_write_lock(&cache_ops_lock);
288 if (ops->co_refcnt > 0) {
293 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
302 NL_DBG(1,
"Unregistered cache operations %s\n", ops->co_name);
306 nl_write_unlock(&cache_ops_lock);
328 struct nl_cache_ops *ops;
330 nl_write_lock(&cache_ops_lock);
332 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
342 if (!ops->co_major_cache)
345 ops->co_major_cache = cache;
348 nl_write_unlock(&cache_ops_lock);
361 struct nl_cache_ops *ops;
363 nl_write_lock(&cache_ops_lock);
365 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
368 else if (ops->co_major_cache == cache) {
371 ops->co_major_cache = NULL;
374 nl_write_unlock(&cache_ops_lock);
377struct nl_cache *__nl_cache_mngt_require(
const char *name)
379 struct nl_cache_ops *ops;
380 struct nl_cache *cache = NULL;
384 cache = ops->co_major_cache;
404 struct nl_cache *cache;
406 if (!(cache = __nl_cache_mngt_require(name)))
407 NL_DBG(1,
"Application BUG: Your application must "
408 "call nl_cache_mngt_provide() and\nprovide a valid "
409 "%s cache to be used for internal lookups.\nSee the "
410 " API documentation for more details.\n", name);
426 struct nl_cache *cache;
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
void nl_cache_ops_foreach(void(*cb)(struct nl_cache_ops *, void *), void *arg)
Call a function for each registered cache operation.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
void nl_cache_ops_set_flags(struct nl_cache_ops *ops, unsigned int flags)
Set default flags for caches of this type.
struct nl_cache_ops * nl_cache_ops_associate(int protocol, int msgtype)
Associate protocol and message type to cache operations.
struct nl_cache * nl_cache_mngt_require(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
void nl_cache_ops_get(struct nl_cache_ops *ops)
Increment reference counter.
struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
Lookup message type cache association.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
struct nl_cache_ops * nl_cache_ops_associate_safe(int protocol, int msgtype)
Associate protocol and message type to cache operations.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.