Re: [PATCH bpf-next v1] bpf, docs: document BPF_MAP_TYPE_ARRAY_OF_MAPS and *_HASH_OF_MAPS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> writes:

> On Mon, Oct 10, 2022 at 4:32 AM Donald Hunter <donald.hunter@xxxxxxxxx> wrote:
>>
>> Add documentation for the ARRAY_OF_MAPS and HASH_OF_MAPS map types,
>> including usage and examples.
>>
>> Signed-off-by: Donald Hunter <donald.hunter@xxxxxxxxx>
>> ---
>
> subject suggestion (as it's pretty long):
>
> bpf, docs: document BPF_MAP_TYPE_{ARRAY,HASH}_OF_MAPS

Thanks for the tip. Hopefully already resolved well enough in v2+.

>> +Examples
>> +========
>> +
>> +Kernel BPF
>> +----------
>> +
>> +This snippet shows how to create an array of devmaps in a BPF program. Note that
>> +the outer array can only be modified from user space using the syscall API.
>> +
>> +.. code-block:: c
>> +
>> +    struct redirect_map {
>> +            __uint(type, BPF_MAP_TYPE_DEVMAP);
>> +            __uint(max_entries, 32);
>> +            __type(key, enum skb_drop_reason);
>> +            __type(value, __u64);
>> +    } redirect_map SEC(".maps");
>> +
>> +    struct {
>> +            __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
>> +            __uint(max_entries, 2);
>> +            __uint(key_size, sizeof(int));
>> +            __uint(value_size, sizeof(int));
>> +            __array(values, struct redirect_map);
>> +    } outer_map SEC(".maps");
>> +
>
> Let's also demonstrate libbpf's declarative way to initialize entries
> in outer map? See progs/test_btf_map_in_map.c under selftests/bpf for
> various examples.

Will do, thanks!

>> +This snippet shows how to lookup an outer map to retrieve an inner map.
>> +
>> +.. code-block:: c
>> +
>> +    SEC("xdp")
>> +    int redirect_by_priority(struct xdp_md *ctx) {
>> +            struct bpf_map *devmap;
>> +            int action = XDP_PASS;
>> +            int index = 0;
>> +
>> +            devmap = bpf_map_lookup_elem(&outer_arr, &index);
>> +            if (!devmap)
>> +                    return XDP_PASS;
>> +
>> +            /* use inner devmap here */
>> +
>> +            return action;
>> +    }
>> +
>> +User Space
>> +----------
>> +
>> +This snippet shows how to create an array based outer map:
>> +
>> +.. code-block:: c
>> +
>> +    int create_outer_array(int inner_fd) {
>> +            int fd;
>> +            LIBBPF_OPTS(bpf_map_create_opts, opts);
>> +            opts.inner_map_fd = inner_fd;
>
> LIBBPF_OPTS(bpf_map_create_opts, opts, .inner_map_fd = inner_fd);

+1, thanks.



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux