blob: ce3eb40a962a118221918edde8d7f06b4786e21a [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * include/common/buffer.h
3 * Buffer management definitions, macros and inline functions.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _COMMON_BUFFER_H
23#define _COMMON_BUFFER_H
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
Willy Tarreau8c89c202012-09-28 16:02:48 +020029#include <common/chunk.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020031#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020032
33
34struct buffer {
35 char *p; /* buffer's start pointer, separates in and out data */
36 unsigned int size; /* buffer size in bytes */
37 unsigned int i; /* number of input bytes pending for analysis in the buffer */
38 unsigned int o; /* number of out bytes the sender can consume from this buffer */
39 char data[0]; /* <size> bytes */
40};
41
Christopher Fauleta73e59b2016-12-09 17:30:18 +010042/* an element of the <buffer_wq> list. It represents an object that need to
43 * acquire a buffer to continue its process. */
44struct buffer_wait {
45 void *target; /* The waiting object that should be woken up */
46 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
47 struct list list; /* Next element in the <buffer_wq> list */
48};
49
Willy Tarreau9b28e032012-10-12 23:49:43 +020050extern struct pool_head *pool2_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010051extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010052extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010053extern struct list buffer_wq;
Willy Tarreauc7e42382012-08-24 19:22:53 +020054
Willy Tarreau9b28e032012-10-12 23:49:43 +020055int init_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020056int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
57int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020058void buffer_dump(FILE *o, struct buffer *b, int from, int to);
59void buffer_slow_realign(struct buffer *buf);
60void buffer_bounce_realign(struct buffer *buf);
61
62/*****************************************************************/
63/* These functions are used to compute various buffer area sizes */
64/*****************************************************************/
65
66/* Returns an absolute pointer for a position relative to the current buffer's
67 * pointer. It is written so that it is optimal when <ofs> is a const. It is
68 * written as a macro instead of an inline function so that the compiler knows
69 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020070 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020071 */
72#define b_ptr(b, ofs) \
73 ({ \
74 char *__ret = (b)->p + (ofs); \
75 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
76 __ret -= (b)->size; \
77 else if ((ofs) < 0 && __ret < (b)->data) \
78 __ret += (b)->size; \
79 __ret; \
80 })
81
Willy Tarreaua75bcef2012-08-24 22:56:11 +020082/* Advances the buffer by <adv> bytes, which means that the buffer
83 * pointer advances, and that as many bytes from in are transferred
84 * to out. The caller is responsible for ensuring that adv is always
85 * smaller than or equal to b->i.
86 */
87static inline void b_adv(struct buffer *b, unsigned int adv)
88{
89 b->i -= adv;
90 b->o += adv;
91 b->p = b_ptr(b, adv);
92}
93
94/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
95 * backwards, and that as many bytes from out are moved to in. The caller is
96 * responsible for ensuring that adv is always smaller than or equal to b->o.
97 */
98static inline void b_rew(struct buffer *b, unsigned int adv)
99{
100 b->i += adv;
101 b->o -= adv;
102 b->p = b_ptr(b, (int)-adv);
103}
104
Willy Tarreauc7e42382012-08-24 19:22:53 +0200105/* Returns the start of the input data in a buffer */
106static inline char *bi_ptr(const struct buffer *b)
107{
108 return b->p;
109}
110
111/* Returns the end of the input data in a buffer (pointer to next
112 * insertion point).
113 */
114static inline char *bi_end(const struct buffer *b)
115{
116 char *ret = b->p + b->i;
117
118 if (ret >= b->data + b->size)
119 ret -= b->size;
120 return ret;
121}
122
123/* Returns the amount of input data that can contiguously be read at once */
124static inline int bi_contig_data(const struct buffer *b)
125{
126 int data = b->data + b->size - b->p;
127
128 if (data > b->i)
129 data = b->i;
130 return data;
131}
132
133/* Returns the start of the output data in a buffer */
134static inline char *bo_ptr(const struct buffer *b)
135{
136 char *ret = b->p - b->o;
137
138 if (ret < b->data)
139 ret += b->size;
140 return ret;
141}
142
143/* Returns the end of the output data in a buffer */
144static inline char *bo_end(const struct buffer *b)
145{
146 return b->p;
147}
148
149/* Returns the amount of output data that can contiguously be read at once */
150static inline int bo_contig_data(const struct buffer *b)
151{
152 char *beg = b->p - b->o;
153
154 if (beg < b->data)
155 return b->data - beg;
156 return b->o;
157}
158
159/* Return the buffer's length in bytes by summing the input and the output */
160static inline int buffer_len(const struct buffer *buf)
161{
162 return buf->i + buf->o;
163}
164
165/* Return non-zero only if the buffer is not empty */
166static inline int buffer_not_empty(const struct buffer *buf)
167{
168 return buf->i | buf->o;
169}
170
171/* Return non-zero only if the buffer is empty */
172static inline int buffer_empty(const struct buffer *buf)
173{
174 return !buffer_not_empty(buf);
175}
176
Willy Tarreau42d06662012-08-27 19:51:36 +0200177/* Returns non-zero if the buffer's INPUT is considered full, which means that
178 * it holds at least as much INPUT data as (size - reserve). This also means
179 * that data that are scheduled for output are considered as potential free
180 * space, and that the reserved space is always considered as not usable. This
181 * information alone cannot be used as a general purpose free space indicator.
182 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100183 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200184 * generic function taking everything into account.
185 */
186static inline int buffer_full(const struct buffer *b, unsigned int reserve)
187{
Willy Tarreau4428a292014-11-28 20:54:13 +0100188 if (b == &buf_empty)
189 return 0;
190
Willy Tarreau42d06662012-08-27 19:51:36 +0200191 return (b->i + reserve >= b->size);
192}
193
Willy Tarreauc7e42382012-08-24 19:22:53 +0200194/* Normalizes a pointer after a subtract */
195static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
196{
197 if (ptr < buf->data)
198 ptr += buf->size;
199 return ptr;
200}
201
202/* Normalizes a pointer after an addition */
203static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
204{
205 if (ptr - buf->size >= buf->data)
206 ptr -= buf->size;
207 return ptr;
208}
209
210/* Return the maximum amount of bytes that can be written into the buffer,
211 * including reserved space which may be overwritten.
212 */
213static inline int buffer_total_space(const struct buffer *buf)
214{
215 return buf->size - buffer_len(buf);
216}
217
218/* Returns the number of contiguous bytes between <start> and <start>+<count>,
219 * and enforces a limit on buf->data + buf->size. <start> must be within the
220 * buffer.
221 */
222static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
223{
224 if (count > buf->data - start + buf->size)
225 count = buf->data - start + buf->size;
226 return count;
227}
228
229/* Return the amount of bytes that can be written into the buffer at once,
230 * including reserved space which may be overwritten.
231 */
232static inline int buffer_contig_space(const struct buffer *buf)
233{
234 const char *left, *right;
235
236 if (buf->data + buf->o <= buf->p)
237 right = buf->data + buf->size;
238 else
239 right = buf->p + buf->size - buf->o;
240
241 left = buffer_wrap_add(buf, buf->p + buf->i);
242 return right - left;
243}
244
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100245/* Returns the amount of byte that can be written starting from <p> into the
246 * input buffer at once, including reserved space which may be overwritten.
247 * This is used by Lua to insert data in the input side just before the other
248 * data using buffer_replace(). The goal is to transfer these new data in the
249 * output buffer.
250 */
251static inline int bi_space_for_replace(const struct buffer *buf)
252{
253 const char *end;
254
255 /* If the input side data overflows, we cannot insert data contiguously. */
256 if (buf->p + buf->i >= buf->data + buf->size)
257 return 0;
258
259 /* Check the last byte used in the buffer, it may be a byte of the output
260 * side if the buffer wraps, or its the end of the buffer.
261 */
262 end = buffer_wrap_sub(buf, buf->p - buf->o);
263 if (end <= buf->p)
264 end = buf->data + buf->size;
265
266 /* Compute the amount of bytes which can be written. */
267 return end - (buf->p + buf->i);
268}
269
270
Willy Tarreauc7e42382012-08-24 19:22:53 +0200271/* Normalizes a pointer which is supposed to be relative to the beginning of a
272 * buffer, so that wrapping is correctly handled. The intent is to use this
273 * when increasing a pointer. Note that the wrapping test is only performed
274 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
275 * otherwise an invalid pointer might be returned.
276 */
277static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
278{
279 if (ptr < buf->data)
280 ptr += buf->size;
281 else if (ptr - buf->size >= buf->data)
282 ptr -= buf->size;
283 return ptr;
284}
285
286/* Returns the distance between two pointers, taking into account the ability
287 * to wrap around the buffer's end.
288 */
289static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
290{
291 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200292
293 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200294 return count;
295}
296
297/* returns the amount of pending bytes in the buffer. It is the amount of bytes
298 * that is not scheduled to be sent.
299 */
300static inline int buffer_pending(const struct buffer *buf)
301{
302 return buf->i;
303}
304
305/* Returns the size of the working area which the caller knows ends at <end>.
306 * If <end> equals buf->r (modulo size), then it means that the free area which
307 * follows is part of the working area. Otherwise, the working area stops at
308 * <end>. It always starts at buf->p. The work area includes the
309 * reserved area.
310 */
311static inline int buffer_work_area(const struct buffer *buf, const char *end)
312{
313 end = buffer_pointer(buf, end);
314 if (end == buffer_wrap_add(buf, buf->p + buf->i))
315 /* pointer exactly at end, lets push forwards */
316 end = buffer_wrap_sub(buf, buf->p - buf->o);
317 return buffer_count(buf, buf->p, end);
318}
319
320/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
321static inline int buffer_almost_full(const struct buffer *buf)
322{
Willy Tarreau4428a292014-11-28 20:54:13 +0100323 if (buf == &buf_empty)
324 return 0;
325
326 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200327 return 1;
328 return 0;
329}
330
331/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
332 * call this function with remaining data waiting to be sent (o > 0). The
333 * caller must ensure that <n> is smaller than the actual buffer's length.
334 * This is mainly used to remove empty lines at the beginning of a request
335 * or a response.
336 */
337static inline void bi_fast_delete(struct buffer *buf, int n)
338{
339 buf->i -= n;
340 buf->p += n;
341}
342
343/*
344 * Tries to realign the given buffer, and returns how many bytes can be written
345 * there at once without overwriting anything.
346 */
347static inline int buffer_realign(struct buffer *buf)
348{
349 if (!(buf->i | buf->o)) {
350 /* let's realign the buffer to optimize I/O */
351 buf->p = buf->data;
352 }
353 return buffer_contig_space(buf);
354}
355
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200356/* Schedule all remaining buffer data to be sent. ->o is not touched if it
357 * already covers those data. That permits doing a flush even after a forward,
358 * although not recommended.
359 */
360static inline void buffer_flush(struct buffer *buf)
361{
362 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
363 buf->o += buf->i;
364 buf->i = 0;
365}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200366
Willy Tarreauaf819352012-08-27 22:08:00 +0200367/* This function writes the string <str> at position <pos> which must be in
368 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
369 * (l, r, lr) are updated to be valid after the shift. the shift value
370 * (positive or negative) is returned. If there's no space left, the move is
371 * not done. The function does not adjust ->o because it does not make sense
372 * to use it on data scheduled to be sent.
373 */
374static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
375{
376 return buffer_replace2(b, pos, end, str, strlen(str));
377}
378
Willy Tarreau8c89c202012-09-28 16:02:48 +0200379/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
380 * Data are truncated if buffer is full.
381 */
382static inline void bo_putchr(struct buffer *b, char c)
383{
384 if (buffer_len(b) == b->size)
385 return;
386 *b->p = c;
387 b->p = b_ptr(b, 1);
388 b->o++;
389}
390
391/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100392 * Data are truncated if buffer is too short. It returns the number of bytes
393 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200394 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100395static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200396{
397 int cur_len = buffer_len(b);
398 int half;
399
400 if (len > b->size - cur_len)
401 len = (b->size - cur_len);
402 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100403 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200404
405 half = buffer_contig_space(b);
406 if (half > len)
407 half = len;
408
409 memcpy(b->p, blk, half);
410 b->p = b_ptr(b, half);
411 if (len > half) {
412 memcpy(b->p, blk, len - half);
413 b->p = b_ptr(b, half);
414 }
415 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100416 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200417}
418
419/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100420 * Data are truncated if buffer is too short. It returns the number of bytes
421 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200422 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100423static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200424{
425 return bo_putblk(b, str, strlen(str));
426}
427
428/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100429 * Data are truncated if buffer is too short. It returns the number of bytes
430 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200431 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100432static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200433{
434 return bo_putblk(b, chk->str, chk->len);
435}
436
Willy Tarreau474cf542014-11-24 10:54:47 +0100437/* Resets a buffer. The size is not touched. */
438static inline void b_reset(struct buffer *buf)
439{
440 buf->o = 0;
441 buf->i = 0;
442 buf->p = buf->data;
443}
444
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100445/* Allocates a buffer and replaces *buf with this buffer. If no memory is
446 * available, &buf_wanted is used instead. No control is made to check if *buf
447 * already pointed to another buffer. The allocated buffer is returned, or
448 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100449 */
450static inline struct buffer *b_alloc(struct buffer **buf)
451{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100452 struct buffer *b;
453
454 *buf = &buf_wanted;
455 b = pool_alloc_dirty(pool2_buffer);
456 if (likely(b)) {
457 b->size = pool2_buffer->size - sizeof(struct buffer);
458 b_reset(b);
459 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100460 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100461 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100462}
463
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100464/* Allocates a buffer and replaces *buf with this buffer. If no memory is
465 * available, &buf_wanted is used instead. No control is made to check if *buf
466 * already pointed to another buffer. The allocated buffer is returned, or
467 * NULL in case no memory is available. The difference with b_alloc() is that
468 * this function only picks from the pool and never calls malloc(), so it can
469 * fail even if some memory is available.
470 */
471static inline struct buffer *b_alloc_fast(struct buffer **buf)
472{
473 struct buffer *b;
474
475 *buf = &buf_wanted;
476 b = pool_get_first(pool2_buffer);
477 if (likely(b)) {
478 b->size = pool2_buffer->size - sizeof(struct buffer);
479 b_reset(b);
480 *buf = b;
481 }
482 return b;
483}
484
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100485/* Releases buffer *buf (no check of emptiness) */
486static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100487{
488 pool_free2(pool2_buffer, *buf);
489}
490
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100491/* Releases buffer *buf if allocated. */
492static inline void b_drop(struct buffer **buf)
493{
494 if (!(*buf)->size)
495 return;
496 __b_drop(buf);
497}
498
499/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
500static inline void b_free(struct buffer **buf)
501{
502 b_drop(buf);
503 *buf = &buf_empty;
504}
505
Willy Tarreauf4718e82014-12-02 13:54:01 +0100506/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
507 * there are still at least <margin> buffers available in the pool after this
508 * allocation so that we don't leave the pool in a condition where a session or
509 * a response buffer could not be allocated anymore, resulting in a deadlock.
510 * This means that we sometimes need to try to allocate extra entries even if
511 * only one buffer is needed.
512 */
513static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
514{
515 struct buffer *next;
516
517 if ((*buf)->size)
518 return *buf;
519
520 /* fast path */
521 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
522 return b_alloc_fast(buf);
523
524 next = pool_refill_alloc(pool2_buffer, margin);
525 if (!next)
526 return next;
527
528 next->size = pool2_buffer->size - sizeof(struct buffer);
529 b_reset(next);
530 *buf = next;
531 return next;
532}
533
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100534
535void __offer_buffer(void *from, unsigned int threshold);
536
537static inline void offer_buffers(void *from, unsigned int threshold)
538{
539 if (LIST_ISEMPTY(&buffer_wq))
540 return;
541 __offer_buffer(from, threshold);
542}
543
Willy Tarreauc7e42382012-08-24 19:22:53 +0200544#endif /* _COMMON_BUFFER_H */
545
546/*
547 * Local variables:
548 * c-indent-level: 8
549 * c-basic-offset: 8
550 * End:
551 */