Skip to contentSkip to conversation

add libbpf getters for individual ringbuffers

PATCH Superseded Martin Kelly 2 participants 24 messages Sep 14, 2023 – Sep 21, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover add libbpf getters for individual ringbuffers

This patch series adds a new ring__ API to libbpf exposing getters for accessing the individual ringbuffers inside a struct ring_buffer. This is useful for polling individually, getting available data, or similar use cases. The API looks like this, and was roughly proposed by Andrii Nakryiko in another thread:

Getting a ring struct:
struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx);

Using the ring struct:
unsigned long ring__consumer_pos(const struct ring *r); unsigned long ring__producer_pos(const struct ring *r); size_t ring__avail_data_size(const struct ring *r); size_t ring__size(const struct ring *r);
int ring__map_fd(const struct ring *r);
int ring__consume(struct ring *r);

Martin Kelly (14):
libbpf: refactor cleanup in ring_buffer__add
libbpf: switch rings to array of pointers
libbpf: add ring_buffer__ring selftests/bpf: add tests for ring_buffer__ring
libbpf: add ring__producer_pos, ring__consumer_pos selftests/bpf: add tests for ring__*_pos
libbpf: add ring__avail_data_size selftests/bpf: add tests for ring__avail_data_size
libbpf: add ring__size
selftests/bpf: add tests for ring__size
libbpf: add ring__map_fd
selftests/bpf: add tests for ring__map_fd
libbpf: add ring__consume
selftests/bpf: add tests for ring__consume

 tools/lib/bpf/libbpf.h                        | 68 +++++++++++++++
 tools/lib/bpf/libbpf.map                      |  7 ++
 tools/lib/bpf/ringbuf.c                       | 87 +++++++++++++++----
 .../selftests/bpf/prog_tests/ringbuf.c        | 38 +++++++-
 .../selftests/bpf/prog_tests/ringbuf_multi.c  | 16 ++++
 5 files changed, 199 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/14 libbpf: refactor cleanup in ring_buffer__add

Refactor the cleanup code in ring_buffer__add to use a unified err_out label. This reduces code duplication, as well as plugging a potential leak if mmap_sz != (__u64)(size_t)mmap_sz (currently this would miss unmapping tmp because ringbuf_unmap_ring isn't called).

---
 tools/lib/bpf/ringbuf.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tools/lib/bpf/ringbuf.c +10 -9
index 02199364db13..f2020807996c 100644
@@ -118,10 +118,9 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
 	/* Map writable consumer page */
 	tmp = mmap(NULL, rb->page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
 	if (tmp == MAP_FAILED) {
-		err = -errno;
 		pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
 			map_fd, err);
-		return libbpf_err(err);
+		goto err_out;
 	}
 	r->consumer_pos = tmp;
 
@@ -131,16 +130,15 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
 	 */
 	mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
 	if (mmap_sz != (__u64)(size_t)mmap_sz) {
+		errno = E2BIG;
 		pr_warn("ringbuf: ring buffer size (%u) is too big\n", info.max_entries);
-		return libbpf_err(-E2BIG);
+		goto err_out;
 	}
 	tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd, rb->page_size);
 	if (tmp == MAP_FAILED) {
-		err = -errno;
-		ringbuf_unmap_ring(rb, r);
 		pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n",
 			map_fd, err);
-		return libbpf_err(err);
+		goto err_out;
 	}
 	r->producer_pos = tmp;
 	r->data = tmp + rb->page_size;
@@ -151,15 +149,18 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
 	e->events = EPOLLIN;
 	e->data.fd = rb->ring_cnt;
 	if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, e) < 0) {
-		err = -errno;
-		ringbuf_unmap_ring(rb, r);
 		pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n",
 			map_fd, err);
-		return libbpf_err(err);
+		goto err_out;
 	}
 
 	rb->ring_cnt++;
 	return 0;
+
+err_out:
+	err = -errno;
+	ringbuf_unmap_ring(rb, r);
+	return libbpf_err(err);
 }
 
 void ring_buffer__free(struct ring_buffer *rb)
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next 01/14] libbpf: refactor cleanup in ring_buffer__add

Quoted 16 lines · Martin Kelly

Refactor the cleanup code in ring_buffer__add to use a unified err_out label. This reduces code duplication, as well as plugging a potential leak if mmap_sz != (__u64)(size_t)mmap_sz (currently this would miss unmapping tmp because ringbuf_unmap_ring isn't called).

---
 tools/lib/bpf/ringbuf.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tools/lib/bpf/ringbuf.c +0 -0
index 02199364db13..f2020807996c 100644

tools/lib/bpf/ringbuf.c int ring_buffer__add(struct ring_buffer *rb, int map_fd, · line 121

Quoted 4 lines
tools/lib/bpf/ringbuf.c +0 -1
@@ -118,10 +118,9 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
        /* Map writable consumer page */
        tmp = mmap(NULL, rb->page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
        if (tmp == MAP_FAILED) {
-               err = -errno;

removing this is wrong, pr_warn() can clobber errno

tools/lib/bpf/ringbuf.c int ring_buffer__add(struct ring_buffer *rb, int map_fd, · line 133

Quoted 12 lines
tools/lib/bpf/ringbuf.c +2 -1
                pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
                        map_fd, err);
-               return libbpf_err(err);
+               goto err_out;
        }
        r->consumer_pos = tmp;
 
@@ -131,16 +130,15 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
         */
        mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
        if (mmap_sz != (__u64)(size_t)mmap_sz) {
+               errno = E2BIG;

ditto, stick to err

Quoted 40 lines
tools/lib/bpf/ringbuf.c +8 -8
                pr_warn("ringbuf: ring buffer size (%u) is too big\n", info.max_entries);
-               return libbpf_err(-E2BIG);
+               goto err_out;
        }
        tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd, rb->page_size);
        if (tmp == MAP_FAILED) {
-               err = -errno;
-               ringbuf_unmap_ring(rb, r);
                pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n",
                        map_fd, err);
-               return libbpf_err(err);
+               goto err_out;
        }
        r->producer_pos = tmp;
        r->data = tmp + rb->page_size;
@@ -151,15 +149,18 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
        e->events = EPOLLIN;
        e->data.fd = rb->ring_cnt;
        if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, e) < 0) {
-               err = -errno;
-               ringbuf_unmap_ring(rb, r);
                pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n",
                        map_fd, err);
-               return libbpf_err(err);
+               goto err_out;
        }
 
        rb->ring_cnt++;
        return 0;
+
+err_out:
+       err = -errno;
+       ringbuf_unmap_ring(rb, r);
+       return libbpf_err(err);
 }
 
 void ring_buffer__free(struct ring_buffer *rb)
--

2.34.1

#3 lore raw

PATCH 2/14 libbpf: switch rings to array of pointers

In reply to Martin Kelly

Switch rb->rings to be an array of pointers instead of a contiguous block. This allows for each ring pointer to be stable after ring_buffer__add is called, which allows us to expose struct ring * to the user without gotchas. Without this change, the realloc in ring_buffer__add could invalidate a struct ring *, making it unsafe to give to the user.

---
 tools/lib/bpf/ringbuf.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
tools/lib/bpf/ringbuf.c +12 -8
index f2020807996c..c2c79e10cfea 100644
@@ -34,7 +34,7 @@ struct ring {
 
 struct ring_buffer {
 	struct epoll_event *events;
-	struct ring *rings;
+	struct ring **rings;
 	size_t page_size;
 	int epoll_fd;
 	int ring_cnt;
@@ -57,7 +57,7 @@ struct ringbuf_hdr {
 	__u32 pad;
 };
 
-static void ringbuf_unmap_ring(struct ring_buffer *rb, struct ring *r)
+static void ringbuf_free_ring(struct ring_buffer *rb, struct ring *r)
 {
 	if (r->consumer_pos) {
 		munmap(r->consumer_pos, rb->page_size);
@@ -67,6 +67,8 @@ static void ringbuf_unmap_ring(struct ring_buffer *rb, struct ring *r)
 		munmap(r->producer_pos, rb->page_size + 2 * (r->mask + 1));
 		r->producer_pos = NULL;
 	}
+
+	free(r);
 }
 
 /* Add extra RINGBUF maps to this ring buffer manager */
@@ -107,8 +109,10 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
 		return libbpf_err(-ENOMEM);
 	rb->events = tmp;
 
-	r = &rb->rings[rb->ring_cnt];
-	memset(r, 0, sizeof(*r));
+	r = calloc(1, sizeof(*r));
+	if (!r)
+		return libbpf_err(-ENOMEM);
+	rb->rings[rb->ring_cnt] = r;
 
 	r->map_fd = map_fd;
 	r->sample_cb = sample_cb;
@@ -159,7 +163,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
 
 err_out:
 	err = -errno;
-	ringbuf_unmap_ring(rb, r);
+	ringbuf_free_ring(rb, r);
 	return libbpf_err(err);
 }
 
@@ -171,7 +175,7 @@ void ring_buffer__free(struct ring_buffer *rb)
 		return;
 
 	for (i = 0; i < rb->ring_cnt; ++i)
-		ringbuf_unmap_ring(rb, &rb->rings[i]);
+		ringbuf_free_ring(rb, rb->rings[i]);
 	if (rb->epoll_fd >= 0)
 		close(rb->epoll_fd);
 
@@ -279,7 +283,7 @@ int ring_buffer__consume(struct ring_buffer *rb)
 	int i;
 
 	for (i = 0; i < rb->ring_cnt; i++) {
-		struct ring *ring = &rb->rings[i];
+		struct ring *ring = rb->rings[i];
 
 		err = ringbuf_process_ring(ring);
 		if (err < 0)
@@ -306,7 +310,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
 
 	for (i = 0; i < cnt; i++) {
 		__u32 ring_id = rb->events[i].data.fd;
-		struct ring *ring = &rb->rings[ring_id];
+		struct ring *ring = rb->rings[ring_id];
 
 		err = ringbuf_process_ring(ring);
 		if (err < 0)
Signature · 1 line
2.34.1
#4 lore raw

PATCH 3/14 libbpf: add ring_buffer__ring

In reply to Martin Kelly

Add a new function ring_buffer__ring, which exposes struct ring * to the user, representing a single ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 14 ++++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  |  8 ++++++++
 3 files changed, 23 insertions(+)
tools/lib/bpf/libbpf.h +14 -0
index 0e52621cba43..2d6c39e20863 100644
@@ -1229,6 +1229,7 @@ LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
 
 /* Ring buffer APIs */
 struct ring_buffer;
+struct ring;
 struct user_ring_buffer;
 
 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
@@ -1249,6 +1250,19 @@ 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__epoll_fd(const struct ring_buffer *rb);
 
+/**
+ * @brief **ring_buffer__ring()** returns the ring object inside a given
+ * ringbuffer.
+ *
+ * @param rb A ringbuffer object.
+ * @param idx An index into the rings contained within the ringbuffer object.
+ * The index is 0-based and corresponds to the order in which ring_buffer__add
+ * was called.
+ * @return A ring object on success; NULL and errno set if the index is invalid.
+ */
+LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
+					  unsigned int idx);
+
 struct user_ring_buffer_opts {
 	size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index 57712321490f..7a7370c2bc25 100644
@@ -400,4 +400,5 @@ LIBBPF_1.3.0 {
 		bpf_program__attach_netfilter;
 		bpf_program__attach_tcx;
 		bpf_program__attach_uprobe_multi;
+		ring_buffer__ring;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +8 -0
index c2c79e10cfea..2857df0f2d03 100644
@@ -328,6 +328,14 @@ int ring_buffer__epoll_fd(const struct ring_buffer *rb)
 	return rb->epoll_fd;
 }
 
+struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
+{
+	if (idx >= rb->ring_cnt)
+		return errno = ERANGE, NULL;
+
+	return rb->rings[idx];
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH bpf-next 03/14] libbpf: add ring_buffer__ring

Quoted 24 lines · Martin Kelly

Add a new function ring_buffer__ring, which exposes struct ring * to the user, representing a single ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 14 ++++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  |  8 ++++++++
 3 files changed, 23 insertions(+)
tools/lib/bpf/libbpf.h +1 -0
index 0e52621cba43..2d6c39e20863 100644
@@ -1229,6 +1229,7 @@ LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
 
 /* Ring buffer APIs */
 struct ring_buffer;
+struct ring;
 struct user_ring_buffer;
 
 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);

tools/lib/bpf/libbpf.h LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); · line 1255

Quoted 6 lines
tools/lib/bpf/libbpf.h +3 -0
@@ -1249,6 +1250,19 @@ 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__epoll_fd(const struct ring_buffer *rb);
 
+/**
+ * @brief **ring_buffer__ring()** returns the ring object inside a given
+ * ringbuffer.

s/ringbuffer/ring buffer manager/

and I'd also add ", representing single BPF_MAP_TYPE_RINGBUF map instance"

tools/lib/bpf/libbpf.h LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); · line 1257

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+ *
+ * @param rb A ringbuffer object.

"ring buffer manager"?

Quoted 43 lines
tools/lib/bpf/libbpf.h +8 -0
+ * @param idx An index into the rings contained within the ringbuffer object.
+ * The index is 0-based and corresponds to the order in which ring_buffer__add
+ * was called.
+ * @return A ring object on success; NULL and errno set if the index is invalid.
+ */
+LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
+                                         unsigned int idx);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index 57712321490f..7a7370c2bc25 100644
@@ -400,4 +400,5 @@ LIBBPF_1.3.0 {
                bpf_program__attach_netfilter;
                bpf_program__attach_tcx;
                bpf_program__attach_uprobe_multi;
+               ring_buffer__ring;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +8 -1
index c2c79e10cfea..2857df0f2d03 100644
@@ -328,6 +328,14 @@ int ring_buffer__epoll_fd(const struct ring_buffer *rb)
        return rb->epoll_fd;
 }
 
+struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
+{
+       if (idx >= rb->ring_cnt)
+               return errno = ERANGE, NULL;
+
+       return rb->rings[idx];
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {
--

2.34.1

#6 lore raw

PATCH 4/14 selftests/bpf: add tests for ring_buffer__ring

In reply to Martin Kelly

Add tests for the new API ring_buffer__ring.

---
 .../selftests/bpf/prog_tests/ringbuf_multi.c     | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +16 -0
index 1455911d9fcb..f0729ffaf030 100644
@@ -42,6 +42,8 @@ void test_ringbuf_multi(void)
 {
 	struct test_ringbuf_multi *skel;
 	struct ring_buffer *ringbuf = NULL;
+	struct ring *ring_old;
+	struct ring *ring;
 	int err;
 	int page_size = getpagesize();
 	int proto_fd = -1;
@@ -84,11 +86,25 @@ void test_ringbuf_multi(void)
 	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
 		goto cleanup;
 
+	/* verify ring_buffer__ring returns expected results */
+	ring = ring_buffer__ring(ringbuf, 0);
+	if (CHECK(ring == NULL, "ringbuf_ring", "valid index returning NULL\n"))
+		goto cleanup;
+	ring_old = ring;
+	ring = ring_buffer__ring(ringbuf, 1);
+	if (CHECK(ring != NULL, "ringbuf_ring", "invalid index not rejected\n"))
+		goto cleanup;
+
 	err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
 			      process_sample, (void *)(long)2);
 	if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
 		goto cleanup;
 
+	/* verify adding a new ring didn't invalidate our older pointer */
+	ring = ring_buffer__ring(ringbuf, 0);
+	if (CHECK(ring != ring_old, "ringbuf_ring", "old ring invalidated\n"))
+		goto cleanup;
+
 	err = test_ringbuf_multi__attach(skel);
 	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
 		goto cleanup;
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH bpf-next 04/14] selftests/bpf: add tests for ring_buffer__ring

Quoted 24 lines · Martin Kelly

Add tests for the new API ring_buffer__ring.

---
 .../selftests/bpf/prog_tests/ringbuf_multi.c     | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +2 -0
index 1455911d9fcb..f0729ffaf030 100644
@@ -42,6 +42,8 @@ void test_ringbuf_multi(void)
 {
        struct test_ringbuf_multi *skel;
        struct ring_buffer *ringbuf = NULL;
+       struct ring *ring_old;
+       struct ring *ring;
        int err;
        int page_size = getpagesize();
        int proto_fd = -1;
@@ -84,11 +86,25 @@ void test_ringbuf_multi(void)
        if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
                goto cleanup;

tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c void test_ringbuf_multi(void) · line 97

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +9 -0
 
+       /* verify ring_buffer__ring returns expected results */
+       ring = ring_buffer__ring(ringbuf, 0);
+       if (CHECK(ring == NULL, "ringbuf_ring", "valid index returning NULL\n"))
+               goto cleanup;
+       ring_old = ring;
+       ring = ring_buffer__ring(ringbuf, 1);
+       if (CHECK(ring != NULL, "ringbuf_ring", "invalid index not rejected\n"))
+               goto cleanup;
+

we don't use CHECK() for new code, please use ASSERT_OK_PTR() and ASSERT_ERR_PTR here

tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c void test_ringbuf_multi(void) · line 105

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +3 -0
        err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
                              process_sample, (void *)(long)2);
        if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
                goto cleanup;
 
+       /* verify adding a new ring didn't invalidate our older pointer */
+       ring = ring_buffer__ring(ringbuf, 0);
+       if (CHECK(ring != ring_old, "ringbuf_ring", "old ring invalidated\n"))

and ASSERT_EQ() here

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +2 -1
+               goto cleanup;
+
        err = test_ringbuf_multi__attach(skel);
        if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
                goto cleanup;
--

2.34.1

#8 lore raw

PATCH 5/14 libbpf: add ring__producer_pos, ring__consumer_pos

In reply to Martin Kelly

Add APIs to get the producer and consumer position for a given ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.map |  2 ++
 tools/lib/bpf/ringbuf.c  | 14 ++++++++++++++
 3 files changed, 32 insertions(+)
tools/lib/bpf/libbpf.h +16 -0
index 2d6c39e20863..935162dbb3bf 100644
@@ -1263,6 +1263,22 @@ LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
 LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
 					  unsigned int idx);
 
+/**
+ * @brief **ring__consumer_pos()** returns the current consumer position.
+ *
+ * @param r A ring object.
+ * @return The current consumer position.
+ */
+LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
+
+/**
+ * @brief **ring__producer_pos()** returns the current producer position.
+ *
+ * @param r A ring object.
+ * @return The current producer position.
+ */
+LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
+
 struct user_ring_buffer_opts {
 	size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +2 -0
index 7a7370c2bc25..1c532fe7a445 100644
@@ -401,4 +401,6 @@ LIBBPF_1.3.0 {
 		bpf_program__attach_tcx;
 		bpf_program__attach_uprobe_multi;
 		ring_buffer__ring;
+		ring__consumer_pos;
+		ring__producer_pos;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +14 -0
index 2857df0f2d03..54c596db57a4 100644
@@ -336,6 +336,20 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
 	return rb->rings[idx];
 }
 
+unsigned long ring__consumer_pos(const struct ring *r)
+{
+	/* Synchronizes with smp_store_release() in ringbuf_process_ring(). */
+	return smp_load_acquire(r->consumer_pos);
+}
+
+unsigned long ring__producer_pos(const struct ring *r)
+{
+	/* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in
+	 * the kernel.
+	 */
+	return smp_load_acquire(r->producer_pos);
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH bpf-next 05/14] libbpf: add ring__producer_pos, ring__consumer_pos

Quoted 43 lines · Martin Kelly

Add APIs to get the producer and consumer position for a given ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.map |  2 ++
 tools/lib/bpf/ringbuf.c  | 14 ++++++++++++++
 3 files changed, 32 insertions(+)
tools/lib/bpf/libbpf.h +16 -0
index 2d6c39e20863..935162dbb3bf 100644
@@ -1263,6 +1263,22 @@ LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
 LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
                                          unsigned int idx);
 
+/**
+ * @brief **ring__consumer_pos()** returns the current consumer position.
+ *
+ * @param r A ring object.
+ * @return The current consumer position.
+ */
+LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
+
+/**
+ * @brief **ring__producer_pos()** returns the current producer position.
+ *
+ * @param r A ring object.
+ * @return The current producer position.
+ */
+LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +0 -0
index 7a7370c2bc25..1c532fe7a445 100644

tools/lib/bpf/libbpf.map LIBBPF_1.3.0 { · line 405

Quoted 5 lines
tools/lib/bpf/libbpf.map +2 -0
@@ -401,4 +401,6 @@ LIBBPF_1.3.0 {
                bpf_program__attach_tcx;
                bpf_program__attach_uprobe_multi;
                ring_buffer__ring;
+               ring__consumer_pos;
+               ring__producer_pos;

we keep this list ordered, so ring__ goes before ring_b

Quoted 29 lines
tools/lib/bpf/libbpf.map +0 -0
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +14 -1
index 2857df0f2d03..54c596db57a4 100644
@@ -336,6 +336,20 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
        return rb->rings[idx];
 }
 
+unsigned long ring__consumer_pos(const struct ring *r)
+{
+       /* Synchronizes with smp_store_release() in ringbuf_process_ring(). */
+       return smp_load_acquire(r->consumer_pos);
+}
+
+unsigned long ring__producer_pos(const struct ring *r)
+{
+       /* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in
+        * the kernel.
+        */
+       return smp_load_acquire(r->producer_pos);
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {
--

2.34.1

#10 lore raw

PATCH 6/14 selftests/bpf: add tests for ring__*_pos

In reply to Martin Kelly

Add tests for the new APIs ring__producer_pos and ring__consumer_pos.

---
 .../testing/selftests/bpf/prog_tests/ringbuf.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +18 -0
index ac104dc652e3..0400123da690 100644
@@ -91,6 +91,8 @@ static void ringbuf_subtest(void)
 	int err, cnt, rb_fd;
 	int page_size = getpagesize();
 	void *mmap_ptr, *tmp_ptr;
+	struct ring *ring;
+	unsigned long cons_pos, prod_pos;
 
 	skel = test_ringbuf_lskel__open();
 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
@@ -162,6 +164,10 @@ static void ringbuf_subtest(void)
 
 	trigger_samples();
 
+	ring = ring_buffer__ring(ringbuf, 0);
+	if (CHECK(ring == NULL, "ringbuf_ring", "valid index returning NULL\n"))
+		goto cleanup;
+
 	/* 2 submitted + 1 discarded records */
 	CHECK(skel->bss->avail_data != 3 * rec_sz,
 	      "err_avail_size", "exp %ld, got %ld\n",
@@ -176,6 +182,18 @@ static void ringbuf_subtest(void)
 	      "err_prod_pos", "exp %ld, got %ld\n",
 	      3L * rec_sz, skel->bss->prod_pos);
 
+	/* verify getting this data directly via the ring object yields the same
+	 * results
+	 */
+	cons_pos = ring__consumer_pos(ring);
+	CHECK(cons_pos != 0,
+	      "err_ring_cons_pos", "exp %ld, got %ld\n",
+	      0L, cons_pos);
+	prod_pos = ring__producer_pos(ring);
+	CHECK(prod_pos != 3 * rec_sz,
+	      "err_ring_prod_pos", "exp %ld, got %ld\n",
+	      3L * rec_sz, prod_pos);
+
 	/* poll for samples */
 	err = ring_buffer__poll(ringbuf, -1);
 
Signature · 1 line
2.34.1
#11 lore raw

PATCH 7/14 libbpf: add ring__avail_data_size

In reply to Martin Kelly

Add ring__avail_data_size for querying the currently available data in the ringbuffer, similar to the BPF_RB_AVAIL_DATA flag in bpf_ringbuf_query. This is racy during ongoing operations but is still useful for overall information on how a ringbuffer is behaving.

---
 tools/lib/bpf/libbpf.h   | 11 +++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  |  5 +++++
 3 files changed, 17 insertions(+)
tools/lib/bpf/libbpf.h +11 -0
index 935162dbb3bf..87e3bad37737 100644
@@ -1279,6 +1279,17 @@ LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
  */
 LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
 
+/**
+ * @brief **ring__avail_data_size()** returns the number of bytes in this
+ * ringbuffer not yet consumed. This has no locking associated with it, so it
+ * can be inaccurate if operations are ongoing while this is called. However, it
+ * should still show the correct trend over the long-term.
+ *
+ * @param r A ring object.
+ * @return The number of bytes not yet consumed.
+ */
+LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
+
 struct user_ring_buffer_opts {
 	size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index 1c532fe7a445..f66d7f0bc224 100644
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 {
 		bpf_program__attach_tcx;
 		bpf_program__attach_uprobe_multi;
 		ring_buffer__ring;
+		ring__avail_data_size;
 		ring__consumer_pos;
 		ring__producer_pos;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +5 -0
index 54c596db57a4..f51ad1af6ab8 100644
@@ -350,6 +350,11 @@ unsigned long ring__producer_pos(const struct ring *r)
 	return smp_load_acquire(r->producer_pos);
 }
 
+size_t ring__avail_data_size(const struct ring *r)
+{
+	return ring__producer_pos(r) - ring__consumer_pos(r);
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH bpf-next 07/14] libbpf: add ring__avail_data_size

Quoted 52 lines · Martin Kelly

Add ring__avail_data_size for querying the currently available data in the ringbuffer, similar to the BPF_RB_AVAIL_DATA flag in bpf_ringbuf_query. This is racy during ongoing operations but is still useful for overall information on how a ringbuffer is behaving.

---
 tools/lib/bpf/libbpf.h   | 11 +++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  |  5 +++++
 3 files changed, 17 insertions(+)
tools/lib/bpf/libbpf.h +11 -0
index 935162dbb3bf..87e3bad37737 100644
@@ -1279,6 +1279,17 @@ LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
  */
 LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
 
+/**
+ * @brief **ring__avail_data_size()** returns the number of bytes in this
+ * ringbuffer not yet consumed. This has no locking associated with it, so it
+ * can be inaccurate if operations are ongoing while this is called. However, it
+ * should still show the correct trend over the long-term.
+ *
+ * @param r A ring object.
+ * @return The number of bytes not yet consumed.
+ */
+LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index 1c532fe7a445..f66d7f0bc224 100644
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 {
                bpf_program__attach_tcx;
                bpf_program__attach_uprobe_multi;
                ring_buffer__ring;
+               ring__avail_data_size;
                ring__consumer_pos;
                ring__producer_pos;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +0 -0
index 54c596db57a4..f51ad1af6ab8 100644

tools/lib/bpf/ringbuf.c unsigned long ring__producer_pos(const struct ring *r) · line 355

Quoted 6 lines
tools/lib/bpf/ringbuf.c +3 -0
@@ -350,6 +350,11 @@ unsigned long ring__producer_pos(const struct ring *r)
        return smp_load_acquire(r->producer_pos);
 }
 
+size_t ring__avail_data_size(const struct ring *r)
+{
+       return ring__producer_pos(r) - ring__consumer_pos(r);

this might be ok as is, but if you look at kernel implementation, we make sure to get consumer position first, and then producer position second, then calculate difference. This is deliberately to avoid the situation when consumer pos is greater than producer pos, which will result in non-sensical negative (or huge) numbers.

Let's do the same, use two local variables, and have conservative
ordering: consumer, then producer.

Quoted 8 lines
tools/lib/bpf/ringbuf.c +2 -1
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {
--

2.34.1

#13 lore raw

PATCH 8/14 selftests/bpf: add tests for ring__avail_data_size

In reply to Martin Kelly

Add test for the new API ring__avail_data_size.

---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +5 -1
index 0400123da690..e4e1171e2e4d 100644
@@ -92,7 +92,7 @@ static void ringbuf_subtest(void)
 	int page_size = getpagesize();
 	void *mmap_ptr, *tmp_ptr;
 	struct ring *ring;
-	unsigned long cons_pos, prod_pos;
+	unsigned long avail_data, cons_pos, prod_pos;
 
 	skel = test_ringbuf_lskel__open();
 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
@@ -185,6 +185,10 @@ static void ringbuf_subtest(void)
 	/* verify getting this data directly via the ring object yields the same
 	 * results
 	 */
+	avail_data = ring__avail_data_size(ring);
+	CHECK(avail_data != 3 * rec_sz,
+	      "err_ring_avail_size", "exp %ld, got %ld\n",
+	      3L * rec_sz, avail_data);
 	cons_pos = ring__consumer_pos(ring);
 	CHECK(cons_pos != 0,
 	      "err_ring_cons_pos", "exp %ld, got %ld\n",
Signature · 1 line
2.34.1
#14 lore raw

PATCH 9/14 libbpf: add ring__size

In reply to Martin Kelly

Add ring__size to get the total size of a given ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 8 ++++++++
 tools/lib/bpf/libbpf.map | 1 +
 tools/lib/bpf/ringbuf.c  | 5 +++++
 3 files changed, 14 insertions(+)
tools/lib/bpf/libbpf.h +8 -0
index 87e3bad37737..299d98402ad4 100644
@@ -1290,6 +1290,14 @@ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
  */
 LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
 
+/**
+ * @brief **ring__size()** returns the total size of the ringbuffer.
+ *
+ * @param r A ring object.
+ * @return The total size of the ringbuffer.
+ */
+LIBBPF_API size_t ring__size(const struct ring *r);
+
 struct user_ring_buffer_opts {
 	size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index f66d7f0bc224..4ca77e715667 100644
@@ -404,4 +404,5 @@ LIBBPF_1.3.0 {
 		ring__avail_data_size;
 		ring__consumer_pos;
 		ring__producer_pos;
+		ring__size;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +5 -0
index f51ad1af6ab8..52c385195f32 100644
@@ -355,6 +355,11 @@ size_t ring__avail_data_size(const struct ring *r)
 	return ring__producer_pos(r) - ring__consumer_pos(r);
 }
 
+size_t ring__size(const struct ring *r)
+{
+	return r->mask + 1;
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH bpf-next 09/14] libbpf: add ring__size

Quoted 15 lines · Martin Kelly

Add ring__size to get the total size of a given ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 8 ++++++++
 tools/lib/bpf/libbpf.map | 1 +
 tools/lib/bpf/ringbuf.c  | 5 +++++
 3 files changed, 14 insertions(+)
tools/lib/bpf/libbpf.h +0 -0
index 87e3bad37737..299d98402ad4 100644

tools/lib/bpf/libbpf.h LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); · line 1294

Quoted 5 lines
tools/lib/bpf/libbpf.h +2 -0
@@ -1290,6 +1290,14 @@ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
  */
 LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
 
+/**
+ * @brief **ring__size()** returns the total size of the ringbuffer.

it's confusing to use "ringbuffer" here, as you refer to ring buffer *manager* and to ring buffer *map* inside it with this. Let's be more precise and also maybe mention that this is the size of ring buffer's map data area (excluding producer/consumer pages)

Quoted 38 lines
tools/lib/bpf/libbpf.h +6 -0
+ *
+ * @param r A ring object.
+ * @return The total size of the ringbuffer.
+ */
+LIBBPF_API size_t ring__size(const struct ring *r);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index f66d7f0bc224..4ca77e715667 100644
@@ -404,4 +404,5 @@ LIBBPF_1.3.0 {
                ring__avail_data_size;
                ring__consumer_pos;
                ring__producer_pos;
+               ring__size;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +5 -1
index f51ad1af6ab8..52c385195f32 100644
@@ -355,6 +355,11 @@ size_t ring__avail_data_size(const struct ring *r)
        return ring__producer_pos(r) - ring__consumer_pos(r);
 }
 
+size_t ring__size(const struct ring *r)
+{
+       return r->mask + 1;
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {
--

2.34.1

#16 lore raw

PATCH 10/14 selftests/bpf: add tests for ring__size

In reply to Martin Kelly

Add tests for the new API ring__size.

---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +5 -1
index e4e1171e2e4d..962dc1bc5f3b 100644
@@ -92,7 +92,7 @@ static void ringbuf_subtest(void)
 	int page_size = getpagesize();
 	void *mmap_ptr, *tmp_ptr;
 	struct ring *ring;
-	unsigned long avail_data, cons_pos, prod_pos;
+	unsigned long avail_data, ring_size, cons_pos, prod_pos;
 
 	skel = test_ringbuf_lskel__open();
 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
@@ -189,6 +189,10 @@ static void ringbuf_subtest(void)
 	CHECK(avail_data != 3 * rec_sz,
 	      "err_ring_avail_size", "exp %ld, got %ld\n",
 	      3L * rec_sz, avail_data);
+	ring_size = ring__size(ring);
+	CHECK(ring_size != page_size,
+	      "err_ring_ring_size", "exp %ld, got %ld\n",
+	      (long)page_size, ring_size);
 	cons_pos = ring__consumer_pos(ring);
 	CHECK(cons_pos != 0,
 	      "err_ring_cons_pos", "exp %ld, got %ld\n",
Signature · 1 line
2.34.1
#17 lore raw

PATCH 11/14 libbpf: add ring__map_fd

In reply to Martin Kelly

Add ring__map_fd to get the file descriptor underlying a given ringbuffer.

---
 tools/lib/bpf/libbpf.h   | 9 +++++++++
 tools/lib/bpf/libbpf.map | 1 +
 tools/lib/bpf/ringbuf.c  | 5 +++++
 3 files changed, 15 insertions(+)
tools/lib/bpf/libbpf.h +9 -0
index 299d98402ad4..d2a237086c2c 100644
@@ -1298,6 +1298,15 @@ LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
  */
 LIBBPF_API size_t ring__size(const struct ring *r);
 
+/**
+ * @brief **ring__map_fd()** returns the file descriptor underlying the given
+ * ringbuffer.
+ *
+ * @param r A ring object.
+ * @return The underlying ringbuffer file descriptor
+ */
+LIBBPF_API int ring__map_fd(const struct ring *r);
+
 struct user_ring_buffer_opts {
 	size_t sz; /* size of this struct, for forward/backward compatibility */
 };
tools/lib/bpf/libbpf.map +1 -0
index 4ca77e715667..13800b73c343 100644
@@ -403,6 +403,7 @@ LIBBPF_1.3.0 {
 		ring_buffer__ring;
 		ring__avail_data_size;
 		ring__consumer_pos;
+		ring__map_fd;
 		ring__producer_pos;
 		ring__size;
 } LIBBPF_1.2.0;
tools/lib/bpf/ringbuf.c +5 -0
index 52c385195f32..2dba2836d85b 100644
@@ -360,6 +360,11 @@ size_t ring__size(const struct ring *r)
 	return r->mask + 1;
 }
 
+int ring__map_fd(const struct ring *r)
+{
+	return r->map_fd;
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#18 lore raw

PATCH 12/14 selftests/bpf: add tests for ring__map_fd

In reply to Martin Kelly

Add tests for the new API ring__map_fd.

---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +6 -0
index 962dc1bc5f3b..27149dee0e2c 100644
@@ -92,6 +92,7 @@ static void ringbuf_subtest(void)
 	int page_size = getpagesize();
 	void *mmap_ptr, *tmp_ptr;
 	struct ring *ring;
+	int map_fd;
 	unsigned long avail_data, ring_size, cons_pos, prod_pos;
 
 	skel = test_ringbuf_lskel__open();
@@ -168,6 +169,11 @@ static void ringbuf_subtest(void)
 	if (CHECK(ring == NULL, "ringbuf_ring", "valid index returning NULL\n"))
 		goto cleanup;
 
+	map_fd = ring__map_fd(ring);
+	CHECK(map_fd != skel->maps.ringbuf.map_fd,
+	      "err_map_fd", "exp %d, got %d\n",
+	      skel->maps.ringbuf.map_fd, map_fd);
+
 	/* 2 submitted + 1 discarded records */
 	CHECK(skel->bss->avail_data != 3 * rec_sz,
 	      "err_avail_size", "exp %ld, got %ld\n",
Signature · 1 line
2.34.1
#19 lore raw

PATCH 13/14 libbpf: add ring__consume

In reply to Martin Kelly

Add ring__consume to consume a single ringbuffer, analogous to ring_buffer__consume.

---
 tools/lib/bpf/libbpf.h   | 10 ++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  | 13 +++++++++++++
 3 files changed, 24 insertions(+)
tools/lib/bpf/libbpf.h +10 -0
index d2a237086c2c..861f8b6795e9 100644
@@ -1307,6 +1307,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 ring 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 */
 };
tools/lib/bpf/libbpf.map +1 -0
index 13800b73c343..4b5124bb0e1a 100644
@@ -402,6 +402,7 @@ LIBBPF_1.3.0 {
 		bpf_program__attach_uprobe_multi;
 		ring_buffer__ring;
 		ring__avail_data_size;
+		ring__consume;
 		ring__consumer_pos;
 		ring__map_fd;
 		ring__producer_pos;
tools/lib/bpf/ringbuf.c +13 -0
index 2dba2836d85b..b7ef003366d3 100644
@@ -365,6 +365,19 @@ 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)
+		res = libbpf_err(res);
+	else if (res > INT_MAX)
+		res = INT_MAX;
+
+	return res;
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
 	if (rb->consumer_pos) {
Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH bpf-next 13/14] libbpf: add ring__consume

Quoted 49 lines · Martin Kelly

Add ring__consume to consume a single ringbuffer, analogous to ring_buffer__consume.

---
 tools/lib/bpf/libbpf.h   | 10 ++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/ringbuf.c  | 13 +++++++++++++
 3 files changed, 24 insertions(+)
tools/lib/bpf/libbpf.h +10 -0
index d2a237086c2c..861f8b6795e9 100644
@@ -1307,6 +1307,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 ring 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 */
 };
tools/lib/bpf/libbpf.map +1 -0
index 13800b73c343..4b5124bb0e1a 100644
@@ -402,6 +402,7 @@ LIBBPF_1.3.0 {
                bpf_program__attach_uprobe_multi;
                ring_buffer__ring;
                ring__avail_data_size;
+               ring__consume;
                ring__consumer_pos;
                ring__map_fd;
                ring__producer_pos;
tools/lib/bpf/ringbuf.c +0 -0
index 2dba2836d85b..b7ef003366d3 100644

tools/lib/bpf/ringbuf.c int ring__map_fd(const struct ring *r) · line 374

Quoted 10 lines
tools/lib/bpf/ringbuf.c +7 -0
@@ -365,6 +365,19 @@ 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)
+               res = libbpf_err(res);

it's a bit confusing to have libbpf_err() not within some return. Let's do return libbpf_err(res); here as the only error path?

tools/lib/bpf/ringbuf.c int ring__map_fd(const struct ring *r) · line 378

Quoted 4 lines
tools/lib/bpf/ringbuf.c +4 -0
+       else if (res > INT_MAX)
+               res = INT_MAX;
+
+       return res;

and here we can have return res > INT_MAX ? INT_MAX : res;

Quoted 8 lines
tools/lib/bpf/ringbuf.c +2 -1
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {
--

2.34.1

#21 lore raw

PATCH 14/14 selftests/bpf: add tests for ring__consume

In reply to Martin Kelly

Add tests for new API ring__consume.

---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +5 -1
index 27149dee0e2c..ccf13f0e36c7 100644
@@ -312,7 +312,11 @@ static void ringbuf_subtest(void)
 	 * samples, so consume them here to collect all the samples
 	 */
 	err = ring_buffer__consume(ringbuf);
-	CHECK(err < 0, "rb_consume", "failed: %d\b", err);
+	CHECK(err < 0, "ringbuffer_consume", "failed: %d\b", err);
+
+	/* also consume using ring__consume to make sure it works the same */
+	err = ring__consume(ring);
+	CHECK(err < 0, "ring_consume", "failed: %d\b", err);
 
 	/* 3 rounds, 2 samples each */
 	cnt = atomic_xchg(&sample_cnt, 0);
Signature · 1 line
2.34.1
#22 lore raw
In reply to Martin Kelly
Quoted 40 lines · Martin Kelly

This patch series adds a new ring__ API to libbpf exposing getters for accessing the individual ringbuffers inside a struct ring_buffer. This is useful for polling individually, getting available data, or similar use cases. The API looks like this, and was roughly proposed by Andrii Nakryiko in another thread:

Getting a ring struct:
struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx);

Using the ring struct:
unsigned long ring__consumer_pos(const struct ring *r); unsigned long ring__producer_pos(const struct ring *r); size_t ring__avail_data_size(const struct ring *r); size_t ring__size(const struct ring *r);
int ring__map_fd(const struct ring *r);
int ring__consume(struct ring *r);

Martin Kelly (14):
libbpf: refactor cleanup in ring_buffer__add
libbpf: switch rings to array of pointers
libbpf: add ring_buffer__ring selftests/bpf: add tests for ring_buffer__ring
libbpf: add ring__producer_pos, ring__consumer_pos selftests/bpf: add tests for ring__*_pos
libbpf: add ring__avail_data_size selftests/bpf: add tests for ring__avail_data_size
libbpf: add ring__size
selftests/bpf: add tests for ring__size
libbpf: add ring__map_fd
selftests/bpf: add tests for ring__map_fd
libbpf: add ring__consume
selftests/bpf: add tests for ring__consume

 tools/lib/bpf/libbpf.h                        | 68 +++++++++++++++
 tools/lib/bpf/libbpf.map                      |  7 ++
 tools/lib/bpf/ringbuf.c                       | 87 +++++++++++++++----
 .../selftests/bpf/prog_tests/ringbuf.c        | 38 +++++++-
 .../selftests/bpf/prog_tests/ringbuf_multi.c  | 16 ++++
 5 files changed, 199 insertions(+), 17 deletions(-)
Quoted 3 lines

--
2.34.1

Looks mostly good, sorry for taking a while to get to these. I left a few comments here and there, please address and submit v2. Try to use consistent "ring buffer manager" and "ring buffer map" to distinguish them in doc comments. And also please don't add new CHECK() uses to selftests, we have a whole family of ASSERT_xxx() macros, please use them.

#23 lore raw
Quoted 44 lines · Andrii Nakryiko
Quoted 39 lines · Martin Kelly

This patch series adds a new ring__ API to libbpf exposing getters for accessing the individual ringbuffers inside a struct ring_buffer. This is useful for polling individually, getting available data, or similar use cases. The API looks like this, and was roughly proposed by Andrii Nakryiko in another thread:

Getting a ring struct:
struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx);

Using the ring struct:
unsigned long ring__consumer_pos(const struct ring *r); unsigned long ring__producer_pos(const struct ring *r); size_t ring__avail_data_size(const struct ring *r); size_t ring__size(const struct ring *r);
int ring__map_fd(const struct ring *r);
int ring__consume(struct ring *r);

Martin Kelly (14):
libbpf: refactor cleanup in ring_buffer__add
libbpf: switch rings to array of pointers
libbpf: add ring_buffer__ring selftests/bpf: add tests for ring_buffer__ring
libbpf: add ring__producer_pos, ring__consumer_pos selftests/bpf: add tests for ring__*_pos
libbpf: add ring__avail_data_size selftests/bpf: add tests for ring__avail_data_size
libbpf: add ring__size
selftests/bpf: add tests for ring__size
libbpf: add ring__map_fd
selftests/bpf: add tests for ring__map_fd
libbpf: add ring__consume
selftests/bpf: add tests for ring__consume

  tools/lib/bpf/libbpf.h                        | 68 +++++++++++++++
  tools/lib/bpf/libbpf.map                      |  7 ++
  tools/lib/bpf/ringbuf.c                       | 87 +++++++++++++++----
  .../selftests/bpf/prog_tests/ringbuf.c        | 38 +++++++-
  .../selftests/bpf/prog_tests/ringbuf_multi.c  | 16 ++++
  5 files changed, 199 insertions(+), 17 deletions(-)
Quoted 3 lines

--
2.34.1

Quoted 6 lines

Looks mostly good, sorry for taking a while to get to these. I left a few comments here and there, please address and submit v2. Try to use consistent "ring buffer manager" and "ring buffer map" to distinguish them in doc comments. And also please don't add new CHECK() uses to selftests, we have a whole family of ASSERT_xxx() macros, please use them.

Thanks, I agree with all the comments and will issue a v2 as soon as I can. I noticed the ringbuffer vs ringbuffer manager discrepancy but wasn't sure which phrasing was better, as the API and struct names are ring_buffer and ring instead of ring_buffer_manager and ring_buffer. I'll change it up to consistently refer to the parent struct as "ring buffer manager".