blob: d4bf1b895bc139b6443a655a14780c7c02ac55fe [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
Willy Tarreaub24281b2011-02-13 13:16:36 +01004 * Copyright 2000-2011 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:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100229 case SI_ST_QUE:
230 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200231 si->state = SI_ST_DIS;
232 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200233
234 if (si->release)
235 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200236 default:
237 si->flags &= ~SI_FL_WAIT_ROOM;
238 si->ib->flags |= BF_SHUTR;
239 si->ib->rex = TICK_ETERNITY;
240 si->exp = TICK_ETERNITY;
241 }
242
243 /* note that if the task exist, it must unregister itself once it runs */
244 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
245 task_wakeup(si->owner, TASK_WOKEN_IO);
246}
247
248/* default chk_rcv function for scheduled tasks */
249void stream_int_chk_rcv(struct stream_interface *si)
250{
251 struct buffer *ib = si->ib;
252
253 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
254 __FUNCTION__,
255 si, si->state, si->ib->flags, si->ob->flags);
256
257 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
258 return;
259
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200260 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200262 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200263 si->flags |= SI_FL_WAIT_ROOM;
264 }
265 else {
266 /* (re)start reading */
267 si->flags &= ~SI_FL_WAIT_ROOM;
268 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
269 task_wakeup(si->owner, TASK_WOKEN_IO);
270 }
271}
272
273/* default chk_snd function for scheduled tasks */
274void stream_int_chk_snd(struct stream_interface *si)
275{
276 struct buffer *ob = si->ob;
277
278 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
279 __FUNCTION__,
280 si, si->state, si->ib->flags, si->ob->flags);
281
282 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
283 return;
284
285 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
286 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
287 return;
288
289 /* Otherwise there are remaining data to be sent in the buffer,
290 * so we tell the handler.
291 */
292 si->flags &= ~SI_FL_WAIT_DATA;
293 if (!tick_isset(ob->wex))
294 ob->wex = tick_add_ifset(now_ms, ob->wto);
295
296 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
297 task_wakeup(si->owner, TASK_WOKEN_IO);
298}
299
Willy Tarreaub24281b2011-02-13 13:16:36 +0100300/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200301 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100302 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200303 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100304 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreau295a8372011-03-10 11:25:07 +0100305 * It also pre-initializes applet.state to zero.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200306 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100307struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200308{
309 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
310
311 si->update = stream_int_update_embedded;
312 si->shutr = stream_int_shutr;
313 si->shutw = stream_int_shutw;
314 si->chk_rcv = stream_int_chk_rcv;
315 si->chk_snd = stream_int_chk_snd;
316 si->connect = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100317 set_target_applet(&si->target, app);
Willy Tarreau295a8372011-03-10 11:25:07 +0100318 si->applet.state = 0;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200319 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200320 si->flags |= SI_FL_WAIT_DATA;
321 return si->owner;
322}
323
324/* Register a function to handle a stream_interface as a standalone task. The
325 * new task itself is returned and is assigned as si->owner. The stream_interface
326 * pointer will be pointed to by the task's context. The handler can be detached
327 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100328 * FIXME: the code should be updated to ensure that we don't change si->owner
329 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200330 */
331struct task *stream_int_register_handler_task(struct stream_interface *si,
332 struct task *(*fct)(struct task *))
333{
334 struct task *t;
335
336 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
337
338 si->update = stream_int_update;
339 si->shutr = stream_int_shutr;
340 si->shutw = stream_int_shutw;
341 si->chk_rcv = stream_int_chk_rcv;
342 si->chk_snd = stream_int_chk_snd;
343 si->connect = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100344 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200345 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200346 si->flags |= SI_FL_WAIT_DATA;
347
348 t = task_new();
349 si->owner = t;
350 if (!t)
351 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100352
Willy Tarreau9e000c62011-03-10 14:03:36 +0100353 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100354
Willy Tarreaufb90d942009-09-05 20:57:35 +0200355 t->process = fct;
356 t->context = si;
357 task_wakeup(si->owner, TASK_WOKEN_INIT);
358
359 return t;
360}
361
362/* Unregister a stream interface handler. This must be called by the handler task
363 * itself when it detects that it is in the SI_ST_DIS state. This function can
364 * both detach standalone handlers and embedded handlers.
365 */
366void stream_int_unregister_handler(struct stream_interface *si)
367{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100368 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200369 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100370 task_delete(si->target.ptr.t);
371 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200372 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200373 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200374 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100375 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200376}
377
Willy Tarreaudded32d2008-11-30 19:48:07 +0100378/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100379 * Local variables:
380 * c-indent-level: 8
381 * c-basic-offset: 8
382 * End:
383 */