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 along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 DEFINE_MTYPE_STATIC(LIB
, BFD_INFO
, "BFD info")
38 struct bfd_gbl bfd_gbl
;
41 * bfd_gbl_init - Initialize the BFD global structure
46 memset(&bfd_gbl
, 0, sizeof (struct bfd_gbl
));
50 * bfd_gbl_exit - Called when daemon exits
55 SET_FLAG (bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
);
59 * bfd_info_create - Allocate the BFD information
64 struct bfd_info
*bfd_info
;
66 bfd_info
= XCALLOC (MTYPE_BFD_INFO
, sizeof (struct bfd_info
));
69 bfd_info
->status
= BFD_STATUS_UNKNOWN
;
70 bfd_info
->type
= BFD_TYPE_NOT_CONFIGURED
;
71 bfd_info
->last_update
= 0;
76 * bfd_info_free - Free the BFD information.
79 bfd_info_free(struct bfd_info
**bfd_info
)
83 XFREE (MTYPE_BFD_INFO
, *bfd_info
);
89 * bfd_validate_param - Validate the BFD paramter information.
92 bfd_validate_param(struct vty
*vty
, const char *dm_str
, const char *rx_str
,
93 const char *tx_str
, u_int8_t
*dm_val
, u_int32_t
*rx_val
,
96 VTY_GET_INTEGER_RANGE ("detect-mul", *dm_val
, dm_str
,
97 BFD_MIN_DETECT_MULT
, BFD_MAX_DETECT_MULT
);
98 VTY_GET_INTEGER_RANGE ("min-rx", *rx_val
, rx_str
,
99 BFD_MIN_MIN_RX
, BFD_MAX_MIN_RX
);
100 VTY_GET_INTEGER_RANGE ("min-tx", *tx_val
, tx_str
,
101 BFD_MIN_MIN_TX
, BFD_MAX_MIN_TX
);
106 * bfd_set_param - Set the configured BFD paramter values
109 bfd_set_param (struct bfd_info
**bfd_info
, u_int32_t min_rx
, u_int32_t min_tx
,
110 u_int8_t detect_mult
, int defaults
, int *command
)
114 *bfd_info
= bfd_info_create();
115 *command
= ZEBRA_BFD_DEST_REGISTER
;
119 if (((*bfd_info
)->required_min_rx
!= min_rx
) ||
120 ((*bfd_info
)->desired_min_tx
!= min_tx
) ||
121 ((*bfd_info
)->detect_mult
!= detect_mult
))
122 *command
= ZEBRA_BFD_DEST_UPDATE
;
127 (*bfd_info
)->required_min_rx
= min_rx
;
128 (*bfd_info
)->desired_min_tx
= min_tx
;
129 (*bfd_info
)->detect_mult
= detect_mult
;
133 SET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
135 UNSET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
139 * bfd_peer_sendmsg - Format and send a peer register/Unregister
140 * command to Zebra to be forwarded to BFD
143 bfd_peer_sendmsg (struct zclient
*zclient
, struct bfd_info
*bfd_info
,
144 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
145 int ttl
, int multihop
, int command
, int set_flag
,
152 /* Individual reg/dereg messages are supressed during shutdown. */
153 if (CHECK_FLAG (bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
))
156 zlog_debug("%s: Suppressing BFD peer reg/dereg messages", __FUNCTION__
);
161 if (!zclient
|| zclient
->sock
< 0)
164 zlog_debug("%s: Can't send BFD peer register, Zebra client not "
165 "established", __FUNCTION__
);
171 zclient_create_header (s
, command
, vrf_id
);
173 stream_putl(s
, getpid());
175 stream_putw(s
, family
);
179 stream_put_in_addr (s
, (struct in_addr
*)dst_ip
);
182 stream_put(s
, dst_ip
, 16);
188 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
190 stream_putl(s
, bfd_info
->required_min_rx
);
191 stream_putl(s
, bfd_info
->desired_min_tx
);
192 stream_putc(s
, bfd_info
->detect_mult
);
198 /* Multi-hop destination send the source IP address to BFD */
201 stream_putw(s
, family
);
205 stream_put_in_addr (s
, (struct in_addr
*) src_ip
);
208 stream_put(s
, src_ip
, 16);
219 if ((family
== AF_INET6
) && (src_ip
))
221 stream_putw(s
, family
);
222 stream_put(s
, src_ip
, 16);
226 len
= strlen(if_name
);
228 stream_put(s
, if_name
, len
);
236 stream_putw_at (s
, 0, stream_get_endp (s
));
238 ret
= zclient_send_message(zclient
);
243 zlog_debug("bfd_peer_sendmsg: zclient_send_message() failed");
249 if (command
== ZEBRA_BFD_DEST_REGISTER
)
250 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
251 else if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
252 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
259 * bfd_get_command_dbg_str - Convert command to a debug string.
262 bfd_get_command_dbg_str(int command
)
266 case ZEBRA_BFD_DEST_REGISTER
:
268 case ZEBRA_BFD_DEST_DEREGISTER
:
270 case ZEBRA_BFD_DEST_UPDATE
:
278 * bfd_get_peer_info - Extract the Peer information for which the BFD session
279 * went down from the message sent from Zebra to clients.
282 bfd_get_peer_info (struct stream
*s
, struct prefix
*dp
, struct prefix
*sp
,
283 int *status
, vrf_id_t vrf_id
)
285 unsigned int ifindex
;
286 struct interface
*ifp
= NULL
;
289 /* Get interface index. */
290 ifindex
= stream_getl (s
);
295 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
299 zlog_debug ("zebra_interface_bfd_read: "
300 "Can't find interface by ifindex: %d ", ifindex
);
305 /* Fetch destination address. */
306 dp
->family
= stream_getc (s
);
308 plen
= prefix_blen (dp
);
309 stream_get (&dp
->u
.prefix
, s
, plen
);
310 dp
->prefixlen
= stream_getc (s
);
312 /* Get BFD status. */
313 *status
= stream_getl (s
);
317 sp
->family
= stream_getc (s
);
319 plen
= prefix_blen (sp
);
320 stream_get (&sp
->u
.prefix
, s
, plen
);
321 sp
->prefixlen
= stream_getc (s
);
327 * bfd_get_status_str - Convert BFD status to a display string.
330 bfd_get_status_str(int status
)
334 case BFD_STATUS_DOWN
:
338 case BFD_STATUS_UNKNOWN
:
345 * bfd_last_update - Calculate the last BFD update time and convert it
346 * into a dd:hh:mm:ss display format.
349 bfd_last_update (time_t last_update
, char *buf
, size_t len
)
356 /* If no BFD satatus update has ever been received, print `never'. */
357 if (last_update
== 0)
359 snprintf (buf
, len
, "never");
363 /* Get current time. */
366 diff
= curr
- last_update
;
369 snprintf (buf
, len
, "%d:%02d:%02d:%02d",
370 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
374 * bfd_show_param - Show the BFD parameter information.
377 bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
378 int extra_space
, u_char use_json
, json_object
*json_obj
)
380 json_object
*json_bfd
= NULL
;
388 json_bfd
= json_object_new_object();
392 json_object_int_add(json_bfd
, "detectMultiplier", bfd_info
->detect_mult
);
393 json_object_int_add(json_bfd
, "rxMinInterval", bfd_info
->required_min_rx
);
394 json_object_int_add(json_bfd
, "txMinInterval", bfd_info
->desired_min_tx
);
396 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
400 vty_out (vty
, " %s%sDetect Mul: %d, Min Rx interval: %d,"
401 " Min Tx interval: %d%s",
402 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
403 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
404 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
409 * bfd_show_status - Show the BFD status information.
412 bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
413 int extra_space
, u_char use_json
, json_object
*json_bfd
)
420 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
423 json_object_string_add(json_bfd
, "status",
424 bfd_get_status_str(bfd_info
->status
));
425 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
429 vty_out (vty
, " %s%sStatus: %s, Last update: %s%s",
430 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
431 bfd_get_status_str(bfd_info
->status
), time_buf
, VTY_NEWLINE
);
436 * bfd_show_info - Show the BFD information.
439 bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
440 int extra_space
, u_char use_json
, json_object
*json_obj
)
442 json_object
*json_bfd
= NULL
;
449 json_bfd
= json_object_new_object();
451 json_object_string_add(json_bfd
, "type", "multi hop");
453 json_object_string_add(json_bfd
, "type", "single hop");
457 vty_out (vty
, " %sBFD: Type: %s%s", (extra_space
) ? " " : "",
458 (multihop
) ? "multi hop" : "single hop", VTY_NEWLINE
);
461 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
462 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
465 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
467 vty_out (vty
, "%s", VTY_NEWLINE
);
471 * bfd_client_sendmsg - Format and send a client register
472 * command to Zebra to be forwarded to BFD
475 bfd_client_sendmsg (struct zclient
*zclient
, int command
)
481 if (!zclient
|| zclient
->sock
< 0)
484 zlog_debug ("%s: Can't send BFD client register, Zebra client not "
485 "established", __FUNCTION__
);
491 zclient_create_header (s
, command
, VRF_DEFAULT
);
493 stream_putl(s
, getpid());
495 stream_putw_at (s
, 0, stream_get_endp (s
));
497 ret
= zclient_send_message(zclient
);
502 zlog_debug ("bfd_client_sendmsg %ld: zclient_send_message() failed",