ldpd: json support for show commands

Signed-off-by: Daniel Walton <dwalton@cumulusnetworks.com>
This commit is contained in:
Daniel Walton 2016-12-17 20:55:05 +00:00 committed by Renato Westphal
parent b6f1faf045
commit e8dbef0420
3 changed files with 2050 additions and 121 deletions

View File

@ -65,10 +65,10 @@ int ldp_vty_l2vpn_pw_nbr_addr(struct vty *, struct vty_arg *[]);
int ldp_vty_l2vpn_pw_nbr_id(struct vty *, struct vty_arg *[]); int ldp_vty_l2vpn_pw_nbr_id(struct vty *, struct vty_arg *[]);
int ldp_vty_l2vpn_pw_pwid(struct vty *, struct vty_arg *[]); int ldp_vty_l2vpn_pw_pwid(struct vty *, struct vty_arg *[]);
int ldp_vty_l2vpn_pw_pwstatus(struct vty *, struct vty_arg *[]); int ldp_vty_l2vpn_pw_pwstatus(struct vty *, struct vty_arg *[]);
int ldp_vty_show_binding(struct vty *, struct vty_arg *[]); int ldp_vty_show_binding(struct vty *, struct vty_arg *[], u_char);
int ldp_vty_show_discovery(struct vty *, struct vty_arg *[]); int ldp_vty_show_discovery(struct vty *, struct vty_arg *[], u_char);
int ldp_vty_show_interface(struct vty *, struct vty_arg *[]); int ldp_vty_show_interface(struct vty *, struct vty_arg *[], u_char);
int ldp_vty_show_neighbor(struct vty *, struct vty_arg *[]); int ldp_vty_show_neighbor(struct vty *, struct vty_arg *[], u_char);
int ldp_vty_show_atom_binding(struct vty *, struct vty_arg *[]); int ldp_vty_show_atom_binding(struct vty *, struct vty_arg *[]);
int ldp_vty_show_atom_vc(struct vty *, struct vty_arg *[]); int ldp_vty_show_atom_vc(struct vty *, struct vty_arg *[]);
int ldp_vty_clear_nbr(struct vty *, struct vty_arg *[]); int ldp_vty_clear_nbr(struct vty *, struct vty_arg *[]);

1726
ldpd/ldp_vty_cmds.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,7 @@
#include "lde.h" #include "lde.h"
#include "log.h" #include "log.h"
#include "ldp_vty.h" #include "ldp_vty.h"
#include "lib/json.h"
#include "command.h" #include "command.h"
#include "vty.h" #include "vty.h"
@ -50,29 +51,31 @@ struct show_filter {
#define LDPBUFSIZ 65535 #define LDPBUFSIZ 65535
static int show_interface_msg(struct vty *, struct imsg *, static int show_interface_msg(struct vty *, struct imsg *,
struct show_filter *); struct show_filter *, json_object *);
static void show_discovery_adj(struct vty *, char *, static void show_discovery_adj(struct vty *, char *,
struct ctl_adj *); struct ctl_adj *, json_object *);
static int show_discovery_msg(struct vty *, struct imsg *, static int show_discovery_msg(struct vty *, struct imsg *,
struct show_filter *); struct show_filter *, json_object *);
static void show_nbr_adj(struct vty *, char *, struct ctl_adj *); static void show_nbr_adj(struct vty *, char *, struct ctl_adj *,
json_object *);
static int show_nbr_msg(struct vty *, struct imsg *, static int show_nbr_msg(struct vty *, struct imsg *,
struct show_filter *); struct show_filter *, json_object *, struct in_addr *);
static int show_lib_msg(struct vty *, struct imsg *, static int show_lib_msg(struct vty *, struct imsg *,
struct show_filter *); struct show_filter *, json_object *);
static int show_l2vpn_binding_msg(struct vty *, struct imsg *); static int show_l2vpn_binding_msg(struct vty *, struct imsg *);
static int show_l2vpn_pw_msg(struct vty *, struct imsg *); static int show_l2vpn_pw_msg(struct vty *, struct imsg *);
static int ldp_vty_connect(struct imsgbuf *); static int ldp_vty_connect(struct imsgbuf *);
static int ldp_vty_dispatch(struct vty *, struct imsgbuf *, static int ldp_vty_dispatch(struct vty *, struct imsgbuf *,
enum show_command, struct show_filter *); enum show_command, struct show_filter *, u_char);
static int ldp_vty_get_af(const char *, int *); static int ldp_vty_get_af(const char *, int *);
static int static int
show_interface_msg(struct vty *vty, struct imsg *imsg, show_interface_msg(struct vty *vty, struct imsg *imsg,
struct show_filter *filter) struct show_filter *filter, json_object *json)
{ {
struct ctl_iface *iface; struct ctl_iface *iface;
char timers[BUFSIZ]; char timers[BUFSIZ];
json_object *json_iface = NULL;
switch (imsg->hdr.type) { switch (imsg->hdr.type) {
case IMSG_CTL_SHOW_INTERFACE: case IMSG_CTL_SHOW_INTERFACE:
@ -81,14 +84,25 @@ show_interface_msg(struct vty *vty, struct imsg *imsg,
if (filter->family != AF_UNSPEC && filter->family != iface->af) if (filter->family != AF_UNSPEC && filter->family != iface->af)
break; break;
snprintf(timers, sizeof(timers), "%u/%u", if (json) {
iface->hello_interval, iface->hello_holdtime); json_iface = json_object_new_object();
json_object_string_add(json_iface, "addressFamily", af_name(iface->af));
json_object_string_add(json_iface, "state", if_state_name(iface->state));
json_object_string_add(json_iface, "uptime", log_time(iface->uptime));
json_object_int_add(json_iface, "helloInterval", iface->hello_interval);
json_object_int_add(json_iface, "holdtime", iface->hello_holdtime);
json_object_int_add(json_iface, "adjacencyCount", iface->adj_cnt);
json_object_object_add(json, iface->name, json_iface);
} else {
snprintf(timers, sizeof(timers), "%u/%u",
iface->hello_interval, iface->hello_holdtime);
vty_out(vty, "%-4s %-11s %-6s %-8s %-12s %3u%s", vty_out(vty, "%-4s %-11s %-6s %-8s %-12s %3u%s",
af_name(iface->af), iface->name, af_name(iface->af), iface->name,
if_state_name(iface->state), iface->uptime == 0 ? if_state_name(iface->state), iface->uptime == 0 ?
"00:00:00" : log_time(iface->uptime), timers, "00:00:00" : log_time(iface->uptime), timers,
iface->adj_cnt, VTY_NEWLINE); iface->adj_cnt, VTY_NEWLINE);
}
break; break;
case IMSG_CTL_END: case IMSG_CTL_END:
vty_out(vty, "%s", VTY_NEWLINE); vty_out(vty, "%s", VTY_NEWLINE);
@ -101,22 +115,52 @@ show_interface_msg(struct vty *vty, struct imsg *imsg,
} }
static void static void
show_discovery_adj(struct vty *vty, char *buffer, struct ctl_adj *adj) show_discovery_adj(struct vty *vty, char *buffer, struct ctl_adj *adj, json_object *json)
{ {
size_t buflen = strlen(buffer); size_t buflen = strlen(buffer);
json_object *json_adj = NULL;
json_object *json_array = NULL;
snprintf(buffer + buflen, LDPBUFSIZ - buflen, if (json) {
" LDP Id: %s:0, Transport address: %s%s", switch(adj->type) {
inet_ntoa(adj->id), log_addr(adj->af, case HELLO_LINK:
&adj->trans_addr), VTY_NEWLINE); json_object_object_get_ex(json, "adjacencyLink", &json_array);
buflen = strlen(buffer);
snprintf(buffer + buflen, LDPBUFSIZ - buflen, if (!json_array) {
" Hold time: %u sec%s", adj->holdtime, VTY_NEWLINE); json_array = json_object_new_array();
json_object_object_add(json, "adjacencyLink", json_array);
}
break;
case HELLO_TARGETED:
json_object_object_get_ex(json, "adjacencyTargeted", &json_array);
if (!json_array) {
json_array = json_object_new_array();
json_object_object_add(json, "adjacencyTargeted", json_array);
}
break;
}
json_adj = json_object_new_object();
json_object_string_add(json_adj, "id", inet_ntoa(adj->id));
json_object_string_add(json_adj, "transportAddress", log_addr(adj->af, &adj->trans_addr));
json_object_int_add(json_adj, "holdtime", adj->holdtime);
json_object_array_add(json_array, json_adj);
} else {
snprintf(buffer + buflen, LDPBUFSIZ - buflen,
" LDP Id: %s:0, Transport address: %s%s",
inet_ntoa(adj->id), log_addr(adj->af,
&adj->trans_addr), VTY_NEWLINE);
buflen = strlen(buffer);
snprintf(buffer + buflen, LDPBUFSIZ - buflen,
" Hold time: %u sec%s", adj->holdtime, VTY_NEWLINE);
}
} }
static int static int
show_discovery_msg(struct vty *vty, struct imsg *imsg, show_discovery_msg(struct vty *vty, struct imsg *imsg,
struct show_filter *filter) struct show_filter *filter, json_object *json)
{ {
struct ctl_adj *adj; struct ctl_adj *adj;
struct ctl_disc_if *iface; struct ctl_disc_if *iface;
@ -126,6 +170,10 @@ show_discovery_msg(struct vty *vty, struct imsg *imsg,
size_t buflen; size_t buflen;
static char ifaces_buffer[LDPBUFSIZ]; static char ifaces_buffer[LDPBUFSIZ];
static char tnbrs_buffer[LDPBUFSIZ]; static char tnbrs_buffer[LDPBUFSIZ];
json_object *json_interface = NULL;
json_object *json_interfaces = NULL;
json_object *json_target = NULL;
json_object *json_targets = NULL;
switch (imsg->hdr.type) { switch (imsg->hdr.type) {
case IMSG_CTL_SHOW_DISCOVERY: case IMSG_CTL_SHOW_DISCOVERY:
@ -140,10 +188,28 @@ show_discovery_msg(struct vty *vty, struct imsg *imsg,
(filter->family == AF_INET6 && !iface->active_v6))) (filter->family == AF_INET6 && !iface->active_v6)))
break; break;
buflen = strlen(ifaces_buffer); if (json) {
snprintf(ifaces_buffer + buflen, LDPBUFSIZ - buflen, json_object_object_get_ex(json, "interfaces", &json_interfaces);
" %s: %s%s", iface->name, (iface->no_adj) ?
"xmit" : "xmit/recv", VTY_NEWLINE); if (!json_interfaces) {
json_interfaces = json_object_new_object();
json_object_object_add(json, "interfaces", json_interfaces);
}
json_interface = json_object_new_object();
json_object_boolean_true_add(json_interface, "transmit");
if (iface->no_adj)
json_object_boolean_true_add(json_interface, "receive");
json_object_object_add(json_interfaces, iface->name, json_interface);
} else {
buflen = strlen(ifaces_buffer);
snprintf(ifaces_buffer + buflen, LDPBUFSIZ - buflen,
" %s: %s%s", iface->name, (iface->no_adj) ?
"xmit" : "xmit/recv", VTY_NEWLINE);
}
break; break;
case IMSG_CTL_SHOW_DISC_TNBR: case IMSG_CTL_SHOW_DISC_TNBR:
tnbr = imsg->data; tnbr = imsg->data;
@ -151,13 +217,31 @@ show_discovery_msg(struct vty *vty, struct imsg *imsg,
if (filter->family != AF_UNSPEC && filter->family != tnbr->af) if (filter->family != AF_UNSPEC && filter->family != tnbr->af)
break; break;
trans_addr = &(ldp_af_conf_get(ldpd_conf, trans_addr = &(ldp_af_conf_get(ldpd_conf, tnbr->af))->trans_addr;
tnbr->af))->trans_addr;
buflen = strlen(tnbrs_buffer); if (json) {
snprintf(tnbrs_buffer + buflen, LDPBUFSIZ - buflen, json_object_object_get_ex(json, "targeted", &json_targets);
" %s -> %s: %s%s", log_addr(tnbr->af, trans_addr),
log_addr(tnbr->af, &tnbr->addr), (tnbr->no_adj) ? "xmit" : if (!json_targets) {
"xmit/recv", VTY_NEWLINE); json_targets = json_object_new_array();
json_object_object_add(json, "targeted", json_targets);
}
json_target = json_object_new_object();
json_object_string_add(json_target, "sourceAddress", log_addr(tnbr->af, trans_addr));
json_object_boolean_true_add(json_target, "transmit");
if (tnbr->no_adj)
json_object_boolean_true_add(json_target, "receive");
json_object_object_add(json_targets, log_addr(tnbr->af, &tnbr->addr), json_interface);
} else {
buflen = strlen(tnbrs_buffer);
snprintf(tnbrs_buffer + buflen, LDPBUFSIZ - buflen,
" %s -> %s: %s%s", log_addr(tnbr->af, trans_addr),
log_addr(tnbr->af, &tnbr->addr), (tnbr->no_adj) ? "xmit" :
"xmit/recv", VTY_NEWLINE);
}
break; break;
case IMSG_CTL_SHOW_DISC_ADJ: case IMSG_CTL_SHOW_DISC_ADJ:
adj = imsg->data; adj = imsg->data;
@ -167,24 +251,29 @@ show_discovery_msg(struct vty *vty, struct imsg *imsg,
switch(adj->type) { switch(adj->type) {
case HELLO_LINK: case HELLO_LINK:
show_discovery_adj(vty, ifaces_buffer, adj); show_discovery_adj(vty, ifaces_buffer, adj, json);
break; break;
case HELLO_TARGETED: case HELLO_TARGETED:
show_discovery_adj(vty, tnbrs_buffer, adj); show_discovery_adj(vty, tnbrs_buffer, adj, json);
break; break;
} }
break; break;
case IMSG_CTL_END: case IMSG_CTL_END:
rtr_id.s_addr = ldp_rtr_id_get(ldpd_conf); rtr_id.s_addr = ldp_rtr_id_get(ldpd_conf);
vty_out(vty, "Local LDP Identifier: %s:0%s", inet_ntoa(rtr_id),
VTY_NEWLINE); if (json) {
vty_out(vty, "Discovery Sources:%s", VTY_NEWLINE); json_object_string_add(json, "id", inet_ntoa(rtr_id));
vty_out(vty, " Interfaces:%s", VTY_NEWLINE); } else {
vty_out(vty, "%s", ifaces_buffer); vty_out(vty, "Local LDP Identifier: %s:0%s", inet_ntoa(rtr_id),
vty_out(vty, " Targeted Hellos:%s", VTY_NEWLINE); VTY_NEWLINE);
vty_out(vty, "%s", tnbrs_buffer); vty_out(vty, "Discovery Sources:%s", VTY_NEWLINE);
vty_out(vty, "%s", VTY_NEWLINE); vty_out(vty, " Interfaces:%s", VTY_NEWLINE);
return (1); vty_out(vty, "%s", ifaces_buffer);
vty_out(vty, " Targeted Hellos:%s", VTY_NEWLINE);
vty_out(vty, "%s", tnbrs_buffer);
vty_out(vty, "%s", VTY_NEWLINE);
}
return (1);
default: default:
break; break;
} }
@ -193,75 +282,131 @@ show_discovery_msg(struct vty *vty, struct imsg *imsg,
} }
static void static void
show_nbr_adj(struct vty *vty, char *buffer, struct ctl_adj *adj) show_nbr_adj(struct vty *vty, char *buffer, struct ctl_adj *adj, json_object *json_nbr)
{ {
size_t buflen = strlen(buffer); size_t buflen = strlen(buffer);
json_object *json_adj_link = NULL;
json_object *json_adj_targeted = NULL;
switch (adj->type) { switch (adj->type) {
case HELLO_LINK: case HELLO_LINK:
snprintf(buffer + buflen, LDPBUFSIZ - buflen, if (json_nbr) {
" Interface: %s%s", adj->ifname, VTY_NEWLINE); json_object_object_get_ex(json_nbr, "adjacencyLink", &json_adj_link);
if (!json_adj_link) {
json_adj_link = json_object_new_array();
json_object_object_add(json_nbr, "adjacencyLink", json_adj_link);
}
json_object_array_add(json_adj_link, json_object_new_string(adj->ifname));
} else {
snprintf(buffer + buflen, LDPBUFSIZ - buflen,
" Interface: %s%s", adj->ifname, VTY_NEWLINE);
}
break; break;
case HELLO_TARGETED: case HELLO_TARGETED:
snprintf(buffer + buflen, LDPBUFSIZ - buflen, if (json_nbr) {
" Targeted Hello: %s%s", log_addr(adj->af, json_object_object_get_ex(json_nbr, "adjacencyTargeted", &json_adj_targeted);
&adj->src_addr), VTY_NEWLINE);
if (!json_adj_targeted) {
json_adj_targeted = json_object_new_array();
json_object_object_add(json_nbr, "adjacencyTargeted", json_adj_targeted);
}
json_object_array_add(json_adj_targeted,
json_object_new_string(log_addr(adj->af, &adj->src_addr)));
} else {
snprintf(buffer + buflen, LDPBUFSIZ - buflen,
" Targeted Hello: %s%s", log_addr(adj->af,
&adj->src_addr), VTY_NEWLINE);
}
break; break;
} }
} }
static int static int
show_nbr_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter) show_nbr_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter,
json_object *json, struct in_addr *nbr_id)
{ {
struct ctl_adj *adj; struct ctl_adj *adj;
struct ctl_nbr *nbr; struct ctl_nbr *nbr;
static char v4adjs_buffer[LDPBUFSIZ]; static char v4adjs_buffer[LDPBUFSIZ];
static char v6adjs_buffer[LDPBUFSIZ]; static char v6adjs_buffer[LDPBUFSIZ];
json_object *json_nbr = NULL;
switch (imsg->hdr.type) { switch (imsg->hdr.type) {
case IMSG_CTL_SHOW_NBR: case IMSG_CTL_SHOW_NBR:
nbr = imsg->data; nbr = imsg->data;
v4adjs_buffer[0] = '\0'; if (json) {
v6adjs_buffer[0] = '\0'; /* Remember the nbr->id so when we are in the other
vty_out(vty, "Peer LDP Identifier: %s:0%s", inet_ntoa(nbr->id), * case statements below we know what nbr we are
VTY_NEWLINE); * dealing with
vty_out(vty, " TCP connection: %s:%u - %s:%u%s", */
log_addr(nbr->af, &nbr->laddr), ntohs(nbr->lport), *nbr_id = nbr->id;
log_addr(nbr->af, &nbr->raddr), ntohs(nbr->rport), json_nbr = json_object_new_object();
VTY_NEWLINE); json_object_string_add(json_nbr, "peerId", inet_ntoa(nbr->id));
vty_out(vty, " Session Holdtime: %u sec%s", nbr->holdtime, json_object_string_add(json_nbr, "tcpLocalAddress", log_addr(nbr->af, &nbr->laddr));
VTY_NEWLINE); json_object_int_add(json_nbr, "tcpLocalPort", ntohs(nbr->lport));
vty_out(vty, " State: %s; Downstream-Unsolicited%s", json_object_string_add(json_nbr, "tcpRemoteAddress", log_addr(nbr->af, &nbr->raddr));
nbr_state_name(nbr->nbr_state), VTY_NEWLINE); json_object_int_add(json_nbr, "tcpRemotePort", ntohs(nbr->rport));
vty_out(vty, " Up time: %s%s", log_time(nbr->uptime), json_object_int_add(json_nbr, "holdtime", nbr->holdtime);
VTY_NEWLINE); json_object_string_add(json_nbr, "state", nbr_state_name(nbr->nbr_state));
json_object_boolean_true_add(json_nbr, "downstreamUnsolicited");
json_object_string_add(json_nbr, "upTime", log_time(nbr->uptime));
json_object_object_add(json, inet_ntoa(nbr->id), json_nbr);
} else {
v4adjs_buffer[0] = '\0';
v6adjs_buffer[0] = '\0';
vty_out(vty, "Peer LDP Identifier: %s:0%s", inet_ntoa(nbr->id),
VTY_NEWLINE);
vty_out(vty, " TCP connection: %s:%u - %s:%u%s",
log_addr(nbr->af, &nbr->laddr), ntohs(nbr->lport),
log_addr(nbr->af, &nbr->raddr), ntohs(nbr->rport),
VTY_NEWLINE);
vty_out(vty, " Session Holdtime: %u sec%s", nbr->holdtime,
VTY_NEWLINE);
vty_out(vty, " State: %s; Downstream-Unsolicited%s",
nbr_state_name(nbr->nbr_state), VTY_NEWLINE);
vty_out(vty, " Up time: %s%s", log_time(nbr->uptime),
VTY_NEWLINE);
}
break; break;
case IMSG_CTL_SHOW_NBR_DISC: case IMSG_CTL_SHOW_NBR_DISC:
adj = imsg->data; adj = imsg->data;
/* get the json_nbr by looking for the nbr_id key */
if (json)
json_object_object_get_ex(json, inet_ntoa(*nbr_id), &json_nbr);
switch (adj->af) { switch (adj->af) {
case AF_INET: case AF_INET:
show_nbr_adj(vty, v4adjs_buffer, adj); show_nbr_adj(vty, v4adjs_buffer, adj, json_nbr);
break; break;
case AF_INET6: case AF_INET6:
show_nbr_adj(vty, v6adjs_buffer, adj); show_nbr_adj(vty, v6adjs_buffer, adj, json_nbr);
break; break;
default: default:
fatalx("show_nbr_msg: unknown af"); fatalx("show_nbr_msg: unknown af");
} }
break; break;
case IMSG_CTL_SHOW_NBR_END: case IMSG_CTL_SHOW_NBR_END:
vty_out(vty, " LDP Discovery Sources:%s", VTY_NEWLINE); /* For the json scenario the adjacencies were already added
if (v4adjs_buffer[0] != '\0') { * in show_nbr_adj
vty_out(vty, " IPv4:%s", VTY_NEWLINE); */
vty_out(vty, "%s", v4adjs_buffer); if (!json) {
} vty_out(vty, " LDP Discovery Sources:%s", VTY_NEWLINE);
if (v6adjs_buffer[0] != '\0') { if (v4adjs_buffer[0] != '\0') {
vty_out(vty, " IPv6:%s", VTY_NEWLINE); vty_out(vty, " IPv4:%s", VTY_NEWLINE);
vty_out(vty, "%s", v6adjs_buffer); vty_out(vty, "%s", v4adjs_buffer);
} }
vty_out(vty, "%s", VTY_NEWLINE); if (v6adjs_buffer[0] != '\0') {
vty_out(vty, " IPv6:%s", VTY_NEWLINE);
vty_out(vty, "%s", v6adjs_buffer);
}
vty_out(vty, "%s", VTY_NEWLINE);
}
break; break;
case IMSG_CTL_END: case IMSG_CTL_END:
return (1); return (1);
@ -273,10 +418,13 @@ show_nbr_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter)
} }
static int static int
show_lib_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter) show_lib_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter, json_object *json)
{ {
struct ctl_rt *rt; struct ctl_rt *rt;
char dstnet[BUFSIZ]; char dstnet[BUFSIZ];
json_object *json_binding = NULL;
json_object *json_remote_label = NULL;
json_object *json_remote_labels = NULL;
switch (imsg->hdr.type) { switch (imsg->hdr.type) {
case IMSG_CTL_SHOW_LIB: case IMSG_CTL_SHOW_LIB:
@ -286,27 +434,50 @@ show_lib_msg(struct vty *vty, struct imsg *imsg, struct show_filter *filter)
break; break;
snprintf(dstnet, sizeof(dstnet), "%s/%d", snprintf(dstnet, sizeof(dstnet), "%s/%d",
log_addr(rt->af, &rt->prefix), rt->prefixlen); log_addr(rt->af, &rt->prefix), rt->prefixlen);
if (rt->first) { if (json) {
vty_out(vty, "%s%s", dstnet, VTY_NEWLINE); if (rt->first) {
vty_out(vty, "%-8sLocal binding: label: %s%s", "", json_binding = json_object_new_object();
log_label(rt->local_label), VTY_NEWLINE); json_object_string_add(json_binding, "localLabel", log_label(rt->local_label));
if (rt->remote_label != NO_LABEL) { json_remote_labels = json_object_new_array();
vty_out(vty, "%-8sRemote bindings:%s", "", json_object_object_add(json_binding, "remoteLabel", json_remote_labels);
VTY_NEWLINE);
vty_out(vty, "%-12sPeer Label%s", json_object_object_add(json, dstnet, json_binding);
"", VTY_NEWLINE); } else {
vty_out(vty, "%-12s----------------- " json_object_object_get_ex(json, dstnet, &json_binding);
"---------%s", "", VTY_NEWLINE); }
} else
vty_out(vty, "%-8sNo remote bindings%s", "", if (rt->remote_label != NO_LABEL) {
VTY_NEWLINE); json_object_object_get_ex(json_binding, "remoteLabel", &json_remote_labels);
} json_remote_label = json_object_new_object();
if (rt->remote_label != NO_LABEL) json_object_string_add(json_remote_label, "nexthop", inet_ntoa(rt->nexthop));
vty_out(vty, "%12s%-20s%s%s", "", inet_ntoa(rt->nexthop), json_object_string_add(json_remote_label, "label", log_label(rt->remote_label));
log_label(rt->remote_label), VTY_NEWLINE); json_object_array_add(json_remote_labels, json_remote_label);
}
} else {
if (rt->first) {
vty_out(vty, "%s%s", dstnet, VTY_NEWLINE);
vty_out(vty, "%-8sLocal binding: label: %s%s", "",
log_label(rt->local_label), VTY_NEWLINE);
if (rt->remote_label != NO_LABEL) {
vty_out(vty, "%-8sRemote bindings:%s", "",
VTY_NEWLINE);
vty_out(vty, "%-12sPeer Label%s",
"", VTY_NEWLINE);
vty_out(vty, "%-12s----------------- "
"---------%s", "", VTY_NEWLINE);
} else
vty_out(vty, "%-8sNo remote bindings%s", "",
VTY_NEWLINE);
}
if (rt->remote_label != NO_LABEL)
vty_out(vty, "%12s%-20s%s%s", "", inet_ntoa(rt->nexthop),
log_label(rt->remote_label), VTY_NEWLINE);
}
break; break;
case IMSG_CTL_END: case IMSG_CTL_END:
vty_out(vty, "%s", VTY_NEWLINE); vty_out(vty, "%s", VTY_NEWLINE);
@ -419,10 +590,12 @@ ldp_vty_connect(struct imsgbuf *ibuf)
static int static int
ldp_vty_dispatch(struct vty *vty, struct imsgbuf *ibuf, enum show_command cmd, ldp_vty_dispatch(struct vty *vty, struct imsgbuf *ibuf, enum show_command cmd,
struct show_filter *filter) struct show_filter *filter, u_char uj)
{ {
struct imsg imsg; struct imsg imsg;
int n, done = 0; int n, done = 0;
struct in_addr nbr_id;
json_object *json = NULL;
while (ibuf->w.queued) while (ibuf->w.queued)
if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) { if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) {
@ -431,15 +604,30 @@ ldp_vty_dispatch(struct vty *vty, struct imsgbuf *ibuf, enum show_command cmd,
return (CMD_WARNING); return (CMD_WARNING);
} }
if (uj)
json = json_object_new_object();
while (!done) { while (!done) {
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) { if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) {
log_warnx("imsg_read error"); log_warnx("imsg_read error");
close(ibuf->fd); close(ibuf->fd);
if (json) {
vty_out (vty, "%s%s",
json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY),
VTY_NEWLINE);
json_object_free(json);
}
return (CMD_WARNING); return (CMD_WARNING);
} }
if (n == 0) { if (n == 0) {
log_warnx("pipe closed"); log_warnx("pipe closed");
close(ibuf->fd); close(ibuf->fd);
if (json) {
vty_out (vty, "%s%s",
json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY),
VTY_NEWLINE);
json_object_free(json);
}
return (CMD_WARNING); return (CMD_WARNING);
} }
@ -447,22 +635,28 @@ ldp_vty_dispatch(struct vty *vty, struct imsgbuf *ibuf, enum show_command cmd,
if ((n = imsg_get(ibuf, &imsg)) == -1) { if ((n = imsg_get(ibuf, &imsg)) == -1) {
log_warnx("imsg_get error"); log_warnx("imsg_get error");
close(ibuf->fd); close(ibuf->fd);
if (json) {
vty_out (vty, "%s%s",
json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY),
VTY_NEWLINE);
json_object_free(json);
}
return (CMD_WARNING); return (CMD_WARNING);
} }
if (n == 0) if (n == 0)
break; break;
switch (cmd) { switch (cmd) {
case SHOW_IFACE: case SHOW_IFACE:
done = show_interface_msg(vty, &imsg, filter); done = show_interface_msg(vty, &imsg, filter, json);
break; break;
case SHOW_DISC: case SHOW_DISC:
done = show_discovery_msg(vty, &imsg, filter); done = show_discovery_msg(vty, &imsg, filter, json);
break; break;
case SHOW_NBR: case SHOW_NBR:
done = show_nbr_msg(vty, &imsg, filter); done = show_nbr_msg(vty, &imsg, filter, json, &nbr_id);
break; break;
case SHOW_LIB: case SHOW_LIB:
done = show_lib_msg(vty, &imsg, filter); done = show_lib_msg(vty, &imsg, filter, json);
break; break;
case SHOW_L2VPN_PW: case SHOW_L2VPN_PW:
done = show_l2vpn_pw_msg(vty, &imsg); done = show_l2vpn_pw_msg(vty, &imsg);
@ -479,6 +673,13 @@ ldp_vty_dispatch(struct vty *vty, struct imsgbuf *ibuf, enum show_command cmd,
close(ibuf->fd); close(ibuf->fd);
if (json) {
vty_out (vty, "%s%s",
json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY),
VTY_NEWLINE);
json_object_free(json);
}
return (CMD_SUCCESS); return (CMD_SUCCESS);
} }
@ -500,7 +701,7 @@ ldp_vty_get_af(const char *str, int *af)
} }
int int
ldp_vty_show_binding(struct vty *vty, struct vty_arg *args[]) ldp_vty_show_binding(struct vty *vty, struct vty_arg *args[], u_char uj)
{ {
struct imsgbuf ibuf; struct imsgbuf ibuf;
struct show_filter filter; struct show_filter filter;
@ -519,11 +720,11 @@ ldp_vty_show_binding(struct vty *vty, struct vty_arg *args[])
memset(&filter, 0, sizeof(filter)); memset(&filter, 0, sizeof(filter));
filter.family = af; filter.family = af;
return (ldp_vty_dispatch(vty, &ibuf, SHOW_LIB, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_LIB, &filter, uj));
} }
int int
ldp_vty_show_discovery(struct vty *vty, struct vty_arg *args[]) ldp_vty_show_discovery(struct vty *vty, struct vty_arg *args[], u_char uj)
{ {
struct imsgbuf ibuf; struct imsgbuf ibuf;
struct show_filter filter; struct show_filter filter;
@ -542,11 +743,11 @@ ldp_vty_show_discovery(struct vty *vty, struct vty_arg *args[])
memset(&filter, 0, sizeof(filter)); memset(&filter, 0, sizeof(filter));
filter.family = af; filter.family = af;
return (ldp_vty_dispatch(vty, &ibuf, SHOW_DISC, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_DISC, &filter, uj));
} }
int int
ldp_vty_show_interface(struct vty *vty, struct vty_arg *args[]) ldp_vty_show_interface(struct vty *vty, struct vty_arg *args[], u_char uj)
{ {
struct imsgbuf ibuf; struct imsgbuf ibuf;
struct show_filter filter; struct show_filter filter;
@ -568,14 +769,16 @@ ldp_vty_show_interface(struct vty *vty, struct vty_arg *args[])
filter.family = af; filter.family = af;
/* header */ /* header */
vty_out(vty, "%-4s %-11s %-6s %-8s %-12s %3s%s", "AF", if (!uj) {
"Interface", "State", "Uptime", "Hello Timers", "ac", VTY_NEWLINE); vty_out(vty, "%-4s %-11s %-6s %-8s %-12s %3s%s", "AF",
"Interface", "State", "Uptime", "Hello Timers", "ac", VTY_NEWLINE);
}
return (ldp_vty_dispatch(vty, &ibuf, SHOW_IFACE, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_IFACE, &filter, uj));
} }
int int
ldp_vty_show_neighbor(struct vty *vty, struct vty_arg *args[]) ldp_vty_show_neighbor(struct vty *vty, struct vty_arg *args[], u_char uj)
{ {
struct imsgbuf ibuf; struct imsgbuf ibuf;
struct show_filter filter; struct show_filter filter;
@ -588,7 +791,7 @@ ldp_vty_show_neighbor(struct vty *vty, struct vty_arg *args[])
/* not used */ /* not used */
memset(&filter, 0, sizeof(filter)); memset(&filter, 0, sizeof(filter));
return (ldp_vty_dispatch(vty, &ibuf, SHOW_NBR, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_NBR, &filter, uj));
} }
int int
@ -605,7 +808,7 @@ ldp_vty_show_atom_binding(struct vty *vty, struct vty_arg *args[])
/* not used */ /* not used */
memset(&filter, 0, sizeof(filter)); memset(&filter, 0, sizeof(filter));
return (ldp_vty_dispatch(vty, &ibuf, SHOW_L2VPN_BINDING, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_L2VPN_BINDING, &filter, 0));
} }
int int
@ -629,7 +832,7 @@ ldp_vty_show_atom_vc(struct vty *vty, struct vty_arg *args[])
"---------", "---------------", "----------", "---------", "---------------", "----------",
"----------------", "----------", VTY_NEWLINE); "----------------", "----------", VTY_NEWLINE);
return (ldp_vty_dispatch(vty, &ibuf, SHOW_L2VPN_PW, &filter)); return (ldp_vty_dispatch(vty, &ibuf, SHOW_L2VPN_PW, &filter, 0));
} }
int int