Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 29 | #include <common/buf.h> |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 31 | #include <common/config.h> |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 32 | #include <common/ist.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 34 | |
| 35 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 36 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 37 | * acquire a buffer to continue its process. */ |
| 38 | struct 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 Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 44 | extern struct pool_head *pool_head_buffer; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 45 | extern struct buffer buf_empty; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 46 | extern struct buffer buf_wanted; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 47 | extern struct list buffer_wq; |
Willy Tarreau | 53bae85 | 2017-11-26 11:00:37 +0100 | [diff] [blame] | 48 | __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 50 | int init_buffer(); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 51 | void deinit_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 52 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 53 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 54 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 55 | |
| 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 Tarreau | ce39bfb | 2012-09-22 18:36:29 +0200 | [diff] [blame] | 64 | * Note that callers MUST cast <ofs> to int if they expect negative values. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 65 | */ |
| 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 Tarreau | 26488ad | 2017-09-19 21:14:08 +0200 | [diff] [blame] | 76 | /* Returns the pointer to the buffer's end (data+size) */ |
| 77 | static 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) */ |
| 83 | static inline unsigned int b_to_end(const struct buffer *b) |
| 84 | { |
| 85 | return b->data + b->size - b->p; |
| 86 | } |
| 87 | |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 88 | /* 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 Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 93 | static inline void bi_del(struct buffer *b, unsigned int del) |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 94 | { |
| 95 | b->i -= del; |
| 96 | b->p = b_ptr(b, del); |
| 97 | } |
| 98 | |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 99 | /* 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 | */ |
| 103 | static inline void bo_del(struct buffer *b, unsigned int del) |
| 104 | { |
| 105 | b->o -= del; |
| 106 | } |
| 107 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 108 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 109 | static inline int buffer_len(const struct buffer *buf) |
| 110 | { |
| 111 | return buf->i + buf->o; |
| 112 | } |
| 113 | |
| 114 | /* Return non-zero only if the buffer is not empty */ |
| 115 | static inline int buffer_not_empty(const struct buffer *buf) |
| 116 | { |
| 117 | return buf->i | buf->o; |
| 118 | } |
| 119 | |
| 120 | /* Return non-zero only if the buffer is empty */ |
| 121 | static inline int buffer_empty(const struct buffer *buf) |
| 122 | { |
| 123 | return !buffer_not_empty(buf); |
| 124 | } |
| 125 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 126 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 127 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 128 | * that data that are scheduled for output are considered as potential free |
| 129 | * space, and that the reserved space is always considered as not usable. This |
| 130 | * information alone cannot be used as a general purpose free space indicator. |
| 131 | * However it accurately indicates that too many data were fed in the buffer |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 132 | * for an analyzer for instance. See the channel_may_recv() function for a more |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 133 | * generic function taking everything into account. |
| 134 | */ |
| 135 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 136 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 137 | if (b == &buf_empty) |
| 138 | return 0; |
| 139 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 140 | return (b->i + reserve >= b->size); |
| 141 | } |
| 142 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 143 | /* Normalizes a pointer after a subtract */ |
| 144 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 145 | { |
| 146 | if (ptr < buf->data) |
| 147 | ptr += buf->size; |
| 148 | return ptr; |
| 149 | } |
| 150 | |
| 151 | /* Normalizes a pointer after an addition */ |
| 152 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 153 | { |
| 154 | if (ptr - buf->size >= buf->data) |
| 155 | ptr -= buf->size; |
| 156 | return ptr; |
| 157 | } |
| 158 | |
| 159 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 160 | * including reserved space which may be overwritten. |
| 161 | */ |
| 162 | static inline int buffer_total_space(const struct buffer *buf) |
| 163 | { |
| 164 | return buf->size - buffer_len(buf); |
| 165 | } |
| 166 | |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 167 | /* Returns the amount of byte that can be written starting from <p> into the |
| 168 | * input buffer at once, including reserved space which may be overwritten. |
| 169 | * This is used by Lua to insert data in the input side just before the other |
| 170 | * data using buffer_replace(). The goal is to transfer these new data in the |
| 171 | * output buffer. |
| 172 | */ |
| 173 | static inline int bi_space_for_replace(const struct buffer *buf) |
| 174 | { |
| 175 | const char *end; |
| 176 | |
| 177 | /* If the input side data overflows, we cannot insert data contiguously. */ |
| 178 | if (buf->p + buf->i >= buf->data + buf->size) |
| 179 | return 0; |
| 180 | |
| 181 | /* Check the last byte used in the buffer, it may be a byte of the output |
| 182 | * side if the buffer wraps, or its the end of the buffer. |
| 183 | */ |
| 184 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 185 | if (end <= buf->p) |
| 186 | end = buf->data + buf->size; |
| 187 | |
| 188 | /* Compute the amount of bytes which can be written. */ |
| 189 | return end - (buf->p + buf->i); |
| 190 | } |
| 191 | |
| 192 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 193 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 194 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 195 | * when increasing a pointer. Note that the wrapping test is only performed |
| 196 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 197 | * otherwise an invalid pointer might be returned. |
| 198 | */ |
| 199 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 200 | { |
| 201 | if (ptr < buf->data) |
| 202 | ptr += buf->size; |
| 203 | else if (ptr - buf->size >= buf->data) |
| 204 | ptr -= buf->size; |
| 205 | return ptr; |
| 206 | } |
| 207 | |
| 208 | /* Returns the distance between two pointers, taking into account the ability |
| 209 | * to wrap around the buffer's end. |
| 210 | */ |
| 211 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 212 | { |
| 213 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 214 | |
| 215 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 216 | return count; |
| 217 | } |
| 218 | |
| 219 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 220 | * that is not scheduled to be sent. |
| 221 | */ |
| 222 | static inline int buffer_pending(const struct buffer *buf) |
| 223 | { |
| 224 | return buf->i; |
| 225 | } |
| 226 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 227 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 228 | static inline int buffer_almost_full(const struct buffer *buf) |
| 229 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 230 | if (buf == &buf_empty) |
| 231 | return 0; |
| 232 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 233 | return b_almost_full(buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 237 | * call this function with remaining data waiting to be sent (o > 0). The |
| 238 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 239 | * This is mainly used to remove empty lines at the beginning of a request |
| 240 | * or a response. |
| 241 | */ |
| 242 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 243 | { |
| 244 | buf->i -= n; |
| 245 | buf->p += n; |
| 246 | } |
| 247 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 248 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 249 | * already covers those data. That permits doing a flush even after a forward, |
| 250 | * although not recommended. |
| 251 | */ |
| 252 | static inline void buffer_flush(struct buffer *buf) |
| 253 | { |
| 254 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 255 | buf->o += buf->i; |
| 256 | buf->i = 0; |
| 257 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 259 | /* This function writes the string <str> at position <pos> which must be in |
| 260 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 261 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 262 | * (positive or negative) is returned. If there's no space left, the move is |
| 263 | * not done. The function does not adjust ->o because it does not make sense |
| 264 | * to use it on data scheduled to be sent. |
| 265 | */ |
| 266 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 267 | { |
| 268 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 269 | } |
| 270 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 271 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 272 | * Data are truncated if buffer is full. |
| 273 | */ |
| 274 | static inline void bo_putchr(struct buffer *b, char c) |
| 275 | { |
| 276 | if (buffer_len(b) == b->size) |
| 277 | return; |
| 278 | *b->p = c; |
| 279 | b->p = b_ptr(b, 1); |
| 280 | b->o++; |
| 281 | } |
| 282 | |
| 283 | /* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 284 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 285 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 286 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 287 | static inline int bo_putblk(struct buffer *b, const char *blk, int len) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 288 | { |
| 289 | int cur_len = buffer_len(b); |
| 290 | int half; |
| 291 | |
| 292 | if (len > b->size - cur_len) |
| 293 | len = (b->size - cur_len); |
| 294 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 295 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 296 | |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 297 | half = b_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 298 | if (half > len) |
| 299 | half = len; |
| 300 | |
| 301 | memcpy(b->p, blk, half); |
| 302 | b->p = b_ptr(b, half); |
| 303 | if (len > half) { |
Christopher Faulet | b2b2794 | 2018-02-26 10:47:03 +0100 | [diff] [blame] | 304 | memcpy(b->p, blk + half, len - half); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 305 | b->p = b_ptr(b, half); |
| 306 | } |
| 307 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 308 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Tries to copy string <str> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 312 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 313 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 314 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 315 | static inline int bo_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 316 | { |
| 317 | return bo_putblk(b, str, strlen(str)); |
| 318 | } |
| 319 | |
| 320 | /* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 321 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 322 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 323 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 324 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 325 | { |
| 326 | return bo_putblk(b, chk->str, chk->len); |
| 327 | } |
| 328 | |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 329 | /* Tries to write char <c> into input data at buffer <b>. Supports wrapping. |
| 330 | * Data are truncated if buffer is full. |
| 331 | */ |
| 332 | static inline void bi_putchr(struct buffer *b, char c) |
| 333 | { |
| 334 | if (buffer_len(b) == b->size) |
| 335 | return; |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 336 | *b_tail(b) = c; |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 337 | b->i++; |
| 338 | } |
| 339 | |
| 340 | /* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping. |
| 341 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 342 | * copied. |
| 343 | */ |
| 344 | static inline int bi_putblk(struct buffer *b, const char *blk, int len) |
| 345 | { |
| 346 | int cur_len = buffer_len(b); |
| 347 | int half; |
| 348 | |
| 349 | if (len > b->size - cur_len) |
| 350 | len = (b->size - cur_len); |
| 351 | if (!len) |
| 352 | return 0; |
| 353 | |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 354 | half = b_contig_space(b); |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 355 | if (half > len) |
| 356 | half = len; |
| 357 | |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 358 | memcpy(b_tail(b), blk, half); |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 359 | if (len > half) |
Christopher Faulet | ca6ef50 | 2018-02-26 10:51:28 +0100 | [diff] [blame] | 360 | memcpy(b_ptr(b, b->i + half), blk + half, len - half); |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 361 | b->i += len; |
| 362 | return len; |
| 363 | } |
| 364 | |
| 365 | /* Tries to copy string <str> into input data at buffer <b>. Supports wrapping. |
| 366 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 367 | * copied. |
| 368 | */ |
| 369 | static inline int bi_putstr(struct buffer *b, const char *str) |
| 370 | { |
| 371 | return bi_putblk(b, str, strlen(str)); |
| 372 | } |
| 373 | |
| 374 | /* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping. |
| 375 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 376 | * copied. |
| 377 | */ |
| 378 | static inline int bi_putchk(struct buffer *b, const struct chunk *chk) |
| 379 | { |
| 380 | return bi_putblk(b, chk->str, chk->len); |
| 381 | } |
| 382 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 383 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 384 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 385 | * already pointed to another buffer. The allocated buffer is returned, or |
| 386 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 387 | */ |
| 388 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 389 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 390 | struct buffer *b; |
| 391 | |
| 392 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 393 | b = pool_alloc_dirty(pool_head_buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 394 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 395 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 396 | b_reset(b); |
| 397 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 398 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 399 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 402 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 403 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 404 | * already pointed to another buffer. The allocated buffer is returned, or |
| 405 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 406 | * this function only picks from the pool and never calls malloc(), so it can |
| 407 | * fail even if some memory is available. |
| 408 | */ |
| 409 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 410 | { |
| 411 | struct buffer *b; |
| 412 | |
| 413 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 414 | b = pool_get_first(pool_head_buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 415 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 416 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 417 | b_reset(b); |
| 418 | *buf = b; |
| 419 | } |
| 420 | return b; |
| 421 | } |
| 422 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 423 | /* Releases buffer *buf (no check of emptiness) */ |
| 424 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 425 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 426 | pool_free(pool_head_buffer, *buf); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 429 | /* Releases buffer *buf if allocated. */ |
| 430 | static inline void b_drop(struct buffer **buf) |
| 431 | { |
| 432 | if (!(*buf)->size) |
| 433 | return; |
| 434 | __b_drop(buf); |
| 435 | } |
| 436 | |
| 437 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 438 | static inline void b_free(struct buffer **buf) |
| 439 | { |
| 440 | b_drop(buf); |
| 441 | *buf = &buf_empty; |
| 442 | } |
| 443 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 444 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 445 | * there are still at least <margin> buffers available in the pool after this |
| 446 | * allocation so that we don't leave the pool in a condition where a session or |
| 447 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 448 | * This means that we sometimes need to try to allocate extra entries even if |
| 449 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 450 | * |
| 451 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 452 | * after the allocation, regardless how many threads that doing it in the same |
| 453 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 454 | */ |
| 455 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 456 | { |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 457 | struct buffer *b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 458 | |
| 459 | if ((*buf)->size) |
| 460 | return *buf; |
| 461 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 462 | *buf = &buf_wanted; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 463 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 464 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 465 | #endif |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 466 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 467 | /* fast path */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 468 | if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) { |
| 469 | b = __pool_get_first(pool_head_buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 470 | if (likely(b)) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 471 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 472 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 473 | #endif |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 474 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 475 | b_reset(b); |
| 476 | *buf = b; |
| 477 | return b; |
| 478 | } |
| 479 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 480 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 481 | /* slow path, uses malloc() */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 482 | b = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 483 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 484 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 485 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 486 | #endif |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 487 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 488 | if (b) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 489 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 490 | b_reset(b); |
| 491 | *buf = b; |
| 492 | } |
| 493 | return b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 496 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 497 | /* Offer a buffer currently belonging to target <from> to whoever needs one. |
| 498 | * Any pointer is valid for <from>, including NULL. Its purpose is to avoid |
| 499 | * passing a buffer to oneself in case of failed allocations (e.g. need two |
| 500 | * buffers, get one, fail, release it and wake up self again). In case of |
| 501 | * normal buffer release where it is expected that the caller is not waiting |
| 502 | * for a buffer, NULL is fine. |
| 503 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 504 | void __offer_buffer(void *from, unsigned int threshold); |
| 505 | |
| 506 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 507 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 508 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 509 | if (LIST_ISEMPTY(&buffer_wq)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 510 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 511 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 512 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 513 | __offer_buffer(from, threshold); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 514 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 517 | /*************************************************************************/ |
| 518 | /* functions used to manipulate strings and blocks with wrapping buffers */ |
| 519 | /*************************************************************************/ |
| 520 | |
| 521 | /* returns > 0 if the first <n> characters of buffer <b> starting at |
| 522 | * offset <o> relative to b->p match <ist>. (empty strings do match). It is |
| 523 | * designed to be use with reasonably small strings (ie matches a single byte |
| 524 | * per iteration). This function is usable both with input and output data. To |
| 525 | * be used like this depending on what to match : |
| 526 | * - input contents : b_isteq(b, 0, b->i, ist); |
| 527 | * - output contents : b_isteq(b, -b->o, b->o, ist); |
| 528 | * Return value : |
| 529 | * >0 : the number of matching bytes |
| 530 | * =0 : not enough bytes (or matching of empty string) |
| 531 | * <0 : non-matching byte found |
| 532 | */ |
| 533 | static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) |
| 534 | { |
| 535 | struct ist r = ist; |
| 536 | const char *p; |
| 537 | const char *end = b->data + b->size; |
| 538 | |
| 539 | if (n < r.len) |
| 540 | return 0; |
| 541 | |
| 542 | p = b_ptr(b, o); |
| 543 | while (r.len--) { |
| 544 | if (*p++ != *r.ptr++) |
| 545 | return -1; |
| 546 | if (unlikely(p == end)) |
| 547 | p = b->data; |
| 548 | } |
| 549 | return ist.len; |
| 550 | } |
| 551 | |
| 552 | /* "eats" string <ist> from the input region of buffer <b>. Wrapping data is |
| 553 | * explicitly supported. It matches a single byte per iteration so strings |
| 554 | * should remain reasonably small. Returns : |
| 555 | * > 0 : number of bytes matched and eaten |
| 556 | * = 0 : not enough bytes (or matching an empty string) |
| 557 | * < 0 : non-matching byte found |
| 558 | */ |
| 559 | static inline int bi_eat(struct buffer *b, const struct ist ist) |
| 560 | { |
| 561 | int ret = b_isteq(b, 0, b->i, ist); |
| 562 | if (ret > 0) |
| 563 | bi_del(b, ret); |
| 564 | return ret; |
| 565 | } |
| 566 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 567 | /* injects string <ist> into the input region of buffer <b> provided that it |
| 568 | * fits. Wrapping is supported. It's designed for small strings as it only |
| 569 | * writes a single byte per iteration. Returns the number of characters copied |
| 570 | * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It |
| 571 | * will only modify the buffer upon success. In all cases, the contents are |
| 572 | * copied prior to reporting an error, so that the destination at least |
| 573 | * contains a valid but truncated string. |
| 574 | */ |
| 575 | static inline int bi_istput(struct buffer *b, const struct ist ist) |
| 576 | { |
| 577 | const char *end = b->data + b->size; |
| 578 | struct ist r = ist; |
| 579 | char *p; |
| 580 | |
| 581 | if (r.len > (size_t)(b->size - b->i - b->o)) |
| 582 | return r.len < b->size ? 0 : -1; |
| 583 | |
| 584 | p = b_ptr(b, b->i); |
| 585 | b->i += r.len; |
| 586 | while (r.len--) { |
| 587 | *p++ = *r.ptr++; |
| 588 | if (unlikely(p == end)) |
| 589 | p = b->data; |
| 590 | } |
| 591 | return ist.len; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /* injects string <ist> into the output region of buffer <b> provided that it |
| 596 | * fits. Input data is assumed not to exist and will silently be overwritten. |
| 597 | * Wrapping is supported. It's designed for small strings as it only writes a |
| 598 | * single byte per iteration. Returns the number of characters copied (ist.len), |
| 599 | * 0 if it temporarily does not fit or -1 if it will never fit. It will only |
| 600 | * modify the buffer upon success. In all cases, the contents are copied prior |
| 601 | * to reporting an error, so that the destination at least contains a valid |
| 602 | * but truncated string. |
| 603 | */ |
| 604 | static inline int bo_istput(struct buffer *b, const struct ist ist) |
| 605 | { |
| 606 | const char *end = b->data + b->size; |
| 607 | struct ist r = ist; |
| 608 | char *p; |
| 609 | |
| 610 | if (r.len > (size_t)(b->size - b->o)) |
| 611 | return r.len < b->size ? 0 : -1; |
| 612 | |
| 613 | p = b->p; |
| 614 | b->o += r.len; |
| 615 | b->p = b_ptr(b, r.len); |
| 616 | while (r.len--) { |
| 617 | *p++ = *r.ptr++; |
| 618 | if (unlikely(p == end)) |
| 619 | p = b->data; |
| 620 | } |
| 621 | return ist.len; |
| 622 | } |
| 623 | |
| 624 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 625 | #endif /* _COMMON_BUFFER_H */ |
| 626 | |
| 627 | /* |
| 628 | * Local variables: |
| 629 | * c-indent-level: 8 |
| 630 | * c-basic-offset: 8 |
| 631 | * End: |
| 632 | */ |