blob: a21e13f0296292ea480d1f15457b1c9f8fc9c4da [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauc7e42382012-08-24 19:22:53 +02002 * include/types/channel.h
3 * Channel management definitions, macros and inline functions.
Willy Tarreau7c3c5412009-12-13 15:53:05 +01004 *
Willy Tarreauc7e42382012-08-24 19:22:53 +02005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau7c3c5412009-12-13 15:53:05 +01006 *
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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreauc7e42382012-08-24 19:22:53 +020022#ifndef _TYPES_CHANNEL_H
23#define _TYPES_CHANNEL_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020026#include <common/chunk.h>
27#include <common/buffer.h>
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +010028#include <types/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020030/* The CF_* macros designate Channel Flags, which may be ORed in the bit field
31 * member 'flags' in struct channel. Here we have several types of flags :
Willy Tarreau3da77c52008-08-29 09:58:42 +020032 *
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020033 * - pure status flags, reported by the data layer, which must be cleared
Willy Tarreau3da77c52008-08-29 09:58:42 +020034 * before doing further I/O :
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020035 * CF_*_NULL, CF_*_PARTIAL
Willy Tarreau3da77c52008-08-29 09:58:42 +020036 *
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020037 * - pure status flags, reported by stream-interface layer, which must also
38 * be cleared before doing further I/O :
39 * CF_*_TIMEOUT, CF_*_ERROR
Willy Tarreau3da77c52008-08-29 09:58:42 +020040 *
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020041 * - read-only indicators reported by lower data levels :
42 * CF_STREAMER, CF_STREAMER_FAST
Willy Tarreau3da77c52008-08-29 09:58:42 +020043 *
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020044 * - write-once status flags reported by the stream-interface layer :
45 * CF_SHUTR, CF_SHUTW
Willy Tarreau3da77c52008-08-29 09:58:42 +020046 *
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020047 * - persistent control flags managed only by application level :
48 * CF_SHUT*_NOW, CF_*_ENA, CF_HIJACK
Willy Tarreau3da77c52008-08-29 09:58:42 +020049 *
50 * The flags have been arranged for readability, so that the read and write
Willy Tarreau418fd472009-09-06 21:37:23 +020051 * bits have the same position in a byte (read being the lower byte and write
Willy Tarreauc7e42382012-08-24 19:22:53 +020052 * the second one). All flag names are relative to the channel. For instance,
53 * 'write' indicates the direction from the channel to the stream interface.
Willy Tarreau54469402006-07-29 16:59:06 +020054 */
Willy Tarreaue393fe22008-08-16 22:18:07 +020055
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020056#define CF_READ_NULL 0x00000001 /* last read detected on producer side */
57#define CF_READ_PARTIAL 0x00000002 /* some data were read from producer */
58#define CF_READ_TIMEOUT 0x00000004 /* timeout while waiting for producer */
59#define CF_READ_ERROR 0x00000008 /* unrecoverable error on producer side */
60#define CF_READ_ACTIVITY (CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ERROR)
Willy Tarreau0f9f5052006-07-29 17:39:25 +020061
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020062/* unused: 0x00000010 */
63#define CF_SHUTR 0x00000020 /* producer has already shut down */
64#define CF_SHUTR_NOW 0x00000040 /* the producer must shut down for reads ASAP */
65#define CF_READ_NOEXP 0x00000080 /* producer should not expire */
Willy Tarreau54469402006-07-29 16:59:06 +020066
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020067#define CF_WRITE_NULL 0x00000100 /* write(0) or connect() succeeded on consumer side */
68#define CF_WRITE_PARTIAL 0x00000200 /* some data were written to the consumer */
69#define CF_WRITE_TIMEOUT 0x00000400 /* timeout while waiting for consumer */
70#define CF_WRITE_ERROR 0x00000800 /* unrecoverable error on consumer side */
71#define CF_WRITE_ACTIVITY (CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_WRITE_ERROR)
Willy Tarreau54469402006-07-29 16:59:06 +020072
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020073/* unused: 0x00001000 */
74#define CF_SHUTW 0x00002000 /* consumer has already shut down */
75#define CF_SHUTW_NOW 0x00004000 /* the consumer must shut down for writes ASAP */
76#define CF_AUTO_CLOSE 0x00008000 /* producer can forward shutdown to other side */
Willy Tarreau54469402006-07-29 16:59:06 +020077
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020078/* When either CF_SHUTR_NOW or CF_HIJACK is set, it is strictly forbidden for
79 * the producer to alter the buffer contents. When CF_SHUTW_NOW is set, the
Willy Tarreau418fd472009-09-06 21:37:23 +020080 * consumer is free to perform a shutw() when it has consumed the last contents,
81 * otherwise the session processor will do it anyway.
82 *
83 * The SHUT* flags work like this :
84 *
85 * SHUTR SHUTR_NOW meaning
86 * 0 0 normal case, connection still open and data is being read
87 * 0 1 closing : the producer cannot feed data anymore but can close
88 * 1 0 closed: the producer has closed its input channel.
89 * 1 1 impossible
90 *
91 * SHUTW SHUTW_NOW meaning
92 * 0 0 normal case, connection still open and data is being written
93 * 0 1 closing: the consumer can send last data and may then close
94 * 1 0 closed: the consumer has closed its output channel.
95 * 1 1 impossible
96 *
Willy Tarreau520d95e2009-09-19 21:04:57 +020097 * The SHUTW_NOW flag should be set by the session processor when SHUTR and AUTO_CLOSE
Willy Tarreau418fd472009-09-06 21:37:23 +020098 * are both set. It may also be set by a hijacker at the end of data. And it may also
99 * be set by the producer when it detects SHUTR while directly forwarding data to the
100 * consumer.
101 *
102 * The SHUTR_NOW flag is mostly used to force the producer to abort when an error is
103 * detected on the consumer side.
104 */
105
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200106#define CF_STREAMER 0x00010000 /* the producer is identified as streaming data */
107#define CF_STREAMER_FAST 0x00020000 /* the consumer seems to eat the stream very fast */
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200108
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200109#define CF_HIJACK 0x00040000 /* the producer is temporarily replaced by ->hijacker */
110#define CF_ANA_TIMEOUT 0x00080000 /* the analyser timeout has expired */
111#define CF_READ_ATTACHED 0x00100000 /* the read side is attached for the first time */
112#define CF_KERN_SPLICING 0x00200000 /* kernel splicing desired for this channel */
113#define CF_READ_DONTWAIT 0x00400000 /* wake the task up after every read (eg: HTTP request) */
114#define CF_AUTO_CONNECT 0x00800000 /* consumer may attempt to establish a new connection */
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200115
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200116#define CF_DONT_READ 0x01000000 /* disable reading for now */
117#define CF_EXPECT_MORE 0x02000000 /* more data expected to be sent very soon (one-shoot) */
118#define CF_SEND_DONTWAIT 0x04000000 /* don't wait for sending data (one-shoot) */
119#define CF_NEVER_WAIT 0x08000000 /* never wait for sending data (permanent) */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200120
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200121#define CF_WAKE_ONCE 0x10000000 /* pretend there is activity on this channel (one-shoot) */
122/* unused: 0x20000000, 0x20000000, 0x80000000 */
Willy Tarreau0499e352010-12-17 07:13:42 +0100123
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200124/* Use these masks to clear the flags before going back to lower layers */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200125#define CF_CLEAR_READ (~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ERROR|CF_READ_ATTACHED))
126#define CF_CLEAR_WRITE (~(CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_WRITE_ERROR))
127#define CF_CLEAR_TIMEOUT (~(CF_READ_TIMEOUT|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200128
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100129/* Masks which define input events for stream analysers */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200130#define CF_MASK_ANALYSER (CF_READ_ATTACHED|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_ANA_TIMEOUT|CF_WRITE_ACTIVITY|CF_WAKE_ONCE)
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100131
Willy Tarreau1d315ea2010-06-04 12:25:31 +0200132/* Mask for static flags which cause analysers to be woken up when they change */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200133#define CF_MASK_STATIC (CF_SHUTR|CF_SHUTW|CF_SHUTR_NOW|CF_SHUTW_NOW)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200134
Willy Tarreau2df28e82008-08-17 15:20:19 +0200135
Willy Tarreauc7e42382012-08-24 19:22:53 +0200136/* Analysers (channel->analysers).
Willy Tarreau2df28e82008-08-17 15:20:19 +0200137 * Those bits indicate that there are some processing to do on the buffer
Willy Tarreau70cb6332008-12-07 11:28:08 +0100138 * contents. It will probably evolve into a linked list later. Those
Willy Tarreau2df28e82008-08-17 15:20:19 +0200139 * analysers could be compared to higher level processors.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200140 * The field is blanked by channel_init() and only by analysers themselves
Willy Tarreau2df28e82008-08-17 15:20:19 +0200141 * afterwards.
142 */
Willy Tarreau74172ff2012-08-31 17:52:35 +0200143/* unused: 0x00000001 */
Willy Tarreau6e595772010-10-15 14:12:12 +0200144#define AN_REQ_INSPECT_FE 0x00000002 /* inspect request contents in the frontend */
145#define AN_REQ_WAIT_HTTP 0x00000004 /* wait for an HTTP request */
146#define AN_REQ_HTTP_PROCESS_FE 0x00000008 /* process the frontend's HTTP part */
147#define AN_REQ_SWITCHING_RULES 0x00000010 /* apply the switching rules */
148#define AN_REQ_INSPECT_BE 0x00000020 /* inspect request contents in the backend */
149#define AN_REQ_HTTP_PROCESS_BE 0x00000040 /* process the backend's HTTP part */
Willy Tarreau4a5cade2012-04-05 21:09:48 +0200150#define AN_REQ_SRV_RULES 0x00000080 /* use-server rules */
151#define AN_REQ_HTTP_INNER 0x00000100 /* inner processing of HTTP request */
152#define AN_REQ_HTTP_TARPIT 0x00000200 /* wait for end of HTTP tarpit */
153#define AN_REQ_HTTP_BODY 0x00000400 /* inspect HTTP request body */
154#define AN_REQ_STICKING_RULES 0x00000800 /* table persistence matching */
155#define AN_REQ_PRST_RDP_COOKIE 0x00001000 /* persistence on rdp cookie */
156#define AN_REQ_HTTP_XFER_BODY 0x00002000 /* forward request body */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200157
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200158/* response analysers */
159#define AN_RES_INSPECT 0x00010000 /* content inspection */
160#define AN_RES_WAIT_HTTP 0x00020000 /* wait for HTTP response */
161#define AN_RES_HTTP_PROCESS_BE 0x00040000 /* process backend's HTTP part */
162#define AN_RES_HTTP_PROCESS_FE 0x00040000 /* process frontend's HTTP part (same for now) */
Emeric Brun1d33b292010-01-04 15:47:17 +0100163#define AN_RES_STORE_RULES 0x00080000 /* table persistence matching */
Willy Tarreaud98cf932009-12-27 22:54:55 +0100164#define AN_RES_HTTP_XFER_BODY 0x00100000 /* forward response body */
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200165
166
Willy Tarreau31971e52009-09-20 12:07:52 +0200167/* Magic value to forward infinite size (TCP, ...), used with ->to_forward */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200168#define CHN_INFINITE_FORWARD MAX_RANGE(int)
Willy Tarreau31971e52009-09-20 12:07:52 +0200169
Willy Tarreau01bf8672008-12-07 18:03:29 +0100170/* needed for a declaration below */
171struct session;
172
Willy Tarreau7421efb2012-07-02 15:11:27 +0200173struct channel {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200174 unsigned int flags; /* CF_* */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200175 int rex; /* expiration date for a read, in ticks */
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200176 int wex; /* expiration date for a write or connect, in ticks */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200177 int rto; /* read timeout, in ticks */
178 int wto; /* write timeout, in ticks */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100179 unsigned int to_forward; /* number of bytes to forward after out without a wake-up */
Willy Tarreauc7e42382012-08-24 19:22:53 +0200180 unsigned int analysers; /* bit field indicating what to do on the channel */
Willy Tarreauffab5b42008-08-17 18:03:28 +0200181 int analyse_exp; /* expiration date for current analysers (if set) */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200182 void (*hijacker)(struct session *, struct channel *); /* alternative content producer */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200183 unsigned char xfer_large; /* number of consecutive large xfers */
184 unsigned char xfer_small; /* number of consecutive small xfers */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 unsigned long long total; /* total data read */
Willy Tarreauc7e42382012-08-24 19:22:53 +0200186 struct stream_interface *prod; /* producer attached to this channel */
187 struct stream_interface *cons; /* consumer attached to this channel */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100188 struct pipe *pipe; /* non-NULL only when data present */
Willy Tarreau572bf902012-07-02 17:01:20 +0200189 struct buffer buf; /* embedded buffer for now, will move */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190};
191
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200192
193/* Note about the channel structure
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200195 A channel stores information needed to reliably transport data in a single
196 direction. It stores status flags, timeouts, counters, subscribed analysers,
197 pointers to a data producer and to a data consumer, and information about
198 the amount of data which is allowed to flow directly from the producer to
199 the consumer without waking up the analysers.
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100200
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200201 A channel may buffer data into two locations :
202 - a visible buffer (->buf)
203 - an invisible buffer which right now consists in a pipe making use of
204 kernel buffers that cannot be tampered with.
205
206 Data stored into the first location may be analysed and altered by analysers
207 while data stored in pipes is only aimed at being transported from one
208 network socket to another one without being subject to memory copies. This
209 buffer may only be used when both the socket layer and the data layer of the
210 producer and the consumer support it, which typically is the case with Linux
211 splicing over sockets, and when there are enough data to be transported
212 without being analyzed (transport of TCP/HTTP payload or tunnelled data,
213 which is indicated by ->to_forward).
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100214
215 In order not to mix data streams, the producer may only feed the invisible
216 data with data to forward, and only when the visible buffer is empty. The
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100217 producer may not always be able to feed the invisible buffer due to platform
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100218 limitations (lack of kernel support).
219
220 Conversely, the consumer must always take data from the invisible data first
221 before ever considering visible data. There is no limit to the size of data
222 to consume from the invisible buffer, as platform-specific implementations
223 will rarely leave enough control on this. So any byte fed into the invisible
224 buffer is expected to reach the destination file descriptor, by any means.
225 However, it's the consumer's responsibility to ensure that the invisible
226 data has been entirely consumed before consuming visible data. This must be
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100227 reflected by ->pipe->data. This is very important as this and only this can
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100228 ensure strict ordering of data between buffers.
229
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200230 The producer is responsible for decreasing ->to_forward. The ->to_forward
231 parameter indicates how many bytes may be fed into either data buffer
232 without waking the parent up. The special value CHN_INFINITE_FORWARD is
233 never decreased nor increased.
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100234
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200235 The buf->o parameter says how many bytes may be consumed from the visible
236 buffer. This parameter is updated by any buffer_write() as well as any data
237 forwarded through the visible buffer. Since the ->to_forward attribute
238 applies to data after buf->p, an analyser will not see a buffer which has a
239 non-null ->to_forward with buf->i > 0. A producer is responsible for raising
240 buf->o by min(to_forward, buf->i) when it injects data into the buffer.
241
242 The consumer is responsible for decreasing ->buf->o when it sends data
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100243 from the visible buffer, and ->pipe->data when it sends data from the
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100244 invisible buffer.
245
246 A real-world example consists in part in an HTTP response waiting in a
247 buffer to be forwarded. We know the header length (300) and the amount of
248 data to forward (content-length=9000). The buffer already contains 1000
249 bytes of data after the 300 bytes of headers. Thus the caller will set
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200250 buf->o to 300 indicating that it explicitly wants to send those data, and
251 set ->to_forward to 9000 (content-length). This value must be normalised
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100252 immediately after updating ->to_forward : since there are already 1300 bytes
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200253 in the buffer, 300 of which are already counted in buf->o, and that size
254 is smaller than ->to_forward, we must update buf->o to 1300 to flush the
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100255 whole buffer, and reduce ->to_forward to 8000. After that, the producer may
256 try to feed the additional data through the invisible buffer using a
257 platform-specific method such as splice().
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100258
259 The ->to_forward entry is also used to detect whether we can fill the buffer
260 or not. The idea is that we need to save some space for data manipulation
261 (mainly header rewriting in HTTP) so we don't want to have a full buffer on
262 input before processing a request or response. Thus, we ensure that there is
263 always global.maxrewrite bytes of free space. Since we don't want to forward
264 chunks without filling the buffer, we rely on ->to_forward. When ->to_forward
265 is null, we may have some processing to do so we don't want to fill the
266 buffer. When ->to_forward is non-null, we know we don't care for at least as
267 many bytes. In the end, we know that each of the ->to_forward bytes will
268 eventually leave the buffer. So as long as ->to_forward is larger than
269 global.maxrewrite, we can fill the buffer. If ->to_forward is smaller than
270 global.maxrewrite, then we don't want to fill the buffer with more than
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200271 vuf->size - global.maxrewrite + ->to_forward.
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100272
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100273 A buffer may contain up to 5 areas :
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200274 - the data waiting to be sent. These data are located between buf->p-o and
275 buf->p ;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100276 - the data to process and possibly transform. These data start at
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200277 buf->p and may be up to ->i bytes long.
278 - the data to preserve. They start at ->p and stop at ->p+i. The limit
279 between the two solely depends on the protocol being analysed.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100280 - the spare area : it is the remainder of the buffer, which can be used to
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200281 store new incoming data. It starts at ->p+i and is up to ->size-i-o long.
282 It may be limited by global.maxrewrite.
283 - the reserved area : this is the area which must not be filled and is
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100284 reserved for possible rewrites ; it is up to global.maxrewrite bytes
285 long.
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100286 */
287
Willy Tarreauc7e42382012-08-24 19:22:53 +0200288#endif /* _TYPES_CHANNEL_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289
290/*
291 * Local variables:
292 * c-indent-level: 8
293 * c-basic-offset: 8
294 * End:
295 */