zebra: simplify and optimize vrf display in show ip route

In all outputs (text and json): simplify and optimize the vrf name
display, use the vrf_id_to_name() handler.

Note: vrf_id_to_name() has a safeguard system that prevents from
crashing when the vrf cannot be found because it changed in some
(unexpected) manner, it returns "n/a".

Note: "vrf n/a" will now be displayed instead of "vrf UNKNOWN" in this
case, like in most other frr components.

This safeguard was missing for show ip route json, so this
optimization also fixes a potential crash.

Signed-off-by: Christophe Gouault <christophe.gouault@6wind.com>
This commit is contained in:
Christophe Gouault 2020-08-20 11:15:33 +02:00
parent 7c1119cb7a
commit 6ac9404177

View File

@ -342,15 +342,8 @@ static void show_nexthop_detail_helper(struct vty *vty,
} }
if ((re->vrf_id != nexthop->vrf_id) if ((re->vrf_id != nexthop->vrf_id)
&& (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
struct vrf *vrf = vty_out(vty, "(vrf %s)", vrf_id_to_name(nexthop->vrf_id));
vrf_lookup_by_id(nexthop->vrf_id);
if (vrf)
vty_out(vty, "(vrf %s)", vrf->name);
else
vty_out(vty, "(vrf UNKNOWN)");
}
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)) if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
vty_out(vty, " (duplicate nexthop removed)"); vty_out(vty, " (duplicate nexthop removed)");
@ -548,15 +541,9 @@ static void show_route_nexthop_helper(struct vty *vty,
break; break;
} }
if ((re == NULL || (nexthop->vrf_id != re->vrf_id)) && if ((re == NULL || (nexthop->vrf_id != re->vrf_id))
(nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id); vty_out(vty, " (vrf %s)", vrf_id_to_name(nexthop->vrf_id));
if (vrf)
vty_out(vty, " (vrf %s)", vrf->name);
else
vty_out(vty, " (vrf UNKNOWN)");
}
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
vty_out(vty, " inactive"); vty_out(vty, " inactive");
@ -620,7 +607,6 @@ static void show_nexthop_json_helper(json_object *json_nexthop,
const struct route_entry *re) const struct route_entry *re)
{ {
char buf[SRCDEST2STR_BUFFER]; char buf[SRCDEST2STR_BUFFER];
struct vrf *vrf = NULL;
json_object *json_labels = NULL; json_object *json_labels = NULL;
json_object *json_backups = NULL; json_object *json_backups = NULL;
int i; int i;
@ -714,11 +700,10 @@ static void show_nexthop_json_helper(json_object *json_nexthop,
} }
if ((nexthop->vrf_id != re->vrf_id) if ((nexthop->vrf_id != re->vrf_id)
&& (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
vrf = vrf_lookup_by_id(nexthop->vrf_id);
json_object_string_add(json_nexthop, "vrf", json_object_string_add(json_nexthop, "vrf",
vrf->name); vrf_id_to_name(nexthop->vrf_id));
}
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)) if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
json_object_boolean_true_add(json_nexthop, json_object_boolean_true_add(json_nexthop,
"duplicate"); "duplicate");
@ -813,7 +798,6 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,
json_object *json_nexthop = NULL; json_object *json_nexthop = NULL;
json_object *json_route = NULL; json_object *json_route = NULL;
time_t uptime; time_t uptime;
const struct vrf *vrf = NULL;
const rib_dest_t *dest = rib_dest_from_rnode(rn); const rib_dest_t *dest = rib_dest_from_rnode(rn);
const struct nexthop_group *nhg; const struct nexthop_group *nhg;
char up_str[MONOTIME_STRLEN]; char up_str[MONOTIME_STRLEN];
@ -848,11 +832,10 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,
if (re->vrf_id) { if (re->vrf_id) {
json_object_int_add(json_route, "vrfId", re->vrf_id); json_object_int_add(json_route, "vrfId", re->vrf_id);
vrf = vrf_lookup_by_id(re->vrf_id);
json_object_string_add(json_route, "vrfName", json_object_string_add(json_route, "vrfName",
vrf->name); vrf_id_to_name(re->vrf_id));
} }
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
json_object_boolean_true_add(json_route, "selected"); json_object_boolean_true_add(json_route, "selected");
@ -1302,18 +1285,11 @@ static void show_nexthop_group_out(struct vty *vty, struct nhg_hash_entry *nhe)
{ {
struct nexthop *nexthop = NULL; struct nexthop *nexthop = NULL;
struct nhg_connected *rb_node_dep = NULL; struct nhg_connected *rb_node_dep = NULL;
struct vrf *nhe_vrf = vrf_lookup_by_id(nhe->vrf_id);
struct nexthop_group *backup_nhg; struct nexthop_group *backup_nhg;
vty_out(vty, "ID: %u\n", nhe->id); vty_out(vty, "ID: %u\n", nhe->id);
vty_out(vty, " RefCnt: %d\n", nhe->refcnt); vty_out(vty, " RefCnt: %d\n", nhe->refcnt);
vty_out(vty, " VRF: %s\n", vrf_id_to_name(nhe->vrf_id));
if (nhe_vrf)
vty_out(vty, " VRF: %s AFI: %s\n", nhe_vrf->name,
afi2str(nhe->afi));
else
vty_out(vty, " VRF: UNKNOWN AFI: %s\n",
afi2str(nhe->afi));
if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE)) if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE))
vty_out(vty, " Duplicate - from kernel not hashable\n"); vty_out(vty, " Duplicate - from kernel not hashable\n");