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