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