]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfdd_vty.c
Merge pull request #11643 from ARShreenidhi/df_or_ntw_auto
[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"
0287a64a 26#include "lib/northbound_cli.h"
c2f29cf3
RZ
27#include "lib/vty.h"
28
29#include "bfd.h"
30
31#ifndef VTYSH_EXTRACT_PL
32#include "bfdd/bfdd_vty_clippy.c"
33#endif
34
35/*
36 * Commands help string definitions.
37 */
c2f29cf3
RZ
38#define PEER_IPV4_STR "IPv4 peer address\n"
39#define PEER_IPV6_STR "IPv6 peer address\n"
40#define MHOP_STR "Configure multihop\n"
41#define LOCAL_STR "Configure local address\n"
42#define LOCAL_IPV4_STR "IPv4 local address\n"
43#define LOCAL_IPV6_STR "IPv6 local address\n"
44#define LOCAL_INTF_STR "Configure local interface name to use\n"
c2f29cf3
RZ
45
46/*
47 * Prototypes
48 */
c2f29cf3
RZ
49static int bfd_configure_peer(struct bfd_peer_cfg *bpc, bool mhop,
50 const struct sockaddr_any *peer,
51 const struct sockaddr_any *local,
52 const char *ifname, const char *vrfname,
53 char *ebuf, size_t ebuflen);
54
89a634c1 55static void _display_peer_header(struct vty *vty, struct bfd_session *bs);
c2f29cf3 56static struct json_object *__display_peer_json(struct bfd_session *bs);
89a634c1 57static struct json_object *_peer_json_header(struct bfd_session *bs);
c2f29cf3
RZ
58static void _display_peer_json(struct vty *vty, struct bfd_session *bs);
59static void _display_peer(struct vty *vty, struct bfd_session *bs);
9146cc2a 60static void _display_all_peers(struct vty *vty, char *vrfname, bool use_json);
e3b78da8
TB
61static void _display_peer_iter(struct hash_bucket *hb, void *arg);
62static void _display_peer_json_iter(struct hash_bucket *hb, void *arg);
0684c9b1
RZ
63static void _display_peer_counter(struct vty *vty, struct bfd_session *bs);
64static struct json_object *__display_peer_counters_json(struct bfd_session *bs);
65static void _display_peer_counters_json(struct vty *vty, struct bfd_session *bs);
e3b78da8
TB
66static void _display_peer_counter_iter(struct hash_bucket *hb, void *arg);
67static void _display_peer_counter_json_iter(struct hash_bucket *hb, void *arg);
6f374165 68static void _display_peers_counter(struct vty *vty, char *vrfname, bool use_json);
89a634c1
RZ
69static struct bfd_session *
70_find_peer_or_error(struct vty *vty, int argc, struct cmd_token **argv,
71 const char *label, const char *peer_str,
72 const char *local_str, const char *ifname,
73 const char *vrfname);
c2f29cf3 74
c2f29cf3
RZ
75
76/*
77 * Show commands helper functions
78 */
89a634c1 79static void _display_peer_header(struct vty *vty, struct bfd_session *bs)
c2f29cf3 80{
79b4a6fc
RZ
81 char addr_buf[INET6_ADDRSTRLEN];
82
83 vty_out(vty, "\tpeer %s",
84 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
85 sizeof(addr_buf)));
86
b88113ef 87 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
c2f29cf3 88 vty_out(vty, " multihop");
79b4a6fc
RZ
89
90 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
91 vty_out(vty, " local-address %s",
92 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
93 sizeof(addr_buf)));
94
95 if (bs->key.vrfname[0])
96 vty_out(vty, " vrf %s", bs->key.vrfname);
97 if (bs->key.ifname[0])
98 vty_out(vty, " interface %s", bs->key.ifname);
99 vty_out(vty, "\n");
c2f29cf3
RZ
100
101 if (bs->pl)
102 vty_out(vty, "\t\tlabel: %s\n", bs->pl->pl_label);
89a634c1
RZ
103}
104
105static void _display_peer(struct vty *vty, struct bfd_session *bs)
106{
107 char buf[256];
108 time_t now;
109
110 _display_peer_header(vty, bs);
c2f29cf3
RZ
111
112 vty_out(vty, "\t\tID: %u\n", bs->discrs.my_discr);
113 vty_out(vty, "\t\tRemote ID: %u\n", bs->discrs.remote_discr);
1a2e2fff
RZ
114 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE))
115 vty_out(vty, "\t\tPassive mode\n");
116 else
117 vty_out(vty, "\t\tActive mode\n");
262e1d25
RZ
118 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
119 vty_out(vty, "\t\tMinimum TTL: %d\n", bs->mh_ttl);
c2f29cf3
RZ
120
121 vty_out(vty, "\t\tStatus: ");
122 switch (bs->ses_state) {
123 case PTM_BFD_ADM_DOWN:
124 vty_out(vty, "shutdown\n");
125 break;
126 case PTM_BFD_DOWN:
127 vty_out(vty, "down\n");
128
129 now = monotime(NULL);
98ef9c16 130 integer2timestr(now - bs->downtime.tv_sec, buf, sizeof(buf));
c2f29cf3
RZ
131 vty_out(vty, "\t\tDowntime: %s\n", buf);
132 break;
133 case PTM_BFD_INIT:
134 vty_out(vty, "init\n");
135 break;
136 case PTM_BFD_UP:
137 vty_out(vty, "up\n");
138
139 now = monotime(NULL);
140 integer2timestr(now - bs->uptime.tv_sec, buf, sizeof(buf));
141 vty_out(vty, "\t\tUptime: %s\n", buf);
142 break;
143
144 default:
145 vty_out(vty, "unknown\n");
146 break;
147 }
148
149 vty_out(vty, "\t\tDiagnostics: %s\n", diag2str(bs->local_diag));
150 vty_out(vty, "\t\tRemote diagnostics: %s\n", diag2str(bs->remote_diag));
825e3b94 151 vty_out(vty, "\t\tPeer Type: %s\n",
b88113ef 152 CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG) ? "configured" : "dynamic");
c2f29cf3
RZ
153
154 vty_out(vty, "\t\tLocal timers:\n");
6cde4b45 155 vty_out(vty, "\t\t\tDetect-multiplier: %u\n",
825e3b94 156 bs->detect_mult);
6cde4b45 157 vty_out(vty, "\t\t\tReceive interval: %ums\n",
c2f29cf3 158 bs->timers.required_min_rx / 1000);
6cde4b45 159 vty_out(vty, "\t\t\tTransmission interval: %ums\n",
c2f29cf3 160 bs->timers.desired_min_tx / 1000);
4df3e31c
IR
161 if (bs->timers.required_min_echo_rx != 0)
162 vty_out(vty, "\t\t\tEcho receive interval: %ums\n",
163 bs->timers.required_min_echo_rx / 1000);
164 else
165 vty_out(vty, "\t\t\tEcho receive interval: disabled\n");
166 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
167 vty_out(vty, "\t\t\tEcho transmission interval: %ums\n",
168 bs->timers.desired_min_echo_tx / 1000);
169 else
170 vty_out(vty, "\t\t\tEcho transmission interval: disabled\n");
c2f29cf3
RZ
171
172 vty_out(vty, "\t\tRemote timers:\n");
6cde4b45 173 vty_out(vty, "\t\t\tDetect-multiplier: %u\n",
825e3b94 174 bs->remote_detect_mult);
6cde4b45 175 vty_out(vty, "\t\t\tReceive interval: %ums\n",
c2f29cf3 176 bs->remote_timers.required_min_rx / 1000);
6cde4b45 177 vty_out(vty, "\t\t\tTransmission interval: %ums\n",
c2f29cf3 178 bs->remote_timers.desired_min_tx / 1000);
4df3e31c
IR
179 if (bs->remote_timers.required_min_echo != 0)
180 vty_out(vty, "\t\t\tEcho receive interval: %ums\n",
181 bs->remote_timers.required_min_echo / 1000);
182 else
183 vty_out(vty, "\t\t\tEcho receive interval: disabled\n");
c2f29cf3
RZ
184
185 vty_out(vty, "\n");
186}
187
89a634c1 188static struct json_object *_peer_json_header(struct bfd_session *bs)
c2f29cf3
RZ
189{
190 struct json_object *jo = json_object_new_object();
79b4a6fc 191 char addr_buf[INET6_ADDRSTRLEN];
c2f29cf3 192
79b4a6fc 193 if (bs->key.mhop)
c2f29cf3 194 json_object_boolean_true_add(jo, "multihop");
79b4a6fc 195 else
c2f29cf3 196 json_object_boolean_false_add(jo, "multihop");
79b4a6fc
RZ
197
198 json_object_string_add(jo, "peer",
199 inet_ntop(bs->key.family, &bs->key.peer,
200 addr_buf, sizeof(addr_buf)));
201 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local)))
202 json_object_string_add(jo, "local",
203 inet_ntop(bs->key.family, &bs->key.local,
204 addr_buf, sizeof(addr_buf)));
205
206 if (bs->key.vrfname[0])
207 json_object_string_add(jo, "vrf", bs->key.vrfname);
208 if (bs->key.ifname[0])
209 json_object_string_add(jo, "interface", bs->key.ifname);
c2f29cf3
RZ
210
211 if (bs->pl)
212 json_object_string_add(jo, "label", bs->pl->pl_label);
213
89a634c1
RZ
214 return jo;
215}
216
217static struct json_object *__display_peer_json(struct bfd_session *bs)
218{
219 struct json_object *jo = _peer_json_header(bs);
220
c2f29cf3
RZ
221 json_object_int_add(jo, "id", bs->discrs.my_discr);
222 json_object_int_add(jo, "remote-id", bs->discrs.remote_discr);
1a2e2fff
RZ
223 json_object_boolean_add(jo, "passive-mode",
224 CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE));
262e1d25
RZ
225 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
226 json_object_int_add(jo, "minimum-ttl", bs->mh_ttl);
c2f29cf3
RZ
227
228 switch (bs->ses_state) {
229 case PTM_BFD_ADM_DOWN:
230 json_object_string_add(jo, "status", "shutdown");
231 break;
232 case PTM_BFD_DOWN:
233 json_object_string_add(jo, "status", "down");
234 json_object_int_add(jo, "downtime",
98ef9c16 235 monotime(NULL) - bs->downtime.tv_sec);
c2f29cf3
RZ
236 break;
237 case PTM_BFD_INIT:
238 json_object_string_add(jo, "status", "init");
239 break;
240 case PTM_BFD_UP:
241 json_object_string_add(jo, "status", "up");
242 json_object_int_add(jo, "uptime",
243 monotime(NULL) - bs->uptime.tv_sec);
244 break;
245
246 default:
247 json_object_string_add(jo, "status", "unknown");
248 break;
249 }
250
251 json_object_string_add(jo, "diagnostic", diag2str(bs->local_diag));
252 json_object_string_add(jo, "remote-diagnostic",
253 diag2str(bs->remote_diag));
254
255 json_object_int_add(jo, "receive-interval",
256 bs->timers.required_min_rx / 1000);
257 json_object_int_add(jo, "transmit-interval",
258 bs->timers.desired_min_tx / 1000);
4df3e31c
IR
259 json_object_int_add(jo, "echo-receive-interval",
260 bs->timers.required_min_echo_rx / 1000);
b88113ef 261 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
4df3e31c
IR
262 json_object_int_add(jo, "echo-transmit-interval",
263 bs->timers.desired_min_echo_tx / 1000);
c2f29cf3 264 else
4df3e31c 265 json_object_int_add(jo, "echo-transmit-interval", 0);
c2f29cf3 266
1d0a11e6
S
267 json_object_int_add(jo, "detect-multiplier", bs->detect_mult);
268
c2f29cf3
RZ
269 json_object_int_add(jo, "remote-receive-interval",
270 bs->remote_timers.required_min_rx / 1000);
271 json_object_int_add(jo, "remote-transmit-interval",
272 bs->remote_timers.desired_min_tx / 1000);
4df3e31c 273 json_object_int_add(jo, "remote-echo-receive-interval",
c2f29cf3 274 bs->remote_timers.required_min_echo / 1000);
1d0a11e6
S
275 json_object_int_add(jo, "remote-detect-multiplier",
276 bs->remote_detect_mult);
c2f29cf3
RZ
277
278 return jo;
279}
280
281static void _display_peer_json(struct vty *vty, struct bfd_session *bs)
282{
283 struct json_object *jo = __display_peer_json(bs);
284
4e37fcb6 285 vty_json(vty, jo);
c2f29cf3
RZ
286}
287
9146cc2a 288struct bfd_vrf_tuple {
fa6e709f 289 const char *vrfname;
9146cc2a
PG
290 struct vty *vty;
291 struct json_object *jo;
292};
293
e3b78da8 294static void _display_peer_iter(struct hash_bucket *hb, void *arg)
c2f29cf3 295{
9146cc2a
PG
296 struct bfd_vrf_tuple *bvt = (struct bfd_vrf_tuple *)arg;
297 struct vty *vty;
c2f29cf3
RZ
298 struct bfd_session *bs = hb->data;
299
9146cc2a
PG
300 if (!bvt)
301 return;
302 vty = bvt->vty;
303
304 if (bvt->vrfname) {
305 if (!bs->key.vrfname[0] ||
306 !strmatch(bs->key.vrfname, bvt->vrfname))
307 return;
308 }
c2f29cf3
RZ
309 _display_peer(vty, bs);
310}
311
e3b78da8 312static void _display_peer_json_iter(struct hash_bucket *hb, void *arg)
c2f29cf3 313{
9146cc2a
PG
314 struct bfd_vrf_tuple *bvt = (struct bfd_vrf_tuple *)arg;
315 struct json_object *jo, *jon = NULL;
c2f29cf3
RZ
316 struct bfd_session *bs = hb->data;
317
9146cc2a
PG
318 if (!bvt)
319 return;
320 jo = bvt->jo;
321
322 if (bvt->vrfname) {
323 if (!bs->key.vrfname[0] ||
324 !strmatch(bs->key.vrfname, bvt->vrfname))
325 return;
326 }
327
c2f29cf3
RZ
328 jon = __display_peer_json(bs);
329 if (jon == NULL) {
259b64eb 330 zlog_warn("%s: not enough memory", __func__);
c2f29cf3
RZ
331 return;
332 }
333
334 json_object_array_add(jo, jon);
335}
336
9146cc2a 337static void _display_all_peers(struct vty *vty, char *vrfname, bool use_json)
c2f29cf3
RZ
338{
339 struct json_object *jo;
1d0a11e6 340 struct bfd_vrf_tuple bvt = {0};
9146cc2a 341
9146cc2a 342 bvt.vrfname = vrfname;
c2f29cf3 343
d8729f8c 344 if (!use_json) {
9146cc2a 345 bvt.vty = vty;
89a634c1 346 vty_out(vty, "BFD Peers:\n");
9146cc2a 347 bfd_id_iterate(_display_peer_iter, &bvt);
c2f29cf3
RZ
348 return;
349 }
350
351 jo = json_object_new_array();
9146cc2a
PG
352 bvt.jo = jo;
353 bfd_id_iterate(_display_peer_json_iter, &bvt);
c2f29cf3 354
4e37fcb6 355 vty_json(vty, jo);
c2f29cf3
RZ
356}
357
0684c9b1
RZ
358static void _display_peer_counter(struct vty *vty, struct bfd_session *bs)
359{
360 _display_peer_header(vty, bs);
361
400632a9
RZ
362 /* Ask data plane for updated counters. */
363 if (bfd_dplane_update_session_counters(bs) == -1)
364 zlog_debug("%s: failed to update BFD session counters (%s)",
365 __func__, bs_to_string(bs));
366
0684c9b1
RZ
367 vty_out(vty, "\t\tControl packet input: %" PRIu64 " packets\n",
368 bs->stats.rx_ctrl_pkt);
369 vty_out(vty, "\t\tControl packet output: %" PRIu64 " packets\n",
370 bs->stats.tx_ctrl_pkt);
371 vty_out(vty, "\t\tEcho packet input: %" PRIu64 " packets\n",
372 bs->stats.rx_echo_pkt);
373 vty_out(vty, "\t\tEcho packet output: %" PRIu64 " packets\n",
374 bs->stats.tx_echo_pkt);
375 vty_out(vty, "\t\tSession up events: %" PRIu64 "\n",
376 bs->stats.session_up);
377 vty_out(vty, "\t\tSession down events: %" PRIu64 "\n",
378 bs->stats.session_down);
379 vty_out(vty, "\t\tZebra notifications: %" PRIu64 "\n",
380 bs->stats.znotification);
381 vty_out(vty, "\n");
382}
383
384static struct json_object *__display_peer_counters_json(struct bfd_session *bs)
385{
386 struct json_object *jo = _peer_json_header(bs);
387
400632a9
RZ
388 /* Ask data plane for updated counters. */
389 if (bfd_dplane_update_session_counters(bs) == -1)
390 zlog_debug("%s: failed to update BFD session counters (%s)",
391 __func__, bs_to_string(bs));
392
0684c9b1
RZ
393 json_object_int_add(jo, "control-packet-input", bs->stats.rx_ctrl_pkt);
394 json_object_int_add(jo, "control-packet-output", bs->stats.tx_ctrl_pkt);
395 json_object_int_add(jo, "echo-packet-input", bs->stats.rx_echo_pkt);
396 json_object_int_add(jo, "echo-packet-output", bs->stats.tx_echo_pkt);
397 json_object_int_add(jo, "session-up", bs->stats.session_up);
398 json_object_int_add(jo, "session-down", bs->stats.session_down);
399 json_object_int_add(jo, "zebra-notifications", bs->stats.znotification);
400
401 return jo;
402}
403
404static void _display_peer_counters_json(struct vty *vty, struct bfd_session *bs)
405{
406 struct json_object *jo = __display_peer_counters_json(bs);
407
4e37fcb6 408 vty_json(vty, jo);
0684c9b1
RZ
409}
410
e3b78da8 411static void _display_peer_counter_iter(struct hash_bucket *hb, void *arg)
0684c9b1 412{
6f374165
PG
413 struct bfd_vrf_tuple *bvt = arg;
414 struct vty *vty;
0684c9b1
RZ
415 struct bfd_session *bs = hb->data;
416
6f374165
PG
417 if (!bvt)
418 return;
419 vty = bvt->vty;
420
421 if (bvt->vrfname) {
422 if (!bs->key.vrfname[0] ||
423 !strmatch(bs->key.vrfname, bvt->vrfname))
424 return;
425 }
426
0684c9b1
RZ
427 _display_peer_counter(vty, bs);
428}
429
e3b78da8 430static void _display_peer_counter_json_iter(struct hash_bucket *hb, void *arg)
0684c9b1 431{
6f374165 432 struct json_object *jo, *jon = NULL;
0684c9b1 433 struct bfd_session *bs = hb->data;
6f374165
PG
434 struct bfd_vrf_tuple *bvt = arg;
435
436 if (!bvt)
437 return;
438 jo = bvt->jo;
439
440 if (bvt->vrfname) {
441 if (!bs->key.vrfname[0] ||
442 !strmatch(bs->key.vrfname, bvt->vrfname))
443 return;
444 }
0684c9b1
RZ
445
446 jon = __display_peer_counters_json(bs);
447 if (jon == NULL) {
259b64eb 448 zlog_warn("%s: not enough memory", __func__);
0684c9b1
RZ
449 return;
450 }
451
452 json_object_array_add(jo, jon);
453}
454
6f374165 455static void _display_peers_counter(struct vty *vty, char *vrfname, bool use_json)
0684c9b1
RZ
456{
457 struct json_object *jo;
1d0a11e6 458 struct bfd_vrf_tuple bvt = {0};
0684c9b1 459
6f374165 460 bvt.vrfname = vrfname;
d8729f8c 461 if (!use_json) {
6f374165 462 bvt.vty = vty;
0684c9b1 463 vty_out(vty, "BFD Peers:\n");
6f374165 464 bfd_id_iterate(_display_peer_counter_iter, &bvt);
0684c9b1
RZ
465 return;
466 }
467
468 jo = json_object_new_array();
6f374165 469 bvt.jo = jo;
1d923374 470 bfd_id_iterate(_display_peer_counter_json_iter, &bvt);
0684c9b1 471
4e37fcb6 472 vty_json(vty, jo);
0684c9b1
RZ
473}
474
2d73a326 475static void _clear_peer_counter(struct bfd_session *bs)
d6790033
S
476{
477 /* Clear only pkt stats, intention is not to loose system
478 events counters */
479 bs->stats.rx_ctrl_pkt = 0;
480 bs->stats.tx_ctrl_pkt = 0;
481 bs->stats.rx_echo_pkt = 0;
482 bs->stats.tx_echo_pkt = 0;
483}
484
fa6e709f
S
485static void _display_peer_brief(struct vty *vty, struct bfd_session *bs)
486{
487 char addr_buf[INET6_ADDRSTRLEN];
488
e9cea444 489 vty_out(vty, "%-10u", bs->discrs.my_discr);
490 inet_ntop(bs->key.family, &bs->key.local, addr_buf, sizeof(addr_buf));
491 vty_out(vty, " %-40s", addr_buf);
492 inet_ntop(bs->key.family, &bs->key.peer, addr_buf, sizeof(addr_buf));
493 vty_out(vty, " %-40s", addr_buf);
494 vty_out(vty, "%-15s\n", state_list[bs->ses_state].str);
fa6e709f
S
495}
496
7f5818fb 497static void _display_peer_brief_iter(struct hash_bucket *hb, void *arg)
fa6e709f
S
498{
499 struct bfd_vrf_tuple *bvt = arg;
500 struct vty *vty;
501 struct bfd_session *bs = hb->data;
502
503 if (!bvt)
504 return;
505 vty = bvt->vty;
506
507 if (bvt->vrfname) {
508 if (!bs->key.vrfname[0] ||
509 !strmatch(bs->key.vrfname, bvt->vrfname))
510 return;
511 }
512
513 _display_peer_brief(vty, bs);
514}
515
516static void _display_peers_brief(struct vty *vty, const char *vrfname, bool use_json)
517{
518 struct json_object *jo;
1d0a11e6 519 struct bfd_vrf_tuple bvt = {0};
fa6e709f 520
fa6e709f
S
521 bvt.vrfname = vrfname;
522
a8f58eb6 523 if (!use_json) {
fa6e709f
S
524 bvt.vty = vty;
525
526 vty_out(vty, "Session count: %lu\n", bfd_get_session_count());
527 vty_out(vty, "%-10s", "SessionId");
528 vty_out(vty, " %-40s", "LocalAddress");
529 vty_out(vty, " %-40s", "PeerAddress");
530 vty_out(vty, "%-15s\n", "Status");
531
532 vty_out(vty, "%-10s", "=========");
533 vty_out(vty, " %-40s", "============");
534 vty_out(vty, " %-40s", "===========");
535 vty_out(vty, "%-15s\n", "======");
536
537 bfd_id_iterate(_display_peer_brief_iter, &bvt);
538 return;
539 }
540
541 jo = json_object_new_array();
542 bvt.jo = jo;
543
544 bfd_id_iterate(_display_peer_json_iter, &bvt);
545
4e37fcb6 546 vty_json(vty, jo);
fa6e709f
S
547}
548
89a634c1
RZ
549static struct bfd_session *
550_find_peer_or_error(struct vty *vty, int argc, struct cmd_token **argv,
551 const char *label, const char *peer_str,
552 const char *local_str, const char *ifname,
553 const char *vrfname)
c2f29cf3
RZ
554{
555 int idx;
556 bool mhop;
557 struct bfd_session *bs = NULL;
558 struct peer_label *pl;
559 struct bfd_peer_cfg bpc;
560 struct sockaddr_any psa, lsa, *lsap;
561 char errormsg[128];
562
563 /* Look up the BFD peer. */
564 if (label) {
565 pl = pl_find(label);
566 if (pl)
567 bs = pl->pl_bs;
c3634e44 568 } else if (peer_str) {
c2f29cf3 569 strtosa(peer_str, &psa);
89a634c1 570 if (local_str) {
c2f29cf3
RZ
571 strtosa(local_str, &lsa);
572 lsap = &lsa;
573 } else
574 lsap = NULL;
575
576 idx = 0;
577 mhop = argv_find(argv, argc, "multihop", &idx);
578
579 if (bfd_configure_peer(&bpc, mhop, &psa, lsap, ifname, vrfname,
580 errormsg, sizeof(errormsg))
581 != 0) {
582 vty_out(vty, "%% Invalid peer configuration: %s\n",
583 errormsg);
89a634c1 584 return NULL;
c2f29cf3
RZ
585 }
586
587 bs = bs_peer_find(&bpc);
c3634e44
IR
588 } else {
589 vty_out(vty, "%% Invalid arguments\n");
590 return NULL;
c2f29cf3
RZ
591 }
592
593 /* Find peer data. */
594 if (bs == NULL) {
595 vty_out(vty, "%% Unable to find 'peer %s",
596 label ? label : peer_str);
597 if (ifname)
598 vty_out(vty, " interface %s", ifname);
89a634c1 599 if (local_str)
c2f29cf3
RZ
600 vty_out(vty, " local-address %s", local_str);
601 if (vrfname)
602 vty_out(vty, " vrf %s", vrfname);
603 vty_out(vty, "'\n");
604
89a634c1 605 return NULL;
c2f29cf3
RZ
606 }
607
89a634c1
RZ
608 return bs;
609}
610
611
612/*
613 * Show commands.
614 */
1e81afc3 615DEFPY(bfd_show_peers, bfd_show_peers_cmd, "show bfd [vrf NAME] peers [json]",
89a634c1
RZ
616 SHOW_STR
617 "Bidirection Forwarding Detection\n"
9146cc2a 618 VRF_CMD_HELP_STR
89a634c1
RZ
619 "BFD peers status\n" JSON_STR)
620{
9146cc2a
PG
621 char *vrf_name = NULL;
622 int idx_vrf = 0;
623
624 if (argv_find(argv, argc, "vrf", &idx_vrf))
625 vrf_name = argv[idx_vrf + 1]->arg;
626
627 _display_all_peers(vty, vrf_name, use_json(argc, argv));
89a634c1
RZ
628
629 return CMD_SUCCESS;
630}
631
632DEFPY(bfd_show_peer, bfd_show_peer_cmd,
1e81afc3 633 "show bfd [vrf NAME$vrf_name] 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}]> [json]",
89a634c1
RZ
634 SHOW_STR
635 "Bidirection Forwarding Detection\n"
533ba31f 636 VRF_CMD_HELP_STR
89a634c1
RZ
637 "BFD peers status\n"
638 "Peer label\n" PEER_IPV4_STR PEER_IPV6_STR MHOP_STR LOCAL_STR
533ba31f 639 LOCAL_IPV4_STR LOCAL_IPV6_STR INTERFACE_STR LOCAL_INTF_STR JSON_STR)
89a634c1
RZ
640{
641 struct bfd_session *bs;
642
643 /* Look up the BFD peer. */
644 bs = _find_peer_or_error(vty, argc, argv, label, peer_str, local_str,
e429a2a0 645 ifname, vrf_name);
89a634c1
RZ
646 if (bs == NULL)
647 return CMD_WARNING_CONFIG_FAILED;
648
c2f29cf3
RZ
649 if (use_json(argc, argv)) {
650 _display_peer_json(vty, bs);
651 } else {
652 vty_out(vty, "BFD Peer:\n");
653 _display_peer(vty, bs);
654 }
655
656 return CMD_SUCCESS;
657}
658
0684c9b1 659DEFPY(bfd_show_peer_counters, bfd_show_peer_counters_cmd,
1e81afc3 660 "show bfd [vrf NAME$vrf_name] 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}]> counters [json]",
0684c9b1
RZ
661 SHOW_STR
662 "Bidirection Forwarding Detection\n"
533ba31f 663 VRF_CMD_HELP_STR
0684c9b1
RZ
664 "BFD peers status\n"
665 "Peer label\n"
666 PEER_IPV4_STR
667 PEER_IPV6_STR
668 MHOP_STR
669 LOCAL_STR
670 LOCAL_IPV4_STR
671 LOCAL_IPV6_STR
672 INTERFACE_STR
673 LOCAL_INTF_STR
0684c9b1
RZ
674 "Show BFD peer counters information\n"
675 JSON_STR)
676{
677 struct bfd_session *bs;
678
679 /* Look up the BFD peer. */
680 bs = _find_peer_or_error(vty, argc, argv, label, peer_str, local_str,
e429a2a0 681 ifname, vrf_name);
0684c9b1
RZ
682 if (bs == NULL)
683 return CMD_WARNING_CONFIG_FAILED;
684
685 if (use_json(argc, argv))
686 _display_peer_counters_json(vty, bs);
687 else
688 _display_peer_counter(vty, bs);
689
690 return CMD_SUCCESS;
691}
692
693DEFPY(bfd_show_peers_counters, bfd_show_peers_counters_cmd,
1e81afc3 694 "show bfd [vrf NAME] peers counters [json]",
0684c9b1
RZ
695 SHOW_STR
696 "Bidirection Forwarding Detection\n"
6f374165 697 VRF_CMD_HELP_STR
0684c9b1
RZ
698 "BFD peers status\n"
699 "Show BFD peer counters information\n"
700 JSON_STR)
701{
6f374165
PG
702 char *vrf_name = NULL;
703 int idx_vrf = 0;
704
705 if (argv_find(argv, argc, "vrf", &idx_vrf))
706 vrf_name = argv[idx_vrf + 1]->arg;
707
708 _display_peers_counter(vty, vrf_name, use_json(argc, argv));
0684c9b1
RZ
709
710 return CMD_SUCCESS;
711}
712
d6790033
S
713DEFPY(bfd_clear_peer_counters, bfd_clear_peer_counters_cmd,
714 "clear bfd [vrf <NAME$vrfname>] 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}]> counters",
715 SHOW_STR
716 "Bidirection Forwarding Detection\n"
717 VRF_CMD_HELP_STR
718 "BFD peers status\n"
719 "Peer label\n"
720 PEER_IPV4_STR
721 PEER_IPV6_STR
722 MHOP_STR
723 LOCAL_STR
724 LOCAL_IPV4_STR
725 LOCAL_IPV6_STR
726 INTERFACE_STR
727 LOCAL_INTF_STR
728 "clear BFD peer counters information\n")
729{
730 struct bfd_session *bs;
731
732 /* Look up the BFD peer. */
733 bs = _find_peer_or_error(vty, argc, argv, label, peer_str, local_str,
734 ifname, vrfname);
735 if (bs == NULL)
736 return CMD_WARNING_CONFIG_FAILED;
2d73a326 737
d6790033
S
738 _clear_peer_counter(bs);
739
740 return CMD_SUCCESS;
741}
c2f29cf3 742
fa6e709f
S
743DEFPY(bfd_show_peers_brief, bfd_show_peers_brief_cmd,
744 "show bfd [vrf <NAME$vrfname>] peers brief [json]",
745 SHOW_STR
746 "Bidirection Forwarding Detection\n"
747 VRF_CMD_HELP_STR
748 "BFD peers status\n"
749 "Show BFD peer information in tabular form\n"
750 JSON_STR)
751{
1d0a11e6
S
752 char *vrf_name = NULL;
753 int idx_vrf = 0;
754
755 if (argv_find(argv, argc, "vrf", &idx_vrf))
756 vrf_name = argv[idx_vrf + 1]->arg;
757
758 _display_peers_brief(vty, vrf_name, use_json(argc, argv));
fa6e709f
S
759
760 return CMD_SUCCESS;
761}
762
400632a9
RZ
763DEFPY(show_bfd_distributed, show_bfd_distributed_cmd,
764 "show bfd distributed",
765 SHOW_STR
766 "Bidirection Forwarding Detection\n"
767 "Show BFD data plane (distributed BFD) statistics\n")
768{
769 bfd_dplane_show_counters(vty);
770 return CMD_SUCCESS;
771}
772
8ba709dd
RZ
773DEFPY(
774 bfd_debug_distributed, bfd_debug_distributed_cmd,
775 "[no] debug bfd distributed",
776 NO_STR
777 DEBUG_STR
778 "Bidirection Forwarding Detection\n"
779 "BFD data plane (distributed BFD) debugging\n")
780{
781 bglobal.debug_dplane = !no;
782 return CMD_SUCCESS;
783}
784
48da2c31
RZ
785DEFPY(
786 bfd_debug_peer, bfd_debug_peer_cmd,
787 "[no] debug bfd peer",
788 NO_STR
789 DEBUG_STR
790 "Bidirection Forwarding Detection\n"
791 "Peer events debugging\n")
792{
793 bglobal.debug_peer_event = !no;
794 return CMD_SUCCESS;
795}
796
797DEFPY(
798 bfd_debug_zebra, bfd_debug_zebra_cmd,
799 "[no] debug bfd zebra",
800 NO_STR
801 DEBUG_STR
802 "Bidirection Forwarding Detection\n"
803 "Zebra events debugging\n")
804{
805 bglobal.debug_zebra = !no;
806 return CMD_SUCCESS;
807}
808
809DEFPY(
810 bfd_debug_network, bfd_debug_network_cmd,
811 "[no] debug bfd network",
812 NO_STR
813 DEBUG_STR
814 "Bidirection Forwarding Detection\n"
815 "Network layer debugging\n")
816{
817 bglobal.debug_network = !no;
818 return CMD_SUCCESS;
819}
820
c2f29cf3
RZ
821/*
822 * Function definitions.
823 */
824
825/*
826 * Configuration rules:
827 *
828 * Single hop:
8a9f760e 829 * peer + (interface name)
c2f29cf3
RZ
830 *
831 * Multi hop:
832 * peer + local + (optional vrf)
833 *
834 * Anything else is misconfiguration.
835 */
836static int bfd_configure_peer(struct bfd_peer_cfg *bpc, bool mhop,
837 const struct sockaddr_any *peer,
838 const struct sockaddr_any *local,
839 const char *ifname, const char *vrfname,
840 char *ebuf, size_t ebuflen)
841{
842 memset(bpc, 0, sizeof(*bpc));
843
844 /* Defaults */
59dad813 845 bpc->bpc_shutdown = false;
c2f29cf3
RZ
846 bpc->bpc_detectmultiplier = BPC_DEF_DETECTMULTIPLIER;
847 bpc->bpc_recvinterval = BPC_DEF_RECEIVEINTERVAL;
848 bpc->bpc_txinterval = BPC_DEF_TRANSMITINTERVAL;
4df3e31c
IR
849 bpc->bpc_echorecvinterval = BPC_DEF_ECHORECEIVEINTERVAL;
850 bpc->bpc_echotxinterval = BPC_DEF_ECHOTRANSMITINTERVAL;
c2f29cf3
RZ
851 bpc->bpc_lastevent = monotime(NULL);
852
853 /* Safety check: when no error buf is provided len must be zero. */
854 if (ebuf == NULL)
855 ebuflen = 0;
856
857 /* Peer is always mandatory. */
858 if (peer == NULL) {
859 snprintf(ebuf, ebuflen, "peer must not be empty");
860 return -1;
861 }
862
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");
868 return -1;
869 }
870
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");
876 return -1;
877 }
878
879 bpc->bpc_ipv4 = false;
880 } else {
881 snprintf(ebuf, ebuflen, "invalid peer address family");
882 return -1;
883 }
884
885 /* Copy local and/or peer addresses. */
886 if (local)
887 bpc->bpc_local = *local;
888
4848ef74 889 bpc->bpc_peer = *peer;
c2f29cf3
RZ
890 bpc->bpc_mhop = mhop;
891
c2f29cf3
RZ
892 /* Handle interface specification configuration. */
893 if (ifname) {
c2f29cf3
RZ
894 bpc->bpc_has_localif = true;
895 if (strlcpy(bpc->bpc_localif, ifname, sizeof(bpc->bpc_localif))
896 > sizeof(bpc->bpc_localif)) {
897 snprintf(ebuf, ebuflen, "interface name too long");
898 return -1;
899 }
900 }
901
902 /* Handle VRF configuration. */
903 if (vrfname) {
904 bpc->bpc_has_vrfname = true;
905 if (strlcpy(bpc->bpc_vrfname, vrfname, sizeof(bpc->bpc_vrfname))
906 > sizeof(bpc->bpc_vrfname)) {
907 snprintf(ebuf, ebuflen, "vrf name too long");
908 return -1;
909 }
e0f36c91
PG
910 } else {
911 bpc->bpc_has_vrfname = true;
912 strlcpy(bpc->bpc_vrfname, VRF_DEFAULT_NAME, sizeof(bpc->bpc_vrfname));
c2f29cf3
RZ
913 }
914
915 return 0;
916}
c2f29cf3 917
a244e599
DS
918DEFUN_NOSH(show_debugging_bfd,
919 show_debugging_bfd_cmd,
920 "show debugging [bfd]",
921 SHOW_STR
922 DEBUG_STR
923 "BFD daemon\n")
924{
925 vty_out(vty, "BFD debugging status:\n");
8ba709dd
RZ
926 if (bglobal.debug_dplane)
927 vty_out(vty, " Distributed BFD debugging is on.\n");
9be4b18a
PG
928 if (bglobal.debug_peer_event)
929 vty_out(vty, " Peer events debugging is on.\n");
930 if (bglobal.debug_zebra)
931 vty_out(vty, " Zebra events debugging is on.\n");
932 if (bglobal.debug_network)
933 vty_out(vty, " Network layer debugging is on.\n");
a244e599
DS
934
935 return CMD_SUCCESS;
936}
937
612c2c15 938static int bfdd_write_config(struct vty *vty);
c2f29cf3 939struct cmd_node bfd_node = {
f4b8291f 940 .name = "bfd",
62b346ee 941 .node = BFD_NODE,
24389580 942 .parent_node = CONFIG_NODE,
62b346ee 943 .prompt = "%s(config-bfd)# ",
612c2c15 944 .config_write = bfdd_write_config,
c2f29cf3
RZ
945};
946
947struct cmd_node bfd_peer_node = {
f4b8291f 948 .name = "bfd peer",
62b346ee 949 .node = BFD_PEER_NODE,
24389580 950 .parent_node = BFD_NODE,
62b346ee 951 .prompt = "%s(config-bfd-peer)# ",
c2f29cf3
RZ
952};
953
0287a64a
RZ
954static int bfdd_write_config(struct vty *vty)
955{
956 struct lyd_node *dnode;
957 int written = 0;
958
8ba709dd
RZ
959 if (bglobal.debug_dplane) {
960 vty_out(vty, "debug bfd distributed\n");
961 written = 1;
962 }
963
48da2c31
RZ
964 if (bglobal.debug_peer_event) {
965 vty_out(vty, "debug bfd peer\n");
966 written = 1;
967 }
968
969 if (bglobal.debug_zebra) {
970 vty_out(vty, "debug bfd zebra\n");
971 written = 1;
972 }
973
974 if (bglobal.debug_network) {
975 vty_out(vty, "debug bfd network\n");
976 written = 1;
977 }
978
0287a64a
RZ
979 dnode = yang_dnode_get(running_config->dnode, "/frr-bfdd:bfdd");
980 if (dnode) {
981 nb_cli_show_dnode_cmds(vty, dnode, false);
982 written = 1;
983 }
984
985 return written;
986}
987
c2f29cf3
RZ
988void bfdd_vty_init(void)
989{
0684c9b1
RZ
990 install_element(ENABLE_NODE, &bfd_show_peers_counters_cmd);
991 install_element(ENABLE_NODE, &bfd_show_peer_counters_cmd);
d6790033 992 install_element(ENABLE_NODE, &bfd_clear_peer_counters_cmd);
c2f29cf3
RZ
993 install_element(ENABLE_NODE, &bfd_show_peers_cmd);
994 install_element(ENABLE_NODE, &bfd_show_peer_cmd);
fa6e709f 995 install_element(ENABLE_NODE, &bfd_show_peers_brief_cmd);
400632a9 996 install_element(ENABLE_NODE, &show_bfd_distributed_cmd);
a244e599 997 install_element(ENABLE_NODE, &show_debugging_bfd_cmd);
c2f29cf3 998
8ba709dd 999 install_element(ENABLE_NODE, &bfd_debug_distributed_cmd);
48da2c31
RZ
1000 install_element(ENABLE_NODE, &bfd_debug_peer_cmd);
1001 install_element(ENABLE_NODE, &bfd_debug_zebra_cmd);
1002 install_element(ENABLE_NODE, &bfd_debug_network_cmd);
1003
8ba709dd 1004 install_element(CONFIG_NODE, &bfd_debug_distributed_cmd);
48da2c31
RZ
1005 install_element(CONFIG_NODE, &bfd_debug_peer_cmd);
1006 install_element(CONFIG_NODE, &bfd_debug_zebra_cmd);
1007 install_element(CONFIG_NODE, &bfd_debug_network_cmd);
1008
c2f29cf3 1009 /* Install BFD node and commands. */
612c2c15 1010 install_node(&bfd_node);
c2f29cf3 1011 install_default(BFD_NODE);
c2f29cf3
RZ
1012
1013 /* Install BFD peer node. */
612c2c15 1014 install_node(&bfd_peer_node);
c2f29cf3 1015 install_default(BFD_PEER_NODE);
adc26455
RZ
1016
1017 bfdd_cli_init();
c2f29cf3 1018}