• David Vernet's avatar
    bpf: Add libbpf logic for user-space ring buffer · b66ccae0
    David Vernet authored
    Now that all of the logic is in place in the kernel to support user-space
    produced ring buffers, we can add the user-space logic to libbpf. This
    patch therefore adds the following public symbols to libbpf:
    
    struct user_ring_buffer *
    user_ring_buffer__new(int map_fd,
    		      const struct user_ring_buffer_opts *opts);
    void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
    void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
                                             __u32 size, int timeout_ms);
    void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
    void user_ring_buffer__discard(struct user_ring_buffer *rb,
    void user_ring_buffer__free(struct user_ring_buffer *rb);
    
    A user-space producer must first create a struct user_ring_buffer * object
    with user_ring_buffer__new(), and can then reserve samples in the
    ring buffer using one of the following two symbols:
    
    void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
    void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
                                             __u32 size, int timeout_ms);
    
    With user_ring_buffer__reserve(), a pointer to a 'size' region of the ring
    buffer will be returned if sufficient space is available in the buffer.
    user_ring_buffer__reserve_blocking() provides similar semantics, but will
    block for up to 'timeout_ms' in epoll_wait if there is insufficient space
    in the buffer. This function has the guarantee from the kernel that it will
    receive at least one event-notification per invocation to
    bpf_ringbuf_drain(), provided that at least one sample is drained, and the
    BPF program did not pass the BPF_RB_NO_WAKEUP flag to bpf_ringbuf_drain().
    
    Once a sample is reserved, it must either be committed to the ring buffer
    with user_ring_buffer__submit(), or discarded with
    user_ring_buffer__discard().
    Signed-off-by: default avatarDavid Vernet <void@manifault.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220920000100.477320-4-void@manifault.com
    b66ccae0
libbpf.c 326 KB