blob: 2a6bb02d18ff17c5660e5894422f721f74da3648 [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>
22#include <common/buf.h>
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/hathreads.h>
Willy Tarreau072931c2019-08-27 11:55:39 +020026#include <types/applet.h>
27#include <proto/cli.h>
Willy Tarreau172945f2019-08-08 15:28:52 +020028#include <proto/ring.h>
Willy Tarreau072931c2019-08-27 11:55:39 +020029#include <proto/stream_interface.h>
Willy Tarreau172945f2019-08-08 15:28:52 +020030
31/* Creates and returns a ring buffer of size <size> bytes. Returns NULL on
32 * allocation failure.
33 */
34struct ring *ring_new(size_t size)
35{
36 struct ring *ring = NULL;
37 void *area = NULL;
38
39 if (size < 2)
40 goto fail;
41
42 ring = malloc(sizeof(*ring));
43 if (!ring)
44 goto fail;
45
46 area = malloc(size);
47 if (!area)
48 goto fail;
49
50 HA_RWLOCK_INIT(&ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +020051 LIST_INIT(&ring->waiters);
Willy Tarreau172945f2019-08-08 15:28:52 +020052 ring->readers_count = 0;
53 ring->ofs = 0;
54 ring->buf = b_make(area, size, 0, 0);
55 /* write the initial RC byte */
56 b_putchr(&ring->buf, 0);
57 return ring;
58 fail:
59 free(area);
60 free(ring);
61 return NULL;
62}
63
64/* Resizes existing ring <ring> to <size> which must be larger, without losing
65 * its contents. The new size must be at least as large as the previous one or
66 * no change will be performed. The pointer to the ring is returned on success,
67 * or NULL on allocation failure. This will lock the ring for writes.
68 */
69struct ring *ring_resize(struct ring *ring, size_t size)
70{
71 void *area;
72
73 if (b_size(&ring->buf) >= size)
74 return ring;
75
76 area = malloc(size);
77 if (!area)
78 return NULL;
79
80 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
81
82 /* recheck the buffer's size, it may have changed during the malloc */
83 if (b_size(&ring->buf) < size) {
84 /* copy old contents */
85 b_getblk(&ring->buf, area, ring->buf.data, 0);
86 area = HA_ATOMIC_XCHG(&ring->buf.area, area);
87 ring->buf.size = size;
88 ring->buf.head = 0;
89 }
90
91 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
92
93 free(area);
94 return ring;
95}
96
97/* destroys and frees ring <ring> */
98void ring_free(struct ring *ring)
99{
100 if (!ring)
101 return;
102 free(ring->buf.area);
103 free(ring);
104}
105
Willy Tarreaube978532019-08-27 11:44:13 +0200106/* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg>
107 * to ring <ring>. The message is sent atomically. It may be truncated to
108 * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the
109 * two lists, it's just a convenience to help the caller prepend some prefixes
110 * when necessary. It takes the ring's write lock to make sure no other thread
111 * will touch the buffer during the update. Returns the number of bytes sent,
112 * or <=0 on failure.
113 */
114ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg)
115{
116 struct buffer *buf = &ring->buf;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200117 struct appctx *appctx;
Willy Tarreaube978532019-08-27 11:44:13 +0200118 size_t totlen = 0;
119 size_t lenlen;
Willy Tarreau30362902019-08-30 15:06:10 +0200120 uint64_t dellen;
Willy Tarreaube978532019-08-27 11:44:13 +0200121 int dellenlen;
122 ssize_t sent = 0;
123 int i;
124
125 /* we have to find some room to add our message (the buffer is
126 * never empty and at least contains the previous counter) and
127 * to update both the buffer contents and heads at the same
128 * time (it's doable using atomic ops but not worth the
129 * trouble, let's just lock). For this we first need to know
130 * the total message's length. We cannot measure it while
131 * copying due to the varint encoding of the length.
132 */
133 for (i = 0; i < npfx; i++)
134 totlen += pfx[i].len;
135 for (i = 0; i < nmsg; i++)
136 totlen += msg[i].len;
137
138 if (totlen > maxlen)
139 totlen = maxlen;
140
141 lenlen = varint_bytes(totlen);
142
143 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
144 if (lenlen + totlen + 1 + 1 > b_size(buf))
145 goto done_buf;
146
147 while (b_room(buf) < lenlen + totlen + 1) {
148 /* we need to delete the oldest message (from the end),
149 * and we have to stop if there's a reader stuck there.
150 * Unless there's corruption in the buffer it's guaranteed
151 * that we have enough data to find 1 counter byte, a
152 * varint-encoded length (1 byte min) and the message
153 * payload (0 bytes min).
154 */
155 if (*b_head(buf))
156 goto done_buf;
157 dellenlen = b_peek_varint(buf, 1, &dellen);
158 if (!dellenlen)
159 goto done_buf;
160 BUG_ON(b_data(buf) < 1 + dellenlen + dellen);
161
162 b_del(buf, 1 + dellenlen + dellen);
163 ring->ofs += 1 + dellenlen + dellen;
164 }
165
166 /* OK now we do have room */
167 __b_put_varint(buf, totlen);
168
169 totlen = 0;
170 for (i = 0; i < npfx; i++) {
171 size_t len = pfx[i].len;
172
173 if (len + totlen > maxlen)
174 len = maxlen - totlen;
175 if (len)
176 __b_putblk(buf, pfx[i].ptr, len);
177 totlen += len;
178 }
179
180 for (i = 0; i < nmsg; i++) {
181 size_t len = msg[i].len;
182
183 if (len + totlen > maxlen)
184 len = maxlen - totlen;
185 if (len)
186 __b_putblk(buf, msg[i].ptr, len);
187 totlen += len;
188 }
189
190 *b_tail(buf) = 0; buf->data++;; // new read counter
191 sent = lenlen + totlen + 1;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200192
193 /* notify potential readers */
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200194 list_for_each_entry(appctx, &ring->waiters, wait_entry)
Willy Tarreau1d181e42019-08-30 11:17:01 +0200195 appctx_wakeup(appctx);
196
Willy Tarreaube978532019-08-27 11:44:13 +0200197 done_buf:
198 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
199 return sent;
200}
Willy Tarreau172945f2019-08-08 15:28:52 +0200201
Willy Tarreau928068a2020-05-19 19:14:42 +0200202/* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is
203 * meant to be used by low level appctx code such as CLI or ring forwarding.
204 * For higher level functions, please see the relevant parts in appctx or CLI.
205 * It returns non-zero on success or zero on failure if too many users are
206 * already attached. On success, the caller MUST call ring_detach_appctx()
207 * to detach itself, even if it was never woken up.
208 */
Emeric Brundcd58af2020-05-28 14:39:30 +0200209int ring_attach(struct ring *ring)
Willy Tarreau928068a2020-05-19 19:14:42 +0200210{
211 int users = ring->readers_count;
212
213 do {
214 if (users >= 255)
215 return 0;
216 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
217 return 1;
218}
219
220/* detach an appctx from a ring. The appctx is expected to be waiting at
221 * offset <ofs>. Nothing is done if <ring> is NULL.
222 */
223void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
224{
225 if (!ring)
226 return;
227
228 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
229 if (ofs != ~0) {
230 /* reader was still attached */
231 ofs -= ring->ofs;
232 BUG_ON(ofs >= b_size(&ring->buf));
233 LIST_DEL_INIT(&appctx->wait_entry);
234 HA_ATOMIC_SUB(b_peek(&ring->buf, ofs), 1);
235 }
236 HA_ATOMIC_SUB(&ring->readers_count, 1);
237 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
238}
239
Willy Tarreau072931c2019-08-27 11:55:39 +0200240/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
241 * meant to be used when registering a CLI function to dump a buffer, so it
242 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100243 * CLI context. It automatically sets the io_handler and io_release callbacks if
244 * they were not set.
Willy Tarreau072931c2019-08-27 11:55:39 +0200245 */
246int ring_attach_cli(struct ring *ring, struct appctx *appctx)
247{
Emeric Brundcd58af2020-05-28 14:39:30 +0200248 if (!ring_attach(ring))
Willy Tarreau928068a2020-05-19 19:14:42 +0200249 return cli_err(appctx,
250 "Sorry, too many watchers (255) on this ring buffer. "
251 "What could it have so interesting to attract so many watchers ?");
Willy Tarreau072931c2019-08-27 11:55:39 +0200252
Willy Tarreaufcf94982019-11-15 15:07:21 +0100253 if (!appctx->io_handler)
254 appctx->io_handler = cli_io_handler_show_ring;
255 if (!appctx->io_release)
256 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200257 appctx->ctx.cli.p0 = ring;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200258 appctx->ctx.cli.o0 = ~0; // start from the oldest event
Willy Tarreau072931c2019-08-27 11:55:39 +0200259 return 0;
260}
261
262/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200263 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200264 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
265 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
266 * means it must seek directly to the end to wait for new contents. It returns
267 * 0 if the output buffer or events are missing is full and it needs to be
268 * called again, otherwise non-zero. It is meant to be used with
269 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200270 */
271int cli_io_handler_show_ring(struct appctx *appctx)
272{
273 struct stream_interface *si = appctx->owner;
274 struct ring *ring = appctx->ctx.cli.p0;
275 struct buffer *buf = &ring->buf;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200276 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200277 uint64_t msg_len;
278 size_t len, cnt;
279 int ret;
280
281 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
282 return 1;
283
Willy Tarreau223dded2020-05-19 19:21:45 +0200284 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200285 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200286 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
287
288 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200289
Willy Tarreau072931c2019-08-27 11:55:39 +0200290 /* explanation for the initialization below: it would be better to do
291 * this in the parsing function but this would occasionally result in
292 * dropped events because we'd take a reference on the oldest message
293 * and keep it while being scheduled. Thus instead let's take it the
294 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200295 * existing messages before grabbing a reference to a location. This
296 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200297 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200298 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200299 ofs = 0;
300
301 /* going to the end means looking at tail-1 */
302 if (appctx->ctx.cli.i0 & 2)
303 ofs += b_data(buf) - 1;
304
305 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
306 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200307 }
308
309 /* we were already there, adjust the offset to be relative to
310 * the buffer's head and remove us from the counter.
311 */
312 ofs -= ring->ofs;
313 BUG_ON(ofs >= buf->size);
314 HA_ATOMIC_SUB(b_peek(buf, ofs), 1);
315
316 /* in this loop, ofs always points to the counter byte that precedes
317 * the message so that we can take our reference there if we have to
318 * stop before the end (ret=0).
319 */
320 ret = 1;
321 while (ofs + 1 < b_data(buf)) {
322 cnt = 1;
323 len = b_peek_varint(buf, ofs + cnt, &msg_len);
324 if (!len)
325 break;
326 cnt += len;
327 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
328
329 if (unlikely(msg_len + 1 > b_size(&trash))) {
330 /* too large a message to ever fit, let's skip it */
331 ofs += cnt + msg_len;
332 continue;
333 }
334
335 chunk_reset(&trash);
336 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
337 trash.data += len;
338 trash.area[trash.data++] = '\n';
339
340 if (ci_putchk(si_ic(si), &trash) == -1) {
341 si_rx_room_blk(si);
342 ret = 0;
343 break;
344 }
345 ofs += cnt + msg_len;
346 }
347
348 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
349 ofs += ring->ofs;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200350 appctx->ctx.cli.o0 = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200351 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200352
353 if (ret && (appctx->ctx.cli.i0 & 1)) {
354 /* we've drained everything and are configured to wait for more
355 * data or an event (keypress, close)
356 */
357 if (!si_oc(si)->output && !(si_oc(si)->flags & CF_SHUTW)) {
358 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200359 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200360 LIST_ADDQ(&ring->waiters, &appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200361 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200362 si_rx_endp_done(si);
363 ret = 0;
364 }
365 /* always drain all the request */
366 co_skip(si_oc(si), si_oc(si)->output);
367 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200368 return ret;
369}
370
371/* must be called after cli_io_handler_show_ring() above */
372void cli_io_release_show_ring(struct appctx *appctx)
373{
374 struct ring *ring = appctx->ctx.cli.p0;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200375 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200376
Willy Tarreau928068a2020-05-19 19:14:42 +0200377 ring_detach_appctx(ring, appctx, ofs);
Willy Tarreau072931c2019-08-27 11:55:39 +0200378}
379
380
Willy Tarreau172945f2019-08-08 15:28:52 +0200381/*
382 * Local variables:
383 * c-indent-level: 8
384 * c-basic-offset: 8
385 * End:
386 */