In struct rib_entry bgpd keeps the 'best' or active prefix cached.
Now to support more than one one prefix per path (for ECMP and add-path)
I need the ability to access the previous element. The currently used
LIST macros do not support that. So I want to switch that to TAILQ but
the TAILQ head is 2 pointers not 1 and so I need to free a pointer from
struct rib_entry. Also this active cache makes less sense with multiple
paths.

Access to the active prefix is replaced with the better named
prefix_best() which returns the LIST_FIRST entry if that one is eligible.

-- 
:wq Claudio

Index: rde.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.c,v
retrieving revision 1.542
diff -u -p -r1.542 rde.c
--- rde.c       21 Mar 2022 13:33:20 -0000      1.542
+++ rde.c       21 Mar 2022 16:02:16 -0000
@@ -2400,7 +2400,7 @@ rde_dump_rib_as(struct prefix *p, struct
        rib.validation_state = p->validation_state;
        rib.flags = 0;
        re = prefix_re(p);
-       if (re != NULL && re->active == p)
+       if (re != NULL && prefix_best(re) == p)
                rib.flags |= F_PREF_BEST;
        if (!peer->conf.ebgp)
                rib.flags |= F_PREF_INTERNAL;
@@ -2502,7 +2502,7 @@ rde_dump_filter(struct prefix *p, struct
        re = prefix_re(p);
        if (asp == NULL)        /* skip pending withdraw in Adj-RIB-Out */
                return;
-       if ((req->flags & F_CTL_BEST) && re != NULL && re->active != p)
+       if ((req->flags & F_CTL_BEST) && re != NULL && prefix_best(re) != p)
                return;
        if ((req->flags & F_CTL_INVALID) &&
            (asp->flags & F_ATTR_PARSE_ERR) == 0)
@@ -3713,11 +3713,11 @@ rde_softreconfig_in(struct rib_entry *re
 static void
 rde_softreconfig_out(struct rib_entry *re, void *bula)
 {
-       struct prefix           *p = re->active;
+       struct prefix           *p;
        struct rde_peer         *peer;
        uint8_t                  aid = re->prefix->aid;
 
-       if (p == NULL)
+       if ((p = prefix_best(re)) == NULL)
                /* no valid path for prefix */
                return;
 
@@ -3750,12 +3750,10 @@ rde_softreconfig_sync_reeval(struct rib_
                        if (p->flags & PREFIX_NEXTHOP_LINKED)
                                nexthop_unlink(p);
                }
-               re->active = NULL;
                return;
        }
 
        /* evaluation process is turned on, so evaluate all prefixes again */
-       re->active = NULL;
        prefixes = re->prefix_h;
        LIST_INIT(&re->prefix_h);
 
@@ -3777,8 +3775,10 @@ rde_softreconfig_sync_reeval(struct rib_
 static void
 rde_softreconfig_sync_fib(struct rib_entry *re, void *bula)
 {
-       if (re->active)
-               rde_send_kroute(re_rib(re), re->active, NULL);
+       struct prefix *p;
+
+       if ((p = prefix_best(re)) != NULL)
+               rde_send_kroute(re_rib(re), p, NULL);
 }
 
 static void
Index: rde.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
retrieving revision 1.249
diff -u -p -r1.249 rde.h
--- rde.h       21 Mar 2022 13:33:20 -0000      1.249
+++ rde.h       21 Mar 2022 16:07:56 -0000
@@ -43,7 +43,6 @@ RB_HEAD(rib_tree, rib_entry);
 struct rib_entry {
        RB_ENTRY(rib_entry)      rib_e;
        struct prefix_list       prefix_h;
-       struct prefix           *active;        /* for fast access */
        struct pt_entry         *prefix;
        uint16_t                 rib_id;
        uint16_t                 lock;
@@ -499,8 +498,10 @@ communities_unref(struct rde_community *
 int    community_to_rd(struct community *, uint64_t *);
 
 /* rde_decide.c */
-int    prefix_eligible(struct prefix *);
-void   prefix_evaluate(struct rib_entry *, struct prefix *, struct prefix *);
+int             prefix_eligible(struct prefix *);
+struct prefix  *prefix_best(struct rib_entry *);
+void            prefix_evaluate(struct rib_entry *, struct prefix *,
+                    struct prefix *);
 
 /* rde_filter.c */
 void   rde_apply_set(struct filter_set_head *, struct rde_peer *,
Index: rde_decide.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_decide.c,v
retrieving revision 1.89
diff -u -p -r1.89 rde_decide.c
--- rde_decide.c        3 Mar 2022 13:06:15 -0000       1.89
+++ rde_decide.c        21 Mar 2022 16:03:05 -0000
@@ -443,6 +443,23 @@ prefix_eligible(struct prefix *p)
        return 1;
 }
 
+struct prefix *
+prefix_best(struct rib_entry *re)
+{
+       struct prefix   *xp;
+       struct rib      *rib;
+
+       rib = re_rib(re);
+       if (rib->flags & F_RIB_NOEVALUATE)
+               /* decision process is turned off */
+               return NULL;
+
+        xp = LIST_FIRST(&re->prefix_h);
+       if (xp != NULL && !prefix_eligible(xp))
+               xp = NULL;
+       return xp;
+}
+
 /*
  * Find the correct place to insert the prefix in the prefix list.
  * If the active prefix has changed we need to send an update also special
@@ -453,7 +470,7 @@ prefix_eligible(struct prefix *p)
 void
 prefix_evaluate(struct rib_entry *re, struct prefix *new, struct prefix *old)
 {
-       struct prefix   *xp;
+       struct prefix   *xp, *active;
        struct rib      *rib;
 
        rib = re_rib(re);
@@ -463,19 +480,11 @@ prefix_evaluate(struct rib_entry *re, st
                        LIST_REMOVE(old, entry.list.rib);
                if (new != NULL)
                        LIST_INSERT_HEAD(&re->prefix_h, new, entry.list.rib);
-               if (re->active) {
-                       /*
-                        * During reloads it is possible that the decision
-                        * process is turned off but prefixes are still
-                        * active. Clean up now to ensure that the RIB
-                        * is consistant.
-                        */
-                       rde_generate_updates(rib, NULL, re->active, 0);
-                       re->active = NULL;
-               }
                return;
        }
 
+       active = prefix_best(re);
+
        if (old != NULL)
                prefix_remove(old, re);
 
@@ -490,16 +499,15 @@ prefix_evaluate(struct rib_entry *re, st
         * If the active prefix changed or the active prefix was removed
         * and added again then generate an update.
         */
-       if (re->active != xp || (old != NULL && xp == old)) {
+       if (active != xp || (old != NULL && xp == old)) {
                /*
                 * Send update withdrawing re->active and adding xp
                 * but remember that xp may be NULL aka ineligible.
                 * Additional decision may be made by the called functions.
                 */
-               rde_generate_updates(rib, xp, re->active, 0);
+               rde_generate_updates(rib, xp, active, 0);
                if ((rib->flags & F_RIB_NOFIB) == 0)
-                       rde_send_kroute(rib, xp, re->active);
-               re->active = xp;
+                       rde_send_kroute(rib, xp, active);
                return;
        }
 
@@ -510,5 +518,5 @@ prefix_evaluate(struct rib_entry *re, st
         */
        if (rde_evaluate_all())
                if ((new != NULL && prefix_eligible(new)) || old != NULL)
-                       rde_generate_updates(rib, re->active, NULL, 1);
+                       rde_generate_updates(rib, prefix_best(re), NULL, 1);
 }
Index: rde_peer.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_peer.c,v
retrieving revision 1.13
diff -u -p -r1.13 rde_peer.c
--- rde_peer.c  6 Feb 2022 09:51:19 -0000       1.13
+++ rde_peer.c  21 Mar 2022 16:02:16 -0000
@@ -308,6 +308,7 @@ static void
 rde_up_dump_upcall(struct rib_entry *re, void *ptr)
 {
        struct rde_peer         *peer = ptr;
+       struct prefix           *p;
 
        if (peer->state != PEER_UP)
                return;
@@ -319,10 +320,10 @@ rde_up_dump_upcall(struct rib_entry *re,
                    aid2str(re->prefix->aid));
 
        /* no eligible prefix, not even for 'evaluate all' */
-       if (re->active == NULL)
+       if ((p = prefix_best(re)) == NULL)
                return;
 
-       up_generate_updates(out_rules, peer, re->active, NULL);
+       up_generate_updates(out_rules, peer, p, NULL);
 }
 
 static void

Reply via email to