cache-api.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * netlink/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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_CACHE_API_H_
  12. #define NETLINK_CACHE_API_H_
  13. #include <netlink/netlink.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * @ingroup cache
  19. * @defgroup cache_api Cache Implementation
  20. * @brief
  21. *
  22. * @par 1) Cache Definition
  23. * @code
  24. * struct nl_cache_ops my_cache_ops = {
  25. * .co_name = "route/link",
  26. * .co_protocol = NETLINK_ROUTE,
  27. * .co_hdrsize = sizeof(struct ifinfomsg),
  28. * .co_obj_ops = &my_obj_ops,
  29. * };
  30. * @endcode
  31. *
  32. * @par 2)
  33. * @code
  34. * // The simplest way to fill a cache is by providing a request-update
  35. * // function which must trigger a complete dump on the kernel-side of
  36. * // whatever the cache covers.
  37. * static int my_request_update(struct nl_cache *cache,
  38. * struct nl_sock *socket)
  39. * {
  40. * // In this example, we request a full dump of the interface table
  41. * return nl_rtgen_request(socket, RTM_GETLINK, AF_UNSPEC, NLM_F_DUMP);
  42. * }
  43. *
  44. * // The resulting netlink messages sent back will be fed into a message
  45. * // parser one at a time. The message parser has to extract all relevant
  46. * // information from the message and create an object reflecting the
  47. * // contents of the message and pass it on to the parser callback function
  48. * // provide which will add the object to the cache.
  49. * static int my_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  50. * struct nlmsghdr *nlh, struct nl_parser_param *pp)
  51. * {
  52. * struct my_obj *obj;
  53. *
  54. * obj = my_obj_alloc();
  55. * obj->ce_msgtype = nlh->nlmsg_type;
  56. *
  57. * // Parse the netlink message and continue creating the object.
  58. *
  59. * err = pp->pp_cb((struct nl_object *) obj, pp);
  60. * if (err < 0)
  61. * goto errout;
  62. * }
  63. *
  64. * struct nl_cache_ops my_cache_ops = {
  65. * ...
  66. * .co_request_update = my_request_update,
  67. * .co_msg_parser = my_msg_parser,
  68. * };
  69. * @endcode
  70. *
  71. * @par 3) Notification based Updates
  72. * @code
  73. * // Caches can be kept up-to-date based on notifications if the kernel
  74. * // sends out notifications whenever an object is added/removed/changed.
  75. * //
  76. * // It is trivial to support this, first a list of groups needs to be
  77. * // defined which are required to join in order to receive all necessary
  78. * // notifications. The groups are separated by address family to support
  79. * // the common situation where a separate group is used for each address
  80. * // family. If there is only one group, simply specify AF_UNSPEC.
  81. * static struct nl_af_group addr_groups[] = {
  82. * { AF_INET, RTNLGRP_IPV4_IFADDR },
  83. * { AF_INET6, RTNLGRP_IPV6_IFADDR },
  84. * { END_OF_GROUP_LIST },
  85. * };
  86. *
  87. * // In order for the caching system to know the meaning of each message
  88. * // type it requires a table which maps each supported message type to
  89. * // a cache action, e.g. RTM_NEWADDR means address has been added or
  90. * // updated, RTM_DELADDR means address has been removed.
  91. * static struct nl_cache_ops rtnl_addr_ops = {
  92. * ...
  93. * .co_msgtypes = {
  94. * { RTM_NEWADDR, NL_ACT_NEW, "new" },
  95. * { RTM_DELADDR, NL_ACT_DEL, "del" },
  96. * { RTM_GETADDR, NL_ACT_GET, "get" },
  97. * END_OF_MSGTYPES_LIST,
  98. * },
  99. * .co_groups = addr_groups,
  100. * };
  101. *
  102. * // It is now possible to keep the cache up-to-date using the cache manager.
  103. * @endcode
  104. * @{
  105. */
  106. enum {
  107. NL_ACT_UNSPEC,
  108. NL_ACT_NEW,
  109. NL_ACT_DEL,
  110. NL_ACT_GET,
  111. NL_ACT_SET,
  112. NL_ACT_CHANGE,
  113. __NL_ACT_MAX,
  114. };
  115. #define NL_ACT_MAX (__NL_ACT_MAX - 1)
  116. #define END_OF_MSGTYPES_LIST { -1, -1, NULL }
  117. /**
  118. * Message type to cache action association
  119. */
  120. struct nl_msgtype
  121. {
  122. /** Netlink message type */
  123. int mt_id;
  124. /** Cache action to take */
  125. int mt_act;
  126. /** Name of operation for human-readable printing */
  127. char * mt_name;
  128. };
  129. /**
  130. * Address family to netlink group association
  131. */
  132. struct nl_af_group
  133. {
  134. /** Address family */
  135. int ag_family;
  136. /** Netlink group identifier */
  137. int ag_group;
  138. };
  139. #define END_OF_GROUP_LIST AF_UNSPEC, 0
  140. struct nl_parser_param
  141. {
  142. int (*pp_cb)(struct nl_object *, struct nl_parser_param *);
  143. void * pp_arg;
  144. };
  145. /**
  146. * Cache Operations
  147. */
  148. struct nl_cache_ops
  149. {
  150. char * co_name;
  151. int co_hdrsize;
  152. int co_protocol;
  153. struct nl_af_group * co_groups;
  154. /**
  155. * Called whenever an update of the cache is required. Must send
  156. * a request message to the kernel requesting a complete dump.
  157. */
  158. int (*co_request_update)(struct nl_cache *, struct nl_sock *);
  159. /**
  160. * Called whenever a message was received that needs to be parsed.
  161. * Must parse the message and call the paser callback function
  162. * (nl_parser_param) provided via the argument.
  163. */
  164. int (*co_msg_parser)(struct nl_cache_ops *, struct sockaddr_nl *,
  165. struct nlmsghdr *, struct nl_parser_param *);
  166. struct nl_object_ops * co_obj_ops;
  167. struct nl_cache_ops *co_next;
  168. struct nl_cache *co_major_cache;
  169. struct genl_ops * co_genl;
  170. struct nl_msgtype co_msgtypes[];
  171. };
  172. /** @} */
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif