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