Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 2 | * include/proto/buffers.h |
| 3 | * Buffer management definitions, macros and inline functions. |
| 4 | * |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_BUFFERS_H |
| 23 | #define _PROTO_BUFFERS_H |
| 24 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 27 | #include <string.h> |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 29 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 31 | #include <common/ticks.h> |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 32 | #include <common/time.h> |
| 33 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <types/buffers.h> |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 35 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 37 | extern struct pool_head *pool2_buffer; |
| 38 | |
| 39 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 40 | int init_buffer(); |
| 41 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 42 | /* SI-to-buffer functions : buffer_{get,put}_{char,block,string,chunk} */ |
| 43 | int buffer_write(struct buffer *buf, const char *msg, int len); |
| 44 | int buffer_put_block(struct buffer *buf, const char *str, int len); |
| 45 | int buffer_put_char(struct buffer *buf, char c); |
| 46 | int buffer_get_line(struct buffer *buf, char *str, int len); |
| 47 | int buffer_get_block(struct buffer *buf, char *blk, int len, int offset); |
| 48 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); |
| 49 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 50 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
| 51 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 52 | void buffer_bounce_realign(struct buffer *buf); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 53 | unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 55 | /* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */ |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 56 | static inline void buffer_init(struct buffer *buf) |
| 57 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 58 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 59 | buf->to_forward = 0; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 60 | buf->l = buf->total = 0; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 61 | buf->pipe = NULL; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 62 | buf->analysers = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 63 | buf->cons = NULL; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 64 | buf->flags = BF_OUT_EMPTY; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 65 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 68 | /*****************************************************************/ |
| 69 | /* These functions are used to compute various buffer area sizes */ |
| 70 | /*****************************************************************/ |
| 71 | |
| 72 | /* Return the number of reserved bytes in the buffer, which ensures that once |
| 73 | * all pending data are forwarded, the buffer still has global.tune.maxrewrite |
| 74 | * bytes free. The result is between 0 and global.maxrewrite, which is itself |
| 75 | * smaller than any buf->size. |
| 76 | */ |
| 77 | static inline int buffer_reserved(const struct buffer *buf) |
| 78 | { |
| 79 | int ret = global.tune.maxrewrite - buf->to_forward - buf->send_max; |
| 80 | |
| 81 | if (buf->to_forward == BUF_INFINITE_FORWARD) |
| 82 | return 0; |
| 83 | if (ret <= 0) |
| 84 | return 0; |
| 85 | return ret; |
| 86 | } |
| 87 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 88 | /* Return the max number of bytes the buffer can contain so that once all the |
| 89 | * pending bytes are forwarded, the buffer still has global.tune.maxrewrite |
| 90 | * bytes free. The result sits between buf->size - maxrewrite and buf->size. |
| 91 | */ |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 92 | static inline int buffer_max_len(const struct buffer *buf) |
| 93 | { |
| 94 | return buf->size - buffer_reserved(buf); |
| 95 | } |
| 96 | |
| 97 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 98 | * including reserved space which may be overwritten. |
| 99 | */ |
| 100 | static inline int buffer_total_space(const struct buffer *buf) |
| 101 | { |
| 102 | return buf->size - buf->l; |
| 103 | } |
| 104 | |
| 105 | /* Return the maximum amount of bytes that can be written into the buffer, |
Willy Tarreau | fe4b1f9 | 2011-11-28 13:40:49 +0100 | [diff] [blame^] | 106 | * excluding the reserved space, which is preserved. 0 may be returned if |
| 107 | * the reserved space was already reached or used. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 108 | */ |
| 109 | static inline int buffer_total_space_res(const struct buffer *buf) |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 110 | { |
Willy Tarreau | fe4b1f9 | 2011-11-28 13:40:49 +0100 | [diff] [blame^] | 111 | int len = buffer_max_len(buf) - buf->l; |
| 112 | return len < 0 ? 0 : len; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Returns the number of contiguous bytes between <start> and <start>+<count>, |
| 116 | * and enforces a limit on buf->data + buf->size. <start> must be within the |
| 117 | * buffer. |
| 118 | */ |
| 119 | static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count) |
| 120 | { |
| 121 | if (count > buf->data - start + buf->size) |
| 122 | count = buf->data - start + buf->size; |
| 123 | return count; |
| 124 | } |
| 125 | |
| 126 | /* Return the amount of bytes that can be written into the buffer at once, |
| 127 | * including reserved space which may be overwritten. This version is optimized |
| 128 | * to reduce the amount of operations but it's not easy to understand as it is. |
| 129 | * Drawing a buffer with wrapping data on a paper helps a lot. |
| 130 | */ |
| 131 | static inline int buffer_contig_space(struct buffer *buf) |
| 132 | { |
| 133 | int space_from_end = buf->l - (buf->r - buf->data); |
| 134 | if (space_from_end < 0) /* data does not wrap */ |
| 135 | space_from_end = buf->r - buf->data; |
| 136 | return buf->size - space_from_end; |
| 137 | } |
| 138 | |
| 139 | /* Return the amount of bytes that can be written into the buffer at once, |
| 140 | * excluding reserved space, which is preserved. Same comment as above for |
| 141 | * the optimization leading to hardly understandable code. |
| 142 | */ |
| 143 | static inline int buffer_contig_space_res(struct buffer *buf) |
| 144 | { |
| 145 | /* Proceed differently if the buffer is full, partially used or empty. |
| 146 | * The hard situation is when it's partially used and either data or |
| 147 | * reserved space wraps at the end. |
| 148 | */ |
| 149 | int res = buffer_reserved(buf); |
| 150 | int spare = buf->size - res; |
| 151 | |
| 152 | if (buf->l >= spare) |
| 153 | spare = 0; |
| 154 | else if (buf->l) { |
| 155 | spare = buf->w - res - buf->r; |
| 156 | if (spare <= 0) |
| 157 | spare += buf->size; |
| 158 | spare = buffer_contig_area(buf, buf->r, spare); |
| 159 | } |
| 160 | return spare; |
| 161 | } |
| 162 | |
| 163 | /* Same as above, but lets the caller pass the pre-computed value of |
| 164 | * buffer_reserved() in <res> if it already knows it, to save some |
| 165 | * computations. |
| 166 | */ |
| 167 | static inline int buffer_contig_space_with_res(struct buffer *buf, int res) |
| 168 | { |
| 169 | /* Proceed differently if the buffer is full, partially used or empty. |
| 170 | * The hard situation is when it's partially used and either data or |
| 171 | * reserved space wraps at the end. |
| 172 | */ |
| 173 | int spare = buf->size - res; |
| 174 | |
| 175 | if (buf->l >= spare) |
| 176 | spare = 0; |
| 177 | else if (buf->l) { |
| 178 | spare = buf->w - res - buf->r; |
| 179 | if (spare <= 0) |
| 180 | spare += buf->size; |
| 181 | spare = buffer_contig_area(buf, buf->r, spare); |
| 182 | } |
| 183 | return spare; |
| 184 | } |
| 185 | |
| 186 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 187 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 188 | * when increasing a pointer. Note that the wrapping test is only performed |
| 189 | * once, so the original pointer must be between ->data and ->data+2*size - 1, |
| 190 | * otherwise an invalid pointer might be returned. |
| 191 | */ |
| 192 | static inline char *buffer_pointer(const struct buffer *buf, char *ptr) |
| 193 | { |
| 194 | if (ptr - buf->size >= buf->data) |
| 195 | ptr -= buf->size; |
| 196 | return ptr; |
| 197 | } |
| 198 | |
| 199 | /* Returns the distance between two pointers, taking into account the ability |
| 200 | * to wrap around the buffer's end. |
| 201 | */ |
| 202 | static inline int buffer_count(const struct buffer *buf, char *from, char *to) |
| 203 | { |
| 204 | int count = to - from; |
| 205 | if (count < 0) |
| 206 | count += buf->size; |
| 207 | return count; |
| 208 | } |
| 209 | |
| 210 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 211 | * that is not scheduled to be sent. |
| 212 | */ |
| 213 | static inline int buffer_pending(const struct buffer *buf) |
| 214 | { |
| 215 | return buf->l - buf->send_max; |
| 216 | } |
| 217 | |
| 218 | /* Returns the size of the working area which the caller knows ends at <end>. |
| 219 | * If <end> equals buf->r (modulo size), then it means that the free area which |
| 220 | * follows is part of the working area. Otherwise, the working area stops at |
| 221 | * <end>. It always starts at buf->w+send_max. The work area includes the |
| 222 | * reserved area. |
| 223 | */ |
| 224 | static inline int buffer_work_area(const struct buffer *buf, char *end) |
| 225 | { |
| 226 | end = buffer_pointer(buf, end); |
| 227 | if (end == buf->r) /* pointer exactly at end, lets push forwards */ |
| 228 | end = buf->w; |
| 229 | return buffer_count(buf, buffer_pointer(buf, buf->w + buf->send_max), end); |
| 230 | } |
| 231 | |
| 232 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 233 | static inline int buffer_almost_full(const struct buffer *buf) |
| 234 | { |
| 235 | if (buffer_total_space(buf) < buf->size / 4) |
| 236 | return 1; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Return the max amount of bytes that can be read from the buffer at once. |
| 242 | * Note that this may be lower than the actual buffer length when the data |
| 243 | * wrap after the end, so it's preferable to call this function again after |
| 244 | * reading. Also note that this function respects the send_max limit. |
| 245 | */ |
| 246 | static inline int buffer_contig_data(struct buffer *buf) |
| 247 | { |
| 248 | int ret; |
| 249 | |
| 250 | if (!buf->send_max || !buf->l) |
| 251 | return 0; |
| 252 | |
| 253 | if (buf->r > buf->w) |
| 254 | ret = buf->r - buf->w; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 255 | else |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 256 | ret = buf->data + buf->size - buf->w; |
| 257 | |
| 258 | /* limit the amount of outgoing data if required */ |
| 259 | if (ret > buf->send_max) |
| 260 | ret = buf->send_max; |
| 261 | |
| 262 | return ret; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 265 | /* Returns true if the buffer's input is already closed */ |
| 266 | static inline int buffer_input_closed(struct buffer *buf) |
| 267 | { |
| 268 | return ((buf->flags & BF_SHUTR) != 0); |
| 269 | } |
| 270 | |
| 271 | /* Returns true if the buffer's output is already closed */ |
| 272 | static inline int buffer_output_closed(struct buffer *buf) |
| 273 | { |
| 274 | return ((buf->flags & BF_SHUTW) != 0); |
| 275 | } |
| 276 | |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 277 | /* Check buffer timeouts, and set the corresponding flags. The |
| 278 | * likely/unlikely have been optimized for fastest normal path. |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 279 | * The read/write timeouts are not set if there was activity on the buffer. |
| 280 | * That way, we don't have to update the timeout on every I/O. Note that the |
| 281 | * analyser timeout is always checked. |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 282 | */ |
| 283 | static inline void buffer_check_timeouts(struct buffer *b) |
| 284 | { |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 285 | if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 286 | unlikely(tick_is_expired(b->rex, now_ms))) |
| 287 | b->flags |= BF_READ_TIMEOUT; |
| 288 | |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 289 | if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 290 | unlikely(tick_is_expired(b->wex, now_ms))) |
| 291 | b->flags |= BF_WRITE_TIMEOUT; |
| 292 | |
| 293 | if (likely(!(b->flags & BF_ANA_TIMEOUT)) && |
| 294 | unlikely(tick_is_expired(b->analyse_exp, now_ms))) |
| 295 | b->flags |= BF_ANA_TIMEOUT; |
| 296 | } |
| 297 | |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 298 | /* Schedule all remaining buffer data to be sent. send_max is not touched if it |
| 299 | * already covers those data. That permits doing a flush even after a forward, |
| 300 | * although not recommended. |
| 301 | */ |
| 302 | static inline void buffer_flush(struct buffer *buf) |
| 303 | { |
| 304 | if (buf->send_max < buf->l) |
| 305 | buf->send_max = buf->l; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 306 | if (buf->send_max) |
| 307 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 310 | /* Erase any content from buffer <buf> and adjusts flags accordingly. Note |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 311 | * that any spliced data is not affected since we may not have any access to |
| 312 | * it. |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 313 | */ |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 314 | static inline void buffer_erase(struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 315 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 316 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 317 | buf->to_forward = 0; |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 318 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | buf->l = 0; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 320 | buf->flags &= ~(BF_FULL | BF_OUT_EMPTY); |
| 321 | if (!buf->pipe) |
| 322 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 325 | /* Cut the "tail" of the buffer, which means strip it to the length of unsent |
| 326 | * data only, and kill any remaining unsent data. Any scheduled forwarding is |
| 327 | * stopped. This is mainly to be used to send error messages after existing |
| 328 | * data. |
| 329 | */ |
| 330 | static inline void buffer_cut_tail(struct buffer *buf) |
| 331 | { |
| 332 | if (!buf->send_max) |
| 333 | return buffer_erase(buf); |
| 334 | |
| 335 | buf->to_forward = 0; |
| 336 | if (buf->l == buf->send_max) |
| 337 | return; |
| 338 | |
| 339 | buf->l = buf->send_max; |
| 340 | buf->r = buf->w + buf->l; |
| 341 | if (buf->r >= buf->data + buf->size) |
| 342 | buf->r -= buf->size; |
| 343 | buf->lr = buf->r; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 344 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 345 | if (buf->l >= buffer_max_len(buf)) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 346 | buf->flags |= BF_FULL; |
| 347 | } |
| 348 | |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 349 | /* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n> |
| 350 | * is smaller than the actual buffer's length. This is mainly used to remove |
| 351 | * empty lines at the beginning of a request or a response. |
| 352 | */ |
| 353 | static inline void buffer_ignore(struct buffer *buf, int n) |
| 354 | { |
| 355 | buf->l -= n; |
| 356 | buf->w += n; |
| 357 | if (buf->w >= buf->data + buf->size) |
| 358 | buf->w -= buf->size; |
| 359 | buf->flags &= ~BF_FULL; |
| 360 | if (buf->l >= buffer_max_len(buf)) |
| 361 | buf->flags |= BF_FULL; |
| 362 | } |
| 363 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 364 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 365 | static inline void buffer_shutr_now(struct buffer *buf) |
| 366 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 367 | buf->flags |= BF_SHUTR_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 371 | static inline void buffer_shutw_now(struct buffer *buf) |
| 372 | { |
| 373 | buf->flags |= BF_SHUTW_NOW; |
| 374 | } |
| 375 | |
| 376 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 377 | static inline void buffer_abort(struct buffer *buf) |
| 378 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 379 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 380 | buf->flags &= ~BF_AUTO_CONNECT; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 383 | /* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack |
| 384 | * flag is set, and the function called once. The function is responsible for |
| 385 | * clearing the hijack bit. It is possible that the function clears the flag |
| 386 | * during this first call. |
| 387 | */ |
| 388 | static inline void buffer_install_hijacker(struct session *s, |
| 389 | struct buffer *b, |
| 390 | void (*func)(struct session *, struct buffer *)) |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 391 | { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 392 | b->hijacker = func; |
| 393 | b->flags |= BF_HIJACK; |
| 394 | func(s, b); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 397 | /* Releases the buffer from hijacking mode. Often used by the hijack function */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 398 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 399 | { |
| 400 | buf->flags &= ~BF_HIJACK; |
| 401 | } |
| 402 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 403 | /* allow the consumer to try to establish a new connection. */ |
| 404 | static inline void buffer_auto_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 405 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 406 | buf->flags |= BF_AUTO_CONNECT; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 409 | /* prevent the consumer from trying to establish a new connection, and also |
| 410 | * disable auto shutdown forwarding. |
| 411 | */ |
| 412 | static inline void buffer_dont_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 413 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 414 | buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 417 | /* allow the producer to forward shutdown requests */ |
| 418 | static inline void buffer_auto_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 419 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 420 | buf->flags |= BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 423 | /* prevent the producer from forwarding shutdown requests */ |
| 424 | static inline void buffer_dont_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 425 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 426 | buf->flags &= ~BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 429 | /* allow the producer to read / poll the input */ |
| 430 | static inline void buffer_auto_read(struct buffer *buf) |
| 431 | { |
| 432 | buf->flags &= ~BF_DONT_READ; |
| 433 | } |
| 434 | |
| 435 | /* prevent the producer from read / poll the input */ |
| 436 | static inline void buffer_dont_read(struct buffer *buf) |
| 437 | { |
| 438 | buf->flags |= BF_DONT_READ; |
| 439 | } |
| 440 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 441 | /* |
| 442 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 443 | * there at once without overwriting anything. |
| 444 | */ |
| 445 | static inline int buffer_realign(struct buffer *buf) |
| 446 | { |
| 447 | if (buf->l == 0) { |
| 448 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 449 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 450 | } |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 451 | return buffer_contig_space(buf); |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Advance the buffer's read pointer by <len> bytes. This is useful when data |
| 456 | * have been read directly from the buffer. It is illegal to call this function |
| 457 | * with <len> causing a wrapping at the end of the buffer. It's the caller's |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 458 | * responsibility to ensure that <len> is never larger than buf->send_max. |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 459 | */ |
| 460 | static inline void buffer_skip(struct buffer *buf, int len) |
| 461 | { |
| 462 | buf->w += len; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 463 | if (buf->w >= buf->data + buf->size) |
| 464 | buf->w -= buf->size; /* wrap around the buffer */ |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 465 | |
| 466 | buf->l -= len; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 467 | if (!buf->l) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 468 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 469 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 470 | if (buf->l < buffer_max_len(buf)) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 471 | buf->flags &= ~BF_FULL; |
| 472 | |
| 473 | buf->send_max -= len; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 474 | if (!buf->send_max && !buf->pipe) |
| 475 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 476 | |
| 477 | /* notify that some data was written to the SI from the buffer */ |
| 478 | buf->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 479 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 480 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 481 | /* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success, |
| 482 | * -2 if it is larger than the buffer size, or the number of bytes available |
| 483 | * otherwise. If the chunk has been written, its size is automatically reset |
| 484 | * to zero. The send limit is automatically adjusted with the amount of data |
| 485 | * written. |
| 486 | */ |
| 487 | static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk) |
| 488 | { |
| 489 | int ret; |
| 490 | |
| 491 | ret = buffer_write(buf, chunk->str, chunk->len); |
| 492 | if (ret == -1) |
| 493 | chunk->len = 0; |
| 494 | return ret; |
| 495 | } |
| 496 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 497 | /* Tries to copy chunk <chunk> into buffer <buf> after length controls. |
| 498 | * The send_max and to_forward pointers are updated. If the buffer's input is |
| 499 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 500 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 501 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 502 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 503 | * transferred. The chunk's length is updated with the number of bytes sent. |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 504 | */ |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 505 | static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk) |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 506 | { |
| 507 | int ret; |
| 508 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 509 | ret = buffer_put_block(buf, chunk->str, chunk->len); |
| 510 | if (ret > 0) |
| 511 | chunk->len -= ret; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 512 | return ret; |
| 513 | } |
| 514 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 515 | /* Tries to copy string <str> at once into buffer <buf> after length controls. |
| 516 | * The send_max and to_forward pointers are updated. If the buffer's input is |
| 517 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 518 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 519 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 520 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 521 | * transferred. |
| 522 | */ |
| 523 | static inline int buffer_put_string(struct buffer *buf, const char *str) |
| 524 | { |
| 525 | return buffer_put_block(buf, str, strlen(str)); |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Return one char from the buffer. If the buffer is empty and closed, return -2. |
| 530 | * If the buffer is just empty, return -1. The buffer's pointer is not advanced, |
| 531 | * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char. |
| 532 | * Also note that this function respects the send_max limit. |
| 533 | */ |
| 534 | static inline int buffer_get_char(struct buffer *buf) |
| 535 | { |
| 536 | /* closed or empty + imminent close = -2; empty = -1 */ |
| 537 | if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) { |
| 538 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 539 | return -2; |
| 540 | return -1; |
| 541 | } |
| 542 | return *buf->w; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | /* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!! |
| 547 | * returns >= 0 if the buffer is too small to hold the message, -1 if the |
| 548 | * transfer was OK, -2 in case of failure. |
| 549 | */ |
| 550 | static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg) |
| 551 | { |
| 552 | int ret = buffer_put_chunk(buf, msg); |
| 553 | if (ret >= 0) /* transfer OK */ |
| 554 | return -1; |
| 555 | if (ret == -1) /* missing room */ |
| 556 | return 1; |
| 557 | /* failure */ |
| 558 | return -2; |
| 559 | } |
| 560 | |
| 561 | /* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!! |
| 562 | * returns >= 0 if the buffer is too small to hold the message, -1 if the |
| 563 | * transfer was OK, -2 in case of failure. |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 564 | */ |
| 565 | static inline int buffer_feed(struct buffer *buf, const char *str) |
| 566 | { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 567 | int ret = buffer_put_string(buf, str); |
| 568 | if (ret >= 0) /* transfer OK */ |
| 569 | return -1; |
| 570 | if (ret == -1) /* missing room */ |
| 571 | return 1; |
| 572 | /* failure */ |
| 573 | return -2; |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 576 | /* |
| 577 | * |
| 578 | * Functions below are used to manage chunks |
| 579 | * |
| 580 | */ |
| 581 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 582 | static inline void chunk_init(struct chunk *chk, char *str, size_t size) { |
| 583 | chk->str = str; |
| 584 | chk->len = 0; |
| 585 | chk->size = size; |
| 586 | } |
| 587 | |
| 588 | /* report 0 in case of error, 1 if OK. */ |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 589 | static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) { |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 590 | |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 591 | if (size && len > size) |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 592 | return 0; |
| 593 | |
| 594 | chk->str = str; |
| 595 | chk->len = len; |
| 596 | chk->size = size; |
| 597 | |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | static inline void chunk_initstr(struct chunk *chk, char *str) { |
| 602 | chk->str = str; |
| 603 | chk->len = strlen(str); |
| 604 | chk->size = 0; /* mark it read-only */ |
| 605 | } |
| 606 | |
| 607 | static inline int chunk_strcpy(struct chunk *chk, const char *str) { |
| 608 | size_t len; |
| 609 | |
| 610 | len = strlen(str); |
| 611 | |
| 612 | if (unlikely(len > chk->size)) |
| 613 | return 0; |
| 614 | |
| 615 | chk->len = len; |
| 616 | memcpy(chk->str, str, len); |
| 617 | |
| 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 622 | __attribute__ ((format(printf, 2, 3))); |
| 623 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 624 | int chunk_htmlencode(struct chunk *dst, struct chunk *src); |
| 625 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc); |
| 626 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 627 | static inline void chunk_reset(struct chunk *chk) { |
| 628 | chk->str = NULL; |
| 629 | chk->len = -1; |
| 630 | chk->size = 0; |
| 631 | } |
| 632 | |
| 633 | static inline void chunk_destroy(struct chunk *chk) { |
| 634 | |
| 635 | if (!chk->size) |
| 636 | return; |
| 637 | |
| 638 | if (chk->str) |
| 639 | free(chk->str); |
| 640 | |
| 641 | chunk_reset(chk); |
| 642 | } |
| 643 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 644 | /* |
| 645 | * frees the destination chunk if already allocated, allocates a new string, |
| 646 | * and copies the source into it. The pointer to the destination string is |
| 647 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 648 | */ |
| 649 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 650 | if (!dst || !src || !src->str) |
| 651 | return NULL; |
| 652 | if (dst->str) |
| 653 | free(dst->str); |
| 654 | dst->len = src->len; |
| 655 | dst->str = (char *)malloc(dst->len); |
| 656 | memcpy(dst->str, src->str, dst->len); |
| 657 | return dst->str; |
| 658 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 659 | |
| 660 | #endif /* _PROTO_BUFFERS_H */ |
| 661 | |
| 662 | /* |
| 663 | * Local variables: |
| 664 | * c-indent-level: 8 |
| 665 | * c-basic-offset: 8 |
| 666 | * End: |
| 667 | */ |