Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/netipsec Use pslist(9) for sah->savtree



details:   https://anonhg.NetBSD.org/src/rev/690617773347
branches:  trunk
changeset: 355563:690617773347
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Thu Aug 03 06:30:40 2017 +0000

description:
Use pslist(9) for sah->savtree

diffstat:

 sys/netipsec/key.c   |  142 ++++++++++++++++++++++++++++++++++----------------
 sys/netipsec/keydb.h |    6 +-
 2 files changed, 99 insertions(+), 49 deletions(-)

diffs (truncated from 425 to 300 lines):

diff -r cee3416c49ca -r 690617773347 sys/netipsec/key.c
--- a/sys/netipsec/key.c        Thu Aug 03 06:30:04 2017 +0000
+++ b/sys/netipsec/key.c        Thu Aug 03 06:30:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: key.c,v 1.202 2017/08/03 06:30:04 ozaki-r Exp $        */
+/*     $NetBSD: key.c,v 1.203 2017/08/03 06:30:40 ozaki-r Exp $        */
 /*     $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $        */
 /*     $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $   */
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.202 2017/08/03 06:30:04 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.203 2017/08/03 06:30:40 ozaki-r Exp $");
 
 /*
  * This code is referd to RFC 2367
@@ -258,6 +258,51 @@
 #define SAHLIST_WRITER_INSERT_HEAD(sah)                                        \
        PSLIST_WRITER_INSERT_HEAD(&sahtree, (sah), pslist_entry)
 
+#define SAVLIST_ENTRY_INIT(sav)                                                \
+       PSLIST_ENTRY_INIT((sav), pslist_entry)
+#define SAVLIST_ENTRY_DESTROY(sav)                                     \
+       PSLIST_ENTRY_DESTROY((sav), pslist_entry)
+#define SAVLIST_READER_FIRST(sah, state)                               \
+       PSLIST_READER_FIRST(&(sah)->savtree[(state)], struct secasvar,  \
+                           pslist_entry)
+#define SAVLIST_WRITER_REMOVE(sav)                                     \
+       PSLIST_WRITER_REMOVE((sav), pslist_entry)
+#define SAVLIST_READER_FOREACH(sav, sah, state)                                \
+       PSLIST_READER_FOREACH((sav), &(sah)->savtree[(state)],          \
+                             struct secasvar, pslist_entry)
+#define SAVLIST_WRITER_FOREACH(sav, sah, state)                                \
+       PSLIST_WRITER_FOREACH((sav), &(sah)->savtree[(state)],          \
+                             struct secasvar, pslist_entry)
+#define SAVLIST_WRITER_INSERT_BEFORE(sav, new)                         \
+       PSLIST_WRITER_INSERT_BEFORE((sav), (new), pslist_entry)
+#define SAVLIST_WRITER_INSERT_AFTER(sav, new)                          \
+       PSLIST_WRITER_INSERT_AFTER((sav), (new), pslist_entry)
+#define SAVLIST_WRITER_EMPTY(sah, state)                               \
+       (PSLIST_WRITER_FIRST(&(sah)->savtree[(state)], struct secasvar, \
+                            pslist_entry) == NULL)
+#define SAVLIST_WRITER_INSERT_HEAD(sah, state, sav)                    \
+       PSLIST_WRITER_INSERT_HEAD(&(sah)->savtree[(state)], (sav),      \
+                                 pslist_entry)
+#define SAVLIST_WRITER_NEXT(sav)                                       \
+       PSLIST_WRITER_NEXT((sav), struct secasvar, pslist_entry)
+#define SAVLIST_WRITER_INSERT_TAIL(sah, state, new)                    \
+       do {                                                            \
+               if (SAVLIST_WRITER_EMPTY((sah), (state))) {             \
+                       SAVLIST_WRITER_INSERT_HEAD((sah), (state), (new));\
+               } else {                                                \
+                       struct secasvar *__sav;                         \
+                       SAVLIST_WRITER_FOREACH(__sav, (sah), (state)) { \
+                               if (SAVLIST_WRITER_NEXT(__sav) == NULL) {\
+                                       SAVLIST_WRITER_INSERT_AFTER(__sav,\
+                                           (new));                     \
+                                       break;                          \
+                               }                                       \
+                       }                                               \
+               }                                                       \
+       } while (0)
+#define SAVLIST_READER_NEXT(sav)                                       \
+       PSLIST_READER_NEXT((sav), struct secasvar, pslist_entry)
+
 /*
  * The list has SPs that are set to a socket via setsockopt(IP_IPSEC_POLICY)
  * from userland. See ipsec_set_policy.
@@ -970,12 +1015,12 @@
                state = saorder_state_valid[stateidx];
 
                if (key_prefered_oldsa)
-                       sav = LIST_FIRST(&sah->savtree[state]);
+                       sav = SAVLIST_READER_FIRST(sah, state);
                else {
                        /* XXX need O(1) lookup */
                        struct secasvar *last = NULL;
 
-                       LIST_FOREACH(sav, &sah->savtree[state], chain)
+                       SAVLIST_READER_FOREACH(sav, sah, state)
                                last = sav;
                        sav = last;
                }
@@ -1133,7 +1178,7 @@
                /* search valid state */
                for (stateidx = 0; stateidx < arraysize; stateidx++) {
                        state = saorder_state_valid[stateidx];
-                       LIST_FOREACH(sav, &sah->savtree[state], chain) {
+                       SAVLIST_READER_FOREACH(sav, sah, state) {
                                KEYDEBUG_PRINTF(KEYDEBUG_MATCH,
                                    "try match spi %#x, %#x\n",
                                    ntohl(spi), ntohl(sav->spi));
@@ -1201,7 +1246,8 @@
         * The list should be sorted by lft_c->sadb_lifetime_addtime
         * in ascending order.
         */
-       LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, next) {
+       SAVLIST_READER_FOREACH(sav, sah, state) {
+               next = SAVLIST_READER_NEXT(sav);
                if (next != NULL &&
                    sav->lft_c != NULL && next->lft_c != NULL) {
                        KDASSERTMSG(sav->lft_c->sadb_lifetime_addtime <=
@@ -1361,8 +1407,7 @@
                *psav = NULL;
 
                /* remove from SA header */
-               KASSERT(__LIST_CHAINED(sav));
-               LIST_REMOVE(sav, chain);
+               SAVLIST_WRITER_REMOVE(sav);
 
                key_delsav(sav);
        }
@@ -2864,7 +2909,7 @@
 
        newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP);
        for (i = 0; i < __arraycount(newsah->savtree); i++)
-               LIST_INIT(&newsah->savtree[i]);
+               PSLIST_INIT(&newsah->savtree[i]);
        newsah->saidx = *saidx;
 
        /* add to saidxtree */
@@ -2893,7 +2938,7 @@
 
        /* searching all SA registerd in the secindex. */
        SASTATE_ANY_FOREACH(state) {
-               LIST_FOREACH(sav, &sah->savtree[state], chain) {
+               SAVLIST_READER_FOREACH(sav, sah, state) {
                        /* give up to delete this sa */
                        zombie++;
                }
@@ -3040,6 +3085,7 @@
 
        key_clear_xform(sav);
        key_freesaval(sav);
+       SAVLIST_ENTRY_DESTROY(sav);
        kmem_intr_free(sav, sizeof(*sav));
 
        return;
@@ -3113,8 +3159,7 @@
 
        /* search all status */
        SASTATE_ALIVE_FOREACH(state) {
-               LIST_FOREACH(sav, &sah->savtree[state], chain) {
-
+               SAVLIST_READER_FOREACH(sav, sah, state) {
                        /* sanity check */
                        if (sav->state != state) {
                                IPSECLOG(LOG_DEBUG,
@@ -4501,7 +4546,7 @@
 key_timehandler_sad(time_t now)
 {
        struct secashead *sah;
-       struct secasvar *sav, *nextsav;
+       struct secasvar *sav;
 
 restart:
        SAHLIST_WRITER_FOREACH(sah) {
@@ -4512,10 +4557,11 @@
                }
 
                /* if LARVAL entry doesn't become MATURE, delete it. */
-               LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL],
-                   chain, nextsav) {
+       restart_sav_LARVAL:
+               SAVLIST_READER_FOREACH(sav, sah, SADB_SASTATE_LARVAL) {
                        if (now - sav->created > key_larval_lifetime) {
                                KEY_FREESAV(&sav);
+                               goto restart_sav_LARVAL;
                        }
                }
 
@@ -4523,8 +4569,8 @@
                 * check MATURE entry to start to send expire message
                 * whether or not.
                 */
-               LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE],
-                   chain, nextsav) {
+       restart_sav_MATURE:
+               SAVLIST_READER_FOREACH(sav, sah, SADB_SASTATE_MATURE) {
                        /* we don't need to check. */
                        if (sav->lft_s == NULL)
                                continue;
@@ -4551,6 +4597,7 @@
                                         */
                                        key_expire(sav);
                                }
+                               goto restart_sav_MATURE;
                        }
                        /* check SOFT lifetime by bytes */
                        /*
@@ -4569,12 +4616,13 @@
                                 * DYING. Do remove below code.
                                 */
                                key_expire(sav);
+                               goto restart_sav_MATURE;
                        }
                }
 
                /* check DYING entry to change status to DEAD. */
-               LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING],
-                   chain, nextsav) {
+       restart_sav_DYING:
+               SAVLIST_READER_FOREACH(sav, sah, SADB_SASTATE_DYING) {
                        /* we don't need to check. */
                        if (sav->lft_h == NULL)
                                continue;
@@ -4586,6 +4634,7 @@
                            now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
                                key_sa_chgstate(sav, SADB_SASTATE_DEAD);
                                KEY_FREESAV(&sav);
+                               goto restart_sav_DYING;
                        }
 #if 0  /* XXX Should we keep to send expire message until HARD lifetime ? */
                        else if (sav->lft_s != NULL
@@ -4609,12 +4658,12 @@
                                 sav->lft_c->sadb_lifetime_bytes) {
                                key_sa_chgstate(sav, SADB_SASTATE_DEAD);
                                KEY_FREESAV(&sav);
+                               goto restart_sav_DYING;
                        }
                }
 
                /* delete entry in DEAD */
-               LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD],
-                   chain, nextsav) {
+               SAVLIST_READER_FOREACH(sav, sah, SADB_SASTATE_DEAD) {
                        /* sanity check */
                        if (sav->state != SADB_SASTATE_DEAD) {
                                IPSECLOG(LOG_DEBUG,
@@ -4912,8 +4961,8 @@
        newsav->refcnt = 1;
        newsav->sah = newsah;
        newsav->state = SADB_SASTATE_LARVAL;
-       LIST_INSERT_TAIL(&newsah->savtree[SADB_SASTATE_LARVAL], newsav,
-           secasvar, chain);
+       SAVLIST_ENTRY_INIT(newsav);
+       SAVLIST_WRITER_INSERT_TAIL(newsah, SADB_SASTATE_LARVAL, newsav);
        key_validate_savlist(newsah, SADB_SASTATE_LARVAL);
 
 #ifndef IPSEC_NONBLOCK_ACQUIRE
@@ -5352,8 +5401,8 @@
        /* add to satree */
        newsav->refcnt = 1;
        newsav->state = SADB_SASTATE_MATURE;
-       LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_MATURE], newsav,
-           secasvar, chain);
+       SAVLIST_ENTRY_INIT(newsav);
+       SAVLIST_WRITER_INSERT_TAIL(sah, SADB_SASTATE_MATURE, newsav);
        key_validate_savlist(sah, SADB_SASTATE_MATURE);
 
        key_sa_chgstate(sav, SADB_SASTATE_DEAD);
@@ -5395,8 +5444,7 @@
        state = SADB_SASTATE_LARVAL;
 
        /* search SAD with sequence number ? */
-       LIST_FOREACH(sav, &sah->savtree[state], chain) {
-
+       SAVLIST_READER_FOREACH(sav, sah, state) {
                KEY_CHKSASTATE(state, sav->state);
 
                if (sav->seq == seq) {
@@ -5541,8 +5589,8 @@
        /* add to satree */
        newsav->refcnt = 1;
        newsav->state = SADB_SASTATE_MATURE;
-       LIST_INSERT_TAIL(&newsah->savtree[SADB_SASTATE_MATURE], newsav,
-           secasvar, chain);
+       SAVLIST_ENTRY_INIT(newsav);
+       SAVLIST_WRITER_INSERT_TAIL(newsah, SADB_SASTATE_MATURE, newsav);
        key_validate_savlist(newsah, SADB_SASTATE_MATURE);
 
        /*
@@ -5791,7 +5839,7 @@
        const struct sockaddr *src, *dst;
        struct secasindex saidx;
        struct secashead *sah;
-       struct secasvar *sav, *nextsav;
+       struct secasvar *sav;
        u_int state;
        int error;
 
@@ -5812,8 +5860,8 @@
                SASTATE_ALIVE_FOREACH(state) {
                        if (state == SADB_SASTATE_LARVAL)
                                continue;
-                       LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain,
-                           nextsav) {
+               restart:
+                       SAVLIST_WRITER_FOREACH(sav, sah, state) {
                                /* sanity check */
                                if (sav->state != state) {
                                        IPSECLOG(LOG_DEBUG,
@@ -5825,6 +5873,7 @@
 
                                key_sa_chgstate(sav, SADB_SASTATE_DEAD);
                                KEY_FREESAV(&sav);
+                               goto restart;
                        }



Home | Main Index | Thread Index | Old Index