blob: 3b34267b0cb10b4465648e2a1e0c1ffbfd42f97d [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 Tarreau41806d12018-07-11 09:39:05 +020029#include <common/buf.h>
Willy Tarreau8c89c202012-09-28 16:02:48 +020030#include <common/chunk.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <common/config.h>
Willy Tarreau6634b632017-09-22 15:02:54 +020032#include <common/ist.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020033#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034
35
Christopher Fauleta73e59b2016-12-09 17:30:18 +010036/* an element of the <buffer_wq> list. It represents an object that need to
37 * acquire a buffer to continue its process. */
38struct buffer_wait {
39 void *target; /* The waiting object that should be woken up */
40 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
41 struct list list; /* Next element in the <buffer_wq> list */
42};
43
Willy Tarreaubafbe012017-11-24 17:34:44 +010044extern struct pool_head *pool_head_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010045extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010046extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010047extern struct list buffer_wq;
Willy Tarreau53bae852017-11-26 11:00:37 +010048__decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock);
Willy Tarreauc7e42382012-08-24 19:22:53 +020049
Willy Tarreau9b28e032012-10-12 23:49:43 +020050int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020051void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020052int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
53int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020054void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreau4cf13002018-06-06 06:53:15 +020055void buffer_slow_realign(struct buffer *buf, size_t output);
Willy Tarreauc7e42382012-08-24 19:22:53 +020056
57/*****************************************************************/
58/* These functions are used to compute various buffer area sizes */
59/*****************************************************************/
60
61/* Returns an absolute pointer for a position relative to the current buffer's
62 * pointer. It is written so that it is optimal when <ofs> is a const. It is
63 * written as a macro instead of an inline function so that the compiler knows
64 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020065 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020066 */
67#define b_ptr(b, ofs) \
68 ({ \
69 char *__ret = (b)->p + (ofs); \
70 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
71 __ret -= (b)->size; \
72 else if ((ofs) < 0 && __ret < (b)->data) \
73 __ret += (b)->size; \
74 __ret; \
75 })
76
Willy Tarreau26488ad2017-09-19 21:14:08 +020077/* Returns the pointer to the buffer's end (data+size) */
78static inline const char *b_end(const struct buffer *b)
79{
80 return b->data + b->size;
81}
82
83/* Returns the distance between <p> and the buffer's end (data+size) */
84static inline unsigned int b_to_end(const struct buffer *b)
85{
86 return b->data + b->size - b->p;
87}
88
Willy Tarreau4a6425d2017-09-19 14:18:46 +020089/* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i>
90 * shrinks by <del> as well, and <o> is left untouched (supposed to be zero).
91 * The caller is responsible for ensuring that <del> is always smaller than or
92 * equal to b->i.
93 */
Willy Tarreau7f564d22017-10-18 08:32:12 +020094static inline void bi_del(struct buffer *b, unsigned int del)
Willy Tarreau4a6425d2017-09-19 14:18:46 +020095{
96 b->i -= del;
97 b->p = b_ptr(b, del);
98}
99
Willy Tarreau7f564d22017-10-18 08:32:12 +0200100/* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>.
101 * The caller is responsible for ensuring that <del> is always smaller than or
102 * equal to b->o.
103 */
104static inline void bo_del(struct buffer *b, unsigned int del)
105{
106 b->o -= del;
107}
108
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200109/* Advances the buffer by <adv> bytes, which means that the buffer
110 * pointer advances, and that as many bytes from in are transferred
111 * to out. The caller is responsible for ensuring that adv is always
112 * smaller than or equal to b->i.
113 */
114static inline void b_adv(struct buffer *b, unsigned int adv)
115{
116 b->i -= adv;
117 b->o += adv;
118 b->p = b_ptr(b, adv);
119}
120
121/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
122 * backwards, and that as many bytes from out are moved to in. The caller is
123 * responsible for ensuring that adv is always smaller than or equal to b->o.
124 */
125static inline void b_rew(struct buffer *b, unsigned int adv)
126{
127 b->i += adv;
128 b->o -= adv;
129 b->p = b_ptr(b, (int)-adv);
130}
131
Willy Tarreauc7e42382012-08-24 19:22:53 +0200132/* Returns the start of the input data in a buffer */
133static inline char *bi_ptr(const struct buffer *b)
134{
135 return b->p;
136}
137
138/* Returns the end of the input data in a buffer (pointer to next
139 * insertion point).
140 */
141static inline char *bi_end(const struct buffer *b)
142{
143 char *ret = b->p + b->i;
144
145 if (ret >= b->data + b->size)
146 ret -= b->size;
147 return ret;
148}
149
150/* Returns the amount of input data that can contiguously be read at once */
151static inline int bi_contig_data(const struct buffer *b)
152{
153 int data = b->data + b->size - b->p;
154
155 if (data > b->i)
156 data = b->i;
157 return data;
158}
159
160/* Returns the start of the output data in a buffer */
161static inline char *bo_ptr(const struct buffer *b)
162{
163 char *ret = b->p - b->o;
164
165 if (ret < b->data)
166 ret += b->size;
167 return ret;
168}
169
170/* Returns the end of the output data in a buffer */
171static inline char *bo_end(const struct buffer *b)
172{
173 return b->p;
174}
175
176/* Returns the amount of output data that can contiguously be read at once */
177static inline int bo_contig_data(const struct buffer *b)
178{
179 char *beg = b->p - b->o;
180
181 if (beg < b->data)
182 return b->data - beg;
183 return b->o;
184}
185
Christopher Faulet637f8f22017-03-29 11:58:28 +0200186/* Return the amount of bytes that can be written into the input area at once
187 * including reserved space which may be overwritten (this is the caller
188 * responsibility to know if the reserved space is protected or not).
189*/
190static inline int bi_contig_space(const struct buffer *b)
191{
192 const char *left, *right;
193
Christopher Fauleta36b3112017-06-13 22:00:22 +0200194 left = b->p + b->i;
195 right = b->p - b->o;
196 if (left >= b->data + b->size)
197 left -= b->size;
198 else {
199 if (right < b->data)
200 right += b->size;
201 else
202 right = b->data + b->size;
203 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200204 return (right - left);
205}
206
207/* Return the amount of bytes that can be written into the output area at once
208 * including reserved space which may be overwritten (this is the caller
209 * responsibility to know if the reserved space is protected or not). Input data
210 * are assumed to not exist.
211*/
212static inline int bo_contig_space(const struct buffer *b)
213{
214 const char *left, *right;
215
Christopher Fauleta36b3112017-06-13 22:00:22 +0200216 left = b->p;
217 right = b->p - b->o;
218 if (right < b->data)
219 right += b->size;
220 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200221 right = b->data + b->size;
222
223 return (right - left);
224}
225
Willy Tarreauc7e42382012-08-24 19:22:53 +0200226/* Return the buffer's length in bytes by summing the input and the output */
227static inline int buffer_len(const struct buffer *buf)
228{
229 return buf->i + buf->o;
230}
231
232/* Return non-zero only if the buffer is not empty */
233static inline int buffer_not_empty(const struct buffer *buf)
234{
235 return buf->i | buf->o;
236}
237
238/* Return non-zero only if the buffer is empty */
239static inline int buffer_empty(const struct buffer *buf)
240{
241 return !buffer_not_empty(buf);
242}
243
Willy Tarreau5b9834f2017-10-16 14:01:18 +0200244/* Return non-zero only if the buffer's free space wraps :
245 * [ |oooo| ] => yes
246 * [ |iiii| ] => yes
247 * [ |oooo|iiii| ] => yes
248 * [oooo| ] => no
249 * [ |oooo] => no
250 * [iiii| ] => no
251 * [ |iiii] => no
252 * [oooo|iiii| ] => no
253 * [ |oooo|iiii] => no
254 * [iiii| |oooo] => no
255 * [oo|iiii| |oo] => no
256 * [iiii| |oo|ii] => no
257 * [oooooooooo|iiiiiiiiiii] => no
258 * [iiiiiiiiiiiii|oooooooo] => no
259 *
260 * So the only case where the buffer does not wrap is when there's data either
261 * at the beginning or at the end of the buffer. Thus we have this :
262 * - if (p+i >= size) ==> doesn't wrap
263 * - if (p-data <= o) ==> doesn't wrap
264 * - otherwise wraps
265 */
266static inline int buffer_space_wraps(const struct buffer *buf)
267{
268 if (buf->p + buf->i >= buf->data + buf->size)
269 return 0;
270 if (buf->p <= buf->data + buf->o)
271 return 0;
272 return 1;
273}
274
Willy Tarreau42d06662012-08-27 19:51:36 +0200275/* Returns non-zero if the buffer's INPUT is considered full, which means that
276 * it holds at least as much INPUT data as (size - reserve). This also means
277 * that data that are scheduled for output are considered as potential free
278 * space, and that the reserved space is always considered as not usable. This
279 * information alone cannot be used as a general purpose free space indicator.
280 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100281 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200282 * generic function taking everything into account.
283 */
284static inline int buffer_full(const struct buffer *b, unsigned int reserve)
285{
Willy Tarreau4428a292014-11-28 20:54:13 +0100286 if (b == &buf_empty)
287 return 0;
288
Willy Tarreau42d06662012-08-27 19:51:36 +0200289 return (b->i + reserve >= b->size);
290}
291
Willy Tarreauc7e42382012-08-24 19:22:53 +0200292/* Normalizes a pointer after a subtract */
293static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
294{
295 if (ptr < buf->data)
296 ptr += buf->size;
297 return ptr;
298}
299
300/* Normalizes a pointer after an addition */
301static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
302{
303 if (ptr - buf->size >= buf->data)
304 ptr -= buf->size;
305 return ptr;
306}
307
308/* Return the maximum amount of bytes that can be written into the buffer,
309 * including reserved space which may be overwritten.
310 */
311static inline int buffer_total_space(const struct buffer *buf)
312{
313 return buf->size - buffer_len(buf);
314}
315
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100316/* Returns the amount of byte that can be written starting from <p> into the
317 * input buffer at once, including reserved space which may be overwritten.
318 * This is used by Lua to insert data in the input side just before the other
319 * data using buffer_replace(). The goal is to transfer these new data in the
320 * output buffer.
321 */
322static inline int bi_space_for_replace(const struct buffer *buf)
323{
324 const char *end;
325
326 /* If the input side data overflows, we cannot insert data contiguously. */
327 if (buf->p + buf->i >= buf->data + buf->size)
328 return 0;
329
330 /* Check the last byte used in the buffer, it may be a byte of the output
331 * side if the buffer wraps, or its the end of the buffer.
332 */
333 end = buffer_wrap_sub(buf, buf->p - buf->o);
334 if (end <= buf->p)
335 end = buf->data + buf->size;
336
337 /* Compute the amount of bytes which can be written. */
338 return end - (buf->p + buf->i);
339}
340
341
Willy Tarreauc7e42382012-08-24 19:22:53 +0200342/* Normalizes a pointer which is supposed to be relative to the beginning of a
343 * buffer, so that wrapping is correctly handled. The intent is to use this
344 * when increasing a pointer. Note that the wrapping test is only performed
345 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
346 * otherwise an invalid pointer might be returned.
347 */
348static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
349{
350 if (ptr < buf->data)
351 ptr += buf->size;
352 else if (ptr - buf->size >= buf->data)
353 ptr -= buf->size;
354 return ptr;
355}
356
357/* Returns the distance between two pointers, taking into account the ability
358 * to wrap around the buffer's end.
359 */
360static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
361{
362 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200363
364 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200365 return count;
366}
367
368/* returns the amount of pending bytes in the buffer. It is the amount of bytes
369 * that is not scheduled to be sent.
370 */
371static inline int buffer_pending(const struct buffer *buf)
372{
373 return buf->i;
374}
375
Willy Tarreauc7e42382012-08-24 19:22:53 +0200376/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
377static inline int buffer_almost_full(const struct buffer *buf)
378{
Willy Tarreau4428a292014-11-28 20:54:13 +0100379 if (buf == &buf_empty)
380 return 0;
381
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200382 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200383}
384
385/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
386 * call this function with remaining data waiting to be sent (o > 0). The
387 * caller must ensure that <n> is smaller than the actual buffer's length.
388 * This is mainly used to remove empty lines at the beginning of a request
389 * or a response.
390 */
391static inline void bi_fast_delete(struct buffer *buf, int n)
392{
393 buf->i -= n;
394 buf->p += n;
395}
396
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200397/* Schedule all remaining buffer data to be sent. ->o is not touched if it
398 * already covers those data. That permits doing a flush even after a forward,
399 * although not recommended.
400 */
401static inline void buffer_flush(struct buffer *buf)
402{
403 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
404 buf->o += buf->i;
405 buf->i = 0;
406}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200407
Willy Tarreauaf819352012-08-27 22:08:00 +0200408/* This function writes the string <str> at position <pos> which must be in
409 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
410 * (l, r, lr) are updated to be valid after the shift. the shift value
411 * (positive or negative) is returned. If there's no space left, the move is
412 * not done. The function does not adjust ->o because it does not make sense
413 * to use it on data scheduled to be sent.
414 */
415static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
416{
417 return buffer_replace2(b, pos, end, str, strlen(str));
418}
419
Willy Tarreau8c89c202012-09-28 16:02:48 +0200420/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
421 * Data are truncated if buffer is full.
422 */
423static inline void bo_putchr(struct buffer *b, char c)
424{
425 if (buffer_len(b) == b->size)
426 return;
427 *b->p = c;
428 b->p = b_ptr(b, 1);
429 b->o++;
430}
431
432/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100433 * Data are truncated if buffer is too short. It returns the number of bytes
434 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200435 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100436static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200437{
438 int cur_len = buffer_len(b);
439 int half;
440
441 if (len > b->size - cur_len)
442 len = (b->size - cur_len);
443 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100444 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200445
Christopher Faulet637f8f22017-03-29 11:58:28 +0200446 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200447 if (half > len)
448 half = len;
449
450 memcpy(b->p, blk, half);
451 b->p = b_ptr(b, half);
452 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100453 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200454 b->p = b_ptr(b, half);
455 }
456 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100457 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200458}
459
460/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100461 * Data are truncated if buffer is too short. It returns the number of bytes
462 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200463 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100464static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200465{
466 return bo_putblk(b, str, strlen(str));
467}
468
469/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100470 * Data are truncated if buffer is too short. It returns the number of bytes
471 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200472 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100473static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200474{
475 return bo_putblk(b, chk->str, chk->len);
476}
477
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200478/* Gets one full block of data at once from a buffer's output, optionally
479 * starting at a specific offset. Return values :
480 * >0 : number of bytes read, equal to requested size.
481 * =0 : not enough data available. <blk> is left undefined.
482 * The buffer is left unaffected.
483 */
484static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
485{
486 int firstblock;
487
488 if (len + offset > buf->o)
489 return 0;
490
491 firstblock = buf->data + buf->size - bo_ptr(buf);
492 if (firstblock > offset) {
493 if (firstblock >= len + offset) {
494 memcpy(blk, bo_ptr(buf) + offset, len);
495 return len;
496 }
497
498 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
499 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
500 return len;
501 }
502
503 memcpy(blk, buf->data + offset - firstblock, len);
504 return len;
505}
506
507/* Gets one or two blocks of data at once from a buffer's output.
508 * Return values :
509 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
510 * =0 : not enough data available. <blk*> are left undefined.
511 * The buffer is left unaffected. Unused buffers are left in an undefined state.
512 */
513static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
514{
515 if (unlikely(buf->o == 0))
516 return 0;
517
Willy Tarreau0621da52017-10-20 18:21:49 +0200518 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200519 *blk1 = buf->p - buf->o + buf->size;
520 *len1 = buf->data + buf->size - *blk1;
521 *blk2 = buf->data;
522 *len2 = buf->p - buf->data;
523 return 2;
524 }
525
Willy Tarreau4b75fff2017-11-02 17:16:07 +0100526 *blk1 = bo_ptr(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200527 *len1 = buf->o;
528 return 1;
529}
530
Willy Tarreau145746c2017-10-26 15:26:17 +0200531/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
532 * Data are truncated if buffer is full.
533 */
534static inline void bi_putchr(struct buffer *b, char c)
535{
536 if (buffer_len(b) == b->size)
537 return;
538 *bi_end(b) = c;
539 b->i++;
540}
541
542/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
543 * Data are truncated if buffer is too short. It returns the number of bytes
544 * copied.
545 */
546static inline int bi_putblk(struct buffer *b, const char *blk, int len)
547{
548 int cur_len = buffer_len(b);
549 int half;
550
551 if (len > b->size - cur_len)
552 len = (b->size - cur_len);
553 if (!len)
554 return 0;
555
556 half = bi_contig_space(b);
557 if (half > len)
558 half = len;
559
560 memcpy(bi_end(b), blk, half);
561 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100562 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200563 b->i += len;
564 return len;
565}
566
567/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
568 * Data are truncated if buffer is too short. It returns the number of bytes
569 * copied.
570 */
571static inline int bi_putstr(struct buffer *b, const char *str)
572{
573 return bi_putblk(b, str, strlen(str));
574}
575
576/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
577 * Data are truncated if buffer is too short. It returns the number of bytes
578 * copied.
579 */
580static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
581{
582 return bi_putblk(b, chk->str, chk->len);
583}
584
585/* Gets one full block of data at once from a buffer's input. Return values :
586 * >0 : number of bytes read, equal to requested size.
587 * =0 : not enough data available. <blk> is left undefined.
588 * The buffer is left unaffected.
589 */
590static inline int bi_getblk(const struct buffer *buf, char *blk, int len)
591{
592 int firstblock;
593
594 if (len > buf->i)
595 return 0;
596
597 firstblock = bi_contig_data(buf);
598 if (firstblock > len)
599 firstblock = len;
600
601 memcpy(blk, bi_ptr(buf), firstblock);
602 if (len > firstblock)
603 memcpy(blk + firstblock, buf->data, len - firstblock);
604 return len;
605}
606
607/* Gets one or two blocks of data at once from a buffer's input.
608 * Return values :
609 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
610 * =0 : not enough data available. <blk*> are left undefined.
611 * The buffer is left unaffected. Unused buffers are left in an undefined state.
612 */
613static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
614{
615 if (unlikely(buf->i == 0))
616 return 0;
617
618 if (unlikely(buf->p + buf->i > buf->data + buf->size)) {
619 *blk1 = buf->p;
620 *len1 = buf->data + buf->size - buf->p;
621 *blk2 = buf->data;
622 *len2 = buf->i - *len1;
623 return 2;
624 }
625
626 *blk1 = buf->p;
627 *len1 = buf->i;
628 return 1;
629}
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200630
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100631/* Allocates a buffer and replaces *buf with this buffer. If no memory is
632 * available, &buf_wanted is used instead. No control is made to check if *buf
633 * already pointed to another buffer. The allocated buffer is returned, or
634 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100635 */
636static inline struct buffer *b_alloc(struct buffer **buf)
637{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100638 struct buffer *b;
639
640 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100641 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100642 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100643 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100644 b_reset(b);
645 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100646 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100647 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100648}
649
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100650/* Allocates a buffer and replaces *buf with this buffer. If no memory is
651 * available, &buf_wanted is used instead. No control is made to check if *buf
652 * already pointed to another buffer. The allocated buffer is returned, or
653 * NULL in case no memory is available. The difference with b_alloc() is that
654 * this function only picks from the pool and never calls malloc(), so it can
655 * fail even if some memory is available.
656 */
657static inline struct buffer *b_alloc_fast(struct buffer **buf)
658{
659 struct buffer *b;
660
661 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100662 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100663 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100664 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100665 b_reset(b);
666 *buf = b;
667 }
668 return b;
669}
670
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100671/* Releases buffer *buf (no check of emptiness) */
672static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100673{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100674 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100675}
676
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100677/* Releases buffer *buf if allocated. */
678static inline void b_drop(struct buffer **buf)
679{
680 if (!(*buf)->size)
681 return;
682 __b_drop(buf);
683}
684
685/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
686static inline void b_free(struct buffer **buf)
687{
688 b_drop(buf);
689 *buf = &buf_empty;
690}
691
Willy Tarreauf4718e82014-12-02 13:54:01 +0100692/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
693 * there are still at least <margin> buffers available in the pool after this
694 * allocation so that we don't leave the pool in a condition where a session or
695 * a response buffer could not be allocated anymore, resulting in a deadlock.
696 * This means that we sometimes need to try to allocate extra entries even if
697 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100698 *
699 * We need to lock the pool here to be sure to have <margin> buffers available
700 * after the allocation, regardless how many threads that doing it in the same
701 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100702 */
703static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
704{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100705 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100706
707 if ((*buf)->size)
708 return *buf;
709
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100710 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100711#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100712 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100713#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100714
Willy Tarreauf4718e82014-12-02 13:54:01 +0100715 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100716 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
717 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100718 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100719#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100720 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100721#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100722 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100723 b_reset(b);
724 *buf = b;
725 return b;
726 }
727 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100728
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100729 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100730 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100731
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100732#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100733 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100734#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100735
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100736 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100737 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100738 b_reset(b);
739 *buf = b;
740 }
741 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100742}
743
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100744
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100745/* Offer a buffer currently belonging to target <from> to whoever needs one.
746 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
747 * passing a buffer to oneself in case of failed allocations (e.g. need two
748 * buffers, get one, fail, release it and wake up self again). In case of
749 * normal buffer release where it is expected that the caller is not waiting
750 * for a buffer, NULL is fine.
751 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100752void __offer_buffer(void *from, unsigned int threshold);
753
754static inline void offer_buffers(void *from, unsigned int threshold)
755{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100756 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200757 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100758 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100759 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200760 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100761 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100762 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100763}
764
Willy Tarreau6634b632017-09-22 15:02:54 +0200765/*************************************************************************/
766/* functions used to manipulate strings and blocks with wrapping buffers */
767/*************************************************************************/
768
769/* returns > 0 if the first <n> characters of buffer <b> starting at
770 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
771 * designed to be use with reasonably small strings (ie matches a single byte
772 * per iteration). This function is usable both with input and output data. To
773 * be used like this depending on what to match :
774 * - input contents : b_isteq(b, 0, b->i, ist);
775 * - output contents : b_isteq(b, -b->o, b->o, ist);
776 * Return value :
777 * >0 : the number of matching bytes
778 * =0 : not enough bytes (or matching of empty string)
779 * <0 : non-matching byte found
780 */
781static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
782{
783 struct ist r = ist;
784 const char *p;
785 const char *end = b->data + b->size;
786
787 if (n < r.len)
788 return 0;
789
790 p = b_ptr(b, o);
791 while (r.len--) {
792 if (*p++ != *r.ptr++)
793 return -1;
794 if (unlikely(p == end))
795 p = b->data;
796 }
797 return ist.len;
798}
799
800/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
801 * explicitly supported. It matches a single byte per iteration so strings
802 * should remain reasonably small. Returns :
803 * > 0 : number of bytes matched and eaten
804 * = 0 : not enough bytes (or matching an empty string)
805 * < 0 : non-matching byte found
806 */
807static inline int bi_eat(struct buffer *b, const struct ist ist)
808{
809 int ret = b_isteq(b, 0, b->i, ist);
810 if (ret > 0)
811 bi_del(b, ret);
812 return ret;
813}
814
Willy Tarreaue5676e72017-09-22 15:47:51 +0200815/* injects string <ist> into the input region of buffer <b> provided that it
816 * fits. Wrapping is supported. It's designed for small strings as it only
817 * writes a single byte per iteration. Returns the number of characters copied
818 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
819 * will only modify the buffer upon success. In all cases, the contents are
820 * copied prior to reporting an error, so that the destination at least
821 * contains a valid but truncated string.
822 */
823static inline int bi_istput(struct buffer *b, const struct ist ist)
824{
825 const char *end = b->data + b->size;
826 struct ist r = ist;
827 char *p;
828
829 if (r.len > (size_t)(b->size - b->i - b->o))
830 return r.len < b->size ? 0 : -1;
831
832 p = b_ptr(b, b->i);
833 b->i += r.len;
834 while (r.len--) {
835 *p++ = *r.ptr++;
836 if (unlikely(p == end))
837 p = b->data;
838 }
839 return ist.len;
840}
841
842
843/* injects string <ist> into the output region of buffer <b> provided that it
844 * fits. Input data is assumed not to exist and will silently be overwritten.
845 * Wrapping is supported. It's designed for small strings as it only writes a
846 * single byte per iteration. Returns the number of characters copied (ist.len),
847 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
848 * modify the buffer upon success. In all cases, the contents are copied prior
849 * to reporting an error, so that the destination at least contains a valid
850 * but truncated string.
851 */
852static inline int bo_istput(struct buffer *b, const struct ist ist)
853{
854 const char *end = b->data + b->size;
855 struct ist r = ist;
856 char *p;
857
858 if (r.len > (size_t)(b->size - b->o))
859 return r.len < b->size ? 0 : -1;
860
861 p = b->p;
862 b->o += r.len;
863 b->p = b_ptr(b, r.len);
864 while (r.len--) {
865 *p++ = *r.ptr++;
866 if (unlikely(p == end))
867 p = b->data;
868 }
869 return ist.len;
870}
871
872
Willy Tarreauc7e42382012-08-24 19:22:53 +0200873#endif /* _COMMON_BUFFER_H */
874
875/*
876 * Local variables:
877 * c-indent-level: 8
878 * c-basic-offset: 8
879 * End:
880 */