Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing stream_interface structures |
| 3 | * |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 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> |
| 30 | #include <proto/client.h> |
| 31 | #include <proto/fd.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/stream_sock.h> |
| 34 | #include <proto/task.h> |
| 35 | |
| 36 | /* |
| 37 | * This function only has to be called once after a wakeup event in case of |
| 38 | * suspected timeout. It controls the stream interface timeouts and sets |
| 39 | * si->flags accordingly. It does NOT close anything, as this timeout may |
| 40 | * be used for any purpose. It returns 1 if the timeout fired, otherwise |
| 41 | * zero. |
| 42 | */ |
| 43 | int stream_int_check_timeouts(struct stream_interface *si) |
| 44 | { |
| 45 | if (tick_is_expired(si->exp, now_ms)) { |
| 46 | si->flags |= SI_FL_EXP; |
| 47 | return 1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 52 | /* 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] | 53 | void stream_int_report_error(struct stream_interface *si) |
| 54 | { |
| 55 | if (!si->err_type) |
| 56 | si->err_type = SI_ET_DATA_ERR; |
| 57 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 58 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | si->ib->flags |= BF_READ_ERROR; |
| 60 | } |
| 61 | |
| 62 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 63 | * Returns a message to the client ; the connection is shut down for read, |
| 64 | * and the request is cleared so that no server connection can be initiated. |
| 65 | * The buffer is marked for read shutdown on the other side to protect the |
| 66 | * 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] | 67 | * "chunk". If it is null, then an empty message is used. The reply buffer does |
| 68 | * not need to be empty before this, and its contents will not be overwritten. |
| 69 | * 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] | 70 | */ |
| 71 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 72 | { |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 73 | buffer_auto_read(si->ib); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 74 | buffer_abort(si->ib); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 75 | buffer_auto_close(si->ib); |
| 76 | buffer_erase(si->ib); |
| 77 | if (likely(msg && msg->len)) |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 78 | buffer_write(si->ob, msg->str, msg->len); |
| 79 | |
| 80 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 81 | buffer_auto_read(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 82 | buffer_auto_close(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 83 | buffer_shutr_now(si->ob); |
Willy Tarreau | 5d881d0 | 2009-12-27 22:51:06 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 86 | /* default update function for scheduled tasks, not used for embedded tasks */ |
| 87 | void stream_int_update(struct stream_interface *si) |
| 88 | { |
| 89 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 90 | __FUNCTION__, |
| 91 | si, si->state, si->ib->flags, si->ob->flags); |
| 92 | |
| 93 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 94 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 95 | } |
| 96 | |
| 97 | /* default update function for embedded tasks, to be used at the end of the i/o handler */ |
| 98 | void stream_int_update_embedded(struct stream_interface *si) |
| 99 | { |
| 100 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 101 | __FUNCTION__, |
| 102 | si, si->state, si->ib->flags, si->ob->flags); |
| 103 | |
| 104 | if (si->state != SI_ST_EST) |
| 105 | return; |
| 106 | |
| 107 | if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)) |
| 108 | si->shutw(si); |
| 109 | |
| 110 | if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0) |
| 111 | si->flags |= SI_FL_WAIT_DATA; |
| 112 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 113 | /* we're almost sure that we need some space if the buffer is not |
| 114 | * empty, even if it's not full, because the applets can't fill it. |
| 115 | */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 116 | 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] | 117 | si->flags |= SI_FL_WAIT_ROOM; |
| 118 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 119 | if (si->ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 120 | if (tick_isset(si->ob->wex)) |
| 121 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
| 122 | } |
| 123 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 124 | if (si->ib->flags & BF_READ_ACTIVITY || |
| 125 | (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) { |
| 126 | if (tick_isset(si->ib->rex)) |
| 127 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 128 | } |
| 129 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 130 | 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] | 131 | (si->ob->prod->flags & SI_FL_WAIT_ROOM))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 132 | si->ob->prod->chk_rcv(si->ob->prod); |
| 133 | |
Willy Tarreau | 96fd4b5 | 2009-10-04 17:18:35 +0200 | [diff] [blame] | 134 | if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) && |
| 135 | (si->ib->cons->flags & SI_FL_WAIT_DATA)) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 136 | si->ib->cons->chk_snd(si->ib->cons); |
| 137 | |
| 138 | /* Note that we're trying to wake up in two conditions here : |
| 139 | * - special event, which needs the holder task attention |
| 140 | * - status indicating that the applet can go on working. This |
| 141 | * is rather hard because we might be blocking on output and |
| 142 | * don't want to wake up on input and vice-versa. The idea is |
| 143 | * the to only rely the changes the chk_* might have performed. |
| 144 | */ |
| 145 | if (/* check stream interface changes */ |
| 146 | (si->flags & SI_FL_ERR) || si->state != SI_ST_EST || si->ib->cons->state != SI_ST_EST || |
| 147 | /* check response buffer changes */ |
| 148 | (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR|BF_READ_DONTWAIT)) || |
| 149 | ((si->ib->flags & BF_READ_ACTIVITY) && !si->ib->to_forward) || |
| 150 | (!(si->ib->flags & BF_FULL) && (si->ib->flags & BF_WRITE_ACTIVITY) && si->ib->to_forward) || |
| 151 | /* check request buffer changes */ |
| 152 | (si->ob->flags & (BF_WRITE_ERROR)) || |
| 153 | ((si->ob->flags & BF_WRITE_ACTIVITY) && (si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward) || |
| 154 | (si->ob->flags & BF_READ_ACTIVITY)) { |
| 155 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 156 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* default shutr function for scheduled tasks */ |
| 161 | void stream_int_shutr(struct stream_interface *si) |
| 162 | { |
| 163 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 164 | __FUNCTION__, |
| 165 | si, si->state, si->ib->flags, si->ob->flags); |
| 166 | |
| 167 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 168 | if (si->ib->flags & BF_SHUTR) |
| 169 | return; |
| 170 | si->ib->flags |= BF_SHUTR; |
| 171 | si->ib->rex = TICK_ETERNITY; |
| 172 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 173 | |
| 174 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 175 | return; |
| 176 | |
| 177 | if (si->ob->flags & BF_SHUTW) { |
| 178 | si->state = SI_ST_DIS; |
| 179 | si->exp = TICK_ETERNITY; |
| 180 | } |
| 181 | |
| 182 | /* note that if the task exist, it must unregister itself once it runs */ |
| 183 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 184 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 185 | } |
| 186 | |
| 187 | /* default shutw function for scheduled tasks */ |
| 188 | void stream_int_shutw(struct stream_interface *si) |
| 189 | { |
| 190 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 191 | __FUNCTION__, |
| 192 | si, si->state, si->ib->flags, si->ob->flags); |
| 193 | |
| 194 | si->ob->flags &= ~BF_SHUTW_NOW; |
| 195 | if (si->ob->flags & BF_SHUTW) |
| 196 | return; |
| 197 | si->ob->flags |= BF_SHUTW; |
| 198 | si->ob->wex = TICK_ETERNITY; |
| 199 | si->flags &= ~SI_FL_WAIT_DATA; |
| 200 | |
| 201 | switch (si->state) { |
| 202 | case SI_ST_EST: |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 203 | if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 204 | break; |
| 205 | |
| 206 | /* fall through */ |
| 207 | case SI_ST_CON: |
| 208 | case SI_ST_CER: |
| 209 | si->state = SI_ST_DIS; |
| 210 | /* fall through */ |
| 211 | default: |
| 212 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 213 | si->ib->flags |= BF_SHUTR; |
| 214 | si->ib->rex = TICK_ETERNITY; |
| 215 | si->exp = TICK_ETERNITY; |
| 216 | } |
| 217 | |
| 218 | /* note that if the task exist, it must unregister itself once it runs */ |
| 219 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 220 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 221 | } |
| 222 | |
| 223 | /* default chk_rcv function for scheduled tasks */ |
| 224 | void stream_int_chk_rcv(struct stream_interface *si) |
| 225 | { |
| 226 | struct buffer *ib = si->ib; |
| 227 | |
| 228 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 229 | __FUNCTION__, |
| 230 | si, si->state, si->ib->flags, si->ob->flags); |
| 231 | |
| 232 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 233 | return; |
| 234 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 235 | if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 236 | /* stop reading */ |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 237 | if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 238 | si->flags |= SI_FL_WAIT_ROOM; |
| 239 | } |
| 240 | else { |
| 241 | /* (re)start reading */ |
| 242 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 243 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 244 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* default chk_snd function for scheduled tasks */ |
| 249 | void stream_int_chk_snd(struct stream_interface *si) |
| 250 | { |
| 251 | struct buffer *ob = si->ob; |
| 252 | |
| 253 | DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n", |
| 254 | __FUNCTION__, |
| 255 | si, si->state, si->ib->flags, si->ob->flags); |
| 256 | |
| 257 | if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW))) |
| 258 | return; |
| 259 | |
| 260 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 261 | (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
| 262 | return; |
| 263 | |
| 264 | /* Otherwise there are remaining data to be sent in the buffer, |
| 265 | * so we tell the handler. |
| 266 | */ |
| 267 | si->flags &= ~SI_FL_WAIT_DATA; |
| 268 | if (!tick_isset(ob->wex)) |
| 269 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 270 | |
| 271 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 272 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 273 | } |
| 274 | |
| 275 | /* Register a function to handle a stream_interface as part of the stream |
| 276 | * interface's owner task, which is returned. The SI will wake it up everytime |
| 277 | * it is solicited. The task's processing function must call the specified |
| 278 | * function before returning. It must be deleted by the task handler using |
| 279 | * stream_int_unregister_handler(), possibly from withing the function itself. |
| 280 | */ |
| 281 | struct task *stream_int_register_handler(struct stream_interface *si, |
| 282 | void (*fct)(struct stream_interface *)) |
| 283 | { |
| 284 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 285 | |
| 286 | si->update = stream_int_update_embedded; |
| 287 | si->shutr = stream_int_shutr; |
| 288 | si->shutw = stream_int_shutw; |
| 289 | si->chk_rcv = stream_int_chk_rcv; |
| 290 | si->chk_snd = stream_int_chk_snd; |
| 291 | si->connect = NULL; |
| 292 | si->iohandler = fct; |
| 293 | si->flags |= SI_FL_WAIT_DATA; |
| 294 | return si->owner; |
| 295 | } |
| 296 | |
| 297 | /* Register a function to handle a stream_interface as a standalone task. The |
| 298 | * new task itself is returned and is assigned as si->owner. The stream_interface |
| 299 | * pointer will be pointed to by the task's context. The handler can be detached |
| 300 | * by using stream_int_unregister_handler(). |
| 301 | */ |
| 302 | struct task *stream_int_register_handler_task(struct stream_interface *si, |
| 303 | struct task *(*fct)(struct task *)) |
| 304 | { |
| 305 | struct task *t; |
| 306 | |
| 307 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner); |
| 308 | |
| 309 | si->update = stream_int_update; |
| 310 | si->shutr = stream_int_shutr; |
| 311 | si->shutw = stream_int_shutw; |
| 312 | si->chk_rcv = stream_int_chk_rcv; |
| 313 | si->chk_snd = stream_int_chk_snd; |
| 314 | si->connect = NULL; |
| 315 | si->iohandler = NULL; /* not used when running as an external task */ |
| 316 | si->flags |= SI_FL_WAIT_DATA; |
| 317 | |
| 318 | t = task_new(); |
| 319 | si->owner = t; |
| 320 | if (!t) |
| 321 | return t; |
| 322 | t->process = fct; |
| 323 | t->context = si; |
| 324 | task_wakeup(si->owner, TASK_WOKEN_INIT); |
| 325 | |
| 326 | return t; |
| 327 | } |
| 328 | |
| 329 | /* Unregister a stream interface handler. This must be called by the handler task |
| 330 | * itself when it detects that it is in the SI_ST_DIS state. This function can |
| 331 | * both detach standalone handlers and embedded handlers. |
| 332 | */ |
| 333 | void stream_int_unregister_handler(struct stream_interface *si) |
| 334 | { |
| 335 | if (!si->iohandler && si->owner) { |
| 336 | /* external handler : kill the task */ |
| 337 | task_delete(si->owner); |
| 338 | task_free(si->owner); |
| 339 | } |
| 340 | si->iohandler = NULL; |
| 341 | si->owner = NULL; |
| 342 | } |
| 343 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 344 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 345 | * Local variables: |
| 346 | * c-indent-level: 8 |
| 347 | * c-basic-offset: 8 |
| 348 | * End: |
| 349 | */ |