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 | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 42 | /* 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] | 43 | static inline void buffer_init(struct buffer *buf) |
| 44 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 45 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 46 | buf->to_forward = 0; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 47 | buf->l = buf->total = 0; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 48 | buf->pipe = NULL; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 49 | buf->analysers = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 50 | buf->cons = NULL; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 51 | buf->flags = BF_OUT_EMPTY; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 52 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 55 | /* Return the max number of bytes the buffer can contain so that once all the |
| 56 | * pending bytes are forwarded, the buffer still has global.tune.maxrewrite |
| 57 | * bytes free. The result sits between buf->size - maxrewrite and buf->size. |
| 58 | */ |
| 59 | static inline int buffer_max_len(struct buffer *buf) |
| 60 | { |
| 61 | if (buf->to_forward == BUF_INFINITE_FORWARD || |
| 62 | buf->to_forward + buf->send_max >= global.tune.maxrewrite) |
| 63 | return buf->size; |
| 64 | else |
| 65 | return buf->size - global.tune.maxrewrite + buf->to_forward + buf->send_max; |
| 66 | } |
| 67 | |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 68 | /* Check buffer timeouts, and set the corresponding flags. The |
| 69 | * likely/unlikely have been optimized for fastest normal path. |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 70 | * The read/write timeouts are not set if there was activity on the buffer. |
| 71 | * That way, we don't have to update the timeout on every I/O. Note that the |
| 72 | * analyser timeout is always checked. |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 73 | */ |
| 74 | static inline void buffer_check_timeouts(struct buffer *b) |
| 75 | { |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 76 | 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] | 77 | unlikely(tick_is_expired(b->rex, now_ms))) |
| 78 | b->flags |= BF_READ_TIMEOUT; |
| 79 | |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 80 | if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 81 | unlikely(tick_is_expired(b->wex, now_ms))) |
| 82 | b->flags |= BF_WRITE_TIMEOUT; |
| 83 | |
| 84 | if (likely(!(b->flags & BF_ANA_TIMEOUT)) && |
| 85 | unlikely(tick_is_expired(b->analyse_exp, now_ms))) |
| 86 | b->flags |= BF_ANA_TIMEOUT; |
| 87 | } |
| 88 | |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 89 | /* Schedule <bytes> more bytes to be forwarded by the buffer without notifying |
| 90 | * the task. Any pending data in the buffer is scheduled to be sent as well, |
| 91 | * in the limit of the number of bytes to forward. This must be the only method |
| 92 | * to use to schedule bytes to be sent. Directly touching ->to_forward will |
| 93 | * cause lockups when send_max goes down to zero if nobody is ready to push the |
| 94 | * remaining data. |
| 95 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 96 | static inline void buffer_forward(struct buffer *buf, unsigned long bytes) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 97 | { |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 98 | unsigned long data_left; |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 99 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 100 | if (!bytes) |
| 101 | return; |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 102 | data_left = buf->l - buf->send_max; |
Willy Tarreau | 91aa577 | 2009-09-15 20:32:30 +0200 | [diff] [blame] | 103 | if (data_left >= bytes) { |
| 104 | buf->send_max += bytes; |
Willy Tarreau | 2d028db | 2009-09-20 22:56:25 +0200 | [diff] [blame] | 105 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | 91aa577 | 2009-09-15 20:32:30 +0200 | [diff] [blame] | 106 | return; |
| 107 | } |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 108 | |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 109 | buf->send_max += data_left; |
Willy Tarreau | 2d028db | 2009-09-20 22:56:25 +0200 | [diff] [blame] | 110 | if (buf->send_max) |
| 111 | buf->flags &= ~BF_OUT_EMPTY; |
| 112 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 113 | if (buf->to_forward != BUF_INFINITE_FORWARD) { |
| 114 | buf->to_forward += bytes - data_left; |
| 115 | if (bytes == BUF_INFINITE_FORWARD) |
| 116 | buf->to_forward = bytes; |
| 117 | } |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 119 | if (buf->l < buffer_max_len(buf)) |
| 120 | buf->flags &= ~BF_FULL; |
| 121 | else |
| 122 | buf->flags |= BF_FULL; |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 125 | /* Schedule all remaining buffer data to be sent. send_max is not touched if it |
| 126 | * already covers those data. That permits doing a flush even after a forward, |
| 127 | * although not recommended. |
| 128 | */ |
| 129 | static inline void buffer_flush(struct buffer *buf) |
| 130 | { |
| 131 | if (buf->send_max < buf->l) |
| 132 | buf->send_max = buf->l; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 133 | if (buf->send_max) |
| 134 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 137 | /* Erase any content from buffer <buf> and adjusts flags accordingly. Note |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 138 | * that any spliced data is not affected since we may not have any access to |
| 139 | * it. |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 140 | */ |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 141 | static inline void buffer_erase(struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 142 | { |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 143 | buf->send_max = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 144 | buf->to_forward = 0; |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 145 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | buf->l = 0; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 147 | buf->flags &= ~(BF_FULL | BF_OUT_EMPTY); |
| 148 | if (!buf->pipe) |
| 149 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 152 | /* Cut the "tail" of the buffer, which means strip it to the length of unsent |
| 153 | * data only, and kill any remaining unsent data. Any scheduled forwarding is |
| 154 | * stopped. This is mainly to be used to send error messages after existing |
| 155 | * data. |
| 156 | */ |
| 157 | static inline void buffer_cut_tail(struct buffer *buf) |
| 158 | { |
| 159 | if (!buf->send_max) |
| 160 | return buffer_erase(buf); |
| 161 | |
| 162 | buf->to_forward = 0; |
| 163 | if (buf->l == buf->send_max) |
| 164 | return; |
| 165 | |
| 166 | buf->l = buf->send_max; |
| 167 | buf->r = buf->w + buf->l; |
| 168 | if (buf->r >= buf->data + buf->size) |
| 169 | buf->r -= buf->size; |
| 170 | buf->lr = buf->r; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 171 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 172 | if (buf->l >= buffer_max_len(buf)) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 173 | buf->flags |= BF_FULL; |
| 174 | } |
| 175 | |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 176 | /* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n> |
| 177 | * is smaller than the actual buffer's length. This is mainly used to remove |
| 178 | * empty lines at the beginning of a request or a response. |
| 179 | */ |
| 180 | static inline void buffer_ignore(struct buffer *buf, int n) |
| 181 | { |
| 182 | buf->l -= n; |
| 183 | buf->w += n; |
| 184 | if (buf->w >= buf->data + buf->size) |
| 185 | buf->w -= buf->size; |
| 186 | buf->flags &= ~BF_FULL; |
| 187 | if (buf->l >= buffer_max_len(buf)) |
| 188 | buf->flags |= BF_FULL; |
| 189 | } |
| 190 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 191 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 192 | static inline void buffer_shutr_now(struct buffer *buf) |
| 193 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 194 | buf->flags |= BF_SHUTR_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 198 | static inline void buffer_shutw_now(struct buffer *buf) |
| 199 | { |
| 200 | buf->flags |= BF_SHUTW_NOW; |
| 201 | } |
| 202 | |
| 203 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 204 | static inline void buffer_abort(struct buffer *buf) |
| 205 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 206 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 207 | buf->flags &= ~BF_AUTO_CONNECT; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 210 | /* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack |
| 211 | * flag is set, and the function called once. The function is responsible for |
| 212 | * clearing the hijack bit. It is possible that the function clears the flag |
| 213 | * during this first call. |
| 214 | */ |
| 215 | static inline void buffer_install_hijacker(struct session *s, |
| 216 | struct buffer *b, |
| 217 | void (*func)(struct session *, struct buffer *)) |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 218 | { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 219 | b->hijacker = func; |
| 220 | b->flags |= BF_HIJACK; |
| 221 | func(s, b); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 224 | /* Releases the buffer from hijacking mode. Often used by the hijack function */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 225 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 226 | { |
| 227 | buf->flags &= ~BF_HIJACK; |
| 228 | } |
| 229 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 230 | /* allow the consumer to try to establish a new connection. */ |
| 231 | static inline void buffer_auto_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 232 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 233 | buf->flags |= BF_AUTO_CONNECT; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 236 | /* prevent the consumer from trying to establish a new connection, and also |
| 237 | * disable auto shutdown forwarding. |
| 238 | */ |
| 239 | static inline void buffer_dont_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 240 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 241 | buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 244 | /* allow the producer to forward shutdown requests */ |
| 245 | static inline void buffer_auto_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 246 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 247 | buf->flags |= BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 250 | /* prevent the producer from forwarding shutdown requests */ |
| 251 | static inline void buffer_dont_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 252 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 253 | buf->flags &= ~BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 256 | /* allow the producer to read / poll the input */ |
| 257 | static inline void buffer_auto_read(struct buffer *buf) |
| 258 | { |
| 259 | buf->flags &= ~BF_DONT_READ; |
| 260 | } |
| 261 | |
| 262 | /* prevent the producer from read / poll the input */ |
| 263 | static inline void buffer_dont_read(struct buffer *buf) |
| 264 | { |
| 265 | buf->flags |= BF_DONT_READ; |
| 266 | } |
| 267 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 268 | /* returns the maximum number of bytes writable at once in this buffer */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 269 | static inline int buffer_max(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | { |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 271 | if (buf->l == buf->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | return 0; |
| 273 | else if (buf->r >= buf->w) |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 274 | return buf->data + buf->size - buf->r; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 275 | else |
| 276 | return buf->w - buf->r; |
| 277 | } |
| 278 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 279 | /* |
| 280 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 281 | * there at once without overwriting anything. |
| 282 | */ |
| 283 | static inline int buffer_realign(struct buffer *buf) |
| 284 | { |
| 285 | if (buf->l == 0) { |
| 286 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 287 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 288 | } |
| 289 | return buffer_max(buf); |
| 290 | } |
| 291 | |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 292 | /* |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 293 | * Return the maximum amount of bytes that can be written into the buffer in |
| 294 | * one call to buffer_feed*(). |
| 295 | */ |
| 296 | static inline int buffer_free_space(struct buffer *buf) |
| 297 | { |
| 298 | return buffer_max_len(buf) - buf->l; |
| 299 | } |
| 300 | |
| 301 | /* |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 302 | * Return the max amount of bytes that can be stuffed into the buffer at once. |
| 303 | * Note that this may be lower than the actual buffer size when the free space |
| 304 | * wraps after the end, so it's preferable to call this function again after |
| 305 | * writing. Also note that this function respects max_len. |
| 306 | */ |
| 307 | static inline int buffer_contig_space(struct buffer *buf) |
| 308 | { |
| 309 | int ret; |
| 310 | |
| 311 | if (buf->l == 0) { |
| 312 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 313 | ret = buffer_max_len(buf); |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 314 | } |
| 315 | else if (buf->r > buf->w) { |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 316 | ret = buf->data + buffer_max_len(buf) - buf->r; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 317 | } |
| 318 | else { |
| 319 | ret = buf->w - buf->r; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 320 | if (ret > buffer_max_len(buf)) |
| 321 | ret = buffer_max_len(buf); |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 322 | } |
| 323 | return ret; |
| 324 | } |
| 325 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 326 | /* |
| 327 | * Same as above but the caller may pass the value of buffer_max_len(buf) if it |
| 328 | * knows it, thus avoiding some calculations. |
| 329 | */ |
| 330 | static inline int buffer_contig_space_with_len(struct buffer *buf, int maxlen) |
| 331 | { |
| 332 | int ret; |
| 333 | |
| 334 | if (buf->l == 0) { |
| 335 | buf->r = buf->w = buf->lr = buf->data; |
| 336 | ret = maxlen; |
| 337 | } |
| 338 | else if (buf->r > buf->w) { |
| 339 | ret = buf->data + maxlen - buf->r; |
| 340 | } |
| 341 | else { |
| 342 | ret = buf->w - buf->r; |
| 343 | if (ret > maxlen) |
| 344 | ret = maxlen; |
| 345 | } |
| 346 | return ret; |
| 347 | } |
| 348 | |
Willy Tarreau | 4e33d86 | 2009-10-11 23:35:10 +0200 | [diff] [blame] | 349 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 350 | static inline int buffer_almost_full(struct buffer *buf) |
| 351 | { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 352 | if (buffer_free_space(buf) < buf->size / 4) |
Willy Tarreau | 4e33d86 | 2009-10-11 23:35:10 +0200 | [diff] [blame] | 353 | return 1; |
| 354 | return 0; |
| 355 | } |
| 356 | |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 357 | /* |
| 358 | * Return the max amount of bytes that can be read from the buffer at once. |
| 359 | * Note that this may be lower than the actual buffer length when the data |
| 360 | * wrap after the end, so it's preferable to call this function again after |
| 361 | * reading. Also note that this function respects the send_max limit. |
| 362 | */ |
| 363 | static inline int buffer_contig_data(struct buffer *buf) |
| 364 | { |
| 365 | int ret; |
| 366 | |
| 367 | if (!buf->send_max || !buf->l) |
| 368 | return 0; |
| 369 | |
| 370 | if (buf->r > buf->w) |
| 371 | ret = buf->r - buf->w; |
| 372 | else |
| 373 | ret = buf->data + buf->size - buf->w; |
| 374 | |
| 375 | /* limit the amount of outgoing data if required */ |
| 376 | if (ret > buf->send_max) |
| 377 | ret = buf->send_max; |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Advance the buffer's read pointer by <len> bytes. This is useful when data |
| 384 | * have been read directly from the buffer. It is illegal to call this function |
| 385 | * 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] | 386 | * responsibility to ensure that <len> is never larger than buf->send_max. |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 387 | */ |
| 388 | static inline void buffer_skip(struct buffer *buf, int len) |
| 389 | { |
| 390 | buf->w += len; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 391 | if (buf->w >= buf->data + buf->size) |
| 392 | buf->w -= buf->size; /* wrap around the buffer */ |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 393 | |
| 394 | buf->l -= len; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 395 | if (!buf->l) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 396 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 397 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 398 | if (buf->l < buffer_max_len(buf)) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 399 | buf->flags &= ~BF_FULL; |
| 400 | |
| 401 | buf->send_max -= len; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 402 | if (!buf->send_max && !buf->pipe) |
| 403 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 404 | |
| 405 | /* notify that some data was written to the SI from the buffer */ |
| 406 | buf->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 407 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 408 | |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 409 | /* |
| 410 | * Return one char from the buffer. If the buffer is empty and closed, return -1. |
| 411 | * If the buffer is just empty, return -2. The buffer's pointer is not advanced, |
| 412 | * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char. |
| 413 | * Also note that this function respects the send_max limit. |
| 414 | */ |
| 415 | static inline int buffer_si_peekchar(struct buffer *buf) |
| 416 | { |
| 417 | if (buf->send_max) |
| 418 | return *buf->w; |
| 419 | |
| 420 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 421 | return -1; |
| 422 | else |
| 423 | return -2; |
| 424 | } |
| 425 | |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 426 | /* Try to write character <c> into buffer <buf> after length controls. This |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 427 | * work like buffer_feed2(buf, &c, 1). |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 428 | * Returns non-zero in case of success, 0 if the buffer was full. |
| 429 | * The send limit is automatically adjusted with the amount of data written. |
| 430 | */ |
| 431 | static inline int buffer_si_putchar(struct buffer *buf, char c) |
| 432 | { |
| 433 | if (buf->flags & BF_FULL) |
| 434 | return 0; |
| 435 | |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 436 | *buf->r = c; |
| 437 | |
| 438 | buf->l++; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 439 | if (buf->l >= buffer_max_len(buf)) |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 440 | buf->flags |= BF_FULL; |
| 441 | |
| 442 | buf->r++; |
| 443 | if (buf->r - buf->data == buf->size) |
| 444 | buf->r -= buf->size; |
| 445 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 446 | if (buf->to_forward >= 1) { |
| 447 | if (buf->to_forward != BUF_INFINITE_FORWARD) |
| 448 | buf->to_forward--; |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 449 | buf->send_max++; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 450 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | c77e761 | 2009-09-13 14:58:00 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | buf->total++; |
| 454 | return 1; |
| 455 | } |
| 456 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 457 | int buffer_write(struct buffer *buf, const char *msg, int len); |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 458 | int buffer_feed2(struct buffer *buf, const char *str, int len); |
Willy Tarreau | 36a5c53 | 2009-09-03 07:13:50 +0200 | [diff] [blame] | 459 | int buffer_si_putchar(struct buffer *buf, char c); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 460 | int buffer_si_peekline(struct buffer *buf, char *str, int len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 461 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); |
| 462 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 463 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 464 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 465 | void buffer_bounce_realign(struct buffer *buf); |
| 466 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 467 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 468 | |
| 469 | /* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success, |
| 470 | * -2 if it is larger than the buffer size, or the number of bytes available |
| 471 | * otherwise. If the chunk has been written, its size is automatically reset |
| 472 | * to zero. The send limit is automatically adjusted with the amount of data |
| 473 | * written. |
| 474 | */ |
| 475 | static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk) |
| 476 | { |
| 477 | int ret; |
| 478 | |
| 479 | ret = buffer_write(buf, chunk->str, chunk->len); |
| 480 | if (ret == -1) |
| 481 | chunk->len = 0; |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | /* Try to write chunk <chunk> into buffer <buf> after length controls. This is |
| 486 | * the equivalent of buffer_write_chunk() except that to_forward and send_max |
| 487 | * are updated and that max_len is respected. Returns -1 in case of success, |
| 488 | * -2 if it is larger than the buffer size, or the number of bytes available |
| 489 | * otherwise. If the chunk has been written, its size is automatically reset |
| 490 | * to zero. The send limit is automatically adjusted with the amount of data |
| 491 | * written. |
| 492 | */ |
| 493 | static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *chunk) |
| 494 | { |
| 495 | int ret; |
| 496 | |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 497 | ret = buffer_feed2(buf, chunk->str, chunk->len); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 498 | if (ret == -1) |
| 499 | chunk->len = 0; |
| 500 | return ret; |
| 501 | } |
| 502 | |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 503 | /* Try to write string <str> into buffer <buf> after length controls. This is |
| 504 | * the equivalent of buffer_feed2() except that string length is measured by |
| 505 | * the function. Returns -1 in case of success, -2 if it is larger than the |
| 506 | * buffer size, or the number of bytes available otherwise. The send limit is |
| 507 | * automatically adjusted with the amount of data written. |
| 508 | */ |
| 509 | static inline int buffer_feed(struct buffer *buf, const char *str) |
| 510 | { |
| 511 | return buffer_feed2(buf, str, strlen(str)); |
| 512 | } |
| 513 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 514 | static inline void chunk_init(struct chunk *chk, char *str, size_t size) { |
| 515 | chk->str = str; |
| 516 | chk->len = 0; |
| 517 | chk->size = size; |
| 518 | } |
| 519 | |
| 520 | /* report 0 in case of error, 1 if OK. */ |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 521 | 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] | 522 | |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 523 | if (size && len > size) |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 524 | return 0; |
| 525 | |
| 526 | chk->str = str; |
| 527 | chk->len = len; |
| 528 | chk->size = size; |
| 529 | |
| 530 | return 1; |
| 531 | } |
| 532 | |
| 533 | static inline void chunk_initstr(struct chunk *chk, char *str) { |
| 534 | chk->str = str; |
| 535 | chk->len = strlen(str); |
| 536 | chk->size = 0; /* mark it read-only */ |
| 537 | } |
| 538 | |
| 539 | static inline int chunk_strcpy(struct chunk *chk, const char *str) { |
| 540 | size_t len; |
| 541 | |
| 542 | len = strlen(str); |
| 543 | |
| 544 | if (unlikely(len > chk->size)) |
| 545 | return 0; |
| 546 | |
| 547 | chk->len = len; |
| 548 | memcpy(chk->str, str, len); |
| 549 | |
| 550 | return 1; |
| 551 | } |
| 552 | |
| 553 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 554 | __attribute__ ((format(printf, 2, 3))); |
| 555 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 556 | int chunk_htmlencode(struct chunk *dst, struct chunk *src); |
| 557 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc); |
| 558 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 559 | static inline void chunk_reset(struct chunk *chk) { |
| 560 | chk->str = NULL; |
| 561 | chk->len = -1; |
| 562 | chk->size = 0; |
| 563 | } |
| 564 | |
| 565 | static inline void chunk_destroy(struct chunk *chk) { |
| 566 | |
| 567 | if (!chk->size) |
| 568 | return; |
| 569 | |
| 570 | if (chk->str) |
| 571 | free(chk->str); |
| 572 | |
| 573 | chunk_reset(chk); |
| 574 | } |
| 575 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 576 | /* |
| 577 | * frees the destination chunk if already allocated, allocates a new string, |
| 578 | * and copies the source into it. The pointer to the destination string is |
| 579 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 580 | */ |
| 581 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 582 | if (!dst || !src || !src->str) |
| 583 | return NULL; |
| 584 | if (dst->str) |
| 585 | free(dst->str); |
| 586 | dst->len = src->len; |
| 587 | dst->str = (char *)malloc(dst->len); |
| 588 | memcpy(dst->str, src->str, dst->len); |
| 589 | return dst->str; |
| 590 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 591 | |
| 592 | #endif /* _PROTO_BUFFERS_H */ |
| 593 | |
| 594 | /* |
| 595 | * Local variables: |
| 596 | * c-indent-level: 8 |
| 597 | * c-basic-offset: 8 |
| 598 | * End: |
| 599 | */ |