Skip to content

Commit 059a8c0

Browse files
Martin Kellyanakryiko
authored andcommitted
libbpf: Add ring__producer_pos, ring__consumer_pos
Add APIs to get the producer and consumer position for a given ringbuffer. 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-6-martin.kelly@crowdstrike.com
1 parent c1ad2e4 commit 059a8c0

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,24 @@ LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
12641264
LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
12651265
unsigned int idx);
12661266

1267+
/**
1268+
* @brief **ring__consumer_pos()** returns the current consumer position in the
1269+
* given ringbuffer.
1270+
*
1271+
* @param r A ringbuffer object.
1272+
* @return The current consumer position.
1273+
*/
1274+
LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
1275+
1276+
/**
1277+
* @brief **ring__producer_pos()** returns the current producer position in the
1278+
* given ringbuffer.
1279+
*
1280+
* @param r A ringbuffer object.
1281+
* @return The current producer position.
1282+
*/
1283+
LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
1284+
12671285
struct user_ring_buffer_opts {
12681286
size_t sz; /* size of this struct, for forward/backward compatibility */
12691287
};

tools/lib/bpf/libbpf.map

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,7 @@ LIBBPF_1.3.0 {
400400
bpf_program__attach_netfilter;
401401
bpf_program__attach_tcx;
402402
bpf_program__attach_uprobe_multi;
403+
ring__consumer_pos;
404+
ring__producer_pos;
403405
ring_buffer__ring;
404406
} LIBBPF_1.2.0;

tools/lib/bpf/ringbuf.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,20 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
338338
return rb->rings[idx];
339339
}
340340

341+
unsigned long ring__consumer_pos(const struct ring *r)
342+
{
343+
/* Synchronizes with smp_store_release() in ringbuf_process_ring(). */
344+
return smp_load_acquire(r->consumer_pos);
345+
}
346+
347+
unsigned long ring__producer_pos(const struct ring *r)
348+
{
349+
/* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in
350+
* the kernel.
351+
*/
352+
return smp_load_acquire(r->producer_pos);
353+
}
354+
341355
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
342356
{
343357
if (rb->consumer_pos) {

0 commit comments

Comments
 (0)