diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index b6d0b26125..04581be19b 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -61,12 +61,19 @@ extern int allow_delete; +/* context to manage dumps in multiple tables or vrfs */ +struct route_show_ctx { + bool multi; /* dump multiple tables or vrf */ + bool header_done; /* common header already displayed */ +}; + static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, safi_t safi, bool use_fib, bool use_json, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id, uint32_t tableid); + unsigned short ospf_instance_id, uint32_t tableid, + struct route_show_ctx *ctx); static void vty_show_ip_route_detail(struct vty *vty, struct route_node *rn, int mcast, bool use_fib, bool show_ng); static void vty_show_ip_route_summary(struct vty *vty, @@ -140,8 +147,12 @@ DEFUN (show_ip_rpf, JSON_STR) { bool uj = use_json(argc, argv); + struct route_show_ctx ctx = { + .multi = false, + }; + return do_show_ip_route(vty, VRF_DEFAULT_NAME, AFI_IP, SAFI_MULTICAST, - false, uj, 0, NULL, false, 0, 0, 0); + false, uj, 0, NULL, false, 0, 0, 0, &ctx); } DEFUN (show_ip_rpf_addr, @@ -1038,7 +1049,7 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, const struct prefix *longer_prefix_p, bool supernets_only, int type, unsigned short ospf_instance_id, bool use_json, - uint32_t tableid) + uint32_t tableid, struct route_show_ctx *ctx) { struct route_node *rn; struct route_entry *re; @@ -1049,6 +1060,17 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, uint32_t addr; char buf[BUFSIZ]; + /* + * ctx->multi indicates if we are dumping multiple tables or vrfs. + * if set: + * => display the common header at most once + * => add newline at each call except first + * => always display the VRF and table + * else: + * => display the common header if at least one entry is found + * => display the VRF and table if specific + */ + if (use_json) json = json_object_new_object(); @@ -1092,23 +1114,30 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, if (use_json) { if (!json_prefix) json_prefix = json_object_new_array(); - } else { - if (first) { + } else if (first) { + if (!ctx->header_done) { if (afi == AFI_IP) vty_out(vty, SHOW_ROUTE_V4_HEADER); else vty_out(vty, SHOW_ROUTE_V6_HEADER); - - if (tableid && tableid != RT_TABLE_MAIN) - vty_out(vty, "\nVRF %s table %u:\n", - zvrf_name(zvrf), tableid); - else if (zvrf_id(zvrf) != VRF_DEFAULT) - vty_out(vty, "\nVRF %s:\n", - zvrf_name(zvrf)); - first = 0; } + if (ctx->multi && ctx->header_done) + vty_out(vty, "\n"); + if (ctx->multi || zvrf_id(zvrf) != VRF_DEFAULT + || tableid) { + if (!tableid) + vty_out(vty, "VRF %s:\n", + zvrf_name(zvrf)); + else + vty_out(vty, + "VRF %s table %u:\n", + zvrf_name(zvrf), + tableid); + } + ctx->header_done = true; + first = 0; } vty_show_ip_route(vty, rn, re, json_prefix, use_fib); @@ -1133,7 +1162,8 @@ static void do_show_ip_route_all(struct vty *vty, struct zebra_vrf *zvrf, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id) + unsigned short ospf_instance_id, + struct route_show_ctx *ctx) { struct zebra_router_table *zrt; struct rib_table_info *info; @@ -1148,11 +1178,10 @@ static void do_show_ip_route_all(struct vty *vty, struct zebra_vrf *zvrf, zrt->safi != SAFI_UNICAST) continue; - do_show_ip_route(vty, zvrf_name(zvrf), afi, - SAFI_UNICAST, use_fib, use_json, - tag, longer_prefix_p, - supernets_only, type, - ospf_instance_id, zrt->tableid); + do_show_ip_route(vty, zvrf_name(zvrf), afi, SAFI_UNICAST, + use_fib, use_json, tag, longer_prefix_p, + supernets_only, type, ospf_instance_id, + zrt->tableid, ctx); } } @@ -1161,7 +1190,8 @@ static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id, uint32_t tableid) + unsigned short ospf_instance_id, uint32_t tableid, + struct route_show_ctx *ctx) { struct route_table *table; struct zebra_vrf *zvrf = NULL; @@ -1194,7 +1224,7 @@ static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, do_show_route_helper(vty, zvrf, table, afi, use_fib, tag, longer_prefix_p, supernets_only, type, - ospf_instance_id, use_json, tableid); + ospf_instance_id, use_json, tableid, ctx); return CMD_SUCCESS; } @@ -1671,6 +1701,9 @@ DEFPY (show_route, struct vrf *vrf; int type = 0; struct zebra_vrf *zvrf; + struct route_show_ctx ctx = { + .multi = vrf_all || table_all, + }; if (!vrf_is_backend_netns()) { if ((vrf_all || vrf_name) && (table || table_all)) { @@ -1698,17 +1731,18 @@ DEFPY (show_route, continue; if (table_all) - do_show_ip_route_all(vty, zvrf, afi, - !!fib, !!json, - tag, prefix_str ? prefix : NULL, + do_show_ip_route_all(vty, zvrf, afi, !!fib, + !!json, tag, + prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id); + ospf_instance_id, &ctx); else do_show_ip_route(vty, zvrf_name(zvrf), afi, - SAFI_UNICAST, !!fib, !!json, tag, + SAFI_UNICAST, !!fib, !!json, + tag, prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id, table); + ospf_instance_id, table, &ctx); } } else { vrf_id_t vrf_id = VRF_DEFAULT; @@ -1722,15 +1756,16 @@ DEFPY (show_route, return CMD_SUCCESS; if (table_all) - do_show_ip_route_all(vty, zvrf, afi, - !!fib, !!json, - tag, prefix_str ? prefix : NULL, + do_show_ip_route_all(vty, zvrf, afi, !!fib, !!json, tag, + prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id); + ospf_instance_id, &ctx); else - do_show_ip_route(vty, vrf->name, afi, SAFI_UNICAST, !!fib, - !!json, tag, prefix_str ? prefix : NULL, - !!supernets_only, type, ospf_instance_id, table); + do_show_ip_route(vty, vrf->name, afi, SAFI_UNICAST, + !!fib, !!json, tag, + prefix_str ? prefix : NULL, + !!supernets_only, type, + ospf_instance_id, table, &ctx); } return CMD_SUCCESS;