libnl  1.1.4
Modules
Caching

Modules

 Cache
 
 Manager
 Helps keeping caches up to date.
 

Cache Operations Sets

struct nl_cache_ops__nl_cache_ops_lookup (const char *name)
 
void nl_cache_ops_get (struct nl_cache_ops *ops)
 Increment reference counter. More...
 
void nl_cache_ops_put (struct nl_cache_ops *ops)
 Decrement reference counter. More...
 
struct nl_cache_opsnl_cache_ops_lookup (const char *name)
 Lookup cache operations by name. More...
 
struct nl_cache_opsnl_cache_ops_lookup_safe (const char *name)
 Lookup cache operations by name. More...
 
struct nl_cache_opsnl_cache_ops_associate (int protocol, int msgtype)
 Associate protocol and message type to cache operations. More...
 
struct nl_cache_opsnl_cache_ops_associate_safe (int protocol, int msgtype)
 Associate protocol and message type to cache operations. More...
 
struct nl_msgtypenl_msgtype_lookup (struct nl_cache_ops *ops, int msgtype)
 Lookup message type cache association. More...
 
void nl_cache_ops_foreach (void(*cb)(struct nl_cache_ops *, void *), void *arg)
 Call a function for each registered cache operation. More...
 
int nl_cache_mngt_register (struct nl_cache_ops *ops)
 Register a set of cache operations. More...
 
int nl_cache_mngt_unregister (struct nl_cache_ops *ops)
 Unregister a set of cache operations. More...
 

Global Cache Provisioning/Requiring

void nl_cache_mngt_provide (struct nl_cache *cache)
 Provide a cache for global use. More...
 
void nl_cache_mngt_unprovide (struct nl_cache *cache)
 Unprovide a cache for global use. More...
 
struct nl_cache * nl_cache_mngt_require (const char *name)
 Demand the use of a global cache. More...
 

Detailed Description

Function Documentation

◆ nl_cache_ops_get()

void nl_cache_ops_get ( struct nl_cache_ops ops)
Parameters
opsCache operations

Definition at line 45 of file cache_mngt.c.

References nl_cache_ops::co_refcnt.

46 {
47  ops->co_refcnt++;
48 }
unsigned int co_refcnt
Reference counter.
Definition: cache-api.h:193

◆ nl_cache_ops_put()

void nl_cache_ops_put ( struct nl_cache_ops ops)
Parameters
opsCache operations

Definition at line 54 of file cache_mngt.c.

References nl_cache_ops::co_refcnt.

55 {
56  ops->co_refcnt--;
57 }
unsigned int co_refcnt
Reference counter.
Definition: cache-api.h:193

◆ nl_cache_ops_lookup()

struct nl_cache_ops* nl_cache_ops_lookup ( const char *  name)
Parameters
namename of the cache type
Attention
This function is not safe, it does not increment the reference counter. Please use nl_cache_ops_lookup_safe().
Returns
The cache operations or NULL if not found.

Definition at line 68 of file cache_mngt.c.

Referenced by nl_cache_alloc_name(), nl_cache_mngr_add(), nl_cache_mngt_require(), and nl_object_alloc_name().

69 {
70  struct nl_cache_ops *ops;
71 
72  nl_read_lock(&cache_ops_lock);
73  ops = __nl_cache_ops_lookup(name);
74  nl_read_unlock(&cache_ops_lock);
75 
76  return ops;
77 }
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_ops_lookup_safe()

struct nl_cache_ops* nl_cache_ops_lookup_safe ( const char *  name)
Parameters
namename of the cache type
Note
The reference counter of the returned cache operation is incremented and must be decremented after use with nl_cache_ops_put().
Returns
The cache operations or NULL if not found.

Definition at line 88 of file cache_mngt.c.

89 {
90  struct nl_cache_ops *ops;
91 
92  nl_write_lock(&cache_ops_lock);
93  if ((ops = __nl_cache_ops_lookup(name)))
94  nl_cache_ops_get(ops);
95  nl_write_unlock(&cache_ops_lock);
96 
97  return ops;
98 }
void nl_cache_ops_get(struct nl_cache_ops *ops)
Increment reference counter.
Definition: cache_mngt.c:45
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_ops_associate()

struct nl_cache_ops* nl_cache_ops_associate ( int  protocol,
int  msgtype 
)
Parameters
protocolnetlink protocol
msgtypenetlink message type
Attention
This function is not safe, it does not increment the reference counter. Please use nl_cache_ops_associate_safe().
See also
nl_cache_ops_associate_safe()
Returns
The cache operations or NULL if no match found.

Definition at line 129 of file cache_mngt.c.

130 {
131  struct nl_cache_ops *ops;
132 
133  nl_read_lock(&cache_ops_lock);
134  ops = __cache_ops_associate(protocol, msgtype);
135  nl_read_unlock(&cache_ops_lock);
136 
137  return ops;
138 }
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_ops_associate_safe()

struct nl_cache_ops* nl_cache_ops_associate_safe ( int  protocol,
int  msgtype 
)
Parameters
protocolnetlink protocol
msgtypenetlink message type

Searches the registered cache operations for a matching protocol and message type.

Note
The reference counter of the returned cache operation is incremented and must be decremented after use with nl_cache_ops_put().
Returns
The cache operations or NULL if no no match was found.

Definition at line 153 of file cache_mngt.c.

154 {
155  struct nl_cache_ops *ops;
156 
157  nl_write_lock(&cache_ops_lock);
158  if ((ops = __cache_ops_associate(protocol, msgtype)))
159  nl_cache_ops_get(ops);
160  nl_write_unlock(&cache_ops_lock);
161 
162  return ops;
163 }
void nl_cache_ops_get(struct nl_cache_ops *ops)
Increment reference counter.
Definition: cache_mngt.c:45
Cache Operations.
Definition: cache-api.h:163

◆ nl_msgtype_lookup()

struct nl_msgtype* nl_msgtype_lookup ( struct nl_cache_ops ops,
int  msgtype 
)
Parameters
opscache operations
msgtypenetlink message type

Searches for a matching message type association ing the specified cache operations.

Attention
The guranteed lifetime of the returned message type is bound to the lifetime of the underlying cache operations.
Returns
A message type association or NULL.

Definition at line 178 of file cache_mngt.c.

References nl_msgtype::mt_id.

179 {
180  int i;
181 
182  for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
183  if (ops->co_msgtypes[i].mt_id == msgtype)
184  return &ops->co_msgtypes[i];
185 
186  return NULL;
187 }
int mt_id
Netlink message type.
Definition: cache-api.h:131

◆ nl_cache_ops_foreach()

void nl_cache_ops_foreach ( void(*)(struct nl_cache_ops *, void *)  cb,
void *  arg 
)
Parameters
cbCallback function to be called
argUser specific argument.

Definition at line 207 of file cache_mngt.c.

208 {
209  struct nl_cache_ops *ops;
210 
211  nl_read_lock(&cache_ops_lock);
212  for (ops = cache_ops; ops; ops = ops->co_next)
213  cb(ops, arg);
214  nl_read_unlock(&cache_ops_lock);
215 }
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_mngt_register()

int nl_cache_mngt_register ( struct nl_cache_ops ops)
Parameters
opscache operations

Called by users of caches to announce the avaibility of a certain cache type.

Returns
0 on success or a negative error code.

Definition at line 226 of file cache_mngt.c.

227 {
228  if (!ops->co_name)
229  return nl_error(EINVAL, "No cache name specified");
230 
231  if (!ops->co_obj_ops)
232  return nl_error(EINVAL, "No obj cache ops specified");
233 
234  nl_write_lock(&cache_ops_lock);
235  if (__nl_cache_ops_lookup(ops->co_name)) {
236  nl_write_unlock(&cache_ops_lock);
237  return nl_error(EEXIST, "Cache operations already exist");
238  }
239 
240  ops->co_refcnt = 0;
241  ops->co_next = cache_ops;
242  cache_ops = ops;
243  nl_write_unlock(&cache_ops_lock);
244 
245  NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
246 
247  return 0;
248 }
unsigned int co_refcnt
Reference counter.
Definition: cache-api.h:193

◆ nl_cache_mngt_unregister()

int nl_cache_mngt_unregister ( struct nl_cache_ops ops)
Parameters
opscache operations

Called by users of caches to announce a set of cache operations is no longer available. The specified cache operations must have been registered previously using nl_cache_mngt_register()

Returns
0 on success or a negative error code

Definition at line 261 of file cache_mngt.c.

References nl_cache_ops::co_refcnt.

Referenced by genl_unregister().

262 {
263  struct nl_cache_ops *t, **tp;
264 
265  nl_write_lock(&cache_ops_lock);
266 
267  if (ops->co_refcnt > 0) {
268  nl_write_unlock(&cache_ops_lock);
269  return nl_error(EBUSY, "Cache operations busy");
270  }
271 
272  for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
273  if (t == ops)
274  break;
275 
276  if (!t) {
277  nl_write_unlock(&cache_ops_lock);
278  return nl_error(ENOENT, "No such cache operations");
279  }
280 
281  NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
282 
283  *tp = t->co_next;
284  nl_write_unlock(&cache_ops_lock);
285 
286  return 0;
287 }
unsigned int co_refcnt
Reference counter.
Definition: cache-api.h:193
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_mngt_provide()

void nl_cache_mngt_provide ( struct nl_cache *  cache)
Parameters
cachecache to provide

Offers the specified cache to be used by other modules. Only one cache per type may be shared at a time, a previsouly provided caches will be overwritten.

Definition at line 304 of file cache_mngt.c.

Referenced by nl_cache_mngr_add().

305 {
306  struct nl_cache_ops *ops;
307 
308  nl_write_lock(&cache_ops_lock);
309 
310  ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
311  if (!ops)
312  BUG();
313  else {
314  nl_cache_get(cache);
315  ops->co_major_cache = cache;
316  }
317 
318  nl_write_unlock(&cache_ops_lock);
319 }
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
Definition: cache.c:264
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_mngt_unprovide()

void nl_cache_mngt_unprovide ( struct nl_cache *  cache)
Parameters
cachecache to unprovide

Cancels the offer to use a cache globally. The cache will no longer be returned via lookups but may still be in use.

Definition at line 329 of file cache_mngt.c.

330 {
331  struct nl_cache_ops *ops;
332 
333  nl_write_lock(&cache_ops_lock);
334 
335  ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
336  if (!ops)
337  BUG();
338  else if (ops->co_major_cache == cache) {
339  nl_cache_free(ops->co_major_cache);
340  ops->co_major_cache = NULL;
341  }
342 
343  nl_write_unlock(&cache_ops_lock);
344 }
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:277
Cache Operations.
Definition: cache-api.h:163

◆ nl_cache_mngt_require()

struct nl_cache* nl_cache_mngt_require ( const char *  name)
Parameters
namename of the required object type

Trys to find a cache of the specified type for global use.

Returns
A cache provided by another subsystem of the specified type marked to be available.

Definition at line 356 of file cache_mngt.c.

References nl_cache_ops_lookup().

357 {
358  struct nl_cache_ops *ops;
359 
360  ops = nl_cache_ops_lookup(name);
361  if (!ops || !ops->co_major_cache) {
362  fprintf(stderr, "Application BUG: Your application must "
363  "call nl_cache_mngt_provide() and\nprovide a valid "
364  "%s cache to be used for internal lookups.\nSee the "
365  " API documentation for more details.\n", name);
366 
367  return NULL;
368  }
369 
370  return ops->co_major_cache;
371 }
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
Definition: cache_mngt.c:68
Cache Operations.
Definition: cache-api.h:163