Skip to content

Commit

Permalink
bpf: Add uniqueness invariant to trivial lpm test implementation
Browse files Browse the repository at this point in the history
The 'trivial' lpm implementation in this test allows equivalent nodes
to be added (that is, nodes consisting of the same prefix and prefix
length).  For lookup operations, this is fine because insertion happens
at the head of the (singly linked) list and the first, best match is
returned.  In order to support deletion, the tlpm data structue must
first enforce uniqueness.  This change modifies the insertion algorithm
to search for equivalent nodes and remove them.  Note: the
BPF_MAP_TYPE_LPM_TRIE already has a uniqueness invariant that is
implemented as node replacement.

Signed-off-by: Craig Gallek <kraig@google.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Craig Gallek authored and David S. Miller committed Sep 19, 2017
1 parent e454cf5 commit bae3046
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tools/testing/selftests/bpf/test_lpm_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,28 @@ struct tlpm_node {
uint8_t key[];
};

static struct tlpm_node *tlpm_match(struct tlpm_node *list,
const uint8_t *key,
size_t n_bits);

static struct tlpm_node *tlpm_add(struct tlpm_node *list,
const uint8_t *key,
size_t n_bits)
{
struct tlpm_node *node;
size_t n;

n = (n_bits + 7) / 8;

/* 'overwrite' an equivalent entry if one already exists */
node = tlpm_match(list, key, n_bits);
if (node && node->n_bits == n_bits) {
memcpy(node->key, key, n);
return list;
}

/* add new entry with @key/@n_bits to @list and return new head */

n = (n_bits + 7) / 8;
node = malloc(sizeof(*node) + n);
assert(node);

Expand Down

0 comments on commit bae3046

Please sign in to comment.