2 * bfd.c: BFD handling routines
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
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 DEFINE_MTYPE_STATIC(LIB
, BFD_INFO
, "BFD info")
39 struct bfd_gbl bfd_gbl
;
42 * bfd_gbl_init - Initialize the BFD global structure
47 memset(&bfd_gbl
, 0, sizeof (struct bfd_gbl
));
51 * bfd_gbl_exit - Called when daemon exits
56 SET_FLAG (bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
);
60 * bfd_info_create - Allocate the BFD information
65 struct bfd_info
*bfd_info
;
67 bfd_info
= XCALLOC (MTYPE_BFD_INFO
, sizeof (struct bfd_info
));
70 bfd_info
->status
= BFD_STATUS_UNKNOWN
;
71 bfd_info
->type
= BFD_TYPE_NOT_CONFIGURED
;
72 bfd_info
->last_update
= 0;
77 * bfd_info_free - Free the BFD information.
80 bfd_info_free(struct bfd_info
**bfd_info
)
84 XFREE (MTYPE_BFD_INFO
, *bfd_info
);
90 * bfd_validate_param - Validate the BFD paramter information.
93 bfd_validate_param(struct vty
*vty
, const char *dm_str
, const char *rx_str
,
94 const char *tx_str
, u_int8_t
*dm_val
, u_int32_t
*rx_val
,
97 VTY_GET_INTEGER_RANGE ("detect-mul", *dm_val
, dm_str
,
98 BFD_MIN_DETECT_MULT
, BFD_MAX_DETECT_MULT
);
99 VTY_GET_INTEGER_RANGE ("min-rx", *rx_val
, rx_str
,
100 BFD_MIN_MIN_RX
, BFD_MAX_MIN_RX
);
101 VTY_GET_INTEGER_RANGE ("min-tx", *tx_val
, tx_str
,
102 BFD_MIN_MIN_TX
, BFD_MAX_MIN_TX
);
107 * bfd_set_param - Set the configured BFD paramter values
110 bfd_set_param (struct bfd_info
**bfd_info
, u_int32_t min_rx
, u_int32_t min_tx
,
111 u_int8_t detect_mult
, int defaults
, int *command
)
115 *bfd_info
= bfd_info_create();
116 *command
= ZEBRA_BFD_DEST_REGISTER
;
120 if (((*bfd_info
)->required_min_rx
!= min_rx
) ||
121 ((*bfd_info
)->desired_min_tx
!= min_tx
) ||
122 ((*bfd_info
)->detect_mult
!= detect_mult
))
123 *command
= ZEBRA_BFD_DEST_UPDATE
;
128 (*bfd_info
)->required_min_rx
= min_rx
;
129 (*bfd_info
)->desired_min_tx
= min_tx
;
130 (*bfd_info
)->detect_mult
= detect_mult
;
134 SET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
136 UNSET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
140 * bfd_peer_sendmsg - Format and send a peer register/Unregister
141 * command to Zebra to be forwarded to BFD
144 bfd_peer_sendmsg (struct zclient
*zclient
, struct bfd_info
*bfd_info
,
145 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
146 int ttl
, int multihop
, int command
, int set_flag
,
153 /* Individual reg/dereg messages are supressed during shutdown. */
154 if (CHECK_FLAG (bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
))
157 zlog_debug("%s: Suppressing BFD peer reg/dereg messages", __FUNCTION__
);
162 if (!zclient
|| zclient
->sock
< 0)
165 zlog_debug("%s: Can't send BFD peer register, Zebra client not "
166 "established", __FUNCTION__
);
172 zclient_create_header (s
, command
, vrf_id
);
174 stream_putl(s
, getpid());
176 stream_putw(s
, family
);
180 stream_put_in_addr (s
, (struct in_addr
*)dst_ip
);
184 stream_put(s
, dst_ip
, 16);
191 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
193 stream_putl(s
, bfd_info
->required_min_rx
);
194 stream_putl(s
, bfd_info
->desired_min_tx
);
195 stream_putc(s
, bfd_info
->detect_mult
);
201 /* Multi-hop destination send the source IP address to BFD */
204 stream_putw(s
, family
);
208 stream_put_in_addr (s
, (struct in_addr
*) src_ip
);
212 stream_put(s
, src_ip
, 16);
225 if ((family
== AF_INET6
) && (src_ip
))
227 stream_putw(s
, family
);
228 stream_put(s
, src_ip
, 16);
233 len
= strlen(if_name
);
235 stream_put(s
, if_name
, len
);
243 stream_putw_at (s
, 0, stream_get_endp (s
));
245 ret
= zclient_send_message(zclient
);
250 zlog_debug("bfd_peer_sendmsg: zclient_send_message() failed");
256 if (command
== ZEBRA_BFD_DEST_REGISTER
)
257 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
258 else if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
259 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
266 * bfd_get_command_dbg_str - Convert command to a debug string.
269 bfd_get_command_dbg_str(int command
)
273 case ZEBRA_BFD_DEST_REGISTER
:
275 case ZEBRA_BFD_DEST_DEREGISTER
:
277 case ZEBRA_BFD_DEST_UPDATE
:
285 * bfd_get_peer_info - Extract the Peer information for which the BFD session
286 * went down from the message sent from Zebra to clients.
289 bfd_get_peer_info (struct stream
*s
, struct prefix
*dp
, struct prefix
*sp
,
290 int *status
, vrf_id_t vrf_id
)
292 unsigned int ifindex
;
293 struct interface
*ifp
= NULL
;
296 /* Get interface index. */
297 ifindex
= stream_getl (s
);
302 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
306 zlog_debug ("zebra_interface_bfd_read: "
307 "Can't find interface by ifindex: %d ", ifindex
);
312 /* Fetch destination address. */
313 dp
->family
= stream_getc (s
);
315 plen
= prefix_blen (dp
);
316 stream_get (&dp
->u
.prefix
, s
, plen
);
317 dp
->prefixlen
= stream_getc (s
);
319 /* Get BFD status. */
320 *status
= stream_getl (s
);
324 sp
->family
= stream_getc (s
);
326 plen
= prefix_blen (sp
);
327 stream_get (&sp
->u
.prefix
, s
, plen
);
328 sp
->prefixlen
= stream_getc (s
);
334 * bfd_get_status_str - Convert BFD status to a display string.
337 bfd_get_status_str(int status
)
341 case BFD_STATUS_DOWN
:
345 case BFD_STATUS_UNKNOWN
:
352 * bfd_last_update - Calculate the last BFD update time and convert it
353 * into a dd:hh:mm:ss display format.
356 bfd_last_update (time_t last_update
, char *buf
, size_t len
)
363 /* If no BFD satatus update has ever been received, print `never'. */
364 if (last_update
== 0)
366 snprintf (buf
, len
, "never");
370 /* Get current time. */
371 quagga_gettime(QUAGGA_CLK_MONOTONIC
, &tv
);
373 diff
= curr
- last_update
;
376 snprintf (buf
, len
, "%d:%02d:%02d:%02d",
377 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
381 * bfd_show_param - Show the BFD parameter information.
384 bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
385 int extra_space
, u_char use_json
, json_object
*json_obj
)
387 json_object
*json_bfd
= NULL
;
395 json_bfd
= json_object_new_object();
399 json_object_int_add(json_bfd
, "detectMultiplier", bfd_info
->detect_mult
);
400 json_object_int_add(json_bfd
, "rxMinInterval", bfd_info
->required_min_rx
);
401 json_object_int_add(json_bfd
, "txMinInterval", bfd_info
->desired_min_tx
);
403 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
407 vty_out (vty
, " %s%sDetect Mul: %d, Min Rx interval: %d,"
408 " Min Tx interval: %d%s",
409 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
410 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
411 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
416 * bfd_show_status - Show the BFD status information.
419 bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
420 int extra_space
, u_char use_json
, json_object
*json_bfd
)
427 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
430 json_object_string_add(json_bfd
, "status",
431 bfd_get_status_str(bfd_info
->status
));
432 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
436 vty_out (vty
, " %s%sStatus: %s, Last update: %s%s",
437 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
438 bfd_get_status_str(bfd_info
->status
), time_buf
, VTY_NEWLINE
);
443 * bfd_show_info - Show the BFD information.
446 bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
447 int extra_space
, u_char use_json
, json_object
*json_obj
)
449 json_object
*json_bfd
= NULL
;
456 json_bfd
= json_object_new_object();
458 json_object_string_add(json_bfd
, "type", "multi hop");
460 json_object_string_add(json_bfd
, "type", "single hop");
464 vty_out (vty
, " %sBFD: Type: %s%s", (extra_space
) ? " " : "",
465 (multihop
) ? "multi hop" : "single hop", VTY_NEWLINE
);
468 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
469 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
472 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
474 vty_out (vty
, "%s", VTY_NEWLINE
);
478 * bfd_client_sendmsg - Format and send a client register
479 * command to Zebra to be forwarded to BFD
482 bfd_client_sendmsg (struct zclient
*zclient
, int command
)
488 if (!zclient
|| zclient
->sock
< 0)
491 zlog_debug ("%s: Can't send BFD client register, Zebra client not "
492 "established", __FUNCTION__
);
498 zclient_create_header (s
, command
, VRF_DEFAULT
);
500 stream_putl(s
, getpid());
502 stream_putw_at (s
, 0, stream_get_endp (s
));
504 ret
= zclient_send_message(zclient
);
509 zlog_debug ("bfd_client_sendmsg %ld: zclient_send_message() failed",