~ubuntu-branches/debian/stretch/haproxy/stretch

« back to all changes in this revision

Viewing changes to include/types/buffers.h

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2009-06-26 00:11:01 UTC
  • mfrom: (1.1.6 upstream) (2.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090626001101-qo261ke2mjh3d8cn
* New Upstream Version (Closes: #534583).
* Add contrib directory in docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
  include/types/buffers.h
3
3
  Buffer management definitions, macros and inline functions.
4
4
 
5
 
  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
6
 
  
 
5
  Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
 
6
 
7
7
  This library is free software; you can redistribute it and/or
8
8
  modify it under the terms of the GNU Lesser General Public
9
9
  License as published by the Free Software Foundation, version 2.1
24
24
 
25
25
#include <common/config.h>
26
26
#include <common/memory.h>
27
 
 
28
 
#include <sys/time.h>
29
 
#include <sys/types.h>
 
27
#include <types/stream_interface.h>
30
28
 
31
29
/* The BF_* macros designate Buffer Flags, which may be ORed in the bit field
32
 
 * member 'flags' in struct buffer.
33
 
 */
34
 
#define BF_SHUTR_PENDING        1
35
 
#define BF_SHUTR_DONE           2
36
 
#define BF_SHUTR_STATUS         (BF_SHUTR_PENDING|BF_SHUTR_DONE)
37
 
 
38
 
#define BF_SHUTW_PENDING        4
39
 
#define BF_SHUTW_DONE           8
40
 
#define BF_SHUTW_STATUS         (BF_SHUTW_PENDING|BF_SHUTW_DONE)
41
 
 
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)
48
 
 
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)
55
 
 
56
 
 
 
30
 * 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).
 
51
 */
 
52
 
 
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)
 
58
 
 
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 */
 
63
 
 
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)
 
69
 
 
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 */
 
74
 
 
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 */
 
77
 
 
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
 
81
 * buffer.
 
82
 */
 
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) */
 
88
 
 
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))
 
93
 
 
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)
 
96
 
 
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)
 
99
 
 
100
 
 
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
 
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 */
 
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 */
57
114
 
58
115
/* describes a chunk of string */
59
116
struct chunk {
61
118
        int len;        /* size of the string from first to last char. <0 = uninit. */
62
119
};
63
120
 
 
121
/* needed for a declaration below */
 
122
struct session;
 
123
 
64
124
struct buffer {
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];
77
146
};
78
147
 
79
148
 
 
149
/* 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
 
156
       the source stream sock to the destination stream sock (->pipe->data
 
157
       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
 
171
   reflected by ->pipe->data. This is very important as this and only this can
 
172
   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
 
182
   from the visible buffer, and ->pipe->data when it sends data from the
 
183
   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
 
80
199
#endif /* _TYPES_BUFFERS_H */
81
200
 
82
201
/*