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 */
175 #define BFD_SET_FLAG(field, flag) (field |= flag)
176 #define BFD_UNSET_FLAG(field, flag) (field &= ~flag)
177 #define BFD_CHECK_FLAG(field, flag) (field & flag)
179 /* BFD session hash keys */
183 struct in6_addr peer
;
184 struct in6_addr local
;
185 char ifname
[MAXNAMELEN
];
186 char vrfname
[MAXNAMELEN
];
189 struct bfd_session_stats
{
190 uint64_t rx_ctrl_pkt
;
191 uint64_t tx_ctrl_pkt
;
192 uint64_t rx_echo_pkt
;
193 uint64_t tx_echo_pkt
;
195 uint64_t session_down
;
196 uint64_t znotification
;
199 /* bfd_session shortcut label forwarding. */
203 * Session state information
207 /* protocol state per RFC 5880*/
209 struct bfd_discrs discrs
;
213 uint8_t remote_detect_mult
;
218 struct bfd_timers timers
;
219 struct bfd_timers cur_timers
;
221 struct thread
*echo_recvtimer_ev
;
222 struct thread
*recvtimer_ev
;
224 uint64_t echo_xmt_TO
;
225 struct thread
*xmttimer_ev
;
226 struct thread
*echo_xmttimer_ev
;
227 uint64_t echo_detect_TO
;
229 /* software object state */
232 /* This and the localDiscr are the keys to state info */
234 struct peer_label
*pl
;
236 struct sockaddr_any local_address
;
237 struct interface
*ifp
;
242 /* BFD session flags */
243 enum bfd_session_flags flags
;
245 struct bfd_session_stats stats
;
247 struct timeval uptime
; /* last up time */
248 struct timeval downtime
; /* last down time */
250 /* Remote peer data (for debugging mostly) */
252 struct bfd_timers remote_timers
;
254 uint64_t refcount
; /* number of pointers referencing this. */
258 TAILQ_ENTRY(peer_label
) pl_entry
;
260 struct bfd_session
*pl_bs
;
261 char pl_label
[MAXNAMELEN
];
263 TAILQ_HEAD(pllist
, peer_label
);
265 struct bfd_diag_str_list
{
270 struct bfd_state_str_list
{
275 struct bfd_session_observer
{
276 struct bfd_session
*bso_bs
;
277 char bso_entryname
[MAXNAMELEN
];
278 struct prefix bso_addr
;
280 TAILQ_ENTRY(bfd_session_observer
) bso_entry
;
282 TAILQ_HEAD(obslist
, bfd_session_observer
);
285 /* States defined per 4.1 */
286 #define PTM_BFD_ADM_DOWN 0
287 #define PTM_BFD_DOWN 1
288 #define PTM_BFD_INIT 2
292 /* Various constants */
293 /* Retrieved from ptm_timer.h from Cumulus PTM sources. */
294 #define BFD_DEF_DEMAND 0
295 #define BFD_DEFDETECTMULT 3
296 #define BFD_DEFDESIREDMINTX (300 * 1000) /* microseconds. */
297 #define BFD_DEFREQUIREDMINRX (300 * 1000) /* microseconds. */
298 #define BFD_DEF_REQ_MIN_ECHO (50 * 1000) /* microseconds. */
299 #define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
300 #define BFD_DEF_MHOP_TTL 5
301 #define BFD_PKT_LEN 24 /* Length of control packet */
302 #define BFD_TTL_VAL 255
303 #define BFD_RCV_TTL_VAL 1
304 #define BFD_TOS_VAL 0xC0
305 #define BFD_PKT_INFO_VAL 1
306 #define BFD_IPV6_PKT_INFO_VAL 1
307 #define BFD_IPV6_ONLY_VAL 1
308 #define BFD_SRCPORTINIT 49152
309 #define BFD_SRCPORTMAX 65535
310 #define BFD_DEFDESTPORT 3784
311 #define BFD_DEF_ECHO_PORT 3785
312 #define BFD_DEF_MHOP_DEST_PORT 4784
317 * Daemon control code to speak with local consumers.
320 /* See 'bfdctrl.h' for client protocol definitions. */
322 struct bfd_control_buffer
{
326 struct bfd_control_msg
*bcb_bcm
;
331 struct bfd_control_queue
{
332 TAILQ_ENTRY(bfd_control_queue
) bcq_entry
;
334 struct bfd_control_buffer bcq_bcb
;
336 TAILQ_HEAD(bcqueue
, bfd_control_queue
);
338 struct bfd_notify_peer
{
339 TAILQ_ENTRY(bfd_notify_peer
) bnp_entry
;
341 struct bfd_session
*bnp_bs
;
343 TAILQ_HEAD(bnplist
, bfd_notify_peer
);
345 struct bfd_control_socket
{
346 TAILQ_ENTRY(bfd_control_socket
) bcs_entry
;
349 struct thread
*bcs_ev
;
350 struct thread
*bcs_outev
;
351 struct bcqueue bcs_bcqueue
;
353 /* Notification data */
355 struct bnplist bcs_bnplist
;
357 enum bc_msg_version bcs_version
;
358 enum bc_msg_type bcs_type
;
360 /* Message buffering */
361 struct bfd_control_buffer bcs_bin
;
362 struct bfd_control_buffer
*bcs_bout
;
364 TAILQ_HEAD(bcslist
, bfd_control_socket
);
366 int control_init(const char *path
);
367 void control_shutdown(void);
368 int control_notify(struct bfd_session
*bs
, uint8_t notify_state
);
369 int control_notify_config(const char *op
, struct bfd_session
*bs
);
370 int control_accept(struct thread
*t
);
376 * Daemon specific code.
378 struct bfd_vrf_global
{
387 struct thread
*bg_ev
[6];
392 struct thread
*bg_csockev
;
393 struct bcslist bg_bcslist
;
395 struct pllist bg_pllist
;
397 struct obslist bg_obslist
;
399 struct zebra_privs_t bfdd_privs
;
401 extern struct bfd_global bglobal
;
402 extern const struct bfd_diag_str_list diag_list
[];
403 extern const struct bfd_state_str_list state_list
[];
405 void socket_close(int *s
);
411 * Contains the code related with loading/reloading configuration.
413 int parse_config(const char *fname
);
414 int config_request_add(const char *jsonstr
);
415 int config_request_del(const char *jsonstr
);
416 char *config_response(const char *status
, const char *error
);
417 char *config_notify(struct bfd_session
*bs
);
418 char *config_notify_config(const char *op
, struct bfd_session
*bs
);
420 typedef int (*bpc_handle
)(struct bfd_peer_cfg
*, void *arg
);
421 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
424 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
);
425 struct peer_label
*pl_find(const char *label
);
426 void pl_free(struct peer_label
*pl
);
430 * logging - alias to zebra log
433 #define log_debug zlog_debug
434 #define log_info zlog_info
435 #define log_warning zlog_warn
436 #define log_error zlog_err
438 #define log_fatal(msg, ...) \
440 zlog_err(msg, ## __VA_ARGS__); \
449 * Contains the code related with receiving/seding, packing/unpacking BFD data.
451 int bp_set_ttlv6(int sd
, uint8_t value
);
452 int bp_set_ttl(int sd
, uint8_t value
);
453 int bp_set_tosv6(int sd
, uint8_t value
);
454 int bp_set_tos(int sd
, uint8_t value
);
455 int bp_bind_dev(int sd
, const char *dev
);
457 int bp_udp_shop(const struct vrf
*vrf
);
458 int bp_udp_mhop(const struct vrf
*vrf
);
459 int bp_udp6_shop(const struct vrf
*vrf
);
460 int bp_udp6_mhop(const struct vrf
*vrf
);
461 int bp_peer_socket(const struct bfd_session
*bs
);
462 int bp_peer_socketv6(const struct bfd_session
*bs
);
463 int bp_echo_socket(const struct vrf
*vrf
);
464 int bp_echov6_socket(const struct vrf
*vrf
);
466 void ptm_bfd_snd(struct bfd_session
*bfd
, int fbit
);
467 void ptm_bfd_echo_snd(struct bfd_session
*bfd
);
469 int bfd_recv_cb(struct thread
*t
);
475 * Contains the code related with event loop.
477 typedef void (*bfd_ev_cb
)(struct thread
*t
);
479 void bfd_recvtimer_update(struct bfd_session
*bs
);
480 void bfd_echo_recvtimer_update(struct bfd_session
*bs
);
481 void bfd_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
482 void bfd_echo_xmttimer_update(struct bfd_session
*bs
, uint64_t jitter
);
484 void bfd_xmttimer_delete(struct bfd_session
*bs
);
485 void bfd_echo_xmttimer_delete(struct bfd_session
*bs
);
486 void bfd_recvtimer_delete(struct bfd_session
*bs
);
487 void bfd_echo_recvtimer_delete(struct bfd_session
*bs
);
489 void bfd_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
490 void bfd_echo_recvtimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
, int sd
);
491 void bfd_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
492 void bfd_echo_xmttimer_assign(struct bfd_session
*bs
, bfd_ev_cb cb
);
498 * BFD protocol specific code.
500 int bfd_session_enable(struct bfd_session
*bs
);
501 void bfd_session_disable(struct bfd_session
*bs
);
502 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
);
503 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
);
504 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
);
505 void ptm_bfd_sess_up(struct bfd_session
*bfd
);
506 void ptm_bfd_echo_stop(struct bfd_session
*bfd
);
507 void ptm_bfd_echo_start(struct bfd_session
*bfd
);
508 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
);
509 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
);
510 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
511 struct sockaddr_any
*peer
,
512 struct sockaddr_any
*local
,
513 ifindex_t ifindex
, vrf_id_t vrfid
,
516 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
);
517 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
);
518 void bfd_set_polling(struct bfd_session
*bs
);
519 void bs_state_handler(struct bfd_session
*bs
, int nstate
);
520 void bs_echo_timer_handler(struct bfd_session
*bs
);
521 void bs_final_handler(struct bfd_session
*bs
);
522 void bs_set_slow_timers(struct bfd_session
*bs
);
523 const char *satostr(struct sockaddr_any
*sa
);
524 const char *diag2str(uint8_t diag
);
525 int strtosa(const char *addr
, struct sockaddr_any
*sa
);
526 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
);
527 const char *bs_to_string(const struct bfd_session
*bs
);
529 int bs_observer_add(struct bfd_session
*bs
);
530 void bs_observer_del(struct bfd_session_observer
*bso
);
532 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
534 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
535 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
536 const char *vrfname
);
537 struct bfd_session
*bfd_session_new(void);
538 struct bfd_session
*bs_registrate(struct bfd_session
*bs
);
539 void bfd_session_free(struct bfd_session
*bs
);
540 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
542 void bfd_sessions_remove_manual(void);
544 /* BFD hash data structures interface */
545 void bfd_initialize(void);
546 void bfd_shutdown(void);
547 void bfd_vrf_init(void);
548 void bfd_vrf_terminate(void);
549 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
);
550 struct bfd_session
*bfd_id_lookup(uint32_t id
);
551 struct bfd_session
*bfd_key_lookup(struct bfd_key key
);
553 struct bfd_session
*bfd_id_delete(uint32_t id
);
554 struct bfd_session
*bfd_key_delete(struct bfd_key key
);
556 bool bfd_id_insert(struct bfd_session
*bs
);
557 bool bfd_key_insert(struct bfd_session
*bs
);
559 typedef void (*hash_iter_func
)(struct hash_bucket
*hb
, void *arg
);
560 void bfd_id_iterate(hash_iter_func hif
, void *arg
);
561 void bfd_key_iterate(hash_iter_func hif
, void *arg
);
563 unsigned long bfd_get_session_count(void);
565 /* Export callback functions for `event.c`. */
566 extern struct thread_master
*master
;
568 int bfd_recvtimer_cb(struct thread
*t
);
569 int bfd_echo_recvtimer_cb(struct thread
*t
);
570 int bfd_xmt_cb(struct thread
*t
);
571 int bfd_echo_xmt_cb(struct thread
*t
);
573 extern struct in6_addr zero_addr
;
579 * BFD daemon vty shell commands.
581 void bfdd_vty_init(void);
587 * BFD daemon CLI implementation.
589 void bfdd_cli_init(void);
595 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
);
596 void bfdd_zclient_stop(void);
597 void bfdd_zclient_unregister(vrf_id_t vrf_id
);
598 void bfdd_zclient_register(vrf_id_t vrf_id
);
599 void bfdd_sessions_enable_vrf(struct vrf
*vrf
);
600 void bfdd_sessions_disable_vrf(struct vrf
*vrf
);
601 void bfd_session_update_vrf_name(struct bfd_session
*bs
, struct vrf
*vrf
);
603 int ptm_bfd_notify(struct bfd_session
*bs
, uint8_t notify_state
);