BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 00/10] Ring Buffer Iterator API
@ 2026-11-23 21:13 Kumar Kartikeya Dwivedi
  2026-11-23 21:13 ` [PATCH bpf-next v1 01/10] bpf: Order record data after the producer position in overwrite mode Kumar Kartikeya Dwivedi
                   ` (9 more replies)
  0 siblings, 10 replies; 15+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-23 21:13 UTC (permalink / raw)
  To: bpf
  Cc: Xu Kuohai, Tamir Duberstein, Alexei Starovoitov, Andrii Nakryiko,
	Daniel Borkmann, Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai,
	kkd, kernel-team

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/20250804022101.2171981-3-xukuohai@huaweicloud.com/
  [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
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-11-23 21:46 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 21:13 [PATCH bpf-next v1 00/10] Ring Buffer Iterator API Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 01/10] bpf: Order record data after the producer position in overwrite mode Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 02/10] bpf: Keep an overwritable ring buffer readable while it is overwritten Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 03/10] bpf, docs: Document the overwrite mode of BPF ring buffer Kumar Kartikeya Dwivedi
2026-11-23 21:46   ` bot+bpf-ci
2026-11-23 21:13 ` [PATCH bpf-next v1 04/10] libbpf: Account for the overwrite position in ring__avail_data_size() Kumar Kartikeya Dwivedi
2026-11-23 21:22   ` sashiko-bot
2026-11-23 21:13 ` [PATCH bpf-next v1 05/10] libbpf: Add a record API for consuming BPF ring buffers Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 06/10] libbpf: Add ring_buffer__wait() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 07/10] libbpf: Support overwritable ring buffers in the record API Kumar Kartikeya Dwivedi
2026-11-23 21:28   ` sashiko-bot
2026-11-23 21:13 ` [PATCH bpf-next v1 08/10] libbpf: Split ring setup out of ring_buffer__add() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 09/10] libbpf: Add ring__new() and ring__free() Kumar Kartikeya Dwivedi
2026-11-23 21:20   ` sashiko-bot
2026-11-23 21:13 ` [PATCH bpf-next v1 10/10] selftests/bpf: Add tests for the ring buffer record API Kumar Kartikeya Dwivedi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox