blob: 1668ee3872d965a0d7dcd65840be3f779920f284 [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>
Christopher Faulet908628c2022-03-25 16:43:49 +010026#include <haproxy/conn_stream.h>
27#include <haproxy/cs_utils.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020028#include <haproxy/ring.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020029#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/thread.h>
Willy Tarreau172945f2019-08-08 15:28:52 +020031
Emeric Brune14b98c2021-01-12 14:21:00 +010032/* Initialize a pre-allocated ring with the buffer area
33 * of size */
34void ring_init(struct ring *ring, void *area, size_t size)
35{
36 HA_RWLOCK_INIT(&ring->lock);
37 LIST_INIT(&ring->waiters);
38 ring->readers_count = 0;
39 ring->ofs = 0;
40 ring->buf = b_make(area, size, 0, 0);
41 /* write the initial RC byte */
42 b_putchr(&ring->buf, 0);
43}
44
Willy Tarreau172945f2019-08-08 15:28:52 +020045/* Creates and returns a ring buffer of size <size> bytes. Returns NULL on
46 * allocation failure.
47 */
48struct ring *ring_new(size_t size)
49{
50 struct ring *ring = NULL;
51 void *area = NULL;
52
53 if (size < 2)
54 goto fail;
55
56 ring = malloc(sizeof(*ring));
57 if (!ring)
58 goto fail;
59
60 area = malloc(size);
61 if (!area)
62 goto fail;
63
Emeric Brune14b98c2021-01-12 14:21:00 +010064 ring_init(ring, area, size);
Willy Tarreau172945f2019-08-08 15:28:52 +020065 return ring;
66 fail:
67 free(area);
68 free(ring);
69 return NULL;
70}
71
72/* Resizes existing ring <ring> to <size> which must be larger, without losing
73 * its contents. The new size must be at least as large as the previous one or
74 * no change will be performed. The pointer to the ring is returned on success,
75 * or NULL on allocation failure. This will lock the ring for writes.
76 */
77struct ring *ring_resize(struct ring *ring, size_t size)
78{
79 void *area;
80
81 if (b_size(&ring->buf) >= size)
82 return ring;
83
84 area = malloc(size);
85 if (!area)
86 return NULL;
87
88 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
89
90 /* recheck the buffer's size, it may have changed during the malloc */
91 if (b_size(&ring->buf) < size) {
92 /* copy old contents */
93 b_getblk(&ring->buf, area, ring->buf.data, 0);
94 area = HA_ATOMIC_XCHG(&ring->buf.area, area);
95 ring->buf.size = size;
96 ring->buf.head = 0;
97 }
98
99 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
100
101 free(area);
102 return ring;
103}
104
105/* destroys and frees ring <ring> */
106void ring_free(struct ring *ring)
107{
108 if (!ring)
109 return;
110 free(ring->buf.area);
111 free(ring);
112}
113
Willy Tarreaube978532019-08-27 11:44:13 +0200114/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
115 * to ring <ring>. The message is sent atomically. It may be truncated to
116 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
117 * two lists, it's just a convenience to help the caller prepend some prefixes
118 * when necessary. It takes the ring's write lock to make sure no other thread
119 * will touch the buffer during the update. Returns the number of bytes sent,
120 * or <=0 on failure.
121 */
122ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg)
123{
124 struct buffer *buf = &ring->buf;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200125 struct appctx *appctx;
Willy Tarreaube978532019-08-27 11:44:13 +0200126 size_t totlen = 0;
127 size_t lenlen;
Willy Tarreau30362902019-08-30 15:06:10 +0200128 uint64_t dellen;
Willy Tarreaube978532019-08-27 11:44:13 +0200129 int dellenlen;
130 ssize_t sent = 0;
131 int i;
132
133 /* we have to find some room to add our message (the buffer is
134 * never empty and at least contains the previous counter) and
135 * to update both the buffer contents and heads at the same
136 * time (it's doable using atomic ops but not worth the
137 * trouble, let's just lock). For this we first need to know
138 * the total message's length. We cannot measure it while
139 * copying due to the varint encoding of the length.
140 */
141 for (i = 0; i < npfx; i++)
142 totlen += pfx[i].len;
143 for (i = 0; i < nmsg; i++)
144 totlen += msg[i].len;
145
146 if (totlen > maxlen)
147 totlen = maxlen;
148
149 lenlen = varint_bytes(totlen);
150
151 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
152 if (lenlen + totlen + 1 + 1 > b_size(buf))
153 goto done_buf;
154
155 while (b_room(buf) < lenlen + totlen + 1) {
156 /* we need to delete the oldest message (from the end),
157 * and we have to stop if there's a reader stuck there.
158 * Unless there's corruption in the buffer it's guaranteed
159 * that we have enough data to find 1 counter byte, a
160 * varint-encoded length (1 byte min) and the message
161 * payload (0 bytes min).
162 */
163 if (*b_head(buf))
164 goto done_buf;
165 dellenlen = b_peek_varint(buf, 1, &dellen);
166 if (!dellenlen)
167 goto done_buf;
168 BUG_ON(b_data(buf) < 1 + dellenlen + dellen);
169
170 b_del(buf, 1 + dellenlen + dellen);
171 ring->ofs += 1 + dellenlen + dellen;
172 }
173
174 /* OK now we do have room */
175 __b_put_varint(buf, totlen);
176
177 totlen = 0;
178 for (i = 0; i < npfx; i++) {
179 size_t len = pfx[i].len;
180
181 if (len + totlen > maxlen)
182 len = maxlen - totlen;
183 if (len)
184 __b_putblk(buf, pfx[i].ptr, len);
185 totlen += len;
186 }
187
188 for (i = 0; i < nmsg; i++) {
189 size_t len = msg[i].len;
190
191 if (len + totlen > maxlen)
192 len = maxlen - totlen;
193 if (len)
194 __b_putblk(buf, msg[i].ptr, len);
195 totlen += len;
196 }
197
William Dauchy477757c2020-08-07 22:19:23 +0200198 *b_tail(buf) = 0; buf->data++; // new read counter
Willy Tarreaube978532019-08-27 11:44:13 +0200199 sent = lenlen + totlen + 1;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200200
201 /* notify potential readers */
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200202 list_for_each_entry(appctx, &ring->waiters, wait_entry)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200203 appctx_wakeup(appctx);
204
Willy Tarreaube978532019-08-27 11:44:13 +0200205 done_buf:
206 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
207 return sent;
208}
Willy Tarreau172945f2019-08-08 15:28:52 +0200209
Willy Tarreau928068a2020-05-19 19:14:42 +0200210/* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is
211 * meant to be used by low level appctx code such as CLI or ring forwarding.
212 * For higher level functions, please see the relevant parts in appctx or CLI.
213 * It returns non-zero on success or zero on failure if too many users are
214 * already attached. On success, the caller MUST call ring_detach_appctx()
215 * to detach itself, even if it was never woken up.
216 */
Emeric Brundcd58af2020-05-28 14:39:30 +0200217int ring_attach(struct ring *ring)
Willy Tarreau928068a2020-05-19 19:14:42 +0200218{
219 int users = ring->readers_count;
220
221 do {
222 if (users >= 255)
223 return 0;
224 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
225 return 1;
226}
227
228/* detach an appctx from a ring. The appctx is expected to be waiting at
229 * offset <ofs>. Nothing is done if <ring> is NULL.
230 */
231void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
232{
233 if (!ring)
234 return;
235
236 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
237 if (ofs != ~0) {
238 /* reader was still attached */
239 ofs -= ring->ofs;
240 BUG_ON(ofs >= b_size(&ring->buf));
241 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau4781b152021-04-06 13:53:36 +0200242 HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
Willy Tarreau928068a2020-05-19 19:14:42 +0200243 }
Willy Tarreau4781b152021-04-06 13:53:36 +0200244 HA_ATOMIC_DEC(&ring->readers_count);
Willy Tarreau928068a2020-05-19 19:14:42 +0200245 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
246}
247
Willy Tarreau072931c2019-08-27 11:55:39 +0200248/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
249 * meant to be used when registering a CLI function to dump a buffer, so it
250 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100251 * CLI context. It automatically sets the io_handler and io_release callbacks if
252 * they were not set.
Willy Tarreau072931c2019-08-27 11:55:39 +0200253 */
254int ring_attach_cli(struct ring *ring, struct appctx *appctx)
255{
Emeric Brundcd58af2020-05-28 14:39:30 +0200256 if (!ring_attach(ring))
Willy Tarreau928068a2020-05-19 19:14:42 +0200257 return cli_err(appctx,
258 "Sorry, too many watchers (255) on this ring buffer. "
259 "What could it have so interesting to attract so many watchers ?");
Willy Tarreau072931c2019-08-27 11:55:39 +0200260
Willy Tarreaufcf94982019-11-15 15:07:21 +0100261 if (!appctx->io_handler)
262 appctx->io_handler = cli_io_handler_show_ring;
263 if (!appctx->io_release)
264 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200265 appctx->ctx.cli.p0 = ring;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200266 appctx->ctx.cli.o0 = ~0; // start from the oldest event
Willy Tarreau072931c2019-08-27 11:55:39 +0200267 return 0;
268}
269
270/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200271 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200272 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
273 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
274 * means it must seek directly to the end to wait for new contents. It returns
275 * 0 if the output buffer or events are missing is full and it needs to be
276 * called again, otherwise non-zero. It is meant to be used with
277 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200278 */
279int cli_io_handler_show_ring(struct appctx *appctx)
280{
Christopher Faulet908628c2022-03-25 16:43:49 +0100281 struct conn_stream *cs = appctx->owner;
Willy Tarreau072931c2019-08-27 11:55:39 +0200282 struct ring *ring = appctx->ctx.cli.p0;
283 struct buffer *buf = &ring->buf;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200284 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200285 uint64_t msg_len;
286 size_t len, cnt;
287 int ret;
288
Christopher Faulet908628c2022-03-25 16:43:49 +0100289 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau072931c2019-08-27 11:55:39 +0200290 return 1;
291
Willy Tarreau223dded2020-05-19 19:21:45 +0200292 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200293 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200294 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
295
296 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200297
Willy Tarreau072931c2019-08-27 11:55:39 +0200298 /* explanation for the initialization below: it would be better to do
299 * this in the parsing function but this would occasionally result in
300 * dropped events because we'd take a reference on the oldest message
301 * and keep it while being scheduled. Thus instead let's take it the
302 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200303 * existing messages before grabbing a reference to a location. This
304 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200305 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200306 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200307 ofs = 0;
308
309 /* going to the end means looking at tail-1 */
310 if (appctx->ctx.cli.i0 & 2)
311 ofs += b_data(buf) - 1;
312
Willy Tarreau4781b152021-04-06 13:53:36 +0200313 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau1d181e42019-08-30 11:17:01 +0200314 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200315 }
316
317 /* we were already there, adjust the offset to be relative to
318 * the buffer's head and remove us from the counter.
319 */
320 ofs -= ring->ofs;
321 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200322 HA_ATOMIC_DEC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200323
324 /* in this loop, ofs always points to the counter byte that precedes
325 * the message so that we can take our reference there if we have to
326 * stop before the end (ret=0).
327 */
328 ret = 1;
329 while (ofs + 1 < b_data(buf)) {
330 cnt = 1;
331 len = b_peek_varint(buf, ofs + cnt, &msg_len);
332 if (!len)
333 break;
334 cnt += len;
335 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
336
337 if (unlikely(msg_len + 1 > b_size(&trash))) {
338 /* too large a message to ever fit, let's skip it */
339 ofs += cnt + msg_len;
340 continue;
341 }
342
343 chunk_reset(&trash);
344 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
345 trash.data += len;
346 trash.area[trash.data++] = '\n';
347
Christopher Faulet908628c2022-03-25 16:43:49 +0100348 if (ci_putchk(cs_ic(cs), &trash) == -1) {
349 si_rx_room_blk(cs->si);
Willy Tarreau072931c2019-08-27 11:55:39 +0200350 ret = 0;
351 break;
352 }
353 ofs += cnt + msg_len;
354 }
355
Willy Tarreau4781b152021-04-06 13:53:36 +0200356 HA_ATOMIC_INC(b_peek(buf, ofs));
Willy Tarreau072931c2019-08-27 11:55:39 +0200357 ofs += ring->ofs;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200358 appctx->ctx.cli.o0 = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200359 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200360
361 if (ret && (appctx->ctx.cli.i0 & 1)) {
362 /* we've drained everything and are configured to wait for more
363 * data or an event (keypress, close)
364 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100365 if (!cs_oc(cs)->output && !(cs_oc(cs)->flags & CF_SHUTW)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200366 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200367 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200368 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200369 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Christopher Faulet908628c2022-03-25 16:43:49 +0100370 si_rx_endp_done(cs->si);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200371 ret = 0;
372 }
373 /* always drain all the request */
Christopher Faulet908628c2022-03-25 16:43:49 +0100374 co_skip(cs_oc(cs), cs_oc(cs)->output);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200375 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200376 return ret;
377}
378
379/* must be called after cli_io_handler_show_ring() above */
380void cli_io_release_show_ring(struct appctx *appctx)
381{
382 struct ring *ring = appctx->ctx.cli.p0;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200383 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200384
Willy Tarreau928068a2020-05-19 19:14:42 +0200385 ring_detach_appctx(ring, appctx, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200386}
387
388
Willy Tarreau172945f2019-08-08 15:28:52 +0200389/*
390 * Local variables:
391 * c-indent-level: 8
392 * c-basic-offset: 8
393 * End:
394 */