blob: 7a49ba63d4986d67f11b70544cc2963dc8436e86 [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);
36
Christopher Fauletad405f12017-08-29 15:30:11 +020037/* this buffer is always the same size as standard buffers and is used for
38 * swapping data inside a buffer.
39 */
40static char *swap_buffer = NULL;
41
Willy Tarreau9b28e032012-10-12 23:49:43 +020042/* perform minimal intializations, report 0 in case of error, 1 if OK. */
43int init_buffer()
44{
Willy Tarreaua24adf02014-11-27 01:11:56 +010045 void *buffer;
46
Willy Tarreau484b53d2016-01-25 02:23:25 +010047 pool2_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT);
Willy Tarreaua24adf02014-11-27 01:11:56 +010048 if (!pool2_buffer)
49 return 0;
50
51 /* The reserved buffer is what we leave behind us. Thus we always need
52 * at least one extra buffer in minavail otherwise we'll end up waking
53 * up tasks with no memory available, causing a lot of useless wakeups.
54 * That means that we always want to have at least 3 buffers available
55 * (2 for current session, one for next session that might be needed to
56 * release a server connection).
57 */
58 pool2_buffer->minavail = MAX(global.tune.reserved_bufs, 3);
Willy Tarreau33cb0652014-12-23 22:52:37 +010059 if (global.tune.buf_limit)
60 pool2_buffer->limit = global.tune.buf_limit;
Willy Tarreaua24adf02014-11-27 01:11:56 +010061
62 buffer = pool_refill_alloc(pool2_buffer, pool2_buffer->minavail - 1);
63 if (!buffer)
64 return 0;
65
66 pool_free2(pool2_buffer, buffer);
Christopher Fauletad405f12017-08-29 15:30:11 +020067
68 swap_buffer = calloc(1, global.tune.bufsize);
69 if (swap_buffer == NULL)
70 return 0;
71
Willy Tarreaua24adf02014-11-27 01:11:56 +010072 return 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +020073}
74
Christopher Fauletad405f12017-08-29 15:30:11 +020075void deinit_buffer()
76{
77 free(swap_buffer); swap_buffer = NULL;
78 pool_destroy2(pool2_buffer);
79}
80
Willy Tarreauaf819352012-08-27 22:08:00 +020081/* This function writes the string <str> at position <pos> which must be in
82 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
83 * <l> and <r> are updated to be valid after the shift. The shift value
84 * (positive or negative) is returned. If there's no space left, the move is
85 * not done. The function does not adjust ->o because it does not make sense to
86 * use it on data scheduled to be sent. For the same reason, it does not make
87 * sense to call this function on unparsed data, so <orig> is not updated. The
88 * string length is taken from parameter <len>. If <len> is null, the <str>
89 * pointer is allowed to be null.
90 */
91int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
92{
93 int delta;
94
95 delta = len - (end - pos);
96
Thierry FOURNIERfdda6772015-03-10 01:55:01 +010097 if (bi_end(b) + delta > b->data + b->size)
Willy Tarreauaf819352012-08-27 22:08:00 +020098 return 0; /* no space left */
99
100 if (buffer_not_empty(b) &&
101 bi_end(b) + delta > bo_ptr(b) &&
102 bo_ptr(b) >= bi_end(b))
103 return 0; /* no space left before wrapping data */
104
105 /* first, protect the end of the buffer */
106 memmove(end + delta, end, bi_end(b) - end);
107
108 /* now, copy str over pos */
109 if (len)
110 memcpy(pos, str, len);
111
112 b->i += delta;
113
Willy Tarreau5fb38032012-12-16 19:39:09 +0100114 if (buffer_empty(b))
Willy Tarreauaf819352012-08-27 22:08:00 +0200115 b->p = b->data;
116
117 return delta;
118}
119
120/*
121 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
122 * argument informs about the length of string <str> so that we don't have to
123 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
124 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
125 * some circumstances. The send limit is *not* adjusted. Same comments as above
126 * for the valid use cases.
127 *
128 * The number of bytes added is returned on success. 0 is returned on failure.
129 */
130int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
131{
132 int delta;
133
134 delta = len + 2;
135
136 if (bi_end(b) + delta >= b->data + b->size)
137 return 0; /* no space left */
138
Godbacha6547c12014-10-31 13:16:37 +0800139 if (buffer_not_empty(b) &&
140 bi_end(b) + delta > bo_ptr(b) &&
141 bo_ptr(b) >= bi_end(b))
142 return 0; /* no space left before wrapping data */
143
Willy Tarreauaf819352012-08-27 22:08:00 +0200144 /* first, protect the end of the buffer */
145 memmove(pos + delta, pos, bi_end(b) - pos);
146
147 /* now, copy str over pos */
148 if (len && str) {
149 memcpy(pos, str, len);
150 pos[len] = '\r';
151 pos[len + 1] = '\n';
152 }
153
154 b->i += delta;
155 return delta;
156}
157
Willy Tarreau27187ab2015-07-02 12:50:23 +0200158/* This function realigns a possibly wrapping buffer so that the input part is
159 * contiguous and starts at the beginning of the buffer and the output part
160 * ends at the end of the buffer. This provides the best conditions since it
161 * allows the largest inputs to be processed at once and ensures that once the
162 * output data leaves, the whole buffer is available at once.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200163 */
164void buffer_slow_realign(struct buffer *buf)
165{
Willy Tarreau27187ab2015-07-02 12:50:23 +0200166 int block1 = buf->o;
167 int block2 = 0;
168
169 /* process output data in two steps to cover wrapping */
170 if (block1 > buf->p - buf->data) {
171 block2 = buf->p - buf->data;
172 block1 -= block2;
173 }
174 memcpy(swap_buffer + buf->size - buf->o, bo_ptr(buf), block1);
175 memcpy(swap_buffer + buf->size - block2, buf->data, block2);
176
177 /* process input data in two steps to cover wrapping */
178 block1 = buf->i;
179 block2 = 0;
180
181 if (block1 > buf->data + buf->size - buf->p) {
182 block1 = buf->data + buf->size - buf->p;
183 block2 = buf->i - block1;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200184 }
Willy Tarreau27187ab2015-07-02 12:50:23 +0200185 memcpy(swap_buffer, bi_ptr(buf), block1);
186 memcpy(swap_buffer + block1, buf->data, block2);
187
188 /* reinject changes into the buffer */
189 memcpy(buf->data, swap_buffer, buf->i);
190 memcpy(buf->data + buf->size - buf->o, swap_buffer + buf->size - buf->o, buf->o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200191
192 buf->p = buf->data;
193}
194
Willy Tarreauc7e42382012-08-24 19:22:53 +0200195/*
196 * Dumps part or all of a buffer.
197 */
198void buffer_dump(FILE *o, struct buffer *b, int from, int to)
199{
200 fprintf(o, "Dumping buffer %p\n", b);
William Lallemandbe0efd82012-11-22 18:01:40 +0100201 fprintf(o, " data=%p o=%d i=%d p=%p\n"
202 " relative: p=0x%04x\n",
203 b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data));
Willy Tarreauc7e42382012-08-24 19:22:53 +0200204
205 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100206 fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
207 /* dump hexa */
208 while (from < to) {
209 int i;
210
211 fprintf(o, " %04x: ", from);
212 for (i = 0; ((from + i) < to) && (i < 16) ; i++) {
213 fprintf(o, "%02x ", (unsigned char)b->data[from + i]);
214 if (((from + i) & 15) == 7)
215 fprintf(o, "- ");
216 }
Godbachc08057c2013-11-14 10:15:20 +0800217 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +0800218 int j = 0;
219
Godbachc08057c2013-11-14 10:15:20 +0800220 for (j = 0; j < from + 16 - to; j++)
221 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +0800222 if (j > 8)
223 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +0800224 }
William Lallemandbe0efd82012-11-22 18:01:40 +0100225 fprintf(o, " ");
226 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau95898ac2012-11-26 00:57:40 +0100227 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100228 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
229 fprintf(o, "\n");
230 }
231 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200232 }
233 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100234 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200235}
236
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100237void __offer_buffer(void *from, unsigned int threshold)
238{
239 struct buffer_wait *wait, *bak;
240 int avail;
241
242 /* For now, we consider that all objects need 1 buffer, so we can stop
243 * waking up them once we have enough of them to eat all the available
244 * buffers. Note that we don't really know if they are streams or just
245 * other tasks, but that's a rough estimate. Similarly, for each cached
246 * event we'll need 1 buffer. If no buffer is currently used, always
247 * wake up the number of tasks we can offer a buffer based on what is
248 * allocated, and in any case at least one task per two reserved
249 * buffers.
250 */
251 avail = pool2_buffer->allocated - pool2_buffer->used - global.tune.reserved_bufs / 2;
252
253 list_for_each_entry_safe(wait, bak, &buffer_wq, list) {
254 if (avail <= threshold)
255 break;
256
257 if (wait->target == from || !wait->wakeup_cb(wait->target))
258 continue;
259
260 LIST_DEL(&wait->list);
261 LIST_INIT(&wait->list);
262
263 avail--;
264 }
265}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200266
267/*
268 * Local variables:
269 * c-indent-level: 8
270 * c-basic-offset: 8
271 * End:
272 */