blob: 9d6f9616c47f56daa8a08e9d0cf6ce8f53ee7c82 [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 Tarreau2c6be842012-07-06 17:12:34 +020031#include <proto/frontend.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020032#include <proto/sock_raw.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020033#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010034#include <proto/task.h>
35
Willy Tarreaufd31e532012-07-23 18:24:25 +020036#include <types/pipe.h>
37
Willy Tarreauf873d752012-05-11 17:47:17 +020038/* socket functions used when running a stream interface as a task */
39static void stream_int_update(struct stream_interface *si);
40static void stream_int_update_embedded(struct stream_interface *si);
41static void stream_int_shutr(struct stream_interface *si);
42static void stream_int_shutw(struct stream_interface *si);
43static void stream_int_chk_rcv(struct stream_interface *si);
44static void stream_int_chk_snd(struct stream_interface *si);
45
Willy Tarreau5c979a92012-05-07 17:15:39 +020046/* socket operations for embedded tasks */
47struct sock_ops stream_int_embedded = {
48 .update = stream_int_update_embedded,
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,
Willy Tarreau24208272012-05-21 17:28:50 +020055 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020056};
57
58/* socket operations for external tasks */
59struct sock_ops stream_int_task = {
60 .update = stream_int_update,
61 .shutr = stream_int_shutr,
62 .shutw = stream_int_shutw,
63 .chk_rcv = stream_int_chk_rcv,
64 .chk_snd = stream_int_chk_snd,
65 .read = NULL,
66 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020067 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020068};
69
Willy Tarreaucff64112008-11-03 06:26:53 +010070/*
71 * This function only has to be called once after a wakeup event in case of
72 * suspected timeout. It controls the stream interface timeouts and sets
73 * si->flags accordingly. It does NOT close anything, as this timeout may
74 * be used for any purpose. It returns 1 if the timeout fired, otherwise
75 * zero.
76 */
77int stream_int_check_timeouts(struct stream_interface *si)
78{
79 if (tick_is_expired(si->exp, now_ms)) {
80 si->flags |= SI_FL_EXP;
81 return 1;
82 }
83 return 0;
84}
85
Willy Tarreaufe3718a2008-11-30 18:14:12 +010086/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010087void stream_int_report_error(struct stream_interface *si)
88{
89 if (!si->err_type)
90 si->err_type = SI_ET_DATA_ERR;
91
Willy Tarreaucff64112008-11-03 06:26:53 +010092 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010093 si->ib->flags |= BF_READ_ERROR;
94}
95
96/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010097 * Returns a message to the client ; the connection is shut down for read,
98 * and the request is cleared so that no server connection can be initiated.
99 * The buffer is marked for read shutdown on the other side to protect the
100 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +0100101 * "chunk". If it is null, then an empty message is used. The reply buffer does
102 * not need to be empty before this, and its contents will not be overwritten.
103 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100104 */
105void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
106{
Willy Tarreau148d0992010-01-10 10:21:21 +0100107 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100108 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100109 buffer_auto_close(si->ib);
110 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100111
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200112 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100113 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200114 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100115
116 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100117 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200118 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100119 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100120}
121
Willy Tarreaufb90d942009-09-05 20:57:35 +0200122/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200123static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200124{
125 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->flags & SI_FL_DONT_WAKE) && si->owner)
130 task_wakeup(si->owner, TASK_WOKEN_IO);
131}
132
133/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200134static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200135{
Willy Tarreau3488e252010-08-09 16:24:56 +0200136 int old_flags = si->flags;
137
Willy Tarreaufb90d942009-09-05 20:57:35 +0200138 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
139 __FUNCTION__,
140 si, si->state, si->ib->flags, si->ob->flags);
141
142 if (si->state != SI_ST_EST)
143 return;
144
145 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 +0200146 si_shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200147
148 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
149 si->flags |= SI_FL_WAIT_DATA;
150
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200151 /* we're almost sure that we need some space if the buffer is not
152 * empty, even if it's not full, because the applets can't fill it.
153 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200154 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200155 si->flags |= SI_FL_WAIT_ROOM;
156
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200157 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200158 if (tick_isset(si->ob->wex))
159 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
160 }
161
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200162 if (si->ib->flags & BF_READ_ACTIVITY ||
163 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
164 if (tick_isset(si->ib->rex))
165 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
166 }
167
Willy Tarreau3488e252010-08-09 16:24:56 +0200168 /* save flags to detect changes */
169 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200170 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 +0200171 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200172 si_chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200173
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200174 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200175 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200176 si_chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200177 /* check if the consumer has freed some space */
178 if (!(si->ib->flags & BF_FULL))
179 si->flags &= ~SI_FL_WAIT_ROOM;
180 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200181
182 /* Note that we're trying to wake up in two conditions here :
183 * - special event, which needs the holder task attention
184 * - status indicating that the applet can go on working. This
185 * is rather hard because we might be blocking on output and
186 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200187 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200188 */
189 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200190 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
191
192 /* changes on the production side */
193 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
194 si->state != SI_ST_EST ||
195 (si->flags & SI_FL_ERR) ||
196 ((si->ib->flags & BF_READ_PARTIAL) &&
197 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
198
199 /* changes on the consumption side */
200 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
201 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
202 ((si->ob->flags & BF_SHUTW) ||
203 si->ob->prod->state != SI_ST_EST ||
204 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200205 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
206 task_wakeup(si->owner, TASK_WOKEN_IO);
207 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200208 if (si->ib->flags & BF_READ_ACTIVITY)
209 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200210}
211
212/* default shutr function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200213static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200214{
215 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
216 __FUNCTION__,
217 si, si->state, si->ib->flags, si->ob->flags);
218
219 si->ib->flags &= ~BF_SHUTR_NOW;
220 if (si->ib->flags & BF_SHUTR)
221 return;
222 si->ib->flags |= BF_SHUTR;
223 si->ib->rex = TICK_ETERNITY;
224 si->flags &= ~SI_FL_WAIT_ROOM;
225
226 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
227 return;
228
229 if (si->ob->flags & BF_SHUTW) {
230 si->state = SI_ST_DIS;
231 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200232
Willy Tarreau4da69a92012-05-21 18:05:40 +0200233 si_data_close(si);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200234 if (si->release)
235 si->release(si);
236 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200237
Willy Tarreaufb90d942009-09-05 20:57:35 +0200238 /* note that if the task exist, it must unregister itself once it runs */
239 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
240 task_wakeup(si->owner, TASK_WOKEN_IO);
241}
242
243/* default shutw function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200244static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200245{
246 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
247 __FUNCTION__,
248 si, si->state, si->ib->flags, si->ob->flags);
249
250 si->ob->flags &= ~BF_SHUTW_NOW;
251 if (si->ob->flags & BF_SHUTW)
252 return;
253 si->ob->flags |= BF_SHUTW;
254 si->ob->wex = TICK_ETERNITY;
255 si->flags &= ~SI_FL_WAIT_DATA;
256
257 switch (si->state) {
258 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200259 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200260 break;
261
262 /* fall through */
263 case SI_ST_CON:
264 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100265 case SI_ST_QUE:
266 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200267 si->state = SI_ST_DIS;
268 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200269
Willy Tarreau4da69a92012-05-21 18:05:40 +0200270 si_data_close(si);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200271 if (si->release)
272 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200273 default:
274 si->flags &= ~SI_FL_WAIT_ROOM;
275 si->ib->flags |= BF_SHUTR;
276 si->ib->rex = TICK_ETERNITY;
277 si->exp = TICK_ETERNITY;
278 }
279
280 /* note that if the task exist, it must unregister itself once it runs */
281 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
282 task_wakeup(si->owner, TASK_WOKEN_IO);
283}
284
285/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200286static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200287{
288 struct buffer *ib = si->ib;
289
290 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
291 __FUNCTION__,
292 si, si->state, si->ib->flags, si->ob->flags);
293
294 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
295 return;
296
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200297 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200298 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200299 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200300 si->flags |= SI_FL_WAIT_ROOM;
301 }
302 else {
303 /* (re)start reading */
304 si->flags &= ~SI_FL_WAIT_ROOM;
305 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
306 task_wakeup(si->owner, TASK_WOKEN_IO);
307 }
308}
309
310/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200311static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200312{
313 struct buffer *ob = si->ob;
314
315 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
316 __FUNCTION__,
317 si, si->state, si->ib->flags, si->ob->flags);
318
319 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
320 return;
321
322 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
323 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
324 return;
325
326 /* Otherwise there are remaining data to be sent in the buffer,
327 * so we tell the handler.
328 */
329 si->flags &= ~SI_FL_WAIT_DATA;
330 if (!tick_isset(ob->wex))
331 ob->wex = tick_add_ifset(now_ms, ob->wto);
332
333 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
334 task_wakeup(si->owner, TASK_WOKEN_IO);
335}
336
Willy Tarreaub24281b2011-02-13 13:16:36 +0100337/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200338 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100339 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200340 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100341 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreaufa6bac62012-05-31 14:16:59 +0200342 * It also pre-initializes applet.state to zero and the connection context
343 * to NULL.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200344 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100345struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200346{
Simon Horman7abd00d2011-08-13 08:03:51 +0900347 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200348
Willy Tarreau5c979a92012-05-07 17:15:39 +0200349 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200350 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100351 set_target_applet(&si->target, app);
Aman Gupta9a13e842012-04-02 18:57:53 -0700352 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200353 si->flags |= SI_FL_WAIT_DATA;
354 return si->owner;
355}
356
357/* Register a function to handle a stream_interface as a standalone task. The
358 * new task itself is returned and is assigned as si->owner. The stream_interface
359 * pointer will be pointed to by the task's context. The handler can be detached
360 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100361 * FIXME: the code should be updated to ensure that we don't change si->owner
362 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200363 */
364struct task *stream_int_register_handler_task(struct stream_interface *si,
365 struct task *(*fct)(struct task *))
366{
367 struct task *t;
368
369 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
370
Willy Tarreau5c979a92012-05-07 17:15:39 +0200371 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200372 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100373 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200374 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200375 si->flags |= SI_FL_WAIT_DATA;
376
377 t = task_new();
378 si->owner = t;
379 if (!t)
380 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100381
Willy Tarreau9e000c62011-03-10 14:03:36 +0100382 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100383
Willy Tarreaufb90d942009-09-05 20:57:35 +0200384 t->process = fct;
385 t->context = si;
386 task_wakeup(si->owner, TASK_WOKEN_INIT);
387
388 return t;
389}
390
391/* Unregister a stream interface handler. This must be called by the handler task
392 * itself when it detects that it is in the SI_ST_DIS state. This function can
393 * both detach standalone handlers and embedded handlers.
394 */
395void stream_int_unregister_handler(struct stream_interface *si)
396{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100397 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200398 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100399 task_delete(si->target.ptr.t);
400 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200401 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200402 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200403 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100404 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200405}
406
Willy Tarreau2c6be842012-07-06 17:12:34 +0200407/* This callback is used to send a valid PROXY protocol line to a socket being
408 * established. It returns a combination of FD_WAIT_* if it wants some polling
409 * before being called again, otherwise it returns zero and removes itself from
410 * the connection's flags (the bit is provided in <flag> by the caller).
411 */
412int conn_si_send_proxy(struct connection *conn, unsigned int flag)
413{
414 int fd = conn->t.sock.fd;
415 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
416 struct buffer *b = si->ob;
417
418 /* we might have been called just after an asynchronous shutw */
419 if (b->flags & BF_SHUTW)
420 goto out_error;
421
422 /* If we have a PROXY line to send, we'll use this to validate the
423 * connection, in which case the connection is validated only once
424 * we've sent the whole proxy line. Otherwise we use connect().
425 */
426 if (si->send_proxy_ofs) {
427 int ret;
428
429 /* The target server expects a PROXY line to be sent first.
430 * If the send_proxy_ofs is negative, it corresponds to the
431 * offset to start sending from then end of the proxy string
432 * (which is recomputed every time since it's constant). If
433 * it is positive, it means we have to send from the start.
434 */
435 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
436 if (!ret)
437 goto out_error;
438
439 if (si->send_proxy_ofs > 0)
440 si->send_proxy_ofs = -ret; /* first call */
441
442 /* we have to send trash from (ret+sp for -sp bytes) */
443 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
444 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
445
446 if (ret == 0)
447 goto out_wait;
448
449 if (ret < 0) {
450 if (errno == EAGAIN)
451 goto out_wait;
452 goto out_error;
453 }
454
455 si->send_proxy_ofs += ret; /* becomes zero once complete */
456 if (si->send_proxy_ofs != 0)
457 goto out_wait;
458
459 /* OK we've sent the whole line, we're connected */
460 }
461
462 /* The FD is ready now, simply return and let the connection handler
463 * notify upper layers if needed.
464 */
465 if (conn->flags & CO_FL_WAIT_L4_CONN)
466 conn->flags &= ~CO_FL_WAIT_L4_CONN;
467 b->flags |= BF_WRITE_NULL;
468 si->exp = TICK_ETERNITY;
469
470 out_leave:
471 conn->flags &= ~flag;
472 return 0;
473
474 out_error:
475 /* Write error on the file descriptor. We mark the FD as STERROR so
476 * that we don't use it anymore. The error is reported to the stream
477 * interface which will take proper action. We must not perturbate the
478 * buffer because the stream interface wants to ensure transparent
479 * connection retries.
480 */
481
482 conn->flags |= CO_FL_ERROR;
483 fdtab[fd].ev &= ~FD_POLL_STICKY;
484 EV_FD_REM(fd);
485 goto out_leave;
486
487 out_wait:
488 return FD_WAIT_WRITE;
489}
490
Willy Tarreaufd31e532012-07-23 18:24:25 +0200491/* function to be called on stream sockets after all I/O handlers */
492void stream_sock_update_conn(struct connection *conn)
493{
494 int fd = conn->t.sock.fd;
495 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
496
497 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
498 __FUNCTION__,
499 si, si->state, si->ib->flags, si->ob->flags);
500
501 /* process consumer side, only once if possible */
502 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) {
503 if (si->ob->flags & BF_OUT_EMPTY) {
504 if (((si->ob->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) &&
505 (si->state == SI_ST_EST))
506 si_shutw(si);
507 EV_FD_CLR(fd, DIR_WR);
508 si->ob->wex = TICK_ETERNITY;
509 }
510
511 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
512 si->flags |= SI_FL_WAIT_DATA;
513
514 if (si->ob->flags & BF_WRITE_ACTIVITY) {
515 /* update timeouts if we have written something */
516 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
517 if (tick_isset(si->ob->wex))
518 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
519
520 if (!(si->flags & SI_FL_INDEP_STR))
521 if (tick_isset(si->ib->rex))
522 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
523
524 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
525 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
526 si_chk_rcv(si->ob->prod);
527 }
528 }
529
530 /* process producer side, only once if possible */
531 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) {
532 /* We might have some data the consumer is waiting for.
533 * We can do fast-forwarding, but we avoid doing this for partial
534 * buffers, because it is very likely that it will be done again
535 * immediately afterwards once the following data is parsed (eg:
536 * HTTP chunking).
537 */
538 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
539 (si->ib->pipe /* always try to send spliced data */ ||
540 (si->ib->i == 0 && (si->ib->cons->flags & SI_FL_WAIT_DATA)))) {
541 int last_len = si->ib->pipe ? si->ib->pipe->data : 0;
542
543 si_chk_snd(si->ib->cons);
544
545 /* check if the consumer has freed some space */
546 if (!(si->ib->flags & BF_FULL) &&
547 (!last_len || !si->ib->pipe || si->ib->pipe->data < last_len))
548 si->flags &= ~SI_FL_WAIT_ROOM;
549 }
550
551 if (si->flags & SI_FL_WAIT_ROOM) {
552 EV_FD_CLR(fd, DIR_RD);
553 si->ib->rex = TICK_ETERNITY;
554 }
555 else if ((si->ib->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL) {
556 if (tick_isset(si->ib->rex))
557 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
558 }
559 }
560
561 /* wake the task up only when needed */
562 if (/* changes on the production side */
563 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
564 si->state != SI_ST_EST ||
565 (si->flags & SI_FL_ERR) ||
566 ((si->ib->flags & BF_READ_PARTIAL) &&
567 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
568
569 /* changes on the consumption side */
570 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
571 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
572 ((si->ob->flags & BF_SHUTW) ||
573 si->ob->prod->state != SI_ST_EST ||
574 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
575 task_wakeup(si->owner, TASK_WOKEN_IO);
576 }
577 if (si->ib->flags & BF_READ_ACTIVITY)
578 si->ib->flags &= ~BF_READ_DONTWAIT;
579}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200580
Willy Tarreaudded32d2008-11-30 19:48:07 +0100581/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100582 * Local variables:
583 * c-indent-level: 8
584 * c-basic-offset: 8
585 * End:
586 */