blob: 48003b38bc01d2b28e496a83e977cec34dd55bbf [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 Tarreau072931c2019-08-27 11:55:39 +0200202/* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is
203 * meant to be used when registering a CLI function to dump a buffer, so it
204 * returns zero on success, or non-zero on failure with a message in the appctx
Willy Tarreaufcf94982019-11-15 15:07:21 +0100205 * CLI context. It automatically sets the io_handler and io_release callbacks if
206 * they were not set.
Willy Tarreau072931c2019-08-27 11:55:39 +0200207 */
208int ring_attach_cli(struct ring *ring, struct appctx *appctx)
209{
210 int users = ring->readers_count;
211
212 do {
Willy Tarreau13696ff2019-08-30 10:16:14 +0200213 if (users >= 255)
Willy Tarreau072931c2019-08-27 11:55:39 +0200214 return cli_err(appctx,
215 "Sorry, too many watchers (255) on this ring buffer. "
216 "What could it have so interesting to attract so many watchers ?");
217
218 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
219
Willy Tarreaufcf94982019-11-15 15:07:21 +0100220 if (!appctx->io_handler)
221 appctx->io_handler = cli_io_handler_show_ring;
222 if (!appctx->io_release)
223 appctx->io_release = cli_io_release_show_ring;
Willy Tarreau072931c2019-08-27 11:55:39 +0200224 appctx->ctx.cli.p0 = ring;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200225 appctx->ctx.cli.o0 = ~0; // start from the oldest event
Willy Tarreau072931c2019-08-27 11:55:39 +0200226 return 0;
227}
228
229/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200230 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200231 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
232 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
233 * means it must seek directly to the end to wait for new contents. It returns
234 * 0 if the output buffer or events are missing is full and it needs to be
235 * called again, otherwise non-zero. It is meant to be used with
236 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200237 */
238int cli_io_handler_show_ring(struct appctx *appctx)
239{
240 struct stream_interface *si = appctx->owner;
241 struct ring *ring = appctx->ctx.cli.p0;
242 struct buffer *buf = &ring->buf;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200243 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200244 uint64_t msg_len;
245 size_t len, cnt;
246 int ret;
247
248 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
249 return 1;
250
Willy Tarreau223dded2020-05-19 19:21:45 +0200251 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200252 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200253 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
254
255 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200256
Willy Tarreau072931c2019-08-27 11:55:39 +0200257 /* explanation for the initialization below: it would be better to do
258 * this in the parsing function but this would occasionally result in
259 * dropped events because we'd take a reference on the oldest message
260 * and keep it while being scheduled. Thus instead let's take it the
261 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200262 * existing messages before grabbing a reference to a location. This
263 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200264 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200265 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200266 ofs = 0;
267
268 /* going to the end means looking at tail-1 */
269 if (appctx->ctx.cli.i0 & 2)
270 ofs += b_data(buf) - 1;
271
272 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
273 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200274 }
275
276 /* we were already there, adjust the offset to be relative to
277 * the buffer's head and remove us from the counter.
278 */
279 ofs -= ring->ofs;
280 BUG_ON(ofs >= buf->size);
281 HA_ATOMIC_SUB(b_peek(buf, ofs), 1);
282
283 /* in this loop, ofs always points to the counter byte that precedes
284 * the message so that we can take our reference there if we have to
285 * stop before the end (ret=0).
286 */
287 ret = 1;
288 while (ofs + 1 < b_data(buf)) {
289 cnt = 1;
290 len = b_peek_varint(buf, ofs + cnt, &msg_len);
291 if (!len)
292 break;
293 cnt += len;
294 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
295
296 if (unlikely(msg_len + 1 > b_size(&trash))) {
297 /* too large a message to ever fit, let's skip it */
298 ofs += cnt + msg_len;
299 continue;
300 }
301
302 chunk_reset(&trash);
303 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
304 trash.data += len;
305 trash.area[trash.data++] = '\n';
306
307 if (ci_putchk(si_ic(si), &trash) == -1) {
308 si_rx_room_blk(si);
309 ret = 0;
310 break;
311 }
312 ofs += cnt + msg_len;
313 }
314
315 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
316 ofs += ring->ofs;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200317 appctx->ctx.cli.o0 = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200318 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200319
320 if (ret && (appctx->ctx.cli.i0 & 1)) {
321 /* we've drained everything and are configured to wait for more
322 * data or an event (keypress, close)
323 */
324 if (!si_oc(si)->output && !(si_oc(si)->flags & CF_SHUTW)) {
325 /* let's be woken up once new data arrive */
Willy Tarreau223dded2020-05-19 19:21:45 +0200326 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200327 LIST_ADDQ(&ring->waiters, &appctx->wait_entry);
Willy Tarreau223dded2020-05-19 19:21:45 +0200328 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200329 si_rx_endp_done(si);
330 ret = 0;
331 }
332 /* always drain all the request */
333 co_skip(si_oc(si), si_oc(si)->output);
334 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200335 return ret;
336}
337
338/* must be called after cli_io_handler_show_ring() above */
339void cli_io_release_show_ring(struct appctx *appctx)
340{
341 struct ring *ring = appctx->ctx.cli.p0;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200342 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200343
344 if (!ring)
345 return;
346
Willy Tarreau223dded2020-05-19 19:21:45 +0200347 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200348 if (ofs != ~0) {
349 /* reader was still attached */
350 ofs -= ring->ofs;
351 BUG_ON(ofs >= b_size(&ring->buf));
Willy Tarreau9597cbd2020-05-19 17:07:30 +0200352 LIST_DEL_INIT(&appctx->wait_entry);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200353 HA_ATOMIC_SUB(b_peek(&ring->buf, ofs), 1);
354 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200355 HA_ATOMIC_SUB(&ring->readers_count, 1);
Willy Tarreau223dded2020-05-19 19:21:45 +0200356 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau072931c2019-08-27 11:55:39 +0200357}
358
359
Willy Tarreau172945f2019-08-08 15:28:52 +0200360/*
361 * Local variables:
362 * c-indent-level: 8
363 * c-basic-offset: 8
364 * End:
365 */