blob: f7f0881690ec5e3d83637e9f9b4c2ac44377c626 [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);
76 if (likely(msg && msg->len))
Willy Tarreaudded32d2008-11-30 19:48:07 +010077 buffer_write(si->ob, msg->str, msg->len);
78
79 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +010080 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +020081 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +010082 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +010083}
84
Willy Tarreaufb90d942009-09-05 20:57:35 +020085/* default update function for scheduled tasks, not used for embedded tasks */
86void stream_int_update(struct stream_interface *si)
87{
88 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
89 __FUNCTION__,
90 si, si->state, si->ib->flags, si->ob->flags);
91
92 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
93 task_wakeup(si->owner, TASK_WOKEN_IO);
94}
95
96/* default update function for embedded tasks, to be used at the end of the i/o handler */
97void stream_int_update_embedded(struct stream_interface *si)
98{
Willy Tarreau3488e252010-08-09 16:24:56 +020099 int old_flags = si->flags;
100
Willy Tarreaufb90d942009-09-05 20:57:35 +0200101 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
102 __FUNCTION__,
103 si, si->state, si->ib->flags, si->ob->flags);
104
105 if (si->state != SI_ST_EST)
106 return;
107
108 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
109 si->shutw(si);
110
111 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
112 si->flags |= SI_FL_WAIT_DATA;
113
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200114 /* we're almost sure that we need some space if the buffer is not
115 * empty, even if it's not full, because the applets can't fill it.
116 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200117 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200118 si->flags |= SI_FL_WAIT_ROOM;
119
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200120 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200121 if (tick_isset(si->ob->wex))
122 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
123 }
124
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200125 if (si->ib->flags & BF_READ_ACTIVITY ||
126 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
127 if (tick_isset(si->ib->rex))
128 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
129 }
130
Willy Tarreau3488e252010-08-09 16:24:56 +0200131 /* save flags to detect changes */
132 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200133 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 +0200134 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200135 si->ob->prod->chk_rcv(si->ob->prod);
136
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200137 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200138 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200139 si->ib->cons->chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200140 /* check if the consumer has freed some space */
141 if (!(si->ib->flags & BF_FULL))
142 si->flags &= ~SI_FL_WAIT_ROOM;
143 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200144
145 /* Note that we're trying to wake up in two conditions here :
146 * - special event, which needs the holder task attention
147 * - status indicating that the applet can go on working. This
148 * is rather hard because we might be blocking on output and
149 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200150 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200151 */
152 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200153 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
154
155 /* changes on the production side */
156 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
157 si->state != SI_ST_EST ||
158 (si->flags & SI_FL_ERR) ||
159 ((si->ib->flags & BF_READ_PARTIAL) &&
160 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
161
162 /* changes on the consumption side */
163 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
164 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
165 ((si->ob->flags & BF_SHUTW) ||
166 si->ob->prod->state != SI_ST_EST ||
167 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200168 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
169 task_wakeup(si->owner, TASK_WOKEN_IO);
170 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200171 if (si->ib->flags & BF_READ_ACTIVITY)
172 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200173}
174
175/* default shutr function for scheduled tasks */
176void stream_int_shutr(struct stream_interface *si)
177{
178 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
179 __FUNCTION__,
180 si, si->state, si->ib->flags, si->ob->flags);
181
182 si->ib->flags &= ~BF_SHUTR_NOW;
183 if (si->ib->flags & BF_SHUTR)
184 return;
185 si->ib->flags |= BF_SHUTR;
186 si->ib->rex = TICK_ETERNITY;
187 si->flags &= ~SI_FL_WAIT_ROOM;
188
189 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
190 return;
191
192 if (si->ob->flags & BF_SHUTW) {
193 si->state = SI_ST_DIS;
194 si->exp = TICK_ETERNITY;
195 }
196
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200197 if (si->release)
198 si->release(si);
199
Willy Tarreaufb90d942009-09-05 20:57:35 +0200200 /* note that if the task exist, it must unregister itself once it runs */
201 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
202 task_wakeup(si->owner, TASK_WOKEN_IO);
203}
204
205/* default shutw function for scheduled tasks */
206void stream_int_shutw(struct stream_interface *si)
207{
208 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
209 __FUNCTION__,
210 si, si->state, si->ib->flags, si->ob->flags);
211
212 si->ob->flags &= ~BF_SHUTW_NOW;
213 if (si->ob->flags & BF_SHUTW)
214 return;
215 si->ob->flags |= BF_SHUTW;
216 si->ob->wex = TICK_ETERNITY;
217 si->flags &= ~SI_FL_WAIT_DATA;
218
219 switch (si->state) {
220 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200221 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200222 break;
223
224 /* fall through */
225 case SI_ST_CON:
226 case SI_ST_CER:
227 si->state = SI_ST_DIS;
228 /* fall through */
229 default:
230 si->flags &= ~SI_FL_WAIT_ROOM;
231 si->ib->flags |= BF_SHUTR;
232 si->ib->rex = TICK_ETERNITY;
233 si->exp = TICK_ETERNITY;
234 }
235
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200236 if (si->release)
237 si->release(si);
238
Willy Tarreaufb90d942009-09-05 20:57:35 +0200239 /* note that if the task exist, it must unregister itself once it runs */
240 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
241 task_wakeup(si->owner, TASK_WOKEN_IO);
242}
243
244/* default chk_rcv function for scheduled tasks */
245void stream_int_chk_rcv(struct stream_interface *si)
246{
247 struct buffer *ib = si->ib;
248
249 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
250 __FUNCTION__,
251 si, si->state, si->ib->flags, si->ob->flags);
252
253 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
254 return;
255
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200256 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200257 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200258 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200259 si->flags |= SI_FL_WAIT_ROOM;
260 }
261 else {
262 /* (re)start reading */
263 si->flags &= ~SI_FL_WAIT_ROOM;
264 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
265 task_wakeup(si->owner, TASK_WOKEN_IO);
266 }
267}
268
269/* default chk_snd function for scheduled tasks */
270void stream_int_chk_snd(struct stream_interface *si)
271{
272 struct buffer *ob = si->ob;
273
274 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
275 __FUNCTION__,
276 si, si->state, si->ib->flags, si->ob->flags);
277
278 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
279 return;
280
281 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
282 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
283 return;
284
285 /* Otherwise there are remaining data to be sent in the buffer,
286 * so we tell the handler.
287 */
288 si->flags &= ~SI_FL_WAIT_DATA;
289 if (!tick_isset(ob->wex))
290 ob->wex = tick_add_ifset(now_ms, ob->wto);
291
292 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
293 task_wakeup(si->owner, TASK_WOKEN_IO);
294}
295
296/* Register a function to handle a stream_interface as part of the stream
297 * interface's owner task, which is returned. The SI will wake it up everytime
298 * it is solicited. The task's processing function must call the specified
299 * function before returning. It must be deleted by the task handler using
300 * stream_int_unregister_handler(), possibly from withing the function itself.
301 */
302struct task *stream_int_register_handler(struct stream_interface *si,
303 void (*fct)(struct stream_interface *))
304{
305 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
306
307 si->update = stream_int_update_embedded;
308 si->shutr = stream_int_shutr;
309 si->shutw = stream_int_shutw;
310 si->chk_rcv = stream_int_chk_rcv;
311 si->chk_snd = stream_int_chk_snd;
312 si->connect = NULL;
313 si->iohandler = fct;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200314 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200315 si->flags |= SI_FL_WAIT_DATA;
316 return si->owner;
317}
318
319/* Register a function to handle a stream_interface as a standalone task. The
320 * new task itself is returned and is assigned as si->owner. The stream_interface
321 * pointer will be pointed to by the task's context. The handler can be detached
322 * by using stream_int_unregister_handler().
323 */
324struct task *stream_int_register_handler_task(struct stream_interface *si,
325 struct task *(*fct)(struct task *))
326{
327 struct task *t;
328
329 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
330
331 si->update = stream_int_update;
332 si->shutr = stream_int_shutr;
333 si->shutw = stream_int_shutw;
334 si->chk_rcv = stream_int_chk_rcv;
335 si->chk_snd = stream_int_chk_snd;
336 si->connect = NULL;
337 si->iohandler = NULL; /* not used when running as an external task */
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200338 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200339 si->flags |= SI_FL_WAIT_DATA;
340
341 t = task_new();
342 si->owner = t;
343 if (!t)
344 return t;
345 t->process = fct;
346 t->context = si;
347 task_wakeup(si->owner, TASK_WOKEN_INIT);
348
349 return t;
350}
351
352/* Unregister a stream interface handler. This must be called by the handler task
353 * itself when it detects that it is in the SI_ST_DIS state. This function can
354 * both detach standalone handlers and embedded handlers.
355 */
356void stream_int_unregister_handler(struct stream_interface *si)
357{
358 if (!si->iohandler && si->owner) {
359 /* external handler : kill the task */
360 task_delete(si->owner);
361 task_free(si->owner);
362 }
363 si->iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200364 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200365 si->owner = NULL;
366}
367
Willy Tarreaudded32d2008-11-30 19:48:07 +0100368/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100369 * Local variables:
370 * c-indent-level: 8
371 * c-basic-offset: 8
372 * End:
373 */