Sunjal
Sunjal

Reputation: 11

How to set the size of ring buffer in user space

In the ebpf program,I want to set the size of the Ring buffer in user space. Is there any way to achieve this? Even reloading the ebpf program is possible.

Upvotes: 0

Views: 57

Answers (1)

Farbod Shahinfar
Farbod Shahinfar

Reputation: 814

I do not think that you can change the size of a MAP at runtime. For achieving this you should reload the program. During reloading you can use the bpf_map__set_max_entries helper function fromlibbpf to set the maximum number of entries of a map. Below is an example code snippet showing how possibly the user-space program loading the BPF program could look like.

Loader program:

int main()
{
    int ret;
    struct bpf_object *bpfobj;
    struct bpf_map *map_obj;
    char *bpf_program_binary = "bpf_prog.o";
    bpfobj = bpf_object__open_file(bpf_program_binary, NULL);
    if (!bpfobj)
        return EXIT_FAILURE;
    char *name_of_the_map = "my_ring_map";
    map_obj = bpf_object__find_map_by_name(bpfobj, name_of_the_map);
    if (!map_obj)
        return EXIT_FAILURE;

    /* Changing the size of the Ring */
    uint32_t new_size = 2048 * 4096;
    ret = bpf_map__set_max_entries(map_obj, new_size);
    if (ret != 0) {
        perror("failed to set max entries:");
        return EXIT_FAILURE;
    }

    ret = bpf_object__load(bpfobj);
    if(ret != 0)
        return EXIT_FAILURE;
    /* rest of the loader program ... */
}

Upvotes: 1

Related Questions