blob: b94b5699bf54261112ee262cc9eba96ad2646f9f [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
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
251 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
252
Willy Tarreau1d181e42019-08-30 11:17:01 +0200253 LIST_DEL_INIT(&appctx->ctx.cli.l0);
254
Willy Tarreau072931c2019-08-27 11:55:39 +0200255 /* explanation for the initialization below: it would be better to do
256 * this in the parsing function but this would occasionally result in
257 * dropped events because we'd take a reference on the oldest message
258 * and keep it while being scheduled. Thus instead let's take it the
259 * first time we enter here so that we have a chance to pass many
Willy Tarreau13696ff2019-08-30 10:16:14 +0200260 * existing messages before grabbing a reference to a location. This
261 * value cannot be produced after initialization.
Willy Tarreau072931c2019-08-27 11:55:39 +0200262 */
Willy Tarreau13696ff2019-08-30 10:16:14 +0200263 if (unlikely(ofs == ~0)) {
Willy Tarreau1d181e42019-08-30 11:17:01 +0200264 ofs = 0;
265
266 /* going to the end means looking at tail-1 */
267 if (appctx->ctx.cli.i0 & 2)
268 ofs += b_data(buf) - 1;
269
270 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
271 ofs += ring->ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200272 }
273
274 /* we were already there, adjust the offset to be relative to
275 * the buffer's head and remove us from the counter.
276 */
277 ofs -= ring->ofs;
278 BUG_ON(ofs >= buf->size);
279 HA_ATOMIC_SUB(b_peek(buf, ofs), 1);
280
281 /* in this loop, ofs always points to the counter byte that precedes
282 * the message so that we can take our reference there if we have to
283 * stop before the end (ret=0).
284 */
285 ret = 1;
286 while (ofs + 1 < b_data(buf)) {
287 cnt = 1;
288 len = b_peek_varint(buf, ofs + cnt, &msg_len);
289 if (!len)
290 break;
291 cnt += len;
292 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
293
294 if (unlikely(msg_len + 1 > b_size(&trash))) {
295 /* too large a message to ever fit, let's skip it */
296 ofs += cnt + msg_len;
297 continue;
298 }
299
300 chunk_reset(&trash);
301 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
302 trash.data += len;
303 trash.area[trash.data++] = '\n';
304
305 if (ci_putchk(si_ic(si), &trash) == -1) {
306 si_rx_room_blk(si);
307 ret = 0;
308 break;
309 }
310 ofs += cnt + msg_len;
311 }
312
313 HA_ATOMIC_ADD(b_peek(buf, ofs), 1);
314 ofs += ring->ofs;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200315 appctx->ctx.cli.o0 = ofs;
Willy Tarreau072931c2019-08-27 11:55:39 +0200316 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau1d181e42019-08-30 11:17:01 +0200317
318 if (ret && (appctx->ctx.cli.i0 & 1)) {
319 /* we've drained everything and are configured to wait for more
320 * data or an event (keypress, close)
321 */
322 if (!si_oc(si)->output && !(si_oc(si)->flags & CF_SHUTW)) {
323 /* let's be woken up once new data arrive */
324 LIST_ADDQ(&ring->waiters, &appctx->ctx.cli.l0);
325 si_rx_endp_done(si);
326 ret = 0;
327 }
328 /* always drain all the request */
329 co_skip(si_oc(si), si_oc(si)->output);
330 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200331 return ret;
332}
333
334/* must be called after cli_io_handler_show_ring() above */
335void cli_io_release_show_ring(struct appctx *appctx)
336{
337 struct ring *ring = appctx->ctx.cli.p0;
Willy Tarreau13696ff2019-08-30 10:16:14 +0200338 size_t ofs = appctx->ctx.cli.o0;
Willy Tarreau072931c2019-08-27 11:55:39 +0200339
340 if (!ring)
341 return;
342
343 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200344 if (ofs != ~0) {
345 /* reader was still attached */
346 ofs -= ring->ofs;
347 BUG_ON(ofs >= b_size(&ring->buf));
Willy Tarreau1d181e42019-08-30 11:17:01 +0200348 LIST_DEL_INIT(&appctx->ctx.cli.l0);
Willy Tarreau13696ff2019-08-30 10:16:14 +0200349 HA_ATOMIC_SUB(b_peek(&ring->buf, ofs), 1);
350 }
Willy Tarreau072931c2019-08-27 11:55:39 +0200351 HA_ATOMIC_SUB(&ring->readers_count, 1);
352 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
353}
354
355
Willy Tarreau172945f2019-08-08 15:28:52 +0200356/*
357 * Local variables:
358 * c-indent-level: 8
359 * c-basic-offset: 8
360 * End:
361 */