blob: dc9961a2bd97d4ab6f91bbb40c2f887aca82d93a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/buffers.h
3 Buffer management definitions, macros and inline functions.
4
Willy Tarreauba392ce2008-08-16 21:13:23 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +01006
Willy Tarreaubaaee002006-06-26 02:48:02 +02007 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 Tarreau7341d942007-05-13 19:56:02 +020025#include <stdio.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010026#include <stdlib.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020027#include <string.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010028
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020029#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020030#include <common/memory.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020031#include <common/ticks.h>
Willy Tarreaufa645582007-06-03 15:59:52 +020032#include <common/time.h>
33
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/buffers.h>
35
Willy Tarreau7341d942007-05-13 19:56:02 +020036extern struct pool_head *pool2_buffer;
37
38/* perform minimal intializations, report 0 in case of error, 1 if OK. */
39int init_buffer();
40
Willy Tarreau03d60bb2009-01-09 11:13:00 +010041/* Initializes all fields in the buffer. The ->max_len field is initialized last
Willy Tarreau54469402006-07-29 16:59:06 +020042 * so that the compiler can optimize it away if changed immediately after the
Willy Tarreaue393fe22008-08-16 22:18:07 +020043 * call to this function. By default, it is set to the full size of the buffer.
44 * The BF_EMPTY flags is set.
Willy Tarreau54469402006-07-29 16:59:06 +020045 */
46static inline void buffer_init(struct buffer *buf)
47{
Willy Tarreauf890dc92008-12-13 21:12:26 +010048 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010049 buf->to_forward = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +020050 buf->l = buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010051 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020052 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020053 buf->cons = NULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +020054 buf->flags = BF_EMPTY;
Willy Tarreau2df28e82008-08-17 15:20:19 +020055 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreau03d60bb2009-01-09 11:13:00 +010056 buf->max_len = BUFSIZE;
Willy Tarreau54469402006-07-29 16:59:06 +020057}
58
Willy Tarreaubaaee002006-06-26 02:48:02 +020059/* returns 1 if the buffer is empty, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020060static inline int buffer_isempty(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020061{
62 return buf->l == 0;
63}
64
65/* returns 1 if the buffer is full, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020066static inline int buffer_isfull(const struct buffer *buf) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 return buf->l == BUFSIZE;
68}
69
Willy Tarreau2eb52f02008-09-04 09:14:08 +020070/* Check buffer timeouts, and set the corresponding flags. The
71 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +010072 * 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 Tarreau2eb52f02008-09-04 09:14:08 +020075 */
76static inline void buffer_check_timeouts(struct buffer *b)
77{
Willy Tarreau86491c32008-12-14 09:04:47 +010078 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +020079 unlikely(tick_is_expired(b->rex, now_ms)))
80 b->flags |= BF_READ_TIMEOUT;
81
Willy Tarreaudd80c6f2008-12-13 22:25:59 +010082 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +020083 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 Tarreau0abebcc2009-01-08 00:09:41 +010091/* 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 */
98static 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 Tarreaue8a28bf2009-03-08 21:12:04 +0100111/* 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 */
115static inline void buffer_flush(struct buffer *buf)
116{
117 if (buf->send_max < buf->l)
118 buf->send_max = buf->l;
119}
120
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100121/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100122 * that any spliced data is not affected since we may not have any access to
123 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200124 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100125static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100127 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100128 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100129 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130 buf->l = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200131 buf->flags |= BF_EMPTY | BF_FULL;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100132 if (buf->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200133 buf->flags &= ~BF_FULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134}
135
Willy Tarreauba392ce2008-08-16 21:13:23 +0200136/* marks the buffer as "shutdown" for reads and cancels the timeout */
Willy Tarreaufa645582007-06-03 15:59:52 +0200137static inline void buffer_shutr(struct buffer *buf)
138{
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200139 buf->rex = TICK_ETERNITY;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200140 buf->flags |= BF_SHUTR;
Willy Tarreaufa645582007-06-03 15:59:52 +0200141}
142
Willy Tarreauba392ce2008-08-16 21:13:23 +0200143/* marks the buffer as "shutdown" for writes and cancels the timeout */
Willy Tarreaufa645582007-06-03 15:59:52 +0200144static inline void buffer_shutw(struct buffer *buf)
145{
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200146 buf->wex = TICK_ETERNITY;
Willy Tarreauba392ce2008-08-16 21:13:23 +0200147 buf->flags |= BF_SHUTW;
Willy Tarreaufa645582007-06-03 15:59:52 +0200148}
149
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200150/* marks the buffer as "shutdown" ASAP for reads */
151static inline void buffer_shutr_now(struct buffer *buf)
152{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100153 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200154}
155
156/* marks the buffer as "shutdown" ASAP for writes */
157static 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 */
163static inline void buffer_abort(struct buffer *buf)
164{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100165 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200166}
167
Willy Tarreau01bf8672008-12-07 18:03:29 +0100168/* 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 */
173static inline void buffer_install_hijacker(struct session *s,
174 struct buffer *b,
175 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200176{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100177 b->hijacker = func;
178 b->flags |= BF_HIJACK;
179 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200180}
181
Willy Tarreau01bf8672008-12-07 18:03:29 +0100182/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200183static inline void buffer_stop_hijack(struct buffer *buf)
184{
185 buf->flags &= ~BF_HIJACK;
186}
187
Willy Tarreau3da77c52008-08-29 09:58:42 +0200188/* allows the consumer to send the buffer contents */
189static 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 */
195static inline void buffer_write_dis(struct buffer *buf)
196{
197 buf->flags &= ~BF_WRITE_ENA;
198}
199
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100200/* 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 */
204static 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 Tarreau0a5d5dd2008-11-23 19:31:35 +0100215 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 */
222static 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 Tarreau0a5d5dd2008-11-23 19:31:35 +0100234 b->cons->shutw(b->cons);
235 }
236}
237
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200239static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240{
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 Tarreaue393fe22008-08-16 22:18:07 +0200249/* sets the buffer read limit to <size> bytes, and adjusts the FULL
250 * flag accordingly.
251 */
252static inline void buffer_set_rlim(struct buffer *buf, int size)
253{
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100254 buf->max_len = size;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200255 if (buf->l < size)
256 buf->flags &= ~BF_FULL;
257 else
258 buf->flags |= BF_FULL;
259}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260
261/*
262 * Tries to realign the given buffer, and returns how many bytes can be written
263 * there at once without overwriting anything.
264 */
265static inline int buffer_realign(struct buffer *buf)
266{
267 if (buf->l == 0) {
268 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100269 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 }
271 return buffer_max(buf);
272}
273
274
275int buffer_write(struct buffer *buf, const char *msg, int len);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100276int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100277int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
278int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
279int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreau40d25162009-04-03 12:01:47 +0200280int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
281 __attribute__ ((format(printf, 3, 4)));
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100282void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283
Willy Tarreau0f772532006-12-23 20:51:41 +0100284/*
285 * frees the destination chunk if already allocated, allocates a new string,
286 * and copies the source into it. The pointer to the destination string is
287 * returned, or NULL if the allocation fails or if any pointer is NULL..
288 */
289static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
290 if (!dst || !src || !src->str)
291 return NULL;
292 if (dst->str)
293 free(dst->str);
294 dst->len = src->len;
295 dst->str = (char *)malloc(dst->len);
296 memcpy(dst->str, src->str, dst->len);
297 return dst->str;
298}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299
300#endif /* _PROTO_BUFFERS_H */
301
302/*
303 * Local variables:
304 * c-indent-level: 8
305 * c-basic-offset: 8
306 * End:
307 */