Commit 3271e8f3 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'BTF-map-in-map'

Andrii Nakryiko says:

====================
This patch set teaches libbpf how to declare and initialize ARRAY_OF_MAPS and
HASH_OF_MAPS maps. See patch #3 for all the details.

Patch #1 refactors parsing BTF definition of map to re-use it cleanly for
inner map definition parsing.

Patch #2 refactors map creation and destruction logic for reuse. It also fixes
existing bug with not closing successfully created maps when bpf_object map
creation overall fails.

Patch #3 adds support for an extension of BTF-defined map syntax, as well as
parsing, recording, and use of relocations to allow declaratively initialize
outer maps with references to inner maps.

v1->v2:
  - rename __inner to __array (Alexei).
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 1f427a80 646f02ff
......@@ -12,6 +12,7 @@
#define __uint(name, val) int (*name)[val]
#define __type(name, val) typeof(val) *name
#define __array(name, val) typeof(val) *name[]
/* Helper macro to print out debug messages */
#define bpf_printk(fmt, ...) \
......
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include <test_progs.h>
#include "test_btf_map_in_map.skel.h"
void test_btf_map_in_map(void)
{
int duration = 0, err, key = 0, val;
struct test_btf_map_in_map* skel;
skel = test_btf_map_in_map__open_and_load();
if (CHECK(!skel, "skel_open", "failed to open&load skeleton\n"))
return;
err = test_btf_map_in_map__attach(skel);
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
goto cleanup;
/* inner1 = input, inner2 = input + 1 */
val = bpf_map__fd(skel->maps.inner_map1);
bpf_map_update_elem(bpf_map__fd(skel->maps.outer_arr), &key, &val, 0);
val = bpf_map__fd(skel->maps.inner_map2);
bpf_map_update_elem(bpf_map__fd(skel->maps.outer_hash), &key, &val, 0);
skel->bss->input = 1;
usleep(1);
bpf_map_lookup_elem(bpf_map__fd(skel->maps.inner_map1), &key, &val);
CHECK(val != 1, "inner1", "got %d != exp %d\n", val, 1);
bpf_map_lookup_elem(bpf_map__fd(skel->maps.inner_map2), &key, &val);
CHECK(val != 2, "inner2", "got %d != exp %d\n", val, 2);
/* inner1 = input + 1, inner2 = input */
val = bpf_map__fd(skel->maps.inner_map2);
bpf_map_update_elem(bpf_map__fd(skel->maps.outer_arr), &key, &val, 0);
val = bpf_map__fd(skel->maps.inner_map1);
bpf_map_update_elem(bpf_map__fd(skel->maps.outer_hash), &key, &val, 0);
skel->bss->input = 3;
usleep(1);
bpf_map_lookup_elem(bpf_map__fd(skel->maps.inner_map1), &key, &val);
CHECK(val != 4, "inner1", "got %d != exp %d\n", val, 4);
bpf_map_lookup_elem(bpf_map__fd(skel->maps.inner_map2), &key, &val);
CHECK(val != 3, "inner2", "got %d != exp %d\n", val, 3);
cleanup:
test_btf_map_in_map__destroy(skel);
}
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2020 Facebook */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
struct inner_map {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} inner_map1 SEC(".maps"),
inner_map2 SEC(".maps");
struct outer_arr {
__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
__uint(max_entries, 3);
__uint(key_size, sizeof(int));
__uint(value_size, sizeof(int));
/* it's possible to use anonymous struct as inner map definition here */
__array(values, struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
/* changing max_entries to 2 will fail during load
* due to incompatibility with inner_map definition */
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
});
} outer_arr SEC(".maps") = {
/* (void *) cast is necessary because we didn't use `struct inner_map`
* in __inner(values, ...)
* Actually, a conscious effort is required to screw up initialization
* of inner map slots, which is a great thing!
*/
.values = { (void *)&inner_map1, 0, (void *)&inner_map2 },
};
struct outer_hash {
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
__uint(max_entries, 5);
__uint(key_size, sizeof(int));
/* Here everything works flawlessly due to reuse of struct inner_map
* and compiler will complain at the attempt to use non-inner_map
* references below. This is great experience.
*/
__array(values, struct inner_map);
} outer_hash SEC(".maps") = {
.values = {
[0] = &inner_map2,
[4] = &inner_map1,
},
};
int input = 0;
SEC("raw_tp/sys_enter")
int handle__sys_enter(void *ctx)
{
struct inner_map *inner_map;
int key = 0, val;
inner_map = bpf_map_lookup_elem(&outer_arr, &key);
if (!inner_map)
return 1;
val = input;
bpf_map_update_elem(inner_map, &key, &val, 0);
inner_map = bpf_map_lookup_elem(&outer_hash, &key);
if (!inner_map)
return 1;
val = input + 1;
bpf_map_update_elem(inner_map, &key, &val, 0);
return 0;
}
char _license[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment