blob: 040a26e2f08ea9c8a8780b78c88eba3917c7a3c1 [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
166 left = bi_end(b);
167 right = bo_ptr(b);
168
169 if (left >= right)
170 right = b->data + b->size;
171
172 return (right - left);
173}
174
175/* Return the amount of bytes that can be written into the output area at once
176 * including reserved space which may be overwritten (this is the caller
177 * responsibility to know if the reserved space is protected or not). Input data
178 * are assumed to not exist.
179*/
180static inline int bo_contig_space(const struct buffer *b)
181{
182 const char *left, *right;
183
184 left = bo_end(b);
185 right = bo_ptr(b);
186
187 if (left >= right)
188 right = b->data + b->size;
189
190 return (right - left);
191}
192
Willy Tarreauc7e42382012-08-24 19:22:53 +0200193/* Return the buffer's length in bytes by summing the input and the output */
194static inline int buffer_len(const struct buffer *buf)
195{
196 return buf->i + buf->o;
197}
198
199/* Return non-zero only if the buffer is not empty */
200static inline int buffer_not_empty(const struct buffer *buf)
201{
202 return buf->i | buf->o;
203}
204
205/* Return non-zero only if the buffer is empty */
206static inline int buffer_empty(const struct buffer *buf)
207{
208 return !buffer_not_empty(buf);
209}
210
Willy Tarreau42d06662012-08-27 19:51:36 +0200211/* Returns non-zero if the buffer's INPUT is considered full, which means that
212 * it holds at least as much INPUT data as (size - reserve). This also means
213 * that data that are scheduled for output are considered as potential free
214 * space, and that the reserved space is always considered as not usable. This
215 * information alone cannot be used as a general purpose free space indicator.
216 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100217 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200218 * generic function taking everything into account.
219 */
220static inline int buffer_full(const struct buffer *b, unsigned int reserve)
221{
Willy Tarreau4428a292014-11-28 20:54:13 +0100222 if (b == &buf_empty)
223 return 0;
224
Willy Tarreau42d06662012-08-27 19:51:36 +0200225 return (b->i + reserve >= b->size);
226}
227
Willy Tarreauc7e42382012-08-24 19:22:53 +0200228/* Normalizes a pointer after a subtract */
229static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
230{
231 if (ptr < buf->data)
232 ptr += buf->size;
233 return ptr;
234}
235
236/* Normalizes a pointer after an addition */
237static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
238{
239 if (ptr - buf->size >= buf->data)
240 ptr -= buf->size;
241 return ptr;
242}
243
244/* Return the maximum amount of bytes that can be written into the buffer,
245 * including reserved space which may be overwritten.
246 */
247static inline int buffer_total_space(const struct buffer *buf)
248{
249 return buf->size - buffer_len(buf);
250}
251
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100252/* Returns the amount of byte that can be written starting from <p> into the
253 * input buffer at once, including reserved space which may be overwritten.
254 * This is used by Lua to insert data in the input side just before the other
255 * data using buffer_replace(). The goal is to transfer these new data in the
256 * output buffer.
257 */
258static inline int bi_space_for_replace(const struct buffer *buf)
259{
260 const char *end;
261
262 /* If the input side data overflows, we cannot insert data contiguously. */
263 if (buf->p + buf->i >= buf->data + buf->size)
264 return 0;
265
266 /* Check the last byte used in the buffer, it may be a byte of the output
267 * side if the buffer wraps, or its the end of the buffer.
268 */
269 end = buffer_wrap_sub(buf, buf->p - buf->o);
270 if (end <= buf->p)
271 end = buf->data + buf->size;
272
273 /* Compute the amount of bytes which can be written. */
274 return end - (buf->p + buf->i);
275}
276
277
Willy Tarreauc7e42382012-08-24 19:22:53 +0200278/* Normalizes a pointer which is supposed to be relative to the beginning of a
279 * buffer, so that wrapping is correctly handled. The intent is to use this
280 * when increasing a pointer. Note that the wrapping test is only performed
281 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
282 * otherwise an invalid pointer might be returned.
283 */
284static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
285{
286 if (ptr < buf->data)
287 ptr += buf->size;
288 else if (ptr - buf->size >= buf->data)
289 ptr -= buf->size;
290 return ptr;
291}
292
293/* Returns the distance between two pointers, taking into account the ability
294 * to wrap around the buffer's end.
295 */
296static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
297{
298 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200299
300 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200301 return count;
302}
303
304/* returns the amount of pending bytes in the buffer. It is the amount of bytes
305 * that is not scheduled to be sent.
306 */
307static inline int buffer_pending(const struct buffer *buf)
308{
309 return buf->i;
310}
311
Willy Tarreauc7e42382012-08-24 19:22:53 +0200312/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
313static inline int buffer_almost_full(const struct buffer *buf)
314{
Willy Tarreau4428a292014-11-28 20:54:13 +0100315 if (buf == &buf_empty)
316 return 0;
317
318 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200319 return 1;
320 return 0;
321}
322
323/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
324 * call this function with remaining data waiting to be sent (o > 0). The
325 * caller must ensure that <n> is smaller than the actual buffer's length.
326 * This is mainly used to remove empty lines at the beginning of a request
327 * or a response.
328 */
329static inline void bi_fast_delete(struct buffer *buf, int n)
330{
331 buf->i -= n;
332 buf->p += n;
333}
334
Christopher Faulet637f8f22017-03-29 11:58:28 +0200335/* Tries to realign the given buffer. */
336static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200337{
338 if (!(buf->i | buf->o)) {
339 /* let's realign the buffer to optimize I/O */
340 buf->p = buf->data;
341 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200342}
343
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200344/* Schedule all remaining buffer data to be sent. ->o is not touched if it
345 * already covers those data. That permits doing a flush even after a forward,
346 * although not recommended.
347 */
348static inline void buffer_flush(struct buffer *buf)
349{
350 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
351 buf->o += buf->i;
352 buf->i = 0;
353}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200354
Willy Tarreauaf819352012-08-27 22:08:00 +0200355/* This function writes the string <str> at position <pos> which must be in
356 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
357 * (l, r, lr) are updated to be valid after the shift. the shift value
358 * (positive or negative) is returned. If there's no space left, the move is
359 * not done. The function does not adjust ->o because it does not make sense
360 * to use it on data scheduled to be sent.
361 */
362static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
363{
364 return buffer_replace2(b, pos, end, str, strlen(str));
365}
366
Willy Tarreau8c89c202012-09-28 16:02:48 +0200367/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
368 * Data are truncated if buffer is full.
369 */
370static inline void bo_putchr(struct buffer *b, char c)
371{
372 if (buffer_len(b) == b->size)
373 return;
374 *b->p = c;
375 b->p = b_ptr(b, 1);
376 b->o++;
377}
378
379/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100380 * Data are truncated if buffer is too short. It returns the number of bytes
381 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200382 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100383static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200384{
385 int cur_len = buffer_len(b);
386 int half;
387
388 if (len > b->size - cur_len)
389 len = (b->size - cur_len);
390 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100391 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200392
Christopher Faulet637f8f22017-03-29 11:58:28 +0200393 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200394 if (half > len)
395 half = len;
396
397 memcpy(b->p, blk, half);
398 b->p = b_ptr(b, half);
399 if (len > half) {
400 memcpy(b->p, blk, len - half);
401 b->p = b_ptr(b, half);
402 }
403 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100404 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200405}
406
407/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100408 * Data are truncated if buffer is too short. It returns the number of bytes
409 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200410 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100411static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200412{
413 return bo_putblk(b, str, strlen(str));
414}
415
416/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100417 * Data are truncated if buffer is too short. It returns the number of bytes
418 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200419 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100420static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200421{
422 return bo_putblk(b, chk->str, chk->len);
423}
424
Willy Tarreau474cf542014-11-24 10:54:47 +0100425/* Resets a buffer. The size is not touched. */
426static inline void b_reset(struct buffer *buf)
427{
428 buf->o = 0;
429 buf->i = 0;
430 buf->p = buf->data;
431}
432
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100433/* Allocates a buffer and replaces *buf with this buffer. If no memory is
434 * available, &buf_wanted is used instead. No control is made to check if *buf
435 * already pointed to another buffer. The allocated buffer is returned, or
436 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100437 */
438static inline struct buffer *b_alloc(struct buffer **buf)
439{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100440 struct buffer *b;
441
442 *buf = &buf_wanted;
443 b = pool_alloc_dirty(pool2_buffer);
444 if (likely(b)) {
445 b->size = pool2_buffer->size - sizeof(struct buffer);
446 b_reset(b);
447 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100448 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100449 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100450}
451
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100452/* Allocates a buffer and replaces *buf with this buffer. If no memory is
453 * available, &buf_wanted is used instead. No control is made to check if *buf
454 * already pointed to another buffer. The allocated buffer is returned, or
455 * NULL in case no memory is available. The difference with b_alloc() is that
456 * this function only picks from the pool and never calls malloc(), so it can
457 * fail even if some memory is available.
458 */
459static inline struct buffer *b_alloc_fast(struct buffer **buf)
460{
461 struct buffer *b;
462
463 *buf = &buf_wanted;
464 b = pool_get_first(pool2_buffer);
465 if (likely(b)) {
466 b->size = pool2_buffer->size - sizeof(struct buffer);
467 b_reset(b);
468 *buf = b;
469 }
470 return b;
471}
472
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100473/* Releases buffer *buf (no check of emptiness) */
474static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100475{
476 pool_free2(pool2_buffer, *buf);
477}
478
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100479/* Releases buffer *buf if allocated. */
480static inline void b_drop(struct buffer **buf)
481{
482 if (!(*buf)->size)
483 return;
484 __b_drop(buf);
485}
486
487/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
488static inline void b_free(struct buffer **buf)
489{
490 b_drop(buf);
491 *buf = &buf_empty;
492}
493
Willy Tarreauf4718e82014-12-02 13:54:01 +0100494/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
495 * there are still at least <margin> buffers available in the pool after this
496 * allocation so that we don't leave the pool in a condition where a session or
497 * a response buffer could not be allocated anymore, resulting in a deadlock.
498 * This means that we sometimes need to try to allocate extra entries even if
499 * only one buffer is needed.
500 */
501static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
502{
503 struct buffer *next;
504
505 if ((*buf)->size)
506 return *buf;
507
508 /* fast path */
509 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
510 return b_alloc_fast(buf);
511
512 next = pool_refill_alloc(pool2_buffer, margin);
513 if (!next)
514 return next;
515
516 next->size = pool2_buffer->size - sizeof(struct buffer);
517 b_reset(next);
518 *buf = next;
519 return next;
520}
521
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100522
523void __offer_buffer(void *from, unsigned int threshold);
524
525static inline void offer_buffers(void *from, unsigned int threshold)
526{
527 if (LIST_ISEMPTY(&buffer_wq))
528 return;
529 __offer_buffer(from, threshold);
530}
531
Willy Tarreauc7e42382012-08-24 19:22:53 +0200532#endif /* _COMMON_BUFFER_H */
533
534/*
535 * Local variables:
536 * c-indent-level: 8
537 * c-basic-offset: 8
538 * End:
539 */