blob: 35ebc30596fefb4bbe0c9f1580dccdd34eed3d34 [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
Willy Tarreau5c979a92012-05-07 17:15:39 +020035/* socket operations for embedded tasks */
36struct sock_ops stream_int_embedded = {
37 .update = stream_int_update_embedded,
38 .shutr = stream_int_shutr,
39 .shutw = stream_int_shutw,
40 .chk_rcv = stream_int_chk_rcv,
41 .chk_snd = stream_int_chk_snd,
42 .read = NULL,
43 .write = NULL,
44};
45
46/* socket operations for external tasks */
47struct sock_ops stream_int_task = {
48 .update = stream_int_update,
49 .shutr = stream_int_shutr,
50 .shutw = stream_int_shutw,
51 .chk_rcv = stream_int_chk_rcv,
52 .chk_snd = stream_int_chk_snd,
53 .read = NULL,
54 .write = NULL,
55};
56
Willy Tarreaucff64112008-11-03 06:26:53 +010057/*
58 * This function only has to be called once after a wakeup event in case of
59 * suspected timeout. It controls the stream interface timeouts and sets
60 * si->flags accordingly. It does NOT close anything, as this timeout may
61 * be used for any purpose. It returns 1 if the timeout fired, otherwise
62 * zero.
63 */
64int stream_int_check_timeouts(struct stream_interface *si)
65{
66 if (tick_is_expired(si->exp, now_ms)) {
67 si->flags |= SI_FL_EXP;
68 return 1;
69 }
70 return 0;
71}
72
Willy Tarreaufe3718a2008-11-30 18:14:12 +010073/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010074void stream_int_report_error(struct stream_interface *si)
75{
76 if (!si->err_type)
77 si->err_type = SI_ET_DATA_ERR;
78
Willy Tarreaucff64112008-11-03 06:26:53 +010079 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010080 si->ib->flags |= BF_READ_ERROR;
81}
82
83/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010084 * Returns a message to the client ; the connection is shut down for read,
85 * and the request is cleared so that no server connection can be initiated.
86 * The buffer is marked for read shutdown on the other side to protect the
87 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +010088 * "chunk". If it is null, then an empty message is used. The reply buffer does
89 * not need to be empty before this, and its contents will not be overwritten.
90 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +010091 */
92void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
93{
Willy Tarreau148d0992010-01-10 10:21:21 +010094 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +010095 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +010096 buffer_auto_close(si->ib);
97 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +010098
Willy Tarreau9dab5fc2012-05-07 11:56:55 +020099 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100100 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200101 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100102
103 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100104 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200105 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100106 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100107}
108
Willy Tarreaufb90d942009-09-05 20:57:35 +0200109/* default update function for scheduled tasks, not used for embedded tasks */
110void stream_int_update(struct stream_interface *si)
111{
112 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
113 __FUNCTION__,
114 si, si->state, si->ib->flags, si->ob->flags);
115
116 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
117 task_wakeup(si->owner, TASK_WOKEN_IO);
118}
119
120/* default update function for embedded tasks, to be used at the end of the i/o handler */
121void stream_int_update_embedded(struct stream_interface *si)
122{
Willy Tarreau3488e252010-08-09 16:24:56 +0200123 int old_flags = si->flags;
124
Willy Tarreaufb90d942009-09-05 20:57:35 +0200125 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
126 __FUNCTION__,
127 si, si->state, si->ib->flags, si->ob->flags);
128
129 if (si->state != SI_ST_EST)
130 return;
131
132 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
Willy Tarreau060781f2012-05-07 16:50:03 +0200133 si->sock.shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200134
135 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
136 si->flags |= SI_FL_WAIT_DATA;
137
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200138 /* we're almost sure that we need some space if the buffer is not
139 * empty, even if it's not full, because the applets can't fill it.
140 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200141 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200142 si->flags |= SI_FL_WAIT_ROOM;
143
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200144 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200145 if (tick_isset(si->ob->wex))
146 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
147 }
148
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200149 if (si->ib->flags & BF_READ_ACTIVITY ||
150 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
151 if (tick_isset(si->ib->rex))
152 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
153 }
154
Willy Tarreau3488e252010-08-09 16:24:56 +0200155 /* save flags to detect changes */
156 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200157 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 +0200158 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau060781f2012-05-07 16:50:03 +0200159 si->ob->prod->sock.chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200160
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200161 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200162 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau060781f2012-05-07 16:50:03 +0200163 si->ib->cons->sock.chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200164 /* check if the consumer has freed some space */
165 if (!(si->ib->flags & BF_FULL))
166 si->flags &= ~SI_FL_WAIT_ROOM;
167 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200168
169 /* Note that we're trying to wake up in two conditions here :
170 * - special event, which needs the holder task attention
171 * - status indicating that the applet can go on working. This
172 * is rather hard because we might be blocking on output and
173 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200174 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200175 */
176 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200177 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
178
179 /* changes on the production side */
180 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
181 si->state != SI_ST_EST ||
182 (si->flags & SI_FL_ERR) ||
183 ((si->ib->flags & BF_READ_PARTIAL) &&
184 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
185
186 /* changes on the consumption side */
187 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
188 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
189 ((si->ob->flags & BF_SHUTW) ||
190 si->ob->prod->state != SI_ST_EST ||
191 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200192 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
193 task_wakeup(si->owner, TASK_WOKEN_IO);
194 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200195 if (si->ib->flags & BF_READ_ACTIVITY)
196 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200197}
198
199/* default shutr function for scheduled tasks */
200void stream_int_shutr(struct stream_interface *si)
201{
202 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
203 __FUNCTION__,
204 si, si->state, si->ib->flags, si->ob->flags);
205
206 si->ib->flags &= ~BF_SHUTR_NOW;
207 if (si->ib->flags & BF_SHUTR)
208 return;
209 si->ib->flags |= BF_SHUTR;
210 si->ib->rex = TICK_ETERNITY;
211 si->flags &= ~SI_FL_WAIT_ROOM;
212
213 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
214 return;
215
216 if (si->ob->flags & BF_SHUTW) {
217 si->state = SI_ST_DIS;
218 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200219
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200220 if (si->release)
221 si->release(si);
222 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200223
Willy Tarreaufb90d942009-09-05 20:57:35 +0200224 /* note that if the task exist, it must unregister itself once it runs */
225 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
226 task_wakeup(si->owner, TASK_WOKEN_IO);
227}
228
229/* default shutw function for scheduled tasks */
230void stream_int_shutw(struct stream_interface *si)
231{
232 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
233 __FUNCTION__,
234 si, si->state, si->ib->flags, si->ob->flags);
235
236 si->ob->flags &= ~BF_SHUTW_NOW;
237 if (si->ob->flags & BF_SHUTW)
238 return;
239 si->ob->flags |= BF_SHUTW;
240 si->ob->wex = TICK_ETERNITY;
241 si->flags &= ~SI_FL_WAIT_DATA;
242
243 switch (si->state) {
244 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200245 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200246 break;
247
248 /* fall through */
249 case SI_ST_CON:
250 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100251 case SI_ST_QUE:
252 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200253 si->state = SI_ST_DIS;
254 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200255
256 if (si->release)
257 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200258 default:
259 si->flags &= ~SI_FL_WAIT_ROOM;
260 si->ib->flags |= BF_SHUTR;
261 si->ib->rex = TICK_ETERNITY;
262 si->exp = TICK_ETERNITY;
263 }
264
265 /* note that if the task exist, it must unregister itself once it runs */
266 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
267 task_wakeup(si->owner, TASK_WOKEN_IO);
268}
269
270/* default chk_rcv function for scheduled tasks */
271void stream_int_chk_rcv(struct stream_interface *si)
272{
273 struct buffer *ib = si->ib;
274
275 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
276 __FUNCTION__,
277 si, si->state, si->ib->flags, si->ob->flags);
278
279 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
280 return;
281
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200282 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200283 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200284 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200285 si->flags |= SI_FL_WAIT_ROOM;
286 }
287 else {
288 /* (re)start reading */
289 si->flags &= ~SI_FL_WAIT_ROOM;
290 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
291 task_wakeup(si->owner, TASK_WOKEN_IO);
292 }
293}
294
295/* default chk_snd function for scheduled tasks */
296void stream_int_chk_snd(struct stream_interface *si)
297{
298 struct buffer *ob = si->ob;
299
300 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
301 __FUNCTION__,
302 si, si->state, si->ib->flags, si->ob->flags);
303
304 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
305 return;
306
307 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
308 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
309 return;
310
311 /* Otherwise there are remaining data to be sent in the buffer,
312 * so we tell the handler.
313 */
314 si->flags &= ~SI_FL_WAIT_DATA;
315 if (!tick_isset(ob->wex))
316 ob->wex = tick_add_ifset(now_ms, ob->wto);
317
318 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
319 task_wakeup(si->owner, TASK_WOKEN_IO);
320}
321
Willy Tarreaub24281b2011-02-13 13:16:36 +0100322/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200323 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100324 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200325 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100326 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreau295a8372011-03-10 11:25:07 +0100327 * It also pre-initializes applet.state to zero.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200328 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100329struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200330{
Simon Horman7abd00d2011-08-13 08:03:51 +0900331 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200332
Willy Tarreau5c979a92012-05-07 17:15:39 +0200333 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200334 si->connect = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100335 set_target_applet(&si->target, app);
Willy Tarreau295a8372011-03-10 11:25:07 +0100336 si->applet.state = 0;
Aman Gupta9a13e842012-04-02 18:57:53 -0700337 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200338 si->flags |= SI_FL_WAIT_DATA;
339 return si->owner;
340}
341
342/* Register a function to handle a stream_interface as a standalone task. The
343 * new task itself is returned and is assigned as si->owner. The stream_interface
344 * pointer will be pointed to by the task's context. The handler can be detached
345 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100346 * FIXME: the code should be updated to ensure that we don't change si->owner
347 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200348 */
349struct task *stream_int_register_handler_task(struct stream_interface *si,
350 struct task *(*fct)(struct task *))
351{
352 struct task *t;
353
354 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
355
Willy Tarreau5c979a92012-05-07 17:15:39 +0200356 stream_interface_prepare(si, &stream_int_task);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200357 si->connect = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100358 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200359 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200360 si->flags |= SI_FL_WAIT_DATA;
361
362 t = task_new();
363 si->owner = t;
364 if (!t)
365 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100366
Willy Tarreau9e000c62011-03-10 14:03:36 +0100367 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100368
Willy Tarreaufb90d942009-09-05 20:57:35 +0200369 t->process = fct;
370 t->context = si;
371 task_wakeup(si->owner, TASK_WOKEN_INIT);
372
373 return t;
374}
375
376/* Unregister a stream interface handler. This must be called by the handler task
377 * itself when it detects that it is in the SI_ST_DIS state. This function can
378 * both detach standalone handlers and embedded handlers.
379 */
380void stream_int_unregister_handler(struct stream_interface *si)
381{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100382 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200383 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100384 task_delete(si->target.ptr.t);
385 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200386 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200387 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200388 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100389 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200390}
391
Willy Tarreaudded32d2008-11-30 19:48:07 +0100392/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100393 * Local variables:
394 * c-indent-level: 8
395 * c-basic-offset: 8
396 * End:
397 */