blob: 030c7c10dfdfde381b00da43c267c905359f065c [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 Tarreau8b117082012-08-06 15:06:49 +020030#include <proto/connection.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010031#include <proto/fd.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020032#include <proto/frontend.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020033#include <proto/sock_raw.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020034#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010035#include <proto/task.h>
36
Willy Tarreaufd31e532012-07-23 18:24:25 +020037#include <types/pipe.h>
38
Willy Tarreauf873d752012-05-11 17:47:17 +020039/* socket functions used when running a stream interface as a task */
40static void stream_int_update(struct stream_interface *si);
41static void stream_int_update_embedded(struct stream_interface *si);
42static void stream_int_shutr(struct stream_interface *si);
43static void stream_int_shutw(struct stream_interface *si);
44static void stream_int_chk_rcv(struct stream_interface *si);
45static void stream_int_chk_snd(struct stream_interface *si);
46
Willy Tarreau5c979a92012-05-07 17:15:39 +020047/* socket operations for embedded tasks */
48struct sock_ops stream_int_embedded = {
49 .update = stream_int_update_embedded,
50 .shutr = stream_int_shutr,
51 .shutw = stream_int_shutw,
52 .chk_rcv = stream_int_chk_rcv,
53 .chk_snd = stream_int_chk_snd,
54 .read = NULL,
55 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020056 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020057};
58
59/* socket operations for external tasks */
60struct sock_ops stream_int_task = {
61 .update = stream_int_update,
62 .shutr = stream_int_shutr,
63 .shutw = stream_int_shutw,
64 .chk_rcv = stream_int_chk_rcv,
65 .chk_snd = stream_int_chk_snd,
66 .read = NULL,
67 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020068 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020069};
70
Willy Tarreaucff64112008-11-03 06:26:53 +010071/*
72 * This function only has to be called once after a wakeup event in case of
73 * suspected timeout. It controls the stream interface timeouts and sets
74 * si->flags accordingly. It does NOT close anything, as this timeout may
75 * be used for any purpose. It returns 1 if the timeout fired, otherwise
76 * zero.
77 */
78int stream_int_check_timeouts(struct stream_interface *si)
79{
80 if (tick_is_expired(si->exp, now_ms)) {
81 si->flags |= SI_FL_EXP;
82 return 1;
83 }
84 return 0;
85}
86
Willy Tarreaufe3718a2008-11-30 18:14:12 +010087/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010088void stream_int_report_error(struct stream_interface *si)
89{
90 if (!si->err_type)
91 si->err_type = SI_ET_DATA_ERR;
92
Willy Tarreaucff64112008-11-03 06:26:53 +010093 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010094 si->ib->flags |= BF_READ_ERROR;
95}
96
97/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010098 * Returns a message to the client ; the connection is shut down for read,
99 * and the request is cleared so that no server connection can be initiated.
100 * The buffer is marked for read shutdown on the other side to protect the
101 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +0100102 * "chunk". If it is null, then an empty message is used. The reply buffer does
103 * not need to be empty before this, and its contents will not be overwritten.
104 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100105 */
106void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
107{
Willy Tarreau148d0992010-01-10 10:21:21 +0100108 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100109 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100110 buffer_auto_close(si->ib);
111 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100112
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200113 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100114 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200115 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100116
117 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100118 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200119 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100120 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100121}
122
Willy Tarreaufb90d942009-09-05 20:57:35 +0200123/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200124static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200125{
126 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
127 __FUNCTION__,
128 si, si->state, si->ib->flags, si->ob->flags);
129
130 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
131 task_wakeup(si->owner, TASK_WOKEN_IO);
132}
133
134/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200135static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200136{
Willy Tarreau3488e252010-08-09 16:24:56 +0200137 int old_flags = si->flags;
138
Willy Tarreaufb90d942009-09-05 20:57:35 +0200139 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
140 __FUNCTION__,
141 si, si->state, si->ib->flags, si->ob->flags);
142
143 if (si->state != SI_ST_EST)
144 return;
145
146 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 +0200147 si_shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200148
149 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
150 si->flags |= SI_FL_WAIT_DATA;
151
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200152 /* we're almost sure that we need some space if the buffer is not
153 * empty, even if it's not full, because the applets can't fill it.
154 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200155 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200156 si->flags |= SI_FL_WAIT_ROOM;
157
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200158 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200159 if (tick_isset(si->ob->wex))
160 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
161 }
162
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200163 if (si->ib->flags & BF_READ_ACTIVITY ||
164 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
165 if (tick_isset(si->ib->rex))
166 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
167 }
168
Willy Tarreau3488e252010-08-09 16:24:56 +0200169 /* save flags to detect changes */
170 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200171 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 +0200172 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200173 si_chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200174
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200175 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200176 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200177 si_chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200178 /* check if the consumer has freed some space */
179 if (!(si->ib->flags & BF_FULL))
180 si->flags &= ~SI_FL_WAIT_ROOM;
181 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200182
183 /* Note that we're trying to wake up in two conditions here :
184 * - special event, which needs the holder task attention
185 * - status indicating that the applet can go on working. This
186 * is rather hard because we might be blocking on output and
187 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200188 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200189 */
190 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200191 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
192
193 /* changes on the production side */
194 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
195 si->state != SI_ST_EST ||
196 (si->flags & SI_FL_ERR) ||
197 ((si->ib->flags & BF_READ_PARTIAL) &&
198 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
199
200 /* changes on the consumption side */
201 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
202 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
203 ((si->ob->flags & BF_SHUTW) ||
204 si->ob->prod->state != SI_ST_EST ||
205 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200206 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
207 task_wakeup(si->owner, TASK_WOKEN_IO);
208 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200209 if (si->ib->flags & BF_READ_ACTIVITY)
210 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200211}
212
213/* default shutr function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200214static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200215{
216 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
217 __FUNCTION__,
218 si, si->state, si->ib->flags, si->ob->flags);
219
220 si->ib->flags &= ~BF_SHUTR_NOW;
221 if (si->ib->flags & BF_SHUTR)
222 return;
223 si->ib->flags |= BF_SHUTR;
224 si->ib->rex = TICK_ETERNITY;
225 si->flags &= ~SI_FL_WAIT_ROOM;
226
227 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
228 return;
229
230 if (si->ob->flags & BF_SHUTW) {
231 si->state = SI_ST_DIS;
232 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200233
Willy Tarreau8b117082012-08-06 15:06:49 +0200234 conn_data_close(&si->conn);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200235 if (si->release)
236 si->release(si);
237 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200238
Willy Tarreaufb90d942009-09-05 20:57:35 +0200239 /* note that if the task exist, it must unregister itself once it runs */
240 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
241 task_wakeup(si->owner, TASK_WOKEN_IO);
242}
243
244/* default shutw function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200245static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200246{
247 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
248 __FUNCTION__,
249 si, si->state, si->ib->flags, si->ob->flags);
250
251 si->ob->flags &= ~BF_SHUTW_NOW;
252 if (si->ob->flags & BF_SHUTW)
253 return;
254 si->ob->flags |= BF_SHUTW;
255 si->ob->wex = TICK_ETERNITY;
256 si->flags &= ~SI_FL_WAIT_DATA;
257
258 switch (si->state) {
259 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200260 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261 break;
262
263 /* fall through */
264 case SI_ST_CON:
265 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100266 case SI_ST_QUE:
267 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200268 si->state = SI_ST_DIS;
269 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200270
Willy Tarreau8b117082012-08-06 15:06:49 +0200271 conn_data_close(&si->conn);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200272 if (si->release)
273 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200274 default:
275 si->flags &= ~SI_FL_WAIT_ROOM;
276 si->ib->flags |= BF_SHUTR;
277 si->ib->rex = TICK_ETERNITY;
278 si->exp = TICK_ETERNITY;
279 }
280
281 /* note that if the task exist, it must unregister itself once it runs */
282 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
283 task_wakeup(si->owner, TASK_WOKEN_IO);
284}
285
286/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200287static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200288{
289 struct buffer *ib = si->ib;
290
291 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
292 __FUNCTION__,
293 si, si->state, si->ib->flags, si->ob->flags);
294
295 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
296 return;
297
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200298 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200299 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200300 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200301 si->flags |= SI_FL_WAIT_ROOM;
302 }
303 else {
304 /* (re)start reading */
305 si->flags &= ~SI_FL_WAIT_ROOM;
306 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
307 task_wakeup(si->owner, TASK_WOKEN_IO);
308 }
309}
310
311/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200312static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200313{
314 struct buffer *ob = si->ob;
315
316 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
317 __FUNCTION__,
318 si, si->state, si->ib->flags, si->ob->flags);
319
320 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
321 return;
322
323 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
324 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
325 return;
326
327 /* Otherwise there are remaining data to be sent in the buffer,
328 * so we tell the handler.
329 */
330 si->flags &= ~SI_FL_WAIT_DATA;
331 if (!tick_isset(ob->wex))
332 ob->wex = tick_add_ifset(now_ms, ob->wto);
333
334 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
335 task_wakeup(si->owner, TASK_WOKEN_IO);
336}
337
Willy Tarreaub24281b2011-02-13 13:16:36 +0100338/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200339 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100340 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200341 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100342 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreaufa6bac62012-05-31 14:16:59 +0200343 * It also pre-initializes applet.state to zero and the connection context
344 * to NULL.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200345 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100346struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200347{
Simon Horman7abd00d2011-08-13 08:03:51 +0900348 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200349
Willy Tarreau5c979a92012-05-07 17:15:39 +0200350 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200351 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100352 set_target_applet(&si->target, app);
Aman Gupta9a13e842012-04-02 18:57:53 -0700353 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200354 si->flags |= SI_FL_WAIT_DATA;
355 return si->owner;
356}
357
358/* Register a function to handle a stream_interface as a standalone task. The
359 * new task itself is returned and is assigned as si->owner. The stream_interface
360 * pointer will be pointed to by the task's context. The handler can be detached
361 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100362 * FIXME: the code should be updated to ensure that we don't change si->owner
363 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200364 */
365struct task *stream_int_register_handler_task(struct stream_interface *si,
366 struct task *(*fct)(struct task *))
367{
368 struct task *t;
369
370 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
371
Willy Tarreau5c979a92012-05-07 17:15:39 +0200372 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200373 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100374 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200375 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200376 si->flags |= SI_FL_WAIT_DATA;
377
378 t = task_new();
379 si->owner = t;
380 if (!t)
381 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100382
Willy Tarreau9e000c62011-03-10 14:03:36 +0100383 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100384
Willy Tarreaufb90d942009-09-05 20:57:35 +0200385 t->process = fct;
386 t->context = si;
387 task_wakeup(si->owner, TASK_WOKEN_INIT);
388
389 return t;
390}
391
392/* Unregister a stream interface handler. This must be called by the handler task
393 * itself when it detects that it is in the SI_ST_DIS state. This function can
394 * both detach standalone handlers and embedded handlers.
395 */
396void stream_int_unregister_handler(struct stream_interface *si)
397{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100398 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200399 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100400 task_delete(si->target.ptr.t);
401 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200402 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200403 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200404 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100405 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200406}
407
Willy Tarreau2c6be842012-07-06 17:12:34 +0200408/* This callback is used to send a valid PROXY protocol line to a socket being
409 * established. It returns a combination of FD_WAIT_* if it wants some polling
410 * before being called again, otherwise it returns zero and removes itself from
411 * the connection's flags (the bit is provided in <flag> by the caller).
412 */
413int conn_si_send_proxy(struct connection *conn, unsigned int flag)
414{
415 int fd = conn->t.sock.fd;
416 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
417 struct buffer *b = si->ob;
418
419 /* we might have been called just after an asynchronous shutw */
420 if (b->flags & BF_SHUTW)
421 goto out_error;
422
423 /* If we have a PROXY line to send, we'll use this to validate the
424 * connection, in which case the connection is validated only once
425 * we've sent the whole proxy line. Otherwise we use connect().
426 */
427 if (si->send_proxy_ofs) {
428 int ret;
429
430 /* The target server expects a PROXY line to be sent first.
431 * If the send_proxy_ofs is negative, it corresponds to the
432 * offset to start sending from then end of the proxy string
433 * (which is recomputed every time since it's constant). If
434 * it is positive, it means we have to send from the start.
435 */
436 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
437 if (!ret)
438 goto out_error;
439
440 if (si->send_proxy_ofs > 0)
441 si->send_proxy_ofs = -ret; /* first call */
442
443 /* we have to send trash from (ret+sp for -sp bytes) */
444 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
445 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
446
447 if (ret == 0)
448 goto out_wait;
449
450 if (ret < 0) {
451 if (errno == EAGAIN)
452 goto out_wait;
453 goto out_error;
454 }
455
456 si->send_proxy_ofs += ret; /* becomes zero once complete */
457 if (si->send_proxy_ofs != 0)
458 goto out_wait;
459
460 /* OK we've sent the whole line, we're connected */
461 }
462
463 /* The FD is ready now, simply return and let the connection handler
464 * notify upper layers if needed.
465 */
466 if (conn->flags & CO_FL_WAIT_L4_CONN)
467 conn->flags &= ~CO_FL_WAIT_L4_CONN;
468 b->flags |= BF_WRITE_NULL;
469 si->exp = TICK_ETERNITY;
470
471 out_leave:
472 conn->flags &= ~flag;
473 return 0;
474
475 out_error:
476 /* Write error on the file descriptor. We mark the FD as STERROR so
477 * that we don't use it anymore. The error is reported to the stream
478 * interface which will take proper action. We must not perturbate the
479 * buffer because the stream interface wants to ensure transparent
480 * connection retries.
481 */
482
483 conn->flags |= CO_FL_ERROR;
484 fdtab[fd].ev &= ~FD_POLL_STICKY;
485 EV_FD_REM(fd);
486 goto out_leave;
487
488 out_wait:
489 return FD_WAIT_WRITE;
490}
491
Willy Tarreaufd31e532012-07-23 18:24:25 +0200492/* function to be called on stream sockets after all I/O handlers */
493void stream_sock_update_conn(struct connection *conn)
494{
495 int fd = conn->t.sock.fd;
496 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
497
498 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
499 __FUNCTION__,
500 si, si->state, si->ib->flags, si->ob->flags);
501
Willy Tarreau3c55ec22012-07-23 19:19:51 +0200502 if (conn->flags & CO_FL_ERROR)
503 si->flags |= SI_FL_ERR;
504
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200505 /* check for recent connection establishment */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200506 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) {
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200507 si->exp = TICK_ETERNITY;
508 si->ob->flags |= BF_WRITE_NULL;
509 }
510
Willy Tarreaufd31e532012-07-23 18:24:25 +0200511 /* process consumer side, only once if possible */
512 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) {
513 if (si->ob->flags & BF_OUT_EMPTY) {
514 if (((si->ob->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) &&
515 (si->state == SI_ST_EST))
516 si_shutw(si);
517 EV_FD_CLR(fd, DIR_WR);
518 si->ob->wex = TICK_ETERNITY;
519 }
520
521 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
522 si->flags |= SI_FL_WAIT_DATA;
523
524 if (si->ob->flags & BF_WRITE_ACTIVITY) {
525 /* update timeouts if we have written something */
526 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
527 if (tick_isset(si->ob->wex))
528 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
529
530 if (!(si->flags & SI_FL_INDEP_STR))
531 if (tick_isset(si->ib->rex))
532 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
533
534 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
535 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
536 si_chk_rcv(si->ob->prod);
537 }
538 }
539
540 /* process producer side, only once if possible */
541 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) {
542 /* We might have some data the consumer is waiting for.
543 * We can do fast-forwarding, but we avoid doing this for partial
544 * buffers, because it is very likely that it will be done again
545 * immediately afterwards once the following data is parsed (eg:
546 * HTTP chunking).
547 */
548 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
549 (si->ib->pipe /* always try to send spliced data */ ||
550 (si->ib->i == 0 && (si->ib->cons->flags & SI_FL_WAIT_DATA)))) {
551 int last_len = si->ib->pipe ? si->ib->pipe->data : 0;
552
553 si_chk_snd(si->ib->cons);
554
555 /* check if the consumer has freed some space */
556 if (!(si->ib->flags & BF_FULL) &&
557 (!last_len || !si->ib->pipe || si->ib->pipe->data < last_len))
558 si->flags &= ~SI_FL_WAIT_ROOM;
559 }
560
561 if (si->flags & SI_FL_WAIT_ROOM) {
562 EV_FD_CLR(fd, DIR_RD);
563 si->ib->rex = TICK_ETERNITY;
564 }
565 else if ((si->ib->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL) {
566 if (tick_isset(si->ib->rex))
567 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
568 }
569 }
570
571 /* wake the task up only when needed */
572 if (/* changes on the production side */
573 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
574 si->state != SI_ST_EST ||
575 (si->flags & SI_FL_ERR) ||
576 ((si->ib->flags & BF_READ_PARTIAL) &&
577 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
578
579 /* changes on the consumption side */
580 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
581 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
582 ((si->ob->flags & BF_SHUTW) ||
583 si->ob->prod->state != SI_ST_EST ||
584 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
585 task_wakeup(si->owner, TASK_WOKEN_IO);
586 }
587 if (si->ib->flags & BF_READ_ACTIVITY)
588 si->ib->flags &= ~BF_READ_DONTWAIT;
589}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200590
Willy Tarreaudded32d2008-11-30 19:48:07 +0100591/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100592 * Local variables:
593 * c-indent-level: 8
594 * c-basic-offset: 8
595 * End:
596 */