blob: 96b26def587bcf37fe574a09b1d6b5015aa08c0f [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 Tarreau6f0aa472009-03-08 20:33:29 +010063 * Erase any content from input and output buffers, and return a message into
64 * the output buffer. The message is provided as a "chunk". If it is null,
65 * then an empty message is used.
Willy Tarreau81acfab2008-11-30 19:22:53 +010066 */
67void stream_int_return(struct stream_interface *si, const struct chunk *msg)
68{
Willy Tarreau6f0aa472009-03-08 20:33:29 +010069 buffer_erase(si->ib);
Willy Tarreaucb359e32009-09-15 21:23:54 +020070 buffer_cut_tail(si->ob);
Willy Tarreau81acfab2008-11-30 19:22:53 +010071 if (msg && msg->len)
72 buffer_write(si->ob, msg->str, msg->len);
73}
74
75/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010076 * Returns a message to the client ; the connection is shut down for read,
77 * and the request is cleared so that no server connection can be initiated.
78 * The buffer is marked for read shutdown on the other side to protect the
79 * message, and the buffer write is enabled. The message is contained in a
80 * "chunk". If it is null, then an empty message is used. The reply buffer
81 * doesn't need to be empty before this. The goal of this function is to
82 * return error messages to a client.
83 */
84void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
85{
86 buffer_abort(si->ib);
Willy Tarreau6f0aa472009-03-08 20:33:29 +010087 buffer_erase(si->ob);
Willy Tarreaudded32d2008-11-30 19:48:07 +010088 buffer_shutr_now(si->ob);
89 if (msg && msg->len)
90 buffer_write(si->ob, msg->str, msg->len);
91
92 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau520d95e2009-09-19 21:04:57 +020093 buffer_auto_close(si->ob);
Willy Tarreaudded32d2008-11-30 19:48:07 +010094}
95
Willy Tarreaufb90d942009-09-05 20:57:35 +020096/* default update function for scheduled tasks, not used for embedded tasks */
97void stream_int_update(struct stream_interface *si)
98{
99 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
100 __FUNCTION__,
101 si, si->state, si->ib->flags, si->ob->flags);
102
103 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
104 task_wakeup(si->owner, TASK_WOKEN_IO);
105}
106
107/* default update function for embedded tasks, to be used at the end of the i/o handler */
108void stream_int_update_embedded(struct stream_interface *si)
109{
110 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
111 __FUNCTION__,
112 si, si->state, si->ib->flags, si->ob->flags);
113
114 if (si->state != SI_ST_EST)
115 return;
116
117 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
118 si->shutw(si);
119
120 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
121 si->flags |= SI_FL_WAIT_DATA;
122
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200123 /* we're almost sure that we need some space if the buffer is not
124 * empty, even if it's not full, because the applets can't fill it.
125 */
126 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200127 si->flags |= SI_FL_WAIT_ROOM;
128
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200129 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200130 if (tick_isset(si->ob->wex))
131 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
132 }
133
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200134 if (si->ib->flags & BF_READ_ACTIVITY ||
135 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
136 if (tick_isset(si->ib->rex))
137 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
138 }
139
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200140 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
141 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200142 si->ob->prod->chk_rcv(si->ob->prod);
143
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200144 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
145 (si->ib->cons->flags & SI_FL_WAIT_DATA))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200146 si->ib->cons->chk_snd(si->ib->cons);
147
148 /* Note that we're trying to wake up in two conditions here :
149 * - special event, which needs the holder task attention
150 * - status indicating that the applet can go on working. This
151 * is rather hard because we might be blocking on output and
152 * don't want to wake up on input and vice-versa. The idea is
153 * the to only rely the changes the chk_* might have performed.
154 */
155 if (/* check stream interface changes */
156 (si->flags & SI_FL_ERR) || si->state != SI_ST_EST || si->ib->cons->state != SI_ST_EST ||
157 /* check response buffer changes */
158 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR|BF_READ_DONTWAIT)) ||
159 ((si->ib->flags & BF_READ_ACTIVITY) && !si->ib->to_forward) ||
160 (!(si->ib->flags & BF_FULL) && (si->ib->flags & BF_WRITE_ACTIVITY) && si->ib->to_forward) ||
161 /* check request buffer changes */
162 (si->ob->flags & (BF_WRITE_ERROR)) ||
163 ((si->ob->flags & BF_WRITE_ACTIVITY) && (si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward) ||
164 (si->ob->flags & BF_READ_ACTIVITY)) {
165 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
166 task_wakeup(si->owner, TASK_WOKEN_IO);
167 }
168}
169
170/* default shutr function for scheduled tasks */
171void stream_int_shutr(struct stream_interface *si)
172{
173 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
174 __FUNCTION__,
175 si, si->state, si->ib->flags, si->ob->flags);
176
177 si->ib->flags &= ~BF_SHUTR_NOW;
178 if (si->ib->flags & BF_SHUTR)
179 return;
180 si->ib->flags |= BF_SHUTR;
181 si->ib->rex = TICK_ETERNITY;
182 si->flags &= ~SI_FL_WAIT_ROOM;
183
184 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
185 return;
186
187 if (si->ob->flags & BF_SHUTW) {
188 si->state = SI_ST_DIS;
189 si->exp = TICK_ETERNITY;
190 }
191
192 /* note that if the task exist, it must unregister itself once it runs */
193 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
194 task_wakeup(si->owner, TASK_WOKEN_IO);
195}
196
197/* default shutw function for scheduled tasks */
198void stream_int_shutw(struct stream_interface *si)
199{
200 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
201 __FUNCTION__,
202 si, si->state, si->ib->flags, si->ob->flags);
203
204 si->ob->flags &= ~BF_SHUTW_NOW;
205 if (si->ob->flags & BF_SHUTW)
206 return;
207 si->ob->flags |= BF_SHUTW;
208 si->ob->wex = TICK_ETERNITY;
209 si->flags &= ~SI_FL_WAIT_DATA;
210
211 switch (si->state) {
212 case SI_ST_EST:
213 if (!(si->ib->flags & BF_SHUTR))
214 break;
215
216 /* fall through */
217 case SI_ST_CON:
218 case SI_ST_CER:
219 si->state = SI_ST_DIS;
220 /* fall through */
221 default:
222 si->flags &= ~SI_FL_WAIT_ROOM;
223 si->ib->flags |= BF_SHUTR;
224 si->ib->rex = TICK_ETERNITY;
225 si->exp = TICK_ETERNITY;
226 }
227
228 /* note that if the task exist, it must unregister itself once it runs */
229 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
230 task_wakeup(si->owner, TASK_WOKEN_IO);
231}
232
233/* default chk_rcv function for scheduled tasks */
234void stream_int_chk_rcv(struct stream_interface *si)
235{
236 struct buffer *ib = si->ib;
237
238 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
239 __FUNCTION__,
240 si, si->state, si->ib->flags, si->ob->flags);
241
242 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
243 return;
244
245 if (ib->flags & (BF_FULL|BF_HIJACK)) {
246 /* stop reading */
247 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
248 si->flags |= SI_FL_WAIT_ROOM;
249 }
250 else {
251 /* (re)start reading */
252 si->flags &= ~SI_FL_WAIT_ROOM;
253 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
254 task_wakeup(si->owner, TASK_WOKEN_IO);
255 }
256}
257
258/* default chk_snd function for scheduled tasks */
259void stream_int_chk_snd(struct stream_interface *si)
260{
261 struct buffer *ob = si->ob;
262
263 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
264 __FUNCTION__,
265 si, si->state, si->ib->flags, si->ob->flags);
266
267 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
268 return;
269
270 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
271 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
272 return;
273
274 /* Otherwise there are remaining data to be sent in the buffer,
275 * so we tell the handler.
276 */
277 si->flags &= ~SI_FL_WAIT_DATA;
278 if (!tick_isset(ob->wex))
279 ob->wex = tick_add_ifset(now_ms, ob->wto);
280
281 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
282 task_wakeup(si->owner, TASK_WOKEN_IO);
283}
284
285/* Register a function to handle a stream_interface as part of the stream
286 * interface's owner task, which is returned. The SI will wake it up everytime
287 * it is solicited. The task's processing function must call the specified
288 * function before returning. It must be deleted by the task handler using
289 * stream_int_unregister_handler(), possibly from withing the function itself.
290 */
291struct task *stream_int_register_handler(struct stream_interface *si,
292 void (*fct)(struct stream_interface *))
293{
294 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
295
296 si->update = stream_int_update_embedded;
297 si->shutr = stream_int_shutr;
298 si->shutw = stream_int_shutw;
299 si->chk_rcv = stream_int_chk_rcv;
300 si->chk_snd = stream_int_chk_snd;
301 si->connect = NULL;
302 si->iohandler = fct;
303 si->flags |= SI_FL_WAIT_DATA;
304 return si->owner;
305}
306
307/* Register a function to handle a stream_interface as a standalone task. The
308 * new task itself is returned and is assigned as si->owner. The stream_interface
309 * pointer will be pointed to by the task's context. The handler can be detached
310 * by using stream_int_unregister_handler().
311 */
312struct task *stream_int_register_handler_task(struct stream_interface *si,
313 struct task *(*fct)(struct task *))
314{
315 struct task *t;
316
317 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
318
319 si->update = stream_int_update;
320 si->shutr = stream_int_shutr;
321 si->shutw = stream_int_shutw;
322 si->chk_rcv = stream_int_chk_rcv;
323 si->chk_snd = stream_int_chk_snd;
324 si->connect = NULL;
325 si->iohandler = NULL; /* not used when running as an external task */
326 si->flags |= SI_FL_WAIT_DATA;
327
328 t = task_new();
329 si->owner = t;
330 if (!t)
331 return t;
332 t->process = fct;
333 t->context = si;
334 task_wakeup(si->owner, TASK_WOKEN_INIT);
335
336 return t;
337}
338
339/* Unregister a stream interface handler. This must be called by the handler task
340 * itself when it detects that it is in the SI_ST_DIS state. This function can
341 * both detach standalone handlers and embedded handlers.
342 */
343void stream_int_unregister_handler(struct stream_interface *si)
344{
345 if (!si->iohandler && si->owner) {
346 /* external handler : kill the task */
347 task_delete(si->owner);
348 task_free(si->owner);
349 }
350 si->iohandler = NULL;
351 si->owner = NULL;
352}
353
Willy Tarreaudded32d2008-11-30 19:48:07 +0100354/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100355 * Local variables:
356 * c-indent-level: 8
357 * c-basic-offset: 8
358 * End:
359 */