28 #include <netlink-private/netlink.h>
29 #include <netlink/netlink.h>
30 #include <netlink/cache.h>
31 #include <netlink/utils.h>
33 static struct nl_cache_ops *cache_ops;
34 static NL_RW_LOCK(cache_ops_lock);
41 struct nl_cache_ops *__nl_cache_ops_lookup(
const char *name)
43 struct nl_cache_ops *ops;
45 for (ops = cache_ops; ops; ops = ops->co_next)
46 if (!strcmp(ops->co_name, name))
81 struct nl_cache_ops *ops;
83 nl_read_lock(&cache_ops_lock);
84 ops = __nl_cache_ops_lookup(name);
85 nl_read_unlock(&cache_ops_lock);
101 struct nl_cache_ops *ops;
103 nl_write_lock(&cache_ops_lock);
104 if ((ops = __nl_cache_ops_lookup(name)))
106 nl_write_unlock(&cache_ops_lock);
111 static struct nl_cache_ops *__cache_ops_associate(
int protocol,
int msgtype)
114 struct nl_cache_ops *ops;
116 for (ops = cache_ops; ops; ops = ops->co_next) {
117 if (ops->co_protocol != protocol)
120 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
121 if (ops->co_msgtypes[i].mt_id == msgtype)
142 struct nl_cache_ops *ops;
144 nl_read_lock(&cache_ops_lock);
145 ops = __cache_ops_associate(protocol, msgtype);
146 nl_read_unlock(&cache_ops_lock);
166 struct nl_cache_ops *ops;
168 nl_write_lock(&cache_ops_lock);
169 if ((ops = __cache_ops_associate(protocol, msgtype)))
171 nl_write_unlock(&cache_ops_lock);
193 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
194 if (ops->co_msgtypes[i].mt_id == msgtype)
195 return &ops->co_msgtypes[i];
201 static struct nl_cache_ops *cache_ops_lookup_for_obj(
struct nl_object_ops *obj_ops)
203 struct nl_cache_ops *ops;
205 for (ops = cache_ops; ops; ops = ops->co_next)
206 if (ops->co_obj_ops == obj_ops)
220 struct nl_cache_ops *ops;
222 nl_read_lock(&cache_ops_lock);
223 for (ops = cache_ops; ops; ops = ops->co_next)
225 nl_read_unlock(&cache_ops_lock);
238 nl_write_lock(&cache_ops_lock);
239 ops->co_flags |= flags;
240 nl_write_unlock(&cache_ops_lock);
254 if (!ops->co_name || !ops->co_obj_ops)
257 nl_write_lock(&cache_ops_lock);
258 if (__nl_cache_ops_lookup(ops->co_name)) {
259 nl_write_unlock(&cache_ops_lock);
264 ops->co_next = cache_ops;
266 nl_write_unlock(&cache_ops_lock);
268 NL_DBG(1,
"Registered cache operations %s\n", ops->co_name);
286 struct nl_cache_ops *t, **tp;
289 nl_write_lock(&cache_ops_lock);
291 if (ops->co_refcnt > 0) {
296 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
305 NL_DBG(1,
"Unregistered cache operations %s\n", ops->co_name);
309 nl_write_unlock(&cache_ops_lock);
331 struct nl_cache_ops *ops;
333 nl_write_lock(&cache_ops_lock);
335 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
345 if (!ops->co_major_cache)
348 ops->co_major_cache = cache;
351 nl_write_unlock(&cache_ops_lock);
364 struct nl_cache_ops *ops;
366 nl_write_lock(&cache_ops_lock);
368 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
371 else if (ops->co_major_cache == cache) {
374 ops->co_major_cache = NULL;
377 nl_write_unlock(&cache_ops_lock);
380 struct nl_cache *__nl_cache_mngt_require(
const char *name)
382 struct nl_cache_ops *ops;
383 struct nl_cache *cache = NULL;
387 cache = ops->co_major_cache;
407 struct nl_cache *cache;
409 if (!(cache = __nl_cache_mngt_require(name)))
410 NL_DBG(1,
"Application BUG: Your application must "
411 "call nl_cache_mngt_provide() and\nprovide a valid "
412 "%s cache to be used for internal lookups.\nSee the "
413 " API documentation for more details.\n", name);
429 struct nl_cache *cache;