zebra: improve the output of "show mpls table"

Use the table generator to adjust the columns width according to
the table's content.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
This commit is contained in:
Renato Westphal 2019-06-12 21:43:34 -03:00
parent c50addb34a
commit a971aeb68e
6 changed files with 74 additions and 64 deletions

View File

@ -1,8 +1,8 @@
Inbound Outbound
Label Type Nexthop Label
-------- ------- --------------- --------
XX LDP 10.0.1.2 XX
XX LDP 10.0.1.2 XX
XX LDP 10.0.1.2 implicit-null
XX LDP 10.0.1.2 implicit-null
XX LDP 10.0.1.2 implicit-null
Inbound Label Type Nexthop Outbound Label
-----------------------------------------------
XX LDP 10.0.1.2 XX
XX LDP 10.0.1.2 XX
XX LDP 10.0.1.2 implicit-null
XX LDP 10.0.1.2 implicit-null
XX LDP 10.0.1.2 implicit-null

View File

@ -1,7 +1,7 @@
Inbound Outbound
Label Type Nexthop Label
-------- ------- --------------- --------
XX LDP 10.0.1.1 implicit-null
XX LDP 10.0.2.3 implicit-null
XX LDP 10.0.2.4 implicit-null
XX LDP 10.0.3.3 implicit-null
Inbound Label Type Nexthop Outbound Label
-----------------------------------------------
XX LDP 10.0.1.1 implicit-null
XX LDP 10.0.2.3 implicit-null
XX LDP 10.0.2.4 implicit-null
XX LDP 10.0.3.3 implicit-null

View File

@ -1,10 +1,10 @@
Inbound Outbound
Label Type Nexthop Label
-------- ------- --------------- --------
XX LDP 10.0.2.2 XX
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.4 implicit-null
XX LDP 10.0.3.2 XX
XX LDP 10.0.3.2 implicit-null
XX LDP 10.0.3.2 implicit-null
Inbound Label Type Nexthop Outbound Label
-----------------------------------------------
XX LDP 10.0.2.2 XX
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.4 implicit-null
XX LDP 10.0.3.2 XX
XX LDP 10.0.3.2 implicit-null
XX LDP 10.0.3.2 implicit-null

View File

@ -1,9 +1,9 @@
Inbound Outbound
Label Type Nexthop Label
-------- ------- --------------- --------
XX LDP 10.0.2.2 XX
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.3 implicit-null
XX LDP 10.0.2.3 implicit-null
Inbound Label Type Nexthop Outbound Label
-----------------------------------------------
XX LDP 10.0.2.2 XX
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.2 implicit-null
XX LDP 10.0.2.3 implicit-null
XX LDP 10.0.2.3 implicit-null

View File

@ -618,12 +618,12 @@ def test_mpls_table():
refTableFile = '%s/r%s/show_mpls_table.ref%s' % (thisDir, i, version)
if os.path.isfile(refTableFile):
# Read expected result from file
expected = open(refTableFile).read().rstrip()
expected = open(refTableFile).read()
# Fix newlines (make them all the same)
expected = ('\n'.join(expected.splitlines()) + '\n').splitlines(1)
# Actual output from router
actual = net['r%s' % i].cmd('vtysh -c "show mpls table" 2> /dev/null').rstrip()
actual = net['r%s' % i].cmd('vtysh -c "show mpls table" 2> /dev/null')
# Fix inconsistent Label numbers at beginning of line
actual = re.sub(r"(\s+)[0-9]+(\s+LDP)", r"\1XX\2", actual)

View File

@ -34,6 +34,7 @@
#include "routemap.h"
#include "stream.h"
#include "nexthop.h"
#include "termtable.h"
#include "lib/json.h"
#include "zebra/rib.h"
@ -3047,7 +3048,6 @@ void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
json_object *json = NULL;
zebra_lsp_t *lsp = NULL;
zebra_nhlfe_t *nhlfe = NULL;
struct nexthop *nexthop = NULL;
struct listnode *node = NULL;
struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
@ -3063,15 +3063,23 @@ void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
json, JSON_C_TO_STRING_PRETTY));
json_object_free(json);
} else {
vty_out(vty, " Inbound Outbound\n");
vty_out(vty, " Label Type Nexthop Label\n");
vty_out(vty, "-------- ------- --------------- --------\n");
struct ttable *tt;
/* Prepare table. */
tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
ttable_add_row(tt, "Inbound Label|Type|Nexthop|Outbound Label");
tt->style.cell.rpad = 2;
tt->style.corner = '+';
ttable_restyle(tt);
ttable_rowseps(tt, 0, BOTTOM, true, '-');
for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
for (nhlfe = lsp->nhlfe_list; nhlfe;
nhlfe = nhlfe->next) {
vty_out(vty, "%8d %7s ", lsp->ile.in_label,
nhlfe_type2str(nhlfe->type));
struct nexthop *nexthop;
const char *out_label_str;
char nh_buf[NEXTHOP_STRLEN];
nexthop = nhlfe->nexthop;
switch (nexthop->type) {
@ -3081,45 +3089,47 @@ void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(
zns,
nexthop->ifindex);
if (ifp)
vty_out(vty, "%15s", ifp->name);
else
vty_out(vty, "%15s", "Null");
zns, nexthop->ifindex);
snprintf(nh_buf, sizeof(nh_buf), "%s",
ifp ? ifp->name : "Null");
break;
}
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
vty_out(vty, "%15s",
inet_ntoa(nexthop->gate.ipv4));
inet_ntop(AF_INET, &nexthop->gate.ipv4,
nh_buf, sizeof(nh_buf));
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
vty_out(vty, "%15s",
inet_ntop(AF_INET6,
&nexthop->gate.ipv6,
buf, BUFSIZ));
inet_ntop(AF_INET6, &nexthop->gate.ipv6,
nh_buf, sizeof(nh_buf));
break;
default:
break;
}
if (nexthop->type != NEXTHOP_TYPE_IFINDEX)
vty_out(vty, " %8s\n",
mpls_label2str(
nexthop->nh_label
->num_labels,
&nexthop->nh_label
->label[0],
buf, BUFSIZ, 1));
out_label_str = mpls_label2str(
nexthop->nh_label->num_labels,
&nexthop->nh_label->label[0],
buf, BUFSIZ, 1);
else
vty_out(vty, "\n");
out_label_str = "-";
ttable_add_row(tt, "%u|%s|%s|%s",
lsp->ile.in_label,
nhlfe_type2str(nhlfe->type),
nh_buf, out_label_str);
}
}
vty_out(vty, "\n");
/* Dump the generated table. */
if (tt->nrows > 1) {
char *table = ttable_dump(tt, "\n");
vty_out(vty, "%s\n", table);
XFREE(MTYPE_TMP, table);
}
ttable_del(tt);
}
list_delete(&lsp_list);