Skip to content

Commit 16058ff

Browse files
Martin Kellyanakryiko
authored andcommitted
libbpf: Add ring__consume
Add ring__consume to consume a single ringbuffer, analogous to ring_buffer__consume. Signed-off-by: Martin Kelly <martin.kelly@crowdstrike.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20230925215045.2375758-14-martin.kelly@crowdstrike.com
1 parent 6e38ba5 commit 16058ff

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,16 @@ LIBBPF_API size_t ring__size(const struct ring *r);
13121312
*/
13131313
LIBBPF_API int ring__map_fd(const struct ring *r);
13141314

1315+
/**
1316+
* @brief **ring__consume()** consumes available ringbuffer data without event
1317+
* polling.
1318+
*
1319+
* @param r A ringbuffer object.
1320+
* @return The number of records consumed (or INT_MAX, whichever is less), or
1321+
* a negative number if any of the callbacks return an error.
1322+
*/
1323+
LIBBPF_API int ring__consume(struct ring *r);
1324+
13151325
struct user_ring_buffer_opts {
13161326
size_t sz; /* size of this struct, for forward/backward compatibility */
13171327
};

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 {
401401
bpf_program__attach_tcx;
402402
bpf_program__attach_uprobe_multi;
403403
ring__avail_data_size;
404+
ring__consume;
404405
ring__consumer_pos;
405406
ring__map_fd;
406407
ring__producer_pos;

tools/lib/bpf/ringbuf.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ int ring__map_fd(const struct ring *r)
371371
return r->map_fd;
372372
}
373373

374+
int ring__consume(struct ring *r)
375+
{
376+
int64_t res;
377+
378+
res = ringbuf_process_ring(r);
379+
if (res < 0)
380+
return libbpf_err(res);
381+
382+
return res > INT_MAX ? INT_MAX : res;
383+
}
384+
374385
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
375386
{
376387
if (rb->consumer_pos) {

0 commit comments

Comments
 (0)