blob: 590078c0de19df4e82f2d4be04895863feb82486 [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);
55void buffer_slow_realign(struct buffer *buf);
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
Christopher Faulet637f8f22017-03-29 11:58:28 +0200397/* Tries to realign the given buffer. */
398static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200399{
400 if (!(buf->i | buf->o)) {
401 /* let's realign the buffer to optimize I/O */
402 buf->p = buf->data;
403 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200404}
405
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200406/* Schedule all remaining buffer data to be sent. ->o is not touched if it
407 * already covers those data. That permits doing a flush even after a forward,
408 * although not recommended.
409 */
410static inline void buffer_flush(struct buffer *buf)
411{
412 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
413 buf->o += buf->i;
414 buf->i = 0;
415}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200416
Willy Tarreauaf819352012-08-27 22:08:00 +0200417/* This function writes the string <str> at position <pos> which must be in
418 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
419 * (l, r, lr) are updated to be valid after the shift. the shift value
420 * (positive or negative) is returned. If there's no space left, the move is
421 * not done. The function does not adjust ->o because it does not make sense
422 * to use it on data scheduled to be sent.
423 */
424static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
425{
426 return buffer_replace2(b, pos, end, str, strlen(str));
427}
428
Willy Tarreau8c89c202012-09-28 16:02:48 +0200429/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
430 * Data are truncated if buffer is full.
431 */
432static inline void bo_putchr(struct buffer *b, char c)
433{
434 if (buffer_len(b) == b->size)
435 return;
436 *b->p = c;
437 b->p = b_ptr(b, 1);
438 b->o++;
439}
440
441/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100442 * Data are truncated if buffer is too short. It returns the number of bytes
443 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200444 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100445static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200446{
447 int cur_len = buffer_len(b);
448 int half;
449
450 if (len > b->size - cur_len)
451 len = (b->size - cur_len);
452 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100453 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200454
Christopher Faulet637f8f22017-03-29 11:58:28 +0200455 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200456 if (half > len)
457 half = len;
458
459 memcpy(b->p, blk, half);
460 b->p = b_ptr(b, half);
461 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100462 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200463 b->p = b_ptr(b, half);
464 }
465 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100466 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200467}
468
469/* Tries to copy string <str> 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_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200474{
475 return bo_putblk(b, str, strlen(str));
476}
477
478/* Tries to copy chunk <chk> 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_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200483{
484 return bo_putblk(b, chk->str, chk->len);
485}
486
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200487/* Gets one full block of data at once from a buffer's output, optionally
488 * starting at a specific offset. Return values :
489 * >0 : number of bytes read, equal to requested size.
490 * =0 : not enough data available. <blk> is left undefined.
491 * The buffer is left unaffected.
492 */
493static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
494{
495 int firstblock;
496
497 if (len + offset > buf->o)
498 return 0;
499
500 firstblock = buf->data + buf->size - bo_ptr(buf);
501 if (firstblock > offset) {
502 if (firstblock >= len + offset) {
503 memcpy(blk, bo_ptr(buf) + offset, len);
504 return len;
505 }
506
507 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
508 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
509 return len;
510 }
511
512 memcpy(blk, buf->data + offset - firstblock, len);
513 return len;
514}
515
516/* Gets one or two blocks of data at once from a buffer's output.
517 * Return values :
518 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
519 * =0 : not enough data available. <blk*> are left undefined.
520 * The buffer is left unaffected. Unused buffers are left in an undefined state.
521 */
522static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
523{
524 if (unlikely(buf->o == 0))
525 return 0;
526
Willy Tarreau0621da52017-10-20 18:21:49 +0200527 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200528 *blk1 = buf->p - buf->o + buf->size;
529 *len1 = buf->data + buf->size - *blk1;
530 *blk2 = buf->data;
531 *len2 = buf->p - buf->data;
532 return 2;
533 }
534
Willy Tarreau4b75fff2017-11-02 17:16:07 +0100535 *blk1 = bo_ptr(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200536 *len1 = buf->o;
537 return 1;
538}
539
Willy Tarreau145746c2017-10-26 15:26:17 +0200540/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
541 * Data are truncated if buffer is full.
542 */
543static inline void bi_putchr(struct buffer *b, char c)
544{
545 if (buffer_len(b) == b->size)
546 return;
547 *bi_end(b) = c;
548 b->i++;
549}
550
551/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
552 * Data are truncated if buffer is too short. It returns the number of bytes
553 * copied.
554 */
555static inline int bi_putblk(struct buffer *b, const char *blk, int len)
556{
557 int cur_len = buffer_len(b);
558 int half;
559
560 if (len > b->size - cur_len)
561 len = (b->size - cur_len);
562 if (!len)
563 return 0;
564
565 half = bi_contig_space(b);
566 if (half > len)
567 half = len;
568
569 memcpy(bi_end(b), blk, half);
570 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100571 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200572 b->i += len;
573 return len;
574}
575
576/* Tries to copy string <str> 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_putstr(struct buffer *b, const char *str)
581{
582 return bi_putblk(b, str, strlen(str));
583}
584
585/* Tries to copy chunk <chk> 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_putchk(struct buffer *b, const struct chunk *chk)
590{
591 return bi_putblk(b, chk->str, chk->len);
592}
593
594/* Gets one full block of data at once from a buffer's input. Return values :
595 * >0 : number of bytes read, equal to requested size.
596 * =0 : not enough data available. <blk> is left undefined.
597 * The buffer is left unaffected.
598 */
599static inline int bi_getblk(const struct buffer *buf, char *blk, int len)
600{
601 int firstblock;
602
603 if (len > buf->i)
604 return 0;
605
606 firstblock = bi_contig_data(buf);
607 if (firstblock > len)
608 firstblock = len;
609
610 memcpy(blk, bi_ptr(buf), firstblock);
611 if (len > firstblock)
612 memcpy(blk + firstblock, buf->data, len - firstblock);
613 return len;
614}
615
616/* Gets one or two blocks of data at once from a buffer's input.
617 * Return values :
618 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
619 * =0 : not enough data available. <blk*> are left undefined.
620 * The buffer is left unaffected. Unused buffers are left in an undefined state.
621 */
622static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
623{
624 if (unlikely(buf->i == 0))
625 return 0;
626
627 if (unlikely(buf->p + buf->i > buf->data + buf->size)) {
628 *blk1 = buf->p;
629 *len1 = buf->data + buf->size - buf->p;
630 *blk2 = buf->data;
631 *len2 = buf->i - *len1;
632 return 2;
633 }
634
635 *blk1 = buf->p;
636 *len1 = buf->i;
637 return 1;
638}
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200639
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100640/* Allocates a buffer and replaces *buf with this buffer. If no memory is
641 * available, &buf_wanted is used instead. No control is made to check if *buf
642 * already pointed to another buffer. The allocated buffer is returned, or
643 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100644 */
645static inline struct buffer *b_alloc(struct buffer **buf)
646{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100647 struct buffer *b;
648
649 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100650 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100651 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100652 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100653 b_reset(b);
654 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100655 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100656 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100657}
658
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100659/* Allocates a buffer and replaces *buf with this buffer. If no memory is
660 * available, &buf_wanted is used instead. No control is made to check if *buf
661 * already pointed to another buffer. The allocated buffer is returned, or
662 * NULL in case no memory is available. The difference with b_alloc() is that
663 * this function only picks from the pool and never calls malloc(), so it can
664 * fail even if some memory is available.
665 */
666static inline struct buffer *b_alloc_fast(struct buffer **buf)
667{
668 struct buffer *b;
669
670 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100671 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100672 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100673 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100674 b_reset(b);
675 *buf = b;
676 }
677 return b;
678}
679
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100680/* Releases buffer *buf (no check of emptiness) */
681static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100682{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100683 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100684}
685
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100686/* Releases buffer *buf if allocated. */
687static inline void b_drop(struct buffer **buf)
688{
689 if (!(*buf)->size)
690 return;
691 __b_drop(buf);
692}
693
694/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
695static inline void b_free(struct buffer **buf)
696{
697 b_drop(buf);
698 *buf = &buf_empty;
699}
700
Willy Tarreauf4718e82014-12-02 13:54:01 +0100701/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
702 * there are still at least <margin> buffers available in the pool after this
703 * allocation so that we don't leave the pool in a condition where a session or
704 * a response buffer could not be allocated anymore, resulting in a deadlock.
705 * This means that we sometimes need to try to allocate extra entries even if
706 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100707 *
708 * We need to lock the pool here to be sure to have <margin> buffers available
709 * after the allocation, regardless how many threads that doing it in the same
710 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100711 */
712static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
713{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100714 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100715
716 if ((*buf)->size)
717 return *buf;
718
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100719 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100720#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100721 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100722#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100723
Willy Tarreauf4718e82014-12-02 13:54:01 +0100724 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100725 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
726 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100727 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100728#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100729 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100730#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100731 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100732 b_reset(b);
733 *buf = b;
734 return b;
735 }
736 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100737
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100738 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100739 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100740
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100741#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100742 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100743#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100744
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100745 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100746 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100747 b_reset(b);
748 *buf = b;
749 }
750 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100751}
752
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100753
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100754/* Offer a buffer currently belonging to target <from> to whoever needs one.
755 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
756 * passing a buffer to oneself in case of failed allocations (e.g. need two
757 * buffers, get one, fail, release it and wake up self again). In case of
758 * normal buffer release where it is expected that the caller is not waiting
759 * for a buffer, NULL is fine.
760 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100761void __offer_buffer(void *from, unsigned int threshold);
762
763static inline void offer_buffers(void *from, unsigned int threshold)
764{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100765 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200766 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100767 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100768 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200769 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100770 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100771 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100772}
773
Willy Tarreau6634b632017-09-22 15:02:54 +0200774/*************************************************************************/
775/* functions used to manipulate strings and blocks with wrapping buffers */
776/*************************************************************************/
777
778/* returns > 0 if the first <n> characters of buffer <b> starting at
779 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
780 * designed to be use with reasonably small strings (ie matches a single byte
781 * per iteration). This function is usable both with input and output data. To
782 * be used like this depending on what to match :
783 * - input contents : b_isteq(b, 0, b->i, ist);
784 * - output contents : b_isteq(b, -b->o, b->o, ist);
785 * Return value :
786 * >0 : the number of matching bytes
787 * =0 : not enough bytes (or matching of empty string)
788 * <0 : non-matching byte found
789 */
790static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
791{
792 struct ist r = ist;
793 const char *p;
794 const char *end = b->data + b->size;
795
796 if (n < r.len)
797 return 0;
798
799 p = b_ptr(b, o);
800 while (r.len--) {
801 if (*p++ != *r.ptr++)
802 return -1;
803 if (unlikely(p == end))
804 p = b->data;
805 }
806 return ist.len;
807}
808
809/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
810 * explicitly supported. It matches a single byte per iteration so strings
811 * should remain reasonably small. Returns :
812 * > 0 : number of bytes matched and eaten
813 * = 0 : not enough bytes (or matching an empty string)
814 * < 0 : non-matching byte found
815 */
816static inline int bi_eat(struct buffer *b, const struct ist ist)
817{
818 int ret = b_isteq(b, 0, b->i, ist);
819 if (ret > 0)
820 bi_del(b, ret);
821 return ret;
822}
823
Willy Tarreaue5676e72017-09-22 15:47:51 +0200824/* injects string <ist> into the input region of buffer <b> provided that it
825 * fits. Wrapping is supported. It's designed for small strings as it only
826 * writes a single byte per iteration. Returns the number of characters copied
827 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
828 * will only modify the buffer upon success. In all cases, the contents are
829 * copied prior to reporting an error, so that the destination at least
830 * contains a valid but truncated string.
831 */
832static inline int bi_istput(struct buffer *b, const struct ist ist)
833{
834 const char *end = b->data + b->size;
835 struct ist r = ist;
836 char *p;
837
838 if (r.len > (size_t)(b->size - b->i - b->o))
839 return r.len < b->size ? 0 : -1;
840
841 p = b_ptr(b, b->i);
842 b->i += r.len;
843 while (r.len--) {
844 *p++ = *r.ptr++;
845 if (unlikely(p == end))
846 p = b->data;
847 }
848 return ist.len;
849}
850
851
852/* injects string <ist> into the output region of buffer <b> provided that it
853 * fits. Input data is assumed not to exist and will silently be overwritten.
854 * Wrapping is supported. It's designed for small strings as it only writes a
855 * single byte per iteration. Returns the number of characters copied (ist.len),
856 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
857 * modify the buffer upon success. In all cases, the contents are copied prior
858 * to reporting an error, so that the destination at least contains a valid
859 * but truncated string.
860 */
861static inline int bo_istput(struct buffer *b, const struct ist ist)
862{
863 const char *end = b->data + b->size;
864 struct ist r = ist;
865 char *p;
866
867 if (r.len > (size_t)(b->size - b->o))
868 return r.len < b->size ? 0 : -1;
869
870 p = b->p;
871 b->o += r.len;
872 b->p = b_ptr(b, r.len);
873 while (r.len--) {
874 *p++ = *r.ptr++;
875 if (unlikely(p == end))
876 p = b->data;
877 }
878 return ist.len;
879}
880
881
Willy Tarreauc7e42382012-08-24 19:22:53 +0200882#endif /* _COMMON_BUFFER_H */
883
884/*
885 * Local variables:
886 * c-indent-level: 8
887 * c-basic-offset: 8
888 * End:
889 */