2 * Copyright (C) 2016 by Open Source Routing.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
53 #define LDPBUFSIZ 65535
55 static int show_interface_msg(struct vty
*, struct imsg
*,
56 struct show_params
*);
57 static int show_interface_msg_json(struct imsg
*,
58 struct show_params
*, json_object
*);
59 static int show_discovery_msg(struct vty
*, struct imsg
*,
60 struct show_params
*);
61 static void show_discovery_detail_adj(struct vty
*, char *,
63 static int show_discovery_detail_msg(struct vty
*, struct imsg
*,
64 struct show_params
*);
65 static int show_discovery_msg_json(struct imsg
*,
66 struct show_params
*, json_object
*);
67 static void show_discovery_detail_adj_json(json_object
*,
69 static int show_discovery_detail_msg_json(struct imsg
*,
70 struct show_params
*, json_object
*);
71 static int show_nbr_msg(struct vty
*, struct imsg
*,
72 struct show_params
*);
73 static int show_nbr_msg_json(struct imsg
*, struct show_params
*,
75 static void show_nbr_detail_adj(struct vty
*, char *,
77 static int show_nbr_detail_msg(struct vty
*, struct imsg
*,
78 struct show_params
*);
79 static void show_nbr_detail_adj_json(struct ctl_adj
*,
81 static int show_nbr_detail_msg_json(struct imsg
*,
82 struct show_params
*, json_object
*);
83 static int show_lib_msg(struct vty
*, struct imsg
*,
84 struct show_params
*);
85 static int show_lib_detail_msg(struct vty
*, struct imsg
*,
86 struct show_params
*);
87 static int show_lib_msg_json(struct imsg
*, struct show_params
*,
89 static int show_lib_detail_msg_json(struct imsg
*,
90 struct show_params
*, json_object
*);
91 static int show_l2vpn_binding_msg(struct vty
*, struct imsg
*,
92 struct show_params
*);
93 static int show_l2vpn_binding_msg_json(struct imsg
*,
94 struct show_params
*, json_object
*);
95 static int show_l2vpn_pw_msg(struct vty
*, struct imsg
*,
96 struct show_params
*);
97 static int show_l2vpn_pw_msg_json(struct imsg
*,
98 struct show_params
*, json_object
*);
99 static int ldp_vty_connect(struct imsgbuf
*);
100 static int ldp_vty_dispatch_msg(struct vty
*, struct imsg
*,
101 enum show_command
, struct show_params
*,
103 static int ldp_vty_dispatch(struct vty
*, struct imsgbuf
*,
104 enum show_command
, struct show_params
*);
105 static int ldp_vty_get_af(const char *, int *);
108 show_interface_msg(struct vty
*vty
, struct imsg
*imsg
,
109 struct show_params
*params
)
111 struct ctl_iface
*iface
;
114 switch (imsg
->hdr
.type
) {
115 case IMSG_CTL_SHOW_INTERFACE
:
118 if (params
->family
!= AF_UNSPEC
&& params
->family
!= iface
->af
)
121 snprintf(timers
, sizeof(timers
), "%u/%u",
122 iface
->hello_interval
, iface
->hello_holdtime
);
124 vty_out(vty
, "%-4s %-11s %-6s %-8s %-12s %3u%s",
125 af_name(iface
->af
), iface
->name
,
126 if_state_name(iface
->state
), iface
->uptime
== 0 ?
127 "00:00:00" : log_time(iface
->uptime
), timers
,
128 iface
->adj_cnt
, VTY_NEWLINE
);
131 vty_out(vty
, "%s", VTY_NEWLINE
);
141 show_interface_msg_json(struct imsg
*imsg
, struct show_params
*params
,
144 struct ctl_iface
*iface
;
145 json_object
*json_iface
;
148 switch (imsg
->hdr
.type
) {
149 case IMSG_CTL_SHOW_INTERFACE
:
152 if (params
->family
!= AF_UNSPEC
&& params
->family
!= iface
->af
)
155 json_iface
= json_object_new_object();
156 json_object_string_add(json_iface
, "name", iface
->name
);
157 json_object_string_add(json_iface
, "addressFamily",
159 json_object_string_add(json_iface
, "state",
160 if_state_name(iface
->state
));
161 json_object_string_add(json_iface
, "upTime",
162 log_time(iface
->uptime
));
163 json_object_int_add(json_iface
, "helloInterval",
164 iface
->hello_interval
);
165 json_object_int_add(json_iface
, "helloHoldtime",
166 iface
->hello_holdtime
);
167 json_object_int_add(json_iface
, "adjacencyCount",
170 sprintf(key_name
, "%s: %s", iface
->name
, af_name(iface
->af
));
171 json_object_object_add(json
, key_name
, json_iface
);
183 show_discovery_msg(struct vty
*vty
, struct imsg
*imsg
,
184 struct show_params
*params
)
189 switch (imsg
->hdr
.type
) {
190 case IMSG_CTL_SHOW_DISCOVERY
:
193 if (params
->family
!= AF_UNSPEC
&& params
->family
!= adj
->af
)
196 vty_out(vty
, "%-4s %-15s ", af_name(adj
->af
),
200 vty_out(vty
, "%-8s %-15s ", "Link", adj
->ifname
);
203 addr
= log_addr(adj
->af
, &adj
->src_addr
);
205 vty_out(vty
, "%-8s %-15s ", "Targeted", addr
);
206 if (strlen(addr
) > 15)
207 vty_out(vty
, "\n%46s", " ");
210 vty_out(vty
, "%9u\n", adj
->holdtime
);
213 vty_out(vty
, "%s", VTY_NEWLINE
);
223 show_discovery_detail_adj(struct vty
*vty
, char *buffer
, struct ctl_adj
*adj
)
225 size_t buflen
= strlen(buffer
);
227 snprintf(buffer
+ buflen
, LDPBUFSIZ
- buflen
,
228 " LDP Id: %s:0, Transport address: %s%s",
229 inet_ntoa(adj
->id
), log_addr(adj
->af
,
230 &adj
->trans_addr
), VTY_NEWLINE
);
231 buflen
= strlen(buffer
);
232 snprintf(buffer
+ buflen
, LDPBUFSIZ
- buflen
,
233 " Hold time: %u sec%s", adj
->holdtime
, VTY_NEWLINE
);
237 show_discovery_detail_msg(struct vty
*vty
, struct imsg
*imsg
,
238 struct show_params
*params
)
241 struct ctl_disc_if
*iface
;
242 struct ctl_disc_tnbr
*tnbr
;
243 struct in_addr rtr_id
;
244 union ldpd_addr
*trans_addr
;
246 static char ifaces_buffer
[LDPBUFSIZ
];
247 static char tnbrs_buffer
[LDPBUFSIZ
];
249 switch (imsg
->hdr
.type
) {
250 case IMSG_CTL_SHOW_DISCOVERY
:
251 ifaces_buffer
[0] = '\0';
252 tnbrs_buffer
[0] = '\0';
254 case IMSG_CTL_SHOW_DISC_IFACE
:
257 if (params
->family
!= AF_UNSPEC
&&
258 ((params
->family
== AF_INET
&& !iface
->active_v4
) ||
259 (params
->family
== AF_INET6
&& !iface
->active_v6
)))
262 buflen
= strlen(ifaces_buffer
);
263 snprintf(ifaces_buffer
+ buflen
, LDPBUFSIZ
- buflen
,
264 " %s: %s%s", iface
->name
, (iface
->no_adj
) ?
265 "xmit" : "xmit/recv", VTY_NEWLINE
);
267 case IMSG_CTL_SHOW_DISC_TNBR
:
270 if (params
->family
!= AF_UNSPEC
&& params
->family
!= tnbr
->af
)
273 trans_addr
= &(ldp_af_conf_get(ldpd_conf
,
274 tnbr
->af
))->trans_addr
;
275 buflen
= strlen(tnbrs_buffer
);
276 snprintf(tnbrs_buffer
+ buflen
, LDPBUFSIZ
- buflen
,
277 " %s -> %s: %s%s", log_addr(tnbr
->af
, trans_addr
),
278 log_addr(tnbr
->af
, &tnbr
->addr
), (tnbr
->no_adj
) ? "xmit" :
279 "xmit/recv", VTY_NEWLINE
);
281 case IMSG_CTL_SHOW_DISC_ADJ
:
284 if (params
->family
!= AF_UNSPEC
&& params
->family
!= adj
->af
)
289 show_discovery_detail_adj(vty
, ifaces_buffer
, adj
);
292 show_discovery_detail_adj(vty
, tnbrs_buffer
, adj
);
297 rtr_id
.s_addr
= ldp_rtr_id_get(ldpd_conf
);
298 vty_out(vty
, "Local LDP Identifier: %s:0%s", inet_ntoa(rtr_id
),
300 vty_out(vty
, "Discovery Sources:%s", VTY_NEWLINE
);
301 vty_out(vty
, " Interfaces:%s", VTY_NEWLINE
);
302 vty_out(vty
, "%s", ifaces_buffer
);
303 vty_out(vty
, " Targeted Hellos:%s", VTY_NEWLINE
);
304 vty_out(vty
, "%s", tnbrs_buffer
);
305 vty_out(vty
, "%s", VTY_NEWLINE
);
315 show_discovery_msg_json(struct imsg
*imsg
, struct show_params
*params
,
319 json_object
*json_array
;
320 json_object
*json_adj
;
322 switch (imsg
->hdr
.type
) {
323 case IMSG_CTL_SHOW_DISCOVERY
:
326 if (params
->family
!= AF_UNSPEC
&& params
->family
!= adj
->af
)
329 json_object_object_get_ex(json
, "adjacencies", &json_array
);
331 json_array
= json_object_new_array();
332 json_object_object_add(json
, "adjacencies", json_array
);
335 json_adj
= json_object_new_object();
336 json_object_string_add(json_adj
, "addressFamily",
338 json_object_string_add(json_adj
, "neighborId",
342 json_object_string_add(json_adj
, "type", "link");
343 json_object_string_add(json_adj
, "interface",
347 json_object_string_add(json_adj
, "type", "targeted");
348 json_object_string_add(json_adj
, "peer",
349 log_addr(adj
->af
, &adj
->src_addr
));
352 json_object_int_add(json_adj
, "helloHoldtime", adj
->holdtime
);
354 json_object_array_add(json_array
, json_adj
);
366 show_discovery_detail_adj_json(json_object
*json
, struct ctl_adj
*adj
)
368 json_object
*json_adj
;
369 json_object
*json_array
;
371 json_object_object_get_ex(json
, "adjacencies", &json_array
);
373 json_array
= json_object_new_array();
374 json_object_object_add(json
, "adjacencies", json_array
);
377 json_adj
= json_object_new_object();
378 json_object_string_add(json_adj
, "id", inet_ntoa(adj
->id
));
379 json_object_string_add(json_adj
, "transportAddress", log_addr(adj
->af
,
381 json_object_int_add(json_adj
, "holdtime", adj
->holdtime
);
382 json_object_array_add(json_array
, json_adj
);
386 show_discovery_detail_msg_json(struct imsg
*imsg
, struct show_params
*params
,
390 struct ctl_disc_if
*iface
;
391 struct ctl_disc_tnbr
*tnbr
;
392 struct in_addr rtr_id
;
393 union ldpd_addr
*trans_addr
;
394 json_object
*json_interface
;
395 json_object
*json_target
;
396 static json_object
*json_interfaces
;
397 static json_object
*json_targets
;
398 static json_object
*json_container
;
400 switch (imsg
->hdr
.type
) {
401 case IMSG_CTL_SHOW_DISCOVERY
:
402 rtr_id
.s_addr
= ldp_rtr_id_get(ldpd_conf
);
403 json_object_string_add(json
, "id", inet_ntoa(rtr_id
));
404 json_interfaces
= json_object_new_object();
405 json_object_object_add(json
, "interfaces", json_interfaces
);
406 json_targets
= json_object_new_object();
407 json_object_object_add(json
, "targetedHellos", json_targets
);
408 json_container
= NULL
;
410 case IMSG_CTL_SHOW_DISC_IFACE
:
413 if (params
->family
!= AF_UNSPEC
&&
414 ((params
->family
== AF_INET
&& !iface
->active_v4
) ||
415 (params
->family
== AF_INET6
&& !iface
->active_v6
)))
418 json_interface
= json_object_new_object();
419 json_object_boolean_true_add(json_interface
, "transmit");
421 json_object_boolean_true_add(json_interface
, "receive");
423 json_object_object_add(json_interfaces
, iface
->name
,
425 json_container
= json_interface
;
427 case IMSG_CTL_SHOW_DISC_TNBR
:
430 if (params
->family
!= AF_UNSPEC
&& params
->family
!= tnbr
->af
)
433 trans_addr
= &(ldp_af_conf_get(ldpd_conf
, tnbr
->af
))->trans_addr
;
435 json_target
= json_object_new_object();
436 json_object_string_add(json_target
, "sourceAddress",
437 log_addr(tnbr
->af
, trans_addr
));
438 json_object_boolean_true_add(json_target
, "transmit");
440 json_object_boolean_true_add(json_target
, "receive");
442 json_object_object_add(json_targets
, log_addr(tnbr
->af
,
443 &tnbr
->addr
), json_target
);
444 json_container
= json_target
;
446 case IMSG_CTL_SHOW_DISC_ADJ
:
449 if (params
->family
!= AF_UNSPEC
&& params
->family
!= adj
->af
)
454 show_discovery_detail_adj_json(json_container
, adj
);
457 show_discovery_detail_adj_json(json_container
, adj
);
471 show_nbr_msg(struct vty
*vty
, struct imsg
*imsg
, struct show_params
*params
)
476 switch (imsg
->hdr
.type
) {
477 case IMSG_CTL_SHOW_NBR
:
480 addr
= log_addr(nbr
->af
, &nbr
->raddr
);
482 vty_out(vty
, "%-4s %-15s %-11s %-15s",
483 af_name(nbr
->af
), inet_ntoa(nbr
->id
),
484 nbr_state_name(nbr
->nbr_state
), addr
);
485 if (strlen(addr
) > 15)
486 vty_out(vty
, "\n%48s", " ");
487 vty_out(vty
, " %8s\n", nbr
->uptime
== 0 ? "-" :
488 log_time(nbr
->uptime
));
500 show_nbr_detail_adj(struct vty
*vty
, char *buffer
, struct ctl_adj
*adj
)
502 size_t buflen
= strlen(buffer
);
506 snprintf(buffer
+ buflen
, LDPBUFSIZ
- buflen
,
507 " Interface: %s%s", adj
->ifname
, VTY_NEWLINE
);
510 snprintf(buffer
+ buflen
, LDPBUFSIZ
- buflen
,
511 " Targeted Hello: %s%s", log_addr(adj
->af
,
512 &adj
->src_addr
), VTY_NEWLINE
);
518 show_nbr_detail_msg(struct vty
*vty
, struct imsg
*imsg
, struct show_params
*params
)
522 static char v4adjs_buffer
[LDPBUFSIZ
];
523 static char v6adjs_buffer
[LDPBUFSIZ
];
525 switch (imsg
->hdr
.type
) {
526 case IMSG_CTL_SHOW_NBR
:
529 v4adjs_buffer
[0] = '\0';
530 v6adjs_buffer
[0] = '\0';
531 vty_out(vty
, "Peer LDP Identifier: %s:0%s", inet_ntoa(nbr
->id
),
533 vty_out(vty
, " TCP connection: %s:%u - %s:%u%s",
534 log_addr(nbr
->af
, &nbr
->laddr
), ntohs(nbr
->lport
),
535 log_addr(nbr
->af
, &nbr
->raddr
), ntohs(nbr
->rport
),
537 vty_out(vty
, " Session Holdtime: %u sec%s", nbr
->holdtime
,
539 vty_out(vty
, " State: %s; Downstream-Unsolicited%s",
540 nbr_state_name(nbr
->nbr_state
), VTY_NEWLINE
);
541 vty_out(vty
, " Up time: %s%s", log_time(nbr
->uptime
),
544 case IMSG_CTL_SHOW_NBR_DISC
:
549 show_nbr_detail_adj(vty
, v4adjs_buffer
, adj
);
552 show_nbr_detail_adj(vty
, v6adjs_buffer
, adj
);
555 fatalx("show_nbr_detail_msg: unknown af");
558 case IMSG_CTL_SHOW_NBR_END
:
559 vty_out(vty
, " LDP Discovery Sources:%s", VTY_NEWLINE
);
560 if (v4adjs_buffer
[0] != '\0') {
561 vty_out(vty
, " IPv4:%s", VTY_NEWLINE
);
562 vty_out(vty
, "%s", v4adjs_buffer
);
564 if (v6adjs_buffer
[0] != '\0') {
565 vty_out(vty
, " IPv6:%s", VTY_NEWLINE
);
566 vty_out(vty
, "%s", v6adjs_buffer
);
568 vty_out(vty
, "%s", VTY_NEWLINE
);
580 show_nbr_msg_json(struct imsg
*imsg
, struct show_params
*params
,
584 json_object
*json_array
;
585 json_object
*json_nbr
;
587 switch (imsg
->hdr
.type
) {
588 case IMSG_CTL_SHOW_NBR
:
591 json_object_object_get_ex(json
, "neighbors", &json_array
);
593 json_array
= json_object_new_array();
594 json_object_object_add(json
, "neighbors", json_array
);
597 json_nbr
= json_object_new_object();
598 json_object_string_add(json_nbr
, "addressFamily",
600 json_object_string_add(json_nbr
, "neighborId",
602 json_object_string_add(json_nbr
, "state",
603 nbr_state_name(nbr
->nbr_state
));
604 json_object_string_add(json_nbr
, "transportAddress",
605 log_addr(nbr
->af
, &nbr
->raddr
));
606 json_object_string_add(json_nbr
, "upTime",
607 log_time(nbr
->uptime
));
609 json_object_array_add(json_array
, json_nbr
);
621 show_nbr_detail_adj_json(struct ctl_adj
*adj
, json_object
*adj_list
)
623 char adj_string
[128];
627 strlcpy(adj_string
, "interface: ", sizeof(adj_string
));
628 strlcat(adj_string
, adj
->ifname
, sizeof(adj_string
));
631 strlcpy(adj_string
, "targetedHello: ", sizeof(adj_string
));
632 strlcat(adj_string
, log_addr(adj
->af
, &adj
->src_addr
),
637 json_object_array_add(adj_list
, json_object_new_string(adj_string
));
641 show_nbr_detail_msg_json(struct imsg
*imsg
, struct show_params
*params
,
646 json_object
*json_nbr
;
647 static json_object
*json_nbr_sources
;
648 static json_object
*json_v4adjs
;
649 static json_object
*json_v6adjs
;
651 switch (imsg
->hdr
.type
) {
652 case IMSG_CTL_SHOW_NBR
:
655 json_nbr
= json_object_new_object();
656 json_object_string_add(json_nbr
, "peerId", inet_ntoa(nbr
->id
));
657 json_object_string_add(json_nbr
, "tcpLocalAddress",
658 log_addr(nbr
->af
, &nbr
->laddr
));
659 json_object_int_add(json_nbr
, "tcpLocalPort",
661 json_object_string_add(json_nbr
, "tcpRemoteAddress",
662 log_addr(nbr
->af
, &nbr
->raddr
));
663 json_object_int_add(json_nbr
, "tcpRemotePort",
665 json_object_int_add(json_nbr
, "holdtime", nbr
->holdtime
);
666 json_object_string_add(json_nbr
, "state",
667 nbr_state_name(nbr
->nbr_state
));
668 json_object_string_add(json_nbr
, "upTime",
669 log_time(nbr
->uptime
));
670 json_object_object_add(json
, inet_ntoa(nbr
->id
), json_nbr
);
672 json_nbr_sources
= json_object_new_object();
673 json_object_object_add(json_nbr
, "discoverySources",
678 case IMSG_CTL_SHOW_NBR_DISC
:
684 json_v4adjs
= json_object_new_array();
685 json_object_object_add(json_nbr_sources
, "ipv4",
688 show_nbr_detail_adj_json(adj
, json_v4adjs
);
692 json_v6adjs
= json_object_new_array();
693 json_object_object_add(json_nbr_sources
, "ipv6",
696 show_nbr_detail_adj_json(adj
, json_v6adjs
);
699 fatalx("show_nbr_detail_msg_json: unknown af");
702 case IMSG_CTL_SHOW_NBR_END
:
714 show_lib_msg(struct vty
*vty
, struct imsg
*imsg
, struct show_params
*params
)
719 switch (imsg
->hdr
.type
) {
720 case IMSG_CTL_SHOW_LIB
:
723 if (params
->family
!= AF_UNSPEC
&& params
->family
!= rt
->af
)
726 snprintf(dstnet
, sizeof(dstnet
), "%s/%d",
727 log_addr(rt
->af
, &rt
->prefix
), rt
->prefixlen
);
729 vty_out(vty
, "%-4s %-20s", af_name(rt
->af
), dstnet
);
730 if (strlen(dstnet
) > 20)
731 vty_out(vty
, "\n%25s", " ");
732 vty_out(vty
, " %-15s %-11s %-13s %6s\n", inet_ntoa(rt
->nexthop
),
733 log_label(rt
->local_label
), log_label(rt
->remote_label
),
734 rt
->in_use
? "yes" : "no");
737 vty_out(vty
, "%s", VTY_NEWLINE
);
747 show_lib_detail_msg(struct vty
*vty
, struct imsg
*imsg
, struct show_params
*params
)
752 switch (imsg
->hdr
.type
) {
753 case IMSG_CTL_SHOW_LIB
:
756 if (params
->family
!= AF_UNSPEC
&& params
->family
!= rt
->af
)
759 snprintf(dstnet
, sizeof(dstnet
), "%s/%d",
760 log_addr(rt
->af
, &rt
->prefix
), rt
->prefixlen
);
763 vty_out(vty
, "%s%s", dstnet
, VTY_NEWLINE
);
764 vty_out(vty
, "%-8sLocal binding: label: %s%s", "",
765 log_label(rt
->local_label
), VTY_NEWLINE
);
767 if (rt
->remote_label
!= NO_LABEL
) {
768 vty_out(vty
, "%-8sRemote bindings:%s", "",
770 vty_out(vty
, "%-12sPeer Label%s",
772 vty_out(vty
, "%-12s----------------- "
773 "---------%s", "", VTY_NEWLINE
);
775 vty_out(vty
, "%-8sNo remote bindings%s", "",
778 if (rt
->remote_label
!= NO_LABEL
)
779 vty_out(vty
, "%12s%-20s%s%s", "", inet_ntoa(rt
->nexthop
),
780 log_label(rt
->remote_label
), VTY_NEWLINE
);
783 vty_out(vty
, "%s", VTY_NEWLINE
);
793 show_lib_msg_json(struct imsg
*imsg
, struct show_params
*params
,
797 json_object
*json_array
;
798 json_object
*json_lib_entry
;
801 switch (imsg
->hdr
.type
) {
802 case IMSG_CTL_SHOW_LIB
:
805 if (params
->family
!= AF_UNSPEC
&& params
->family
!= rt
->af
)
808 json_object_object_get_ex(json
, "bindings", &json_array
);
810 json_array
= json_object_new_array();
811 json_object_object_add(json
, "bindings", json_array
);
814 json_lib_entry
= json_object_new_object();
815 json_object_string_add(json_lib_entry
, "addressFamily",
817 snprintf(dstnet
, sizeof(dstnet
), "%s/%d",
818 log_addr(rt
->af
, &rt
->prefix
), rt
->prefixlen
);
819 json_object_string_add(json_lib_entry
, "prefix", dstnet
);
820 json_object_string_add(json_lib_entry
, "neighborId",
821 inet_ntoa(rt
->nexthop
));
822 json_object_string_add(json_lib_entry
, "localLabel",
823 log_label(rt
->local_label
));
824 json_object_string_add(json_lib_entry
, "remoteLabel",
825 log_label(rt
->remote_label
));
826 json_object_int_add(json_lib_entry
, "inUse", rt
->in_use
);
828 json_object_array_add(json_array
, json_lib_entry
);
840 show_lib_detail_msg_json(struct imsg
*imsg
, struct show_params
*params
,
845 static json_object
*json_binding
;
846 static json_object
*json_remote_labels
;
847 json_object
*json_remote_label
;
849 switch (imsg
->hdr
.type
) {
850 case IMSG_CTL_SHOW_LIB
:
853 if (params
->family
!= AF_UNSPEC
&& params
->family
!= rt
->af
)
856 snprintf(dstnet
, sizeof(dstnet
), "%s/%d",
857 log_addr(rt
->af
, &rt
->prefix
), rt
->prefixlen
);
860 json_binding
= json_object_new_object();
861 json_object_string_add(json_binding
, "localLabel",
862 log_label(rt
->local_label
));
864 json_remote_labels
= json_object_new_array();
865 json_object_object_add(json_binding
, "remoteLabels",
867 json_object_object_add(json
, dstnet
, json_binding
);
870 if (rt
->remote_label
!= NO_LABEL
) {
871 json_remote_label
= json_object_new_object();
872 json_object_string_add(json_remote_label
, "nexthop",
873 inet_ntoa(rt
->nexthop
));
874 json_object_string_add(json_remote_label
, "label",
875 log_label(rt
->remote_label
));
876 json_object_array_add(json_remote_labels
,
890 show_l2vpn_binding_msg(struct vty
*vty
, struct imsg
*imsg
,
891 struct show_params
*params
)
895 switch (imsg
->hdr
.type
) {
896 case IMSG_CTL_SHOW_L2VPN_BINDING
:
899 vty_out(vty
, " Destination Address: %s, VC ID: %u%s",
900 inet_ntoa(pw
->lsr_id
), pw
->pwid
, VTY_NEWLINE
);
903 if (pw
->local_label
!= NO_LABEL
) {
904 vty_out(vty
, " Local Label: %u%s", pw
->local_label
,
906 vty_out(vty
, "%-8sCbit: %u, VC Type: %s, "
907 "GroupID: %u%s", "", pw
->local_cword
,
908 pw_type_name(pw
->type
), pw
->local_gid
,
910 vty_out(vty
, "%-8sMTU: %u%s", "", pw
->local_ifmtu
,
913 vty_out(vty
, " Local Label: unassigned%s",
917 if (pw
->remote_label
!= NO_LABEL
) {
918 vty_out(vty
, " Remote Label: %u%s",
919 pw
->remote_label
, VTY_NEWLINE
);
920 vty_out(vty
, "%-8sCbit: %u, VC Type: %s, "
921 "GroupID: %u%s", "", pw
->remote_cword
,
922 pw_type_name(pw
->type
), pw
->remote_gid
,
924 vty_out(vty
, "%-8sMTU: %u%s", "", pw
->remote_ifmtu
,
927 vty_out(vty
, " Remote Label: unassigned%s",
931 vty_out(vty
, "%s", VTY_NEWLINE
);
941 show_l2vpn_binding_msg_json(struct imsg
*imsg
, struct show_params
*params
,
945 json_object
*json_pw
;
948 switch (imsg
->hdr
.type
) {
949 case IMSG_CTL_SHOW_L2VPN_BINDING
:
952 json_pw
= json_object_new_object();
953 json_object_string_add(json_pw
, "destination",
954 inet_ntoa(pw
->lsr_id
));
955 json_object_int_add(json_pw
, "vcId", pw
->pwid
);
958 if (pw
->local_label
!= NO_LABEL
) {
959 json_object_int_add(json_pw
, "localLabel",
961 json_object_int_add(json_pw
, "localControlWord",
963 json_object_string_add(json_pw
, "localVcType",
964 pw_type_name(pw
->type
));
965 json_object_int_add(json_pw
, "localGroupID",
967 json_object_int_add(json_pw
, "localIfMtu",
970 json_object_string_add(json_pw
, "localLabel",
974 if (pw
->remote_label
!= NO_LABEL
) {
975 json_object_int_add(json_pw
, "remoteLabel",
977 json_object_int_add(json_pw
, "remoteControlWord",
979 json_object_string_add(json_pw
, "remoteVcType",
980 pw_type_name(pw
->type
));
981 json_object_int_add(json_pw
, "remoteGroupID",
983 json_object_int_add(json_pw
, "remoteIfMtu",
986 json_object_string_add(json_pw
, "remoteLabel",
989 sprintf(key_name
, "%s: %u", inet_ntoa(pw
->lsr_id
), pw
->pwid
);
990 json_object_object_add(json
, key_name
, json_pw
);
1002 show_l2vpn_pw_msg(struct vty
*vty
, struct imsg
*imsg
, struct show_params
*params
)
1006 switch (imsg
->hdr
.type
) {
1007 case IMSG_CTL_SHOW_L2VPN_PW
:
1010 vty_out(vty
, "%-9s %-15s %-10u %-16s %-10s%s", pw
->ifname
,
1011 inet_ntoa(pw
->lsr_id
), pw
->pwid
, pw
->l2vpn_name
,
1012 (pw
->status
? "UP" : "DOWN"), VTY_NEWLINE
);
1015 vty_out(vty
, "%s", VTY_NEWLINE
);
1025 show_l2vpn_pw_msg_json(struct imsg
*imsg
, struct show_params
*params
,
1029 json_object
*json_pw
;
1031 switch (imsg
->hdr
.type
) {
1032 case IMSG_CTL_SHOW_L2VPN_PW
:
1035 json_pw
= json_object_new_object();
1036 json_object_string_add(json_pw
, "peerId", inet_ntoa(pw
->lsr_id
));
1037 json_object_int_add(json_pw
, "vcId", pw
->pwid
);
1038 json_object_string_add(json_pw
, "VpnName", pw
->l2vpn_name
);
1040 json_object_string_add(json_pw
, "status", "up");
1042 json_object_string_add(json_pw
, "status", "down");
1043 json_object_object_add(json
, pw
->ifname
, json_pw
);
1055 ldp_vty_connect(struct imsgbuf
*ibuf
)
1057 struct sockaddr_un s_un
;
1060 /* connect to ldpd control socket */
1061 if ((ctl_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1062 log_warn("%s: socket", __func__
);
1066 memset(&s_un
, 0, sizeof(s_un
));
1067 s_un
.sun_family
= AF_UNIX
;
1068 strlcpy(s_un
.sun_path
, ctl_sock_path
, sizeof(s_un
.sun_path
));
1069 if (connect(ctl_sock
, (struct sockaddr
*)&s_un
, sizeof(s_un
)) == -1) {
1070 log_warn("%s: connect: %s", __func__
, ctl_sock_path
);
1075 imsg_init(ibuf
, ctl_sock
);
1081 ldp_vty_dispatch_msg(struct vty
*vty
, struct imsg
*imsg
, enum show_command cmd
,
1082 struct show_params
*params
, json_object
*json
)
1089 ret
= show_interface_msg_json(imsg
, params
, json
);
1091 ret
= show_interface_msg(vty
, imsg
, params
);
1094 if (params
->detail
) {
1096 ret
= show_discovery_detail_msg_json(imsg
,
1099 ret
= show_discovery_detail_msg(vty
, imsg
,
1103 ret
= show_discovery_msg_json(imsg
, params
,
1106 ret
= show_discovery_msg(vty
, imsg
, params
);
1110 if (params
->detail
) {
1112 ret
= show_nbr_detail_msg_json(imsg
, params
,
1115 ret
= show_nbr_detail_msg(vty
, imsg
, params
);
1118 ret
= show_nbr_msg_json(imsg
, params
, json
);
1120 ret
= show_nbr_msg(vty
, imsg
, params
);
1124 if (params
->detail
) {
1126 ret
= show_lib_detail_msg_json(imsg
, params
,
1129 ret
= show_lib_detail_msg(vty
, imsg
, params
);
1132 ret
= show_lib_msg_json(imsg
, params
, json
);
1134 ret
= show_lib_msg(vty
, imsg
, params
);
1139 ret
= show_l2vpn_pw_msg_json(imsg
, params
, json
);
1141 ret
= show_l2vpn_pw_msg(vty
, imsg
, params
);
1143 case SHOW_L2VPN_BINDING
:
1145 ret
= show_l2vpn_binding_msg_json(imsg
, params
, json
);
1147 ret
= show_l2vpn_binding_msg(vty
, imsg
, params
);
1157 ldp_vty_dispatch(struct vty
*vty
, struct imsgbuf
*ibuf
, enum show_command cmd
,
1158 struct show_params
*params
)
1161 int n
, done
= 0, ret
= CMD_SUCCESS
;
1162 json_object
*json
= NULL
;
1164 while (ibuf
->w
.queued
)
1165 if (msgbuf_write(&ibuf
->w
) <= 0 && errno
!= EAGAIN
) {
1166 log_warn("write error");
1168 return (CMD_WARNING
);
1172 json
= json_object_new_object();
1175 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
) {
1176 log_warnx("imsg_read error");
1181 log_warnx("pipe closed");
1187 if ((n
= imsg_get(ibuf
, &imsg
)) == -1) {
1188 log_warnx("imsg_get error");
1194 done
= ldp_vty_dispatch_msg(vty
, &imsg
, cmd
, params
,
1203 vty_out(vty
, "%s%s", json_object_to_json_string_ext(json
,
1204 JSON_C_TO_STRING_PRETTY
), VTY_NEWLINE
);
1205 json_object_free(json
);
1212 ldp_vty_get_af(const char *str
, int *af
)
1217 } else if (strcmp(str
, "ipv4") == 0) {
1220 } else if (strcmp(str
, "ipv6") == 0) {
1229 ldp_vty_show_binding(struct vty
*vty
, struct vty_arg
*args
[])
1231 struct imsgbuf ibuf
;
1232 struct show_params params
;
1236 if (ldp_vty_connect(&ibuf
) < 0)
1237 return (CMD_WARNING
);
1239 af_str
= vty_get_arg_value(args
, "address-family");
1240 if (ldp_vty_get_af(af_str
, &af
) < 0)
1241 return (CMD_ERR_NO_MATCH
);
1243 memset(¶ms
, 0, sizeof(params
));
1245 params
.detail
= vty_get_arg_value(args
, "detail") ? 1 : 0;
1246 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1248 if (!params
.detail
&& !params
.json
)
1249 vty_out(vty
, "%-4s %-20s %-15s %-11s %-13s %6s\n", "AF",
1250 "Destination", "Nexthop", "Local Label", "Remote Label",
1253 imsg_compose(&ibuf
, IMSG_CTL_SHOW_LIB
, 0, 0, -1, NULL
, 0);
1254 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_LIB
, ¶ms
));
1258 ldp_vty_show_discovery(struct vty
*vty
, struct vty_arg
*args
[])
1260 struct imsgbuf ibuf
;
1261 struct show_params params
;
1265 if (ldp_vty_connect(&ibuf
) < 0)
1266 return (CMD_WARNING
);
1268 af_str
= vty_get_arg_value(args
, "address-family");
1269 if (ldp_vty_get_af(af_str
, &af
) < 0)
1270 return (CMD_ERR_NO_MATCH
);
1272 memset(¶ms
, 0, sizeof(params
));
1274 params
.detail
= vty_get_arg_value(args
, "detail") ? 1 : 0;
1275 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1277 if (!params
.detail
&& !params
.json
)
1278 vty_out(vty
, "%-4s %-15s %-8s %-15s %9s\n",
1279 "AF", "ID", "Type", "Source", "Holdtime");
1282 imsg_compose(&ibuf
, IMSG_CTL_SHOW_DISCOVERY_DTL
, 0, 0, -1,
1285 imsg_compose(&ibuf
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0, -1, NULL
, 0);
1286 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_DISC
, ¶ms
));
1290 ldp_vty_show_interface(struct vty
*vty
, struct vty_arg
*args
[])
1292 struct imsgbuf ibuf
;
1293 struct show_params params
;
1294 unsigned int ifidx
= 0;
1298 if (ldp_vty_connect(&ibuf
) < 0)
1299 return (CMD_WARNING
);
1301 af_str
= vty_get_arg_value(args
, "address-family");
1302 if (ldp_vty_get_af(af_str
, &af
) < 0)
1303 return (CMD_ERR_NO_MATCH
);
1305 memset(¶ms
, 0, sizeof(params
));
1307 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1311 vty_out(vty
, "%-4s %-11s %-6s %-8s %-12s %3s%s", "AF",
1312 "Interface", "State", "Uptime", "Hello Timers", "ac",
1316 imsg_compose(&ibuf
, IMSG_CTL_SHOW_INTERFACE
, 0, 0, -1, &ifidx
,
1318 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_IFACE
, ¶ms
));
1322 ldp_vty_show_neighbor(struct vty
*vty
, struct vty_arg
*args
[])
1324 struct imsgbuf ibuf
;
1325 struct show_params params
;
1327 if (ldp_vty_connect(&ibuf
) < 0)
1328 return (CMD_WARNING
);
1330 memset(¶ms
, 0, sizeof(params
));
1331 params
.detail
= vty_get_arg_value(args
, "detail") ? 1 : 0;
1332 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1334 if (!params
.detail
&& !params
.json
)
1335 vty_out(vty
, "%-4s %-15s %-11s %-15s %8s\n",
1336 "AF", "ID", "State", "Remote Address", "Uptime");
1338 imsg_compose(&ibuf
, IMSG_CTL_SHOW_NBR
, 0, 0, -1, NULL
, 0);
1339 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_NBR
, ¶ms
));
1343 ldp_vty_show_atom_binding(struct vty
*vty
, struct vty_arg
*args
[])
1345 struct imsgbuf ibuf
;
1346 struct show_params params
;
1348 if (ldp_vty_connect(&ibuf
) < 0)
1349 return (CMD_WARNING
);
1351 memset(¶ms
, 0, sizeof(params
));
1352 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1354 imsg_compose(&ibuf
, IMSG_CTL_SHOW_L2VPN_BINDING
, 0, 0, -1, NULL
, 0);
1355 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_L2VPN_BINDING
, ¶ms
));
1359 ldp_vty_show_atom_vc(struct vty
*vty
, struct vty_arg
*args
[])
1361 struct imsgbuf ibuf
;
1362 struct show_params params
;
1364 if (ldp_vty_connect(&ibuf
) < 0)
1365 return (CMD_WARNING
);
1367 memset(¶ms
, 0, sizeof(params
));
1368 params
.json
= vty_get_arg_value(args
, "json") ? 1 : 0;
1372 vty_out(vty
, "%-9s %-15s %-10s %-16s %-10s%s",
1373 "Interface", "Peer ID", "VC ID", "Name", "Status",
1375 vty_out(vty
, "%-9s %-15s %-10s %-16s %-10s%s",
1376 "---------", "---------------", "----------",
1377 "----------------", "----------", VTY_NEWLINE
);
1380 imsg_compose(&ibuf
, IMSG_CTL_SHOW_L2VPN_PW
, 0, 0, -1, NULL
, 0);
1381 return (ldp_vty_dispatch(vty
, &ibuf
, SHOW_L2VPN_PW
, ¶ms
));
1385 ldp_vty_clear_nbr(struct vty
*vty
, struct vty_arg
*args
[])
1387 struct imsgbuf ibuf
;
1388 const char *addr_str
;
1391 addr_str
= vty_get_arg_value(args
, "addr");
1393 memset(&nbr
, 0, sizeof(nbr
));
1395 (ldp_get_address(addr_str
, &nbr
.af
, &nbr
.raddr
) == -1 ||
1396 bad_addr(nbr
.af
, &nbr
.raddr
))) {
1397 vty_out(vty
, "%% Malformed address%s", VTY_NEWLINE
);
1398 return (CMD_WARNING
);
1401 if (ldp_vty_connect(&ibuf
) < 0)
1402 return (CMD_WARNING
);
1404 imsg_compose(&ibuf
, IMSG_CTL_CLEAR_NBR
, 0, 0, -1, &nbr
, sizeof(nbr
));
1406 while (ibuf
.w
.queued
)
1407 if (msgbuf_write(&ibuf
.w
) <= 0 && errno
!= EAGAIN
) {
1408 log_warn("write error");
1410 return (CMD_WARNING
);
1415 return (CMD_SUCCESS
);