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
)
77 XFREE(MTYPE_BFD_INFO
, *bfd_info
);
81 * bfd_validate_param - Validate the BFD paramter information.
83 int bfd_validate_param(struct vty
*vty
, const char *dm_str
, const char *rx_str
,
84 const char *tx_str
, uint8_t *dm_val
, uint32_t *rx_val
,
87 *dm_val
= strtoul(dm_str
, NULL
, 10);
88 *rx_val
= strtoul(rx_str
, NULL
, 10);
89 *tx_val
= strtoul(tx_str
, NULL
, 10);
94 * bfd_set_param - Set the configured BFD paramter values
96 void bfd_set_param(struct bfd_info
**bfd_info
, uint32_t min_rx
, uint32_t min_tx
,
97 uint8_t detect_mult
, const char *profile
, int defaults
,
101 *bfd_info
= bfd_info_create();
102 *command
= ZEBRA_BFD_DEST_REGISTER
;
104 if (((*bfd_info
)->required_min_rx
!= min_rx
)
105 || ((*bfd_info
)->desired_min_tx
!= min_tx
)
106 || ((*bfd_info
)->detect_mult
!= detect_mult
)
107 || ((*bfd_info
)->profile
[0] == 0 && profile
)
108 || ((*bfd_info
)->profile
[0] && profile
== NULL
)
109 || (profile
&& (*bfd_info
)->profile
[0]
110 && strcmp((*bfd_info
)->profile
, profile
)))
111 *command
= ZEBRA_BFD_DEST_UPDATE
;
115 (*bfd_info
)->required_min_rx
= min_rx
;
116 (*bfd_info
)->desired_min_tx
= min_tx
;
117 (*bfd_info
)->detect_mult
= detect_mult
;
119 strlcpy((*bfd_info
)->profile
, profile
,
120 BFD_PROFILE_NAME_LEN
);
122 (*bfd_info
)->profile
[0] = '\0';
126 SET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
128 UNSET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
132 * bfd_peer_sendmsg - Format and send a peer register/Unregister
133 * command to Zebra to be forwarded to BFD
135 * DEPRECATED: use zclient_bfd_command instead
137 void bfd_peer_sendmsg(struct zclient
*zclient
, struct bfd_info
*bfd_info
,
138 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
139 int ttl
, int multihop
, int cbit
, int command
,
140 int set_flag
, vrf_id_t vrf_id
)
142 struct bfd_session_arg args
= {};
145 /* Individual reg/dereg messages are suppressed during shutdown. */
146 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
149 "%s: Suppressing BFD peer reg/dereg messages",
155 if (!zclient
|| zclient
->sock
< 0) {
158 "%s: Can't send BFD peer register, Zebra client not established",
163 /* Fill in all arguments. */
166 args
.family
= family
;
167 args
.mhop
= multihop
;
168 args
.vrf_id
= vrf_id
;
169 args
.command
= command
;
170 args
.set_flag
= set_flag
;
171 args
.bfd_info
= bfd_info
;
173 args
.min_rx
= bfd_info
->required_min_rx
;
174 args
.min_tx
= bfd_info
->desired_min_tx
;
175 args
.detection_multiplier
= bfd_info
->detect_mult
;
176 if (bfd_info
->profile
[0]) {
177 args
.profilelen
= strlen(bfd_info
->profile
);
178 strlcpy(args
.profile
, bfd_info
->profile
,
179 sizeof(args
.profile
));
183 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
184 : sizeof(struct in6_addr
);
185 memcpy(&args
.dst
, dst_ip
, addrlen
);
187 memcpy(&args
.src
, src_ip
, addrlen
);
191 strlcpy(args
.ifname
, if_name
, sizeof(args
.ifname
));
193 zclient_bfd_command(zclient
, &args
);
197 * bfd_get_command_dbg_str - Convert command to a debug string.
199 const char *bfd_get_command_dbg_str(int command
)
202 case ZEBRA_BFD_DEST_REGISTER
:
204 case ZEBRA_BFD_DEST_DEREGISTER
:
206 case ZEBRA_BFD_DEST_UPDATE
:
214 * bfd_get_peer_info - Extract the Peer information for which the BFD session
215 * went down from the message sent from Zebra to clients.
217 struct interface
*bfd_get_peer_info(struct stream
*s
, struct prefix
*dp
,
218 struct prefix
*sp
, int *status
,
222 unsigned int ifindex
;
223 struct interface
*ifp
= NULL
;
225 int local_remote_cbit
;
228 * If the ifindex lookup fails the
229 * rest of the data in the stream is
230 * not read. All examples of this function
231 * call immediately use the dp->family which
232 * is not good. Ensure we are not using
235 memset(dp
, 0, sizeof(*dp
));
236 memset(sp
, 0, sizeof(*sp
));
238 /* Get interface index. */
239 ifindex
= stream_getl(s
);
243 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
247 "zebra_interface_bfd_read: Can't find interface by ifindex: %d ",
253 /* Fetch destination address. */
254 dp
->family
= stream_getc(s
);
256 plen
= prefix_blen(dp
);
257 stream_get(&dp
->u
.prefix
, s
, plen
);
258 dp
->prefixlen
= stream_getc(s
);
260 /* Get BFD status. */
261 *status
= stream_getl(s
);
263 sp
->family
= stream_getc(s
);
265 plen
= prefix_blen(sp
);
266 stream_get(&sp
->u
.prefix
, s
, plen
);
267 sp
->prefixlen
= stream_getc(s
);
269 local_remote_cbit
= stream_getc(s
);
271 *remote_cbit
= local_remote_cbit
;
276 * bfd_get_status_str - Convert BFD status to a display string.
278 const char *bfd_get_status_str(int status
)
281 case BFD_STATUS_DOWN
:
285 case BFD_STATUS_ADMIN_DOWN
:
287 case BFD_STATUS_UNKNOWN
:
294 * bfd_last_update - Calculate the last BFD update time and convert it
295 * into a dd:hh:mm:ss display format.
297 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
304 /* If no BFD satatus update has ever been received, print `never'. */
305 if (last_update
== 0) {
306 snprintf(buf
, len
, "never");
310 /* Get current time. */
313 diff
= curr
- last_update
;
314 gmtime_r(&diff
, &tm
);
316 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
.tm_yday
, tm
.tm_hour
,
317 tm
.tm_min
, tm
.tm_sec
);
321 * bfd_show_param - Show the BFD parameter information.
323 void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
324 int extra_space
, bool use_json
, json_object
*json_obj
)
326 json_object
*json_bfd
= NULL
;
333 json_bfd
= json_object_new_object();
337 json_object_int_add(json_bfd
, "detectMultiplier",
338 bfd_info
->detect_mult
);
339 json_object_int_add(json_bfd
, "rxMinInterval",
340 bfd_info
->required_min_rx
);
341 json_object_int_add(json_bfd
, "txMinInterval",
342 bfd_info
->desired_min_tx
);
344 json_object_object_add(json_obj
, "peerBfdInfo",
348 " %s%sDetect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
349 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
350 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
351 bfd_info
->desired_min_tx
);
356 * bfd_show_status - Show the BFD status information.
358 static void bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
,
359 int bfd_tag
, int extra_space
, bool use_json
,
360 json_object
*json_bfd
)
367 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
369 json_object_string_add(json_bfd
, "status",
370 bfd_get_status_str(bfd_info
->status
));
371 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
373 vty_out(vty
, " %s%sStatus: %s, Last update: %s\n",
374 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
375 bfd_get_status_str(bfd_info
->status
), time_buf
);
380 * bfd_show_info - Show the BFD information.
382 void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
383 int extra_space
, bool use_json
, json_object
*json_obj
)
385 json_object
*json_bfd
= NULL
;
391 json_bfd
= json_object_new_object();
393 json_object_string_add(json_bfd
, "type", "multi hop");
395 json_object_string_add(json_bfd
, "type", "single hop");
397 vty_out(vty
, " %sBFD: Type: %s\n", (extra_space
) ? " " : "",
398 (multihop
) ? "multi hop" : "single hop");
401 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
402 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
405 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
411 * bfd_client_sendmsg - Format and send a client register
412 * command to Zebra to be forwarded to BFD
414 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
418 enum zclient_send_status ret
;
421 if (!zclient
|| zclient
->sock
< 0) {
424 "%s: Can't send BFD client register, Zebra client not established",
431 zclient_create_header(s
, command
, vrf_id
);
433 stream_putl(s
, getpid());
435 stream_putw_at(s
, 0, stream_get_endp(s
));
437 ret
= zclient_send_message(zclient
);
439 if (ret
== ZCLIENT_SEND_FAILURE
) {
442 "bfd_client_sendmsg %ld: zclient_send_message() failed",
450 int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*args
)
455 /* Individual reg/dereg messages are suppressed during shutdown. */
456 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
459 "%s: Suppressing BFD peer reg/dereg messages",
465 if (!zc
|| zc
->sock
< 0) {
467 zlog_debug("%s: zclient unavailable", __func__
);
474 /* Create new message. */
475 zclient_create_header(s
, args
->command
, args
->vrf_id
);
476 stream_putl(s
, getpid());
478 /* Encode destination address. */
479 stream_putw(s
, args
->family
);
480 addrlen
= (args
->family
== AF_INET
) ? sizeof(struct in_addr
)
481 : sizeof(struct in6_addr
);
482 stream_put(s
, &args
->dst
, addrlen
);
485 * For more BFD integration protocol details, see function
486 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
489 /* Session timers. */
490 stream_putl(s
, args
->min_rx
);
491 stream_putl(s
, args
->min_tx
);
492 stream_putc(s
, args
->detection_multiplier
);
495 stream_putc(s
, args
->mhop
!= 0);
497 /* Source address. */
498 stream_putw(s
, args
->family
);
499 stream_put(s
, &args
->src
, addrlen
);
501 /* Send the expected TTL. */
502 stream_putc(s
, args
->ttl
);
504 /* Send interface name if any. */
505 stream_putc(s
, args
->ifnamelen
);
507 stream_put(s
, args
->ifname
, args
->ifnamelen
);
509 /* Send the C bit indicator. */
510 stream_putc(s
, args
->cbit
);
512 /* Send profile name if any. */
513 stream_putc(s
, args
->profilelen
);
514 if (args
->profilelen
)
515 stream_put(s
, args
->profile
, args
->profilelen
);
517 /* Encode timers if this is a registration message. */
518 if (args
->command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
519 stream_putl(s
, args
->min_rx
);
520 stream_putl(s
, args
->min_tx
);
521 stream_putc(s
, args
->detection_multiplier
);
525 /* Multi hop indicator. */
528 /* Multi hop always sends the source address. */
529 stream_putw(s
, args
->family
);
530 stream_put(s
, &args
->src
, addrlen
);
532 /* Send the expected TTL. */
533 stream_putc(s
, args
->ttl
);
535 /* Multi hop indicator. */
538 /* Single hop only sends the source address when IPv6. */
539 if (args
->family
== AF_INET6
) {
540 stream_putw(s
, args
->family
);
541 stream_put(s
, &args
->src
, addrlen
);
544 /* Send interface name if any. */
545 stream_putc(s
, args
->ifnamelen
);
547 stream_put(s
, args
->ifname
, args
->ifnamelen
);
549 #endif /* HAVE_BFDD */
551 /* Finish the message by writing the size. */
552 stream_putw_at(s
, 0, stream_get_endp(s
));
554 /* Send message to zebra. */
555 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
557 zlog_debug("%s: zclient_send_message failed", __func__
);
561 /* Write registration indicator into data structure. */
562 if (args
->bfd_info
&& args
->set_flag
) {
563 if (args
->command
== ZEBRA_BFD_DEST_REGISTER
)
564 SET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);
565 else if (args
->command
== ZEBRA_BFD_DEST_DEREGISTER
)
566 UNSET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);