Skip to content

Commit 1c97f6a

Browse files
Martin Kellyanakryiko
authored andcommitted
libbpf: Add ring_buffer__ring
Add a new function ring_buffer__ring, which exposes struct ring * to the user, representing a single 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-4-martin.kelly@crowdstrike.com
1 parent ef3b820 commit 1c97f6a

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
12291229

12301230
/* Ring buffer APIs */
12311231
struct ring_buffer;
1232+
struct ring;
12321233
struct user_ring_buffer;
12331234

12341235
typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
@@ -1249,6 +1250,20 @@ LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
12491250
LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
12501251
LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
12511252

1253+
/**
1254+
* @brief **ring_buffer__ring()** returns the ringbuffer object inside a given
1255+
* ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.
1256+
*
1257+
* @param rb A ringbuffer manager object.
1258+
* @param idx An index into the ringbuffers contained within the ringbuffer
1259+
* manager object. The index is 0-based and corresponds to the order in which
1260+
* ring_buffer__add was called.
1261+
* @return A ringbuffer object on success; NULL and errno set if the index is
1262+
* invalid.
1263+
*/
1264+
LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
1265+
unsigned int idx);
1266+
12521267
struct user_ring_buffer_opts {
12531268
size_t sz; /* size of this struct, for forward/backward compatibility */
12541269
};

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,5 @@ LIBBPF_1.3.0 {
400400
bpf_program__attach_netfilter;
401401
bpf_program__attach_tcx;
402402
bpf_program__attach_uprobe_multi;
403+
ring_buffer__ring;
403404
} LIBBPF_1.2.0;

tools/lib/bpf/ringbuf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ int ring_buffer__epoll_fd(const struct ring_buffer *rb)
330330
return rb->epoll_fd;
331331
}
332332

333+
struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
334+
{
335+
if (idx >= rb->ring_cnt)
336+
return errno = ERANGE, NULL;
337+
338+
return rb->rings[idx];
339+
}
340+
333341
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
334342
{
335343
if (rb->consumer_pos) {

0 commit comments

Comments
 (0)