25
25
#include <common/config.h>
26
26
#include <common/memory.h>
29
#include <sys/types.h>
27
#include <types/stream_interface.h>
31
29
/* The BF_* macros designate Buffer Flags, which may be ORed in the bit field
32
* member 'flags' in struct buffer.
34
#define BF_SHUTR_PENDING 1
35
#define BF_SHUTR_DONE 2
36
#define BF_SHUTR_STATUS (BF_SHUTR_PENDING|BF_SHUTR_DONE)
38
#define BF_SHUTW_PENDING 4
39
#define BF_SHUTW_DONE 8
40
#define BF_SHUTW_STATUS (BF_SHUTW_PENDING|BF_SHUTW_DONE)
42
#define BF_PARTIAL_READ 16
43
#define BF_COMPLETE_READ 32
44
#define BF_READ_ERROR 64
45
#define BF_READ_NULL 128
46
#define BF_READ_STATUS (BF_PARTIAL_READ|BF_COMPLETE_READ|BF_READ_ERROR|BF_READ_NULL)
47
#define BF_CLEAR_READ (~BF_READ_STATUS)
49
#define BF_PARTIAL_WRITE 256
50
#define BF_COMPLETE_WRITE 512
51
#define BF_WRITE_ERROR 1024
52
#define BF_WRITE_NULL 2048
53
#define BF_WRITE_STATUS (BF_PARTIAL_WRITE|BF_COMPLETE_WRITE|BF_WRITE_ERROR|BF_WRITE_NULL)
54
#define BF_CLEAR_WRITE (~BF_WRITE_STATUS)
30
* member 'flags' in struct buffer. Here we have several types of flags :
32
* - pure status flags, reported by the lower layer, which must be cleared
33
* before doing further I/O :
34
* BF_*_NULL, BF_*_PARTIAL
36
* - pure status flags, reported by mid-layer, which must also be cleared
37
* before doing further I/O :
38
* BF_*_TIMEOUT, BF_*_ERROR
40
* - read-only indicators reported by lower levels :
41
* BF_STREAMER, BF_STREAMER_FAST
43
* - write-once status flags reported by the mid-level : BF_SHUTR, BF_SHUTW
45
* - persistent control flags managed only by higher level :
46
* BF_SHUT*_NOW, BF_*_ENA, BF_HIJACK
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
53
#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)
59
#define BF_FULL 0x000010 /* buffer cannot accept any more data (l >= max_len) */
60
#define BF_SHUTR 0x000020 /* producer has already shut down */
61
#define BF_SHUTR_NOW 0x000040 /* the producer must shut down for reads immediately */
62
#define BF_READ_NOEXP 0x000080 /* producer should not expire */
64
#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)
70
#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 */
75
#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 */
78
/* 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
83
#define BF_HIJACK 0x040000 /* the producer is temporarily replaced by ->hijacker */
84
#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 */
86
#define BF_KERN_SPLICING 0x200000 /* kernel splicing desired for this buffer */
87
#define BF_READ_DONTWAIT 0x400000 /* wake the task up after every read (eg: HTTP request) */
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))
94
/* Masks which define input events for stream analysers */
95
#define BF_MASK_ANALYSER (BF_READ_ATTACHED|BF_READ_ACTIVITY|BF_READ_TIMEOUT|BF_ANA_TIMEOUT|BF_WRITE_ACTIVITY)
97
/* Mask for static flags which are not events, but might change during processing */
98
#define BF_MASK_STATIC (BF_EMPTY|BF_FULL|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR|BF_SHUTW|BF_SHUTR_NOW|BF_SHUTW_NOW)
101
/* Analysers (buffer->analysers).
102
* Those bits indicate that there are some processing to do on the buffer
103
* contents. It will probably evolve into a linked list later. Those
104
* analysers could be compared to higher level processors.
105
* The field is blanked by buffer_init() and only by analysers themselves
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 */
111
#define AN_REQ_HTTP_TARPIT 0x00000008 /* wait for end of HTTP tarpit */
112
#define AN_RTR_HTTP_HDR 0x00000010 /* inspect HTTP response headers */
113
#define AN_REQ_UNIX_STATS 0x00000020 /* process unix stats socket request */
58
115
/* describes a chunk of string */
61
118
int len; /* size of the string from first to last char. <0 = uninit. */
121
/* needed for a declaration below */
65
125
unsigned int flags; /* BF_* */
66
struct timeval rex; /* expiration date for a read */
67
struct timeval wex; /* expiration date for a write */
68
struct timeval cex; /* expiration date for a connect */
69
struct timeval rto; /* read timeout */
70
struct timeval wto; /* write timeout */
71
struct timeval cto; /* connect timeout */
126
int rex; /* expiration date for a read, in ticks */
127
int wex; /* expiration date for a write or connect, in ticks */
128
int rto; /* read timeout, in ticks */
129
int wto; /* write timeout, in ticks */
130
int cto; /* connect timeout, in ticks */
72
131
unsigned int l; /* data length */
73
132
char *r, *w, *lr; /* read ptr, write ptr, last read */
74
char *rlim; /* read limit, used for header rewriting */
133
unsigned int max_len; /* read limit, used to keep room for header rewriting */
134
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 */
136
unsigned int analysers; /* bit field indicating what to do on the buffer */
137
int analyse_exp; /* expiration date for current analysers (if set) */
138
void (*hijacker)(struct session *, struct buffer *); /* alternative content producer */
139
unsigned char xfer_large; /* number of consecutive large xfers */
140
unsigned char xfer_small; /* number of consecutive small xfers */
75
141
unsigned long long total; /* total data read */
142
struct stream_interface *prod; /* producer attached to this buffer */
143
struct stream_interface *cons; /* consumer attached to this buffer */
144
struct pipe *pipe; /* non-NULL only when data present */
76
145
char data[BUFSIZE];
149
/* Note about the buffer structure
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
154
- the visible data (->data, for ->l bytes)
155
- the invisible data, typically in kernel buffers forwarded directly from
156
the source stream sock to the destination stream sock (->pipe->data
157
bytes). Those are used only during forward.
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).
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
171
reflected by ->pipe->data. This is very important as this and only this can
172
ensure strict ordering of data between buffers.
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.
181
The consumer is responsible for decreasing ->send_max when it sends data
182
from the visible buffer, and ->pipe->data when it sends data from the
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().
80
199
#endif /* _TYPES_BUFFERS_H */