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