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