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