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