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 = { |
| 45 | .update = stream_int_update_embedded, |
| 46 | .shutr = stream_int_shutr, |
| 47 | .shutw = stream_int_shutw, |
| 48 | .chk_rcv = stream_int_chk_rcv, |
| 49 | .chk_snd = stream_int_chk_snd, |
| 50 | .read = NULL, |
| 51 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 52 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* socket operations for external tasks */ |
| 56 | struct sock_ops stream_int_task = { |
| 57 | .update = stream_int_update, |
| 58 | .shutr = stream_int_shutr, |
| 59 | .shutw = stream_int_shutw, |
| 60 | .chk_rcv = stream_int_chk_rcv, |
| 61 | .chk_snd = stream_int_chk_snd, |
| 62 | .read = NULL, |
| 63 | .write = NULL, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 64 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 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 | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 143 | si_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 | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 169 | si_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 | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 173 | si_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 | 4da69a9 | 2012-05-21 18:05:40 +0200 | [diff] [blame] | 230 | si_data_close(si); |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 231 | if (si->release) |
| 232 | si->release(si); |
| 233 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 234 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 235 | /* note that if the task exist, it must unregister itself once it runs */ |
| 236 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 237 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 238 | } |
| 239 | |
| 240 | /* default shutw function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 241 | static void stream_int_shutw(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 242 | { |
| 243 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 244 | __FUNCTION__, |
| 245 | si, si->state, si->ib->flags, si->ob->flags); |
| 246 | |
| 247 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 248 | if (si->ob->flags & BF_SHUTW) |
| 249 | return; |
| 250 | si->ob->flags |= BF_SHUTW; |
| 251 | si->ob->wex = TICK_ETERNITY; |
| 252 | si->flags &= ~SI_FL_WAIT_DATA; |
| 253 | |
| 254 | switch (si->state) { |
| 255 | case SI_ST_EST: |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 256 | if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | /* fall through */ |
| 260 | case SI_ST_CON: |
| 261 | case SI_ST_CER: |
Willy Tarreau | 32d3ee9 | 2010-12-29 14:03:02 +0100 | [diff] [blame] | 262 | case SI_ST_QUE: |
| 263 | case SI_ST_TAR: |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 264 | si->state = SI_ST_DIS; |
| 265 | /* fall through */ |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 266 | |
Willy Tarreau | 4da69a9 | 2012-05-21 18:05:40 +0200 | [diff] [blame] | 267 | si_data_close(si); |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 268 | if (si->release) |
| 269 | si->release(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 270 | default: |
| 271 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 272 | si->ib->flags |= BF_SHUTR; |
| 273 | si->ib->rex = TICK_ETERNITY; |
| 274 | si->exp = TICK_ETERNITY; |
| 275 | } |
| 276 | |
| 277 | /* note that if the task exist, it must unregister itself once it runs */ |
| 278 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 279 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 280 | } |
| 281 | |
| 282 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 283 | static void stream_int_chk_rcv(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 284 | { |
| 285 | struct buffer *ib = si->ib; |
| 286 | |
| 287 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 288 | __FUNCTION__, |
| 289 | si, si->state, si->ib->flags, si->ob->flags); |
| 290 | |
| 291 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 292 | return; |
| 293 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 294 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 295 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 296 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 297 | si->flags |= SI_FL_WAIT_ROOM; |
| 298 | } |
| 299 | else { |
| 300 | /* (re)start reading */ |
| 301 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 302 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 303 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* default chk_snd function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 308 | static void stream_int_chk_snd(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 309 | { |
| 310 | struct buffer *ob = si->ob; |
| 311 | |
| 312 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 313 | __FUNCTION__, |
| 314 | si, si->state, si->ib->flags, si->ob->flags); |
| 315 | |
| 316 | if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW))) |
| 317 | return; |
| 318 | |
| 319 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 320 | (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
| 321 | return; |
| 322 | |
| 323 | /* Otherwise there are remaining data to be sent in the buffer, |
| 324 | * so we tell the handler. |
| 325 | */ |
| 326 | si->flags &= ~SI_FL_WAIT_DATA; |
| 327 | if (!tick_isset(ob->wex)) |
| 328 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 329 | |
| 330 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 331 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 332 | } |
| 333 | |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 334 | /* 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] | 335 | * 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] | 336 | * 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] | 337 | * function before returning. It must be deleted by the task handler using |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 338 | * stream_int_unregister_handler(), possibly from within the function itself. |
Willy Tarreau | fa6bac6 | 2012-05-31 14:16:59 +0200 | [diff] [blame] | 339 | * It also pre-initializes applet.state to zero and the connection context |
| 340 | * to NULL. |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 341 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 342 | 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] | 343 | { |
Simon Horman | 7abd00d | 2011-08-13 08:03:51 +0900 | [diff] [blame] | 344 | 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] | 345 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 346 | stream_interface_prepare(si, &stream_int_embedded); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 347 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 348 | set_target_applet(&si->target, app); |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 349 | si->release = app->release; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 350 | si->flags |= SI_FL_WAIT_DATA; |
| 351 | return si->owner; |
| 352 | } |
| 353 | |
| 354 | /* Register a function to handle a stream_interface as a standalone task. The |
| 355 | * new task itself is returned and is assigned as si->owner. The stream_interface |
| 356 | * pointer will be pointed to by the task's context. The handler can be detached |
| 357 | * by using stream_int_unregister_handler(). |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 358 | * FIXME: the code should be updated to ensure that we don't change si->owner |
| 359 | * 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] | 360 | */ |
| 361 | struct task *stream_int_register_handler_task(struct stream_interface *si, |
| 362 | struct task *(*fct)(struct task *)) |
| 363 | { |
| 364 | struct task *t; |
| 365 | |
| 366 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 367 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 368 | stream_interface_prepare(si, &stream_int_task); |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 369 | si->conn.ctrl = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 370 | clear_target(&si->target); |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 371 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 372 | si->flags |= SI_FL_WAIT_DATA; |
| 373 | |
| 374 | t = task_new(); |
| 375 | si->owner = t; |
| 376 | if (!t) |
| 377 | return t; |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 378 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 379 | set_target_task(&si->target, t); |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 380 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 381 | t->process = fct; |
| 382 | t->context = si; |
| 383 | task_wakeup(si->owner, TASK_WOKEN_INIT); |
| 384 | |
| 385 | return t; |
| 386 | } |
| 387 | |
| 388 | /* Unregister a stream interface handler. This must be called by the handler task |
| 389 | * itself when it detects that it is in the SI_ST_DIS state. This function can |
| 390 | * both detach standalone handlers and embedded handlers. |
| 391 | */ |
| 392 | void stream_int_unregister_handler(struct stream_interface *si) |
| 393 | { |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 394 | if (si->target.type == TARG_TYPE_TASK) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 395 | /* external handler : kill the task */ |
Willy Tarreau | 7c0a151 | 2011-03-10 11:17:02 +0100 | [diff] [blame] | 396 | task_delete(si->target.ptr.t); |
| 397 | task_free(si->target.ptr.t); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 398 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 399 | si->release = NULL; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 400 | si->owner = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 401 | clear_target(&si->target); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 404 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 405 | * Local variables: |
| 406 | * c-indent-level: 8 |
| 407 | * c-basic-offset: 8 |
| 408 | * End: |
| 409 | */ |