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 | |
| 34 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 35 | int init_buffer() |
| 36 | { |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 37 | void *buffer; |
| 38 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 39 | pool2_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 40 | if (!pool2_buffer) |
| 41 | return 0; |
| 42 | |
| 43 | /* The reserved buffer is what we leave behind us. Thus we always need |
| 44 | * at least one extra buffer in minavail otherwise we'll end up waking |
| 45 | * up tasks with no memory available, causing a lot of useless wakeups. |
| 46 | * That means that we always want to have at least 3 buffers available |
| 47 | * (2 for current session, one for next session that might be needed to |
| 48 | * release a server connection). |
| 49 | */ |
| 50 | pool2_buffer->minavail = MAX(global.tune.reserved_bufs, 3); |
Willy Tarreau | 33cb065 | 2014-12-23 22:52:37 +0100 | [diff] [blame] | 51 | if (global.tune.buf_limit) |
| 52 | pool2_buffer->limit = global.tune.buf_limit; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 53 | |
| 54 | buffer = pool_refill_alloc(pool2_buffer, pool2_buffer->minavail - 1); |
| 55 | if (!buffer) |
| 56 | return 0; |
| 57 | |
| 58 | pool_free2(pool2_buffer, buffer); |
| 59 | return 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 62 | /* This function writes the string <str> at position <pos> which must be in |
| 63 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 64 | * <l> and <r> are updated to be valid after the shift. The shift value |
| 65 | * (positive or negative) is returned. If there's no space left, the move is |
| 66 | * not done. The function does not adjust ->o because it does not make sense to |
| 67 | * use it on data scheduled to be sent. For the same reason, it does not make |
| 68 | * sense to call this function on unparsed data, so <orig> is not updated. The |
| 69 | * string length is taken from parameter <len>. If <len> is null, the <str> |
| 70 | * pointer is allowed to be null. |
| 71 | */ |
| 72 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
| 73 | { |
| 74 | int delta; |
| 75 | |
| 76 | delta = len - (end - pos); |
| 77 | |
Thierry FOURNIER | fdda677 | 2015-03-10 01:55:01 +0100 | [diff] [blame] | 78 | if (bi_end(b) + delta > b->data + b->size) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 79 | return 0; /* no space left */ |
| 80 | |
| 81 | if (buffer_not_empty(b) && |
| 82 | bi_end(b) + delta > bo_ptr(b) && |
| 83 | bo_ptr(b) >= bi_end(b)) |
| 84 | return 0; /* no space left before wrapping data */ |
| 85 | |
| 86 | /* first, protect the end of the buffer */ |
| 87 | memmove(end + delta, end, bi_end(b) - end); |
| 88 | |
| 89 | /* now, copy str over pos */ |
| 90 | if (len) |
| 91 | memcpy(pos, str, len); |
| 92 | |
| 93 | b->i += delta; |
| 94 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 95 | if (buffer_empty(b)) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 96 | b->p = b->data; |
| 97 | |
| 98 | return delta; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 103 | * argument informs about the length of string <str> so that we don't have to |
| 104 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 105 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
| 106 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 107 | * for the valid use cases. |
| 108 | * |
| 109 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 110 | */ |
| 111 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 112 | { |
| 113 | int delta; |
| 114 | |
| 115 | delta = len + 2; |
| 116 | |
| 117 | if (bi_end(b) + delta >= b->data + b->size) |
| 118 | return 0; /* no space left */ |
| 119 | |
Godbach | a6547c1 | 2014-10-31 13:16:37 +0800 | [diff] [blame] | 120 | if (buffer_not_empty(b) && |
| 121 | bi_end(b) + delta > bo_ptr(b) && |
| 122 | bo_ptr(b) >= bi_end(b)) |
| 123 | return 0; /* no space left before wrapping data */ |
| 124 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 125 | /* first, protect the end of the buffer */ |
| 126 | memmove(pos + delta, pos, bi_end(b) - pos); |
| 127 | |
| 128 | /* now, copy str over pos */ |
| 129 | if (len && str) { |
| 130 | memcpy(pos, str, len); |
| 131 | pos[len] = '\r'; |
| 132 | pos[len + 1] = '\n'; |
| 133 | } |
| 134 | |
| 135 | b->i += delta; |
| 136 | return delta; |
| 137 | } |
| 138 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 139 | /* This function realigns input data in a possibly wrapping buffer so that it |
| 140 | * becomes contiguous and starts at the beginning of the buffer area. The |
| 141 | * function may only be used when the buffer's output is empty. |
| 142 | */ |
| 143 | void buffer_slow_realign(struct buffer *buf) |
| 144 | { |
| 145 | /* two possible cases : |
| 146 | * - the buffer is in one contiguous block, we move it in-place |
| 147 | * - the buffer is in two blocks, we move it via the swap_buffer |
| 148 | */ |
| 149 | if (buf->i) { |
| 150 | int block1 = buf->i; |
| 151 | int block2 = 0; |
| 152 | if (buf->p + buf->i > buf->data + buf->size) { |
| 153 | /* non-contiguous block */ |
| 154 | block1 = buf->data + buf->size - buf->p; |
| 155 | block2 = buf->p + buf->i - (buf->data + buf->size); |
| 156 | } |
| 157 | if (block2) |
| 158 | memcpy(swap_buffer, buf->data, block2); |
| 159 | memmove(buf->data, buf->p, block1); |
| 160 | if (block2) |
| 161 | memcpy(buf->data + block1, swap_buffer, block2); |
| 162 | } |
| 163 | |
| 164 | buf->p = buf->data; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 169 | * destination. It does not use any intermediate buffer and does the move in |
| 170 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 171 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 172 | * changed, the caller is responsible for that. |
| 173 | */ |
| 174 | void buffer_bounce_realign(struct buffer *buf) |
| 175 | { |
| 176 | int advance, to_move; |
| 177 | char *from, *to; |
| 178 | |
| 179 | from = bo_ptr(buf); |
| 180 | advance = buf->data + buf->size - from; |
| 181 | if (!advance) |
| 182 | return; |
| 183 | |
| 184 | to_move = buffer_len(buf); |
| 185 | while (to_move) { |
| 186 | char last, save; |
| 187 | |
| 188 | last = *from; |
| 189 | to = from + advance; |
| 190 | if (to >= buf->data + buf->size) |
| 191 | to -= buf->size; |
| 192 | |
| 193 | while (1) { |
| 194 | save = *to; |
| 195 | *to = last; |
| 196 | last = save; |
| 197 | to_move--; |
| 198 | if (!to_move) |
| 199 | break; |
| 200 | |
| 201 | /* check if we went back home after rotating a number of bytes */ |
| 202 | if (to == from) |
| 203 | break; |
| 204 | |
| 205 | /* if we ended up in the empty area, let's walk to next place. The |
| 206 | * empty area is either between buf->r and from or before from or |
| 207 | * after buf->r. |
| 208 | */ |
| 209 | if (from > bi_end(buf)) { |
| 210 | if (to >= bi_end(buf) && to < from) |
| 211 | break; |
| 212 | } else if (from < bi_end(buf)) { |
| 213 | if (to < from || to >= bi_end(buf)) |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | /* we have overwritten a byte of the original set, let's move it */ |
| 218 | to += advance; |
| 219 | if (to >= buf->data + buf->size) |
| 220 | to -= buf->size; |
| 221 | } |
| 222 | |
| 223 | from++; |
| 224 | if (from >= buf->data + buf->size) |
| 225 | from -= buf->size; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Dumps part or all of a buffer. |
| 232 | */ |
| 233 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 234 | { |
| 235 | fprintf(o, "Dumping buffer %p\n", b); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 236 | fprintf(o, " data=%p o=%d i=%d p=%p\n" |
| 237 | " relative: p=0x%04x\n", |
| 238 | 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] | 239 | |
| 240 | 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] | 241 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 242 | /* dump hexa */ |
| 243 | while (from < to) { |
| 244 | int i; |
| 245 | |
| 246 | fprintf(o, " %04x: ", from); |
| 247 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
| 248 | fprintf(o, "%02x ", (unsigned char)b->data[from + i]); |
| 249 | if (((from + i) & 15) == 7) |
| 250 | fprintf(o, "- "); |
| 251 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 252 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 253 | int j = 0; |
| 254 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 255 | for (j = 0; j < from + 16 - to; j++) |
| 256 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 257 | if (j > 8) |
| 258 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 259 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 260 | fprintf(o, " "); |
| 261 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 95898ac | 2012-11-26 00:57:40 +0100 | [diff] [blame] | 262 | 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] | 263 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 264 | fprintf(o, "\n"); |
| 265 | } |
| 266 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 267 | } |
| 268 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 269 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Local variables: |
| 275 | * c-indent-level: 8 |
| 276 | * c-basic-offset: 8 |
| 277 | * End: |
| 278 | */ |