From 2aad8c4289b1b8e6b87faea57a247e710dd966ed Mon Sep 17 00:00:00 2001 From: Mark Stapp Date: Wed, 29 Sep 2021 17:09:51 -0400 Subject: [PATCH] bgpd: add 'header' info to show bgp afi safi json detail The '... json detail' output is missing some data that's shown via the 'route_vty_out_detail_header' function. Integrate the json version of that function in the 'json detail' path. Signed-off-by: Mark Stapp --- bgpd/bgp_route.c | 58 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index abf480e806..3bd73cd178 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -102,6 +102,10 @@ DEFINE_HOOK(bgp_rpki_prefix_status, const struct prefix *prefix), (peer, attr, prefix)); +/* Render dest to prefix_rd based on safi */ +static const struct prefix_rd *bgp_rd_from_dest(const struct bgp_dest *dest, + safi_t safi); + /* Extern from bgp_dump.c */ extern const char *bgp_origin_str[]; extern const char *bgp_origin_long_str[]; @@ -10696,7 +10700,8 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi, { struct bgp_path_info *pi; struct bgp_dest *dest; - int header = 1; + bool header = true; + bool json_detail_header = false; int display; unsigned long output_count = 0; unsigned long total_count = 0; @@ -10708,7 +10713,7 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi, bool all = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL); if (output_cum && *output_cum != 0) - header = 0; + header = false; if (use_json && !*json_header_depth) { if (all) @@ -10738,10 +10743,19 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi, vty_out(vty, " \"%s\" : { ", rd); } + /* Check for 'json detail', where we need header output once per dest */ + if (use_json && CHECK_FLAG(show_flags, BGP_SHOW_OPT_DETAIL) && + type != bgp_show_type_dampend_paths && + type != bgp_show_type_damp_neighbor && + type != bgp_show_type_flap_statistics && + type != bgp_show_type_flap_neighbor) + json_detail_header = true; + /* Start processing of routes. */ for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) { const struct prefix *dest_p = bgp_dest_get_prefix(dest); enum rpki_states rpki_curr_state = RPKI_NOT_BEING_USED; + bool json_detail = json_detail_header; pi = bgp_dest_get_bgp_path_info(dest); if (pi == NULL) @@ -10993,8 +11007,28 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi, else vty_out(vty, (wide ? BGP_SHOW_HEADER_WIDE : BGP_SHOW_HEADER)); - header = 0; + header = false; + + } else if (json_detail && json_paths != NULL) { + const struct prefix_rd *prd; + json_object *jtemp; + + /* Use common detail header, for most types; + * need a json 'object'. + */ + + jtemp = json_object_new_object(); + prd = bgp_rd_from_dest(dest, safi); + + route_vty_out_detail_header( + vty, bgp, dest, prd, table->afi, + safi, jtemp); + + json_object_array_add(json_paths, jtemp); + + json_detail = false; } + if (rd != NULL && !display && !output_count) { if (!use_json) vty_out(vty, @@ -11153,6 +11187,7 @@ int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi, } return CMD_SUCCESS; } + static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi, enum bgp_show_type type, void *output_arg, uint16_t show_flags, enum rpki_states rpki_target_state) @@ -11508,6 +11543,23 @@ static void bgp_show_path_info(const struct prefix_rd *pfx_rd, } } +/* + * Return rd based on safi + */ +static const struct prefix_rd *bgp_rd_from_dest(const struct bgp_dest *dest, + safi_t safi) +{ + switch (safi) { + case SAFI_MPLS_VPN: + case SAFI_ENCAP: + case SAFI_EVPN: + return (struct prefix_rd *)(bgp_dest_get_prefix(dest)); + default: + return NULL; + + } +} + /* Display specified route of BGP table. */ static int bgp_show_route_in_table(struct vty *vty, struct bgp *bgp, struct bgp_table *rib, const char *ip_str, -- 2.39.5