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