blob: 089a2fc9f053b4b5f5d1630bc12c24d8e25c30a6 [file] [log] [blame]
Willy Tarreau172945f2019-08-08 15:28:52 +02001/*
2 * Ring buffer management
3 *
4 * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020022#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/applet.h>
Willy Tarreau8dabda72020-05-27 17:22:10 +020024#include <haproxy/buf.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020026#include <haproxy/ring.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020027#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020028#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/thread.h>
Willy Tarreau172945f2019-08-08 15:28:52 +020030
Willy Tarreau6e3fc482022-05-05 15:29:43 +020031/* context used to dump the contents of a ring via "show events" or "show errors" */
32struct show_ring_ctx {
33 struct ring *ring; /* ring to be dumped */
34 size_t ofs; /* offset to restart from, ~0 = end */
35 uint flags; /* set of RING_WF_* */
36};
37
Emeric Brune14b98c2021-01-12 14:21:00 +010038/* Initialize a pre-allocated ring with the buffer area
39 * of size */
40void ring_init(struct ring *ring, void *area, size_t size)
41{
42 HA_RWLOCK_INIT(&ring->lock);
43 LIST_INIT(&ring->waiters);
44 ring->readers_count = 0;
45 ring->ofs = 0;
46 ring->buf = b_make(area, size, 0, 0);
47 /* write the initial RC byte */
48 b_putchr(&ring->buf, 0);
49}
50
Willy Tarreau172945f2019-08-08 15:28:52 +020051/* Creates and returns a ring buffer of size <size> bytes. Returns NULL on
52 * allocation failure.
53 */
54struct ring *ring_new(size_t size)
55{
56 struct ring *ring = NULL;
57 void *area = NULL;
58
59 if (size < 2)
60 goto fail;
61
62 ring = malloc(sizeof(*ring));
63 if (!ring)
64 goto fail;
65
66 area = malloc(size);
67 if (!area)
68 goto fail;
69
Emeric Brune14b98c2021-01-12 14:21:00 +010070 ring_init(ring, area, size);
Willy Tarreau172945f2019-08-08 15:28:52 +020071 return ring;
72 fail:
73 free(area);
74 free(ring);
75 return NULL;
76}
77
Willy Tarreau6df10d82022-08-12 07:50:43 +020078/* Creates a unified ring + storage area at address <area> for <size> bytes.
79 * If <area> is null, then it's allocated of the requested size. The ring
80 * struct is part of the area so the usable area is slightly reduced. However
81 * the ring storage is immediately adjacent to the struct. ring_free() will
82 * ignore such rings, so the caller is responsible for releasing them.
83 */
84struct ring *ring_make_from_area(void *area, size_t size)
85{
86 struct ring *ring = NULL;
87
88 if (size < sizeof(ring))
89 return NULL;
90
91 if (!area)
92 area = malloc(size);
93 if (!area)
94 return NULL;
95
96 ring = area;
97 area += sizeof(*ring);
98 ring_init(ring, area, size - sizeof(*ring));
99 return ring;
100}
101
Willy Tarreau172945f2019-08-08 15:28:52 +0200102/* Resizes existing ring <ring> to <size> which must be larger, without losing
103 * its contents. The new size must be at least as large as the previous one or
104 * no change will be performed. The pointer to the ring is returned on success,
105 * or NULL on allocation failure. This will lock the ring for writes.
106 */
107struct ring *ring_resize(struct ring *ring, size_t size)
108{
109 void *area;
110
111 if (b_size(&ring->buf) >= size)
112 return ring;
113
114 area = malloc(size);
115 if (!area)
116 return NULL;
117
118 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
119
120 /* recheck the buffer's size, it may have changed during the malloc */
121 if (b_size(&ring->buf) < size) {
122 /* copy old contents */
123 b_getblk(&ring->buf, area, ring->buf.data, 0);
124 area = HA_ATOMIC_XCHG(&ring->buf.area, area);
125 ring->buf.size = size;
126 ring->buf.head = 0;
127 }
128
129 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
130
131 free(area);
132 return ring;
133}
134
135/* destroys and frees ring <ring> */
136void ring_free(struct ring *ring)
137{
138 if (!ring)
139 return;
Willy Tarreau6df10d82022-08-12 07:50:43 +0200140
141 /* make sure it was not allocated by ring_make_from_area */
142 if (ring->buf.area == (void *)ring + sizeof(*ring))
143 return;
144
Willy Tarreau172945f2019-08-08 15:28:52 +0200145 free(ring->buf.area);
146 free(ring);
147}
148
Willy Tarreaube978532019-08-27 11:44:13 +0200149/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
150 * to ring <ring>. The message is sent atomically. It may be truncated to
151 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
152 * two lists, it's just a convenience to help the caller prepend some prefixes
153 * when necessary. It takes the ring's write lock to make sure no other thread
154 * will touch the buffer during the update. Returns the number of bytes sent,
155 * or <=0 on failure.
156 */
157ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg)
158{
159 struct buffer *buf = &ring->buf;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200160 struct appctx *appctx;
Willy Tarreaube978532019-08-27 11:44:13 +0200161 size_t totlen = 0;
162 size_t lenlen;
Willy Tarreau30362902019-08-30 15:06:10 +0200163 uint64_t dellen;
Willy Tarreaube978532019-08-27 11:44:13 +0200164 int dellenlen;
165 ssize_t sent = 0;
166 int i;
167
168 /* we have to find some room to add our message (the buffer is
169 * never empty and at least contains the previous counter) and
170 * to update both the buffer contents and heads at the same
171 * time (it's doable using atomic ops but not worth the
172 * trouble, let's just lock). For this we first need to know
173 * the total message's length. We cannot measure it while
174 * copying due to the varint encoding of the length.
175 */
176 for (i = 0; i < npfx; i++)
177 totlen += pfx[i].len;
178 for (i = 0; i < nmsg; i++)
179 totlen += msg[i].len;
180
181 if (totlen > maxlen)
182 totlen = maxlen;
183
184 lenlen = varint_bytes(totlen);
185
186 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
187 if (lenlen + totlen + 1 + 1 > b_size(buf))
188 goto done_buf;
189
190 while (b_room(buf) < lenlen + totlen + 1) {
191 /* we need to delete the oldest message (from the end),
192 * and we have to stop if there's a reader stuck there.
193 * Unless there's corruption in the buffer it's guaranteed
194 * that we have enough data to find 1 counter byte, a
195 * varint-encoded length (1 byte min) and the message
196 * payload (0 bytes min).
197 */
198 if (*b_head(buf))
199 goto done_buf;
200 dellenlen = b_peek_varint(buf, 1, &dellen);
201 if (!dellenlen)
202 goto done_buf;
203 BUG_ON(b_data(buf) < 1 + dellenlen + dellen);
204
205 b_del(buf, 1 + dellenlen + dellen);
206 ring->ofs += 1 + dellenlen + dellen;
207 }
208
209 /* OK now we do have room */
210 __b_put_varint(buf, totlen);
211
212 totlen = 0;
213 for (i = 0; i < npfx; i++) {
214 size_t len = pfx[i].len;
215
216 if (len + totlen > maxlen)
217 len = maxlen - totlen;
218 if (len)
219 __b_putblk(buf, pfx[i].ptr, len);
220 totlen += len;
221 }
222
223 for (i = 0; i < nmsg; i++) {
224 size_t len = msg[i].len;
225
226 if (len + totlen > maxlen)
227 len = maxlen - totlen;
228 if (len)
229 __b_putblk(buf, msg[i].ptr, len);
230 totlen += len;
231 }
232
William Dauchy477757c2020-08-07 22:19:23 +0200233 *b_tail(buf) = 0; buf->data++; // new read counter
Willy Tarreaube978532019-08-27 11:44:13 +0200234 sent = lenlen + totlen + 1;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200235
236 /* notify potential readers */
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200237 list_for_each_entry(appctx, &ring->waiters, wait_entry)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200238 appctx_wakeup(appctx);
239
Willy Tarreaube978532019-08-27 11:44:13 +0200240 done_buf:
241 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
242 return sent;
243}
Willy Tarreau172945f2019-08-08 15:28:52 +0200244
Willy Tarreau928068a2020-05-19 19:14:42 +0200245/* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is
246 * meant to be used by low level appctx code such as CLI or ring forwarding.
247 * For higher level functions, please see the relevant parts in appctx or CLI.
248 * It returns non-zero on success or zero on failure if too many users are
249 * already attached. On success, the caller MUST call ring_detach_appctx()
250 * to detach itself, even if it was never woken up.
251 */
Emeric Brundcd58af2020-05-28 14:39:30 +0200252int ring_attach(struct ring *ring)
Willy Tarreau928068a2020-05-19 19:14:42 +0200253{
254 int users = ring->readers_count;
255
256 do {
257 if (users >= 255)
258 return 0;
259 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
260 return 1;
261}
262
263/* detach an appctx from a ring. The appctx is expected to be waiting at
264 * offset <ofs>. Nothing is done if <ring> is NULL.
265 */
266void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
267{
268 if (!ring)
269 return;
270
271 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
272 if (ofs != ~0) {
273 /* reader was still attached */
274 ofs -= ring->ofs;
275 BUG_ON(ofs >= b_size(&ring->buf));
276 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau4781b152021-04-06 13:53:36 +0200277 HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
Willy Tarreau928068a2020-05-19 19:14:42 +0200278 }
Willy Tarreau4781b152021-04-06 13:53:36 +0200279 HA_ATOMIC_DEC(&ring->readers_count);
Willy Tarreau928068a2020-05-19 19:14:42 +0200280 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
281}
282
Willy Tarreau072931c2019-08-27 11:55:39 +0200283/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
284 * meant to be used when registering a CLI function to dump a buffer, so it
285 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100286 * CLI context. It automatically sets the io_handler and io_release callbacks if
Willy Tarreaucba88382022-05-05 15:18:57 +0200287 * they were not set. The <flags> take a combination of RING_WF_*.
Willy Tarreau072931c2019-08-27 11:55:39 +0200288 */
Willy Tarreaucba88382022-05-05 15:18:57 +0200289int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags)
Willy Tarreau072931c2019-08-27 11:55:39 +0200290{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200291 struct show_ring_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
292
Emeric Brundcd58af2020-05-28 14:39:30 +0200293 if (!ring_attach(ring))
Willy Tarreau928068a2020-05-19 19:14:42 +0200294 return cli_err(appctx,
295 "Sorry, too many watchers (255) on this ring buffer. "
296 "What could it have so interesting to attract so many watchers ?");
Willy Tarreau072931c2019-08-27 11:55:39 +0200297
Willy Tarreaufcf94982019-11-15 15:07:21 +0100298 if (!appctx->io_handler)
299 appctx->io_handler = cli_io_handler_show_ring;
300 if (!appctx->io_release)
301 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200302
303 memset(ctx, 0, sizeof(*ctx));
304 ctx->ring = ring;
305 ctx->ofs = ~0; // start from the oldest event
306 ctx->flags = flags;
Willy Tarreau072931c2019-08-27 11:55:39 +0200307 return 0;
308}
309
310/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200311 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200312 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
313 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
314 * means it must seek directly to the end to wait for new contents. It returns
315 * 0 if the output buffer or events are missing is full and it needs to be
316 * called again, otherwise non-zero. It is meant to be used with
317 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200318 */
319int cli_io_handler_show_ring(struct appctx *appctx)
320{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200321 struct show_ring_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200322 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200323 struct ring *ring = ctx->ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200324 struct buffer *buf = &ring->buf;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200325 size_t ofs = ctx->ofs;
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200326 size_t last_ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200327 uint64_t msg_len;
328 size_t len, cnt;
329 int ret;
330
Willy Tarreau475e4632022-05-27 10:26:46 +0200331 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau072931c2019-08-27 11:55:39 +0200332 return 1;
333
Willy Tarreau223dded2020-05-19 19:21:45 +0200334 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200335 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200336 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
337
338 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200339
Willy Tarreau072931c2019-08-27 11:55:39 +0200340 /* explanation for the initialization below: it would be better to do
341 * this in the parsing function but this would occasionally result in
342 * dropped events because we'd take a reference on the oldest message
343 * and keep it while being scheduled. Thus instead let's take it the
344 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200345 * existing messages before grabbing a reference to a location. This
346 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200347 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200348 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200349 ofs = 0;
350
351 /* going to the end means looking at tail-1 */
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200352 if (ctx->flags & RING_WF_SEEK_NEW)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200353 ofs += b_data(buf) - 1;
354
Willy Tarreau4781b152021-04-06 13:53:36 +0200355 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau1d181e42019-08-30 11:17:01 +0200356 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200357 }
358
359 /* we were already there, adjust the offset to be relative to
360 * the buffer's head and remove us from the counter.
361 */
362 ofs -= ring->ofs;
363 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200364 HA_ATOMIC_DEC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200365
366 /* in this loop, ofs always points to the counter byte that precedes
367 * the message so that we can take our reference there if we have to
368 * stop before the end (ret=0).
369 */
370 ret = 1;
371 while (ofs + 1 < b_data(buf)) {
372 cnt = 1;
373 len = b_peek_varint(buf, ofs + cnt, &msg_len);
374 if (!len)
375 break;
376 cnt += len;
377 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
378
379 if (unlikely(msg_len + 1 > b_size(&trash))) {
380 /* too large a message to ever fit, let's skip it */
381 ofs += cnt + msg_len;
382 continue;
383 }
384
385 chunk_reset(&trash);
386 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
387 trash.data += len;
388 trash.area[trash.data++] = '\n';
389
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200390 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau072931c2019-08-27 11:55:39 +0200391 ret = 0;
392 break;
393 }
394 ofs += cnt + msg_len;
395 }
396
Willy Tarreau4781b152021-04-06 13:53:36 +0200397 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200398 ofs += ring->ofs;
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200399 last_ofs = ring->ofs;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200400 ctx->ofs = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200401 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200402
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200403 if (ret && (ctx->flags & RING_WF_WAIT_MODE)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200404 /* we've drained everything and are configured to wait for more
405 * data or an event (keypress, close)
406 */
Willy Tarreau475e4632022-05-27 10:26:46 +0200407 if (!sc_oc(sc)->output && !(sc_oc(sc)->flags & CF_SHUTW)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200408 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200409 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200410 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200411 ofs = ring->ofs;
Willy Tarreau223dded2020-05-19 19:21:45 +0200412 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200413 if (ofs != last_ofs) {
414 /* more data was added into the ring between the
415 * unlock and the lock, and the writer might not
416 * have seen us. We need to reschedule a read.
417 */
418 applet_have_more_data(appctx);
419 } else
420 applet_have_no_more_data(appctx);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200421 ret = 0;
422 }
423 /* always drain all the request */
Willy Tarreau475e4632022-05-27 10:26:46 +0200424 co_skip(sc_oc(sc), sc_oc(sc)->output);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200425 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200426 return ret;
427}
428
429/* must be called after cli_io_handler_show_ring() above */
430void cli_io_release_show_ring(struct appctx *appctx)
431{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200432 struct show_ring_ctx *ctx = appctx->svcctx;
433 struct ring *ring = ctx->ring;
434 size_t ofs = ctx->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200435
Willy Tarreau928068a2020-05-19 19:14:42 +0200436 ring_detach_appctx(ring, appctx, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200437}
438
439
Willy Tarreau172945f2019-08-08 15:28:52 +0200440/*
441 * Local variables:
442 * c-indent-level: 8
443 * c-basic-offset: 8
444 * End:
445 */