After reading man bpf
and a few other sources of documentation, I was under impression that a map
can be only created by user process. However the following small program seems to magically create bpf
map:
struct bpf_map_def SEC("maps") my_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = 10,
};
SEC("sockops")
int my_prog(struct bpf_sock_ops *skops)
{
u32 key = 1;
long *value;
...
value = bpf_map_lookup_elem(&my_map, &key);
...
return 1;
}
So I load the program with the kernel's tools/bpf/bpftool
and also verify that program is loaded:
$ bpftool prog show
1: sock_ops name my_prog tag f3a3583cdd82ae8d
loaded_at Jan 02/18:46 uid 0
xlated 728B not jited memlock 4096B
$ bpftool map show
1: array name my_map flags 0x0
key 4B value 8B max_entries 10 memlock 4096B
Of course the map is empty. However, removing bpf_map_lookup_elem
from the program results in no map being created.
UPDATE
I debugged it with strace
and found that in both cases, i.e. with bpf_map_lookup_elem
and without it, bpftool does invoke bpf(BPF_MAP_CREATE, ...)
and it apparently succeeds. Then, in case of bpf_map_lookup_elem left out, I strace on bpftool map show
, and bpf(BPF_MAP_GET_NEXT_ID, ..)
immediately returns ENOENT
, and it never gets to dump a map. So obviously something is not completing the map creation.
So I wonder if this is expected behavior?
Thanks.
umount /sys/fs/bpf/xxx
? Does this guarantee that the bpf blob will be removed from the memory? – Hesione