libbpf: Add ring__consume_n / ring_buffer__consume_n
authorAndrea Righi <andrea.righi@canonical.com>
Sat, 6 Apr 2024 09:15:43 +0000 (11:15 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Sat, 6 Apr 2024 16:11:55 +0000 (09:11 -0700)
Introduce a new API to consume items from a ring buffer, limited to a
specified amount, and return to the caller the actual number of items
consumed.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/lkml/20240310154726.734289-1-andrea.righi@canonical.com/T
Link: https://lore.kernel.org/bpf/20240406092005.92399-4-andrea.righi@canonical.com
tools/lib/bpf/libbpf.h
tools/lib/bpf/libbpf.map
tools/lib/bpf/ringbuf.c

index f88ab50c02299b0af8f162cf2309f99e571771f9..4f775a6dcaa0076010a0493027a1a3ddfccb363b 100644 (file)
@@ -1293,6 +1293,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_n(struct ring_buffer *rb, size_t n);
 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
 
 /**
@@ -1367,6 +1368,17 @@ LIBBPF_API int ring__map_fd(const struct ring *r);
  */
 LIBBPF_API int ring__consume(struct ring *r);
 
+/**
+ * @brief **ring__consume_n()** consumes up to a requested amount of items from
+ * a ringbuffer without event polling.
+ *
+ * @param r A ringbuffer object.
+ * @param n Maximum amount of items to consume.
+ * @return The number of items consumed, or a negative number if any of the
+ * callbacks return an error.
+ */
+LIBBPF_API int ring__consume_n(struct ring *r, size_t n);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
index 5dd81a7b96b504cab653ff7f21036e180e09cbff..23d82bba021a396e87fbc6053a12b7925762d207 100644 (file)
@@ -418,4 +418,7 @@ LIBBPF_1.4.0 {
 } LIBBPF_1.3.0;
 
 LIBBPF_1.5.0 {
+       global:
+               ring__consume_n;
+               ring_buffer__consume_n;
 } LIBBPF_1.4.0;
index db05e6f526de751c1c69dedf513b937c83ac66d8..99e44cf02321209cc5e887389c49445fd3c2289b 100644 (file)
@@ -277,6 +277,33 @@ done:
        return cnt;
 }
 
+/* Consume available ring buffer(s) data without event polling, up to n
+ * records.
+ *
+ * Returns number of records consumed across all registered ring buffers (or
+ * n, whichever is less), or negative number if any of the callbacks return
+ * error.
+ */
+int ring_buffer__consume_n(struct ring_buffer *rb, size_t n)
+{
+       int64_t err, res = 0;
+       int i;
+
+       for (i = 0; i < rb->ring_cnt; i++) {
+               struct ring *ring = rb->rings[i];
+
+               err = ringbuf_process_ring(ring, n);
+               if (err < 0)
+                       return libbpf_err(err);
+               res += err;
+               n -= err;
+
+               if (n == 0)
+                       break;
+       }
+       return res;
+}
+
 /* Consume available ring buffer(s) data without event polling.
  * Returns number of records consumed across all registered ring buffers (or
  * INT_MAX, whichever is less), or negative number if any of the callbacks
@@ -376,17 +403,22 @@ int ring__map_fd(const struct ring *r)
        return r->map_fd;
 }
 
-int ring__consume(struct ring *r)
+int ring__consume_n(struct ring *r, size_t n)
 {
-       int64_t res;
+       int res;
 
-       res = ringbuf_process_ring(r, INT_MAX);
+       res = ringbuf_process_ring(r, n);
        if (res < 0)
                return libbpf_err(res);
 
        return res > INT_MAX ? INT_MAX : res;
 }
 
+int ring__consume(struct ring *r)
+{
+       return ring__consume_n(r, INT_MAX);
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {