blob: 7c36f7bef49c68bf0bbc0539fa548aa650cf7c5e [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
78/* Resizes existing ring <ring> to <size> which must be larger, without losing
79 * its contents. The new size must be at least as large as the previous one or
80 * no change will be performed. The pointer to the ring is returned on success,
81 * or NULL on allocation failure. This will lock the ring for writes.
82 */
83struct ring *ring_resize(struct ring *ring, size_t size)
84{
85 void *area;
86
87 if (b_size(&ring->buf) >= size)
88 return ring;
89
90 area = malloc(size);
91 if (!area)
92 return NULL;
93
94 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
95
96 /* recheck the buffer's size, it may have changed during the malloc */
97 if (b_size(&ring->buf) < size) {
98 /* copy old contents */
99 b_getblk(&ring->buf, area, ring->buf.data, 0);
100 area = HA_ATOMIC_XCHG(&ring->buf.area, area);
101 ring->buf.size = size;
102 ring->buf.head = 0;
103 }
104
105 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
106
107 free(area);
108 return ring;
109}
110
111/* destroys and frees ring <ring> */
112void ring_free(struct ring *ring)
113{
114 if (!ring)
115 return;
116 free(ring->buf.area);
117 free(ring);
118}
119
Willy Tarreaube978532019-08-27 11:44:13 +0200120/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
121 * to ring <ring>. The message is sent atomically. It may be truncated to
122 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
123 * two lists, it's just a convenience to help the caller prepend some prefixes
124 * when necessary. It takes the ring's write lock to make sure no other thread
125 * will touch the buffer during the update. Returns the number of bytes sent,
126 * or <=0 on failure.
127 */
128ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg)
129{
130 struct buffer *buf = &ring->buf;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200131 struct appctx *appctx;
Willy Tarreaube978532019-08-27 11:44:13 +0200132 size_t totlen = 0;
133 size_t lenlen;
Willy Tarreau30362902019-08-30 15:06:10 +0200134 uint64_t dellen;
Willy Tarreaube978532019-08-27 11:44:13 +0200135 int dellenlen;
136 ssize_t sent = 0;
137 int i;
138
139 /* we have to find some room to add our message (the buffer is
140 * never empty and at least contains the previous counter) and
141 * to update both the buffer contents and heads at the same
142 * time (it's doable using atomic ops but not worth the
143 * trouble, let's just lock). For this we first need to know
144 * the total message's length. We cannot measure it while
145 * copying due to the varint encoding of the length.
146 */
147 for (i = 0; i < npfx; i++)
148 totlen += pfx[i].len;
149 for (i = 0; i < nmsg; i++)
150 totlen += msg[i].len;
151
152 if (totlen > maxlen)
153 totlen = maxlen;
154
155 lenlen = varint_bytes(totlen);
156
157 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
158 if (lenlen + totlen + 1 + 1 > b_size(buf))
159 goto done_buf;
160
161 while (b_room(buf) < lenlen + totlen + 1) {
162 /* we need to delete the oldest message (from the end),
163 * and we have to stop if there's a reader stuck there.
164 * Unless there's corruption in the buffer it's guaranteed
165 * that we have enough data to find 1 counter byte, a
166 * varint-encoded length (1 byte min) and the message
167 * payload (0 bytes min).
168 */
169 if (*b_head(buf))
170 goto done_buf;
171 dellenlen = b_peek_varint(buf, 1, &dellen);
172 if (!dellenlen)
173 goto done_buf;
174 BUG_ON(b_data(buf) < 1 + dellenlen + dellen);
175
176 b_del(buf, 1 + dellenlen + dellen);
177 ring->ofs += 1 + dellenlen + dellen;
178 }
179
180 /* OK now we do have room */
181 __b_put_varint(buf, totlen);
182
183 totlen = 0;
184 for (i = 0; i < npfx; i++) {
185 size_t len = pfx[i].len;
186
187 if (len + totlen > maxlen)
188 len = maxlen - totlen;
189 if (len)
190 __b_putblk(buf, pfx[i].ptr, len);
191 totlen += len;
192 }
193
194 for (i = 0; i < nmsg; i++) {
195 size_t len = msg[i].len;
196
197 if (len + totlen > maxlen)
198 len = maxlen - totlen;
199 if (len)
200 __b_putblk(buf, msg[i].ptr, len);
201 totlen += len;
202 }
203
William Dauchy477757c2020-08-07 22:19:23 +0200204 *b_tail(buf) = 0; buf->data++; // new read counter
Willy Tarreaube978532019-08-27 11:44:13 +0200205 sent = lenlen + totlen + 1;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200206
207 /* notify potential readers */
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200208 list_for_each_entry(appctx, &ring->waiters, wait_entry)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200209 appctx_wakeup(appctx);
210
Willy Tarreaube978532019-08-27 11:44:13 +0200211 done_buf:
212 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
213 return sent;
214}
Willy Tarreau172945f2019-08-08 15:28:52 +0200215
Willy Tarreau928068a2020-05-19 19:14:42 +0200216/* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is
217 * meant to be used by low level appctx code such as CLI or ring forwarding.
218 * For higher level functions, please see the relevant parts in appctx or CLI.
219 * It returns non-zero on success or zero on failure if too many users are
220 * already attached. On success, the caller MUST call ring_detach_appctx()
221 * to detach itself, even if it was never woken up.
222 */
Emeric Brundcd58af2020-05-28 14:39:30 +0200223int ring_attach(struct ring *ring)
Willy Tarreau928068a2020-05-19 19:14:42 +0200224{
225 int users = ring->readers_count;
226
227 do {
228 if (users >= 255)
229 return 0;
230 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
231 return 1;
232}
233
234/* detach an appctx from a ring. The appctx is expected to be waiting at
235 * offset <ofs>. Nothing is done if <ring> is NULL.
236 */
237void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
238{
239 if (!ring)
240 return;
241
242 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
243 if (ofs != ~0) {
244 /* reader was still attached */
245 ofs -= ring->ofs;
246 BUG_ON(ofs >= b_size(&ring->buf));
247 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau4781b152021-04-06 13:53:36 +0200248 HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
Willy Tarreau928068a2020-05-19 19:14:42 +0200249 }
Willy Tarreau4781b152021-04-06 13:53:36 +0200250 HA_ATOMIC_DEC(&ring->readers_count);
Willy Tarreau928068a2020-05-19 19:14:42 +0200251 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
252}
253
Willy Tarreau072931c2019-08-27 11:55:39 +0200254/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
255 * meant to be used when registering a CLI function to dump a buffer, so it
256 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100257 * CLI context. It automatically sets the io_handler and io_release callbacks if
Willy Tarreaucba88382022-05-05 15:18:57 +0200258 * they were not set. The <flags> take a combination of RING_WF_*.
Willy Tarreau072931c2019-08-27 11:55:39 +0200259 */
Willy Tarreaucba88382022-05-05 15:18:57 +0200260int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags)
Willy Tarreau072931c2019-08-27 11:55:39 +0200261{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200262 struct show_ring_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
263
Emeric Brundcd58af2020-05-28 14:39:30 +0200264 if (!ring_attach(ring))
Willy Tarreau928068a2020-05-19 19:14:42 +0200265 return cli_err(appctx,
266 "Sorry, too many watchers (255) on this ring buffer. "
267 "What could it have so interesting to attract so many watchers ?");
Willy Tarreau072931c2019-08-27 11:55:39 +0200268
Willy Tarreaufcf94982019-11-15 15:07:21 +0100269 if (!appctx->io_handler)
270 appctx->io_handler = cli_io_handler_show_ring;
271 if (!appctx->io_release)
272 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200273
274 memset(ctx, 0, sizeof(*ctx));
275 ctx->ring = ring;
276 ctx->ofs = ~0; // start from the oldest event
277 ctx->flags = flags;
Willy Tarreau072931c2019-08-27 11:55:39 +0200278 return 0;
279}
280
281/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200282 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200283 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
284 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
285 * means it must seek directly to the end to wait for new contents. It returns
286 * 0 if the output buffer or events are missing is full and it needs to be
287 * called again, otherwise non-zero. It is meant to be used with
288 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200289 */
290int cli_io_handler_show_ring(struct appctx *appctx)
291{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200292 struct show_ring_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200293 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200294 struct ring *ring = ctx->ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200295 struct buffer *buf = &ring->buf;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200296 size_t ofs = ctx->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200297 uint64_t msg_len;
298 size_t len, cnt;
299 int ret;
300
Willy Tarreau475e4632022-05-27 10:26:46 +0200301 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau072931c2019-08-27 11:55:39 +0200302 return 1;
303
Willy Tarreau223dded2020-05-19 19:21:45 +0200304 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200305 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200306 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
307
308 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200309
Willy Tarreau072931c2019-08-27 11:55:39 +0200310 /* explanation for the initialization below: it would be better to do
311 * this in the parsing function but this would occasionally result in
312 * dropped events because we'd take a reference on the oldest message
313 * and keep it while being scheduled. Thus instead let's take it the
314 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200315 * existing messages before grabbing a reference to a location. This
316 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200317 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200318 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200319 ofs = 0;
320
321 /* going to the end means looking at tail-1 */
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200322 if (ctx->flags & RING_WF_SEEK_NEW)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200323 ofs += b_data(buf) - 1;
324
Willy Tarreau4781b152021-04-06 13:53:36 +0200325 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau1d181e42019-08-30 11:17:01 +0200326 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200327 }
328
329 /* we were already there, adjust the offset to be relative to
330 * the buffer's head and remove us from the counter.
331 */
332 ofs -= ring->ofs;
333 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200334 HA_ATOMIC_DEC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200335
336 /* in this loop, ofs always points to the counter byte that precedes
337 * the message so that we can take our reference there if we have to
338 * stop before the end (ret=0).
339 */
340 ret = 1;
341 while (ofs + 1 < b_data(buf)) {
342 cnt = 1;
343 len = b_peek_varint(buf, ofs + cnt, &msg_len);
344 if (!len)
345 break;
346 cnt += len;
347 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
348
349 if (unlikely(msg_len + 1 > b_size(&trash))) {
350 /* too large a message to ever fit, let's skip it */
351 ofs += cnt + msg_len;
352 continue;
353 }
354
355 chunk_reset(&trash);
356 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
357 trash.data += len;
358 trash.area[trash.data++] = '\n';
359
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200360 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau072931c2019-08-27 11:55:39 +0200361 ret = 0;
362 break;
363 }
364 ofs += cnt + msg_len;
365 }
366
Willy Tarreau4781b152021-04-06 13:53:36 +0200367 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200368 ofs += ring->ofs;
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200369 ctx->ofs = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200370 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200371
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200372 if (ret && (ctx->flags & RING_WF_WAIT_MODE)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200373 /* we've drained everything and are configured to wait for more
374 * data or an event (keypress, close)
375 */
Willy Tarreau475e4632022-05-27 10:26:46 +0200376 if (!sc_oc(sc)->output && !(sc_oc(sc)->flags & CF_SHUTW)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200377 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200378 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200379 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200380 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200381 applet_have_no_more_data(appctx);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200382 ret = 0;
383 }
384 /* always drain all the request */
Willy Tarreau475e4632022-05-27 10:26:46 +0200385 co_skip(sc_oc(sc), sc_oc(sc)->output);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200386 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200387 return ret;
388}
389
390/* must be called after cli_io_handler_show_ring() above */
391void cli_io_release_show_ring(struct appctx *appctx)
392{
Willy Tarreau6e3fc482022-05-05 15:29:43 +0200393 struct show_ring_ctx *ctx = appctx->svcctx;
394 struct ring *ring = ctx->ring;
395 size_t ofs = ctx->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200396
Willy Tarreau928068a2020-05-19 19:14:42 +0200397 ring_detach_appctx(ring, appctx, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200398}
399
400
Willy Tarreau172945f2019-08-08 15:28:52 +0200401/*
402 * Local variables:
403 * c-indent-level: 8
404 * c-basic-offset: 8
405 * End:
406 */