Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/buffers.h |
| 3 | Buffer management definitions, macros and inline functions. |
| 4 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 6 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _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> |
| 35 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 36 | extern struct pool_head *pool2_buffer; |
| 37 | |
| 38 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 39 | int init_buffer(); |
| 40 | |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 41 | /* Initializes all fields in the buffer. The ->max_len field is initialized last |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 42 | * so that the compiler can optimize it away if changed immediately after the |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 43 | * call to this function. By default, it is set to the full size of the buffer. |
| 44 | * The BF_EMPTY flags is set. |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 45 | */ |
| 46 | static inline void buffer_init(struct buffer *buf) |
| 47 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 48 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 49 | buf->to_forward = 0; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 50 | buf->l = buf->total = 0; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 51 | buf->pipe = NULL; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 52 | buf->analysers = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 53 | buf->cons = NULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 54 | buf->flags = BF_EMPTY; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 55 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 56 | buf->max_len = BUFSIZE; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | /* returns 1 if the buffer is empty, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 60 | static inline int buffer_isempty(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | { |
| 62 | return buf->l == 0; |
| 63 | } |
| 64 | |
| 65 | /* returns 1 if the buffer is full, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 66 | static inline int buffer_isfull(const struct buffer *buf) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | return buf->l == BUFSIZE; |
| 68 | } |
| 69 | |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 70 | /* Check buffer timeouts, and set the corresponding flags. The |
| 71 | * likely/unlikely have been optimized for fastest normal path. |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 72 | * The read/write timeouts are not set if there was activity on the buffer. |
| 73 | * That way, we don't have to update the timeout on every I/O. Note that the |
| 74 | * analyser timeout is always checked. |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 75 | */ |
| 76 | static inline void buffer_check_timeouts(struct buffer *b) |
| 77 | { |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 78 | 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] | 79 | unlikely(tick_is_expired(b->rex, now_ms))) |
| 80 | b->flags |= BF_READ_TIMEOUT; |
| 81 | |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 82 | if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 83 | unlikely(tick_is_expired(b->wex, now_ms))) |
| 84 | b->flags |= BF_WRITE_TIMEOUT; |
| 85 | |
| 86 | if (likely(!(b->flags & BF_ANA_TIMEOUT)) && |
| 87 | unlikely(tick_is_expired(b->analyse_exp, now_ms))) |
| 88 | b->flags |= BF_ANA_TIMEOUT; |
| 89 | } |
| 90 | |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 91 | /* Schedule <bytes> more bytes to be forwarded by the buffer without notifying |
| 92 | * the task. Any pending data in the buffer is scheduled to be sent as well, |
| 93 | * in the limit of the number of bytes to forward. This must be the only method |
| 94 | * to use to schedule bytes to be sent. Directly touching ->to_forward will |
| 95 | * cause lockups when send_max goes down to zero if nobody is ready to push the |
| 96 | * remaining data. |
| 97 | */ |
| 98 | static inline void buffer_forward(struct buffer *buf, unsigned int bytes) |
| 99 | { |
| 100 | unsigned int data_left; |
| 101 | |
| 102 | buf->to_forward += bytes; |
| 103 | data_left = buf->l - buf->send_max; |
| 104 | if (data_left > buf->to_forward) |
| 105 | data_left = buf->to_forward; |
| 106 | |
| 107 | buf->to_forward -= data_left; |
| 108 | buf->send_max += data_left; |
| 109 | } |
| 110 | |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 111 | /* Schedule all remaining buffer data to be sent. send_max is not touched if it |
| 112 | * already covers those data. That permits doing a flush even after a forward, |
| 113 | * although not recommended. |
| 114 | */ |
| 115 | static inline void buffer_flush(struct buffer *buf) |
| 116 | { |
| 117 | if (buf->send_max < buf->l) |
| 118 | buf->send_max = buf->l; |
| 119 | } |
| 120 | |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 121 | /* Erase any content from buffer <buf> and adjusts flags accordingly. Note |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 122 | * that any spliced data is not affected since we may not have any access to |
| 123 | * it. |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 125 | static inline void buffer_erase(struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 127 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 128 | buf->to_forward = 0; |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 129 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | buf->l = 0; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 131 | buf->flags |= BF_EMPTY | BF_FULL; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 132 | if (buf->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 133 | buf->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 136 | /* marks the buffer as "shutdown" for reads and cancels the timeout */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 137 | static inline void buffer_shutr(struct buffer *buf) |
| 138 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 139 | buf->rex = TICK_ETERNITY; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 140 | buf->flags |= BF_SHUTR; |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 143 | /* marks the buffer as "shutdown" for writes and cancels the timeout */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 144 | static inline void buffer_shutw(struct buffer *buf) |
| 145 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 146 | buf->wex = TICK_ETERNITY; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 147 | buf->flags |= BF_SHUTW; |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 150 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 151 | static inline void buffer_shutr_now(struct buffer *buf) |
| 152 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 153 | buf->flags |= BF_SHUTR_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 157 | static inline void buffer_shutw_now(struct buffer *buf) |
| 158 | { |
| 159 | buf->flags |= BF_SHUTW_NOW; |
| 160 | } |
| 161 | |
| 162 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 163 | static inline void buffer_abort(struct buffer *buf) |
| 164 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 165 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 168 | /* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack |
| 169 | * flag is set, and the function called once. The function is responsible for |
| 170 | * clearing the hijack bit. It is possible that the function clears the flag |
| 171 | * during this first call. |
| 172 | */ |
| 173 | static inline void buffer_install_hijacker(struct session *s, |
| 174 | struct buffer *b, |
| 175 | void (*func)(struct session *, struct buffer *)) |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 176 | { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 177 | b->hijacker = func; |
| 178 | b->flags |= BF_HIJACK; |
| 179 | func(s, b); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 182 | /* Releases the buffer from hijacking mode. Often used by the hijack function */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 183 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 184 | { |
| 185 | buf->flags &= ~BF_HIJACK; |
| 186 | } |
| 187 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 188 | /* allows the consumer to send the buffer contents */ |
| 189 | static inline void buffer_write_ena(struct buffer *buf) |
| 190 | { |
| 191 | buf->flags |= BF_WRITE_ENA; |
| 192 | } |
| 193 | |
| 194 | /* prevents the consumer from sending the buffer contents */ |
| 195 | static inline void buffer_write_dis(struct buffer *buf) |
| 196 | { |
| 197 | buf->flags &= ~BF_WRITE_ENA; |
| 198 | } |
| 199 | |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 200 | /* check if the buffer needs to be shut down for read, and perform the shutdown |
| 201 | * at the stream_interface level if needed. This must not be used with a buffer |
| 202 | * for which a connection is currently in queue or turn-around. |
| 203 | */ |
| 204 | static inline void buffer_check_shutr(struct buffer *b) |
| 205 | { |
| 206 | if (b->flags & BF_SHUTR) |
| 207 | return; |
| 208 | |
| 209 | if (!(b->flags & (BF_SHUTR_NOW|BF_SHUTW))) |
| 210 | return; |
| 211 | |
| 212 | /* Last read, forced read-shutdown, or other end closed. We have to |
| 213 | * close our read side and inform the stream_interface. |
| 214 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 215 | b->prod->shutr(b->prod); |
| 216 | } |
| 217 | |
| 218 | /* check if the buffer needs to be shut down for write, and perform the shutdown |
| 219 | * at the stream_interface level if needed. This must not be used with a buffer |
| 220 | * for which a connection is currently in queue or turn-around. |
| 221 | */ |
| 222 | static inline void buffer_check_shutw(struct buffer *b) |
| 223 | { |
| 224 | if (b->flags & BF_SHUTW) |
| 225 | return; |
| 226 | |
| 227 | if ((b->flags & BF_SHUTW_NOW) || |
| 228 | (b->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 229 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) { |
| 230 | /* Application requested write-shutdown, or other end closed |
| 231 | * with empty buffer. We have to close our write side and |
| 232 | * inform the stream_interface. |
| 233 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 234 | b->cons->shutw(b->cons); |
| 235 | } |
| 236 | } |
| 237 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | /* returns the maximum number of bytes writable at once in this buffer */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 239 | static inline int buffer_max(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 240 | { |
| 241 | if (buf->l == BUFSIZE) |
| 242 | return 0; |
| 243 | else if (buf->r >= buf->w) |
| 244 | return buf->data + BUFSIZE - buf->r; |
| 245 | else |
| 246 | return buf->w - buf->r; |
| 247 | } |
| 248 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 249 | /* sets the buffer read limit to <size> bytes, and adjusts the FULL |
| 250 | * flag accordingly. |
| 251 | */ |
| 252 | static inline void buffer_set_rlim(struct buffer *buf, int size) |
| 253 | { |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 254 | buf->max_len = size; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 255 | if (buf->l < size) |
| 256 | buf->flags &= ~BF_FULL; |
| 257 | else |
| 258 | buf->flags |= BF_FULL; |
| 259 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 263 | * there at once without overwriting anything. |
| 264 | */ |
| 265 | static inline int buffer_realign(struct buffer *buf) |
| 266 | { |
| 267 | if (buf->l == 0) { |
| 268 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 269 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | } |
| 271 | return buffer_max(buf); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | int buffer_write(struct buffer *buf, const char *msg, int len); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 276 | int buffer_write_chunk(struct buffer *buf, struct chunk *chunk); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 277 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); |
| 278 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 279 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 280 | int chunk_printf(struct chunk *chk, int size, const char *fmt, ...); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 281 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 282 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 283 | /* |
| 284 | * frees the destination chunk if already allocated, allocates a new string, |
| 285 | * and copies the source into it. The pointer to the destination string is |
| 286 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 287 | */ |
| 288 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 289 | if (!dst || !src || !src->str) |
| 290 | return NULL; |
| 291 | if (dst->str) |
| 292 | free(dst->str); |
| 293 | dst->len = src->len; |
| 294 | dst->str = (char *)malloc(dst->len); |
| 295 | memcpy(dst->str, src->str, dst->len); |
| 296 | return dst->str; |
| 297 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | |
| 299 | #endif /* _PROTO_BUFFERS_H */ |
| 300 | |
| 301 | /* |
| 302 | * Local variables: |
| 303 | * c-indent-level: 8 |
| 304 | * c-basic-offset: 8 |
| 305 | * End: |
| 306 | */ |