cache.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * lib/cache.c Caching Module
  3. *
  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
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup cache_mngt
  13. * @defgroup cache Cache
  14. *
  15. * @code
  16. * Cache Management | | Type Specific Cache Operations
  17. *
  18. * | | +----------------+ +------------+
  19. * | request update | | msg_parser |
  20. * | | +----------------+ +------------+
  21. * +- - - - -^- - - - - - - -^- -|- - - -
  22. * nl_cache_update: | | | |
  23. * 1) --------- co_request_update ------+ | |
  24. * | | |
  25. * 2) destroy old cache +----------- pp_cb ---------|---+
  26. * | | |
  27. * 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
  28. * +--------------+ | | | |
  29. * | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
  30. * +--------------+ | | +-------------+
  31. * | nl_recvmsgs |
  32. * | | +-----|-^-----+
  33. * +---v-|---+
  34. * | | | nl_recv |
  35. * +---------+
  36. * | | Core Netlink
  37. * @endcode
  38. *
  39. * @{
  40. */
  41. #include <netlink-local.h>
  42. #include <netlink/netlink.h>
  43. #include <netlink/cache.h>
  44. #include <netlink/object.h>
  45. #include <netlink/utils.h>
  46. /**
  47. * @name Access Functions
  48. * @{
  49. */
  50. /**
  51. * Return the number of items in the cache
  52. * @arg cache cache handle
  53. */
  54. int nl_cache_nitems(struct nl_cache *cache)
  55. {
  56. return cache->c_nitems;
  57. }
  58. /**
  59. * Return the number of items matching a filter in the cache
  60. * @arg cache Cache object.
  61. * @arg filter Filter object.
  62. */
  63. int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
  64. {
  65. struct nl_object *obj;
  66. int nitems = 0;
  67. if (cache->c_ops == NULL)
  68. BUG();
  69. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  70. if (filter && !nl_object_match_filter(obj, filter))
  71. continue;
  72. nitems++;
  73. }
  74. return nitems;
  75. }
  76. /**
  77. * Returns \b true if the cache is empty.
  78. * @arg cache Cache to check
  79. * @return \a true if the cache is empty, otherwise \b false is returned.
  80. */
  81. int nl_cache_is_empty(struct nl_cache *cache)
  82. {
  83. return nl_list_empty(&cache->c_items);
  84. }
  85. /**
  86. * Return the operations set of the cache
  87. * @arg cache cache handle
  88. */
  89. struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
  90. {
  91. return cache->c_ops;
  92. }
  93. /**
  94. * Return the first element in the cache
  95. * @arg cache cache handle
  96. */
  97. struct nl_object *nl_cache_get_first(struct nl_cache *cache)
  98. {
  99. if (nl_list_empty(&cache->c_items))
  100. return NULL;
  101. return nl_list_entry(cache->c_items.next,
  102. struct nl_object, ce_list);
  103. }
  104. /**
  105. * Return the last element in the cache
  106. * @arg cache cache handle
  107. */
  108. struct nl_object *nl_cache_get_last(struct nl_cache *cache)
  109. {
  110. if (nl_list_empty(&cache->c_items))
  111. return NULL;
  112. return nl_list_entry(cache->c_items.prev,
  113. struct nl_object, ce_list);
  114. }
  115. /**
  116. * Return the next element in the cache
  117. * @arg obj current object
  118. */
  119. struct nl_object *nl_cache_get_next(struct nl_object *obj)
  120. {
  121. if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
  122. return NULL;
  123. else
  124. return nl_list_entry(obj->ce_list.next,
  125. struct nl_object, ce_list);
  126. }
  127. /**
  128. * Return the previous element in the cache
  129. * @arg obj current object
  130. */
  131. struct nl_object *nl_cache_get_prev(struct nl_object *obj)
  132. {
  133. if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
  134. return NULL;
  135. else
  136. return nl_list_entry(obj->ce_list.prev,
  137. struct nl_object, ce_list);
  138. }
  139. /** @} */
  140. /**
  141. * @name Cache Creation/Deletion
  142. * @{
  143. */
  144. /**
  145. * Allocate an empty cache
  146. * @arg ops cache operations to base the cache on
  147. *
  148. * @return A newly allocated and initialized cache.
  149. */
  150. struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
  151. {
  152. struct nl_cache *cache;
  153. cache = calloc(1, sizeof(*cache));
  154. if (!cache) {
  155. nl_errno(ENOMEM);
  156. return NULL;
  157. }
  158. nl_init_list_head(&cache->c_items);
  159. cache->c_ops = ops;
  160. cache->c_refcnt = 1;
  161. NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
  162. return cache;
  163. }
  164. /**
  165. * Allocate an empty cache based on type name
  166. * @arg kind Name of cache type
  167. * @return A newly allocated and initialized cache.
  168. */
  169. struct nl_cache *nl_cache_alloc_name(const char *kind)
  170. {
  171. struct nl_cache_ops *ops;
  172. ops = nl_cache_ops_lookup(kind);
  173. if (!ops) {
  174. nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
  175. return NULL;
  176. }
  177. return nl_cache_alloc(ops);
  178. }
  179. /**
  180. * Allocate a new cache containing a subset of a cache
  181. * @arg orig Original cache to be based on
  182. * @arg filter Filter defining the subset to be filled into new cache
  183. * @return A newly allocated cache or NULL.
  184. */
  185. struct nl_cache *nl_cache_subset(struct nl_cache *orig,
  186. struct nl_object *filter)
  187. {
  188. struct nl_cache *cache;
  189. struct nl_object *obj;
  190. if (!filter)
  191. BUG();
  192. cache = nl_cache_alloc(orig->c_ops);
  193. if (!cache)
  194. return NULL;
  195. nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
  196. if (!nl_object_match_filter(obj, filter))
  197. continue;
  198. nl_cache_add(cache, obj);
  199. }
  200. return cache;
  201. }
  202. /**
  203. * Clear a cache.
  204. * @arg cache cache to clear
  205. *
  206. * Removes all elements of a cache.
  207. */
  208. void nl_cache_clear(struct nl_cache *cache)
  209. {
  210. struct nl_object *obj, *tmp;
  211. NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
  212. nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
  213. nl_cache_remove(obj);
  214. }
  215. static void __nl_cache_free(struct nl_cache *cache)
  216. {
  217. nl_cache_clear(cache);
  218. NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
  219. free(cache);
  220. }
  221. /**
  222. * Increase reference counter of cache
  223. * @arg cache Cache
  224. */
  225. void nl_cache_get(struct nl_cache *cache)
  226. {
  227. cache->c_refcnt++;
  228. }
  229. /**
  230. * Free a cache.
  231. * @arg cache Cache to free.
  232. *
  233. * Removes all elements of a cache and frees all memory.
  234. *
  235. * @note Use this function if you are working with allocated caches.
  236. */
  237. void nl_cache_free(struct nl_cache *cache)
  238. {
  239. if (!cache)
  240. return;
  241. cache->c_refcnt--;
  242. NL_DBG(4, "Returned cache reference %p, %d remaining\n",
  243. cache, cache->c_refcnt);
  244. if (cache->c_refcnt <= 0)
  245. __nl_cache_free(cache);
  246. }
  247. /** @} */
  248. /**
  249. * @name Cache Modifications
  250. * @{
  251. */
  252. static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
  253. {
  254. obj->ce_cache = cache;
  255. nl_list_add_tail(&obj->ce_list, &cache->c_items);
  256. cache->c_nitems++;
  257. NL_DBG(1, "Added %p to cache %p <%s>.\n",
  258. obj, cache, nl_cache_name(cache));
  259. return 0;
  260. }
  261. /**
  262. * Add object to a cache.
  263. * @arg cache Cache to add object to
  264. * @arg obj Object to be added to the cache
  265. *
  266. * Adds the given object to the specified cache. The object is cloned
  267. * if it has been added to another cache already.
  268. *
  269. * @return 0 or a negative error code.
  270. */
  271. int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
  272. {
  273. struct nl_object *new;
  274. if (cache->c_ops->co_obj_ops != obj->ce_ops)
  275. return nl_error(EINVAL, "Object mismatches cache type");
  276. if (!nl_list_empty(&obj->ce_list)) {
  277. new = nl_object_clone(obj);
  278. if (!new)
  279. return nl_errno(ENOMEM);
  280. } else {
  281. nl_object_get(obj);
  282. new = obj;
  283. }
  284. return __cache_add(cache, new);
  285. }
  286. /**
  287. * Move object from one cache to another
  288. * @arg cache Cache to move object to.
  289. * @arg obj Object subject to be moved
  290. *
  291. * Removes the given object from its associated cache if needed
  292. * and adds it to the new cache.
  293. *
  294. * @return 0 on success or a negative error code.
  295. */
  296. int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
  297. {
  298. if (cache->c_ops->co_obj_ops != obj->ce_ops)
  299. return nl_error(EINVAL, "Object mismatches cache type");
  300. NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
  301. /* Acquire reference, if already in a cache this will be
  302. * reverted during removal */
  303. nl_object_get(obj);
  304. if (!nl_list_empty(&obj->ce_list))
  305. nl_cache_remove(obj);
  306. return __cache_add(cache, obj);
  307. }
  308. /**
  309. * Removes an object from a cache.
  310. * @arg obj Object to remove from its cache
  311. *
  312. * Removes the object \c obj from the cache it is assigned to, since
  313. * an object can only be assigned to one cache at a time, the cache
  314. * must ne be passed along with it.
  315. */
  316. void nl_cache_remove(struct nl_object *obj)
  317. {
  318. struct nl_cache *cache = obj->ce_cache;
  319. if (cache == NULL)
  320. return;
  321. nl_list_del(&obj->ce_list);
  322. obj->ce_cache = NULL;
  323. nl_object_put(obj);
  324. cache->c_nitems--;
  325. NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
  326. obj, cache, nl_cache_name(cache));
  327. }
  328. /**
  329. * Search for an object in a cache
  330. * @arg cache Cache to search in.
  331. * @arg needle Object to look for.
  332. *
  333. * Iterates over the cache and looks for an object with identical
  334. * identifiers as the needle.
  335. *
  336. * @return Reference to object or NULL if not found.
  337. * @note The returned object must be returned via nl_object_put().
  338. */
  339. struct nl_object *nl_cache_search(struct nl_cache *cache,
  340. struct nl_object *needle)
  341. {
  342. struct nl_object *obj;
  343. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  344. if (nl_object_identical(obj, needle)) {
  345. nl_object_get(obj);
  346. return obj;
  347. }
  348. }
  349. return NULL;
  350. }
  351. /** @} */
  352. /**
  353. * @name Synchronization
  354. * @{
  355. */
  356. /**
  357. * Request a full dump from the kernel to fill a cache
  358. * @arg handle Netlink handle
  359. * @arg cache Cache subjected to be filled.
  360. *
  361. * Send a dumping request to the kernel causing it to dump all objects
  362. * related to the specified cache to the netlink socket.
  363. *
  364. * Use nl_cache_pickup() to read the objects from the socket and fill them
  365. * into a cache.
  366. */
  367. int nl_cache_request_full_dump(struct nl_handle *handle, struct nl_cache *cache)
  368. {
  369. NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
  370. cache, nl_cache_name(cache));
  371. if (cache->c_ops->co_request_update == NULL)
  372. return nl_error(EOPNOTSUPP, "Operation not supported");
  373. return cache->c_ops->co_request_update(cache, handle);
  374. }
  375. /** @cond SKIP */
  376. struct update_xdata {
  377. struct nl_cache_ops *ops;
  378. struct nl_parser_param *params;
  379. };
  380. static int update_msg_parser(struct nl_msg *msg, void *arg)
  381. {
  382. struct update_xdata *x = arg;
  383. return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
  384. }
  385. /** @endcond */
  386. int __cache_pickup(struct nl_handle *handle, struct nl_cache *cache,
  387. struct nl_parser_param *param)
  388. {
  389. int err;
  390. struct nl_cb *cb;
  391. struct update_xdata x = {
  392. .ops = cache->c_ops,
  393. .params = param,
  394. };
  395. NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
  396. cache, nl_cache_name(cache));
  397. cb = nl_cb_clone(handle->h_cb);
  398. if (cb == NULL)
  399. return nl_get_errno();
  400. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
  401. err = nl_recvmsgs(handle, cb);
  402. if (err < 0)
  403. NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
  404. "%d: %s", cache, nl_cache_name(cache),
  405. err, nl_geterror());
  406. nl_cb_put(cb);
  407. return err;
  408. }
  409. static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
  410. {
  411. return nl_cache_add((struct nl_cache *) p->pp_arg, c);
  412. }
  413. /**
  414. * Pickup a netlink dump response and put it into a cache.
  415. * @arg handle Netlink handle.
  416. * @arg cache Cache to put items into.
  417. *
  418. * Waits for netlink messages to arrive, parses them and puts them into
  419. * the specified cache.
  420. *
  421. * @return 0 on success or a negative error code.
  422. */
  423. int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
  424. {
  425. struct nl_parser_param p = {
  426. .pp_cb = pickup_cb,
  427. .pp_arg = cache,
  428. };
  429. return __cache_pickup(handle, cache, &p);
  430. }
  431. static int cache_include(struct nl_cache *cache, struct nl_object *obj,
  432. struct nl_msgtype *type, change_func_t cb)
  433. {
  434. struct nl_object *old;
  435. switch (type->mt_act) {
  436. case NL_ACT_NEW:
  437. case NL_ACT_DEL:
  438. old = nl_cache_search(cache, obj);
  439. if (old) {
  440. nl_cache_remove(old);
  441. if (type->mt_act == NL_ACT_DEL) {
  442. if (cb)
  443. cb(cache, old, NL_ACT_DEL);
  444. nl_object_put(old);
  445. }
  446. }
  447. if (type->mt_act == NL_ACT_NEW) {
  448. nl_cache_move(cache, obj);
  449. if (old == NULL && cb)
  450. cb(cache, obj, NL_ACT_NEW);
  451. else if (old) {
  452. if (nl_object_diff(old, obj) && cb)
  453. cb(cache, obj, NL_ACT_CHANGE);
  454. nl_object_put(old);
  455. }
  456. }
  457. break;
  458. default:
  459. NL_DBG(2, "Unknown action associated to object %p\n", obj);
  460. return 0;
  461. }
  462. return 0;
  463. }
  464. int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
  465. change_func_t change_cb)
  466. {
  467. struct nl_cache_ops *ops = cache->c_ops;
  468. int i;
  469. if (ops->co_obj_ops != obj->ce_ops)
  470. return nl_error(EINVAL, "Object mismatches cache type");
  471. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
  472. if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
  473. return cache_include(cache, obj, &ops->co_msgtypes[i],
  474. change_cb);
  475. return nl_errno(EINVAL);
  476. }
  477. static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
  478. {
  479. struct nl_cache_assoc *ca = p->pp_arg;
  480. return nl_cache_include(ca->ca_cache, c, ca->ca_change);
  481. }
  482. int nl_cache_resync(struct nl_handle *handle, struct nl_cache *cache,
  483. change_func_t change_cb)
  484. {
  485. struct nl_object *obj, *next;
  486. struct nl_cache_assoc ca = {
  487. .ca_cache = cache,
  488. .ca_change = change_cb,
  489. };
  490. struct nl_parser_param p = {
  491. .pp_cb = resync_cb,
  492. .pp_arg = &ca,
  493. };
  494. int err;
  495. NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
  496. /* Mark all objects so we can see if some of them are obsolete */
  497. nl_cache_mark_all(cache);
  498. err = nl_cache_request_full_dump(handle, cache);
  499. if (err < 0)
  500. goto errout;
  501. err = __cache_pickup(handle, cache, &p);
  502. if (err < 0)
  503. goto errout;
  504. nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
  505. if (nl_object_is_marked(obj))
  506. nl_cache_remove(obj);
  507. NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
  508. err = 0;
  509. errout:
  510. return err;
  511. }
  512. /** @} */
  513. /**
  514. * @name Parsing
  515. * @{
  516. */
  517. /** @cond SKIP */
  518. int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  519. struct nlmsghdr *nlh, struct nl_parser_param *params)
  520. {
  521. int i, err;
  522. if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) {
  523. err = nl_error(EINVAL, "netlink message too short to be "
  524. "of kind %s", ops->co_name);
  525. goto errout;
  526. }
  527. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
  528. if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
  529. err = ops->co_msg_parser(ops, who, nlh, params);
  530. if (err != -ENOENT)
  531. goto errout;
  532. }
  533. }
  534. err = nl_error(EINVAL, "Unsupported netlink message type %d",
  535. nlh->nlmsg_type);
  536. errout:
  537. return err;
  538. }
  539. /** @endcond */
  540. /**
  541. * Parse a netlink message and add it to the cache.
  542. * @arg cache cache to add element to
  543. * @arg msg netlink message
  544. *
  545. * Parses a netlink message by calling the cache specific message parser
  546. * and adds the new element to the cache.
  547. *
  548. * @return 0 or a negative error code.
  549. */
  550. int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
  551. {
  552. struct nl_parser_param p = {
  553. .pp_cb = pickup_cb,
  554. .pp_arg = cache,
  555. };
  556. return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
  557. }
  558. /**
  559. * (Re)fill a cache with the contents in the kernel.
  560. * @arg handle netlink handle
  561. * @arg cache cache to update
  562. *
  563. * Clears the specified cache and fills it with the current state in
  564. * the kernel.
  565. *
  566. * @return 0 or a negative error code.
  567. */
  568. int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
  569. {
  570. int err;
  571. err = nl_cache_request_full_dump(handle, cache);
  572. if (err < 0)
  573. return err;
  574. NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
  575. cache, nl_cache_name(cache));
  576. nl_cache_clear(cache);
  577. return nl_cache_pickup(handle, cache);
  578. }
  579. /** @} */
  580. /**
  581. * @name Utillities
  582. * @{
  583. */
  584. /**
  585. * Mark all objects in a cache
  586. * @arg cache Cache to mark all objects in
  587. */
  588. void nl_cache_mark_all(struct nl_cache *cache)
  589. {
  590. struct nl_object *obj;
  591. NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
  592. cache, nl_cache_name(cache));
  593. nl_list_for_each_entry(obj, &cache->c_items, ce_list)
  594. nl_object_mark(obj);
  595. }
  596. /** @} */
  597. /**
  598. * @name Dumping
  599. * @{
  600. */
  601. /**
  602. * Dump all elements of a cache.
  603. * @arg cache cache to dump
  604. * @arg params dumping parameters
  605. *
  606. * Dumps all elements of the \a cache to the file descriptor \a fd.
  607. */
  608. void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
  609. {
  610. nl_cache_dump_filter(cache, params, NULL);
  611. }
  612. /**
  613. * Dump all elements of a cache (filtered).
  614. * @arg cache cache to dump
  615. * @arg params dumping parameters (optional)
  616. * @arg filter filter object
  617. *
  618. * Dumps all elements of the \a cache to the file descriptor \a fd
  619. * given they match the given filter \a filter.
  620. */
  621. void nl_cache_dump_filter(struct nl_cache *cache,
  622. struct nl_dump_params *params,
  623. struct nl_object *filter)
  624. {
  625. int type = params ? params->dp_type : NL_DUMP_FULL;
  626. struct nl_object_ops *ops;
  627. struct nl_object *obj;
  628. NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
  629. cache, nl_cache_name(cache), filter);
  630. if (type > NL_DUMP_MAX || type < 0)
  631. BUG();
  632. if (cache->c_ops == NULL)
  633. BUG();
  634. ops = cache->c_ops->co_obj_ops;
  635. if (!ops->oo_dump[type])
  636. return;
  637. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  638. if (filter && !nl_object_match_filter(obj, filter))
  639. continue;
  640. NL_DBG(4, "Dumping object %p...\n", obj);
  641. dump_from_ops(obj, params);
  642. }
  643. }
  644. /** @} */
  645. /**
  646. * @name Iterators
  647. * @{
  648. */
  649. /**
  650. * Call a callback on each element of the cache.
  651. * @arg cache cache to iterate on
  652. * @arg cb callback function
  653. * @arg arg argument passed to callback function
  654. *
  655. * Calls a callback function \a cb on each element of the \a cache.
  656. * The argument \a arg is passed on the callback function.
  657. */
  658. void nl_cache_foreach(struct nl_cache *cache,
  659. void (*cb)(struct nl_object *, void *), void *arg)
  660. {
  661. nl_cache_foreach_filter(cache, NULL, cb, arg);
  662. }
  663. /**
  664. * Call a callback on each element of the cache (filtered).
  665. * @arg cache cache to iterate on
  666. * @arg filter filter object
  667. * @arg cb callback function
  668. * @arg arg argument passed to callback function
  669. *
  670. * Calls a callback function \a cb on each element of the \a cache
  671. * that matches the \a filter. The argument \a arg is passed on
  672. * to the callback function.
  673. */
  674. void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
  675. void (*cb)(struct nl_object *, void *), void *arg)
  676. {
  677. struct nl_object *obj, *tmp;
  678. if (cache->c_ops == NULL)
  679. BUG();
  680. nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
  681. if (filter && !nl_object_match_filter(obj, filter))
  682. continue;
  683. cb(obj, arg);
  684. }
  685. }
  686. /** @} */
  687. /** @} */