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 | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 31 | #include <common/ist.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 32 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | struct buffer { |
| 36 | char *p; /* buffer's start pointer, separates in and out data */ |
| 37 | unsigned int size; /* buffer size in bytes */ |
| 38 | unsigned int i; /* number of input bytes pending for analysis in the buffer */ |
| 39 | unsigned int o; /* number of out bytes the sender can consume from this buffer */ |
| 40 | char data[0]; /* <size> bytes */ |
| 41 | }; |
| 42 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 43 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 44 | * acquire a buffer to continue its process. */ |
| 45 | struct buffer_wait { |
| 46 | void *target; /* The waiting object that should be woken up */ |
| 47 | int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */ |
| 48 | struct list list; /* Next element in the <buffer_wq> list */ |
| 49 | }; |
| 50 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 51 | extern struct pool_head *pool2_buffer; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 52 | extern struct buffer buf_empty; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 53 | extern struct buffer buf_wanted; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 54 | extern struct list buffer_wq; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 55 | #ifdef USE_THREAD |
| 56 | extern HA_SPINLOCK_T buffer_wq_lock; |
| 57 | #endif |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 59 | int init_buffer(); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 60 | void deinit_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 61 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 62 | 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] | 63 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 64 | void buffer_slow_realign(struct buffer *buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 65 | |
| 66 | /*****************************************************************/ |
| 67 | /* These functions are used to compute various buffer area sizes */ |
| 68 | /*****************************************************************/ |
| 69 | |
| 70 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 71 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 72 | * written as a macro instead of an inline function so that the compiler knows |
| 73 | * 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] | 74 | * 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] | 75 | */ |
| 76 | #define b_ptr(b, ofs) \ |
| 77 | ({ \ |
| 78 | char *__ret = (b)->p + (ofs); \ |
| 79 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 80 | __ret -= (b)->size; \ |
| 81 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 82 | __ret += (b)->size; \ |
| 83 | __ret; \ |
| 84 | }) |
| 85 | |
Willy Tarreau | 26488ad | 2017-09-19 21:14:08 +0200 | [diff] [blame] | 86 | /* Returns the pointer to the buffer's end (data+size) */ |
| 87 | static inline const char *b_end(const struct buffer *b) |
| 88 | { |
| 89 | return b->data + b->size; |
| 90 | } |
| 91 | |
| 92 | /* Returns the distance between <p> and the buffer's end (data+size) */ |
| 93 | static inline unsigned int b_to_end(const struct buffer *b) |
| 94 | { |
| 95 | return b->data + b->size - b->p; |
| 96 | } |
| 97 | |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 98 | /* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i> |
| 99 | * shrinks by <del> as well, and <o> is left untouched (supposed to be zero). |
| 100 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 101 | * equal to b->i. |
| 102 | */ |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 103 | static inline void bi_del(struct buffer *b, unsigned int del) |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 104 | { |
| 105 | b->i -= del; |
| 106 | b->p = b_ptr(b, del); |
| 107 | } |
| 108 | |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 109 | /* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>. |
| 110 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 111 | * equal to b->o. |
| 112 | */ |
| 113 | static inline void bo_del(struct buffer *b, unsigned int del) |
| 114 | { |
| 115 | b->o -= del; |
| 116 | } |
| 117 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 118 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 119 | * pointer advances, and that as many bytes from in are transferred |
| 120 | * to out. The caller is responsible for ensuring that adv is always |
| 121 | * smaller than or equal to b->i. |
| 122 | */ |
| 123 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 124 | { |
| 125 | b->i -= adv; |
| 126 | b->o += adv; |
| 127 | b->p = b_ptr(b, adv); |
| 128 | } |
| 129 | |
| 130 | /* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes |
| 131 | * backwards, and that as many bytes from out are moved to in. The caller is |
| 132 | * responsible for ensuring that adv is always smaller than or equal to b->o. |
| 133 | */ |
| 134 | static inline void b_rew(struct buffer *b, unsigned int adv) |
| 135 | { |
| 136 | b->i += adv; |
| 137 | b->o -= adv; |
| 138 | b->p = b_ptr(b, (int)-adv); |
| 139 | } |
| 140 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 141 | /* Returns the start of the input data in a buffer */ |
| 142 | static inline char *bi_ptr(const struct buffer *b) |
| 143 | { |
| 144 | return b->p; |
| 145 | } |
| 146 | |
| 147 | /* Returns the end of the input data in a buffer (pointer to next |
| 148 | * insertion point). |
| 149 | */ |
| 150 | static inline char *bi_end(const struct buffer *b) |
| 151 | { |
| 152 | char *ret = b->p + b->i; |
| 153 | |
| 154 | if (ret >= b->data + b->size) |
| 155 | ret -= b->size; |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /* Returns the amount of input data that can contiguously be read at once */ |
| 160 | static inline int bi_contig_data(const struct buffer *b) |
| 161 | { |
| 162 | int data = b->data + b->size - b->p; |
| 163 | |
| 164 | if (data > b->i) |
| 165 | data = b->i; |
| 166 | return data; |
| 167 | } |
| 168 | |
| 169 | /* Returns the start of the output data in a buffer */ |
| 170 | static inline char *bo_ptr(const struct buffer *b) |
| 171 | { |
| 172 | char *ret = b->p - b->o; |
| 173 | |
| 174 | if (ret < b->data) |
| 175 | ret += b->size; |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* Returns the end of the output data in a buffer */ |
| 180 | static inline char *bo_end(const struct buffer *b) |
| 181 | { |
| 182 | return b->p; |
| 183 | } |
| 184 | |
| 185 | /* Returns the amount of output data that can contiguously be read at once */ |
| 186 | static inline int bo_contig_data(const struct buffer *b) |
| 187 | { |
| 188 | char *beg = b->p - b->o; |
| 189 | |
| 190 | if (beg < b->data) |
| 191 | return b->data - beg; |
| 192 | return b->o; |
| 193 | } |
| 194 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 195 | /* Return the amount of bytes that can be written into the input area at once |
| 196 | * including reserved space which may be overwritten (this is the caller |
| 197 | * responsibility to know if the reserved space is protected or not). |
| 198 | */ |
| 199 | static inline int bi_contig_space(const struct buffer *b) |
| 200 | { |
| 201 | const char *left, *right; |
| 202 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 203 | left = b->p + b->i; |
| 204 | right = b->p - b->o; |
| 205 | if (left >= b->data + b->size) |
| 206 | left -= b->size; |
| 207 | else { |
| 208 | if (right < b->data) |
| 209 | right += b->size; |
| 210 | else |
| 211 | right = b->data + b->size; |
| 212 | } |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 213 | return (right - left); |
| 214 | } |
| 215 | |
| 216 | /* Return the amount of bytes that can be written into the output area at once |
| 217 | * including reserved space which may be overwritten (this is the caller |
| 218 | * responsibility to know if the reserved space is protected or not). Input data |
| 219 | * are assumed to not exist. |
| 220 | */ |
| 221 | static inline int bo_contig_space(const struct buffer *b) |
| 222 | { |
| 223 | const char *left, *right; |
| 224 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 225 | left = b->p; |
| 226 | right = b->p - b->o; |
| 227 | if (right < b->data) |
| 228 | right += b->size; |
| 229 | else |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 230 | right = b->data + b->size; |
| 231 | |
| 232 | return (right - left); |
| 233 | } |
| 234 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 235 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 236 | static inline int buffer_len(const struct buffer *buf) |
| 237 | { |
| 238 | return buf->i + buf->o; |
| 239 | } |
| 240 | |
| 241 | /* Return non-zero only if the buffer is not empty */ |
| 242 | static inline int buffer_not_empty(const struct buffer *buf) |
| 243 | { |
| 244 | return buf->i | buf->o; |
| 245 | } |
| 246 | |
| 247 | /* Return non-zero only if the buffer is empty */ |
| 248 | static inline int buffer_empty(const struct buffer *buf) |
| 249 | { |
| 250 | return !buffer_not_empty(buf); |
| 251 | } |
| 252 | |
Willy Tarreau | 5b9834f | 2017-10-16 14:01:18 +0200 | [diff] [blame] | 253 | /* Return non-zero only if the buffer's free space wraps : |
| 254 | * [ |oooo| ] => yes |
| 255 | * [ |iiii| ] => yes |
| 256 | * [ |oooo|iiii| ] => yes |
| 257 | * [oooo| ] => no |
| 258 | * [ |oooo] => no |
| 259 | * [iiii| ] => no |
| 260 | * [ |iiii] => no |
| 261 | * [oooo|iiii| ] => no |
| 262 | * [ |oooo|iiii] => no |
| 263 | * [iiii| |oooo] => no |
| 264 | * [oo|iiii| |oo] => no |
| 265 | * [iiii| |oo|ii] => no |
| 266 | * [oooooooooo|iiiiiiiiiii] => no |
| 267 | * [iiiiiiiiiiiii|oooooooo] => no |
| 268 | * |
| 269 | * So the only case where the buffer does not wrap is when there's data either |
| 270 | * at the beginning or at the end of the buffer. Thus we have this : |
| 271 | * - if (p+i >= size) ==> doesn't wrap |
| 272 | * - if (p-data <= o) ==> doesn't wrap |
| 273 | * - otherwise wraps |
| 274 | */ |
| 275 | static inline int buffer_space_wraps(const struct buffer *buf) |
| 276 | { |
| 277 | if (buf->p + buf->i >= buf->data + buf->size) |
| 278 | return 0; |
| 279 | if (buf->p <= buf->data + buf->o) |
| 280 | return 0; |
| 281 | return 1; |
| 282 | } |
| 283 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 284 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 285 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 286 | * that data that are scheduled for output are considered as potential free |
| 287 | * space, and that the reserved space is always considered as not usable. This |
| 288 | * information alone cannot be used as a general purpose free space indicator. |
| 289 | * 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] | 290 | * 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] | 291 | * generic function taking everything into account. |
| 292 | */ |
| 293 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 294 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 295 | if (b == &buf_empty) |
| 296 | return 0; |
| 297 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 298 | return (b->i + reserve >= b->size); |
| 299 | } |
| 300 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 301 | /* Normalizes a pointer after a subtract */ |
| 302 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 303 | { |
| 304 | if (ptr < buf->data) |
| 305 | ptr += buf->size; |
| 306 | return ptr; |
| 307 | } |
| 308 | |
| 309 | /* Normalizes a pointer after an addition */ |
| 310 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 311 | { |
| 312 | if (ptr - buf->size >= buf->data) |
| 313 | ptr -= buf->size; |
| 314 | return ptr; |
| 315 | } |
| 316 | |
| 317 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 318 | * including reserved space which may be overwritten. |
| 319 | */ |
| 320 | static inline int buffer_total_space(const struct buffer *buf) |
| 321 | { |
| 322 | return buf->size - buffer_len(buf); |
| 323 | } |
| 324 | |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 325 | /* Returns the amount of byte that can be written starting from <p> into the |
| 326 | * input buffer at once, including reserved space which may be overwritten. |
| 327 | * This is used by Lua to insert data in the input side just before the other |
| 328 | * data using buffer_replace(). The goal is to transfer these new data in the |
| 329 | * output buffer. |
| 330 | */ |
| 331 | static inline int bi_space_for_replace(const struct buffer *buf) |
| 332 | { |
| 333 | const char *end; |
| 334 | |
| 335 | /* If the input side data overflows, we cannot insert data contiguously. */ |
| 336 | if (buf->p + buf->i >= buf->data + buf->size) |
| 337 | return 0; |
| 338 | |
| 339 | /* Check the last byte used in the buffer, it may be a byte of the output |
| 340 | * side if the buffer wraps, or its the end of the buffer. |
| 341 | */ |
| 342 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 343 | if (end <= buf->p) |
| 344 | end = buf->data + buf->size; |
| 345 | |
| 346 | /* Compute the amount of bytes which can be written. */ |
| 347 | return end - (buf->p + buf->i); |
| 348 | } |
| 349 | |
| 350 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 351 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 352 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 353 | * when increasing a pointer. Note that the wrapping test is only performed |
| 354 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 355 | * otherwise an invalid pointer might be returned. |
| 356 | */ |
| 357 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 358 | { |
| 359 | if (ptr < buf->data) |
| 360 | ptr += buf->size; |
| 361 | else if (ptr - buf->size >= buf->data) |
| 362 | ptr -= buf->size; |
| 363 | return ptr; |
| 364 | } |
| 365 | |
| 366 | /* Returns the distance between two pointers, taking into account the ability |
| 367 | * to wrap around the buffer's end. |
| 368 | */ |
| 369 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 370 | { |
| 371 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 372 | |
| 373 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 374 | return count; |
| 375 | } |
| 376 | |
| 377 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 378 | * that is not scheduled to be sent. |
| 379 | */ |
| 380 | static inline int buffer_pending(const struct buffer *buf) |
| 381 | { |
| 382 | return buf->i; |
| 383 | } |
| 384 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 385 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 386 | static inline int buffer_almost_full(const struct buffer *buf) |
| 387 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 388 | if (buf == &buf_empty) |
| 389 | return 0; |
| 390 | |
| 391 | if (!buf->size || buffer_total_space(buf) < buf->size / 4) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 392 | return 1; |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 397 | * call this function with remaining data waiting to be sent (o > 0). The |
| 398 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 399 | * This is mainly used to remove empty lines at the beginning of a request |
| 400 | * or a response. |
| 401 | */ |
| 402 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 403 | { |
| 404 | buf->i -= n; |
| 405 | buf->p += n; |
| 406 | } |
| 407 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 408 | /* Tries to realign the given buffer. */ |
| 409 | static inline void buffer_realign(struct buffer *buf) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 410 | { |
| 411 | if (!(buf->i | buf->o)) { |
| 412 | /* let's realign the buffer to optimize I/O */ |
| 413 | buf->p = buf->data; |
| 414 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 417 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 418 | * already covers those data. That permits doing a flush even after a forward, |
| 419 | * although not recommended. |
| 420 | */ |
| 421 | static inline void buffer_flush(struct buffer *buf) |
| 422 | { |
| 423 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 424 | buf->o += buf->i; |
| 425 | buf->i = 0; |
| 426 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 427 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 428 | /* This function writes the string <str> at position <pos> which must be in |
| 429 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 430 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 431 | * (positive or negative) is returned. If there's no space left, the move is |
| 432 | * not done. The function does not adjust ->o because it does not make sense |
| 433 | * to use it on data scheduled to be sent. |
| 434 | */ |
| 435 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 436 | { |
| 437 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 438 | } |
| 439 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 440 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 441 | * Data are truncated if buffer is full. |
| 442 | */ |
| 443 | static inline void bo_putchr(struct buffer *b, char c) |
| 444 | { |
| 445 | if (buffer_len(b) == b->size) |
| 446 | return; |
| 447 | *b->p = c; |
| 448 | b->p = b_ptr(b, 1); |
| 449 | b->o++; |
| 450 | } |
| 451 | |
| 452 | /* 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] | 453 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 454 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 455 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 456 | 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] | 457 | { |
| 458 | int cur_len = buffer_len(b); |
| 459 | int half; |
| 460 | |
| 461 | if (len > b->size - cur_len) |
| 462 | len = (b->size - cur_len); |
| 463 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 464 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 466 | half = bo_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 467 | if (half > len) |
| 468 | half = len; |
| 469 | |
| 470 | memcpy(b->p, blk, half); |
| 471 | b->p = b_ptr(b, half); |
| 472 | if (len > half) { |
| 473 | memcpy(b->p, blk, len - half); |
| 474 | b->p = b_ptr(b, half); |
| 475 | } |
| 476 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 477 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* 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] | 481 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 482 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 483 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 484 | static inline int bo_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 485 | { |
| 486 | return bo_putblk(b, str, strlen(str)); |
| 487 | } |
| 488 | |
| 489 | /* 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] | 490 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 491 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 492 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 493 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 494 | { |
| 495 | return bo_putblk(b, chk->str, chk->len); |
| 496 | } |
| 497 | |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 498 | /* Gets one full block of data at once from a buffer's output, optionally |
| 499 | * starting at a specific offset. Return values : |
| 500 | * >0 : number of bytes read, equal to requested size. |
| 501 | * =0 : not enough data available. <blk> is left undefined. |
| 502 | * The buffer is left unaffected. |
| 503 | */ |
| 504 | static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset) |
| 505 | { |
| 506 | int firstblock; |
| 507 | |
| 508 | if (len + offset > buf->o) |
| 509 | return 0; |
| 510 | |
| 511 | firstblock = buf->data + buf->size - bo_ptr(buf); |
| 512 | if (firstblock > offset) { |
| 513 | if (firstblock >= len + offset) { |
| 514 | memcpy(blk, bo_ptr(buf) + offset, len); |
| 515 | return len; |
| 516 | } |
| 517 | |
| 518 | memcpy(blk, bo_ptr(buf) + offset, firstblock - offset); |
| 519 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 520 | return len; |
| 521 | } |
| 522 | |
| 523 | memcpy(blk, buf->data + offset - firstblock, len); |
| 524 | return len; |
| 525 | } |
| 526 | |
| 527 | /* Gets one or two blocks of data at once from a buffer's output. |
| 528 | * Return values : |
| 529 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 530 | * =0 : not enough data available. <blk*> are left undefined. |
| 531 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 532 | */ |
| 533 | static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 534 | { |
| 535 | if (unlikely(buf->o == 0)) |
| 536 | return 0; |
| 537 | |
Willy Tarreau | 0621da5 | 2017-10-20 18:21:49 +0200 | [diff] [blame] | 538 | if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) { |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 539 | *blk1 = buf->p - buf->o + buf->size; |
| 540 | *len1 = buf->data + buf->size - *blk1; |
| 541 | *blk2 = buf->data; |
| 542 | *len2 = buf->p - buf->data; |
| 543 | return 2; |
| 544 | } |
| 545 | |
Willy Tarreau | 4b75fff | 2017-11-02 17:16:07 +0100 | [diff] [blame] | 546 | *blk1 = bo_ptr(buf); |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 547 | *len1 = buf->o; |
| 548 | return 1; |
| 549 | } |
| 550 | |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 551 | /* Tries to write char <c> into input data at buffer <b>. Supports wrapping. |
| 552 | * Data are truncated if buffer is full. |
| 553 | */ |
| 554 | static inline void bi_putchr(struct buffer *b, char c) |
| 555 | { |
| 556 | if (buffer_len(b) == b->size) |
| 557 | return; |
| 558 | *bi_end(b) = c; |
| 559 | b->i++; |
| 560 | } |
| 561 | |
| 562 | /* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping. |
| 563 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 564 | * copied. |
| 565 | */ |
| 566 | static inline int bi_putblk(struct buffer *b, const char *blk, int len) |
| 567 | { |
| 568 | int cur_len = buffer_len(b); |
| 569 | int half; |
| 570 | |
| 571 | if (len > b->size - cur_len) |
| 572 | len = (b->size - cur_len); |
| 573 | if (!len) |
| 574 | return 0; |
| 575 | |
| 576 | half = bi_contig_space(b); |
| 577 | if (half > len) |
| 578 | half = len; |
| 579 | |
| 580 | memcpy(bi_end(b), blk, half); |
| 581 | if (len > half) |
| 582 | memcpy(b_ptr(b, b->i + half), blk, len - half); |
| 583 | b->i += len; |
| 584 | return len; |
| 585 | } |
| 586 | |
| 587 | /* Tries to copy string <str> into input data at buffer <b>. Supports wrapping. |
| 588 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 589 | * copied. |
| 590 | */ |
| 591 | static inline int bi_putstr(struct buffer *b, const char *str) |
| 592 | { |
| 593 | return bi_putblk(b, str, strlen(str)); |
| 594 | } |
| 595 | |
| 596 | /* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping. |
| 597 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 598 | * copied. |
| 599 | */ |
| 600 | static inline int bi_putchk(struct buffer *b, const struct chunk *chk) |
| 601 | { |
| 602 | return bi_putblk(b, chk->str, chk->len); |
| 603 | } |
| 604 | |
| 605 | /* Gets one full block of data at once from a buffer's input. Return values : |
| 606 | * >0 : number of bytes read, equal to requested size. |
| 607 | * =0 : not enough data available. <blk> is left undefined. |
| 608 | * The buffer is left unaffected. |
| 609 | */ |
| 610 | static inline int bi_getblk(const struct buffer *buf, char *blk, int len) |
| 611 | { |
| 612 | int firstblock; |
| 613 | |
| 614 | if (len > buf->i) |
| 615 | return 0; |
| 616 | |
| 617 | firstblock = bi_contig_data(buf); |
| 618 | if (firstblock > len) |
| 619 | firstblock = len; |
| 620 | |
| 621 | memcpy(blk, bi_ptr(buf), firstblock); |
| 622 | if (len > firstblock) |
| 623 | memcpy(blk + firstblock, buf->data, len - firstblock); |
| 624 | return len; |
| 625 | } |
| 626 | |
| 627 | /* Gets one or two blocks of data at once from a buffer's input. |
| 628 | * Return values : |
| 629 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 630 | * =0 : not enough data available. <blk*> are left undefined. |
| 631 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 632 | */ |
| 633 | static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 634 | { |
| 635 | if (unlikely(buf->i == 0)) |
| 636 | return 0; |
| 637 | |
| 638 | if (unlikely(buf->p + buf->i > buf->data + buf->size)) { |
| 639 | *blk1 = buf->p; |
| 640 | *len1 = buf->data + buf->size - buf->p; |
| 641 | *blk2 = buf->data; |
| 642 | *len2 = buf->i - *len1; |
| 643 | return 2; |
| 644 | } |
| 645 | |
| 646 | *blk1 = buf->p; |
| 647 | *len1 = buf->i; |
| 648 | return 1; |
| 649 | } |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 650 | |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 651 | /* Resets a buffer. The size is not touched. */ |
| 652 | static inline void b_reset(struct buffer *buf) |
| 653 | { |
| 654 | buf->o = 0; |
| 655 | buf->i = 0; |
| 656 | buf->p = buf->data; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 660 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 661 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 662 | * already pointed to another buffer. The allocated buffer is returned, or |
| 663 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 664 | */ |
| 665 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 666 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 667 | struct buffer *b; |
| 668 | |
| 669 | *buf = &buf_wanted; |
| 670 | b = pool_alloc_dirty(pool2_buffer); |
| 671 | if (likely(b)) { |
| 672 | b->size = pool2_buffer->size - sizeof(struct buffer); |
| 673 | b_reset(b); |
| 674 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 675 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 676 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 679 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 680 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 681 | * already pointed to another buffer. The allocated buffer is returned, or |
| 682 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 683 | * this function only picks from the pool and never calls malloc(), so it can |
| 684 | * fail even if some memory is available. |
| 685 | */ |
| 686 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 687 | { |
| 688 | struct buffer *b; |
| 689 | |
| 690 | *buf = &buf_wanted; |
| 691 | b = pool_get_first(pool2_buffer); |
| 692 | if (likely(b)) { |
| 693 | b->size = pool2_buffer->size - sizeof(struct buffer); |
| 694 | b_reset(b); |
| 695 | *buf = b; |
| 696 | } |
| 697 | return b; |
| 698 | } |
| 699 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 700 | /* Releases buffer *buf (no check of emptiness) */ |
| 701 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 702 | { |
| 703 | pool_free2(pool2_buffer, *buf); |
| 704 | } |
| 705 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 706 | /* Releases buffer *buf if allocated. */ |
| 707 | static inline void b_drop(struct buffer **buf) |
| 708 | { |
| 709 | if (!(*buf)->size) |
| 710 | return; |
| 711 | __b_drop(buf); |
| 712 | } |
| 713 | |
| 714 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 715 | static inline void b_free(struct buffer **buf) |
| 716 | { |
| 717 | b_drop(buf); |
| 718 | *buf = &buf_empty; |
| 719 | } |
| 720 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 721 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 722 | * there are still at least <margin> buffers available in the pool after this |
| 723 | * allocation so that we don't leave the pool in a condition where a session or |
| 724 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 725 | * This means that we sometimes need to try to allocate extra entries even if |
| 726 | * only one buffer is needed. |
| 727 | */ |
| 728 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 729 | { |
| 730 | struct buffer *next; |
| 731 | |
| 732 | if ((*buf)->size) |
| 733 | return *buf; |
| 734 | |
| 735 | /* fast path */ |
| 736 | if ((pool2_buffer->allocated - pool2_buffer->used) > margin) |
| 737 | return b_alloc_fast(buf); |
| 738 | |
| 739 | next = pool_refill_alloc(pool2_buffer, margin); |
| 740 | if (!next) |
| 741 | return next; |
| 742 | |
| 743 | next->size = pool2_buffer->size - sizeof(struct buffer); |
| 744 | b_reset(next); |
| 745 | *buf = next; |
| 746 | return next; |
| 747 | } |
| 748 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 749 | |
| 750 | void __offer_buffer(void *from, unsigned int threshold); |
| 751 | |
| 752 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 753 | { |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 754 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 755 | if (LIST_ISEMPTY(&buffer_wq)) { |
| 756 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 757 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 758 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 759 | __offer_buffer(from, threshold); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 760 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 763 | /*************************************************************************/ |
| 764 | /* functions used to manipulate strings and blocks with wrapping buffers */ |
| 765 | /*************************************************************************/ |
| 766 | |
| 767 | /* returns > 0 if the first <n> characters of buffer <b> starting at |
| 768 | * offset <o> relative to b->p match <ist>. (empty strings do match). It is |
| 769 | * designed to be use with reasonably small strings (ie matches a single byte |
| 770 | * per iteration). This function is usable both with input and output data. To |
| 771 | * be used like this depending on what to match : |
| 772 | * - input contents : b_isteq(b, 0, b->i, ist); |
| 773 | * - output contents : b_isteq(b, -b->o, b->o, ist); |
| 774 | * Return value : |
| 775 | * >0 : the number of matching bytes |
| 776 | * =0 : not enough bytes (or matching of empty string) |
| 777 | * <0 : non-matching byte found |
| 778 | */ |
| 779 | static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) |
| 780 | { |
| 781 | struct ist r = ist; |
| 782 | const char *p; |
| 783 | const char *end = b->data + b->size; |
| 784 | |
| 785 | if (n < r.len) |
| 786 | return 0; |
| 787 | |
| 788 | p = b_ptr(b, o); |
| 789 | while (r.len--) { |
| 790 | if (*p++ != *r.ptr++) |
| 791 | return -1; |
| 792 | if (unlikely(p == end)) |
| 793 | p = b->data; |
| 794 | } |
| 795 | return ist.len; |
| 796 | } |
| 797 | |
| 798 | /* "eats" string <ist> from the input region of buffer <b>. Wrapping data is |
| 799 | * explicitly supported. It matches a single byte per iteration so strings |
| 800 | * should remain reasonably small. Returns : |
| 801 | * > 0 : number of bytes matched and eaten |
| 802 | * = 0 : not enough bytes (or matching an empty string) |
| 803 | * < 0 : non-matching byte found |
| 804 | */ |
| 805 | static inline int bi_eat(struct buffer *b, const struct ist ist) |
| 806 | { |
| 807 | int ret = b_isteq(b, 0, b->i, ist); |
| 808 | if (ret > 0) |
| 809 | bi_del(b, ret); |
| 810 | return ret; |
| 811 | } |
| 812 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 813 | /* injects string <ist> into the input region of buffer <b> provided that it |
| 814 | * fits. Wrapping is supported. It's designed for small strings as it only |
| 815 | * writes a single byte per iteration. Returns the number of characters copied |
| 816 | * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It |
| 817 | * will only modify the buffer upon success. In all cases, the contents are |
| 818 | * copied prior to reporting an error, so that the destination at least |
| 819 | * contains a valid but truncated string. |
| 820 | */ |
| 821 | static inline int bi_istput(struct buffer *b, const struct ist ist) |
| 822 | { |
| 823 | const char *end = b->data + b->size; |
| 824 | struct ist r = ist; |
| 825 | char *p; |
| 826 | |
| 827 | if (r.len > (size_t)(b->size - b->i - b->o)) |
| 828 | return r.len < b->size ? 0 : -1; |
| 829 | |
| 830 | p = b_ptr(b, b->i); |
| 831 | b->i += r.len; |
| 832 | while (r.len--) { |
| 833 | *p++ = *r.ptr++; |
| 834 | if (unlikely(p == end)) |
| 835 | p = b->data; |
| 836 | } |
| 837 | return ist.len; |
| 838 | } |
| 839 | |
| 840 | |
| 841 | /* injects string <ist> into the output region of buffer <b> provided that it |
| 842 | * fits. Input data is assumed not to exist and will silently be overwritten. |
| 843 | * Wrapping is supported. It's designed for small strings as it only writes a |
| 844 | * single byte per iteration. Returns the number of characters copied (ist.len), |
| 845 | * 0 if it temporarily does not fit or -1 if it will never fit. It will only |
| 846 | * modify the buffer upon success. In all cases, the contents are copied prior |
| 847 | * to reporting an error, so that the destination at least contains a valid |
| 848 | * but truncated string. |
| 849 | */ |
| 850 | static inline int bo_istput(struct buffer *b, const struct ist ist) |
| 851 | { |
| 852 | const char *end = b->data + b->size; |
| 853 | struct ist r = ist; |
| 854 | char *p; |
| 855 | |
| 856 | if (r.len > (size_t)(b->size - b->o)) |
| 857 | return r.len < b->size ? 0 : -1; |
| 858 | |
| 859 | p = b->p; |
| 860 | b->o += r.len; |
| 861 | b->p = b_ptr(b, r.len); |
| 862 | while (r.len--) { |
| 863 | *p++ = *r.ptr++; |
| 864 | if (unlikely(p == end)) |
| 865 | p = b->data; |
| 866 | } |
| 867 | return ist.len; |
| 868 | } |
| 869 | |
| 870 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 871 | #endif /* _COMMON_BUFFER_H */ |
| 872 | |
| 873 | /* |
| 874 | * Local variables: |
| 875 | * c-indent-level: 8 |
| 876 | * c-basic-offset: 8 |
| 877 | * End: |
| 878 | */ |