cache.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. * Related sections in the development guide:
  40. * - @core_doc{core_cache, Caching System}
  41. *
  42. * @{
  43. *
  44. * Header
  45. * ------
  46. * ~~~~{.c}
  47. * #include <netlink/cache.h>
  48. * ~~~~
  49. */
  50. #include <netlink-private/netlink.h>
  51. #include <netlink/netlink.h>
  52. #include <netlink/cache.h>
  53. #include <netlink/object.h>
  54. #include <netlink/hashtable.h>
  55. #include <netlink/utils.h>
  56. /**
  57. * @name Access Functions
  58. * @{
  59. */
  60. /**
  61. * Return the number of items in the cache
  62. * @arg cache cache handle
  63. */
  64. int nl_cache_nitems(struct nl_cache *cache)
  65. {
  66. return cache->c_nitems;
  67. }
  68. /**
  69. * Return the number of items matching a filter in the cache
  70. * @arg cache Cache object.
  71. * @arg filter Filter object.
  72. */
  73. int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
  74. {
  75. struct nl_object *obj;
  76. int nitems = 0;
  77. if (cache->c_ops == NULL)
  78. BUG();
  79. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  80. if (filter && !nl_object_match_filter(obj, filter))
  81. continue;
  82. nitems++;
  83. }
  84. return nitems;
  85. }
  86. /**
  87. * Returns \b true if the cache is empty.
  88. * @arg cache Cache to check
  89. * @return \a true if the cache is empty, otherwise \b false is returned.
  90. */
  91. int nl_cache_is_empty(struct nl_cache *cache)
  92. {
  93. return nl_list_empty(&cache->c_items);
  94. }
  95. /**
  96. * Return the operations set of the cache
  97. * @arg cache cache handle
  98. */
  99. struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
  100. {
  101. return cache->c_ops;
  102. }
  103. /**
  104. * Return the first element in the cache
  105. * @arg cache cache handle
  106. */
  107. struct nl_object *nl_cache_get_first(struct nl_cache *cache)
  108. {
  109. if (nl_list_empty(&cache->c_items))
  110. return NULL;
  111. return nl_list_entry(cache->c_items.next,
  112. struct nl_object, ce_list);
  113. }
  114. /**
  115. * Return the last element in the cache
  116. * @arg cache cache handle
  117. */
  118. struct nl_object *nl_cache_get_last(struct nl_cache *cache)
  119. {
  120. if (nl_list_empty(&cache->c_items))
  121. return NULL;
  122. return nl_list_entry(cache->c_items.prev,
  123. struct nl_object, ce_list);
  124. }
  125. /**
  126. * Return the next element in the cache
  127. * @arg obj current object
  128. */
  129. struct nl_object *nl_cache_get_next(struct nl_object *obj)
  130. {
  131. if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
  132. return NULL;
  133. else
  134. return nl_list_entry(obj->ce_list.next,
  135. struct nl_object, ce_list);
  136. }
  137. /**
  138. * Return the previous element in the cache
  139. * @arg obj current object
  140. */
  141. struct nl_object *nl_cache_get_prev(struct nl_object *obj)
  142. {
  143. if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
  144. return NULL;
  145. else
  146. return nl_list_entry(obj->ce_list.prev,
  147. struct nl_object, ce_list);
  148. }
  149. /** @} */
  150. /**
  151. * @name Cache Allocation/Deletion
  152. * @{
  153. */
  154. /**
  155. * Allocate new cache
  156. * @arg ops Cache operations
  157. *
  158. * Allocate and initialize a new cache based on the cache operations
  159. * provided.
  160. *
  161. * @return Allocated cache or NULL if allocation failed.
  162. */
  163. struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
  164. {
  165. struct nl_cache *cache;
  166. cache = calloc(1, sizeof(*cache));
  167. if (!cache)
  168. return NULL;
  169. nl_init_list_head(&cache->c_items);
  170. cache->c_ops = ops;
  171. cache->c_flags |= ops->co_flags;
  172. cache->c_refcnt = 1;
  173. /*
  174. * If object type provides a hash keygen
  175. * functions, allocate a hash table for the
  176. * cache objects for faster lookups
  177. */
  178. if (ops->co_obj_ops->oo_keygen) {
  179. int hashtable_size;
  180. if (ops->co_hash_size)
  181. hashtable_size = ops->co_hash_size;
  182. else
  183. hashtable_size = NL_MAX_HASH_ENTRIES;
  184. cache->hashtable = nl_hash_table_alloc(hashtable_size);
  185. }
  186. NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
  187. return cache;
  188. }
  189. /**
  190. * Allocate new cache and fill it
  191. * @arg ops Cache operations
  192. * @arg sock Netlink socket
  193. * @arg result Result pointer
  194. *
  195. * Allocate new cache and fill it. Equivalent to calling:
  196. * @code
  197. * cache = nl_cache_alloc(ops);
  198. * nl_cache_refill(sock, cache);
  199. * @endcode
  200. *
  201. * @see nl_cache_alloc
  202. *
  203. * @return 0 on success or a negative error code.
  204. */
  205. int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
  206. struct nl_cache **result)
  207. {
  208. struct nl_cache *cache;
  209. int err;
  210. if (!(cache = nl_cache_alloc(ops)))
  211. return -NLE_NOMEM;
  212. if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
  213. nl_cache_free(cache);
  214. return err;
  215. }
  216. *result = cache;
  217. return 0;
  218. }
  219. /**
  220. * Allocate new cache based on type name
  221. * @arg kind Name of cache type
  222. * @arg result Result pointer
  223. *
  224. * Lookup cache ops via nl_cache_ops_lookup() and allocate the cache
  225. * by calling nl_cache_alloc(). Stores the allocated cache in the
  226. * result pointer provided.
  227. *
  228. * @see nl_cache_alloc
  229. *
  230. * @return 0 on success or a negative error code.
  231. */
  232. int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
  233. {
  234. struct nl_cache_ops *ops;
  235. struct nl_cache *cache;
  236. ops = nl_cache_ops_lookup_safe(kind);
  237. if (!ops)
  238. return -NLE_NOCACHE;
  239. cache = nl_cache_alloc(ops);
  240. nl_cache_ops_put(ops);
  241. if (!cache)
  242. return -NLE_NOMEM;
  243. *result = cache;
  244. return 0;
  245. }
  246. /**
  247. * Allocate new cache containing a subset of an existing cache
  248. * @arg orig Original cache to base new cache on
  249. * @arg filter Filter defining the subset to be filled into the new cache
  250. *
  251. * Allocates a new cache matching the type of the cache specified by
  252. * \p orig. Iterates over the \p orig cache applying the specified
  253. * \p filter and copies all objects that match to the new cache.
  254. *
  255. * The copied objects are clones but do not contain a reference to each
  256. * other. Later modifications to objects in the original cache will
  257. * not affect objects in the new cache.
  258. *
  259. * @return A newly allocated cache or NULL.
  260. */
  261. struct nl_cache *nl_cache_subset(struct nl_cache *orig,
  262. struct nl_object *filter)
  263. {
  264. struct nl_cache *cache;
  265. struct nl_object *obj;
  266. if (!filter)
  267. BUG();
  268. cache = nl_cache_alloc(orig->c_ops);
  269. if (!cache)
  270. return NULL;
  271. NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n",
  272. orig, nl_cache_name(orig), filter, cache);
  273. nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
  274. if (!nl_object_match_filter(obj, filter))
  275. continue;
  276. nl_cache_add(cache, obj);
  277. }
  278. return cache;
  279. }
  280. /**
  281. * Allocate new cache and copy the contents of an existing cache
  282. * @arg cache Original cache to base new cache on
  283. *
  284. * Allocates a new cache matching the type of the cache specified by
  285. * \p cache. Iterates over the \p cache cache and copies all objects
  286. * to the new cache.
  287. *
  288. * The copied objects are clones but do not contain a reference to each
  289. * other. Later modifications to objects in the original cache will
  290. * not affect objects in the new cache.
  291. *
  292. * @return A newly allocated cache or NULL.
  293. */
  294. struct nl_cache *nl_cache_clone(struct nl_cache *cache)
  295. {
  296. struct nl_cache_ops *ops = nl_cache_get_ops(cache);
  297. struct nl_cache *clone;
  298. struct nl_object *obj;
  299. clone = nl_cache_alloc(ops);
  300. if (!clone)
  301. return NULL;
  302. NL_DBG(2, "Cloning %p into %p\n", cache, clone);
  303. nl_list_for_each_entry(obj, &cache->c_items, ce_list)
  304. nl_cache_add(clone, obj);
  305. return clone;
  306. }
  307. /**
  308. * Remove all objects of a cache.
  309. * @arg cache Cache to clear
  310. *
  311. * The objects are unliked/removed from the cache by calling
  312. * nl_cache_remove() on each object in the cache. If any of the objects
  313. * to not contain any further references to them, those objects will
  314. * be freed.
  315. *
  316. * Unlike with nl_cache_free(), the cache is not freed just emptied.
  317. */
  318. void nl_cache_clear(struct nl_cache *cache)
  319. {
  320. struct nl_object *obj, *tmp;
  321. NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
  322. nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
  323. nl_cache_remove(obj);
  324. }
  325. static void __nl_cache_free(struct nl_cache *cache)
  326. {
  327. nl_cache_clear(cache);
  328. if (cache->hashtable)
  329. nl_hash_table_free(cache->hashtable);
  330. NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
  331. free(cache);
  332. }
  333. /**
  334. * Increase reference counter of cache
  335. * @arg cache Cache
  336. */
  337. void nl_cache_get(struct nl_cache *cache)
  338. {
  339. cache->c_refcnt++;
  340. NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n",
  341. cache, nl_cache_name(cache), cache->c_refcnt);
  342. }
  343. /**
  344. * Free a cache.
  345. * @arg cache Cache to free.
  346. *
  347. * Calls nl_cache_clear() to remove all objects associated with the
  348. * cache and frees the cache afterwards.
  349. *
  350. * @see nl_cache_clear()
  351. */
  352. void nl_cache_free(struct nl_cache *cache)
  353. {
  354. if (!cache)
  355. return;
  356. cache->c_refcnt--;
  357. NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n",
  358. cache, nl_cache_name(cache), cache->c_refcnt);
  359. if (cache->c_refcnt <= 0)
  360. __nl_cache_free(cache);
  361. }
  362. void nl_cache_put(struct nl_cache *cache)
  363. {
  364. return nl_cache_free(cache);
  365. }
  366. /** @} */
  367. /**
  368. * @name Cache Modifications
  369. * @{
  370. */
  371. static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
  372. {
  373. int ret;
  374. obj->ce_cache = cache;
  375. if (cache->hashtable) {
  376. ret = nl_hash_table_add(cache->hashtable, obj);
  377. if (ret < 0) {
  378. obj->ce_cache = NULL;
  379. return ret;
  380. }
  381. }
  382. nl_list_add_tail(&obj->ce_list, &cache->c_items);
  383. cache->c_nitems++;
  384. NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n",
  385. obj, cache, nl_cache_name(cache), cache->c_nitems);
  386. return 0;
  387. }
  388. /**
  389. * Add object to cache.
  390. * @arg cache Cache
  391. * @arg obj Object to be added to the cache
  392. *
  393. * Adds the object \p obj to the specified \p cache. In case the object
  394. * is already associated with another cache, the object is cloned before
  395. * adding it to the cache. In this case, the sole reference to the object
  396. * will be the one of the cache. Therefore clearing/freeing the cache
  397. * will result in the object being freed again.
  398. *
  399. * If the object has not been associated with a cache yet, the reference
  400. * counter of the object is incremented to account for the additional
  401. * reference.
  402. *
  403. * The type of the object and cache must match, otherwise an error is
  404. * returned (-NLE_OBJ_MISMATCH).
  405. *
  406. * @see nl_cache_move()
  407. *
  408. * @return 0 or a negative error code.
  409. */
  410. int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
  411. {
  412. struct nl_object *new;
  413. int ret = 0;
  414. if (cache->c_ops->co_obj_ops != obj->ce_ops)
  415. return -NLE_OBJ_MISMATCH;
  416. if (!nl_list_empty(&obj->ce_list)) {
  417. NL_DBG(3, "Object %p already in cache, cloning new object\n", obj);
  418. new = nl_object_clone(obj);
  419. if (!new)
  420. return -NLE_NOMEM;
  421. } else {
  422. nl_object_get(obj);
  423. new = obj;
  424. }
  425. ret = __cache_add(cache, new);
  426. if (ret < 0)
  427. nl_object_put(new);
  428. return ret;
  429. }
  430. /**
  431. * Move object from one cache to another
  432. * @arg cache Cache to move object to.
  433. * @arg obj Object subject to be moved
  434. *
  435. * Removes the the specified object \p obj from its associated cache
  436. * and moves it to another cache.
  437. *
  438. * If the object is not associated with a cache, the function behaves
  439. * just like nl_cache_add().
  440. *
  441. * The type of the object and cache must match, otherwise an error is
  442. * returned (-NLE_OBJ_MISMATCH).
  443. *
  444. * @see nl_cache_add()
  445. *
  446. * @return 0 on success or a negative error code.
  447. */
  448. int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
  449. {
  450. if (cache->c_ops->co_obj_ops != obj->ce_ops)
  451. return -NLE_OBJ_MISMATCH;
  452. NL_DBG(3, "Moving object %p from cache %p to cache %p\n",
  453. obj, obj->ce_cache, cache);
  454. /* Acquire reference, if already in a cache this will be
  455. * reverted during removal */
  456. nl_object_get(obj);
  457. if (!nl_list_empty(&obj->ce_list))
  458. nl_cache_remove(obj);
  459. return __cache_add(cache, obj);
  460. }
  461. /**
  462. * Remove object from cache.
  463. * @arg obj Object to remove from cache
  464. *
  465. * Removes the object \c obj from the cache it is associated with. The
  466. * reference counter of the object will be decremented. If the reference
  467. * to the object was the only one remaining, the object will be freed.
  468. *
  469. * If no cache is associated with the object, this function is a NOP.
  470. */
  471. void nl_cache_remove(struct nl_object *obj)
  472. {
  473. int ret;
  474. struct nl_cache *cache = obj->ce_cache;
  475. if (cache == NULL)
  476. return;
  477. if (cache->hashtable) {
  478. ret = nl_hash_table_del(cache->hashtable, obj);
  479. if (ret < 0)
  480. NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n",
  481. obj, cache, nl_cache_name(cache));
  482. }
  483. nl_list_del(&obj->ce_list);
  484. obj->ce_cache = NULL;
  485. nl_object_put(obj);
  486. cache->c_nitems--;
  487. NL_DBG(2, "Deleted object %p from cache %p <%s>.\n",
  488. obj, cache, nl_cache_name(cache));
  489. }
  490. /** @} */
  491. /**
  492. * @name Synchronization
  493. * @{
  494. */
  495. /**
  496. * Set synchronization arg1 of cache
  497. * @arg cache Cache
  498. * @arg arg argument
  499. *
  500. * Synchronization arguments are used to specify filters when
  501. * requesting dumps from the kernel.
  502. */
  503. void nl_cache_set_arg1(struct nl_cache *cache, int arg)
  504. {
  505. cache->c_iarg1 = arg;
  506. }
  507. /**
  508. * Set synchronization arg2 of cache
  509. * @arg cache Cache
  510. * @arg arg argument
  511. *
  512. * Synchronization arguments are used to specify filters when
  513. * requesting dumps from the kernel.
  514. */
  515. void nl_cache_set_arg2(struct nl_cache *cache, int arg)
  516. {
  517. cache->c_iarg2 = arg;
  518. }
  519. /**
  520. * Set cache flags
  521. * @arg cache Cache
  522. * @arg flags Flags
  523. */
  524. void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
  525. {
  526. cache->c_flags |= flags;
  527. }
  528. /**
  529. * Invoke the request-update operation
  530. * @arg sk Netlink socket.
  531. * @arg cache Cache
  532. *
  533. * This function causes the \e request-update function of the cache
  534. * operations to be invoked. This usually causes a dump request to
  535. * be sent over the netlink socket which triggers the kernel to dump
  536. * all objects of a specific type to be dumped onto the netlink
  537. * socket for pickup.
  538. *
  539. * The behaviour of this function depends on the implemenation of
  540. * the \e request_update function of each individual type of cache.
  541. *
  542. * This function will not have any effects on the cache (unless the
  543. * request_update implementation of the cache operations does so).
  544. *
  545. * Use nl_cache_pickup() to pick-up (read) the objects from the socket
  546. * and fill them into the cache.
  547. *
  548. * @see nl_cache_pickup(), nl_cache_resync()
  549. *
  550. * @return 0 on success or a negative error code.
  551. */
  552. static int nl_cache_request_full_dump(struct nl_sock *sk,
  553. struct nl_cache *cache)
  554. {
  555. if (sk->s_proto != cache->c_ops->co_protocol)
  556. return -NLE_PROTO_MISMATCH;
  557. if (cache->c_ops->co_request_update == NULL)
  558. return -NLE_OPNOTSUPP;
  559. NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n",
  560. cache, nl_cache_name(cache));
  561. return cache->c_ops->co_request_update(cache, sk);
  562. }
  563. /** @cond SKIP */
  564. struct update_xdata {
  565. struct nl_cache_ops *ops;
  566. struct nl_parser_param *params;
  567. };
  568. static int update_msg_parser(struct nl_msg *msg, void *arg)
  569. {
  570. struct update_xdata *x = arg;
  571. int ret = 0;
  572. ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
  573. if (ret == -NLE_EXIST)
  574. return NL_SKIP;
  575. else
  576. return ret;
  577. }
  578. /** @endcond */
  579. /**
  580. * Pick-up a netlink request-update with your own parser
  581. * @arg sk Netlink socket
  582. * @arg cache Cache
  583. * @arg param Parser parameters
  584. */
  585. static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
  586. struct nl_parser_param *param)
  587. {
  588. int err;
  589. struct nl_cb *cb;
  590. struct update_xdata x = {
  591. .ops = cache->c_ops,
  592. .params = param,
  593. };
  594. NL_DBG(2, "Picking up answer for cache %p <%s>\n",
  595. cache, nl_cache_name(cache));
  596. cb = nl_cb_clone(sk->s_cb);
  597. if (cb == NULL)
  598. return -NLE_NOMEM;
  599. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
  600. err = nl_recvmsgs(sk, cb);
  601. if (err < 0)
  602. NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned %d: %s\n",
  603. cache, nl_cache_name(cache), err, nl_geterror(err));
  604. nl_cb_put(cb);
  605. return err;
  606. }
  607. static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
  608. {
  609. struct nl_cache *cache = (struct nl_cache *)p->pp_arg;
  610. struct nl_object *old;
  611. old = nl_cache_search(cache, c);
  612. if (old) {
  613. if (nl_object_update(old, c) == 0) {
  614. nl_object_put(old);
  615. return 0;
  616. }
  617. nl_cache_remove(old);
  618. nl_object_put(old);
  619. }
  620. return nl_cache_add(cache, c);
  621. }
  622. /**
  623. * Pickup a netlink dump response and put it into a cache.
  624. * @arg sk Netlink socket.
  625. * @arg cache Cache to put items into.
  626. *
  627. * Waits for netlink messages to arrive, parses them and puts them into
  628. * the specified cache. If an old object with same key attributes is
  629. * present in the cache, it is replaced with the new object.
  630. * If the old object type supports an update operation, an update is
  631. * attempted before a replace.
  632. *
  633. * @return 0 on success or a negative error code.
  634. */
  635. int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
  636. {
  637. struct nl_parser_param p = {
  638. .pp_cb = pickup_cb,
  639. .pp_arg = cache,
  640. };
  641. if (sk->s_proto != cache->c_ops->co_protocol)
  642. return -NLE_PROTO_MISMATCH;
  643. return __cache_pickup(sk, cache, &p);
  644. }
  645. static int cache_include(struct nl_cache *cache, struct nl_object *obj,
  646. struct nl_msgtype *type, change_func_t cb, void *data)
  647. {
  648. struct nl_object *old;
  649. switch (type->mt_act) {
  650. case NL_ACT_NEW:
  651. case NL_ACT_DEL:
  652. old = nl_cache_search(cache, obj);
  653. if (old) {
  654. /*
  655. * Some objects types might support merging the new
  656. * object with the old existing cache object.
  657. * Handle them first.
  658. */
  659. if (nl_object_update(old, obj) == 0) {
  660. if (cb)
  661. cb(cache, old, NL_ACT_CHANGE, data);
  662. nl_object_put(old);
  663. return 0;
  664. }
  665. nl_cache_remove(old);
  666. if (type->mt_act == NL_ACT_DEL) {
  667. if (cb)
  668. cb(cache, old, NL_ACT_DEL, data);
  669. nl_object_put(old);
  670. }
  671. }
  672. if (type->mt_act == NL_ACT_NEW) {
  673. nl_cache_move(cache, obj);
  674. if (old == NULL && cb)
  675. cb(cache, obj, NL_ACT_NEW, data);
  676. else if (old) {
  677. if (nl_object_diff(old, obj) && cb)
  678. cb(cache, obj, NL_ACT_CHANGE, data);
  679. nl_object_put(old);
  680. }
  681. }
  682. break;
  683. default:
  684. NL_DBG(2, "Unknown action associated to object %p\n", obj);
  685. return 0;
  686. }
  687. return 0;
  688. }
  689. int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
  690. change_func_t change_cb, void *data)
  691. {
  692. struct nl_cache_ops *ops = cache->c_ops;
  693. int i;
  694. if (ops->co_obj_ops != obj->ce_ops)
  695. return -NLE_OBJ_MISMATCH;
  696. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
  697. if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
  698. return cache_include(cache, obj, &ops->co_msgtypes[i],
  699. change_cb, data);
  700. NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n",
  701. obj, cache, nl_cache_name(cache));
  702. return -NLE_MSGTYPE_NOSUPPORT;
  703. }
  704. static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
  705. {
  706. struct nl_cache_assoc *ca = p->pp_arg;
  707. return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
  708. }
  709. int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
  710. change_func_t change_cb, void *data)
  711. {
  712. struct nl_object *obj, *next;
  713. struct nl_af_group *grp;
  714. struct nl_cache_assoc ca = {
  715. .ca_cache = cache,
  716. .ca_change = change_cb,
  717. .ca_change_data = data,
  718. };
  719. struct nl_parser_param p = {
  720. .pp_cb = resync_cb,
  721. .pp_arg = &ca,
  722. };
  723. int err;
  724. if (sk->s_proto != cache->c_ops->co_protocol)
  725. return -NLE_PROTO_MISMATCH;
  726. NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
  727. /* Mark all objects so we can see if some of them are obsolete */
  728. nl_cache_mark_all(cache);
  729. grp = cache->c_ops->co_groups;
  730. do {
  731. if (grp && grp->ag_group &&
  732. (cache->c_flags & NL_CACHE_AF_ITER))
  733. nl_cache_set_arg1(cache, grp->ag_family);
  734. restart:
  735. err = nl_cache_request_full_dump(sk, cache);
  736. if (err < 0)
  737. goto errout;
  738. err = __cache_pickup(sk, cache, &p);
  739. if (err == -NLE_DUMP_INTR)
  740. goto restart;
  741. else if (err < 0)
  742. goto errout;
  743. if (grp)
  744. grp++;
  745. } while (grp && grp->ag_group &&
  746. (cache->c_flags & NL_CACHE_AF_ITER));
  747. nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
  748. if (nl_object_is_marked(obj)) {
  749. nl_object_get(obj);
  750. nl_cache_remove(obj);
  751. if (change_cb)
  752. change_cb(cache, obj, NL_ACT_DEL, data);
  753. nl_object_put(obj);
  754. }
  755. }
  756. NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
  757. err = 0;
  758. errout:
  759. return err;
  760. }
  761. /** @} */
  762. /**
  763. * @name Parsing
  764. * @{
  765. */
  766. /** @cond SKIP */
  767. int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  768. struct nlmsghdr *nlh, struct nl_parser_param *params)
  769. {
  770. int i, err;
  771. if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
  772. return -NLE_MSG_TOOSHORT;
  773. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
  774. if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
  775. err = ops->co_msg_parser(ops, who, nlh, params);
  776. if (err != -NLE_OPNOTSUPP)
  777. goto errout;
  778. }
  779. }
  780. err = -NLE_MSGTYPE_NOSUPPORT;
  781. errout:
  782. return err;
  783. }
  784. /** @endcond */
  785. /**
  786. * Parse a netlink message and add it to the cache.
  787. * @arg cache cache to add element to
  788. * @arg msg netlink message
  789. *
  790. * Parses a netlink message by calling the cache specific message parser
  791. * and adds the new element to the cache. If an old object with same key
  792. * attributes is present in the cache, it is replaced with the new object.
  793. * If the old object type supports an update operation, an update is
  794. * attempted before a replace.
  795. *
  796. * @return 0 or a negative error code.
  797. */
  798. int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
  799. {
  800. struct nl_parser_param p = {
  801. .pp_cb = pickup_cb,
  802. .pp_arg = cache,
  803. };
  804. return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
  805. }
  806. /**
  807. * (Re)fill a cache with the contents in the kernel.
  808. * @arg sk Netlink socket.
  809. * @arg cache cache to update
  810. *
  811. * Clears the specified cache and fills it with the current state in
  812. * the kernel.
  813. *
  814. * @return 0 or a negative error code.
  815. */
  816. int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
  817. {
  818. struct nl_af_group *grp;
  819. int err;
  820. if (sk->s_proto != cache->c_ops->co_protocol)
  821. return -NLE_PROTO_MISMATCH;
  822. nl_cache_clear(cache);
  823. grp = cache->c_ops->co_groups;
  824. do {
  825. if (grp && grp->ag_group &&
  826. (cache->c_flags & NL_CACHE_AF_ITER))
  827. nl_cache_set_arg1(cache, grp->ag_family);
  828. restart:
  829. err = nl_cache_request_full_dump(sk, cache);
  830. if (err < 0)
  831. return err;
  832. NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
  833. cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
  834. err = nl_cache_pickup(sk, cache);
  835. if (err == -NLE_DUMP_INTR) {
  836. NL_DBG(2, "Dump interrupted, restarting!\n");
  837. goto restart;
  838. } else if (err < 0)
  839. break;
  840. if (grp)
  841. grp++;
  842. } while (grp && grp->ag_group &&
  843. (cache->c_flags & NL_CACHE_AF_ITER));
  844. return err;
  845. }
  846. /** @} */
  847. /**
  848. * @name Utillities
  849. * @{
  850. */
  851. static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
  852. struct nl_object *needle)
  853. {
  854. struct nl_object *obj;
  855. obj = nl_hash_table_lookup(cache->hashtable, needle);
  856. if (obj) {
  857. nl_object_get(obj);
  858. return obj;
  859. }
  860. return NULL;
  861. }
  862. /**
  863. * Search object in cache
  864. * @arg cache Cache
  865. * @arg needle Object to look for.
  866. *
  867. * Searches the cache for an object which matches the object \p needle.
  868. * The function nl_object_identical() is used to determine if the
  869. * objects match. If a matching object is found, the reference counter
  870. * is incremented and the object is returned.
  871. *
  872. * Therefore, if an object is returned, the reference to the object
  873. * must be returned by calling nl_object_put() after usage.
  874. *
  875. * @return Reference to object or NULL if not found.
  876. */
  877. struct nl_object *nl_cache_search(struct nl_cache *cache,
  878. struct nl_object *needle)
  879. {
  880. struct nl_object *obj;
  881. if (cache->hashtable)
  882. return __cache_fast_lookup(cache, needle);
  883. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  884. if (nl_object_identical(obj, needle)) {
  885. nl_object_get(obj);
  886. return obj;
  887. }
  888. }
  889. return NULL;
  890. }
  891. /**
  892. * Find object in cache
  893. * @arg cache Cache
  894. * @arg filter object acting as a filter
  895. *
  896. * Searches the cache for an object which matches the object filter.
  897. * If the filter attributes matches the object type id attributes,
  898. * and the cache supports hash lookups, a faster hashtable lookup
  899. * is used to return the object. Else, function nl_object_match_filter() is
  900. * used to determine if the objects match. If a matching object is
  901. * found, the reference counter is incremented and the object is returned.
  902. *
  903. * Therefore, if an object is returned, the reference to the object
  904. * must be returned by calling nl_object_put() after usage.
  905. *
  906. * @return Reference to object or NULL if not found.
  907. */
  908. struct nl_object *nl_cache_find(struct nl_cache *cache,
  909. struct nl_object *filter)
  910. {
  911. struct nl_object *obj;
  912. if (cache->c_ops == NULL)
  913. BUG();
  914. if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
  915. && cache->hashtable)
  916. return __cache_fast_lookup(cache, filter);
  917. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  918. if (nl_object_match_filter(obj, filter)) {
  919. nl_object_get(obj);
  920. return obj;
  921. }
  922. }
  923. return NULL;
  924. }
  925. /**
  926. * Mark all objects of a cache
  927. * @arg cache Cache
  928. *
  929. * Marks all objects of a cache by calling nl_object_mark() on each
  930. * object associated with the cache.
  931. */
  932. void nl_cache_mark_all(struct nl_cache *cache)
  933. {
  934. struct nl_object *obj;
  935. NL_DBG(2, "Marking all objects in cache %p <%s>\n",
  936. cache, nl_cache_name(cache));
  937. nl_list_for_each_entry(obj, &cache->c_items, ce_list)
  938. nl_object_mark(obj);
  939. }
  940. /** @} */
  941. /**
  942. * @name Dumping
  943. * @{
  944. */
  945. /**
  946. * Dump all elements of a cache.
  947. * @arg cache cache to dump
  948. * @arg params dumping parameters
  949. *
  950. * Dumps all elements of the \a cache to the file descriptor \a fd.
  951. */
  952. void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
  953. {
  954. nl_cache_dump_filter(cache, params, NULL);
  955. }
  956. /**
  957. * Dump all elements of a cache (filtered).
  958. * @arg cache cache to dump
  959. * @arg params dumping parameters (optional)
  960. * @arg filter filter object
  961. *
  962. * Dumps all elements of the \a cache to the file descriptor \a fd
  963. * given they match the given filter \a filter.
  964. */
  965. void nl_cache_dump_filter(struct nl_cache *cache,
  966. struct nl_dump_params *params,
  967. struct nl_object *filter)
  968. {
  969. int type = params ? params->dp_type : NL_DUMP_DETAILS;
  970. struct nl_object_ops *ops;
  971. struct nl_object *obj;
  972. NL_DBG(2, "Dumping cache %p <%s> with filter %p\n",
  973. cache, nl_cache_name(cache), filter);
  974. if (type > NL_DUMP_MAX || type < 0)
  975. BUG();
  976. if (cache->c_ops == NULL)
  977. BUG();
  978. ops = cache->c_ops->co_obj_ops;
  979. if (!ops->oo_dump[type])
  980. return;
  981. if (params && params->dp_buf)
  982. memset(params->dp_buf, 0, params->dp_buflen);
  983. nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
  984. if (filter && !nl_object_match_filter(obj, filter))
  985. continue;
  986. NL_DBG(4, "Dumping object %p...\n", obj);
  987. dump_from_ops(obj, params);
  988. }
  989. }
  990. /** @} */
  991. /**
  992. * @name Iterators
  993. * @{
  994. */
  995. /**
  996. * Call a callback on each element of the cache.
  997. * @arg cache cache to iterate on
  998. * @arg cb callback function
  999. * @arg arg argument passed to callback function
  1000. *
  1001. * Calls a callback function \a cb on each element of the \a cache.
  1002. * The argument \a arg is passed on the callback function.
  1003. */
  1004. void nl_cache_foreach(struct nl_cache *cache,
  1005. void (*cb)(struct nl_object *, void *), void *arg)
  1006. {
  1007. nl_cache_foreach_filter(cache, NULL, cb, arg);
  1008. }
  1009. /**
  1010. * Call a callback on each element of the cache (filtered).
  1011. * @arg cache cache to iterate on
  1012. * @arg filter filter object
  1013. * @arg cb callback function
  1014. * @arg arg argument passed to callback function
  1015. *
  1016. * Calls a callback function \a cb on each element of the \a cache
  1017. * that matches the \a filter. The argument \a arg is passed on
  1018. * to the callback function.
  1019. */
  1020. void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
  1021. void (*cb)(struct nl_object *, void *), void *arg)
  1022. {
  1023. struct nl_object *obj, *tmp;
  1024. if (cache->c_ops == NULL)
  1025. BUG();
  1026. nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
  1027. if (filter) {
  1028. int diff = nl_object_match_filter(obj, filter);
  1029. NL_DBG(3, "%p<->%p object difference: %x\n",
  1030. obj, filter, diff);
  1031. if (!diff)
  1032. continue;
  1033. }
  1034. /* Caller may hold obj for a long time */
  1035. nl_object_get(obj);
  1036. cb(obj, arg);
  1037. nl_object_put(obj);
  1038. }
  1039. }
  1040. /** @} */
  1041. /** @} */