3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
5 * FRR is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with FRR; see the file COPYING. If not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "lib/command.h"
30 #ifndef VTYSH_EXTRACT_PL
31 #include "bfdd/bfdd_vty_clippy.c"
35 * Commands help string definitions.
37 #define PEER_STR "Configure peer\n"
38 #define INTERFACE_NAME_STR "Configure interface name to use\n"
39 #define PEER_IPV4_STR "IPv4 peer address\n"
40 #define PEER_IPV6_STR "IPv6 peer address\n"
41 #define MHOP_STR "Configure multihop\n"
42 #define LOCAL_STR "Configure local address\n"
43 #define LOCAL_IPV4_STR "IPv4 local address\n"
44 #define LOCAL_IPV6_STR "IPv6 local address\n"
45 #define LOCAL_INTF_STR "Configure local interface name to use\n"
46 #define VRF_STR "Configure VRF\n"
47 #define VRF_NAME_STR "Configure VRF name\n"
52 static int bfdd_write_config(struct vty
*vty
);
53 static int bfdd_peer_write_config(struct vty
*vty
);
54 static void _bfdd_peer_write_config(struct hash_backet
*hb
, void *arg
);
55 static int bfd_configure_peer(struct bfd_peer_cfg
*bpc
, bool mhop
,
56 const struct sockaddr_any
*peer
,
57 const struct sockaddr_any
*local
,
58 const char *ifname
, const char *vrfname
,
59 char *ebuf
, size_t ebuflen
);
61 static void _display_peer_header(struct vty
*vty
, struct bfd_session
*bs
);
62 static struct json_object
*__display_peer_json(struct bfd_session
*bs
);
63 static struct json_object
*_peer_json_header(struct bfd_session
*bs
);
64 static void _display_peer_json(struct vty
*vty
, struct bfd_session
*bs
);
65 static void _display_peer(struct vty
*vty
, struct bfd_session
*bs
);
66 static void _display_all_peers(struct vty
*vty
, bool use_json
);
67 static void _display_peer_iter(struct hash_backet
*hb
, void *arg
);
68 static void _display_peer_json_iter(struct hash_backet
*hb
, void *arg
);
69 static void _display_peer_counter(struct vty
*vty
, struct bfd_session
*bs
);
70 static struct json_object
*__display_peer_counters_json(struct bfd_session
*bs
);
71 static void _display_peer_counters_json(struct vty
*vty
, struct bfd_session
*bs
);
72 static void _display_peer_counter_iter(struct hash_backet
*hb
, void *arg
);
73 static void _display_peer_counter_json_iter(struct hash_backet
*hb
, void *arg
);
74 static void _display_peers_counter(struct vty
*vty
, bool use_json
);
75 static struct bfd_session
*
76 _find_peer_or_error(struct vty
*vty
, int argc
, struct cmd_token
**argv
,
77 const char *label
, const char *peer_str
,
78 const char *local_str
, const char *ifname
,
83 * Commands definition.
85 DEFUN_NOSH(bfd_enter
, bfd_enter_cmd
, "bfd", "Configure BFD peers\n")
92 bfd_peer_enter
, bfd_peer_enter_cmd
,
93 "peer <A.B.C.D|X:X::X:X> [{multihop|local-address <A.B.C.D|X:X::X:X>|interface IFNAME|vrf NAME}]",
94 PEER_STR PEER_IPV4_STR PEER_IPV6_STR
96 LOCAL_STR LOCAL_IPV4_STR LOCAL_IPV6_STR
103 struct bfd_session
*bs
;
104 const char *peer
, *ifname
, *local
, *vrfname
;
105 struct bfd_peer_cfg bpc
;
106 struct sockaddr_any psa
, lsa
, *lsap
;
109 vrfname
= peer
= ifname
= local
= NULL
;
111 /* Gather all provided information. */
115 mhop
= argv_find(argv
, argc
, "multihop", &idx
);
118 if (argv_find(argv
, argc
, "interface", &idx
))
119 ifname
= argv
[idx
+ 1]->arg
;
122 if (argv_find(argv
, argc
, "local-address", &idx
))
123 local
= argv
[idx
+ 1]->arg
;
126 if (argv_find(argv
, argc
, "vrf", &idx
))
127 vrfname
= argv
[idx
+ 1]->arg
;
129 if (vrfname
&& ifname
) {
130 vty_out(vty
, "%% VRF is not mixable with interface\n");
131 return CMD_WARNING_CONFIG_FAILED
;
136 strtosa(local
, &lsa
);
141 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
142 errormsg
, sizeof(errormsg
))
144 vty_out(vty
, "%% Invalid peer configuration: %s\n", errormsg
);
145 return CMD_WARNING_CONFIG_FAILED
;
148 bs
= bs_peer_find(&bpc
);
150 bs
= ptm_bfd_sess_new(&bpc
);
152 vty_out(vty
, "%% Failed to add peer.\n");
153 return CMD_WARNING_CONFIG_FAILED
;
157 VTY_PUSH_CONTEXT(BFD_PEER_NODE
, bs
);
162 DEFPY(bfd_peer_detectmultiplier
, bfd_peer_detectmultiplier_cmd
,
163 "detect-multiplier (2-255)$multiplier",
164 "Configure peer detection multiplier\n"
165 "Configure peer detection multiplier value\n")
167 struct bfd_session
*bs
;
169 bs
= VTY_GET_CONTEXT(bfd_session
);
170 if (bs
->detect_mult
== multiplier
)
173 bs
->detect_mult
= multiplier
;
179 DEFPY(bfd_peer_recvinterval
, bfd_peer_recvinterval_cmd
,
180 "receive-interval (10-60000)$interval",
181 "Configure peer receive interval\n"
182 "Configure peer receive interval value in milliseconds\n")
184 struct bfd_session
*bs
;
186 bs
= VTY_GET_CONTEXT(bfd_session
);
187 if (bs
->timers
.required_min_rx
== (uint32_t)(interval
* 1000))
190 bs
->timers
.required_min_rx
= interval
* 1000;
196 DEFPY(bfd_peer_txinterval
, bfd_peer_txinterval_cmd
,
197 "transmit-interval (10-60000)$interval",
198 "Configure peer transmit interval\n"
199 "Configure peer transmit interval value in milliseconds\n")
201 struct bfd_session
*bs
;
203 bs
= VTY_GET_CONTEXT(bfd_session
);
204 if (bs
->up_min_tx
== (uint32_t)(interval
* 1000))
207 bs
->up_min_tx
= interval
* 1000;
213 DEFPY(bfd_peer_echointerval
, bfd_peer_echointerval_cmd
,
214 "echo-interval (10-60000)$interval",
215 "Configure peer echo interval\n"
216 "Configure peer echo interval value in milliseconds\n")
218 struct bfd_session
*bs
;
220 bs
= VTY_GET_CONTEXT(bfd_session
);
221 if (bs
->timers
.required_min_echo
== (uint32_t)(interval
* 1000))
224 bs
->timers
.required_min_echo
= interval
* 1000;
230 DEFPY(bfd_peer_shutdown
, bfd_peer_shutdown_cmd
, "[no] shutdown",
231 NO_STR
"Disable BFD peer")
233 struct bfd_session
*bs
;
235 bs
= VTY_GET_CONTEXT(bfd_session
);
237 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
240 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
242 /* Change and notify state change. */
243 bs
->ses_state
= PTM_BFD_DOWN
;
246 /* Enable all timers. */
247 bfd_recvtimer_update(bs
);
248 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
249 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
)) {
250 bfd_echo_recvtimer_update(bs
);
251 bfd_echo_xmttimer_update(bs
, bs
->echo_xmt_TO
);
254 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
257 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
259 /* Disable all events. */
260 bfd_recvtimer_delete(bs
);
261 bfd_echo_recvtimer_delete(bs
);
262 bfd_xmttimer_delete(bs
);
263 bfd_echo_xmttimer_delete(bs
);
265 /* Change and notify state change. */
266 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
275 DEFPY(bfd_peer_echo
, bfd_peer_echo_cmd
, "[no] echo-mode",
276 NO_STR
"Configure echo mode\n")
278 struct bfd_session
*bs
;
280 bs
= VTY_GET_CONTEXT(bfd_session
);
282 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
285 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
286 ptm_bfd_echo_stop(bs
, 0);
288 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
291 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
292 /* Apply setting immediately. */
293 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
)) {
294 ptm_bfd_echo_start(bs
);
295 bfd_echo_recvtimer_update(bs
);
302 DEFPY(bfd_peer_label
, bfd_peer_label_cmd
, "label WORD$label",
303 "Register peer label\n"
304 "Register peer label identification\n")
306 struct bfd_session
*bs
;
308 /* Validate label length. */
309 if (strlen(label
) >= MAXNAMELEN
) {
310 vty_out(vty
, "%% Label name is too long\n");
311 return CMD_WARNING_CONFIG_FAILED
;
314 bs
= VTY_GET_CONTEXT(bfd_session
);
315 if (bfd_session_update_label(bs
, label
) == -1) {
316 vty_out(vty
, "%% Failed to update peer label.\n");
317 return CMD_WARNING_CONFIG_FAILED
;
323 DEFPY(bfd_no_peer
, bfd_no_peer_cmd
,
324 "no peer <A.B.C.D|X:X::X:X>$peer [{multihop|local-address <A.B.C.D|X:X::X:X>$local|interface IFNAME$ifname|vrf NAME$vrfname}]",
326 PEER_STR PEER_IPV4_STR PEER_IPV6_STR
328 LOCAL_STR LOCAL_IPV4_STR LOCAL_IPV6_STR
331 VRF_STR VRF_NAME_STR
)
335 struct bfd_peer_cfg bpc
;
336 struct sockaddr_any psa
, lsa
, *lsap
;
339 strtosa(peer_str
, &psa
);
341 strtosa(local_str
, &lsa
);
348 mhop
= argv_find(argv
, argc
, "multihop", &idx
);
350 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
351 errormsg
, sizeof(errormsg
))
353 vty_out(vty
, "%% Invalid peer configuration: %s\n", errormsg
);
354 return CMD_WARNING_CONFIG_FAILED
;
357 if (ptm_bfd_ses_del(&bpc
) != 0) {
358 vty_out(vty
, "%% Failed to remove peer.\n");
359 return CMD_WARNING_CONFIG_FAILED
;
367 * Show commands helper functions
369 static void _display_peer_header(struct vty
*vty
, struct bfd_session
*bs
)
371 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
372 vty_out(vty
, "\tpeer %s", satostr(&bs
->mhop
.peer
));
373 vty_out(vty
, " multihop");
374 vty_out(vty
, " local-address %s", satostr(&bs
->mhop
.local
));
375 if (bs
->mhop
.vrf_name
[0])
376 vty_out(vty
, " vrf %s", bs
->mhop
.vrf_name
);
379 vty_out(vty
, "\tpeer %s", satostr(&bs
->shop
.peer
));
380 if (bs
->local_address
.sa_sin
.sin_family
!= AF_UNSPEC
)
381 vty_out(vty
, " local-address %s",
382 satostr(&bs
->local_address
));
383 if (bs
->shop
.port_name
[0])
384 vty_out(vty
, " interface %s", bs
->shop
.port_name
);
389 vty_out(vty
, "\t\tlabel: %s\n", bs
->pl
->pl_label
);
392 static void _display_peer(struct vty
*vty
, struct bfd_session
*bs
)
397 _display_peer_header(vty
, bs
);
399 vty_out(vty
, "\t\tID: %u\n", bs
->discrs
.my_discr
);
400 vty_out(vty
, "\t\tRemote ID: %u\n", bs
->discrs
.remote_discr
);
402 vty_out(vty
, "\t\tStatus: ");
403 switch (bs
->ses_state
) {
404 case PTM_BFD_ADM_DOWN
:
405 vty_out(vty
, "shutdown\n");
408 vty_out(vty
, "down\n");
410 now
= monotime(NULL
);
411 integer2timestr(now
- bs
->downtime
.tv_sec
, buf
, sizeof(buf
));
412 vty_out(vty
, "\t\tDowntime: %s\n", buf
);
415 vty_out(vty
, "init\n");
418 vty_out(vty
, "up\n");
420 now
= monotime(NULL
);
421 integer2timestr(now
- bs
->uptime
.tv_sec
, buf
, sizeof(buf
));
422 vty_out(vty
, "\t\tUptime: %s\n", buf
);
426 vty_out(vty
, "unknown\n");
430 vty_out(vty
, "\t\tDiagnostics: %s\n", diag2str(bs
->local_diag
));
431 vty_out(vty
, "\t\tRemote diagnostics: %s\n", diag2str(bs
->remote_diag
));
433 vty_out(vty
, "\t\tLocal timers:\n");
434 vty_out(vty
, "\t\t\tReceive interval: %" PRIu32
"ms\n",
435 bs
->timers
.required_min_rx
/ 1000);
436 vty_out(vty
, "\t\t\tTransmission interval: %" PRIu32
"ms",
437 bs
->timers
.desired_min_tx
/ 1000);
438 if (bs
->up_min_tx
!= bs
->timers
.desired_min_tx
)
439 vty_out(vty
, " (configured %" PRIu32
"ms)\n",
440 bs
->up_min_tx
/ 1000);
444 vty_out(vty
, "\t\t\tEcho transmission interval: ");
445 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
446 vty_out(vty
, "%" PRIu32
"ms\n",
447 bs
->timers
.required_min_echo
/ 1000);
449 vty_out(vty
, "disabled\n");
451 vty_out(vty
, "\t\tRemote timers:\n");
452 vty_out(vty
, "\t\t\tReceive interval: %" PRIu32
"ms\n",
453 bs
->remote_timers
.required_min_rx
/ 1000);
454 vty_out(vty
, "\t\t\tTransmission interval: %" PRIu32
"ms\n",
455 bs
->remote_timers
.desired_min_tx
/ 1000);
456 vty_out(vty
, "\t\t\tEcho transmission interval: %" PRIu32
"ms\n",
457 bs
->remote_timers
.required_min_echo
/ 1000);
462 static struct json_object
*_peer_json_header(struct bfd_session
*bs
)
464 struct json_object
*jo
= json_object_new_object();
466 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
467 json_object_boolean_true_add(jo
, "multihop");
468 json_object_string_add(jo
, "peer", satostr(&bs
->mhop
.peer
));
469 json_object_string_add(jo
, "local", satostr(&bs
->mhop
.local
));
470 if (bs
->mhop
.vrf_name
[0])
471 json_object_string_add(jo
, "vrf", bs
->mhop
.vrf_name
);
473 json_object_boolean_false_add(jo
, "multihop");
474 json_object_string_add(jo
, "peer", satostr(&bs
->shop
.peer
));
475 if (bs
->local_address
.sa_sin
.sin_family
!= AF_UNSPEC
)
476 json_object_string_add(jo
, "local",
477 satostr(&bs
->local_address
));
478 if (bs
->shop
.port_name
[0])
479 json_object_string_add(jo
, "interface",
484 json_object_string_add(jo
, "label", bs
->pl
->pl_label
);
489 static struct json_object
*__display_peer_json(struct bfd_session
*bs
)
491 struct json_object
*jo
= _peer_json_header(bs
);
493 json_object_int_add(jo
, "id", bs
->discrs
.my_discr
);
494 json_object_int_add(jo
, "remote-id", bs
->discrs
.remote_discr
);
496 switch (bs
->ses_state
) {
497 case PTM_BFD_ADM_DOWN
:
498 json_object_string_add(jo
, "status", "shutdown");
501 json_object_string_add(jo
, "status", "down");
502 json_object_int_add(jo
, "downtime",
503 monotime(NULL
) - bs
->downtime
.tv_sec
);
506 json_object_string_add(jo
, "status", "init");
509 json_object_string_add(jo
, "status", "up");
510 json_object_int_add(jo
, "uptime",
511 monotime(NULL
) - bs
->uptime
.tv_sec
);
515 json_object_string_add(jo
, "status", "unknown");
519 json_object_string_add(jo
, "diagnostic", diag2str(bs
->local_diag
));
520 json_object_string_add(jo
, "remote-diagnostic",
521 diag2str(bs
->remote_diag
));
523 json_object_int_add(jo
, "receive-interval",
524 bs
->timers
.required_min_rx
/ 1000);
525 json_object_int_add(jo
, "transmit-interval",
526 bs
->timers
.desired_min_tx
/ 1000);
527 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
528 json_object_int_add(jo
, "echo-interval",
529 bs
->timers
.required_min_echo
/ 1000);
531 json_object_int_add(jo
, "echo-interval", 0);
533 json_object_int_add(jo
, "remote-receive-interval",
534 bs
->remote_timers
.required_min_rx
/ 1000);
535 json_object_int_add(jo
, "remote-transmit-interval",
536 bs
->remote_timers
.desired_min_tx
/ 1000);
537 json_object_int_add(jo
, "remote-echo-interval",
538 bs
->remote_timers
.required_min_echo
/ 1000);
543 static void _display_peer_json(struct vty
*vty
, struct bfd_session
*bs
)
545 struct json_object
*jo
= __display_peer_json(bs
);
547 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
548 json_object_free(jo
);
551 static void _display_peer_iter(struct hash_backet
*hb
, void *arg
)
553 struct vty
*vty
= arg
;
554 struct bfd_session
*bs
= hb
->data
;
556 _display_peer(vty
, bs
);
559 static void _display_peer_json_iter(struct hash_backet
*hb
, void *arg
)
561 struct json_object
*jo
= arg
, *jon
= NULL
;
562 struct bfd_session
*bs
= hb
->data
;
564 jon
= __display_peer_json(bs
);
566 log_warning("%s: not enough memory", __func__
);
570 json_object_array_add(jo
, jon
);
573 static void _display_all_peers(struct vty
*vty
, bool use_json
)
575 struct json_object
*jo
;
577 if (use_json
== false) {
578 vty_out(vty
, "BFD Peers:\n");
579 bfd_id_iterate(_display_peer_iter
, vty
);
583 jo
= json_object_new_array();
584 bfd_id_iterate(_display_peer_json_iter
, jo
);
586 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
587 json_object_free(jo
);
590 static void _display_peer_counter(struct vty
*vty
, struct bfd_session
*bs
)
592 _display_peer_header(vty
, bs
);
594 vty_out(vty
, "\t\tControl packet input: %" PRIu64
" packets\n",
595 bs
->stats
.rx_ctrl_pkt
);
596 vty_out(vty
, "\t\tControl packet output: %" PRIu64
" packets\n",
597 bs
->stats
.tx_ctrl_pkt
);
598 vty_out(vty
, "\t\tEcho packet input: %" PRIu64
" packets\n",
599 bs
->stats
.rx_echo_pkt
);
600 vty_out(vty
, "\t\tEcho packet output: %" PRIu64
" packets\n",
601 bs
->stats
.tx_echo_pkt
);
602 vty_out(vty
, "\t\tSession up events: %" PRIu64
"\n",
603 bs
->stats
.session_up
);
604 vty_out(vty
, "\t\tSession down events: %" PRIu64
"\n",
605 bs
->stats
.session_down
);
606 vty_out(vty
, "\t\tZebra notifications: %" PRIu64
"\n",
607 bs
->stats
.znotification
);
611 static struct json_object
*__display_peer_counters_json(struct bfd_session
*bs
)
613 struct json_object
*jo
= _peer_json_header(bs
);
615 json_object_int_add(jo
, "control-packet-input", bs
->stats
.rx_ctrl_pkt
);
616 json_object_int_add(jo
, "control-packet-output", bs
->stats
.tx_ctrl_pkt
);
617 json_object_int_add(jo
, "echo-packet-input", bs
->stats
.rx_echo_pkt
);
618 json_object_int_add(jo
, "echo-packet-output", bs
->stats
.tx_echo_pkt
);
619 json_object_int_add(jo
, "session-up", bs
->stats
.session_up
);
620 json_object_int_add(jo
, "session-down", bs
->stats
.session_down
);
621 json_object_int_add(jo
, "zebra-notifications", bs
->stats
.znotification
);
626 static void _display_peer_counters_json(struct vty
*vty
, struct bfd_session
*bs
)
628 struct json_object
*jo
= __display_peer_counters_json(bs
);
630 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
631 json_object_free(jo
);
634 static void _display_peer_counter_iter(struct hash_backet
*hb
, void *arg
)
636 struct vty
*vty
= arg
;
637 struct bfd_session
*bs
= hb
->data
;
639 _display_peer_counter(vty
, bs
);
642 static void _display_peer_counter_json_iter(struct hash_backet
*hb
, void *arg
)
644 struct json_object
*jo
= arg
, *jon
= NULL
;
645 struct bfd_session
*bs
= hb
->data
;
647 jon
= __display_peer_counters_json(bs
);
649 log_warning("%s: not enough memory", __func__
);
653 json_object_array_add(jo
, jon
);
656 static void _display_peers_counter(struct vty
*vty
, bool use_json
)
658 struct json_object
*jo
;
660 if (use_json
== false) {
661 vty_out(vty
, "BFD Peers:\n");
662 bfd_id_iterate(_display_peer_counter_iter
, vty
);
666 jo
= json_object_new_array();
667 bfd_id_iterate(_display_peer_counter_json_iter
, jo
);
669 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
670 json_object_free(jo
);
673 static struct bfd_session
*
674 _find_peer_or_error(struct vty
*vty
, int argc
, struct cmd_token
**argv
,
675 const char *label
, const char *peer_str
,
676 const char *local_str
, const char *ifname
,
681 struct bfd_session
*bs
= NULL
;
682 struct peer_label
*pl
;
683 struct bfd_peer_cfg bpc
;
684 struct sockaddr_any psa
, lsa
, *lsap
;
687 /* Look up the BFD peer. */
693 strtosa(peer_str
, &psa
);
695 strtosa(local_str
, &lsa
);
701 mhop
= argv_find(argv
, argc
, "multihop", &idx
);
703 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
704 errormsg
, sizeof(errormsg
))
706 vty_out(vty
, "%% Invalid peer configuration: %s\n",
711 bs
= bs_peer_find(&bpc
);
714 /* Find peer data. */
716 vty_out(vty
, "%% Unable to find 'peer %s",
717 label
? label
: peer_str
);
719 vty_out(vty
, " interface %s", ifname
);
721 vty_out(vty
, " local-address %s", local_str
);
723 vty_out(vty
, " vrf %s", vrfname
);
736 DEFPY(bfd_show_peers
, bfd_show_peers_cmd
, "show bfd peers [json]",
738 "Bidirection Forwarding Detection\n"
739 "BFD peers status\n" JSON_STR
)
741 _display_all_peers(vty
, use_json(argc
, argv
));
746 DEFPY(bfd_show_peer
, bfd_show_peer_cmd
,
747 "show bfd peer <WORD$label|<A.B.C.D|X:X::X:X>$peer [{multihop|local-address <A.B.C.D|X:X::X:X>$local|interface IFNAME$ifname|vrf NAME$vrfname}]> [json]",
749 "Bidirection Forwarding Detection\n"
751 "Peer label\n" PEER_IPV4_STR PEER_IPV6_STR MHOP_STR LOCAL_STR
752 LOCAL_IPV4_STR LOCAL_IPV6_STR INTERFACE_STR LOCAL_INTF_STR VRF_STR
753 VRF_NAME_STR JSON_STR
)
755 struct bfd_session
*bs
;
757 /* Look up the BFD peer. */
758 bs
= _find_peer_or_error(vty
, argc
, argv
, label
, peer_str
, local_str
,
761 return CMD_WARNING_CONFIG_FAILED
;
763 if (use_json(argc
, argv
)) {
764 _display_peer_json(vty
, bs
);
766 vty_out(vty
, "BFD Peer:\n");
767 _display_peer(vty
, bs
);
773 DEFPY(bfd_show_peer_counters
, bfd_show_peer_counters_cmd
,
774 "show bfd peer <WORD$label|<A.B.C.D|X:X::X:X>$peer [{multihop|local-address <A.B.C.D|X:X::X:X>$local|interface IFNAME$ifname|vrf NAME$vrfname}]> counters [json]",
776 "Bidirection Forwarding Detection\n"
789 "Show BFD peer counters information\n"
792 struct bfd_session
*bs
;
794 /* Look up the BFD peer. */
795 bs
= _find_peer_or_error(vty
, argc
, argv
, label
, peer_str
, local_str
,
798 return CMD_WARNING_CONFIG_FAILED
;
800 if (use_json(argc
, argv
))
801 _display_peer_counters_json(vty
, bs
);
803 _display_peer_counter(vty
, bs
);
808 DEFPY(bfd_show_peers_counters
, bfd_show_peers_counters_cmd
,
809 "show bfd peers counters [json]",
811 "Bidirection Forwarding Detection\n"
813 "Show BFD peer counters information\n"
816 _display_peers_counter(vty
, use_json(argc
, argv
));
823 * Function definitions.
827 * Configuration rules:
830 * peer + (optional vxlan or interface name)
833 * peer + local + (optional vrf)
835 * Anything else is misconfiguration.
837 static int bfd_configure_peer(struct bfd_peer_cfg
*bpc
, bool mhop
,
838 const struct sockaddr_any
*peer
,
839 const struct sockaddr_any
*local
,
840 const char *ifname
, const char *vrfname
,
841 char *ebuf
, size_t ebuflen
)
843 memset(bpc
, 0, sizeof(*bpc
));
846 bpc
->bpc_shutdown
= true;
847 bpc
->bpc_detectmultiplier
= BPC_DEF_DETECTMULTIPLIER
;
848 bpc
->bpc_recvinterval
= BPC_DEF_RECEIVEINTERVAL
;
849 bpc
->bpc_txinterval
= BPC_DEF_TRANSMITINTERVAL
;
850 bpc
->bpc_echointerval
= BPC_DEF_ECHOINTERVAL
;
851 bpc
->bpc_lastevent
= monotime(NULL
);
853 /* Safety check: when no error buf is provided len must be zero. */
857 /* Peer is always mandatory. */
859 snprintf(ebuf
, ebuflen
, "peer must not be empty");
863 /* Validate address families. */
864 if (peer
->sa_sin
.sin_family
== AF_INET
) {
865 if (local
&& local
->sa_sin
.sin_family
!= AF_INET
) {
866 snprintf(ebuf
, ebuflen
,
867 "local is IPv6, but peer is IPv4");
871 bpc
->bpc_ipv4
= true;
872 } else if (peer
->sa_sin
.sin_family
== AF_INET6
) {
873 if (local
&& local
->sa_sin
.sin_family
!= AF_INET6
) {
874 snprintf(ebuf
, ebuflen
,
875 "local is IPv4, but peer is IPv6");
879 bpc
->bpc_ipv4
= false;
881 snprintf(ebuf
, ebuflen
, "invalid peer address family");
885 /* Copy local and/or peer addresses. */
887 bpc
->bpc_local
= *local
;
890 bpc
->bpc_peer
= *peer
;
892 /* Peer configuration is mandatory. */
893 snprintf(ebuf
, ebuflen
, "no peer configured");
897 bpc
->bpc_mhop
= mhop
;
900 /* Handle VxLAN configuration. */
902 if (vxlan
> ((1 << 24) - 1)) {
903 snprintf(ebuf
, ebuflen
, "invalid VxLAN %d", vxlan
);
907 snprintf(ebuf
, ebuflen
,
908 "multihop doesn't accept VxLAN");
912 bpc
->bpc_vxlan
= vxlan
;
916 /* Handle interface specification configuration. */
919 snprintf(ebuf
, ebuflen
,
920 "multihop doesn't accept interface names");
924 bpc
->bpc_has_localif
= true;
925 if (strlcpy(bpc
->bpc_localif
, ifname
, sizeof(bpc
->bpc_localif
))
926 > sizeof(bpc
->bpc_localif
)) {
927 snprintf(ebuf
, ebuflen
, "interface name too long");
932 /* Handle VRF configuration. */
934 bpc
->bpc_has_vrfname
= true;
935 if (strlcpy(bpc
->bpc_vrfname
, vrfname
, sizeof(bpc
->bpc_vrfname
))
936 > sizeof(bpc
->bpc_vrfname
)) {
937 snprintf(ebuf
, ebuflen
, "vrf name too long");
944 static int bfdd_write_config(struct vty
*vty
)
946 vty_out(vty
, "bfd\n");
951 static void _bfdd_peer_write_config(struct hash_backet
*hb
, void *arg
)
953 struct vty
*vty
= arg
;
954 struct bfd_session
*bs
= hb
->data
;
956 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
957 vty_out(vty
, " peer %s", satostr(&bs
->mhop
.peer
));
958 vty_out(vty
, " multihop");
959 vty_out(vty
, " local-address %s", satostr(&bs
->mhop
.local
));
960 if (bs
->mhop
.vrf_name
[0])
961 vty_out(vty
, " vrf %s", bs
->mhop
.vrf_name
);
964 vty_out(vty
, " peer %s", satostr(&bs
->shop
.peer
));
965 if (bs
->local_address
.sa_sin
.sin_family
!= AF_UNSPEC
)
966 vty_out(vty
, " local-address %s",
967 satostr(&bs
->local_address
));
968 if (bs
->shop
.port_name
[0])
969 vty_out(vty
, " interface %s", bs
->shop
.port_name
);
973 if (bs
->detect_mult
!= BPC_DEF_DETECTMULTIPLIER
)
974 vty_out(vty
, " detect-multiplier %d\n", bs
->detect_mult
);
975 if (bs
->timers
.required_min_rx
!= (BPC_DEF_RECEIVEINTERVAL
* 1000))
976 vty_out(vty
, " receive-interval %" PRIu32
"\n",
977 bs
->timers
.required_min_rx
/ 1000);
978 if (bs
->up_min_tx
!= (BPC_DEF_TRANSMITINTERVAL
* 1000))
979 vty_out(vty
, " transmit-interval %" PRIu32
"\n",
980 bs
->up_min_tx
/ 1000);
981 if (bs
->timers
.required_min_echo
!= (BPC_DEF_ECHOINTERVAL
* 1000))
982 vty_out(vty
, " echo-interval %" PRIu32
"\n",
983 bs
->timers
.required_min_echo
/ 1000);
985 vty_out(vty
, " label %s\n", bs
->pl
->pl_label
);
986 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
987 vty_out(vty
, " echo-mode\n");
989 vty_out(vty
, " %sshutdown\n",
990 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
) ? "" : "no ");
992 vty_out(vty
, " !\n");
995 static int bfdd_peer_write_config(struct vty
*vty
)
997 bfd_id_iterate(_bfdd_peer_write_config
, vty
);
1001 struct cmd_node bfd_node
= {
1007 struct cmd_node bfd_peer_node
= {
1009 "%s(config-bfd-peer)# ",
1013 void bfdd_vty_init(void)
1015 install_element(ENABLE_NODE
, &bfd_show_peers_counters_cmd
);
1016 install_element(ENABLE_NODE
, &bfd_show_peer_counters_cmd
);
1017 install_element(ENABLE_NODE
, &bfd_show_peers_cmd
);
1018 install_element(ENABLE_NODE
, &bfd_show_peer_cmd
);
1019 install_element(CONFIG_NODE
, &bfd_enter_cmd
);
1021 /* Install BFD node and commands. */
1022 install_node(&bfd_node
, bfdd_write_config
);
1023 install_default(BFD_NODE
);
1024 install_element(BFD_NODE
, &bfd_peer_enter_cmd
);
1025 install_element(BFD_NODE
, &bfd_no_peer_cmd
);
1027 /* Install BFD peer node. */
1028 install_node(&bfd_peer_node
, bfdd_peer_write_config
);
1029 install_default(BFD_PEER_NODE
);
1030 install_element(BFD_PEER_NODE
, &bfd_peer_detectmultiplier_cmd
);
1031 install_element(BFD_PEER_NODE
, &bfd_peer_recvinterval_cmd
);
1032 install_element(BFD_PEER_NODE
, &bfd_peer_txinterval_cmd
);
1033 install_element(BFD_PEER_NODE
, &bfd_peer_echointerval_cmd
);
1034 install_element(BFD_PEER_NODE
, &bfd_peer_shutdown_cmd
);
1035 install_element(BFD_PEER_NODE
, &bfd_peer_echo_cmd
);
1036 install_element(BFD_PEER_NODE
, &bfd_peer_label_cmd
);