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