2 * BFD daemon CLI implementation.
4 * Copyright (C) 2019 Network Device Education Foundation, Inc. ("NetDEF")
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "lib/command.h"
27 #include "lib/northbound_cli.h"
29 #ifndef VTYSH_EXTRACT_PL
30 #include "bfdd/bfdd_cli_clippy.c"
31 #endif /* VTYSH_EXTRACT_PL */
39 #define PEER_STR "Configure peer\n"
40 #define INTERFACE_NAME_STR "Configure interface name to use\n"
41 #define PEER_IPV4_STR "IPv4 peer address\n"
42 #define PEER_IPV6_STR "IPv6 peer address\n"
43 #define MHOP_STR "Configure multihop\n"
44 #define LOCAL_STR "Configure local address\n"
45 #define LOCAL_IPV4_STR "IPv4 local address\n"
46 #define LOCAL_IPV6_STR "IPv6 local address\n"
47 #define LOCAL_INTF_STR "Configure local interface name to use\n"
48 #define VRF_STR "Configure VRF\n"
49 #define VRF_NAME_STR "Configure VRF name\n"
55 bfd_cli_is_single_hop(struct vty
*vty
)
57 return strstr(VTY_CURR_XPATH
, "/single-hop") != NULL
;
61 bfd_cli_is_profile(struct vty
*vty
)
63 return strstr(VTY_CURR_XPATH
, "/bfd/profile") != NULL
;
70 bfd_enter
, bfd_enter_cmd
,
72 "Configure BFD peers\n")
76 nb_cli_enqueue_change(vty
, "/frr-bfdd:bfdd/bfd", NB_OP_CREATE
, NULL
);
77 ret
= nb_cli_apply_changes(vty
, NULL
);
78 if (ret
== CMD_SUCCESS
)
79 VTY_PUSH_XPATH(BFD_NODE
, "/frr-bfdd:bfdd/bfd");
85 bfd_config_reset
, bfd_config_reset_cmd
,
88 "Configure BFD peers\n")
90 nb_cli_enqueue_change(vty
, "/frr-bfdd:bfdd/bfd", NB_OP_DESTROY
, NULL
);
91 return nb_cli_apply_changes(vty
, NULL
);
94 void bfd_cli_show_header(struct vty
*vty
,
95 struct lyd_node
*dnode
__attribute__((__unused__
)),
96 bool show_defaults
__attribute__((__unused__
)))
98 vty_out(vty
, "!\nbfd\n");
101 void bfd_cli_show_header_end(struct vty
*vty
,
102 struct lyd_node
*dnode
__attribute__((__unused__
)))
108 bfd_peer_enter
, bfd_peer_enter_cmd
,
109 "peer <A.B.C.D|X:X::X:X> [{multihop$multihop|local-address <A.B.C.D|X:X::X:X>|interface IFNAME$ifname|vrf NAME}]",
123 char source_str
[INET6_ADDRSTRLEN
+ 32];
124 char xpath
[XPATH_MAXLEN
], xpath_srcaddr
[XPATH_MAXLEN
+ 32];
127 if (!local_address_str
) {
128 vty_out(vty
, "%% local-address is required when using multihop\n");
129 return CMD_WARNING_CONFIG_FAILED
;
131 snprintf(source_str
, sizeof(source_str
), "[source-addr='%s']",
136 slen
= snprintf(xpath
, sizeof(xpath
),
137 "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']",
138 multihop
? "multi-hop" : "single-hop", source_str
,
141 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
142 "[interface='%s']", ifname
);
144 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
147 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']", vrf
);
149 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']",
152 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
153 if (multihop
== NULL
&& local_address_str
!= NULL
) {
154 snprintf(xpath_srcaddr
, sizeof(xpath_srcaddr
),
155 "%s/source-addr", xpath
);
156 nb_cli_enqueue_change(vty
, xpath_srcaddr
, NB_OP_MODIFY
,
160 /* Apply settings immediately. */
161 ret
= nb_cli_apply_changes(vty
, NULL
);
162 if (ret
== CMD_SUCCESS
)
163 VTY_PUSH_XPATH(BFD_PEER_NODE
, xpath
);
169 bfd_no_peer
, bfd_no_peer_cmd
,
170 "no peer <A.B.C.D|X:X::X:X> [{multihop$multihop|local-address <A.B.C.D|X:X::X:X>|interface IFNAME$ifname|vrf NAME}]",
185 char xpath
[XPATH_MAXLEN
];
186 char source_str
[INET6_ADDRSTRLEN
+ 32];
189 snprintf(source_str
, sizeof(source_str
), "[source-addr='%s']",
194 slen
= snprintf(xpath
, sizeof(xpath
),
195 "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']",
196 multihop
? "multi-hop" : "single-hop", source_str
,
199 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
200 "[interface='%s']", ifname
);
202 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
205 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']", vrf
);
207 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']",
210 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
212 /* Apply settings immediatly. */
213 return nb_cli_apply_changes(vty
, NULL
);
216 static void _bfd_cli_show_peer(struct vty
*vty
, struct lyd_node
*dnode
,
217 bool show_defaults
__attribute__((__unused__
)),
220 const char *vrf
= yang_dnode_get_string(dnode
, "./vrf");
221 const char *ifname
= yang_dnode_get_string(dnode
, "./interface");
223 vty_out(vty
, " peer %s",
224 yang_dnode_get_string(dnode
, "./dest-addr"));
227 vty_out(vty
, " multihop");
229 if (yang_dnode_exists(dnode
, "./source-addr"))
230 vty_out(vty
, " local-address %s",
231 yang_dnode_get_string(dnode
, "./source-addr"));
233 if (strcmp(vrf
, VRF_DEFAULT_NAME
))
234 vty_out(vty
, " vrf %s", vrf
);
236 if (strcmp(ifname
, "*"))
237 vty_out(vty
, " interface %s", ifname
);
242 void bfd_cli_show_single_hop_peer(struct vty
*vty
,
243 struct lyd_node
*dnode
,
246 _bfd_cli_show_peer(vty
, dnode
, show_defaults
, false);
249 void bfd_cli_show_multi_hop_peer(struct vty
*vty
,
250 struct lyd_node
*dnode
,
253 _bfd_cli_show_peer(vty
, dnode
, show_defaults
, true);
256 void bfd_cli_show_peer_end(struct vty
*vty
,
257 struct lyd_node
*dnode
__attribute__((__unused__
)))
259 vty_out(vty
, " !\n");
263 bfd_peer_shutdown
, bfd_peer_shutdown_cmd
,
266 "Disable BFD peer\n")
268 nb_cli_enqueue_change(vty
, "./administrative-down", NB_OP_MODIFY
,
269 no
? "false" : "true");
270 return nb_cli_apply_changes(vty
, NULL
);
273 void bfd_cli_show_shutdown(struct vty
*vty
, struct lyd_node
*dnode
,
276 vty_out(vty
, " %sshutdown\n",
277 yang_dnode_get_bool(dnode
, NULL
) ? "" : "no ");
281 bfd_peer_passive
, bfd_peer_passive_cmd
,
284 "Don't attempt to start sessions\n")
286 nb_cli_enqueue_change(vty
, "./passive-mode", NB_OP_MODIFY
,
287 no
? "false" : "true");
288 return nb_cli_apply_changes(vty
, NULL
);
291 void bfd_cli_show_passive(struct vty
*vty
, struct lyd_node
*dnode
,
294 vty_out(vty
, " %spassive-mode\n",
295 yang_dnode_get_bool(dnode
, NULL
) ? "" : "no ");
299 bfd_peer_minimum_ttl
, bfd_peer_minimum_ttl_cmd
,
300 "[no] minimum-ttl (1-254)$ttl",
302 "Expect packets with at least this TTL\n"
303 "Minimum TTL expected\n")
305 if (bfd_cli_is_single_hop(vty
)) {
306 vty_out(vty
, "%% Minimum TTL is only available for multi hop sessions.\n");
307 return CMD_WARNING_CONFIG_FAILED
;
311 nb_cli_enqueue_change(vty
, "./minimum-ttl", NB_OP_DESTROY
,
314 nb_cli_enqueue_change(vty
, "./minimum-ttl", NB_OP_MODIFY
,
316 return nb_cli_apply_changes(vty
, NULL
);
320 no_bfd_peer_minimum_ttl
, no_bfd_peer_minimum_ttl_cmd
,
323 "Expect packets with at least this TTL\n")
325 nb_cli_enqueue_change(vty
, "./minimum-ttl", NB_OP_DESTROY
, NULL
);
326 return nb_cli_apply_changes(vty
, NULL
);
329 void bfd_cli_show_minimum_ttl(struct vty
*vty
, struct lyd_node
*dnode
,
332 vty_out(vty
, " minimum-ttl %s\n", yang_dnode_get_string(dnode
, NULL
));
336 bfd_peer_mult
, bfd_peer_mult_cmd
,
337 "detect-multiplier (2-255)$multiplier",
338 "Configure peer detection multiplier\n"
339 "Configure peer detection multiplier value\n")
341 nb_cli_enqueue_change(vty
, "./detection-multiplier", NB_OP_MODIFY
,
343 return nb_cli_apply_changes(vty
, NULL
);
346 void bfd_cli_show_mult(struct vty
*vty
, struct lyd_node
*dnode
,
349 vty_out(vty
, " detect-multiplier %s\n",
350 yang_dnode_get_string(dnode
, NULL
));
354 bfd_peer_rx
, bfd_peer_rx_cmd
,
355 "receive-interval (10-60000)$interval",
356 "Configure peer receive interval\n"
357 "Configure peer receive interval value in milliseconds\n")
361 snprintf(value
, sizeof(value
), "%ld", interval
* 1000);
362 nb_cli_enqueue_change(vty
, "./required-receive-interval", NB_OP_MODIFY
,
365 return nb_cli_apply_changes(vty
, NULL
);
368 void bfd_cli_show_rx(struct vty
*vty
, struct lyd_node
*dnode
,
371 uint32_t value
= yang_dnode_get_uint32(dnode
, NULL
);
373 vty_out(vty
, " receive-interval %u\n", value
/ 1000);
377 bfd_peer_tx
, bfd_peer_tx_cmd
,
378 "transmit-interval (10-60000)$interval",
379 "Configure peer transmit interval\n"
380 "Configure peer transmit interval value in milliseconds\n")
384 snprintf(value
, sizeof(value
), "%ld", interval
* 1000);
385 nb_cli_enqueue_change(vty
, "./desired-transmission-interval",
386 NB_OP_MODIFY
, value
);
388 return nb_cli_apply_changes(vty
, NULL
);
391 void bfd_cli_show_tx(struct vty
*vty
, struct lyd_node
*dnode
,
394 uint32_t value
= yang_dnode_get_uint32(dnode
, NULL
);
396 vty_out(vty
, " transmit-interval %u\n", value
/ 1000);
400 bfd_peer_echo
, bfd_peer_echo_cmd
,
403 "Configure echo mode\n")
405 if (!bfd_cli_is_profile(vty
) && !bfd_cli_is_single_hop(vty
)) {
406 vty_out(vty
, "%% Echo mode is only available for single hop sessions.\n");
407 return CMD_WARNING_CONFIG_FAILED
;
410 if (!no
&& !bglobal
.bg_use_dplane
) {
411 vty_out(vty
, "%% Current implementation of echo mode works only when the peer is also FRR.\n");
414 nb_cli_enqueue_change(vty
, "./echo-mode", NB_OP_MODIFY
,
415 no
? "false" : "true");
416 return nb_cli_apply_changes(vty
, NULL
);
419 void bfd_cli_show_echo(struct vty
*vty
, struct lyd_node
*dnode
,
422 vty_out(vty
, " %secho-mode\n",
423 yang_dnode_get_bool(dnode
, NULL
) ? "" : "no ");
427 bfd_peer_echo_interval
, bfd_peer_echo_interval_cmd
,
428 "echo-interval (10-60000)$interval",
429 "Configure peer echo intervals\n"
430 "Configure peer echo rx/tx intervals value in milliseconds\n")
434 if (!bfd_cli_is_profile(vty
) && !bfd_cli_is_single_hop(vty
)) {
435 vty_out(vty
, "%% Echo mode is only available for single hop sessions.\n");
436 return CMD_WARNING_CONFIG_FAILED
;
439 snprintf(value
, sizeof(value
), "%ld", interval
* 1000);
440 nb_cli_enqueue_change(vty
, "./desired-echo-transmission-interval",
441 NB_OP_MODIFY
, value
);
442 nb_cli_enqueue_change(vty
, "./required-echo-receive-interval",
443 NB_OP_MODIFY
, value
);
445 return nb_cli_apply_changes(vty
, NULL
);
449 bfd_peer_echo_transmit_interval
, bfd_peer_echo_transmit_interval_cmd
,
450 "echo transmit-interval (10-60000)$interval",
451 "Configure peer echo intervals\n"
452 "Configure desired transmit interval\n"
453 "Configure interval value in milliseconds\n")
457 if (!bfd_cli_is_profile(vty
) && !bfd_cli_is_single_hop(vty
)) {
458 vty_out(vty
, "%% Echo mode is only available for single hop sessions.\n");
459 return CMD_WARNING_CONFIG_FAILED
;
462 snprintf(value
, sizeof(value
), "%ld", interval
* 1000);
463 nb_cli_enqueue_change(vty
, "./desired-echo-transmission-interval",
464 NB_OP_MODIFY
, value
);
466 return nb_cli_apply_changes(vty
, NULL
);
469 void bfd_cli_show_desired_echo_transmission_interval(struct vty
*vty
,
470 struct lyd_node
*dnode
, bool show_defaults
)
472 uint32_t value
= yang_dnode_get_uint32(dnode
, NULL
);
474 vty_out(vty
, " echo transmit-interval %u\n", value
/ 1000);
478 bfd_peer_echo_receive_interval
, bfd_peer_echo_receive_interval_cmd
,
479 "echo receive-interval <disabled$disabled|(10-60000)$interval>",
480 "Configure peer echo intervals\n"
481 "Configure required receive interval\n"
482 "Disable echo packets receive\n"
483 "Configure interval value in milliseconds\n")
487 if (!bfd_cli_is_profile(vty
) && !bfd_cli_is_single_hop(vty
)) {
488 vty_out(vty
, "%% Echo mode is only available for single hop sessions.\n");
489 return CMD_WARNING_CONFIG_FAILED
;
493 snprintf(value
, sizeof(value
), "0");
495 snprintf(value
, sizeof(value
), "%ld", interval
* 1000);
497 nb_cli_enqueue_change(vty
, "./required-echo-receive-interval",
498 NB_OP_MODIFY
, value
);
500 return nb_cli_apply_changes(vty
, NULL
);
503 void bfd_cli_show_required_echo_receive_interval(struct vty
*vty
,
504 struct lyd_node
*dnode
, bool show_defaults
)
506 uint32_t value
= yang_dnode_get_uint32(dnode
, NULL
);
509 vty_out(vty
, " echo receive-interval %u\n", value
/ 1000);
511 vty_out(vty
, " echo receive-interval disabled\n");
517 DEFPY_YANG_NOSH(bfd_profile
, bfd_profile_cmd
,
518 "profile BFDPROF$name",
520 BFD_PROFILE_NAME_STR
)
522 char xpath
[XPATH_MAXLEN
];
525 snprintf(xpath
, sizeof(xpath
), "/frr-bfdd:bfdd/bfd/profile[name='%s']",
528 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
530 /* Apply settings immediately. */
531 rv
= nb_cli_apply_changes(vty
, NULL
);
532 if (rv
== CMD_SUCCESS
)
533 VTY_PUSH_XPATH(BFD_PROFILE_NODE
, xpath
);
538 DEFPY_YANG(no_bfd_profile
, no_bfd_profile_cmd
,
539 "no profile BFDPROF$name",
542 BFD_PROFILE_NAME_STR
)
544 char xpath
[XPATH_MAXLEN
];
546 snprintf(xpath
, sizeof(xpath
), "/frr-bfdd:bfdd/bfd/profile[name='%s']",
549 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
551 /* Apply settings immediately. */
552 return nb_cli_apply_changes(vty
, NULL
);
555 void bfd_cli_show_profile(struct vty
*vty
, struct lyd_node
*dnode
,
558 vty_out(vty
, " profile %s\n", yang_dnode_get_string(dnode
, "./name"));
561 ALIAS_YANG(bfd_peer_mult
, bfd_profile_mult_cmd
,
562 "detect-multiplier (2-255)$multiplier",
563 "Configure peer detection multiplier\n"
564 "Configure peer detection multiplier value\n")
566 ALIAS_YANG(bfd_peer_tx
, bfd_profile_tx_cmd
,
567 "transmit-interval (10-60000)$interval",
568 "Configure peer transmit interval\n"
569 "Configure peer transmit interval value in milliseconds\n")
571 ALIAS_YANG(bfd_peer_rx
, bfd_profile_rx_cmd
,
572 "receive-interval (10-60000)$interval",
573 "Configure peer receive interval\n"
574 "Configure peer receive interval value in milliseconds\n")
576 ALIAS_YANG(bfd_peer_shutdown
, bfd_profile_shutdown_cmd
,
579 "Disable BFD peer\n")
581 ALIAS_YANG(bfd_peer_passive
, bfd_profile_passive_cmd
,
584 "Don't attempt to start sessions\n")
586 ALIAS_YANG(bfd_peer_minimum_ttl
, bfd_profile_minimum_ttl_cmd
,
587 "[no] minimum-ttl (1-254)$ttl",
589 "Expect packets with at least this TTL\n"
590 "Minimum TTL expected\n")
592 ALIAS_YANG(no_bfd_peer_minimum_ttl
, no_bfd_profile_minimum_ttl_cmd
,
595 "Expect packets with at least this TTL\n")
597 ALIAS_YANG(bfd_peer_echo
, bfd_profile_echo_cmd
,
600 "Configure echo mode\n")
602 ALIAS_YANG(bfd_peer_echo_interval
, bfd_profile_echo_interval_cmd
,
603 "echo-interval (10-60000)$interval",
604 "Configure peer echo interval\n"
605 "Configure peer echo interval value in milliseconds\n")
608 bfd_peer_echo_transmit_interval
, bfd_profile_echo_transmit_interval_cmd
,
609 "echo transmit-interval (10-60000)$interval",
610 "Configure peer echo intervals\n"
611 "Configure desired transmit interval\n"
612 "Configure interval value in milliseconds\n")
615 bfd_peer_echo_receive_interval
, bfd_profile_echo_receive_interval_cmd
,
616 "echo receive-interval <disabled$disabled|(10-60000)$interval>",
617 "Configure peer echo intervals\n"
618 "Configure required receive interval\n"
619 "Disable echo packets receive\n"
620 "Configure interval value in milliseconds\n")
622 DEFPY_YANG(bfd_peer_profile
, bfd_peer_profile_cmd
,
623 "[no] profile BFDPROF$pname",
625 "Use BFD profile settings\n"
626 BFD_PROFILE_NAME_STR
)
629 nb_cli_enqueue_change(vty
, "./profile", NB_OP_DESTROY
, NULL
);
631 nb_cli_enqueue_change(vty
, "./profile", NB_OP_MODIFY
, pname
);
633 return nb_cli_apply_changes(vty
, NULL
);
636 void bfd_cli_peer_profile_show(struct vty
*vty
, struct lyd_node
*dnode
,
639 vty_out(vty
, " profile %s\n", yang_dnode_get_string(dnode
, NULL
));
642 struct cmd_node bfd_profile_node
= {
643 .name
= "bfd profile",
644 .node
= BFD_PROFILE_NODE
,
645 .parent_node
= BFD_NODE
,
646 .prompt
= "%s(config-bfd-profile)# ",
649 static void bfd_profile_var(vector comps
, struct cmd_token
*token
)
651 extern struct bfdproflist bplist
;
652 struct bfd_profile
*bp
;
654 TAILQ_FOREACH (bp
, &bplist
, entry
) {
655 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bp
->name
));
659 static const struct cmd_variable_handler bfd_vars
[] = {
660 {.tokenname
= "BFDPROF", .completions
= bfd_profile_var
},
661 {.completions
= NULL
}
667 install_element(CONFIG_NODE
, &bfd_enter_cmd
);
668 install_element(CONFIG_NODE
, &bfd_config_reset_cmd
);
670 install_element(BFD_NODE
, &bfd_peer_enter_cmd
);
671 install_element(BFD_NODE
, &bfd_no_peer_cmd
);
673 install_element(BFD_PEER_NODE
, &bfd_peer_shutdown_cmd
);
674 install_element(BFD_PEER_NODE
, &bfd_peer_mult_cmd
);
675 install_element(BFD_PEER_NODE
, &bfd_peer_rx_cmd
);
676 install_element(BFD_PEER_NODE
, &bfd_peer_tx_cmd
);
677 install_element(BFD_PEER_NODE
, &bfd_peer_echo_cmd
);
678 install_element(BFD_PEER_NODE
, &bfd_peer_echo_interval_cmd
);
679 install_element(BFD_PEER_NODE
, &bfd_peer_echo_transmit_interval_cmd
);
680 install_element(BFD_PEER_NODE
, &bfd_peer_echo_receive_interval_cmd
);
681 install_element(BFD_PEER_NODE
, &bfd_peer_profile_cmd
);
682 install_element(BFD_PEER_NODE
, &bfd_peer_passive_cmd
);
683 install_element(BFD_PEER_NODE
, &bfd_peer_minimum_ttl_cmd
);
684 install_element(BFD_PEER_NODE
, &no_bfd_peer_minimum_ttl_cmd
);
686 /* Profile commands. */
687 cmd_variable_handler_register(bfd_vars
);
689 install_node(&bfd_profile_node
);
690 install_default(BFD_PROFILE_NODE
);
692 install_element(BFD_NODE
, &bfd_profile_cmd
);
693 install_element(BFD_NODE
, &no_bfd_profile_cmd
);
695 install_element(BFD_PROFILE_NODE
, &bfd_profile_mult_cmd
);
696 install_element(BFD_PROFILE_NODE
, &bfd_profile_tx_cmd
);
697 install_element(BFD_PROFILE_NODE
, &bfd_profile_rx_cmd
);
698 install_element(BFD_PROFILE_NODE
, &bfd_profile_shutdown_cmd
);
699 install_element(BFD_PROFILE_NODE
, &bfd_profile_echo_cmd
);
700 install_element(BFD_PROFILE_NODE
, &bfd_profile_echo_interval_cmd
);
701 install_element(BFD_PROFILE_NODE
, &bfd_profile_echo_transmit_interval_cmd
);
702 install_element(BFD_PROFILE_NODE
, &bfd_profile_echo_receive_interval_cmd
);
703 install_element(BFD_PROFILE_NODE
, &bfd_profile_passive_cmd
);
704 install_element(BFD_PROFILE_NODE
, &bfd_profile_minimum_ttl_cmd
);
705 install_element(BFD_PROFILE_NODE
, &no_bfd_profile_minimum_ttl_cmd
);