Ring Buffer Iterator API
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest10 patches 0 repliesNew
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/10 | bpf: Order record data after the producer position in overwrite modekernel/ | +10 | 0 replies | |
| 2/10 | bpf: Keep an overwritable ring buffer readable while it is overwrittenkernel/ | +10 | 0 replies | |
| 3/10 | bpf, docs: Document the overwrite mode of BPF ring bufferDocumentation/ | +42 | 0 replies | |
| 4/10 | libbpf: Account for the overwrite position in ring__avail_data_size()tools/ | +15 | 0 replies | |
| 5/10 | libbpf: Add a record API for consuming BPF ring bufferstools/ tools/ tools/ | +157 | 0 replies | |
| 6/10 | libbpf: Add ring_buffer__wait()tools/ tools/ tools/ | +28 | 0 replies | |
| 7/10 | libbpf: Support overwritable ring buffers in the record APItools/ tools/ tools/ | +123 | 0 replies | |
| 8/10 | libbpf: Split ring setup out of ring_buffer__add()tools/ | +51 | 0 replies | |
| 9/10 | libbpf: Add ring__new() and ring__free()tools/ tools/ tools/ | +71 | 0 replies | |
| 10/10 | selftests/bpf: Add tests for the ring buffer record APItools/ tools/ | +652 | 0 replies |
Reviews trailers per patch
1 of 10 patches has a Reviewed-by or Acked-by.
| Patch | Andrii Nakryiko |
|---|---|
| 1/10 | |
| 2/10 | A-b |
| 3/10 | |
| 4/10 | |
| 5/10 | |
| 6/10 | |
| 7/10 | |
| 8/10 | |
| 9/10 | |
| 10/10 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH bpf-next v1 00/10] Ring Buffer Iterator API
libbpf can only consume a BPF ring buffer through a sample callback, e.g. from ring_buffer__poll() or ring__consume(). This series adds an API that returns records to the caller one by one instead.
It is needed for overwritable ring buffers (BPF_F_RB_OVERWRITE). There, the kernel doesn't wait for the consumer and overwrites the oldest records when the ring buffer is full, so a record can change while it is being read.
libbpf rejects callbacks for such ring buffers since commit f756eedb24d4 ("libbpf: Reject overwrite callback use"). v1 of the overwrite mode series copied the readable data before calling the callbacks [1], and Andrii asked for an iterator-style API instead [2].
The API works for regular ring buffers too, e.g. to process a number of records before consuming them, or to consume a ring buffer from the application's own event loop without a ring_buffer manager.
API
===
struct ring_rec {
const void *data;
unsigned long pos;
__u32 len;};
const struct ring_rec *ring__next(struct ring *r); int ring__release(struct ring *r); void ring__rewind(struct ring *r); bool ring__invalidated(const struct ring *r); struct ring *ring__new(int map_fd, const struct ring_opts *opts); void ring__free(struct ring *r); int ring_buffer__wait(struct ring_buffer *rb, int timeout_ms);
ring__next() returns the next record without consuming it, or NULL if there is nothing to consume. ring__release() consumes all records returned since the last release or rewind. If it returns 1, the caller has to call ring__next() again instead of waiting. ring__rewind() goes back to the first record that was not released, so that ring__next() returns the same records again.
ring__invalidated() is for overwritable ring buffers. It returns true if any of the records returned since the last release or rewind may have been overwritten. The caller has to copy record data first, and use the copy only if ring__invalidated() returns false afterwards.
ring__new() and ring__free() create and free a ring without a ring_buffer manager. ring_buffer__wait() waits for data in the rings of a manager without consuming it, for rings with and without callbacks.
ring__next(), ring__release(), ring__rewind() and ring__invalidated() can only be used on a ring without a sample callback.
Usage
=====
Wait with the ring_buffer manager:
rb = ring_buffer__new(map_fd, NULL, NULL, NULL);
r = ring_buffer__ring(rb, 0);
while (ring_buffer__wait(rb, -1) >= 0) {
do {
while ((rec = ring__next(r)))
handle(rec->data, rec->len);
} while (ring__release(r) > 0);
}Wait in the application's event loop:
r = ring__new(map_fd, NULL);
epoll_ctl(epfd, EPOLL_CTL_ADD, ring__map_fd(r), &ev);
for (;;) {
epoll_wait(epfd, &ev, 1, -1);
do {
while ((rec = ring__next(r)))
handle(rec->data, rec->len);
} while (ring__release(r) > 0);
}Consume an overwritable ring buffer:
do {
while ((rec = ring__next(r))) {
memcpy(copy, rec->data, rec->len);
if (ring__invalidated(r))
break;
handle(copy, rec->len);
}
} while (ring__release(r) > 0);Dump an overwritable ring buffer without consuming it:
n = 0;
while ((rec = ring__next(r))) {
memcpy(copies[n], rec->data, rec->len);
lens[n++] = rec->len;
}
if (!ring__invalidated(r))
dump(copies, lens, n);
ring__rewind(r);Patches
=======
Patches 1-2 are two kernel fixes for overwrite mode; they don't depend on the rest of the series. Patch 3 documents overwrite mode. Patches 4-9 are the libbpf changes and patch 10 adds the selftests.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/CAEf4Bzaq5drHWChXoRBnrmkb6reAsSVj8r=uByFSup31FMA7hw@mail.gmail.com/
Kumar Kartikeya Dwivedi (10):
bpf: Order record data after the producer position in overwrite mode
bpf: Keep an overwritable ring buffer readable while it is overwritten bpf, docs: Document the overwrite mode of BPF ring buffer
libbpf: Account for the overwrite position in ring__avail_data_size()
libbpf: Add a record API for consuming BPF ring buffers
libbpf: Add ring_buffer__wait()
libbpf: Support overwritable ring buffers in the record API
libbpf: Split ring setup out of ring_buffer__add()
libbpf: Add ring__new() and ring__free()
selftests/bpf: Add tests for the ring buffer record API
Documentation/bpf/ringbuf.rst | 44 +- kernel/bpf/ringbuf.c | 24 +- tools/lib/bpf/libbpf.h | 139 ++++- tools/lib/bpf/libbpf.map | 7 + tools/lib/bpf/ringbuf.c | 336 ++++++++-- .../selftests/bpf/prog_tests/ringbuf_iter.c | 586 ++++++++++++++++++ .../selftests/bpf/progs/test_ringbuf_iter.c | 66 ++ 7 files changed, 1155 insertions(+), 47 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ringbuf_iter.c create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_iter.c
base-commit: 2bf3351c5f894697e8443eeef5333d4c4ce90cab
Signature · 1 line
2.53.1-Meta