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); |
| 42 | static void stream_int_shutr(struct stream_interface *si); |
| 43 | static void stream_int_shutw(struct stream_interface *si); |
| 44 | static void stream_int_chk_rcv(struct stream_interface *si); |
| 45 | static void stream_int_chk_snd(struct stream_interface *si); |
| 46 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 47 | /* socket operations for embedded tasks */ |
| 48 | struct sock_ops stream_int_embedded = { |
| 49 | .update = stream_int_update_embedded, |
| 50 | .shutr = stream_int_shutr, |
| 51 | .shutw = stream_int_shutw, |
| 52 | .chk_rcv = stream_int_chk_rcv, |
| 53 | .chk_snd = stream_int_chk_snd, |
| 54 | .read = NULL, |
| 55 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 56 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /* socket operations for external tasks */ |
| 60 | struct sock_ops stream_int_task = { |
| 61 | .update = stream_int_update, |
| 62 | .shutr = stream_int_shutr, |
| 63 | .shutw = stream_int_shutw, |
| 64 | .chk_rcv = stream_int_chk_rcv, |
| 65 | .chk_snd = stream_int_chk_snd, |
| 66 | .read = NULL, |
| 67 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 68 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 71 | /* |
| 72 | * This function only has to be called once after a wakeup event in case of |
| 73 | * suspected timeout. It controls the stream interface timeouts and sets |
| 74 | * si->flags accordingly. It does NOT close anything, as this timeout may |
| 75 | * be used for any purpose. It returns 1 if the timeout fired, otherwise |
| 76 | * zero. |
| 77 | */ |
| 78 | int stream_int_check_timeouts(struct stream_interface *si) |
| 79 | { |
| 80 | if (tick_is_expired(si->exp, now_ms)) { |
| 81 | si->flags |= SI_FL_EXP; |
| 82 | return 1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 87 | /* 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] | 88 | void stream_int_report_error(struct stream_interface *si) |
| 89 | { |
| 90 | if (!si->err_type) |
| 91 | si->err_type = SI_ET_DATA_ERR; |
| 92 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 93 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 94 | si->ib->flags |= BF_READ_ERROR; |
| 95 | } |
| 96 | |
| 97 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 98 | * Returns a message to the client ; the connection is shut down for read, |
| 99 | * and the request is cleared so that no server connection can be initiated. |
| 100 | * The buffer is marked for read shutdown on the other side to protect the |
| 101 | * message, and the buffer write is enabled. The message is contained in a |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 102 | * "chunk". If it is null, then an empty message is used. The reply buffer does |
| 103 | * not need to be empty before this, and its contents will not be overwritten. |
| 104 | * The primary goal of this function is to return error messages to a client. |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 105 | */ |
| 106 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 107 | { |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 108 | buffer_auto_read(si->ib); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 109 | buffer_abort(si->ib); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 110 | buffer_auto_close(si->ib); |
| 111 | buffer_erase(si->ib); |
Willy Tarreau | 798e128 | 2010-12-12 13:06:00 +0100 | [diff] [blame] | 112 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 113 | bi_erase(si->ob); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 114 | if (likely(msg && msg->len)) |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 115 | bo_inject(si->ob, msg->str, msg->len); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 116 | |
| 117 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 118 | buffer_auto_read(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 119 | buffer_auto_close(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 120 | buffer_shutr_now(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 123 | /* default update function for scheduled tasks, not used for embedded tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 124 | static void stream_int_update(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 125 | { |
| 126 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 127 | __FUNCTION__, |
| 128 | si, si->state, si->ib->flags, si->ob->flags); |
| 129 | |
| 130 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 131 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 132 | } |
| 133 | |
| 134 | /* default update function for embedded tasks, to be used at the end of the i/o handler */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 135 | static void stream_int_update_embedded(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 136 | { |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 137 | int old_flags = si->flags; |
| 138 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 139 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 140 | __FUNCTION__, |
| 141 | si, si->state, si->ib->flags, si->ob->flags); |
| 142 | |
| 143 | if (si->state != SI_ST_EST) |
| 144 | return; |
| 145 | |
| 146 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)) |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 147 | si_shutw(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 148 | |
| 149 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 150 | si->flags |= SI_FL_WAIT_DATA; |
| 151 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 152 | /* we're almost sure that we need some space if the buffer is not |
| 153 | * empty, even if it's not full, because the applets can't fill it. |
| 154 | */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 155 | 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] | 156 | si->flags |= SI_FL_WAIT_ROOM; |
| 157 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 158 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 159 | if (tick_isset(si->ob->wex)) |
| 160 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 161 | } |
| 162 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 163 | if (si->ib->flags & BF_READ_ACTIVITY || |
| 164 | (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) { |
| 165 | if (tick_isset(si->ib->rex)) |
| 166 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 167 | } |
| 168 | |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 169 | /* save flags to detect changes */ |
| 170 | old_flags = si->flags; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 171 | 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] | 172 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 173 | si_chk_rcv(si->ob->prod); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 175 | 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] | 176 | (si->ib->cons->flags & SI_FL_WAIT_DATA)) { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 177 | si_chk_snd(si->ib->cons); |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 178 | /* check if the consumer has freed some space */ |
| 179 | if (!(si->ib->flags & BF_FULL)) |
| 180 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 181 | } |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 182 | |
| 183 | /* Note that we're trying to wake up in two conditions here : |
| 184 | * - special event, which needs the holder task attention |
| 185 | * - status indicating that the applet can go on working. This |
| 186 | * is rather hard because we might be blocking on output and |
| 187 | * don't want to wake up on input and vice-versa. The idea is |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 188 | * to only rely on the changes the chk_* might have performed. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 189 | */ |
| 190 | if (/* check stream interface changes */ |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 191 | ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) || |
| 192 | |
| 193 | /* changes on the production side */ |
| 194 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) || |
| 195 | si->state != SI_ST_EST || |
| 196 | (si->flags & SI_FL_ERR) || |
| 197 | ((si->ib->flags & BF_READ_PARTIAL) && |
| 198 | (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) || |
| 199 | |
| 200 | /* changes on the consumption side */ |
| 201 | (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) || |
| 202 | ((si->ob->flags & BF_WRITE_ACTIVITY) && |
| 203 | ((si->ob->flags & BF_SHUTW) || |
| 204 | si->ob->prod->state != SI_ST_EST || |
| 205 | ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 206 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 207 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 208 | } |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 209 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 210 | si->ib->flags &= ~BF_READ_DONTWAIT; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* default shutr function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 214 | static void stream_int_shutr(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 215 | { |
| 216 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 217 | __FUNCTION__, |
| 218 | si, si->state, si->ib->flags, si->ob->flags); |
| 219 | |
| 220 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 221 | if (si->ib->flags & BF_SHUTR) |
| 222 | return; |
| 223 | si->ib->flags |= BF_SHUTR; |
| 224 | si->ib->rex = TICK_ETERNITY; |
| 225 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 226 | |
| 227 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 228 | return; |
| 229 | |
| 230 | if (si->ob->flags & BF_SHUTW) { |
| 231 | si->state = SI_ST_DIS; |
| 232 | si->exp = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 234 | conn_data_close(&si->conn); |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 235 | if (si->release) |
| 236 | si->release(si); |
| 237 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 238 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 239 | /* note that if the task exist, it must unregister itself once it runs */ |
| 240 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 241 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 242 | } |
| 243 | |
| 244 | /* default shutw function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 245 | static void stream_int_shutw(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 246 | { |
| 247 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 248 | __FUNCTION__, |
| 249 | si, si->state, si->ib->flags, si->ob->flags); |
| 250 | |
| 251 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 252 | if (si->ob->flags & BF_SHUTW) |
| 253 | return; |
| 254 | si->ob->flags |= BF_SHUTW; |
| 255 | si->ob->wex = TICK_ETERNITY; |
| 256 | si->flags &= ~SI_FL_WAIT_DATA; |
| 257 | |
| 258 | switch (si->state) { |
| 259 | case SI_ST_EST: |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 260 | if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | /* fall through */ |
| 264 | case SI_ST_CON: |
| 265 | case SI_ST_CER: |
Willy Tarreau | 32d3ee9 | 2010-12-29 14:03:02 +0100 | [diff] [blame] | 266 | case SI_ST_QUE: |
| 267 | case SI_ST_TAR: |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 268 | si->state = SI_ST_DIS; |
| 269 | /* fall through */ |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 270 | |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 271 | conn_data_close(&si->conn); |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 272 | if (si->release) |
| 273 | si->release(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 274 | default: |
| 275 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 276 | si->ib->flags |= BF_SHUTR; |
| 277 | si->ib->rex = TICK_ETERNITY; |
| 278 | si->exp = TICK_ETERNITY; |
| 279 | } |
| 280 | |
| 281 | /* note that if the task exist, it must unregister itself once it runs */ |
| 282 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 283 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 284 | } |
| 285 | |
| 286 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 287 | static void stream_int_chk_rcv(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 288 | { |
| 289 | struct buffer *ib = si->ib; |
| 290 | |
| 291 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 292 | __FUNCTION__, |
| 293 | si, si->state, si->ib->flags, si->ob->flags); |
| 294 | |
| 295 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 296 | return; |
| 297 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 298 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 299 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 300 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 301 | si->flags |= SI_FL_WAIT_ROOM; |
| 302 | } |
| 303 | else { |
| 304 | /* (re)start reading */ |
| 305 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 306 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 307 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* default chk_snd function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 312 | static void stream_int_chk_snd(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 313 | { |
| 314 | struct buffer *ob = si->ob; |
| 315 | |
| 316 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 317 | __FUNCTION__, |
| 318 | si, si->state, si->ib->flags, si->ob->flags); |
| 319 | |
| 320 | if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW))) |
| 321 | return; |
| 322 | |
| 323 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 324 | (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
| 325 | return; |
| 326 | |
| 327 | /* Otherwise there are remaining data to be sent in the buffer, |
| 328 | * so we tell the handler. |
| 329 | */ |
| 330 | si->flags &= ~SI_FL_WAIT_DATA; |
| 331 | if (!tick_isset(ob->wex)) |
| 332 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 333 | |
| 334 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 335 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 336 | } |
| 337 | |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 338 | /* 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] | 339 | * 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] | 340 | * 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] | 341 | * function before returning. It must be deleted by the task handler using |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 342 | * stream_int_unregister_handler(), possibly from within the function itself. |
Willy Tarreau | fa6bac6 | 2012-05-31 14:16:59 +0200 | [diff] [blame] | 343 | * It also pre-initializes applet.state to zero and the connection context |
| 344 | * to NULL. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 345 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 346 | 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] | 347 | { |
Simon Horman | 7abd00d | 2011-08-13 08:03:51 +0900 | [diff] [blame] | 348 | 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] | 349 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 350 | stream_interface_prepare(si, &stream_int_embedded); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 351 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 352 | set_target_applet(&si->target, app); |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 353 | si->release = app->release; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 354 | si->flags |= SI_FL_WAIT_DATA; |
| 355 | return si->owner; |
| 356 | } |
| 357 | |
| 358 | /* Register a function to handle a stream_interface as a standalone task. The |
| 359 | * new task itself is returned and is assigned as si->owner. The stream_interface |
| 360 | * pointer will be pointed to by the task's context. The handler can be detached |
| 361 | * by using stream_int_unregister_handler(). |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 362 | * FIXME: the code should be updated to ensure that we don't change si->owner |
| 363 | * anymore as this is not needed. However, process_session still relies on it. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 364 | */ |
| 365 | struct task *stream_int_register_handler_task(struct stream_interface *si, |
| 366 | struct task *(*fct)(struct task *)) |
| 367 | { |
| 368 | struct task *t; |
| 369 | |
| 370 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 371 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 372 | stream_interface_prepare(si, &stream_int_task); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 373 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 374 | clear_target(&si->target); |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 375 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 376 | si->flags |= SI_FL_WAIT_DATA; |
| 377 | |
| 378 | t = task_new(); |
| 379 | si->owner = t; |
| 380 | if (!t) |
| 381 | return t; |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 382 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 383 | set_target_task(&si->target, t); |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 385 | t->process = fct; |
| 386 | t->context = si; |
| 387 | task_wakeup(si->owner, TASK_WOKEN_INIT); |
| 388 | |
| 389 | return t; |
| 390 | } |
| 391 | |
| 392 | /* Unregister a stream interface handler. This must be called by the handler task |
| 393 | * itself when it detects that it is in the SI_ST_DIS state. This function can |
| 394 | * both detach standalone handlers and embedded handlers. |
| 395 | */ |
| 396 | void stream_int_unregister_handler(struct stream_interface *si) |
| 397 | { |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 398 | if (si->target.type == TARG_TYPE_TASK) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 399 | /* external handler : kill the task */ |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 400 | task_delete(si->target.ptr.t); |
| 401 | task_free(si->target.ptr.t); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 402 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 403 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 404 | si->owner = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 405 | clear_target(&si->target); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 408 | /* This callback is used to send a valid PROXY protocol line to a socket being |
| 409 | * established. It returns a combination of FD_WAIT_* if it wants some polling |
| 410 | * before being called again, otherwise it returns zero and removes itself from |
| 411 | * the connection's flags (the bit is provided in <flag> by the caller). |
| 412 | */ |
| 413 | int conn_si_send_proxy(struct connection *conn, unsigned int flag) |
| 414 | { |
| 415 | int fd = conn->t.sock.fd; |
| 416 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
| 417 | struct buffer *b = si->ob; |
| 418 | |
| 419 | /* we might have been called just after an asynchronous shutw */ |
| 420 | if (b->flags & BF_SHUTW) |
| 421 | goto out_error; |
| 422 | |
| 423 | /* If we have a PROXY line to send, we'll use this to validate the |
| 424 | * connection, in which case the connection is validated only once |
| 425 | * we've sent the whole proxy line. Otherwise we use connect(). |
| 426 | */ |
| 427 | if (si->send_proxy_ofs) { |
| 428 | int ret; |
| 429 | |
| 430 | /* The target server expects a PROXY line to be sent first. |
| 431 | * If the send_proxy_ofs is negative, it corresponds to the |
| 432 | * offset to start sending from then end of the proxy string |
| 433 | * (which is recomputed every time since it's constant). If |
| 434 | * it is positive, it means we have to send from the start. |
| 435 | */ |
| 436 | ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to); |
| 437 | if (!ret) |
| 438 | goto out_error; |
| 439 | |
| 440 | if (si->send_proxy_ofs > 0) |
| 441 | si->send_proxy_ofs = -ret; /* first call */ |
| 442 | |
| 443 | /* we have to send trash from (ret+sp for -sp bytes) */ |
| 444 | ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs, |
| 445 | (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE); |
| 446 | |
| 447 | if (ret == 0) |
| 448 | goto out_wait; |
| 449 | |
| 450 | if (ret < 0) { |
| 451 | if (errno == EAGAIN) |
| 452 | goto out_wait; |
| 453 | goto out_error; |
| 454 | } |
| 455 | |
| 456 | si->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 457 | if (si->send_proxy_ofs != 0) |
| 458 | goto out_wait; |
| 459 | |
| 460 | /* OK we've sent the whole line, we're connected */ |
| 461 | } |
| 462 | |
| 463 | /* The FD is ready now, simply return and let the connection handler |
| 464 | * notify upper layers if needed. |
| 465 | */ |
| 466 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 467 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 468 | b->flags |= BF_WRITE_NULL; |
| 469 | si->exp = TICK_ETERNITY; |
| 470 | |
| 471 | out_leave: |
| 472 | conn->flags &= ~flag; |
| 473 | return 0; |
| 474 | |
| 475 | out_error: |
| 476 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 477 | * that we don't use it anymore. The error is reported to the stream |
| 478 | * interface which will take proper action. We must not perturbate the |
| 479 | * buffer because the stream interface wants to ensure transparent |
| 480 | * connection retries. |
| 481 | */ |
| 482 | |
| 483 | conn->flags |= CO_FL_ERROR; |
| 484 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
| 485 | EV_FD_REM(fd); |
| 486 | goto out_leave; |
| 487 | |
| 488 | out_wait: |
| 489 | return FD_WAIT_WRITE; |
| 490 | } |
| 491 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 492 | /* function to be called on stream sockets after all I/O handlers */ |
| 493 | void stream_sock_update_conn(struct connection *conn) |
| 494 | { |
| 495 | int fd = conn->t.sock.fd; |
| 496 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
| 497 | |
| 498 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 499 | __FUNCTION__, |
| 500 | si, si->state, si->ib->flags, si->ob->flags); |
| 501 | |
Willy Tarreau | 3c55ec2 | 2012-07-23 19:19:51 +0200 | [diff] [blame] | 502 | if (conn->flags & CO_FL_ERROR) |
| 503 | si->flags |= SI_FL_ERR; |
| 504 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 505 | /* check for recent connection establishment */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 506 | 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] | 507 | si->exp = TICK_ETERNITY; |
| 508 | si->ob->flags |= BF_WRITE_NULL; |
| 509 | } |
| 510 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 511 | /* process consumer side, only once if possible */ |
| 512 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) { |
| 513 | if (si->ob->flags & BF_OUT_EMPTY) { |
| 514 | if (((si->ob->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) && |
| 515 | (si->state == SI_ST_EST)) |
| 516 | si_shutw(si); |
| 517 | EV_FD_CLR(fd, DIR_WR); |
| 518 | si->ob->wex = TICK_ETERNITY; |
| 519 | } |
| 520 | |
| 521 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 522 | si->flags |= SI_FL_WAIT_DATA; |
| 523 | |
| 524 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
| 525 | /* update timeouts if we have written something */ |
| 526 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL) |
| 527 | if (tick_isset(si->ob->wex)) |
| 528 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 529 | |
| 530 | if (!(si->flags & SI_FL_INDEP_STR)) |
| 531 | if (tick_isset(si->ib->rex)) |
| 532 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 533 | |
| 534 | if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL && |
| 535 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
| 536 | si_chk_rcv(si->ob->prod); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* process producer side, only once if possible */ |
| 541 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) { |
| 542 | /* We might have some data the consumer is waiting for. |
| 543 | * We can do fast-forwarding, but we avoid doing this for partial |
| 544 | * buffers, because it is very likely that it will be done again |
| 545 | * immediately afterwards once the following data is parsed (eg: |
| 546 | * HTTP chunking). |
| 547 | */ |
| 548 | if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) && |
| 549 | (si->ib->pipe /* always try to send spliced data */ || |
| 550 | (si->ib->i == 0 && (si->ib->cons->flags & SI_FL_WAIT_DATA)))) { |
| 551 | int last_len = si->ib->pipe ? si->ib->pipe->data : 0; |
| 552 | |
| 553 | si_chk_snd(si->ib->cons); |
| 554 | |
| 555 | /* check if the consumer has freed some space */ |
| 556 | if (!(si->ib->flags & BF_FULL) && |
| 557 | (!last_len || !si->ib->pipe || si->ib->pipe->data < last_len)) |
| 558 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 559 | } |
| 560 | |
| 561 | if (si->flags & SI_FL_WAIT_ROOM) { |
| 562 | EV_FD_CLR(fd, DIR_RD); |
| 563 | si->ib->rex = TICK_ETERNITY; |
| 564 | } |
| 565 | else if ((si->ib->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL) { |
| 566 | if (tick_isset(si->ib->rex)) |
| 567 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* wake the task up only when needed */ |
| 572 | if (/* changes on the production side */ |
| 573 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) || |
| 574 | si->state != SI_ST_EST || |
| 575 | (si->flags & SI_FL_ERR) || |
| 576 | ((si->ib->flags & BF_READ_PARTIAL) && |
| 577 | (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) || |
| 578 | |
| 579 | /* changes on the consumption side */ |
| 580 | (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) || |
| 581 | ((si->ob->flags & BF_WRITE_ACTIVITY) && |
| 582 | ((si->ob->flags & BF_SHUTW) || |
| 583 | si->ob->prod->state != SI_ST_EST || |
| 584 | ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) { |
| 585 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 586 | } |
| 587 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 588 | si->ib->flags &= ~BF_READ_DONTWAIT; |
| 589 | } |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 591 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 592 | * Local variables: |
| 593 | * c-indent-level: 8 |
| 594 | * c-basic-offset: 8 |
| 595 | * End: |
| 596 | */ |