bgpd: Nexthop Stringify changes

Issue: 3576989

Ticket# 3576989

Signed-off-by: Sindhu Parvathi Gopinathan's <sgopinathan@nvidia.com>

Signed-off-by: Ashwini Reddy's <ashred@nvidia.com>
This commit is contained in:
Ashwini Reddy 2023-11-07 15:29:45 -08:00 committed by Krishnasamy R
parent 4838bac033
commit c8290e97e7

View File

@ -964,9 +964,8 @@ static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
json_object_object_add(json, "nexthops", json_gates); json_object_object_add(json, "nexthops", json_gates);
} }
static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp, static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp, struct bgp_nexthop_cache *bnc,
struct bgp_nexthop_cache *bnc, bool specific, bool detail, bool uj)
json_object *json)
{ {
char buf[PREFIX2STR_BUFFER]; char buf[PREFIX2STR_BUFFER];
time_t tbuf; time_t tbuf;
@ -977,10 +976,10 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
peer = (struct peer *)bnc->nht_info; peer = (struct peer *)bnc->nht_info;
if (json) if (uj)
json_nexthop = json_object_new_object(); json_nexthop = json_object_new_object();
if (bnc->srte_color) { if (bnc->srte_color) {
if (json) if (uj)
json_object_int_add(json_nexthop, "srteColor", json_object_int_add(json_nexthop, "srteColor",
bnc->srte_color); bnc->srte_color);
else else
@ -988,7 +987,7 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
} }
inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix, buf, sizeof(buf)); inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix, buf, sizeof(buf));
if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) { if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
if (json) { if (uj) {
json_object_boolean_true_add(json_nexthop, "valid"); json_object_boolean_true_add(json_nexthop, "valid");
json_object_boolean_true_add(json_nexthop, "complete"); json_object_boolean_true_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "igpMetric", json_object_int_add(json_nexthop, "igpMetric",
@ -1016,7 +1015,7 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
} }
bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop); bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop);
} else if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE)) { } else if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE)) {
if (json) { if (uj) {
json_object_boolean_true_add(json_nexthop, "valid"); json_object_boolean_true_add(json_nexthop, "valid");
json_object_boolean_false_add(json_nexthop, "complete"); json_object_boolean_false_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "igpMetric", json_object_int_add(json_nexthop, "igpMetric",
@ -1036,7 +1035,7 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
} }
bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop); bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop);
} else { } else {
if (json) { if (uj) {
json_object_boolean_false_add(json_nexthop, "valid"); json_object_boolean_false_add(json_nexthop, "valid");
json_object_boolean_false_add(json_nexthop, "complete"); json_object_boolean_false_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "pathCount", json_object_int_add(json_nexthop, "pathCount",
@ -1068,8 +1067,8 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
} }
} }
tbuf = time(NULL) - (monotime(NULL) - bnc->last_update); tbuf = time(NULL) - (monotime(NULL) - bnc->last_update);
if (json) { if (uj) {
if (!specific) { if (detail) {
json_last_update = json_object_new_object(); json_last_update = json_object_new_object();
json_object_int_add(json_last_update, "epoch", tbuf); json_object_int_add(json_last_update, "epoch", tbuf);
json_object_string_add(json_last_update, "string", json_object_string_add(json_last_update, "string",
@ -1084,22 +1083,25 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
} }
/* show paths dependent on nexthop, if needed. */ /* show paths dependent on nexthop, if needed. */
if (specific) if (detail)
bgp_show_nexthop_paths(vty, bgp, bnc, json_nexthop); bgp_show_nexthop_paths(vty, bgp, bnc, json_nexthop);
if (json)
json_object_object_add(json, buf, json_nexthop); if (uj) {
vty_out(vty, "\"%s\":", buf);
vty_json_no_pretty(vty, json_nexthop);
}
} }
static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, bool import_table, bool uj,
bool import_table, json_object *json, afi_t afi, afi_t afi, bool detail)
bool detail)
{ {
struct bgp_nexthop_cache *bnc; struct bgp_nexthop_cache *bnc;
struct bgp_nexthop_cache_head(*tree)[AFI_MAX]; struct bgp_nexthop_cache_head(*tree)[AFI_MAX];
json_object *json_afi = NULL;
bool found = false; bool found = false;
bool firstafi = true;
bool firstnh = true;
if (!json) { if (!uj) {
if (import_table) if (import_table)
vty_out(vty, "Current BGP import check cache:\n"); vty_out(vty, "Current BGP import check cache:\n");
else else
@ -1111,34 +1113,42 @@ static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp,
tree = &bgp->nexthop_cache_table; tree = &bgp->nexthop_cache_table;
if (afi == AFI_IP || afi == AFI_IP6) { if (afi == AFI_IP || afi == AFI_IP6) {
if (json) if (uj)
json_afi = json_object_new_object(); vty_out(vty, "%s:{", (afi == AFI_IP) ? "\"ipv4\"" : "\"ipv6\"");
frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc) { frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc) {
bgp_show_nexthop(vty, bgp, bnc, detail, json_afi); if (uj)
vty_out(vty, "%s", firstnh ? "" : ",");
bgp_show_nexthop(vty, bgp, bnc, detail, uj);
found = true; found = true;
firstnh = false;
} }
if (found && json) if (found && uj)
json_object_object_add( vty_out(vty, "}");
json, (afi == AFI_IP) ? "ipv4" : "ipv6",
json_afi);
return; return;
} }
for (afi = AFI_IP; afi < AFI_MAX; afi++) { for (afi = AFI_IP; afi < AFI_MAX; afi++) {
if (json && (afi == AFI_IP || afi == AFI_IP6)) if (afi != AFI_IP && afi != AFI_IP6)
json_afi = json_object_new_object(); continue;
frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc) if (uj)
bgp_show_nexthop(vty, bgp, bnc, detail, json_afi); vty_out(vty, "%s%s:{", firstafi ? "" : ",",
if (json && (afi == AFI_IP || afi == AFI_IP6)) (afi == AFI_IP) ? "\"ipv4\"" : "\"ipv6\"");
json_object_object_add( firstafi = false;
json, (afi == AFI_IP) ? "ipv4" : "ipv6", firstnh = true;
json_afi); frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc) {
if (uj)
vty_out(vty, "%s", firstnh ? "" : ",");
bgp_show_nexthop(vty, bgp, bnc, detail, uj);
firstnh = false;
}
if (uj)
vty_out(vty, "}");
} }
} }
static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name, static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name, const char *nhopip_str,
const char *nhopip_str, bool import_table, bool import_table, bool uj, afi_t afi, bool detail)
json_object *json, afi_t afi, bool detail)
{ {
struct bgp *bgp; struct bgp *bgp;
@ -1147,7 +1157,7 @@ static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
else else
bgp = bgp_get_default(); bgp = bgp_get_default();
if (!bgp) { if (!bgp) {
if (!json) if (!uj)
vty_out(vty, "%% No such BGP instance exist\n"); vty_out(vty, "%% No such BGP instance exist\n");
return CMD_WARNING; return CMD_WARNING;
} }
@ -1157,61 +1167,57 @@ static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
struct bgp_nexthop_cache_head (*tree)[AFI_MAX]; struct bgp_nexthop_cache_head (*tree)[AFI_MAX];
struct bgp_nexthop_cache *bnc; struct bgp_nexthop_cache *bnc;
bool found = false; bool found = false;
json_object *json_afi = NULL;
if (!str2prefix(nhopip_str, &nhop)) { if (!str2prefix(nhopip_str, &nhop)) {
if (!json) if (!uj)
vty_out(vty, "nexthop address is malformed\n"); vty_out(vty, "nexthop address is malformed\n");
return CMD_WARNING; return CMD_WARNING;
} }
tree = import_table ? &bgp->import_check_table tree = import_table ? &bgp->import_check_table
: &bgp->nexthop_cache_table; : &bgp->nexthop_cache_table;
if (json) if (uj)
json_afi = json_object_new_object(); vty_out(vty, "%s:{",
(family2afi(nhop.family) == AFI_IP) ? "\"ipv4\"" : "\"ipv6\"");
frr_each (bgp_nexthop_cache, &(*tree)[family2afi(nhop.family)], frr_each (bgp_nexthop_cache, &(*tree)[family2afi(nhop.family)],
bnc) { bnc) {
if (prefix_cmp(&bnc->prefix, &nhop)) if (prefix_cmp(&bnc->prefix, &nhop))
continue; continue;
bgp_show_nexthop(vty, bgp, bnc, true, json_afi); bgp_show_nexthop(vty, bgp, bnc, true, uj);
found = true; found = true;
} }
if (json) if (!found && !uj)
json_object_object_add(
json,
(family2afi(nhop.family) == AFI_IP) ? "ipv4"
: "ipv6",
json_afi);
if (!found && !json)
vty_out(vty, "nexthop %s does not have entry\n", vty_out(vty, "nexthop %s does not have entry\n",
nhopip_str); nhopip_str);
if (uj)
vty_out(vty, "}");
} else } else
bgp_show_nexthops(vty, bgp, import_table, json, afi, detail); bgp_show_nexthops(vty, bgp, import_table, uj, afi, detail);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
static void bgp_show_all_instances_nexthops_vty(struct vty *vty, static void bgp_show_all_instances_nexthops_vty(struct vty *vty, bool uj, afi_t afi, bool detail)
json_object *json, afi_t afi,
bool detail)
{ {
struct listnode *node, *nnode; struct listnode *node, *nnode;
struct bgp *bgp; struct bgp *bgp;
const char *inst_name; const char *inst_name;
json_object *json_instance = NULL; bool firstinst = true;
for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) { for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
inst_name = (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) inst_name = (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
? VRF_DEFAULT_NAME ? VRF_DEFAULT_NAME
: bgp->name; : bgp->name;
if (json) if (uj)
json_instance = json_object_new_object(); vty_out(vty, "%s\"%s\":{", firstinst ? "" : ",", inst_name);
else else
vty_out(vty, "\nInstance %s:\n", inst_name); vty_out(vty, "\nInstance %s:\n", inst_name);
bgp_show_nexthops(vty, bgp, false, json_instance, afi, detail); bgp_show_nexthops(vty, bgp, false, uj, afi, detail);
firstinst = false;
if (json) if (uj)
json_object_object_add(json, inst_name, json_instance); vty_out(vty, "}");
} }
} }
@ -1235,20 +1241,18 @@ DEFPY (show_ip_bgp_nexthop,
JSON_STR) JSON_STR)
{ {
int rc = 0; int rc = 0;
json_object *json = NULL;
afi_t afiz = AFI_UNSPEC; afi_t afiz = AFI_UNSPEC;
if (uj) if (uj)
json = json_object_new_object(); vty_out(vty, "{\n");
if (afi) if (afi)
afiz = bgp_vty_afi_from_str(afi); afiz = bgp_vty_afi_from_str(afi);
rc = show_ip_bgp_nexthop_table(vty, vrf, nhop_str, false, json, afiz, rc = show_ip_bgp_nexthop_table(vty, vrf, nhop_str, false, uj, afiz, detail);
detail);
if (uj) if (uj)
vty_json(vty, json); vty_out(vty, "}\n");
return rc; return rc;
} }
@ -1265,16 +1269,14 @@ DEFPY (show_ip_bgp_import_check,
JSON_STR) JSON_STR)
{ {
int rc = 0; int rc = 0;
json_object *json = NULL;
if (uj) if (uj)
json = json_object_new_object(); vty_out(vty, "{\n");
rc = show_ip_bgp_nexthop_table(vty, vrf, NULL, true, json, AFI_UNSPEC, rc = show_ip_bgp_nexthop_table(vty, vrf, NULL, true, uj, AFI_UNSPEC, detail);
detail);
if (uj) if (uj)
vty_json(vty, json); vty_out(vty, "}\n");
return rc; return rc;
} }
@ -1292,19 +1294,18 @@ DEFPY (show_ip_bgp_instance_all_nexthop,
"Show detailed information\n" "Show detailed information\n"
JSON_STR) JSON_STR)
{ {
json_object *json = NULL;
afi_t afiz = AFI_UNSPEC; afi_t afiz = AFI_UNSPEC;
if (uj) if (uj)
json = json_object_new_object(); vty_out(vty, "{");
if (afi) if (afi)
afiz = bgp_vty_afi_from_str(afi); afiz = bgp_vty_afi_from_str(afi);
bgp_show_all_instances_nexthops_vty(vty, json, afiz, detail); bgp_show_all_instances_nexthops_vty(vty, uj, afiz, detail);
if (uj) if (uj)
vty_json(vty, json); vty_out(vty, "}");
return CMD_SUCCESS; return CMD_SUCCESS;
} }