Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
| 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/buffer.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 20 | |
| 21 | #include <types/global.h> |
| 22 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 23 | struct pool_head *pool2_buffer; |
| 24 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 25 | /* These buffers are used to always have a valid pointer to an empty buffer in |
| 26 | * channels. The first buffer is set once a buffer is empty. The second one is |
| 27 | * set when a buffer is desired but no more are available. It helps knowing |
| 28 | * what channel wants a buffer. They can reliably be exchanged, the split |
| 29 | * between the two is only an optimization. |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 30 | */ |
| 31 | struct buffer buf_empty = { .p = buf_empty.data }; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 32 | struct buffer buf_wanted = { .p = buf_wanted.data }; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 33 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 34 | /* list of objects waiting for at least one buffer */ |
| 35 | struct list buffer_wq = LIST_HEAD_INIT(buffer_wq); |
| 36 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 37 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 38 | int init_buffer() |
| 39 | { |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 40 | void *buffer; |
| 41 | |
Willy Tarreau | 484b53d | 2016-01-25 02:23:25 +0100 | [diff] [blame] | 42 | pool2_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 43 | if (!pool2_buffer) |
| 44 | return 0; |
| 45 | |
| 46 | /* The reserved buffer is what we leave behind us. Thus we always need |
| 47 | * at least one extra buffer in minavail otherwise we'll end up waking |
| 48 | * up tasks with no memory available, causing a lot of useless wakeups. |
| 49 | * That means that we always want to have at least 3 buffers available |
| 50 | * (2 for current session, one for next session that might be needed to |
| 51 | * release a server connection). |
| 52 | */ |
| 53 | pool2_buffer->minavail = MAX(global.tune.reserved_bufs, 3); |
Willy Tarreau | 33cb065 | 2014-12-23 22:52:37 +0100 | [diff] [blame] | 54 | if (global.tune.buf_limit) |
| 55 | pool2_buffer->limit = global.tune.buf_limit; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 56 | |
| 57 | buffer = pool_refill_alloc(pool2_buffer, pool2_buffer->minavail - 1); |
| 58 | if (!buffer) |
| 59 | return 0; |
| 60 | |
| 61 | pool_free2(pool2_buffer, buffer); |
| 62 | return 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 65 | /* This function writes the string <str> at position <pos> which must be in |
| 66 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 67 | * <l> and <r> are updated to be valid after the shift. The shift value |
| 68 | * (positive or negative) is returned. If there's no space left, the move is |
| 69 | * not done. The function does not adjust ->o because it does not make sense to |
| 70 | * use it on data scheduled to be sent. For the same reason, it does not make |
| 71 | * sense to call this function on unparsed data, so <orig> is not updated. The |
| 72 | * string length is taken from parameter <len>. If <len> is null, the <str> |
| 73 | * pointer is allowed to be null. |
| 74 | */ |
| 75 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
| 76 | { |
| 77 | int delta; |
| 78 | |
| 79 | delta = len - (end - pos); |
| 80 | |
Thierry FOURNIER | fdda677 | 2015-03-10 01:55:01 +0100 | [diff] [blame] | 81 | if (bi_end(b) + delta > b->data + b->size) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 82 | return 0; /* no space left */ |
| 83 | |
| 84 | if (buffer_not_empty(b) && |
| 85 | bi_end(b) + delta > bo_ptr(b) && |
| 86 | bo_ptr(b) >= bi_end(b)) |
| 87 | return 0; /* no space left before wrapping data */ |
| 88 | |
| 89 | /* first, protect the end of the buffer */ |
| 90 | memmove(end + delta, end, bi_end(b) - end); |
| 91 | |
| 92 | /* now, copy str over pos */ |
| 93 | if (len) |
| 94 | memcpy(pos, str, len); |
| 95 | |
| 96 | b->i += delta; |
| 97 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 98 | if (buffer_empty(b)) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 99 | b->p = b->data; |
| 100 | |
| 101 | return delta; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 106 | * argument informs about the length of string <str> so that we don't have to |
| 107 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 108 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
| 109 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 110 | * for the valid use cases. |
| 111 | * |
| 112 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 113 | */ |
| 114 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 115 | { |
| 116 | int delta; |
| 117 | |
| 118 | delta = len + 2; |
| 119 | |
| 120 | if (bi_end(b) + delta >= b->data + b->size) |
| 121 | return 0; /* no space left */ |
| 122 | |
Godbach | a6547c1 | 2014-10-31 13:16:37 +0800 | [diff] [blame] | 123 | if (buffer_not_empty(b) && |
| 124 | bi_end(b) + delta > bo_ptr(b) && |
| 125 | bo_ptr(b) >= bi_end(b)) |
| 126 | return 0; /* no space left before wrapping data */ |
| 127 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 128 | /* first, protect the end of the buffer */ |
| 129 | memmove(pos + delta, pos, bi_end(b) - pos); |
| 130 | |
| 131 | /* now, copy str over pos */ |
| 132 | if (len && str) { |
| 133 | memcpy(pos, str, len); |
| 134 | pos[len] = '\r'; |
| 135 | pos[len + 1] = '\n'; |
| 136 | } |
| 137 | |
| 138 | b->i += delta; |
| 139 | return delta; |
| 140 | } |
| 141 | |
Willy Tarreau | 27187ab | 2015-07-02 12:50:23 +0200 | [diff] [blame] | 142 | /* This function realigns a possibly wrapping buffer so that the input part is |
| 143 | * contiguous and starts at the beginning of the buffer and the output part |
| 144 | * ends at the end of the buffer. This provides the best conditions since it |
| 145 | * allows the largest inputs to be processed at once and ensures that once the |
| 146 | * output data leaves, the whole buffer is available at once. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 147 | */ |
| 148 | void buffer_slow_realign(struct buffer *buf) |
| 149 | { |
Willy Tarreau | 27187ab | 2015-07-02 12:50:23 +0200 | [diff] [blame] | 150 | int block1 = buf->o; |
| 151 | int block2 = 0; |
| 152 | |
| 153 | /* process output data in two steps to cover wrapping */ |
| 154 | if (block1 > buf->p - buf->data) { |
| 155 | block2 = buf->p - buf->data; |
| 156 | block1 -= block2; |
| 157 | } |
| 158 | memcpy(swap_buffer + buf->size - buf->o, bo_ptr(buf), block1); |
| 159 | memcpy(swap_buffer + buf->size - block2, buf->data, block2); |
| 160 | |
| 161 | /* process input data in two steps to cover wrapping */ |
| 162 | block1 = buf->i; |
| 163 | block2 = 0; |
| 164 | |
| 165 | if (block1 > buf->data + buf->size - buf->p) { |
| 166 | block1 = buf->data + buf->size - buf->p; |
| 167 | block2 = buf->i - block1; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 168 | } |
Willy Tarreau | 27187ab | 2015-07-02 12:50:23 +0200 | [diff] [blame] | 169 | memcpy(swap_buffer, bi_ptr(buf), block1); |
| 170 | memcpy(swap_buffer + block1, buf->data, block2); |
| 171 | |
| 172 | /* reinject changes into the buffer */ |
| 173 | memcpy(buf->data, swap_buffer, buf->i); |
| 174 | memcpy(buf->data + buf->size - buf->o, swap_buffer + buf->size - buf->o, buf->o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 175 | |
| 176 | buf->p = buf->data; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 181 | * destination. It does not use any intermediate buffer and does the move in |
| 182 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 183 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 184 | * changed, the caller is responsible for that. |
| 185 | */ |
| 186 | void buffer_bounce_realign(struct buffer *buf) |
| 187 | { |
| 188 | int advance, to_move; |
| 189 | char *from, *to; |
| 190 | |
| 191 | from = bo_ptr(buf); |
| 192 | advance = buf->data + buf->size - from; |
| 193 | if (!advance) |
| 194 | return; |
| 195 | |
| 196 | to_move = buffer_len(buf); |
| 197 | while (to_move) { |
| 198 | char last, save; |
| 199 | |
| 200 | last = *from; |
| 201 | to = from + advance; |
| 202 | if (to >= buf->data + buf->size) |
| 203 | to -= buf->size; |
| 204 | |
| 205 | while (1) { |
| 206 | save = *to; |
| 207 | *to = last; |
| 208 | last = save; |
| 209 | to_move--; |
| 210 | if (!to_move) |
| 211 | break; |
| 212 | |
| 213 | /* check if we went back home after rotating a number of bytes */ |
| 214 | if (to == from) |
| 215 | break; |
| 216 | |
| 217 | /* if we ended up in the empty area, let's walk to next place. The |
| 218 | * empty area is either between buf->r and from or before from or |
| 219 | * after buf->r. |
| 220 | */ |
| 221 | if (from > bi_end(buf)) { |
| 222 | if (to >= bi_end(buf) && to < from) |
| 223 | break; |
| 224 | } else if (from < bi_end(buf)) { |
| 225 | if (to < from || to >= bi_end(buf)) |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | /* we have overwritten a byte of the original set, let's move it */ |
| 230 | to += advance; |
| 231 | if (to >= buf->data + buf->size) |
| 232 | to -= buf->size; |
| 233 | } |
| 234 | |
| 235 | from++; |
| 236 | if (from >= buf->data + buf->size) |
| 237 | from -= buf->size; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | * Dumps part or all of a buffer. |
| 244 | */ |
| 245 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 246 | { |
| 247 | fprintf(o, "Dumping buffer %p\n", b); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 248 | fprintf(o, " data=%p o=%d i=%d p=%p\n" |
| 249 | " relative: p=0x%04x\n", |
| 250 | b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data)); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 251 | |
| 252 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 253 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 254 | /* dump hexa */ |
| 255 | while (from < to) { |
| 256 | int i; |
| 257 | |
| 258 | fprintf(o, " %04x: ", from); |
| 259 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
| 260 | fprintf(o, "%02x ", (unsigned char)b->data[from + i]); |
| 261 | if (((from + i) & 15) == 7) |
| 262 | fprintf(o, "- "); |
| 263 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 264 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 265 | int j = 0; |
| 266 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 267 | for (j = 0; j < from + 16 - to; j++) |
| 268 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 269 | if (j > 8) |
| 270 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 271 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 272 | fprintf(o, " "); |
| 273 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 95898ac | 2012-11-26 00:57:40 +0100 | [diff] [blame] | 274 | fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ; |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 275 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 276 | fprintf(o, "\n"); |
| 277 | } |
| 278 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 279 | } |
| 280 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 281 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 284 | void __offer_buffer(void *from, unsigned int threshold) |
| 285 | { |
| 286 | struct buffer_wait *wait, *bak; |
| 287 | int avail; |
| 288 | |
| 289 | /* For now, we consider that all objects need 1 buffer, so we can stop |
| 290 | * waking up them once we have enough of them to eat all the available |
| 291 | * buffers. Note that we don't really know if they are streams or just |
| 292 | * other tasks, but that's a rough estimate. Similarly, for each cached |
| 293 | * event we'll need 1 buffer. If no buffer is currently used, always |
| 294 | * wake up the number of tasks we can offer a buffer based on what is |
| 295 | * allocated, and in any case at least one task per two reserved |
| 296 | * buffers. |
| 297 | */ |
| 298 | avail = pool2_buffer->allocated - pool2_buffer->used - global.tune.reserved_bufs / 2; |
| 299 | |
| 300 | list_for_each_entry_safe(wait, bak, &buffer_wq, list) { |
| 301 | if (avail <= threshold) |
| 302 | break; |
| 303 | |
| 304 | if (wait->target == from || !wait->wakeup_cb(wait->target)) |
| 305 | continue; |
| 306 | |
| 307 | LIST_DEL(&wait->list); |
| 308 | LIST_INIT(&wait->list); |
| 309 | |
| 310 | avail--; |
| 311 | } |
| 312 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * Local variables: |
| 316 | * c-indent-level: 8 |
| 317 | * c-basic-offset: 8 |
| 318 | * End: |
| 319 | */ |