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 *dm_val
= strtoul(dm_str
, NULL
, 10);
97 *rx_val
= strtoul(rx_str
, NULL
, 10);
98 *tx_val
= strtoul(tx_str
, NULL
, 10);
103 * bfd_set_param - Set the configured BFD paramter values
106 bfd_set_param (struct bfd_info
**bfd_info
, u_int32_t min_rx
, u_int32_t min_tx
,
107 u_int8_t detect_mult
, int defaults
, int *command
)
111 *bfd_info
= bfd_info_create();
112 *command
= ZEBRA_BFD_DEST_REGISTER
;
116 if (((*bfd_info
)->required_min_rx
!= min_rx
) ||
117 ((*bfd_info
)->desired_min_tx
!= min_tx
) ||
118 ((*bfd_info
)->detect_mult
!= detect_mult
))
119 *command
= ZEBRA_BFD_DEST_UPDATE
;
124 (*bfd_info
)->required_min_rx
= min_rx
;
125 (*bfd_info
)->desired_min_tx
= min_tx
;
126 (*bfd_info
)->detect_mult
= detect_mult
;
130 SET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
132 UNSET_FLAG ((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
136 * bfd_peer_sendmsg - Format and send a peer register/Unregister
137 * command to Zebra to be forwarded to BFD
140 bfd_peer_sendmsg (struct zclient
*zclient
, struct bfd_info
*bfd_info
,
141 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
142 int ttl
, int multihop
, int command
, int set_flag
,
149 /* Individual reg/dereg messages are supressed during shutdown. */
150 if (CHECK_FLAG (bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
))
153 zlog_debug("%s: Suppressing BFD peer reg/dereg messages", __FUNCTION__
);
158 if (!zclient
|| zclient
->sock
< 0)
161 zlog_debug("%s: Can't send BFD peer register, Zebra client not "
162 "established", __FUNCTION__
);
168 zclient_create_header (s
, command
, vrf_id
);
170 stream_putl(s
, getpid());
172 stream_putw(s
, family
);
176 stream_put_in_addr (s
, (struct in_addr
*)dst_ip
);
179 stream_put(s
, dst_ip
, 16);
185 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
187 stream_putl(s
, bfd_info
->required_min_rx
);
188 stream_putl(s
, bfd_info
->desired_min_tx
);
189 stream_putc(s
, bfd_info
->detect_mult
);
195 /* Multi-hop destination send the source IP address to BFD */
198 stream_putw(s
, family
);
202 stream_put_in_addr (s
, (struct in_addr
*) src_ip
);
205 stream_put(s
, src_ip
, 16);
216 if ((family
== AF_INET6
) && (src_ip
))
218 stream_putw(s
, family
);
219 stream_put(s
, src_ip
, 16);
223 len
= strlen(if_name
);
225 stream_put(s
, if_name
, len
);
233 stream_putw_at (s
, 0, stream_get_endp (s
));
235 ret
= zclient_send_message(zclient
);
240 zlog_debug("bfd_peer_sendmsg: zclient_send_message() failed");
246 if (command
== ZEBRA_BFD_DEST_REGISTER
)
247 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
248 else if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
249 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
256 * bfd_get_command_dbg_str - Convert command to a debug string.
259 bfd_get_command_dbg_str(int command
)
263 case ZEBRA_BFD_DEST_REGISTER
:
265 case ZEBRA_BFD_DEST_DEREGISTER
:
267 case ZEBRA_BFD_DEST_UPDATE
:
275 * bfd_get_peer_info - Extract the Peer information for which the BFD session
276 * went down from the message sent from Zebra to clients.
279 bfd_get_peer_info (struct stream
*s
, struct prefix
*dp
, struct prefix
*sp
,
280 int *status
, vrf_id_t vrf_id
)
282 unsigned int ifindex
;
283 struct interface
*ifp
= NULL
;
286 /* Get interface index. */
287 ifindex
= stream_getl (s
);
292 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
296 zlog_debug ("zebra_interface_bfd_read: "
297 "Can't find interface by ifindex: %d ", ifindex
);
302 /* Fetch destination address. */
303 dp
->family
= stream_getc (s
);
305 plen
= prefix_blen (dp
);
306 stream_get (&dp
->u
.prefix
, s
, plen
);
307 dp
->prefixlen
= stream_getc (s
);
309 /* Get BFD status. */
310 *status
= stream_getl (s
);
314 sp
->family
= stream_getc (s
);
316 plen
= prefix_blen (sp
);
317 stream_get (&sp
->u
.prefix
, s
, plen
);
318 sp
->prefixlen
= stream_getc (s
);
324 * bfd_get_status_str - Convert BFD status to a display string.
327 bfd_get_status_str(int status
)
331 case BFD_STATUS_DOWN
:
335 case BFD_STATUS_UNKNOWN
:
342 * bfd_last_update - Calculate the last BFD update time and convert it
343 * into a dd:hh:mm:ss display format.
346 bfd_last_update (time_t last_update
, char *buf
, size_t len
)
353 /* If no BFD satatus update has ever been received, print `never'. */
354 if (last_update
== 0)
356 snprintf (buf
, len
, "never");
360 /* Get current time. */
363 diff
= curr
- last_update
;
366 snprintf (buf
, len
, "%d:%02d:%02d:%02d",
367 tm
->tm_yday
, tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
371 * bfd_show_param - Show the BFD parameter information.
374 bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
375 int extra_space
, u_char use_json
, json_object
*json_obj
)
377 json_object
*json_bfd
= NULL
;
385 json_bfd
= json_object_new_object();
389 json_object_int_add(json_bfd
, "detectMultiplier", bfd_info
->detect_mult
);
390 json_object_int_add(json_bfd
, "rxMinInterval", bfd_info
->required_min_rx
);
391 json_object_int_add(json_bfd
, "txMinInterval", bfd_info
->desired_min_tx
);
393 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
397 vty_outln (vty
, " %s%sDetect Mul: %d, Min Rx interval: %d,"
398 " Min Tx interval: %d",
399 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
400 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
401 bfd_info
->desired_min_tx
);
406 * bfd_show_status - Show the BFD status information.
409 bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
410 int extra_space
, u_char use_json
, json_object
*json_bfd
)
417 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
420 json_object_string_add(json_bfd
, "status",
421 bfd_get_status_str(bfd_info
->status
));
422 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
426 vty_outln (vty
, " %s%sStatus: %s, Last update: %s",
427 (extra_space
) ? " ": "", (bfd_tag
) ? "BFD: " : " ",
428 bfd_get_status_str(bfd_info
->status
), time_buf
);
433 * bfd_show_info - Show the BFD information.
436 bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
437 int extra_space
, u_char use_json
, json_object
*json_obj
)
439 json_object
*json_bfd
= NULL
;
446 json_bfd
= json_object_new_object();
448 json_object_string_add(json_bfd
, "type", "multi hop");
450 json_object_string_add(json_bfd
, "type", "single hop");
454 vty_outln (vty
, " %sBFD: Type: %s", (extra_space
) ? " " : "",
455 (multihop
) ? "multi hop" : "single hop");
458 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
459 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
462 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
468 * bfd_client_sendmsg - Format and send a client register
469 * command to Zebra to be forwarded to BFD
472 bfd_client_sendmsg (struct zclient
*zclient
, int command
)
478 if (!zclient
|| zclient
->sock
< 0)
481 zlog_debug ("%s: Can't send BFD client register, Zebra client not "
482 "established", __FUNCTION__
);
488 zclient_create_header (s
, command
, VRF_DEFAULT
);
490 stream_putl(s
, getpid());
492 stream_putw_at (s
, 0, stream_get_endp (s
));
494 ret
= zclient_send_message(zclient
);
499 zlog_debug ("bfd_client_sendmsg %ld: zclient_send_message() failed",