blob: b34c389224ffd789909f73dd894f499c3473a5e5 [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 Tarreauc7e42382012-08-24 19:22:53 +020055
56/*****************************************************************/
57/* These functions are used to compute various buffer area sizes */
58/*****************************************************************/
59
60/* Returns an absolute pointer for a position relative to the current buffer's
61 * pointer. It is written so that it is optimal when <ofs> is a const. It is
62 * written as a macro instead of an inline function so that the compiler knows
63 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020064 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020065 */
66#define b_ptr(b, ofs) \
67 ({ \
68 char *__ret = (b)->p + (ofs); \
69 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
70 __ret -= (b)->size; \
71 else if ((ofs) < 0 && __ret < (b)->data) \
72 __ret += (b)->size; \
73 __ret; \
74 })
75
Willy Tarreau26488ad2017-09-19 21:14:08 +020076/* Returns the pointer to the buffer's end (data+size) */
77static inline const char *b_end(const struct buffer *b)
78{
79 return b->data + b->size;
80}
81
82/* Returns the distance between <p> and the buffer's end (data+size) */
83static inline unsigned int b_to_end(const struct buffer *b)
84{
85 return b->data + b->size - b->p;
86}
87
Willy Tarreau4a6425d2017-09-19 14:18:46 +020088/* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i>
89 * shrinks by <del> as well, and <o> is left untouched (supposed to be zero).
90 * The caller is responsible for ensuring that <del> is always smaller than or
91 * equal to b->i.
92 */
Willy Tarreau7f564d22017-10-18 08:32:12 +020093static inline void bi_del(struct buffer *b, unsigned int del)
Willy Tarreau4a6425d2017-09-19 14:18:46 +020094{
95 b->i -= del;
96 b->p = b_ptr(b, del);
97}
98
Willy Tarreau7f564d22017-10-18 08:32:12 +020099/* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>.
100 * The caller is responsible for ensuring that <del> is always smaller than or
101 * equal to b->o.
102 */
103static inline void bo_del(struct buffer *b, unsigned int del)
104{
105 b->o -= del;
106}
107
Willy Tarreauc7e42382012-08-24 19:22:53 +0200108/* Returns the start of the input data in a buffer */
109static inline char *bi_ptr(const struct buffer *b)
110{
111 return b->p;
112}
113
114/* Returns the end of the input data in a buffer (pointer to next
115 * insertion point).
116 */
117static inline char *bi_end(const struct buffer *b)
118{
119 char *ret = b->p + b->i;
120
121 if (ret >= b->data + b->size)
122 ret -= b->size;
123 return ret;
124}
125
126/* Returns the amount of input data that can contiguously be read at once */
127static inline int bi_contig_data(const struct buffer *b)
128{
129 int data = b->data + b->size - b->p;
130
131 if (data > b->i)
132 data = b->i;
133 return data;
134}
135
136/* Returns the start of the output data in a buffer */
137static inline char *bo_ptr(const struct buffer *b)
138{
139 char *ret = b->p - b->o;
140
141 if (ret < b->data)
142 ret += b->size;
143 return ret;
144}
145
146/* Returns the end of the output data in a buffer */
147static inline char *bo_end(const struct buffer *b)
148{
149 return b->p;
150}
151
152/* Returns the amount of output data that can contiguously be read at once */
153static inline int bo_contig_data(const struct buffer *b)
154{
155 char *beg = b->p - b->o;
156
157 if (beg < b->data)
158 return b->data - beg;
159 return b->o;
160}
161
Christopher Faulet637f8f22017-03-29 11:58:28 +0200162/* Return the amount of bytes that can be written into the input area at once
163 * including reserved space which may be overwritten (this is the caller
164 * responsibility to know if the reserved space is protected or not).
165*/
166static inline int bi_contig_space(const struct buffer *b)
167{
168 const char *left, *right;
169
Christopher Fauleta36b3112017-06-13 22:00:22 +0200170 left = b->p + b->i;
171 right = b->p - b->o;
172 if (left >= b->data + b->size)
173 left -= b->size;
174 else {
175 if (right < b->data)
176 right += b->size;
177 else
178 right = b->data + b->size;
179 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200180 return (right - left);
181}
182
183/* Return the amount of bytes that can be written into the output area at once
184 * including reserved space which may be overwritten (this is the caller
185 * responsibility to know if the reserved space is protected or not). Input data
186 * are assumed to not exist.
187*/
188static inline int bo_contig_space(const struct buffer *b)
189{
190 const char *left, *right;
191
Christopher Fauleta36b3112017-06-13 22:00:22 +0200192 left = b->p;
193 right = b->p - b->o;
194 if (right < b->data)
195 right += b->size;
196 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200197 right = b->data + b->size;
198
199 return (right - left);
200}
201
Willy Tarreauc7e42382012-08-24 19:22:53 +0200202/* Return the buffer's length in bytes by summing the input and the output */
203static inline int buffer_len(const struct buffer *buf)
204{
205 return buf->i + buf->o;
206}
207
208/* Return non-zero only if the buffer is not empty */
209static inline int buffer_not_empty(const struct buffer *buf)
210{
211 return buf->i | buf->o;
212}
213
214/* Return non-zero only if the buffer is empty */
215static inline int buffer_empty(const struct buffer *buf)
216{
217 return !buffer_not_empty(buf);
218}
219
Willy Tarreau42d06662012-08-27 19:51:36 +0200220/* Returns non-zero if the buffer's INPUT is considered full, which means that
221 * it holds at least as much INPUT data as (size - reserve). This also means
222 * that data that are scheduled for output are considered as potential free
223 * space, and that the reserved space is always considered as not usable. This
224 * information alone cannot be used as a general purpose free space indicator.
225 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100226 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200227 * generic function taking everything into account.
228 */
229static inline int buffer_full(const struct buffer *b, unsigned int reserve)
230{
Willy Tarreau4428a292014-11-28 20:54:13 +0100231 if (b == &buf_empty)
232 return 0;
233
Willy Tarreau42d06662012-08-27 19:51:36 +0200234 return (b->i + reserve >= b->size);
235}
236
Willy Tarreauc7e42382012-08-24 19:22:53 +0200237/* Normalizes a pointer after a subtract */
238static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
239{
240 if (ptr < buf->data)
241 ptr += buf->size;
242 return ptr;
243}
244
245/* Normalizes a pointer after an addition */
246static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
247{
248 if (ptr - buf->size >= buf->data)
249 ptr -= buf->size;
250 return ptr;
251}
252
253/* Return the maximum amount of bytes that can be written into the buffer,
254 * including reserved space which may be overwritten.
255 */
256static inline int buffer_total_space(const struct buffer *buf)
257{
258 return buf->size - buffer_len(buf);
259}
260
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100261/* Returns the amount of byte that can be written starting from <p> into the
262 * input buffer at once, including reserved space which may be overwritten.
263 * This is used by Lua to insert data in the input side just before the other
264 * data using buffer_replace(). The goal is to transfer these new data in the
265 * output buffer.
266 */
267static inline int bi_space_for_replace(const struct buffer *buf)
268{
269 const char *end;
270
271 /* If the input side data overflows, we cannot insert data contiguously. */
272 if (buf->p + buf->i >= buf->data + buf->size)
273 return 0;
274
275 /* Check the last byte used in the buffer, it may be a byte of the output
276 * side if the buffer wraps, or its the end of the buffer.
277 */
278 end = buffer_wrap_sub(buf, buf->p - buf->o);
279 if (end <= buf->p)
280 end = buf->data + buf->size;
281
282 /* Compute the amount of bytes which can be written. */
283 return end - (buf->p + buf->i);
284}
285
286
Willy Tarreauc7e42382012-08-24 19:22:53 +0200287/* Normalizes a pointer which is supposed to be relative to the beginning of a
288 * buffer, so that wrapping is correctly handled. The intent is to use this
289 * when increasing a pointer. Note that the wrapping test is only performed
290 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
291 * otherwise an invalid pointer might be returned.
292 */
293static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
294{
295 if (ptr < buf->data)
296 ptr += buf->size;
297 else if (ptr - buf->size >= buf->data)
298 ptr -= buf->size;
299 return ptr;
300}
301
302/* Returns the distance between two pointers, taking into account the ability
303 * to wrap around the buffer's end.
304 */
305static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
306{
307 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200308
309 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200310 return count;
311}
312
313/* returns the amount of pending bytes in the buffer. It is the amount of bytes
314 * that is not scheduled to be sent.
315 */
316static inline int buffer_pending(const struct buffer *buf)
317{
318 return buf->i;
319}
320
Willy Tarreauc7e42382012-08-24 19:22:53 +0200321/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
322static inline int buffer_almost_full(const struct buffer *buf)
323{
Willy Tarreau4428a292014-11-28 20:54:13 +0100324 if (buf == &buf_empty)
325 return 0;
326
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200327 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200328}
329
330/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
331 * call this function with remaining data waiting to be sent (o > 0). The
332 * caller must ensure that <n> is smaller than the actual buffer's length.
333 * This is mainly used to remove empty lines at the beginning of a request
334 * or a response.
335 */
336static inline void bi_fast_delete(struct buffer *buf, int n)
337{
338 buf->i -= n;
339 buf->p += n;
340}
341
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200342/* Schedule all remaining buffer data to be sent. ->o is not touched if it
343 * already covers those data. That permits doing a flush even after a forward,
344 * although not recommended.
345 */
346static inline void buffer_flush(struct buffer *buf)
347{
348 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
349 buf->o += buf->i;
350 buf->i = 0;
351}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200352
Willy Tarreauaf819352012-08-27 22:08:00 +0200353/* This function writes the string <str> at position <pos> which must be in
354 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
355 * (l, r, lr) are updated to be valid after the shift. the shift value
356 * (positive or negative) is returned. If there's no space left, the move is
357 * not done. The function does not adjust ->o because it does not make sense
358 * to use it on data scheduled to be sent.
359 */
360static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
361{
362 return buffer_replace2(b, pos, end, str, strlen(str));
363}
364
Willy Tarreau8c89c202012-09-28 16:02:48 +0200365/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
366 * Data are truncated if buffer is full.
367 */
368static inline void bo_putchr(struct buffer *b, char c)
369{
370 if (buffer_len(b) == b->size)
371 return;
372 *b->p = c;
373 b->p = b_ptr(b, 1);
374 b->o++;
375}
376
377/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100378 * Data are truncated if buffer is too short. It returns the number of bytes
379 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200380 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100381static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200382{
383 int cur_len = buffer_len(b);
384 int half;
385
386 if (len > b->size - cur_len)
387 len = (b->size - cur_len);
388 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100389 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200390
Christopher Faulet637f8f22017-03-29 11:58:28 +0200391 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200392 if (half > len)
393 half = len;
394
395 memcpy(b->p, blk, half);
396 b->p = b_ptr(b, half);
397 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100398 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200399 b->p = b_ptr(b, half);
400 }
401 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100402 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200403}
404
405/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100406 * Data are truncated if buffer is too short. It returns the number of bytes
407 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200408 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100409static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200410{
411 return bo_putblk(b, str, strlen(str));
412}
413
414/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100415 * Data are truncated if buffer is too short. It returns the number of bytes
416 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200417 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100418static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200419{
420 return bo_putblk(b, chk->str, chk->len);
421}
422
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200423/* Gets one full block of data at once from a buffer's output, optionally
424 * starting at a specific offset. Return values :
425 * >0 : number of bytes read, equal to requested size.
426 * =0 : not enough data available. <blk> is left undefined.
427 * The buffer is left unaffected.
428 */
429static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
430{
431 int firstblock;
432
433 if (len + offset > buf->o)
434 return 0;
435
436 firstblock = buf->data + buf->size - bo_ptr(buf);
437 if (firstblock > offset) {
438 if (firstblock >= len + offset) {
439 memcpy(blk, bo_ptr(buf) + offset, len);
440 return len;
441 }
442
443 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
444 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
445 return len;
446 }
447
448 memcpy(blk, buf->data + offset - firstblock, len);
449 return len;
450}
451
452/* Gets one or two blocks of data at once from a buffer's output.
453 * Return values :
454 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
455 * =0 : not enough data available. <blk*> are left undefined.
456 * The buffer is left unaffected. Unused buffers are left in an undefined state.
457 */
458static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
459{
460 if (unlikely(buf->o == 0))
461 return 0;
462
Willy Tarreau0621da52017-10-20 18:21:49 +0200463 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200464 *blk1 = buf->p - buf->o + buf->size;
465 *len1 = buf->data + buf->size - *blk1;
466 *blk2 = buf->data;
467 *len2 = buf->p - buf->data;
468 return 2;
469 }
470
Willy Tarreau4b75fff2017-11-02 17:16:07 +0100471 *blk1 = bo_ptr(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200472 *len1 = buf->o;
473 return 1;
474}
475
Willy Tarreau145746c2017-10-26 15:26:17 +0200476/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
477 * Data are truncated if buffer is full.
478 */
479static inline void bi_putchr(struct buffer *b, char c)
480{
481 if (buffer_len(b) == b->size)
482 return;
483 *bi_end(b) = c;
484 b->i++;
485}
486
487/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
488 * Data are truncated if buffer is too short. It returns the number of bytes
489 * copied.
490 */
491static inline int bi_putblk(struct buffer *b, const char *blk, int len)
492{
493 int cur_len = buffer_len(b);
494 int half;
495
496 if (len > b->size - cur_len)
497 len = (b->size - cur_len);
498 if (!len)
499 return 0;
500
501 half = bi_contig_space(b);
502 if (half > len)
503 half = len;
504
505 memcpy(bi_end(b), blk, half);
506 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100507 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200508 b->i += len;
509 return len;
510}
511
512/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
513 * Data are truncated if buffer is too short. It returns the number of bytes
514 * copied.
515 */
516static inline int bi_putstr(struct buffer *b, const char *str)
517{
518 return bi_putblk(b, str, strlen(str));
519}
520
521/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
522 * Data are truncated if buffer is too short. It returns the number of bytes
523 * copied.
524 */
525static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
526{
527 return bi_putblk(b, chk->str, chk->len);
528}
529
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100530/* Allocates a buffer and replaces *buf with this buffer. If no memory is
531 * available, &buf_wanted is used instead. No control is made to check if *buf
532 * already pointed to another buffer. The allocated buffer is returned, or
533 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100534 */
535static inline struct buffer *b_alloc(struct buffer **buf)
536{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100537 struct buffer *b;
538
539 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100540 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100541 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100542 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100543 b_reset(b);
544 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100545 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100546 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100547}
548
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100549/* Allocates a buffer and replaces *buf with this buffer. If no memory is
550 * available, &buf_wanted is used instead. No control is made to check if *buf
551 * already pointed to another buffer. The allocated buffer is returned, or
552 * NULL in case no memory is available. The difference with b_alloc() is that
553 * this function only picks from the pool and never calls malloc(), so it can
554 * fail even if some memory is available.
555 */
556static inline struct buffer *b_alloc_fast(struct buffer **buf)
557{
558 struct buffer *b;
559
560 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100561 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100562 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100563 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100564 b_reset(b);
565 *buf = b;
566 }
567 return b;
568}
569
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100570/* Releases buffer *buf (no check of emptiness) */
571static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100572{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100573 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100574}
575
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100576/* Releases buffer *buf if allocated. */
577static inline void b_drop(struct buffer **buf)
578{
579 if (!(*buf)->size)
580 return;
581 __b_drop(buf);
582}
583
584/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
585static inline void b_free(struct buffer **buf)
586{
587 b_drop(buf);
588 *buf = &buf_empty;
589}
590
Willy Tarreauf4718e82014-12-02 13:54:01 +0100591/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
592 * there are still at least <margin> buffers available in the pool after this
593 * allocation so that we don't leave the pool in a condition where a session or
594 * a response buffer could not be allocated anymore, resulting in a deadlock.
595 * This means that we sometimes need to try to allocate extra entries even if
596 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100597 *
598 * We need to lock the pool here to be sure to have <margin> buffers available
599 * after the allocation, regardless how many threads that doing it in the same
600 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100601 */
602static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
603{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100604 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100605
606 if ((*buf)->size)
607 return *buf;
608
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100609 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100610#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100611 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100612#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100613
Willy Tarreauf4718e82014-12-02 13:54:01 +0100614 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100615 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
616 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100617 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100618#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100619 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100620#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100621 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100622 b_reset(b);
623 *buf = b;
624 return b;
625 }
626 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100627
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100628 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100629 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100630
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100631#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100632 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100633#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100634
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100635 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100636 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100637 b_reset(b);
638 *buf = b;
639 }
640 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100641}
642
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100643
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100644/* Offer a buffer currently belonging to target <from> to whoever needs one.
645 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
646 * passing a buffer to oneself in case of failed allocations (e.g. need two
647 * buffers, get one, fail, release it and wake up self again). In case of
648 * normal buffer release where it is expected that the caller is not waiting
649 * for a buffer, NULL is fine.
650 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100651void __offer_buffer(void *from, unsigned int threshold);
652
653static inline void offer_buffers(void *from, unsigned int threshold)
654{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100655 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200656 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100657 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100658 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200659 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100660 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100661 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100662}
663
Willy Tarreau6634b632017-09-22 15:02:54 +0200664/*************************************************************************/
665/* functions used to manipulate strings and blocks with wrapping buffers */
666/*************************************************************************/
667
668/* returns > 0 if the first <n> characters of buffer <b> starting at
669 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
670 * designed to be use with reasonably small strings (ie matches a single byte
671 * per iteration). This function is usable both with input and output data. To
672 * be used like this depending on what to match :
673 * - input contents : b_isteq(b, 0, b->i, ist);
674 * - output contents : b_isteq(b, -b->o, b->o, ist);
675 * Return value :
676 * >0 : the number of matching bytes
677 * =0 : not enough bytes (or matching of empty string)
678 * <0 : non-matching byte found
679 */
680static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
681{
682 struct ist r = ist;
683 const char *p;
684 const char *end = b->data + b->size;
685
686 if (n < r.len)
687 return 0;
688
689 p = b_ptr(b, o);
690 while (r.len--) {
691 if (*p++ != *r.ptr++)
692 return -1;
693 if (unlikely(p == end))
694 p = b->data;
695 }
696 return ist.len;
697}
698
699/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
700 * explicitly supported. It matches a single byte per iteration so strings
701 * should remain reasonably small. Returns :
702 * > 0 : number of bytes matched and eaten
703 * = 0 : not enough bytes (or matching an empty string)
704 * < 0 : non-matching byte found
705 */
706static inline int bi_eat(struct buffer *b, const struct ist ist)
707{
708 int ret = b_isteq(b, 0, b->i, ist);
709 if (ret > 0)
710 bi_del(b, ret);
711 return ret;
712}
713
Willy Tarreaue5676e72017-09-22 15:47:51 +0200714/* injects string <ist> into the input region of buffer <b> provided that it
715 * fits. Wrapping is supported. It's designed for small strings as it only
716 * writes a single byte per iteration. Returns the number of characters copied
717 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
718 * will only modify the buffer upon success. In all cases, the contents are
719 * copied prior to reporting an error, so that the destination at least
720 * contains a valid but truncated string.
721 */
722static inline int bi_istput(struct buffer *b, const struct ist ist)
723{
724 const char *end = b->data + b->size;
725 struct ist r = ist;
726 char *p;
727
728 if (r.len > (size_t)(b->size - b->i - b->o))
729 return r.len < b->size ? 0 : -1;
730
731 p = b_ptr(b, b->i);
732 b->i += r.len;
733 while (r.len--) {
734 *p++ = *r.ptr++;
735 if (unlikely(p == end))
736 p = b->data;
737 }
738 return ist.len;
739}
740
741
742/* injects string <ist> into the output region of buffer <b> provided that it
743 * fits. Input data is assumed not to exist and will silently be overwritten.
744 * Wrapping is supported. It's designed for small strings as it only writes a
745 * single byte per iteration. Returns the number of characters copied (ist.len),
746 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
747 * modify the buffer upon success. In all cases, the contents are copied prior
748 * to reporting an error, so that the destination at least contains a valid
749 * but truncated string.
750 */
751static inline int bo_istput(struct buffer *b, const struct ist ist)
752{
753 const char *end = b->data + b->size;
754 struct ist r = ist;
755 char *p;
756
757 if (r.len > (size_t)(b->size - b->o))
758 return r.len < b->size ? 0 : -1;
759
760 p = b->p;
761 b->o += r.len;
762 b->p = b_ptr(b, r.len);
763 while (r.len--) {
764 *p++ = *r.ptr++;
765 if (unlikely(p == end))
766 p = b->data;
767 }
768 return ist.len;
769}
770
771
Willy Tarreauc7e42382012-08-24 19:22:53 +0200772#endif /* _COMMON_BUFFER_H */
773
774/*
775 * Local variables:
776 * c-indent-level: 8
777 * c-basic-offset: 8
778 * End:
779 */