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

« back to all changes in this revision

Viewing changes to include/proto/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/proto/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
28
28
 
29
29
#include <common/config.h>
30
30
#include <common/memory.h>
 
31
#include <common/ticks.h>
31
32
#include <common/time.h>
32
33
 
33
34
#include <types/buffers.h>
37
38
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
38
39
int init_buffer();
39
40
 
40
 
/* Initializes all fields in the buffer. The ->rlim field is initialized last
 
41
/* Initializes all fields in the buffer. The ->max_len field is initialized last
41
42
 * so that the compiler can optimize it away if changed immediately after the
42
 
 * call to this function.
 
43
 * call to this function. By default, it is set to the full size of the buffer.
 
44
 * The BF_EMPTY flags is set.
43
45
 */
44
46
static inline void buffer_init(struct buffer *buf)
45
47
{
46
 
        buf->l = buf->total = buf->flags = 0;
47
 
        buf->rlim = buf->r = buf->lr = buf->w = buf->data;
 
48
        buf->send_max = 0;
 
49
        buf->to_forward = 0;
 
50
        buf->l = buf->total = 0;
 
51
        buf->pipe = NULL;
 
52
        buf->analysers = 0;
 
53
        buf->cons = NULL;
 
54
        buf->flags = BF_EMPTY;
 
55
        buf->r = buf->lr = buf->w = buf->data;
 
56
        buf->max_len = BUFSIZE;
48
57
}
49
58
 
50
59
/* returns 1 if the buffer is empty, 0 otherwise */
58
67
        return buf->l == BUFSIZE;
59
68
}
60
69
 
61
 
/* flushes any content from buffer <buf> */
 
70
/* Check buffer timeouts, and set the corresponding flags. The
 
71
 * likely/unlikely have been optimized for fastest normal path.
 
72
 * The read/write timeouts are not set if there was activity on the buffer.
 
73
 * That way, we don't have to update the timeout on every I/O. Note that the
 
74
 * analyser timeout is always checked.
 
75
 */
 
76
static inline void buffer_check_timeouts(struct buffer *b)
 
77
{
 
78
        if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
 
79
            unlikely(tick_is_expired(b->rex, now_ms)))
 
80
                b->flags |= BF_READ_TIMEOUT;
 
81
 
 
82
        if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
 
83
            unlikely(tick_is_expired(b->wex, now_ms)))
 
84
                b->flags |= BF_WRITE_TIMEOUT;
 
85
 
 
86
        if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
 
87
            unlikely(tick_is_expired(b->analyse_exp, now_ms)))
 
88
                b->flags |= BF_ANA_TIMEOUT;
 
89
}
 
90
 
 
91
/* Schedule <bytes> more bytes to be forwarded by the buffer without notifying
 
92
 * the task. Any pending data in the buffer is scheduled to be sent as well,
 
93
 * in the limit of the number of bytes to forward. This must be the only method
 
94
 * to use to schedule bytes to be sent. Directly touching ->to_forward will
 
95
 * cause lockups when send_max goes down to zero if nobody is ready to push the
 
96
 * remaining data.
 
97
 */
 
98
static inline void buffer_forward(struct buffer *buf, unsigned int bytes)
 
99
{
 
100
        unsigned int data_left;
 
101
 
 
102
        buf->to_forward += bytes;
 
103
        data_left = buf->l - buf->send_max;
 
104
        if (data_left > buf->to_forward)
 
105
                data_left = buf->to_forward;
 
106
 
 
107
        buf->to_forward -= data_left;
 
108
        buf->send_max += data_left;
 
109
}
 
110
 
 
111
/* Schedule all remaining buffer data to be sent. send_max is not touched if it
 
112
 * already covers those data. That permits doing a flush even after a forward,
 
113
 * although not recommended.
 
114
 */
62
115
static inline void buffer_flush(struct buffer *buf)
63
116
{
 
117
        if (buf->send_max < buf->l)
 
118
                buf->send_max = buf->l;
 
119
}
 
120
 
 
121
/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
 
122
 * that any spliced data is not affected since we may not have any access to
 
123
 * it.
 
124
 */
 
125
static inline void buffer_erase(struct buffer *buf)
 
126
{
 
127
        buf->send_max = 0;
 
128
        buf->to_forward = 0;
64
129
        buf->r = buf->lr = buf->w = buf->data;
65
130
        buf->l = 0;
 
131
        buf->flags |= BF_EMPTY | BF_FULL;
 
132
        if (buf->max_len)
 
133
                buf->flags &= ~BF_FULL;
66
134
}
67
135
 
68
 
/* marks the buffer as "shutdown pending" for reads and cancels the timeout */
 
136
/* marks the buffer as "shutdown" for reads and cancels the timeout */
69
137
static inline void buffer_shutr(struct buffer *buf)
70
138
{
71
 
        tv_eternity(&buf->rex);
72
 
        buf->flags |= BF_SHUTR_PENDING;
 
139
        buf->rex = TICK_ETERNITY;
 
140
        buf->flags |= BF_SHUTR;
73
141
}
74
142
 
75
 
/* marks the buffer as "shutdown pending" for writes and cancels the timeout */
 
143
/* marks the buffer as "shutdown" for writes and cancels the timeout */
76
144
static inline void buffer_shutw(struct buffer *buf)
77
145
{
78
 
        tv_eternity(&buf->wex);
79
 
        buf->flags |= BF_SHUTW_PENDING;
80
 
}
81
 
 
 
146
        buf->wex = TICK_ETERNITY;
 
147
        buf->flags |= BF_SHUTW;
 
148
}
 
149
 
 
150
/* marks the buffer as "shutdown" ASAP for reads */
 
151
static inline void buffer_shutr_now(struct buffer *buf)
 
152
{
 
153
        buf->flags |= BF_SHUTR_NOW;
 
154
}
 
155
 
 
156
/* marks the buffer as "shutdown" ASAP for writes */
 
157
static inline void buffer_shutw_now(struct buffer *buf)
 
158
{
 
159
        buf->flags |= BF_SHUTW_NOW;
 
160
}
 
161
 
 
162
/* marks the buffer as "shutdown" ASAP in both directions */
 
163
static inline void buffer_abort(struct buffer *buf)
 
164
{
 
165
        buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
 
166
}
 
167
 
 
168
/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
 
169
 * flag is set, and the function called once. The function is responsible for
 
170
 * clearing the hijack bit. It is possible that the function clears the flag
 
171
 * during this first call.
 
172
 */
 
173
static inline void buffer_install_hijacker(struct session *s,
 
174
                                           struct buffer *b,
 
175
                                           void (*func)(struct session *, struct buffer *))
 
176
{
 
177
        b->hijacker = func;
 
178
        b->flags |= BF_HIJACK;
 
179
        func(s, b);
 
180
}
 
181
 
 
182
/* Releases the buffer from hijacking mode. Often used by the hijack function */
 
183
static inline void buffer_stop_hijack(struct buffer *buf)
 
184
{
 
185
        buf->flags &= ~BF_HIJACK;
 
186
}
 
187
 
 
188
/* allows the consumer to send the buffer contents */
 
189
static inline void buffer_write_ena(struct buffer *buf)
 
190
{
 
191
        buf->flags |= BF_WRITE_ENA;
 
192
}
 
193
 
 
194
/* prevents the consumer from sending the buffer contents */
 
195
static inline void buffer_write_dis(struct buffer *buf)
 
196
{
 
197
        buf->flags &= ~BF_WRITE_ENA;
 
198
}
 
199
 
 
200
/* check if the buffer needs to be shut down for read, and perform the shutdown
 
201
 * at the stream_interface level if needed. This must not be used with a buffer
 
202
 * for which a connection is currently in queue or turn-around.
 
203
 */
 
204
static inline void buffer_check_shutr(struct buffer *b)
 
205
{
 
206
        if (b->flags & BF_SHUTR)
 
207
                return;
 
208
 
 
209
        if (!(b->flags & (BF_SHUTR_NOW|BF_SHUTW)))
 
210
                return;
 
211
 
 
212
        /* Last read, forced read-shutdown, or other end closed. We have to
 
213
         * close our read side and inform the stream_interface.
 
214
         */
 
215
        b->prod->shutr(b->prod);
 
216
}
 
217
 
 
218
/* check if the buffer needs to be shut down for write, and perform the shutdown
 
219
 * at the stream_interface level if needed. This must not be used with a buffer
 
220
 * for which a connection is currently in queue or turn-around.
 
221
 */
 
222
static inline void buffer_check_shutw(struct buffer *b)
 
223
{
 
224
        if (b->flags & BF_SHUTW)
 
225
                return;
 
226
 
 
227
        if ((b->flags & BF_SHUTW_NOW) ||
 
228
            (b->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
 
229
            (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
 
230
                /* Application requested write-shutdown, or other end closed
 
231
                 * with empty buffer. We have to close our write side and
 
232
                 * inform the stream_interface.
 
233
                 */
 
234
                b->cons->shutw(b->cons);
 
235
        }
 
236
}
82
237
 
83
238
/* returns the maximum number of bytes writable at once in this buffer */
84
239
static inline int buffer_max(const struct buffer *buf)
91
246
                return buf->w - buf->r;
92
247
}
93
248
 
 
249
/* sets the buffer read limit to <size> bytes, and adjusts the FULL
 
250
 * flag accordingly.
 
251
 */
 
252
static inline void buffer_set_rlim(struct buffer *buf, int size)
 
253
{
 
254
        buf->max_len = size;
 
255
        if (buf->l < size)
 
256
                buf->flags &= ~BF_FULL;
 
257
        else
 
258
                buf->flags |= BF_FULL;
 
259
}
94
260
 
95
261
/*
96
262
 * Tries to realign the given buffer, and returns how many bytes can be written
111
277
int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
112
278
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
113
279
int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
114
 
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
 
280
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
 
281
        __attribute__ ((format(printf, 3, 4)));
115
282
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
116
283
 
117
284
/*