blob: 2b8e3c85e9a17014203d1312a5d5d6ab28125f72 [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 */
Willy Tarreaud9c71882023-02-22 14:50:14 +010034 size_t ofs; /* storage offset to restart from; ~0=oldest */
Willy Tarreau6e3fc482022-05-05 15:29:43 +020035 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;
Emeric Brune14b98c2021-01-12 14:21:00 +010045 ring->buf = b_make(area, size, 0, 0);
46 /* write the initial RC byte */
47 b_putchr(&ring->buf, 0);
48}
49
Willy Tarreau172945f2019-08-08 15:28:52 +020050/* Creates and returns a ring buffer of size <size> bytes. Returns NULL on
51 * allocation failure.
52 */
53struct ring *ring_new(size_t size)
54{
55 struct ring *ring = NULL;
56 void *area = NULL;
57
58 if (size < 2)
59 goto fail;
60
61 ring = malloc(sizeof(*ring));
62 if (!ring)
63 goto fail;
64
65 area = malloc(size);
66 if (!area)
67 goto fail;
68
Emeric Brune14b98c2021-01-12 14:21:00 +010069 ring_init(ring, area, size);
Willy Tarreau172945f2019-08-08 15:28:52 +020070 return ring;
71 fail:
72 free(area);
73 free(ring);
74 return NULL;
75}
76
Willy Tarreau6df10d82022-08-12 07:50:43 +020077/* Creates a unified ring + storage area at address <area> for <size> bytes.
78 * If <area> is null, then it's allocated of the requested size. The ring
79 * struct is part of the area so the usable area is slightly reduced. However
80 * the ring storage is immediately adjacent to the struct. ring_free() will
81 * ignore such rings, so the caller is responsible for releasing them.
82 */
83struct ring *ring_make_from_area(void *area, size_t size)
84{
85 struct ring *ring = NULL;
86
William Lallemand3a374ea2022-09-27 14:31:37 +020087 if (size < sizeof(*ring))
Willy Tarreau6df10d82022-08-12 07:50:43 +020088 return NULL;
89
90 if (!area)
91 area = malloc(size);
92 if (!area)
93 return NULL;
94
95 ring = area;
96 area += sizeof(*ring);
97 ring_init(ring, area, size - sizeof(*ring));
98 return ring;
99}
100
William Lallemand9e4ead32022-09-27 15:53:53 +0200101/* Cast an unified ring + storage area to a ring from <area>, without
102 * reinitializing the data buffer.
103 *
104 * Reinitialize the waiters and the lock.
105 */
106struct ring *ring_cast_from_area(void *area)
107{
108 struct ring *ring = NULL;
109
110 ring = area;
111 ring->buf.area = area + sizeof(*ring);
112
113 HA_RWLOCK_INIT(&ring->lock);
114 LIST_INIT(&ring->waiters);
115 ring->readers_count = 0;
116
117 return ring;
118}
119
Willy Tarreau172945f2019-08-08 15:28:52 +0200120/* Resizes existing ring <ring> to <size> which must be larger, without losing
121 * its contents. The new size must be at least as large as the previous one or
122 * no change will be performed. The pointer to the ring is returned on success,
123 * or NULL on allocation failure. This will lock the ring for writes.
124 */
125struct ring *ring_resize(struct ring *ring, size_t size)
126{
127 void *area;
128
129 if (b_size(&ring->buf) >= size)
130 return ring;
131
132 area = malloc(size);
133 if (!area)
134 return NULL;
135
136 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
137
138 /* recheck the buffer's size, it may have changed during the malloc */
139 if (b_size(&ring->buf) < size) {
140 /* copy old contents */
141 b_getblk(&ring->buf, area, ring->buf.data, 0);
142 area = HA_ATOMIC_XCHG(&ring->buf.area, area);
143 ring->buf.size = size;
Willy Tarreau172945f2019-08-08 15:28:52 +0200144 }
145
146 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
147
148 free(area);
149 return ring;
150}
151
152/* destroys and frees ring <ring> */
153void ring_free(struct ring *ring)
154{
155 if (!ring)
156 return;
Willy Tarreau6df10d82022-08-12 07:50:43 +0200157
158 /* make sure it was not allocated by ring_make_from_area */
159 if (ring->buf.area == (void *)ring + sizeof(*ring))
160 return;
161
Willy Tarreau172945f2019-08-08 15:28:52 +0200162 free(ring->buf.area);
163 free(ring);
164}
165
Willy Tarreaube978532019-08-27 11:44:13 +0200166/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
167 * to ring <ring>. The message is sent atomically. It may be truncated to
168 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
169 * two lists, it's just a convenience to help the caller prepend some prefixes
170 * when necessary. It takes the ring's write lock to make sure no other thread
171 * will touch the buffer during the update. Returns the number of bytes sent,
172 * or <=0 on failure.
173 */
174ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg)
175{
176 struct buffer *buf = &ring->buf;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200177 struct appctx *appctx;
Willy Tarreaube978532019-08-27 11:44:13 +0200178 size_t totlen = 0;
179 size_t lenlen;
Willy Tarreau30362902019-08-30 15:06:10 +0200180 uint64_t dellen;
Willy Tarreaube978532019-08-27 11:44:13 +0200181 int dellenlen;
182 ssize_t sent = 0;
183 int i;
184
185 /* we have to find some room to add our message (the buffer is
186 * never empty and at least contains the previous counter) and
187 * to update both the buffer contents and heads at the same
188 * time (it's doable using atomic ops but not worth the
189 * trouble, let's just lock). For this we first need to know
190 * the total message's length. We cannot measure it while
191 * copying due to the varint encoding of the length.
192 */
193 for (i = 0; i < npfx; i++)
194 totlen += pfx[i].len;
195 for (i = 0; i < nmsg; i++)
196 totlen += msg[i].len;
197
198 if (totlen > maxlen)
199 totlen = maxlen;
200
201 lenlen = varint_bytes(totlen);
202
203 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
204 if (lenlen + totlen + 1 + 1 > b_size(buf))
205 goto done_buf;
206
207 while (b_room(buf) < lenlen + totlen + 1) {
208 /* we need to delete the oldest message (from the end),
209 * and we have to stop if there's a reader stuck there.
210 * Unless there's corruption in the buffer it's guaranteed
211 * that we have enough data to find 1 counter byte, a
212 * varint-encoded length (1 byte min) and the message
213 * payload (0 bytes min).
214 */
215 if (*b_head(buf))
216 goto done_buf;
217 dellenlen = b_peek_varint(buf, 1, &dellen);
218 if (!dellenlen)
219 goto done_buf;
220 BUG_ON(b_data(buf) < 1 + dellenlen + dellen);
221
222 b_del(buf, 1 + dellenlen + dellen);
Willy Tarreaube978532019-08-27 11:44:13 +0200223 }
224
225 /* OK now we do have room */
226 __b_put_varint(buf, totlen);
227
228 totlen = 0;
229 for (i = 0; i < npfx; i++) {
230 size_t len = pfx[i].len;
231
232 if (len + totlen > maxlen)
233 len = maxlen - totlen;
234 if (len)
235 __b_putblk(buf, pfx[i].ptr, len);
236 totlen += len;
237 }
238
239 for (i = 0; i < nmsg; i++) {
240 size_t len = msg[i].len;
241
242 if (len + totlen > maxlen)
243 len = maxlen - totlen;
244 if (len)
245 __b_putblk(buf, msg[i].ptr, len);
246 totlen += len;
247 }
248
William Dauchy477757c2020-08-07 22:19:23 +0200249 *b_tail(buf) = 0; buf->data++; // new read counter
Willy Tarreaube978532019-08-27 11:44:13 +0200250 sent = lenlen + totlen + 1;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200251
252 /* notify potential readers */
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200253 list_for_each_entry(appctx, &ring->waiters, wait_entry)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200254 appctx_wakeup(appctx);
255
Willy Tarreaube978532019-08-27 11:44:13 +0200256 done_buf:
257 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
258 return sent;
259}
Willy Tarreau172945f2019-08-08 15:28:52 +0200260
Willy Tarreau928068a2020-05-19 19:14:42 +0200261/* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is
262 * meant to be used by low level appctx code such as CLI or ring forwarding.
263 * For higher level functions, please see the relevant parts in appctx or CLI.
264 * It returns non-zero on success or zero on failure if too many users are
265 * already attached. On success, the caller MUST call ring_detach_appctx()
266 * to detach itself, even if it was never woken up.
267 */
Emeric Brundcd58af2020-05-28 14:39:30 +0200268int ring_attach(struct ring *ring)
Willy Tarreau928068a2020-05-19 19:14:42 +0200269{
270 int users = ring->readers_count;
271
272 do {
273 if (users >= 255)
274 return 0;
275 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
276 return 1;
277}
278
Willy Tarreaud9c71882023-02-22 14:50:14 +0100279/* detach an appctx from a ring. The appctx is expected to be waiting at offset
280 * <ofs> relative to the beginning of the storage, or ~0 if not waiting yet.
281 * Nothing is done if <ring> is NULL.
Willy Tarreau928068a2020-05-19 19:14:42 +0200282 */
283void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
284{
285 if (!ring)
286 return;
287
288 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
289 if (ofs != ~0) {
290 /* reader was still attached */
Willy Tarreaud9c71882023-02-22 14:50:14 +0100291 if (ofs < b_head_ofs(&ring->buf))
292 ofs += b_size(&ring->buf) - b_head_ofs(&ring->buf);
293 else
294 ofs -= b_head_ofs(&ring->buf);
295
Willy Tarreau928068a2020-05-19 19:14:42 +0200296 BUG_ON(ofs >= b_size(&ring->buf));
297 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau4781b152021-04-06 13:53:36 +0200298 HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
Willy Tarreau928068a2020-05-19 19:14:42 +0200299 }
Willy Tarreau4781b152021-04-06 13:53:36 +0200300 HA_ATOMIC_DEC(&ring->readers_count);
Willy Tarreau928068a2020-05-19 19:14:42 +0200301 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
302}
303
Willy Tarreau072931c2019-08-27 11:55:39 +0200304/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
305 * meant to be used when registering a CLI function to dump a buffer, so it
306 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100307 * CLI context. It automatically sets the io_handler and io_release callbacks if
Willy Tarreaucba88382022-05-05 15:18:57 +0200308 * they were not set. The <flags> take a combination of RING_WF_*.
Willy Tarreau072931c2019-08-27 11:55:39 +0200309 */
Willy Tarreaucba88382022-05-05 15:18:57 +0200310int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags)
Willy Tarreau072931c2019-08-27 11:55:39 +0200311{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200312 struct show_ring_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
313
Emeric Brundcd58af2020-05-28 14:39:30 +0200314 if (!ring_attach(ring))
Willy Tarreau928068a2020-05-19 19:14:42 +0200315 return cli_err(appctx,
316 "Sorry, too many watchers (255) on this ring buffer. "
317 "What could it have so interesting to attract so many watchers ?");
Willy Tarreau072931c2019-08-27 11:55:39 +0200318
Willy Tarreaufcf94982019-11-15 15:07:21 +0100319 if (!appctx->io_handler)
320 appctx->io_handler = cli_io_handler_show_ring;
321 if (!appctx->io_release)
322 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200323
324 memset(ctx, 0, sizeof(*ctx));
325 ctx->ring = ring;
326 ctx->ofs = ~0; // start from the oldest event
327 ctx->flags = flags;
Willy Tarreau072931c2019-08-27 11:55:39 +0200328 return 0;
329}
330
331/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200332 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200333 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
334 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
335 * means it must seek directly to the end to wait for new contents. It returns
336 * 0 if the output buffer or events are missing is full and it needs to be
337 * called again, otherwise non-zero. It is meant to be used with
338 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200339 */
340int cli_io_handler_show_ring(struct appctx *appctx)
341{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200342 struct show_ring_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200343 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200344 struct ring *ring = ctx->ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200345 struct buffer *buf = &ring->buf;
Willy Tarreaud9c71882023-02-22 14:50:14 +0100346 size_t ofs;
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200347 size_t last_ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200348 uint64_t msg_len;
349 size_t len, cnt;
350 int ret;
351
Christopher Faulet87633c32023-04-03 18:32:50 +0200352 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +0200353 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau072931c2019-08-27 11:55:39 +0200354 return 1;
355
Willy Tarreau223dded2020-05-19 19:21:45 +0200356 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200357 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200358 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
359
360 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200361
Willy Tarreau072931c2019-08-27 11:55:39 +0200362 /* explanation for the initialization below: it would be better to do
363 * this in the parsing function but this would occasionally result in
364 * dropped events because we'd take a reference on the oldest message
365 * and keep it while being scheduled. Thus instead let's take it the
366 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200367 * existing messages before grabbing a reference to a location. This
368 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200369 */
Willy Tarreaud9c71882023-02-22 14:50:14 +0100370 if (unlikely(ctx->ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200371 /* going to the end means looking at tail-1 */
Willy Tarreaud9c71882023-02-22 14:50:14 +0100372 ctx->ofs = b_peek_ofs(buf, (ctx->flags & RING_WF_SEEK_NEW) ? b_data(buf) - 1 : 0);
373 HA_ATOMIC_INC(b_orig(buf) + ctx->ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200374 }
375
376 /* we were already there, adjust the offset to be relative to
377 * the buffer's head and remove us from the counter.
378 */
Willy Tarreaud9c71882023-02-22 14:50:14 +0100379 ofs = ctx->ofs - b_head_ofs(buf);
380 if (ctx->ofs < b_head_ofs(buf))
381 ofs += b_size(buf);
382
Willy Tarreau072931c2019-08-27 11:55:39 +0200383 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200384 HA_ATOMIC_DEC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200385
386 /* in this loop, ofs always points to the counter byte that precedes
387 * the message so that we can take our reference there if we have to
388 * stop before the end (ret=0).
389 */
390 ret = 1;
391 while (ofs + 1 < b_data(buf)) {
392 cnt = 1;
393 len = b_peek_varint(buf, ofs + cnt, &msg_len);
394 if (!len)
395 break;
396 cnt += len;
397 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
398
399 if (unlikely(msg_len + 1 > b_size(&trash))) {
400 /* too large a message to ever fit, let's skip it */
401 ofs += cnt + msg_len;
402 continue;
403 }
404
405 chunk_reset(&trash);
406 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
407 trash.data += len;
408 trash.area[trash.data++] = '\n';
409
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200410 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau072931c2019-08-27 11:55:39 +0200411 ret = 0;
412 break;
413 }
414 ofs += cnt + msg_len;
415 }
416
Willy Tarreau4781b152021-04-06 13:53:36 +0200417 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreaud9c71882023-02-22 14:50:14 +0100418 last_ofs = b_tail_ofs(buf);
419 ctx->ofs = b_peek_ofs(buf, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200420 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200421
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200422 if (ret && (ctx->flags & RING_WF_WAIT_MODE)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200423 /* we've drained everything and are configured to wait for more
424 * data or an event (keypress, close)
425 */
Christopher Faulet208c7122023-04-13 16:16:15 +0200426 if (!sc_oc(sc)->output && !(sc->flags & SC_FL_SHUT_DONE)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200427 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200428 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200429 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreaud9c71882023-02-22 14:50:14 +0100430 ofs = b_tail_ofs(&ring->buf);
Willy Tarreau223dded2020-05-19 19:21:45 +0200431 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreaub8e0fb92022-08-04 17:00:21 +0200432 if (ofs != last_ofs) {
433 /* more data was added into the ring between the
434 * unlock and the lock, and the writer might not
435 * have seen us. We need to reschedule a read.
436 */
437 applet_have_more_data(appctx);
438 } else
439 applet_have_no_more_data(appctx);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200440 ret = 0;
441 }
442 /* always drain all the request */
Willy Tarreau475e4632022-05-27 10:26:46 +0200443 co_skip(sc_oc(sc), sc_oc(sc)->output);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200444 }
Christopher Faulet35219a52023-09-06 09:26:06 +0200445
446 applet_expect_no_data(appctx);
Willy Tarreau072931c2019-08-27 11:55:39 +0200447 return ret;
448}
449
450/* must be called after cli_io_handler_show_ring() above */
451void cli_io_release_show_ring(struct appctx *appctx)
452{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200453 struct show_ring_ctx *ctx = appctx->svcctx;
454 struct ring *ring = ctx->ring;
455 size_t ofs = ctx->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200456
Willy Tarreau928068a2020-05-19 19:14:42 +0200457 ring_detach_appctx(ring, appctx, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200458}
459
460
Willy Tarreau172945f2019-08-08 15:28:52 +0200461/*
462 * Local variables:
463 * c-indent-level: 8
464 * c-basic-offset: 8
465 * End:
466 */