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