2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
include/proto/buffers.h
|
|
|
|
Buffer management definitions, macros and inline functions.
|
|
|
|
|
2008-08-16 19:13:23 +00:00
|
|
|
Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
|
2008-11-23 18:31:35 +00:00
|
|
|
|
2006-06-26 00:48:02 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_BUFFERS_H
|
|
|
|
#define _PROTO_BUFFERS_H
|
|
|
|
|
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>
|
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>
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <types/buffers.h>
|
|
|
|
|
2007-05-13 17:56:02 +00:00
|
|
|
extern struct pool_head *pool2_buffer;
|
|
|
|
|
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
|
|
|
int init_buffer();
|
|
|
|
|
2006-07-29 14:59:06 +00:00
|
|
|
/* Initializes all fields in the buffer. The ->rlim field is initialized last
|
|
|
|
* so that the compiler can optimize it away if changed immediately after the
|
2008-08-16 20:18:07 +00:00
|
|
|
* call to this function. By default, it is set to the full size of the buffer.
|
|
|
|
* The BF_EMPTY flags is set.
|
2006-07-29 14:59:06 +00:00
|
|
|
*/
|
|
|
|
static inline void buffer_init(struct buffer *buf)
|
|
|
|
{
|
2008-08-16 20:18:07 +00:00
|
|
|
buf->l = buf->total = 0;
|
2008-08-17 13:20:19 +00:00
|
|
|
buf->analysers = 0;
|
[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
|
|
|
buf->cons = NULL;
|
2008-08-16 20:18:07 +00:00
|
|
|
buf->flags = BF_EMPTY;
|
2008-08-17 13:20:19 +00:00
|
|
|
buf->r = buf->lr = buf->w = buf->data;
|
2008-08-16 20:18:07 +00:00
|
|
|
buf->rlim = buf->data + BUFSIZE;
|
2006-07-29 14:59:06 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/* returns 1 if the buffer is empty, 0 otherwise */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_isempty(const struct buffer *buf)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
return buf->l == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 1 if the buffer is full, 0 otherwise */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_isfull(const struct buffer *buf) {
|
2006-06-26 00:48:02 +00:00
|
|
|
return buf->l == BUFSIZE;
|
|
|
|
}
|
|
|
|
|
2008-09-04 07:14:08 +00:00
|
|
|
/* Check buffer timeouts, and set the corresponding flags. The
|
|
|
|
* likely/unlikely have been optimized for fastest normal path.
|
|
|
|
*/
|
|
|
|
static inline void buffer_check_timeouts(struct buffer *b)
|
|
|
|
{
|
|
|
|
if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT))) &&
|
|
|
|
unlikely(tick_is_expired(b->rex, now_ms)))
|
|
|
|
b->flags |= BF_READ_TIMEOUT;
|
|
|
|
|
|
|
|
if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT))) &&
|
|
|
|
unlikely(tick_is_expired(b->wex, now_ms)))
|
|
|
|
b->flags |= BF_WRITE_TIMEOUT;
|
|
|
|
|
|
|
|
if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
|
|
|
|
unlikely(tick_is_expired(b->analyse_exp, now_ms)))
|
|
|
|
b->flags |= BF_ANA_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2008-08-16 20:18:07 +00:00
|
|
|
/* flushes any content from buffer <buf> and adjusts flags
|
|
|
|
* accordingly.
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
static inline void buffer_flush(struct buffer *buf)
|
|
|
|
{
|
2007-03-18 15:31:29 +00:00
|
|
|
buf->r = buf->lr = buf->w = buf->data;
|
2006-06-26 00:48:02 +00:00
|
|
|
buf->l = 0;
|
2008-08-16 20:18:07 +00:00
|
|
|
buf->flags |= BF_EMPTY | BF_FULL;
|
|
|
|
if (buf->rlim)
|
|
|
|
buf->flags &= ~BF_FULL;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2008-08-16 19:13:23 +00:00
|
|
|
/* marks the buffer as "shutdown" for reads and cancels the timeout */
|
2007-06-03 13:59:52 +00:00
|
|
|
static inline void buffer_shutr(struct buffer *buf)
|
|
|
|
{
|
2008-07-06 22:09:58 +00:00
|
|
|
buf->rex = TICK_ETERNITY;
|
2008-08-16 19:13:23 +00:00
|
|
|
buf->flags |= BF_SHUTR;
|
2007-06-03 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-16 19:13:23 +00:00
|
|
|
/* marks the buffer as "shutdown" for writes and cancels the timeout */
|
2007-06-03 13:59:52 +00:00
|
|
|
static inline void buffer_shutw(struct buffer *buf)
|
|
|
|
{
|
2008-07-06 22:09:58 +00:00
|
|
|
buf->wex = TICK_ETERNITY;
|
2008-08-16 19:13:23 +00:00
|
|
|
buf->flags |= BF_SHUTW;
|
2007-06-03 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
[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
|
|
|
/* marks the buffer as "shutdown" ASAP for reads */
|
|
|
|
static inline void buffer_shutr_now(struct buffer *buf)
|
|
|
|
{
|
2008-11-30 17:14:12 +00:00
|
|
|
buf->flags |= BF_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
|
|
|
}
|
|
|
|
|
|
|
|
/* marks the buffer as "shutdown" ASAP for writes */
|
|
|
|
static inline void buffer_shutw_now(struct buffer *buf)
|
|
|
|
{
|
|
|
|
buf->flags |= BF_SHUTW_NOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* marks the buffer as "shutdown" ASAP in both directions */
|
|
|
|
static inline void buffer_abort(struct buffer *buf)
|
|
|
|
{
|
2008-11-30 17:14:12 +00:00
|
|
|
buf->flags |= BF_SHUTR_NOW | BF_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
|
|
|
}
|
|
|
|
|
2008-12-07 17:03:29 +00:00
|
|
|
/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
|
|
|
|
* flag is set, and the function called once. The function is responsible for
|
|
|
|
* clearing the hijack bit. It is possible that the function clears the flag
|
|
|
|
* during this first call.
|
|
|
|
*/
|
|
|
|
static inline void buffer_install_hijacker(struct session *s,
|
|
|
|
struct buffer *b,
|
|
|
|
void (*func)(struct session *, struct buffer *))
|
2008-08-28 14:01:32 +00:00
|
|
|
{
|
2008-12-07 17:03:29 +00:00
|
|
|
b->hijacker = func;
|
|
|
|
b->flags |= BF_HIJACK;
|
|
|
|
func(s, b);
|
2008-08-28 14:01:32 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:03:29 +00:00
|
|
|
/* Releases the buffer from hijacking mode. Often used by the hijack function */
|
2008-08-28 14:01:32 +00:00
|
|
|
static inline void buffer_stop_hijack(struct buffer *buf)
|
|
|
|
{
|
|
|
|
buf->flags &= ~BF_HIJACK;
|
|
|
|
}
|
|
|
|
|
2008-08-29 07:58:42 +00:00
|
|
|
/* allows the consumer to send the buffer contents */
|
|
|
|
static inline void buffer_write_ena(struct buffer *buf)
|
|
|
|
{
|
|
|
|
buf->flags |= BF_WRITE_ENA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prevents the consumer from sending the buffer contents */
|
|
|
|
static inline void buffer_write_dis(struct buffer *buf)
|
|
|
|
{
|
|
|
|
buf->flags &= ~BF_WRITE_ENA;
|
|
|
|
}
|
|
|
|
|
2008-11-23 18:31:35 +00:00
|
|
|
/* check if the buffer needs to be shut down for read, and perform the shutdown
|
|
|
|
* at the stream_interface level if needed. This must not be used with a buffer
|
|
|
|
* for which a connection is currently in queue or turn-around.
|
|
|
|
*/
|
|
|
|
static inline void buffer_check_shutr(struct buffer *b)
|
|
|
|
{
|
|
|
|
if (b->flags & BF_SHUTR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(b->flags & (BF_SHUTR_NOW|BF_SHUTW)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Last read, forced read-shutdown, or other end closed. We have to
|
|
|
|
* close our read side and inform the stream_interface.
|
|
|
|
*/
|
|
|
|
b->prod->shutr(b->prod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the buffer needs to be shut down for write, and perform the shutdown
|
|
|
|
* at the stream_interface level if needed. This must not be used with a buffer
|
|
|
|
* for which a connection is currently in queue or turn-around.
|
|
|
|
*/
|
|
|
|
static inline void buffer_check_shutw(struct buffer *b)
|
|
|
|
{
|
|
|
|
if (b->flags & BF_SHUTW)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((b->flags & BF_SHUTW_NOW) ||
|
|
|
|
(b->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
|
|
|
|
(BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
|
|
|
|
/* Application requested write-shutdown, or other end closed
|
|
|
|
* with empty buffer. We have to close our write side and
|
|
|
|
* inform the stream_interface.
|
|
|
|
*/
|
|
|
|
b->cons->shutw(b->cons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/* returns the maximum number of bytes writable at once in this buffer */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_max(const struct buffer *buf)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
if (buf->l == BUFSIZE)
|
|
|
|
return 0;
|
|
|
|
else if (buf->r >= buf->w)
|
|
|
|
return buf->data + BUFSIZE - buf->r;
|
|
|
|
else
|
|
|
|
return buf->w - buf->r;
|
|
|
|
}
|
|
|
|
|
2008-08-16 20:18:07 +00:00
|
|
|
/* sets the buffer read limit to <size> bytes, and adjusts the FULL
|
|
|
|
* flag accordingly.
|
|
|
|
*/
|
|
|
|
static inline void buffer_set_rlim(struct buffer *buf, int size)
|
|
|
|
{
|
|
|
|
buf->rlim = buf->data + size;
|
|
|
|
if (buf->l < size)
|
|
|
|
buf->flags &= ~BF_FULL;
|
|
|
|
else
|
|
|
|
buf->flags |= BF_FULL;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tries to realign the given buffer, and returns how many bytes can be written
|
|
|
|
* there at once without overwriting anything.
|
|
|
|
*/
|
|
|
|
static inline int buffer_realign(struct buffer *buf)
|
|
|
|
{
|
|
|
|
if (buf->l == 0) {
|
|
|
|
/* let's realign the buffer to optimize I/O */
|
2007-03-18 15:31:29 +00:00
|
|
|
buf->r = buf->w = buf->lr = buf->data;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
return buffer_max(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int buffer_write(struct buffer *buf, const char *msg, int len);
|
2007-01-01 20:38:07 +00:00
|
|
|
int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
|
2007-03-18 21:36:26 +00:00
|
|
|
int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
|
|
|
|
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
|
|
|
|
int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
|
2007-01-01 20:38:07 +00:00
|
|
|
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
|
2007-01-21 18:16:41 +00:00
|
|
|
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-12-23 19:51:41 +00:00
|
|
|
/*
|
|
|
|
* frees the destination chunk if already allocated, allocates a new string,
|
|
|
|
* and copies the source into it. The pointer to the destination string is
|
|
|
|
* returned, or NULL if the allocation fails or if any pointer is NULL..
|
|
|
|
*/
|
|
|
|
static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
|
|
|
|
if (!dst || !src || !src->str)
|
|
|
|
return NULL;
|
|
|
|
if (dst->str)
|
|
|
|
free(dst->str);
|
|
|
|
dst->len = src->len;
|
|
|
|
dst->str = (char *)malloc(dst->len);
|
|
|
|
memcpy(dst->str, src->str, dst->len);
|
|
|
|
return dst->str;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#endif /* _PROTO_BUFFERS_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|