summaryrefslogtreecommitdiffstats
path: root/lib/cache_mngt.c
blob: fbb702f68d8f851d2a38a56ff3582cdb3da6e77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * lib/cache_mngt.c	Cache Management
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup core
 * @defgroup cache_mngt Caching System
 *
 * Related sections in the development guide:
 * - @core_doc{core_cache, Caching System}
 *
 * @{
 *
 * Header
 * ------
 * ~~~~{.c}
 * #include <netlink/cache.h>
 * ~~~~
 */

#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>

static struct nl_cache_ops *cache_ops;
static NL_RW_LOCK(cache_ops_lock);

/**
 * @name Cache Operations Sets
 * @{
 */

struct nl_cache_ops *__nl_cache_ops_lookup(const char *name)
{
	struct nl_cache_ops *ops;

	for (ops = cache_ops; ops; ops = ops->co_next)
		if (!strcmp(ops->co_name, name))
			return ops;

	return NULL;
}

/**
 * Increment reference counter
 * @arg ops		Cache operations
 */
void nl_cache_ops_get(struct nl_cache_ops *ops)
{
	ops->co_refcnt++;
}

/**
 * Decrement reference counter
 * @arg ops		Cache operations
 */
void nl_cache_ops_put(struct nl_cache_ops *ops)
{
	ops->co_refcnt--;
}

/**
 * Lookup cache operations by name
 * @arg name		name of the cache type
 *
 * @attention This function is not safe, it does not increment the reference
 *            counter. Please use nl_cache_ops_lookup_safe().
 *
 * @return The cache operations or NULL if not found.
 */
struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
{
	struct nl_cache_ops *ops;

	nl_read_lock(&cache_ops_lock);
	ops = __nl_cache_ops_lookup(name);
	nl_read_unlock(&cache_ops_lock);

	return ops;
}

/**
 * Lookup cache operations by name
 * @arg name		name 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().
 *
 * @return The cache operations or NULL if not found.
 */
struct nl_cache_ops *nl_cache_ops_lookup_safe(const char *name)
{
	struct nl_cache_ops *ops;

	nl_write_lock(&cache_ops_lock);
	if ((ops = __nl_cache_ops_lookup(name)))
		nl_cache_ops_get(ops);
	nl_write_unlock(&cache_ops_lock);

	return ops;
}

static struct nl_cache_ops *__cache_ops_associate(int protocol, int msgtype)
{
	int i;
	struct nl_cache_ops *ops;

	for (ops = cache_ops; ops; ops = ops->co_next) {
		if (ops->co_protocol != protocol)
			continue;

		for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
			if (ops->co_msgtypes[i].mt_id == msgtype)
				return ops;
	}

	return NULL;
}

/**
 * Associate protocol and message type to cache operations
 * @arg protocol		netlink protocol
 * @arg msgtype			netlink message type
 *
 * @attention This function is not safe, it does not increment the reference
 *            counter. Please use nl_cache_ops_associate_safe().
 *
 * @see nl_cache_ops_associate_safe()
 *
 * @return The cache operations or NULL if no match found.
 */
struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
{
	struct nl_cache_ops *ops;

	nl_read_lock(&cache_ops_lock);
	ops = __cache_ops_associate(protocol, msgtype);
	nl_read_unlock(&cache_ops_lock);

	return ops;
}

/**
 * Associate protocol and message type to cache operations
 * @arg protocol		netlink protocol
 * @arg msgtype			netlink 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().
 *
 * @return The cache operations or NULL if no no match was found.
 */
struct nl_cache_ops *nl_cache_ops_associate_safe(int protocol, int msgtype)
{
	struct nl_cache_ops *ops;

	nl_write_lock(&cache_ops_lock);
	if ((ops = __cache_ops_associate(protocol, msgtype)))
		nl_cache_ops_get(ops);
	nl_write_unlock(&cache_ops_lock);

	return ops;
}

/**
 * Lookup message type cache association
 * @arg ops			cache operations
 * @arg msgtype			netlink 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.
 *
 * @return A message type association or NULL.
 */
struct nl_msgtype *nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
{
	int i;

	for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
		if (ops->co_msgtypes[i].mt_id == msgtype)
			return &ops->co_msgtypes[i];

	return NULL;
}

/* Must hold cache_ops_lock */
static struct nl_cache_ops *cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
{
	struct nl_cache_ops *ops;

	for (ops = cache_ops; ops; ops = ops->co_next)
		if (ops->co_obj_ops == obj_ops)
			return ops;

	return NULL;

}

/**
 * Call a function for each registered cache operation
 * @arg cb		Callback function to be called
 * @arg arg		User specific argument.
 */
void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
{
	struct nl_cache_ops *ops;

	nl_read_lock(&cache_ops_lock);
	for (ops = cache_ops; ops; ops = ops->co_next)
		cb(ops, arg);
	nl_read_unlock(&cache_ops_lock);
}

/**
 * Set default flags for caches of this type
 * @arg ops		Cache ops
 * @arg flags		Flags to set
 *
 * The cache operation flags will be derived to all caches allocates
 * based on this set of cache operations.
 */
void nl_cache_ops_set_flags(struct nl_cache_ops *ops, unsigned int flags)
{
	ops->co_flags |= flags;
}

/**
 * Register a set of cache operations
 * @arg ops		cache operations
 *
 * Called by users of caches to announce the avaibility of
 * a certain cache type.
 *
 * @return 0 on success or a negative error code.
 */
int nl_cache_mngt_register(struct nl_cache_ops *ops)
{
	if (!ops->co_name || !ops->co_obj_ops)
		return -NLE_INVAL;

	nl_write_lock(&cache_ops_lock);
	if (__nl_cache_ops_lookup(ops->co_name)) {
		nl_write_unlock(&cache_ops_lock);
		return -NLE_EXIST;
	}

	ops->co_refcnt = 0;
	ops->co_next = cache_ops;
	cache_ops = ops;
	nl_write_unlock(&cache_ops_lock);

	NL_DBG(1, "Registered cache operations %s\n", ops->co_name);

	return 0;
}

/**
 * Unregister a set of cache operations
 * @arg ops		cache 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()
 *
 * @return 0 on success or a negative error code
 */
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
{
	struct nl_cache_ops *t, **tp;
	int err = 0;

	nl_write_lock(&cache_ops_lock);

	if (ops->co_refcnt > 0) {
		err = -NLE_BUSY;
		goto errout;
	}

	for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
		if (t == ops)
			break;

	if (!t) {
		err = -NLE_NOCACHE;
		goto errout;
	}

	NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);

	*tp = t->co_next;
errout:
	nl_write_unlock(&cache_ops_lock);

	return err;
}

/** @} */

/**
 * @name Global Cache Provisioning/Requiring
 * @{
 */

/**
 * Provide a cache for global use
 * @arg cache		cache 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.
 */
void nl_cache_mngt_provide(struct nl_cache *cache)
{
	struct nl_cache_ops *ops;

	nl_write_lock(&cache_ops_lock);

	ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
	if (!ops)
		BUG();
	else {
		nl_cache_get(cache);

		/*
		 * Hold a reference to the cache operations to ensure the
		 * ops don't go away while we use it to store the cache pointer.
		 */
		if (!ops->co_major_cache)
			nl_cache_ops_get(ops);

		ops->co_major_cache = cache;
	}

	nl_write_unlock(&cache_ops_lock);
}

/**
 * Unprovide a cache for global use
 * @arg cache		cache 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.
 */
void nl_cache_mngt_unprovide(struct nl_cache *cache)
{
	struct nl_cache_ops *ops;

	nl_write_lock(&cache_ops_lock);

	ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
	if (!ops)
		BUG();
	else if (ops->co_major_cache == cache) {
		nl_cache_free(ops->co_major_cache);
		nl_cache_ops_put(ops);
		ops->co_major_cache = NULL;
	}

	nl_write_unlock(&cache_ops_lock);
}

struct nl_cache *__nl_cache_mngt_require(const char *name)
{
	struct nl_cache_ops *ops;
	struct nl_cache *cache = NULL;

	ops = nl_cache_ops_lookup_safe(name);
	if (ops) {
		cache = ops->co_major_cache;
		nl_cache_ops_put(ops);
	}
	
	return cache;
}

/**
 * Return cache previously provided via nl_cache_mngt_provide()
 * @arg name		Name of cache to lookup
 *
 * @attention This function is not safe, it does not increment the reference
 *            counter. Please use nl_cache_mngt_require_safe().
 *
 * @see nl_cache_mngt_require_safe()
 *
 * @return Pointer to cache or NULL if none registered
 */
struct nl_cache *nl_cache_mngt_require(const char *name)
{
	struct nl_cache *cache;

	if (!(cache = __nl_cache_mngt_require(name)))
		NL_DBG(1, "Application BUG: Your application must "
		       "call nl_cache_mngt_provide() and\nprovide a valid "
		       "%s cache to be used for internal lookups.\nSee the "
		       " API documentation for more details.\n", name);
	
	return cache;
}

/**
 * Return cache previously provided via nl_cache_mngt_provide()
 * @arg name		Name of cache to lookup
 *
 * @note The reference counter of the returned cache is incremented
 *       and must be decremented after use with nl_cache_put().
 *
 * @return Pointer to cache or NULL if none registered
 */
struct nl_cache *nl_cache_mngt_require_safe(const char *name)
{
	struct nl_cache *cache;

	if ((cache = nl_cache_mngt_require(name)))
		nl_cache_get(cache);
	
	return cache;
}

/** @} */

/** @} */