OSDN Git Service

libbpf: Support static initialization of BPF_MAP_TYPE_PROG_ARRAY
authorHengqi Chen <hengqi.chen@gmail.com>
Sun, 28 Nov 2021 14:16:32 +0000 (22:16 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 29 Nov 2021 06:24:52 +0000 (22:24 -0800)
commit341ac5ffc4bd859103899c876902caf07cc97ea4
treeec45526551ecc13ef65cf53b47e396e0caca3c97
parente32cb12ff52a2840fc1248998717f7b95c42f064
libbpf: Support static initialization of BPF_MAP_TYPE_PROG_ARRAY

Support static initialization of BPF_MAP_TYPE_PROG_ARRAY with a
syntax similar to map-in-map initialization ([0]):

    SEC("socket")
    int tailcall_1(void *ctx)
    {
        return 0;
    }

    struct {
        __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
        __uint(max_entries, 2);
        __uint(key_size, sizeof(__u32));
        __array(values, int (void *));
    } prog_array_init SEC(".maps") = {
        .values = {
            [1] = (void *)&tailcall_1,
        },
    };

Here's the relevant part of libbpf debug log showing what's
going on with prog-array initialization:

libbpf: sec '.relsocket': collecting relocation for section(3) 'socket'
libbpf: sec '.relsocket': relo #0: insn #2 against 'prog_array_init'
libbpf: prog 'entry': found map 0 (prog_array_init, sec 4, off 0) for insn #0
libbpf: .maps relo #0: for 3 value 0 rel->r_offset 32 name 53 ('tailcall_1')
libbpf: .maps relo #0: map 'prog_array_init' slot [1] points to prog 'tailcall_1'
libbpf: map 'prog_array_init': created successfully, fd=5
libbpf: map 'prog_array_init': slot [1] set to prog 'tailcall_1' fd=6

  [0] Closes: https://github.com/libbpf/libbpf/issues/354

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211128141633.502339-2-hengqi.chen@gmail.com
tools/lib/bpf/libbpf.c