cache.c 19 KB

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