cache_mngr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * lib/cache_mngr.c Cache Manager
  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_mngr Manager
  14. * @brief Manager keeping caches up to date automatically.
  15. *
  16. * The cache manager keeps caches up to date automatically by listening to
  17. * netlink notifications and integrating the received information into the
  18. * existing cache.
  19. *
  20. * @note This functionality is still considered experimental.
  21. *
  22. * Related sections in the development guide:
  23. * - @core_doc{_cache_manager,Cache Manager}
  24. *
  25. * @{
  26. *
  27. * Header
  28. * ------
  29. * ~~~~{.c}
  30. * #include <netlink/cache.h>
  31. * ~~~~
  32. */
  33. #include <netlink-private/netlink.h>
  34. #include <netlink/netlink.h>
  35. #include <netlink/cache.h>
  36. #include <netlink/utils.h>
  37. /** @cond SKIP */
  38. #define NASSOC_INIT 16
  39. #define NASSOC_EXPAND 8
  40. /** @endcond */
  41. static int include_cb(struct nl_object *obj, struct nl_parser_param *p)
  42. {
  43. struct nl_cache_assoc *ca = p->pp_arg;
  44. struct nl_cache_ops *ops = ca->ca_cache->c_ops;
  45. NL_DBG(2, "Including object %p into cache %p\n", obj, ca->ca_cache);
  46. #ifdef NL_DEBUG
  47. if (nl_debug >= 4)
  48. nl_object_dump(obj, &nl_debug_dp);
  49. #endif
  50. if (ops->co_event_filter)
  51. if (ops->co_event_filter(ca->ca_cache, obj) != NL_OK)
  52. return 0;
  53. if (ops->co_include_event)
  54. return ops->co_include_event(ca->ca_cache, obj, ca->ca_change,
  55. ca->ca_change_data);
  56. else
  57. return nl_cache_include(ca->ca_cache, obj, ca->ca_change, ca->ca_change_data);
  58. }
  59. static int event_input(struct nl_msg *msg, void *arg)
  60. {
  61. struct nl_cache_mngr *mngr = arg;
  62. int protocol = nlmsg_get_proto(msg);
  63. int type = nlmsg_hdr(msg)->nlmsg_type;
  64. struct nl_cache_ops *ops;
  65. int i, n;
  66. struct nl_parser_param p = {
  67. .pp_cb = include_cb,
  68. };
  69. NL_DBG(2, "Cache manager %p, handling new message %p as event\n",
  70. mngr, msg);
  71. #ifdef NL_DEBUG
  72. if (nl_debug >= 4)
  73. nl_msg_dump(msg, stderr);
  74. #endif
  75. if (mngr->cm_protocol != protocol)
  76. BUG();
  77. for (i = 0; i < mngr->cm_nassocs; i++) {
  78. if (mngr->cm_assocs[i].ca_cache) {
  79. ops = mngr->cm_assocs[i].ca_cache->c_ops;
  80. for (n = 0; ops->co_msgtypes[n].mt_id >= 0; n++)
  81. if (ops->co_msgtypes[n].mt_id == type)
  82. goto found;
  83. }
  84. }
  85. return NL_SKIP;
  86. found:
  87. NL_DBG(2, "Associated message %p to cache %p\n",
  88. msg, mngr->cm_assocs[i].ca_cache);
  89. p.pp_arg = &mngr->cm_assocs[i];
  90. return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
  91. }
  92. /**
  93. * Allocate new cache manager
  94. * @arg sk Netlink socket or NULL to auto allocate
  95. * @arg protocol Netlink protocol this manager is used for
  96. * @arg flags Flags (\c NL_AUTO_PROVIDE)
  97. * @arg result Result pointer
  98. *
  99. * Allocates a new cache manager for the specified netlink protocol.
  100. *
  101. * 1. If sk is not specified (\c NULL) a netlink socket matching the
  102. * specified protocol will be automatically allocated.
  103. *
  104. * 2. The socket will be put in non-blocking mode and sequence checking
  105. * will be disabled regardless of whether the socket was provided by
  106. * the caller or automatically allocated.
  107. *
  108. * 3. The socket will be connected.
  109. *
  110. * If the flag \c NL_AUTO_PROVIDE is specified, any cache added to the
  111. * manager will automatically be made available to other users using
  112. * nl_cache_mngt_provide().
  113. *
  114. * @note If the socket is provided by the caller, it is NOT recommended
  115. * to use the socket for anything else besides receiving netlink
  116. * notifications.
  117. *
  118. * @return 0 on success or a negative error code.
  119. */
  120. int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags,
  121. struct nl_cache_mngr **result)
  122. {
  123. struct nl_cache_mngr *mngr;
  124. int err = -NLE_NOMEM;
  125. /* Catch abuse of flags */
  126. if (flags & NL_ALLOCATED_SOCK)
  127. BUG();
  128. mngr = calloc(1, sizeof(*mngr));
  129. if (!mngr)
  130. return -NLE_NOMEM;
  131. if (!sk) {
  132. if (!(sk = nl_socket_alloc()))
  133. goto errout;
  134. flags |= NL_ALLOCATED_SOCK;
  135. }
  136. mngr->cm_sock = sk;
  137. mngr->cm_nassocs = NASSOC_INIT;
  138. mngr->cm_protocol = protocol;
  139. mngr->cm_flags = flags;
  140. mngr->cm_assocs = calloc(mngr->cm_nassocs,
  141. sizeof(struct nl_cache_assoc));
  142. if (!mngr->cm_assocs)
  143. goto errout;
  144. /* Required to receive async event notifications */
  145. nl_socket_disable_seq_check(mngr->cm_sock);
  146. if ((err = nl_connect(mngr->cm_sock, protocol)) < 0)
  147. goto errout;
  148. if ((err = nl_socket_set_nonblocking(mngr->cm_sock)) < 0)
  149. goto errout;
  150. /* Create and allocate socket for sync cache fills */
  151. mngr->cm_sync_sock = nl_socket_alloc();
  152. if (!mngr->cm_sync_sock) {
  153. err = -NLE_NOMEM;
  154. goto errout;
  155. }
  156. if ((err = nl_connect(mngr->cm_sync_sock, protocol)) < 0)
  157. goto errout_free_sync_sock;
  158. NL_DBG(1, "Allocated cache manager %p, protocol %d, %d caches\n",
  159. mngr, protocol, mngr->cm_nassocs);
  160. *result = mngr;
  161. return 0;
  162. errout_free_sync_sock:
  163. nl_socket_free(mngr->cm_sync_sock);
  164. errout:
  165. nl_cache_mngr_free(mngr);
  166. return err;
  167. }
  168. /**
  169. * Add cache to cache manager
  170. * @arg mngr Cache manager.
  171. * @arg cache Cache to be added to cache manager
  172. * @arg cb Function to be called upon changes.
  173. * @arg data Argument passed on to change callback
  174. *
  175. * Adds cache to the manager. The operation will trigger a full
  176. * dump request from the kernel to initially fill the contents
  177. * of the cache. The manager will subscribe to the notification group
  178. * of the cache and keep track of any further changes.
  179. *
  180. * The user is responsible for calling nl_cache_mngr_poll() or monitor
  181. * the socket and call nl_cache_mngr_data_ready() to allow the library
  182. * to process netlink notification events.
  183. *
  184. * @see nl_cache_mngr_poll()
  185. * @see nl_cache_mngr_data_ready()
  186. *
  187. * @return 0 on success or a negative error code.
  188. * @return -NLE_PROTO_MISMATCH Protocol mismatch between cache manager and
  189. * cache type
  190. * @return -NLE_OPNOTSUPP Cache type does not support updates
  191. * @return -NLE_EXIST Cache of this type already being managed
  192. */
  193. int nl_cache_mngr_add_cache(struct nl_cache_mngr *mngr, struct nl_cache *cache,
  194. change_func_t cb, void *data)
  195. {
  196. struct nl_cache_ops *ops;
  197. struct nl_af_group *grp;
  198. int err, i;
  199. ops = cache->c_ops;
  200. if (!ops)
  201. return -NLE_INVAL;
  202. if (ops->co_protocol != mngr->cm_protocol)
  203. return -NLE_PROTO_MISMATCH;
  204. if (ops->co_groups == NULL)
  205. return -NLE_OPNOTSUPP;
  206. for (i = 0; i < mngr->cm_nassocs; i++)
  207. if (mngr->cm_assocs[i].ca_cache &&
  208. mngr->cm_assocs[i].ca_cache->c_ops == ops)
  209. return -NLE_EXIST;
  210. retry:
  211. for (i = 0; i < mngr->cm_nassocs; i++)
  212. if (!mngr->cm_assocs[i].ca_cache)
  213. break;
  214. if (i >= mngr->cm_nassocs) {
  215. mngr->cm_nassocs += NASSOC_EXPAND;
  216. mngr->cm_assocs = realloc(mngr->cm_assocs,
  217. mngr->cm_nassocs *
  218. sizeof(struct nl_cache_assoc));
  219. if (mngr->cm_assocs == NULL)
  220. return -NLE_NOMEM;
  221. memset(mngr->cm_assocs + (mngr->cm_nassocs - NASSOC_EXPAND), 0,
  222. NASSOC_EXPAND * sizeof(struct nl_cache_assoc));
  223. NL_DBG(1, "Increased capacity of cache manager %p " \
  224. "to %d\n", mngr, mngr->cm_nassocs);
  225. goto retry;
  226. }
  227. for (grp = ops->co_groups; grp->ag_group; grp++) {
  228. err = nl_socket_add_membership(mngr->cm_sock, grp->ag_group);
  229. if (err < 0)
  230. return err;
  231. }
  232. err = nl_cache_refill(mngr->cm_sync_sock, cache);
  233. if (err < 0)
  234. goto errout_drop_membership;
  235. mngr->cm_assocs[i].ca_cache = cache;
  236. mngr->cm_assocs[i].ca_change = cb;
  237. mngr->cm_assocs[i].ca_change_data = data;
  238. if (mngr->cm_flags & NL_AUTO_PROVIDE)
  239. nl_cache_mngt_provide(cache);
  240. NL_DBG(1, "Added cache %p <%s> to cache manager %p\n",
  241. cache, nl_cache_name(cache), mngr);
  242. return 0;
  243. errout_drop_membership:
  244. for (grp = ops->co_groups; grp->ag_group; grp++)
  245. nl_socket_drop_membership(mngr->cm_sock, grp->ag_group);
  246. return err;
  247. }
  248. /**
  249. * Add cache to cache manager
  250. * @arg mngr Cache manager.
  251. * @arg name Name of cache to keep track of
  252. * @arg cb Function to be called upon changes.
  253. * @arg data Argument passed on to change callback
  254. * @arg result Pointer to store added cache (optional)
  255. *
  256. * Allocates a new cache of the specified type and adds it to the manager.
  257. * The operation will trigger a full dump request from the kernel to
  258. * initially fill the contents of the cache. The manager will subscribe
  259. * to the notification group of the cache and keep track of any further
  260. * changes.
  261. *
  262. * The user is responsible for calling nl_cache_mngr_poll() or monitor
  263. * the socket and call nl_cache_mngr_data_ready() to allow the library
  264. * to process netlink notification events.
  265. *
  266. * @see nl_cache_mngr_poll()
  267. * @see nl_cache_mngr_data_ready()
  268. *
  269. * @return 0 on success or a negative error code.
  270. * @return -NLE_NOCACHE Unknown cache type
  271. * @return -NLE_PROTO_MISMATCH Protocol mismatch between cache manager and
  272. * cache type
  273. * @return -NLE_OPNOTSUPP Cache type does not support updates
  274. * @return -NLE_EXIST Cache of this type already being managed
  275. */
  276. int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name,
  277. change_func_t cb, void *data, struct nl_cache **result)
  278. {
  279. struct nl_cache_ops *ops;
  280. struct nl_cache *cache;
  281. int err;
  282. ops = nl_cache_ops_lookup_safe(name);
  283. if (!ops)
  284. return -NLE_NOCACHE;
  285. cache = nl_cache_alloc(ops);
  286. nl_cache_ops_put(ops);
  287. if (!cache)
  288. return -NLE_NOMEM;
  289. err = nl_cache_mngr_add_cache(mngr, cache, cb, data);
  290. if (err < 0)
  291. goto errout_free_cache;
  292. *result = cache;
  293. return 0;
  294. errout_free_cache:
  295. nl_cache_free(cache);
  296. return err;
  297. }
  298. /**
  299. * Get socket file descriptor
  300. * @arg mngr Cache Manager
  301. *
  302. * Get the file descriptor of the socket associated with the manager.
  303. *
  304. * @note Do not use the socket for anything besides receiving
  305. * notifications.
  306. */
  307. int nl_cache_mngr_get_fd(struct nl_cache_mngr *mngr)
  308. {
  309. return nl_socket_get_fd(mngr->cm_sock);
  310. }
  311. /**
  312. * Check for event notifications
  313. * @arg mngr Cache Manager
  314. * @arg timeout Upper limit poll() will block, in milliseconds.
  315. *
  316. * Causes poll() to be called to check for new event notifications
  317. * being available. Calls nl_cache_mngr_data_ready() to process
  318. * available data.
  319. *
  320. * This functionally is ideally called regularly during an idle
  321. * period.
  322. *
  323. * A timeout can be specified in milliseconds to limit the time the
  324. * function will wait for updates.
  325. *
  326. * @see nl_cache_mngr_data_ready()
  327. *
  328. * @return The number of messages processed or a negative error code.
  329. */
  330. int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
  331. {
  332. int ret;
  333. struct pollfd fds = {
  334. .fd = nl_socket_get_fd(mngr->cm_sock),
  335. .events = POLLIN,
  336. };
  337. NL_DBG(3, "Cache manager %p, poll() fd %d\n", mngr, fds.fd);
  338. ret = poll(&fds, 1, timeout);
  339. NL_DBG(3, "Cache manager %p, poll() returned %d\n", mngr, ret);
  340. if (ret < 0)
  341. return -nl_syserr2nlerr(errno);
  342. /* No events, return */
  343. if (ret == 0)
  344. return 0;
  345. return nl_cache_mngr_data_ready(mngr);
  346. }
  347. /**
  348. * Receive available event notifications
  349. * @arg mngr Cache manager
  350. *
  351. * This function can be called if the socket associated to the manager
  352. * contains updates to be received. This function should only be used
  353. * if nl_cache_mngr_poll() is not used.
  354. *
  355. * The function will process messages until there is no more data to
  356. * be read from the socket.
  357. *
  358. * @see nl_cache_mngr_poll()
  359. *
  360. * @return The number of messages processed or a negative error code.
  361. */
  362. int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
  363. {
  364. int err, nread = 0;
  365. struct nl_cb *cb;
  366. NL_DBG(2, "Cache manager %p, reading new data from fd %d\n",
  367. mngr, nl_socket_get_fd(mngr->cm_sock));
  368. cb = nl_cb_clone(mngr->cm_sock->s_cb);
  369. if (cb == NULL)
  370. return -NLE_NOMEM;
  371. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, event_input, mngr);
  372. while ((err = nl_recvmsgs_report(mngr->cm_sock, cb)) > 0) {
  373. NL_DBG(2, "Cache manager %p, recvmsgs read %d messages\n",
  374. mngr, err);
  375. nread += err;
  376. }
  377. nl_cb_put(cb);
  378. if (err < 0 && err != -NLE_AGAIN)
  379. return err;
  380. return nread;
  381. }
  382. /**
  383. * Print information about cache manager
  384. * @arg mngr Cache manager
  385. * @arg p Dumping parameters
  386. *
  387. * Prints information about the cache manager including all managed caches.
  388. *
  389. * @note This is a debugging function.
  390. */
  391. void nl_cache_mngr_info(struct nl_cache_mngr *mngr, struct nl_dump_params *p)
  392. {
  393. char buf[128];
  394. int i;
  395. nl_dump_line(p, "cache-manager <%p>\n", mngr);
  396. nl_dump_line(p, " .protocol = %s\n",
  397. nl_nlfamily2str(mngr->cm_protocol, buf, sizeof(buf)));
  398. nl_dump_line(p, " .flags = %#x\n", mngr->cm_flags);
  399. nl_dump_line(p, " .nassocs = %u\n", mngr->cm_nassocs);
  400. nl_dump_line(p, " .sock = <%p>\n", mngr->cm_sock);
  401. for (i = 0; i < mngr->cm_nassocs; i++) {
  402. struct nl_cache_assoc *assoc = &mngr->cm_assocs[i];
  403. if (assoc->ca_cache) {
  404. nl_dump_line(p, " .cache[%d] = <%p> {\n", i, assoc->ca_cache);
  405. nl_dump_line(p, " .name = %s\n", assoc->ca_cache->c_ops->co_name);
  406. nl_dump_line(p, " .change_func = <%p>\n", assoc->ca_change);
  407. nl_dump_line(p, " .change_data = <%p>\n", assoc->ca_change_data);
  408. nl_dump_line(p, " .nitems = %u\n", nl_cache_nitems(assoc->ca_cache));
  409. nl_dump_line(p, " .objects = {\n");
  410. p->dp_prefix += 6;
  411. nl_cache_dump(assoc->ca_cache, p);
  412. p->dp_prefix -= 6;
  413. nl_dump_line(p, " }\n");
  414. nl_dump_line(p, " }\n");
  415. }
  416. }
  417. }
  418. /**
  419. * Free cache manager and all caches.
  420. * @arg mngr Cache manager.
  421. *
  422. * Release all resources held by a cache manager.
  423. */
  424. void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
  425. {
  426. int i;
  427. if (!mngr)
  428. return;
  429. if (mngr->cm_sock)
  430. nl_close(mngr->cm_sock);
  431. if (mngr->cm_sync_sock) {
  432. nl_close(mngr->cm_sync_sock);
  433. nl_socket_free(mngr->cm_sync_sock);
  434. }
  435. if (mngr->cm_flags & NL_ALLOCATED_SOCK)
  436. nl_socket_free(mngr->cm_sock);
  437. for (i = 0; i < mngr->cm_nassocs; i++) {
  438. if (mngr->cm_assocs[i].ca_cache) {
  439. nl_cache_mngt_unprovide(mngr->cm_assocs[i].ca_cache);
  440. nl_cache_free(mngr->cm_assocs[i].ca_cache);
  441. }
  442. }
  443. free(mngr->cm_assocs);
  444. NL_DBG(1, "Cache manager %p freed\n", mngr);
  445. free(mngr);
  446. }
  447. /** @} */