Merge pull request #9700 from mjstapp/add_json_det_attrs

bgpd: Add 'show bgp <afi> <safi> json detail' header data
This commit is contained in:
Donatas Abraitis 2021-11-10 16:42:30 +02:00 committed by GitHub
commit 37b6787730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 10 deletions

View File

@ -65,7 +65,7 @@ static inline int bgp_is_withdraw_label(mpls_label_t *label)
return 0;
}
static inline int bgp_is_valid_label(mpls_label_t *label)
static inline int bgp_is_valid_label(const mpls_label_t *label)
{
uint8_t *t = (uint8_t *)label;
if (!t)
@ -99,7 +99,7 @@ static inline void bgp_unregister_for_label(struct bgp_dest *dest)
}
/* Label stream to value */
static inline uint32_t label_pton(mpls_label_t *label)
static inline uint32_t label_pton(const mpls_label_t *label)
{
uint8_t *t = (uint8_t *)label;
return ((((unsigned int)t[0]) << 12) | (((unsigned int)t[1]) << 4)

View File

@ -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[];
@ -10712,14 +10716,15 @@ static int bgp_show_community(struct vty *vty, struct bgp *bgp,
static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
struct bgp_table *table, enum bgp_show_type type,
void *output_arg, char *rd, int is_last,
void *output_arg, const char *rd, int is_last,
unsigned long *output_cum, unsigned long *total_cum,
unsigned long *json_header_depth, uint16_t show_flags,
enum rpki_states rpki_target_state)
{
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;
@ -10731,7 +10736,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)
@ -10761,10 +10766,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)
@ -11016,8 +11030,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,
@ -11176,6 +11210,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)
@ -11260,7 +11295,8 @@ static void bgp_show_all_instances_routes_vty(struct vty *vty, afi_t afi,
/* Header of detailed BGP route information */
void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
struct bgp_dest *dest, struct prefix_rd *prd,
struct bgp_dest *dest,
const struct prefix_rd *prd,
afi_t afi, safi_t safi, json_object *json)
{
struct bgp_path_info *pi;
@ -11470,7 +11506,7 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
}
}
static void bgp_show_path_info(struct prefix_rd *pfx_rd,
static void bgp_show_path_info(const struct prefix_rd *pfx_rd,
struct bgp_dest *bgp_node, struct vty *vty,
struct bgp *bgp, afi_t afi, safi_t safi,
json_object *json, enum bgp_path_type pathtype,
@ -11530,6 +11566,23 @@ static void bgp_show_path_info(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,

View File

@ -784,7 +784,7 @@ extern bool bgp_zebra_has_route_changed(struct bgp_path_info *selected);
extern void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
struct bgp_dest *dest,
struct prefix_rd *prd, afi_t afi,
const struct prefix_rd *prd, afi_t afi,
safi_t safi, json_object *json);
extern void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
struct bgp_dest *bn,

View File

@ -122,7 +122,7 @@ class BgpRib:
luResult(target, True, title, logstr)
def RequireUnicastRoutes(self, target, afi, vrf, title, wantroutes, debug=0):
logstr = "RequireVpnRoutes %s" % str(wantroutes)
logstr = "RequireUnicastRoutes %s" % str(wantroutes)
vrfstr = ""
if vrf != "":
vrfstr = "vrf %s" % (vrf)