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