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 vty
*vty
, struct bfd_session
*bs
);
55 static void _bfdd_peer_write_config_iter(struct hash_bucket
*hb
, void *arg
);
56 static int bfd_configure_peer(struct bfd_peer_cfg
*bpc
, bool mhop
,
57 const struct sockaddr_any
*peer
,
58 const struct sockaddr_any
*local
,
59 const char *ifname
, const char *vrfname
,
60 char *ebuf
, size_t ebuflen
);
62 static void _display_peer_header(struct vty
*vty
, struct bfd_session
*bs
);
63 static struct json_object
*__display_peer_json(struct bfd_session
*bs
);
64 static struct json_object
*_peer_json_header(struct bfd_session
*bs
);
65 static void _display_peer_json(struct vty
*vty
, struct bfd_session
*bs
);
66 static void _display_peer(struct vty
*vty
, struct bfd_session
*bs
);
67 static void _display_all_peers(struct vty
*vty
, bool use_json
);
68 static void _display_peer_iter(struct hash_bucket
*hb
, void *arg
);
69 static void _display_peer_json_iter(struct hash_bucket
*hb
, void *arg
);
70 static void _display_peer_counter(struct vty
*vty
, struct bfd_session
*bs
);
71 static struct json_object
*__display_peer_counters_json(struct bfd_session
*bs
);
72 static void _display_peer_counters_json(struct vty
*vty
, struct bfd_session
*bs
);
73 static void _display_peer_counter_iter(struct hash_bucket
*hb
, void *arg
);
74 static void _display_peer_counter_json_iter(struct hash_bucket
*hb
, void *arg
);
75 static void _display_peers_counter(struct vty
*vty
, bool use_json
);
76 static struct bfd_session
*
77 _find_peer_or_error(struct vty
*vty
, int argc
, struct cmd_token
**argv
,
78 const char *label
, const char *peer_str
,
79 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
;
133 if (vrfname
&& !mhop
) {
134 vty_out(vty
, "%% VRF only applies with multihop.\n");
135 return CMD_WARNING_CONFIG_FAILED
;
140 strtosa(local
, &lsa
);
145 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
146 errormsg
, sizeof(errormsg
))
148 vty_out(vty
, "%% Invalid peer configuration: %s\n", errormsg
);
149 return CMD_WARNING_CONFIG_FAILED
;
152 bs
= bs_peer_find(&bpc
);
154 bs
= ptm_bfd_sess_new(&bpc
);
156 vty_out(vty
, "%% Failed to add peer.\n");
157 return CMD_WARNING_CONFIG_FAILED
;
161 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
)) {
163 vty_out(vty
, "%% session peer is now configurable via bfd daemon.\n");
164 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
);
167 VTY_PUSH_CONTEXT(BFD_PEER_NODE
, bs
);
172 DEFPY(bfd_peer_detectmultiplier
, bfd_peer_detectmultiplier_cmd
,
173 "detect-multiplier (2-255)$multiplier",
174 "Configure peer detection multiplier\n"
175 "Configure peer detection multiplier value\n")
177 struct bfd_session
*bs
;
179 bs
= VTY_GET_CONTEXT(bfd_session
);
180 if (bs
->detect_mult
== multiplier
)
183 bs
->detect_mult
= multiplier
;
188 DEFPY(bfd_peer_recvinterval
, bfd_peer_recvinterval_cmd
,
189 "receive-interval (10-60000)$interval",
190 "Configure peer receive interval\n"
191 "Configure peer receive interval value in milliseconds\n")
193 struct bfd_session
*bs
;
195 bs
= VTY_GET_CONTEXT(bfd_session
);
196 if (bs
->timers
.required_min_rx
== (uint32_t)(interval
* 1000))
199 bs
->timers
.required_min_rx
= interval
* 1000;
205 DEFPY(bfd_peer_txinterval
, bfd_peer_txinterval_cmd
,
206 "transmit-interval (10-60000)$interval",
207 "Configure peer transmit interval\n"
208 "Configure peer transmit interval value in milliseconds\n")
210 struct bfd_session
*bs
;
212 bs
= VTY_GET_CONTEXT(bfd_session
);
213 if (bs
->timers
.desired_min_tx
== (uint32_t)(interval
* 1000))
216 bs
->timers
.desired_min_tx
= interval
* 1000;
222 DEFPY(bfd_peer_echointerval
, bfd_peer_echointerval_cmd
,
223 "echo-interval (10-60000)$interval",
224 "Configure peer echo interval\n"
225 "Configure peer echo interval value in milliseconds\n")
227 struct bfd_session
*bs
;
229 bs
= VTY_GET_CONTEXT(bfd_session
);
230 if (bs
->timers
.required_min_echo
== (uint32_t)(interval
* 1000))
233 bs
->timers
.required_min_echo
= interval
* 1000;
238 DEFPY(bfd_peer_shutdown
, bfd_peer_shutdown_cmd
, "[no] shutdown",
239 NO_STR
"Disable BFD peer")
241 struct bfd_session
*bs
;
243 bs
= VTY_GET_CONTEXT(bfd_session
);
245 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
248 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
250 /* Change and notify state change. */
251 bs
->ses_state
= PTM_BFD_DOWN
;
254 /* Enable all timers. */
255 bfd_recvtimer_update(bs
);
256 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
257 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
)) {
258 bfd_echo_recvtimer_update(bs
);
259 bfd_echo_xmttimer_update(bs
, bs
->echo_xmt_TO
);
262 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
265 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
267 /* Disable all events. */
268 bfd_recvtimer_delete(bs
);
269 bfd_echo_recvtimer_delete(bs
);
270 bfd_xmttimer_delete(bs
);
271 bfd_echo_xmttimer_delete(bs
);
273 /* Change and notify state change. */
274 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
283 DEFPY(bfd_peer_echo
, bfd_peer_echo_cmd
, "[no] echo-mode",
284 NO_STR
"Configure echo mode\n")
286 struct bfd_session
*bs
;
288 bs
= VTY_GET_CONTEXT(bfd_session
);
290 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
293 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
294 ptm_bfd_echo_stop(bs
);
296 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
299 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
300 /* Apply setting immediately. */
301 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
302 bs_echo_timer_handler(bs
);
308 DEFPY(bfd_peer_label
, bfd_peer_label_cmd
, "label WORD$label",
309 "Register peer label\n"
310 "Register peer label identification\n")
312 struct bfd_session
*bs
;
314 /* Validate label length. */
315 if (strlen(label
) >= MAXNAMELEN
) {
316 vty_out(vty
, "%% Label name is too long\n");
317 return CMD_WARNING_CONFIG_FAILED
;
320 bs
= VTY_GET_CONTEXT(bfd_session
);
321 if (bfd_session_update_label(bs
, label
) == -1) {
322 vty_out(vty
, "%% Failed to update peer label.\n");
323 return CMD_WARNING_CONFIG_FAILED
;
329 DEFPY(bfd_no_peer
, bfd_no_peer_cmd
,
330 "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}]",
332 PEER_STR PEER_IPV4_STR PEER_IPV6_STR
334 LOCAL_STR LOCAL_IPV4_STR LOCAL_IPV6_STR
337 VRF_STR VRF_NAME_STR
)
341 struct bfd_peer_cfg bpc
;
342 struct sockaddr_any psa
, lsa
, *lsap
;
345 strtosa(peer_str
, &psa
);
347 strtosa(local_str
, &lsa
);
354 mhop
= argv_find(argv
, argc
, "multihop", &idx
);
356 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
357 errormsg
, sizeof(errormsg
))
359 vty_out(vty
, "%% Invalid peer configuration: %s\n", errormsg
);
360 return CMD_WARNING_CONFIG_FAILED
;
363 if (ptm_bfd_ses_del(&bpc
) != 0) {
364 vty_out(vty
, "%% Failed to remove peer.\n");
365 return CMD_WARNING_CONFIG_FAILED
;
373 * Show commands helper functions
375 static void _display_peer_header(struct vty
*vty
, struct bfd_session
*bs
)
377 char addr_buf
[INET6_ADDRSTRLEN
];
379 vty_out(vty
, "\tpeer %s",
380 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
383 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
))
384 vty_out(vty
, " multihop");
386 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
)))
387 vty_out(vty
, " local-address %s",
388 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
391 if (bs
->key
.vrfname
[0])
392 vty_out(vty
, " vrf %s", bs
->key
.vrfname
);
393 if (bs
->key
.ifname
[0])
394 vty_out(vty
, " interface %s", bs
->key
.ifname
);
398 vty_out(vty
, "\t\tlabel: %s\n", bs
->pl
->pl_label
);
401 static void _display_peer(struct vty
*vty
, struct bfd_session
*bs
)
406 _display_peer_header(vty
, bs
);
408 vty_out(vty
, "\t\tID: %u\n", bs
->discrs
.my_discr
);
409 vty_out(vty
, "\t\tRemote ID: %u\n", bs
->discrs
.remote_discr
);
411 vty_out(vty
, "\t\tStatus: ");
412 switch (bs
->ses_state
) {
413 case PTM_BFD_ADM_DOWN
:
414 vty_out(vty
, "shutdown\n");
417 vty_out(vty
, "down\n");
419 now
= monotime(NULL
);
420 integer2timestr(now
- bs
->downtime
.tv_sec
, buf
, sizeof(buf
));
421 vty_out(vty
, "\t\tDowntime: %s\n", buf
);
424 vty_out(vty
, "init\n");
427 vty_out(vty
, "up\n");
429 now
= monotime(NULL
);
430 integer2timestr(now
- bs
->uptime
.tv_sec
, buf
, sizeof(buf
));
431 vty_out(vty
, "\t\tUptime: %s\n", buf
);
435 vty_out(vty
, "unknown\n");
439 vty_out(vty
, "\t\tDiagnostics: %s\n", diag2str(bs
->local_diag
));
440 vty_out(vty
, "\t\tRemote diagnostics: %s\n", diag2str(bs
->remote_diag
));
442 vty_out(vty
, "\t\tLocal timers:\n");
443 vty_out(vty
, "\t\t\tReceive interval: %" PRIu32
"ms\n",
444 bs
->timers
.required_min_rx
/ 1000);
445 vty_out(vty
, "\t\t\tTransmission interval: %" PRIu32
"ms\n",
446 bs
->timers
.desired_min_tx
/ 1000);
447 vty_out(vty
, "\t\t\tEcho transmission interval: %" PRIu32
"ms\n",
448 bs
->timers
.required_min_echo
/ 1000);
450 vty_out(vty
, "\t\tRemote timers:\n");
451 vty_out(vty
, "\t\t\tReceive interval: %" PRIu32
"ms\n",
452 bs
->remote_timers
.required_min_rx
/ 1000);
453 vty_out(vty
, "\t\t\tTransmission interval: %" PRIu32
"ms\n",
454 bs
->remote_timers
.desired_min_tx
/ 1000);
455 vty_out(vty
, "\t\t\tEcho transmission interval: %" PRIu32
"ms\n",
456 bs
->remote_timers
.required_min_echo
/ 1000);
461 static struct json_object
*_peer_json_header(struct bfd_session
*bs
)
463 struct json_object
*jo
= json_object_new_object();
464 char addr_buf
[INET6_ADDRSTRLEN
];
467 json_object_boolean_true_add(jo
, "multihop");
469 json_object_boolean_false_add(jo
, "multihop");
471 json_object_string_add(jo
, "peer",
472 inet_ntop(bs
->key
.family
, &bs
->key
.peer
,
473 addr_buf
, sizeof(addr_buf
)));
474 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
)))
475 json_object_string_add(jo
, "local",
476 inet_ntop(bs
->key
.family
, &bs
->key
.local
,
477 addr_buf
, sizeof(addr_buf
)));
479 if (bs
->key
.vrfname
[0])
480 json_object_string_add(jo
, "vrf", bs
->key
.vrfname
);
481 if (bs
->key
.ifname
[0])
482 json_object_string_add(jo
, "interface", bs
->key
.ifname
);
485 json_object_string_add(jo
, "label", bs
->pl
->pl_label
);
490 static struct json_object
*__display_peer_json(struct bfd_session
*bs
)
492 struct json_object
*jo
= _peer_json_header(bs
);
494 json_object_int_add(jo
, "id", bs
->discrs
.my_discr
);
495 json_object_int_add(jo
, "remote-id", bs
->discrs
.remote_discr
);
497 switch (bs
->ses_state
) {
498 case PTM_BFD_ADM_DOWN
:
499 json_object_string_add(jo
, "status", "shutdown");
502 json_object_string_add(jo
, "status", "down");
503 json_object_int_add(jo
, "downtime",
504 monotime(NULL
) - bs
->downtime
.tv_sec
);
507 json_object_string_add(jo
, "status", "init");
510 json_object_string_add(jo
, "status", "up");
511 json_object_int_add(jo
, "uptime",
512 monotime(NULL
) - bs
->uptime
.tv_sec
);
516 json_object_string_add(jo
, "status", "unknown");
520 json_object_string_add(jo
, "diagnostic", diag2str(bs
->local_diag
));
521 json_object_string_add(jo
, "remote-diagnostic",
522 diag2str(bs
->remote_diag
));
524 json_object_int_add(jo
, "receive-interval",
525 bs
->timers
.required_min_rx
/ 1000);
526 json_object_int_add(jo
, "transmit-interval",
527 bs
->timers
.desired_min_tx
/ 1000);
528 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
529 json_object_int_add(jo
, "echo-interval",
530 bs
->timers
.required_min_echo
/ 1000);
532 json_object_int_add(jo
, "echo-interval", 0);
534 json_object_int_add(jo
, "remote-receive-interval",
535 bs
->remote_timers
.required_min_rx
/ 1000);
536 json_object_int_add(jo
, "remote-transmit-interval",
537 bs
->remote_timers
.desired_min_tx
/ 1000);
538 json_object_int_add(jo
, "remote-echo-interval",
539 bs
->remote_timers
.required_min_echo
/ 1000);
544 static void _display_peer_json(struct vty
*vty
, struct bfd_session
*bs
)
546 struct json_object
*jo
= __display_peer_json(bs
);
548 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
549 json_object_free(jo
);
552 static void _display_peer_iter(struct hash_bucket
*hb
, void *arg
)
554 struct vty
*vty
= arg
;
555 struct bfd_session
*bs
= hb
->data
;
557 _display_peer(vty
, bs
);
560 static void _display_peer_json_iter(struct hash_bucket
*hb
, void *arg
)
562 struct json_object
*jo
= arg
, *jon
= NULL
;
563 struct bfd_session
*bs
= hb
->data
;
565 jon
= __display_peer_json(bs
);
567 log_warning("%s: not enough memory", __func__
);
571 json_object_array_add(jo
, jon
);
574 static void _display_all_peers(struct vty
*vty
, bool use_json
)
576 struct json_object
*jo
;
579 vty_out(vty
, "BFD Peers:\n");
580 bfd_id_iterate(_display_peer_iter
, vty
);
584 jo
= json_object_new_array();
585 bfd_id_iterate(_display_peer_json_iter
, jo
);
587 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
588 json_object_free(jo
);
591 static void _display_peer_counter(struct vty
*vty
, struct bfd_session
*bs
)
593 _display_peer_header(vty
, bs
);
595 vty_out(vty
, "\t\tControl packet input: %" PRIu64
" packets\n",
596 bs
->stats
.rx_ctrl_pkt
);
597 vty_out(vty
, "\t\tControl packet output: %" PRIu64
" packets\n",
598 bs
->stats
.tx_ctrl_pkt
);
599 vty_out(vty
, "\t\tEcho packet input: %" PRIu64
" packets\n",
600 bs
->stats
.rx_echo_pkt
);
601 vty_out(vty
, "\t\tEcho packet output: %" PRIu64
" packets\n",
602 bs
->stats
.tx_echo_pkt
);
603 vty_out(vty
, "\t\tSession up events: %" PRIu64
"\n",
604 bs
->stats
.session_up
);
605 vty_out(vty
, "\t\tSession down events: %" PRIu64
"\n",
606 bs
->stats
.session_down
);
607 vty_out(vty
, "\t\tZebra notifications: %" PRIu64
"\n",
608 bs
->stats
.znotification
);
612 static struct json_object
*__display_peer_counters_json(struct bfd_session
*bs
)
614 struct json_object
*jo
= _peer_json_header(bs
);
616 json_object_int_add(jo
, "control-packet-input", bs
->stats
.rx_ctrl_pkt
);
617 json_object_int_add(jo
, "control-packet-output", bs
->stats
.tx_ctrl_pkt
);
618 json_object_int_add(jo
, "echo-packet-input", bs
->stats
.rx_echo_pkt
);
619 json_object_int_add(jo
, "echo-packet-output", bs
->stats
.tx_echo_pkt
);
620 json_object_int_add(jo
, "session-up", bs
->stats
.session_up
);
621 json_object_int_add(jo
, "session-down", bs
->stats
.session_down
);
622 json_object_int_add(jo
, "zebra-notifications", bs
->stats
.znotification
);
627 static void _display_peer_counters_json(struct vty
*vty
, struct bfd_session
*bs
)
629 struct json_object
*jo
= __display_peer_counters_json(bs
);
631 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
632 json_object_free(jo
);
635 static void _display_peer_counter_iter(struct hash_bucket
*hb
, void *arg
)
637 struct vty
*vty
= arg
;
638 struct bfd_session
*bs
= hb
->data
;
640 _display_peer_counter(vty
, bs
);
643 static void _display_peer_counter_json_iter(struct hash_bucket
*hb
, void *arg
)
645 struct json_object
*jo
= arg
, *jon
= NULL
;
646 struct bfd_session
*bs
= hb
->data
;
648 jon
= __display_peer_counters_json(bs
);
650 log_warning("%s: not enough memory", __func__
);
654 json_object_array_add(jo
, jon
);
657 static void _display_peers_counter(struct vty
*vty
, bool use_json
)
659 struct json_object
*jo
;
662 vty_out(vty
, "BFD Peers:\n");
663 bfd_id_iterate(_display_peer_counter_iter
, vty
);
667 jo
= json_object_new_array();
668 bfd_id_iterate(_display_peer_counter_json_iter
, jo
);
670 vty_out(vty
, "%s\n", json_object_to_json_string_ext(jo
, 0));
671 json_object_free(jo
);
674 static struct bfd_session
*
675 _find_peer_or_error(struct vty
*vty
, int argc
, struct cmd_token
**argv
,
676 const char *label
, const char *peer_str
,
677 const char *local_str
, const char *ifname
,
682 struct bfd_session
*bs
= NULL
;
683 struct peer_label
*pl
;
684 struct bfd_peer_cfg bpc
;
685 struct sockaddr_any psa
, lsa
, *lsap
;
688 /* Look up the BFD peer. */
694 strtosa(peer_str
, &psa
);
696 strtosa(local_str
, &lsa
);
702 mhop
= argv_find(argv
, argc
, "multihop", &idx
);
704 if (bfd_configure_peer(&bpc
, mhop
, &psa
, lsap
, ifname
, vrfname
,
705 errormsg
, sizeof(errormsg
))
707 vty_out(vty
, "%% Invalid peer configuration: %s\n",
712 bs
= bs_peer_find(&bpc
);
715 /* Find peer data. */
717 vty_out(vty
, "%% Unable to find 'peer %s",
718 label
? label
: peer_str
);
720 vty_out(vty
, " interface %s", ifname
);
722 vty_out(vty
, " local-address %s", local_str
);
724 vty_out(vty
, " vrf %s", vrfname
);
737 DEFPY(bfd_show_peers
, bfd_show_peers_cmd
, "show bfd peers [json]",
739 "Bidirection Forwarding Detection\n"
740 "BFD peers status\n" JSON_STR
)
742 _display_all_peers(vty
, use_json(argc
, argv
));
747 DEFPY(bfd_show_peer
, bfd_show_peer_cmd
,
748 "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]",
750 "Bidirection Forwarding Detection\n"
752 "Peer label\n" PEER_IPV4_STR PEER_IPV6_STR MHOP_STR LOCAL_STR
753 LOCAL_IPV4_STR LOCAL_IPV6_STR INTERFACE_STR LOCAL_INTF_STR VRF_STR
754 VRF_NAME_STR JSON_STR
)
756 struct bfd_session
*bs
;
758 /* Look up the BFD peer. */
759 bs
= _find_peer_or_error(vty
, argc
, argv
, label
, peer_str
, local_str
,
762 return CMD_WARNING_CONFIG_FAILED
;
764 if (use_json(argc
, argv
)) {
765 _display_peer_json(vty
, bs
);
767 vty_out(vty
, "BFD Peer:\n");
768 _display_peer(vty
, bs
);
774 DEFPY(bfd_show_peer_counters
, bfd_show_peer_counters_cmd
,
775 "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]",
777 "Bidirection Forwarding Detection\n"
790 "Show BFD peer counters information\n"
793 struct bfd_session
*bs
;
795 /* Look up the BFD peer. */
796 bs
= _find_peer_or_error(vty
, argc
, argv
, label
, peer_str
, local_str
,
799 return CMD_WARNING_CONFIG_FAILED
;
801 if (use_json(argc
, argv
))
802 _display_peer_counters_json(vty
, bs
);
804 _display_peer_counter(vty
, bs
);
809 DEFPY(bfd_show_peers_counters
, bfd_show_peers_counters_cmd
,
810 "show bfd peers counters [json]",
812 "Bidirection Forwarding Detection\n"
814 "Show BFD peer counters information\n"
817 _display_peers_counter(vty
, use_json(argc
, argv
));
824 * Function definitions.
828 * Configuration rules:
831 * peer + (interface name)
834 * peer + local + (optional vrf)
836 * Anything else is misconfiguration.
838 static int bfd_configure_peer(struct bfd_peer_cfg
*bpc
, bool mhop
,
839 const struct sockaddr_any
*peer
,
840 const struct sockaddr_any
*local
,
841 const char *ifname
, const char *vrfname
,
842 char *ebuf
, size_t ebuflen
)
844 memset(bpc
, 0, sizeof(*bpc
));
847 bpc
->bpc_shutdown
= true;
848 bpc
->bpc_detectmultiplier
= BPC_DEF_DETECTMULTIPLIER
;
849 bpc
->bpc_recvinterval
= BPC_DEF_RECEIVEINTERVAL
;
850 bpc
->bpc_txinterval
= BPC_DEF_TRANSMITINTERVAL
;
851 bpc
->bpc_echointerval
= BPC_DEF_ECHOINTERVAL
;
852 bpc
->bpc_lastevent
= monotime(NULL
);
854 /* Safety check: when no error buf is provided len must be zero. */
858 /* Peer is always mandatory. */
860 snprintf(ebuf
, ebuflen
, "peer must not be empty");
864 /* Validate address families. */
865 if (peer
->sa_sin
.sin_family
== AF_INET
) {
866 if (local
&& local
->sa_sin
.sin_family
!= AF_INET
) {
867 snprintf(ebuf
, ebuflen
,
868 "local is IPv6, but peer is IPv4");
872 bpc
->bpc_ipv4
= true;
873 } else if (peer
->sa_sin
.sin_family
== AF_INET6
) {
874 if (local
&& local
->sa_sin
.sin_family
!= AF_INET6
) {
875 snprintf(ebuf
, ebuflen
,
876 "local is IPv4, but peer is IPv6");
880 bpc
->bpc_ipv4
= false;
882 snprintf(ebuf
, ebuflen
, "invalid peer address family");
886 /* Copy local and/or peer addresses. */
888 bpc
->bpc_local
= *local
;
890 bpc
->bpc_peer
= *peer
;
891 bpc
->bpc_mhop
= mhop
;
893 /* Handle interface specification configuration. */
896 snprintf(ebuf
, ebuflen
,
897 "multihop doesn't accept interface names");
901 bpc
->bpc_has_localif
= true;
902 if (strlcpy(bpc
->bpc_localif
, ifname
, sizeof(bpc
->bpc_localif
))
903 > sizeof(bpc
->bpc_localif
)) {
904 snprintf(ebuf
, ebuflen
, "interface name too long");
909 /* Handle VRF configuration. */
911 bpc
->bpc_has_vrfname
= true;
912 if (strlcpy(bpc
->bpc_vrfname
, vrfname
, sizeof(bpc
->bpc_vrfname
))
913 > sizeof(bpc
->bpc_vrfname
)) {
914 snprintf(ebuf
, ebuflen
, "vrf name too long");
921 static int bfdd_write_config(struct vty
*vty
)
923 vty_out(vty
, "bfd\n");
928 static void _bfdd_peer_write_config(struct vty
*vty
, struct bfd_session
*bs
)
930 char addr_buf
[INET6_ADDRSTRLEN
];
932 vty_out(vty
, " peer %s",
933 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
937 vty_out(vty
, " multihop");
939 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
)))
940 vty_out(vty
, " local-address %s",
941 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
944 if (bs
->key
.vrfname
[0])
945 vty_out(vty
, " vrf %s", bs
->key
.vrfname
);
946 if (bs
->key
.ifname
[0])
947 vty_out(vty
, " interface %s", bs
->key
.ifname
);
952 " ! vrf, interface or local-address doesn't exist\n");
954 if (bs
->detect_mult
!= BPC_DEF_DETECTMULTIPLIER
)
955 vty_out(vty
, " detect-multiplier %d\n", bs
->detect_mult
);
956 if (bs
->timers
.required_min_rx
!= (BPC_DEF_RECEIVEINTERVAL
* 1000))
957 vty_out(vty
, " receive-interval %" PRIu32
"\n",
958 bs
->timers
.required_min_rx
/ 1000);
959 if (bs
->timers
.desired_min_tx
!= (BPC_DEF_TRANSMITINTERVAL
* 1000))
960 vty_out(vty
, " transmit-interval %" PRIu32
"\n",
961 bs
->timers
.desired_min_tx
/ 1000);
962 if (bs
->timers
.required_min_echo
!= (BPC_DEF_ECHOINTERVAL
* 1000))
963 vty_out(vty
, " echo-interval %" PRIu32
"\n",
964 bs
->timers
.required_min_echo
/ 1000);
966 vty_out(vty
, " label %s\n", bs
->pl
->pl_label
);
967 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
968 vty_out(vty
, " echo-mode\n");
970 vty_out(vty
, " %sshutdown\n",
971 BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
) ? "" : "no ");
973 vty_out(vty
, " !\n");
976 DEFUN_NOSH(show_debugging_bfd
,
977 show_debugging_bfd_cmd
,
978 "show debugging [bfd]",
983 vty_out(vty
, "BFD debugging status:\n");
988 static void _bfdd_peer_write_config_iter(struct hash_bucket
*hb
, void *arg
)
990 struct vty
*vty
= arg
;
991 struct bfd_session
*bs
= hb
->data
;
993 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
))
996 _bfdd_peer_write_config(vty
, bs
);
999 static int bfdd_peer_write_config(struct vty
*vty
)
1001 bfd_id_iterate(_bfdd_peer_write_config_iter
, vty
);
1006 struct cmd_node bfd_node
= {
1012 struct cmd_node bfd_peer_node
= {
1014 "%s(config-bfd-peer)# ",
1018 void bfdd_vty_init(void)
1020 install_element(ENABLE_NODE
, &bfd_show_peers_counters_cmd
);
1021 install_element(ENABLE_NODE
, &bfd_show_peer_counters_cmd
);
1022 install_element(ENABLE_NODE
, &bfd_show_peers_cmd
);
1023 install_element(ENABLE_NODE
, &bfd_show_peer_cmd
);
1024 install_element(CONFIG_NODE
, &bfd_enter_cmd
);
1025 install_element(ENABLE_NODE
, &show_debugging_bfd_cmd
);
1027 /* Install BFD node and commands. */
1028 install_node(&bfd_node
, bfdd_write_config
);
1029 install_default(BFD_NODE
);
1030 install_element(BFD_NODE
, &bfd_peer_enter_cmd
);
1031 install_element(BFD_NODE
, &bfd_no_peer_cmd
);
1033 /* Install BFD peer node. */
1034 install_node(&bfd_peer_node
, bfdd_peer_write_config
);
1035 install_default(BFD_PEER_NODE
);
1036 install_element(BFD_PEER_NODE
, &bfd_peer_detectmultiplier_cmd
);
1037 install_element(BFD_PEER_NODE
, &bfd_peer_recvinterval_cmd
);
1038 install_element(BFD_PEER_NODE
, &bfd_peer_txinterval_cmd
);
1039 install_element(BFD_PEER_NODE
, &bfd_peer_echointerval_cmd
);
1040 install_element(BFD_PEER_NODE
, &bfd_peer_shutdown_cmd
);
1041 install_element(BFD_PEER_NODE
, &bfd_peer_echo_cmd
);
1042 install_element(BFD_PEER_NODE
, &bfd_peer_label_cmd
);