Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/applet.h> |
Willy Tarreau | 8dabda7 | 2020-05-27 17:22:10 +0200 | [diff] [blame] | 24 | #include <haproxy/buf.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Willy Tarreau | d2ad57c | 2020-06-03 19:43:35 +0200 | [diff] [blame] | 26 | #include <haproxy/ring.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 27 | #include <haproxy/sc_strm.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 28 | #include <haproxy/stconn.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/thread.h> |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 31 | /* context used to dump the contents of a ring via "show events" or "show errors" */ |
| 32 | struct show_ring_ctx { |
| 33 | struct ring *ring; /* ring to be dumped */ |
| 34 | size_t ofs; /* offset to restart from, ~0 = end */ |
| 35 | uint flags; /* set of RING_WF_* */ |
| 36 | }; |
| 37 | |
Emeric Brun | e14b98c | 2021-01-12 14:21:00 +0100 | [diff] [blame] | 38 | /* Initialize a pre-allocated ring with the buffer area |
| 39 | * of size */ |
| 40 | void ring_init(struct ring *ring, void *area, size_t size) |
| 41 | { |
| 42 | HA_RWLOCK_INIT(&ring->lock); |
| 43 | LIST_INIT(&ring->waiters); |
| 44 | ring->readers_count = 0; |
| 45 | ring->ofs = 0; |
| 46 | ring->buf = b_make(area, size, 0, 0); |
| 47 | /* write the initial RC byte */ |
| 48 | b_putchr(&ring->buf, 0); |
| 49 | } |
| 50 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 51 | /* Creates and returns a ring buffer of size <size> bytes. Returns NULL on |
| 52 | * allocation failure. |
| 53 | */ |
| 54 | struct ring *ring_new(size_t size) |
| 55 | { |
| 56 | struct ring *ring = NULL; |
| 57 | void *area = NULL; |
| 58 | |
| 59 | if (size < 2) |
| 60 | goto fail; |
| 61 | |
| 62 | ring = malloc(sizeof(*ring)); |
| 63 | if (!ring) |
| 64 | goto fail; |
| 65 | |
| 66 | area = malloc(size); |
| 67 | if (!area) |
| 68 | goto fail; |
| 69 | |
Emeric Brun | e14b98c | 2021-01-12 14:21:00 +0100 | [diff] [blame] | 70 | ring_init(ring, area, size); |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 71 | return ring; |
| 72 | fail: |
| 73 | free(area); |
| 74 | free(ring); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Willy Tarreau | 6df10d8 | 2022-08-12 07:50:43 +0200 | [diff] [blame] | 78 | /* Creates a unified ring + storage area at address <area> for <size> bytes. |
| 79 | * If <area> is null, then it's allocated of the requested size. The ring |
| 80 | * struct is part of the area so the usable area is slightly reduced. However |
| 81 | * the ring storage is immediately adjacent to the struct. ring_free() will |
| 82 | * ignore such rings, so the caller is responsible for releasing them. |
| 83 | */ |
| 84 | struct ring *ring_make_from_area(void *area, size_t size) |
| 85 | { |
| 86 | struct ring *ring = NULL; |
| 87 | |
William Lallemand | 3a374ea | 2022-09-27 14:31:37 +0200 | [diff] [blame] | 88 | if (size < sizeof(*ring)) |
Willy Tarreau | 6df10d8 | 2022-08-12 07:50:43 +0200 | [diff] [blame] | 89 | return NULL; |
| 90 | |
| 91 | if (!area) |
| 92 | area = malloc(size); |
| 93 | if (!area) |
| 94 | return NULL; |
| 95 | |
| 96 | ring = area; |
| 97 | area += sizeof(*ring); |
| 98 | ring_init(ring, area, size - sizeof(*ring)); |
| 99 | return ring; |
| 100 | } |
| 101 | |
William Lallemand | 9e4ead3 | 2022-09-27 15:53:53 +0200 | [diff] [blame] | 102 | /* Cast an unified ring + storage area to a ring from <area>, without |
| 103 | * reinitializing the data buffer. |
| 104 | * |
| 105 | * Reinitialize the waiters and the lock. |
| 106 | */ |
| 107 | struct ring *ring_cast_from_area(void *area) |
| 108 | { |
| 109 | struct ring *ring = NULL; |
| 110 | |
| 111 | ring = area; |
| 112 | ring->buf.area = area + sizeof(*ring); |
| 113 | |
| 114 | HA_RWLOCK_INIT(&ring->lock); |
| 115 | LIST_INIT(&ring->waiters); |
| 116 | ring->readers_count = 0; |
| 117 | |
| 118 | return ring; |
| 119 | } |
| 120 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 121 | /* Resizes existing ring <ring> to <size> which must be larger, without losing |
| 122 | * its contents. The new size must be at least as large as the previous one or |
| 123 | * no change will be performed. The pointer to the ring is returned on success, |
| 124 | * or NULL on allocation failure. This will lock the ring for writes. |
| 125 | */ |
| 126 | struct ring *ring_resize(struct ring *ring, size_t size) |
| 127 | { |
| 128 | void *area; |
| 129 | |
| 130 | if (b_size(&ring->buf) >= size) |
| 131 | return ring; |
| 132 | |
| 133 | area = malloc(size); |
| 134 | if (!area) |
| 135 | return NULL; |
| 136 | |
| 137 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 138 | |
| 139 | /* recheck the buffer's size, it may have changed during the malloc */ |
| 140 | if (b_size(&ring->buf) < size) { |
| 141 | /* copy old contents */ |
| 142 | b_getblk(&ring->buf, area, ring->buf.data, 0); |
| 143 | area = HA_ATOMIC_XCHG(&ring->buf.area, area); |
| 144 | ring->buf.size = size; |
| 145 | ring->buf.head = 0; |
| 146 | } |
| 147 | |
| 148 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 149 | |
| 150 | free(area); |
| 151 | return ring; |
| 152 | } |
| 153 | |
| 154 | /* destroys and frees ring <ring> */ |
| 155 | void ring_free(struct ring *ring) |
| 156 | { |
| 157 | if (!ring) |
| 158 | return; |
Willy Tarreau | 6df10d8 | 2022-08-12 07:50:43 +0200 | [diff] [blame] | 159 | |
| 160 | /* make sure it was not allocated by ring_make_from_area */ |
| 161 | if (ring->buf.area == (void *)ring + sizeof(*ring)) |
| 162 | return; |
| 163 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 164 | free(ring->buf.area); |
| 165 | free(ring); |
| 166 | } |
| 167 | |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 168 | /* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg> |
| 169 | * to ring <ring>. The message is sent atomically. It may be truncated to |
| 170 | * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the |
| 171 | * two lists, it's just a convenience to help the caller prepend some prefixes |
| 172 | * when necessary. It takes the ring's write lock to make sure no other thread |
| 173 | * will touch the buffer during the update. Returns the number of bytes sent, |
| 174 | * or <=0 on failure. |
| 175 | */ |
| 176 | ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg) |
| 177 | { |
| 178 | struct buffer *buf = &ring->buf; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 179 | struct appctx *appctx; |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 180 | size_t totlen = 0; |
| 181 | size_t lenlen; |
Willy Tarreau | 3036290 | 2019-08-30 15:06:10 +0200 | [diff] [blame] | 182 | uint64_t dellen; |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 183 | int dellenlen; |
| 184 | ssize_t sent = 0; |
| 185 | int i; |
| 186 | |
| 187 | /* we have to find some room to add our message (the buffer is |
| 188 | * never empty and at least contains the previous counter) and |
| 189 | * to update both the buffer contents and heads at the same |
| 190 | * time (it's doable using atomic ops but not worth the |
| 191 | * trouble, let's just lock). For this we first need to know |
| 192 | * the total message's length. We cannot measure it while |
| 193 | * copying due to the varint encoding of the length. |
| 194 | */ |
| 195 | for (i = 0; i < npfx; i++) |
| 196 | totlen += pfx[i].len; |
| 197 | for (i = 0; i < nmsg; i++) |
| 198 | totlen += msg[i].len; |
| 199 | |
| 200 | if (totlen > maxlen) |
| 201 | totlen = maxlen; |
| 202 | |
| 203 | lenlen = varint_bytes(totlen); |
| 204 | |
| 205 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 206 | if (lenlen + totlen + 1 + 1 > b_size(buf)) |
| 207 | goto done_buf; |
| 208 | |
| 209 | while (b_room(buf) < lenlen + totlen + 1) { |
| 210 | /* we need to delete the oldest message (from the end), |
| 211 | * and we have to stop if there's a reader stuck there. |
| 212 | * Unless there's corruption in the buffer it's guaranteed |
| 213 | * that we have enough data to find 1 counter byte, a |
| 214 | * varint-encoded length (1 byte min) and the message |
| 215 | * payload (0 bytes min). |
| 216 | */ |
| 217 | if (*b_head(buf)) |
| 218 | goto done_buf; |
| 219 | dellenlen = b_peek_varint(buf, 1, &dellen); |
| 220 | if (!dellenlen) |
| 221 | goto done_buf; |
| 222 | BUG_ON(b_data(buf) < 1 + dellenlen + dellen); |
| 223 | |
| 224 | b_del(buf, 1 + dellenlen + dellen); |
| 225 | ring->ofs += 1 + dellenlen + dellen; |
| 226 | } |
| 227 | |
| 228 | /* OK now we do have room */ |
| 229 | __b_put_varint(buf, totlen); |
| 230 | |
| 231 | totlen = 0; |
| 232 | for (i = 0; i < npfx; i++) { |
| 233 | size_t len = pfx[i].len; |
| 234 | |
| 235 | if (len + totlen > maxlen) |
| 236 | len = maxlen - totlen; |
| 237 | if (len) |
| 238 | __b_putblk(buf, pfx[i].ptr, len); |
| 239 | totlen += len; |
| 240 | } |
| 241 | |
| 242 | for (i = 0; i < nmsg; i++) { |
| 243 | size_t len = msg[i].len; |
| 244 | |
| 245 | if (len + totlen > maxlen) |
| 246 | len = maxlen - totlen; |
| 247 | if (len) |
| 248 | __b_putblk(buf, msg[i].ptr, len); |
| 249 | totlen += len; |
| 250 | } |
| 251 | |
William Dauchy | 477757c | 2020-08-07 22:19:23 +0200 | [diff] [blame] | 252 | *b_tail(buf) = 0; buf->data++; // new read counter |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 253 | sent = lenlen + totlen + 1; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 254 | |
| 255 | /* notify potential readers */ |
Willy Tarreau | 9597cbd | 2020-05-19 17:07:30 +0200 | [diff] [blame] | 256 | list_for_each_entry(appctx, &ring->waiters, wait_entry) |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 257 | appctx_wakeup(appctx); |
| 258 | |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame] | 259 | done_buf: |
| 260 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 261 | return sent; |
| 262 | } |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 264 | /* Tries to attach appctx <appctx> as a new reader on ring <ring>. This is |
| 265 | * meant to be used by low level appctx code such as CLI or ring forwarding. |
| 266 | * For higher level functions, please see the relevant parts in appctx or CLI. |
| 267 | * It returns non-zero on success or zero on failure if too many users are |
| 268 | * already attached. On success, the caller MUST call ring_detach_appctx() |
| 269 | * to detach itself, even if it was never woken up. |
| 270 | */ |
Emeric Brun | dcd58af | 2020-05-28 14:39:30 +0200 | [diff] [blame] | 271 | int ring_attach(struct ring *ring) |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 272 | { |
| 273 | int users = ring->readers_count; |
| 274 | |
| 275 | do { |
| 276 | if (users >= 255) |
| 277 | return 0; |
| 278 | } while (!_HA_ATOMIC_CAS(&ring->readers_count, &users, users + 1)); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | /* detach an appctx from a ring. The appctx is expected to be waiting at |
| 283 | * offset <ofs>. Nothing is done if <ring> is NULL. |
| 284 | */ |
| 285 | void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs) |
| 286 | { |
| 287 | if (!ring) |
| 288 | return; |
| 289 | |
| 290 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 291 | if (ofs != ~0) { |
| 292 | /* reader was still attached */ |
| 293 | ofs -= ring->ofs; |
| 294 | BUG_ON(ofs >= b_size(&ring->buf)); |
| 295 | LIST_DEL_INIT(&appctx->wait_entry); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 296 | HA_ATOMIC_DEC(b_peek(&ring->buf, ofs)); |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 297 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 298 | HA_ATOMIC_DEC(&ring->readers_count); |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 299 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 300 | } |
| 301 | |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 302 | /* Tries to attach CLI handler <appctx> as a new reader on ring <ring>. This is |
| 303 | * meant to be used when registering a CLI function to dump a buffer, so it |
| 304 | * returns zero on success, or non-zero on failure with a message in the appctx |
Willy Tarreau | fcf9498 | 2019-11-15 15:07:21 +0100 | [diff] [blame] | 305 | * CLI context. It automatically sets the io_handler and io_release callbacks if |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 306 | * they were not set. The <flags> take a combination of RING_WF_*. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 307 | */ |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 308 | int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags) |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 309 | { |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 310 | struct show_ring_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 311 | |
Emeric Brun | dcd58af | 2020-05-28 14:39:30 +0200 | [diff] [blame] | 312 | if (!ring_attach(ring)) |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 313 | return cli_err(appctx, |
| 314 | "Sorry, too many watchers (255) on this ring buffer. " |
| 315 | "What could it have so interesting to attract so many watchers ?"); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 316 | |
Willy Tarreau | fcf9498 | 2019-11-15 15:07:21 +0100 | [diff] [blame] | 317 | if (!appctx->io_handler) |
| 318 | appctx->io_handler = cli_io_handler_show_ring; |
| 319 | if (!appctx->io_release) |
| 320 | appctx->io_release = cli_io_release_show_ring; |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 321 | |
| 322 | memset(ctx, 0, sizeof(*ctx)); |
| 323 | ctx->ring = ring; |
| 324 | ctx->ofs = ~0; // start from the oldest event |
| 325 | ctx->flags = flags; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* This function dumps all events from the ring whose pointer is in <p0> into |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 330 | * the appctx's output buffer, and takes from <o0> the seek offset into the |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 331 | * buffer's history (0 for oldest known event). It looks at <i0> for boolean |
| 332 | * options: bit0 means it must wait for new data or any key to be pressed. Bit1 |
| 333 | * means it must seek directly to the end to wait for new contents. It returns |
| 334 | * 0 if the output buffer or events are missing is full and it needs to be |
| 335 | * called again, otherwise non-zero. It is meant to be used with |
| 336 | * cli_release_show_ring() to clean up. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 337 | */ |
| 338 | int cli_io_handler_show_ring(struct appctx *appctx) |
| 339 | { |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 340 | struct show_ring_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 341 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 342 | struct ring *ring = ctx->ring; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 343 | struct buffer *buf = &ring->buf; |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 344 | size_t ofs = ctx->ofs; |
Willy Tarreau | b8e0fb9 | 2022-08-04 17:00:21 +0200 | [diff] [blame] | 345 | size_t last_ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 346 | uint64_t msg_len; |
| 347 | size_t len, cnt; |
| 348 | int ret; |
| 349 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 350 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 351 | return 1; |
| 352 | |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 353 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 9597cbd | 2020-05-19 17:07:30 +0200 | [diff] [blame] | 354 | LIST_DEL_INIT(&appctx->wait_entry); |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 355 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 356 | |
| 357 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 358 | |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 359 | /* explanation for the initialization below: it would be better to do |
| 360 | * this in the parsing function but this would occasionally result in |
| 361 | * dropped events because we'd take a reference on the oldest message |
| 362 | * and keep it while being scheduled. Thus instead let's take it the |
| 363 | * first time we enter here so that we have a chance to pass many |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 364 | * existing messages before grabbing a reference to a location. This |
| 365 | * value cannot be produced after initialization. |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 366 | */ |
Willy Tarreau | 13696ff | 2019-08-30 10:16:14 +0200 | [diff] [blame] | 367 | if (unlikely(ofs == ~0)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 368 | ofs = 0; |
| 369 | |
| 370 | /* going to the end means looking at tail-1 */ |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 371 | if (ctx->flags & RING_WF_SEEK_NEW) |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 372 | ofs += b_data(buf) - 1; |
| 373 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 374 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 375 | ofs += ring->ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* we were already there, adjust the offset to be relative to |
| 379 | * the buffer's head and remove us from the counter. |
| 380 | */ |
| 381 | ofs -= ring->ofs; |
| 382 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 383 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 384 | |
| 385 | /* in this loop, ofs always points to the counter byte that precedes |
| 386 | * the message so that we can take our reference there if we have to |
| 387 | * stop before the end (ret=0). |
| 388 | */ |
| 389 | ret = 1; |
| 390 | while (ofs + 1 < b_data(buf)) { |
| 391 | cnt = 1; |
| 392 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 393 | if (!len) |
| 394 | break; |
| 395 | cnt += len; |
| 396 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 397 | |
| 398 | if (unlikely(msg_len + 1 > b_size(&trash))) { |
| 399 | /* too large a message to ever fit, let's skip it */ |
| 400 | ofs += cnt + msg_len; |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | chunk_reset(&trash); |
| 405 | len = b_getblk(buf, trash.area, msg_len, ofs + cnt); |
| 406 | trash.data += len; |
| 407 | trash.area[trash.data++] = '\n'; |
| 408 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 409 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 410 | ret = 0; |
| 411 | break; |
| 412 | } |
| 413 | ofs += cnt + msg_len; |
| 414 | } |
| 415 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 416 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 417 | ofs += ring->ofs; |
Willy Tarreau | b8e0fb9 | 2022-08-04 17:00:21 +0200 | [diff] [blame] | 418 | last_ofs = ring->ofs; |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 419 | ctx->ofs = ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 420 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 421 | |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 422 | if (ret && (ctx->flags & RING_WF_WAIT_MODE)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 423 | /* we've drained everything and are configured to wait for more |
| 424 | * data or an event (keypress, close) |
| 425 | */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 426 | if (!sc_oc(sc)->output && !(sc_oc(sc)->flags & CF_SHUTW)) { |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 427 | /* let's be woken up once new data arrive */ |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 428 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 429 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Willy Tarreau | b8e0fb9 | 2022-08-04 17:00:21 +0200 | [diff] [blame] | 430 | ofs = ring->ofs; |
Willy Tarreau | 223dded | 2020-05-19 19:21:45 +0200 | [diff] [blame] | 431 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | b8e0fb9 | 2022-08-04 17:00:21 +0200 | [diff] [blame] | 432 | if (ofs != last_ofs) { |
| 433 | /* more data was added into the ring between the |
| 434 | * unlock and the lock, and the writer might not |
| 435 | * have seen us. We need to reschedule a read. |
| 436 | */ |
| 437 | applet_have_more_data(appctx); |
| 438 | } else |
| 439 | applet_have_no_more_data(appctx); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 440 | ret = 0; |
| 441 | } |
| 442 | /* always drain all the request */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 443 | co_skip(sc_oc(sc), sc_oc(sc)->output); |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 444 | } |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | /* must be called after cli_io_handler_show_ring() above */ |
| 449 | void cli_io_release_show_ring(struct appctx *appctx) |
| 450 | { |
Willy Tarreau | 6e3fc48 | 2022-05-05 15:29:43 +0200 | [diff] [blame] | 451 | struct show_ring_ctx *ctx = appctx->svcctx; |
| 452 | struct ring *ring = ctx->ring; |
| 453 | size_t ofs = ctx->ofs; |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 454 | |
Willy Tarreau | 928068a | 2020-05-19 19:14:42 +0200 | [diff] [blame] | 455 | ring_detach_appctx(ring, appctx, ofs); |
Willy Tarreau | 072931c | 2019-08-27 11:55:39 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 459 | /* |
| 460 | * Local variables: |
| 461 | * c-indent-level: 8 |
| 462 | * c-basic-offset: 8 |
| 463 | * End: |
| 464 | */ |