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