blob: 1a577ca3dfa91d6496c03c34c553de9a0dd5c0a0 [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 Tarreaubafbe012017-11-24 17:34:44 +010051extern struct pool_head *pool_head_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 Tarreau53bae852017-11-26 11:00:37 +010055__decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock);
Willy Tarreauc7e42382012-08-24 19:22:53 +020056
Willy Tarreau9b28e032012-10-12 23:49:43 +020057int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020058void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020059int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
60int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020061void buffer_dump(FILE *o, struct buffer *b, int from, int to);
62void buffer_slow_realign(struct buffer *buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +020063
64/*****************************************************************/
65/* These functions are used to compute various buffer area sizes */
66/*****************************************************************/
67
68/* Returns an absolute pointer for a position relative to the current buffer's
69 * pointer. It is written so that it is optimal when <ofs> is a const. It is
70 * written as a macro instead of an inline function so that the compiler knows
71 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020072 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020073 */
74#define b_ptr(b, ofs) \
75 ({ \
76 char *__ret = (b)->p + (ofs); \
77 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
78 __ret -= (b)->size; \
79 else if ((ofs) < 0 && __ret < (b)->data) \
80 __ret += (b)->size; \
81 __ret; \
82 })
83
Willy Tarreau26488ad2017-09-19 21:14:08 +020084/* Returns the pointer to the buffer's end (data+size) */
85static inline const char *b_end(const struct buffer *b)
86{
87 return b->data + b->size;
88}
89
90/* Returns the distance between <p> and the buffer's end (data+size) */
91static inline unsigned int b_to_end(const struct buffer *b)
92{
93 return b->data + b->size - b->p;
94}
95
Willy Tarreau4a6425d2017-09-19 14:18:46 +020096/* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i>
97 * shrinks by <del> as well, and <o> is left untouched (supposed to be zero).
98 * The caller is responsible for ensuring that <del> is always smaller than or
99 * equal to b->i.
100 */
Willy Tarreau7f564d22017-10-18 08:32:12 +0200101static inline void bi_del(struct buffer *b, unsigned int del)
Willy Tarreau4a6425d2017-09-19 14:18:46 +0200102{
103 b->i -= del;
104 b->p = b_ptr(b, del);
105}
106
Willy Tarreau7f564d22017-10-18 08:32:12 +0200107/* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>.
108 * The caller is responsible for ensuring that <del> is always smaller than or
109 * equal to b->o.
110 */
111static inline void bo_del(struct buffer *b, unsigned int del)
112{
113 b->o -= del;
114}
115
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200116/* Advances the buffer by <adv> bytes, which means that the buffer
117 * pointer advances, and that as many bytes from in are transferred
118 * to out. The caller is responsible for ensuring that adv is always
119 * smaller than or equal to b->i.
120 */
121static inline void b_adv(struct buffer *b, unsigned int adv)
122{
123 b->i -= adv;
124 b->o += adv;
125 b->p = b_ptr(b, adv);
126}
127
128/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
129 * backwards, and that as many bytes from out are moved to in. The caller is
130 * responsible for ensuring that adv is always smaller than or equal to b->o.
131 */
132static inline void b_rew(struct buffer *b, unsigned int adv)
133{
134 b->i += adv;
135 b->o -= adv;
136 b->p = b_ptr(b, (int)-adv);
137}
138
Willy Tarreauc7e42382012-08-24 19:22:53 +0200139/* Returns the start of the input data in a buffer */
140static inline char *bi_ptr(const struct buffer *b)
141{
142 return b->p;
143}
144
145/* Returns the end of the input data in a buffer (pointer to next
146 * insertion point).
147 */
148static inline char *bi_end(const struct buffer *b)
149{
150 char *ret = b->p + b->i;
151
152 if (ret >= b->data + b->size)
153 ret -= b->size;
154 return ret;
155}
156
157/* Returns the amount of input data that can contiguously be read at once */
158static inline int bi_contig_data(const struct buffer *b)
159{
160 int data = b->data + b->size - b->p;
161
162 if (data > b->i)
163 data = b->i;
164 return data;
165}
166
167/* Returns the start of the output data in a buffer */
168static inline char *bo_ptr(const struct buffer *b)
169{
170 char *ret = b->p - b->o;
171
172 if (ret < b->data)
173 ret += b->size;
174 return ret;
175}
176
177/* Returns the end of the output data in a buffer */
178static inline char *bo_end(const struct buffer *b)
179{
180 return b->p;
181}
182
183/* Returns the amount of output data that can contiguously be read at once */
184static inline int bo_contig_data(const struct buffer *b)
185{
186 char *beg = b->p - b->o;
187
188 if (beg < b->data)
189 return b->data - beg;
190 return b->o;
191}
192
Christopher Faulet637f8f22017-03-29 11:58:28 +0200193/* Return the amount of bytes that can be written into the input area at once
194 * including reserved space which may be overwritten (this is the caller
195 * responsibility to know if the reserved space is protected or not).
196*/
197static inline int bi_contig_space(const struct buffer *b)
198{
199 const char *left, *right;
200
Christopher Fauleta36b3112017-06-13 22:00:22 +0200201 left = b->p + b->i;
202 right = b->p - b->o;
203 if (left >= b->data + b->size)
204 left -= b->size;
205 else {
206 if (right < b->data)
207 right += b->size;
208 else
209 right = b->data + b->size;
210 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200211 return (right - left);
212}
213
214/* Return the amount of bytes that can be written into the output area at once
215 * including reserved space which may be overwritten (this is the caller
216 * responsibility to know if the reserved space is protected or not). Input data
217 * are assumed to not exist.
218*/
219static inline int bo_contig_space(const struct buffer *b)
220{
221 const char *left, *right;
222
Christopher Fauleta36b3112017-06-13 22:00:22 +0200223 left = b->p;
224 right = b->p - b->o;
225 if (right < b->data)
226 right += b->size;
227 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200228 right = b->data + b->size;
229
230 return (right - left);
231}
232
Willy Tarreauc7e42382012-08-24 19:22:53 +0200233/* Return the buffer's length in bytes by summing the input and the output */
234static inline int buffer_len(const struct buffer *buf)
235{
236 return buf->i + buf->o;
237}
238
239/* Return non-zero only if the buffer is not empty */
240static inline int buffer_not_empty(const struct buffer *buf)
241{
242 return buf->i | buf->o;
243}
244
245/* Return non-zero only if the buffer is empty */
246static inline int buffer_empty(const struct buffer *buf)
247{
248 return !buffer_not_empty(buf);
249}
250
Willy Tarreau5b9834f2017-10-16 14:01:18 +0200251/* Return non-zero only if the buffer's free space wraps :
252 * [ |oooo| ] => yes
253 * [ |iiii| ] => yes
254 * [ |oooo|iiii| ] => yes
255 * [oooo| ] => no
256 * [ |oooo] => no
257 * [iiii| ] => no
258 * [ |iiii] => no
259 * [oooo|iiii| ] => no
260 * [ |oooo|iiii] => no
261 * [iiii| |oooo] => no
262 * [oo|iiii| |oo] => no
263 * [iiii| |oo|ii] => no
264 * [oooooooooo|iiiiiiiiiii] => no
265 * [iiiiiiiiiiiii|oooooooo] => no
266 *
267 * So the only case where the buffer does not wrap is when there's data either
268 * at the beginning or at the end of the buffer. Thus we have this :
269 * - if (p+i >= size) ==> doesn't wrap
270 * - if (p-data <= o) ==> doesn't wrap
271 * - otherwise wraps
272 */
273static inline int buffer_space_wraps(const struct buffer *buf)
274{
275 if (buf->p + buf->i >= buf->data + buf->size)
276 return 0;
277 if (buf->p <= buf->data + buf->o)
278 return 0;
279 return 1;
280}
281
Willy Tarreau42d06662012-08-27 19:51:36 +0200282/* Returns non-zero if the buffer's INPUT is considered full, which means that
283 * it holds at least as much INPUT data as (size - reserve). This also means
284 * that data that are scheduled for output are considered as potential free
285 * space, and that the reserved space is always considered as not usable. This
286 * information alone cannot be used as a general purpose free space indicator.
287 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100288 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200289 * generic function taking everything into account.
290 */
291static inline int buffer_full(const struct buffer *b, unsigned int reserve)
292{
Willy Tarreau4428a292014-11-28 20:54:13 +0100293 if (b == &buf_empty)
294 return 0;
295
Willy Tarreau42d06662012-08-27 19:51:36 +0200296 return (b->i + reserve >= b->size);
297}
298
Willy Tarreauc7e42382012-08-24 19:22:53 +0200299/* Normalizes a pointer after a subtract */
300static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
301{
302 if (ptr < buf->data)
303 ptr += buf->size;
304 return ptr;
305}
306
307/* Normalizes a pointer after an addition */
308static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
309{
310 if (ptr - buf->size >= buf->data)
311 ptr -= buf->size;
312 return ptr;
313}
314
315/* Return the maximum amount of bytes that can be written into the buffer,
316 * including reserved space which may be overwritten.
317 */
318static inline int buffer_total_space(const struct buffer *buf)
319{
320 return buf->size - buffer_len(buf);
321}
322
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100323/* Returns the amount of byte that can be written starting from <p> into the
324 * input buffer at once, including reserved space which may be overwritten.
325 * This is used by Lua to insert data in the input side just before the other
326 * data using buffer_replace(). The goal is to transfer these new data in the
327 * output buffer.
328 */
329static inline int bi_space_for_replace(const struct buffer *buf)
330{
331 const char *end;
332
333 /* If the input side data overflows, we cannot insert data contiguously. */
334 if (buf->p + buf->i >= buf->data + buf->size)
335 return 0;
336
337 /* Check the last byte used in the buffer, it may be a byte of the output
338 * side if the buffer wraps, or its the end of the buffer.
339 */
340 end = buffer_wrap_sub(buf, buf->p - buf->o);
341 if (end <= buf->p)
342 end = buf->data + buf->size;
343
344 /* Compute the amount of bytes which can be written. */
345 return end - (buf->p + buf->i);
346}
347
348
Willy Tarreauc7e42382012-08-24 19:22:53 +0200349/* Normalizes a pointer which is supposed to be relative to the beginning of a
350 * buffer, so that wrapping is correctly handled. The intent is to use this
351 * when increasing a pointer. Note that the wrapping test is only performed
352 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
353 * otherwise an invalid pointer might be returned.
354 */
355static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
356{
357 if (ptr < buf->data)
358 ptr += buf->size;
359 else if (ptr - buf->size >= buf->data)
360 ptr -= buf->size;
361 return ptr;
362}
363
364/* Returns the distance between two pointers, taking into account the ability
365 * to wrap around the buffer's end.
366 */
367static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
368{
369 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200370
371 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200372 return count;
373}
374
375/* returns the amount of pending bytes in the buffer. It is the amount of bytes
376 * that is not scheduled to be sent.
377 */
378static inline int buffer_pending(const struct buffer *buf)
379{
380 return buf->i;
381}
382
Willy Tarreauc7e42382012-08-24 19:22:53 +0200383/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
384static inline int buffer_almost_full(const struct buffer *buf)
385{
Willy Tarreau4428a292014-11-28 20:54:13 +0100386 if (buf == &buf_empty)
387 return 0;
388
389 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200390 return 1;
391 return 0;
392}
393
394/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
395 * call this function with remaining data waiting to be sent (o > 0). The
396 * caller must ensure that <n> is smaller than the actual buffer's length.
397 * This is mainly used to remove empty lines at the beginning of a request
398 * or a response.
399 */
400static inline void bi_fast_delete(struct buffer *buf, int n)
401{
402 buf->i -= n;
403 buf->p += n;
404}
405
Christopher Faulet637f8f22017-03-29 11:58:28 +0200406/* Tries to realign the given buffer. */
407static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200408{
409 if (!(buf->i | buf->o)) {
410 /* let's realign the buffer to optimize I/O */
411 buf->p = buf->data;
412 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200413}
414
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200415/* Schedule all remaining buffer data to be sent. ->o is not touched if it
416 * already covers those data. That permits doing a flush even after a forward,
417 * although not recommended.
418 */
419static inline void buffer_flush(struct buffer *buf)
420{
421 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
422 buf->o += buf->i;
423 buf->i = 0;
424}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200425
Willy Tarreauaf819352012-08-27 22:08:00 +0200426/* This function writes the string <str> at position <pos> which must be in
427 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
428 * (l, r, lr) are updated to be valid after the shift. the shift value
429 * (positive or negative) is returned. If there's no space left, the move is
430 * not done. The function does not adjust ->o because it does not make sense
431 * to use it on data scheduled to be sent.
432 */
433static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
434{
435 return buffer_replace2(b, pos, end, str, strlen(str));
436}
437
Willy Tarreau8c89c202012-09-28 16:02:48 +0200438/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
439 * Data are truncated if buffer is full.
440 */
441static inline void bo_putchr(struct buffer *b, char c)
442{
443 if (buffer_len(b) == b->size)
444 return;
445 *b->p = c;
446 b->p = b_ptr(b, 1);
447 b->o++;
448}
449
450/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100451 * Data are truncated if buffer is too short. It returns the number of bytes
452 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200453 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100454static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200455{
456 int cur_len = buffer_len(b);
457 int half;
458
459 if (len > b->size - cur_len)
460 len = (b->size - cur_len);
461 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100462 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200463
Christopher Faulet637f8f22017-03-29 11:58:28 +0200464 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200465 if (half > len)
466 half = len;
467
468 memcpy(b->p, blk, half);
469 b->p = b_ptr(b, half);
470 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100471 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200472 b->p = b_ptr(b, half);
473 }
474 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100475 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200476}
477
478/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100479 * Data are truncated if buffer is too short. It returns the number of bytes
480 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200481 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100482static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200483{
484 return bo_putblk(b, str, strlen(str));
485}
486
487/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100488 * Data are truncated if buffer is too short. It returns the number of bytes
489 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200490 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100491static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200492{
493 return bo_putblk(b, chk->str, chk->len);
494}
495
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200496/* Gets one full block of data at once from a buffer's output, optionally
497 * starting at a specific offset. Return values :
498 * >0 : number of bytes read, equal to requested size.
499 * =0 : not enough data available. <blk> is left undefined.
500 * The buffer is left unaffected.
501 */
502static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
503{
504 int firstblock;
505
506 if (len + offset > buf->o)
507 return 0;
508
509 firstblock = buf->data + buf->size - bo_ptr(buf);
510 if (firstblock > offset) {
511 if (firstblock >= len + offset) {
512 memcpy(blk, bo_ptr(buf) + offset, len);
513 return len;
514 }
515
516 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
517 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
518 return len;
519 }
520
521 memcpy(blk, buf->data + offset - firstblock, len);
522 return len;
523}
524
525/* Gets one or two blocks of data at once from a buffer's output.
526 * Return values :
527 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
528 * =0 : not enough data available. <blk*> are left undefined.
529 * The buffer is left unaffected. Unused buffers are left in an undefined state.
530 */
531static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
532{
533 if (unlikely(buf->o == 0))
534 return 0;
535
Willy Tarreau0621da52017-10-20 18:21:49 +0200536 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200537 *blk1 = buf->p - buf->o + buf->size;
538 *len1 = buf->data + buf->size - *blk1;
539 *blk2 = buf->data;
540 *len2 = buf->p - buf->data;
541 return 2;
542 }
543
Willy Tarreau4b75fff2017-11-02 17:16:07 +0100544 *blk1 = bo_ptr(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200545 *len1 = buf->o;
546 return 1;
547}
548
Willy Tarreau145746c2017-10-26 15:26:17 +0200549/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
550 * Data are truncated if buffer is full.
551 */
552static inline void bi_putchr(struct buffer *b, char c)
553{
554 if (buffer_len(b) == b->size)
555 return;
556 *bi_end(b) = c;
557 b->i++;
558}
559
560/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
561 * Data are truncated if buffer is too short. It returns the number of bytes
562 * copied.
563 */
564static inline int bi_putblk(struct buffer *b, const char *blk, int len)
565{
566 int cur_len = buffer_len(b);
567 int half;
568
569 if (len > b->size - cur_len)
570 len = (b->size - cur_len);
571 if (!len)
572 return 0;
573
574 half = bi_contig_space(b);
575 if (half > len)
576 half = len;
577
578 memcpy(bi_end(b), blk, half);
579 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100580 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200581 b->i += len;
582 return len;
583}
584
585/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
586 * Data are truncated if buffer is too short. It returns the number of bytes
587 * copied.
588 */
589static inline int bi_putstr(struct buffer *b, const char *str)
590{
591 return bi_putblk(b, str, strlen(str));
592}
593
594/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
595 * Data are truncated if buffer is too short. It returns the number of bytes
596 * copied.
597 */
598static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
599{
600 return bi_putblk(b, chk->str, chk->len);
601}
602
603/* Gets one full block of data at once from a buffer's input. Return values :
604 * >0 : number of bytes read, equal to requested size.
605 * =0 : not enough data available. <blk> is left undefined.
606 * The buffer is left unaffected.
607 */
608static inline int bi_getblk(const struct buffer *buf, char *blk, int len)
609{
610 int firstblock;
611
612 if (len > buf->i)
613 return 0;
614
615 firstblock = bi_contig_data(buf);
616 if (firstblock > len)
617 firstblock = len;
618
619 memcpy(blk, bi_ptr(buf), firstblock);
620 if (len > firstblock)
621 memcpy(blk + firstblock, buf->data, len - firstblock);
622 return len;
623}
624
625/* Gets one or two blocks of data at once from a buffer's input.
626 * Return values :
627 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
628 * =0 : not enough data available. <blk*> are left undefined.
629 * The buffer is left unaffected. Unused buffers are left in an undefined state.
630 */
631static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
632{
633 if (unlikely(buf->i == 0))
634 return 0;
635
636 if (unlikely(buf->p + buf->i > buf->data + buf->size)) {
637 *blk1 = buf->p;
638 *len1 = buf->data + buf->size - buf->p;
639 *blk2 = buf->data;
640 *len2 = buf->i - *len1;
641 return 2;
642 }
643
644 *blk1 = buf->p;
645 *len1 = buf->i;
646 return 1;
647}
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200648
Willy Tarreau474cf542014-11-24 10:54:47 +0100649/* Resets a buffer. The size is not touched. */
650static inline void b_reset(struct buffer *buf)
651{
652 buf->o = 0;
653 buf->i = 0;
654 buf->p = buf->data;
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200655
Willy Tarreau474cf542014-11-24 10:54:47 +0100656}
657
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100658/* Allocates a buffer and replaces *buf with this buffer. If no memory is
659 * available, &buf_wanted is used instead. No control is made to check if *buf
660 * already pointed to another buffer. The allocated buffer is returned, or
661 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100662 */
663static inline struct buffer *b_alloc(struct buffer **buf)
664{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100665 struct buffer *b;
666
667 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100668 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100669 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100670 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100671 b_reset(b);
672 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100673 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100674 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100675}
676
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100677/* Allocates a buffer and replaces *buf with this buffer. If no memory is
678 * available, &buf_wanted is used instead. No control is made to check if *buf
679 * already pointed to another buffer. The allocated buffer is returned, or
680 * NULL in case no memory is available. The difference with b_alloc() is that
681 * this function only picks from the pool and never calls malloc(), so it can
682 * fail even if some memory is available.
683 */
684static inline struct buffer *b_alloc_fast(struct buffer **buf)
685{
686 struct buffer *b;
687
688 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100689 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100690 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100691 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100692 b_reset(b);
693 *buf = b;
694 }
695 return b;
696}
697
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100698/* Releases buffer *buf (no check of emptiness) */
699static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100700{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100701 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100702}
703
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100704/* Releases buffer *buf if allocated. */
705static inline void b_drop(struct buffer **buf)
706{
707 if (!(*buf)->size)
708 return;
709 __b_drop(buf);
710}
711
712/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
713static inline void b_free(struct buffer **buf)
714{
715 b_drop(buf);
716 *buf = &buf_empty;
717}
718
Willy Tarreauf4718e82014-12-02 13:54:01 +0100719/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
720 * there are still at least <margin> buffers available in the pool after this
721 * allocation so that we don't leave the pool in a condition where a session or
722 * a response buffer could not be allocated anymore, resulting in a deadlock.
723 * This means that we sometimes need to try to allocate extra entries even if
724 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100725 *
726 * We need to lock the pool here to be sure to have <margin> buffers available
727 * after the allocation, regardless how many threads that doing it in the same
728 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100729 */
730static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
731{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100732 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100733
734 if ((*buf)->size)
735 return *buf;
736
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100737 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100738#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100739 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100740#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100741
Willy Tarreauf4718e82014-12-02 13:54:01 +0100742 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100743 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
744 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100745 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100746#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100747 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100748#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100749 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100750 b_reset(b);
751 *buf = b;
752 return b;
753 }
754 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100755
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100756 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100757 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100758
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100759#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100760 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100761#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100762
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100763 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100764 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100765 b_reset(b);
766 *buf = b;
767 }
768 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100769}
770
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100771
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100772/* Offer a buffer currently belonging to target <from> to whoever needs one.
773 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
774 * passing a buffer to oneself in case of failed allocations (e.g. need two
775 * buffers, get one, fail, release it and wake up self again). In case of
776 * normal buffer release where it is expected that the caller is not waiting
777 * for a buffer, NULL is fine.
778 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100779void __offer_buffer(void *from, unsigned int threshold);
780
781static inline void offer_buffers(void *from, unsigned int threshold)
782{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100783 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200784 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100785 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100786 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200787 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100788 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100789 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100790}
791
Willy Tarreau6634b632017-09-22 15:02:54 +0200792/*************************************************************************/
793/* functions used to manipulate strings and blocks with wrapping buffers */
794/*************************************************************************/
795
796/* returns > 0 if the first <n> characters of buffer <b> starting at
797 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
798 * designed to be use with reasonably small strings (ie matches a single byte
799 * per iteration). This function is usable both with input and output data. To
800 * be used like this depending on what to match :
801 * - input contents : b_isteq(b, 0, b->i, ist);
802 * - output contents : b_isteq(b, -b->o, b->o, ist);
803 * Return value :
804 * >0 : the number of matching bytes
805 * =0 : not enough bytes (or matching of empty string)
806 * <0 : non-matching byte found
807 */
808static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
809{
810 struct ist r = ist;
811 const char *p;
812 const char *end = b->data + b->size;
813
814 if (n < r.len)
815 return 0;
816
817 p = b_ptr(b, o);
818 while (r.len--) {
819 if (*p++ != *r.ptr++)
820 return -1;
821 if (unlikely(p == end))
822 p = b->data;
823 }
824 return ist.len;
825}
826
827/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
828 * explicitly supported. It matches a single byte per iteration so strings
829 * should remain reasonably small. Returns :
830 * > 0 : number of bytes matched and eaten
831 * = 0 : not enough bytes (or matching an empty string)
832 * < 0 : non-matching byte found
833 */
834static inline int bi_eat(struct buffer *b, const struct ist ist)
835{
836 int ret = b_isteq(b, 0, b->i, ist);
837 if (ret > 0)
838 bi_del(b, ret);
839 return ret;
840}
841
Willy Tarreaue5676e72017-09-22 15:47:51 +0200842/* injects string <ist> into the input region of buffer <b> provided that it
843 * fits. Wrapping is supported. It's designed for small strings as it only
844 * writes a single byte per iteration. Returns the number of characters copied
845 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
846 * will only modify the buffer upon success. In all cases, the contents are
847 * copied prior to reporting an error, so that the destination at least
848 * contains a valid but truncated string.
849 */
850static inline int bi_istput(struct buffer *b, const struct ist ist)
851{
852 const char *end = b->data + b->size;
853 struct ist r = ist;
854 char *p;
855
856 if (r.len > (size_t)(b->size - b->i - b->o))
857 return r.len < b->size ? 0 : -1;
858
859 p = b_ptr(b, b->i);
860 b->i += r.len;
861 while (r.len--) {
862 *p++ = *r.ptr++;
863 if (unlikely(p == end))
864 p = b->data;
865 }
866 return ist.len;
867}
868
869
870/* injects string <ist> into the output region of buffer <b> provided that it
871 * fits. Input data is assumed not to exist and will silently be overwritten.
872 * Wrapping is supported. It's designed for small strings as it only writes a
873 * single byte per iteration. Returns the number of characters copied (ist.len),
874 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
875 * modify the buffer upon success. In all cases, the contents are copied prior
876 * to reporting an error, so that the destination at least contains a valid
877 * but truncated string.
878 */
879static inline int bo_istput(struct buffer *b, const struct ist ist)
880{
881 const char *end = b->data + b->size;
882 struct ist r = ist;
883 char *p;
884
885 if (r.len > (size_t)(b->size - b->o))
886 return r.len < b->size ? 0 : -1;
887
888 p = b->p;
889 b->o += r.len;
890 b->p = b_ptr(b, r.len);
891 while (r.len--) {
892 *p++ = *r.ptr++;
893 if (unlikely(p == end))
894 p = b->data;
895 }
896 return ist.len;
897}
898
899
Willy Tarreauc7e42382012-08-24 19:22:53 +0200900#endif /* _COMMON_BUFFER_H */
901
902/*
903 * Local variables:
904 * c-indent-level: 8
905 * c-basic-offset: 8
906 * End:
907 */