This uses struct filterstate in rde_update_dispatch() and passes the
struct to a few functions down (rde_update_update, path_update and some
more friends). After that it should become easier to move nexthop and
later maybe the peer out of struct rde_aspath into struct prefix.

-- 
:wq Claudio


Index: rde.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.c,v
retrieving revision 1.390
diff -u -p -r1.390 rde.c
--- rde.c       10 Jul 2018 15:13:35 -0000      1.390
+++ rde.c       10 Jul 2018 16:25:52 -0000
@@ -51,16 +51,16 @@ void                 rde_sighdlr(int);
 void            rde_dispatch_imsg_session(struct imsgbuf *);
 void            rde_dispatch_imsg_parent(struct imsgbuf *);
 int             rde_update_dispatch(struct imsg *);
-int             rde_update_update(struct rde_peer *, struct rde_aspath *,
+int             rde_update_update(struct rde_peer *, struct filterstate *,
                     struct bgpd_addr *, u_int8_t);
 void            rde_update_withdraw(struct rde_peer *, struct bgpd_addr *,
                     u_int8_t);
 int             rde_attr_parse(u_char *, u_int16_t, struct rde_peer *,
-                    struct rde_aspath *, struct mpattr *);
+                    struct filterstate *, struct mpattr *);
 int             rde_attr_add(struct rde_aspath *, u_char *, u_int16_t);
 u_int8_t        rde_attr_missing(struct rde_aspath *, int, u_int16_t);
 int             rde_get_mp_nexthop(u_char *, u_int16_t, u_int8_t,
-                    struct rde_aspath *);
+                    struct filterstate *);
 int             rde_update_extract_prefix(u_char *, u_int16_t, void *,
                     u_int8_t, u_int8_t);
 int             rde_update_get_prefix(u_char *, u_int16_t, struct bgpd_addr *,
@@ -938,7 +938,7 @@ rde_dispatch_imsg_parent(struct imsgbuf 
 int
 rde_update_dispatch(struct imsg *imsg)
 {
-       struct rde_aspath        asp;
+       struct filterstate       state;
        struct bgpd_addr         prefix;
        struct mpattr            mpa;
        struct rde_peer         *peer;
@@ -986,11 +986,11 @@ rde_update_dispatch(struct imsg *imsg)
            imsg->hdr.len - IMSG_HEADER_SIZE - 4 - withdrawn_len - attrpath_len;
        bzero(&mpa, sizeof(mpa));
 
-       path_prep(&asp);
+       rde_filterstate_prep(&state, NULL);
        if (attrpath_len != 0) { /* 0 = no NLRI information in this message */
                /* parse path attributes */
                while (len > 0) {
-                       if ((pos = rde_attr_parse(p, len, peer, &asp,
+                       if ((pos = rde_attr_parse(p, len, peer, &state,
                            &mpa)) < 0)
                                goto done;
                        p += pos;
@@ -998,19 +998,19 @@ rde_update_dispatch(struct imsg *imsg)
                }
 
                /* check for missing but necessary attributes */
-               if ((subtype = rde_attr_missing(&asp, peer->conf.ebgp,
+               if ((subtype = rde_attr_missing(&state.aspath, peer->conf.ebgp,
                    nlri_len))) {
                        rde_update_err(peer, ERR_UPDATE, ERR_UPD_MISSNG_WK_ATTR,
                            &subtype, sizeof(u_int8_t));
                        goto done;
                }
 
-               rde_as4byte_fixup(peer, &asp);
+               rde_as4byte_fixup(peer, &state.aspath);
 
                /* enforce remote AS if requested */
-               if (asp.flags & F_ATTR_ASPATH &&
+               if (state.aspath.flags & F_ATTR_ASPATH &&
                    peer->conf.enforce_as == ENFORCE_AS_ON) {
-                       fas = aspath_neighbor(asp.aspath);
+                       fas = aspath_neighbor(state.aspath.aspath);
                        if (peer->conf.remote_as != fas) {
                            log_peer_warnx(&peer->conf, "bad path, "
                                "starting with %s, "
@@ -1021,7 +1021,7 @@ rde_update_dispatch(struct imsg *imsg)
                        }
                }
 
-               rde_reflector(peer, &asp);
+               rde_reflector(peer, &state.aspath);
        }
 
        p = imsg->data;
@@ -1096,7 +1096,8 @@ rde_update_dispatch(struct imsg *imsg)
                        goto done;
                }
 
-               if ((asp.flags & ~F_ATTR_MP_UNREACH) == 0 && mplen == 0) {
+               if ((state.aspath.flags & ~F_ATTR_MP_UNREACH) == 0 &&
+                   mplen == 0) {
                        /* EoR marker */
                        peer_recv_eor(peer, aid);
                }
@@ -1141,7 +1142,7 @@ rde_update_dispatch(struct imsg *imsg)
                        break;
                }
 
-               if ((asp.flags & ~F_ATTR_MP_UNREACH) == 0) {
+               if ((state.aspath.flags & ~F_ATTR_MP_UNREACH) == 0) {
                        error = 0;
                        goto done;
                }
@@ -1153,8 +1154,8 @@ rde_update_dispatch(struct imsg *imsg)
        /* aspath needs to be loop free nota bene this is not a hard error */
        if (peer->conf.ebgp &&
            peer->conf.enforce_local_as == ENFORCE_AS_ON &&
-           !aspath_loopfree(asp.aspath, peer->conf.local_as))
-               asp.flags |= F_ATTR_LOOP;
+           !aspath_loopfree(state.aspath.aspath, peer->conf.local_as))
+               state.aspath.flags |= F_ATTR_LOOP;
 
        /* parse nlri prefix */
        while (nlri_len > 0) {
@@ -1176,7 +1177,7 @@ rde_update_dispatch(struct imsg *imsg)
                        goto done;
                }
 
-               if (rde_update_update(peer, &asp, &prefix, prefixlen) == -1)
+               if (rde_update_update(peer, &state, &prefix, prefixlen) == -1)
                        goto done;
 
        }
@@ -1212,11 +1213,11 @@ rde_update_dispatch(struct imsg *imsg)
                 * this works because asp is not linked.
                 * But first unlock the previously locked nexthop.
                 */
-               if (asp.nexthop) {
-                       (void)nexthop_put(asp.nexthop);
-                       asp.nexthop = NULL;
+               if (state.aspath.nexthop) {
+                       (void)nexthop_put(state.aspath.nexthop);
+                       state.aspath.nexthop = NULL;
                }
-               if ((pos = rde_get_mp_nexthop(mpp, mplen, aid, &asp)) == -1) {
+               if ((pos = rde_get_mp_nexthop(mpp, mplen, aid, &state)) == -1) {
                        log_peer_warnx(&peer->conf, "bad nlri prefix");
                        rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
                            mpa.reach, mpa.reach_len);
@@ -1240,7 +1241,7 @@ rde_update_dispatch(struct imsg *imsg)
                                mpp += pos;
                                mplen -= pos;
 
-                               if (rde_update_update(peer, &asp, &prefix,
+                               if (rde_update_update(peer, &state, &prefix,
                                    prefixlen) == -1)
                                        goto done;
                        }
@@ -1259,7 +1260,7 @@ rde_update_dispatch(struct imsg *imsg)
                                mpp += pos;
                                mplen -= pos;
 
-                               if (rde_update_update(peer, &asp, &prefix,
+                               if (rde_update_update(peer, &state, &prefix,
                                    prefixlen) == -1)
                                        goto done;
                        }
@@ -1271,13 +1272,13 @@ rde_update_dispatch(struct imsg *imsg)
        }
 
 done:
-       path_clean(&asp);
+       rde_filterstate_clean(&state);
 
        return (error);
 }
 
 int
-rde_update_update(struct rde_peer *peer, struct rde_aspath *asp,
+rde_update_update(struct rde_peer *peer, struct filterstate *in,
     struct bgpd_addr *prefix, u_int8_t prefixlen)
 {
        struct filterstate       state;
@@ -1287,7 +1288,7 @@ rde_update_update(struct rde_peer *peer,
 
        peer->prefix_rcvd_update++;
        /* add original path to the Adj-RIB-In */
-       if (path_update(&ribs[RIB_ADJ_IN].rib, peer, asp, prefix, prefixlen, 0))
+       if (path_update(&ribs[RIB_ADJ_IN].rib, peer, in, prefix, prefixlen, 0))
                peer->prefix_cnt++;
 
        /* max prefix checker */
@@ -1305,7 +1306,7 @@ rde_update_update(struct rde_peer *peer,
        for (i = RIB_LOC_START; i < rib_size; i++) {
                if (*ribs[i].name == '\0')
                        break;
-               rde_filterstate_prep(&state, asp);
+               rde_filterstate_prep(&state, &in->aspath);
                /* input filter */
                action = rde_filter(ribs[i].in_rules, peer, p, &state);
 
@@ -1313,7 +1314,7 @@ rde_update_update(struct rde_peer *peer,
                        rde_update_log("update", i, peer,
                            &state.aspath.nexthop->exit_nexthop, prefix,
                            prefixlen);
-                       path_update(&ribs[i].rib, peer, &state.aspath, prefix,
+                       path_update(&ribs[i].rib, peer, &state, prefix,
                            prefixlen, 0);
                } else if (prefix_remove(&ribs[i].rib, peer, prefix, prefixlen,
                    0)) {
@@ -1366,9 +1367,10 @@ rde_update_withdraw(struct rde_peer *pee
 
 int
 rde_attr_parse(u_char *p, u_int16_t len, struct rde_peer *peer,
-    struct rde_aspath *a, struct mpattr *mpa)
+    struct filterstate *state, struct mpattr *mpa)
 {
        struct bgpd_addr nexthop;
+       struct rde_aspath *a = &state->aspath;
        u_char          *op = p, *npath;
        u_int32_t        tmp32;
        int              error;
@@ -1755,7 +1757,7 @@ rde_attr_missing(struct rde_aspath *a, i
 
 int
 rde_get_mp_nexthop(u_char *data, u_int16_t len, u_int8_t aid,
-    struct rde_aspath *asp)
+    struct filterstate *state)
 {
        struct bgpd_addr        nexthop;
        u_int8_t                totlen, nhlen;
@@ -1815,7 +1817,7 @@ rde_get_mp_nexthop(u_char *data, u_int16
                return (-1);
        }
 
-       asp->nexthop = nexthop_get(&nexthop);
+       state->aspath.nexthop = nexthop_get(&nexthop);
 
        /* ignore reserved (old SNPA) field as per RFC4760 */
        totlen += nhlen + 1;
@@ -2994,7 +2996,7 @@ rde_softreconfig_in(struct rib_entry *re
 
                if (action == ACTION_ALLOW) {
                        /* update Local-RIB */
-                       path_update(&rib->rib, peer, &state.aspath, &addr,
+                       path_update(&rib->rib, peer, &state, &addr,
                            pt->prefixlen, 0);
                } else if (action == ACTION_DENY) {
                        /* remove from Local-RIB */
@@ -3550,7 +3552,7 @@ network_add(struct network_config *nc, i
        for (i = RIB_LOC_START; i < rib_size; i++) {
                if (*ribs[i].name == '\0')
                        break;
-               path_update(&ribs[i].rib, peerself, asp, &nc->prefix,
+               path_update(&ribs[i].rib, peerself, &state, &nc->prefix,
                    nc->prefixlen, 0);
        }
        rde_filterstate_clean(&state);
Index: rde.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
retrieving revision 1.176
diff -u -p -r1.176 rde.h
--- rde.h       9 Jul 2018 14:08:48 -0000       1.176
+++ rde.h       10 Jul 2018 16:23:24 -0000
@@ -473,7 +473,7 @@ void                 path_init(u_int32_t);
 void            path_init(u_int32_t);
 void            path_shutdown(void);
 int             path_update(struct rib *, struct rde_peer *,
-                    struct rde_aspath *, struct bgpd_addr *, int, int);
+                    struct filterstate *, struct bgpd_addr *, int, int);
 int             path_compare(struct rde_aspath *, struct rde_aspath *);
 void            path_remove(struct rde_aspath *);
 u_int32_t       path_remove_stale(struct rde_aspath *, u_int8_t);
Index: rde_filter.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_filter.c,v
retrieving revision 1.95
diff -u -p -r1.95 rde_filter.c
--- rde_filter.c        9 Jul 2018 14:08:48 -0000       1.95
+++ rde_filter.c        10 Jul 2018 16:07:16 -0000
@@ -639,7 +639,9 @@ rde_filterstate_prep(struct filterstate 
 {
        memset(state, 0, sizeof(*state));
 
-       path_copy(path_prep(&state->aspath), asp);
+       path_prep(&state->aspath);
+       if (asp)
+               path_copy(&state->aspath, asp);
 }
 
 void
Index: rde_rib.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_rib.c,v
retrieving revision 1.168
diff -u -p -r1.168 rde_rib.c
--- rde_rib.c   10 Jul 2018 07:58:13 -0000      1.168
+++ rde_rib.c   10 Jul 2018 16:26:15 -0000
@@ -390,10 +390,10 @@ path_shutdown(void)
 }
 
 int
-path_update(struct rib *rib, struct rde_peer *peer, struct rde_aspath *nasp,
+path_update(struct rib *rib, struct rde_peer *peer, struct filterstate *state,
     struct bgpd_addr *prefix, int prefixlen, int flag)
 {
-       struct rde_aspath       *asp;
+       struct rde_aspath       *asp, *nasp = &state->aspath;
        struct prefix           *p;
        int                      pflag = 0;
 

Reply via email to