2 * netlink-private/cache-api.h Caching API
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
9 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_CACHE_API_H_
13 #define NETLINK_CACHE_API_H_
15 #include <netlink/netlink.h>
16 #include <netlink/cache.h>
24 * @defgroup cache_api Cache Implementation
27 * @par 1) Cache Definition
29 * struct nl_cache_ops my_cache_ops = {
30 * .co_name = "route/link",
31 * .co_protocol = NETLINK_ROUTE,
32 * .co_hdrsize = sizeof(struct ifinfomsg),
33 * .co_obj_ops = &my_obj_ops,
39 * // The simplest way to fill a cache is by providing a request-update
40 * // function which must trigger a complete dump on the kernel-side of
41 * // whatever the cache covers.
42 * static int my_request_update(struct nl_cache *cache,
43 * struct nl_sock *socket)
45 * // In this example, we request a full dump of the interface table
46 * return nl_rtgen_request(socket, RTM_GETLINK, AF_UNSPEC, NLM_F_DUMP);
49 * // The resulting netlink messages sent back will be fed into a message
50 * // parser one at a time. The message parser has to extract all relevant
51 * // information from the message and create an object reflecting the
52 * // contents of the message and pass it on to the parser callback function
53 * // provide which will add the object to the cache.
54 * static int my_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
55 * struct nlmsghdr *nlh, struct nl_parser_param *pp)
59 * obj = my_obj_alloc();
60 * obj->ce_msgtype = nlh->nlmsg_type;
62 * // Parse the netlink message and continue creating the object.
64 * err = pp->pp_cb((struct nl_object *) obj, pp);
69 * struct nl_cache_ops my_cache_ops = {
71 * .co_request_update = my_request_update,
72 * .co_msg_parser = my_msg_parser,
76 * @par 3) Notification based Updates
78 * // Caches can be kept up-to-date based on notifications if the kernel
79 * // sends out notifications whenever an object is added/removed/changed.
81 * // It is trivial to support this, first a list of groups needs to be
82 * // defined which are required to join in order to receive all necessary
83 * // notifications. The groups are separated by address family to support
84 * // the common situation where a separate group is used for each address
85 * // family. If there is only one group, simply specify AF_UNSPEC.
86 * static struct nl_af_group addr_groups[] = {
87 * { AF_INET, RTNLGRP_IPV4_IFADDR },
88 * { AF_INET6, RTNLGRP_IPV6_IFADDR },
89 * { END_OF_GROUP_LIST },
92 * // In order for the caching system to know the meaning of each message
93 * // type it requires a table which maps each supported message type to
94 * // a cache action, e.g. RTM_NEWADDR means address has been added or
95 * // updated, RTM_DELADDR means address has been removed.
96 * static struct nl_cache_ops rtnl_addr_ops = {
99 * { RTM_NEWADDR, NL_ACT_NEW, "new" },
100 * { RTM_DELADDR, NL_ACT_DEL, "del" },
101 * { RTM_GETADDR, NL_ACT_GET, "get" },
102 * END_OF_MSGTYPES_LIST,
104 * .co_groups = addr_groups,
107 * // It is now possible to keep the cache up-to-date using the cache manager.
112 #define END_OF_MSGTYPES_LIST { -1, -1, NULL }
115 * Message type to cache action association
119 /** Netlink message type */
122 /** Cache action to take */
125 /** Name of operation for human-readable printing */
130 * Address family to netlink group association
134 /** Address family */
137 /** Netlink group identifier */
141 #define END_OF_GROUP_LIST AF_UNSPEC, 0
146 * This structure is used to configure what kind of parser to use
147 * when parsing netlink messages to create objects.
149 struct nl_parser_param
151 /** Function to parse netlink messages into objects */
152 int (*pp_cb)(struct nl_object *, struct nl_parser_param *);
154 /** Arbitary argument to be passed to the parser */
161 * This structure defines the characterstics of a cache type. It contains
162 * pointers to functions which implement the specifics of the object type
163 * the cache can hold.
167 /** Name of cache type (must be unique) */
170 /** Size of family specific netlink header */
173 /** Netlink protocol */
176 /** cache object hash size **/
180 unsigned int co_flags;
182 /** Reference counter */
183 unsigned int co_refcnt;
185 /** Group definition */
186 struct nl_af_group * co_groups;
189 * Called whenever an update of the cache is required. Must send
190 * a request message to the kernel requesting a complete dump.
192 int (*co_request_update)(struct nl_cache *, struct nl_sock *);
195 * Called whenever a message was received that needs to be parsed.
196 * Must parse the message and call the paser callback function
197 * (nl_parser_param) provided via the argument.
199 int (*co_msg_parser)(struct nl_cache_ops *, struct sockaddr_nl *,
200 struct nlmsghdr *, struct nl_parser_param *);
203 * The function registered under this callback is called after a
204 * netlink notification associated with this cache type has been
205 * parsed into an object and is being considered for inclusio into
206 * the specified cache.
208 * The purpose of this function is to filter out notifications
209 * which should be ignored when updating caches.
211 * The function must return NL_SKIP to prevent the object from
212 * being included, or NL_OK to include it.
215 * int my_filter(struct nl_cache *cache, struct nl_object *obj)
217 * if (reason_to_not_include_obj(obj))
224 int (*co_event_filter)(struct nl_cache *, struct nl_object *obj);
227 * The function registered under this callback is called when an
228 * object formed from a notification event needs to be included in
231 * For each modified object, the change callback \c change_cb must
232 * be called with the \c data argument provided.
234 * If no function is registered, the function nl_cache_include()
235 * will be used for this purpose.
237 * @see nl_cache_include()
239 int (*co_include_event)(struct nl_cache *cache, struct nl_object *obj,
240 change_func_t change_cb, void *data);
242 void (*reserved_1)(void);
243 void (*reserved_2)(void);
244 void (*reserved_3)(void);
245 void (*reserved_4)(void);
246 void (*reserved_5)(void);
247 void (*reserved_6)(void);
248 void (*reserved_7)(void);
249 void (*reserved_8)(void);
251 /** Object operations */
252 struct nl_object_ops * co_obj_ops;
254 /** Internal, do not touch! */
255 struct nl_cache_ops *co_next;
257 struct nl_cache *co_major_cache;
258 struct genl_ops * co_genl;
260 /* Message type definition */
261 struct nl_msgtype co_msgtypes[];