summaryrefslogtreecommitdiff
path: root/tools/lib
diff options
context:
space:
mode:
authorMartin Kelly <martin.kelly@crowdstrike.com>2023-09-25 14:50:44 -0700
committerAndrii Nakryiko <andrii@kernel.org>2023-09-25 16:22:43 -0700
commit16058ff28b7eedd62f1643beb841e3bd611674fe (patch)
tree0b419a0d7d89786071693e3fac64782d61c25706 /tools/lib
parent6e38ba5291f9e082f9472a8ef682dc54cff0b3e4 (diff)
downloadlwn-16058ff28b7eedd62f1643beb841e3bd611674fe.tar.gz
lwn-16058ff28b7eedd62f1643beb841e3bd611674fe.zip
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
Diffstat (limited to 'tools/lib')
-rw-r--r--tools/lib/bpf/libbpf.h10
-rw-r--r--tools/lib/bpf/libbpf.map1
-rw-r--r--tools/lib/bpf/ringbuf.c11
3 files changed, 22 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 114e306c6507..475378438545 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1312,6 +1312,16 @@ LIBBPF_API size_t ring__size(const struct ring *r);
*/
LIBBPF_API int ring__map_fd(const struct ring *r);
+/**
+ * @brief **ring__consume()** consumes available ringbuffer data without event
+ * polling.
+ *
+ * @param r A ringbuffer object.
+ * @return The number of records consumed (or INT_MAX, whichever is less), or
+ * a negative number if any of the callbacks return an error.
+ */
+LIBBPF_API int ring__consume(struct ring *r);
+
struct user_ring_buffer_opts {
size_t sz; /* size of this struct, for forward/backward compatibility */
};
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 1b4225327ab6..cc973b678a39 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 {
bpf_program__attach_tcx;
bpf_program__attach_uprobe_multi;
ring__avail_data_size;
+ ring__consume;
ring__consumer_pos;
ring__map_fd;
ring__producer_pos;
diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index 8aec20216b7b..aacb64278a01 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -371,6 +371,17 @@ int ring__map_fd(const struct ring *r)
return r->map_fd;
}
+int ring__consume(struct ring *r)
+{
+ int64_t res;
+
+ res = ringbuf_process_ring(r);
+ if (res < 0)
+ return libbpf_err(res);
+
+ return res > INT_MAX ? INT_MAX : res;
+}
+
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
{
if (rb->consumer_pos) {