| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2012 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @ingroup cache_mngt |
| 8 |
* @defgroup cache Cache |
| 9 |
* |
| 10 |
* @code |
| 11 |
* Cache Management | | Type Specific Cache Operations |
| 12 |
* |
| 13 |
* | | +----------------+ +------------+ |
| 14 |
* | request update | | msg_parser | |
| 15 |
* | | +----------------+ +------------+ |
| 16 |
* +- - - - -^- - - - - - - -^- -|- - - - |
| 17 |
* nl_cache_update: | | | | |
| 18 |
* 1) --------- co_request_update ------+ | | |
| 19 |
* | | | |
| 20 |
* 2) destroy old cache +----------- pp_cb ---------|---+ |
| 21 |
* | | | |
| 22 |
* 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+ |
| 23 |
* +--------------+ | | | | |
| 24 |
* | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - - |
| 25 |
* +--------------+ | | +-------------+ |
| 26 |
* | nl_recvmsgs | |
| 27 |
* | | +-----|-^-----+ |
| 28 |
* +---v-|---+ |
| 29 |
* | | | nl_recv | |
| 30 |
* +---------+ |
| 31 |
* | | Core Netlink |
| 32 |
* @endcode |
| 33 |
* |
| 34 |
* Related sections in the development guide: |
| 35 |
* - @core_doc{core_cache, Caching System} |
| 36 |
* |
| 37 |
* @{ |
| 38 |
* |
| 39 |
* Header |
| 40 |
* ------ |
| 41 |
* ~~~~{.c} |
| 42 |
* #include <netlink/cache.h> |
| 43 |
* ~~~~ |
| 44 |
*/ |
| 45 |
|
| 46 |
#include "nl-default.h" |
| 47 |
|
| 48 |
#include <netlink/netlink.h> |
| 49 |
#include <netlink/cache.h> |
| 50 |
#include <netlink/object.h> |
| 51 |
#include <netlink/hashtable.h> |
| 52 |
#include <netlink/utils.h> |
| 53 |
|
| 54 |
#include "nl-core.h" |
| 55 |
#include "nl-priv-dynamic-core/nl-core.h" |
| 56 |
#include "nl-priv-dynamic-core/object-api.h" |
| 57 |
#include "nl-priv-dynamic-core/cache-api.h" |
| 58 |
#include "hashtable-api.h" |
| 59 |
#include "nl-aux-core/nl-core.h" |
| 60 |
|
| 61 |
/** |
| 62 |
* @name Access Functions |
| 63 |
* @{ |
| 64 |
*/ |
| 65 |
|
| 66 |
/** |
| 67 |
* Return the number of items in the cache |
| 68 |
* @arg cache cache handle |
| 69 |
*/ |
| 70 |
int nl_cache_nitems(struct nl_cache *cache) |
| 71 |
{ |
| 72 |
return cache->c_nitems; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Return the number of items matching a filter in the cache |
| 77 |
* @arg cache Cache object. |
| 78 |
* @arg filter Filter object. |
| 79 |
*/ |
| 80 |
int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter) |
| 81 |
{ |
| 82 |
struct nl_object *obj; |
| 83 |
int nitems = 0; |
| 84 |
|
| 85 |
if (cache->c_ops == NULL) |
| 86 |
BUG(); |
| 87 |
|
| 88 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) { |
| 89 |
if (filter && !nl_object_match_filter(obj, filter)) |
| 90 |
continue; |
| 91 |
|
| 92 |
nitems++; |
| 93 |
} |
| 94 |
|
| 95 |
return nitems; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns \b true if the cache is empty. |
| 100 |
* @arg cache Cache to check |
| 101 |
* @return \a true if the cache is empty, otherwise \b false is returned. |
| 102 |
*/ |
| 103 |
int nl_cache_is_empty(struct nl_cache *cache) |
| 104 |
{ |
| 105 |
return nl_list_empty(&cache->c_items); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return the operations set of the cache |
| 110 |
* @arg cache cache handle |
| 111 |
*/ |
| 112 |
struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache) |
| 113 |
{ |
| 114 |
return cache->c_ops; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Return the first element in the cache |
| 119 |
* @arg cache cache handle |
| 120 |
*/ |
| 121 |
struct nl_object *nl_cache_get_first(struct nl_cache *cache) |
| 122 |
{ |
| 123 |
if (nl_list_empty(&cache->c_items)) |
| 124 |
return NULL; |
| 125 |
|
| 126 |
return nl_list_entry(cache->c_items.next, |
| 127 |
struct nl_object, ce_list); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Return the last element in the cache |
| 132 |
* @arg cache cache handle |
| 133 |
*/ |
| 134 |
struct nl_object *nl_cache_get_last(struct nl_cache *cache) |
| 135 |
{ |
| 136 |
if (nl_list_empty(&cache->c_items)) |
| 137 |
return NULL; |
| 138 |
|
| 139 |
return nl_list_entry(cache->c_items.prev, |
| 140 |
struct nl_object, ce_list); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Return the next element in the cache |
| 145 |
* @arg obj current object |
| 146 |
*/ |
| 147 |
struct nl_object *nl_cache_get_next(struct nl_object *obj) |
| 148 |
{ |
| 149 |
if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list)) |
| 150 |
return NULL; |
| 151 |
else |
| 152 |
return nl_list_entry(obj->ce_list.next, |
| 153 |
struct nl_object, ce_list); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return the previous element in the cache |
| 158 |
* @arg obj current object |
| 159 |
*/ |
| 160 |
struct nl_object *nl_cache_get_prev(struct nl_object *obj) |
| 161 |
{ |
| 162 |
if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list)) |
| 163 |
return NULL; |
| 164 |
else |
| 165 |
return nl_list_entry(obj->ce_list.prev, |
| 166 |
struct nl_object, ce_list); |
| 167 |
} |
| 168 |
|
| 169 |
/** @} */ |
| 170 |
|
| 171 |
/** |
| 172 |
* @name Cache Allocation/Deletion |
| 173 |
* @{ |
| 174 |
*/ |
| 175 |
|
| 176 |
/** |
| 177 |
* Allocate new cache |
| 178 |
* @arg ops Cache operations |
| 179 |
* |
| 180 |
* Allocate and initialize a new cache based on the cache operations |
| 181 |
* provided. |
| 182 |
* |
| 183 |
* @return Allocated cache or NULL if allocation failed. |
| 184 |
*/ |
| 185 |
struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops) |
| 186 |
{ |
| 187 |
struct nl_cache *cache; |
| 188 |
|
| 189 |
cache = calloc(1, sizeof(*cache)); |
| 190 |
if (!cache) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
nl_init_list_head(&cache->c_items); |
| 194 |
cache->c_ops = ops; |
| 195 |
cache->c_flags |= ops->co_flags; |
| 196 |
cache->c_refcnt = 1; |
| 197 |
|
| 198 |
/* |
| 199 |
* If object type provides a hash keygen |
| 200 |
* functions, allocate a hash table for the |
| 201 |
* cache objects for faster lookups |
| 202 |
*/ |
| 203 |
if (ops->co_obj_ops->oo_keygen) { |
| 204 |
cache->hashtable = nl_rhash_table_alloc(); |
| 205 |
} |
| 206 |
|
| 207 |
NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache)); |
| 208 |
|
| 209 |
return cache; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Allocate new cache and fill it |
| 214 |
* @arg ops Cache operations |
| 215 |
* @arg sock Netlink socket |
| 216 |
* @arg result Result pointer |
| 217 |
* |
| 218 |
* Allocate new cache and fill it. Equivalent to calling: |
| 219 |
* @code |
| 220 |
* cache = nl_cache_alloc(ops); |
| 221 |
* nl_cache_refill(sock, cache); |
| 222 |
* @endcode |
| 223 |
* |
| 224 |
* @see nl_cache_alloc |
| 225 |
* |
| 226 |
* @return 0 on success or a negative error code. |
| 227 |
*/ |
| 228 |
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, |
| 229 |
struct nl_cache **result) |
| 230 |
{ |
| 231 |
struct nl_cache *cache; |
| 232 |
int err; |
| 233 |
|
| 234 |
if (!(cache = nl_cache_alloc(ops))) |
| 235 |
return -NLE_NOMEM; |
| 236 |
|
| 237 |
if (sock && (err = nl_cache_refill(sock, cache)) < 0) { |
| 238 |
nl_cache_free(cache); |
| 239 |
return err; |
| 240 |
} |
| 241 |
|
| 242 |
*result = cache; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Allocate new cache based on type name |
| 248 |
* @arg kind Name of cache type |
| 249 |
* @arg result Result pointer |
| 250 |
* |
| 251 |
* Lookup cache ops via nl_cache_ops_lookup() and allocate the cache |
| 252 |
* by calling nl_cache_alloc(). Stores the allocated cache in the |
| 253 |
* result pointer provided. |
| 254 |
* |
| 255 |
* @see nl_cache_alloc |
| 256 |
* |
| 257 |
* @return 0 on success or a negative error code. |
| 258 |
*/ |
| 259 |
int nl_cache_alloc_name(const char *kind, struct nl_cache **result) |
| 260 |
{ |
| 261 |
struct nl_cache_ops *ops; |
| 262 |
struct nl_cache *cache; |
| 263 |
|
| 264 |
ops = nl_cache_ops_lookup_safe(kind); |
| 265 |
if (!ops) |
| 266 |
return -NLE_NOCACHE; |
| 267 |
|
| 268 |
cache = nl_cache_alloc(ops); |
| 269 |
nl_cache_ops_put(ops); |
| 270 |
if (!cache) |
| 271 |
return -NLE_NOMEM; |
| 272 |
|
| 273 |
*result = cache; |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Allocate new cache containing a subset of an existing cache |
| 279 |
* @arg orig Original cache to base new cache on |
| 280 |
* @arg filter Filter defining the subset to be filled into the new cache |
| 281 |
* |
| 282 |
* Allocates a new cache matching the type of the cache specified by |
| 283 |
* \p orig. Iterates over the \p orig cache applying the specified |
| 284 |
* \p filter and copies all objects that match to the new cache. |
| 285 |
* |
| 286 |
* The copied objects are clones but do not contain a reference to each |
| 287 |
* other. Later modifications to objects in the original cache will |
| 288 |
* not affect objects in the new cache. |
| 289 |
* |
| 290 |
* @return A newly allocated cache or NULL. |
| 291 |
*/ |
| 292 |
struct nl_cache *nl_cache_subset(struct nl_cache *orig, |
| 293 |
struct nl_object *filter) |
| 294 |
{ |
| 295 |
struct nl_cache *cache; |
| 296 |
struct nl_object *obj; |
| 297 |
|
| 298 |
if (!filter) |
| 299 |
BUG(); |
| 300 |
|
| 301 |
cache = nl_cache_alloc(orig->c_ops); |
| 302 |
if (!cache) |
| 303 |
return NULL; |
| 304 |
|
| 305 |
NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n", |
| 306 |
orig, nl_cache_name(orig), filter, cache); |
| 307 |
|
| 308 |
nl_list_for_each_entry(obj, &orig->c_items, ce_list) { |
| 309 |
if (!nl_object_match_filter(obj, filter)) |
| 310 |
continue; |
| 311 |
|
| 312 |
nl_cache_add(cache, obj); |
| 313 |
} |
| 314 |
|
| 315 |
return cache; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Allocate new cache and copy the contents of an existing cache |
| 320 |
* @arg cache Original cache to base new cache on |
| 321 |
* |
| 322 |
* Allocates a new cache matching the type of the cache specified by |
| 323 |
* \p cache. Iterates over the \p cache cache and copies all objects |
| 324 |
* to the new cache. |
| 325 |
* |
| 326 |
* The copied objects are clones but do not contain a reference to each |
| 327 |
* other. Later modifications to objects in the original cache will |
| 328 |
* not affect objects in the new cache. |
| 329 |
* |
| 330 |
* @return A newly allocated cache or NULL. |
| 331 |
*/ |
| 332 |
struct nl_cache *nl_cache_clone(struct nl_cache *cache) |
| 333 |
{ |
| 334 |
struct nl_cache_ops *ops = nl_cache_get_ops(cache); |
| 335 |
struct nl_cache *clone; |
| 336 |
struct nl_object *obj; |
| 337 |
|
| 338 |
clone = nl_cache_alloc(ops); |
| 339 |
if (!clone) |
| 340 |
return NULL; |
| 341 |
|
| 342 |
NL_DBG(2, "Cloning %p into %p\n", cache, clone); |
| 343 |
|
| 344 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) |
| 345 |
nl_cache_add(clone, obj); |
| 346 |
|
| 347 |
return clone; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Remove all objects of a cache. |
| 352 |
* @arg cache Cache to clear |
| 353 |
* |
| 354 |
* The objects are unliked/removed from the cache by calling |
| 355 |
* nl_cache_remove() on each object in the cache. If any of the objects |
| 356 |
* to not contain any further references to them, those objects will |
| 357 |
* be freed. |
| 358 |
* |
| 359 |
* Unlike with nl_cache_free(), the cache is not freed just emptied. |
| 360 |
*/ |
| 361 |
void nl_cache_clear(struct nl_cache *cache) |
| 362 |
{ |
| 363 |
struct nl_object *obj, *tmp; |
| 364 |
|
| 365 |
NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache)); |
| 366 |
|
| 367 |
nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) |
| 368 |
nl_cache_remove(obj); |
| 369 |
} |
| 370 |
|
| 371 |
static void __nl_cache_free(struct nl_cache *cache) |
| 372 |
{ |
| 373 |
nl_cache_clear(cache); |
| 374 |
|
| 375 |
if (cache->hashtable) |
| 376 |
nl_rhash_table_free(cache->hashtable); |
| 377 |
|
| 378 |
NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache)); |
| 379 |
free(cache); |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Increase reference counter of cache |
| 384 |
* @arg cache Cache |
| 385 |
*/ |
| 386 |
void nl_cache_get(struct nl_cache *cache) |
| 387 |
{ |
| 388 |
cache->c_refcnt++; |
| 389 |
|
| 390 |
NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n", |
| 391 |
cache, nl_cache_name(cache), cache->c_refcnt); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Free a cache. |
| 396 |
* @arg cache Cache to free. |
| 397 |
* |
| 398 |
* Calls nl_cache_clear() to remove all objects associated with the |
| 399 |
* cache and frees the cache afterwards. |
| 400 |
* |
| 401 |
* @see nl_cache_clear() |
| 402 |
*/ |
| 403 |
void nl_cache_free(struct nl_cache *cache) |
| 404 |
{ |
| 405 |
if (!cache) |
| 406 |
return; |
| 407 |
|
| 408 |
cache->c_refcnt--; |
| 409 |
|
| 410 |
NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n", |
| 411 |
cache, nl_cache_name(cache), cache->c_refcnt); |
| 412 |
|
| 413 |
if (cache->c_refcnt <= 0) |
| 414 |
__nl_cache_free(cache); |
| 415 |
} |
| 416 |
|
| 417 |
void nl_cache_put(struct nl_cache *cache) |
| 418 |
{ |
| 419 |
nl_cache_free(cache); |
| 420 |
} |
| 421 |
|
| 422 |
/** @} */ |
| 423 |
|
| 424 |
/** |
| 425 |
* @name Cache Modifications |
| 426 |
* @{ |
| 427 |
*/ |
| 428 |
|
| 429 |
static int __cache_add(struct nl_cache *cache, struct nl_object *obj) |
| 430 |
{ |
| 431 |
int ret; |
| 432 |
|
| 433 |
obj->ce_cache = cache; |
| 434 |
|
| 435 |
if (cache->hashtable) { |
| 436 |
ret = nl_rhash_table_add(cache->hashtable, obj); |
| 437 |
if (ret < 0) { |
| 438 |
obj->ce_cache = NULL; |
| 439 |
return ret; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
nl_list_add_tail(&obj->ce_list, &cache->c_items); |
| 444 |
cache->c_nitems++; |
| 445 |
|
| 446 |
NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n", |
| 447 |
obj, cache, nl_cache_name(cache), cache->c_nitems); |
| 448 |
|
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Add object to cache. |
| 454 |
* @arg cache Cache |
| 455 |
* @arg obj Object to be added to the cache |
| 456 |
* |
| 457 |
* Adds the object \p obj to the specified \p cache. In case the object |
| 458 |
* is already associated with another cache, the object is cloned before |
| 459 |
* adding it to the cache. In this case, the sole reference to the object |
| 460 |
* will be the one of the cache. Therefore clearing/freeing the cache |
| 461 |
* will result in the object being freed again. |
| 462 |
* |
| 463 |
* If the object has not been associated with a cache yet, the reference |
| 464 |
* counter of the object is incremented to account for the additional |
| 465 |
* reference. |
| 466 |
* |
| 467 |
* The type of the object and cache must match, otherwise an error is |
| 468 |
* returned (-NLE_OBJ_MISMATCH). |
| 469 |
* |
| 470 |
* @see nl_cache_move() |
| 471 |
* |
| 472 |
* @return 0 or a negative error code. |
| 473 |
*/ |
| 474 |
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj) |
| 475 |
{ |
| 476 |
struct nl_object *new; |
| 477 |
int ret = 0; |
| 478 |
|
| 479 |
if (cache->c_ops->co_obj_ops != obj->ce_ops) |
| 480 |
return -NLE_OBJ_MISMATCH; |
| 481 |
|
| 482 |
if (!nl_list_empty(&obj->ce_list)) { |
| 483 |
NL_DBG(3, "Object %p already in cache, cloning new object\n", obj); |
| 484 |
|
| 485 |
new = nl_object_clone(obj); |
| 486 |
if (!new) |
| 487 |
return -NLE_NOMEM; |
| 488 |
} else { |
| 489 |
nl_object_get(obj); |
| 490 |
new = obj; |
| 491 |
} |
| 492 |
|
| 493 |
ret = __cache_add(cache, new); |
| 494 |
if (ret < 0) |
| 495 |
nl_object_put(new); |
| 496 |
|
| 497 |
return ret; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Move object from one cache to another |
| 502 |
* @arg cache Cache to move object to. |
| 503 |
* @arg obj Object subject to be moved |
| 504 |
* |
| 505 |
* Removes the the specified object \p obj from its associated cache |
| 506 |
* and moves it to another cache. |
| 507 |
* |
| 508 |
* If the object is not associated with a cache, the function behaves |
| 509 |
* just like nl_cache_add(). |
| 510 |
* |
| 511 |
* The type of the object and cache must match, otherwise an error is |
| 512 |
* returned (-NLE_OBJ_MISMATCH). |
| 513 |
* |
| 514 |
* @see nl_cache_add() |
| 515 |
* |
| 516 |
* @return 0 on success or a negative error code. |
| 517 |
*/ |
| 518 |
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj) |
| 519 |
{ |
| 520 |
if (cache->c_ops->co_obj_ops != obj->ce_ops) |
| 521 |
return -NLE_OBJ_MISMATCH; |
| 522 |
|
| 523 |
NL_DBG(3, "Moving object %p from cache %p to cache %p\n", |
| 524 |
obj, obj->ce_cache, cache); |
| 525 |
|
| 526 |
/* Acquire reference, if already in a cache this will be |
| 527 |
* reverted during removal */ |
| 528 |
nl_object_get(obj); |
| 529 |
|
| 530 |
if (!nl_list_empty(&obj->ce_list)) |
| 531 |
nl_cache_remove(obj); |
| 532 |
|
| 533 |
return __cache_add(cache, obj); |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* Remove object from cache. |
| 538 |
* @arg obj Object to remove from cache |
| 539 |
* |
| 540 |
* Removes the object \c obj from the cache it is associated with. The |
| 541 |
* reference counter of the object will be decremented. If the reference |
| 542 |
* to the object was the only one remaining, the object will be freed. |
| 543 |
* |
| 544 |
* If no cache is associated with the object, this function is a NOP. |
| 545 |
*/ |
| 546 |
void nl_cache_remove(struct nl_object *obj) |
| 547 |
{ |
| 548 |
int ret; |
| 549 |
struct nl_cache *cache = obj->ce_cache; |
| 550 |
|
| 551 |
if (cache == NULL) |
| 552 |
return; |
| 553 |
|
| 554 |
if (cache->hashtable) { |
| 555 |
ret = nl_rhash_table_del(cache->hashtable, obj); |
| 556 |
if (ret < 0) |
| 557 |
NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n", |
| 558 |
obj, cache, nl_cache_name(cache)); |
| 559 |
} |
| 560 |
|
| 561 |
nl_list_del(&obj->ce_list); |
| 562 |
obj->ce_cache = NULL; |
| 563 |
nl_object_put(obj); |
| 564 |
cache->c_nitems--; |
| 565 |
|
| 566 |
NL_DBG(2, "Deleted object %p from cache %p <%s>.\n", |
| 567 |
obj, cache, nl_cache_name(cache)); |
| 568 |
} |
| 569 |
|
| 570 |
/** @} */ |
| 571 |
|
| 572 |
/** |
| 573 |
* @name Synchronization |
| 574 |
* @{ |
| 575 |
*/ |
| 576 |
|
| 577 |
/** |
| 578 |
* Set synchronization arg1 of cache |
| 579 |
* @arg cache Cache |
| 580 |
* @arg arg argument |
| 581 |
* |
| 582 |
* Synchronization arguments are used to specify filters when |
| 583 |
* requesting dumps from the kernel. |
| 584 |
*/ |
| 585 |
void nl_cache_set_arg1(struct nl_cache *cache, int arg) |
| 586 |
{ |
| 587 |
cache->c_iarg1 = arg; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Set synchronization arg2 of cache |
| 592 |
* @arg cache Cache |
| 593 |
* @arg arg argument |
| 594 |
* |
| 595 |
* Synchronization arguments are used to specify filters when |
| 596 |
* requesting dumps from the kernel. |
| 597 |
*/ |
| 598 |
void nl_cache_set_arg2(struct nl_cache *cache, int arg) |
| 599 |
{ |
| 600 |
cache->c_iarg2 = arg; |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* Set cache flags |
| 605 |
* @arg cache Cache |
| 606 |
* @arg flags Flags |
| 607 |
*/ |
| 608 |
void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags) |
| 609 |
{ |
| 610 |
cache->c_flags |= flags; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Invoke the request-update operation |
| 615 |
* @arg sk Netlink socket. |
| 616 |
* @arg cache Cache |
| 617 |
* |
| 618 |
* This function causes the \e request-update function of the cache |
| 619 |
* operations to be invoked. This usually causes a dump request to |
| 620 |
* be sent over the netlink socket which triggers the kernel to dump |
| 621 |
* all objects of a specific type to be dumped onto the netlink |
| 622 |
* socket for pickup. |
| 623 |
* |
| 624 |
* The behaviour of this function depends on the implemenation of |
| 625 |
* the \e request_update function of each individual type of cache. |
| 626 |
* |
| 627 |
* This function will not have any effects on the cache (unless the |
| 628 |
* request_update implementation of the cache operations does so). |
| 629 |
* |
| 630 |
* Use nl_cache_pickup() to pick-up (read) the objects from the socket |
| 631 |
* and fill them into the cache. |
| 632 |
* |
| 633 |
* @see nl_cache_pickup(), nl_cache_resync() |
| 634 |
* |
| 635 |
* @return 0 on success or a negative error code. Some implementations |
| 636 |
* of co_request_update() return a positive number on success that is |
| 637 |
* the number of bytes sent. Treat any non-negative number as success too. |
| 638 |
*/ |
| 639 |
static int nl_cache_request_full_dump(struct nl_sock *sk, |
| 640 |
struct nl_cache *cache) |
| 641 |
{ |
| 642 |
if (sk->s_proto != cache->c_ops->co_protocol) |
| 643 |
return -NLE_PROTO_MISMATCH; |
| 644 |
|
| 645 |
if (cache->c_ops->co_request_update == NULL) |
| 646 |
return -NLE_OPNOTSUPP; |
| 647 |
|
| 648 |
NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n", |
| 649 |
cache, nl_cache_name(cache)); |
| 650 |
|
| 651 |
return cache->c_ops->co_request_update(cache, sk); |
| 652 |
} |
| 653 |
|
| 654 |
/** @cond SKIP */ |
| 655 |
struct update_xdata { |
| 656 |
struct nl_cache_ops *ops; |
| 657 |
struct nl_parser_param *params; |
| 658 |
}; |
| 659 |
|
| 660 |
static int update_msg_parser(struct nl_msg *msg, void *arg) |
| 661 |
{ |
| 662 |
struct update_xdata *x = arg; |
| 663 |
int ret = 0; |
| 664 |
|
| 665 |
ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params); |
| 666 |
if (ret == -NLE_EXIST) |
| 667 |
return NL_SKIP; |
| 668 |
else |
| 669 |
return ret; |
| 670 |
} |
| 671 |
/** @endcond */ |
| 672 |
|
| 673 |
/** |
| 674 |
* Pick-up a netlink request-update with your own parser |
| 675 |
* @arg sk Netlink socket |
| 676 |
* @arg cache Cache |
| 677 |
* @arg param Parser parameters |
| 678 |
*/ |
| 679 |
static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache, |
| 680 |
struct nl_parser_param *param) |
| 681 |
{ |
| 682 |
int err; |
| 683 |
struct nl_cb *cb; |
| 684 |
struct update_xdata x = { |
| 685 |
.ops = cache->c_ops, |
| 686 |
.params = param, |
| 687 |
}; |
| 688 |
|
| 689 |
NL_DBG(2, "Picking up answer for cache %p <%s>\n", |
| 690 |
cache, nl_cache_name(cache)); |
| 691 |
|
| 692 |
cb = nl_cb_clone(sk->s_cb); |
| 693 |
if (cb == NULL) |
| 694 |
return -NLE_NOMEM; |
| 695 |
|
| 696 |
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x); |
| 697 |
|
| 698 |
err = nl_recvmsgs(sk, cb); |
| 699 |
if (err < 0) |
| 700 |
NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned %d: %s\n", |
| 701 |
cache, nl_cache_name(cache), err, nl_geterror(err)); |
| 702 |
|
| 703 |
nl_cb_put(cb); |
| 704 |
|
| 705 |
return err; |
| 706 |
} |
| 707 |
|
| 708 |
static int pickup_checkdup_cb(struct nl_object *c, struct nl_parser_param *p) |
| 709 |
{ |
| 710 |
struct nl_cache *cache = (struct nl_cache *)p->pp_arg; |
| 711 |
_nl_auto_nl_object struct nl_object *old = NULL; |
| 712 |
|
| 713 |
old = nl_cache_search(cache, c); |
| 714 |
if (old) { |
| 715 |
if (nl_object_update(old, c) == 0) |
| 716 |
return 0; |
| 717 |
|
| 718 |
nl_cache_remove(old); |
| 719 |
} |
| 720 |
|
| 721 |
return nl_cache_add(cache, c); |
| 722 |
} |
| 723 |
|
| 724 |
static int pickup_cb(struct nl_object *c, struct nl_parser_param *p) |
| 725 |
{ |
| 726 |
struct nl_cache *cache = p->pp_arg; |
| 727 |
|
| 728 |
return nl_cache_add(cache, c); |
| 729 |
} |
| 730 |
|
| 731 |
static int __nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache, |
| 732 |
int checkdup) |
| 733 |
{ |
| 734 |
struct nl_parser_param p; |
| 735 |
|
| 736 |
p.pp_cb = checkdup ? pickup_checkdup_cb : pickup_cb; |
| 737 |
p.pp_arg = cache; |
| 738 |
|
| 739 |
if (sk->s_proto != cache->c_ops->co_protocol) |
| 740 |
return -NLE_PROTO_MISMATCH; |
| 741 |
|
| 742 |
return __cache_pickup(sk, cache, &p); |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Pickup a netlink dump response and put it into a cache. |
| 747 |
* @arg sk Netlink socket. |
| 748 |
* @arg cache Cache to put items into. |
| 749 |
* |
| 750 |
* Waits for netlink messages to arrive, parses them and puts them into |
| 751 |
* the specified cache. If an old object with same key attributes is |
| 752 |
* present in the cache, it is replaced with the new object. |
| 753 |
* If the old object type supports an update operation, an update is |
| 754 |
* attempted before a replace. |
| 755 |
* |
| 756 |
* @return 0 on success or a negative error code. |
| 757 |
*/ |
| 758 |
int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache) |
| 759 |
{ |
| 760 |
return __nl_cache_pickup(sk, cache, 1); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Pickup a netlink dump response and put it into a cache. |
| 765 |
* @arg sk Netlink socket. |
| 766 |
* @arg cache Cache to put items into. |
| 767 |
* |
| 768 |
* Waits for netlink messages to arrive, parses them and puts them into |
| 769 |
* the specified cache. |
| 770 |
* |
| 771 |
* @return 0 on success or a negative error code. |
| 772 |
*/ |
| 773 |
int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache) |
| 774 |
{ |
| 775 |
return __nl_cache_pickup(sk, cache, 0); |
| 776 |
} |
| 777 |
|
| 778 |
static int cache_include(struct nl_cache *cache, struct nl_object *obj, |
| 779 |
struct nl_msgtype *type, change_func_t cb, |
| 780 |
change_func_v2_t cb_v2, void *data) |
| 781 |
{ |
| 782 |
_nl_auto_nl_object struct nl_object *old = NULL; |
| 783 |
_nl_auto_nl_object struct nl_object *clone = NULL; |
| 784 |
uint64_t diff = 0; |
| 785 |
|
| 786 |
switch (type->mt_act) { |
| 787 |
case NL_ACT_NEW: |
| 788 |
case NL_ACT_DEL: |
| 789 |
old = nl_cache_search(cache, obj); |
| 790 |
if (old) { |
| 791 |
if (cb_v2 && old->ce_ops->oo_update) { |
| 792 |
clone = nl_object_clone(old); |
| 793 |
diff = nl_object_diff64(old, obj); |
| 794 |
} |
| 795 |
/* |
| 796 |
* Some objects types might support merging the new |
| 797 |
* object with the old existing cache object. |
| 798 |
* Handle them first. |
| 799 |
*/ |
| 800 |
if (nl_object_update(old, obj) == 0) { |
| 801 |
if (cb_v2) { |
| 802 |
cb_v2(cache, clone, old, diff, |
| 803 |
NL_ACT_CHANGE, data); |
| 804 |
} else if (cb) |
| 805 |
cb(cache, old, NL_ACT_CHANGE, data); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
nl_cache_remove(old); |
| 809 |
if (type->mt_act == NL_ACT_DEL) { |
| 810 |
if (cb_v2) |
| 811 |
cb_v2(cache, old, NULL, 0, NL_ACT_DEL, |
| 812 |
data); |
| 813 |
else if (cb) |
| 814 |
cb(cache, old, NL_ACT_DEL, data); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
if (type->mt_act == NL_ACT_NEW) { |
| 819 |
nl_cache_move(cache, obj); |
| 820 |
if (old == NULL) { |
| 821 |
if (cb_v2) { |
| 822 |
cb_v2(cache, NULL, obj, 0, NL_ACT_NEW, |
| 823 |
data); |
| 824 |
} else if (cb) |
| 825 |
cb(cache, obj, NL_ACT_NEW, data); |
| 826 |
} else if (old) { |
| 827 |
diff = 0; |
| 828 |
if (cb || cb_v2) |
| 829 |
diff = nl_object_diff64(old, obj); |
| 830 |
if (diff && cb_v2) { |
| 831 |
cb_v2(cache, old, obj, diff, NL_ACT_CHANGE, |
| 832 |
data); |
| 833 |
} else if (diff && cb) |
| 834 |
cb(cache, obj, NL_ACT_CHANGE, data); |
| 835 |
|
| 836 |
} |
| 837 |
} |
| 838 |
break; |
| 839 |
default: |
| 840 |
NL_DBG(2, "Unknown action associated to object %p\n", obj); |
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
int nl_cache_include(struct nl_cache *cache, struct nl_object *obj, |
| 848 |
change_func_t change_cb, void *data) |
| 849 |
{ |
| 850 |
struct nl_cache_ops *ops = cache->c_ops; |
| 851 |
int i; |
| 852 |
|
| 853 |
if (ops->co_obj_ops != obj->ce_ops) |
| 854 |
return -NLE_OBJ_MISMATCH; |
| 855 |
|
| 856 |
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) |
| 857 |
if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype) |
| 858 |
return cache_include(cache, obj, &ops->co_msgtypes[i], |
| 859 |
change_cb, NULL, data); |
| 860 |
|
| 861 |
NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", |
| 862 |
obj, cache, nl_cache_name(cache)); |
| 863 |
|
| 864 |
return -NLE_MSGTYPE_NOSUPPORT; |
| 865 |
} |
| 866 |
|
| 867 |
int nl_cache_include_v2(struct nl_cache *cache, struct nl_object *obj, |
| 868 |
change_func_v2_t change_cb, void *data) |
| 869 |
{ |
| 870 |
struct nl_cache_ops *ops = cache->c_ops; |
| 871 |
int i; |
| 872 |
|
| 873 |
if (ops->co_obj_ops != obj->ce_ops) |
| 874 |
return -NLE_OBJ_MISMATCH; |
| 875 |
|
| 876 |
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) |
| 877 |
if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype) |
| 878 |
return cache_include(cache, obj, &ops->co_msgtypes[i], |
| 879 |
NULL, change_cb, data); |
| 880 |
|
| 881 |
NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", |
| 882 |
obj, cache, nl_cache_name(cache)); |
| 883 |
|
| 884 |
return -NLE_MSGTYPE_NOSUPPORT; |
| 885 |
} |
| 886 |
|
| 887 |
static int resync_cb(struct nl_object *c, struct nl_parser_param *p) |
| 888 |
{ |
| 889 |
struct nl_cache_assoc *ca = p->pp_arg; |
| 890 |
|
| 891 |
if (ca->ca_change_v2) |
| 892 |
return nl_cache_include_v2(ca->ca_cache, c, ca->ca_change_v2, |
| 893 |
ca->ca_change_data); |
| 894 |
else |
| 895 |
return nl_cache_include(ca->ca_cache, c, ca->ca_change, |
| 896 |
ca->ca_change_data); |
| 897 |
} |
| 898 |
|
| 899 |
static int cache_resync(struct nl_sock *sk, struct nl_cache *cache, |
| 900 |
change_func_t change_cb, change_func_v2_t change_cb_v2, |
| 901 |
void *data) |
| 902 |
{ |
| 903 |
struct nl_object *obj, *next; |
| 904 |
struct nl_af_group *grp; |
| 905 |
struct nl_cache_assoc ca = { |
| 906 |
.ca_cache = cache, |
| 907 |
.ca_change = change_cb, |
| 908 |
.ca_change_v2 = change_cb_v2, |
| 909 |
.ca_change_data = data, |
| 910 |
}; |
| 911 |
struct nl_parser_param p = { |
| 912 |
.pp_cb = resync_cb, |
| 913 |
.pp_arg = &ca, |
| 914 |
}; |
| 915 |
int err; |
| 916 |
|
| 917 |
if (sk->s_proto != cache->c_ops->co_protocol) |
| 918 |
return -NLE_PROTO_MISMATCH; |
| 919 |
|
| 920 |
NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache)); |
| 921 |
|
| 922 |
/* Mark all objects so we can see if some of them are obsolete */ |
| 923 |
nl_cache_mark_all(cache); |
| 924 |
|
| 925 |
grp = cache->c_ops->co_groups; |
| 926 |
do { |
| 927 |
if (grp && grp->ag_group && |
| 928 |
(cache->c_flags & NL_CACHE_AF_ITER)) |
| 929 |
nl_cache_set_arg1(cache, grp->ag_family); |
| 930 |
|
| 931 |
restart: |
| 932 |
err = nl_cache_request_full_dump(sk, cache); |
| 933 |
if (err < 0) |
| 934 |
goto errout; |
| 935 |
|
| 936 |
err = __cache_pickup(sk, cache, &p); |
| 937 |
if (err == -NLE_DUMP_INTR) |
| 938 |
goto restart; |
| 939 |
else if (err < 0) |
| 940 |
goto errout; |
| 941 |
|
| 942 |
if (grp) |
| 943 |
grp++; |
| 944 |
} while (grp && grp->ag_group && |
| 945 |
(cache->c_flags & NL_CACHE_AF_ITER)); |
| 946 |
|
| 947 |
nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) { |
| 948 |
if (nl_object_is_marked(obj)) { |
| 949 |
nl_object_get(obj); |
| 950 |
nl_cache_remove(obj); |
| 951 |
if (change_cb) |
| 952 |
change_cb(cache, obj, NL_ACT_DEL, data); |
| 953 |
else if (change_cb_v2) |
| 954 |
change_cb_v2(cache, obj, NULL, 0, NL_ACT_DEL, |
| 955 |
data); |
| 956 |
nl_object_put(obj); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache)); |
| 961 |
|
| 962 |
err = 0; |
| 963 |
errout: |
| 964 |
return err; |
| 965 |
} |
| 966 |
|
| 967 |
int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache, |
| 968 |
change_func_t change_cb, void *data) |
| 969 |
{ |
| 970 |
return cache_resync(sk, cache, change_cb, NULL, data); |
| 971 |
} |
| 972 |
|
| 973 |
int nl_cache_resync_v2(struct nl_sock *sk, struct nl_cache *cache, |
| 974 |
change_func_v2_t change_cb_v2, void *data) |
| 975 |
{ |
| 976 |
return cache_resync(sk, cache, NULL, change_cb_v2, data); |
| 977 |
} |
| 978 |
|
| 979 |
/** @} */ |
| 980 |
|
| 981 |
/** |
| 982 |
* @name Parsing |
| 983 |
* @{ |
| 984 |
*/ |
| 985 |
|
| 986 |
/** @cond SKIP */ |
| 987 |
int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who, |
| 988 |
struct nlmsghdr *nlh, struct nl_parser_param *params) |
| 989 |
{ |
| 990 |
int i, err; |
| 991 |
|
| 992 |
if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) |
| 993 |
return -NLE_MSG_TOOSHORT; |
| 994 |
|
| 995 |
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) { |
| 996 |
if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) { |
| 997 |
err = ops->co_msg_parser(ops, who, nlh, params); |
| 998 |
if (err != -NLE_OPNOTSUPP) |
| 999 |
goto errout; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
err = -NLE_MSGTYPE_NOSUPPORT; |
| 1005 |
errout: |
| 1006 |
return err; |
| 1007 |
} |
| 1008 |
/** @endcond */ |
| 1009 |
|
| 1010 |
/** |
| 1011 |
* Parse a netlink message and add it to the cache. |
| 1012 |
* @arg cache cache to add element to |
| 1013 |
* @arg msg netlink message |
| 1014 |
* |
| 1015 |
* Parses a netlink message by calling the cache specific message parser |
| 1016 |
* and adds the new element to the cache. If an old object with same key |
| 1017 |
* attributes is present in the cache, it is replaced with the new object. |
| 1018 |
* If the old object type supports an update operation, an update is |
| 1019 |
* attempted before a replace. |
| 1020 |
* |
| 1021 |
* @return 0 or a negative error code. |
| 1022 |
*/ |
| 1023 |
int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg) |
| 1024 |
{ |
| 1025 |
struct nl_parser_param p = { |
| 1026 |
.pp_cb = pickup_cb, |
| 1027 |
.pp_arg = cache, |
| 1028 |
}; |
| 1029 |
|
| 1030 |
return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* (Re)fill a cache with the contents in the kernel. |
| 1035 |
* @arg sk Netlink socket. |
| 1036 |
* @arg cache cache to update |
| 1037 |
* |
| 1038 |
* Clears the specified cache and fills it with the current state in |
| 1039 |
* the kernel. |
| 1040 |
* |
| 1041 |
* @return 0 or a negative error code. |
| 1042 |
*/ |
| 1043 |
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache) |
| 1044 |
{ |
| 1045 |
struct nl_af_group *grp; |
| 1046 |
int err; |
| 1047 |
|
| 1048 |
if (sk->s_proto != cache->c_ops->co_protocol) |
| 1049 |
return -NLE_PROTO_MISMATCH; |
| 1050 |
|
| 1051 |
nl_cache_clear(cache); |
| 1052 |
grp = cache->c_ops->co_groups; |
| 1053 |
do { |
| 1054 |
if (grp && grp->ag_group && |
| 1055 |
(cache->c_flags & NL_CACHE_AF_ITER)) |
| 1056 |
nl_cache_set_arg1(cache, grp->ag_family); |
| 1057 |
|
| 1058 |
restart: |
| 1059 |
err = nl_cache_request_full_dump(sk, cache); |
| 1060 |
if (err < 0) |
| 1061 |
return err; |
| 1062 |
|
| 1063 |
NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n", |
| 1064 |
cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC); |
| 1065 |
|
| 1066 |
err = nl_cache_pickup(sk, cache); |
| 1067 |
if (err == -NLE_DUMP_INTR) { |
| 1068 |
NL_DBG(2, "Dump interrupted, restarting!\n"); |
| 1069 |
goto restart; |
| 1070 |
} else if (err < 0) |
| 1071 |
break; |
| 1072 |
|
| 1073 |
if (grp) |
| 1074 |
grp++; |
| 1075 |
} while (grp && grp->ag_group && |
| 1076 |
(cache->c_flags & NL_CACHE_AF_ITER)); |
| 1077 |
|
| 1078 |
return err; |
| 1079 |
} |
| 1080 |
|
| 1081 |
/** @} */ |
| 1082 |
|
| 1083 |
/** |
| 1084 |
* @name Utillities |
| 1085 |
* @{ |
| 1086 |
*/ |
| 1087 |
static struct nl_object *__cache_fast_lookup(struct nl_cache *cache, |
| 1088 |
struct nl_object *needle) |
| 1089 |
{ |
| 1090 |
struct nl_object *obj; |
| 1091 |
|
| 1092 |
obj = nl_rhash_table_lookup(cache->hashtable, needle); |
| 1093 |
if (obj) { |
| 1094 |
nl_object_get(obj); |
| 1095 |
return obj; |
| 1096 |
} |
| 1097 |
|
| 1098 |
return NULL; |
| 1099 |
} |
| 1100 |
|
| 1101 |
/** |
| 1102 |
* Search object in cache |
| 1103 |
* @arg cache Cache |
| 1104 |
* @arg needle Object to look for. |
| 1105 |
* |
| 1106 |
* Searches the cache for an object which matches the object \p needle. |
| 1107 |
* The function nl_object_identical() is used to determine if the |
| 1108 |
* objects match. If a matching object is found, the reference counter |
| 1109 |
* is incremented and the object is returned. |
| 1110 |
* |
| 1111 |
* Therefore, if an object is returned, the reference to the object |
| 1112 |
* must be returned by calling nl_object_put() after usage. |
| 1113 |
* |
| 1114 |
* @return Reference to object or NULL if not found. |
| 1115 |
*/ |
| 1116 |
struct nl_object *nl_cache_search(struct nl_cache *cache, |
| 1117 |
struct nl_object *needle) |
| 1118 |
{ |
| 1119 |
struct nl_object *obj; |
| 1120 |
|
| 1121 |
if (cache->hashtable) |
| 1122 |
return __cache_fast_lookup(cache, needle); |
| 1123 |
|
| 1124 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) { |
| 1125 |
if (nl_object_identical(obj, needle)) { |
| 1126 |
nl_object_get(obj); |
| 1127 |
return obj; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
return NULL; |
| 1132 |
} |
| 1133 |
|
| 1134 |
/** |
| 1135 |
* Find object in cache |
| 1136 |
* @arg cache Cache |
| 1137 |
* @arg filter object acting as a filter |
| 1138 |
* |
| 1139 |
* Searches the cache for an object which matches the object filter. |
| 1140 |
* If the filter attributes matches the object type id attributes, |
| 1141 |
* and the cache supports hash lookups, a faster hashtable lookup |
| 1142 |
* is used to return the object. Else, function nl_object_match_filter() is |
| 1143 |
* used to determine if the objects match. If a matching object is |
| 1144 |
* found, the reference counter is incremented and the object is returned. |
| 1145 |
* |
| 1146 |
* Therefore, if an object is returned, the reference to the object |
| 1147 |
* must be returned by calling nl_object_put() after usage. |
| 1148 |
* |
| 1149 |
* @return Reference to object or NULL if not found. |
| 1150 |
*/ |
| 1151 |
struct nl_object *nl_cache_find(struct nl_cache *cache, |
| 1152 |
struct nl_object *filter) |
| 1153 |
{ |
| 1154 |
struct nl_object *obj; |
| 1155 |
|
| 1156 |
if (cache->c_ops == NULL) |
| 1157 |
BUG(); |
| 1158 |
|
| 1159 |
if ((nl_object_get_id_attrs(filter) == filter->ce_mask) |
| 1160 |
&& cache->hashtable) |
| 1161 |
return __cache_fast_lookup(cache, filter); |
| 1162 |
|
| 1163 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) { |
| 1164 |
if (nl_object_match_filter(obj, filter)) { |
| 1165 |
nl_object_get(obj); |
| 1166 |
return obj; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
return NULL; |
| 1171 |
} |
| 1172 |
|
| 1173 |
/** |
| 1174 |
* Mark all objects of a cache |
| 1175 |
* @arg cache Cache |
| 1176 |
* |
| 1177 |
* Marks all objects of a cache by calling nl_object_mark() on each |
| 1178 |
* object associated with the cache. |
| 1179 |
*/ |
| 1180 |
void nl_cache_mark_all(struct nl_cache *cache) |
| 1181 |
{ |
| 1182 |
struct nl_object *obj; |
| 1183 |
|
| 1184 |
NL_DBG(2, "Marking all objects in cache %p <%s>\n", |
| 1185 |
cache, nl_cache_name(cache)); |
| 1186 |
|
| 1187 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) |
| 1188 |
nl_object_mark(obj); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/** @} */ |
| 1192 |
|
| 1193 |
/** |
| 1194 |
* @name Dumping |
| 1195 |
* @{ |
| 1196 |
*/ |
| 1197 |
|
| 1198 |
/** |
| 1199 |
* Dump all elements of a cache. |
| 1200 |
* @arg cache cache to dump |
| 1201 |
* @arg params dumping parameters |
| 1202 |
* |
| 1203 |
* Dumps all elements of the \a cache to the file descriptor \a fd. |
| 1204 |
*/ |
| 1205 |
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params) |
| 1206 |
{ |
| 1207 |
nl_cache_dump_filter(cache, params, NULL); |
| 1208 |
} |
| 1209 |
|
| 1210 |
/** |
| 1211 |
* Dump all elements of a cache (filtered). |
| 1212 |
* @arg cache cache to dump |
| 1213 |
* @arg params dumping parameters |
| 1214 |
* @arg filter filter object |
| 1215 |
* |
| 1216 |
* Dumps all elements of the \a cache to the file descriptor \a fd |
| 1217 |
* given they match the given filter \a filter. |
| 1218 |
*/ |
| 1219 |
void nl_cache_dump_filter(struct nl_cache *cache, |
| 1220 |
struct nl_dump_params *params, |
| 1221 |
struct nl_object *filter) |
| 1222 |
{ |
| 1223 |
struct nl_dump_params params_copy; |
| 1224 |
struct nl_object_ops *ops; |
| 1225 |
struct nl_object *obj; |
| 1226 |
int type; |
| 1227 |
|
| 1228 |
NL_DBG(2, "Dumping cache %p <%s> with filter %p\n", |
| 1229 |
cache, nl_cache_name(cache), filter); |
| 1230 |
|
| 1231 |
if (!params) { |
| 1232 |
/* It doesn't really make sense that @params is an optional parameter. In the |
| 1233 |
* past, nl_cache_dump() was documented that the @params would be optional, so |
| 1234 |
* try to save it. |
| 1235 |
* |
| 1236 |
* Note that this still isn't useful, because we don't set any dump option. |
| 1237 |
* It only exists not to crash applications that wrongly pass %NULL here. */ |
| 1238 |
_nl_assert_not_reached (); |
| 1239 |
params_copy = (struct nl_dump_params) { |
| 1240 |
.dp_type = NL_DUMP_DETAILS, |
| 1241 |
}; |
| 1242 |
params = ¶ms_copy; |
| 1243 |
} |
| 1244 |
|
| 1245 |
type = params->dp_type; |
| 1246 |
|
| 1247 |
if (type > NL_DUMP_MAX || type < 0) |
| 1248 |
BUG(); |
| 1249 |
|
| 1250 |
if (cache->c_ops == NULL) |
| 1251 |
BUG(); |
| 1252 |
|
| 1253 |
ops = cache->c_ops->co_obj_ops; |
| 1254 |
if (!ops->oo_dump[type]) |
| 1255 |
return; |
| 1256 |
|
| 1257 |
if (params->dp_buf) |
| 1258 |
memset(params->dp_buf, 0, params->dp_buflen); |
| 1259 |
|
| 1260 |
nl_list_for_each_entry(obj, &cache->c_items, ce_list) { |
| 1261 |
if (filter && !nl_object_match_filter(obj, filter)) |
| 1262 |
continue; |
| 1263 |
|
| 1264 |
NL_DBG(4, "Dumping object %p...\n", obj); |
| 1265 |
dump_from_ops(obj, params); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
/** @} */ |
| 1270 |
|
| 1271 |
/** |
| 1272 |
* @name Iterators |
| 1273 |
* @{ |
| 1274 |
*/ |
| 1275 |
|
| 1276 |
/** |
| 1277 |
* Call a callback on each element of the cache. |
| 1278 |
* @arg cache cache to iterate on |
| 1279 |
* @arg cb callback function |
| 1280 |
* @arg arg argument passed to callback function |
| 1281 |
* |
| 1282 |
* Calls a callback function \a cb on each element of the \a cache. |
| 1283 |
* The argument \a arg is passed on the callback function. |
| 1284 |
*/ |
| 1285 |
void nl_cache_foreach(struct nl_cache *cache, |
| 1286 |
void (*cb)(struct nl_object *, void *), void *arg) |
| 1287 |
{ |
| 1288 |
nl_cache_foreach_filter(cache, NULL, cb, arg); |
| 1289 |
} |
| 1290 |
|
| 1291 |
/** |
| 1292 |
* Call a callback on each element of the cache (filtered). |
| 1293 |
* @arg cache cache to iterate on |
| 1294 |
* @arg filter filter object |
| 1295 |
* @arg cb callback function |
| 1296 |
* @arg arg argument passed to callback function |
| 1297 |
* |
| 1298 |
* Calls a callback function \a cb on each element of the \a cache |
| 1299 |
* that matches the \a filter. The argument \a arg is passed on |
| 1300 |
* to the callback function. |
| 1301 |
*/ |
| 1302 |
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, |
| 1303 |
void (*cb)(struct nl_object *, void *), void *arg) |
| 1304 |
{ |
| 1305 |
struct nl_object *obj, *tmp; |
| 1306 |
|
| 1307 |
if (cache->c_ops == NULL) |
| 1308 |
BUG(); |
| 1309 |
|
| 1310 |
nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { |
| 1311 |
if (filter) { |
| 1312 |
int diff = nl_object_match_filter(obj, filter); |
| 1313 |
|
| 1314 |
NL_DBG(3, "%p<->%p object difference: %x\n", |
| 1315 |
obj, filter, diff); |
| 1316 |
|
| 1317 |
if (!diff) |
| 1318 |
continue; |
| 1319 |
} |
| 1320 |
|
| 1321 |
/* Caller may hold obj for a long time */ |
| 1322 |
nl_object_get(obj); |
| 1323 |
|
| 1324 |
cb(obj, arg); |
| 1325 |
|
| 1326 |
nl_object_put(obj); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
/** @} */ |
| 1331 |
|
| 1332 |
/** @} */ |