29 #include <netlink-private/netlink.h> 30 #include <netlink/netlink.h> 31 #include <netlink/cache.h> 32 #include <netlink/utils.h> 34 static struct nl_cache_ops *cache_ops;
35 static NL_RW_LOCK(cache_ops_lock);
42 static struct nl_cache_ops *__nl_cache_ops_lookup(
const char *name)
44 struct nl_cache_ops *ops;
46 for (ops = cache_ops; ops; ops = ops->co_next)
47 if (!strcmp(ops->co_name, name))
82 struct nl_cache_ops *ops;
84 nl_read_lock(&cache_ops_lock);
85 ops = __nl_cache_ops_lookup(name);
86 nl_read_unlock(&cache_ops_lock);
102 struct nl_cache_ops *ops;
104 nl_write_lock(&cache_ops_lock);
105 if ((ops = __nl_cache_ops_lookup(name)))
107 nl_write_unlock(&cache_ops_lock);
112 static struct nl_cache_ops *__cache_ops_associate(
int protocol,
int msgtype)
115 struct nl_cache_ops *ops;
117 for (ops = cache_ops; ops; ops = ops->co_next) {
118 if (ops->co_protocol != protocol)
121 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
122 if (ops->co_msgtypes[i].mt_id == msgtype)
143 struct nl_cache_ops *ops;
145 nl_read_lock(&cache_ops_lock);
146 ops = __cache_ops_associate(protocol, msgtype);
147 nl_read_unlock(&cache_ops_lock);
167 struct nl_cache_ops *ops;
169 nl_write_lock(&cache_ops_lock);
170 if ((ops = __cache_ops_associate(protocol, msgtype)))
172 nl_write_unlock(&cache_ops_lock);
194 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
195 if (ops->co_msgtypes[i].mt_id == msgtype)
196 return &ops->co_msgtypes[i];
202 static struct nl_cache_ops *cache_ops_lookup_for_obj(
struct nl_object_ops *obj_ops)
204 struct nl_cache_ops *ops;
206 for (ops = cache_ops; ops; ops = ops->co_next)
207 if (ops->co_obj_ops == obj_ops)
221 struct nl_cache_ops *ops;
223 nl_read_lock(&cache_ops_lock);
224 for (ops = cache_ops; ops; ops = ops->co_next)
226 nl_read_unlock(&cache_ops_lock);
239 nl_write_lock(&cache_ops_lock);
240 ops->co_flags |= flags;
241 nl_write_unlock(&cache_ops_lock);
255 if (!ops->co_name || !ops->co_obj_ops)
259 BUG_ON (ops->co_obj_ops->oo_keygen && !ops->co_obj_ops->oo_compare);
261 nl_write_lock(&cache_ops_lock);
262 if (__nl_cache_ops_lookup(ops->co_name)) {
263 nl_write_unlock(&cache_ops_lock);
268 ops->co_next = cache_ops;
270 nl_write_unlock(&cache_ops_lock);
272 NL_DBG(1,
"Registered cache operations %s\n", ops->co_name);
290 struct nl_cache_ops *t, **tp;
293 nl_write_lock(&cache_ops_lock);
295 if (ops->co_refcnt > 0) {
300 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
309 NL_DBG(1,
"Unregistered cache operations %s\n", ops->co_name);
313 nl_write_unlock(&cache_ops_lock);
335 struct nl_cache_ops *ops;
337 nl_write_lock(&cache_ops_lock);
339 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
349 if (!ops->co_major_cache)
352 ops->co_major_cache = cache;
355 nl_write_unlock(&cache_ops_lock);
368 struct nl_cache_ops *ops;
370 nl_write_lock(&cache_ops_lock);
372 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
375 else if (ops->co_major_cache == cache) {
378 ops->co_major_cache = NULL;
381 nl_write_unlock(&cache_ops_lock);
384 struct nl_cache *__nl_cache_mngt_require(
const char *name)
386 struct nl_cache_ops *ops;
387 struct nl_cache *cache = NULL;
391 cache = ops->co_major_cache;
411 struct nl_cache *cache;
413 if (!(cache = __nl_cache_mngt_require(name)))
414 NL_DBG(1,
"Application BUG: Your application must " 415 "call nl_cache_mngt_provide() and\nprovide a valid " 416 "%s cache to be used for internal lookups.\nSee the " 417 " API documentation for more details.\n", name);
433 struct nl_cache *cache;
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
void nl_cache_ops_get(struct nl_cache_ops *ops)
Increment reference counter.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of 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_safe(const char *name)
Lookup cache operations by name.
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.
void nl_cache_ops_foreach(void(*cb)(struct nl_cache_ops *, void *), void *arg)
Call a function for each registered cache operation.
struct nl_cache_ops * nl_cache_ops_associate(int protocol, int msgtype)
Associate protocol and message type to cache operations.
struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
Lookup message type cache association.
struct nl_cache_ops * nl_cache_ops_associate_safe(int protocol, int msgtype)
Associate protocol and message type to cache operations.