blob: b365888d089548d34d78fa8f906244248b5b7512 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
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 Lallemandbe0efd82012-11-22 18:01:40 +010013#include <ctype.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/buffer.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020020
21#include <types/global.h>
22
Willy Tarreau9b28e032012-10-12 23:49:43 +020023struct pool_head *pool2_buffer;
24
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010025/* 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 Tarreau2a4b5432014-11-24 11:39:34 +010030 */
31struct buffer buf_empty = { .p = buf_empty.data };
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010032struct buffer buf_wanted = { .p = buf_wanted.data };
Willy Tarreau9b28e032012-10-12 23:49:43 +020033
Christopher Fauleta73e59b2016-12-09 17:30:18 +010034/* list of objects waiting for at least one buffer */
35struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
Emeric Bruna1dd2432017-06-21 15:42:52 +020036#ifdef USE_THREAD
37HA_SPINLOCK_T buffer_wq_lock;
38#endif
Christopher Fauleta73e59b2016-12-09 17:30:18 +010039
Christopher Fauletad405f12017-08-29 15:30:11 +020040/* this buffer is always the same size as standard buffers and is used for
41 * swapping data inside a buffer.
42 */
Christopher Fauletba39f232017-08-29 14:43:04 +020043static THREAD_LOCAL char *swap_buffer = NULL;
44
45static int init_buffer_per_thread()
46{
47 swap_buffer = calloc(1, global.tune.bufsize);
48 if (swap_buffer == NULL)
49 return 0;
50 return 1;
51}
52
53static void deinit_buffer_per_thread()
54{
55 free(swap_buffer); swap_buffer = NULL;
56}
Christopher Fauletad405f12017-08-29 15:30:11 +020057
Willy Tarreau9b28e032012-10-12 23:49:43 +020058/* perform minimal intializations, report 0 in case of error, 1 if OK. */
59int init_buffer()
60{
Willy Tarreaua24adf02014-11-27 01:11:56 +010061 void *buffer;
62
Willy Tarreau484b53d2016-01-25 02:23:25 +010063 pool2_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT);
Willy Tarreaua24adf02014-11-27 01:11:56 +010064 if (!pool2_buffer)
65 return 0;
66
67 /* The reserved buffer is what we leave behind us. Thus we always need
68 * at least one extra buffer in minavail otherwise we'll end up waking
69 * up tasks with no memory available, causing a lot of useless wakeups.
70 * That means that we always want to have at least 3 buffers available
71 * (2 for current session, one for next session that might be needed to
72 * release a server connection).
73 */
74 pool2_buffer->minavail = MAX(global.tune.reserved_bufs, 3);
Willy Tarreau33cb0652014-12-23 22:52:37 +010075 if (global.tune.buf_limit)
76 pool2_buffer->limit = global.tune.buf_limit;
Willy Tarreaua24adf02014-11-27 01:11:56 +010077
Christopher Faulet2a944ee2017-11-07 10:42:54 +010078 HA_SPIN_INIT(&buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +020079
Willy Tarreaua24adf02014-11-27 01:11:56 +010080 buffer = pool_refill_alloc(pool2_buffer, pool2_buffer->minavail - 1);
81 if (!buffer)
82 return 0;
83
84 pool_free2(pool2_buffer, buffer);
Christopher Fauletad405f12017-08-29 15:30:11 +020085
Christopher Fauletcd7879a2017-10-27 13:53:47 +020086 hap_register_per_thread_init(init_buffer_per_thread);
87 hap_register_per_thread_deinit(deinit_buffer_per_thread);
Willy Tarreaua24adf02014-11-27 01:11:56 +010088 return 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +020089}
90
Christopher Fauletad405f12017-08-29 15:30:11 +020091void deinit_buffer()
92{
Christopher Fauletad405f12017-08-29 15:30:11 +020093 pool_destroy2(pool2_buffer);
94}
95
Willy Tarreauaf819352012-08-27 22:08:00 +020096/* This function writes the string <str> at position <pos> which must be in
97 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
98 * <l> and <r> are updated to be valid after the shift. The shift value
99 * (positive or negative) is returned. If there's no space left, the move is
100 * not done. The function does not adjust ->o because it does not make sense to
101 * use it on data scheduled to be sent. For the same reason, it does not make
102 * sense to call this function on unparsed data, so <orig> is not updated. The
103 * string length is taken from parameter <len>. If <len> is null, the <str>
104 * pointer is allowed to be null.
105 */
106int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
107{
108 int delta;
109
110 delta = len - (end - pos);
111
Thierry FOURNIERfdda6772015-03-10 01:55:01 +0100112 if (bi_end(b) + delta > b->data + b->size)
Willy Tarreauaf819352012-08-27 22:08:00 +0200113 return 0; /* no space left */
114
115 if (buffer_not_empty(b) &&
116 bi_end(b) + delta > bo_ptr(b) &&
117 bo_ptr(b) >= bi_end(b))
118 return 0; /* no space left before wrapping data */
119
120 /* first, protect the end of the buffer */
121 memmove(end + delta, end, bi_end(b) - end);
122
123 /* now, copy str over pos */
124 if (len)
125 memcpy(pos, str, len);
126
127 b->i += delta;
128
Willy Tarreau5fb38032012-12-16 19:39:09 +0100129 if (buffer_empty(b))
Willy Tarreauaf819352012-08-27 22:08:00 +0200130 b->p = b->data;
131
132 return delta;
133}
134
135/*
136 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
137 * argument informs about the length of string <str> so that we don't have to
138 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
139 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
140 * some circumstances. The send limit is *not* adjusted. Same comments as above
141 * for the valid use cases.
142 *
143 * The number of bytes added is returned on success. 0 is returned on failure.
144 */
145int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
146{
147 int delta;
148
149 delta = len + 2;
150
151 if (bi_end(b) + delta >= b->data + b->size)
152 return 0; /* no space left */
153
Godbacha6547c12014-10-31 13:16:37 +0800154 if (buffer_not_empty(b) &&
155 bi_end(b) + delta > bo_ptr(b) &&
156 bo_ptr(b) >= bi_end(b))
157 return 0; /* no space left before wrapping data */
158
Willy Tarreauaf819352012-08-27 22:08:00 +0200159 /* first, protect the end of the buffer */
160 memmove(pos + delta, pos, bi_end(b) - pos);
161
162 /* now, copy str over pos */
163 if (len && str) {
164 memcpy(pos, str, len);
165 pos[len] = '\r';
166 pos[len + 1] = '\n';
167 }
168
169 b->i += delta;
170 return delta;
171}
172
Willy Tarreau27187ab2015-07-02 12:50:23 +0200173/* This function realigns a possibly wrapping buffer so that the input part is
174 * contiguous and starts at the beginning of the buffer and the output part
175 * ends at the end of the buffer. This provides the best conditions since it
176 * allows the largest inputs to be processed at once and ensures that once the
177 * output data leaves, the whole buffer is available at once.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200178 */
179void buffer_slow_realign(struct buffer *buf)
180{
Willy Tarreau27187ab2015-07-02 12:50:23 +0200181 int block1 = buf->o;
182 int block2 = 0;
183
184 /* process output data in two steps to cover wrapping */
185 if (block1 > buf->p - buf->data) {
186 block2 = buf->p - buf->data;
187 block1 -= block2;
188 }
189 memcpy(swap_buffer + buf->size - buf->o, bo_ptr(buf), block1);
190 memcpy(swap_buffer + buf->size - block2, buf->data, block2);
191
192 /* process input data in two steps to cover wrapping */
193 block1 = buf->i;
194 block2 = 0;
195
196 if (block1 > buf->data + buf->size - buf->p) {
197 block1 = buf->data + buf->size - buf->p;
198 block2 = buf->i - block1;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200199 }
Willy Tarreau27187ab2015-07-02 12:50:23 +0200200 memcpy(swap_buffer, bi_ptr(buf), block1);
201 memcpy(swap_buffer + block1, buf->data, block2);
202
203 /* reinject changes into the buffer */
204 memcpy(buf->data, swap_buffer, buf->i);
205 memcpy(buf->data + buf->size - buf->o, swap_buffer + buf->size - buf->o, buf->o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200206
207 buf->p = buf->data;
208}
209
Willy Tarreauc7e42382012-08-24 19:22:53 +0200210/*
211 * Dumps part or all of a buffer.
212 */
213void buffer_dump(FILE *o, struct buffer *b, int from, int to)
214{
215 fprintf(o, "Dumping buffer %p\n", b);
William Lallemandbe0efd82012-11-22 18:01:40 +0100216 fprintf(o, " data=%p o=%d i=%d p=%p\n"
217 " relative: p=0x%04x\n",
218 b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data));
Willy Tarreauc7e42382012-08-24 19:22:53 +0200219
220 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100221 fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
222 /* dump hexa */
223 while (from < to) {
224 int i;
225
226 fprintf(o, " %04x: ", from);
227 for (i = 0; ((from + i) < to) && (i < 16) ; i++) {
228 fprintf(o, "%02x ", (unsigned char)b->data[from + i]);
229 if (((from + i) & 15) == 7)
230 fprintf(o, "- ");
231 }
Godbachc08057c2013-11-14 10:15:20 +0800232 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +0800233 int j = 0;
234
Godbachc08057c2013-11-14 10:15:20 +0800235 for (j = 0; j < from + 16 - to; j++)
236 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +0800237 if (j > 8)
238 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +0800239 }
William Lallemandbe0efd82012-11-22 18:01:40 +0100240 fprintf(o, " ");
241 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau95898ac2012-11-26 00:57:40 +0100242 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100243 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
244 fprintf(o, "\n");
245 }
246 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200247 }
248 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100249 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200250}
251
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100252void __offer_buffer(void *from, unsigned int threshold)
253{
254 struct buffer_wait *wait, *bak;
255 int avail;
256
257 /* For now, we consider that all objects need 1 buffer, so we can stop
258 * waking up them once we have enough of them to eat all the available
259 * buffers. Note that we don't really know if they are streams or just
260 * other tasks, but that's a rough estimate. Similarly, for each cached
261 * event we'll need 1 buffer. If no buffer is currently used, always
262 * wake up the number of tasks we can offer a buffer based on what is
263 * allocated, and in any case at least one task per two reserved
264 * buffers.
265 */
266 avail = pool2_buffer->allocated - pool2_buffer->used - global.tune.reserved_bufs / 2;
267
268 list_for_each_entry_safe(wait, bak, &buffer_wq, list) {
269 if (avail <= threshold)
270 break;
271
272 if (wait->target == from || !wait->wakeup_cb(wait->target))
273 continue;
274
275 LIST_DEL(&wait->list);
276 LIST_INIT(&wait->list);
277
278 avail--;
279 }
280}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200281
282/*
283 * Local variables:
284 * c-indent-level: 8
285 * c-basic-offset: 8
286 * End:
287 */