fpm.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Public definitions pertaining to the Forwarding Plane Manager component.
  3. *
  4. * Permission is granted to use, copy, modify and/or distribute this
  5. * software under either one of the licenses below.
  6. *
  7. * Note that if you use other files from the Quagga tree directly or
  8. * indirectly, then the licenses in those files still apply.
  9. *
  10. * Please retain both licenses below when modifying this code in the
  11. * Quagga tree.
  12. *
  13. * Copyright (C) 2012 by Open Source Routing.
  14. * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
  15. */
  16. /*
  17. * License Option 1: GPL
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the Free
  21. * Software Foundation; either version 2 of the License, or (at your option)
  22. * any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,but WITHOUT
  25. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  26. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  27. * more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32. */
  33. /*
  34. * License Option 2: ISC License
  35. *
  36. * Permission to use, copy, modify, and/or distribute this software
  37. * for any purpose with or without fee is hereby granted, provided
  38. * that the above copyright notice and this permission notice appear
  39. * in all copies.
  40. *
  41. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  42. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  43. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  44. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  45. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  46. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  47. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  48. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  49. */
  50. #ifndef _FPM_H
  51. #define _FPM_H
  52. /*
  53. * The Forwarding Plane Manager (FPM) is an optional component that
  54. * may be used in scenarios where the router has a forwarding path
  55. * that is distinct from the kernel, commonly a hardware-based fast
  56. * path. It is responsible for programming forwarding information
  57. * (such as routes and nexthops) in the fast path.
  58. *
  59. * In Quagga, the Routing Information Base is maintained in the
  60. * 'zebra' infrastructure daemon. Routing protocols communicate their
  61. * best routes to zebra, and zebra computes the best route across
  62. * protocols for each prefix. This latter information comprises the
  63. * bulk of the Forwarding Information Base.
  64. *
  65. * This header file defines a point-to-point interface using which
  66. * zebra can update the FPM about changes in routes. The communication
  67. * takes place over a stream socket. The FPM listens on a well-known
  68. * TCP port, and zebra initiates the connection.
  69. *
  70. * All messages sent over the connection start with a short FPM
  71. * header, fpm_msg_hdr_t. In the case of route add/delete messages,
  72. * the header is followed by a netlink message. Zebra should send a
  73. * complete copy of the forwarding table(s) to the FPM, including
  74. * routes that it may have picked up from the kernel.
  75. *
  76. * The FPM interface uses replace semantics. That is, if a 'route add'
  77. * message for a prefix is followed by another 'route add' message, the
  78. * information in the second message is complete by itself, and replaces
  79. * the information sent in the first message.
  80. *
  81. * If the connection to the FPM goes down for some reason, the client
  82. * (zebra) should send the FPM a complete copy of the forwarding
  83. * table(s) when it reconnects.
  84. */
  85. #define FPM_DEFAULT_PORT 2620
  86. /*
  87. * Largest message that can be sent to or received from the FPM.
  88. */
  89. #define FPM_MAX_MSG_LEN 4096
  90. /*
  91. * Header that precedes each fpm message to/from the FPM.
  92. */
  93. typedef struct fpm_msg_hdr_t_
  94. {
  95. /*
  96. * Protocol version.
  97. */
  98. uint8_t version;
  99. /*
  100. * Type of message, see below.
  101. */
  102. uint8_t msg_type;
  103. /*
  104. * Length of entire message, including the header, in network byte
  105. * order.
  106. *
  107. * Note that msg_len is rounded up to make sure that message is at
  108. * the desired alignment. This means that some payloads may need
  109. * padding at the end.
  110. */
  111. uint16_t msg_len;
  112. } fpm_msg_hdr_t;
  113. /*
  114. * The current version of the FPM protocol is 1.
  115. */
  116. #define FPM_PROTO_VERSION 1
  117. typedef enum fpm_msg_type_e_ {
  118. FPM_MSG_TYPE_NONE = 0,
  119. /*
  120. * Indicates that the payload is a completely formed netlink
  121. * message.
  122. */
  123. FPM_MSG_TYPE_NETLINK = 1,
  124. } fpm_msg_type_e;
  125. /*
  126. * The FPM message header is aligned to the same boundary as netlink
  127. * messages (4). This means that a netlink message does not need
  128. * padding when encapsulated in an FPM message.
  129. */
  130. #define FPM_MSG_ALIGNTO 4
  131. /*
  132. * fpm_msg_align
  133. *
  134. * Round up the given length to the desired alignment.
  135. */
  136. static inline size_t
  137. fpm_msg_align (size_t len)
  138. {
  139. return (len + FPM_MSG_ALIGNTO - 1) & ~(FPM_MSG_ALIGNTO - 1);
  140. }
  141. /*
  142. * The (rounded up) size of the FPM message header. This ensures that
  143. * the message payload always starts at an aligned address.
  144. */
  145. #define FPM_MSG_HDR_LEN (fpm_msg_align (sizeof (fpm_msg_hdr_t)))
  146. /*
  147. * fpm_data_len_to_msg_len
  148. *
  149. * The length value that should be placed in the msg_len field of the
  150. * header for a *payload* of size 'data_len'.
  151. */
  152. static inline size_t
  153. fpm_data_len_to_msg_len (size_t data_len)
  154. {
  155. return fpm_msg_align (data_len) + FPM_MSG_HDR_LEN;
  156. }
  157. /*
  158. * fpm_msg_data
  159. *
  160. * Pointer to the payload of the given fpm header.
  161. */
  162. static inline void *
  163. fpm_msg_data (fpm_msg_hdr_t *hdr)
  164. {
  165. return ((char*) hdr) + FPM_MSG_HDR_LEN;
  166. }
  167. /*
  168. * fpm_msg_len
  169. */
  170. static inline size_t
  171. fpm_msg_len (const fpm_msg_hdr_t *hdr)
  172. {
  173. return ntohs (hdr->msg_len);
  174. }
  175. /*
  176. * fpm_msg_data_len
  177. */
  178. static inline size_t
  179. fpm_msg_data_len (const fpm_msg_hdr_t *hdr)
  180. {
  181. return (fpm_msg_len (hdr) - FPM_MSG_HDR_LEN);
  182. }
  183. /*
  184. * fpm_msg_next
  185. *
  186. * Move to the next message in a buffer.
  187. */
  188. static inline fpm_msg_hdr_t *
  189. fpm_msg_next (fpm_msg_hdr_t *hdr, size_t *len)
  190. {
  191. size_t msg_len;
  192. msg_len = fpm_msg_len (hdr);
  193. if (len) {
  194. if (*len < msg_len)
  195. {
  196. assert(0);
  197. return NULL;
  198. }
  199. *len -= msg_len;
  200. }
  201. return (fpm_msg_hdr_t *) (((char*) hdr) + msg_len);
  202. }
  203. /*
  204. * fpm_msg_hdr_ok
  205. *
  206. * Returns TRUE if a message header looks well-formed.
  207. */
  208. static inline int
  209. fpm_msg_hdr_ok (const fpm_msg_hdr_t *hdr)
  210. {
  211. size_t msg_len;
  212. if (hdr->msg_type == FPM_MSG_TYPE_NONE)
  213. return 0;
  214. msg_len = fpm_msg_len (hdr);
  215. if (msg_len < FPM_MSG_HDR_LEN || msg_len > FPM_MAX_MSG_LEN)
  216. return 0;
  217. if (fpm_msg_align (msg_len) != msg_len)
  218. return 0;
  219. return 1;
  220. }
  221. /*
  222. * fpm_msg_ok
  223. *
  224. * Returns TRUE if a message looks well-formed.
  225. *
  226. * @param len The length in bytes from 'hdr' to the end of the buffer.
  227. */
  228. static inline int
  229. fpm_msg_ok (const fpm_msg_hdr_t *hdr, size_t len)
  230. {
  231. if (len < FPM_MSG_HDR_LEN)
  232. return 0;
  233. if (!fpm_msg_hdr_ok (hdr))
  234. return 0;
  235. if (fpm_msg_len (hdr) > len)
  236. return 0;
  237. return 1;
  238. }
  239. #endif /* _FPM_H */