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 | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 23 | struct pool_head *pool_head_buffer; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 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); |
Willy Tarreau | b6a2f58 | 2017-11-26 11:08:14 +0100 | [diff] [blame] | 36 | __decl_hathreads(HA_SPINLOCK_T __attribute__((aligned(64))) buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 37 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 38 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 39 | int init_buffer() |
| 40 | { |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 41 | void *buffer; |
| 42 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 43 | pool_head_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT); |
| 44 | if (!pool_head_buffer) |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | |
| 47 | /* The reserved buffer is what we leave behind us. Thus we always need |
| 48 | * at least one extra buffer in minavail otherwise we'll end up waking |
| 49 | * up tasks with no memory available, causing a lot of useless wakeups. |
| 50 | * That means that we always want to have at least 3 buffers available |
| 51 | * (2 for current session, one for next session that might be needed to |
| 52 | * release a server connection). |
| 53 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 54 | pool_head_buffer->minavail = MAX(global.tune.reserved_bufs, 3); |
Willy Tarreau | 33cb065 | 2014-12-23 22:52:37 +0100 | [diff] [blame] | 55 | if (global.tune.buf_limit) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 56 | pool_head_buffer->limit = global.tune.buf_limit; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 57 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 58 | HA_SPIN_INIT(&buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 60 | buffer = pool_refill_alloc(pool_head_buffer, pool_head_buffer->minavail - 1); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 61 | if (!buffer) |
| 62 | return 0; |
| 63 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 64 | pool_free(pool_head_buffer, buffer); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 65 | return 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 68 | void deinit_buffer() |
| 69 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 70 | pool_destroy(pool_head_buffer); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 73 | /* This function writes the string <str> at position <pos> which must be in |
| 74 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 75 | * <l> and <r> are updated to be valid after the shift. The shift value |
| 76 | * (positive or negative) is returned. If there's no space left, the move is |
| 77 | * not done. The function does not adjust ->o because it does not make sense to |
| 78 | * use it on data scheduled to be sent. For the same reason, it does not make |
| 79 | * sense to call this function on unparsed data, so <orig> is not updated. The |
| 80 | * string length is taken from parameter <len>. If <len> is null, the <str> |
| 81 | * pointer is allowed to be null. |
| 82 | */ |
| 83 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
| 84 | { |
| 85 | int delta; |
| 86 | |
| 87 | delta = len - (end - pos); |
| 88 | |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 89 | if (b_tail(b) + delta > b->data + b->size) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 90 | return 0; /* no space left */ |
| 91 | |
| 92 | if (buffer_not_empty(b) && |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 93 | b_tail(b) + delta > b_head(b) && |
| 94 | b_head(b) >= b_tail(b)) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 95 | return 0; /* no space left before wrapping data */ |
| 96 | |
| 97 | /* first, protect the end of the buffer */ |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 98 | memmove(end + delta, end, b_tail(b) - end); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 99 | |
| 100 | /* now, copy str over pos */ |
| 101 | if (len) |
| 102 | memcpy(pos, str, len); |
| 103 | |
| 104 | b->i += delta; |
| 105 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 106 | if (buffer_empty(b)) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 107 | b->p = b->data; |
| 108 | |
| 109 | return delta; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 114 | * argument informs about the length of string <str> so that we don't have to |
| 115 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 116 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
| 117 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 118 | * for the valid use cases. |
| 119 | * |
| 120 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 121 | */ |
| 122 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 123 | { |
| 124 | int delta; |
| 125 | |
| 126 | delta = len + 2; |
| 127 | |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 128 | if (b_tail(b) + delta >= b->data + b->size) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 129 | return 0; /* no space left */ |
| 130 | |
Godbach | a6547c1 | 2014-10-31 13:16:37 +0800 | [diff] [blame] | 131 | if (buffer_not_empty(b) && |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 132 | b_tail(b) + delta > b_head(b) && |
| 133 | b_head(b) >= b_tail(b)) |
Godbach | a6547c1 | 2014-10-31 13:16:37 +0800 | [diff] [blame] | 134 | return 0; /* no space left before wrapping data */ |
| 135 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 136 | /* first, protect the end of the buffer */ |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 137 | memmove(pos + delta, pos, b_tail(b) - pos); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 138 | |
| 139 | /* now, copy str over pos */ |
| 140 | if (len && str) { |
| 141 | memcpy(pos, str, len); |
| 142 | pos[len] = '\r'; |
| 143 | pos[len + 1] = '\n'; |
| 144 | } |
| 145 | |
| 146 | b->i += delta; |
| 147 | return delta; |
| 148 | } |
| 149 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Dumps part or all of a buffer. |
| 152 | */ |
| 153 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 154 | { |
| 155 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 156 | fprintf(o, " data=%p o=%u i=%u p=%p\n" |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 157 | " relative: p=0x%04x\n", |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 158 | b->data, (unsigned int)b->o, (unsigned int)b->i, b->p, (unsigned int)(b->p - b->data)); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 159 | |
| 160 | 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] | 161 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 162 | /* dump hexa */ |
| 163 | while (from < to) { |
| 164 | int i; |
| 165 | |
| 166 | fprintf(o, " %04x: ", from); |
| 167 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
| 168 | fprintf(o, "%02x ", (unsigned char)b->data[from + i]); |
| 169 | if (((from + i) & 15) == 7) |
| 170 | fprintf(o, "- "); |
| 171 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 172 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 173 | int j = 0; |
| 174 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 175 | for (j = 0; j < from + 16 - to; j++) |
| 176 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 177 | if (j > 8) |
| 178 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 179 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 180 | fprintf(o, " "); |
| 181 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 95898ac | 2012-11-26 00:57:40 +0100 | [diff] [blame] | 182 | 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] | 183 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 184 | fprintf(o, "\n"); |
| 185 | } |
| 186 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 187 | } |
| 188 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 189 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 192 | /* see offer_buffer() for details */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 193 | void __offer_buffer(void *from, unsigned int threshold) |
| 194 | { |
| 195 | struct buffer_wait *wait, *bak; |
| 196 | int avail; |
| 197 | |
| 198 | /* For now, we consider that all objects need 1 buffer, so we can stop |
| 199 | * waking up them once we have enough of them to eat all the available |
| 200 | * buffers. Note that we don't really know if they are streams or just |
| 201 | * other tasks, but that's a rough estimate. Similarly, for each cached |
| 202 | * event we'll need 1 buffer. If no buffer is currently used, always |
| 203 | * wake up the number of tasks we can offer a buffer based on what is |
| 204 | * allocated, and in any case at least one task per two reserved |
| 205 | * buffers. |
| 206 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 207 | avail = pool_head_buffer->allocated - pool_head_buffer->used - global.tune.reserved_bufs / 2; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 208 | |
| 209 | list_for_each_entry_safe(wait, bak, &buffer_wq, list) { |
| 210 | if (avail <= threshold) |
| 211 | break; |
| 212 | |
| 213 | if (wait->target == from || !wait->wakeup_cb(wait->target)) |
| 214 | continue; |
| 215 | |
| 216 | LIST_DEL(&wait->list); |
| 217 | LIST_INIT(&wait->list); |
| 218 | |
| 219 | avail--; |
| 220 | } |
| 221 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Local variables: |
| 225 | * c-indent-level: 8 |
| 226 | * c-basic-offset: 8 |
| 227 | * End: |
| 228 | */ |