Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing stream_interface structures |
| 3 | * |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 5 | * |
| 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 Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 30 | #include <proto/connection.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 31 | #include <proto/fd.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 32 | #include <proto/frontend.h> |
Willy Tarreau | c63190d | 2012-05-11 14:23:52 +0200 | [diff] [blame] | 33 | #include <proto/sock_raw.h> |
Willy Tarreau | 269358d | 2009-09-20 20:14:49 +0200 | [diff] [blame] | 34 | #include <proto/stream_interface.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 35 | #include <proto/task.h> |
| 36 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 37 | #include <types/pipe.h> |
| 38 | |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 39 | /* socket functions used when running a stream interface as a task */ |
| 40 | static void stream_int_update(struct stream_interface *si); |
| 41 | static void stream_int_update_embedded(struct stream_interface *si); |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 42 | static void stream_int_chk_rcv(struct stream_interface *si); |
| 43 | static void stream_int_chk_snd(struct stream_interface *si); |
| 44 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 45 | /* socket operations for embedded tasks */ |
| 46 | struct sock_ops stream_int_embedded = { |
| 47 | .update = stream_int_update_embedded, |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 48 | .shutr = NULL, |
| 49 | .shutw = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 50 | .chk_rcv = stream_int_chk_rcv, |
| 51 | .chk_snd = stream_int_chk_snd, |
| 52 | .read = NULL, |
| 53 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 54 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* socket operations for external tasks */ |
| 58 | struct sock_ops stream_int_task = { |
| 59 | .update = stream_int_update, |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 60 | .shutr = NULL, |
| 61 | .shutw = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 62 | .chk_rcv = stream_int_chk_rcv, |
| 63 | .chk_snd = stream_int_chk_snd, |
| 64 | .read = NULL, |
| 65 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 66 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 69 | /* |
| 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 | */ |
| 76 | int 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 Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 85 | /* to be called only when in SI_ST_DIS with SI_FL_ERR */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 86 | void stream_int_report_error(struct stream_interface *si) |
| 87 | { |
| 88 | if (!si->err_type) |
| 89 | si->err_type = SI_ET_DATA_ERR; |
| 90 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 91 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 92 | si->ib->flags |= BF_READ_ERROR; |
| 93 | } |
| 94 | |
| 95 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 96 | * 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 Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 100 | * "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 Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 103 | */ |
| 104 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 105 | { |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 106 | buffer_auto_read(si->ib); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 107 | buffer_abort(si->ib); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 108 | buffer_auto_close(si->ib); |
| 109 | buffer_erase(si->ib); |
Willy Tarreau | 798e128 | 2010-12-12 13:06:00 +0100 | [diff] [blame] | 110 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 111 | bi_erase(si->ob); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 112 | if (likely(msg && msg->len)) |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 113 | bo_inject(si->ob, msg->str, msg->len); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 114 | |
| 115 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 116 | buffer_auto_read(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 117 | buffer_auto_close(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 118 | buffer_shutr_now(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 121 | /* default update function for scheduled tasks, not used for embedded tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 122 | static void stream_int_update(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 123 | { |
| 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 Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 133 | static void stream_int_update_embedded(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 134 | { |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 135 | int old_flags = si->flags; |
| 136 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 137 | 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 Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 145 | si_shutw(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 146 | |
| 147 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 148 | si->flags |= SI_FL_WAIT_DATA; |
| 149 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 150 | /* 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 Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 153 | if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 154 | si->flags |= SI_FL_WAIT_ROOM; |
| 155 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 156 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 157 | if (tick_isset(si->ob->wex)) |
| 158 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 159 | } |
| 160 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 161 | 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 Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 167 | /* save flags to detect changes */ |
| 168 | old_flags = si->flags; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 169 | if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL && |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 170 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 171 | si_chk_rcv(si->ob->prod); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 172 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 173 | if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) && |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 174 | (si->ib->cons->flags & SI_FL_WAIT_DATA)) { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 175 | si_chk_snd(si->ib->cons); |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 176 | /* check if the consumer has freed some space */ |
| 177 | if (!(si->ib->flags & BF_FULL)) |
| 178 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 179 | } |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 180 | |
| 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 Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 186 | * to only rely on the changes the chk_* might have performed. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 187 | */ |
| 188 | if (/* check stream interface changes */ |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 189 | ((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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 204 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 205 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 206 | } |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 207 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 208 | si->ib->flags &= ~BF_READ_DONTWAIT; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 211 | /* |
| 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 | */ |
| 224 | int stream_int_shutr(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 225 | { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 226 | struct connection *conn = &si->conn; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 227 | |
| 228 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 229 | if (si->ib->flags & BF_SHUTR) |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 230 | return 0; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 231 | 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 Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 236 | return 0; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 237 | |
| 238 | if (si->ob->flags & BF_SHUTW) { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 239 | conn_data_close(&si->conn); |
| 240 | if (conn->ctrl) |
| 241 | fd_delete(si_fd(si)); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 242 | si->state = SI_ST_DIS; |
| 243 | si->exp = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 244 | |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 245 | if (si->release) |
| 246 | si->release(si); |
| 247 | } |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 248 | 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 Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 257 | /* 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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 259 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 260 | return 0; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 263 | /* |
| 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 | */ |
| 275 | int stream_int_shutw(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 276 | { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 277 | struct connection *conn = &si->conn; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 278 | |
| 279 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 280 | if (si->ob->flags & BF_SHUTW) |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 281 | return 0; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 282 | 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 Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 288 | /* 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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 324 | |
| 325 | /* fall through */ |
| 326 | case SI_ST_CON: |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 327 | /* 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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 334 | case SI_ST_CER: |
Willy Tarreau | 32d3ee9 | 2010-12-29 14:03:02 +0100 | [diff] [blame] | 335 | case SI_ST_QUE: |
| 336 | case SI_ST_TAR: |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 337 | si->state = SI_ST_DIS; |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 338 | |
| 339 | if (si->release) |
| 340 | si->release(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 341 | 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 Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 348 | /* 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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 350 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 351 | return 0; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 355 | static void stream_int_chk_rcv(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 356 | { |
| 357 | struct buffer *ib = si->ib; |
| 358 | |
| 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 Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 366 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 367 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 368 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 369 | 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 Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 380 | static void stream_int_chk_snd(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 381 | { |
| 382 | struct buffer *ob = si->ob; |
| 383 | |
| 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 Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 406 | /* Register an applet to handle a stream_interface as part of the stream |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 407 | * interface's owner task, which is returned. The SI will wake it up everytime |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 408 | * it is solicited. The task's processing function must call the applet's |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 409 | * function before returning. It must be deleted by the task handler using |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 410 | * stream_int_unregister_handler(), possibly from within the function itself. |
Willy Tarreau | fa6bac6 | 2012-05-31 14:16:59 +0200 | [diff] [blame] | 411 | * It also pre-initializes applet.state to zero and the connection context |
| 412 | * to NULL. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 413 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 414 | struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 415 | { |
Simon Horman | 7abd00d | 2011-08-13 08:03:51 +0900 | [diff] [blame] | 416 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 417 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 418 | stream_interface_prepare(si, &stream_int_embedded); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 419 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 420 | set_target_applet(&si->target, app); |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 421 | si->release = app->release; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 422 | 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 Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 430 | * 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 Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 432 | */ |
| 433 | struct 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 Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 440 | stream_interface_prepare(si, &stream_int_task); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 441 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 442 | clear_target(&si->target); |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 443 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 444 | si->flags |= SI_FL_WAIT_DATA; |
| 445 | |
| 446 | t = task_new(); |
| 447 | si->owner = t; |
| 448 | if (!t) |
| 449 | return t; |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 450 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 451 | set_target_task(&si->target, t); |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 452 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 453 | 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 | */ |
| 464 | void stream_int_unregister_handler(struct stream_interface *si) |
| 465 | { |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 466 | if (si->target.type == TARG_TYPE_TASK) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 467 | /* external handler : kill the task */ |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 468 | task_delete(si->target.ptr.t); |
| 469 | task_free(si->target.ptr.t); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 470 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 471 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 472 | si->owner = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 473 | clear_target(&si->target); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 476 | /* This callback is used to send a valid PROXY protocol line to a socket being |
| 477 | * established. It returns a combination of FD_WAIT_* if it wants some polling |
| 478 | * before being called again, otherwise it returns zero and removes itself from |
| 479 | * the connection's flags (the bit is provided in <flag> by the caller). |
| 480 | */ |
| 481 | int conn_si_send_proxy(struct connection *conn, unsigned int flag) |
| 482 | { |
| 483 | int fd = conn->t.sock.fd; |
| 484 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
| 485 | struct buffer *b = si->ob; |
| 486 | |
| 487 | /* we might have been called just after an asynchronous shutw */ |
| 488 | if (b->flags & BF_SHUTW) |
| 489 | goto out_error; |
| 490 | |
| 491 | /* If we have a PROXY line to send, we'll use this to validate the |
| 492 | * connection, in which case the connection is validated only once |
| 493 | * we've sent the whole proxy line. Otherwise we use connect(). |
| 494 | */ |
| 495 | if (si->send_proxy_ofs) { |
| 496 | int ret; |
| 497 | |
| 498 | /* The target server expects a PROXY line to be sent first. |
| 499 | * If the send_proxy_ofs is negative, it corresponds to the |
| 500 | * offset to start sending from then end of the proxy string |
| 501 | * (which is recomputed every time since it's constant). If |
| 502 | * it is positive, it means we have to send from the start. |
| 503 | */ |
| 504 | ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to); |
| 505 | if (!ret) |
| 506 | goto out_error; |
| 507 | |
| 508 | if (si->send_proxy_ofs > 0) |
| 509 | si->send_proxy_ofs = -ret; /* first call */ |
| 510 | |
| 511 | /* we have to send trash from (ret+sp for -sp bytes) */ |
| 512 | ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs, |
| 513 | (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE); |
| 514 | |
| 515 | if (ret == 0) |
| 516 | goto out_wait; |
| 517 | |
| 518 | if (ret < 0) { |
| 519 | if (errno == EAGAIN) |
| 520 | goto out_wait; |
| 521 | goto out_error; |
| 522 | } |
| 523 | |
| 524 | si->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 525 | if (si->send_proxy_ofs != 0) |
| 526 | goto out_wait; |
| 527 | |
| 528 | /* OK we've sent the whole line, we're connected */ |
| 529 | } |
| 530 | |
| 531 | /* The FD is ready now, simply return and let the connection handler |
| 532 | * notify upper layers if needed. |
| 533 | */ |
| 534 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 535 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 536 | b->flags |= BF_WRITE_NULL; |
| 537 | si->exp = TICK_ETERNITY; |
| 538 | |
| 539 | out_leave: |
| 540 | conn->flags &= ~flag; |
| 541 | return 0; |
| 542 | |
| 543 | out_error: |
| 544 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 545 | * that we don't use it anymore. The error is reported to the stream |
| 546 | * interface which will take proper action. We must not perturbate the |
| 547 | * buffer because the stream interface wants to ensure transparent |
| 548 | * connection retries. |
| 549 | */ |
| 550 | |
| 551 | conn->flags |= CO_FL_ERROR; |
| 552 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 553 | fd_stop_both(fd); |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 554 | goto out_leave; |
| 555 | |
| 556 | out_wait: |
| 557 | return FD_WAIT_WRITE; |
| 558 | } |
| 559 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 560 | /* function to be called on stream sockets after all I/O handlers */ |
| 561 | void stream_sock_update_conn(struct connection *conn) |
| 562 | { |
| 563 | int fd = conn->t.sock.fd; |
| 564 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
| 565 | |
| 566 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 567 | __FUNCTION__, |
| 568 | si, si->state, si->ib->flags, si->ob->flags); |
| 569 | |
Willy Tarreau | 3c55ec2 | 2012-07-23 19:19:51 +0200 | [diff] [blame] | 570 | if (conn->flags & CO_FL_ERROR) |
| 571 | si->flags |= SI_FL_ERR; |
| 572 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 573 | /* check for recent connection establishment */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 574 | if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) { |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 575 | si->exp = TICK_ETERNITY; |
| 576 | si->ob->flags |= BF_WRITE_NULL; |
| 577 | } |
| 578 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 579 | /* process consumer side, only once if possible */ |
| 580 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) { |
| 581 | if (si->ob->flags & BF_OUT_EMPTY) { |
| 582 | if (((si->ob->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) && |
| 583 | (si->state == SI_ST_EST)) |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 584 | stream_int_shutw(si); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 585 | fd_stop_send(fd); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 586 | si->ob->wex = TICK_ETERNITY; |
| 587 | } |
| 588 | |
| 589 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 590 | si->flags |= SI_FL_WAIT_DATA; |
| 591 | |
| 592 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
| 593 | /* update timeouts if we have written something */ |
| 594 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL) |
| 595 | if (tick_isset(si->ob->wex)) |
| 596 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 597 | |
| 598 | if (!(si->flags & SI_FL_INDEP_STR)) |
| 599 | if (tick_isset(si->ib->rex)) |
| 600 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 601 | |
| 602 | if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL && |
| 603 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
| 604 | si_chk_rcv(si->ob->prod); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* process producer side, only once if possible */ |
| 609 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) { |
| 610 | /* We might have some data the consumer is waiting for. |
| 611 | * We can do fast-forwarding, but we avoid doing this for partial |
| 612 | * buffers, because it is very likely that it will be done again |
| 613 | * immediately afterwards once the following data is parsed (eg: |
| 614 | * HTTP chunking). |
| 615 | */ |
| 616 | if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) && |
| 617 | (si->ib->pipe /* always try to send spliced data */ || |
| 618 | (si->ib->i == 0 && (si->ib->cons->flags & SI_FL_WAIT_DATA)))) { |
| 619 | int last_len = si->ib->pipe ? si->ib->pipe->data : 0; |
| 620 | |
| 621 | si_chk_snd(si->ib->cons); |
| 622 | |
| 623 | /* check if the consumer has freed some space */ |
| 624 | if (!(si->ib->flags & BF_FULL) && |
| 625 | (!last_len || !si->ib->pipe || si->ib->pipe->data < last_len)) |
| 626 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 627 | } |
| 628 | |
| 629 | if (si->flags & SI_FL_WAIT_ROOM) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 630 | fd_stop_recv(fd); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 631 | si->ib->rex = TICK_ETERNITY; |
| 632 | } |
| 633 | else if ((si->ib->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL) { |
| 634 | if (tick_isset(si->ib->rex)) |
| 635 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | /* wake the task up only when needed */ |
| 640 | if (/* changes on the production side */ |
| 641 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) || |
| 642 | si->state != SI_ST_EST || |
| 643 | (si->flags & SI_FL_ERR) || |
| 644 | ((si->ib->flags & BF_READ_PARTIAL) && |
| 645 | (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) || |
| 646 | |
| 647 | /* changes on the consumption side */ |
| 648 | (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) || |
| 649 | ((si->ob->flags & BF_WRITE_ACTIVITY) && |
| 650 | ((si->ob->flags & BF_SHUTW) || |
| 651 | si->ob->prod->state != SI_ST_EST || |
| 652 | ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) { |
| 653 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 654 | } |
| 655 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 656 | si->ib->flags &= ~BF_READ_DONTWAIT; |
| 657 | } |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 658 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 659 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 660 | * Local variables: |
| 661 | * c-indent-level: 8 |
| 662 | * c-basic-offset: 8 |
| 663 | * End: |
| 664 | */ |