1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
3 * Ring buffer operations.
5 * Copyright (C) 2020 Facebook, Inc.
14 #include <linux/err.h>
15 #include <linux/bpf.h>
16 #include <asm/barrier.h>
18 #include <sys/epoll.h>
21 #include "libbpf_internal.h"
25 ring_buffer_sample_fn sample_cb;
28 unsigned long *consumer_pos;
29 unsigned long *producer_pos;
35 struct epoll_event *events;
42 static void ringbuf_unmap_ring(struct ring_buffer *rb, struct ring *r)
44 if (r->consumer_pos) {
45 munmap(r->consumer_pos, rb->page_size);
46 r->consumer_pos = NULL;
48 if (r->producer_pos) {
49 munmap(r->producer_pos, rb->page_size + 2 * (r->mask + 1));
50 r->producer_pos = NULL;
54 /* Add extra RINGBUF maps to this ring buffer manager */
55 int ring_buffer__add(struct ring_buffer *rb, int map_fd,
56 ring_buffer_sample_fn sample_cb, void *ctx)
58 struct bpf_map_info info;
59 __u32 len = sizeof(info);
60 struct epoll_event *e;
66 memset(&info, 0, sizeof(info));
68 err = bpf_obj_get_info_by_fd(map_fd, &info, &len);
71 pr_warn("ringbuf: failed to get map info for fd=%d: %d\n",
76 if (info.type != BPF_MAP_TYPE_RINGBUF) {
77 pr_warn("ringbuf: map fd=%d is not BPF_MAP_TYPE_RINGBUF\n",
82 tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings));
87 tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events));
92 r = &rb->rings[rb->ring_cnt];
93 memset(r, 0, sizeof(*r));
96 r->sample_cb = sample_cb;
98 r->mask = info.max_entries - 1;
100 /* Map writable consumer page */
101 tmp = mmap(NULL, rb->page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
102 if (tmp == MAP_FAILED) {
104 pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
108 r->consumer_pos = tmp;
110 /* Map read-only producer page and data pages. We map twice as big
111 * data size to allow simple reading of samples that wrap around the
112 * end of a ring buffer. See kernel implementation for details.
114 mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
115 if (mmap_sz != (__u64)(size_t)mmap_sz) {
116 pr_warn("ringbuf: ring buffer size (%u) is too big\n", info.max_entries);
119 tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd, rb->page_size);
120 if (tmp == MAP_FAILED) {
122 ringbuf_unmap_ring(rb, r);
123 pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n",
127 r->producer_pos = tmp;
128 r->data = tmp + rb->page_size;
130 e = &rb->events[rb->ring_cnt];
131 memset(e, 0, sizeof(*e));
134 e->data.fd = rb->ring_cnt;
135 if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, e) < 0) {
137 ringbuf_unmap_ring(rb, r);
138 pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n",
147 void ring_buffer__free(struct ring_buffer *rb)
154 for (i = 0; i < rb->ring_cnt; ++i)
155 ringbuf_unmap_ring(rb, &rb->rings[i]);
156 if (rb->epoll_fd >= 0)
165 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
166 const struct ring_buffer_opts *opts)
168 struct ring_buffer *rb;
171 if (!OPTS_VALID(opts, ring_buffer_opts))
174 rb = calloc(1, sizeof(*rb));
178 rb->page_size = getpagesize();
180 rb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
181 if (rb->epoll_fd < 0) {
183 pr_warn("ringbuf: failed to create epoll instance: %d\n", err);
187 err = ring_buffer__add(rb, map_fd, sample_cb, ctx);
194 ring_buffer__free(rb);
198 static inline int roundup_len(__u32 len)
200 /* clear out top 2 bits (discard and busy, if set) */
203 /* add length prefix */
204 len += BPF_RINGBUF_HDR_SZ;
205 /* round up to 8 byte alignment */
206 return (len + 7) / 8 * 8;
209 static int64_t ringbuf_process_ring(struct ring* r)
211 int *len_ptr, len, err;
212 /* 64-bit to avoid overflow in case of extreme application behavior */
214 unsigned long cons_pos, prod_pos;
218 cons_pos = smp_load_acquire(r->consumer_pos);
220 got_new_data = false;
221 prod_pos = smp_load_acquire(r->producer_pos);
222 while (cons_pos < prod_pos) {
223 len_ptr = r->data + (cons_pos & r->mask);
224 len = smp_load_acquire(len_ptr);
226 /* sample not committed yet, bail out for now */
227 if (len & BPF_RINGBUF_BUSY_BIT)
231 cons_pos += roundup_len(len);
233 if ((len & BPF_RINGBUF_DISCARD_BIT) == 0) {
234 sample = (void *)len_ptr + BPF_RINGBUF_HDR_SZ;
235 err = r->sample_cb(r->ctx, sample, len);
237 /* update consumer pos and bail out */
238 smp_store_release(r->consumer_pos,
245 smp_store_release(r->consumer_pos, cons_pos);
247 } while (got_new_data);
252 /* Consume available ring buffer(s) data without event polling.
253 * Returns number of records consumed across all registered ring buffers (or
254 * INT_MAX, whichever is less), or negative number if any of the callbacks
257 int ring_buffer__consume(struct ring_buffer *rb)
259 int64_t err, res = 0;
262 for (i = 0; i < rb->ring_cnt; i++) {
263 struct ring *ring = &rb->rings[i];
265 err = ringbuf_process_ring(ring);
275 /* Poll for available data and consume records, if any are available.
276 * Returns number of records consumed (or INT_MAX, whichever is less), or
277 * negative number, if any of the registered callbacks returned error.
279 int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
282 int64_t err, res = 0;
284 cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms);
288 for (i = 0; i < cnt; i++) {
289 __u32 ring_id = rb->events[i].data.fd;
290 struct ring *ring = &rb->rings[ring_id];
292 err = ringbuf_process_ring(ring);