2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2012-08-24 17:22:53 +00:00
|
|
|
* include/proto/channel.h
|
|
|
|
* Channel management definitions, macros and inline functions.
|
2009-12-13 14:53:05 +00:00
|
|
|
*
|
2012-05-07 09:56:55 +00:00
|
|
|
* Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
|
2009-12-13 14:53:05 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-08-24 17:22:53 +00:00
|
|
|
#ifndef _PROTO_CHANNEL_H
|
|
|
|
#define _PROTO_CHANNEL_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <stdio.h>
|
2006-12-23 19:51:41 +00:00
|
|
|
#include <stdlib.h>
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <string.h>
|
2006-12-23 19:51:41 +00:00
|
|
|
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
2012-08-24 17:22:53 +00:00
|
|
|
#include <common/chunk.h>
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <common/memory.h>
|
2008-07-06 22:09:58 +00:00
|
|
|
#include <common/ticks.h>
|
2007-06-03 13:59:52 +00:00
|
|
|
#include <common/time.h>
|
|
|
|
|
2009-12-13 14:53:05 +00:00
|
|
|
#include <types/global.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
extern struct pool_head *pool2_channel;
|
2007-05-13 17:56:02 +00:00
|
|
|
|
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
2012-08-27 22:06:31 +00:00
|
|
|
int init_channel();
|
2007-05-13 17:56:02 +00:00
|
|
|
|
2012-10-25 22:21:52 +00:00
|
|
|
unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes);
|
2012-08-27 22:06:31 +00:00
|
|
|
|
|
|
|
/* SI-to-channel functions working with buffers */
|
2012-10-12 21:11:02 +00:00
|
|
|
int bi_putblk(struct channel *chn, const char *str, int len);
|
|
|
|
int bi_putchr(struct channel *chn, char c);
|
|
|
|
int bo_inject(struct channel *chn, const char *msg, int len);
|
|
|
|
int bo_getline(struct channel *chn, char *str, int len);
|
|
|
|
int bo_getblk(struct channel *chn, char *blk, int len, int offset);
|
2010-09-08 15:04:31 +00:00
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Initialize all fields in the channel. */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_init(struct channel *chn)
|
2006-07-29 14:59:06 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->o = 0;
|
|
|
|
chn->buf->i = 0;
|
|
|
|
chn->buf->p = chn->buf->data;
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->to_forward = 0;
|
2014-02-09 16:45:16 +00:00
|
|
|
chn->last_read = now_ms;
|
2014-02-09 07:31:49 +00:00
|
|
|
chn->xfer_small = chn->xfer_large = 0;
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->total = 0;
|
|
|
|
chn->pipe = NULL;
|
|
|
|
chn->analysers = 0;
|
|
|
|
chn->cons = NULL;
|
|
|
|
chn->flags = 0;
|
2009-12-13 14:53:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-25 22:21:52 +00:00
|
|
|
/* Schedule up to <bytes> more bytes to be forwarded via the channel without
|
|
|
|
* notifying the owner task. Any data pending in the buffer are scheduled to be
|
|
|
|
* sent as well, in the limit of the number of bytes to forward. This must be
|
|
|
|
* the only method to use to schedule bytes to be forwarded. If the requested
|
|
|
|
* number is too large, it is automatically adjusted. The number of bytes taken
|
|
|
|
* into account is returned. Directly touching ->to_forward will cause lockups
|
|
|
|
* when buf->o goes down to zero if nobody is ready to push the remaining data.
|
|
|
|
*/
|
|
|
|
static inline unsigned long long channel_forward(struct channel *chn, unsigned long long bytes)
|
|
|
|
{
|
|
|
|
/* hint: avoid comparisons on long long for the fast case, since if the
|
|
|
|
* length does not fit in an unsigned it, it will never be forwarded at
|
|
|
|
* once anyway.
|
|
|
|
*/
|
|
|
|
if (bytes <= ~0U) {
|
|
|
|
unsigned int bytes32 = bytes;
|
|
|
|
|
|
|
|
if (bytes32 <= chn->buf->i) {
|
|
|
|
/* OK this amount of bytes might be forwarded at once */
|
|
|
|
b_adv(chn->buf, bytes32);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return __channel_forward(chn, bytes);
|
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
/* These functions are used to compute various channel content sizes */
|
|
|
|
/*********************************************************************/
|
2011-11-25 19:33:58 +00:00
|
|
|
|
2012-08-24 20:40:29 +00:00
|
|
|
/* Reports non-zero if the channel is empty, which means both its
|
|
|
|
* buffer and pipe are empty. The construct looks strange but is
|
|
|
|
* jump-less and much more efficient on both 32 and 64-bit than
|
|
|
|
* the boolean test.
|
|
|
|
*/
|
|
|
|
static inline unsigned int channel_is_empty(struct channel *c)
|
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
return !(c->buf->o | (long)c->pipe);
|
2012-08-24 20:40:29 +00:00
|
|
|
}
|
|
|
|
|
BUG/MAJOR: http: always ensure response buffer has some room for a response
Since 1.5-dev12 and commit 3bf1b2b8 (MAJOR: channel: stop relying on
BF_FULL to take action), the HTTP parser switched to channel_full()
instead of BF_FULL to decide whether a buffer had enough room to start
parsing a request or response. The problem is that channel_full()
intentionally ignores outgoing data, so a corner case exists where a
large response might still be left in a response buffer with just a
few bytes left (much less than the reserve), enough to accept a second
response past the last data, but not enough to permit the HTTP processor
to add some headers. Since all the processing relies on this space being
available, we can get some random crashes when clients pipeline requests.
The analysis of a core from haproxy configured with 20480 bytes buffers
shows this : with enough "luck", when sending back the response for the
first request, the client is slow, the TCP window is congested, the socket
buffers are full, and haproxy's buffer fills up. We still have 20230 bytes
of response data in a 20480 response buffer. The second request is sent to
the server which returns 214 bytes which fit in the small 250 bytes left
in this buffer. And the buffer arrangement makes it possible to escape all
the controls in http_wait_for_response() :
|<------ response buffer = 20480 bytes ------>|
[ 2/2 | 3 | 4 | 1/2 ]
^ start of circular buffer
1/2 = beginning of previous response (18240)
2/2 = end of previous response (1990)
3 = current response (214)
4 = free space (36)
- channel_full() returns false (20230 bytes are going to leave)
- the response headers does not wrap at the end of the buffer
- the remaining linear room after the headers is larger than the
reserve, because it's the previous response which wraps :
=> response is processed
Header rewriting causes it to reach 260 bytes, 10 bytes larger than what
the buffer could hold. So all computations during header addition are
wrong and lead to the corruption we've observed.
All the conditions are very hard to meet (which explains why it took
almost one year for this bug to show up) and are almost impossible to
reproduce on purpose on a test platform. But the bug is clearly there.
This issue was reported by Dinko Korunic who kindly devoted a lot of
time to provide countless traces and cores, and to experiment with
troubleshooting patches to knock the bug down. Thanks Dinko!
No backport is needed, but all 1.5-dev versions between dev12 and dev18
included must be upgraded. A workaround consists in setting option
forceclose to prevent pipelined requests from being processed.
2013-06-08 10:55:46 +00:00
|
|
|
/* Returns non-zero if the buffer input has all of its reserve available. This
|
|
|
|
* is used to decide when a request or response may be parsed when some data
|
|
|
|
* from a previous exchange might still be present.
|
|
|
|
*/
|
|
|
|
static inline int channel_reserved(const struct channel *chn)
|
|
|
|
{
|
|
|
|
int rem = chn->buf->size;
|
|
|
|
|
|
|
|
rem -= chn->buf->o;
|
|
|
|
rem -= chn->buf->i;
|
|
|
|
rem -= global.tune.maxrewrite;
|
|
|
|
return rem >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns non-zero if the buffer input is considered full. This is used to
|
|
|
|
* decide when to stop reading into a buffer when we want to ensure that we
|
|
|
|
* leave the reserve untouched after all pending outgoing data are forwarded.
|
|
|
|
* The reserved space is taken into account if ->to_forward indicates that an
|
|
|
|
* end of transfer is close to happen. Note that both ->buf->o and ->to_forward
|
|
|
|
* are considered as available since they're supposed to leave the buffer. The
|
|
|
|
* test is optimized to avoid as many operations as possible for the fast case
|
|
|
|
* and to be used as an "if" condition.
|
2011-11-25 19:33:58 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int channel_full(const struct channel *chn)
|
2011-11-25 19:33:58 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
int rem = chn->buf->size;
|
2012-05-07 09:56:55 +00:00
|
|
|
|
2012-10-12 21:49:43 +00:00
|
|
|
rem -= chn->buf->o;
|
|
|
|
rem -= chn->buf->i;
|
2012-05-07 09:56:55 +00:00
|
|
|
if (!rem)
|
|
|
|
return 1; /* buffer already full */
|
|
|
|
|
2012-10-12 21:49:43 +00:00
|
|
|
if (chn->to_forward >= chn->buf->size ||
|
|
|
|
(CHN_INFINITE_FORWARD < MAX_RANGE(typeof(chn->buf->size)) && // just there to ensure gcc
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->to_forward == CHN_INFINITE_FORWARD)) // avoids the useless second
|
|
|
|
return 0; // test whenever possible
|
2012-05-07 09:56:55 +00:00
|
|
|
|
|
|
|
rem -= global.tune.maxrewrite;
|
2012-10-12 21:49:43 +00:00
|
|
|
rem += chn->buf->o;
|
2012-10-12 21:11:02 +00:00
|
|
|
rem += chn->to_forward;
|
2012-05-07 09:56:55 +00:00
|
|
|
return rem <= 0;
|
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Returns true if the channel's input is already closed */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int channel_input_closed(struct channel *chn)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
return ((chn->flags & CF_SHUTR) != 0);
|
2010-09-08 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Returns true if the channel's output is already closed */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int channel_output_closed(struct channel *chn)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
return ((chn->flags & CF_SHUTW) != 0);
|
2010-09-08 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Check channel timeouts, and set the corresponding flags. The likely/unlikely
|
|
|
|
* have been optimized for fastest normal path. The read/write timeouts are not
|
|
|
|
* set if there was activity on the channel. That way, we don't have to update
|
|
|
|
* the timeout on every I/O. Note that the analyser timeout is always checked.
|
2008-09-04 07:14:08 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_check_timeouts(struct channel *chn)
|
2008-09-04 07:14:08 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
if (likely(!(chn->flags & (CF_SHUTR|CF_READ_TIMEOUT|CF_READ_ACTIVITY|CF_READ_NOEXP))) &&
|
|
|
|
unlikely(tick_is_expired(chn->rex, now_ms)))
|
|
|
|
chn->flags |= CF_READ_TIMEOUT;
|
2008-09-04 07:14:08 +00:00
|
|
|
|
2012-10-12 21:11:02 +00:00
|
|
|
if (likely(!(chn->flags & (CF_SHUTW|CF_WRITE_TIMEOUT|CF_WRITE_ACTIVITY))) &&
|
|
|
|
unlikely(tick_is_expired(chn->wex, now_ms)))
|
|
|
|
chn->flags |= CF_WRITE_TIMEOUT;
|
2008-09-04 07:14:08 +00:00
|
|
|
|
2012-10-12 21:11:02 +00:00
|
|
|
if (likely(!(chn->flags & CF_ANA_TIMEOUT)) &&
|
|
|
|
unlikely(tick_is_expired(chn->analyse_exp, now_ms)))
|
|
|
|
chn->flags |= CF_ANA_TIMEOUT;
|
2008-09-04 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Erase any content from channel <buf> and adjusts flags accordingly. Note
|
[MEDIUM] i/o: rework ->to_forward and ->send_max
The way the buffers and stream interfaces handled ->to_forward was
really not handy for multiple reasons. Now we've moved its control
to the receive-side of the buffer, which is also responsible for
keeping send_max up to date. This makes more sense as it now becomes
possible to send some pre-formatted data followed by forwarded data.
The following explanation has also been added to buffer.h to clarify
the situation. Right now, tests show that the I/O is behaving extremely
well. Some work will have to be done to adapt existing splice code
though.
/* Note about the buffer structure
The buffer contains two length indicators, one to_forward counter and one
send_max limit. First, it must be understood that the buffer is in fact
split in two parts :
- the visible data (->data, for ->l bytes)
- the invisible data, typically in kernel buffers forwarded directly from
the source stream sock to the destination stream sock (->splice_len
bytes). Those are used only during forward.
In order not to mix data streams, the producer may only feed the invisible
data with data to forward, and only when the visible buffer is empty. The
consumer may not always be able to feed the invisible buffer due to platform
limitations (lack of kernel support).
Conversely, the consumer must always take data from the invisible data first
before ever considering visible data. There is no limit to the size of data
to consume from the invisible buffer, as platform-specific implementations
will rarely leave enough control on this. So any byte fed into the invisible
buffer is expected to reach the destination file descriptor, by any means.
However, it's the consumer's responsibility to ensure that the invisible
data has been entirely consumed before consuming visible data. This must be
reflected by ->splice_len. This is very important as this and only this can
ensure strict ordering of data between buffers.
The producer is responsible for decreasing ->to_forward and increasing
->send_max. The ->to_forward parameter indicates how many bytes may be fed
into either data buffer without waking the parent up. The ->send_max
parameter says how many bytes may be read from the visible buffer. Thus it
may never exceed ->l. This parameter is updated by any buffer_write() as
well as any data forwarded through the visible buffer.
The consumer is responsible for decreasing ->send_max when it sends data
from the visible buffer, and ->splice_len when it sends data from the
invisible buffer.
A real-world example consists in part in an HTTP response waiting in a
buffer to be forwarded. We know the header length (300) and the amount of
data to forward (content-length=9000). The buffer already contains 1000
bytes of data after the 300 bytes of headers. Thus the caller will set
->send_max to 300 indicating that it explicitly wants to send those data,
and set ->to_forward to 9000 (content-length). This value must be normalised
immediately after updating ->to_forward : since there are already 1300 bytes
in the buffer, 300 of which are already counted in ->send_max, and that size
is smaller than ->to_forward, we must update ->send_max to 1300 to flush the
whole buffer, and reduce ->to_forward to 8000. After that, the producer may
try to feed the additional data through the invisible buffer using a
platform-specific method such as splice().
*/
2009-01-07 23:09:41 +00:00
|
|
|
* that any spliced data is not affected since we may not have any access to
|
|
|
|
* it.
|
2008-08-16 20:18:07 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_erase(struct channel *chn)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->o = 0;
|
|
|
|
chn->buf->i = 0;
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->to_forward = 0;
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->p = chn->buf->data;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* marks the channel as "shutdown" ASAP for reads */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_shutr_now(struct channel *chn)
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_SHUTR_NOW;
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* marks the channel as "shutdown" ASAP for writes */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_shutw_now(struct channel *chn)
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_SHUTW_NOW;
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* marks the channel as "shutdown" ASAP in both directions */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_abort(struct channel *chn)
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_SHUTR_NOW | CF_SHUTW_NOW;
|
|
|
|
chn->flags &= ~CF_AUTO_CONNECT;
|
[MAJOR] rework of the server FSM
srv_state has been removed from HTTP state machines, and states
have been split in either TCP states or analyzers. For instance,
the TARPIT state has just become a simple analyzer.
New flags have been added to the struct buffer to compensate this.
The high-level stream processors sometimes need to force a disconnection
without touching a file-descriptor (eg: report an error). But if
they touched BF_SHUTW or BF_SHUTR, the file descriptor would not
be closed. Thus, the two SHUT?_NOW flags have been added so that
an application can request a forced close which the stream interface
will be forced to obey.
During this change, a new BF_HIJACK flag was added. It will
be used for data generation, eg during a stats dump. It
prevents the producer on a buffer from sending data into it.
BF_SHUTR_NOW /* the producer must shut down for reads ASAP */
BF_SHUTW_NOW /* the consumer must shut down for writes ASAP */
BF_HIJACK /* the producer is temporarily replaced */
BF_SHUTW_NOW has precedence over BF_HIJACK. BF_HIJACK has
precedence over BF_MAY_FORWARD (so that it does not need it).
New functions buffer_shutr_now(), buffer_shutw_now(), buffer_abort()
are provided to manipulate BF_SHUT* flags.
A new type "stream_interface" has been added to describe both
sides of a buffer. A stream interface has states and error
reporting. The session now has two stream interfaces (one per
side). Each buffer has stream_interface pointers to both
consumer and producer sides.
The server-side file descriptor has moved to its stream interface,
so that even the buffer has access to it.
process_srv() has been split into three parts :
- tcp_get_connection() obtains a connection to the server
- tcp_connection_failed() tests if a previously attempted
connection has succeeded or not.
- process_srv_data() only manages the data phase, and in
this sense should be roughly equivalent to process_cli.
Little code has been removed, and a lot of old code has been
left in comments for now.
2008-10-19 05:30:41 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 19:04:57 +00:00
|
|
|
/* allow the consumer to try to establish a new connection. */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_auto_connect(struct channel *chn)
|
2008-08-29 07:58:42 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_AUTO_CONNECT;
|
2008-08-29 07:58:42 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 19:04:57 +00:00
|
|
|
/* prevent the consumer from trying to establish a new connection, and also
|
|
|
|
* disable auto shutdown forwarding.
|
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_dont_connect(struct channel *chn)
|
2008-08-29 07:58:42 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags &= ~(CF_AUTO_CONNECT|CF_AUTO_CLOSE);
|
2008-08-29 07:58:42 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 19:04:57 +00:00
|
|
|
/* allow the producer to forward shutdown requests */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_auto_close(struct channel *chn)
|
2008-11-23 18:31:35 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_AUTO_CLOSE;
|
2008-11-23 18:31:35 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 19:04:57 +00:00
|
|
|
/* prevent the producer from forwarding shutdown requests */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_dont_close(struct channel *chn)
|
2008-11-23 18:31:35 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags &= ~CF_AUTO_CLOSE;
|
2008-11-23 18:31:35 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 23:20:41 +00:00
|
|
|
/* allow the producer to read / poll the input */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_auto_read(struct channel *chn)
|
2010-01-06 23:20:41 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags &= ~CF_DONT_READ;
|
2010-01-06 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prevent the producer from read / poll the input */
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void channel_dont_read(struct channel *chn)
|
2010-01-06 23:20:41 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_DONT_READ;
|
2010-01-06 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
|
|
|
|
/*************************************************/
|
|
|
|
/* Buffer operations in the context of a channel */
|
|
|
|
/*************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the number of reserved bytes in the channel's visible
|
|
|
|
* buffer, which ensures that once all pending data are forwarded, the
|
|
|
|
* buffer still has global.tune.maxrewrite bytes free. The result is
|
|
|
|
* between 0 and global.tune.maxrewrite, which is itself smaller than
|
2012-10-12 21:11:02 +00:00
|
|
|
* any chn->size.
|
2012-08-27 22:06:31 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int buffer_reserved(const struct channel *chn)
|
2012-08-27 22:06:31 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
int ret = global.tune.maxrewrite - chn->to_forward - chn->buf->o;
|
2012-08-27 22:06:31 +00:00
|
|
|
|
2012-10-12 21:11:02 +00:00
|
|
|
if (chn->to_forward == CHN_INFINITE_FORWARD)
|
2012-08-27 22:06:31 +00:00
|
|
|
return 0;
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the max number of bytes the buffer can contain so that once all the
|
|
|
|
* pending bytes are forwarded, the buffer still has global.tune.maxrewrite
|
2012-10-12 21:11:02 +00:00
|
|
|
* bytes free. The result sits between chn->size - maxrewrite and chn->size.
|
2012-08-27 22:06:31 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int buffer_max_len(const struct channel *chn)
|
2012-08-27 22:06:31 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
return chn->buf->size - buffer_reserved(chn);
|
2012-08-27 22:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the amount of space available at the input of the buffer, taking the
|
|
|
|
* reserved space into account if ->to_forward indicates that an end of transfer
|
|
|
|
* is close to happen. The test is optimized to avoid as many operations as
|
|
|
|
* possible for the fast case.
|
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int bi_avail(const struct channel *chn)
|
2012-08-27 22:06:31 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
int rem = chn->buf->size;
|
2012-08-27 22:06:31 +00:00
|
|
|
int rem2;
|
|
|
|
|
2012-10-12 21:49:43 +00:00
|
|
|
rem -= chn->buf->o;
|
|
|
|
rem -= chn->buf->i;
|
2012-08-27 22:06:31 +00:00
|
|
|
if (!rem)
|
|
|
|
return rem; /* buffer already full */
|
|
|
|
|
2012-10-12 21:49:43 +00:00
|
|
|
if (chn->to_forward >= chn->buf->size ||
|
|
|
|
(CHN_INFINITE_FORWARD < MAX_RANGE(typeof(chn->buf->size)) && // just there to ensure gcc
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->to_forward == CHN_INFINITE_FORWARD)) // avoids the useless second
|
|
|
|
return rem; // test whenever possible
|
2012-08-27 22:06:31 +00:00
|
|
|
|
|
|
|
rem2 = rem - global.tune.maxrewrite;
|
2012-10-12 21:49:43 +00:00
|
|
|
rem2 += chn->buf->o;
|
2012-10-12 21:11:02 +00:00
|
|
|
rem2 += chn->to_forward;
|
2012-08-27 22:06:31 +00:00
|
|
|
|
|
|
|
if (rem > rem2)
|
|
|
|
rem = rem2;
|
|
|
|
if (rem > 0)
|
|
|
|
return rem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cut the "tail" of the channel's buffer, which means strip it to the length
|
|
|
|
* of unsent data only, and kill any remaining unsent data. Any scheduled
|
|
|
|
* forwarding is stopped. This is mainly to be used to send error messages
|
|
|
|
* after existing data.
|
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void bi_erase(struct channel *chn)
|
2012-08-27 22:06:31 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
if (!chn->buf->o)
|
2012-10-12 21:11:02 +00:00
|
|
|
return channel_erase(chn);
|
2012-08-27 22:06:31 +00:00
|
|
|
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->to_forward = 0;
|
2012-10-12 21:49:43 +00:00
|
|
|
if (!chn->buf->i)
|
2012-08-27 22:06:31 +00:00
|
|
|
return;
|
|
|
|
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->i = 0;
|
2012-08-27 22:06:31 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 05:37:22 +00:00
|
|
|
/*
|
2012-08-27 22:06:31 +00:00
|
|
|
* Advance the channel buffer's read pointer by <len> bytes. This is useful
|
|
|
|
* when data have been read directly from the buffer. It is illegal to call
|
|
|
|
* this function with <len> causing a wrapping at the end of the buffer. It's
|
|
|
|
* the caller's responsibility to ensure that <len> is never larger than
|
2012-10-12 21:11:02 +00:00
|
|
|
* chn->o. Channel flag WRITE_PARTIAL is set.
|
2009-08-31 05:37:22 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline void bo_skip(struct channel *chn, int len)
|
2009-08-31 05:37:22 +00:00
|
|
|
{
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->o -= len;
|
2012-05-07 09:56:55 +00:00
|
|
|
|
2012-12-16 18:39:09 +00:00
|
|
|
if (buffer_empty(chn->buf))
|
2012-10-12 21:49:43 +00:00
|
|
|
chn->buf->p = chn->buf->data;
|
2009-08-31 05:37:22 +00:00
|
|
|
|
2009-09-23 21:47:55 +00:00
|
|
|
/* notify that some data was written to the SI from the buffer */
|
2012-10-12 21:11:02 +00:00
|
|
|
chn->flags |= CF_WRITE_PARTIAL;
|
2009-08-31 05:37:22 +00:00
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Tries to copy chunk <chunk> into the channel's buffer after length controls.
|
2012-10-12 21:11:02 +00:00
|
|
|
* The chn->o and to_forward pointers are updated. If the channel's input is
|
2010-09-08 15:04:31 +00:00
|
|
|
* closed, -2 is returned. If the block is too large for this buffer, -3 is
|
|
|
|
* returned. If there is not enough room left in the buffer, -1 is returned.
|
|
|
|
* Otherwise the number of bytes copied is returned (0 being a valid number).
|
2012-08-27 22:06:31 +00:00
|
|
|
* Channel flag READ_PARTIAL is updated if some data can be transferred. The
|
2012-08-27 18:53:34 +00:00
|
|
|
* chunk's length is updated with the number of bytes sent.
|
2009-08-31 06:09:57 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int bi_putchk(struct channel *chn, struct chunk *chunk)
|
2009-08-31 06:09:57 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-10-12 21:11:02 +00:00
|
|
|
ret = bi_putblk(chn, chunk->str, chunk->len);
|
2010-09-08 15:04:31 +00:00
|
|
|
if (ret > 0)
|
|
|
|
chunk->len -= ret;
|
2009-08-31 06:09:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-27 22:06:31 +00:00
|
|
|
/* Tries to copy string <str> at once into the channel's buffer after length
|
2012-10-12 21:11:02 +00:00
|
|
|
* controls. The chn->o and to_forward pointers are updated. If the channel's
|
2012-08-27 22:06:31 +00:00
|
|
|
* input is closed, -2 is returned. If the block is too large for this buffer,
|
|
|
|
* -3 is returned. If there is not enough room left in the buffer, -1 is
|
|
|
|
* returned. Otherwise the number of bytes copied is returned (0 being a valid
|
|
|
|
* number). Channel flag READ_PARTIAL is updated if some data can be
|
|
|
|
* transferred.
|
2010-09-08 15:04:31 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int bi_putstr(struct channel *chn, const char *str)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
2012-10-12 21:11:02 +00:00
|
|
|
return bi_putblk(chn, str, strlen(str));
|
2010-09-08 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-27 22:06:31 +00:00
|
|
|
* Return one char from the channel's buffer. If the buffer is empty and the
|
|
|
|
* channel is closed, return -2. If the buffer is just empty, return -1. The
|
|
|
|
* buffer's pointer is not advanced, it's up to the caller to call bo_skip(buf,
|
|
|
|
* 1) when it has consumed the char. Also note that this function respects the
|
2012-10-12 21:11:02 +00:00
|
|
|
* chn->o limit.
|
2010-09-08 15:04:31 +00:00
|
|
|
*/
|
2012-10-12 21:11:02 +00:00
|
|
|
static inline int bo_getchr(struct channel *chn)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
|
|
|
/* closed or empty + imminent close = -2; empty = -1 */
|
2012-10-12 21:11:02 +00:00
|
|
|
if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
|
|
|
|
if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
|
2010-09-08 15:04:31 +00:00
|
|
|
return -2;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-12 21:49:43 +00:00
|
|
|
return *buffer_wrap_sub(chn->buf, chn->buf->p - chn->buf->o);
|
2010-09-08 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-08-24 17:22:53 +00:00
|
|
|
#endif /* _PROTO_CHANNEL_H */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|