]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfdd_vty.c
bfdd: avoid creating duplicate peer contexts
[mirror_frr.git] / bfdd / bfdd_vty.c
CommitLineData
c2f29cf3
RZ
1/*
2 * BFD daemon code
3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
4 *
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
8 * later version.
9 *
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.
14 *
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
18 * 02111-1307, USA.
19 */
20
21#include <zebra.h>
22
23#include "lib/command.h"
24#include "lib/json.h"
25#include "lib/log.h"
26#include "lib/vty.h"
27
28#include "bfd.h"
29
30#ifndef VTYSH_EXTRACT_PL
31#include "bfdd/bfdd_vty_clippy.c"
32#endif
33
34/*
35 * Commands help string definitions.
36 */
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"
48
49/*
50 * Prototypes
51 */
52static int bfdd_write_config(struct vty *vty);
53static int bfdd_peer_write_config(struct vty *vty);
d245e522 54static void _bfdd_peer_write_config(struct vty *vty, struct bfd_session *bs);
e3b78da8 55static void _bfdd_peer_write_config_iter(struct hash_bucket *hb, void *arg);
c2f29cf3
RZ
56static 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);
61
89a634c1 62static void _display_peer_header(struct vty *vty, struct bfd_session *bs);
c2f29cf3 63static struct json_object *__display_peer_json(struct bfd_session *bs);
89a634c1 64static struct json_object *_peer_json_header(struct bfd_session *bs);
c2f29cf3
RZ
65static void _display_peer_json(struct vty *vty, struct bfd_session *bs);
66static void _display_peer(struct vty *vty, struct bfd_session *bs);
67static void _display_all_peers(struct vty *vty, bool use_json);
e3b78da8
TB
68static void _display_peer_iter(struct hash_bucket *hb, void *arg);
69static void _display_peer_json_iter(struct hash_bucket *hb, void *arg);
0684c9b1
RZ
70static void _display_peer_counter(struct vty *vty, struct bfd_session *bs);
71static struct json_object *__display_peer_counters_json(struct bfd_session *bs);
72static void _display_peer_counters_json(struct vty *vty, struct bfd_session *bs);
e3b78da8
TB
73static void _display_peer_counter_iter(struct hash_bucket *hb, void *arg);
74static void _display_peer_counter_json_iter(struct hash_bucket *hb, void *arg);
0684c9b1 75static void _display_peers_counter(struct vty *vty, bool use_json);
89a634c1
RZ
76static 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,
80 const char *vrfname);
c2f29cf3 81
c2f29cf3
RZ
82/*
83 * Commands definition.
84 */
85DEFUN_NOSH(bfd_enter, bfd_enter_cmd, "bfd", "Configure BFD peers\n")
86{
87 vty->node = BFD_NODE;
88 return CMD_SUCCESS;
89}
90
91DEFUN_NOSH(
92 bfd_peer_enter, bfd_peer_enter_cmd,
5764d816 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}]",
c2f29cf3
RZ
94 PEER_STR PEER_IPV4_STR PEER_IPV6_STR
95 MHOP_STR
96 LOCAL_STR LOCAL_IPV4_STR LOCAL_IPV6_STR
97 INTERFACE_STR
98 LOCAL_INTF_STR
99 VRF_STR VRF_NAME_STR)
100{
101 bool mhop;
102 int idx;
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;
107 char errormsg[128];
108
109 vrfname = peer = ifname = local = NULL;
110
111 /* Gather all provided information. */
112 peer = argv[1]->arg;
113
114 idx = 0;
115 mhop = argv_find(argv, argc, "multihop", &idx);
116
117 idx = 0;
118 if (argv_find(argv, argc, "interface", &idx))
119 ifname = argv[idx + 1]->arg;
120
121 idx = 0;
122 if (argv_find(argv, argc, "local-address", &idx))
123 local = argv[idx + 1]->arg;
124
125 idx = 0;
126 if (argv_find(argv, argc, "vrf", &idx))
127 vrfname = argv[idx + 1]->arg;
128
c2f29cf3
RZ
129 strtosa(peer, &psa);
130 if (local) {
131 strtosa(local, &lsa);
132 lsap = &lsa;
133 } else
134 lsap = NULL;
135
136 if (bfd_configure_peer(&bpc, mhop, &psa, lsap, ifname, vrfname,
137 errormsg, sizeof(errormsg))
138 != 0) {
139 vty_out(vty, "%% Invalid peer configuration: %s\n", errormsg);
140 return CMD_WARNING_CONFIG_FAILED;
141 }
142
143 bs = bs_peer_find(&bpc);
144 if (bs == NULL) {
145 bs = ptm_bfd_sess_new(&bpc);
146 if (bs == NULL) {
147 vty_out(vty, "%% Failed to add peer.\n");
148 return CMD_WARNING_CONFIG_FAILED;
149 }
150 }
151
6bdb4a42
PG
152 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG)) {
153 if (bs->refcount)
154 vty_out(vty, "%% session peer is now configurable via bfd daemon.\n");
155 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG);
156 }
157
c2f29cf3
RZ
158 VTY_PUSH_CONTEXT(BFD_PEER_NODE, bs);
159
160 return CMD_SUCCESS;
161}
162
163DEFPY(bfd_peer_detectmultiplier, bfd_peer_detectmultiplier_cmd,
164 "detect-multiplier (2-255)$multiplier",
165 "Configure peer detection multiplier\n"
166 "Configure peer detection multiplier value\n")
167{
168 struct bfd_session *bs;
169
170 bs = VTY_GET_CONTEXT(bfd_session);
d3f3a2c4
RZ
171 if (bs->detect_mult == multiplier)
172 return CMD_SUCCESS;
173
c2f29cf3
RZ
174 bs->detect_mult = multiplier;
175
176 return CMD_SUCCESS;
177}
178
179DEFPY(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")
183{
184 struct bfd_session *bs;
185
186 bs = VTY_GET_CONTEXT(bfd_session);
d3f3a2c4
RZ
187 if (bs->timers.required_min_rx == (uint32_t)(interval * 1000))
188 return CMD_SUCCESS;
189
c2f29cf3 190 bs->timers.required_min_rx = interval * 1000;
d3f3a2c4 191 bfd_set_polling(bs);
c2f29cf3
RZ
192
193 return CMD_SUCCESS;
194}
195
196DEFPY(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")
200{
201 struct bfd_session *bs;
202
203 bs = VTY_GET_CONTEXT(bfd_session);
f43b9368 204 if (bs->timers.desired_min_tx == (uint32_t)(interval * 1000))
d3f3a2c4
RZ
205 return CMD_SUCCESS;
206
f43b9368 207 bs->timers.desired_min_tx = interval * 1000;
d3f3a2c4 208 bfd_set_polling(bs);
c2f29cf3
RZ
209
210 return CMD_SUCCESS;
211}
212
213DEFPY(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")
217{
218 struct bfd_session *bs;
219
220 bs = VTY_GET_CONTEXT(bfd_session);
d3f3a2c4
RZ
221 if (bs->timers.required_min_echo == (uint32_t)(interval * 1000))
222 return CMD_SUCCESS;
223
c2f29cf3
RZ
224 bs->timers.required_min_echo = interval * 1000;
225
226 return CMD_SUCCESS;
227}
228
229DEFPY(bfd_peer_shutdown, bfd_peer_shutdown_cmd, "[no] shutdown",
230 NO_STR "Disable BFD peer")
231{
232 struct bfd_session *bs;
233
234 bs = VTY_GET_CONTEXT(bfd_session);
235 if (no) {
236 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
237 return CMD_SUCCESS;
238
239 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
240
241 /* Change and notify state change. */
242 bs->ses_state = PTM_BFD_DOWN;
243 control_notify(bs);
244
245 /* Enable all timers. */
246 bfd_recvtimer_update(bs);
247 bfd_xmttimer_update(bs, bs->xmt_TO);
248 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) {
249 bfd_echo_recvtimer_update(bs);
250 bfd_echo_xmttimer_update(bs, bs->echo_xmt_TO);
251 }
252 } else {
253 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
254 return CMD_SUCCESS;
255
256 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
257
258 /* Disable all events. */
259 bfd_recvtimer_delete(bs);
260 bfd_echo_recvtimer_delete(bs);
261 bfd_xmttimer_delete(bs);
262 bfd_echo_xmttimer_delete(bs);
263
264 /* Change and notify state change. */
265 bs->ses_state = PTM_BFD_ADM_DOWN;
266 control_notify(bs);
267
268 ptm_bfd_snd(bs, 0);
269 }
270
271 return CMD_SUCCESS;
272}
273
274DEFPY(bfd_peer_echo, bfd_peer_echo_cmd, "[no] echo-mode",
275 NO_STR "Configure echo mode\n")
276{
277 struct bfd_session *bs;
278
279 bs = VTY_GET_CONTEXT(bfd_session);
280 if (no) {
281 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
282 return CMD_SUCCESS;
283
284 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
8bd859f6 285 ptm_bfd_echo_stop(bs);
c2f29cf3
RZ
286 } else {
287 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
288 return CMD_SUCCESS;
289
290 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
291 /* Apply setting immediately. */
9f37770f 292 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
73c62f8e 293 bs_echo_timer_handler(bs);
c2f29cf3
RZ
294 }
295
296 return CMD_SUCCESS;
297}
298
299DEFPY(bfd_peer_label, bfd_peer_label_cmd, "label WORD$label",
300 "Register peer label\n"
301 "Register peer label identification\n")
302{
303 struct bfd_session *bs;
304
305 /* Validate label length. */
306 if (strlen(label) >= MAXNAMELEN) {
307 vty_out(vty, "%% Label name is too long\n");
308 return CMD_WARNING_CONFIG_FAILED;
309 }
310
311 bs = VTY_GET_CONTEXT(bfd_session);
312 if (bfd_session_update_label(bs, label) == -1) {
313 vty_out(vty, "%% Failed to update peer label.\n");
314 return CMD_WARNING_CONFIG_FAILED;
315 }
316
317 return CMD_SUCCESS;
318}
319
320DEFPY(bfd_no_peer, bfd_no_peer_cmd,
321 "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}]",
322 NO_STR
323 PEER_STR PEER_IPV4_STR PEER_IPV6_STR
324 MHOP_STR
325 LOCAL_STR LOCAL_IPV4_STR LOCAL_IPV6_STR
326 INTERFACE_STR
327 LOCAL_INTF_STR
328 VRF_STR VRF_NAME_STR)
329{
330 int idx;
331 bool mhop;
332 struct bfd_peer_cfg bpc;
333 struct sockaddr_any psa, lsa, *lsap;
334 char errormsg[128];
335
336 strtosa(peer_str, &psa);
337 if (local) {
338 strtosa(local_str, &lsa);
339 lsap = &lsa;
340 } else {
341 lsap = NULL;
342 }
343
344 idx = 0;
345 mhop = argv_find(argv, argc, "multihop", &idx);
346
347 if (bfd_configure_peer(&bpc, mhop, &psa, lsap, ifname, vrfname,
348 errormsg, sizeof(errormsg))
349 != 0) {
350 vty_out(vty, "%% Invalid peer configuration: %s\n", errormsg);
351 return CMD_WARNING_CONFIG_FAILED;
352 }
353
bc50bcc8 354 if (ptm_bfd_sess_del(&bpc) != 0) {
c2f29cf3
RZ
355 vty_out(vty, "%% Failed to remove peer.\n");
356 return CMD_WARNING_CONFIG_FAILED;
357 }
358
359 return CMD_SUCCESS;
360}
361
362
363/*
364 * Show commands helper functions
365 */
89a634c1 366static void _display_peer_header(struct vty *vty, struct bfd_session *bs)
c2f29cf3 367{
79b4a6fc
RZ
368 char addr_buf[INET6_ADDRSTRLEN];
369
370 vty_out(vty, "\tpeer %s",
371 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
372 sizeof(addr_buf)));
373
374 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
c2f29cf3 375 vty_out(vty, " multihop");
79b4a6fc
RZ
376
377 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
378 vty_out(vty, " local-address %s",
379 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
380 sizeof(addr_buf)));
381
382 if (bs->key.vrfname[0])
383 vty_out(vty, " vrf %s", bs->key.vrfname);
384 if (bs->key.ifname[0])
385 vty_out(vty, " interface %s", bs->key.ifname);
386 vty_out(vty, "\n");
c2f29cf3
RZ
387
388 if (bs->pl)
389 vty_out(vty, "\t\tlabel: %s\n", bs->pl->pl_label);
89a634c1
RZ
390}
391
392static void _display_peer(struct vty *vty, struct bfd_session *bs)
393{
394 char buf[256];
395 time_t now;
396
397 _display_peer_header(vty, bs);
c2f29cf3
RZ
398
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);
401
402 vty_out(vty, "\t\tStatus: ");
403 switch (bs->ses_state) {
404 case PTM_BFD_ADM_DOWN:
405 vty_out(vty, "shutdown\n");
406 break;
407 case PTM_BFD_DOWN:
408 vty_out(vty, "down\n");
409
410 now = monotime(NULL);
98ef9c16 411 integer2timestr(now - bs->downtime.tv_sec, buf, sizeof(buf));
c2f29cf3
RZ
412 vty_out(vty, "\t\tDowntime: %s\n", buf);
413 break;
414 case PTM_BFD_INIT:
415 vty_out(vty, "init\n");
416 break;
417 case PTM_BFD_UP:
418 vty_out(vty, "up\n");
419
420 now = monotime(NULL);
421 integer2timestr(now - bs->uptime.tv_sec, buf, sizeof(buf));
422 vty_out(vty, "\t\tUptime: %s\n", buf);
423 break;
424
425 default:
426 vty_out(vty, "unknown\n");
427 break;
428 }
429
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));
432
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);
f43b9368 436 vty_out(vty, "\t\t\tTransmission interval: %" PRIu32 "ms\n",
c2f29cf3 437 bs->timers.desired_min_tx / 1000);
f43b9368
RZ
438 vty_out(vty, "\t\t\tEcho transmission interval: %" PRIu32 "ms\n",
439 bs->timers.required_min_echo / 1000);
c2f29cf3
RZ
440
441 vty_out(vty, "\t\tRemote timers:\n");
442 vty_out(vty, "\t\t\tReceive interval: %" PRIu32 "ms\n",
443 bs->remote_timers.required_min_rx / 1000);
444 vty_out(vty, "\t\t\tTransmission interval: %" PRIu32 "ms\n",
445 bs->remote_timers.desired_min_tx / 1000);
446 vty_out(vty, "\t\t\tEcho transmission interval: %" PRIu32 "ms\n",
447 bs->remote_timers.required_min_echo / 1000);
448
449 vty_out(vty, "\n");
450}
451
89a634c1 452static struct json_object *_peer_json_header(struct bfd_session *bs)
c2f29cf3
RZ
453{
454 struct json_object *jo = json_object_new_object();
79b4a6fc 455 char addr_buf[INET6_ADDRSTRLEN];
c2f29cf3 456
79b4a6fc 457 if (bs->key.mhop)
c2f29cf3 458 json_object_boolean_true_add(jo, "multihop");
79b4a6fc 459 else
c2f29cf3 460 json_object_boolean_false_add(jo, "multihop");
79b4a6fc
RZ
461
462 json_object_string_add(jo, "peer",
463 inet_ntop(bs->key.family, &bs->key.peer,
464 addr_buf, sizeof(addr_buf)));
465 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
466 json_object_string_add(jo, "local",
467 inet_ntop(bs->key.family, &bs->key.local,
468 addr_buf, sizeof(addr_buf)));
469
470 if (bs->key.vrfname[0])
471 json_object_string_add(jo, "vrf", bs->key.vrfname);
472 if (bs->key.ifname[0])
473 json_object_string_add(jo, "interface", bs->key.ifname);
c2f29cf3
RZ
474
475 if (bs->pl)
476 json_object_string_add(jo, "label", bs->pl->pl_label);
477
89a634c1
RZ
478 return jo;
479}
480
481static struct json_object *__display_peer_json(struct bfd_session *bs)
482{
483 struct json_object *jo = _peer_json_header(bs);
484
c2f29cf3
RZ
485 json_object_int_add(jo, "id", bs->discrs.my_discr);
486 json_object_int_add(jo, "remote-id", bs->discrs.remote_discr);
487
488 switch (bs->ses_state) {
489 case PTM_BFD_ADM_DOWN:
490 json_object_string_add(jo, "status", "shutdown");
491 break;
492 case PTM_BFD_DOWN:
493 json_object_string_add(jo, "status", "down");
494 json_object_int_add(jo, "downtime",
98ef9c16 495 monotime(NULL) - bs->downtime.tv_sec);
c2f29cf3
RZ
496 break;
497 case PTM_BFD_INIT:
498 json_object_string_add(jo, "status", "init");
499 break;
500 case PTM_BFD_UP:
501 json_object_string_add(jo, "status", "up");
502 json_object_int_add(jo, "uptime",
503 monotime(NULL) - bs->uptime.tv_sec);
504 break;
505
506 default:
507 json_object_string_add(jo, "status", "unknown");
508 break;
509 }
510
511 json_object_string_add(jo, "diagnostic", diag2str(bs->local_diag));
512 json_object_string_add(jo, "remote-diagnostic",
513 diag2str(bs->remote_diag));
514
515 json_object_int_add(jo, "receive-interval",
516 bs->timers.required_min_rx / 1000);
517 json_object_int_add(jo, "transmit-interval",
518 bs->timers.desired_min_tx / 1000);
519 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
520 json_object_int_add(jo, "echo-interval",
521 bs->timers.required_min_echo / 1000);
522 else
523 json_object_int_add(jo, "echo-interval", 0);
524
525 json_object_int_add(jo, "remote-receive-interval",
526 bs->remote_timers.required_min_rx / 1000);
527 json_object_int_add(jo, "remote-transmit-interval",
528 bs->remote_timers.desired_min_tx / 1000);
529 json_object_int_add(jo, "remote-echo-interval",
530 bs->remote_timers.required_min_echo / 1000);
531
532 return jo;
533}
534
535static void _display_peer_json(struct vty *vty, struct bfd_session *bs)
536{
537 struct json_object *jo = __display_peer_json(bs);
538
539 vty_out(vty, "%s\n", json_object_to_json_string_ext(jo, 0));
540 json_object_free(jo);
541}
542
e3b78da8 543static void _display_peer_iter(struct hash_bucket *hb, void *arg)
c2f29cf3
RZ
544{
545 struct vty *vty = arg;
546 struct bfd_session *bs = hb->data;
547
548 _display_peer(vty, bs);
549}
550
e3b78da8 551static void _display_peer_json_iter(struct hash_bucket *hb, void *arg)
c2f29cf3
RZ
552{
553 struct json_object *jo = arg, *jon = NULL;
554 struct bfd_session *bs = hb->data;
555
556 jon = __display_peer_json(bs);
557 if (jon == NULL) {
558 log_warning("%s: not enough memory", __func__);
559 return;
560 }
561
562 json_object_array_add(jo, jon);
563}
564
565static void _display_all_peers(struct vty *vty, bool use_json)
566{
567 struct json_object *jo;
568
d8729f8c 569 if (!use_json) {
89a634c1 570 vty_out(vty, "BFD Peers:\n");
c2f29cf3
RZ
571 bfd_id_iterate(_display_peer_iter, vty);
572 return;
573 }
574
575 jo = json_object_new_array();
576 bfd_id_iterate(_display_peer_json_iter, jo);
577
578 vty_out(vty, "%s\n", json_object_to_json_string_ext(jo, 0));
579 json_object_free(jo);
580}
581
0684c9b1
RZ
582static void _display_peer_counter(struct vty *vty, struct bfd_session *bs)
583{
584 _display_peer_header(vty, bs);
585
586 vty_out(vty, "\t\tControl packet input: %" PRIu64 " packets\n",
587 bs->stats.rx_ctrl_pkt);
588 vty_out(vty, "\t\tControl packet output: %" PRIu64 " packets\n",
589 bs->stats.tx_ctrl_pkt);
590 vty_out(vty, "\t\tEcho packet input: %" PRIu64 " packets\n",
591 bs->stats.rx_echo_pkt);
592 vty_out(vty, "\t\tEcho packet output: %" PRIu64 " packets\n",
593 bs->stats.tx_echo_pkt);
594 vty_out(vty, "\t\tSession up events: %" PRIu64 "\n",
595 bs->stats.session_up);
596 vty_out(vty, "\t\tSession down events: %" PRIu64 "\n",
597 bs->stats.session_down);
598 vty_out(vty, "\t\tZebra notifications: %" PRIu64 "\n",
599 bs->stats.znotification);
600 vty_out(vty, "\n");
601}
602
603static struct json_object *__display_peer_counters_json(struct bfd_session *bs)
604{
605 struct json_object *jo = _peer_json_header(bs);
606
607 json_object_int_add(jo, "control-packet-input", bs->stats.rx_ctrl_pkt);
608 json_object_int_add(jo, "control-packet-output", bs->stats.tx_ctrl_pkt);
609 json_object_int_add(jo, "echo-packet-input", bs->stats.rx_echo_pkt);
610 json_object_int_add(jo, "echo-packet-output", bs->stats.tx_echo_pkt);
611 json_object_int_add(jo, "session-up", bs->stats.session_up);
612 json_object_int_add(jo, "session-down", bs->stats.session_down);
613 json_object_int_add(jo, "zebra-notifications", bs->stats.znotification);
614
615 return jo;
616}
617
618static void _display_peer_counters_json(struct vty *vty, struct bfd_session *bs)
619{
620 struct json_object *jo = __display_peer_counters_json(bs);
621
622 vty_out(vty, "%s\n", json_object_to_json_string_ext(jo, 0));
623 json_object_free(jo);
624}
625
e3b78da8 626static void _display_peer_counter_iter(struct hash_bucket *hb, void *arg)
0684c9b1
RZ
627{
628 struct vty *vty = arg;
629 struct bfd_session *bs = hb->data;
630
631 _display_peer_counter(vty, bs);
632}
633
e3b78da8 634static void _display_peer_counter_json_iter(struct hash_bucket *hb, void *arg)
0684c9b1
RZ
635{
636 struct json_object *jo = arg, *jon = NULL;
637 struct bfd_session *bs = hb->data;
638
639 jon = __display_peer_counters_json(bs);
640 if (jon == NULL) {
641 log_warning("%s: not enough memory", __func__);
642 return;
643 }
644
645 json_object_array_add(jo, jon);
646}
647
648static void _display_peers_counter(struct vty *vty, bool use_json)
649{
650 struct json_object *jo;
651
d8729f8c 652 if (!use_json) {
0684c9b1
RZ
653 vty_out(vty, "BFD Peers:\n");
654 bfd_id_iterate(_display_peer_counter_iter, vty);
655 return;
656 }
657
658 jo = json_object_new_array();
659 bfd_id_iterate(_display_peer_counter_json_iter, jo);
660
661 vty_out(vty, "%s\n", json_object_to_json_string_ext(jo, 0));
662 json_object_free(jo);
663}
664
89a634c1
RZ
665static struct bfd_session *
666_find_peer_or_error(struct vty *vty, int argc, struct cmd_token **argv,
667 const char *label, const char *peer_str,
668 const char *local_str, const char *ifname,
669 const char *vrfname)
c2f29cf3
RZ
670{
671 int idx;
672 bool mhop;
673 struct bfd_session *bs = NULL;
674 struct peer_label *pl;
675 struct bfd_peer_cfg bpc;
676 struct sockaddr_any psa, lsa, *lsap;
677 char errormsg[128];
678
679 /* Look up the BFD peer. */
680 if (label) {
681 pl = pl_find(label);
682 if (pl)
683 bs = pl->pl_bs;
684 } else {
685 strtosa(peer_str, &psa);
89a634c1 686 if (local_str) {
c2f29cf3
RZ
687 strtosa(local_str, &lsa);
688 lsap = &lsa;
689 } else
690 lsap = NULL;
691
692 idx = 0;
693 mhop = argv_find(argv, argc, "multihop", &idx);
694
695 if (bfd_configure_peer(&bpc, mhop, &psa, lsap, ifname, vrfname,
696 errormsg, sizeof(errormsg))
697 != 0) {
698 vty_out(vty, "%% Invalid peer configuration: %s\n",
699 errormsg);
89a634c1 700 return NULL;
c2f29cf3
RZ
701 }
702
703 bs = bs_peer_find(&bpc);
704 }
705
706 /* Find peer data. */
707 if (bs == NULL) {
708 vty_out(vty, "%% Unable to find 'peer %s",
709 label ? label : peer_str);
710 if (ifname)
711 vty_out(vty, " interface %s", ifname);
89a634c1 712 if (local_str)
c2f29cf3
RZ
713 vty_out(vty, " local-address %s", local_str);
714 if (vrfname)
715 vty_out(vty, " vrf %s", vrfname);
716 vty_out(vty, "'\n");
717
89a634c1 718 return NULL;
c2f29cf3
RZ
719 }
720
89a634c1
RZ
721 return bs;
722}
723
724
725/*
726 * Show commands.
727 */
728DEFPY(bfd_show_peers, bfd_show_peers_cmd, "show bfd peers [json]",
729 SHOW_STR
730 "Bidirection Forwarding Detection\n"
731 "BFD peers status\n" JSON_STR)
732{
733 _display_all_peers(vty, use_json(argc, argv));
734
735 return CMD_SUCCESS;
736}
737
738DEFPY(bfd_show_peer, bfd_show_peer_cmd,
739 "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]",
740 SHOW_STR
741 "Bidirection Forwarding Detection\n"
742 "BFD peers status\n"
743 "Peer label\n" PEER_IPV4_STR PEER_IPV6_STR MHOP_STR LOCAL_STR
744 LOCAL_IPV4_STR LOCAL_IPV6_STR INTERFACE_STR LOCAL_INTF_STR VRF_STR
745 VRF_NAME_STR JSON_STR)
746{
747 struct bfd_session *bs;
748
749 /* Look up the BFD peer. */
750 bs = _find_peer_or_error(vty, argc, argv, label, peer_str, local_str,
751 ifname, vrfname);
752 if (bs == NULL)
753 return CMD_WARNING_CONFIG_FAILED;
754
c2f29cf3
RZ
755 if (use_json(argc, argv)) {
756 _display_peer_json(vty, bs);
757 } else {
758 vty_out(vty, "BFD Peer:\n");
759 _display_peer(vty, bs);
760 }
761
762 return CMD_SUCCESS;
763}
764
0684c9b1
RZ
765DEFPY(bfd_show_peer_counters, bfd_show_peer_counters_cmd,
766 "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]",
767 SHOW_STR
768 "Bidirection Forwarding Detection\n"
769 "BFD peers status\n"
770 "Peer label\n"
771 PEER_IPV4_STR
772 PEER_IPV6_STR
773 MHOP_STR
774 LOCAL_STR
775 LOCAL_IPV4_STR
776 LOCAL_IPV6_STR
777 INTERFACE_STR
778 LOCAL_INTF_STR
779 VRF_STR
780 VRF_NAME_STR
781 "Show BFD peer counters information\n"
782 JSON_STR)
783{
784 struct bfd_session *bs;
785
786 /* Look up the BFD peer. */
787 bs = _find_peer_or_error(vty, argc, argv, label, peer_str, local_str,
788 ifname, vrfname);
789 if (bs == NULL)
790 return CMD_WARNING_CONFIG_FAILED;
791
792 if (use_json(argc, argv))
793 _display_peer_counters_json(vty, bs);
794 else
795 _display_peer_counter(vty, bs);
796
797 return CMD_SUCCESS;
798}
799
800DEFPY(bfd_show_peers_counters, bfd_show_peers_counters_cmd,
801 "show bfd peers counters [json]",
802 SHOW_STR
803 "Bidirection Forwarding Detection\n"
804 "BFD peers status\n"
805 "Show BFD peer counters information\n"
806 JSON_STR)
807{
808 _display_peers_counter(vty, use_json(argc, argv));
809
810 return CMD_SUCCESS;
811}
812
c2f29cf3
RZ
813
814/*
815 * Function definitions.
816 */
817
818/*
819 * Configuration rules:
820 *
821 * Single hop:
8a9f760e 822 * peer + (interface name)
c2f29cf3
RZ
823 *
824 * Multi hop:
825 * peer + local + (optional vrf)
826 *
827 * Anything else is misconfiguration.
828 */
829static int bfd_configure_peer(struct bfd_peer_cfg *bpc, bool mhop,
830 const struct sockaddr_any *peer,
831 const struct sockaddr_any *local,
832 const char *ifname, const char *vrfname,
833 char *ebuf, size_t ebuflen)
834{
835 memset(bpc, 0, sizeof(*bpc));
836
837 /* Defaults */
838 bpc->bpc_shutdown = true;
839 bpc->bpc_detectmultiplier = BPC_DEF_DETECTMULTIPLIER;
840 bpc->bpc_recvinterval = BPC_DEF_RECEIVEINTERVAL;
841 bpc->bpc_txinterval = BPC_DEF_TRANSMITINTERVAL;
842 bpc->bpc_echointerval = BPC_DEF_ECHOINTERVAL;
843 bpc->bpc_lastevent = monotime(NULL);
844
845 /* Safety check: when no error buf is provided len must be zero. */
846 if (ebuf == NULL)
847 ebuflen = 0;
848
849 /* Peer is always mandatory. */
850 if (peer == NULL) {
851 snprintf(ebuf, ebuflen, "peer must not be empty");
852 return -1;
853 }
854
855 /* Validate address families. */
856 if (peer->sa_sin.sin_family == AF_INET) {
857 if (local && local->sa_sin.sin_family != AF_INET) {
858 snprintf(ebuf, ebuflen,
859 "local is IPv6, but peer is IPv4");
860 return -1;
861 }
862
863 bpc->bpc_ipv4 = true;
864 } else if (peer->sa_sin.sin_family == AF_INET6) {
865 if (local && local->sa_sin.sin_family != AF_INET6) {
866 snprintf(ebuf, ebuflen,
867 "local is IPv4, but peer is IPv6");
868 return -1;
869 }
870
871 bpc->bpc_ipv4 = false;
872 } else {
873 snprintf(ebuf, ebuflen, "invalid peer address family");
874 return -1;
875 }
876
877 /* Copy local and/or peer addresses. */
878 if (local)
879 bpc->bpc_local = *local;
880
4848ef74 881 bpc->bpc_peer = *peer;
c2f29cf3
RZ
882 bpc->bpc_mhop = mhop;
883
c2f29cf3
RZ
884 /* Handle interface specification configuration. */
885 if (ifname) {
886 if (bpc->bpc_mhop) {
887 snprintf(ebuf, ebuflen,
888 "multihop doesn't accept interface names");
889 return -1;
890 }
891
892 bpc->bpc_has_localif = true;
893 if (strlcpy(bpc->bpc_localif, ifname, sizeof(bpc->bpc_localif))
894 > sizeof(bpc->bpc_localif)) {
895 snprintf(ebuf, ebuflen, "interface name too long");
896 return -1;
897 }
898 }
899
900 /* Handle VRF configuration. */
901 if (vrfname) {
902 bpc->bpc_has_vrfname = true;
903 if (strlcpy(bpc->bpc_vrfname, vrfname, sizeof(bpc->bpc_vrfname))
904 > sizeof(bpc->bpc_vrfname)) {
905 snprintf(ebuf, ebuflen, "vrf name too long");
906 return -1;
907 }
908 }
909
910 return 0;
911}
912static int bfdd_write_config(struct vty *vty)
913{
914 vty_out(vty, "bfd\n");
915 vty_out(vty, "!\n");
916 return 0;
917}
918
d245e522 919static void _bfdd_peer_write_config(struct vty *vty, struct bfd_session *bs)
c2f29cf3 920{
79b4a6fc
RZ
921 char addr_buf[INET6_ADDRSTRLEN];
922
923 vty_out(vty, " peer %s",
924 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
925 sizeof(addr_buf)));
926
927 if (bs->key.mhop)
c2f29cf3 928 vty_out(vty, " multihop");
79b4a6fc
RZ
929
930 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
931 vty_out(vty, " local-address %s",
932 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
933 sizeof(addr_buf)));
934
935 if (bs->key.vrfname[0])
936 vty_out(vty, " vrf %s", bs->key.vrfname);
937 if (bs->key.ifname[0])
938 vty_out(vty, " interface %s", bs->key.ifname);
939 vty_out(vty, "\n");
c2f29cf3 940
d245e522 941 if (bs->sock == -1)
261e0ba9
RZ
942 vty_out(vty,
943 " ! vrf, interface or local-address doesn't exist\n");
d245e522 944
c2f29cf3
RZ
945 if (bs->detect_mult != BPC_DEF_DETECTMULTIPLIER)
946 vty_out(vty, " detect-multiplier %d\n", bs->detect_mult);
947 if (bs->timers.required_min_rx != (BPC_DEF_RECEIVEINTERVAL * 1000))
948 vty_out(vty, " receive-interval %" PRIu32 "\n",
949 bs->timers.required_min_rx / 1000);
f43b9368 950 if (bs->timers.desired_min_tx != (BPC_DEF_TRANSMITINTERVAL * 1000))
c2f29cf3 951 vty_out(vty, " transmit-interval %" PRIu32 "\n",
f43b9368 952 bs->timers.desired_min_tx / 1000);
c2f29cf3
RZ
953 if (bs->timers.required_min_echo != (BPC_DEF_ECHOINTERVAL * 1000))
954 vty_out(vty, " echo-interval %" PRIu32 "\n",
955 bs->timers.required_min_echo / 1000);
956 if (bs->pl)
957 vty_out(vty, " label %s\n", bs->pl->pl_label);
958 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
959 vty_out(vty, " echo-mode\n");
960
961 vty_out(vty, " %sshutdown\n",
962 BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN) ? "" : "no ");
963
964 vty_out(vty, " !\n");
965}
966
a244e599
DS
967DEFUN_NOSH(show_debugging_bfd,
968 show_debugging_bfd_cmd,
969 "show debugging [bfd]",
970 SHOW_STR
971 DEBUG_STR
972 "BFD daemon\n")
973{
974 vty_out(vty, "BFD debugging status:\n");
975
976 return CMD_SUCCESS;
977}
978
e3b78da8 979static void _bfdd_peer_write_config_iter(struct hash_bucket *hb, void *arg)
d245e522
RZ
980{
981 struct vty *vty = arg;
982 struct bfd_session *bs = hb->data;
983
6bdb4a42
PG
984 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG))
985 return;
986
d245e522
RZ
987 _bfdd_peer_write_config(vty, bs);
988}
989
c2f29cf3
RZ
990static int bfdd_peer_write_config(struct vty *vty)
991{
d245e522 992 bfd_id_iterate(_bfdd_peer_write_config_iter, vty);
d245e522 993
c2f29cf3
RZ
994 return 1;
995}
996
997struct cmd_node bfd_node = {
998 BFD_NODE,
999 "%s(config-bfd)# ",
1000 1,
1001};
1002
1003struct cmd_node bfd_peer_node = {
1004 BFD_PEER_NODE,
1005 "%s(config-bfd-peer)# ",
1006 1,
1007};
1008
1009void bfdd_vty_init(void)
1010{
0684c9b1
RZ
1011 install_element(ENABLE_NODE, &bfd_show_peers_counters_cmd);
1012 install_element(ENABLE_NODE, &bfd_show_peer_counters_cmd);
c2f29cf3
RZ
1013 install_element(ENABLE_NODE, &bfd_show_peers_cmd);
1014 install_element(ENABLE_NODE, &bfd_show_peer_cmd);
1015 install_element(CONFIG_NODE, &bfd_enter_cmd);
a244e599 1016 install_element(ENABLE_NODE, &show_debugging_bfd_cmd);
c2f29cf3
RZ
1017
1018 /* Install BFD node and commands. */
1019 install_node(&bfd_node, bfdd_write_config);
1020 install_default(BFD_NODE);
1021 install_element(BFD_NODE, &bfd_peer_enter_cmd);
1022 install_element(BFD_NODE, &bfd_no_peer_cmd);
1023
1024 /* Install BFD peer node. */
1025 install_node(&bfd_peer_node, bfdd_peer_write_config);
1026 install_default(BFD_PEER_NODE);
1027 install_element(BFD_PEER_NODE, &bfd_peer_detectmultiplier_cmd);
1028 install_element(BFD_PEER_NODE, &bfd_peer_recvinterval_cmd);
1029 install_element(BFD_PEER_NODE, &bfd_peer_txinterval_cmd);
1030 install_element(BFD_PEER_NODE, &bfd_peer_echointerval_cmd);
1031 install_element(BFD_PEER_NODE, &bfd_peer_shutdown_cmd);
1032 install_element(BFD_PEER_NODE, &bfd_peer_echo_cmd);
1033 install_element(BFD_PEER_NODE, &bfd_peer_label_cmd);
1034}