blob: 2c0364fa5536ced63cb29eed27610a53d756fb5b [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 Tarreau96199b12012-08-24 00:46:52 +020033#include <proto/pipe.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);
Willy Tarreauf873d752012-05-11 17:47:17 +020042static void stream_int_chk_rcv(struct stream_interface *si);
43static void stream_int_chk_snd(struct stream_interface *si);
44
Willy Tarreau5c979a92012-05-07 17:15:39 +020045/* socket operations for embedded tasks */
46struct sock_ops stream_int_embedded = {
47 .update = stream_int_update_embedded,
Willy Tarreau4a36b562012-08-06 19:31:45 +020048 .shutr = NULL,
49 .shutw = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020050 .chk_rcv = stream_int_chk_rcv,
51 .chk_snd = stream_int_chk_snd,
52 .read = NULL,
53 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020054 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020055};
56
57/* socket operations for external tasks */
58struct sock_ops stream_int_task = {
59 .update = stream_int_update,
Willy Tarreau4a36b562012-08-06 19:31:45 +020060 .shutr = NULL,
61 .shutw = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020062 .chk_rcv = stream_int_chk_rcv,
63 .chk_snd = stream_int_chk_snd,
64 .read = NULL,
65 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020066 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020067};
68
Willy Tarreaucff64112008-11-03 06:26:53 +010069/*
70 * This function only has to be called once after a wakeup event in case of
71 * suspected timeout. It controls the stream interface timeouts and sets
72 * si->flags accordingly. It does NOT close anything, as this timeout may
73 * be used for any purpose. It returns 1 if the timeout fired, otherwise
74 * zero.
75 */
76int stream_int_check_timeouts(struct stream_interface *si)
77{
78 if (tick_is_expired(si->exp, now_ms)) {
79 si->flags |= SI_FL_EXP;
80 return 1;
81 }
82 return 0;
83}
84
Willy Tarreaufe3718a2008-11-30 18:14:12 +010085/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010086void stream_int_report_error(struct stream_interface *si)
87{
88 if (!si->err_type)
89 si->err_type = SI_ET_DATA_ERR;
90
Willy Tarreaucff64112008-11-03 06:26:53 +010091 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010092 si->ib->flags |= BF_READ_ERROR;
93}
94
95/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010096 * Returns a message to the client ; the connection is shut down for read,
97 * and the request is cleared so that no server connection can be initiated.
98 * The buffer is marked for read shutdown on the other side to protect the
99 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +0100100 * "chunk". If it is null, then an empty message is used. The reply buffer does
101 * not need to be empty before this, and its contents will not be overwritten.
102 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100103 */
104void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
105{
Willy Tarreau148d0992010-01-10 10:21:21 +0100106 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100107 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100108 buffer_auto_close(si->ib);
109 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100110
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200111 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100112 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200113 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100114
115 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100116 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200117 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100118 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100119}
120
Willy Tarreaufb90d942009-09-05 20:57:35 +0200121/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200122static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200123{
124 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
125 __FUNCTION__,
126 si, si->state, si->ib->flags, si->ob->flags);
127
128 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
129 task_wakeup(si->owner, TASK_WOKEN_IO);
130}
131
132/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200133static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200134{
Willy Tarreau3488e252010-08-09 16:24:56 +0200135 int old_flags = si->flags;
136
Willy Tarreaufb90d942009-09-05 20:57:35 +0200137 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
138 __FUNCTION__,
139 si, si->state, si->ib->flags, si->ob->flags);
140
141 if (si->state != SI_ST_EST)
142 return;
143
144 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 +0200145 si_shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200146
147 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
148 si->flags |= SI_FL_WAIT_DATA;
149
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200150 /* we're almost sure that we need some space if the buffer is not
151 * empty, even if it's not full, because the applets can't fill it.
152 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200153 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200154 si->flags |= SI_FL_WAIT_ROOM;
155
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200156 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200157 if (tick_isset(si->ob->wex))
158 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
159 }
160
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200161 if (si->ib->flags & BF_READ_ACTIVITY ||
162 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
163 if (tick_isset(si->ib->rex))
164 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
165 }
166
Willy Tarreau3488e252010-08-09 16:24:56 +0200167 /* save flags to detect changes */
168 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200169 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 +0200170 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200171 si_chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200172
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200173 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200174 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200175 si_chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200176 /* check if the consumer has freed some space */
177 if (!(si->ib->flags & BF_FULL))
178 si->flags &= ~SI_FL_WAIT_ROOM;
179 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200180
181 /* Note that we're trying to wake up in two conditions here :
182 * - special event, which needs the holder task attention
183 * - status indicating that the applet can go on working. This
184 * is rather hard because we might be blocking on output and
185 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200186 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200187 */
188 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200189 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
190
191 /* changes on the production side */
192 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
193 si->state != SI_ST_EST ||
194 (si->flags & SI_FL_ERR) ||
195 ((si->ib->flags & BF_READ_PARTIAL) &&
196 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
197
198 /* changes on the consumption side */
199 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
200 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
201 ((si->ob->flags & BF_SHUTW) ||
202 si->ob->prod->state != SI_ST_EST ||
203 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200204 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
205 task_wakeup(si->owner, TASK_WOKEN_IO);
206 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200207 if (si->ib->flags & BF_READ_ACTIVITY)
208 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200209}
210
Willy Tarreau4a36b562012-08-06 19:31:45 +0200211/*
212 * This function performs a shutdown-read on a stream interface in a connected
213 * or init state (it does nothing for other states). It either shuts the read
214 * side or marks itself as closed. The buffer flags are updated to reflect the
215 * new state. If the stream interface has SI_FL_NOHALF, we also forward the
216 * close to the write side. If a control layer is defined, then it is supposed
217 * to be a socket layer and file descriptors are then shutdown or closed
218 * accordingly. If no control layer is defined, then the SI is supposed to be
219 * an embedded one and the owner task is woken up if it exists. The function
220 * does not disable polling on the FD by itself, it returns non-zero instead
221 * if the caller needs to do so (except when the FD is deleted where this is
222 * implicit).
223 */
224int stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200225{
Willy Tarreau4a36b562012-08-06 19:31:45 +0200226 struct connection *conn = &si->conn;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200227
228 si->ib->flags &= ~BF_SHUTR_NOW;
229 if (si->ib->flags & BF_SHUTR)
Willy Tarreau4a36b562012-08-06 19:31:45 +0200230 return 0;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200231 si->ib->flags |= BF_SHUTR;
232 si->ib->rex = TICK_ETERNITY;
233 si->flags &= ~SI_FL_WAIT_ROOM;
234
235 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau4a36b562012-08-06 19:31:45 +0200236 return 0;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200237
238 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200239 conn_data_close(&si->conn);
240 if (conn->ctrl)
241 fd_delete(si_fd(si));
Willy Tarreaufb90d942009-09-05 20:57:35 +0200242 si->state = SI_ST_DIS;
243 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200244
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200245 if (si->release)
246 si->release(si);
247 }
Willy Tarreau4a36b562012-08-06 19:31:45 +0200248 else if (si->flags & SI_FL_NOHALF) {
249 /* we want to immediately forward this close to the write side */
250 return stream_int_shutw(si);
251 }
252 else if (conn->ctrl) {
253 /* we want the caller to disable polling on this FD */
254 return 1;
255 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200256
Willy Tarreau4a36b562012-08-06 19:31:45 +0200257 /* note that if the task exists, it must unregister itself once it runs */
258 if (!conn->ctrl && !(si->flags & SI_FL_DONT_WAKE) && si->owner)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200259 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreau4a36b562012-08-06 19:31:45 +0200260 return 0;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261}
262
Willy Tarreau4a36b562012-08-06 19:31:45 +0200263/*
264 * This function performs a shutdown-write on a stream interface in a connected or
265 * init state (it does nothing for other states). It either shuts the write side
266 * or marks itself as closed. The buffer flags are updated to reflect the new state.
267 * It does also close everything if the SI was marked as being in error state. If
268 * there is a data-layer shutdown, it is called. If a control layer is defined, then
269 * it is supposed to be a socket layer and file descriptors are then shutdown or
270 * closed accordingly. If no control layer is defined, then the SI is supposed to
271 * be an embedded one and the owner task is woken up if it exists. The function
272 * does not disable polling on the FD by itself, it returns non-zero instead if
273 * the caller needs to do so (except when the FD is deleted where this is implicit).
274 */
275int stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200276{
Willy Tarreau4a36b562012-08-06 19:31:45 +0200277 struct connection *conn = &si->conn;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200278
279 si->ob->flags &= ~BF_SHUTW_NOW;
280 if (si->ob->flags & BF_SHUTW)
Willy Tarreau4a36b562012-08-06 19:31:45 +0200281 return 0;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200282 si->ob->flags |= BF_SHUTW;
283 si->ob->wex = TICK_ETERNITY;
284 si->flags &= ~SI_FL_WAIT_DATA;
285
286 switch (si->state) {
287 case SI_ST_EST:
Willy Tarreau4a36b562012-08-06 19:31:45 +0200288 /* we have to shut before closing, otherwise some short messages
289 * may never leave the system, especially when there are remaining
290 * unread data in the socket input buffer, or when nolinger is set.
291 * However, if SI_FL_NOLINGER is explicitly set, we know there is
292 * no risk so we close both sides immediately.
293 */
294 if (si->flags & SI_FL_ERR) {
295 /* quick close, the socket is already shut. Remove pending flags. */
296 si->flags &= ~SI_FL_NOLINGER;
297 } else if (si->flags & SI_FL_NOLINGER) {
298 si->flags &= ~SI_FL_NOLINGER;
299 if (conn->ctrl) {
300 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
301 (struct linger *) &nolinger, sizeof(struct linger));
302 }
303 /* unclean data-layer shutdown */
304 if (conn->data && conn->data->shutw)
305 conn->data->shutw(conn, 0);
306 } else {
307 /* clean data-layer shutdown */
308 if (conn->data && conn->data->shutw)
309 conn->data->shutw(conn, 1);
310
311 if (!(si->flags & SI_FL_NOHALF)) {
312 /* We shutdown transport layer */
313 if (conn->ctrl)
314 shutdown(si_fd(si), SHUT_WR);
315
316 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) {
317 /* OK just a shutw, but we want the caller
318 * to disable polling on this FD if exists.
319 */
320 return !!conn->ctrl;
321 }
322 }
323 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200324
325 /* fall through */
326 case SI_ST_CON:
Willy Tarreau4a36b562012-08-06 19:31:45 +0200327 /* we may have to close a pending connection, and mark the
328 * response buffer as shutr
329 */
330 conn_data_close(&si->conn);
331 if (conn->ctrl)
332 fd_delete(si_fd(si));
333 /* fall through */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200334 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100335 case SI_ST_QUE:
336 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200337 si->state = SI_ST_DIS;
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200338
339 if (si->release)
340 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200341 default:
342 si->flags &= ~SI_FL_WAIT_ROOM;
343 si->ib->flags |= BF_SHUTR;
344 si->ib->rex = TICK_ETERNITY;
345 si->exp = TICK_ETERNITY;
346 }
347
Willy Tarreau4a36b562012-08-06 19:31:45 +0200348 /* note that if the task exists, it must unregister itself once it runs */
349 if (!conn->ctrl && !(si->flags & SI_FL_DONT_WAKE) && si->owner)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200350 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreau4a36b562012-08-06 19:31:45 +0200351 return 0;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200352}
353
354/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200355static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200356{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200357 struct channel *ib = si->ib;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200358
359 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
360 __FUNCTION__,
361 si, si->state, si->ib->flags, si->ob->flags);
362
363 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
364 return;
365
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200366 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200367 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200368 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200369 si->flags |= SI_FL_WAIT_ROOM;
370 }
371 else {
372 /* (re)start reading */
373 si->flags &= ~SI_FL_WAIT_ROOM;
374 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
375 task_wakeup(si->owner, TASK_WOKEN_IO);
376 }
377}
378
379/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200380static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200381{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200382 struct channel *ob = si->ob;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200383
384 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
385 __FUNCTION__,
386 si, si->state, si->ib->flags, si->ob->flags);
387
388 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
389 return;
390
391 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
392 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
393 return;
394
395 /* Otherwise there are remaining data to be sent in the buffer,
396 * so we tell the handler.
397 */
398 si->flags &= ~SI_FL_WAIT_DATA;
399 if (!tick_isset(ob->wex))
400 ob->wex = tick_add_ifset(now_ms, ob->wto);
401
402 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
403 task_wakeup(si->owner, TASK_WOKEN_IO);
404}
405
Willy Tarreaub24281b2011-02-13 13:16:36 +0100406/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200407 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100408 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200409 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100410 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreaufa6bac62012-05-31 14:16:59 +0200411 * It also pre-initializes applet.state to zero and the connection context
412 * to NULL.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200413 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100414struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200415{
Simon Horman7abd00d2011-08-13 08:03:51 +0900416 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200417
Willy Tarreau5c979a92012-05-07 17:15:39 +0200418 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200419 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100420 set_target_applet(&si->target, app);
Aman Gupta9a13e842012-04-02 18:57:53 -0700421 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200422 si->flags |= SI_FL_WAIT_DATA;
423 return si->owner;
424}
425
426/* Register a function to handle a stream_interface as a standalone task. The
427 * new task itself is returned and is assigned as si->owner. The stream_interface
428 * pointer will be pointed to by the task's context. The handler can be detached
429 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100430 * FIXME: the code should be updated to ensure that we don't change si->owner
431 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200432 */
433struct task *stream_int_register_handler_task(struct stream_interface *si,
434 struct task *(*fct)(struct task *))
435{
436 struct task *t;
437
438 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
439
Willy Tarreau5c979a92012-05-07 17:15:39 +0200440 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200441 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100442 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200443 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200444 si->flags |= SI_FL_WAIT_DATA;
445
446 t = task_new();
447 si->owner = t;
448 if (!t)
449 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100450
Willy Tarreau9e000c62011-03-10 14:03:36 +0100451 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100452
Willy Tarreaufb90d942009-09-05 20:57:35 +0200453 t->process = fct;
454 t->context = si;
455 task_wakeup(si->owner, TASK_WOKEN_INIT);
456
457 return t;
458}
459
460/* Unregister a stream interface handler. This must be called by the handler task
461 * itself when it detects that it is in the SI_ST_DIS state. This function can
462 * both detach standalone handlers and embedded handlers.
463 */
464void stream_int_unregister_handler(struct stream_interface *si)
465{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100466 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200467 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100468 task_delete(si->target.ptr.t);
469 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200470 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200471 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200472 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100473 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200474}
475
Willy Tarreau2c6be842012-07-06 17:12:34 +0200476/* This callback is used to send a valid PROXY protocol line to a socket being
Willy Tarreauafad0e02012-08-09 14:45:22 +0200477 * established. It returns 0 if it fails in a fatal way or needs to poll to go
478 * further, otherwise it returns non-zero and removes itself from the connection's
Willy Tarreaua1a74742012-08-24 12:14:49 +0200479 * flags (the bit is provided in <flag> by the caller). It is designed to be
480 * called by the connection handler and relies on it to commit polling changes.
Willy Tarreau2c6be842012-07-06 17:12:34 +0200481 */
482int conn_si_send_proxy(struct connection *conn, unsigned int flag)
483{
Willy Tarreau2c6be842012-07-06 17:12:34 +0200484 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200485
486 /* we might have been called just after an asynchronous shutw */
Willy Tarreaua1a74742012-08-24 12:14:49 +0200487 if (conn->flags & CO_FL_SOCK_WR_SH)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200488 goto out_error;
489
490 /* If we have a PROXY line to send, we'll use this to validate the
491 * connection, in which case the connection is validated only once
492 * we've sent the whole proxy line. Otherwise we use connect().
493 */
494 if (si->send_proxy_ofs) {
495 int ret;
496
497 /* The target server expects a PROXY line to be sent first.
498 * If the send_proxy_ofs is negative, it corresponds to the
499 * offset to start sending from then end of the proxy string
500 * (which is recomputed every time since it's constant). If
501 * it is positive, it means we have to send from the start.
502 */
Willy Tarreaua1a74742012-08-24 12:14:49 +0200503 ret = make_proxy_line(trash, trashlen, &si->ob->prod->addr.from, &si->ob->prod->addr.to);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200504 if (!ret)
505 goto out_error;
506
507 if (si->send_proxy_ofs > 0)
508 si->send_proxy_ofs = -ret; /* first call */
509
Willy Tarreaua1a74742012-08-24 12:14:49 +0200510 /* we have to send trash from (ret+sp for -sp bytes). If the
511 * data layer has a pending write, we'll also set MSG_MORE.
512 */
513 ret = send(conn->t.sock.fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
514 (conn->flags & CO_FL_DATA_WR_ENA) ? MSG_MORE : 0);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200515
516 if (ret == 0)
517 goto out_wait;
518
519 if (ret < 0) {
520 if (errno == EAGAIN)
521 goto out_wait;
522 goto out_error;
523 }
524
525 si->send_proxy_ofs += ret; /* becomes zero once complete */
526 if (si->send_proxy_ofs != 0)
527 goto out_wait;
528
529 /* OK we've sent the whole line, we're connected */
530 }
531
Willy Tarreaua1a74742012-08-24 12:14:49 +0200532 /* The connection is ready now, simply return and let the connection
533 * handler notify upper layers if needed.
Willy Tarreau2c6be842012-07-06 17:12:34 +0200534 */
535 if (conn->flags & CO_FL_WAIT_L4_CONN)
536 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200537 conn->flags &= ~flag;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200538 return 1;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200539
540 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200541 /* Write error on the file descriptor */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200542 conn->flags |= CO_FL_ERROR;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200543 conn->flags &= ~flag;
Willy Tarreaua1a74742012-08-24 12:14:49 +0200544 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200545 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200546
547 out_wait:
Willy Tarreaua1a74742012-08-24 12:14:49 +0200548 __conn_sock_stop_recv(conn);
549 __conn_sock_poll_send(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200550 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200551}
552
Willy Tarreau100c4672012-08-20 12:06:26 +0200553/* Callback to be used by connection I/O handlers upon completion. It differs from
554 * the function below in that it is designed to be called by lower layers after I/O
555 * events have been completed. It will also try to wake the associated task up if
Willy Tarreauf16723e2012-08-24 12:52:22 +0200556 * an important event requires special handling. It relies on the connection handler
557 * to commit any polling updates.
Willy Tarreau100c4672012-08-20 12:06:26 +0200558 */
559void conn_notify_si(struct connection *conn)
Willy Tarreaufd31e532012-07-23 18:24:25 +0200560{
Willy Tarreaufd31e532012-07-23 18:24:25 +0200561 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
562
563 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
564 __FUNCTION__,
565 si, si->state, si->ib->flags, si->ob->flags);
566
Willy Tarreau3c55ec22012-07-23 19:19:51 +0200567 if (conn->flags & CO_FL_ERROR)
568 si->flags |= SI_FL_ERR;
569
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200570 /* check for recent connection establishment */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200571 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) {
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200572 si->exp = TICK_ETERNITY;
573 si->ob->flags |= BF_WRITE_NULL;
574 }
575
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200576 /* process consumer side */
577 if (si->ob->flags & BF_OUT_EMPTY) {
578 if (((si->ob->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) &&
579 (si->state == SI_ST_EST))
580 stream_int_shutw(si);
Willy Tarreauf16723e2012-08-24 12:52:22 +0200581 __conn_data_stop_send(conn);
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200582 si->ob->wex = TICK_ETERNITY;
583 }
Willy Tarreaufd31e532012-07-23 18:24:25 +0200584
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200585 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
586 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200587
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200588 if (si->ob->flags & BF_WRITE_ACTIVITY) {
589 /* update timeouts if we have written something */
590 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
591 if (tick_isset(si->ob->wex))
592 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200593
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200594 if (!(si->flags & SI_FL_INDEP_STR))
595 if (tick_isset(si->ib->rex))
596 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200597
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200598 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
599 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
600 si_chk_rcv(si->ob->prod);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200601 }
602
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200603 /* process producer side.
604 * We might have some data the consumer is waiting for.
605 * We can do fast-forwarding, but we avoid doing this for partial
606 * buffers, because it is very likely that it will be done again
607 * immediately afterwards once the following data is parsed (eg:
608 * HTTP chunking).
609 */
610 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
611 (si->ib->pipe /* always try to send spliced data */ ||
612 (si->ib->buf.i == 0 && (si->ib->cons->flags & SI_FL_WAIT_DATA)))) {
613 int last_len = si->ib->pipe ? si->ib->pipe->data : 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200614
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200615 si_chk_snd(si->ib->cons);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200616
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200617 /* check if the consumer has freed some space either in the
618 * buffer or in the pipe.
619 */
620 if (!(si->ib->flags & BF_FULL) &&
621 (!last_len || !si->ib->pipe || si->ib->pipe->data < last_len))
622 si->flags &= ~SI_FL_WAIT_ROOM;
623 }
Willy Tarreaufd31e532012-07-23 18:24:25 +0200624
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200625 if (si->flags & SI_FL_WAIT_ROOM) {
Willy Tarreauf16723e2012-08-24 12:52:22 +0200626 __conn_data_stop_recv(conn);
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200627 si->ib->rex = TICK_ETERNITY;
628 }
629 else if ((si->ib->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL) {
630 if (tick_isset(si->ib->rex))
631 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200632 }
633
634 /* wake the task up only when needed */
635 if (/* changes on the production side */
636 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
637 si->state != SI_ST_EST ||
638 (si->flags & SI_FL_ERR) ||
639 ((si->ib->flags & BF_READ_PARTIAL) &&
640 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
641
642 /* changes on the consumption side */
643 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
644 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
645 ((si->ob->flags & BF_SHUTW) ||
646 si->ob->prod->state != SI_ST_EST ||
647 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
648 task_wakeup(si->owner, TASK_WOKEN_IO);
649 }
650 if (si->ib->flags & BF_READ_ACTIVITY)
651 si->ib->flags &= ~BF_READ_DONTWAIT;
652}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200653
Willy Tarreau5368d802012-08-21 18:22:06 +0200654/*
655 * This function is called to send buffer data to a stream socket.
656 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreauf16723e2012-08-24 12:52:22 +0200657 * It iterates the data layer's snd_buf function. It relies on the
658 * caller to commit polling changes.
Willy Tarreau5368d802012-08-21 18:22:06 +0200659 */
660static int si_conn_send_loop(struct connection *conn)
661{
662 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
663 struct channel *b = si->ob;
664 int write_poll = MAX_WRITE_POLL_LOOPS;
665 int ret;
666
Willy Tarreau96199b12012-08-24 00:46:52 +0200667 conn->flags &= ~(CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM);
Willy Tarreau5368d802012-08-21 18:22:06 +0200668
Willy Tarreau96199b12012-08-24 00:46:52 +0200669 if (b->pipe && conn->data->snd_pipe) {
670 ret = conn->data->snd_pipe(conn, b->pipe);
671 if (ret > 0)
672 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau5368d802012-08-21 18:22:06 +0200673
674 if (!b->pipe->data) {
675 put_pipe(b->pipe);
676 b->pipe = NULL;
Willy Tarreau5368d802012-08-21 18:22:06 +0200677 }
678
Willy Tarreau96199b12012-08-24 00:46:52 +0200679 if (conn->flags & CO_FL_ERROR)
680 return -1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200681
Willy Tarreau96199b12012-08-24 00:46:52 +0200682 if (conn->flags & CO_FL_WAIT_ROOM) {
Willy Tarreauf16723e2012-08-24 12:52:22 +0200683 __conn_data_poll_send(conn);
Willy Tarreau96199b12012-08-24 00:46:52 +0200684 return 0;
685 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200686 }
687
688 /* At this point, the pipe is empty, but we may still have data pending
689 * in the normal buffer.
690 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200691 if (!b->buf.o) {
692 b->flags |= BF_OUT_EMPTY;
693 return 0;
694 }
695
696 /* when we're in this loop, we already know that there is no spliced
697 * data left, and that there are sendable buffered data.
698 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200699 while (!(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH | CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM | CO_FL_HANDSHAKE))) {
700 /* check if we want to inform the kernel that we're interested in
701 * sending more data after this call. We want this if :
702 * - we're about to close after this last send and want to merge
703 * the ongoing FIN with the last segment.
704 * - we know we can't send everything at once and must get back
705 * here because of unaligned data
706 * - there is still a finite amount of data to forward
707 * The test is arranged so that the most common case does only 2
708 * tests.
709 */
710 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
711
712 if ((!(b->flags & (BF_NEVER_WAIT|BF_SEND_DONTWAIT)) &&
713 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
714 (b->flags & BF_EXPECT_MORE))) ||
715 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW))
716 send_flag |= MSG_MORE;
717
718 ret = conn->data->snd_buf(conn, &b->buf, send_flag);
719 if (ret <= 0)
720 break;
721
722 if (si->conn.flags & CO_FL_WAIT_L4_CONN)
723 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
724
725 b->flags |= BF_WRITE_PARTIAL;
726
727 if (likely(!bi_full(b)))
728 b->flags &= ~BF_FULL;
729
730 if (!b->buf.o) {
731 /* Always clear both flags once everything has been sent, they're one-shot */
732 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
733 if (likely(!b->pipe))
734 b->flags |= BF_OUT_EMPTY;
735 break;
736 }
737
738 if (--write_poll <= 0)
739 break;
740 } /* while */
741
742 if (conn->flags & CO_FL_ERROR)
743 return -1;
744
745 if (conn->flags & CO_FL_WAIT_ROOM) {
746 /* we need to poll before going on */
Willy Tarreauf16723e2012-08-24 12:52:22 +0200747 __conn_data_poll_send(&si->conn);
Willy Tarreau5368d802012-08-21 18:22:06 +0200748 return 0;
749 }
750 return 0;
751}
752
753
Willy Tarreau100c4672012-08-20 12:06:26 +0200754/* Updates the timers and flags of a stream interface attached to a connection,
755 * depending on the buffers' flags. It should only be called once after the
756 * buffer flags have settled down, and before they are cleared. It doesn't
757 * harm to call it as often as desired (it just slightly hurts performance).
758 * It is only meant to be called by upper layers after buffer flags have been
759 * manipulated by analysers.
760 */
761void stream_int_update_conn(struct stream_interface *si)
762{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200763 struct channel *ib = si->ib;
764 struct channel *ob = si->ob;
Willy Tarreau100c4672012-08-20 12:06:26 +0200765
766 if (si->conn.flags & CO_FL_HANDSHAKE) {
767 /* a handshake is in progress */
768 si->flags &= ~SI_FL_WAIT_DATA;
769 return;
770 }
771
772 /* Check if we need to close the read side */
773 if (!(ib->flags & BF_SHUTR)) {
774 /* Read not closed, update FD status and timeout for reads */
775 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
776 /* stop reading */
777 if (!(si->flags & SI_FL_WAIT_ROOM)) {
778 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
779 si->flags |= SI_FL_WAIT_ROOM;
780 conn_data_stop_recv(&si->conn);
781 ib->rex = TICK_ETERNITY;
782 }
783 }
784 else {
785 /* (re)start reading and update timeout. Note: we don't recompute the timeout
786 * everytime we get here, otherwise it would risk never to expire. We only
787 * update it if is was not yet set. The stream socket handler will already
788 * have updated it if there has been a completed I/O.
789 */
790 si->flags &= ~SI_FL_WAIT_ROOM;
791 conn_data_want_recv(&si->conn);
792 if (!(ib->flags & (BF_READ_NOEXP|BF_DONT_READ)) && !tick_isset(ib->rex))
793 ib->rex = tick_add_ifset(now_ms, ib->rto);
794 }
795 }
796
797 /* Check if we need to close the write side */
798 if (!(ob->flags & BF_SHUTW)) {
799 /* Write not closed, update FD status and timeout for writes */
800 if (ob->flags & BF_OUT_EMPTY) {
801 /* stop writing */
802 if (!(si->flags & SI_FL_WAIT_DATA)) {
803 if ((ob->flags & (BF_FULL|BF_HIJACK|BF_SHUTW_NOW)) == 0)
804 si->flags |= SI_FL_WAIT_DATA;
805 conn_data_stop_send(&si->conn);
806 ob->wex = TICK_ETERNITY;
807 }
808 }
809 else {
810 /* (re)start writing and update timeout. Note: we don't recompute the timeout
811 * everytime we get here, otherwise it would risk never to expire. We only
812 * update it if is was not yet set. The stream socket handler will already
813 * have updated it if there has been a completed I/O.
814 */
815 si->flags &= ~SI_FL_WAIT_DATA;
816 conn_data_want_send(&si->conn);
817 if (!tick_isset(ob->wex)) {
818 ob->wex = tick_add_ifset(now_ms, ob->wto);
819 if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
820 /* Note: depending on the protocol, we don't know if we're waiting
821 * for incoming data or not. So in order to prevent the socket from
822 * expiring read timeouts during writes, we refresh the read timeout,
823 * except if it was already infinite or if we have explicitly setup
824 * independent streams.
825 */
826 ib->rex = tick_add_ifset(now_ms, ib->rto);
827 }
828 }
829 }
830 }
831}
832
Willy Tarreau46a8d922012-08-20 12:38:36 +0200833/* This function is used for inter-stream-interface calls. It is called by the
834 * consumer to inform the producer side that it may be interested in checking
835 * for free space in the buffer. Note that it intentionally does not update
836 * timeouts, so that we can still check them later at wake-up. This function is
837 * dedicated to connection-based stream interfaces.
838 */
839void stream_int_chk_rcv_conn(struct stream_interface *si)
840{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200841 struct channel *ib = si->ib;
Willy Tarreau46a8d922012-08-20 12:38:36 +0200842
843 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
844 return;
845
846 if (si->conn.flags & CO_FL_HANDSHAKE) {
847 /* a handshake is in progress */
848 return;
849 }
850
851 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
852 /* stop reading */
853 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
854 si->flags |= SI_FL_WAIT_ROOM;
855 conn_data_stop_recv(&si->conn);
856 }
857 else {
858 /* (re)start reading */
859 si->flags &= ~SI_FL_WAIT_ROOM;
860 conn_data_want_recv(&si->conn);
861 }
862}
863
864
Willy Tarreaude5722c2012-08-20 15:01:10 +0200865/* This function is used for inter-stream-interface calls. It is called by the
866 * producer to inform the consumer side that it may be interested in checking
867 * for data in the buffer. Note that it intentionally does not update timeouts,
868 * so that we can still check them later at wake-up.
869 */
870void stream_int_chk_snd_conn(struct stream_interface *si)
871{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200872 struct channel *ob = si->ob;
Willy Tarreaude5722c2012-08-20 15:01:10 +0200873
874 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
875 return;
876
877 /* handshake running on producer */
878 if (si->conn.flags & CO_FL_HANDSHAKE) {
879 /* a handshake is in progress */
880 si->flags &= ~SI_FL_WAIT_DATA;
881 return;
882 }
883
884 if (unlikely((ob->flags & BF_OUT_EMPTY))) /* called with nothing to send ! */
885 return;
886
887 if (!ob->pipe && /* spliced data wants to be forwarded ASAP */
888 (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
889 (fdtab[si_fd(si)].ev & FD_POLL_OUT))) /* we'll be called anyway */
890 return;
891
Willy Tarreau5368d802012-08-21 18:22:06 +0200892 if (si_conn_send_loop(&si->conn) < 0) {
Willy Tarreaude5722c2012-08-20 15:01:10 +0200893 /* Write error on the file descriptor. We mark the FD as STERROR so
894 * that we don't use it anymore and we notify the task.
895 */
896 fdtab[si_fd(si)].ev &= ~FD_POLL_STICKY;
Willy Tarreauf16723e2012-08-24 12:52:22 +0200897 __conn_data_stop_both(&si->conn);
Willy Tarreaude5722c2012-08-20 15:01:10 +0200898 si->flags |= SI_FL_ERR;
899 si->conn.flags |= CO_FL_ERROR;
900 goto out_wakeup;
901 }
902
903 /* OK, so now we know that some data might have been sent, and that we may
904 * have to poll first. We have to do that too if the buffer is not empty.
905 */
906 if (ob->flags & BF_OUT_EMPTY) {
907 /* the connection is established but we can't write. Either the
908 * buffer is empty, or we just refrain from sending because the
909 * ->o limit was reached. Maybe we just wrote the last
910 * chunk and need to close.
911 */
912 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
913 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
914 (si->state == SI_ST_EST)) {
915 si_shutw(si);
916 goto out_wakeup;
917 }
918
919 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
920 si->flags |= SI_FL_WAIT_DATA;
921 ob->wex = TICK_ETERNITY;
922 }
923 else {
924 /* Otherwise there are remaining data to be sent in the buffer,
925 * which means we have to poll before doing so.
926 */
Willy Tarreauf16723e2012-08-24 12:52:22 +0200927 __conn_data_want_send(&si->conn);
Willy Tarreaude5722c2012-08-20 15:01:10 +0200928 si->flags &= ~SI_FL_WAIT_DATA;
929 if (!tick_isset(ob->wex))
930 ob->wex = tick_add_ifset(now_ms, ob->wto);
931 }
932
933 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
934 /* update timeout if we have written something */
935 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
936 ob->wex = tick_add_ifset(now_ms, ob->wto);
937
938 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
939 /* Note: to prevent the client from expiring read timeouts
940 * during writes, we refresh it. We only do this if the
941 * interface is not configured for "independent streams",
942 * because for some applications it's better not to do this,
943 * for instance when continuously exchanging small amounts
944 * of data which can full the socket buffers long before a
945 * write timeout is detected.
946 */
947 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
948 }
949 }
950
951 /* in case of special condition (error, shutdown, end of write...), we
952 * have to notify the task.
953 */
954 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
955 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
956 si->state != SI_ST_EST)) {
957 out_wakeup:
958 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
959 task_wakeup(si->owner, TASK_WOKEN_IO);
960 }
Willy Tarreauf16723e2012-08-24 12:52:22 +0200961
962 /* commit possible polling changes */
963 conn_cond_update_polling(&si->conn);
Willy Tarreaude5722c2012-08-20 15:01:10 +0200964}
965
Willy Tarreaueecf6ca2012-08-20 15:09:53 +0200966/*
Willy Tarreauce323de2012-08-20 21:41:06 +0200967 * This is the callback which is called by the connection layer to receive data
968 * into the buffer from the connection. It iterates over the data layer's rcv_buf
969 * function.
970 */
971void si_conn_recv_cb(struct connection *conn)
972{
973 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
974 struct channel *b = si->ib;
975 int ret, max, cur_read;
976 int read_poll = MAX_READ_POLL_LOOPS;
977
978 /* stop immediately on errors. Note that we DON'T want to stop on
979 * POLL_ERR, as the poller might report a write error while there
980 * are still data available in the recv buffer. This typically
981 * happens when we send too large a request to a backend server
982 * which rejects it before reading it all.
983 */
984 if (conn->flags & CO_FL_ERROR)
985 goto out_error;
986
987 /* stop here if we reached the end of data */
988 if (conn_data_read0_pending(conn))
989 goto out_shutdown_r;
990
991 /* maybe we were called immediately after an asynchronous shutr */
992 if (b->flags & BF_SHUTR)
993 return;
994
Willy Tarreau96199b12012-08-24 00:46:52 +0200995 cur_read = 0;
996 conn->flags &= ~(CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM);
997
998 /* First, let's see if we may splice data across the channel without
999 * using a buffer.
1000 */
1001 if (conn->data->rcv_pipe &&
1002 b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
1003 if (buffer_not_empty(&b->buf)) {
1004 /* We're embarrassed, there are already data pending in
1005 * the buffer and we don't want to have them at two
1006 * locations at a time. Let's indicate we need some
1007 * place and ask the consumer to hurry.
1008 */
1009 goto abort_splice;
1010 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001011
Willy Tarreau96199b12012-08-24 00:46:52 +02001012 if (unlikely(b->pipe == NULL)) {
1013 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
1014 b->flags &= ~BF_KERN_SPLICING;
1015 goto abort_splice;
1016 }
1017 }
1018
1019 ret = conn->data->rcv_pipe(conn, b->pipe, b->to_forward);
1020 if (ret < 0) {
1021 /* splice not supported on this end, let's disable it */
1022 b->flags &= ~BF_KERN_SPLICING;
1023 si->flags &= ~SI_FL_CAP_SPLICE;
1024 goto abort_splice;
1025 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001026
Willy Tarreau96199b12012-08-24 00:46:52 +02001027 if (ret > 0) {
1028 if (b->to_forward != BUF_INFINITE_FORWARD)
1029 b->to_forward -= ret;
1030 b->total += ret;
1031 cur_read += ret;
1032 b->flags |= BF_READ_PARTIAL;
1033 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreauce323de2012-08-20 21:41:06 +02001034 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001035
1036 if (conn_data_read0_pending(conn))
1037 goto out_shutdown_r;
1038
1039 if (conn->flags & CO_FL_ERROR)
1040 goto out_error;
1041
Willy Tarreauce323de2012-08-20 21:41:06 +02001042 /* splice not possible (anymore), let's go on on standard copy */
1043 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001044
1045 abort_splice:
1046 /* release the pipe if we can, which is almost always the case */
1047 if (b->pipe && !b->pipe->data) {
1048 put_pipe(b->pipe);
1049 b->pipe = NULL;
1050 }
1051
1052 while (!b->pipe && !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH | CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM | CO_FL_HANDSHAKE))) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001053 max = bi_avail(b);
1054
1055 if (!max) {
1056 b->flags |= BF_FULL;
Willy Tarreau96199b12012-08-24 00:46:52 +02001057 conn->flags |= CO_FL_WAIT_ROOM;
Willy Tarreauce323de2012-08-20 21:41:06 +02001058 break;
1059 }
1060
1061 ret = conn->data->rcv_buf(conn, &b->buf, max);
1062 if (ret <= 0)
1063 break;
1064
1065 cur_read += ret;
1066
1067 /* if we're allowed to directly forward data, we must update ->o */
1068 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
1069 unsigned long fwd = ret;
1070 if (b->to_forward != BUF_INFINITE_FORWARD) {
1071 if (fwd > b->to_forward)
1072 fwd = b->to_forward;
1073 b->to_forward -= fwd;
1074 }
1075 b_adv(b, fwd);
1076 }
1077
1078 if (conn->flags & CO_FL_WAIT_L4_CONN)
1079 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1080
1081 b->flags |= BF_READ_PARTIAL;
1082 b->total += ret;
1083
1084 if (bi_full(b)) {
1085 /* The buffer is now full, there's no point in going through
1086 * the loop again.
1087 */
1088 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(&b->buf))) {
1089 b->xfer_small = 0;
1090 b->xfer_large++;
1091 if (b->xfer_large >= 3) {
1092 /* we call this buffer a fast streamer if it manages
1093 * to be filled in one call 3 consecutive times.
1094 */
1095 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
1096 //fputc('+', stderr);
1097 }
1098 }
1099 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
1100 (cur_read <= b->buf.size / 2)) {
1101 b->xfer_large = 0;
1102 b->xfer_small++;
1103 if (b->xfer_small >= 2) {
1104 /* if the buffer has been at least half full twice,
1105 * we receive faster than we send, so at least it
1106 * is not a "fast streamer".
1107 */
1108 b->flags &= ~BF_STREAMER_FAST;
1109 //fputc('-', stderr);
1110 }
1111 }
1112 else {
1113 b->xfer_small = 0;
1114 b->xfer_large = 0;
1115 }
1116
1117 b->flags |= BF_FULL;
1118 si->flags |= SI_FL_WAIT_ROOM;
1119 break;
1120 }
1121
1122 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
1123 break;
1124
1125 /* if too many bytes were missing from last read, it means that
1126 * it's pointless trying to read again because the system does
1127 * not have them in buffers.
1128 */
1129 if (ret < max) {
1130 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
1131 (cur_read <= b->buf.size / 2)) {
1132 b->xfer_large = 0;
1133 b->xfer_small++;
1134 if (b->xfer_small >= 3) {
1135 /* we have read less than half of the buffer in
1136 * one pass, and this happened at least 3 times.
1137 * This is definitely not a streamer.
1138 */
1139 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
1140 //fputc('!', stderr);
1141 }
1142 }
1143
1144 /* if a streamer has read few data, it may be because we
1145 * have exhausted system buffers. It's not worth trying
1146 * again.
1147 */
1148 if (b->flags & BF_STREAMER)
1149 break;
1150
1151 /* if we read a large block smaller than what we requested,
1152 * it's almost certain we'll never get anything more.
1153 */
1154 if (ret >= global.tune.recv_enough)
1155 break;
1156 }
1157 } /* while !flags */
1158
Willy Tarreau96199b12012-08-24 00:46:52 +02001159 if (conn->flags & CO_FL_ERROR)
1160 goto out_error;
1161
1162 if (conn->flags & CO_FL_WAIT_ROOM) {
Willy Tarreau2c052082012-08-24 12:53:56 +02001163 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau96199b12012-08-24 00:46:52 +02001164 }
1165 else if (conn->flags & CO_FL_WAIT_DATA) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001166 /* we don't automatically ask for polling if we have
1167 * read enough data, as it saves some syscalls with
1168 * speculative pollers.
1169 */
1170 if (cur_read < MIN_RET_FOR_READ_LOOP)
1171 __conn_data_poll_recv(conn);
1172 else
1173 __conn_data_want_recv(conn);
1174 }
1175
Willy Tarreauce323de2012-08-20 21:41:06 +02001176 if (conn_data_read0_pending(conn))
1177 /* connection closed */
1178 goto out_shutdown_r;
1179
1180 return;
1181
1182 out_shutdown_r:
1183 /* we received a shutdown */
1184 b->flags |= BF_READ_NULL;
1185 if (b->flags & BF_AUTO_CLOSE)
1186 buffer_shutw_now(b);
1187 stream_sock_read0(si);
1188 conn_data_read0(conn);
1189 return;
1190
1191 out_error:
1192 /* Read error on the connection, report the error and stop I/O */
1193 conn->flags |= CO_FL_ERROR;
Willy Tarreauf16723e2012-08-24 12:52:22 +02001194 __conn_data_stop_both(conn);
Willy Tarreauce323de2012-08-20 21:41:06 +02001195}
1196
1197/*
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001198 * This is the callback which is called by the connection layer to send data
1199 * from the buffer to the connection. It iterates over the data layer's snd_buf
1200 * function.
1201 */
1202void si_conn_send_cb(struct connection *conn)
1203{
1204 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001205 struct channel *b = si->ob;
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001206
1207 if (conn->flags & CO_FL_ERROR)
1208 goto out_error;
1209
1210 if (si->conn.flags & CO_FL_HANDSHAKE)
1211 /* a handshake was requested */
1212 return;
1213
1214 /* we might have been called just after an asynchronous shutw */
1215 if (b->flags & BF_SHUTW)
1216 return;
1217
1218 /* OK there are data waiting to be sent */
Willy Tarreau5368d802012-08-21 18:22:06 +02001219 if (si_conn_send_loop(conn) < 0)
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001220 goto out_error;
1221
1222 /* OK all done */
1223 return;
1224
1225 out_error:
1226 /* Write error on the connection, report the error and stop I/O */
1227 conn->flags |= CO_FL_ERROR;
Willy Tarreauf16723e2012-08-24 12:52:22 +02001228 __conn_data_stop_both(conn);
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001229}
1230
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001231/*
1232 * This function propagates a null read received on a socket-based connection.
1233 * It updates the stream interface. If the stream interface has SI_FL_NOHALF,
1234 * the close is also forwarded to the write side as an abort. This function is
1235 * still socket-specific as it handles a setsockopt() call to set the SO_LINGER
1236 * state on the socket.
1237 */
1238void stream_sock_read0(struct stream_interface *si)
1239{
1240 si->ib->flags &= ~BF_SHUTR_NOW;
1241 if (si->ib->flags & BF_SHUTR)
1242 return;
1243 si->ib->flags |= BF_SHUTR;
1244 si->ib->rex = TICK_ETERNITY;
1245 si->flags &= ~SI_FL_WAIT_ROOM;
1246
1247 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
1248 return;
1249
1250 if (si->ob->flags & BF_SHUTW)
1251 goto do_close;
1252
1253 if (si->flags & SI_FL_NOHALF) {
1254 /* we want to immediately forward this close to the write side */
1255 if (si->flags & SI_FL_NOLINGER) {
1256 si->flags &= ~SI_FL_NOLINGER;
1257 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
1258 (struct linger *) &nolinger, sizeof(struct linger));
1259 }
1260 /* force flag on ssl to keep session in cache */
1261 if (si->conn.data->shutw)
1262 si->conn.data->shutw(&si->conn, 0);
1263 goto do_close;
1264 }
1265
1266 /* otherwise that's just a normal read shutdown */
Willy Tarreauf16723e2012-08-24 12:52:22 +02001267 __conn_data_stop_recv(&si->conn);
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001268 return;
1269
1270 do_close:
1271 conn_data_close(&si->conn);
1272 fd_delete(si_fd(si));
1273 si->state = SI_ST_DIS;
1274 si->exp = TICK_ETERNITY;
1275 if (si->release)
1276 si->release(si);
1277 return;
1278}
1279
Willy Tarreaude5722c2012-08-20 15:01:10 +02001280
Willy Tarreaudded32d2008-11-30 19:48:07 +01001281/*
Willy Tarreaucff64112008-11-03 06:26:53 +01001282 * Local variables:
1283 * c-indent-level: 8
1284 * c-basic-offset: 8
1285 * End:
1286 */