blob: fcfa117b8ba859c3d42e179feaa8751c6e7b36c7 [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>
30#include <proto/client.h>
31#include <proto/fd.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020032#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010033#include <proto/stream_sock.h>
34#include <proto/task.h>
35
36/*
37 * This function only has to be called once after a wakeup event in case of
38 * suspected timeout. It controls the stream interface timeouts and sets
39 * si->flags accordingly. It does NOT close anything, as this timeout may
40 * be used for any purpose. It returns 1 if the timeout fired, otherwise
41 * zero.
42 */
43int stream_int_check_timeouts(struct stream_interface *si)
44{
45 if (tick_is_expired(si->exp, now_ms)) {
46 si->flags |= SI_FL_EXP;
47 return 1;
48 }
49 return 0;
50}
51
Willy Tarreaufe3718a2008-11-30 18:14:12 +010052/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010053void stream_int_report_error(struct stream_interface *si)
54{
55 if (!si->err_type)
56 si->err_type = SI_ET_DATA_ERR;
57
Willy Tarreaucff64112008-11-03 06:26:53 +010058 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010059 si->ib->flags |= BF_READ_ERROR;
60}
61
62/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010063 * Returns a message to the client ; the connection is shut down for read,
64 * and the request is cleared so that no server connection can be initiated.
65 * The buffer is marked for read shutdown on the other side to protect the
66 * message, and the buffer write is enabled. The message is contained in a
67 * "chunk". If it is null, then an empty message is used. The reply buffer
68 * doesn't need to be empty before this. The goal of this function is to
69 * return error messages to a client.
70 */
71void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
72{
73 buffer_abort(si->ib);
Willy Tarreau6f0aa472009-03-08 20:33:29 +010074 buffer_erase(si->ob);
Willy Tarreaudded32d2008-11-30 19:48:07 +010075 buffer_shutr_now(si->ob);
76 if (msg && msg->len)
77 buffer_write(si->ob, msg->str, msg->len);
78
79 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau520d95e2009-09-19 21:04:57 +020080 buffer_auto_close(si->ob);
Willy Tarreaudded32d2008-11-30 19:48:07 +010081}
82
Willy Tarreaufb90d942009-09-05 20:57:35 +020083/* default update function for scheduled tasks, not used for embedded tasks */
84void stream_int_update(struct stream_interface *si)
85{
86 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
87 __FUNCTION__,
88 si, si->state, si->ib->flags, si->ob->flags);
89
90 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
91 task_wakeup(si->owner, TASK_WOKEN_IO);
92}
93
94/* default update function for embedded tasks, to be used at the end of the i/o handler */
95void stream_int_update_embedded(struct stream_interface *si)
96{
97 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
98 __FUNCTION__,
99 si, si->state, si->ib->flags, si->ob->flags);
100
101 if (si->state != SI_ST_EST)
102 return;
103
104 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
105 si->shutw(si);
106
107 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
108 si->flags |= SI_FL_WAIT_DATA;
109
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200110 /* we're almost sure that we need some space if the buffer is not
111 * empty, even if it's not full, because the applets can't fill it.
112 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200113 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200114 si->flags |= SI_FL_WAIT_ROOM;
115
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200116 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200117 if (tick_isset(si->ob->wex))
118 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
119 }
120
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200121 if (si->ib->flags & BF_READ_ACTIVITY ||
122 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
123 if (tick_isset(si->ib->rex))
124 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
125 }
126
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200127 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 +0200128 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200129 si->ob->prod->chk_rcv(si->ob->prod);
130
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200131 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
132 (si->ib->cons->flags & SI_FL_WAIT_DATA))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200133 si->ib->cons->chk_snd(si->ib->cons);
134
135 /* Note that we're trying to wake up in two conditions here :
136 * - special event, which needs the holder task attention
137 * - status indicating that the applet can go on working. This
138 * is rather hard because we might be blocking on output and
139 * don't want to wake up on input and vice-versa. The idea is
140 * the to only rely the changes the chk_* might have performed.
141 */
142 if (/* check stream interface changes */
143 (si->flags & SI_FL_ERR) || si->state != SI_ST_EST || si->ib->cons->state != SI_ST_EST ||
144 /* check response buffer changes */
145 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR|BF_READ_DONTWAIT)) ||
146 ((si->ib->flags & BF_READ_ACTIVITY) && !si->ib->to_forward) ||
147 (!(si->ib->flags & BF_FULL) && (si->ib->flags & BF_WRITE_ACTIVITY) && si->ib->to_forward) ||
148 /* check request buffer changes */
149 (si->ob->flags & (BF_WRITE_ERROR)) ||
150 ((si->ob->flags & BF_WRITE_ACTIVITY) && (si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward) ||
151 (si->ob->flags & BF_READ_ACTIVITY)) {
152 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
153 task_wakeup(si->owner, TASK_WOKEN_IO);
154 }
155}
156
157/* default shutr function for scheduled tasks */
158void stream_int_shutr(struct stream_interface *si)
159{
160 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
161 __FUNCTION__,
162 si, si->state, si->ib->flags, si->ob->flags);
163
164 si->ib->flags &= ~BF_SHUTR_NOW;
165 if (si->ib->flags & BF_SHUTR)
166 return;
167 si->ib->flags |= BF_SHUTR;
168 si->ib->rex = TICK_ETERNITY;
169 si->flags &= ~SI_FL_WAIT_ROOM;
170
171 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
172 return;
173
174 if (si->ob->flags & BF_SHUTW) {
175 si->state = SI_ST_DIS;
176 si->exp = TICK_ETERNITY;
177 }
178
179 /* note that if the task exist, it must unregister itself once it runs */
180 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
181 task_wakeup(si->owner, TASK_WOKEN_IO);
182}
183
184/* default shutw function for scheduled tasks */
185void stream_int_shutw(struct stream_interface *si)
186{
187 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
188 __FUNCTION__,
189 si, si->state, si->ib->flags, si->ob->flags);
190
191 si->ob->flags &= ~BF_SHUTW_NOW;
192 if (si->ob->flags & BF_SHUTW)
193 return;
194 si->ob->flags |= BF_SHUTW;
195 si->ob->wex = TICK_ETERNITY;
196 si->flags &= ~SI_FL_WAIT_DATA;
197
198 switch (si->state) {
199 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200200 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200201 break;
202
203 /* fall through */
204 case SI_ST_CON:
205 case SI_ST_CER:
206 si->state = SI_ST_DIS;
207 /* fall through */
208 default:
209 si->flags &= ~SI_FL_WAIT_ROOM;
210 si->ib->flags |= BF_SHUTR;
211 si->ib->rex = TICK_ETERNITY;
212 si->exp = TICK_ETERNITY;
213 }
214
215 /* note that if the task exist, it must unregister itself once it runs */
216 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
217 task_wakeup(si->owner, TASK_WOKEN_IO);
218}
219
220/* default chk_rcv function for scheduled tasks */
221void stream_int_chk_rcv(struct stream_interface *si)
222{
223 struct buffer *ib = si->ib;
224
225 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
226 __FUNCTION__,
227 si, si->state, si->ib->flags, si->ob->flags);
228
229 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
230 return;
231
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200232 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200233 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200234 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200235 si->flags |= SI_FL_WAIT_ROOM;
236 }
237 else {
238 /* (re)start reading */
239 si->flags &= ~SI_FL_WAIT_ROOM;
240 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
241 task_wakeup(si->owner, TASK_WOKEN_IO);
242 }
243}
244
245/* default chk_snd function for scheduled tasks */
246void stream_int_chk_snd(struct stream_interface *si)
247{
248 struct buffer *ob = si->ob;
249
250 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
251 __FUNCTION__,
252 si, si->state, si->ib->flags, si->ob->flags);
253
254 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
255 return;
256
257 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
258 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
259 return;
260
261 /* Otherwise there are remaining data to be sent in the buffer,
262 * so we tell the handler.
263 */
264 si->flags &= ~SI_FL_WAIT_DATA;
265 if (!tick_isset(ob->wex))
266 ob->wex = tick_add_ifset(now_ms, ob->wto);
267
268 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
269 task_wakeup(si->owner, TASK_WOKEN_IO);
270}
271
272/* Register a function to handle a stream_interface as part of the stream
273 * interface's owner task, which is returned. The SI will wake it up everytime
274 * it is solicited. The task's processing function must call the specified
275 * function before returning. It must be deleted by the task handler using
276 * stream_int_unregister_handler(), possibly from withing the function itself.
277 */
278struct task *stream_int_register_handler(struct stream_interface *si,
279 void (*fct)(struct stream_interface *))
280{
281 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
282
283 si->update = stream_int_update_embedded;
284 si->shutr = stream_int_shutr;
285 si->shutw = stream_int_shutw;
286 si->chk_rcv = stream_int_chk_rcv;
287 si->chk_snd = stream_int_chk_snd;
288 si->connect = NULL;
289 si->iohandler = fct;
290 si->flags |= SI_FL_WAIT_DATA;
291 return si->owner;
292}
293
294/* Register a function to handle a stream_interface as a standalone task. The
295 * new task itself is returned and is assigned as si->owner. The stream_interface
296 * pointer will be pointed to by the task's context. The handler can be detached
297 * by using stream_int_unregister_handler().
298 */
299struct task *stream_int_register_handler_task(struct stream_interface *si,
300 struct task *(*fct)(struct task *))
301{
302 struct task *t;
303
304 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
305
306 si->update = stream_int_update;
307 si->shutr = stream_int_shutr;
308 si->shutw = stream_int_shutw;
309 si->chk_rcv = stream_int_chk_rcv;
310 si->chk_snd = stream_int_chk_snd;
311 si->connect = NULL;
312 si->iohandler = NULL; /* not used when running as an external task */
313 si->flags |= SI_FL_WAIT_DATA;
314
315 t = task_new();
316 si->owner = t;
317 if (!t)
318 return t;
319 t->process = fct;
320 t->context = si;
321 task_wakeup(si->owner, TASK_WOKEN_INIT);
322
323 return t;
324}
325
326/* Unregister a stream interface handler. This must be called by the handler task
327 * itself when it detects that it is in the SI_ST_DIS state. This function can
328 * both detach standalone handlers and embedded handlers.
329 */
330void stream_int_unregister_handler(struct stream_interface *si)
331{
332 if (!si->iohandler && si->owner) {
333 /* external handler : kill the task */
334 task_delete(si->owner);
335 task_free(si->owner);
336 }
337 si->iohandler = NULL;
338 si->owner = NULL;
339}
340
Willy Tarreaudded32d2008-11-30 19:48:07 +0100341/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100342 * Local variables:
343 * c-indent-level: 8
344 * c-basic-offset: 8
345 * End:
346 */