blob: 69db42f240239f03cf0e676ae8b2a6221fa6f281 [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
Willy Tarreauf873d752012-05-11 17:47:17 +02004 * Copyright 2000-2012 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 Tarreauc63190d2012-05-11 14:23:52 +020031#include <proto/sock_raw.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020032#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010033#include <proto/task.h>
34
Willy Tarreauf873d752012-05-11 17:47:17 +020035/* socket functions used when running a stream interface as a task */
36static void stream_int_update(struct stream_interface *si);
37static void stream_int_update_embedded(struct stream_interface *si);
38static void stream_int_shutr(struct stream_interface *si);
39static void stream_int_shutw(struct stream_interface *si);
40static void stream_int_chk_rcv(struct stream_interface *si);
41static void stream_int_chk_snd(struct stream_interface *si);
42
Willy Tarreau5c979a92012-05-07 17:15:39 +020043/* socket operations for embedded tasks */
44struct sock_ops stream_int_embedded = {
Willy Tarreau64798bd2012-05-11 18:38:44 +020045 .init = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020046 .update = stream_int_update_embedded,
47 .shutr = stream_int_shutr,
48 .shutw = stream_int_shutw,
49 .chk_rcv = stream_int_chk_rcv,
50 .chk_snd = stream_int_chk_snd,
51 .read = NULL,
52 .write = NULL,
53};
54
55/* socket operations for external tasks */
56struct sock_ops stream_int_task = {
Willy Tarreau64798bd2012-05-11 18:38:44 +020057 .init = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020058 .update = stream_int_update,
59 .shutr = stream_int_shutr,
60 .shutw = stream_int_shutw,
61 .chk_rcv = stream_int_chk_rcv,
62 .chk_snd = stream_int_chk_snd,
63 .read = NULL,
64 .write = NULL,
65};
66
Willy Tarreaucff64112008-11-03 06:26:53 +010067/*
68 * This function only has to be called once after a wakeup event in case of
69 * suspected timeout. It controls the stream interface timeouts and sets
70 * si->flags accordingly. It does NOT close anything, as this timeout may
71 * be used for any purpose. It returns 1 if the timeout fired, otherwise
72 * zero.
73 */
74int stream_int_check_timeouts(struct stream_interface *si)
75{
76 if (tick_is_expired(si->exp, now_ms)) {
77 si->flags |= SI_FL_EXP;
78 return 1;
79 }
80 return 0;
81}
82
Willy Tarreaufe3718a2008-11-30 18:14:12 +010083/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010084void stream_int_report_error(struct stream_interface *si)
85{
86 if (!si->err_type)
87 si->err_type = SI_ET_DATA_ERR;
88
Willy Tarreaucff64112008-11-03 06:26:53 +010089 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010090 si->ib->flags |= BF_READ_ERROR;
91}
92
93/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010094 * Returns a message to the client ; the connection is shut down for read,
95 * and the request is cleared so that no server connection can be initiated.
96 * The buffer is marked for read shutdown on the other side to protect the
97 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +010098 * "chunk". If it is null, then an empty message is used. The reply buffer does
99 * not need to be empty before this, and its contents will not be overwritten.
100 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100101 */
102void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
103{
Willy Tarreau148d0992010-01-10 10:21:21 +0100104 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100105 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100106 buffer_auto_close(si->ib);
107 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100108
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200109 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100110 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200111 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100112
113 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100114 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200115 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100116 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100117}
118
Willy Tarreaufb90d942009-09-05 20:57:35 +0200119/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200120static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200121{
122 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
123 __FUNCTION__,
124 si, si->state, si->ib->flags, si->ob->flags);
125
126 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
127 task_wakeup(si->owner, TASK_WOKEN_IO);
128}
129
130/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200131static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200132{
Willy Tarreau3488e252010-08-09 16:24:56 +0200133 int old_flags = si->flags;
134
Willy Tarreaufb90d942009-09-05 20:57:35 +0200135 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
136 __FUNCTION__,
137 si, si->state, si->ib->flags, si->ob->flags);
138
139 if (si->state != SI_ST_EST)
140 return;
141
142 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 +0200143 si->sock.shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200144
145 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
146 si->flags |= SI_FL_WAIT_DATA;
147
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200148 /* we're almost sure that we need some space if the buffer is not
149 * empty, even if it's not full, because the applets can't fill it.
150 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200151 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200152 si->flags |= SI_FL_WAIT_ROOM;
153
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200154 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200155 if (tick_isset(si->ob->wex))
156 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
157 }
158
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200159 if (si->ib->flags & BF_READ_ACTIVITY ||
160 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
161 if (tick_isset(si->ib->rex))
162 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
163 }
164
Willy Tarreau3488e252010-08-09 16:24:56 +0200165 /* save flags to detect changes */
166 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200167 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 +0200168 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau060781f2012-05-07 16:50:03 +0200169 si->ob->prod->sock.chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200170
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200171 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200172 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau060781f2012-05-07 16:50:03 +0200173 si->ib->cons->sock.chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200174 /* check if the consumer has freed some space */
175 if (!(si->ib->flags & BF_FULL))
176 si->flags &= ~SI_FL_WAIT_ROOM;
177 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200178
179 /* Note that we're trying to wake up in two conditions here :
180 * - special event, which needs the holder task attention
181 * - status indicating that the applet can go on working. This
182 * is rather hard because we might be blocking on output and
183 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200184 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200185 */
186 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200187 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
188
189 /* changes on the production side */
190 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
191 si->state != SI_ST_EST ||
192 (si->flags & SI_FL_ERR) ||
193 ((si->ib->flags & BF_READ_PARTIAL) &&
194 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
195
196 /* changes on the consumption side */
197 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
198 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
199 ((si->ob->flags & BF_SHUTW) ||
200 si->ob->prod->state != SI_ST_EST ||
201 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200202 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
203 task_wakeup(si->owner, TASK_WOKEN_IO);
204 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200205 if (si->ib->flags & BF_READ_ACTIVITY)
206 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200207}
208
209/* default shutr function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200210static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200211{
212 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
213 __FUNCTION__,
214 si, si->state, si->ib->flags, si->ob->flags);
215
216 si->ib->flags &= ~BF_SHUTR_NOW;
217 if (si->ib->flags & BF_SHUTR)
218 return;
219 si->ib->flags |= BF_SHUTR;
220 si->ib->rex = TICK_ETERNITY;
221 si->flags &= ~SI_FL_WAIT_ROOM;
222
223 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
224 return;
225
226 if (si->ob->flags & BF_SHUTW) {
227 si->state = SI_ST_DIS;
228 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200229
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200230 if (si->release)
231 si->release(si);
232 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200233
Willy Tarreaufb90d942009-09-05 20:57:35 +0200234 /* note that if the task exist, it must unregister itself once it runs */
235 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
236 task_wakeup(si->owner, TASK_WOKEN_IO);
237}
238
239/* default shutw function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200240static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200241{
242 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
243 __FUNCTION__,
244 si, si->state, si->ib->flags, si->ob->flags);
245
246 si->ob->flags &= ~BF_SHUTW_NOW;
247 if (si->ob->flags & BF_SHUTW)
248 return;
249 si->ob->flags |= BF_SHUTW;
250 si->ob->wex = TICK_ETERNITY;
251 si->flags &= ~SI_FL_WAIT_DATA;
252
253 switch (si->state) {
254 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200255 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200256 break;
257
258 /* fall through */
259 case SI_ST_CON:
260 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100261 case SI_ST_QUE:
262 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200263 si->state = SI_ST_DIS;
264 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200265
266 if (si->release)
267 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200268 default:
269 si->flags &= ~SI_FL_WAIT_ROOM;
270 si->ib->flags |= BF_SHUTR;
271 si->ib->rex = TICK_ETERNITY;
272 si->exp = TICK_ETERNITY;
273 }
274
275 /* note that if the task exist, it must unregister itself once it runs */
276 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
277 task_wakeup(si->owner, TASK_WOKEN_IO);
278}
279
280/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200281static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200282{
283 struct buffer *ib = si->ib;
284
285 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
286 __FUNCTION__,
287 si, si->state, si->ib->flags, si->ob->flags);
288
289 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
290 return;
291
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200292 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200293 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200294 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200295 si->flags |= SI_FL_WAIT_ROOM;
296 }
297 else {
298 /* (re)start reading */
299 si->flags &= ~SI_FL_WAIT_ROOM;
300 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
301 task_wakeup(si->owner, TASK_WOKEN_IO);
302 }
303}
304
305/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200306static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200307{
308 struct buffer *ob = si->ob;
309
310 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
311 __FUNCTION__,
312 si, si->state, si->ib->flags, si->ob->flags);
313
314 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
315 return;
316
317 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
318 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
319 return;
320
321 /* Otherwise there are remaining data to be sent in the buffer,
322 * so we tell the handler.
323 */
324 si->flags &= ~SI_FL_WAIT_DATA;
325 if (!tick_isset(ob->wex))
326 ob->wex = tick_add_ifset(now_ms, ob->wto);
327
328 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
329 task_wakeup(si->owner, TASK_WOKEN_IO);
330}
331
Willy Tarreaub24281b2011-02-13 13:16:36 +0100332/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200333 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100334 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200335 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100336 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreau295a8372011-03-10 11:25:07 +0100337 * It also pre-initializes applet.state to zero.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200338 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100339struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200340{
Simon Horman7abd00d2011-08-13 08:03:51 +0900341 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200342
Willy Tarreau5c979a92012-05-07 17:15:39 +0200343 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200344 si->proto = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100345 set_target_applet(&si->target, app);
Willy Tarreau295a8372011-03-10 11:25:07 +0100346 si->applet.state = 0;
Aman Gupta9a13e842012-04-02 18:57:53 -0700347 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200348 si->flags |= SI_FL_WAIT_DATA;
349 return si->owner;
350}
351
352/* Register a function to handle a stream_interface as a standalone task. The
353 * new task itself is returned and is assigned as si->owner. The stream_interface
354 * pointer will be pointed to by the task's context. The handler can be detached
355 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100356 * FIXME: the code should be updated to ensure that we don't change si->owner
357 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200358 */
359struct task *stream_int_register_handler_task(struct stream_interface *si,
360 struct task *(*fct)(struct task *))
361{
362 struct task *t;
363
364 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
365
Willy Tarreau5c979a92012-05-07 17:15:39 +0200366 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200367 si->proto = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100368 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200369 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200370 si->flags |= SI_FL_WAIT_DATA;
371
372 t = task_new();
373 si->owner = t;
374 if (!t)
375 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100376
Willy Tarreau9e000c62011-03-10 14:03:36 +0100377 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100378
Willy Tarreaufb90d942009-09-05 20:57:35 +0200379 t->process = fct;
380 t->context = si;
381 task_wakeup(si->owner, TASK_WOKEN_INIT);
382
383 return t;
384}
385
386/* Unregister a stream interface handler. This must be called by the handler task
387 * itself when it detects that it is in the SI_ST_DIS state. This function can
388 * both detach standalone handlers and embedded handlers.
389 */
390void stream_int_unregister_handler(struct stream_interface *si)
391{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100392 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200393 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100394 task_delete(si->target.ptr.t);
395 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200396 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200397 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200398 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100399 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200400}
401
Willy Tarreaudded32d2008-11-30 19:48:07 +0100402/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100403 * Local variables:
404 * c-indent-level: 8
405 * c-basic-offset: 8
406 * End:
407 */