Skip to content

Commit b95a5c4

Browse files
zonquedavem330
authored andcommitted
bpf: add a longest prefix match trie map implementation
This 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. Signed-off-by: Daniel Mack <[email protected]> Reviewed-by: David Herrmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 10eeb5e commit b95a5c4

File tree

3 files changed

+511
-1
lines changed

3 files changed

+511
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ struct bpf_insn {
6363
__s32 imm; /* signed immediate constant */
6464
};
6565

66+
/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
67+
struct bpf_lpm_trie_key {
68+
__u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
69+
__u8 data[0]; /* Arbitrary size */
70+
};
71+
6672
/* BPF syscall commands, see bpf(2) man-page for details. */
6773
enum bpf_cmd {
6874
BPF_MAP_CREATE,
@@ -89,6 +95,7 @@ enum bpf_map_type {
8995
BPF_MAP_TYPE_CGROUP_ARRAY,
9096
BPF_MAP_TYPE_LRU_HASH,
9197
BPF_MAP_TYPE_LRU_PERCPU_HASH,
98+
BPF_MAP_TYPE_LPM_TRIE,
9299
};
93100

94101
enum bpf_prog_type {

kernel/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
obj-y := core.o
22

33
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o
4-
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o
4+
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o
55
ifeq ($(CONFIG_PERF_EVENTS),y)
66
obj-$(CONFIG_BPF_SYSCALL) += stackmap.o
77
endif

0 commit comments

Comments
 (0)