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 | c63190d | 2012-05-11 14:23:52 +0200 | [diff] [blame] | 31 | #include <proto/sock_raw.h> |
Willy Tarreau | 269358d | 2009-09-20 20:14:49 +0200 | [diff] [blame] | 32 | #include <proto/stream_interface.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 33 | #include <proto/task.h> |
| 34 | |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 35 | /* socket functions used when running a stream interface as a task */ |
| 36 | static void stream_int_update(struct stream_interface *si); |
| 37 | static void stream_int_update_embedded(struct stream_interface *si); |
| 38 | static void stream_int_shutr(struct stream_interface *si); |
| 39 | static void stream_int_shutw(struct stream_interface *si); |
| 40 | static void stream_int_chk_rcv(struct stream_interface *si); |
| 41 | static void stream_int_chk_snd(struct stream_interface *si); |
| 42 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 43 | /* socket operations for embedded tasks */ |
| 44 | struct sock_ops stream_int_embedded = { |
Willy Tarreau | 64798bd | 2012-05-11 18:38:44 +0200 | [diff] [blame] | 45 | .init = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 46 | .update = stream_int_update_embedded, |
| 47 | .shutr = stream_int_shutr, |
| 48 | .shutw = stream_int_shutw, |
| 49 | .chk_rcv = stream_int_chk_rcv, |
| 50 | .chk_snd = stream_int_chk_snd, |
| 51 | .read = NULL, |
| 52 | .write = NULL, |
| 53 | }; |
| 54 | |
| 55 | /* socket operations for external tasks */ |
| 56 | struct sock_ops stream_int_task = { |
Willy Tarreau | 64798bd | 2012-05-11 18:38:44 +0200 | [diff] [blame] | 57 | .init = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 58 | .update = stream_int_update, |
| 59 | .shutr = stream_int_shutr, |
| 60 | .shutw = stream_int_shutw, |
| 61 | .chk_rcv = stream_int_chk_rcv, |
| 62 | .chk_snd = stream_int_chk_snd, |
| 63 | .read = NULL, |
| 64 | .write = NULL, |
| 65 | }; |
| 66 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 67 | /* |
| 68 | * This function only has to be called once after a wakeup event in case of |
| 69 | * suspected timeout. It controls the stream interface timeouts and sets |
| 70 | * si->flags accordingly. It does NOT close anything, as this timeout may |
| 71 | * be used for any purpose. It returns 1 if the timeout fired, otherwise |
| 72 | * zero. |
| 73 | */ |
| 74 | int stream_int_check_timeouts(struct stream_interface *si) |
| 75 | { |
| 76 | if (tick_is_expired(si->exp, now_ms)) { |
| 77 | si->flags |= SI_FL_EXP; |
| 78 | return 1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 83 | /* 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] | 84 | void stream_int_report_error(struct stream_interface *si) |
| 85 | { |
| 86 | if (!si->err_type) |
| 87 | si->err_type = SI_ET_DATA_ERR; |
| 88 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 89 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 90 | si->ib->flags |= BF_READ_ERROR; |
| 91 | } |
| 92 | |
| 93 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 94 | * Returns a message to the client ; the connection is shut down for read, |
| 95 | * and the request is cleared so that no server connection can be initiated. |
| 96 | * The buffer is marked for read shutdown on the other side to protect the |
| 97 | * 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] | 98 | * "chunk". If it is null, then an empty message is used. The reply buffer does |
| 99 | * not need to be empty before this, and its contents will not be overwritten. |
| 100 | * 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] | 101 | */ |
| 102 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 103 | { |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 104 | buffer_auto_read(si->ib); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 105 | buffer_abort(si->ib); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 106 | buffer_auto_close(si->ib); |
| 107 | buffer_erase(si->ib); |
Willy Tarreau | 798e128 | 2010-12-12 13:06:00 +0100 | [diff] [blame] | 108 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 109 | bi_erase(si->ob); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 110 | if (likely(msg && msg->len)) |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 111 | bo_inject(si->ob, msg->str, msg->len); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 112 | |
| 113 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 114 | buffer_auto_read(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 115 | buffer_auto_close(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 116 | buffer_shutr_now(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 119 | /* default update function for scheduled tasks, not used for embedded tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 120 | static void stream_int_update(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 121 | { |
| 122 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 123 | __FUNCTION__, |
| 124 | si, si->state, si->ib->flags, si->ob->flags); |
| 125 | |
| 126 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 127 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 128 | } |
| 129 | |
| 130 | /* 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] | 131 | static void stream_int_update_embedded(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 132 | { |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 133 | int old_flags = si->flags; |
| 134 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 135 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 136 | __FUNCTION__, |
| 137 | si, si->state, si->ib->flags, si->ob->flags); |
| 138 | |
| 139 | if (si->state != SI_ST_EST) |
| 140 | return; |
| 141 | |
| 142 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)) |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame] | 143 | si->sock.shutw(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 144 | |
| 145 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 146 | si->flags |= SI_FL_WAIT_DATA; |
| 147 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 148 | /* we're almost sure that we need some space if the buffer is not |
| 149 | * empty, even if it's not full, because the applets can't fill it. |
| 150 | */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 151 | 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] | 152 | si->flags |= SI_FL_WAIT_ROOM; |
| 153 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 154 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 155 | if (tick_isset(si->ob->wex)) |
| 156 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 157 | } |
| 158 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 159 | if (si->ib->flags & BF_READ_ACTIVITY || |
| 160 | (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) { |
| 161 | if (tick_isset(si->ib->rex)) |
| 162 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 163 | } |
| 164 | |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 165 | /* save flags to detect changes */ |
| 166 | old_flags = si->flags; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 167 | 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] | 168 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame] | 169 | si->ob->prod->sock.chk_rcv(si->ob->prod); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 171 | 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] | 172 | (si->ib->cons->flags & SI_FL_WAIT_DATA)) { |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame] | 173 | si->ib->cons->sock.chk_snd(si->ib->cons); |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 174 | /* check if the consumer has freed some space */ |
| 175 | if (!(si->ib->flags & BF_FULL)) |
| 176 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 177 | } |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 178 | |
| 179 | /* Note that we're trying to wake up in two conditions here : |
| 180 | * - special event, which needs the holder task attention |
| 181 | * - status indicating that the applet can go on working. This |
| 182 | * is rather hard because we might be blocking on output and |
| 183 | * 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] | 184 | * to only rely on the changes the chk_* might have performed. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 185 | */ |
| 186 | if (/* check stream interface changes */ |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 187 | ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) || |
| 188 | |
| 189 | /* changes on the production side */ |
| 190 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) || |
| 191 | si->state != SI_ST_EST || |
| 192 | (si->flags & SI_FL_ERR) || |
| 193 | ((si->ib->flags & BF_READ_PARTIAL) && |
| 194 | (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) || |
| 195 | |
| 196 | /* changes on the consumption side */ |
| 197 | (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) || |
| 198 | ((si->ob->flags & BF_WRITE_ACTIVITY) && |
| 199 | ((si->ob->flags & BF_SHUTW) || |
| 200 | si->ob->prod->state != SI_ST_EST || |
| 201 | ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 202 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 203 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 204 | } |
Willy Tarreau | 3488e25 | 2010-08-09 16:24:56 +0200 | [diff] [blame] | 205 | if (si->ib->flags & BF_READ_ACTIVITY) |
| 206 | si->ib->flags &= ~BF_READ_DONTWAIT; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* default shutr function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 210 | static void stream_int_shutr(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 211 | { |
| 212 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 213 | __FUNCTION__, |
| 214 | si, si->state, si->ib->flags, si->ob->flags); |
| 215 | |
| 216 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 217 | if (si->ib->flags & BF_SHUTR) |
| 218 | return; |
| 219 | si->ib->flags |= BF_SHUTR; |
| 220 | si->ib->rex = TICK_ETERNITY; |
| 221 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 222 | |
| 223 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 224 | return; |
| 225 | |
| 226 | if (si->ob->flags & BF_SHUTW) { |
| 227 | si->state = SI_ST_DIS; |
| 228 | si->exp = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 230 | if (si->release) |
| 231 | si->release(si); |
| 232 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 234 | /* note that if the task exist, it must unregister itself once it runs */ |
| 235 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 236 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 237 | } |
| 238 | |
| 239 | /* default shutw function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 240 | static void stream_int_shutw(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 241 | { |
| 242 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 243 | __FUNCTION__, |
| 244 | si, si->state, si->ib->flags, si->ob->flags); |
| 245 | |
| 246 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 247 | if (si->ob->flags & BF_SHUTW) |
| 248 | return; |
| 249 | si->ob->flags |= BF_SHUTW; |
| 250 | si->ob->wex = TICK_ETERNITY; |
| 251 | si->flags &= ~SI_FL_WAIT_DATA; |
| 252 | |
| 253 | switch (si->state) { |
| 254 | case SI_ST_EST: |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 255 | if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | /* fall through */ |
| 259 | case SI_ST_CON: |
| 260 | case SI_ST_CER: |
Willy Tarreau | 32d3ee9 | 2010-12-29 14:03:02 +0100 | [diff] [blame] | 261 | case SI_ST_QUE: |
| 262 | case SI_ST_TAR: |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 263 | si->state = SI_ST_DIS; |
| 264 | /* fall through */ |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 265 | |
| 266 | if (si->release) |
| 267 | si->release(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 268 | default: |
| 269 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 270 | si->ib->flags |= BF_SHUTR; |
| 271 | si->ib->rex = TICK_ETERNITY; |
| 272 | si->exp = TICK_ETERNITY; |
| 273 | } |
| 274 | |
| 275 | /* note that if the task exist, it must unregister itself once it runs */ |
| 276 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 277 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 278 | } |
| 279 | |
| 280 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 281 | static void stream_int_chk_rcv(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 282 | { |
| 283 | struct buffer *ib = si->ib; |
| 284 | |
| 285 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 286 | __FUNCTION__, |
| 287 | si, si->state, si->ib->flags, si->ob->flags); |
| 288 | |
| 289 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 290 | return; |
| 291 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 292 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 293 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 294 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 295 | si->flags |= SI_FL_WAIT_ROOM; |
| 296 | } |
| 297 | else { |
| 298 | /* (re)start reading */ |
| 299 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 300 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 301 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* default chk_snd function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 306 | static void stream_int_chk_snd(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 307 | { |
| 308 | struct buffer *ob = si->ob; |
| 309 | |
| 310 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 311 | __FUNCTION__, |
| 312 | si, si->state, si->ib->flags, si->ob->flags); |
| 313 | |
| 314 | if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW))) |
| 315 | return; |
| 316 | |
| 317 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 318 | (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
| 319 | return; |
| 320 | |
| 321 | /* Otherwise there are remaining data to be sent in the buffer, |
| 322 | * so we tell the handler. |
| 323 | */ |
| 324 | si->flags &= ~SI_FL_WAIT_DATA; |
| 325 | if (!tick_isset(ob->wex)) |
| 326 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 327 | |
| 328 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 329 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 330 | } |
| 331 | |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 332 | /* 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] | 333 | * 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] | 334 | * 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] | 335 | * function before returning. It must be deleted by the task handler using |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 336 | * stream_int_unregister_handler(), possibly from within the function itself. |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 337 | * It also pre-initializes applet.state to zero. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 338 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 339 | 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] | 340 | { |
Simon Horman | 7abd00d | 2011-08-13 08:03:51 +0900 | [diff] [blame] | 341 | 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] | 342 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 343 | stream_interface_prepare(si, &stream_int_embedded); |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 344 | si->proto = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 345 | set_target_applet(&si->target, app); |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 346 | si->applet.state = 0; |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 347 | si->release = app->release; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 348 | si->flags |= SI_FL_WAIT_DATA; |
| 349 | return si->owner; |
| 350 | } |
| 351 | |
| 352 | /* Register a function to handle a stream_interface as a standalone task. The |
| 353 | * new task itself is returned and is assigned as si->owner. The stream_interface |
| 354 | * pointer will be pointed to by the task's context. The handler can be detached |
| 355 | * by using stream_int_unregister_handler(). |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 356 | * FIXME: the code should be updated to ensure that we don't change si->owner |
| 357 | * 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] | 358 | */ |
| 359 | struct task *stream_int_register_handler_task(struct stream_interface *si, |
| 360 | struct task *(*fct)(struct task *)) |
| 361 | { |
| 362 | struct task *t; |
| 363 | |
| 364 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 365 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 366 | stream_interface_prepare(si, &stream_int_task); |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 367 | si->proto = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 368 | clear_target(&si->target); |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 369 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 370 | si->flags |= SI_FL_WAIT_DATA; |
| 371 | |
| 372 | t = task_new(); |
| 373 | si->owner = t; |
| 374 | if (!t) |
| 375 | return t; |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 376 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 377 | set_target_task(&si->target, t); |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 378 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 379 | t->process = fct; |
| 380 | t->context = si; |
| 381 | task_wakeup(si->owner, TASK_WOKEN_INIT); |
| 382 | |
| 383 | return t; |
| 384 | } |
| 385 | |
| 386 | /* Unregister a stream interface handler. This must be called by the handler task |
| 387 | * itself when it detects that it is in the SI_ST_DIS state. This function can |
| 388 | * both detach standalone handlers and embedded handlers. |
| 389 | */ |
| 390 | void stream_int_unregister_handler(struct stream_interface *si) |
| 391 | { |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 392 | if (si->target.type == TARG_TYPE_TASK) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 393 | /* external handler : kill the task */ |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 394 | task_delete(si->target.ptr.t); |
| 395 | task_free(si->target.ptr.t); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 396 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 397 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 398 | si->owner = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 399 | clear_target(&si->target); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 402 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 403 | * Local variables: |
| 404 | * c-indent-level: 8 |
| 405 | * c-basic-offset: 8 |
| 406 | * End: |
| 407 | */ |