libbpf: Expose API to consume one ring at a time
PATCH v3 1/1 libbpf: Expose API to consume one ring at a time
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 <[email protected]>
---v1->v2: Added entry to libbpf.mapv2->v3: Correctly set errno and handle overflow
tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+)
tools/lib/bpf/libbpf.h +1 -0
@@ -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 {
tools/lib/bpf/libbpf.map +1 -0
@@ -398,4 +398,5 @@ LIBBPF_1.3.0 { bpf_prog_detach_opts; bpf_program__attach_netfilter; bpf_program__attach_tcx; + ring_buffer__consume_ring; } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +22 -0
@@ -290,6 +290,28 @@ 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; +int64_t res; + +if (ring_id >= rb->ring_cnt) +return libbpf_err (-EINVAL ); + + ring = &rb->rings[ring_id]; + res =ringbuf_process_ring (ring); +if (res <0 ) +return libbpf_error (res); + +if (res >INT_MAX ) +return INT_MAX ; + return res +} +/* 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.
Signature · 1 line
2.39.2