blob: c70ee35e7b3427574b07cb0785462d5557e3b739 [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 = {
45 .update = stream_int_update_embedded,
46 .shutr = stream_int_shutr,
47 .shutw = stream_int_shutw,
48 .chk_rcv = stream_int_chk_rcv,
49 .chk_snd = stream_int_chk_snd,
50 .read = NULL,
51 .write = NULL,
52};
53
54/* socket operations for external tasks */
55struct sock_ops stream_int_task = {
56 .update = stream_int_update,
57 .shutr = stream_int_shutr,
58 .shutw = stream_int_shutw,
59 .chk_rcv = stream_int_chk_rcv,
60 .chk_snd = stream_int_chk_snd,
61 .read = NULL,
62 .write = NULL,
63};
64
Willy Tarreaucff64112008-11-03 06:26:53 +010065/*
66 * This function only has to be called once after a wakeup event in case of
67 * suspected timeout. It controls the stream interface timeouts and sets
68 * si->flags accordingly. It does NOT close anything, as this timeout may
69 * be used for any purpose. It returns 1 if the timeout fired, otherwise
70 * zero.
71 */
72int stream_int_check_timeouts(struct stream_interface *si)
73{
74 if (tick_is_expired(si->exp, now_ms)) {
75 si->flags |= SI_FL_EXP;
76 return 1;
77 }
78 return 0;
79}
80
Willy Tarreaufe3718a2008-11-30 18:14:12 +010081/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010082void stream_int_report_error(struct stream_interface *si)
83{
84 if (!si->err_type)
85 si->err_type = SI_ET_DATA_ERR;
86
Willy Tarreaucff64112008-11-03 06:26:53 +010087 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010088 si->ib->flags |= BF_READ_ERROR;
89}
90
91/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010092 * Returns a message to the client ; the connection is shut down for read,
93 * and the request is cleared so that no server connection can be initiated.
94 * The buffer is marked for read shutdown on the other side to protect the
95 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +010096 * "chunk". If it is null, then an empty message is used. The reply buffer does
97 * not need to be empty before this, and its contents will not be overwritten.
98 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +010099 */
100void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
101{
Willy Tarreau148d0992010-01-10 10:21:21 +0100102 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100103 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100104 buffer_auto_close(si->ib);
105 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100106
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200107 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100108 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200109 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100110
111 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100112 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200113 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100114 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100115}
116
Willy Tarreaufb90d942009-09-05 20:57:35 +0200117/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200118static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200119{
120 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
121 __FUNCTION__,
122 si, si->state, si->ib->flags, si->ob->flags);
123
124 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
125 task_wakeup(si->owner, TASK_WOKEN_IO);
126}
127
128/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200129static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200130{
Willy Tarreau3488e252010-08-09 16:24:56 +0200131 int old_flags = si->flags;
132
Willy Tarreaufb90d942009-09-05 20:57:35 +0200133 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
134 __FUNCTION__,
135 si, si->state, si->ib->flags, si->ob->flags);
136
137 if (si->state != SI_ST_EST)
138 return;
139
140 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200141 si_shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200142
143 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
144 si->flags |= SI_FL_WAIT_DATA;
145
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200146 /* we're almost sure that we need some space if the buffer is not
147 * empty, even if it's not full, because the applets can't fill it.
148 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200149 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200150 si->flags |= SI_FL_WAIT_ROOM;
151
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200152 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200153 if (tick_isset(si->ob->wex))
154 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
155 }
156
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200157 if (si->ib->flags & BF_READ_ACTIVITY ||
158 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
159 if (tick_isset(si->ib->rex))
160 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
161 }
162
Willy Tarreau3488e252010-08-09 16:24:56 +0200163 /* save flags to detect changes */
164 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200165 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 +0200166 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200167 si_chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200168
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200169 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200170 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200171 si_chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200172 /* check if the consumer has freed some space */
173 if (!(si->ib->flags & BF_FULL))
174 si->flags &= ~SI_FL_WAIT_ROOM;
175 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200176
177 /* Note that we're trying to wake up in two conditions here :
178 * - special event, which needs the holder task attention
179 * - status indicating that the applet can go on working. This
180 * is rather hard because we might be blocking on output and
181 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200182 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200183 */
184 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200185 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
186
187 /* changes on the production side */
188 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
189 si->state != SI_ST_EST ||
190 (si->flags & SI_FL_ERR) ||
191 ((si->ib->flags & BF_READ_PARTIAL) &&
192 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
193
194 /* changes on the consumption side */
195 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
196 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
197 ((si->ob->flags & BF_SHUTW) ||
198 si->ob->prod->state != SI_ST_EST ||
199 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200200 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
201 task_wakeup(si->owner, TASK_WOKEN_IO);
202 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200203 if (si->ib->flags & BF_READ_ACTIVITY)
204 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200205}
206
207/* default shutr function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200208static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200209{
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->ib->flags &= ~BF_SHUTR_NOW;
215 if (si->ib->flags & BF_SHUTR)
216 return;
217 si->ib->flags |= BF_SHUTR;
218 si->ib->rex = TICK_ETERNITY;
219 si->flags &= ~SI_FL_WAIT_ROOM;
220
221 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
222 return;
223
224 if (si->ob->flags & BF_SHUTW) {
225 si->state = SI_ST_DIS;
226 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200227
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200228 if (si->release)
229 si->release(si);
230 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200231
Willy Tarreaufb90d942009-09-05 20:57:35 +0200232 /* note that if the task exist, it must unregister itself once it runs */
233 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
234 task_wakeup(si->owner, TASK_WOKEN_IO);
235}
236
237/* default shutw function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200238static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200239{
240 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
241 __FUNCTION__,
242 si, si->state, si->ib->flags, si->ob->flags);
243
244 si->ob->flags &= ~BF_SHUTW_NOW;
245 if (si->ob->flags & BF_SHUTW)
246 return;
247 si->ob->flags |= BF_SHUTW;
248 si->ob->wex = TICK_ETERNITY;
249 si->flags &= ~SI_FL_WAIT_DATA;
250
251 switch (si->state) {
252 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200253 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200254 break;
255
256 /* fall through */
257 case SI_ST_CON:
258 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100259 case SI_ST_QUE:
260 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261 si->state = SI_ST_DIS;
262 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200263
264 if (si->release)
265 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200266 default:
267 si->flags &= ~SI_FL_WAIT_ROOM;
268 si->ib->flags |= BF_SHUTR;
269 si->ib->rex = TICK_ETERNITY;
270 si->exp = TICK_ETERNITY;
271 }
272
273 /* note that if the task exist, it must unregister itself once it runs */
274 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
275 task_wakeup(si->owner, TASK_WOKEN_IO);
276}
277
278/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200279static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200280{
281 struct buffer *ib = si->ib;
282
283 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
284 __FUNCTION__,
285 si, si->state, si->ib->flags, si->ob->flags);
286
287 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
288 return;
289
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200290 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200291 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200292 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200293 si->flags |= SI_FL_WAIT_ROOM;
294 }
295 else {
296 /* (re)start reading */
297 si->flags &= ~SI_FL_WAIT_ROOM;
298 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
299 task_wakeup(si->owner, TASK_WOKEN_IO);
300 }
301}
302
303/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200304static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200305{
306 struct buffer *ob = si->ob;
307
308 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
309 __FUNCTION__,
310 si, si->state, si->ib->flags, si->ob->flags);
311
312 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
313 return;
314
315 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
316 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
317 return;
318
319 /* Otherwise there are remaining data to be sent in the buffer,
320 * so we tell the handler.
321 */
322 si->flags &= ~SI_FL_WAIT_DATA;
323 if (!tick_isset(ob->wex))
324 ob->wex = tick_add_ifset(now_ms, ob->wto);
325
326 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
327 task_wakeup(si->owner, TASK_WOKEN_IO);
328}
329
Willy Tarreaub24281b2011-02-13 13:16:36 +0100330/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200331 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100332 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200333 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100334 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreau295a8372011-03-10 11:25:07 +0100335 * It also pre-initializes applet.state to zero.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200336 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100337struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200338{
Simon Horman7abd00d2011-08-13 08:03:51 +0900339 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200340
Willy Tarreau5c979a92012-05-07 17:15:39 +0200341 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200342 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100343 set_target_applet(&si->target, app);
Willy Tarreau295a8372011-03-10 11:25:07 +0100344 si->applet.state = 0;
Aman Gupta9a13e842012-04-02 18:57:53 -0700345 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200346 si->flags |= SI_FL_WAIT_DATA;
347 return si->owner;
348}
349
350/* Register a function to handle a stream_interface as a standalone task. The
351 * new task itself is returned and is assigned as si->owner. The stream_interface
352 * pointer will be pointed to by the task's context. The handler can be detached
353 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100354 * FIXME: the code should be updated to ensure that we don't change si->owner
355 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200356 */
357struct task *stream_int_register_handler_task(struct stream_interface *si,
358 struct task *(*fct)(struct task *))
359{
360 struct task *t;
361
362 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
363
Willy Tarreau5c979a92012-05-07 17:15:39 +0200364 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200365 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100366 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200367 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200368 si->flags |= SI_FL_WAIT_DATA;
369
370 t = task_new();
371 si->owner = t;
372 if (!t)
373 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100374
Willy Tarreau9e000c62011-03-10 14:03:36 +0100375 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100376
Willy Tarreaufb90d942009-09-05 20:57:35 +0200377 t->process = fct;
378 t->context = si;
379 task_wakeup(si->owner, TASK_WOKEN_INIT);
380
381 return t;
382}
383
384/* Unregister a stream interface handler. This must be called by the handler task
385 * itself when it detects that it is in the SI_ST_DIS state. This function can
386 * both detach standalone handlers and embedded handlers.
387 */
388void stream_int_unregister_handler(struct stream_interface *si)
389{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100390 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200391 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100392 task_delete(si->target.ptr.t);
393 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200394 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200395 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200396 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100397 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200398}
399
Willy Tarreaudded32d2008-11-30 19:48:07 +0100400/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100401 * Local variables:
402 * c-indent-level: 8
403 * c-basic-offset: 8
404 * End:
405 */