libnl  3.2.14
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-local.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  NL_DBG(1, "Allocated cache manager %p, protocol %d, %d caches\n",
175  mngr, protocol, mngr->cm_nassocs);
176 
177  *result = mngr;
178  return 0;
179 
180 errout:
181  nl_cache_mngr_free(mngr);
182  return err;
183 }
184 
185 /**
186  * Add cache responsibility to cache manager
187  * @arg mngr Cache manager.
188  * @arg name Name of cache to keep track of
189  * @arg cb Function to be called upon changes.
190  * @arg data Argument passed on to change callback
191  * @arg result Pointer to store added cache (optional)
192  *
193  * Allocates a new cache of the specified type and adds it to the manager.
194  * The operation will trigger a full dump request from the kernel to
195  * initially fill the contents of the cache. The manager will subscribe
196  * to the notification group of the cache and keep track of any further
197  * changes.
198  *
199  * The user is responsible for calling nl_cache_mngr_poll() or monitor
200  * the socket and call nl_cache_mngr_data_ready() to allow the library
201  * to process netlink notification events.
202  *
203  * @see nl_cache_mngr_poll()
204  * @see nl_cache_mngr_data_ready()
205  *
206  * @return 0 on success or a negative error code.
207  * @return -NLE_NOCACHE Unknown cache type
208  * @return -NLE_PROTO_MISMATCH Protocol mismatch between cache manager and
209  * cache type
210  * @return -NLE_OPNOTSUPP Cache type does not support updates
211  * @return -NLE_EXIST Cache of this type already being managed
212  */
213 int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name,
214  change_func_t cb, void *data, struct nl_cache **result)
215 {
216  struct nl_cache_ops *ops;
217  struct nl_cache *cache;
218  struct nl_af_group *grp;
219  int err, i;
220 
221  ops = nl_cache_ops_lookup(name);
222  if (!ops)
223  return -NLE_NOCACHE;
224 
225  if (ops->co_protocol != mngr->cm_protocol)
226  return -NLE_PROTO_MISMATCH;
227 
228  if (ops->co_groups == NULL)
229  return -NLE_OPNOTSUPP;
230 
231  for (i = 0; i < mngr->cm_nassocs; i++)
232  if (mngr->cm_assocs[i].ca_cache &&
233  mngr->cm_assocs[i].ca_cache->c_ops == ops)
234  return -NLE_EXIST;
235 
236 retry:
237  for (i = 0; i < mngr->cm_nassocs; i++)
238  if (!mngr->cm_assocs[i].ca_cache)
239  break;
240 
241  if (i >= mngr->cm_nassocs) {
242  mngr->cm_nassocs += NASSOC_EXPAND;
243  mngr->cm_assocs = realloc(mngr->cm_assocs,
244  mngr->cm_nassocs *
245  sizeof(struct nl_cache_assoc));
246  if (mngr->cm_assocs == NULL)
247  return -NLE_NOMEM;
248 
249  memset(mngr->cm_assocs + (mngr->cm_nassocs - NASSOC_EXPAND), 0,
250  NASSOC_EXPAND * sizeof(struct nl_cache_assoc));
251 
252  NL_DBG(1, "Increased capacity of cache manager %p " \
253  "to %d\n", mngr, mngr->cm_nassocs);
254  goto retry;
255  }
256 
257  cache = nl_cache_alloc(ops);
258  if (!cache)
259  return -NLE_NOMEM;
260 
261  for (grp = ops->co_groups; grp->ag_group; grp++) {
262  err = nl_socket_add_membership(mngr->cm_sock, grp->ag_group);
263  if (err < 0)
264  goto errout_free_cache;
265  }
266 
267  err = nl_cache_refill(mngr->cm_sock, cache);
268  if (err < 0)
269  goto errout_drop_membership;
270 
271  mngr->cm_assocs[i].ca_cache = cache;
272  mngr->cm_assocs[i].ca_change = cb;
273  mngr->cm_assocs[i].ca_change_data = data;
274 
275  if (mngr->cm_flags & NL_AUTO_PROVIDE)
276  nl_cache_mngt_provide(cache);
277 
278  NL_DBG(1, "Added cache %p <%s> to cache manager %p\n",
279  cache, nl_cache_name(cache), mngr);
280 
281  if (result)
282  *result = cache;
283  return 0;
284 
285 errout_drop_membership:
286  for (grp = ops->co_groups; grp->ag_group; grp++)
287  nl_socket_drop_membership(mngr->cm_sock, grp->ag_group);
288 errout_free_cache:
289  nl_cache_free(cache);
290 
291  return err;
292 }
293 
294 /**
295  * Get socket file descriptor
296  * @arg mngr Cache Manager
297  *
298  * Get the file descriptor of the socket associated with the manager.
299  *
300  * @note Do not use the socket for anything besides receiving
301  * notifications.
302  */
303 int nl_cache_mngr_get_fd(struct nl_cache_mngr *mngr)
304 {
305  return nl_socket_get_fd(mngr->cm_sock);
306 }
307 
308 /**
309  * Check for event notifications
310  * @arg mngr Cache Manager
311  * @arg timeout Upper limit poll() will block, in milliseconds.
312  *
313  * Causes poll() to be called to check for new event notifications
314  * being available. Calls nl_cache_mngr_data_ready() to process
315  * available data.
316  *
317  * This functionally is ideally called regularly during an idle
318  * period.
319  *
320  * A timeout can be specified in milliseconds to limit the time the
321  * function will wait for updates.
322  *
323  * @see nl_cache_mngr_data_ready()
324  *
325  * @return The number of messages processed or a negative error code.
326  */
327 int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
328 {
329  int ret;
330  struct pollfd fds = {
331  .fd = nl_socket_get_fd(mngr->cm_sock),
332  .events = POLLIN,
333  };
334 
335  NL_DBG(3, "Cache manager %p, poll() fd %d\n", mngr, fds.fd);
336  ret = poll(&fds, 1, timeout);
337  NL_DBG(3, "Cache manager %p, poll() returned %d\n", mngr, ret);
338  if (ret < 0)
339  return -nl_syserr2nlerr(errno);
340 
341  /* No events, return */
342  if (ret == 0)
343  return 0;
344 
345  return nl_cache_mngr_data_ready(mngr);
346 }
347 
348 /**
349  * Receive available event notifications
350  * @arg mngr Cache manager
351  *
352  * This function can be called if the socket associated to the manager
353  * contains updates to be received. This function should only be used
354  * if nl_cache_mngr_poll() is not used.
355  *
356  * The function will process messages until there is no more data to
357  * be read from the socket.
358  *
359  * @see nl_cache_mngr_poll()
360  *
361  * @return The number of messages processed or a negative error code.
362  */
363 int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
364 {
365  int err, nread = 0;
366  struct nl_cb *cb;
367 
368  NL_DBG(2, "Cache manager %p, reading new data from fd %d\n",
369  mngr, nl_socket_get_fd(mngr->cm_sock));
370 
371  cb = nl_cb_clone(mngr->cm_sock->s_cb);
372  if (cb == NULL)
373  return -NLE_NOMEM;
374 
375  nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, event_input, mngr);
376 
377  while ((err = nl_recvmsgs_report(mngr->cm_sock, cb)) > 0) {
378  NL_DBG(2, "Cache manager %p, recvmsgs read %d messages\n",
379  mngr, err);
380  nread += err;
381  }
382 
383  nl_cb_put(cb);
384  if (err < 0)
385  return err;
386 
387  return nread;
388 }
389 
390 /**
391  * Print information about cache manager
392  * @arg mngr Cache manager
393  * @arg p Dumping parameters
394  *
395  * Prints information about the cache manager including all managed caches.
396  *
397  * @note This is a debugging function.
398  */
399 void nl_cache_mngr_info(struct nl_cache_mngr *mngr, struct nl_dump_params *p)
400 {
401  char buf[128];
402  int i;
403 
404  nl_dump_line(p, "cache-manager <%p>\n", mngr);
405  nl_dump_line(p, " .protocol = %s\n",
406  nl_nlfamily2str(mngr->cm_protocol, buf, sizeof(buf)));
407  nl_dump_line(p, " .flags = %#x\n", mngr->cm_flags);
408  nl_dump_line(p, " .nassocs = %u\n", mngr->cm_nassocs);
409  nl_dump_line(p, " .sock = <%p>\n", mngr->cm_sock);
410 
411  for (i = 0; i < mngr->cm_nassocs; i++) {
412  struct nl_cache_assoc *assoc = &mngr->cm_assocs[i];
413 
414  if (assoc->ca_cache) {
415  nl_dump_line(p, " .cache[%d] = <%p> {\n", i, assoc->ca_cache);
416  nl_dump_line(p, " .name = %s\n", assoc->ca_cache->c_ops->co_name);
417  nl_dump_line(p, " .change_func = <%p>\n", assoc->ca_change);
418  nl_dump_line(p, " .change_data = <%p>\n", assoc->ca_change_data);
419  nl_dump_line(p, " .nitems = %u\n", nl_cache_nitems(assoc->ca_cache));
420  nl_dump_line(p, " .objects = {\n");
421 
422  p->dp_prefix += 6;
423  nl_cache_dump(assoc->ca_cache, p);
424  p->dp_prefix -= 6;
425 
426  nl_dump_line(p, " }\n");
427  nl_dump_line(p, " }\n");
428  }
429  }
430 }
431 
432 /**
433  * Free cache manager and all caches.
434  * @arg mngr Cache manager.
435  *
436  * Release all resources held by a cache manager.
437  */
438 void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
439 {
440  int i;
441 
442  if (!mngr)
443  return;
444 
445  if (mngr->cm_sock)
446  nl_close(mngr->cm_sock);
447 
448  if (mngr->cm_flags & NL_ALLOCATED_SOCK)
449  nl_socket_free(mngr->cm_sock);
450 
451  for (i = 0; i < mngr->cm_nassocs; i++) {
452  if (mngr->cm_assocs[i].ca_cache) {
453  nl_cache_mngt_unprovide(mngr->cm_assocs[i].ca_cache);
454  nl_cache_free(mngr->cm_assocs[i].ca_cache);
455  }
456  }
457 
458  free(mngr->cm_assocs);
459  free(mngr);
460 
461  NL_DBG(1, "Cache manager %p freed\n", mngr);
462 }
463 
464 /** @} */