Skip to content

Commit

Permalink
libbpf: Expose API to consume one ring at a time
Browse files Browse the repository at this point in the history
We already provide ring_buffer__epoll_fd to enable use of external
polling systems. However, the only API available to consume the ring
buffer is ring_buffer__consume, which always checks all rings. When
polling for many events, this can be wasteful.

Signed-off-by: Adam Sindelar <adam@wowsignal.io>
  • Loading branch information
the80srobot authored and Kernel Patches Daemon committed Jul 25, 2023
1 parent 62a607a commit 1358a19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,7 @@ LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
ring_buffer_sample_fn sample_cb, void *ctx);
LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
LIBBPF_API int ring_buffer__consume_ring(struct ring_buffer *rb, uint32_t ring_id);
LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);

struct user_ring_buffer_opts {
Expand Down
15 changes: 15 additions & 0 deletions tools/lib/bpf/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@ int ring_buffer__consume(struct ring_buffer *rb)
return res;
}

/* Consume available data from a single RINGBUF map identified by its ID.
* The ring ID is returned in epoll_data by epoll_wait when called with
* ring_buffer__epoll_fd.
*/
int ring_buffer__consume_ring(struct ring_buffer *rb, uint32_t ring_id)
{
struct ring *ring;

if (ring_id >= rb->ring_cnt)
return libbpf_err(-EINVAL);

ring = &rb->rings[ring_id];
return ringbuf_process_ring(ring);
}

/* Poll for available data and consume records, if any are available.
* Returns number of records consumed (or INT_MAX, whichever is less), or
* negative number, if any of the registered callbacks returned error.
Expand Down

0 comments on commit 1358a19

Please sign in to comment.