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
43 void bfd_gbl_init(void)
45 memset(&bfd_gbl
, 0, sizeof(struct bfd_gbl
));
49 * bfd_gbl_exit - Called when daemon exits
51 void bfd_gbl_exit(void)
53 SET_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
);
57 * bfd_info_create - Allocate the BFD information
59 struct bfd_info
*bfd_info_create(void)
61 struct bfd_info
*bfd_info
;
63 bfd_info
= XCALLOC(MTYPE_BFD_INFO
, sizeof(struct bfd_info
));
66 bfd_info
->status
= BFD_STATUS_UNKNOWN
;
67 bfd_info
->type
= BFD_TYPE_NOT_CONFIGURED
;
68 bfd_info
->last_update
= 0;
73 * bfd_info_free - Free the BFD information.
75 void bfd_info_free(struct bfd_info
**bfd_info
)
78 XFREE(MTYPE_BFD_INFO
, *bfd_info
);
84 * bfd_validate_param - Validate the BFD paramter information.
86 int bfd_validate_param(struct vty
*vty
, const char *dm_str
, const char *rx_str
,
87 const char *tx_str
, u_int8_t
*dm_val
, u_int32_t
*rx_val
,
90 *dm_val
= strtoul(dm_str
, NULL
, 10);
91 *rx_val
= strtoul(rx_str
, NULL
, 10);
92 *tx_val
= strtoul(tx_str
, NULL
, 10);
97 * bfd_set_param - Set the configured BFD paramter values
99 void bfd_set_param(struct bfd_info
**bfd_info
, u_int32_t min_rx
,
100 u_int32_t min_tx
, u_int8_t detect_mult
, int defaults
,
104 *bfd_info
= bfd_info_create();
105 *command
= ZEBRA_BFD_DEST_REGISTER
;
107 if (((*bfd_info
)->required_min_rx
!= min_rx
)
108 || ((*bfd_info
)->desired_min_tx
!= min_tx
)
109 || ((*bfd_info
)->detect_mult
!= detect_mult
))
110 *command
= ZEBRA_BFD_DEST_UPDATE
;
114 (*bfd_info
)->required_min_rx
= min_rx
;
115 (*bfd_info
)->desired_min_tx
= min_tx
;
116 (*bfd_info
)->detect_mult
= detect_mult
;
120 SET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
122 UNSET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
126 * bfd_peer_sendmsg - Format and send a peer register/Unregister
127 * command to Zebra to be forwarded to BFD
129 void bfd_peer_sendmsg(struct zclient
*zclient
, struct bfd_info
*bfd_info
,
130 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
131 int ttl
, int multihop
, int command
, int set_flag
,
138 /* Individual reg/dereg messages are supressed during shutdown. */
139 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
142 "%s: Suppressing BFD peer reg/dereg messages",
148 if (!zclient
|| zclient
->sock
< 0) {
151 "%s: Can't send BFD peer register, Zebra client not "
159 zclient_create_header(s
, command
, vrf_id
);
161 stream_putl(s
, getpid());
163 stream_putw(s
, family
);
166 stream_put_in_addr(s
, (struct in_addr
*)dst_ip
);
169 stream_put(s
, dst_ip
, 16);
175 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
176 stream_putl(s
, bfd_info
->required_min_rx
);
177 stream_putl(s
, bfd_info
->desired_min_tx
);
178 stream_putc(s
, bfd_info
->detect_mult
);
183 /* Multi-hop destination send the source IP address to BFD */
185 stream_putw(s
, family
);
188 stream_put_in_addr(s
, (struct in_addr
*)src_ip
);
191 stream_put(s
, src_ip
, 16);
200 if ((family
== AF_INET6
) && (src_ip
)) {
201 stream_putw(s
, family
);
202 stream_put(s
, src_ip
, 16);
205 len
= strlen(if_name
);
207 stream_put(s
, if_name
, len
);
213 stream_putw_at(s
, 0, stream_get_endp(s
));
215 ret
= zclient_send_message(zclient
);
220 "bfd_peer_sendmsg: zclient_send_message() failed");
225 if (command
== ZEBRA_BFD_DEST_REGISTER
)
226 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
227 else if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
228 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
235 * bfd_get_command_dbg_str - Convert command to a debug string.
237 const char *bfd_get_command_dbg_str(int command
)
240 case ZEBRA_BFD_DEST_REGISTER
:
242 case ZEBRA_BFD_DEST_DEREGISTER
:
244 case ZEBRA_BFD_DEST_UPDATE
:
252 * bfd_get_peer_info - Extract the Peer information for which the BFD session
253 * went down from the message sent from Zebra to clients.
255 struct interface
*bfd_get_peer_info(struct stream
*s
, struct prefix
*dp
,
256 struct prefix
*sp
, int *status
,
259 unsigned int ifindex
;
260 struct interface
*ifp
= NULL
;
263 /* Get interface index. */
264 ifindex
= stream_getl(s
);
268 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
272 "zebra_interface_bfd_read: "
273 "Can't find interface by ifindex: %d ",
279 /* Fetch destination address. */
280 dp
->family
= stream_getc(s
);
282 plen
= prefix_blen(dp
);
283 stream_get(&dp
->u
.prefix
, s
, plen
);
284 dp
->prefixlen
= stream_getc(s
);
286 /* Get BFD status. */
287 *status
= stream_getl(s
);
290 sp
->family
= stream_getc(s
);
292 plen
= prefix_blen(sp
);
293 stream_get(&sp
->u
.prefix
, s
, plen
);
294 sp
->prefixlen
= stream_getc(s
);
300 * bfd_get_status_str - Convert BFD status to a display string.
302 const char *bfd_get_status_str(int status
)
305 case BFD_STATUS_DOWN
:
309 case BFD_STATUS_UNKNOWN
:
316 * bfd_last_update - Calculate the last BFD update time and convert it
317 * into a dd:hh:mm:ss display format.
319 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
326 /* If no BFD satatus update has ever been received, print `never'. */
327 if (last_update
== 0) {
328 snprintf(buf
, len
, "never");
332 /* Get current time. */
335 diff
= curr
- last_update
;
338 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
->tm_yday
, tm
->tm_hour
,
339 tm
->tm_min
, tm
->tm_sec
);
343 * bfd_show_param - Show the BFD parameter information.
345 void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
346 int extra_space
, u_char use_json
, json_object
*json_obj
)
348 json_object
*json_bfd
= NULL
;
355 json_bfd
= json_object_new_object();
359 json_object_int_add(json_bfd
, "detectMultiplier",
360 bfd_info
->detect_mult
);
361 json_object_int_add(json_bfd
, "rxMinInterval",
362 bfd_info
->required_min_rx
);
363 json_object_int_add(json_bfd
, "txMinInterval",
364 bfd_info
->desired_min_tx
);
366 json_object_object_add(json_obj
, "peerBfdInfo",
370 " %s%sDetect Mul: %d, Min Rx interval: %d,"
371 " Min Tx interval: %d\n",
372 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
373 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
374 bfd_info
->desired_min_tx
);
379 * bfd_show_status - Show the BFD status information.
381 static void bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
,
382 int bfd_tag
, int extra_space
, u_char use_json
,
383 json_object
*json_bfd
)
390 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
392 json_object_string_add(json_bfd
, "status",
393 bfd_get_status_str(bfd_info
->status
));
394 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
396 vty_out(vty
, " %s%sStatus: %s, Last update: %s\n",
397 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
398 bfd_get_status_str(bfd_info
->status
), time_buf
);
403 * bfd_show_info - Show the BFD information.
405 void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
406 int extra_space
, u_char use_json
, json_object
*json_obj
)
408 json_object
*json_bfd
= NULL
;
414 json_bfd
= json_object_new_object();
416 json_object_string_add(json_bfd
, "type", "multi hop");
418 json_object_string_add(json_bfd
, "type", "single hop");
420 vty_out(vty
, " %sBFD: Type: %s\n", (extra_space
) ? " " : "",
421 (multihop
) ? "multi hop" : "single hop");
424 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
425 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
428 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
434 * bfd_client_sendmsg - Format and send a client register
435 * command to Zebra to be forwarded to BFD
437 void bfd_client_sendmsg(struct zclient
*zclient
, int command
)
443 if (!zclient
|| zclient
->sock
< 0) {
446 "%s: Can't send BFD client register, Zebra client not "
454 zclient_create_header(s
, command
, VRF_DEFAULT
);
456 stream_putl(s
, getpid());
458 stream_putw_at(s
, 0, stream_get_endp(s
));
460 ret
= zclient_send_message(zclient
);
465 "bfd_client_sendmsg %ld: zclient_send_message() failed",