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