1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*********************************************************************
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
5 * bfd.h: implements the BFD protocol.
11 #include <netinet/in.h>
18 #include "lib/libfrr.h"
20 #include "lib/queue.h"
26 #define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
28 #define BFDD_JSON_CONV_OPTIONS (0)
32 DECLARE_MTYPE(BFDD_CONTROL
);
33 DECLARE_MTYPE(BFDD_NOTIFICATION
);
36 uint32_t desired_min_tx
;
37 uint32_t required_min_rx
;
38 uint32_t required_min_echo
;
43 uint32_t remote_discr
;
47 * Format of control packet. From section 4)
59 struct bfd_discrs discrs
;
60 struct bfd_timers timers
;
64 * Format of Echo packet.
76 uint64_t time_sent_sec
;
77 uint64_t time_sent_usec
;
81 /* Macros for manipulating control packets */
82 #define BFD_VERMASK 0x03
83 #define BFD_DIAGMASK 0x1F
84 #define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
85 #define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
91 #define BFD_DEMANDBIT 0x02
92 #define BFD_SETDEMANDBIT(flags, val) \
95 flags |= BFD_DEMANDBIT; \
97 #define BFD_SETPBIT(flags, val) \
102 #define BFD_GETPBIT(flags) (flags & BFD_PBIT)
103 #define BFD_SETFBIT(flags, val) \
108 #define BFD_GETFBIT(flags) (flags & BFD_FBIT)
109 #define BFD_SETSTATE(flags, val) \
112 flags |= (val & 0x3) << 6; \
114 #define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
115 #define BFD_SETCBIT(flags, val) \
120 #define BFD_GETCBIT(flags) (flags & BFD_FBIT)
121 #define BFD_ECHO_VERSION 1
122 #define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
124 enum bfd_diagnosticis
{
126 /* Control Detection Time Expired. */
127 BD_CONTROL_EXPIRED
= 1,
128 /* Echo Function Failed. */
130 /* Neighbor Signaled Session Down. */
131 BD_NEIGHBOR_DOWN
= 3,
132 /* Forwarding Plane Reset. */
133 BD_FORWARDING_RESET
= 4,
136 /* Concatenated Path Down. */
137 BD_CONCATPATH_DOWN
= 6,
138 /* Administratively Down. */
140 /* Reverse Concatenated Path Down. */
141 BD_REVCONCATPATH_DOWN
= 8,
142 /* 9..31: reserved. */
145 /* BFD session flags */
146 enum bfd_session_flags
{
147 BFD_SESS_FLAG_NONE
= 0,
148 BFD_SESS_FLAG_ECHO
= 1 << 0, /* BFD Echo functionality */
149 BFD_SESS_FLAG_ECHO_ACTIVE
= 1 << 1, /* BFD Echo Packets are being sent
152 BFD_SESS_FLAG_MH
= 1 << 2, /* BFD Multi-hop session */
153 BFD_SESS_FLAG_IPV6
= 1 << 4, /* BFD IPv6 session */
154 BFD_SESS_FLAG_SEND_EVT_ACTIVE
= 1 << 5, /* send event timer active */
155 BFD_SESS_FLAG_SEND_EVT_IGNORE
= 1 << 6, /* ignore send event when timer
158 BFD_SESS_FLAG_SHUTDOWN
= 1 << 7, /* disable BGP peer function */
159 BFD_SESS_FLAG_CONFIG
= 1 << 8, /* Session configured with bfd NB API */
160 BFD_SESS_FLAG_CBIT
= 1 << 9, /* CBIT is set */
161 BFD_SESS_FLAG_PASSIVE
= 1 << 10, /* Passive mode */
162 BFD_SESS_FLAG_MAC_SET
= 1 << 11, /* MAC of peer known */
166 * BFD session hash key.
168 * This structure must not have any padding bytes because their value is
169 * unspecified after the struct assignment. Even when all fields of two keys
170 * are the same, if the padding bytes are different, then the calculated hash
171 * value is different, and the hash lookup will fail.
173 * Currently, the structure fields are correctly aligned, and the "packed"
174 * attribute is added as a precaution. "family" and "mhop" fields are two-bytes
175 * to eliminate unaligned memory access to "peer" and "local".
180 struct in6_addr peer
;
181 struct in6_addr local
;
182 char ifname
[INTERFACE_NAMSIZ
];
183 char vrfname
[VRF_NAMSIZ
];
184 } __attribute__((packed
));
186 struct bfd_session_stats
{
187 uint64_t rx_ctrl_pkt
;
188 uint64_t tx_ctrl_pkt
;
189 uint64_t rx_echo_pkt
;
190 uint64_t tx_echo_pkt
;
192 uint64_t session_down
;
193 uint64_t znotification
;
197 * BFD session profile to override default configurations.
203 /** Session detection multiplier. */
204 uint8_t detection_multiplier
;
205 /** Desired transmission interval (in microseconds). */
207 /** Minimum required receive interval (in microseconds). */
209 /** Administrative state. */
213 /** Minimum expected TTL value. */
216 /** Echo mode (only applies to single hop). */
218 /** Desired echo transmission interval (in microseconds). */
219 uint32_t min_echo_tx
;
220 /** Minimum required echo receive interval (in microseconds). */
221 uint32_t min_echo_rx
;
223 /** Profile list entry. */
224 TAILQ_ENTRY(bfd_profile
) entry
;
227 /** Profile list type. */
228 TAILQ_HEAD(bfdproflist
, bfd_profile
);
230 /* bfd_session shortcut label forwarding. */
233 struct bfd_config_timers
{
234 uint32_t desired_min_tx
;
235 uint32_t required_min_rx
;
236 uint32_t desired_min_echo_tx
;
237 uint32_t required_min_echo_rx
;
240 #define BFD_RTT_SAMPLE 8
243 * Session state information
247 /* protocol state per RFC 5880*/
249 struct bfd_discrs discrs
;
253 uint8_t remote_detect_mult
;
257 /** BFD profile name. */
259 /** BFD pre configured profile. */
260 struct bfd_profile
*profile
;
261 /** BFD peer configuration (without profile). */
262 struct bfd_profile peer_profile
;
265 struct bfd_config_timers timers
;
266 struct bfd_timers cur_timers
;
268 struct event
*echo_recvtimer_ev
;
269 struct event
*recvtimer_ev
;
271 uint64_t echo_xmt_TO
;
272 struct event
*xmttimer_ev
;
273 struct event
*echo_xmttimer_ev
;
274 uint64_t echo_detect_TO
;
276 /* software object state */
279 /* This and the localDiscr are the keys to state info */
281 struct peer_label
*pl
;
283 struct bfd_dplane_ctx
*bdc
;
284 struct sockaddr_any local_address
;
285 uint8_t peer_hw_addr
[ETH_ALEN
];
286 struct interface
*ifp
;
291 /* BFD session flags */
292 enum bfd_session_flags flags
;
294 struct bfd_session_stats stats
;
296 struct timeval uptime
; /* last up time */
297 struct timeval downtime
; /* last down time */
299 /* Remote peer data (for debugging mostly) */
301 struct bfd_timers remote_timers
;
303 uint64_t refcount
; /* number of pointers referencing this. */
305 uint8_t rtt_valid
; /* number of valid samples */
306 uint8_t rtt_index
; /* last index added */
307 uint64_t rtt
[BFD_RTT_SAMPLE
]; /* RRT in usec for echo to be looped */
311 TAILQ_ENTRY(peer_label
) pl_entry
;
313 struct bfd_session
*pl_bs
;
314 char pl_label
[MAXNAMELEN
];
316 TAILQ_HEAD(pllist
, peer_label
);
318 struct bfd_diag_str_list
{
323 struct bfd_state_str_list
{
328 struct bfd_session_observer
{
329 struct bfd_session
*bso_bs
;
330 char bso_entryname
[MAXNAMELEN
];
331 struct prefix bso_addr
;
333 TAILQ_ENTRY(bfd_session_observer
) bso_entry
;
335 TAILQ_HEAD(obslist
, bfd_session_observer
);
338 /* States defined per 4.1 */
339 #define PTM_BFD_ADM_DOWN 0
340 #define PTM_BFD_DOWN 1
341 #define PTM_BFD_INIT 2
345 /* Various constants */
346 /* Retrieved from ptm_timer.h from Cumulus PTM sources. */
347 #define BFD_DEF_DEMAND 0
348 #define BFD_DEFDETECTMULT 3
349 #define BFD_DEFDESIREDMINTX (300 * 1000) /* microseconds. */
350 #define BFD_DEFREQUIREDMINRX (300 * 1000) /* microseconds. */
351 #define BFD_DEF_DES_MIN_ECHO_TX (50 * 1000) /* microseconds. */
352 #define BFD_DEF_REQ_MIN_ECHO_RX (50 * 1000) /* microseconds. */
353 #define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
354 /** Minimum multi hop TTL. */
355 #define BFD_DEF_MHOP_TTL 254
356 #define BFD_PKT_LEN 24 /* Length of control packet */
357 #define BFD_TTL_VAL 255
358 #define BFD_RCV_TTL_VAL 1
359 #define BFD_TOS_VAL 0xC0
360 #define BFD_PKT_INFO_VAL 1
361 #define BFD_IPV6_PKT_INFO_VAL 1
362 #define BFD_IPV6_ONLY_VAL 1
363 #define BFD_SRCPORTINIT 49152
364 #define BFD_SRCPORTMAX 65535
365 #define BFD_DEFDESTPORT 3784
366 #define BFD_DEF_ECHO_PORT 3785
367 #define BFD_DEF_MHOP_DEST_PORT 4784
372 * Daemon control code to speak with local consumers.
375 /* See 'bfdctrl.h' for client protocol definitions. */
377 struct bfd_control_buffer
{
381 struct bfd_control_msg
*bcb_bcm
;
386 struct bfd_control_queue
{
387 TAILQ_ENTRY(bfd_control_queue
) bcq_entry
;
389 struct bfd_control_buffer bcq_bcb
;
391 TAILQ_HEAD(bcqueue
, bfd_control_queue
);
393 struct bfd_notify_peer
{
394 TAILQ_ENTRY(bfd_notify_peer
) bnp_entry
;
396 struct bfd_session
*bnp_bs
;
398 TAILQ_HEAD(bnplist
, bfd_notify_peer
);
400 struct bfd_control_socket
{
401 TAILQ_ENTRY(bfd_control_socket
) bcs_entry
;
404 struct event
*bcs_ev
;
405 struct event
*bcs_outev
;
406 struct bcqueue bcs_bcqueue
;
408 /* Notification data */
410 struct bnplist bcs_bnplist
;
412 enum bc_msg_version bcs_version
;
413 enum bc_msg_type bcs_type
;
415 /* Message buffering */
416 struct bfd_control_buffer bcs_bin
;
417 struct bfd_control_buffer
*bcs_bout
;
419 TAILQ_HEAD(bcslist
, bfd_control_socket
);
421 int control_init(const char *path
);
422 void control_shutdown(void);
423 int control_notify(struct bfd_session
*bs
, uint8_t notify_state
);
424 int control_notify_config(const char *op
, struct bfd_session
*bs
);
425 void control_accept(struct event
*t
);
431 * Daemon specific code.
433 struct bfd_vrf_global
{
442 struct event
*bg_ev
[6];
445 /* Forward declaration of data plane context struct. */
446 struct bfd_dplane_ctx
;
447 TAILQ_HEAD(dplane_queue
, bfd_dplane_ctx
);
451 struct event
*bg_csockev
;
452 struct bcslist bg_bcslist
;
454 struct pllist bg_pllist
;
456 struct obslist bg_obslist
;
458 struct zebra_privs_t bfdd_privs
;
461 * Daemon is exit()ing? Use this to avoid actions that expect a
462 * running system or to avoid unnecessary operations when quitting.
466 /* Distributed BFD items. */
469 struct event
*bg_dplane_sockev
;
470 struct dplane_queue bg_dplaneq
;
473 /* Show distributed BFD debug messages. */
475 /* Show all peer state changes events. */
476 bool debug_peer_event
;
478 * Show zebra message exchanges:
479 * - Interface add/delete.
480 * - Local address add/delete.
485 * Show network level debug information:
486 * - Echo packets without session.
487 * - Unavailable peer sessions.
488 * - Network system call failures.
493 extern struct bfd_global bglobal
;
494 extern const struct bfd_diag_str_list diag_list
[];
495 extern const struct bfd_state_str_list state_list
[];
497 void socket_close(int *s
);
503 * Contains the code related with loading/reloading configuration.
505 int parse_config(const char *fname
);
506 int config_request_add(const char *jsonstr
);
507 int config_request_del(const char *jsonstr
);
508 char *config_response(const char *status
, const char *error
);
509 char *config_notify(struct bfd_session
*bs
);
510 char *config_notify_config(const char *op
, struct bfd_session
*bs
);
512 typedef int (*bpc_handle
)(struct bfd_peer_cfg
*, void *arg
);
513 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
516 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
);
517 struct peer_label
*pl_find(const char *label
);
518 void pl_free(struct peer_label
*pl
);
522 * logging - alias to zebra log
524 #define zlog_fatal(msg, ...) \
526 zlog_err(msg, ##__VA_ARGS__); \
535 * Contains the code related with receiving/seding, packing/unpacking BFD data.
537 int bp_set_ttlv6(int sd
, uint8_t value
);
538 int bp_set_ttl(int sd
, uint8_t value
);
539 int bp_set_tosv6(int sd
, uint8_t value
);
540 int bp_set_tos(int sd
, uint8_t value
);
541 int bp_bind_dev(int sd
, const char *dev
);
543 int bp_udp_shop(const struct vrf
*vrf
);
544 int bp_udp_mhop(const struct vrf
*vrf
);
545 int bp_udp6_shop(const struct vrf
*vrf
);
546 int bp_udp6_mhop(const struct vrf
*vrf
);
547 int bp_peer_socket(const struct bfd_session
*bs
);
548 int bp_peer_socketv6(const struct bfd_session
*bs
);
549 int bp_echo_socket(const struct vrf
*vrf
);
550 int bp_echov6_socket(const struct vrf
*vrf
);
552 void ptm_bfd_snd(struct bfd_session
*bfd
, int fbit
);
553 void ptm_bfd_echo_snd(struct bfd_session
*bfd
);
554 void ptm_bfd_echo_fp_snd(struct bfd_session
*bfd
);
556 void bfd_recv_cb(struct event
*t
);
562 * Contains the code related with event loop.
564 typedef void (*bfd_ev_cb
)(struct event
*t
);
566 void bfd_recvtimer_update(struct bfd_session
*bs
);
567 void bfd_echo_recvtimer_update(struct bfd_session
*bs
);
568 void bfd_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
569 void bfd_echo_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
571 void bfd_xmttimer_delete(struct bfd_session
*bs
);
572 void bfd_echo_xmttimer_delete(struct bfd_session
*bs
);
573 void bfd_recvtimer_delete(struct bfd_session
*bs
);
574 void bfd_echo_recvtimer_delete(struct bfd_session
*bs
);
576 void bfd_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
577 void bfd_echo_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
578 void bfd_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
579 void bfd_echo_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
585 * BFD protocol specific code.
587 int bfd_session_enable(struct bfd_session
*bs
);
588 void bfd_session_disable(struct bfd_session
*bs
);
589 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
);
590 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
);
591 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
);
592 void ptm_bfd_sess_up(struct bfd_session
*bfd
);
593 void ptm_bfd_echo_stop(struct bfd_session
*bfd
);
594 void ptm_bfd_echo_start(struct bfd_session
*bfd
);
595 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
);
596 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
);
597 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
598 struct sockaddr_any
*peer
,
599 struct sockaddr_any
*local
,
600 struct interface
*ifp
,
604 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
);
605 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
);
606 void bfd_set_polling(struct bfd_session
*bs
);
607 void bs_state_handler(struct bfd_session
*bs
, int nstate
);
608 void bs_echo_timer_handler(struct bfd_session
*bs
);
609 void bs_final_handler(struct bfd_session
*bs
);
610 void bs_set_slow_timers(struct bfd_session
*bs
);
611 const char *satostr(const struct sockaddr_any
*sa
);
612 const char *diag2str(uint8_t diag
);
613 int strtosa(const char *addr
, struct sockaddr_any
*sa
);
614 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
);
615 const char *bs_to_string(const struct bfd_session
*bs
);
617 int bs_observer_add(struct bfd_session
*bs
);
618 void bs_observer_del(struct bfd_session_observer
*bso
);
620 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
622 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
623 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
624 const char *vrfname
);
625 struct bfd_session
*bfd_session_new(void);
626 struct bfd_session
*bs_registrate(struct bfd_session
*bs
);
627 void bfd_session_free(struct bfd_session
*bs
);
628 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
630 void bfd_sessions_remove_manual(void);
631 void bfd_profiles_remove(void);
632 void bfd_rtt_init(struct bfd_session
*bfd
);
635 * Set the BFD session echo state.
637 * \param bs the BFD session.
638 * \param echo the echo operational state.
640 void bfd_set_echo(struct bfd_session
*bs
, bool echo
);
643 * Set the BFD session functional state.
645 * \param bs the BFD session.
646 * \param shutdown the operational value.
648 void bfd_set_shutdown(struct bfd_session
*bs
, bool shutdown
);
651 * Set the BFD session passive mode.
653 * \param bs the BFD session.
654 * \param passive the passive mode.
656 void bfd_set_passive_mode(struct bfd_session
*bs
, bool passive
);
659 * Picks the BFD session configuration from the appropriated source:
660 * if using the default peer configuration prefer profile (if it exists),
661 * otherwise use session.
663 * \param bs the BFD session.
665 void bfd_session_apply(struct bfd_session
*bs
);
667 /* BFD hash data structures interface */
668 void bfd_initialize(void);
669 void bfd_shutdown(void);
670 void bfd_vrf_init(void);
671 void bfd_vrf_terminate(void);
672 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
);
673 struct bfd_session
*bfd_id_lookup(uint32_t id
);
674 struct bfd_session
*bfd_key_lookup(struct bfd_key key
);
676 struct bfd_session
*bfd_id_delete(uint32_t id
);
677 struct bfd_session
*bfd_key_delete(struct bfd_key key
);
679 bool bfd_id_insert(struct bfd_session
*bs
);
680 bool bfd_key_insert(struct bfd_session
*bs
);
682 typedef void (*hash_iter_func
)(struct hash_bucket
*hb
, void *arg
);
683 void bfd_id_iterate(hash_iter_func hif
, void *arg
);
684 void bfd_key_iterate(hash_iter_func hif
, void *arg
);
686 unsigned long bfd_get_session_count(void);
688 /* Export callback functions for `event.c`. */
689 extern struct thread_master
*master
;
691 void bfd_recvtimer_cb(struct event
*t
);
692 void bfd_echo_recvtimer_cb(struct event
*t
);
693 void bfd_xmt_cb(struct event
*t
);
694 void bfd_echo_xmt_cb(struct event
*t
);
696 extern struct in6_addr zero_addr
;
699 * Creates a new profile entry and insert into the global list.
701 * \param name the BFD profile name.
703 * \returns `NULL` if it already exists otherwise the new entry.
705 struct bfd_profile
*bfd_profile_new(const char *name
);
708 * Search for configured BFD profiles (profile name is case insensitive).
710 * \param name the BFD profile name.
712 * \returns `NULL` if it doesn't exist otherwise the entry.
714 struct bfd_profile
*bfd_profile_lookup(const char *name
);
717 * Removes profile from list and free memory.
719 * \param bp the BFD profile.
721 void bfd_profile_free(struct bfd_profile
*bp
);
724 * Apply a profile configuration to an existing BFD session. The non default
725 * values will not be overridden.
727 * NOTE: if the profile doesn't exist yet, then the profile will be applied
728 * once it begins to exist.
730 * \param profile_name the BFD profile name.
731 * \param bs the BFD session.
733 void bfd_profile_apply(const char *profname
, struct bfd_session
*bs
);
736 * Remove any applied profile from session and revert the session
739 * \param bs the BFD session.
741 void bfd_profile_remove(struct bfd_session
*bs
);
744 * Apply new profile values to sessions using it.
746 * \param[in] bp the BFD profile that got updated.
748 void bfd_profile_update(struct bfd_profile
*bp
);
753 * BFD daemon vty shell commands.
755 void bfdd_vty_init(void);
761 * BFD daemon CLI implementation.
763 void bfdd_cli_init(void);
769 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
);
770 void bfdd_zclient_stop(void);
771 void bfdd_zclient_unregister(vrf_id_t vrf_id
);
772 void bfdd_zclient_register(vrf_id_t vrf_id
);
773 void bfdd_sessions_enable_vrf(struct vrf
*vrf
);
774 void bfdd_sessions_disable_vrf(struct vrf
*vrf
);
776 int ptm_bfd_notify(struct bfd_session
*bs
, uint8_t notify_state
);
783 * Initialize BFD data plane infrastructure for distributed BFD implementation.
785 * \param sa socket address.
786 * \param salen socket address structure length.
787 * \param client `true` means connecting socket, `false` listening socket.
789 void bfd_dplane_init(const struct sockaddr
*sa
, socklen_t salen
, bool client
);
792 * Attempts to delegate the BFD session liveness detection to hardware.
794 * \param bs the BFD session data structure.
797 * `0` on success and BFD daemon should do nothing or `-1` on failure
798 * and we should fallback to software implementation.
800 int bfd_dplane_add_session(struct bfd_session
*bs
);
803 * Send new session settings to data plane.
805 * \param bs the BFD session to update.
807 int bfd_dplane_update_session(const struct bfd_session
*bs
);
810 * Deletes session from data plane.
812 * \param bs the BFD session to delete.
814 * \returns `0` on success otherwise `-1`.
816 int bfd_dplane_delete_session(struct bfd_session
*bs
);
819 * Asks the data plane for updated counters and update the session data
822 * \param bs the BFD session that needs updating.
824 * \returns `0` on success otherwise `-1` on failure.
826 int bfd_dplane_update_session_counters(struct bfd_session
*bs
);
828 void bfd_dplane_show_counters(struct vty
*vty
);