2 * bfd.h: BFD definitions and structures
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "lib/zclient.h"
33 #define BFD_DEF_MIN_RX 300
34 #define BFD_MIN_MIN_RX 50
35 #define BFD_MAX_MIN_RX 60000
36 #define BFD_DEF_MIN_TX 300
37 #define BFD_MIN_MIN_TX 50
38 #define BFD_MAX_MIN_TX 60000
39 #define BFD_DEF_DETECT_MULT 3
40 #define BFD_MIN_DETECT_MULT 2
41 #define BFD_MAX_DETECT_MULT 255
43 #define BFD_GBL_FLAG_IN_SHUTDOWN (1 << 0) /* The daemon in shutdown */
48 #define BFD_FLAG_PARAM_CFG (1 << 0) /* parameters have been configured */
49 #define BFD_FLAG_BFD_REG (1 << 1) /* Peer registered with BFD */
50 #define BFD_FLAG_BFD_TYPE_MULTIHOP (1 << 2) /* Peer registered with BFD as multihop */
51 #define BFD_FLAG_BFD_CBIT_ON (1 << 3) /* Peer registered with CBIT set to on */
52 #define BFD_FLAG_BFD_CHECK_CONTROLPLANE (1 << 4) /* BFD and controlplane daemon are linked */
54 #define BFD_STATUS_UNKNOWN (1 << 0) /* BFD session status never received */
55 #define BFD_STATUS_DOWN (1 << 1) /* BFD session status is down */
56 #define BFD_STATUS_UP (1 << 2) /* BFD session status is up */
57 #define BFD_STATUS_ADMIN_DOWN (1 << 3) /* BFD session is admin down */
59 #define BFD_PROFILE_NAME_LEN 64
61 #define BFD_SET_CLIENT_STATUS(current_status, new_status) \
64 (((new_status) == BFD_STATUS_ADMIN_DOWN) ? \
65 BFD_STATUS_DOWN : (new_status));\
69 BFD_TYPE_NOT_CONFIGURED
,
77 uint32_t desired_min_tx
;
78 uint32_t required_min_rx
;
81 enum bfd_sess_type type
;
82 char profile
[BFD_PROFILE_NAME_LEN
];
85 extern struct bfd_info
*bfd_info_create(void);
87 extern void bfd_info_free(struct bfd_info
**bfd_info
);
89 extern int bfd_validate_param(struct vty
*vty
, const char *dm_str
,
90 const char *rx_str
, const char *tx_str
,
91 uint8_t *dm_val
, uint32_t *rx_val
,
94 extern void bfd_set_param(struct bfd_info
**bfd_info
, uint32_t min_rx
,
95 uint32_t min_tx
, uint8_t detect_mult
,
96 const char *profile
, int defaults
, int *command
);
97 extern void bfd_peer_sendmsg(struct zclient
*zclient
, struct bfd_info
*bfd_info
,
98 int family
, void *dst_ip
, void *src_ip
,
99 char *if_name
, int ttl
, int multihop
, int cbit
,
100 int command
, int set_flag
, vrf_id_t vrf_id
);
102 extern const char *bfd_get_command_dbg_str(int command
);
104 extern struct interface
*bfd_get_peer_info(struct stream
*s
, struct prefix
*dp
,
105 struct prefix
*sp
, int *status
,
109 const char *bfd_get_status_str(int status
);
111 extern void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
,
112 int bfd_tag
, int extra_space
, bool use_json
,
113 json_object
*json_obj
);
115 extern void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
,
116 int multihop
, int extra_space
, bool use_json
,
117 json_object
*json_obj
);
119 extern void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
122 extern void bfd_gbl_init(void);
124 extern void bfd_gbl_exit(void);
131 /* Forward declaration of argument struct. */
132 struct bfd_session_params
;
134 /** Session state definitions. */
135 enum bfd_session_state
{
136 /** Session state is unknown or not initialized. */
137 BSS_UNKNOWN
= BFD_STATUS_UNKNOWN
,
138 /** Local or remote peer administratively shutdown the session. */
139 BSS_ADMIN_DOWN
= BFD_STATUS_ADMIN_DOWN
,
140 /** Session is down. */
141 BSS_DOWN
= BFD_STATUS_DOWN
,
142 /** Session is up and working correctly. */
143 BSS_UP
= BFD_STATUS_UP
,
146 /** BFD session status information */
147 struct bfd_session_status
{
148 /** Current session state. */
149 enum bfd_session_state state
;
150 /** Previous session state. */
151 enum bfd_session_state previous_state
;
152 /** Remote Control Plane Independent bit state. */
154 /** Last event occurrence. */
159 * Session status update callback.
161 * \param bsp BFD session parameters.
162 * \param bss BFD session status.
163 * \param arg application independent data.
165 typedef void (*bsp_status_update
)(struct bfd_session_params
*bsp
,
166 const struct bfd_session_status
*bss
,
170 * Allocates and initializes the session parameters.
172 * \param updatedb status update notification callback.
173 * \param args application independent data.
175 * \returns pointer to configuration storage.
177 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *args
);
180 * Uninstall session if installed and free resources allocated by the
181 * parameters. Already sets pointer to `NULL` to avoid dangling references.
183 * \param bsp session parameters.
185 void bfd_sess_free(struct bfd_session_params
**bsp
);
188 * Enable/disable session installation.
190 * \param bsp session parameters.
191 * \param enable knob variable.
193 void bfd_sess_enable(struct bfd_session_params
*bsp
, bool enable
);
196 * Set the local and peer address of the BFD session.
198 * \param bsp BFD session parameters.
199 * \param src local address (optional, can be `NULL`).
200 * \param dst remote address (mandatory).
202 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
203 struct in_addr
*src
, struct in_addr
*dst
);
206 * Set the local and peer address of the BFD session.
208 * \param bsp BFD session parameters.
209 * \param src local address (optional, can be `NULL`).
210 * \param dst remote address (mandatory).
212 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
213 struct in6_addr
*src
, struct in6_addr
*dst
);
216 * Configure the BFD session interface.
218 * \param bsp BFD session parameters.
219 * \param ifname interface name (or `NULL` to remove it).
221 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
);
224 * Configure the BFD session profile name.
226 * \param bsp BFD session parameters.
227 * \param profile profile name (or `NULL` to remove it).
229 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
);
232 * Configure the BFD session VRF.
234 * \param bsp BFD session parameters.
235 * \param vrf_id the VRF identification number.
237 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
);
240 * Configure the BFD session single/multi hop setting.
242 * \param bsp BFD session parameters.
243 * \param min_ttl minimum TTL value expected (255 for single hop, 254 for
244 * multi hop with single hop, 253 for multi hop with two hops
245 * and so on). See `BFD_SINGLE_HOP_TTL` and
246 * `BFD_MULTI_HOP_MIN_TTL` for defaults.
248 * To simplify things if your protocol only knows the amount of hops it is
249 * better to use `bfd_sess_set_hops` instead.
251 void bfd_sess_set_mininum_ttl(struct bfd_session_params
*bsp
, uint8_t min_ttl
);
253 /** To use single hop the minimum TTL must be set to this. */
254 #define BFD_SINGLE_HOP_TTL 255
255 /** To use multi hop the minimum TTL must be set to this or less. */
256 #define BFD_MULTI_HOP_MIN_TTL 254
259 * This function is the inverted version of `bfd_sess_set_minimum_ttl`.
260 * Instead of receiving the minimum expected TTL, it receives the amount of
261 * hops the protocol will jump.
263 * \param bsp BFD session parameters.
264 * \param min_ttl minimum amount of hops expected (1 for single hop, 2 or
265 * more for multi hop).
267 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t min_ttl
);
270 * Configure the BFD session to set the Control Plane Independent bit.
272 * \param bsp BFD session parameters.
273 * \param enable BFD Control Plane Independent state.
275 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
);
278 * DEPRECATED: please avoid using timers directly and use profiles instead.
280 * Configures the BFD session timers to use. This is specially useful with
281 * `ptm-bfd` which does not support timers.
283 * \param bsp BFD session parameters.
284 * \param detection_multiplier the detection multiplier value.
285 * \param min_rx minimum required receive period.
286 * \param min_tx minimum required transmission period.
288 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
289 uint8_t detection_multiplier
, uint32_t min_rx
,
293 * Installs or updates the BFD session based on the saved session arguments.
295 * \param bsp session parameters.
297 void bfd_sess_install(struct bfd_session_params
*bsp
);
300 * Uninstall the BFD session based on the saved session arguments.
302 * \param bsp session parameters.
304 void bfd_sess_uninstall(struct bfd_session_params
*bsp
);
307 * Get BFD session current status.
309 * \param bsp session parameters.
311 * \returns BFD session status data structure.
313 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
);
316 * Get BFD session minimum TTL configured value.
318 * \param bsp session parameters.
320 * \returns configured minimum TTL.
322 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params
*bsp
);
325 * Inverted version of `bfd_sess_minimum_ttl`. Gets the amount of hops in the
328 * \param bsp session parameters.
330 * \returns configured amount of hops.
332 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
);
335 * Get BFD session profile configured value.
337 * \param bsp session parameters.
339 * \returns configured profile name (or `NULL` if empty).
341 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
);
344 * Get BFD session addresses.
346 * \param bsp session parameters.
347 * \param family the address family being used (AF_INET or AF_INET6).
348 * \param src source address (optional, may be `NULL`).
349 * \param dst peer address (optional, may be `NULL`).
351 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
352 struct in6_addr
*src
, struct in6_addr
*dst
);
354 * Get BFD session interface name.
356 * \param bsp session parameters.
358 * \returns `NULL` if not set otherwise the interface name.
360 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
);
363 * Get BFD session VRF name.
365 * \param bsp session parameters.
367 * \returns the VRF name.
369 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
);
372 * Get BFD session VRF ID.
374 * \param bsp session parameters.
376 * \returns the VRF name.
378 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
);
381 * Get BFD session control plane independent bit configuration state.
383 * \param bsp session parameters.
385 * \returns `true` if enabled otherwise `false`.
387 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
);
390 * DEPRECATED: please avoid using timers directly and use profiles instead.
392 * Gets the configured timers.
394 * \param bsp BFD session parameters.
395 * \param detection_multiplier the detection multiplier value.
396 * \param min_rx minimum required receive period.
397 * \param min_tx minimum required transmission period.
399 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
400 uint8_t *detection_multiplier
, uint32_t *min_rx
,
404 * Show BFD session configuration and status. If `json` is provided (e.g. not
405 * `NULL`) then information will be inserted in object, otherwise printed to
408 * \param vty Pointer to `vty` for outputting text.
409 * \param json (optional) JSON object pointer.
410 * \param bsp session parameters.
412 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
413 struct bfd_session_params
*bsp
);
416 * Initializes the BFD integration library. This function executes the
419 * - Copy the `struct thread_master` pointer to use as "thread" to execute
420 * the BFD session parameters installation.
421 * - Copy the `struct zclient` pointer to install its callbacks.
422 * - Initializes internal data structures.
424 * \param tm normally the daemon main thread event manager.
425 * \param zc the zebra client of the daemon.
427 void bfd_protocol_integration_init(struct zclient
*zc
,
428 struct thread_master
*tm
);
431 * BFD session registration arguments.
433 struct bfd_session_arg
{
438 * - `ZEBRA_BFD_DEST_REGISTER`
439 * - `ZEBRA_BFD_DEST_DEREGISTER`
451 /** Source address. */
453 /** Source address. */
456 /** Multi hop indicator. */
460 /** C bit (Control Plane Independent bit) indicator. */
463 /** Interface name size. */
465 /** Interface name. */
468 /** Daemon or session VRF. */
471 /** Profile name length. */
474 char profile
[BFD_PROFILE_NAME_LEN
];
477 * Deprecation fields: these fields should be removed once `ptm-bfd`
478 * no longer uses this interface.
481 /** Minimum required receive interval (in microseconds). */
483 /** Minimum desired transmission interval (in microseconds). */
485 /** Detection multiplier. */
486 uint32_t detection_multiplier
;
488 /** BFD client information output. */
489 struct bfd_info
*bfd_info
;
491 /** Write registration indicator. */
496 * Send a message to BFD daemon through the zebra client.
498 * \param zc the zebra client context.
499 * \param arg the BFD session command arguments.
501 * \returns `-1` on failure otherwise `0`.
503 * \see bfd_session_arg.
505 extern int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*arg
);
508 * Enables or disables BFD protocol integration API debugging.
510 * \param enable new API debug state.
512 extern void bfd_protocol_integration_set_debug(bool enable
);
515 * Sets shutdown mode so no more events are processed.
517 * This is useful to avoid the event storm that happens caused by network,
518 * interfaces or VRFs removal. It should also avoid some crashes due hanging
519 * pointers left overs by protocol.
521 * \param enable new API shutdown state.
523 extern void bfd_protocol_integration_set_shutdown(bool enable
);
526 * Get API debugging state.
528 extern bool bfd_protocol_integration_debug(void);
531 * Get API shutdown state.
533 extern bool bfd_protocol_integration_shutting_down(void);
539 #endif /* _ZEBRA_BFD_H */