blob: fcb61f3c70641cb88770512b90391c0ec1d58fb6 [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 end of the input data in a buffer (pointer to next
109 * insertion point).
110 */
111static inline char *bi_end(const struct buffer *b)
112{
113 char *ret = b->p + b->i;
114
115 if (ret >= b->data + b->size)
116 ret -= b->size;
117 return ret;
118}
119
Willy Tarreauc7e42382012-08-24 19:22:53 +0200120/* Returns the end of the output data in a buffer */
121static inline char *bo_end(const struct buffer *b)
122{
123 return b->p;
124}
125
126/* Returns the amount of output data that can contiguously be read at once */
127static inline int bo_contig_data(const struct buffer *b)
128{
129 char *beg = b->p - b->o;
130
131 if (beg < b->data)
132 return b->data - beg;
133 return b->o;
134}
135
Christopher Faulet637f8f22017-03-29 11:58:28 +0200136/* Return the amount of bytes that can be written into the input area at once
137 * including reserved space which may be overwritten (this is the caller
138 * responsibility to know if the reserved space is protected or not).
139*/
140static inline int bi_contig_space(const struct buffer *b)
141{
142 const char *left, *right;
143
Christopher Fauleta36b3112017-06-13 22:00:22 +0200144 left = b->p + b->i;
145 right = b->p - b->o;
146 if (left >= b->data + b->size)
147 left -= b->size;
148 else {
149 if (right < b->data)
150 right += b->size;
151 else
152 right = b->data + b->size;
153 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200154 return (right - left);
155}
156
157/* Return the amount of bytes that can be written into the output area at once
158 * including reserved space which may be overwritten (this is the caller
159 * responsibility to know if the reserved space is protected or not). Input data
160 * are assumed to not exist.
161*/
162static inline int bo_contig_space(const struct buffer *b)
163{
164 const char *left, *right;
165
Christopher Fauleta36b3112017-06-13 22:00:22 +0200166 left = b->p;
167 right = b->p - b->o;
168 if (right < b->data)
169 right += b->size;
170 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200171 right = b->data + b->size;
172
173 return (right - left);
174}
175
Willy Tarreauc7e42382012-08-24 19:22:53 +0200176/* Return the buffer's length in bytes by summing the input and the output */
177static inline int buffer_len(const struct buffer *buf)
178{
179 return buf->i + buf->o;
180}
181
182/* Return non-zero only if the buffer is not empty */
183static inline int buffer_not_empty(const struct buffer *buf)
184{
185 return buf->i | buf->o;
186}
187
188/* Return non-zero only if the buffer is empty */
189static inline int buffer_empty(const struct buffer *buf)
190{
191 return !buffer_not_empty(buf);
192}
193
Willy Tarreau42d06662012-08-27 19:51:36 +0200194/* Returns non-zero if the buffer's INPUT is considered full, which means that
195 * it holds at least as much INPUT data as (size - reserve). This also means
196 * that data that are scheduled for output are considered as potential free
197 * space, and that the reserved space is always considered as not usable. This
198 * information alone cannot be used as a general purpose free space indicator.
199 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100200 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200201 * generic function taking everything into account.
202 */
203static inline int buffer_full(const struct buffer *b, unsigned int reserve)
204{
Willy Tarreau4428a292014-11-28 20:54:13 +0100205 if (b == &buf_empty)
206 return 0;
207
Willy Tarreau42d06662012-08-27 19:51:36 +0200208 return (b->i + reserve >= b->size);
209}
210
Willy Tarreauc7e42382012-08-24 19:22:53 +0200211/* Normalizes a pointer after a subtract */
212static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
213{
214 if (ptr < buf->data)
215 ptr += buf->size;
216 return ptr;
217}
218
219/* Normalizes a pointer after an addition */
220static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
221{
222 if (ptr - buf->size >= buf->data)
223 ptr -= buf->size;
224 return ptr;
225}
226
227/* Return the maximum amount of bytes that can be written into the buffer,
228 * including reserved space which may be overwritten.
229 */
230static inline int buffer_total_space(const struct buffer *buf)
231{
232 return buf->size - buffer_len(buf);
233}
234
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100235/* Returns the amount of byte that can be written starting from <p> into the
236 * input buffer at once, including reserved space which may be overwritten.
237 * This is used by Lua to insert data in the input side just before the other
238 * data using buffer_replace(). The goal is to transfer these new data in the
239 * output buffer.
240 */
241static inline int bi_space_for_replace(const struct buffer *buf)
242{
243 const char *end;
244
245 /* If the input side data overflows, we cannot insert data contiguously. */
246 if (buf->p + buf->i >= buf->data + buf->size)
247 return 0;
248
249 /* Check the last byte used in the buffer, it may be a byte of the output
250 * side if the buffer wraps, or its the end of the buffer.
251 */
252 end = buffer_wrap_sub(buf, buf->p - buf->o);
253 if (end <= buf->p)
254 end = buf->data + buf->size;
255
256 /* Compute the amount of bytes which can be written. */
257 return end - (buf->p + buf->i);
258}
259
260
Willy Tarreauc7e42382012-08-24 19:22:53 +0200261/* Normalizes a pointer which is supposed to be relative to the beginning of a
262 * buffer, so that wrapping is correctly handled. The intent is to use this
263 * when increasing a pointer. Note that the wrapping test is only performed
264 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
265 * otherwise an invalid pointer might be returned.
266 */
267static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
268{
269 if (ptr < buf->data)
270 ptr += buf->size;
271 else if (ptr - buf->size >= buf->data)
272 ptr -= buf->size;
273 return ptr;
274}
275
276/* Returns the distance between two pointers, taking into account the ability
277 * to wrap around the buffer's end.
278 */
279static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
280{
281 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200282
283 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200284 return count;
285}
286
287/* returns the amount of pending bytes in the buffer. It is the amount of bytes
288 * that is not scheduled to be sent.
289 */
290static inline int buffer_pending(const struct buffer *buf)
291{
292 return buf->i;
293}
294
Willy Tarreauc7e42382012-08-24 19:22:53 +0200295/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
296static inline int buffer_almost_full(const struct buffer *buf)
297{
Willy Tarreau4428a292014-11-28 20:54:13 +0100298 if (buf == &buf_empty)
299 return 0;
300
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200301 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200302}
303
304/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
305 * call this function with remaining data waiting to be sent (o > 0). The
306 * caller must ensure that <n> is smaller than the actual buffer's length.
307 * This is mainly used to remove empty lines at the beginning of a request
308 * or a response.
309 */
310static inline void bi_fast_delete(struct buffer *buf, int n)
311{
312 buf->i -= n;
313 buf->p += n;
314}
315
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200316/* Schedule all remaining buffer data to be sent. ->o is not touched if it
317 * already covers those data. That permits doing a flush even after a forward,
318 * although not recommended.
319 */
320static inline void buffer_flush(struct buffer *buf)
321{
322 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
323 buf->o += buf->i;
324 buf->i = 0;
325}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200326
Willy Tarreauaf819352012-08-27 22:08:00 +0200327/* This function writes the string <str> at position <pos> which must be in
328 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
329 * (l, r, lr) are updated to be valid after the shift. the shift value
330 * (positive or negative) is returned. If there's no space left, the move is
331 * not done. The function does not adjust ->o because it does not make sense
332 * to use it on data scheduled to be sent.
333 */
334static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
335{
336 return buffer_replace2(b, pos, end, str, strlen(str));
337}
338
Willy Tarreau8c89c202012-09-28 16:02:48 +0200339/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
340 * Data are truncated if buffer is full.
341 */
342static inline void bo_putchr(struct buffer *b, char c)
343{
344 if (buffer_len(b) == b->size)
345 return;
346 *b->p = c;
347 b->p = b_ptr(b, 1);
348 b->o++;
349}
350
351/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100352 * Data are truncated if buffer is too short. It returns the number of bytes
353 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200354 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100355static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200356{
357 int cur_len = buffer_len(b);
358 int half;
359
360 if (len > b->size - cur_len)
361 len = (b->size - cur_len);
362 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100363 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200364
Christopher Faulet637f8f22017-03-29 11:58:28 +0200365 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200366 if (half > len)
367 half = len;
368
369 memcpy(b->p, blk, half);
370 b->p = b_ptr(b, half);
371 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100372 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200373 b->p = b_ptr(b, half);
374 }
375 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100376 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200377}
378
379/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100380 * Data are truncated if buffer is too short. It returns the number of bytes
381 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200382 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100383static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200384{
385 return bo_putblk(b, str, strlen(str));
386}
387
388/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100389 * Data are truncated if buffer is too short. It returns the number of bytes
390 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200391 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100392static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200393{
394 return bo_putblk(b, chk->str, chk->len);
395}
396
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200397/* Gets one full block of data at once from a buffer's output, optionally
398 * starting at a specific offset. Return values :
399 * >0 : number of bytes read, equal to requested size.
400 * =0 : not enough data available. <blk> is left undefined.
401 * The buffer is left unaffected.
402 */
403static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
404{
405 int firstblock;
406
407 if (len + offset > buf->o)
408 return 0;
409
Willy Tarreau89faf5d2018-06-07 18:16:48 +0200410 firstblock = buf->data + buf->size - b_head(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200411 if (firstblock > offset) {
412 if (firstblock >= len + offset) {
Willy Tarreau89faf5d2018-06-07 18:16:48 +0200413 memcpy(blk, b_head(buf) + offset, len);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200414 return len;
415 }
416
Willy Tarreau89faf5d2018-06-07 18:16:48 +0200417 memcpy(blk, b_head(buf) + offset, firstblock - offset);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200418 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
419 return len;
420 }
421
422 memcpy(blk, buf->data + offset - firstblock, len);
423 return len;
424}
425
426/* Gets one or two blocks of data at once from a buffer's output.
427 * Return values :
428 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
429 * =0 : not enough data available. <blk*> are left undefined.
430 * The buffer is left unaffected. Unused buffers are left in an undefined state.
431 */
432static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
433{
434 if (unlikely(buf->o == 0))
435 return 0;
436
Willy Tarreau0621da52017-10-20 18:21:49 +0200437 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200438 *blk1 = buf->p - buf->o + buf->size;
439 *len1 = buf->data + buf->size - *blk1;
440 *blk2 = buf->data;
441 *len2 = buf->p - buf->data;
442 return 2;
443 }
444
Willy Tarreau89faf5d2018-06-07 18:16:48 +0200445 *blk1 = b_head(buf);
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200446 *len1 = buf->o;
447 return 1;
448}
449
Willy Tarreau145746c2017-10-26 15:26:17 +0200450/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
451 * Data are truncated if buffer is full.
452 */
453static inline void bi_putchr(struct buffer *b, char c)
454{
455 if (buffer_len(b) == b->size)
456 return;
457 *bi_end(b) = c;
458 b->i++;
459}
460
461/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
462 * Data are truncated if buffer is too short. It returns the number of bytes
463 * copied.
464 */
465static inline int bi_putblk(struct buffer *b, const char *blk, int len)
466{
467 int cur_len = buffer_len(b);
468 int half;
469
470 if (len > b->size - cur_len)
471 len = (b->size - cur_len);
472 if (!len)
473 return 0;
474
475 half = bi_contig_space(b);
476 if (half > len)
477 half = len;
478
479 memcpy(bi_end(b), blk, half);
480 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100481 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200482 b->i += len;
483 return len;
484}
485
486/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
487 * Data are truncated if buffer is too short. It returns the number of bytes
488 * copied.
489 */
490static inline int bi_putstr(struct buffer *b, const char *str)
491{
492 return bi_putblk(b, str, strlen(str));
493}
494
495/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
496 * Data are truncated if buffer is too short. It returns the number of bytes
497 * copied.
498 */
499static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
500{
501 return bi_putblk(b, chk->str, chk->len);
502}
503
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100504/* Allocates a buffer and replaces *buf with this buffer. If no memory is
505 * available, &buf_wanted is used instead. No control is made to check if *buf
506 * already pointed to another buffer. The allocated buffer is returned, or
507 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100508 */
509static inline struct buffer *b_alloc(struct buffer **buf)
510{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100511 struct buffer *b;
512
513 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100514 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100515 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100516 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100517 b_reset(b);
518 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100519 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100520 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100521}
522
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100523/* Allocates a buffer and replaces *buf with this buffer. If no memory is
524 * available, &buf_wanted is used instead. No control is made to check if *buf
525 * already pointed to another buffer. The allocated buffer is returned, or
526 * NULL in case no memory is available. The difference with b_alloc() is that
527 * this function only picks from the pool and never calls malloc(), so it can
528 * fail even if some memory is available.
529 */
530static inline struct buffer *b_alloc_fast(struct buffer **buf)
531{
532 struct buffer *b;
533
534 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100535 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100536 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100537 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100538 b_reset(b);
539 *buf = b;
540 }
541 return b;
542}
543
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100544/* Releases buffer *buf (no check of emptiness) */
545static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100546{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100547 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100548}
549
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100550/* Releases buffer *buf if allocated. */
551static inline void b_drop(struct buffer **buf)
552{
553 if (!(*buf)->size)
554 return;
555 __b_drop(buf);
556}
557
558/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
559static inline void b_free(struct buffer **buf)
560{
561 b_drop(buf);
562 *buf = &buf_empty;
563}
564
Willy Tarreauf4718e82014-12-02 13:54:01 +0100565/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
566 * there are still at least <margin> buffers available in the pool after this
567 * allocation so that we don't leave the pool in a condition where a session or
568 * a response buffer could not be allocated anymore, resulting in a deadlock.
569 * This means that we sometimes need to try to allocate extra entries even if
570 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100571 *
572 * We need to lock the pool here to be sure to have <margin> buffers available
573 * after the allocation, regardless how many threads that doing it in the same
574 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100575 */
576static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
577{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100578 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100579
580 if ((*buf)->size)
581 return *buf;
582
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100583 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100584#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100585 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100586#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100587
Willy Tarreauf4718e82014-12-02 13:54:01 +0100588 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100589 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
590 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100591 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100592#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100593 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100594#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100595 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100596 b_reset(b);
597 *buf = b;
598 return b;
599 }
600 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100601
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100602 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100603 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100604
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100605#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100606 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100607#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100608
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100609 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100610 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100611 b_reset(b);
612 *buf = b;
613 }
614 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100615}
616
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100617
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100618/* Offer a buffer currently belonging to target <from> to whoever needs one.
619 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
620 * passing a buffer to oneself in case of failed allocations (e.g. need two
621 * buffers, get one, fail, release it and wake up self again). In case of
622 * normal buffer release where it is expected that the caller is not waiting
623 * for a buffer, NULL is fine.
624 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100625void __offer_buffer(void *from, unsigned int threshold);
626
627static inline void offer_buffers(void *from, unsigned int threshold)
628{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100629 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200630 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100632 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200633 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100634 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100635 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100636}
637
Willy Tarreau6634b632017-09-22 15:02:54 +0200638/*************************************************************************/
639/* functions used to manipulate strings and blocks with wrapping buffers */
640/*************************************************************************/
641
642/* returns > 0 if the first <n> characters of buffer <b> starting at
643 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
644 * designed to be use with reasonably small strings (ie matches a single byte
645 * per iteration). This function is usable both with input and output data. To
646 * be used like this depending on what to match :
647 * - input contents : b_isteq(b, 0, b->i, ist);
648 * - output contents : b_isteq(b, -b->o, b->o, ist);
649 * Return value :
650 * >0 : the number of matching bytes
651 * =0 : not enough bytes (or matching of empty string)
652 * <0 : non-matching byte found
653 */
654static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
655{
656 struct ist r = ist;
657 const char *p;
658 const char *end = b->data + b->size;
659
660 if (n < r.len)
661 return 0;
662
663 p = b_ptr(b, o);
664 while (r.len--) {
665 if (*p++ != *r.ptr++)
666 return -1;
667 if (unlikely(p == end))
668 p = b->data;
669 }
670 return ist.len;
671}
672
673/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
674 * explicitly supported. It matches a single byte per iteration so strings
675 * should remain reasonably small. Returns :
676 * > 0 : number of bytes matched and eaten
677 * = 0 : not enough bytes (or matching an empty string)
678 * < 0 : non-matching byte found
679 */
680static inline int bi_eat(struct buffer *b, const struct ist ist)
681{
682 int ret = b_isteq(b, 0, b->i, ist);
683 if (ret > 0)
684 bi_del(b, ret);
685 return ret;
686}
687
Willy Tarreaue5676e72017-09-22 15:47:51 +0200688/* injects string <ist> into the input region of buffer <b> provided that it
689 * fits. Wrapping is supported. It's designed for small strings as it only
690 * writes a single byte per iteration. Returns the number of characters copied
691 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
692 * will only modify the buffer upon success. In all cases, the contents are
693 * copied prior to reporting an error, so that the destination at least
694 * contains a valid but truncated string.
695 */
696static inline int bi_istput(struct buffer *b, const struct ist ist)
697{
698 const char *end = b->data + b->size;
699 struct ist r = ist;
700 char *p;
701
702 if (r.len > (size_t)(b->size - b->i - b->o))
703 return r.len < b->size ? 0 : -1;
704
705 p = b_ptr(b, b->i);
706 b->i += r.len;
707 while (r.len--) {
708 *p++ = *r.ptr++;
709 if (unlikely(p == end))
710 p = b->data;
711 }
712 return ist.len;
713}
714
715
716/* injects string <ist> into the output region of buffer <b> provided that it
717 * fits. Input data is assumed not to exist and will silently be overwritten.
718 * Wrapping is supported. It's designed for small strings as it only writes a
719 * single byte per iteration. Returns the number of characters copied (ist.len),
720 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
721 * modify the buffer upon success. In all cases, the contents are copied prior
722 * to reporting an error, so that the destination at least contains a valid
723 * but truncated string.
724 */
725static inline int bo_istput(struct buffer *b, const struct ist ist)
726{
727 const char *end = b->data + b->size;
728 struct ist r = ist;
729 char *p;
730
731 if (r.len > (size_t)(b->size - b->o))
732 return r.len < b->size ? 0 : -1;
733
734 p = b->p;
735 b->o += r.len;
736 b->p = b_ptr(b, r.len);
737 while (r.len--) {
738 *p++ = *r.ptr++;
739 if (unlikely(p == end))
740 p = b->data;
741 }
742 return ist.len;
743}
744
745
Willy Tarreauc7e42382012-08-24 19:22:53 +0200746#endif /* _COMMON_BUFFER_H */
747
748/*
749 * Local variables:
750 * c-indent-level: 8
751 * c-basic-offset: 8
752 * End:
753 */