2 * lib/cache_mngt.c Cache Management
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
13 * @defgroup cache_mngt Caching
17 #include <netlink-local.h>
18 #include <netlink/netlink.h>
19 #include <netlink/cache.h>
20 #include <netlink/utils.h>
22 static struct nl_cache_ops *cache_ops;
25 * @name Cache Operations Sets
30 * Lookup the set cache operations of a certain cache type
31 * @arg name name of the cache type
33 * @return The cache operations or NULL if no operations
34 * have been registered under the specified name.
36 struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
38 struct nl_cache_ops *ops;
40 for (ops = cache_ops; ops; ops = ops->co_next)
41 if (!strcmp(ops->co_name, name))
48 * Associate a message type to a set of cache operations
49 * @arg protocol netlink protocol
50 * @arg msgtype netlink message type
52 * Associates the specified netlink message type with
53 * a registered set of cache operations.
55 * @return The cache operations or NULL if no association
58 struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
61 struct nl_cache_ops *ops;
63 for (ops = cache_ops; ops; ops = ops->co_next)
64 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
65 if (ops->co_msgtypes[i].mt_id == msgtype &&
66 ops->co_protocol == protocol)
73 * Lookup message type cache association
74 * @arg ops cache operations
75 * @arg msgtype netlink message type
77 * Searches for a matching message type association ing the specified
80 * @return A message type association or NULL.
82 struct nl_msgtype *nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
86 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
87 if (ops->co_msgtypes[i].mt_id == msgtype)
88 return &ops->co_msgtypes[i];
93 static struct nl_cache_ops *cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
95 struct nl_cache_ops *ops;
97 for (ops = cache_ops; ops; ops = ops->co_next)
98 if (ops->co_obj_ops == obj_ops)
106 * Call a function for each registered cache operation
107 * @arg cb Callback function to be called
108 * @arg arg User specific argument.
110 void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
112 struct nl_cache_ops *ops;
114 for (ops = cache_ops; ops; ops = ops->co_next)
119 * Register a set of cache operations
120 * @arg ops cache operations
122 * Called by users of caches to announce the avaibility of
123 * a certain cache type.
125 * @return 0 on success or a negative error code.
127 int nl_cache_mngt_register(struct nl_cache_ops *ops)
130 return nl_error(EINVAL, "No cache name specified");
132 if (!ops->co_obj_ops)
133 return nl_error(EINVAL, "No obj cache ops specified");
135 if (nl_cache_ops_lookup(ops->co_name))
136 return nl_error(EEXIST, "Cache operations already exist");
138 ops->co_next = cache_ops;
141 NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
147 * Unregister a set of cache operations
148 * @arg ops cache operations
150 * Called by users of caches to announce a set of
151 * cache operations is no longer available. The
152 * specified cache operations must have been registered
153 * previously using nl_cache_mngt_register()
155 * @return 0 on success or a negative error code
157 int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
159 struct nl_cache_ops *t, **tp;
161 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
166 return nl_error(ENOENT, "No such cache operations");
168 NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
177 * @name Global Cache Provisioning/Requiring
182 * Provide a cache for global use
183 * @arg cache cache to provide
185 * Offers the specified cache to be used by other modules.
186 * Only one cache per type may be shared at a time,
187 * a previsouly provided caches will be overwritten.
189 void nl_cache_mngt_provide(struct nl_cache *cache)
191 struct nl_cache_ops *ops;
193 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
197 ops->co_major_cache = cache;
201 * Unprovide a cache for global use
202 * @arg cache cache to unprovide
204 * Cancels the offer to use a cache globally. The
205 * cache will no longer be returned via lookups but
206 * may still be in use.
208 void nl_cache_mngt_unprovide(struct nl_cache *cache)
210 struct nl_cache_ops *ops;
212 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
215 else if (ops->co_major_cache == cache)
216 ops->co_major_cache = NULL;
220 * Demand the use of a global cache
221 * @arg name name of the required object type
223 * Trys to find a cache of the specified type for global
226 * @return A cache provided by another subsystem of the
227 * specified type marked to be available.
229 struct nl_cache *nl_cache_mngt_require(const char *name)
231 struct nl_cache_ops *ops;
233 ops = nl_cache_ops_lookup(name);
234 if (!ops || !ops->co_major_cache) {
235 fprintf(stderr, "Application BUG: Your application must "
236 "call nl_cache_mngt_provide() and\nprovide a valid "
237 "%s cache to be used for internal lookups.\nSee the "
238 " API documentation for more details.\n", name);
243 return ops->co_major_cache;