cache.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <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 Cache Creation/Deletion
  48. * @{
  49. */
  50. /**
  51. * Allocate an empty cache
  52. * @arg ops cache operations to base the cache on
  53. *
  54. * @return A newly allocated and initialized cache.
  55. */
  56. struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
  57. {
  58. struct nl_cache *cache;
  59. cache = calloc(1, sizeof(*cache));
  60. if (!cache)
  61. return NULL;
  62. nl_init_list_head(&cache->c_items);
  63. cache->c_ops = ops;
  64. NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
  65. return cache;
  66. }
  67. int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
  68. struct nl_cache **result)
  69. {
  70. struct nl_cache *cache;
  71. int err;
  72. if (!(cache = nl_cache_alloc(ops)))
  73. return -NLE_NOMEM;
  74. if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
  75. nl_cache_free(cache);
  76. return err;
  77. }
  78. *result = cache;
  79. return 0;
  80. }
  81. /**
  82. * Clear a cache.
  83. * @arg cache cache to clear
  84. *
  85. * Removes all elements of a cache.
  86. */
  87. void nl_cache_clear(struct nl_cache *cache)
  88. {
  89. struct nl_object *obj, *tmp;
  90. NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
  91. nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
  92. nl_cache_remove(obj);
  93. }
  94. /**
  95. * Free a cache.
  96. * @arg cache Cache to free.
  97. *
  98. * Removes all elements of a cache and frees all memory.
  99. *
  100. * @note Use this function if you are working with allocated caches.
  101. */
  102. void nl_cache_free(struct nl_cache *cache)
  103. {
  104. if (!cache)
  105. return;
  106. nl_cache_clear(cache);
  107. NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
  108. free(cache);
  109. }
  110. /** @} */
  111. /**
  112. * @name Cache Modifications
  113. * @{
  114. */
  115. static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
  116. {
  117. obj->ce_cache = cache;
  118. nl_list_add_tail(&obj->ce_list, &cache->c_items);
  119. cache->c_nitems++;
  120. NL_DBG(1, "Added %p to cache %p <%s>.\n",
  121. obj, cache, nl_cache_name(cache));
  122. return 0;
  123. }
  124. /**
  125. * Add object to a cache.
  126. * @arg cache Cache to add object to
  127. * @arg obj Object to be added to the cache
  128. *
  129. * Adds the given object to the specified cache. The object is cloned
  130. * if it has been added to another cache already.
  131. *
  132. * @return 0 or a negative error code.
  133. */
  134. int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
  135. {
  136. struct nl_object *new;
  137. if (cache->c_ops->co_obj_ops != obj->ce_ops)
  138. return -NLE_OBJ_MISMATCH;
  139. if (!nl_list_empty(&obj->ce_list)) {
  140. new = nl_object_clone(obj);
  141. if (!new)
  142. return -NLE_NOMEM;
  143. } else {
  144. nl_object_get(obj);
  145. new = obj;
  146. }
  147. return __cache_add(cache, new);
  148. }
  149. /**
  150. * Removes an object from a cache.
  151. * @arg obj Object to remove from its cache
  152. *
  153. * Removes the object \c obj from the cache it is assigned to, since
  154. * an object can only be assigned to one cache at a time, the cache
  155. * must ne be passed along with it.
  156. */
  157. void nl_cache_remove(struct nl_object *obj)
  158. {
  159. struct nl_cache *cache = obj->ce_cache;
  160. if (cache == NULL)
  161. return;
  162. nl_list_del(&obj->ce_list);
  163. obj->ce_cache = NULL;
  164. nl_object_put(obj);
  165. cache->c_nitems--;
  166. NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
  167. obj, cache, nl_cache_name(cache));
  168. }
  169. /** @} */
  170. /**
  171. * @name Synchronization
  172. * @{
  173. */
  174. /**
  175. * Request a full dump from the kernel to fill a cache
  176. * @arg sk Netlink socket.
  177. * @arg cache Cache subjected to be filled.
  178. *
  179. * Send a dumping request to the kernel causing it to dump all objects
  180. * related to the specified cache to the netlink socket.
  181. *
  182. * Use nl_cache_pickup() to read the objects from the socket and fill them
  183. * into a cache.
  184. */
  185. int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache)
  186. {
  187. NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
  188. cache, nl_cache_name(cache));
  189. if (cache->c_ops->co_request_update == NULL)
  190. return -NLE_OPNOTSUPP;
  191. return cache->c_ops->co_request_update(cache, sk);
  192. }
  193. /** @cond SKIP */
  194. struct update_xdata {
  195. struct nl_cache_ops *ops;
  196. struct nl_parser_param *params;
  197. };
  198. static int update_msg_parser(struct nl_msg *msg, void *arg)
  199. {
  200. struct update_xdata *x = arg;
  201. return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
  202. }
  203. /** @endcond */
  204. int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
  205. struct nl_parser_param *param)
  206. {
  207. int err;
  208. struct nl_cb *cb;
  209. struct update_xdata x = {
  210. .ops = cache->c_ops,
  211. .params = param,
  212. };
  213. NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
  214. cache, nl_cache_name(cache));
  215. cb = nl_cb_clone(sk->s_cb);
  216. if (cb == NULL)
  217. return -NLE_NOMEM;
  218. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
  219. err = nl_recvmsgs(sk, cb);
  220. if (err < 0)
  221. NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
  222. "%d: %s", cache, nl_cache_name(cache),
  223. err, nl_geterror(err));
  224. nl_cb_put(cb);
  225. return err;
  226. }
  227. static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
  228. {
  229. return nl_cache_add((struct nl_cache *) p->pp_arg, c);
  230. }
  231. /**
  232. * Pickup a netlink dump response and put it into a cache.
  233. * @arg sk Netlink socket.
  234. * @arg cache Cache to put items into.
  235. *
  236. * Waits for netlink messages to arrive, parses them and puts them into
  237. * the specified cache.
  238. *
  239. * @return 0 on success or a negative error code.
  240. */
  241. int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
  242. {
  243. struct nl_parser_param p = {
  244. .pp_cb = pickup_cb,
  245. .pp_arg = cache,
  246. };
  247. return __cache_pickup(sk, cache, &p);
  248. }
  249. /** @} */
  250. /**
  251. * @name Parsing
  252. * @{
  253. */
  254. /** @cond SKIP */
  255. int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  256. struct nlmsghdr *nlh, struct nl_parser_param *params)
  257. {
  258. int i, err;
  259. if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
  260. return -NLE_MSG_TOOSHORT;
  261. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
  262. if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
  263. err = ops->co_msg_parser(ops, who, nlh, params);
  264. if (err != -NLE_OPNOTSUPP)
  265. goto errout;
  266. }
  267. }
  268. err = -NLE_MSGTYPE_NOSUPPORT;
  269. errout:
  270. return err;
  271. }
  272. /** @endcond */
  273. /**
  274. * Parse a netlink message and add it to the cache.
  275. * @arg cache cache to add element to
  276. * @arg msg netlink message
  277. *
  278. * Parses a netlink message by calling the cache specific message parser
  279. * and adds the new element to the cache.
  280. *
  281. * @return 0 or a negative error code.
  282. */
  283. int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
  284. {
  285. struct nl_parser_param p = {
  286. .pp_cb = pickup_cb,
  287. .pp_arg = cache,
  288. };
  289. return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
  290. }
  291. /**
  292. * (Re)fill a cache with the contents in the kernel.
  293. * @arg sk Netlink socket.
  294. * @arg cache cache to update
  295. *
  296. * Clears the specified cache and fills it with the current state in
  297. * the kernel.
  298. *
  299. * @return 0 or a negative error code.
  300. */
  301. int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
  302. {
  303. int err;
  304. err = nl_cache_request_full_dump(sk, cache);
  305. if (err < 0)
  306. return err;
  307. NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
  308. cache, nl_cache_name(cache));
  309. nl_cache_clear(cache);
  310. return nl_cache_pickup(sk, cache);
  311. }
  312. /** @} */