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