]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zebra_nhg.c
zebra: can't improve efficiency for recursive depends
[mirror_frr.git] / zebra / zebra_nhg.c
index 1a702506275862f76f4363554731ab16413a0776..2be9238ee87092d6b33685430c733eff3d062e17 100644 (file)
@@ -49,7 +49,8 @@ DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context");
 /* id counter to keep in sync with kernel */
 uint32_t id_counter;
 
-static struct nhg_hash_entry *depends_find(struct nexthop *nh, afi_t afi);
+static struct nhg_hash_entry *depends_find(const struct nexthop *nh,
+                                          afi_t afi);
 static void depends_add(struct nhg_connected_tree_head *head,
                        struct nhg_hash_entry *depend);
 static struct nhg_hash_entry *
@@ -593,7 +594,7 @@ zebra_nhg_find_nexthop(uint32_t id, struct nexthop *nh, afi_t afi, int type)
 
        nexthop_group_add_sorted(&nhg, nh);
 
-       zebra_nhg_find(&nhe, id, &nhg, NULL, nh->vrf_id, afi, 0);
+       zebra_nhg_find(&nhe, id, &nhg, NULL, nh->vrf_id, afi, type);
 
        return nhe;
 }
@@ -1047,17 +1048,20 @@ int zebra_nhg_kernel_del(uint32_t id)
 }
 
 /* Some dependency helper functions */
-static struct nhg_hash_entry *depends_find(struct nexthop *nh, afi_t afi)
+static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh,
+                                                    afi_t afi)
 {
+       struct nhg_hash_entry *nhe;
        struct nexthop *lookup = NULL;
-       struct nhg_hash_entry *nhe = NULL;
-
-       if (!nh)
-               goto done;
 
+       /*
+        * We need to copy its resolved nexthop if its recursively
+        * resolved so that has to be handled with allocs/frees since
+        * it could resolve to a group of unknown size.
+        */
        copy_nexthops(&lookup, nh, NULL);
 
-       /* Clear it, in case its a group */
+       /* Make it a single, recursive nexthop */
        nexthops_free(lookup->next);
        nexthops_free(lookup->prev);
        lookup->next = NULL;
@@ -1067,6 +1071,43 @@ static struct nhg_hash_entry *depends_find(struct nexthop *nh, afi_t afi)
 
        nexthops_free(lookup);
 
+       return nhe;
+}
+
+static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh,
+                                                    afi_t afi)
+{
+       struct nhg_hash_entry *nhe;
+       struct nexthop lookup = {};
+
+       /* Capture a snapshot of this single nh; it might be part of a list,
+        * so we need to make a standalone copy.
+        */
+       nexthop_copy(&lookup, nh, NULL);
+
+       nhe = zebra_nhg_find_nexthop(0, &lookup, afi, 0);
+
+       /* The copy may have allocated labels; free them if necessary. */
+       nexthop_del_labels(&lookup);
+
+       return nhe;
+}
+
+static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi)
+{
+       struct nhg_hash_entry *nhe = NULL;
+
+       if (!nh)
+               goto done;
+
+       /* We are separating these functions out to increase handling speed
+        * in the non-recursive case (by not alloc/freeing)
+        */
+       if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
+               nhe = depends_find_recursive(nh, afi);
+       else
+               nhe = depends_find_singleton(nh, afi);
+
 done:
        return nhe;
 }
@@ -1790,7 +1831,7 @@ uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe,
                if (!duplicate) {
                        grp[i].id = depend->id;
                        /* We aren't using weights for anything right now */
-                       grp[i].weight = 0;
+                       grp[i].weight = depend->nhg->nexthop->weight;
                        i++;
                }