1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * bfd.h: BFD definitions and structures
5 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
12 #include "lib/zclient.h"
18 #define BFD_DEF_MIN_RX 300
19 #define BFD_MIN_MIN_RX 50
20 #define BFD_MAX_MIN_RX 60000
21 #define BFD_DEF_MIN_TX 300
22 #define BFD_MIN_MIN_TX 50
23 #define BFD_MAX_MIN_TX 60000
24 #define BFD_DEF_DETECT_MULT 3
25 #define BFD_MIN_DETECT_MULT 2
26 #define BFD_MAX_DETECT_MULT 255
28 #define BFD_STATUS_UNKNOWN (1 << 0) /* BFD session status never received */
29 #define BFD_STATUS_DOWN (1 << 1) /* BFD session status is down */
30 #define BFD_STATUS_UP (1 << 2) /* BFD session status is up */
31 #define BFD_STATUS_ADMIN_DOWN (1 << 3) /* BFD session is admin down */
33 #define BFD_PROFILE_NAME_LEN 64
35 const char *bfd_get_status_str(int status
);
37 extern void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
44 /* Forward declaration of argument struct. */
45 struct bfd_session_params
;
47 /** Session state definitions. */
48 enum bfd_session_state
{
49 /** Session state is unknown or not initialized. */
50 BSS_UNKNOWN
= BFD_STATUS_UNKNOWN
,
51 /** Local or remote peer administratively shutdown the session. */
52 BSS_ADMIN_DOWN
= BFD_STATUS_ADMIN_DOWN
,
53 /** Session is down. */
54 BSS_DOWN
= BFD_STATUS_DOWN
,
55 /** Session is up and working correctly. */
56 BSS_UP
= BFD_STATUS_UP
,
59 /** BFD session status information */
60 struct bfd_session_status
{
61 /** Current session state. */
62 enum bfd_session_state state
;
63 /** Previous session state. */
64 enum bfd_session_state previous_state
;
65 /** Remote Control Plane Independent bit state. */
67 /** Last event occurrence. */
72 * Session status update callback.
74 * \param bsp BFD session parameters.
75 * \param bss BFD session status.
76 * \param arg application independent data.
78 typedef void (*bsp_status_update
)(struct bfd_session_params
*bsp
,
79 const struct bfd_session_status
*bss
,
83 * Allocates and initializes the session parameters.
85 * \param updatecb status update notification callback.
86 * \param args application independent data.
88 * \returns pointer to configuration storage.
90 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *args
);
93 * Uninstall session if installed and free resources allocated by the
94 * parameters. Already sets pointer to `NULL` to avoid dangling references.
96 * \param bsp session parameters.
98 void bfd_sess_free(struct bfd_session_params
**bsp
);
101 * Set the local and peer address of the BFD session.
104 * If the address changed the session is removed and must be installed again
105 * with `bfd_sess_install`.
107 * \param bsp BFD session parameters.
108 * \param src local address (optional, can be `NULL`).
109 * \param dst remote address (mandatory).
111 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
112 const struct in_addr
*src
,
113 const struct in_addr
*dst
);
116 * Set the local and peer address of the BFD session.
119 * If the address changed the session is removed and must be installed again
120 * with `bfd_sess_install`.
122 * \param bsp BFD session parameters.
123 * \param src local address (optional, can be `NULL`).
124 * \param dst remote address (mandatory).
126 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
127 const struct in6_addr
*src
,
128 const struct in6_addr
*dst
);
131 * Configure the BFD session interface.
134 * If the interface changed the session is removed and must be installed again
135 * with `bfd_sess_install`.
137 * \param bsp BFD session parameters.
138 * \param ifname interface name (or `NULL` to remove it).
140 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
);
143 * Configure the BFD session profile name.
146 * Session profile will only change after a `bfd_sess_install`.
148 * \param bsp BFD session parameters.
149 * \param profile profile name (or `NULL` to remove it).
151 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
);
154 * Configure the BFD session VRF.
157 * If the VRF changed the session is removed and must be installed again
158 * with `bfd_sess_install`.
160 * \param bsp BFD session parameters.
161 * \param vrf_id the VRF identification number.
163 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
);
166 * Configure the BFD session single/multi hop setting.
169 * If the number of hops is changed the session is removed and must be
170 * installed again with `bfd_sess_install`.
172 * \param bsp BFD session parameters.
173 * \param hops maximum amount of hops expected (1 for single hop, 2 or
174 * more for multi hop).
176 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t hops
);
179 * Configure the BFD session to set the Control Plane Independent bit.
182 * Session CPI bit will only change after a `bfd_sess_install`.
184 * \param bsp BFD session parameters.
185 * \param enable BFD Control Plane Independent state.
187 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
);
190 * DEPRECATED: please avoid using timers directly and use profiles instead.
192 * Configures the BFD session timers to use. This is specially useful with
193 * `ptm-bfd` which does not support timers.
196 * Session timers will only apply if the session has not been created yet.
197 * If the session is already installed you must uninstall and install again
200 * \param bsp BFD session parameters.
201 * \param detection_multiplier the detection multiplier value.
202 * \param min_rx minimum required receive period.
203 * \param min_tx minimum required transmission period.
205 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
206 uint8_t detection_multiplier
, uint32_t min_rx
,
210 * Configures the automatic source selection for the BFD session.
213 * Setting this configuration will override the IP source value set by
214 * `bfd_sess_set_ipv4_addrs` or `bfd_sess_set_ipv6_addrs`.
216 * \param bsp BFD session parameters
217 * \param enable BFD automatic source selection state.
219 void bfd_sess_set_auto_source(struct bfd_session_params
*bsp
, bool enable
);
222 * Installs or updates the BFD session based on the saved session arguments.
225 * This function has a delayed effect: it will only install/update after
226 * all northbound/CLI command batch finishes.
228 * \param bsp session parameters.
230 void bfd_sess_install(struct bfd_session_params
*bsp
);
233 * Uninstall the BFD session based on the saved session arguments.
236 * This function uninstalls the session immediately (if installed) and cancels
237 * any previous `bfd_sess_install` calls.
239 * \param bsp session parameters.
241 void bfd_sess_uninstall(struct bfd_session_params
*bsp
);
244 * Get BFD session current status.
246 * \param bsp session parameters.
248 * \returns BFD session status data structure.
250 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
);
253 * Get BFD session amount of hops configured value.
255 * \param bsp session parameters.
257 * \returns configured amount of hops.
259 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
);
262 * Get BFD session profile configured value.
264 * \param bsp session parameters.
266 * \returns configured profile name (or `NULL` if empty).
268 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
);
271 * Get BFD session addresses.
273 * \param bsp session parameters.
274 * \param family the address family being used (AF_INET or AF_INET6).
275 * \param src source address (optional, may be `NULL`).
276 * \param dst peer address (optional, may be `NULL`).
278 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
279 struct in6_addr
*src
, struct in6_addr
*dst
);
281 * Get BFD session interface name.
283 * \param bsp session parameters.
285 * \returns `NULL` if not set otherwise the interface name.
287 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
);
290 * Get BFD session VRF name.
292 * \param bsp session parameters.
294 * \returns the VRF name.
296 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
);
299 * Get BFD session VRF ID.
301 * \param bsp session parameters.
303 * \returns the VRF name.
305 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
);
308 * Get BFD session control plane independent bit configuration state.
310 * \param bsp session parameters.
312 * \returns `true` if enabled otherwise `false`.
314 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
);
317 * DEPRECATED: please avoid using timers directly and use profiles instead.
319 * Gets the configured timers.
321 * \param bsp BFD session parameters.
322 * \param detection_multiplier the detection multiplier value.
323 * \param min_rx minimum required receive period.
324 * \param min_tx minimum required transmission period.
326 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
327 uint8_t *detection_multiplier
, uint32_t *min_rx
,
331 * Gets the automatic source selection state.
333 bool bfd_sess_auto_source(const struct bfd_session_params
*bsp
);
336 * Show BFD session configuration and status. If `json` is provided (e.g. not
337 * `NULL`) then information will be inserted in object, otherwise printed to
340 * \param vty Pointer to `vty` for outputting text.
341 * \param json (optional) JSON object pointer.
342 * \param bsp session parameters.
344 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
345 struct bfd_session_params
*bsp
);
348 * Initializes the BFD integration library. This function executes the
351 * - Copy the `struct thread_master` pointer to use as "thread" to execute
352 * the BFD session parameters installation.
353 * - Copy the `struct zclient` pointer to install its callbacks.
354 * - Initializes internal data structures.
356 * \param tm normally the daemon main thread event manager.
357 * \param zc the zebra client of the daemon.
359 void bfd_protocol_integration_init(struct zclient
*zc
,
360 struct thread_master
*tm
);
363 * BFD session registration arguments.
365 struct bfd_session_arg
{
370 * - `ZEBRA_BFD_DEST_REGISTER`
371 * - `ZEBRA_BFD_DEST_DEREGISTER`
383 /** Source address. */
385 /** Source address. */
388 /** Multi hop indicator. */
390 /** Expected hops. */
392 /** C bit (Control Plane Independent bit) indicator. */
395 /** Interface name size. */
397 /** Interface name. */
400 /** Daemon or session VRF. */
403 /** Profile name length. */
406 char profile
[BFD_PROFILE_NAME_LEN
];
409 * Deprecation fields: these fields should be removed once `ptm-bfd`
410 * no longer uses this interface.
413 /** Minimum required receive interval (in microseconds). */
415 /** Minimum desired transmission interval (in microseconds). */
417 /** Detection multiplier. */
418 uint32_t detection_multiplier
;
422 * Send a message to BFD daemon through the zebra client.
424 * \param zc the zebra client context.
425 * \param arg the BFD session command arguments.
427 * \returns `-1` on failure otherwise `0`.
429 * \see bfd_session_arg.
431 extern int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*arg
);
434 * Enables or disables BFD protocol integration API debugging.
436 * \param enable new API debug state.
438 extern void bfd_protocol_integration_set_debug(bool enable
);
441 * Sets shutdown mode so no more events are processed.
443 * This is useful to avoid the event storm that happens caused by network,
444 * interfaces or VRFs removal. It should also avoid some crashes due hanging
445 * pointers left overs by protocol.
447 * \param enable new API shutdown state.
449 extern void bfd_protocol_integration_set_shutdown(bool enable
);
452 * Get API debugging state.
454 extern bool bfd_protocol_integration_debug(void);
457 * Get API shutdown state.
459 extern bool bfd_protocol_integration_shutting_down(void);
461 /* Update nexthop-tracking (nht) information for BFD auto source selection.
462 * The function must be called from the daemon callback function
463 * that deals with the ZEBRA_NEXTHOP_UPDATE zclient command
465 extern int bfd_nht_update(const struct prefix
*match
,
466 const struct zapi_route
*route
);
472 #endif /* _ZEBRA_BFD_H */