summaryrefslogtreecommitdiffstats
path: root/vrrpd/vrrp_vty.c
blob: 3165ea119af243b45444e1c301ae311e7a521b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
 * VRRP CLI commands.
 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
 * Quentin Young
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <zebra.h>

#include "lib/command.h"
#include "lib/if.h"
#include "lib/ipaddr.h"
#include "lib/json.h"
#include "lib/northbound_cli.h"
#include "lib/prefix.h"
#include "lib/termtable.h"
#include "lib/vty.h"
#include "lib/vrf.h"

#include "vrrp.h"
#include "vrrp_debug.h"
#include "vrrp_vty.h"
#include "vrrp_zebra.h"
#ifndef VTYSH_EXTRACT_PL
#include "vrrpd/vrrp_vty_clippy.c"
#endif


#define VRRP_STR "Virtual Router Redundancy Protocol\n"
#define VRRP_VRID_STR "Virtual Router ID\n"
#define VRRP_PRIORITY_STR "Virtual Router Priority\n"
#define VRRP_ADVINT_STR "Virtual Router Advertisement Interval\n"
#define VRRP_IP_STR "Virtual Router IP address\n"
#define VRRP_VERSION_STR "VRRP protocol version\n"

#define VRRP_XPATH_ENTRY VRRP_XPATH "[virtual-router-id='%ld']"

/* clang-format off */

/*
 * XPath: /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group
 */
DEFPY_YANG(vrrp_vrid,
      vrrp_vrid_cmd,
      "[no] vrrp (1-255)$vrid [version (2-3)]",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      VRRP_VERSION_STR
      VRRP_VERSION_STR)
{
	char valbuf[20];

	snprintf(valbuf, sizeof(valbuf), "%ld", version ? version : vd.version);

	if (no)
		nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
	else {
		nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, NULL);
		nb_cli_enqueue_change(vty, "./version", NB_OP_MODIFY, valbuf);
	}

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_vrrp(struct vty *vty, struct lyd_node *dnode, bool show_defaults)
{
	const char *vrid = yang_dnode_get_string(dnode, "./virtual-router-id");
	const char *ver = yang_dnode_get_string(dnode, "./version");

	vty_out(vty, " vrrp %s", vrid);
	if (show_defaults || !yang_dnode_is_default(dnode, "./version"))
		vty_out(vty, " version %s", ver);
	vty_out(vty, "\n");
}

/*
 * XPath: /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/shutdown
 */
DEFPY_YANG(vrrp_shutdown,
      vrrp_shutdown_cmd,
      "[no] vrrp (1-255)$vrid shutdown",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      "Force VRRP router into administrative shutdown\n")
{
	nb_cli_enqueue_change(vty, "./shutdown", NB_OP_MODIFY,
			      no ? "false" : "true");

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_shutdown(struct vty *vty, struct lyd_node *dnode,
		       bool show_defaults)
{
	const char *vrid = yang_dnode_get_string(dnode, "../virtual-router-id");
	const bool shut = yang_dnode_get_bool(dnode, NULL);

	vty_out(vty, " %svrrp %s shutdown\n", shut ? "" : "no ", vrid);
}

/*
 * XPath: /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/priority
 */
DEFPY_YANG(vrrp_priority,
      vrrp_priority_cmd,
      "vrrp (1-255)$vrid priority (1-254)",
      VRRP_STR
      VRRP_VRID_STR
      VRRP_PRIORITY_STR
      "Priority value")
{
	nb_cli_enqueue_change(vty, "./priority", NB_OP_MODIFY, priority_str);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

/*
 * XPath: /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/priority
 */
DEFPY_YANG(no_vrrp_priority,
      no_vrrp_priority_cmd,
      "no vrrp (1-255)$vrid priority [(1-254)]",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      VRRP_PRIORITY_STR
      "Priority value")
{
	nb_cli_enqueue_change(vty, "./priority", NB_OP_MODIFY, NULL);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_priority(struct vty *vty, struct lyd_node *dnode,
		       bool show_defaults)
{
	const char *vrid = yang_dnode_get_string(dnode, "../virtual-router-id");
	const char *prio = yang_dnode_get_string(dnode, NULL);

	vty_out(vty, " vrrp %s priority %s\n", vrid, prio);
}

/*
 * XPath:
 * /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/advertisement-interval
 */
DEFPY_YANG(vrrp_advertisement_interval,
      vrrp_advertisement_interval_cmd,
      "vrrp (1-255)$vrid advertisement-interval (10-40950)",
      VRRP_STR VRRP_VRID_STR VRRP_ADVINT_STR
      "Advertisement interval in milliseconds; must be multiple of 10")
{
	char val[20];

	/* all internal computations are in centiseconds */
	advertisement_interval /= CS2MS;
	snprintf(val, sizeof(val), "%ld", advertisement_interval);
	nb_cli_enqueue_change(vty, "./advertisement-interval", NB_OP_MODIFY,
			      val);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

/*
 * XPath:
 * /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/advertisement-interval
 */
DEFPY_YANG(no_vrrp_advertisement_interval,
      no_vrrp_advertisement_interval_cmd,
      "no vrrp (1-255)$vrid advertisement-interval [(10-40950)]",
      NO_STR VRRP_STR VRRP_VRID_STR VRRP_ADVINT_STR
      "Advertisement interval in milliseconds; must be multiple of 10")
{
	nb_cli_enqueue_change(vty, "./advertisement-interval", NB_OP_MODIFY,
			      NULL);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_advertisement_interval(struct vty *vty, struct lyd_node *dnode,
				     bool show_defaults)
{
	const char *vrid = yang_dnode_get_string(dnode, "../virtual-router-id");
	uint16_t advint = yang_dnode_get_uint16(dnode, NULL);

	vty_out(vty, " vrrp %s advertisement-interval %u\n", vrid,
		advint * CS2MS);
}

/*
 * XPath:
 * /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/v4/virtual-address
 */
DEFPY_YANG(vrrp_ip,
      vrrp_ip_cmd,
      "[no] vrrp (1-255)$vrid ip A.B.C.D",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      "Add IPv4 address\n"
      VRRP_IP_STR)
{
	int op = no ? NB_OP_DESTROY : NB_OP_CREATE;
	nb_cli_enqueue_change(vty, "./v4/virtual-address", op, ip_str);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_ip(struct vty *vty, struct lyd_node *dnode, bool show_defaults)
{
	const char *vrid =
		yang_dnode_get_string(dnode, "../../virtual-router-id");
	const char *ipv4 = yang_dnode_get_string(dnode, NULL);

	vty_out(vty, " vrrp %s ip %s\n", vrid, ipv4);
}

/*
 * XPath:
 * /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/v6/virtual-address
 */
DEFPY_YANG(vrrp_ip6,
      vrrp_ip6_cmd,
      "[no] vrrp (1-255)$vrid ipv6 X:X::X:X",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      "Add IPv6 address\n"
      VRRP_IP_STR)
{
	int op = no ? NB_OP_DESTROY : NB_OP_CREATE;
	nb_cli_enqueue_change(vty, "./v6/virtual-address", op, ipv6_str);

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_ipv6(struct vty *vty, struct lyd_node *dnode, bool show_defaults)
{
	const char *vrid =
		yang_dnode_get_string(dnode, "../../virtual-router-id");
	const char *ipv6 = yang_dnode_get_string(dnode, NULL);

	vty_out(vty, " vrrp %s ipv6 %s\n", vrid, ipv6);
}

/*
 * XPath: /frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group/preempt
 */
DEFPY_YANG(vrrp_preempt,
      vrrp_preempt_cmd,
      "[no] vrrp (1-255)$vrid preempt",
      NO_STR
      VRRP_STR
      VRRP_VRID_STR
      "Preempt mode\n")
{
	nb_cli_enqueue_change(vty, "./preempt", NB_OP_MODIFY,
			      no ? "false" : "true");

	return nb_cli_apply_changes(vty, VRRP_XPATH_ENTRY, vrid);
}

void cli_show_preempt(struct vty *vty, struct lyd_node *dnode,
		      bool show_defaults)
{
	const char *vrid = yang_dnode_get_string(dnode, "../virtual-router-id");
	const bool pre = yang_dnode_get_bool(dnode, NULL);

	vty_out(vty, " %svrrp %s preempt\n", pre ? "" : "no ", vrid);
}

/* XXX: yang conversion */
DEFPY_YANG(vrrp_autoconfigure,
      vrrp_autoconfigure_cmd,
      "[no] vrrp autoconfigure [version (2-3)]",
      NO_STR
      VRRP_STR
      "Automatically set up VRRP instances on VRRP-compatible interfaces\n"
      "Version for automatically configured instances\n"
      VRRP_VERSION_STR)
{
	version = version ? version : 3;

	if (!no)
		vrrp_autoconfig_on(version);
	else
		vrrp_autoconfig_off();

	return CMD_SUCCESS;
}

/* XXX: yang conversion */
DEFPY_YANG(vrrp_default,
      vrrp_default_cmd,
      "[no] vrrp default <advertisement-interval$adv (10-40950)$advint|preempt$p|priority$prio (1-254)$prioval|shutdown$s>",
      NO_STR
      VRRP_STR
      "Configure defaults for new VRRP instances\n"
      VRRP_ADVINT_STR
      "Advertisement interval in milliseconds\n"
      "Preempt mode\n"
      VRRP_PRIORITY_STR
      "Priority value\n"
      "Force VRRP router into administrative shutdown\n")
{
	if (adv) {
		if (advint % CS2MS != 0) {
			vty_out(vty, "%% Value must be a multiple of %u\n",
				(unsigned int)CS2MS);
			return CMD_WARNING_CONFIG_FAILED;
		}
		/* all internal computations are in centiseconds */
		advint /= CS2MS;
		vd.advertisement_interval = no ? VRRP_DEFAULT_ADVINT : advint;
	}
	if (p)
		vd.preempt_mode = !no;
	if (prio)
		vd.priority = no ? VRRP_DEFAULT_PRIORITY : prioval;
	if (s)
		vd.shutdown = !no;

	return CMD_SUCCESS;
}

/* clang-format on */

/*
 * Build JSON representation of VRRP instance.
 *
 * vr
 *    VRRP router to build json object from
 *
 * Returns:
 *    JSON representation of VRRP instance. Must be freed by caller.
 */
static struct json_object *vrrp_build_json(struct vrrp_vrouter *vr)
{
	char ethstr4[ETHER_ADDR_STRLEN];
	char ethstr6[ETHER_ADDR_STRLEN];
	char ipstr[INET6_ADDRSTRLEN];
	const char *stastr4 = vrrp_state_names[vr->v4->fsm.state];
	const char *stastr6 = vrrp_state_names[vr->v6->fsm.state];
	char sipstr4[INET6_ADDRSTRLEN] = {};
	char sipstr6[INET6_ADDRSTRLEN] = {};
	struct listnode *ln;
	struct ipaddr *ip;
	struct json_object *j = json_object_new_object();
	struct json_object *v4 = json_object_new_object();
	struct json_object *v4_stats = json_object_new_object();
	struct json_object *v4_addrs = json_object_new_array();
	struct json_object *v6 = json_object_new_object();
	struct json_object *v6_stats = json_object_new_object();
	struct json_object *v6_addrs = json_object_new_array();

	prefix_mac2str(&vr->v4->vmac, ethstr4, sizeof(ethstr4));
	prefix_mac2str(&vr->v6->vmac, ethstr6, sizeof(ethstr6));

	json_object_int_add(j, "vrid", vr->vrid);
	json_object_int_add(j, "version", vr->version);
	json_object_boolean_add(j, "autoconfigured", vr->autoconf);
	json_object_boolean_add(j, "shutdown", vr->shutdown);
	json_object_boolean_add(j, "preemptMode", vr->preempt_mode);
	json_object_boolean_add(j, "acceptMode", vr->accept_mode);
	json_object_string_add(j, "interface", vr->ifp->name);
	json_object_int_add(j, "advertisementInterval",
			    vr->advertisement_interval * CS2MS);
	/* v4 */
	json_object_string_add(v4, "interface",
			       vr->v4->mvl_ifp ? vr->v4->mvl_ifp->name : "");
	json_object_string_add(v4, "vmac", ethstr4);
	ipaddr2str(&vr->v4->src, sipstr4, sizeof(sipstr4));
	json_object_string_add(v4, "primaryAddress", sipstr4);
	json_object_string_add(v4, "status", stastr4);
	json_object_int_add(v4, "effectivePriority", vr->v4->priority);
	json_object_int_add(v4, "masterAdverInterval",
			    vr->v4->master_adver_interval * CS2MS);
	json_object_int_add(v4, "skewTime", vr->v4->skew_time * CS2MS);
	json_object_int_add(v4, "masterDownInterval",
			    vr->v4->master_down_interval * CS2MS);
	/* v4 stats */
	json_object_int_add(v4_stats, "adverTx", vr->v4->stats.adver_tx_cnt);
	json_object_int_add(v4_stats, "adverRx", vr->v4->stats.adver_rx_cnt);
	json_object_int_add(v4_stats, "garpTx", vr->v4->stats.garp_tx_cnt);
	json_object_int_add(v4_stats, "transitions", vr->v4->stats.trans_cnt);
	json_object_object_add(v4, "stats", v4_stats);
	/* v4 addrs */
	if (vr->v4->addrs->count) {
		for (ALL_LIST_ELEMENTS_RO(vr->v4->addrs, ln, ip)) {
			inet_ntop(vr->v4->family, &ip->ipaddr_v4, ipstr,
				  sizeof(ipstr));
			json_object_array_add(v4_addrs,
					      json_object_new_string(ipstr));
		}
	}
	json_object_object_add(v4, "addresses", v4_addrs);
	json_object_object_add(j, "v4", v4);

	/* v6 */
	json_object_string_add(v6, "interface",
			       vr->v6->mvl_ifp ? vr->v6->mvl_ifp->name : "");
	json_object_string_add(v6, "vmac", ethstr6);
	ipaddr2str(&vr->v6->src, sipstr6, sizeof(sipstr6));
	if (strlen(sipstr6) == 0 && vr->v6->src.ip.addr == 0x00)
		strlcat(sipstr6, "::", sizeof(sipstr6));
	json_object_string_add(v6, "primaryAddress", sipstr6);
	json_object_string_add(v6, "status", stastr6);
	json_object_int_add(v6, "effectivePriority", vr->v6->priority);
	json_object_int_add(v6, "masterAdverInterval",
			    vr->v6->master_adver_interval * CS2MS);
	json_object_int_add(v6, "skewTime", vr->v6->skew_time * CS2MS);
	json_object_int_add(v6, "masterDownInterval",
			    vr->v6->master_down_interval * CS2MS);
	/* v6 stats */
	json_object_int_add(v6_stats, "adverTx", vr->v6->stats.adver_tx_cnt);
	json_object_int_add(v6_stats, "adverRx", vr->v6->stats.adver_rx_cnt);
	json_object_int_add(v6_stats, "neighborAdverTx",
			    vr->v6->stats.una_tx_cnt);
	json_object_int_add(v6_stats, "transitions", vr->v6->stats.trans_cnt);
	json_object_object_add(v6, "stats", v6_stats);
	/* v6 addrs */
	if (vr->v6->addrs->count) {
		for (ALL_LIST_ELEMENTS_RO(vr->v6->addrs, ln, ip)) {
			inet_ntop(vr->v6->family, &ip->ipaddr_v6, ipstr,
				  sizeof(ipstr));
			json_object_array_add(v6_addrs,
					      json_object_new_string(ipstr));
		}
	}
	json_object_object_add(v6, "addresses", v6_addrs);
	json_object_object_add(j, "v6", v6);

	return j;
}

/*
 * Dump VRRP instance status to VTY.
 *
 * vty
 *    vty to dump to
 *
 * vr
 *    VRRP router to dump
 */
static void vrrp_show(struct vty *vty, struct vrrp_vrouter *vr)
{
	char ethstr4[ETHER_ADDR_STRLEN];
	char ethstr6[ETHER_ADDR_STRLEN];
	char ipstr[INET6_ADDRSTRLEN];
	const char *stastr4 = vrrp_state_names[vr->v4->fsm.state];
	const char *stastr6 = vrrp_state_names[vr->v6->fsm.state];
	char sipstr4[INET6_ADDRSTRLEN] = {};
	char sipstr6[INET6_ADDRSTRLEN] = {};
	struct listnode *ln;
	struct ipaddr *ip;

	struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);

	ttable_add_row(tt, "%s|%u", "Virtual Router ID", vr->vrid);
	ttable_add_row(tt, "%s|%hhu", "Protocol Version", vr->version);
	ttable_add_row(tt, "%s|%s", "Autoconfigured",
		       vr->autoconf ? "Yes" : "No");
	ttable_add_row(tt, "%s|%s", "Shutdown", vr->shutdown ? "Yes" : "No");
	ttable_add_row(tt, "%s|%s", "Interface", vr->ifp->name);
	prefix_mac2str(&vr->v4->vmac, ethstr4, sizeof(ethstr4));
	prefix_mac2str(&vr->v6->vmac, ethstr6, sizeof(ethstr6));
	ttable_add_row(tt, "%s|%s", "VRRP interface (v4)",
		       vr->v4->mvl_ifp ? vr->v4->mvl_ifp->name : "None");
	ttable_add_row(tt, "%s|%s", "VRRP interface (v6)",
		       vr->v6->mvl_ifp ? vr->v6->mvl_ifp->name : "None");
	ipaddr2str(&vr->v4->src, sipstr4, sizeof(sipstr4));
	ipaddr2str(&vr->v6->src, sipstr6, sizeof(sipstr6));
	if (strlen(sipstr6) == 0 && vr->v6->src.ip.addr == 0x00)
		strlcat(sipstr6, "::", sizeof(sipstr6));
	ttable_add_row(tt, "%s|%s", "Primary IP (v4)", sipstr4);
	ttable_add_row(tt, "%s|%s", "Primary IP (v6)", sipstr6);
	ttable_add_row(tt, "%s|%s", "Virtual MAC (v4)", ethstr4);
	ttable_add_row(tt, "%s|%s", "Virtual MAC (v6)", ethstr6);
	ttable_add_row(tt, "%s|%s", "Status (v4)", stastr4);
	ttable_add_row(tt, "%s|%s", "Status (v6)", stastr6);
	ttable_add_row(tt, "%s|%hhu", "Priority", vr->priority);
	ttable_add_row(tt, "%s|%hhu", "Effective Priority (v4)",
		       vr->v4->priority);
	ttable_add_row(tt, "%s|%hhu", "Effective Priority (v6)",
		       vr->v6->priority);
	ttable_add_row(tt, "%s|%s", "Preempt Mode",
		       vr->preempt_mode ? "Yes" : "No");
	ttable_add_row(tt, "%s|%s", "Accept Mode",
		       vr->accept_mode ? "Yes" : "No");
	ttable_add_row(tt, "%s|%d ms", "Advertisement Interval",
		       vr->advertisement_interval * CS2MS);
	ttable_add_row(tt, "%s|%d ms",
		       "Master Advertisement Interval (v4)",
		       vr->v4->master_adver_interval * CS2MS);
	ttable_add_row(tt, "%s|%d ms",
		       "Master Advertisement Interval (v6)",
		       vr->v6->master_adver_interval * CS2MS);
	ttable_add_row(tt, "%s|%u", "Advertisements Tx (v4)",
		       vr->v4->stats.adver_tx_cnt);
	ttable_add_row(tt, "%s|%u", "Advertisements Tx (v6)",
		       vr->v6->stats.adver_tx_cnt);
	ttable_add_row(tt, "%s|%u", "Advertisements Rx (v4)",
		       vr->v4->stats.adver_rx_cnt);
	ttable_add_row(tt, "%s|%u", "Advertisements Rx (v6)",
		       vr->v6->stats.adver_rx_cnt);
	ttable_add_row(tt, "%s|%u", "Gratuitous ARP Tx (v4)",
		       vr->v4->stats.garp_tx_cnt);
	ttable_add_row(tt, "%s|%u", "Neigh. Adverts Tx (v6)",
		       vr->v6->stats.una_tx_cnt);
	ttable_add_row(tt, "%s|%u", "State transitions (v4)",
		       vr->v4->stats.trans_cnt);
	ttable_add_row(tt, "%s|%u", "State transitions (v6)",
		       vr->v6->stats.trans_cnt);
	ttable_add_row(tt, "%s|%d ms", "Skew Time (v4)",
		       vr->v4->skew_time * CS2MS);
	ttable_add_row(tt, "%s|%d ms", "Skew Time (v6)",
		       vr->v6->skew_time * CS2MS);
	ttable_add_row(tt, "%s|%d ms", "Master Down Interval (v4)",
		       vr->v4->master_down_interval * CS2MS);
	ttable_add_row(tt, "%s|%d ms", "Master Down Interval (v6)",
		       vr->v6->master_down_interval * CS2MS);
	ttable_add_row(tt, "%s|%u", "IPv4 Addresses", vr->v4->addrs->count);

	char fill[35];

	memset(fill, '.', sizeof(fill));
	fill[sizeof(fill) - 1] = 0x00;
	if (vr->v4->addrs->count) {
		for (ALL_LIST_ELEMENTS_RO(vr->v4->addrs, ln, ip)) {
			inet_ntop(vr->v4->family, &ip->ipaddr_v4, ipstr,
				  sizeof(ipstr));
			ttable_add_row(tt, "%s|%s", fill, ipstr);
		}
	}

	ttable_add_row(tt, "%s|%u", "IPv6 Addresses", vr->v6->addrs->count);

	if (vr->v6->addrs->count) {
		for (ALL_LIST_ELEMENTS_RO(vr->v6->addrs, ln, ip)) {
			inet_ntop(vr->v6->family, &ip->ipaddr_v6, ipstr,
				  sizeof(ipstr));
			ttable_add_row(tt, "%s|%s", fill, ipstr);
		}
	}

	char *table = ttable_dump(tt, "\n");

	vty_out(vty, "\n%s\n", table);
	XFREE(MTYPE_TMP, table);
	ttable_del(tt);
}

/*
 * Sort comparator, used when sorting VRRP instances for display purposes.
 *
 * Sorts by interface name first, then by VRID ascending.
 */
static int vrrp_instance_display_sort_cmp(const void **d1, const void **d2)
{
	const struct vrrp_vrouter *vr1 = *d1;
	const struct vrrp_vrouter *vr2 = *d2;
	int result;

	result = strcmp(vr1->ifp->name, vr2->ifp->name);
	result += !result * (vr1->vrid - vr2->vrid);

	return result;
}

/* clang-format off */

DEFPY_YANG(vrrp_vrid_show,
      vrrp_vrid_show_cmd,
      "show vrrp [interface INTERFACE$ifn] [(1-255)$vrid] [json$json]",
      SHOW_STR
      VRRP_STR
      INTERFACE_STR
      "Only show VRRP instances on this interface\n"
      VRRP_VRID_STR
      JSON_STR)
{
	struct vrrp_vrouter *vr;
	struct listnode *ln;
	struct list *ll = hash_to_list(vrrp_vrouters_hash);
	struct json_object *j = json_object_new_array();

	list_sort(ll, vrrp_instance_display_sort_cmp);

	for (ALL_LIST_ELEMENTS_RO(ll, ln, vr)) {
		if (ifn && !strmatch(ifn, vr->ifp->name))
			continue;
		if (vrid && ((uint8_t) vrid) != vr->vrid)
			continue;

		if (!json)
			vrrp_show(vty, vr);
		else
			json_object_array_add(j, vrrp_build_json(vr));
	}

	if (json)
		vty_out(vty, "%s\n",
			json_object_to_json_string_ext(
				j, JSON_C_TO_STRING_PRETTY));

	json_object_free(j);

	list_delete(&ll);

	return CMD_SUCCESS;
}

DEFPY_YANG(vrrp_vrid_show_summary,
      vrrp_vrid_show_summary_cmd,
      "show vrrp [interface INTERFACE$ifn] [(1-255)$vrid] summary",
      SHOW_STR
      VRRP_STR
      INTERFACE_STR
      "Only show VRRP instances on this interface\n"
      VRRP_VRID_STR
      "Summarize all VRRP instances\n")
{
	struct vrrp_vrouter *vr;
	struct listnode *ln;
	struct list *ll = hash_to_list(vrrp_vrouters_hash);

	list_sort(ll, vrrp_instance_display_sort_cmp);

	struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);

	ttable_add_row(
		tt, "Interface|VRID|Priority|IPv4|IPv6|State (v4)|State (v6)");
	ttable_rowseps(tt, 0, BOTTOM, true, '-');

	for (ALL_LIST_ELEMENTS_RO(ll, ln, vr)) {
		if (ifn && !strmatch(ifn, vr->ifp->name))
			continue;
		if (vrid && ((uint8_t)vrid) != vr->vrid)
			continue;

		ttable_add_row(
			tt, "%s|%u|%hhu|%d|%d|%s|%s",
			vr->ifp->name, vr->vrid, vr->priority,
			vr->v4->addrs->count, vr->v6->addrs->count,
			vr->v4->fsm.state == VRRP_STATE_MASTER ? "Master"
							       : "Backup",
			vr->v6->fsm.state == VRRP_STATE_MASTER ? "Master"
							       : "Backup");
	}

	char *table = ttable_dump(tt, "\n");

	vty_out(vty, "\n%s\n", table);
	XFREE(MTYPE_TMP, table);
	ttable_del(tt);

	list_delete(&ll);

	return CMD_SUCCESS;
}


DEFPY_YANG(debug_vrrp,
      debug_vrrp_cmd,
      "[no] debug vrrp [{protocol$proto|autoconfigure$ac|packets$pkt|sockets$sock|ndisc$ndisc|arp$arp|zebra$zebra}]",
      NO_STR
      DEBUG_STR
      VRRP_STR
      "Debug protocol state\n"
      "Debug autoconfiguration\n"
      "Debug sent and received packets\n"
      "Debug socket creation and configuration\n"
      "Debug Neighbor Discovery\n"
      "Debug ARP\n"
      "Debug Zebra events\n")
{
	/* If no specific are given on/off them all */
	if (strmatch(argv[argc - 1]->text, "vrrp"))
		vrrp_debug_set(NULL, 0, vty->node, !no, true, true, true, true,
			       true, true, true);
	else
		vrrp_debug_set(NULL, 0, vty->node, !no, !!proto, !!ac, !!pkt,
			       !!sock, !!ndisc, !!arp, !!zebra);

	return CMD_SUCCESS;
}

DEFUN_NOSH (show_debugging_vrrp,
	    show_debugging_vrrp_cmd,
	    "show debugging [vrrp]",
	    SHOW_STR
	    DEBUG_STR
	    "VRRP information\n")
{
	vty_out(vty, "VRRP debugging status:\n");

	vrrp_debug_status_write(vty);

	return CMD_SUCCESS;
}

/* clang-format on */

/*
 * Write per interface VRRP config.
 */
static int vrrp_config_write_interface(struct vty *vty)
{
	struct vrf *vrf;
	int write = 0;

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		struct interface *ifp;

		FOR_ALL_INTERFACES (vrf, ifp) {
			struct lyd_node *dnode;

			dnode = yang_dnode_get(
				running_config->dnode,
				"/frr-interface:lib/interface[name='%s'][vrf='%s']",
				ifp->name, vrf->name);
			if (dnode == NULL)
				continue;

			write = 1;
			nb_cli_show_dnode_cmds(vty, dnode, false);
		}
	}

	return write;
}

static struct cmd_node interface_node = {
	.name = "interface",
	.node = INTERFACE_NODE,
	.parent_node = CONFIG_NODE,
	.prompt = "%s(config-if)# ",
	.config_write = vrrp_config_write_interface,
};

static struct cmd_node debug_node = {
	.name = "debug",
	.node = DEBUG_NODE,
	.prompt = "",
	.config_write = vrrp_config_write_debug,
};

static struct cmd_node vrrp_node = {
	.name = "vrrp",
	.node = VRRP_NODE,
	.prompt = "",
	.config_write = vrrp_config_write_global,
};

void vrrp_vty_init(void)
{
	install_node(&debug_node);
	install_node(&interface_node);
	install_node(&vrrp_node);
	if_cmd_init();

	install_element(VIEW_NODE, &vrrp_vrid_show_cmd);
	install_element(VIEW_NODE, &vrrp_vrid_show_summary_cmd);
	install_element(VIEW_NODE, &show_debugging_vrrp_cmd);
	install_element(VIEW_NODE, &debug_vrrp_cmd);
	install_element(CONFIG_NODE, &debug_vrrp_cmd);
	install_element(CONFIG_NODE, &vrrp_autoconfigure_cmd);
	install_element(CONFIG_NODE, &vrrp_default_cmd);
	install_element(INTERFACE_NODE, &vrrp_vrid_cmd);
	install_element(INTERFACE_NODE, &vrrp_shutdown_cmd);
	install_element(INTERFACE_NODE, &vrrp_priority_cmd);
	install_element(INTERFACE_NODE, &no_vrrp_priority_cmd);
	install_element(INTERFACE_NODE, &vrrp_advertisement_interval_cmd);
	install_element(INTERFACE_NODE, &no_vrrp_advertisement_interval_cmd);
	install_element(INTERFACE_NODE, &vrrp_ip_cmd);
	install_element(INTERFACE_NODE, &vrrp_ip6_cmd);
	install_element(INTERFACE_NODE, &vrrp_preempt_cmd);
}