1 /*********************************************************************
2 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * bfd.h: implements the BFD protocol.
24 #include <netinet/in.h>
31 #include "lib/libfrr.h"
33 #include "lib/queue.h"
39 #define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
41 #define BFDD_JSON_CONV_OPTIONS (0)
45 DECLARE_MTYPE(BFDD_CONTROL
);
46 DECLARE_MTYPE(BFDD_NOTIFICATION
);
49 uint32_t desired_min_tx
;
50 uint32_t required_min_rx
;
51 uint32_t required_min_echo
;
56 uint32_t remote_discr
;
60 * Format of control packet. From section 4)
72 struct bfd_discrs discrs
;
73 struct bfd_timers timers
;
77 * Format of Echo packet.
93 /* Macros for manipulating control packets */
94 #define BFD_VERMASK 0x03
95 #define BFD_DIAGMASK 0x1F
96 #define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
97 #define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
100 #define BFD_FBIT 0x10
101 #define BFD_CBIT 0x08
102 #define BFD_ABIT 0x04
103 #define BFD_DEMANDBIT 0x02
104 #define BFD_SETDEMANDBIT(flags, val) \
107 flags |= BFD_DEMANDBIT; \
109 #define BFD_SETPBIT(flags, val) \
114 #define BFD_GETPBIT(flags) (flags & BFD_PBIT)
115 #define BFD_SETFBIT(flags, val) \
120 #define BFD_GETFBIT(flags) (flags & BFD_FBIT)
121 #define BFD_SETSTATE(flags, val) \
124 flags |= (val & 0x3) << 6; \
126 #define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
127 #define BFD_SETCBIT(flags, val) \
132 #define BFD_GETCBIT(flags) (flags & BFD_FBIT)
133 #define BFD_ECHO_VERSION 1
134 #define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
136 enum bfd_diagnosticis
{
138 /* Control Detection Time Expired. */
139 BD_CONTROL_EXPIRED
= 1,
140 /* Echo Function Failed. */
142 /* Neighbor Signaled Session Down. */
143 BD_NEIGHBOR_DOWN
= 3,
144 /* Forwarding Plane Reset. */
145 BD_FORWARDING_RESET
= 4,
148 /* Concatenated Path Down. */
149 BD_CONCATPATH_DOWN
= 6,
150 /* Administratively Down. */
152 /* Reverse Concatenated Path Down. */
153 BD_REVCONCATPATH_DOWN
= 8,
154 /* 9..31: reserved. */
157 /* BFD session flags */
158 enum bfd_session_flags
{
159 BFD_SESS_FLAG_NONE
= 0,
160 BFD_SESS_FLAG_ECHO
= 1 << 0, /* BFD Echo functionality */
161 BFD_SESS_FLAG_ECHO_ACTIVE
= 1 << 1, /* BFD Echo Packets are being sent
164 BFD_SESS_FLAG_MH
= 1 << 2, /* BFD Multi-hop session */
165 BFD_SESS_FLAG_IPV6
= 1 << 4, /* BFD IPv6 session */
166 BFD_SESS_FLAG_SEND_EVT_ACTIVE
= 1 << 5, /* send event timer active */
167 BFD_SESS_FLAG_SEND_EVT_IGNORE
= 1 << 6, /* ignore send event when timer
170 BFD_SESS_FLAG_SHUTDOWN
= 1 << 7, /* disable BGP peer function */
171 BFD_SESS_FLAG_CONFIG
= 1 << 8, /* Session configured with bfd NB API */
172 BFD_SESS_FLAG_CBIT
= 1 << 9, /* CBIT is set */
173 BFD_SESS_FLAG_PASSIVE
= 1 << 10, /* Passive mode */
177 * BFD session hash key.
179 * This structure must not have any padding bytes because their value is
180 * unspecified after the struct assignment. Even when all fields of two keys
181 * are the same, if the padding bytes are different, then the calculated hash
182 * value is different, and the hash lookup will fail.
184 * Currently, the structure fields are correctly aligned, and the "packed"
185 * attribute is added as a precaution. "family" and "mhop" fields are two-bytes
186 * to eliminate unaligned memory access to "peer" and "local".
191 struct in6_addr peer
;
192 struct in6_addr local
;
193 char ifname
[INTERFACE_NAMSIZ
];
194 char vrfname
[VRF_NAMSIZ
];
195 } __attribute__((packed
));
197 struct bfd_session_stats
{
198 uint64_t rx_ctrl_pkt
;
199 uint64_t tx_ctrl_pkt
;
200 uint64_t rx_echo_pkt
;
201 uint64_t tx_echo_pkt
;
203 uint64_t session_down
;
204 uint64_t znotification
;
208 * BFD session profile to override default configurations.
214 /** Session detection multiplier. */
215 uint8_t detection_multiplier
;
216 /** Desired transmission interval (in microseconds). */
218 /** Minimum required receive interval (in microseconds). */
220 /** Administrative state. */
224 /** Minimum expected TTL value. */
227 /** Echo mode (only applies to single hop). */
229 /** Desired echo transmission interval (in microseconds). */
230 uint32_t min_echo_tx
;
231 /** Minimum required echo receive interval (in microseconds). */
232 uint32_t min_echo_rx
;
234 /** Profile list entry. */
235 TAILQ_ENTRY(bfd_profile
) entry
;
238 /** Profile list type. */
239 TAILQ_HEAD(bfdproflist
, bfd_profile
);
241 /* bfd_session shortcut label forwarding. */
244 struct bfd_config_timers
{
245 uint32_t desired_min_tx
;
246 uint32_t required_min_rx
;
247 uint32_t desired_min_echo_tx
;
248 uint32_t required_min_echo_rx
;
252 * Session state information
256 /* protocol state per RFC 5880*/
258 struct bfd_discrs discrs
;
262 uint8_t remote_detect_mult
;
266 /** BFD profile name. */
268 /** BFD pre configured profile. */
269 struct bfd_profile
*profile
;
270 /** BFD peer configuration (without profile). */
271 struct bfd_profile peer_profile
;
274 struct bfd_config_timers timers
;
275 struct bfd_timers cur_timers
;
277 struct thread
*echo_recvtimer_ev
;
278 struct thread
*recvtimer_ev
;
280 uint64_t echo_xmt_TO
;
281 struct thread
*xmttimer_ev
;
282 struct thread
*echo_xmttimer_ev
;
283 uint64_t echo_detect_TO
;
285 /* software object state */
288 /* This and the localDiscr are the keys to state info */
290 struct peer_label
*pl
;
292 struct bfd_dplane_ctx
*bdc
;
293 struct interface
*ifp
;
298 /* BFD session flags */
299 enum bfd_session_flags flags
;
301 struct bfd_session_stats stats
;
303 struct timeval uptime
; /* last up time */
304 struct timeval downtime
; /* last down time */
306 /* Remote peer data (for debugging mostly) */
308 struct bfd_timers remote_timers
;
310 uint64_t refcount
; /* number of pointers referencing this. */
314 TAILQ_ENTRY(peer_label
) pl_entry
;
316 struct bfd_session
*pl_bs
;
317 char pl_label
[MAXNAMELEN
];
319 TAILQ_HEAD(pllist
, peer_label
);
321 struct bfd_diag_str_list
{
326 struct bfd_state_str_list
{
331 struct bfd_session_observer
{
332 struct bfd_session
*bso_bs
;
333 char bso_entryname
[MAXNAMELEN
];
334 struct prefix bso_addr
;
336 TAILQ_ENTRY(bfd_session_observer
) bso_entry
;
338 TAILQ_HEAD(obslist
, bfd_session_observer
);
341 /* States defined per 4.1 */
342 #define PTM_BFD_ADM_DOWN 0
343 #define PTM_BFD_DOWN 1
344 #define PTM_BFD_INIT 2
348 /* Various constants */
349 /* Retrieved from ptm_timer.h from Cumulus PTM sources. */
350 #define BFD_DEF_DEMAND 0
351 #define BFD_DEFDETECTMULT 3
352 #define BFD_DEFDESIREDMINTX (300 * 1000) /* microseconds. */
353 #define BFD_DEFREQUIREDMINRX (300 * 1000) /* microseconds. */
354 #define BFD_DEF_DES_MIN_ECHO_TX (50 * 1000) /* microseconds. */
355 #define BFD_DEF_REQ_MIN_ECHO_RX (50 * 1000) /* microseconds. */
356 #define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
357 /** Minimum multi hop TTL. */
358 #define BFD_DEF_MHOP_TTL 254
359 #define BFD_PKT_LEN 24 /* Length of control packet */
360 #define BFD_TTL_VAL 255
361 #define BFD_RCV_TTL_VAL 1
362 #define BFD_TOS_VAL 0xC0
363 #define BFD_PKT_INFO_VAL 1
364 #define BFD_IPV6_PKT_INFO_VAL 1
365 #define BFD_IPV6_ONLY_VAL 1
366 #define BFD_SRCPORTINIT 49152
367 #define BFD_SRCPORTMAX 65535
368 #define BFD_DEFDESTPORT 3784
369 #define BFD_DEF_ECHO_PORT 3785
370 #define BFD_DEF_MHOP_DEST_PORT 4784
375 * Daemon control code to speak with local consumers.
378 /* See 'bfdctrl.h' for client protocol definitions. */
380 struct bfd_control_buffer
{
384 struct bfd_control_msg
*bcb_bcm
;
389 struct bfd_control_queue
{
390 TAILQ_ENTRY(bfd_control_queue
) bcq_entry
;
392 struct bfd_control_buffer bcq_bcb
;
394 TAILQ_HEAD(bcqueue
, bfd_control_queue
);
396 struct bfd_notify_peer
{
397 TAILQ_ENTRY(bfd_notify_peer
) bnp_entry
;
399 struct bfd_session
*bnp_bs
;
401 TAILQ_HEAD(bnplist
, bfd_notify_peer
);
403 struct bfd_control_socket
{
404 TAILQ_ENTRY(bfd_control_socket
) bcs_entry
;
407 struct thread
*bcs_ev
;
408 struct thread
*bcs_outev
;
409 struct bcqueue bcs_bcqueue
;
411 /* Notification data */
413 struct bnplist bcs_bnplist
;
415 enum bc_msg_version bcs_version
;
416 enum bc_msg_type bcs_type
;
418 /* Message buffering */
419 struct bfd_control_buffer bcs_bin
;
420 struct bfd_control_buffer
*bcs_bout
;
422 TAILQ_HEAD(bcslist
, bfd_control_socket
);
424 int control_init(const char *path
);
425 void control_shutdown(void);
426 int control_notify(struct bfd_session
*bs
, uint8_t notify_state
);
427 int control_notify_config(const char *op
, struct bfd_session
*bs
);
428 void control_accept(struct thread
*t
);
434 * Daemon specific code.
436 struct bfd_vrf_global
{
445 struct thread
*bg_ev
[6];
448 /* Forward declaration of data plane context struct. */
449 struct bfd_dplane_ctx
;
450 TAILQ_HEAD(dplane_queue
, bfd_dplane_ctx
);
454 struct thread
*bg_csockev
;
455 struct bcslist bg_bcslist
;
457 struct pllist bg_pllist
;
459 struct obslist bg_obslist
;
461 struct zebra_privs_t bfdd_privs
;
464 * Daemon is exit()ing? Use this to avoid actions that expect a
465 * running system or to avoid unnecessary operations when quitting.
469 /* Distributed BFD items. */
472 struct thread
*bg_dplane_sockev
;
473 struct dplane_queue bg_dplaneq
;
476 /* Show distributed BFD debug messages. */
478 /* Show all peer state changes events. */
479 bool debug_peer_event
;
481 * Show zebra message exchanges:
482 * - Interface add/delete.
483 * - Local address add/delete.
488 * Show network level debug information:
489 * - Echo packets without session.
490 * - Unavailable peer sessions.
491 * - Network system call failures.
496 extern struct bfd_global bglobal
;
497 extern const struct bfd_diag_str_list diag_list
[];
498 extern const struct bfd_state_str_list state_list
[];
500 void socket_close(int *s
);
506 * Contains the code related with loading/reloading configuration.
508 int parse_config(const char *fname
);
509 int config_request_add(const char *jsonstr
);
510 int config_request_del(const char *jsonstr
);
511 char *config_response(const char *status
, const char *error
);
512 char *config_notify(struct bfd_session
*bs
);
513 char *config_notify_config(const char *op
, struct bfd_session
*bs
);
515 typedef int (*bpc_handle
)(struct bfd_peer_cfg
*, void *arg
);
516 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
519 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
);
520 struct peer_label
*pl_find(const char *label
);
521 void pl_free(struct peer_label
*pl
);
525 * logging - alias to zebra log
527 #define zlog_fatal(msg, ...) \
529 zlog_err(msg, ##__VA_ARGS__); \
538 * Contains the code related with receiving/seding, packing/unpacking BFD data.
540 int bp_set_ttlv6(int sd
, uint8_t value
);
541 int bp_set_ttl(int sd
, uint8_t value
);
542 int bp_set_tosv6(int sd
, uint8_t value
);
543 int bp_set_tos(int sd
, uint8_t value
);
544 int bp_bind_dev(int sd
, const char *dev
);
546 int bp_udp_shop(const struct vrf
*vrf
);
547 int bp_udp_mhop(const struct vrf
*vrf
);
548 int bp_udp6_shop(const struct vrf
*vrf
);
549 int bp_udp6_mhop(const struct vrf
*vrf
);
550 int bp_peer_socket(const struct bfd_session
*bs
);
551 int bp_peer_socketv6(const struct bfd_session
*bs
);
552 int bp_echo_socket(const struct vrf
*vrf
);
553 int bp_echov6_socket(const struct vrf
*vrf
);
555 void ptm_bfd_snd(struct bfd_session
*bfd
, int fbit
);
556 void ptm_bfd_echo_snd(struct bfd_session
*bfd
);
558 void bfd_recv_cb(struct thread
*t
);
564 * Contains the code related with event loop.
566 typedef void (*bfd_ev_cb
)(struct thread
*t
);
568 void bfd_recvtimer_update(struct bfd_session
*bs
);
569 void bfd_echo_recvtimer_update(struct bfd_session
*bs
);
570 void bfd_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
571 void bfd_echo_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
573 void bfd_xmttimer_delete(struct bfd_session
*bs
);
574 void bfd_echo_xmttimer_delete(struct bfd_session
*bs
);
575 void bfd_recvtimer_delete(struct bfd_session
*bs
);
576 void bfd_echo_recvtimer_delete(struct bfd_session
*bs
);
578 void bfd_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
579 void bfd_echo_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
580 void bfd_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
581 void bfd_echo_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
587 * BFD protocol specific code.
589 int bfd_session_enable(struct bfd_session
*bs
);
590 void bfd_session_disable(struct bfd_session
*bs
);
591 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
);
592 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
);
593 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
);
594 void ptm_bfd_sess_up(struct bfd_session
*bfd
);
595 void ptm_bfd_echo_stop(struct bfd_session
*bfd
);
596 void ptm_bfd_echo_start(struct bfd_session
*bfd
);
597 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
);
598 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
);
599 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
600 struct sockaddr_any
*peer
,
601 struct sockaddr_any
*local
,
602 struct interface
*ifp
,
606 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
);
607 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
);
608 void bfd_set_polling(struct bfd_session
*bs
);
609 void bs_state_handler(struct bfd_session
*bs
, int nstate
);
610 void bs_echo_timer_handler(struct bfd_session
*bs
);
611 void bs_final_handler(struct bfd_session
*bs
);
612 void bs_set_slow_timers(struct bfd_session
*bs
);
613 const char *satostr(const struct sockaddr_any
*sa
);
614 const char *diag2str(uint8_t diag
);
615 int strtosa(const char *addr
, struct sockaddr_any
*sa
);
616 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
);
617 const char *bs_to_string(const struct bfd_session
*bs
);
619 int bs_observer_add(struct bfd_session
*bs
);
620 void bs_observer_del(struct bfd_session_observer
*bso
);
622 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
624 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
625 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
626 const char *vrfname
);
627 struct bfd_session
*bfd_session_new(void);
628 struct bfd_session
*bs_registrate(struct bfd_session
*bs
);
629 void bfd_session_free(struct bfd_session
*bs
);
630 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
632 void bfd_sessions_remove_manual(void);
633 void bfd_profiles_remove(void);
636 * Set the BFD session echo state.
638 * \param bs the BFD session.
639 * \param echo the echo operational state.
641 void bfd_set_echo(struct bfd_session
*bs
, bool echo
);
644 * Set the BFD session functional state.
646 * \param bs the BFD session.
647 * \param shutdown the operational value.
649 void bfd_set_shutdown(struct bfd_session
*bs
, bool shutdown
);
652 * Set the BFD session passive mode.
654 * \param bs the BFD session.
655 * \param passive the passive mode.
657 void bfd_set_passive_mode(struct bfd_session
*bs
, bool passive
);
660 * Picks the BFD session configuration from the appropriated source:
661 * if using the default peer configuration prefer profile (if it exists),
662 * otherwise use session.
664 * \param bs the BFD session.
666 void bfd_session_apply(struct bfd_session
*bs
);
668 /* BFD hash data structures interface */
669 void bfd_initialize(void);
670 void bfd_shutdown(void);
671 void bfd_vrf_init(void);
672 void bfd_vrf_terminate(void);
673 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
);
674 struct bfd_session
*bfd_id_lookup(uint32_t id
);
675 struct bfd_session
*bfd_key_lookup(struct bfd_key key
);
677 struct bfd_session
*bfd_id_delete(uint32_t id
);
678 struct bfd_session
*bfd_key_delete(struct bfd_key key
);
680 bool bfd_id_insert(struct bfd_session
*bs
);
681 bool bfd_key_insert(struct bfd_session
*bs
);
683 typedef void (*hash_iter_func
)(struct hash_bucket
*hb
, void *arg
);
684 void bfd_id_iterate(hash_iter_func hif
, void *arg
);
685 void bfd_key_iterate(hash_iter_func hif
, void *arg
);
687 unsigned long bfd_get_session_count(void);
689 /* Export callback functions for `event.c`. */
690 extern struct thread_master
*master
;
692 void bfd_recvtimer_cb(struct thread
*t
);
693 void bfd_echo_recvtimer_cb(struct thread
*t
);
694 void bfd_xmt_cb(struct thread
*t
);
695 void bfd_echo_xmt_cb(struct thread
*t
);
697 extern struct in6_addr zero_addr
;
700 * Creates a new profile entry and insert into the global list.
702 * \param name the BFD profile name.
704 * \returns `NULL` if it already exists otherwise the new entry.
706 struct bfd_profile
*bfd_profile_new(const char *name
);
709 * Search for configured BFD profiles (profile name is case insensitive).
711 * \param name the BFD profile name.
713 * \returns `NULL` if it doesn't exist otherwise the entry.
715 struct bfd_profile
*bfd_profile_lookup(const char *name
);
718 * Removes profile from list and free memory.
720 * \param bp the BFD profile.
722 void bfd_profile_free(struct bfd_profile
*bp
);
725 * Apply a profile configuration to an existing BFD session. The non default
726 * values will not be overridden.
728 * NOTE: if the profile doesn't exist yet, then the profile will be applied
729 * once it begins to exist.
731 * \param profile_name the BFD profile name.
732 * \param bs the BFD session.
734 void bfd_profile_apply(const char *profname
, struct bfd_session
*bs
);
737 * Remove any applied profile from session and revert the session
740 * \param bs the BFD session.
742 void bfd_profile_remove(struct bfd_session
*bs
);
745 * Apply new profile values to sessions using it.
747 * \param[in] bp the BFD profile that got updated.
749 void bfd_profile_update(struct bfd_profile
*bp
);
754 * BFD daemon vty shell commands.
756 void bfdd_vty_init(void);
762 * BFD daemon CLI implementation.
764 void bfdd_cli_init(void);
770 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
);
771 void bfdd_zclient_stop(void);
772 void bfdd_zclient_unregister(vrf_id_t vrf_id
);
773 void bfdd_zclient_register(vrf_id_t vrf_id
);
774 void bfdd_sessions_enable_vrf(struct vrf
*vrf
);
775 void bfdd_sessions_disable_vrf(struct vrf
*vrf
);
777 int ptm_bfd_notify(struct bfd_session
*bs
, uint8_t notify_state
);
784 * Initialize BFD data plane infrastructure for distributed BFD implementation.
786 * \param sa socket address.
787 * \param salen socket address structure length.
788 * \param client `true` means connecting socket, `false` listening socket.
790 void bfd_dplane_init(const struct sockaddr
*sa
, socklen_t salen
, bool client
);
793 * Attempts to delegate the BFD session liveness detection to hardware.
795 * \param bs the BFD session data structure.
798 * `0` on success and BFD daemon should do nothing or `-1` on failure
799 * and we should fallback to software implementation.
801 int bfd_dplane_add_session(struct bfd_session
*bs
);
804 * Send new session settings to data plane.
806 * \param bs the BFD session to update.
808 int bfd_dplane_update_session(const struct bfd_session
*bs
);
811 * Deletes session from data plane.
813 * \param bs the BFD session to delete.
815 * \returns `0` on success otherwise `-1`.
817 int bfd_dplane_delete_session(struct bfd_session
*bs
);
820 * Asks the data plane for updated counters and update the session data
823 * \param bs the BFD session that needs updating.
825 * \returns `0` on success otherwise `-1` on failure.
827 int bfd_dplane_update_session_counters(struct bfd_session
*bs
);
829 void bfd_dplane_show_counters(struct vty
*vty
);