cache-api.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * netlink-private/cache-api.h Caching API
  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-2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_CACHE_API_H_
  12. #define NETLINK_CACHE_API_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/cache.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * @ingroup cache
  20. * @defgroup cache_api Cache Implementation
  21. * @brief
  22. *
  23. * @par 1) Cache Definition
  24. * @code
  25. * struct nl_cache_ops my_cache_ops = {
  26. * .co_name = "route/link",
  27. * .co_protocol = NETLINK_ROUTE,
  28. * .co_hdrsize = sizeof(struct ifinfomsg),
  29. * .co_obj_ops = &my_obj_ops,
  30. * };
  31. * @endcode
  32. *
  33. * @par 2)
  34. * @code
  35. * // The simplest way to fill a cache is by providing a request-update
  36. * // function which must trigger a complete dump on the kernel-side of
  37. * // whatever the cache covers.
  38. * static int my_request_update(struct nl_cache *cache,
  39. * struct nl_sock *socket)
  40. * {
  41. * // In this example, we request a full dump of the interface table
  42. * return nl_rtgen_request(socket, RTM_GETLINK, AF_UNSPEC, NLM_F_DUMP);
  43. * }
  44. *
  45. * // The resulting netlink messages sent back will be fed into a message
  46. * // parser one at a time. The message parser has to extract all relevant
  47. * // information from the message and create an object reflecting the
  48. * // contents of the message and pass it on to the parser callback function
  49. * // provide which will add the object to the cache.
  50. * static int my_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  51. * struct nlmsghdr *nlh, struct nl_parser_param *pp)
  52. * {
  53. * struct my_obj *obj;
  54. *
  55. * obj = my_obj_alloc();
  56. * obj->ce_msgtype = nlh->nlmsg_type;
  57. *
  58. * // Parse the netlink message and continue creating the object.
  59. *
  60. * err = pp->pp_cb((struct nl_object *) obj, pp);
  61. * if (err < 0)
  62. * goto errout;
  63. * }
  64. *
  65. * struct nl_cache_ops my_cache_ops = {
  66. * ...
  67. * .co_request_update = my_request_update,
  68. * .co_msg_parser = my_msg_parser,
  69. * };
  70. * @endcode
  71. *
  72. * @par 3) Notification based Updates
  73. * @code
  74. * // Caches can be kept up-to-date based on notifications if the kernel
  75. * // sends out notifications whenever an object is added/removed/changed.
  76. * //
  77. * // It is trivial to support this, first a list of groups needs to be
  78. * // defined which are required to join in order to receive all necessary
  79. * // notifications. The groups are separated by address family to support
  80. * // the common situation where a separate group is used for each address
  81. * // family. If there is only one group, simply specify AF_UNSPEC.
  82. * static struct nl_af_group addr_groups[] = {
  83. * { AF_INET, RTNLGRP_IPV4_IFADDR },
  84. * { AF_INET6, RTNLGRP_IPV6_IFADDR },
  85. * { END_OF_GROUP_LIST },
  86. * };
  87. *
  88. * // In order for the caching system to know the meaning of each message
  89. * // type it requires a table which maps each supported message type to
  90. * // a cache action, e.g. RTM_NEWADDR means address has been added or
  91. * // updated, RTM_DELADDR means address has been removed.
  92. * static struct nl_cache_ops rtnl_addr_ops = {
  93. * ...
  94. * .co_msgtypes = {
  95. * { RTM_NEWADDR, NL_ACT_NEW, "new" },
  96. * { RTM_DELADDR, NL_ACT_DEL, "del" },
  97. * { RTM_GETADDR, NL_ACT_GET, "get" },
  98. * END_OF_MSGTYPES_LIST,
  99. * },
  100. * .co_groups = addr_groups,
  101. * };
  102. *
  103. * // It is now possible to keep the cache up-to-date using the cache manager.
  104. * @endcode
  105. * @{
  106. */
  107. #define END_OF_MSGTYPES_LIST { -1, -1, NULL }
  108. /**
  109. * Message type to cache action association
  110. */
  111. struct nl_msgtype
  112. {
  113. /** Netlink message type */
  114. int mt_id;
  115. /** Cache action to take */
  116. int mt_act;
  117. /** Name of operation for human-readable printing */
  118. char * mt_name;
  119. };
  120. /**
  121. * Address family to netlink group association
  122. */
  123. struct nl_af_group
  124. {
  125. /** Address family */
  126. int ag_family;
  127. /** Netlink group identifier */
  128. int ag_group;
  129. };
  130. #define END_OF_GROUP_LIST AF_UNSPEC, 0
  131. /**
  132. * Parser parameters
  133. *
  134. * This structure is used to configure what kind of parser to use
  135. * when parsing netlink messages to create objects.
  136. */
  137. struct nl_parser_param
  138. {
  139. /** Function to parse netlink messages into objects */
  140. int (*pp_cb)(struct nl_object *, struct nl_parser_param *);
  141. /** Arbitary argument to be passed to the parser */
  142. void * pp_arg;
  143. };
  144. /**
  145. * Cache Operations
  146. *
  147. * This structure defines the characterstics of a cache type. It contains
  148. * pointers to functions which implement the specifics of the object type
  149. * the cache can hold.
  150. */
  151. struct nl_cache_ops
  152. {
  153. /** Name of cache type (must be unique) */
  154. char * co_name;
  155. /** Size of family specific netlink header */
  156. int co_hdrsize;
  157. /** Netlink protocol */
  158. int co_protocol;
  159. /** cache object hash size **/
  160. int co_hash_size;
  161. /** cache flags */
  162. unsigned int co_flags;
  163. /** Reference counter */
  164. unsigned int co_refcnt;
  165. /** Group definition */
  166. struct nl_af_group * co_groups;
  167. /**
  168. * Called whenever an update of the cache is required. Must send
  169. * a request message to the kernel requesting a complete dump.
  170. */
  171. int (*co_request_update)(struct nl_cache *, struct nl_sock *);
  172. /**
  173. * Called whenever a message was received that needs to be parsed.
  174. * Must parse the message and call the paser callback function
  175. * (nl_parser_param) provided via the argument.
  176. */
  177. int (*co_msg_parser)(struct nl_cache_ops *, struct sockaddr_nl *,
  178. struct nlmsghdr *, struct nl_parser_param *);
  179. /**
  180. * The function registered under this callback is called after a
  181. * netlink notification associated with this cache type has been
  182. * parsed into an object and is being considered for inclusio into
  183. * the specified cache.
  184. *
  185. * The purpose of this function is to filter out notifications
  186. * which should be ignored when updating caches.
  187. *
  188. * The function must return NL_SKIP to prevent the object from
  189. * being included, or NL_OK to include it.
  190. *
  191. * @code
  192. * int my_filter(struct nl_cache *cache, struct nl_object *obj)
  193. * {
  194. * if (reason_to_not_include_obj(obj))
  195. * return NL_SKIP;
  196. *
  197. * return NL_OK;
  198. * }
  199. * @endcode
  200. */
  201. int (*co_event_filter)(struct nl_cache *, struct nl_object *obj);
  202. /**
  203. * The function registered under this callback is called when an
  204. * object formed from a notification event needs to be included in
  205. * a cache.
  206. *
  207. * For each modified object, the change callback \c change_cb must
  208. * be called with the \c data argument provided.
  209. *
  210. * If no function is registered, the function nl_cache_include()
  211. * will be used for this purpose.
  212. *
  213. * @see nl_cache_include()
  214. */
  215. int (*co_include_event)(struct nl_cache *cache, struct nl_object *obj,
  216. change_func_t change_cb, void *data);
  217. void (*reserved_1)(void);
  218. void (*reserved_2)(void);
  219. void (*reserved_3)(void);
  220. void (*reserved_4)(void);
  221. void (*reserved_5)(void);
  222. void (*reserved_6)(void);
  223. void (*reserved_7)(void);
  224. void (*reserved_8)(void);
  225. /** Object operations */
  226. struct nl_object_ops * co_obj_ops;
  227. /** Internal, do not touch! */
  228. struct nl_cache_ops *co_next;
  229. struct nl_cache *co_major_cache;
  230. struct genl_ops * co_genl;
  231. /* Message type definition */
  232. struct nl_msgtype co_msgtypes[];
  233. };
  234. /** @} */
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif