blob: 755e69cc130c25fdb0e5f924b225f9cced46715e [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 */
194 list_for_each_entry(appctx, &ring->waiters, ctx.cli.l0)
195 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
205 * CLI context.
206 */
207int ring_attach_cli(struct ring *ring, struct appctx *appctx)
208{
209 int users = ring->readers_count;
210
211 do {
Willy Tarreau13696ff2019-08-30 10:16:14 +0200212 if (users >= 255)
Willy Tarreau072931c2019-08-27 11:55:39 +0200213 return cli_err(appctx,
214 "Sorry, too many watchers (255) on this ring buffer. "
215 "What could it have so interesting to attract so many watchers ?");
216
217 } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1));
218
219 appctx->ctx.cli.p0 = ring;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200220 appctx->ctx.cli.o0 = ~0; // start from the oldest event
Willy Tarreau072931c2019-08-27 11:55:39 +0200221 return 0;
222}
223
224/* This function dumps all events from the ring whose pointer is in <p0> into
Willy Tarreau13696ff2019-08-30 10:16:14 +0200225 * the appctx's output buffer, and takes from <o0> the seek offset into the
Willy Tarreau1d181e42019-08-30 11:17:01 +0200226 * buffer's history (0 for oldest known event). It looks at <i0> for boolean
227 * options: bit0 means it must wait for new data or any key to be pressed. Bit1
228 * means it must seek directly to the end to wait for new contents. It returns
229 * 0 if the output buffer or events are missing is full and it needs to be
230 * called again, otherwise non-zero. It is meant to be used with
231 * cli_release_show_ring() to clean up.
Willy Tarreau072931c2019-08-27 11:55:39 +0200232 */
233int cli_io_handler_show_ring(struct appctx *appctx)
234{
235 struct stream_interface *si = appctx->owner;
236 struct ring *ring = appctx->ctx.cli.p0;
237 struct buffer *buf = &ring->buf;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200238 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200239 uint64_t msg_len;
240 size_t len, cnt;
241 int ret;
242
243 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
244 return 1;
245
246 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
247
Willy Tarreau1d181e42019-08-30 11:17:01 +0200248 LIST_DEL_INIT(&appctx->ctx.cli.l0);
249
Willy Tarreau072931c2019-08-27 11:55:39 +0200250 /* explanation for the initialization below: it would be better to do
251 * this in the parsing function but this would occasionally result in
252 * dropped events because we'd take a reference on the oldest message
253 * and keep it while being scheduled. Thus instead let's take it the
254 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200255 * existing messages before grabbing a reference to a location. This
256 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200257 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200258 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200259 ofs = 0;
260
261 /* going to the end means looking at tail-1 */
262 if (appctx->ctx.cli.i0 & 2)
263 ofs += b_data(buf) - 1;
264
265 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
266 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200267 }
268
269 /* we were already there, adjust the offset to be relative to
270 * the buffer's head and remove us from the counter.
271 */
272 ofs -= ring->ofs;
273 BUG_ON(ofs >= buf->size);
274 HA_ATOMIC_SUB(b_peek(buf, ofs), 1);
275
276 /* in this loop, ofs always points to the counter byte that precedes
277 * the message so that we can take our reference there if we have to
278 * stop before the end (ret=0).
279 */
280 ret = 1;
281 while (ofs + 1 < b_data(buf)) {
282 cnt = 1;
283 len = b_peek_varint(buf, ofs + cnt, &msg_len);
284 if (!len)
285 break;
286 cnt += len;
287 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
288
289 if (unlikely(msg_len + 1 > b_size(&trash))) {
290 /* too large a message to ever fit, let's skip it */
291 ofs += cnt + msg_len;
292 continue;
293 }
294
295 chunk_reset(&trash);
296 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
297 trash.data += len;
298 trash.area[trash.data++] = '\n';
299
300 if (ci_putchk(si_ic(si), &trash) == -1) {
301 si_rx_room_blk(si);
302 ret = 0;
303 break;
304 }
305 ofs += cnt + msg_len;
306 }
307
308 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
309 ofs += ring->ofs;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200310 appctx->ctx.cli.o0 = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200311 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200312
313 if (ret && (appctx->ctx.cli.i0 & 1)) {
314 /* we've drained everything and are configured to wait for more
315 * data or an event (keypress, close)
316 */
317 if (!si_oc(si)->output && !(si_oc(si)->flags & CF_SHUTW)) {
318 /* let's be woken up once new data arrive */
319 LIST_ADDQ(&ring->waiters, &appctx->ctx.cli.l0);
320 si_rx_endp_done(si);
321 ret = 0;
322 }
323 /* always drain all the request */
324 co_skip(si_oc(si), si_oc(si)->output);
325 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200326 return ret;
327}
328
329/* must be called after cli_io_handler_show_ring() above */
330void cli_io_release_show_ring(struct appctx *appctx)
331{
332 struct ring *ring = appctx->ctx.cli.p0;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200333 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200334
335 if (!ring)
336 return;
337
338 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200339 if (ofs != ~0) {
340 /* reader was still attached */
341 ofs -= ring->ofs;
342 BUG_ON(ofs >= b_size(&ring->buf));
Willy Tarreau1d181e42019-08-30 11:17:01 +0200343 LIST_DEL_INIT(&appctx->ctx.cli.l0);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200344 HA_ATOMIC_SUB(b_peek(&ring->buf, ofs), 1);
345 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200346 HA_ATOMIC_SUB(&ring->readers_count, 1);
347 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
348}
349
350
Willy Tarreau172945f2019-08-08 15:28:52 +0200351/*
352 * Local variables:
353 * c-indent-level: 8
354 * c-basic-offset: 8
355 * End:
356 */