-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: Add bloom filter map implementation
This patch adds the kernel-side changes for the implementation of a bpf bloom filter map. The bloom filter map supports peek (determining whether an element is present in the map) and push (adding an element to the map) operations.These operations are exposed to userspace applications through the already existing syscalls in the following way: BPF_MAP_LOOKUP_ELEM -> peek BPF_MAP_UPDATE_ELEM -> push The bloom filter map does not have keys, only values. In light of this, the bloom filter map's API matches that of queue stack maps: user applications use BPF_MAP_LOOKUP_ELEM/BPF_MAP_UPDATE_ELEM which correspond internally to bpf_map_peek_elem/bpf_map_push_elem, and bpf programs must use the bpf_map_peek_elem and bpf_map_push_elem APIs to query or add an element to the bloom filter map. When the bloom filter map is created, it must be created with a key_size of 0. For updates, the user will pass in the element to add to the map as the value, with a NULL key. For lookups, the user will pass in the element to query in the map as the value, with a NULL key. In the verifier layer, this requires us to modify the argument type of a bloom filter's BPF_FUNC_map_peek_elem call to ARG_PTR_TO_MAP_VALUE; as well, in the syscall layer, we need to copy over the user value so that in bpf_map_peek_elem, we know which specific value to query. A few things to please take note of: * If there are any concurrent lookups + updates, the user is responsible for synchronizing this to ensure no false negative lookups occur. * The number of hashes to use for the bloom filter is configurable from userspace. If no number is specified, the default used will be 5 hash functions. The benchmarks later in this patchset can help compare the performance of using different number of hashes on different entry sizes. In general, using more hashes decreases both the false positive rate and the speed of a lookup. * Deleting an element in the bloom filter map is not supported. * The bloom filter map may be used as an inner map. * The "max_entries" size that is specified at map creation time is used to approximate a reasonable bitmap size for the bloom filter, and is not otherwise strictly enforced. If the user wishes to insert more entries into the bloom filter than "max_entries", they may do so but they should be aware that this may lead to a higher false positive rate. Signed-off-by: Joanne Koong <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
Showing
8 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
|
||
#include <linux/bitmap.h> | ||
#include <linux/bpf.h> | ||
#include <linux/btf.h> | ||
#include <linux/err.h> | ||
#include <linux/jhash.h> | ||
#include <linux/random.h> | ||
|
||
#define BLOOM_CREATE_FLAG_MASK \ | ||
(BPF_F_NUMA_NODE | BPF_F_ZERO_SEED | BPF_F_ACCESS_MASK) | ||
|
||
struct bpf_bloom_filter { | ||
struct bpf_map map; | ||
u32 bitset_mask; | ||
u32 hash_seed; | ||
/* If the size of the values in the bloom filter is u32 aligned, | ||
* then it is more performant to use jhash2 as the underlying hash | ||
* function, else we use jhash. This tracks the number of u32s | ||
* in an u32-aligned value size. If the value size is not u32 aligned, | ||
* this will be 0. | ||
*/ | ||
u32 aligned_u32_count; | ||
u32 nr_hash_funcs; | ||
unsigned long bitset[]; | ||
}; | ||
|
||
static u32 hash(struct bpf_bloom_filter *bloom, void *value, | ||
u32 value_size, u32 index) | ||
{ | ||
u32 h; | ||
|
||
if (bloom->aligned_u32_count) | ||
h = jhash2(value, bloom->aligned_u32_count, | ||
bloom->hash_seed + index); | ||
else | ||
h = jhash(value, value_size, bloom->hash_seed + index); | ||
|
||
return h & bloom->bitset_mask; | ||
} | ||
|
||
static int peek_elem(struct bpf_map *map, void *value) | ||
{ | ||
struct bpf_bloom_filter *bloom = | ||
container_of(map, struct bpf_bloom_filter, map); | ||
u32 i, h; | ||
|
||
for (i = 0; i < bloom->nr_hash_funcs; i++) { | ||
h = hash(bloom, value, map->value_size, i); | ||
if (!test_bit(h, bloom->bitset)) | ||
return -ENOENT; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int push_elem(struct bpf_map *map, void *value, u64 flags) | ||
{ | ||
struct bpf_bloom_filter *bloom = | ||
container_of(map, struct bpf_bloom_filter, map); | ||
u32 i, h; | ||
|
||
if (flags != BPF_ANY) | ||
return -EINVAL; | ||
|
||
for (i = 0; i < bloom->nr_hash_funcs; i++) { | ||
h = hash(bloom, value, map->value_size, i); | ||
set_bit(h, bloom->bitset); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int pop_elem(struct bpf_map *map, void *value) | ||
{ | ||
return -EOPNOTSUPP; | ||
} | ||
|
||
static struct bpf_map *map_alloc(union bpf_attr *attr) | ||
{ | ||
u32 bitset_bytes, bitset_mask, nr_hash_funcs, nr_bits; | ||
int numa_node = bpf_map_attr_numa_node(attr); | ||
struct bpf_bloom_filter *bloom; | ||
|
||
if (!bpf_capable()) | ||
return ERR_PTR(-EPERM); | ||
|
||
if (attr->key_size != 0 || attr->value_size == 0 || | ||
attr->max_entries == 0 || | ||
attr->map_flags & ~BLOOM_CREATE_FLAG_MASK || | ||
!bpf_map_flags_access_ok(attr->map_flags) || | ||
(attr->map_extra & ~0xF)) | ||
return ERR_PTR(-EINVAL); | ||
|
||
/* The lower 4 bits of map_extra specify the number of hash functions */ | ||
nr_hash_funcs = attr->map_extra & 0xF; | ||
if (nr_hash_funcs == 0) | ||
/* Default to using 5 hash functions if unspecified */ | ||
nr_hash_funcs = 5; | ||
|
||
/* For the bloom filter, the optimal bit array size that minimizes the | ||
* false positive probability is n * k / ln(2) where n is the number of | ||
* expected entries in the bloom filter and k is the number of hash | ||
* functions. We use 7 / 5 to approximate 1 / ln(2). | ||
* | ||
* We round this up to the nearest power of two to enable more efficient | ||
* hashing using bitmasks. The bitmask will be the bit array size - 1. | ||
* | ||
* If this overflows a u32, the bit array size will have 2^32 (4 | ||
* GB) bits. | ||
*/ | ||
if (check_mul_overflow(attr->max_entries, nr_hash_funcs, &nr_bits) || | ||
check_mul_overflow(nr_bits / 5, (u32)7, &nr_bits) || | ||
nr_bits > (1UL << 31)) { | ||
/* The bit array size is 2^32 bits but to avoid overflowing the | ||
* u32, we use U32_MAX, which will round up to the equivalent | ||
* number of bytes | ||
*/ | ||
bitset_bytes = BITS_TO_BYTES(U32_MAX); | ||
bitset_mask = U32_MAX; | ||
} else { | ||
if (nr_bits <= BITS_PER_LONG) | ||
nr_bits = BITS_PER_LONG; | ||
else | ||
nr_bits = roundup_pow_of_two(nr_bits); | ||
bitset_bytes = BITS_TO_BYTES(nr_bits); | ||
bitset_mask = nr_bits - 1; | ||
} | ||
|
||
bitset_bytes = roundup(bitset_bytes, sizeof(unsigned long)); | ||
bloom = bpf_map_area_alloc(sizeof(*bloom) + bitset_bytes, numa_node); | ||
|
||
if (!bloom) | ||
return ERR_PTR(-ENOMEM); | ||
|
||
bpf_map_init_from_attr(&bloom->map, attr); | ||
|
||
bloom->nr_hash_funcs = nr_hash_funcs; | ||
bloom->bitset_mask = bitset_mask; | ||
|
||
/* Check whether the value size is u32-aligned */ | ||
if ((attr->value_size & (sizeof(u32) - 1)) == 0) | ||
bloom->aligned_u32_count = | ||
attr->value_size / sizeof(u32); | ||
|
||
if (!(attr->map_flags & BPF_F_ZERO_SEED)) | ||
bloom->hash_seed = get_random_int(); | ||
|
||
return &bloom->map; | ||
} | ||
|
||
static void map_free(struct bpf_map *map) | ||
{ | ||
struct bpf_bloom_filter *bloom = | ||
container_of(map, struct bpf_bloom_filter, map); | ||
|
||
bpf_map_area_free(bloom); | ||
} | ||
|
||
static void *lookup_elem(struct bpf_map *map, void *key) | ||
{ | ||
/* The eBPF program should use map_peek_elem instead */ | ||
return ERR_PTR(-EINVAL); | ||
} | ||
|
||
static int update_elem(struct bpf_map *map, void *key, | ||
void *value, u64 flags) | ||
{ | ||
/* The eBPF program should use map_push_elem instead */ | ||
return -EINVAL; | ||
} | ||
|
||
static int check_btf(const struct bpf_map *map, const struct btf *btf, | ||
const struct btf_type *key_type, | ||
const struct btf_type *value_type) | ||
{ | ||
/* Bloom filter maps are keyless */ | ||
return btf_type_is_void(key_type) ? 0 : -EINVAL; | ||
} | ||
|
||
static int bpf_bloom_btf_id; | ||
const struct bpf_map_ops bloom_filter_map_ops = { | ||
.map_meta_equal = bpf_map_meta_equal, | ||
.map_alloc = map_alloc, | ||
.map_free = map_free, | ||
.map_push_elem = push_elem, | ||
.map_peek_elem = peek_elem, | ||
.map_pop_elem = pop_elem, | ||
.map_lookup_elem = lookup_elem, | ||
.map_update_elem = update_elem, | ||
.map_check_btf = check_btf, | ||
.map_btf_name = "bpf_bloom_filter", | ||
.map_btf_id = &bpf_bloom_btf_id, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.