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