blob: c34e3ac8c74605694a0539dfe5a02d2a85b037e4 [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 Tarreau6634b632017-09-22 15:02:54 +020031#include <common/ist.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020032#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020033
34
35struct buffer {
36 char *p; /* buffer's start pointer, separates in and out data */
37 unsigned int size; /* buffer size in bytes */
38 unsigned int i; /* number of input bytes pending for analysis in the buffer */
39 unsigned int o; /* number of out bytes the sender can consume from this buffer */
40 char data[0]; /* <size> bytes */
41};
42
Christopher Fauleta73e59b2016-12-09 17:30:18 +010043/* an element of the <buffer_wq> list. It represents an object that need to
44 * acquire a buffer to continue its process. */
45struct buffer_wait {
46 void *target; /* The waiting object that should be woken up */
47 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
48 struct list list; /* Next element in the <buffer_wq> list */
49};
50
Willy Tarreau9b28e032012-10-12 23:49:43 +020051extern struct pool_head *pool2_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010052extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010053extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010054extern struct list buffer_wq;
Willy Tarreauc7e42382012-08-24 19:22:53 +020055
Willy Tarreau9b28e032012-10-12 23:49:43 +020056int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020057void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020058int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
59int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020060void buffer_dump(FILE *o, struct buffer *b, int from, int to);
61void buffer_slow_realign(struct buffer *buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +020062
63/*****************************************************************/
64/* These functions are used to compute various buffer area sizes */
65/*****************************************************************/
66
67/* Returns an absolute pointer for a position relative to the current buffer's
68 * pointer. It is written so that it is optimal when <ofs> is a const. It is
69 * written as a macro instead of an inline function so that the compiler knows
70 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020071 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020072 */
73#define b_ptr(b, ofs) \
74 ({ \
75 char *__ret = (b)->p + (ofs); \
76 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
77 __ret -= (b)->size; \
78 else if ((ofs) < 0 && __ret < (b)->data) \
79 __ret += (b)->size; \
80 __ret; \
81 })
82
Willy Tarreau26488ad2017-09-19 21:14:08 +020083/* Returns the pointer to the buffer's end (data+size) */
84static inline const char *b_end(const struct buffer *b)
85{
86 return b->data + b->size;
87}
88
89/* Returns the distance between <p> and the buffer's end (data+size) */
90static inline unsigned int b_to_end(const struct buffer *b)
91{
92 return b->data + b->size - b->p;
93}
94
Willy Tarreau4a6425d2017-09-19 14:18:46 +020095/* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i>
96 * shrinks by <del> as well, and <o> is left untouched (supposed to be zero).
97 * The caller is responsible for ensuring that <del> is always smaller than or
98 * equal to b->i.
99 */
Willy Tarreau7f564d22017-10-18 08:32:12 +0200100static inline void bi_del(struct buffer *b, unsigned int del)
Willy Tarreau4a6425d2017-09-19 14:18:46 +0200101{
102 b->i -= del;
103 b->p = b_ptr(b, del);
104}
105
Willy Tarreau7f564d22017-10-18 08:32:12 +0200106/* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>.
107 * The caller is responsible for ensuring that <del> is always smaller than or
108 * equal to b->o.
109 */
110static inline void bo_del(struct buffer *b, unsigned int del)
111{
112 b->o -= del;
113}
114
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200115/* Advances the buffer by <adv> bytes, which means that the buffer
116 * pointer advances, and that as many bytes from in are transferred
117 * to out. The caller is responsible for ensuring that adv is always
118 * smaller than or equal to b->i.
119 */
120static inline void b_adv(struct buffer *b, unsigned int adv)
121{
122 b->i -= adv;
123 b->o += adv;
124 b->p = b_ptr(b, adv);
125}
126
127/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
128 * backwards, and that as many bytes from out are moved to in. The caller is
129 * responsible for ensuring that adv is always smaller than or equal to b->o.
130 */
131static inline void b_rew(struct buffer *b, unsigned int adv)
132{
133 b->i += adv;
134 b->o -= adv;
135 b->p = b_ptr(b, (int)-adv);
136}
137
Willy Tarreauc7e42382012-08-24 19:22:53 +0200138/* Returns the start of the input data in a buffer */
139static inline char *bi_ptr(const struct buffer *b)
140{
141 return b->p;
142}
143
144/* Returns the end of the input data in a buffer (pointer to next
145 * insertion point).
146 */
147static inline char *bi_end(const struct buffer *b)
148{
149 char *ret = b->p + b->i;
150
151 if (ret >= b->data + b->size)
152 ret -= b->size;
153 return ret;
154}
155
156/* Returns the amount of input data that can contiguously be read at once */
157static inline int bi_contig_data(const struct buffer *b)
158{
159 int data = b->data + b->size - b->p;
160
161 if (data > b->i)
162 data = b->i;
163 return data;
164}
165
166/* Returns the start of the output data in a buffer */
167static inline char *bo_ptr(const struct buffer *b)
168{
169 char *ret = b->p - b->o;
170
171 if (ret < b->data)
172 ret += b->size;
173 return ret;
174}
175
176/* Returns the end of the output data in a buffer */
177static inline char *bo_end(const struct buffer *b)
178{
179 return b->p;
180}
181
182/* Returns the amount of output data that can contiguously be read at once */
183static inline int bo_contig_data(const struct buffer *b)
184{
185 char *beg = b->p - b->o;
186
187 if (beg < b->data)
188 return b->data - beg;
189 return b->o;
190}
191
Christopher Faulet637f8f22017-03-29 11:58:28 +0200192/* Return the amount of bytes that can be written into the input area at once
193 * including reserved space which may be overwritten (this is the caller
194 * responsibility to know if the reserved space is protected or not).
195*/
196static inline int bi_contig_space(const struct buffer *b)
197{
198 const char *left, *right;
199
Christopher Fauleta36b3112017-06-13 22:00:22 +0200200 left = b->p + b->i;
201 right = b->p - b->o;
202 if (left >= b->data + b->size)
203 left -= b->size;
204 else {
205 if (right < b->data)
206 right += b->size;
207 else
208 right = b->data + b->size;
209 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200210 return (right - left);
211}
212
213/* Return the amount of bytes that can be written into the output area at once
214 * including reserved space which may be overwritten (this is the caller
215 * responsibility to know if the reserved space is protected or not). Input data
216 * are assumed to not exist.
217*/
218static inline int bo_contig_space(const struct buffer *b)
219{
220 const char *left, *right;
221
Christopher Fauleta36b3112017-06-13 22:00:22 +0200222 left = b->p;
223 right = b->p - b->o;
224 if (right < b->data)
225 right += b->size;
226 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200227 right = b->data + b->size;
228
229 return (right - left);
230}
231
Willy Tarreauc7e42382012-08-24 19:22:53 +0200232/* Return the buffer's length in bytes by summing the input and the output */
233static inline int buffer_len(const struct buffer *buf)
234{
235 return buf->i + buf->o;
236}
237
238/* Return non-zero only if the buffer is not empty */
239static inline int buffer_not_empty(const struct buffer *buf)
240{
241 return buf->i | buf->o;
242}
243
244/* Return non-zero only if the buffer is empty */
245static inline int buffer_empty(const struct buffer *buf)
246{
247 return !buffer_not_empty(buf);
248}
249
Willy Tarreau5b9834f2017-10-16 14:01:18 +0200250/* Return non-zero only if the buffer's free space wraps :
251 * [ |oooo| ] => yes
252 * [ |iiii| ] => yes
253 * [ |oooo|iiii| ] => yes
254 * [oooo| ] => no
255 * [ |oooo] => no
256 * [iiii| ] => no
257 * [ |iiii] => no
258 * [oooo|iiii| ] => no
259 * [ |oooo|iiii] => no
260 * [iiii| |oooo] => no
261 * [oo|iiii| |oo] => no
262 * [iiii| |oo|ii] => no
263 * [oooooooooo|iiiiiiiiiii] => no
264 * [iiiiiiiiiiiii|oooooooo] => no
265 *
266 * So the only case where the buffer does not wrap is when there's data either
267 * at the beginning or at the end of the buffer. Thus we have this :
268 * - if (p+i >= size) ==> doesn't wrap
269 * - if (p-data <= o) ==> doesn't wrap
270 * - otherwise wraps
271 */
272static inline int buffer_space_wraps(const struct buffer *buf)
273{
274 if (buf->p + buf->i >= buf->data + buf->size)
275 return 0;
276 if (buf->p <= buf->data + buf->o)
277 return 0;
278 return 1;
279}
280
Willy Tarreau42d06662012-08-27 19:51:36 +0200281/* Returns non-zero if the buffer's INPUT is considered full, which means that
282 * it holds at least as much INPUT data as (size - reserve). This also means
283 * that data that are scheduled for output are considered as potential free
284 * space, and that the reserved space is always considered as not usable. This
285 * information alone cannot be used as a general purpose free space indicator.
286 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100287 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200288 * generic function taking everything into account.
289 */
290static inline int buffer_full(const struct buffer *b, unsigned int reserve)
291{
Willy Tarreau4428a292014-11-28 20:54:13 +0100292 if (b == &buf_empty)
293 return 0;
294
Willy Tarreau42d06662012-08-27 19:51:36 +0200295 return (b->i + reserve >= b->size);
296}
297
Willy Tarreauc7e42382012-08-24 19:22:53 +0200298/* Normalizes a pointer after a subtract */
299static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
300{
301 if (ptr < buf->data)
302 ptr += buf->size;
303 return ptr;
304}
305
306/* Normalizes a pointer after an addition */
307static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
308{
309 if (ptr - buf->size >= buf->data)
310 ptr -= buf->size;
311 return ptr;
312}
313
314/* Return the maximum amount of bytes that can be written into the buffer,
315 * including reserved space which may be overwritten.
316 */
317static inline int buffer_total_space(const struct buffer *buf)
318{
319 return buf->size - buffer_len(buf);
320}
321
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100322/* Returns the amount of byte that can be written starting from <p> into the
323 * input buffer at once, including reserved space which may be overwritten.
324 * This is used by Lua to insert data in the input side just before the other
325 * data using buffer_replace(). The goal is to transfer these new data in the
326 * output buffer.
327 */
328static inline int bi_space_for_replace(const struct buffer *buf)
329{
330 const char *end;
331
332 /* If the input side data overflows, we cannot insert data contiguously. */
333 if (buf->p + buf->i >= buf->data + buf->size)
334 return 0;
335
336 /* Check the last byte used in the buffer, it may be a byte of the output
337 * side if the buffer wraps, or its the end of the buffer.
338 */
339 end = buffer_wrap_sub(buf, buf->p - buf->o);
340 if (end <= buf->p)
341 end = buf->data + buf->size;
342
343 /* Compute the amount of bytes which can be written. */
344 return end - (buf->p + buf->i);
345}
346
347
Willy Tarreauc7e42382012-08-24 19:22:53 +0200348/* Normalizes a pointer which is supposed to be relative to the beginning of a
349 * buffer, so that wrapping is correctly handled. The intent is to use this
350 * when increasing a pointer. Note that the wrapping test is only performed
351 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
352 * otherwise an invalid pointer might be returned.
353 */
354static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
355{
356 if (ptr < buf->data)
357 ptr += buf->size;
358 else if (ptr - buf->size >= buf->data)
359 ptr -= buf->size;
360 return ptr;
361}
362
363/* Returns the distance between two pointers, taking into account the ability
364 * to wrap around the buffer's end.
365 */
366static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
367{
368 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200369
370 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200371 return count;
372}
373
374/* returns the amount of pending bytes in the buffer. It is the amount of bytes
375 * that is not scheduled to be sent.
376 */
377static inline int buffer_pending(const struct buffer *buf)
378{
379 return buf->i;
380}
381
Willy Tarreauc7e42382012-08-24 19:22:53 +0200382/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
383static inline int buffer_almost_full(const struct buffer *buf)
384{
Willy Tarreau4428a292014-11-28 20:54:13 +0100385 if (buf == &buf_empty)
386 return 0;
387
388 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200389 return 1;
390 return 0;
391}
392
393/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
394 * call this function with remaining data waiting to be sent (o > 0). The
395 * caller must ensure that <n> is smaller than the actual buffer's length.
396 * This is mainly used to remove empty lines at the beginning of a request
397 * or a response.
398 */
399static inline void bi_fast_delete(struct buffer *buf, int n)
400{
401 buf->i -= n;
402 buf->p += n;
403}
404
Christopher Faulet637f8f22017-03-29 11:58:28 +0200405/* Tries to realign the given buffer. */
406static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200407{
408 if (!(buf->i | buf->o)) {
409 /* let's realign the buffer to optimize I/O */
410 buf->p = buf->data;
411 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200412}
413
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200414/* Schedule all remaining buffer data to be sent. ->o is not touched if it
415 * already covers those data. That permits doing a flush even after a forward,
416 * although not recommended.
417 */
418static inline void buffer_flush(struct buffer *buf)
419{
420 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
421 buf->o += buf->i;
422 buf->i = 0;
423}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200424
Willy Tarreauaf819352012-08-27 22:08:00 +0200425/* This function writes the string <str> at position <pos> which must be in
426 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
427 * (l, r, lr) are updated to be valid after the shift. the shift value
428 * (positive or negative) is returned. If there's no space left, the move is
429 * not done. The function does not adjust ->o because it does not make sense
430 * to use it on data scheduled to be sent.
431 */
432static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
433{
434 return buffer_replace2(b, pos, end, str, strlen(str));
435}
436
Willy Tarreau8c89c202012-09-28 16:02:48 +0200437/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
438 * Data are truncated if buffer is full.
439 */
440static inline void bo_putchr(struct buffer *b, char c)
441{
442 if (buffer_len(b) == b->size)
443 return;
444 *b->p = c;
445 b->p = b_ptr(b, 1);
446 b->o++;
447}
448
449/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100450 * Data are truncated if buffer is too short. It returns the number of bytes
451 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200452 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100453static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200454{
455 int cur_len = buffer_len(b);
456 int half;
457
458 if (len > b->size - cur_len)
459 len = (b->size - cur_len);
460 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100461 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200462
Christopher Faulet637f8f22017-03-29 11:58:28 +0200463 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200464 if (half > len)
465 half = len;
466
467 memcpy(b->p, blk, half);
468 b->p = b_ptr(b, half);
469 if (len > half) {
470 memcpy(b->p, blk, len - half);
471 b->p = b_ptr(b, half);
472 }
473 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100474 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200475}
476
477/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100478 * Data are truncated if buffer is too short. It returns the number of bytes
479 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200480 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100481static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200482{
483 return bo_putblk(b, str, strlen(str));
484}
485
486/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100487 * Data are truncated if buffer is too short. It returns the number of bytes
488 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200489 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100490static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200491{
492 return bo_putblk(b, chk->str, chk->len);
493}
494
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200495/* Gets one full block of data at once from a buffer's output, optionally
496 * starting at a specific offset. Return values :
497 * >0 : number of bytes read, equal to requested size.
498 * =0 : not enough data available. <blk> is left undefined.
499 * The buffer is left unaffected.
500 */
501static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
502{
503 int firstblock;
504
505 if (len + offset > buf->o)
506 return 0;
507
508 firstblock = buf->data + buf->size - bo_ptr(buf);
509 if (firstblock > offset) {
510 if (firstblock >= len + offset) {
511 memcpy(blk, bo_ptr(buf) + offset, len);
512 return len;
513 }
514
515 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
516 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
517 return len;
518 }
519
520 memcpy(blk, buf->data + offset - firstblock, len);
521 return len;
522}
523
524/* Gets one or two blocks of data at once from a buffer's output.
525 * Return values :
526 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
527 * =0 : not enough data available. <blk*> are left undefined.
528 * The buffer is left unaffected. Unused buffers are left in an undefined state.
529 */
530static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
531{
532 if (unlikely(buf->o == 0))
533 return 0;
534
Willy Tarreau0621da52017-10-20 18:21:49 +0200535 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200536 *blk1 = buf->p - buf->o + buf->size;
537 *len1 = buf->data + buf->size - *blk1;
538 *blk2 = buf->data;
539 *len2 = buf->p - buf->data;
540 return 2;
541 }
542
543 *blk1 = buf->p - buf->o;
544 *len1 = buf->o;
545 return 1;
546}
547
Willy Tarreau145746c2017-10-26 15:26:17 +0200548/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
549 * Data are truncated if buffer is full.
550 */
551static inline void bi_putchr(struct buffer *b, char c)
552{
553 if (buffer_len(b) == b->size)
554 return;
555 *bi_end(b) = c;
556 b->i++;
557}
558
559/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
560 * Data are truncated if buffer is too short. It returns the number of bytes
561 * copied.
562 */
563static inline int bi_putblk(struct buffer *b, const char *blk, int len)
564{
565 int cur_len = buffer_len(b);
566 int half;
567
568 if (len > b->size - cur_len)
569 len = (b->size - cur_len);
570 if (!len)
571 return 0;
572
573 half = bi_contig_space(b);
574 if (half > len)
575 half = len;
576
577 memcpy(bi_end(b), blk, half);
578 if (len > half)
579 memcpy(b_ptr(b, b->i + half), blk, len - half);
580 b->i += len;
581 return len;
582}
583
584/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
585 * Data are truncated if buffer is too short. It returns the number of bytes
586 * copied.
587 */
588static inline int bi_putstr(struct buffer *b, const char *str)
589{
590 return bi_putblk(b, str, strlen(str));
591}
592
593/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
594 * Data are truncated if buffer is too short. It returns the number of bytes
595 * copied.
596 */
597static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
598{
599 return bi_putblk(b, chk->str, chk->len);
600}
601
602/* Gets one full block of data at once from a buffer's input. Return values :
603 * >0 : number of bytes read, equal to requested size.
604 * =0 : not enough data available. <blk> is left undefined.
605 * The buffer is left unaffected.
606 */
607static inline int bi_getblk(const struct buffer *buf, char *blk, int len)
608{
609 int firstblock;
610
611 if (len > buf->i)
612 return 0;
613
614 firstblock = bi_contig_data(buf);
615 if (firstblock > len)
616 firstblock = len;
617
618 memcpy(blk, bi_ptr(buf), firstblock);
619 if (len > firstblock)
620 memcpy(blk + firstblock, buf->data, len - firstblock);
621 return len;
622}
623
624/* Gets one or two blocks of data at once from a buffer's input.
625 * Return values :
626 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
627 * =0 : not enough data available. <blk*> are left undefined.
628 * The buffer is left unaffected. Unused buffers are left in an undefined state.
629 */
630static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
631{
632 if (unlikely(buf->i == 0))
633 return 0;
634
635 if (unlikely(buf->p + buf->i > buf->data + buf->size)) {
636 *blk1 = buf->p;
637 *len1 = buf->data + buf->size - buf->p;
638 *blk2 = buf->data;
639 *len2 = buf->i - *len1;
640 return 2;
641 }
642
643 *blk1 = buf->p;
644 *len1 = buf->i;
645 return 1;
646}
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200647
Willy Tarreau474cf542014-11-24 10:54:47 +0100648/* Resets a buffer. The size is not touched. */
649static inline void b_reset(struct buffer *buf)
650{
651 buf->o = 0;
652 buf->i = 0;
653 buf->p = buf->data;
654}
655
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100656/* Allocates a buffer and replaces *buf with this buffer. If no memory is
657 * available, &buf_wanted is used instead. No control is made to check if *buf
658 * already pointed to another buffer. The allocated buffer is returned, or
659 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100660 */
661static inline struct buffer *b_alloc(struct buffer **buf)
662{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100663 struct buffer *b;
664
665 *buf = &buf_wanted;
666 b = pool_alloc_dirty(pool2_buffer);
667 if (likely(b)) {
668 b->size = pool2_buffer->size - sizeof(struct buffer);
669 b_reset(b);
670 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100671 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100672 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100673}
674
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100675/* Allocates a buffer and replaces *buf with this buffer. If no memory is
676 * available, &buf_wanted is used instead. No control is made to check if *buf
677 * already pointed to another buffer. The allocated buffer is returned, or
678 * NULL in case no memory is available. The difference with b_alloc() is that
679 * this function only picks from the pool and never calls malloc(), so it can
680 * fail even if some memory is available.
681 */
682static inline struct buffer *b_alloc_fast(struct buffer **buf)
683{
684 struct buffer *b;
685
686 *buf = &buf_wanted;
687 b = pool_get_first(pool2_buffer);
688 if (likely(b)) {
689 b->size = pool2_buffer->size - sizeof(struct buffer);
690 b_reset(b);
691 *buf = b;
692 }
693 return b;
694}
695
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100696/* Releases buffer *buf (no check of emptiness) */
697static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100698{
699 pool_free2(pool2_buffer, *buf);
700}
701
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100702/* Releases buffer *buf if allocated. */
703static inline void b_drop(struct buffer **buf)
704{
705 if (!(*buf)->size)
706 return;
707 __b_drop(buf);
708}
709
710/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
711static inline void b_free(struct buffer **buf)
712{
713 b_drop(buf);
714 *buf = &buf_empty;
715}
716
Willy Tarreauf4718e82014-12-02 13:54:01 +0100717/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
718 * there are still at least <margin> buffers available in the pool after this
719 * allocation so that we don't leave the pool in a condition where a session or
720 * a response buffer could not be allocated anymore, resulting in a deadlock.
721 * This means that we sometimes need to try to allocate extra entries even if
722 * only one buffer is needed.
723 */
724static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
725{
726 struct buffer *next;
727
728 if ((*buf)->size)
729 return *buf;
730
731 /* fast path */
732 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
733 return b_alloc_fast(buf);
734
735 next = pool_refill_alloc(pool2_buffer, margin);
736 if (!next)
737 return next;
738
739 next->size = pool2_buffer->size - sizeof(struct buffer);
740 b_reset(next);
741 *buf = next;
742 return next;
743}
744
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100745
746void __offer_buffer(void *from, unsigned int threshold);
747
748static inline void offer_buffers(void *from, unsigned int threshold)
749{
750 if (LIST_ISEMPTY(&buffer_wq))
751 return;
752 __offer_buffer(from, threshold);
753}
754
Willy Tarreau6634b632017-09-22 15:02:54 +0200755/*************************************************************************/
756/* functions used to manipulate strings and blocks with wrapping buffers */
757/*************************************************************************/
758
759/* returns > 0 if the first <n> characters of buffer <b> starting at
760 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
761 * designed to be use with reasonably small strings (ie matches a single byte
762 * per iteration). This function is usable both with input and output data. To
763 * be used like this depending on what to match :
764 * - input contents : b_isteq(b, 0, b->i, ist);
765 * - output contents : b_isteq(b, -b->o, b->o, ist);
766 * Return value :
767 * >0 : the number of matching bytes
768 * =0 : not enough bytes (or matching of empty string)
769 * <0 : non-matching byte found
770 */
771static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
772{
773 struct ist r = ist;
774 const char *p;
775 const char *end = b->data + b->size;
776
777 if (n < r.len)
778 return 0;
779
780 p = b_ptr(b, o);
781 while (r.len--) {
782 if (*p++ != *r.ptr++)
783 return -1;
784 if (unlikely(p == end))
785 p = b->data;
786 }
787 return ist.len;
788}
789
790/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
791 * explicitly supported. It matches a single byte per iteration so strings
792 * should remain reasonably small. Returns :
793 * > 0 : number of bytes matched and eaten
794 * = 0 : not enough bytes (or matching an empty string)
795 * < 0 : non-matching byte found
796 */
797static inline int bi_eat(struct buffer *b, const struct ist ist)
798{
799 int ret = b_isteq(b, 0, b->i, ist);
800 if (ret > 0)
801 bi_del(b, ret);
802 return ret;
803}
804
Willy Tarreaue5676e72017-09-22 15:47:51 +0200805/* injects string <ist> into the input region of buffer <b> provided that it
806 * fits. Wrapping is supported. It's designed for small strings as it only
807 * writes a single byte per iteration. Returns the number of characters copied
808 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
809 * will only modify the buffer upon success. In all cases, the contents are
810 * copied prior to reporting an error, so that the destination at least
811 * contains a valid but truncated string.
812 */
813static inline int bi_istput(struct buffer *b, const struct ist ist)
814{
815 const char *end = b->data + b->size;
816 struct ist r = ist;
817 char *p;
818
819 if (r.len > (size_t)(b->size - b->i - b->o))
820 return r.len < b->size ? 0 : -1;
821
822 p = b_ptr(b, b->i);
823 b->i += r.len;
824 while (r.len--) {
825 *p++ = *r.ptr++;
826 if (unlikely(p == end))
827 p = b->data;
828 }
829 return ist.len;
830}
831
832
833/* injects string <ist> into the output region of buffer <b> provided that it
834 * fits. Input data is assumed not to exist and will silently be overwritten.
835 * Wrapping is supported. It's designed for small strings as it only writes a
836 * single byte per iteration. Returns the number of characters copied (ist.len),
837 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
838 * modify the buffer upon success. In all cases, the contents are copied prior
839 * to reporting an error, so that the destination at least contains a valid
840 * but truncated string.
841 */
842static inline int bo_istput(struct buffer *b, const struct ist ist)
843{
844 const char *end = b->data + b->size;
845 struct ist r = ist;
846 char *p;
847
848 if (r.len > (size_t)(b->size - b->o))
849 return r.len < b->size ? 0 : -1;
850
851 p = b->p;
852 b->o += r.len;
853 b->p = b_ptr(b, r.len);
854 while (r.len--) {
855 *p++ = *r.ptr++;
856 if (unlikely(p == end))
857 p = b->data;
858 }
859 return ist.len;
860}
861
862
Willy Tarreauc7e42382012-08-24 19:22:53 +0200863#endif /* _COMMON_BUFFER_H */
864
865/*
866 * Local variables:
867 * c-indent-level: 8
868 * c-basic-offset: 8
869 * End:
870 */