bpf: support BPF_F_NO_PREALLOC in bpf_map_def.h

Kernel requires maps of type BPF_MAP_TYPE_LPM_TRIE to not be
preallocated, so default enable the flag for these.

But even for non LPM_TRIE maps allow a negative num_entries
value to ask for 'non preallocated' maps.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I48d5d5813628c87094c5efa7266b0354b135dd7e
This commit is contained in:
Maciej Żenczykowski 2025-01-20 14:38:02 -08:00
commit d2459f373c
2 changed files with 15 additions and 3 deletions

View file

@ -231,6 +231,14 @@ static void (*bpf_ringbuf_submit_unsafe)(const void* data, __u64 flags) = (void*
(ignore_userdebug).ignore_on_userdebug), \
"bpfloader min version must be >= 0.33 in order to use ignored_on");
#define ABSOLUTE(x) ((x) < 0 ? -(x) : (x))
#define DEFAULT_BPF_MAP_FLAGS(type, num_entries, mapflags) \
( (mapflags) | \
((num_entries) < 0 ? BPF_F_NO_PREALLOC : 0) | \
(type == BPF_MAP_TYPE_LPM_TRIE ? BPF_F_NO_PREALLOC : 0) \
)
#define DEFINE_BPF_MAP_BASE(the_map, TYPE, keysize, valuesize, num_entries, \
usr, grp, md, selinux, pindir, share, minkver, \
maxkver, minloader, maxloader, ignore_eng, \
@ -239,8 +247,8 @@ static void (*bpf_ringbuf_submit_unsafe)(const void* data, __u64 flags) = (void*
.type = BPF_MAP_TYPE_##TYPE, \
.key_size = (keysize), \
.value_size = (valuesize), \
.max_entries = (num_entries), \
.map_flags = (mapflags), \
.max_entries = ABSOLUTE(num_entries), \
.map_flags = DEFAULT_BPF_MAP_FLAGS(BPF_MAP_TYPE_##TYPE, num_entries, mapflags), \
.uid = (usr), \
.gid = (grp), \
.mode = (md), \

View file

@ -94,6 +94,10 @@ _Static_assert(__alignof__(enum bpf_map_type) == 4, "__alignof__ enum bpf_map_ty
_Static_assert(_Alignof(enum bpf_map_type) == 4, "_Alignof enum bpf_map_type != 4");
// Linux kernel requires sizeof(int) == 4, sizeof(void*) == sizeof(long), sizeof(long long) == 8
_Static_assert(sizeof(int) == 4, "sizeof int != 4");
_Static_assert(__alignof__(int) == 4, "__alignof__ int != 4");
_Static_assert(_Alignof(int) == 4, "_Alignof int != 4");
_Static_assert(sizeof(unsigned int) == 4, "sizeof unsigned int != 4");
_Static_assert(__alignof__(unsigned int) == 4, "__alignof__ unsigned int != 4");
_Static_assert(_Alignof(unsigned int) == 4, "_Alignof unsigned int != 4");
@ -155,7 +159,7 @@ struct bpf_map_def {
enum bpf_map_type type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
int max_entries; // negative means BPF_F_NO_PREALLOC, but *might* not work with S
unsigned int map_flags;
// The following are not supported by the Android bpfloader: