X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Frib%2Frte_rib6.c;h=042ac1f090bf57c12198862fbf50f0994a02b54d;hb=080c84cde42fd06443b02f495cd0397a490a1f71;hp=f6c55ee454bed75977c969aa6a751b16e6a286d1;hpb=99a2dd955fba6e4cc23b77d590a033650ced9c45;p=dpdk.git diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c index f6c55ee454..042ac1f090 100644 --- a/lib/rib/rte_rib6.c +++ b/lib/rib/rte_rib6.c @@ -6,12 +6,10 @@ #include #include -#include #include #include #include #include -#include #include #include @@ -79,20 +77,33 @@ is_covered(const uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE], static inline int get_dir(const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth) { - int i = 0; - uint8_t p_depth, msk; - - for (p_depth = depth; p_depth >= 8; p_depth -= 8) - i++; - - msk = 1 << (7 - p_depth); - return (ip[i] & msk) != 0; + uint8_t index, msk; + + /* + * depth & 127 clamps depth to values that will not + * read off the end of ip. + * depth is the number of bits deep into ip to traverse, and + * is incremented in blocks of 8 (1 byte). This means the last + * 3 bits are irrelevant to what the index of ip should be. + */ + index = (depth & INT8_MAX) / CHAR_BIT; + + /* + * msk is the bitmask used to extract the bit used to decide the + * direction of the next step of the binary search. + */ + msk = 1 << (7 - (depth & 7)); + + return (ip[index] & msk) != 0; } static inline struct rte_rib6_node * get_nxt_node(struct rte_rib6_node *node, const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]) { + if (node->depth == RIB6_MAXDEPTH) + return NULL; + return (get_dir(ip, node->depth)) ? node->right : node->left; } @@ -186,7 +197,7 @@ rte_rib6_lookup_exact(struct rte_rib6 *rib, } /* - * Traverses on subtree and retreeves more specific routes + * Traverses on subtree and retrieves more specific routes * for a given in args ip/depth prefix * last = NULL means the first invocation */