]>
Commit | Line | Data |
---|---|---|
adc26455 RZ |
1 | /* |
2 | * BFD daemon CLI implementation. | |
3 | * | |
4 | * Copyright (C) 2019 Network Device Education Foundation, Inc. ("NetDEF") | |
5 | * Rafael Zalamena | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * 02110-1301 USA. | |
21 | */ | |
22 | ||
8e0c84ff RZ |
23 | #include <zebra.h> |
24 | ||
adc26455 RZ |
25 | #include "lib/command.h" |
26 | #include "lib/log.h" | |
27 | #include "lib/northbound_cli.h" | |
28 | ||
29 | #ifndef VTYSH_EXTRACT_PL | |
30 | #include "bfdd/bfdd_cli_clippy.c" | |
31 | #endif /* VTYSH_EXTRACT_PL */ | |
32 | ||
33 | #include "bfd.h" | |
34 | ||
35 | /* | |
36 | * Definitions. | |
37 | */ | |
38 | #define PEER_STR "Configure peer\n" | |
39 | #define INTERFACE_NAME_STR "Configure interface name to use\n" | |
40 | #define PEER_IPV4_STR "IPv4 peer address\n" | |
41 | #define PEER_IPV6_STR "IPv6 peer address\n" | |
42 | #define MHOP_STR "Configure multihop\n" | |
43 | #define LOCAL_STR "Configure local address\n" | |
44 | #define LOCAL_IPV4_STR "IPv4 local address\n" | |
45 | #define LOCAL_IPV6_STR "IPv6 local address\n" | |
46 | #define LOCAL_INTF_STR "Configure local interface name to use\n" | |
47 | #define VRF_STR "Configure VRF\n" | |
48 | #define VRF_NAME_STR "Configure VRF name\n" | |
49 | ||
50 | /* | |
51 | * Prototypes. | |
52 | */ | |
53 | ||
54 | /* | |
55 | * Functions. | |
56 | */ | |
fdf8ac87 RZ |
57 | DEFPY_NOSH( |
58 | bfd_enter, bfd_enter_cmd, | |
59 | "bfd", | |
60 | "Configure BFD peers\n") | |
61 | { | |
62 | int ret; | |
63 | ||
64 | nb_cli_enqueue_change(vty, "/frr-bfdd:bfdd/bfd", NB_OP_CREATE, NULL); | |
65 | ret = nb_cli_apply_changes(vty, NULL); | |
66 | if (ret == CMD_SUCCESS) | |
67 | VTY_PUSH_XPATH(BFD_NODE, "/frr-bfdd:bfdd/bfd"); | |
68 | ||
69 | return ret; | |
70 | } | |
71 | ||
2a573ff6 RZ |
72 | DEFUN( |
73 | bfd_config_reset, bfd_config_reset_cmd, | |
74 | "no bfd", | |
75 | NO_STR | |
76 | "Configure BFD peers\n") | |
77 | { | |
78 | nb_cli_enqueue_change(vty, "/frr-bfdd:bfdd/bfd", NB_OP_DESTROY, NULL); | |
79 | return nb_cli_apply_changes(vty, NULL); | |
80 | } | |
81 | ||
0287a64a RZ |
82 | void bfd_cli_show_header(struct vty *vty, |
83 | struct lyd_node *dnode __attribute__((__unused__)), | |
84 | bool show_defaults __attribute__((__unused__))) | |
85 | { | |
86 | vty_out(vty, "!\nbfd\n"); | |
87 | } | |
88 | ||
89 | void bfd_cli_show_header_end(struct vty *vty, | |
90 | struct lyd_node *dnode __attribute__((__unused__))) | |
91 | { | |
92 | vty_out(vty, "!\n"); | |
93 | } | |
94 | ||
adc26455 RZ |
95 | DEFPY_NOSH( |
96 | bfd_peer_enter, bfd_peer_enter_cmd, | |
97 | "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}]", | |
98 | PEER_STR | |
99 | PEER_IPV4_STR | |
100 | PEER_IPV6_STR | |
101 | MHOP_STR | |
102 | LOCAL_STR | |
103 | LOCAL_IPV4_STR | |
104 | LOCAL_IPV6_STR | |
105 | INTERFACE_STR | |
106 | LOCAL_INTF_STR | |
107 | VRF_STR | |
108 | VRF_NAME_STR) | |
109 | { | |
110 | int ret, slen; | |
adc26455 | 111 | char source_str[INET6_ADDRSTRLEN]; |
284062bf | 112 | char xpath[XPATH_MAXLEN], xpath_srcaddr[XPATH_MAXLEN + 32]; |
adc26455 RZ |
113 | |
114 | if (multihop) | |
115 | snprintf(source_str, sizeof(source_str), "[source-addr='%s']", | |
116 | local_address_str); | |
117 | else | |
118 | source_str[0] = 0; | |
119 | ||
120 | slen = snprintf(xpath, sizeof(xpath), | |
121 | "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']", | |
122 | multihop ? "multi-hop" : "single-hop", source_str, | |
123 | peer_str); | |
124 | if (ifname) | |
125 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
126 | "[interface='%s']", ifname); | |
127 | else | |
128 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
129 | "[interface='']"); | |
130 | if (vrf) | |
131 | snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']", vrf); | |
132 | else | |
133 | snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']", | |
134 | VRF_DEFAULT_NAME); | |
135 | ||
136 | nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL); | |
284062bf RZ |
137 | if (multihop == NULL && local_address_str != NULL) { |
138 | snprintf(xpath_srcaddr, sizeof(xpath_srcaddr), | |
139 | "%s/source-addr", xpath); | |
140 | nb_cli_enqueue_change(vty, xpath_srcaddr, NB_OP_MODIFY, | |
141 | local_address_str); | |
142 | } | |
adc26455 | 143 | |
fdf8ac87 | 144 | /* Apply settings immediately. */ |
adc26455 RZ |
145 | ret = nb_cli_apply_changes(vty, NULL); |
146 | if (ret == CMD_SUCCESS) | |
147 | VTY_PUSH_XPATH(BFD_PEER_NODE, xpath); | |
148 | ||
149 | return ret; | |
150 | } | |
151 | ||
152 | DEFPY( | |
153 | bfd_no_peer, bfd_no_peer_cmd, | |
154 | "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}]", | |
155 | NO_STR | |
156 | PEER_STR | |
157 | PEER_IPV4_STR | |
158 | PEER_IPV6_STR | |
159 | MHOP_STR | |
160 | LOCAL_STR | |
161 | LOCAL_IPV4_STR | |
162 | LOCAL_IPV6_STR | |
163 | INTERFACE_STR | |
164 | LOCAL_INTF_STR | |
165 | VRF_STR | |
166 | VRF_NAME_STR) | |
167 | { | |
168 | int slen; | |
169 | char xpath[XPATH_MAXLEN]; | |
170 | char source_str[INET6_ADDRSTRLEN]; | |
171 | ||
172 | if (multihop) | |
173 | snprintf(source_str, sizeof(source_str), "[source-addr='%s']", | |
174 | local_address_str); | |
175 | else | |
176 | source_str[0] = 0; | |
177 | ||
178 | slen = snprintf(xpath, sizeof(xpath), | |
179 | "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']", | |
180 | multihop ? "multi-hop" : "single-hop", source_str, | |
181 | peer_str); | |
182 | if (ifname) | |
183 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
184 | "[interface='%s']", ifname); | |
185 | else | |
186 | slen += snprintf(xpath + slen, sizeof(xpath) - slen, | |
187 | "[interface='']"); | |
188 | if (vrf) | |
189 | snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']", vrf); | |
190 | else | |
191 | snprintf(xpath + slen, sizeof(xpath) - slen, "[vrf='%s']", | |
192 | VRF_DEFAULT_NAME); | |
193 | ||
194 | nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL); | |
195 | ||
196 | /* Apply settings immediatly. */ | |
197 | return nb_cli_apply_changes(vty, NULL); | |
198 | } | |
199 | ||
0287a64a RZ |
200 | static void _bfd_cli_show_peer(struct vty *vty, struct lyd_node *dnode, |
201 | bool show_defaults __attribute__((__unused__)), | |
202 | bool mhop) | |
203 | { | |
204 | const char *vrf = yang_dnode_get_string(dnode, "./vrf"); | |
205 | const char *ifname = yang_dnode_get_string(dnode, "./interface"); | |
206 | ||
207 | vty_out(vty, " peer %s", | |
208 | yang_dnode_get_string(dnode, "./dest-addr")); | |
209 | ||
210 | if (mhop) | |
211 | vty_out(vty, " multihop"); | |
212 | ||
213 | if (yang_dnode_exists(dnode, "./source-addr")) | |
214 | vty_out(vty, " local-address %s", | |
215 | yang_dnode_get_string(dnode, "./source-addr")); | |
216 | ||
217 | if (strcmp(vrf, VRF_DEFAULT_NAME)) | |
218 | vty_out(vty, " vrf %s", vrf); | |
219 | ||
220 | if (ifname[0]) | |
221 | vty_out(vty, " interface %s", ifname); | |
222 | ||
223 | vty_out(vty, "\n"); | |
224 | } | |
225 | ||
226 | void bfd_cli_show_single_hop_peer(struct vty *vty, | |
227 | struct lyd_node *dnode, | |
228 | bool show_defaults) | |
229 | { | |
230 | _bfd_cli_show_peer(vty, dnode, show_defaults, false); | |
231 | } | |
232 | ||
233 | void bfd_cli_show_multi_hop_peer(struct vty *vty, | |
234 | struct lyd_node *dnode, | |
235 | bool show_defaults) | |
236 | { | |
237 | _bfd_cli_show_peer(vty, dnode, show_defaults, true); | |
238 | } | |
239 | ||
240 | void bfd_cli_show_peer_end(struct vty *vty, | |
241 | struct lyd_node *dnode __attribute__((__unused__))) | |
242 | { | |
243 | vty_out(vty, " !\n"); | |
244 | } | |
245 | ||
adc26455 RZ |
246 | DEFPY( |
247 | bfd_peer_shutdown, bfd_peer_shutdown_cmd, | |
248 | "[no] shutdown", | |
249 | NO_STR | |
7818c5fb | 250 | "Disable BFD peer\n") |
adc26455 RZ |
251 | { |
252 | nb_cli_enqueue_change(vty, "./administrative-down", NB_OP_MODIFY, | |
253 | no ? "false" : "true"); | |
254 | return nb_cli_apply_changes(vty, NULL); | |
255 | } | |
256 | ||
0287a64a RZ |
257 | void bfd_cli_show_shutdown(struct vty *vty, struct lyd_node *dnode, |
258 | bool show_defaults) | |
259 | { | |
260 | if (show_defaults) | |
261 | vty_out(vty, " shutdown\n"); | |
262 | else | |
263 | vty_out(vty, " %sshutdown\n", | |
264 | yang_dnode_get_bool(dnode, NULL) ? "" : "no "); | |
265 | } | |
266 | ||
adc26455 RZ |
267 | DEFPY( |
268 | bfd_peer_mult, bfd_peer_mult_cmd, | |
269 | "detect-multiplier (2-255)$multiplier", | |
270 | "Configure peer detection multiplier\n" | |
271 | "Configure peer detection multiplier value\n") | |
272 | { | |
273 | nb_cli_enqueue_change(vty, "./detection-multiplier", NB_OP_MODIFY, | |
274 | multiplier_str); | |
275 | return nb_cli_apply_changes(vty, NULL); | |
276 | } | |
277 | ||
0287a64a RZ |
278 | void bfd_cli_show_mult(struct vty *vty, struct lyd_node *dnode, |
279 | bool show_defaults) | |
280 | { | |
281 | if (show_defaults) | |
282 | vty_out(vty, " detect-multiplier %d\n", | |
283 | BFD_DEFDETECTMULT); | |
284 | else | |
285 | vty_out(vty, " detect-multiplier %s\n", | |
286 | yang_dnode_get_string(dnode, NULL)); | |
287 | } | |
288 | ||
adc26455 RZ |
289 | DEFPY( |
290 | bfd_peer_rx, bfd_peer_rx_cmd, | |
291 | "receive-interval (10-60000)$interval", | |
292 | "Configure peer receive interval\n" | |
293 | "Configure peer receive interval value in milliseconds\n") | |
294 | { | |
8a676ce6 RZ |
295 | char value[32]; |
296 | ||
297 | snprintf(value, sizeof(value), "%ld", interval * 1000); | |
adc26455 | 298 | nb_cli_enqueue_change(vty, "./required-receive-interval", NB_OP_MODIFY, |
8a676ce6 RZ |
299 | value); |
300 | ||
adc26455 RZ |
301 | return nb_cli_apply_changes(vty, NULL); |
302 | } | |
303 | ||
0287a64a RZ |
304 | void bfd_cli_show_rx(struct vty *vty, struct lyd_node *dnode, |
305 | bool show_defaults) | |
306 | { | |
8a676ce6 RZ |
307 | uint32_t value; |
308 | ||
0287a64a RZ |
309 | if (show_defaults) |
310 | vty_out(vty, " receive-interval %d\n", | |
311 | BFD_DEFREQUIREDMINRX); | |
8a676ce6 RZ |
312 | else { |
313 | value = yang_dnode_get_uint32(dnode, NULL); | |
314 | vty_out(vty, " receive-interval %" PRIu32 "\n", value / 1000); | |
315 | } | |
0287a64a RZ |
316 | } |
317 | ||
adc26455 RZ |
318 | DEFPY( |
319 | bfd_peer_tx, bfd_peer_tx_cmd, | |
320 | "transmit-interval (10-60000)$interval", | |
321 | "Configure peer transmit interval\n" | |
322 | "Configure peer transmit interval value in milliseconds\n") | |
323 | { | |
8a676ce6 RZ |
324 | char value[32]; |
325 | ||
326 | snprintf(value, sizeof(value), "%ld", interval * 1000); | |
adc26455 | 327 | nb_cli_enqueue_change(vty, "./desired-transmission-interval", |
8a676ce6 RZ |
328 | NB_OP_MODIFY, value); |
329 | ||
adc26455 RZ |
330 | return nb_cli_apply_changes(vty, NULL); |
331 | } | |
332 | ||
0287a64a RZ |
333 | void bfd_cli_show_tx(struct vty *vty, struct lyd_node *dnode, |
334 | bool show_defaults) | |
335 | { | |
8a676ce6 RZ |
336 | uint32_t value; |
337 | ||
0287a64a RZ |
338 | if (show_defaults) |
339 | vty_out(vty, " transmit-interval %d\n", | |
340 | BFD_DEFDESIREDMINTX); | |
8a676ce6 RZ |
341 | else { |
342 | value = yang_dnode_get_uint32(dnode, NULL); | |
343 | vty_out(vty, " transmit-interval %" PRIu32 "\n", value / 1000); | |
344 | } | |
0287a64a RZ |
345 | } |
346 | ||
adc26455 RZ |
347 | DEFPY( |
348 | bfd_peer_echo, bfd_peer_echo_cmd, | |
349 | "[no] echo-mode", | |
350 | NO_STR | |
351 | "Configure echo mode\n") | |
352 | { | |
353 | nb_cli_enqueue_change(vty, "./echo-mode", NB_OP_MODIFY, | |
354 | no ? "false" : "true"); | |
355 | return nb_cli_apply_changes(vty, NULL); | |
356 | } | |
357 | ||
0287a64a RZ |
358 | void bfd_cli_show_echo(struct vty *vty, struct lyd_node *dnode, |
359 | bool show_defaults) | |
360 | { | |
361 | if (show_defaults) | |
362 | vty_out(vty, " no echo-mode\n"); | |
363 | else | |
364 | vty_out(vty, " %secho-mode\n", | |
365 | yang_dnode_get_bool(dnode, NULL) ? "" : "no "); | |
366 | } | |
367 | ||
adc26455 RZ |
368 | DEFPY( |
369 | bfd_peer_echo_interval, bfd_peer_echo_interval_cmd, | |
370 | "echo-interval (10-60000)$interval", | |
371 | "Configure peer echo interval\n" | |
372 | "Configure peer echo interval value in milliseconds\n") | |
373 | { | |
8a676ce6 RZ |
374 | char value[32]; |
375 | ||
376 | snprintf(value, sizeof(value), "%ld", interval * 1000); | |
adc26455 | 377 | nb_cli_enqueue_change(vty, "./desired-echo-transmission-interval", |
8a676ce6 RZ |
378 | NB_OP_MODIFY, value); |
379 | ||
adc26455 RZ |
380 | return nb_cli_apply_changes(vty, NULL); |
381 | } | |
382 | ||
0287a64a RZ |
383 | void bfd_cli_show_echo_interval(struct vty *vty, struct lyd_node *dnode, |
384 | bool show_defaults) | |
385 | { | |
8a676ce6 RZ |
386 | uint32_t value; |
387 | ||
0287a64a RZ |
388 | if (show_defaults) |
389 | vty_out(vty, " echo-interval %d\n", | |
390 | BFD_DEF_REQ_MIN_ECHO); | |
8a676ce6 RZ |
391 | else { |
392 | value = yang_dnode_get_uint32(dnode, NULL); | |
393 | vty_out(vty, " echo-interval %" PRIu32 "\n", value / 1000); | |
394 | } | |
0287a64a RZ |
395 | } |
396 | ||
adc26455 RZ |
397 | void |
398 | bfdd_cli_init(void) | |
399 | { | |
fdf8ac87 | 400 | install_element(CONFIG_NODE, &bfd_enter_cmd); |
2a573ff6 RZ |
401 | install_element(CONFIG_NODE, &bfd_config_reset_cmd); |
402 | ||
adc26455 RZ |
403 | install_element(BFD_NODE, &bfd_peer_enter_cmd); |
404 | install_element(BFD_NODE, &bfd_no_peer_cmd); | |
405 | ||
406 | install_element(BFD_PEER_NODE, &bfd_peer_shutdown_cmd); | |
407 | install_element(BFD_PEER_NODE, &bfd_peer_mult_cmd); | |
408 | install_element(BFD_PEER_NODE, &bfd_peer_rx_cmd); | |
409 | install_element(BFD_PEER_NODE, &bfd_peer_tx_cmd); | |
410 | install_element(BFD_PEER_NODE, &bfd_peer_echo_cmd); | |
411 | install_element(BFD_PEER_NODE, &bfd_peer_echo_interval_cmd); | |
412 | } |