bpf: add longest prefix match map
Subsystems (5)
RFC PATCH 1/2 bpf: add a longest prefix match trie map implementation
In reply to Daniel MackThis trie implements a longest prefix match algorithm that can be used to match IP addresses to a stored set of ranges.
Internally, data is stored in an unbalanced trie of nodes that has a maximum height of n, where n is the prefixlen the trie was created with.
Tries may be created with prefix lengths that are multiples of 8, in the range from 8 to 2048. The key used for lookup and update operations is a struct bpf_lpm_trie_key, and the value is a uint64_t.
The code carries more information about the internal implementation.
--- include/uapi/linux/bpf.h | 7 + kernel/bpf/Makefile | 2 +- kernel/bpf/lpm_trie.c | 491 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/lpm_trie.c
include/uapi/linux/bpf.h +7 -0
@@ -63,6 +63,12 @@ struct bpf_insn {__s32 imm;/* signed immediate constant */ }; +/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ +struct bpf_lpm_trie_key { +__u32 prefixlen;/* up to 32 for AF_INET, 128 for AF_INET6 */ +__u8 data[0 ];/* Arbitrary size */ +}; +/* BPF syscall commands, see bpf(2) man-page for details. */ enum bpf_cmd {BPF_MAP_CREATE ,
@@ -89,6 +95,7 @@ enum bpf_map_type {BPF_MAP_TYPE_CGROUP_ARRAY ,BPF_MAP_TYPE_LRU_HASH ,BPF_MAP_TYPE_LRU_PERCPU_HASH , + BPF_MAP_TYPE_LPM_TRIE, }; enum bpf_prog_type {
kernel/bpf/Makefile +1 -1
@@ -1,7 +1,7 @@ obj-y := core.o obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o -obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o +obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o ifeq ($(CONFIG_PERF_EVENTS),y) obj-$(CONFIG_BPF_SYSCALL) += stackmap.o endif
kernel/bpf/lpm_trie.c new file +491 -0
491 lines not on this page · raw message
Signature · 1 line
2.9.3
cover bpf: add longest prefix match map
This patch set adds longest prefix match algorithm that can be used to match IP addresses to a stored set of ranges. It is exposed as a bpf map type.
Internally, data is stored in an unbalanced tree of nodes that has a maximum height of n, where n is the prefixlen the trie was created with.
Not that this has nothing to do with fib or fib6 and is in no way meant to replace or share code with it. It's rather a much simpler implementation that is specifically written with bpf maps in mind.
Patch 1/2 adds the implementation, and 2/2 an extensive test suite.
Feedback is much appreciated.
Thanks,
Daniel
Daniel Mack (1):
bpf: add a longest prefix match trie map implementation
David Herrmann (1):
bpf: Add tests for the lpm trie map
include/uapi/linux/bpf.h | 7 + kernel/bpf/Makefile | 2 +- kernel/bpf/lpm_trie.c | 491 +++++++++++++++++++++++++++++ tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/test_lpm_map.c | 348 ++++++++++++++++++++ 6 files changed, 850 insertions(+), 3 deletions(-) create mode 100644 kernel/bpf/lpm_trie.c create mode 100644 tools/testing/selftests/bpf/test_lpm_map.c
Signature · 1 line
2.9.3
RFC PATCH 2/2 bpf: Add tests for the lpm trie map
From: David Herrmann <[email protected]>
The first part of this program runs randomized tests against the lpm-bpf-map. It implements a "Trivial Longest Prefix Match" (tlpm) based on simple, linear, single linked lists. The implementation should be pretty straightforward.
Based on tlpm, this inserts randomized data into bpf-lpm-maps and verifies the trie-based bpf-map implementation behaves the same way as tlpm.
The second part uses 'real world' IPv4 and IPv6 addresses and tests the trie with those.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/test_lpm_map.c | 348 +++++++++++++++++++++++++++++ 3 files changed, 351 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_lpm_map.c
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -1,3 +1,4 @@ test_verifier test_maps test_lru_map +test_lpm_map
tools/testing/selftests/bpf/Makefile +2 -2
@@ -1,8 +1,8 @@ CFLAGS += -Wall -O2 -I../../../../usr/include -test_objs = test_verifier test_maps test_lru_map +test_objs = test_verifier test_maps test_lru_map test_lpm_map -TEST_PROGS := test_verifier test_maps test_lru_map test_kmod.sh +TEST_PROGS := test_verifier test_maps test_lru_map test_lpm_map test_kmod.sh TEST_FILES := $(test_objs) all: $(test_objs)
tools/testing/selftests/bpf/test_lpm_map.c new file +348 -0
348 lines not on this page · raw message
Signature · 1 line
2.9.3