bpf(): allow creating nested maps (#3968)

* bpf(): allow creating nested maps

Add support for creating nested maps via the bpf() syscall wrapper.

Updates #3729

* Address feedback from shpalani
This commit is contained in:
Lorenz Bauer 2024-11-01 22:59:38 +00:00 коммит произвёл GitHub
Родитель 2bdec103d7
Коммит c26e4a0341
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 66 добавлений и 2 удалений

Просмотреть файл

@ -92,7 +92,8 @@ typedef struct
uint32_t key_size; ///< Size in bytes of keys.
uint32_t value_size; ///< Size in bytes of values.
uint32_t max_entries; ///< Maximum number of entries in the map.
uint32_t map_flags; ///< Flags (currently 0).
uint32_t map_flags; ///< Not supported, must be zero.
uint32_t inner_map_fd; ///< File descriptor of inner map.
} sys_bpf_map_create_attr_t;
typedef struct

Просмотреть файл

@ -93,7 +93,12 @@ bpf(int cmd, union bpf_attr* attr, unsigned int size)
}
case BPF_MAP_CREATE: {
ExtensibleStruct<sys_bpf_map_create_attr_t> map_create_attr((void*)attr, (size_t)size);
struct bpf_map_create_opts opts = {.map_flags = map_create_attr->map_flags};
struct bpf_map_create_opts opts = {
.inner_map_fd = map_create_attr->inner_map_fd,
.map_flags = map_create_attr->map_flags,
};
return bpf_map_create(
map_create_attr->map_type,
nullptr,

Просмотреть файл

@ -3006,6 +3006,64 @@ TEST_CASE("BPF_PROG_BIND_MAP etc.", "[libbpf]")
attr.prog_bind_map.flags = 0;
REQUIRE(bpf(BPF_PROG_BIND_MAP, &attr, sizeof(attr)) == 0);
// Verify we can create a nested array map.
uint32_t key = 0;
fd_t value = map_fd;
attr.map_create = {};
attr.map_create.map_type = BPF_MAP_TYPE_ARRAY_OF_MAPS;
attr.map_create.key_size = sizeof(key);
attr.map_create.value_size = sizeof(value);
attr.map_create.max_entries = 1;
attr.map_create.inner_map_fd = map_fd;
int nested_map_fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr.map_create));
REQUIRE(nested_map_fd >= 0);
// Ensure we can insert map_fd into the outer map.
attr.map_update = {};
attr.map_update.map_fd = nested_map_fd;
attr.map_update.key = (uintptr_t)&key;
attr.map_update.value = (uintptr_t)&value;
REQUIRE(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr.map_update)) == 0);
// Ensure that looking up the same key gives the ID of the inner map.
ebpf_id_t value_id = 0;
attr.map_lookup = {};
attr.map_lookup.map_fd = nested_map_fd;
attr.map_lookup.key = (uintptr_t)&key;
attr.map_lookup.value = (uintptr_t)&value_id;
REQUIRE(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr.map_lookup)) == 0);
REQUIRE(value_id == map_id);
Platform::_close(nested_map_fd);
// Verify we can create a nested hash map.
attr.map_create = {};
attr.map_create.map_type = BPF_MAP_TYPE_HASH_OF_MAPS;
attr.map_create.key_size = sizeof(key);
attr.map_create.value_size = sizeof(value);
attr.map_create.max_entries = 1;
attr.map_create.inner_map_fd = map_fd;
nested_map_fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr.map_create));
REQUIRE(nested_map_fd >= 0);
// Ensure we can insert map_fd into the outer map.
attr.map_update = {};
attr.map_update.map_fd = nested_map_fd;
attr.map_update.key = (uintptr_t)&key;
attr.map_update.value = (uintptr_t)&value;
REQUIRE(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr.map_update)) == 0);
// Ensure that looking up the same key gives the ID of the inner map.
value_id = 0;
attr.map_lookup = {};
attr.map_lookup.map_fd = nested_map_fd;
attr.map_lookup.key = (uintptr_t)&key;
attr.map_lookup.value = (uintptr_t)&value_id;
REQUIRE(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr.map_lookup)) == 0);
REQUIRE(value_id == map_id);
Platform::_close(nested_map_fd);
// Release our own references on the map and program.
Platform::_close(map_fd);
Platform::_close(program_fd);