]> git.puffer.fish Git - matthieu/frr.git/commitdiff
zebra: simplify and optimize vrf display in show ip route
authorChristophe Gouault <christophe.gouault@6wind.com>
Thu, 20 Aug 2020 09:15:33 +0000 (11:15 +0200)
committerChristophe Gouault <christophe.gouault@6wind.com>
Mon, 21 Sep 2020 08:07:56 +0000 (10:07 +0200)
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>
zebra/zebra_vty.c

index 04581be19b1dd86b2277c0eba8215702038325e1..77a2ce4ae768ac272f17d3dcaa065c74dd8335cf 100644 (file)
@@ -342,15 +342,8 @@ static void show_nexthop_detail_helper(struct vty *vty,
        }
 
        if ((re->vrf_id != nexthop->vrf_id)
-           && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) {
-               struct vrf *vrf =
-                       vrf_lookup_by_id(nexthop->vrf_id);
-
-               if (vrf)
-                       vty_out(vty, "(vrf %s)", vrf->name);
-               else
-                       vty_out(vty, "(vrf UNKNOWN)");
-       }
+           && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
+               vty_out(vty, "(vrf %s)", vrf_id_to_name(nexthop->vrf_id));
 
        if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
                vty_out(vty, " (duplicate nexthop removed)");
@@ -548,15 +541,9 @@ static void show_route_nexthop_helper(struct vty *vty,
                break;
        }
 
-       if ((re == NULL || (nexthop->vrf_id != re->vrf_id)) &&
-           (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) {
-               struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
-
-               if (vrf)
-                       vty_out(vty, " (vrf %s)", vrf->name);
-               else
-                       vty_out(vty, " (vrf UNKNOWN)");
-       }
+       if ((re == NULL || (nexthop->vrf_id != re->vrf_id))
+           && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
+               vty_out(vty, " (vrf %s)", vrf_id_to_name(nexthop->vrf_id));
 
        if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
                vty_out(vty, " inactive");
@@ -620,7 +607,6 @@ static void show_nexthop_json_helper(json_object *json_nexthop,
                                     const struct route_entry *re)
 {
        char buf[SRCDEST2STR_BUFFER];
-       struct vrf *vrf = NULL;
        json_object *json_labels = NULL;
        json_object *json_backups = NULL;
        int i;
@@ -714,11 +700,10 @@ static void show_nexthop_json_helper(json_object *json_nexthop,
        }
 
        if ((nexthop->vrf_id != re->vrf_id)
-           && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) {
-               vrf = vrf_lookup_by_id(nexthop->vrf_id);
+           && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE))
                json_object_string_add(json_nexthop, "vrf",
-                                      vrf->name);
-       }
+                                      vrf_id_to_name(nexthop->vrf_id));
+
        if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
                json_object_boolean_true_add(json_nexthop,
                                             "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_route = NULL;
        time_t uptime;
-       const struct vrf *vrf = NULL;
        const rib_dest_t *dest = rib_dest_from_rnode(rn);
        const struct nexthop_group *nhg;
        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) {
                        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",
-                                              vrf->name);
-
+                                              vrf_id_to_name(re->vrf_id));
                }
+
                if (CHECK_FLAG(re->flags, ZEBRA_FLAG_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 nhg_connected *rb_node_dep = NULL;
-       struct vrf *nhe_vrf = vrf_lookup_by_id(nhe->vrf_id);
        struct nexthop_group *backup_nhg;
 
        vty_out(vty, "ID: %u\n", nhe->id);
        vty_out(vty, "     RefCnt: %d\n", nhe->refcnt);
-
-       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));
+       vty_out(vty, "     VRF: %s\n", vrf_id_to_name(nhe->vrf_id));
 
        if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE))
                vty_out(vty, "     Duplicate - from kernel not hashable\n");