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")
37 static int bfd_debug
= 0;
38 static 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
, uint8_t *dm_val
, uint32_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
, uint32_t min_rx
, uint32_t min_tx
,
100 uint8_t detect_mult
, int defaults
, int *command
)
103 *bfd_info
= bfd_info_create();
104 *command
= ZEBRA_BFD_DEST_REGISTER
;
106 if (((*bfd_info
)->required_min_rx
!= min_rx
)
107 || ((*bfd_info
)->desired_min_tx
!= min_tx
)
108 || ((*bfd_info
)->detect_mult
!= detect_mult
))
109 *command
= ZEBRA_BFD_DEST_UPDATE
;
113 (*bfd_info
)->required_min_rx
= min_rx
;
114 (*bfd_info
)->desired_min_tx
= min_tx
;
115 (*bfd_info
)->detect_mult
= detect_mult
;
119 SET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
121 UNSET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
125 * bfd_peer_sendmsg - Format and send a peer register/Unregister
126 * command to Zebra to be forwarded to BFD
128 void bfd_peer_sendmsg(struct zclient
*zclient
, struct bfd_info
*bfd_info
,
129 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
130 int ttl
, int multihop
, int cbit
, int command
,
131 int set_flag
, vrf_id_t vrf_id
)
137 /* Individual reg/dereg messages are suppressed during shutdown. */
138 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
141 "%s: Suppressing BFD peer reg/dereg messages",
147 if (!zclient
|| zclient
->sock
< 0) {
150 "%s: Can't send BFD peer register, Zebra client not "
158 zclient_create_header(s
, command
, vrf_id
);
160 stream_putl(s
, getpid());
162 stream_putw(s
, family
);
165 stream_put_in_addr(s
, (struct in_addr
*)dst_ip
);
168 stream_put(s
, dst_ip
, 16);
174 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
175 stream_putl(s
, bfd_info
->required_min_rx
);
176 stream_putl(s
, bfd_info
->desired_min_tx
);
177 stream_putc(s
, bfd_info
->detect_mult
);
182 /* Multi-hop destination send the source IP address to BFD */
184 stream_putw(s
, family
);
187 stream_put_in_addr(s
, (struct in_addr
*)src_ip
);
190 stream_put(s
, src_ip
, 16);
199 if ((family
== AF_INET6
) && (src_ip
)) {
200 stream_putw(s
, family
);
201 stream_put(s
, src_ip
, 16);
204 len
= strlen(if_name
);
206 stream_put(s
, if_name
, len
);
217 stream_putw_at(s
, 0, stream_get_endp(s
));
219 ret
= zclient_send_message(zclient
);
224 "bfd_peer_sendmsg: zclient_send_message() failed");
229 if (command
== ZEBRA_BFD_DEST_REGISTER
)
230 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
231 else if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
232 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
);
239 * bfd_get_command_dbg_str - Convert command to a debug string.
241 const char *bfd_get_command_dbg_str(int command
)
244 case ZEBRA_BFD_DEST_REGISTER
:
246 case ZEBRA_BFD_DEST_DEREGISTER
:
248 case ZEBRA_BFD_DEST_UPDATE
:
256 * bfd_get_peer_info - Extract the Peer information for which the BFD session
257 * went down from the message sent from Zebra to clients.
259 struct interface
*bfd_get_peer_info(struct stream
*s
, struct prefix
*dp
,
260 struct prefix
*sp
, int *status
,
264 unsigned int ifindex
;
265 struct interface
*ifp
= NULL
;
267 int local_remote_cbit
;
269 /* Get interface index. */
270 ifindex
= stream_getl(s
);
274 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
278 "zebra_interface_bfd_read: "
279 "Can't find interface by ifindex: %d ",
285 /* Fetch destination address. */
286 dp
->family
= stream_getc(s
);
288 plen
= prefix_blen(dp
);
289 stream_get(&dp
->u
.prefix
, s
, plen
);
290 dp
->prefixlen
= stream_getc(s
);
292 /* Get BFD status. */
293 *status
= stream_getl(s
);
296 sp
->family
= stream_getc(s
);
298 plen
= prefix_blen(sp
);
299 stream_get(&sp
->u
.prefix
, s
, plen
);
300 sp
->prefixlen
= stream_getc(s
);
302 local_remote_cbit
= stream_getc(s
);
304 *remote_cbit
= local_remote_cbit
;
309 * bfd_get_status_str - Convert BFD status to a display string.
311 const char *bfd_get_status_str(int status
)
314 case BFD_STATUS_DOWN
:
318 case BFD_STATUS_UNKNOWN
:
325 * bfd_last_update - Calculate the last BFD update time and convert it
326 * into a dd:hh:mm:ss display format.
328 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
335 /* If no BFD satatus update has ever been received, print `never'. */
336 if (last_update
== 0) {
337 snprintf(buf
, len
, "never");
341 /* Get current time. */
344 diff
= curr
- last_update
;
347 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
->tm_yday
, tm
->tm_hour
,
348 tm
->tm_min
, tm
->tm_sec
);
352 * bfd_show_param - Show the BFD parameter information.
354 void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
355 int extra_space
, bool use_json
, json_object
*json_obj
)
357 json_object
*json_bfd
= NULL
;
364 json_bfd
= json_object_new_object();
368 json_object_int_add(json_bfd
, "detectMultiplier",
369 bfd_info
->detect_mult
);
370 json_object_int_add(json_bfd
, "rxMinInterval",
371 bfd_info
->required_min_rx
);
372 json_object_int_add(json_bfd
, "txMinInterval",
373 bfd_info
->desired_min_tx
);
375 json_object_object_add(json_obj
, "peerBfdInfo",
379 " %s%sDetect Multiplier: %d, Min Rx interval: %d,"
380 " Min Tx interval: %d\n",
381 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
382 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
383 bfd_info
->desired_min_tx
);
388 * bfd_show_status - Show the BFD status information.
390 static void bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
,
391 int bfd_tag
, int extra_space
, bool use_json
,
392 json_object
*json_bfd
)
399 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
401 json_object_string_add(json_bfd
, "status",
402 bfd_get_status_str(bfd_info
->status
));
403 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
405 vty_out(vty
, " %s%sStatus: %s, Last update: %s\n",
406 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
407 bfd_get_status_str(bfd_info
->status
), time_buf
);
412 * bfd_show_info - Show the BFD information.
414 void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
415 int extra_space
, bool use_json
, json_object
*json_obj
)
417 json_object
*json_bfd
= NULL
;
423 json_bfd
= json_object_new_object();
425 json_object_string_add(json_bfd
, "type", "multi hop");
427 json_object_string_add(json_bfd
, "type", "single hop");
429 vty_out(vty
, " %sBFD: Type: %s\n", (extra_space
) ? " " : "",
430 (multihop
) ? "multi hop" : "single hop");
433 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
434 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
437 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
443 * bfd_client_sendmsg - Format and send a client register
444 * command to Zebra to be forwarded to BFD
446 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
453 if (!zclient
|| zclient
->sock
< 0) {
456 "%s: Can't send BFD client register, Zebra client not "
464 zclient_create_header(s
, command
, vrf_id
);
466 stream_putl(s
, getpid());
468 stream_putw_at(s
, 0, stream_get_endp(s
));
470 ret
= zclient_send_message(zclient
);
475 "bfd_client_sendmsg %ld: zclient_send_message() failed",