blob: faad019b2e02465aa47ff54872d5a45592a8ae37 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/types/buffers.h
3 Buffer management definitions, macros and inline functions.
4
Willy Tarreau0c303ee2008-07-07 00:09:58 +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 _TYPES_BUFFERS_H
23#define _TYPES_BUFFERS_H
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/memory.h>
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +010027#include <types/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau54469402006-07-29 16:59:06 +020029/* The BF_* macros designate Buffer Flags, which may be ORed in the bit field
Willy Tarreau3da77c52008-08-29 09:58:42 +020030 * member 'flags' in struct buffer. Here we have several types of flags :
31 *
32 * - pure status flags, reported by the lower layer, which must be cleared
33 * before doing further I/O :
34 * BF_*_NULL, BF_*_PARTIAL
35 *
36 * - pure status flags, reported by mid-layer, which must also be cleared
37 * before doing further I/O :
38 * BF_*_TIMEOUT, BF_*_ERROR
39 *
40 * - read-only indicators reported by lower levels :
41 * BF_STREAMER, BF_STREAMER_FAST
42 *
43 * - write-once status flags reported by the mid-level : BF_SHUTR, BF_SHUTW
44 *
45 * - persistent control flags managed only by higher level :
46 * BF_SHUT*_NOW, BF_*_ENA, BF_HIJACK
47 *
48 * The flags have been arranged for readability, so that the read and write
49 * bits have se same position in a byte (read being the lower byte and write
50 * the second one).
Willy Tarreau54469402006-07-29 16:59:06 +020051 */
Willy Tarreaue393fe22008-08-16 22:18:07 +020052
Willy Tarreau3da77c52008-08-29 09:58:42 +020053#define BF_READ_NULL 0x000001 /* last read detected on producer side */
54#define BF_READ_PARTIAL 0x000002 /* some data were read from producer */
55#define BF_READ_TIMEOUT 0x000004 /* timeout while waiting for producer */
56#define BF_READ_ERROR 0x000008 /* unrecoverable error on producer side */
57#define BF_READ_ACTIVITY (BF_READ_NULL|BF_READ_PARTIAL|BF_READ_ERROR)
Willy Tarreau0f9f5052006-07-29 17:39:25 +020058
Willy Tarreau03d60bb2009-01-09 11:13:00 +010059#define BF_FULL 0x000010 /* buffer cannot accept any more data (l >= max_len) */
Willy Tarreau3da77c52008-08-29 09:58:42 +020060#define BF_SHUTR 0x000020 /* producer has already shut down */
61#define BF_SHUTR_NOW 0x000040 /* the producer must shut down for reads immediately */
Willy Tarreau86491c32008-12-14 09:04:47 +010062#define BF_READ_NOEXP 0x000080 /* producer should not expire */
Willy Tarreau54469402006-07-29 16:59:06 +020063
Willy Tarreau3da77c52008-08-29 09:58:42 +020064#define BF_WRITE_NULL 0x000100 /* write(0) or connect() succeeded on consumer side */
65#define BF_WRITE_PARTIAL 0x000200 /* some data were written to the consumer */
66#define BF_WRITE_TIMEOUT 0x000400 /* timeout while waiting for consumer */
67#define BF_WRITE_ERROR 0x000800 /* unrecoverable error on consumer side */
68#define BF_WRITE_ACTIVITY (BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_WRITE_ERROR)
Willy Tarreau54469402006-07-29 16:59:06 +020069
Willy Tarreau3da77c52008-08-29 09:58:42 +020070#define BF_EMPTY 0x001000 /* buffer is empty */
71#define BF_SHUTW 0x002000 /* consumer has already shut down */
72#define BF_SHUTW_NOW 0x004000 /* the consumer must shut down for writes immediately */
73#define BF_WRITE_ENA 0x008000 /* consumer is allowed to forward all buffer contents */
Willy Tarreau54469402006-07-29 16:59:06 +020074
Willy Tarreau3da77c52008-08-29 09:58:42 +020075#define BF_STREAMER 0x010000 /* the producer is identified as streaming data */
76#define BF_STREAMER_FAST 0x020000 /* the consumer seems to eat the stream very fast */
Willy Tarreau0f9f5052006-07-29 17:39:25 +020077
Willy Tarreaufa7e1022008-10-19 07:30:41 +020078/* When either BF_SHUTR_NOW or BF_HIJACK is set, it is strictly forbidden for
79 * the stream interface to alter the buffer contents. When BF_SHUTW_NOW is set,
80 * it is strictly forbidden for the stream interface to send anything from the
81 * buffer.
82 */
Willy Tarreau01bf8672008-12-07 18:03:29 +010083#define BF_HIJACK 0x040000 /* the producer is temporarily replaced by ->hijacker */
Willy Tarreau9a2d1542008-08-30 12:31:07 +020084#define BF_ANA_TIMEOUT 0x080000 /* the analyser timeout has expired */
85#define BF_READ_ATTACHED 0x100000 /* the read side is attached for the first time */
Willy Tarreau259de1b2009-01-18 21:56:21 +010086#define BF_KERN_SPLICING 0x200000 /* kernel splicing desired for this buffer */
Willy Tarreau1b194fe2009-03-21 21:10:04 +010087#define BF_READ_DONTWAIT 0x400000 /* wake the task up after every read (eg: HTTP request) */
Willy Tarreau9a2d1542008-08-30 12:31:07 +020088
89/* Use these masks to clear the flags before going back to lower layers */
90#define BF_CLEAR_READ (~(BF_READ_NULL|BF_READ_PARTIAL|BF_READ_ERROR|BF_READ_ATTACHED))
91#define BF_CLEAR_WRITE (~(BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_WRITE_ERROR))
92#define BF_CLEAR_TIMEOUT (~(BF_READ_TIMEOUT|BF_WRITE_TIMEOUT|BF_ANA_TIMEOUT))
Willy Tarreaufa7e1022008-10-19 07:30:41 +020093
Willy Tarreaufe3718a2008-11-30 18:14:12 +010094/* Masks which define input events for stream analysers */
Willy Tarreau4ffd51a2008-08-30 13:36:43 +020095#define BF_MASK_ANALYSER (BF_READ_ATTACHED|BF_READ_ACTIVITY|BF_READ_TIMEOUT|BF_ANA_TIMEOUT|BF_WRITE_ACTIVITY)
Willy Tarreaufe3718a2008-11-30 18:14:12 +010096
97/* Mask for static flags which are not events, but might change during processing */
Willy Tarreau86491c32008-12-14 09:04:47 +010098#define BF_MASK_STATIC (BF_EMPTY|BF_FULL|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR|BF_SHUTW|BF_SHUTR_NOW|BF_SHUTW_NOW)
Willy Tarreau3da77c52008-08-29 09:58:42 +020099
Willy Tarreau2df28e82008-08-17 15:20:19 +0200100
101/* Analysers (buffer->analysers).
102 * Those bits indicate that there are some processing to do on the buffer
Willy Tarreau70cb6332008-12-07 11:28:08 +0100103 * contents. It will probably evolve into a linked list later. Those
Willy Tarreau2df28e82008-08-17 15:20:19 +0200104 * analysers could be compared to higher level processors.
105 * The field is blanked by buffer_init() and only by analysers themselves
106 * afterwards.
107 */
108#define AN_REQ_INSPECT 0x00000001 /* inspect request contents */
109#define AN_REQ_HTTP_HDR 0x00000002 /* inspect HTTP request headers */
110#define AN_REQ_HTTP_BODY 0x00000004 /* inspect HTTP request body */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200111#define AN_REQ_HTTP_TARPIT 0x00000008 /* wait for end of HTTP tarpit */
112#define AN_RTR_HTTP_HDR 0x00000010 /* inspect HTTP response headers */
Willy Tarreau70cb6332008-12-07 11:28:08 +0100113#define AN_REQ_UNIX_STATS 0x00000020 /* process unix stats socket request */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200114
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115/* describes a chunk of string */
116struct chunk {
117 char *str; /* beginning of the string itself. Might not be 0-terminated */
118 int len; /* size of the string from first to last char. <0 = uninit. */
119};
120
Willy Tarreau01bf8672008-12-07 18:03:29 +0100121/* needed for a declaration below */
122struct session;
123
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124struct buffer {
Willy Tarreauaad2e492006-10-15 23:32:18 +0200125 unsigned int flags; /* BF_* */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200126 int rex; /* expiration date for a read, in ticks */
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200127 int wex; /* expiration date for a write or connect, in ticks */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200128 int rto; /* read timeout, in ticks */
129 int wto; /* write timeout, in ticks */
130 int cto; /* connect timeout, in ticks */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 unsigned int l; /* data length */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100132 char *r, *w, *lr; /* read ptr, write ptr, last read */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100133 unsigned int max_len; /* read limit, used to keep room for header rewriting */
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100134 unsigned int send_max; /* number of bytes the sender can consume om this buffer, <= l */
135 unsigned int to_forward; /* number of bytes to forward after send_max without a wake-up */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200136 unsigned int analysers; /* bit field indicating what to do on the buffer */
Willy Tarreauffab5b42008-08-17 18:03:28 +0200137 int analyse_exp; /* expiration date for current analysers (if set) */
Willy Tarreau01bf8672008-12-07 18:03:29 +0100138 void (*hijacker)(struct session *, struct buffer *); /* alternative content producer */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200139 unsigned char xfer_large; /* number of consecutive large xfers */
140 unsigned char xfer_small; /* number of consecutive small xfers */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141 unsigned long long total; /* total data read */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200142 struct stream_interface *prod; /* producer attached to this buffer */
143 struct stream_interface *cons; /* consumer attached to this buffer */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100144 struct pipe *pipe; /* non-NULL only when data present */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 char data[BUFSIZE];
146};
147
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100149/* Note about the buffer structure
150
151 The buffer contains two length indicators, one to_forward counter and one
152 send_max limit. First, it must be understood that the buffer is in fact
153 split in two parts :
154 - the visible data (->data, for ->l bytes)
155 - the invisible data, typically in kernel buffers forwarded directly from
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100156 the source stream sock to the destination stream sock (->pipe->data
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100157 bytes). Those are used only during forward.
158
159 In order not to mix data streams, the producer may only feed the invisible
160 data with data to forward, and only when the visible buffer is empty. The
161 consumer may not always be able to feed the invisible buffer due to platform
162 limitations (lack of kernel support).
163
164 Conversely, the consumer must always take data from the invisible data first
165 before ever considering visible data. There is no limit to the size of data
166 to consume from the invisible buffer, as platform-specific implementations
167 will rarely leave enough control on this. So any byte fed into the invisible
168 buffer is expected to reach the destination file descriptor, by any means.
169 However, it's the consumer's responsibility to ensure that the invisible
170 data has been entirely consumed before consuming visible data. This must be
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100171 reflected by ->pipe->data. This is very important as this and only this can
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100172 ensure strict ordering of data between buffers.
173
174 The producer is responsible for decreasing ->to_forward and increasing
175 ->send_max. The ->to_forward parameter indicates how many bytes may be fed
176 into either data buffer without waking the parent up. The ->send_max
177 parameter says how many bytes may be read from the visible buffer. Thus it
178 may never exceed ->l. This parameter is updated by any buffer_write() as
179 well as any data forwarded through the visible buffer.
180
181 The consumer is responsible for decreasing ->send_max when it sends data
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100182 from the visible buffer, and ->pipe->data when it sends data from the
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100183 invisible buffer.
184
185 A real-world example consists in part in an HTTP response waiting in a
186 buffer to be forwarded. We know the header length (300) and the amount of
187 data to forward (content-length=9000). The buffer already contains 1000
188 bytes of data after the 300 bytes of headers. Thus the caller will set
189 ->send_max to 300 indicating that it explicitly wants to send those data,
190 and set ->to_forward to 9000 (content-length). This value must be normalised
191 immediately after updating ->to_forward : since there are already 1300 bytes
192 in the buffer, 300 of which are already counted in ->send_max, and that size
193 is smaller than ->to_forward, we must update ->send_max to 1300 to flush the
194 whole buffer, and reduce ->to_forward to 8000. After that, the producer may
195 try to feed the additional data through the invisible buffer using a
196 platform-specific method such as splice().
197 */
198
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199#endif /* _TYPES_BUFFERS_H */
200
201/*
202 * Local variables:
203 * c-indent-level: 8
204 * c-basic-offset: 8
205 * End:
206 */