diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2014-01-20 22:42:07 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2014-01-20 22:42:07 +0000 |
commit | 38457aca24f247fa99a127fb23f3175f5c131bf6 (patch) | |
tree | be2be94c3c893b7cf2c0870a5aeafc1dae90468a | |
parent | 22823622ec4d70aad9176ce45a3e55253b78bd11 (diff) |
Try to be more consistent in how various variables are setup and named.
Also remove a memset that is already happeing because we use PR_ZERO on
the allocation. Move two memmove to memcpy because the addrs can not
overlap. Make some simple helper functions static __inline and remove one
of the explicit rn_search inlines.
OK mpi@
-rw-r--r-- | sys/net/radix.c | 137 |
1 files changed, 72 insertions, 65 deletions
diff --git a/sys/net/radix.c b/sys/net/radix.c index f9943c3be6f..5b6558da8f6 100644 --- a/sys/net/radix.c +++ b/sys/net/radix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: radix.c,v 1.36 2014/01/19 09:52:25 claudio Exp $ */ +/* $OpenBSD: radix.c,v 1.37 2014/01/20 22:42:06 claudio Exp $ */ /* $NetBSD: radix.c,v 1.20 2003/08/07 16:32:56 agc Exp $ */ /* @@ -60,15 +60,16 @@ struct pool rtmask_pool; /* pool for radix_mask structures */ #define rn_masktop (mask_rnhead->rnh_treetop) -static int rn_satisfies_leaf(char *, struct radix_node *, int); -static int rn_lexobetter(void *, void *); -static struct radix_mask *rn_new_radix_mask(struct radix_node *, +static inline int rn_satisfies_leaf(char *, struct radix_node *, int); +static inline int rn_lexobetter(void *, void *); +static inline struct radix_mask *rn_new_radix_mask(struct radix_node *, struct radix_mask *); struct radix_node *rn_insert(void *, struct radix_node_head *, int *, struct radix_node [2]); struct radix_node *rn_newpair(void *, int, struct radix_node[2]); -struct radix_node *rn_search(void *, struct radix_node *); + +static inline struct radix_node *rn_search(void *, struct radix_node *); struct radix_node *rn_search_m(void *, struct radix_node *, void *); /* @@ -105,13 +106,13 @@ struct radix_node *rn_search_m(void *, struct radix_node *, void *); * that governs a subtree. */ -struct radix_node * +static inline struct radix_node * rn_search(void *v_arg, struct radix_node *head) { struct radix_node *x; - caddr_t v; + caddr_t v = v_arg; - for (x = head, v = v_arg; x->rn_b >= 0;) { + for (x = head; x->rn_b >= 0;) { if (x->rn_bmask & v[x->rn_off]) x = x->rn_r; else @@ -124,7 +125,8 @@ struct radix_node * rn_search_m(void *v_arg, struct radix_node *head, void *m_arg) { struct radix_node *x; - caddr_t v = v_arg, m = m_arg; + caddr_t v = v_arg; + caddr_t m = m_arg; for (x = head; x->rn_b >= 0;) { if ((x->rn_bmask & m[x->rn_off]) && @@ -139,11 +141,14 @@ rn_search_m(void *v_arg, struct radix_node *head, void *m_arg) int rn_refines(void *m_arg, void *n_arg) { - caddr_t m = m_arg, n = n_arg; - caddr_t lim, lim2 = lim = n + *(u_char *)n; - int longer = (*(u_char *)n++) - (int)(*(u_char *)m++); + caddr_t m = m_arg; + caddr_t n = n_arg; + caddr_t lim, lim2; + int longer; int masks_are_equal = 1; + lim2 = lim = n + *(u_char *)n; + longer = (*(u_char *)n++) - (int)(*(u_char *)m++); if (longer > 0) lim -= longer; while (n < lim) { @@ -182,13 +187,16 @@ rn_lookup(void *v_arg, void *m_arg, struct radix_node_head *head) return x; } -static int +static inline int rn_satisfies_leaf(char *trial, struct radix_node *leaf, int skip) { - char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask; + char *cp = trial; + char *cp2 = leaf->rn_key; + char *cp3 = leaf->rn_mask; char *cplim; - int length = min(*(u_char *)cp, *(u_char *)cp2); + int length; + length = min(*(u_char *)cp, *(u_char *)cp2); if (cp3 == NULL) cp3 = rn_ones; else @@ -204,23 +212,14 @@ struct radix_node * rn_match(void *v_arg, struct radix_node_head *head) { caddr_t v = v_arg; - struct radix_node *t = head->rnh_treetop, *x; - caddr_t cp = v, cp2; - caddr_t cplim; - struct radix_node *saved_t, *top = t; - int off = t->rn_off, vlen = *(u_char *)cp, matched_off; + caddr_t cp, cp2, cplim; + struct radix_node *top = head->rnh_treetop; + struct radix_node *saved_t, *t; + int off = top->rn_off; + int vlen, matched_off; int test, b, rn_b; - /* - * Open code rn_search(v, top) to avoid overhead of extra - * subroutine call. - */ - for (; t->rn_b >= 0; ) { - if (t->rn_bmask & cp[t->rn_off]) - t = t->rn_r; - else - t = t->rn_l; - } + t = rn_search(v, top); /* * See if we match exactly as a host destination * or at least learn how many bits match, for normal mask finesse. @@ -234,7 +233,11 @@ rn_match(void *v_arg, struct radix_node_head *head) */ if (t->rn_mask) vlen = *(u_char *)t->rn_mask; - cp += off; cp2 = t->rn_key + off; cplim = v + vlen; + else + vlen = *(u_char *)v; + cp = v + off; + cp2 = t->rn_key + off; + cplim = v + vlen; for (; cp < cplim; cp++, cp2++) if (*cp != *cp2) goto on1; @@ -287,6 +290,7 @@ on1: if (rn_b <= m->rm_b) return (m->rm_leaf); } else { + struct radix_node *x; off = min(t->rn_off, matched_off); x = rn_search_m(v, t, m->rm_mask); while (x && x->rn_mask != m->rm_mask) @@ -298,7 +302,7 @@ on1: } while (m); } } while (t != top); - return 0; + return NULL; } #ifdef RN_DEBUG @@ -311,13 +315,13 @@ int rn_debug = 1; struct radix_node * rn_newpair(void *v, int b, struct radix_node nodes[2]) { - struct radix_node *tt = nodes, *t = tt + 1; + struct radix_node *tt = nodes, *t = nodes + 1; t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7); t->rn_l = tt; t->rn_off = b >> 3; tt->rn_b = -1; - tt->rn_key = (caddr_t)v; + tt->rn_key = v; tt->rn_p = t; tt->rn_flags = t->rn_flags = RNF_ACTIVE; #ifdef RN_DEBUG @@ -332,22 +336,26 @@ rn_newpair(void *v, int b, struct radix_node nodes[2]) struct radix_node * rn_insert(void *v_arg, struct radix_node_head *head, - int *dupentry, struct radix_node nodes[2]) + int *dupentry, struct radix_node nodes[2]) { caddr_t v = v_arg; struct radix_node *top = head->rnh_treetop; - int head_off = top->rn_off, vlen = (int)*((u_char *)v); - struct radix_node *t = rn_search(v_arg, top); - caddr_t cp = v + head_off; + struct radix_node *t, *tt; + int off = top->rn_off; int b; - struct radix_node *tt; + + t = rn_search(v_arg, top); /* * Find first bit at which v and t->rn_key differ */ { - caddr_t cp2 = t->rn_key + head_off; - int cmp_res; - caddr_t cplim = v + vlen; + caddr_t cp, cp2, cplim; + int vlen, cmp_res; + + vlen = *(u_char *)v; + cp = v + off; + cp2 = t->rn_key + off; + cplim = v + vlen; while (cp < cplim) if (*cp2++ != *cp++) @@ -362,14 +370,14 @@ on1: } { struct radix_node *p, *x = top; - cp = v; + caddr_t cp = v; do { p = x; if (cp[x->rn_off] & x->rn_bmask) x = x->rn_r; else x = x->rn_l; - } while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */ + } while (b > (unsigned int) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */ #ifdef RN_DEBUG if (rn_debug) log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p); @@ -399,12 +407,11 @@ on1: struct radix_node * rn_addmask(void *n_arg, int search, int skip) { - caddr_t netmask = (caddr_t)n_arg; - struct radix_node *x; + caddr_t netmask = n_arg; + struct radix_node *x, *saved_x; caddr_t cp, cplim; int b = 0, mlen, j; int maskduplicated, m0, isnormal; - struct radix_node *saved_x; static int last_zeroed = 0; if ((mlen = *(u_char *)netmask) > max_keylen) @@ -414,9 +421,9 @@ rn_addmask(void *n_arg, int search, int skip) if (mlen <= skip) return (mask_rnhead->rnh_nodes); if (skip > 1) - memmove(addmask_key + 1, rn_ones + 1, skip - 1); + memcpy(addmask_key + 1, rn_ones + 1, skip - 1); if ((m0 = mlen) > skip) - memmove(addmask_key + skip, netmask + skip, mlen - skip); + memcpy(addmask_key + skip, netmask + skip, mlen - skip); /* * Trim trailing zeroes. */ @@ -467,7 +474,8 @@ rn_addmask(void *n_arg, int search, int skip) return (x); } -static int /* XXX: arbitrary ordering for non-contiguous masks */ +/* rn_lexobetter: return a arbitrary ordering for non-contiguous masks */ +static inline int rn_lexobetter(void *m_arg, void *n_arg) { u_char *mp = m_arg, *np = n_arg; @@ -489,7 +497,7 @@ rn_lexobetter(void *m_arg, void *n_arg) return (memcmp(mp, np, *mp) > 0); } -static struct radix_mask * +static inline struct radix_mask * rn_new_radix_mask(struct radix_node *tt, struct radix_mask *next) { struct radix_mask *m; @@ -499,7 +507,6 @@ rn_new_radix_mask(struct radix_node *tt, struct radix_mask *next) log(LOG_ERR, "Mask for route not entered\n"); return (0); } - memset(m, 0, sizeof *m); m->rm_b = tt->rn_b; m->rm_flags = tt->rn_flags; if (tt->rn_flags & RNF_NORMAL) @@ -515,9 +522,11 @@ struct radix_node * rn_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, struct radix_node treenodes[2], u_int8_t prio) { - caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg; + caddr_t v = v_arg; + caddr_t netmask = n_arg; + struct radix_node *top = head->rnh_treetop; struct radix_node *t, *x = NULL, *tt; - struct radix_node *saved_tt, *top = head->rnh_treetop; + struct radix_node *saved_tt; short b = 0, b_leaf = 0; int keyduplicated, prioinv = -1; caddr_t mmask; @@ -750,28 +759,26 @@ struct radix_node * rn_delete(void *v_arg, void *netmask_arg, struct radix_node_head *head, struct radix_node *rn) { + caddr_t v = v_arg; + caddr_t netmask = netmask_arg; + struct radix_node *top = head->rnh_treetop; struct radix_node *t, *p, *x, *tt; struct radix_mask *m, *saved_m, **mp; - struct radix_node *dupedkey, *saved_tt, *top; - caddr_t v, netmask; - int b, head_off, vlen; + struct radix_node *dupedkey, *saved_tt; + int off = top->rn_off; + int b, vlen; - v = v_arg; - netmask = netmask_arg; - x = head->rnh_treetop; - tt = rn_search(v, x); - head_off = x->rn_off; vlen = *(u_char *)v; + tt = rn_search(v, top); saved_tt = tt; - top = x; if (tt == NULL || - memcmp(v + head_off, tt->rn_key + head_off, vlen - head_off)) + memcmp(v + off, tt->rn_key + off, vlen - off)) return (0); /* * Delete our route from mask lists. */ if (netmask) { - if ((x = rn_addmask(netmask, 1, head_off)) == NULL) + if ((x = rn_addmask(netmask, 1, off)) == NULL) return (0); netmask = x->rn_key; while (tt->rn_mask != netmask) |