summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_pw.c
blob: deed3b6ad3c3c78080e45fb09bf8c7abfdb2fdd1 (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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// SPDX-License-Identifier: GPL-2.0-or-later
/* Zebra PW code
 * Copyright (C) 2016 Volta Networks, Inc.
 */

#include <zebra.h>

#include "log.h"
#include "memory.h"
#include "frrevent.h"
#include "command.h"
#include "vrf.h"
#include "lib/json.h"
#include "printfrr.h"

#include "zebra/debug.h"
#include "zebra/rib.h"
#include "zebra/zebra_router.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_rnh.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_pw.h"

DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire");

DEFINE_QOBJ_TYPE(zebra_pw);

DEFINE_HOOK(pw_install, (struct zebra_pw * pw), (pw));
DEFINE_HOOK(pw_uninstall, (struct zebra_pw * pw), (pw));

#define MPLS_NO_LABEL MPLS_INVALID_LABEL

static int zebra_pw_enabled(struct zebra_pw *);
static void zebra_pw_install(struct zebra_pw *);
static void zebra_pw_uninstall(struct zebra_pw *);
static void zebra_pw_install_retry(struct event *thread);
static int zebra_pw_check_reachability(const struct zebra_pw *);
static void zebra_pw_update_status(struct zebra_pw *, int);

static inline int zebra_pw_compare(const struct zebra_pw *a,
				   const struct zebra_pw *b)
{
	return (strcmp(a->ifname, b->ifname));
}

RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)

struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
			      uint8_t protocol, struct zserv *client)
{
	struct zebra_pw *pw;

	if (IS_ZEBRA_DEBUG_PW)
		zlog_debug("%u: adding pseudowire %s protocol %s",
			   zvrf_id(zvrf), ifname, zebra_route_string(protocol));

	pw = XCALLOC(MTYPE_PW, sizeof(*pw));
	strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
	pw->protocol = protocol;
	pw->vrf_id = zvrf_id(zvrf);
	pw->client = client;
	pw->status = PW_NOT_FORWARDING;
	pw->local_label = MPLS_NO_LABEL;
	pw->remote_label = MPLS_NO_LABEL;
	pw->flags = F_PSEUDOWIRE_CWORD;

	RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
	if (pw->protocol == ZEBRA_ROUTE_STATIC) {
		RB_INSERT(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
		QOBJ_REG(pw, zebra_pw);
	}

	return pw;
}

void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
{
	if (IS_ZEBRA_DEBUG_PW)
		zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
			   pw->ifname, zebra_route_string(pw->protocol));

	/* remove nexthop tracking */
	zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);

	/* uninstall */
	if (pw->status == PW_FORWARDING) {
		hook_call(pw_uninstall, pw);
		dplane_pw_uninstall(pw);
	}

	EVENT_OFF(pw->install_retry_timer);

	/* unlink and release memory */
	RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
	if (pw->protocol == ZEBRA_ROUTE_STATIC)
		RB_REMOVE(zebra_static_pw_head, &zvrf->static_pseudowires, pw);

	XFREE(MTYPE_PW, pw);
}

void zebra_pw_change(struct zebra_pw *pw, ifindex_t ifindex, int type, int af,
		     union g_addr *nexthop, uint32_t local_label,
		     uint32_t remote_label, uint8_t flags,
		     union pw_protocol_fields *data)
{
	pw->ifindex = ifindex;
	pw->type = type;
	pw->af = af;
	pw->nexthop = *nexthop;
	pw->local_label = local_label;
	pw->remote_label = remote_label;
	pw->flags = flags;
	pw->data = *data;

	if (zebra_pw_enabled(pw)) {
		bool nht_exists;
		zebra_register_rnh_pseudowire(pw->vrf_id, pw, &nht_exists);
		if (nht_exists)
			zebra_pw_update(pw);
	} else {
		if (pw->protocol == ZEBRA_ROUTE_STATIC)
			zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
		zebra_pw_uninstall(pw);
	}
}

struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
{
	struct zebra_pw pw;
	strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
	return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
}

static int zebra_pw_enabled(struct zebra_pw *pw)
{
	if (pw->protocol == ZEBRA_ROUTE_STATIC) {
		if (pw->local_label == MPLS_NO_LABEL
		    || pw->remote_label == MPLS_NO_LABEL || pw->af == AF_UNSPEC)
			return 0;
		return 1;
	} else
		return pw->enabled;
}

void zebra_pw_update(struct zebra_pw *pw)
{
	if (zebra_pw_check_reachability(pw) < 0) {
		zebra_pw_uninstall(pw);
		zebra_pw_install_failure(pw, PW_NOT_FORWARDING);
		/* wait for NHT and try again later */
	} else {
		/*
		 * Install or reinstall the pseudowire (e.g. to update
		 * parameters like the nexthop or the use of the control word).
		 */
		zebra_pw_install(pw);
	}
}

static void zebra_pw_install(struct zebra_pw *pw)
{
	if (IS_ZEBRA_DEBUG_PW)
		zlog_debug("%u: installing pseudowire %s protocol %s",
			   pw->vrf_id, pw->ifname,
			   zebra_route_string(pw->protocol));

	hook_call(pw_install, pw);
	if (dplane_pw_install(pw) == ZEBRA_DPLANE_REQUEST_FAILURE) {
		zebra_pw_install_failure(pw, PW_NOT_FORWARDING);
		return;
	}

	if (pw->status != PW_FORWARDING)
		zebra_pw_update_status(pw, PW_FORWARDING);
}

static void zebra_pw_uninstall(struct zebra_pw *pw)
{
	if (pw->status != PW_FORWARDING)
		return;

	if (IS_ZEBRA_DEBUG_PW)
		zlog_debug("%u: uninstalling pseudowire %s protocol %s",
			   pw->vrf_id, pw->ifname,
			   zebra_route_string(pw->protocol));

	/* ignore any possible error */
	hook_call(pw_uninstall, pw);
	dplane_pw_uninstall(pw);

	if (zebra_pw_enabled(pw))
		zebra_pw_update_status(pw, PW_NOT_FORWARDING);
}

/*
 * Installation of the pseudowire in the kernel or hardware has failed. This
 * function will notify the pseudowire client about the failure and schedule
 * to retry the installation later. This function can be called by an external
 * agent that performs the pseudowire installation in an asynchronous way.
 */
void zebra_pw_install_failure(struct zebra_pw *pw, int pwstatus)
{
	if (IS_ZEBRA_DEBUG_PW)
		zlog_debug(
			"%u: failed installing pseudowire %s, scheduling retry in %u seconds",
			pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);

	/* schedule to retry later */
	EVENT_OFF(pw->install_retry_timer);
	event_add_timer(zrouter.master, zebra_pw_install_retry, pw,
			PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);

	zebra_pw_update_status(pw, pwstatus);
}

static void zebra_pw_install_retry(struct event *thread)
{
	struct zebra_pw *pw = EVENT_ARG(thread);

	zebra_pw_install(pw);
}

static void zebra_pw_update_status(struct zebra_pw *pw, int status)
{
	pw->status = status;
	if (pw->client)
		zsend_pw_update(pw->client, pw);
}

static int zebra_pw_check_reachability_strict(const struct zebra_pw *pw,
					      struct route_entry *re)
{
	const struct nexthop *nexthop;
	const struct nexthop_group *nhg;
	bool found_p = false;
	bool fail_p = false;

	/* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */

	/* All active nexthops must be labelled; look at
	 * primary and backup fib lists, in case there's been
	 * a backup nexthop activation.
	 */
	nhg = rib_get_fib_nhg(re);
	if (nhg && nhg->nexthop) {
		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
				continue;

			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
				if (nexthop->nh_label != NULL)
					found_p = true;
				else {
					fail_p = true;
					break;
				}
			}
		}
	}

	if (fail_p)
		goto done;

	nhg = rib_get_fib_backup_nhg(re);
	if (nhg && nhg->nexthop) {
		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
				continue;

			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
				if (nexthop->nh_label != NULL)
					found_p = true;
				else {
					fail_p = true;
					break;
				}
			}
		}
	}

done:

	if (fail_p || !found_p) {
		if (IS_ZEBRA_DEBUG_PW)
			zlog_debug("%s: unlabeled route for %s",
				   __func__, pw->ifname);
		return -1;
	}

	return 0;
}

static int zebra_pw_check_reachability(const struct zebra_pw *pw)
{
	struct route_entry *re;
	const struct nexthop *nexthop;
	const struct nexthop_group *nhg;
	bool found_p = false;

	/* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */

	/* Find route to the remote end of the pseudowire */
	re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
		       &pw->nexthop, NULL);
	if (!re) {
		if (IS_ZEBRA_DEBUG_PW)
			zlog_debug("%s: no route found for %s", __func__,
				   pw->ifname);
		return -1;
	}

	/* Stricter checking for some OSes (OBSD, e.g.) */
	if (mpls_pw_reach_strict)
		return zebra_pw_check_reachability_strict(pw, re);

	/* There must be at least one installed labelled nexthop;
	 * look at primary and backup fib lists, in case there's been
	 * a backup nexthop activation.
	 */
	nhg = rib_get_fib_nhg(re);
	if (nhg && nhg->nexthop) {
		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
				continue;

			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) &&
			    nexthop->nh_label != NULL) {
				found_p = true;
				break;
			}
		}
	}

	if (found_p)
		return 0;

	nhg = rib_get_fib_backup_nhg(re);
	if (nhg && nhg->nexthop) {
		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
				continue;

			if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) &&
			    nexthop->nh_label != NULL) {
				found_p = true;
				break;
			}
		}
	}

	if (!found_p) {
		if (IS_ZEBRA_DEBUG_PW)
			zlog_debug("%s: unlabeled route for %s",
				   __func__, pw->ifname);
		return -1;
	}

	return 0;
}

static int zebra_pw_client_close(struct zserv *client)
{
	struct vrf *vrf;
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw, *tmp;

	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
		zvrf = vrf->info;
		RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
			if (pw->client != client)
				continue;
			zebra_pw_del(zvrf, pw);
		}
	}

	return 0;
}

static void zebra_pw_init(void)
{
	hook_register(zserv_client_close, zebra_pw_client_close);
}

void zebra_pw_init_vrf(struct zebra_vrf *zvrf)
{
	RB_INIT(zebra_pw_head, &zvrf->pseudowires);
	RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
}

void zebra_pw_exit_vrf(struct zebra_vrf *zvrf)
{
	struct zebra_pw *pw;

	while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) {
		pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires);

		zebra_pw_del(zvrf, pw);
	}
}

void zebra_pw_terminate(void)
{
	hook_unregister(zserv_client_close, zebra_pw_client_close);
}

DEFUN_NOSH (pseudowire_if,
	    pseudowire_if_cmd,
	    "pseudowire IFNAME",
	    "Static pseudowire configuration\n"
	    "Pseudowire name\n")
{
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;
	const char *ifname;
	int idx = 0;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	argv_find(argv, argc, "IFNAME", &idx);
	ifname = argv[idx]->arg;

	pw = zebra_pw_find(zvrf, ifname);
	if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
		vty_out(vty, "%% Pseudowire is not static\n");
		return CMD_WARNING;
	}

	if (!pw)
		pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
	VTY_PUSH_CONTEXT(PW_NODE, pw);

	return CMD_SUCCESS;
}

DEFUN (no_pseudowire_if,
       no_pseudowire_if_cmd,
       "no pseudowire IFNAME",
       NO_STR
       "Static pseudowire configuration\n"
       "Pseudowire name\n")
{
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;
	const char *ifname;
	int idx = 0;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	argv_find(argv, argc, "IFNAME", &idx);
	ifname = argv[idx]->arg;

	pw = zebra_pw_find(zvrf, ifname);
	if (pw) {
		if (pw->protocol != ZEBRA_ROUTE_STATIC) {
			vty_out(vty, "%% Pseudowire is not static\n");
			return CMD_WARNING;
		}
		zebra_pw_del(zvrf, pw);
	}

	return CMD_SUCCESS;
}

DEFUN (pseudowire_labels,
       pseudowire_labels_cmd,
       "[no] mpls label local (16-1048575) remote (16-1048575)",
       NO_STR
       "MPLS L2VPN PW command\n"
       "MPLS L2VPN static labels\n"
       "Local pseudowire label\n"
       "Local pseudowire label\n"
       "Remote pseudowire label\n"
       "Remote pseudowire label\n")
{
	VTY_DECLVAR_CONTEXT(zebra_pw, pw);
	int idx = 0;
	mpls_label_t local_label, remote_label;

	if (argv_find(argv, argc, "no", &idx)) {
		local_label = MPLS_NO_LABEL;
		remote_label = MPLS_NO_LABEL;
	} else {
		argv_find(argv, argc, "local", &idx);
		local_label = atoi(argv[idx + 1]->arg);
		argv_find(argv, argc, "remote", &idx);
		remote_label = atoi(argv[idx + 1]->arg);
	}

	zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
			local_label, remote_label, pw->flags, &pw->data);

	return CMD_SUCCESS;
}

DEFUN (pseudowire_neighbor,
       pseudowire_neighbor_cmd,
       "[no] neighbor <A.B.C.D|X:X::X:X>",
       NO_STR
       "Specify the IPv4 or IPv6 address of the remote endpoint\n"
       "IPv4 address\n"
       "IPv6 address\n")
{
	VTY_DECLVAR_CONTEXT(zebra_pw, pw);
	int idx = 0;
	const char *address;
	int af;
	union g_addr nexthop;

	af = AF_UNSPEC;
	memset(&nexthop, 0, sizeof(nexthop));

	if (!argv_find(argv, argc, "no", &idx)) {
		argv_find(argv, argc, "neighbor", &idx);
		address = argv[idx + 1]->arg;

		if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
			af = AF_INET;
		else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
			af = AF_INET6;
		else {
			vty_out(vty, "%% Malformed address\n");
			return CMD_WARNING;
		}
	}

	zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
			pw->local_label, pw->remote_label, pw->flags,
			&pw->data);

	return CMD_SUCCESS;
}

DEFUN (pseudowire_control_word,
       pseudowire_control_word_cmd,
       "[no] control-word <exclude|include>",
       NO_STR
       "Control-word options\n"
       "Exclude control-word in pseudowire packets\n"
       "Include control-word in pseudowire packets\n")
{
	VTY_DECLVAR_CONTEXT(zebra_pw, pw);
	int idx = 0;
	uint8_t flags = 0;

	if (argv_find(argv, argc, "no", &idx))
		flags = F_PSEUDOWIRE_CWORD;
	else {
		argv_find(argv, argc, "control-word", &idx);
		if (argv[idx + 1]->text[0] == 'i')
			flags = F_PSEUDOWIRE_CWORD;
	}

	zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
			pw->local_label, pw->remote_label, flags, &pw->data);

	return CMD_SUCCESS;
}

DEFUN (show_pseudowires,
       show_pseudowires_cmd,
       "show mpls pseudowires",
       SHOW_STR
       MPLS_STR
       "Pseudowires\n")
{
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
		"Labels", "Protocol", "Status");

	RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
		char buf_nbr[INET6_ADDRSTRLEN];
		char buf_labels[64];

		inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));

		if (pw->local_label != MPLS_NO_LABEL
		    && pw->remote_label != MPLS_NO_LABEL)
			snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
				 pw->local_label, pw->remote_label);
		else
			snprintf(buf_labels, sizeof(buf_labels), "-");

		vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
			(pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
			zebra_route_string(pw->protocol),
			(zebra_pw_enabled(pw) && pw->status == PW_FORWARDING)
				? "UP"
				: "DOWN");
	}

	return CMD_SUCCESS;
}

static void vty_show_mpls_pseudowire_detail(struct vty *vty)
{
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;
	struct route_entry *re;
	struct nexthop *nexthop;
	struct nexthop_group *nhg;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
		char buf_nbr[INET6_ADDRSTRLEN];
		char buf_nh[100];

		vty_out(vty, "Interface: %s\n", pw->ifname);
		inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
		vty_out(vty, "  Neighbor: %s\n",
			(pw->af != AF_UNSPEC) ? buf_nbr : "-");
		if (pw->local_label != MPLS_NO_LABEL)
			vty_out(vty, "  Local Label: %u\n", pw->local_label);
		else
			vty_out(vty, "  Local Label: %s\n", "-");
		if (pw->remote_label != MPLS_NO_LABEL)
			vty_out(vty, "  Remote Label: %u\n", pw->remote_label);
		else
			vty_out(vty, "  Remote Label: %s\n", "-");
		vty_out(vty, "  Protocol: %s\n",
			zebra_route_string(pw->protocol));
		if (pw->protocol == ZEBRA_ROUTE_LDP)
			vty_out(vty, "  VC-ID: %u\n", pw->data.ldp.pwid);
		vty_out(vty, "  Status: %s \n",
			(zebra_pw_enabled(pw) && pw->status == PW_FORWARDING)
			? "Up"
			: "Down");
		re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
			       &pw->nexthop, NULL);
		if (re == NULL)
			continue;

		nhg = rib_get_fib_nhg(re);
		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			snprintfrr(buf_nh, sizeof(buf_nh), "%pNHv",
				   nexthop);
			vty_out(vty, "  Next Hop: %s\n", buf_nh);
			if (nexthop->nh_label)
				vty_out(vty, "  Next Hop label: %u\n",
					nexthop->nh_label->label[0]);
			else
				vty_out(vty, "  Next Hop label: %s\n",
					"-");
		}

		/* Include any installed backups */
		nhg = rib_get_fib_backup_nhg(re);
		if (nhg == NULL)
			continue;

		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
			snprintfrr(buf_nh, sizeof(buf_nh), "%pNHv",
				   nexthop);
			vty_out(vty, "  Next Hop: %s\n", buf_nh);
			if (nexthop->nh_label)
				vty_out(vty, "  Next Hop label: %u\n",
					nexthop->nh_label->label[0]);
			else
				vty_out(vty, "  Next Hop label: %s\n",
					"-");
		}
	}
}

static void vty_show_mpls_pseudowire(struct zebra_pw *pw, json_object *json_pws)
{
	struct route_entry *re;
	struct nexthop *nexthop;
	struct nexthop_group *nhg;
	char buf_nbr[INET6_ADDRSTRLEN];
	char buf_nh[100];
	json_object *json_pw = NULL;
	json_object *json_nexthop = NULL;
	json_object *json_nexthops = NULL;

	json_nexthops = json_object_new_array();
	json_pw = json_object_new_object();

	json_object_string_add(json_pw, "interface", pw->ifname);
	if (pw->af == AF_UNSPEC)
		json_object_string_add(json_pw, "neighbor", "-");
	else {
		inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
		json_object_string_add(json_pw, "neighbor", buf_nbr);
	}
	if (pw->local_label != MPLS_NO_LABEL)
		json_object_int_add(json_pw, "localLabel", pw->local_label);
	else
		json_object_string_add(json_pw, "localLabel", "-");
	if (pw->remote_label != MPLS_NO_LABEL)
		json_object_int_add(json_pw, "remoteLabel", pw->remote_label);
	else
		json_object_string_add(json_pw, "remoteLabel", "-");
	json_object_string_add(json_pw, "protocol",
			       zebra_route_string(pw->protocol));
	if (pw->protocol == ZEBRA_ROUTE_LDP)
		json_object_int_add(json_pw, "vcId", pw->data.ldp.pwid);
	json_object_string_add(
		json_pw, "Status",
		(zebra_pw_enabled(pw) && pw->status == PW_FORWARDING) ? "Up"
								      : "Down");
	re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
		       &pw->nexthop, NULL);
	if (re == NULL)
		goto done;

	nhg = rib_get_fib_nhg(re);
	for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
		json_nexthop = json_object_new_object();
		snprintfrr(buf_nh, sizeof(buf_nh), "%pNHv", nexthop);
		json_object_string_add(json_nexthop, "nexthop", buf_nh);
		if (nexthop->nh_label)
			json_object_int_add(
				json_nexthop, "nhLabel",
				nexthop->nh_label->label[0]);
		else
			json_object_string_add(json_nexthop, "nhLabel",
					       "-");

		json_object_array_add(json_nexthops, json_nexthop);
	}

	/* Include installed backup nexthops also */
	nhg = rib_get_fib_backup_nhg(re);
	if (nhg == NULL)
		goto done;

	for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
		json_nexthop = json_object_new_object();
		snprintfrr(buf_nh, sizeof(buf_nh), "%pNHv", nexthop);
		json_object_string_add(json_nexthop, "nexthop", buf_nh);
		if (nexthop->nh_label)
			json_object_int_add(
				json_nexthop, "nhLabel",
				nexthop->nh_label->label[0]);
		else
			json_object_string_add(json_nexthop, "nhLabel",
					       "-");

		json_object_array_add(json_nexthops, json_nexthop);
	}

done:

	json_object_object_add(json_pw, "nexthops", json_nexthops);
	json_object_array_add(json_pws, json_pw);
}

static void vty_show_mpls_pseudowire_detail_json(struct vty *vty)
{
	json_object *json = NULL;
	json_object *json_pws = NULL;
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	json = json_object_new_object();
	json_pws = json_object_new_array();
	RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
		vty_show_mpls_pseudowire(pw, json_pws);
	}
	json_object_object_add(json, "pw", json_pws);
	vty_json(vty, json);
}

DEFUN(show_pseudowires_detail, show_pseudowires_detail_cmd,
      "show mpls pseudowires detail [json]$json",
      SHOW_STR MPLS_STR
      "Pseudowires\n"
      "Detailed output\n" JSON_STR)
{
	bool uj = use_json(argc, argv);

	if (uj)
		vty_show_mpls_pseudowire_detail_json(vty);
	else
		vty_show_mpls_pseudowire_detail(vty);

	return CMD_SUCCESS;
}

/* Pseudowire configuration write function. */
static int zebra_pw_config(struct vty *vty)
{
	int write = 0;
	struct zebra_vrf *zvrf;
	struct zebra_pw *pw;

	zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);

	RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
		vty_out(vty, "pseudowire %s\n", pw->ifname);
		if (pw->local_label != MPLS_NO_LABEL
		    && pw->remote_label != MPLS_NO_LABEL)
			vty_out(vty, " mpls label local %u remote %u\n",
				pw->local_label, pw->remote_label);
		else
			vty_out(vty,
				" ! Incomplete config, specify the static MPLS labels\n");

		if (pw->af != AF_UNSPEC) {
			char buf[INET6_ADDRSTRLEN];
			inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
			vty_out(vty, " neighbor %s\n", buf);
		} else
			vty_out(vty,
				" ! Incomplete config, specify a neighbor address\n");

		if (!(pw->flags & F_PSEUDOWIRE_CWORD))
			vty_out(vty, " control-word exclude\n");

		vty_out(vty, "exit\n");
		vty_out(vty, "!\n");
		write = 1;
	}

	return write;
}

static int zebra_pw_config(struct vty *vty);
static struct cmd_node pw_node = {
	.name = "pw",
	.node = PW_NODE,
	.parent_node = CONFIG_NODE,
	.prompt = "%s(config-pw)# ",
	.config_write = zebra_pw_config,
};

void zebra_pw_vty_init(void)
{
	install_node(&pw_node);
	install_default(PW_NODE);

	install_element(CONFIG_NODE, &pseudowire_if_cmd);
	install_element(CONFIG_NODE, &no_pseudowire_if_cmd);
	install_element(PW_NODE, &pseudowire_labels_cmd);
	install_element(PW_NODE, &pseudowire_neighbor_cmd);
	install_element(PW_NODE, &pseudowire_control_word_cmd);

	install_element(VIEW_NODE, &show_pseudowires_cmd);
	install_element(VIEW_NODE, &show_pseudowires_detail_cmd);

	zebra_pw_init();
}