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