blob: aae602ec54908efcf9b41357f1b9194a90dc8a1f [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
Willy Tarreaufb90d942009-09-05 20:57:35 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaucff64112008-11-03 06:26:53 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <common/compat.h>
23#include <common/config.h>
24#include <common/debug.h>
25#include <common/standard.h>
26#include <common/ticks.h>
27#include <common/time.h>
28
29#include <proto/buffers.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010030#include <proto/fd.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020031#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010032#include <proto/stream_sock.h>
33#include <proto/task.h>
34
35/*
36 * This function only has to be called once after a wakeup event in case of
37 * suspected timeout. It controls the stream interface timeouts and sets
38 * si->flags accordingly. It does NOT close anything, as this timeout may
39 * be used for any purpose. It returns 1 if the timeout fired, otherwise
40 * zero.
41 */
42int stream_int_check_timeouts(struct stream_interface *si)
43{
44 if (tick_is_expired(si->exp, now_ms)) {
45 si->flags |= SI_FL_EXP;
46 return 1;
47 }
48 return 0;
49}
50
Willy Tarreaufe3718a2008-11-30 18:14:12 +010051/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010052void stream_int_report_error(struct stream_interface *si)
53{
54 if (!si->err_type)
55 si->err_type = SI_ET_DATA_ERR;
56
Willy Tarreaucff64112008-11-03 06:26:53 +010057 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010058 si->ib->flags |= BF_READ_ERROR;
59}
60
61/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010062 * Returns a message to the client ; the connection is shut down for read,
63 * and the request is cleared so that no server connection can be initiated.
64 * The buffer is marked for read shutdown on the other side to protect the
65 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +010066 * "chunk". If it is null, then an empty message is used. The reply buffer does
67 * not need to be empty before this, and its contents will not be overwritten.
68 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +010069 */
70void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
71{
Willy Tarreau148d0992010-01-10 10:21:21 +010072 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +010073 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +010074 buffer_auto_close(si->ib);
75 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +010076
77 buffer_cut_tail(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +010078 if (likely(msg && msg->len))
Willy Tarreaudded32d2008-11-30 19:48:07 +010079 buffer_write(si->ob, msg->str, msg->len);
80
81 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +010082 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +020083 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +010084 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +010085}
86
Willy Tarreaufb90d942009-09-05 20:57:35 +020087/* default update function for scheduled tasks, not used for embedded tasks */
88void stream_int_update(struct stream_interface *si)
89{
90 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
91 __FUNCTION__,
92 si, si->state, si->ib->flags, si->ob->flags);
93
94 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
95 task_wakeup(si->owner, TASK_WOKEN_IO);
96}
97
98/* default update function for embedded tasks, to be used at the end of the i/o handler */
99void stream_int_update_embedded(struct stream_interface *si)
100{
Willy Tarreau3488e252010-08-09 16:24:56 +0200101 int old_flags = si->flags;
102
Willy Tarreaufb90d942009-09-05 20:57:35 +0200103 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
104 __FUNCTION__,
105 si, si->state, si->ib->flags, si->ob->flags);
106
107 if (si->state != SI_ST_EST)
108 return;
109
110 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
111 si->shutw(si);
112
113 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
114 si->flags |= SI_FL_WAIT_DATA;
115
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200116 /* we're almost sure that we need some space if the buffer is not
117 * empty, even if it's not full, because the applets can't fill it.
118 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200119 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200120 si->flags |= SI_FL_WAIT_ROOM;
121
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200122 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200123 if (tick_isset(si->ob->wex))
124 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
125 }
126
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200127 if (si->ib->flags & BF_READ_ACTIVITY ||
128 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
129 if (tick_isset(si->ib->rex))
130 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
131 }
132
Willy Tarreau3488e252010-08-09 16:24:56 +0200133 /* save flags to detect changes */
134 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200135 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200136 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200137 si->ob->prod->chk_rcv(si->ob->prod);
138
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200139 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200140 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200141 si->ib->cons->chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200142 /* check if the consumer has freed some space */
143 if (!(si->ib->flags & BF_FULL))
144 si->flags &= ~SI_FL_WAIT_ROOM;
145 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200146
147 /* Note that we're trying to wake up in two conditions here :
148 * - special event, which needs the holder task attention
149 * - status indicating that the applet can go on working. This
150 * is rather hard because we might be blocking on output and
151 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200152 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200153 */
154 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200155 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
156
157 /* changes on the production side */
158 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
159 si->state != SI_ST_EST ||
160 (si->flags & SI_FL_ERR) ||
161 ((si->ib->flags & BF_READ_PARTIAL) &&
162 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
163
164 /* changes on the consumption side */
165 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
166 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
167 ((si->ob->flags & BF_SHUTW) ||
168 si->ob->prod->state != SI_ST_EST ||
169 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200170 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
171 task_wakeup(si->owner, TASK_WOKEN_IO);
172 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200173 if (si->ib->flags & BF_READ_ACTIVITY)
174 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200175}
176
177/* default shutr function for scheduled tasks */
178void stream_int_shutr(struct stream_interface *si)
179{
180 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
181 __FUNCTION__,
182 si, si->state, si->ib->flags, si->ob->flags);
183
184 si->ib->flags &= ~BF_SHUTR_NOW;
185 if (si->ib->flags & BF_SHUTR)
186 return;
187 si->ib->flags |= BF_SHUTR;
188 si->ib->rex = TICK_ETERNITY;
189 si->flags &= ~SI_FL_WAIT_ROOM;
190
191 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
192 return;
193
194 if (si->ob->flags & BF_SHUTW) {
195 si->state = SI_ST_DIS;
196 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200197
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200198 if (si->release)
199 si->release(si);
200 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200201
Willy Tarreaufb90d942009-09-05 20:57:35 +0200202 /* note that if the task exist, it must unregister itself once it runs */
203 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
204 task_wakeup(si->owner, TASK_WOKEN_IO);
205}
206
207/* default shutw function for scheduled tasks */
208void stream_int_shutw(struct stream_interface *si)
209{
210 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
211 __FUNCTION__,
212 si, si->state, si->ib->flags, si->ob->flags);
213
214 si->ob->flags &= ~BF_SHUTW_NOW;
215 if (si->ob->flags & BF_SHUTW)
216 return;
217 si->ob->flags |= BF_SHUTW;
218 si->ob->wex = TICK_ETERNITY;
219 si->flags &= ~SI_FL_WAIT_DATA;
220
221 switch (si->state) {
222 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200223 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200224 break;
225
226 /* fall through */
227 case SI_ST_CON:
228 case SI_ST_CER:
229 si->state = SI_ST_DIS;
230 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200231
232 if (si->release)
233 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200234 default:
235 si->flags &= ~SI_FL_WAIT_ROOM;
236 si->ib->flags |= BF_SHUTR;
237 si->ib->rex = TICK_ETERNITY;
238 si->exp = TICK_ETERNITY;
239 }
240
241 /* note that if the task exist, it must unregister itself once it runs */
242 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
243 task_wakeup(si->owner, TASK_WOKEN_IO);
244}
245
246/* default chk_rcv function for scheduled tasks */
247void stream_int_chk_rcv(struct stream_interface *si)
248{
249 struct buffer *ib = si->ib;
250
251 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
252 __FUNCTION__,
253 si, si->state, si->ib->flags, si->ob->flags);
254
255 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
256 return;
257
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200258 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200259 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200260 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261 si->flags |= SI_FL_WAIT_ROOM;
262 }
263 else {
264 /* (re)start reading */
265 si->flags &= ~SI_FL_WAIT_ROOM;
266 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
267 task_wakeup(si->owner, TASK_WOKEN_IO);
268 }
269}
270
271/* default chk_snd function for scheduled tasks */
272void stream_int_chk_snd(struct stream_interface *si)
273{
274 struct buffer *ob = si->ob;
275
276 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
277 __FUNCTION__,
278 si, si->state, si->ib->flags, si->ob->flags);
279
280 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
281 return;
282
283 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
284 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
285 return;
286
287 /* Otherwise there are remaining data to be sent in the buffer,
288 * so we tell the handler.
289 */
290 si->flags &= ~SI_FL_WAIT_DATA;
291 if (!tick_isset(ob->wex))
292 ob->wex = tick_add_ifset(now_ms, ob->wto);
293
294 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
295 task_wakeup(si->owner, TASK_WOKEN_IO);
296}
297
298/* Register a function to handle a stream_interface as part of the stream
299 * interface's owner task, which is returned. The SI will wake it up everytime
300 * it is solicited. The task's processing function must call the specified
301 * function before returning. It must be deleted by the task handler using
302 * stream_int_unregister_handler(), possibly from withing the function itself.
303 */
304struct task *stream_int_register_handler(struct stream_interface *si,
305 void (*fct)(struct stream_interface *))
306{
307 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
308
309 si->update = stream_int_update_embedded;
310 si->shutr = stream_int_shutr;
311 si->shutw = stream_int_shutw;
312 si->chk_rcv = stream_int_chk_rcv;
313 si->chk_snd = stream_int_chk_snd;
314 si->connect = NULL;
315 si->iohandler = fct;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200316 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200317 si->flags |= SI_FL_WAIT_DATA;
318 return si->owner;
319}
320
321/* Register a function to handle a stream_interface as a standalone task. The
322 * new task itself is returned and is assigned as si->owner. The stream_interface
323 * pointer will be pointed to by the task's context. The handler can be detached
324 * by using stream_int_unregister_handler().
325 */
326struct task *stream_int_register_handler_task(struct stream_interface *si,
327 struct task *(*fct)(struct task *))
328{
329 struct task *t;
330
331 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
332
333 si->update = stream_int_update;
334 si->shutr = stream_int_shutr;
335 si->shutw = stream_int_shutw;
336 si->chk_rcv = stream_int_chk_rcv;
337 si->chk_snd = stream_int_chk_snd;
338 si->connect = NULL;
339 si->iohandler = NULL; /* not used when running as an external task */
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200340 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200341 si->flags |= SI_FL_WAIT_DATA;
342
343 t = task_new();
344 si->owner = t;
345 if (!t)
346 return t;
347 t->process = fct;
348 t->context = si;
349 task_wakeup(si->owner, TASK_WOKEN_INIT);
350
351 return t;
352}
353
354/* Unregister a stream interface handler. This must be called by the handler task
355 * itself when it detects that it is in the SI_ST_DIS state. This function can
356 * both detach standalone handlers and embedded handlers.
357 */
358void stream_int_unregister_handler(struct stream_interface *si)
359{
360 if (!si->iohandler && si->owner) {
361 /* external handler : kill the task */
362 task_delete(si->owner);
363 task_free(si->owner);
364 }
365 si->iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200366 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200367 si->owner = NULL;
368}
369
Willy Tarreaudded32d2008-11-30 19:48:07 +0100370/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100371 * Local variables:
372 * c-indent-level: 8
373 * c-basic-offset: 8
374 * End:
375 */