Cache
[Caching]

Modules

 Object
 Cache Implementation

Access Functions



int nl_cache_nitems (struct nl_cache *cache)
 Return the number of items in the cache.
int nl_cache_nitems_filter (struct nl_cache *cache, struct nl_object *filter)
 Return the number of items matching a filter in the cache.
int nl_cache_is_empty (struct nl_cache *cache)
 Returns true if the cache is empty.
struct nl_cache_opsnl_cache_get_ops (struct nl_cache *cache)
 Return the operations set of the cache.
struct nl_object * nl_cache_get_first (struct nl_cache *cache)
 Return the first element in the cache.
struct nl_object * nl_cache_get_last (struct nl_cache *cache)
 Return the last element in the cache.
struct nl_object * nl_cache_get_next (struct nl_object *obj)
 Return the next element in the cache.
struct nl_object * nl_cache_get_prev (struct nl_object *obj)
 Return the previous element in the cache.

Cache Creation/Deletion



struct nl_cache * nl_cache_alloc (struct nl_cache_ops *ops)
 Allocate an empty cache.
struct nl_cache * nl_cache_alloc_name (const char *kind)
 Allocate an empty cache based on type name.
struct nl_cache * nl_cache_subset (struct nl_cache *orig, struct nl_object *filter)
 Allocate a new cache containing a subset of a cache.
void nl_cache_clear (struct nl_cache *cache)
 Clear a cache.
void nl_cache_get (struct nl_cache *cache)
 Increase reference counter of cache.
void nl_cache_free (struct nl_cache *cache)
 Free a cache.

Cache Modifications



int nl_cache_add (struct nl_cache *cache, struct nl_object *obj)
 Add object to a cache.
int nl_cache_move (struct nl_cache *cache, struct nl_object *obj)
 Move object from one cache to another.
void nl_cache_remove (struct nl_object *obj)
 Removes an object from a cache.
struct nl_object * nl_cache_search (struct nl_cache *cache, struct nl_object *needle)
 Search for an object in a cache.

Synchronization



int nl_cache_request_full_dump (struct nl_handle *handle, struct nl_cache *cache)
 Request a full dump from the kernel to fill a cache.
int __cache_pickup (struct nl_handle *handle, struct nl_cache *cache, struct nl_parser_param *param)
int nl_cache_pickup (struct nl_handle *handle, struct nl_cache *cache)
 Pickup a netlink dump response and put it into a cache.
int nl_cache_include (struct nl_cache *cache, struct nl_object *obj, change_func_t change_cb)
int nl_cache_resync (struct nl_handle *handle, struct nl_cache *cache, change_func_t change_cb)

Parsing



int nl_cache_parse_and_add (struct nl_cache *cache, struct nl_msg *msg)
 Parse a netlink message and add it to the cache.
int nl_cache_refill (struct nl_handle *handle, struct nl_cache *cache)
 (Re)fill a cache with the contents in the kernel.

Utillities



void nl_cache_mark_all (struct nl_cache *cache)
 Mark all objects in a cache.

Dumping



void nl_cache_dump (struct nl_cache *cache, struct nl_dump_params *params)
 Dump all elements of a cache.
void nl_cache_dump_filter (struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
 Dump all elements of a cache (filtered).

Iterators



void nl_cache_foreach (struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
 Call a callback on each element of the cache.
void nl_cache_foreach_filter (struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
 Call a callback on each element of the cache (filtered).

Detailed Description

   Cache Management             |    | Type Specific Cache Operations
                                      
                                |    | +----------------+ +------------+
                                       | request update | | msg_parser |
                                |    | +----------------+ +------------+
                                     +- - - - -^- - - - - - - -^- -|- - - -
    nl_cache_update:            |              |               |   |
          1) --------- co_request_update ------+               |   |
                                |                              |   |
          2) destroy old cache     +----------- pp_cb ---------|---+
                                |  |                           |
          3) ---------- nl_recvmsgs ----------+   +- cb_valid -+
             +--------------+   |  |          |   |
             | nl_cache_add |<-----+   + - - -v- -|- - - - - - - - - - -
             +--------------+   |      | +-------------+
                                         | nl_recvmsgs |
                                |      | +-----|-^-----+
                                           +---v-|---+
                                |      |   | nl_recv |
                                           +---------+
                                |      |                 Core Netlink

Function Documentation

int nl_cache_nitems ( struct nl_cache *  cache  ) 
Parameters:
cache cache handle

Definition at line 58 of file cache.c.

00059 {
00060         return cache->c_nitems;
00061 }

int nl_cache_nitems_filter ( struct nl_cache *  cache,
struct nl_object *  filter 
)
Parameters:
cache Cache object.
filter Filter object.

Definition at line 68 of file cache.c.

References nl_object_match_filter().

00069 {
00070         struct nl_object *obj;
00071         int nitems = 0;
00072 
00073         if (cache->c_ops == NULL)
00074                 BUG();
00075 
00076         nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00077                 if (filter && !nl_object_match_filter(obj, filter))
00078                         continue;
00079 
00080                 nitems++;
00081         }
00082 
00083         return nitems;
00084 }

int nl_cache_is_empty ( struct nl_cache *  cache  ) 
Parameters:
cache Cache to check
Returns:
true if the cache is empty, otherwise false is returned.

Definition at line 91 of file cache.c.

00092 {
00093         return nl_list_empty(&cache->c_items);
00094 }

struct nl_cache_ops* nl_cache_get_ops ( struct nl_cache *  cache  )  [read]
Parameters:
cache cache handle

Definition at line 100 of file cache.c.

00101 {
00102         return cache->c_ops;
00103 }

struct nl_object* nl_cache_get_first ( struct nl_cache *  cache  )  [read]
Parameters:
cache cache handle

Definition at line 109 of file cache.c.

00110 {
00111         if (nl_list_empty(&cache->c_items))
00112                 return NULL;
00113 
00114         return nl_list_entry(cache->c_items.next,
00115                              struct nl_object, ce_list);
00116 }

struct nl_object* nl_cache_get_last ( struct nl_cache *  cache  )  [read]
Parameters:
cache cache handle

Definition at line 122 of file cache.c.

00123 {
00124         if (nl_list_empty(&cache->c_items))
00125                 return NULL;
00126 
00127         return nl_list_entry(cache->c_items.prev,
00128                              struct nl_object, ce_list);
00129 }

struct nl_object* nl_cache_get_next ( struct nl_object *  obj  )  [read]
Parameters:
obj current object

Definition at line 135 of file cache.c.

00136 {
00137         if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
00138                 return NULL;
00139         else
00140                 return nl_list_entry(obj->ce_list.next,
00141                                      struct nl_object, ce_list);
00142 }

struct nl_object* nl_cache_get_prev ( struct nl_object *  obj  )  [read]
Parameters:
obj current object

Definition at line 148 of file cache.c.

00149 {
00150         if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
00151                 return NULL;
00152         else
00153                 return nl_list_entry(obj->ce_list.prev,
00154                                      struct nl_object, ce_list);
00155 }

struct nl_cache* nl_cache_alloc ( struct nl_cache_ops ops  )  [read]
Parameters:
ops cache operations to base the cache on
Returns:
A newly allocated and initialized cache.

Definition at line 170 of file cache.c.

Referenced by flnl_result_alloc_cache(), nfnl_ct_alloc_cache(), nl_cache_alloc_name(), nl_cache_mngr_add(), nl_cache_subset(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), rtnl_qdisc_alloc_cache(), rtnl_route_alloc_cache(), and rtnl_rule_alloc_cache_by_family().

00171 {
00172         struct nl_cache *cache;
00173 
00174         cache = calloc(1, sizeof(*cache));
00175         if (!cache) {
00176                 nl_errno(ENOMEM);
00177                 return NULL;
00178         }
00179 
00180         nl_init_list_head(&cache->c_items);
00181         cache->c_ops = ops;
00182         cache->c_refcnt = 1;
00183 
00184         NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
00185 
00186         return cache;
00187 }

struct nl_cache* nl_cache_alloc_name ( const char *  kind  )  [read]
Parameters:
kind Name of cache type
Returns:
A newly allocated and initialized cache.

Definition at line 194 of file cache.c.

References nl_cache_alloc(), and nl_cache_ops_lookup().

00195 {
00196         struct nl_cache_ops *ops;
00197 
00198         ops = nl_cache_ops_lookup(kind);
00199         if (!ops) {
00200                 nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
00201                 return NULL;
00202         }
00203 
00204         return nl_cache_alloc(ops);
00205 }

struct nl_cache* nl_cache_subset ( struct nl_cache *  orig,
struct nl_object *  filter 
) [read]
Parameters:
orig Original cache to be based on
filter Filter defining the subset to be filled into new cache
Returns:
A newly allocated cache or NULL.

Definition at line 213 of file cache.c.

References nl_cache_add(), nl_cache_alloc(), and nl_object_match_filter().

00215 {
00216         struct nl_cache *cache;
00217         struct nl_object *obj;
00218 
00219         if (!filter)
00220                 BUG();
00221 
00222         cache = nl_cache_alloc(orig->c_ops);
00223         if (!cache)
00224                 return NULL;
00225 
00226         nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
00227                 if (!nl_object_match_filter(obj, filter))
00228                         continue;
00229 
00230                 nl_cache_add(cache, obj);
00231         }
00232 
00233         return cache;
00234 }

void nl_cache_clear ( struct nl_cache *  cache  ) 
Parameters:
cache cache to clear

Removes all elements of a cache.

Definition at line 242 of file cache.c.

References nl_cache_remove().

Referenced by nl_cache_refill().

00243 {
00244         struct nl_object *obj, *tmp;
00245 
00246         NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
00247 
00248         nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
00249                 nl_cache_remove(obj);
00250 }

void nl_cache_get ( struct nl_cache *  cache  ) 
Parameters:
cache Cache

Definition at line 264 of file cache.c.

Referenced by nl_cache_mngt_provide().

00265 {
00266         cache->c_refcnt++;
00267 }

void nl_cache_free ( struct nl_cache *  cache  ) 
Parameters:
cache Cache to free.

Removes all elements of a cache and frees all memory.

Note:
Use this function if you are working with allocated caches.

Definition at line 277 of file cache.c.

Referenced by genl_ctrl_resolve(), nl_cache_mngr_add(), nl_cache_mngt_unprovide(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), and rtnl_qdisc_alloc_cache().

00278 {
00279         if (!cache)
00280                 return;
00281 
00282         cache->c_refcnt--;
00283         NL_DBG(4, "Returned cache reference %p, %d remaining\n",
00284                cache, cache->c_refcnt);
00285 
00286         if (cache->c_refcnt <= 0)
00287                 __nl_cache_free(cache);
00288 }

int nl_cache_add ( struct nl_cache *  cache,
struct nl_object *  obj 
)
Parameters:
cache Cache to add object to
obj Object to be added to the cache

Adds the given object to the specified cache. The object is cloned if it has been added to another cache already.

Returns:
0 or a negative error code.

Definition at line 320 of file cache.c.

References nl_object_clone(), and nl_object_get().

Referenced by nl_cache_subset().

00321 {
00322         struct nl_object *new;
00323 
00324         if (cache->c_ops->co_obj_ops != obj->ce_ops)
00325                 return nl_error(EINVAL, "Object mismatches cache type");
00326 
00327         if (!nl_list_empty(&obj->ce_list)) {
00328                 new = nl_object_clone(obj);
00329                 if (!new)
00330                         return nl_errno(ENOMEM);
00331         } else {
00332                 nl_object_get(obj);
00333                 new = obj;
00334         }
00335 
00336         return __cache_add(cache, new);
00337 }

int nl_cache_move ( struct nl_cache *  cache,
struct nl_object *  obj 
)
Parameters:
cache Cache to move object to.
obj Object subject to be moved

Removes the given object from its associated cache if needed and adds it to the new cache.

Returns:
0 on success or a negative error code.

Definition at line 349 of file cache.c.

References nl_cache_remove(), and nl_object_get().

00350 {
00351         if (cache->c_ops->co_obj_ops != obj->ce_ops)
00352                 return nl_error(EINVAL, "Object mismatches cache type");
00353 
00354         NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
00355         
00356         /* Acquire reference, if already in a cache this will be
00357          * reverted during removal */
00358         nl_object_get(obj);
00359 
00360         if (!nl_list_empty(&obj->ce_list))
00361                 nl_cache_remove(obj);
00362 
00363         return __cache_add(cache, obj);
00364 }

void nl_cache_remove ( struct nl_object *  obj  ) 
Parameters:
obj Object to remove from its cache

Removes the object obj from the cache it is assigned to, since an object can only be assigned to one cache at a time, the cache must ne be passed along with it.

Definition at line 374 of file cache.c.

References nl_object_put().

Referenced by nl_cache_clear(), nl_cache_move(), and nl_object_free().

00375 {
00376         struct nl_cache *cache = obj->ce_cache;
00377 
00378         if (cache == NULL)
00379                 return;
00380 
00381         nl_list_del(&obj->ce_list);
00382         obj->ce_cache = NULL;
00383         nl_object_put(obj);
00384         cache->c_nitems--;
00385 
00386         NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
00387                obj, cache, nl_cache_name(cache));
00388 }

struct nl_object* nl_cache_search ( struct nl_cache *  cache,
struct nl_object *  needle 
) [read]
Parameters:
cache Cache to search in.
needle Object to look for.

Iterates over the cache and looks for an object with identical identifiers as the needle.

Returns:
Reference to object or NULL if not found.
Note:
The returned object must be returned via nl_object_put().

Definition at line 401 of file cache.c.

References nl_object_get(), and nl_object_identical().

00403 {
00404         struct nl_object *obj;
00405 
00406         nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00407                 if (nl_object_identical(obj, needle)) {
00408                         nl_object_get(obj);
00409                         return obj;
00410                 }
00411         }
00412 
00413         return NULL;
00414 }

int nl_cache_request_full_dump ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters:
handle Netlink handle
cache Cache subjected to be filled.

Send a dumping request to the kernel causing it to dump all objects related to the specified cache to the netlink socket.

Use nl_cache_pickup() to read the objects from the socket and fill them into a cache.

Definition at line 435 of file cache.c.

Referenced by nl_cache_refill().

00436 {
00437         NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
00438                   cache, nl_cache_name(cache));
00439 
00440         if (cache->c_ops->co_request_update == NULL)
00441                 return nl_error(EOPNOTSUPP, "Operation not supported");
00442 
00443         return cache->c_ops->co_request_update(cache, handle);
00444 }

int nl_cache_pickup ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters:
handle Netlink handle.
cache Cache to put items into.

Waits for netlink messages to arrive, parses them and puts them into the specified cache.

Returns:
0 on success or a negative error code.

Definition at line 505 of file cache.c.

Referenced by flnl_lookup(), and nl_cache_refill().

00506 {
00507         struct nl_parser_param p = {
00508                 .pp_cb = pickup_cb,
00509                 .pp_arg = cache,
00510         };
00511 
00512         return __cache_pickup(handle, cache, &p);
00513 }

int nl_cache_parse_and_add ( struct nl_cache *  cache,
struct nl_msg *  msg 
)
Parameters:
cache cache to add element to
msg netlink message

Parses a netlink message by calling the cache specific message parser and adds the new element to the cache.

Returns:
0 or a negative error code.

Definition at line 660 of file cache.c.

References nlmsg_hdr().

00661 {
00662         struct nl_parser_param p = {
00663                 .pp_cb = pickup_cb,
00664                 .pp_arg = cache,
00665         };
00666 
00667         return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
00668 }

int nl_cache_refill ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters:
handle netlink handle
cache cache to update

Clears the specified cache and fills it with the current state in the kernel.

Returns:
0 or a negative error code.

Definition at line 680 of file cache.c.

References nl_cache_clear(), nl_cache_pickup(), and nl_cache_request_full_dump().

Referenced by nfnl_ct_alloc_cache(), nl_cache_mngr_add(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), rtnl_qdisc_alloc_cache(), rtnl_route_alloc_cache(), and rtnl_rule_alloc_cache_by_family().

00681 {
00682         int err;
00683 
00684         err = nl_cache_request_full_dump(handle, cache);
00685         if (err < 0)
00686                 return err;
00687 
00688         NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
00689                cache, nl_cache_name(cache));
00690         nl_cache_clear(cache);
00691 
00692         return nl_cache_pickup(handle, cache);
00693 }

void nl_cache_mark_all ( struct nl_cache *  cache  ) 
Parameters:
cache Cache to mark all objects in

Definition at line 706 of file cache.c.

References nl_object_mark().

00707 {
00708         struct nl_object *obj;
00709 
00710         NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
00711                   cache, nl_cache_name(cache));
00712 
00713         nl_list_for_each_entry(obj, &cache->c_items, ce_list)
00714                 nl_object_mark(obj);
00715 }

void nl_cache_dump ( struct nl_cache *  cache,
struct nl_dump_params params 
)
Parameters:
cache cache to dump
params dumping parameters

Dumps all elements of the cache to the file descriptor fd.

Definition at line 731 of file cache.c.

References nl_cache_dump_filter().

00732 {
00733         nl_cache_dump_filter(cache, params, NULL);
00734 }

void nl_cache_dump_filter ( struct nl_cache *  cache,
struct nl_dump_params params,
struct nl_object *  filter 
)
Parameters:
cache cache to dump
params dumping parameters (optional)
filter filter object

Dumps all elements of the cache to the file descriptor fd given they match the given filter filter.

Definition at line 745 of file cache.c.

References nl_dump_params::dp_type, NL_DUMP_FULL, nl_object_match_filter(), and nl_object_ops::oo_dump.

Referenced by nl_cache_dump().

00748 {
00749         int type = params ? params->dp_type : NL_DUMP_FULL;
00750         struct nl_object_ops *ops;
00751         struct nl_object *obj;
00752 
00753         NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
00754                cache, nl_cache_name(cache), filter);
00755 
00756         if (type > NL_DUMP_MAX || type < 0)
00757                 BUG();
00758 
00759         if (cache->c_ops == NULL)
00760                 BUG();
00761 
00762         ops = cache->c_ops->co_obj_ops;
00763         if (!ops->oo_dump[type])
00764                 return;
00765 
00766         nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00767                 if (filter && !nl_object_match_filter(obj, filter))
00768                         continue;
00769 
00770                 NL_DBG(4, "Dumping object %p...\n", obj);
00771                 dump_from_ops(obj, params);
00772         }
00773 }

void nl_cache_foreach ( struct nl_cache *  cache,
void(*)(struct nl_object *, void *)  cb,
void *  arg 
)
Parameters:
cache cache to iterate on
cb callback function
arg argument passed to callback function

Calls a callback function cb on each element of the cache. The argument arg is passed on the callback function.

Definition at line 791 of file cache.c.

References nl_cache_foreach_filter().

00793 {
00794         nl_cache_foreach_filter(cache, NULL, cb, arg);
00795 }

void nl_cache_foreach_filter ( struct nl_cache *  cache,
struct nl_object *  filter,
void(*)(struct nl_object *, void *)  cb,
void *  arg 
)
Parameters:
cache cache to iterate on
filter filter object
cb callback function
arg argument passed to callback function

Calls a callback function cb on each element of the cache that matches the filter. The argument arg is passed on to the callback function.

Definition at line 808 of file cache.c.

References nl_object_match_filter().

Referenced by nl_cache_foreach(), rtnl_class_foreach_child(), rtnl_class_foreach_cls(), rtnl_qdisc_foreach_child(), and rtnl_qdisc_foreach_cls().

00810 {
00811         struct nl_object *obj, *tmp;
00812 
00813         if (cache->c_ops == NULL)
00814                 BUG();
00815 
00816         nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
00817                 if (filter && !nl_object_match_filter(obj, filter))
00818                         continue;
00819 
00820                 cb(obj, arg);
00821         }
00822 }


Generated on 27 Jun 2013 for libnl by  doxygen 1.6.1