MINOR: ebtree: add ebmb_lookup_shorter() to pursue lookups

This function is designed to enlarge the scope of a lookup performed
by a caller via ebmb_lookup_longest() that was not satisfied with the
result. It will first visit next duplicates, and if none are found,
it will go up in the tree to visit similar keys with shorter prefixes
and will return them if they match. We only use the starting point's
value to perform the comparison since it was expected to be valid for
the looked up key, hence it has all bits in common with its own length.

The algorithm is a bit complex because when going up we may visit nodes
that are located beneath the level we just come from. However it is
guaranteed that keys having a shorter prefix will be present above the
current location, though they may be attached to the left branch of a
cover node, so we just visit all nodes as long as their prefix is too
large, possibly go down along the left branch on cover nodes, and stop
when either there's a match, or there's a non-matching prefix anymore.

The following tricky case now works fine and properly finds 10.0.0.0/7
when looking up 11.0.0.1 from tree version 1 though both belong to
different sub-trees:

  prepare map #1
    add map @1 #1 10.0.0.0/7 10.0.0.0/7
    add map @1 #1 10.0.0.0/7 10.0.0.0/7
  commit map @1 #1
  prepare map #1
    add map @2 #1 11.0.0.0/8 11.0.0.0/8
    add map @2 #1 11.0.0.0/8 11.0.0.0/8

  prepare map #1
    add map @1 #1 10.0.0.0/7 10.0.0.0/7
  commit map @1 #1
  prepare map #1
    add map @2 #1 10.0.0.0/7 10.0.0.0/7
    add map @2 #1 11.0.0.0/8 11.0.0.0/8
    add map @2 #1 11.0.0.0/8 11.0.0.0/8
This commit is contained in:
Willy Tarreau 2022-08-01 10:37:29 +02:00
parent 0dc9e6dca2
commit 81f3b80e32
1 changed files with 53 additions and 0 deletions

View File

@ -98,6 +98,59 @@ struct ebmb_node *ebmb_lookup_longest(struct eb_root *root, const void *x);
struct ebmb_node *ebmb_lookup_prefix(struct eb_root *root, const void *x, unsigned int pfx);
struct ebmb_node *ebmb_insert_prefix(struct eb_root *root, struct ebmb_node *new, unsigned int len);
/* start from a valid leaf and find the next matching prefix that's either a
* duplicate, or immediately shorter than the node's current one and still
* matches it. The purpose is to permit a caller that is not satisfied with a
* result provided by ebmb_lookup_longest() to evaluate the next matching
* entry. Given that shorter keys are necessarily attached to nodes located
* above the current one, it's sufficient to restart from the current leaf and
* go up until we find a shorter prefix, or a non-matching one.
*/
static inline struct ebmb_node *ebmb_lookup_shorter(struct ebmb_node *start)
{
eb_troot_t *t = start->node.leaf_p;
struct ebmb_node *node;
/* first, chcek for duplicates */
node = ebmb_next_dup(start);
if (node)
return node;
while (1) {
if (eb_gettag(t) == EB_LEFT) {
/* Walking up from left branch. We must ensure that we never
* walk beyond root.
*/
if (unlikely(eb_clrtag((eb_untag(t, EB_LEFT))->b[EB_RGHT]) == NULL))
return NULL;
node = container_of(eb_root_to_node(eb_untag(t, EB_LEFT)), struct ebmb_node, node);
} else {
/* Walking up from right branch, so we cannot be below
* root. However, if we end up on a node with an even
* and positive bit, this is a cover node, which mandates
* that the left branch only contains cover values, so we
* must descend it.
*/
node = container_of(eb_root_to_node(eb_untag(t, EB_RGHT)), struct ebmb_node, node);
if (node->node.bit > 0 && !(node->node.bit & 1))
return ebmb_entry(eb_walk_down(t, EB_LEFT), struct ebmb_node, node);
}
/* Note that <t> cannot be NULL at this stage */
t = node->node.node_p;
/* this is a node attached to a deeper (and possibly different)
* leaf, not interesting for us.
*/
if (node->node.pfx >= start->node.pfx)
continue;
if (check_bits(start->key, node->key, 0, node->node.pfx) == 0)
break;
}
return node;
}
/* The following functions are less likely to be used directly, because their
* code is larger. The non-inlined version is preferred.
*/