blob: f8bd26efbb4c07d2c9aa3ae3e01b271455dc7e64 [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);
Willy Tarreauc7e42382012-08-24 19:22:53 +020060
61/*****************************************************************/
62/* These functions are used to compute various buffer area sizes */
63/*****************************************************************/
64
65/* Returns an absolute pointer for a position relative to the current buffer's
66 * pointer. It is written so that it is optimal when <ofs> is a const. It is
67 * written as a macro instead of an inline function so that the compiler knows
68 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020069 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020070 */
71#define b_ptr(b, ofs) \
72 ({ \
73 char *__ret = (b)->p + (ofs); \
74 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
75 __ret -= (b)->size; \
76 else if ((ofs) < 0 && __ret < (b)->data) \
77 __ret += (b)->size; \
78 __ret; \
79 })
80
Willy Tarreaua75bcef2012-08-24 22:56:11 +020081/* Advances the buffer by <adv> bytes, which means that the buffer
82 * pointer advances, and that as many bytes from in are transferred
83 * to out. The caller is responsible for ensuring that adv is always
84 * smaller than or equal to b->i.
85 */
86static inline void b_adv(struct buffer *b, unsigned int adv)
87{
88 b->i -= adv;
89 b->o += adv;
90 b->p = b_ptr(b, adv);
91}
92
93/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
94 * backwards, and that as many bytes from out are moved to in. The caller is
95 * responsible for ensuring that adv is always smaller than or equal to b->o.
96 */
97static inline void b_rew(struct buffer *b, unsigned int adv)
98{
99 b->i += adv;
100 b->o -= adv;
101 b->p = b_ptr(b, (int)-adv);
102}
103
Willy Tarreauc7e42382012-08-24 19:22:53 +0200104/* Returns the start of the input data in a buffer */
105static inline char *bi_ptr(const struct buffer *b)
106{
107 return b->p;
108}
109
110/* Returns the end of the input data in a buffer (pointer to next
111 * insertion point).
112 */
113static inline char *bi_end(const struct buffer *b)
114{
115 char *ret = b->p + b->i;
116
117 if (ret >= b->data + b->size)
118 ret -= b->size;
119 return ret;
120}
121
122/* Returns the amount of input data that can contiguously be read at once */
123static inline int bi_contig_data(const struct buffer *b)
124{
125 int data = b->data + b->size - b->p;
126
127 if (data > b->i)
128 data = b->i;
129 return data;
130}
131
132/* Returns the start of the output data in a buffer */
133static inline char *bo_ptr(const struct buffer *b)
134{
135 char *ret = b->p - b->o;
136
137 if (ret < b->data)
138 ret += b->size;
139 return ret;
140}
141
142/* Returns the end of the output data in a buffer */
143static inline char *bo_end(const struct buffer *b)
144{
145 return b->p;
146}
147
148/* Returns the amount of output data that can contiguously be read at once */
149static inline int bo_contig_data(const struct buffer *b)
150{
151 char *beg = b->p - b->o;
152
153 if (beg < b->data)
154 return b->data - beg;
155 return b->o;
156}
157
Christopher Faulet637f8f22017-03-29 11:58:28 +0200158/* Return the amount of bytes that can be written into the input area at once
159 * including reserved space which may be overwritten (this is the caller
160 * responsibility to know if the reserved space is protected or not).
161*/
162static inline int bi_contig_space(const struct buffer *b)
163{
164 const char *left, *right;
165
Christopher Fauleta36b3112017-06-13 22:00:22 +0200166 left = b->p + b->i;
167 right = b->p - b->o;
168 if (left >= b->data + b->size)
169 left -= b->size;
170 else {
171 if (right < b->data)
172 right += b->size;
173 else
174 right = b->data + b->size;
175 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200176 return (right - left);
177}
178
179/* Return the amount of bytes that can be written into the output area at once
180 * including reserved space which may be overwritten (this is the caller
181 * responsibility to know if the reserved space is protected or not). Input data
182 * are assumed to not exist.
183*/
184static inline int bo_contig_space(const struct buffer *b)
185{
186 const char *left, *right;
187
Christopher Fauleta36b3112017-06-13 22:00:22 +0200188 left = b->p;
189 right = b->p - b->o;
190 if (right < b->data)
191 right += b->size;
192 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200193 right = b->data + b->size;
194
195 return (right - left);
196}
197
Willy Tarreauc7e42382012-08-24 19:22:53 +0200198/* Return the buffer's length in bytes by summing the input and the output */
199static inline int buffer_len(const struct buffer *buf)
200{
201 return buf->i + buf->o;
202}
203
204/* Return non-zero only if the buffer is not empty */
205static inline int buffer_not_empty(const struct buffer *buf)
206{
207 return buf->i | buf->o;
208}
209
210/* Return non-zero only if the buffer is empty */
211static inline int buffer_empty(const struct buffer *buf)
212{
213 return !buffer_not_empty(buf);
214}
215
Willy Tarreau42d06662012-08-27 19:51:36 +0200216/* Returns non-zero if the buffer's INPUT is considered full, which means that
217 * it holds at least as much INPUT data as (size - reserve). This also means
218 * that data that are scheduled for output are considered as potential free
219 * space, and that the reserved space is always considered as not usable. This
220 * information alone cannot be used as a general purpose free space indicator.
221 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100222 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200223 * generic function taking everything into account.
224 */
225static inline int buffer_full(const struct buffer *b, unsigned int reserve)
226{
Willy Tarreau4428a292014-11-28 20:54:13 +0100227 if (b == &buf_empty)
228 return 0;
229
Willy Tarreau42d06662012-08-27 19:51:36 +0200230 return (b->i + reserve >= b->size);
231}
232
Willy Tarreauc7e42382012-08-24 19:22:53 +0200233/* Normalizes a pointer after a subtract */
234static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
235{
236 if (ptr < buf->data)
237 ptr += buf->size;
238 return ptr;
239}
240
241/* Normalizes a pointer after an addition */
242static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
243{
244 if (ptr - buf->size >= buf->data)
245 ptr -= buf->size;
246 return ptr;
247}
248
249/* Return the maximum amount of bytes that can be written into the buffer,
250 * including reserved space which may be overwritten.
251 */
252static inline int buffer_total_space(const struct buffer *buf)
253{
254 return buf->size - buffer_len(buf);
255}
256
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100257/* Returns the amount of byte that can be written starting from <p> into the
258 * input buffer at once, including reserved space which may be overwritten.
259 * This is used by Lua to insert data in the input side just before the other
260 * data using buffer_replace(). The goal is to transfer these new data in the
261 * output buffer.
262 */
263static inline int bi_space_for_replace(const struct buffer *buf)
264{
265 const char *end;
266
267 /* If the input side data overflows, we cannot insert data contiguously. */
268 if (buf->p + buf->i >= buf->data + buf->size)
269 return 0;
270
271 /* Check the last byte used in the buffer, it may be a byte of the output
272 * side if the buffer wraps, or its the end of the buffer.
273 */
274 end = buffer_wrap_sub(buf, buf->p - buf->o);
275 if (end <= buf->p)
276 end = buf->data + buf->size;
277
278 /* Compute the amount of bytes which can be written. */
279 return end - (buf->p + buf->i);
280}
281
282
Willy Tarreauc7e42382012-08-24 19:22:53 +0200283/* Normalizes a pointer which is supposed to be relative to the beginning of a
284 * buffer, so that wrapping is correctly handled. The intent is to use this
285 * when increasing a pointer. Note that the wrapping test is only performed
286 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
287 * otherwise an invalid pointer might be returned.
288 */
289static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
290{
291 if (ptr < buf->data)
292 ptr += buf->size;
293 else if (ptr - buf->size >= buf->data)
294 ptr -= buf->size;
295 return ptr;
296}
297
298/* Returns the distance between two pointers, taking into account the ability
299 * to wrap around the buffer's end.
300 */
301static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
302{
303 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200304
305 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200306 return count;
307}
308
309/* returns the amount of pending bytes in the buffer. It is the amount of bytes
310 * that is not scheduled to be sent.
311 */
312static inline int buffer_pending(const struct buffer *buf)
313{
314 return buf->i;
315}
316
Willy Tarreauc7e42382012-08-24 19:22:53 +0200317/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
318static inline int buffer_almost_full(const struct buffer *buf)
319{
Willy Tarreau4428a292014-11-28 20:54:13 +0100320 if (buf == &buf_empty)
321 return 0;
322
323 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200324 return 1;
325 return 0;
326}
327
328/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
329 * call this function with remaining data waiting to be sent (o > 0). The
330 * caller must ensure that <n> is smaller than the actual buffer's length.
331 * This is mainly used to remove empty lines at the beginning of a request
332 * or a response.
333 */
334static inline void bi_fast_delete(struct buffer *buf, int n)
335{
336 buf->i -= n;
337 buf->p += n;
338}
339
Christopher Faulet637f8f22017-03-29 11:58:28 +0200340/* Tries to realign the given buffer. */
341static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200342{
343 if (!(buf->i | buf->o)) {
344 /* let's realign the buffer to optimize I/O */
345 buf->p = buf->data;
346 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200347}
348
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200349/* Schedule all remaining buffer data to be sent. ->o is not touched if it
350 * already covers those data. That permits doing a flush even after a forward,
351 * although not recommended.
352 */
353static inline void buffer_flush(struct buffer *buf)
354{
355 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
356 buf->o += buf->i;
357 buf->i = 0;
358}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200359
Willy Tarreauaf819352012-08-27 22:08:00 +0200360/* This function writes the string <str> at position <pos> which must be in
361 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
362 * (l, r, lr) are updated to be valid after the shift. the shift value
363 * (positive or negative) is returned. If there's no space left, the move is
364 * not done. The function does not adjust ->o because it does not make sense
365 * to use it on data scheduled to be sent.
366 */
367static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
368{
369 return buffer_replace2(b, pos, end, str, strlen(str));
370}
371
Willy Tarreau8c89c202012-09-28 16:02:48 +0200372/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
373 * Data are truncated if buffer is full.
374 */
375static inline void bo_putchr(struct buffer *b, char c)
376{
377 if (buffer_len(b) == b->size)
378 return;
379 *b->p = c;
380 b->p = b_ptr(b, 1);
381 b->o++;
382}
383
384/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100385 * Data are truncated if buffer is too short. It returns the number of bytes
386 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200387 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100388static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200389{
390 int cur_len = buffer_len(b);
391 int half;
392
393 if (len > b->size - cur_len)
394 len = (b->size - cur_len);
395 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100396 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200397
Christopher Faulet637f8f22017-03-29 11:58:28 +0200398 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200399 if (half > len)
400 half = len;
401
402 memcpy(b->p, blk, half);
403 b->p = b_ptr(b, half);
404 if (len > half) {
405 memcpy(b->p, blk, len - half);
406 b->p = b_ptr(b, half);
407 }
408 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100409 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200410}
411
412/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100413 * Data are truncated if buffer is too short. It returns the number of bytes
414 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200415 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100416static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200417{
418 return bo_putblk(b, str, strlen(str));
419}
420
421/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100422 * Data are truncated if buffer is too short. It returns the number of bytes
423 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200424 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100425static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200426{
427 return bo_putblk(b, chk->str, chk->len);
428}
429
Willy Tarreau474cf542014-11-24 10:54:47 +0100430/* Resets a buffer. The size is not touched. */
431static inline void b_reset(struct buffer *buf)
432{
433 buf->o = 0;
434 buf->i = 0;
435 buf->p = buf->data;
436}
437
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100438/* Allocates a buffer and replaces *buf with this buffer. If no memory is
439 * available, &buf_wanted is used instead. No control is made to check if *buf
440 * already pointed to another buffer. The allocated buffer is returned, or
441 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100442 */
443static inline struct buffer *b_alloc(struct buffer **buf)
444{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100445 struct buffer *b;
446
447 *buf = &buf_wanted;
448 b = pool_alloc_dirty(pool2_buffer);
449 if (likely(b)) {
450 b->size = pool2_buffer->size - sizeof(struct buffer);
451 b_reset(b);
452 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100453 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100454 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100455}
456
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100457/* Allocates a buffer and replaces *buf with this buffer. If no memory is
458 * available, &buf_wanted is used instead. No control is made to check if *buf
459 * already pointed to another buffer. The allocated buffer is returned, or
460 * NULL in case no memory is available. The difference with b_alloc() is that
461 * this function only picks from the pool and never calls malloc(), so it can
462 * fail even if some memory is available.
463 */
464static inline struct buffer *b_alloc_fast(struct buffer **buf)
465{
466 struct buffer *b;
467
468 *buf = &buf_wanted;
469 b = pool_get_first(pool2_buffer);
470 if (likely(b)) {
471 b->size = pool2_buffer->size - sizeof(struct buffer);
472 b_reset(b);
473 *buf = b;
474 }
475 return b;
476}
477
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100478/* Releases buffer *buf (no check of emptiness) */
479static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100480{
481 pool_free2(pool2_buffer, *buf);
482}
483
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100484/* Releases buffer *buf if allocated. */
485static inline void b_drop(struct buffer **buf)
486{
487 if (!(*buf)->size)
488 return;
489 __b_drop(buf);
490}
491
492/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
493static inline void b_free(struct buffer **buf)
494{
495 b_drop(buf);
496 *buf = &buf_empty;
497}
498
Willy Tarreauf4718e82014-12-02 13:54:01 +0100499/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
500 * there are still at least <margin> buffers available in the pool after this
501 * allocation so that we don't leave the pool in a condition where a session or
502 * a response buffer could not be allocated anymore, resulting in a deadlock.
503 * This means that we sometimes need to try to allocate extra entries even if
504 * only one buffer is needed.
505 */
506static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
507{
508 struct buffer *next;
509
510 if ((*buf)->size)
511 return *buf;
512
513 /* fast path */
514 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
515 return b_alloc_fast(buf);
516
517 next = pool_refill_alloc(pool2_buffer, margin);
518 if (!next)
519 return next;
520
521 next->size = pool2_buffer->size - sizeof(struct buffer);
522 b_reset(next);
523 *buf = next;
524 return next;
525}
526
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100527
528void __offer_buffer(void *from, unsigned int threshold);
529
530static inline void offer_buffers(void *from, unsigned int threshold)
531{
532 if (LIST_ISEMPTY(&buffer_wq))
533 return;
534 __offer_buffer(from, threshold);
535}
536
Willy Tarreauc7e42382012-08-24 19:22:53 +0200537#endif /* _COMMON_BUFFER_H */
538
539/*
540 * Local variables:
541 * c-indent-level: 8
542 * c-basic-offset: 8
543 * End:
544 */