Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [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 <stdlib.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 16 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <types/capture.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 20 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 22 | #include <proto/acl.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 23 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 24 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 25 | #include <proto/checks.h> |
Willy Tarreau | 5ca791d | 2009-08-16 19:06:42 +0200 | [diff] [blame] | 26 | #include <proto/dumpstats.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 27 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 28 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 30 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 31 | #include <proto/proto_http.h> |
| 32 | #include <proto/proto_tcp.h> |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 33 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 35 | #include <proto/server.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 36 | #include <proto/stream_interface.h> |
| 37 | #include <proto/stream_sock.h> |
| 38 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 40 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 41 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * frees the context associated to a session. It must have been removed first. |
| 45 | */ |
| 46 | void session_free(struct session *s) |
| 47 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 48 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 49 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 50 | struct bref *bref, *back; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | if (s->pend_pos) |
| 53 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 55 | if (s->srv) { /* there may be requests left pending in queue */ |
| 56 | if (s->flags & SN_CURR_SESS) { |
| 57 | s->flags &= ~SN_CURR_SESS; |
| 58 | s->srv->cur_sess--; |
| 59 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 60 | if (may_dequeue_tasks(s->srv, s->be)) |
| 61 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 62 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 63 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 64 | if (unlikely(s->srv_conn)) { |
| 65 | /* the session still has a reserved slot on a server, but |
| 66 | * it should normally be only the same as the one above, |
| 67 | * so this should not happen in fact. |
| 68 | */ |
| 69 | sess_change_server(s, NULL); |
| 70 | } |
| 71 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 72 | if (s->req->pipe) |
| 73 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 74 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 75 | if (s->rep->pipe) |
| 76 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 78 | pool_free2(pool2_buffer, s->req); |
| 79 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 81 | http_end_txn(s); |
| 82 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 83 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 84 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 85 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
| 86 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | } |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 88 | |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 89 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 90 | /* we have to unlink all watchers. We must not relink them if |
| 91 | * this session was the last one in the list. |
| 92 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 93 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 94 | LIST_INIT(&bref->users); |
| 95 | if (s->list.n != &sessions) |
| 96 | LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 97 | bref->ref = s->list.n; |
| 98 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 99 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 100 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 101 | |
| 102 | /* We may want to free the maximum amount of pools if the proxy is stopping */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 103 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 104 | pool_flush2(pool2_buffer); |
| 105 | pool_flush2(fe->hdr_idx_pool); |
| 106 | pool_flush2(pool2_requri); |
| 107 | pool_flush2(pool2_capture); |
| 108 | pool_flush2(pool2_session); |
| 109 | pool_flush2(fe->req_cap_pool); |
| 110 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 111 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 116 | int init_session() |
| 117 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 118 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 119 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 120 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 123 | void session_process_counters(struct session *s) |
| 124 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 125 | unsigned long long bytes; |
| 126 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 127 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 128 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 129 | s->logs.bytes_in = s->req->total; |
| 130 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 131 | s->fe->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 132 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 133 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 134 | s->be->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 136 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 137 | s->srv->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 138 | |
| 139 | if (s->listener->counters) |
| 140 | s->listener->counters->bytes_in += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 141 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 144 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 145 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 146 | s->logs.bytes_out = s->rep->total; |
| 147 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 148 | s->fe->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 149 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 150 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 151 | s->be->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 152 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 153 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 154 | s->srv->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 155 | |
| 156 | if (s->listener->counters) |
| 157 | s->listener->counters->bytes_out += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 158 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 162 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 163 | * connection was attempted and that the file descriptor is already allocated. |
| 164 | * We must check for establishment, error and abort. Possible output states |
| 165 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 166 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 167 | * otherwise 1. |
| 168 | */ |
| 169 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 170 | { |
| 171 | struct buffer *req = si->ob; |
| 172 | struct buffer *rep = si->ib; |
| 173 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 174 | /* If we got an error, or if nothing happened and the connection timed |
| 175 | * out, we must give up. The CER state handler will take care of retry |
| 176 | * attempts and error reports. |
| 177 | */ |
| 178 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 179 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 180 | si->state = SI_ST_CER; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 181 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 182 | fd_delete(si->fd); |
| 183 | |
| 184 | if (si->err_type) |
| 185 | return 0; |
| 186 | |
| 187 | si->err_loc = s->srv; |
| 188 | if (si->flags & SI_FL_ERR) |
| 189 | si->err_type = SI_ET_CONN_ERR; |
| 190 | else |
| 191 | si->err_type = SI_ET_CONN_TO; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* OK, maybe we want to abort */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 196 | if (unlikely((rep->flags & BF_SHUTW) || |
| 197 | ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 198 | (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 199 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 200 | /* give up */ |
| 201 | si->shutw(si); |
| 202 | si->err_type |= SI_ET_CONN_ABRT; |
| 203 | si->err_loc = s->srv; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 204 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 205 | if (s->srv_error) |
| 206 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | /* we need to wait a bit more if there was no activity either */ |
| 211 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 212 | return 1; |
| 213 | |
| 214 | /* OK, this means that a connection succeeded. The caller will be |
| 215 | * responsible for handling the transition from CON to EST. |
| 216 | */ |
| 217 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 218 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 219 | si->state = SI_ST_EST; |
| 220 | si->err_type = SI_ET_NONE; |
| 221 | si->err_loc = NULL; |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 226 | * previous connection attempt has failed and that the file descriptor |
| 227 | * has already been released. Possible causes include asynchronous error |
| 228 | * notification and time out. Possible output states are SI_ST_CLO when |
| 229 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 230 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 231 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 232 | * marked as in error state. It returns 0. |
| 233 | */ |
| 234 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 235 | { |
| 236 | /* we probably have to release last session from the server */ |
| 237 | if (s->srv) { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 238 | health_adjust(s->srv, HANA_STATUS_L4_ERR); |
| 239 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 240 | if (s->flags & SN_CURR_SESS) { |
| 241 | s->flags &= ~SN_CURR_SESS; |
| 242 | s->srv->cur_sess--; |
| 243 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* ensure that we have enough retries left */ |
| 247 | s->conn_retries--; |
| 248 | if (s->conn_retries < 0) { |
| 249 | if (!si->err_type) { |
| 250 | si->err_type = SI_ET_CONN_ERR; |
| 251 | si->err_loc = s->srv; |
| 252 | } |
| 253 | |
| 254 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 255 | s->srv->counters.failed_conns++; |
| 256 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 257 | if (may_dequeue_tasks(s->srv, s->be)) |
| 258 | process_srv_queue(s->srv); |
| 259 | |
| 260 | /* shutw is enough so stop a connecting socket */ |
| 261 | si->shutw(si); |
| 262 | si->ob->flags |= BF_WRITE_ERROR; |
| 263 | si->ib->flags |= BF_READ_ERROR; |
| 264 | |
| 265 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 266 | if (s->srv_error) |
| 267 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 272 | * retry on another server for the last retry. In order to achieve this, |
| 273 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 274 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 275 | * redispatch yet, because this will depend on what server is selected. |
| 276 | */ |
| 277 | if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) { |
| 278 | if (may_dequeue_tasks(s->srv, s->be)) |
| 279 | process_srv_queue(s->srv); |
| 280 | |
| 281 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 282 | s->prev_srv = s->srv; |
| 283 | si->state = SI_ST_REQ; |
| 284 | } else { |
| 285 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 286 | s->srv->counters.retries++; |
| 287 | s->be->counters.retries++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 288 | si->state = SI_ST_ASS; |
| 289 | } |
| 290 | |
| 291 | if (si->flags & SI_FL_ERR) { |
| 292 | /* The error was an asynchronous connection error, and we will |
| 293 | * likely have to retry connecting to the same server, most |
| 294 | * likely leading to the same result. To avoid this, we wait |
| 295 | * one second before retrying. |
| 296 | */ |
| 297 | |
| 298 | if (!si->err_type) |
| 299 | si->err_type = SI_ET_CONN_ERR; |
| 300 | |
| 301 | si->state = SI_ST_TAR; |
| 302 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 303 | return 0; |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * This function handles the transition between the SI_ST_CON state and the |
| 310 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON to |
| 311 | * SI_ST_EST. |
| 312 | */ |
| 313 | void sess_establish(struct session *s, struct stream_interface *si) |
| 314 | { |
| 315 | struct buffer *req = si->ob; |
| 316 | struct buffer *rep = si->ib; |
| 317 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 318 | if (s->srv) |
| 319 | health_adjust(s->srv, HANA_STATUS_L4_OK); |
| 320 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 321 | if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 322 | /* if the user wants to log as soon as possible, without counting |
| 323 | * bytes from the server, then this is the right moment. */ |
| 324 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 325 | s->logs.t_close = s->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 326 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 327 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 328 | } |
| 329 | else { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 330 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 331 | /* reset hdr_idx which was already initialized by the request. |
| 332 | * right now, the http parser does it. |
| 333 | * hdr_idx_init(&s->txn.hdr_idx); |
| 334 | */ |
| 335 | } |
| 336 | |
Willy Tarreau | 4e5b828 | 2009-08-16 22:57:50 +0200 | [diff] [blame] | 337 | rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 338 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 339 | req->wex = TICK_ETERNITY; |
| 340 | } |
| 341 | |
| 342 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 343 | * Other input states are simply ignored. |
| 344 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 345 | * Flags must have previously been updated for timeouts and other conditions. |
| 346 | */ |
| 347 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 348 | { |
| 349 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 350 | now_ms, __FUNCTION__, |
| 351 | s, |
| 352 | s->req, s->rep, |
| 353 | s->req->rex, s->rep->wex, |
| 354 | s->req->flags, s->rep->flags, |
| 355 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 356 | |
| 357 | if (si->state == SI_ST_ASS) { |
| 358 | /* Server assigned to connection request, we have to try to connect now */ |
| 359 | int conn_err; |
| 360 | |
| 361 | conn_err = connect_server(s); |
| 362 | if (conn_err == SN_ERR_NONE) { |
| 363 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 364 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 365 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
| 369 | /* We have received a synchronous error. We might have to |
| 370 | * abort, retry immediately or redispatch. |
| 371 | */ |
| 372 | if (conn_err == SN_ERR_INTERNAL) { |
| 373 | if (!si->err_type) { |
| 374 | si->err_type = SI_ET_CONN_OTHER; |
| 375 | si->err_loc = s->srv; |
| 376 | } |
| 377 | |
| 378 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 379 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 380 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 381 | s->srv->counters.failed_conns++; |
| 382 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 383 | |
| 384 | /* release other sessions waiting for this server */ |
| 385 | if (may_dequeue_tasks(s->srv, s->be)) |
| 386 | process_srv_queue(s->srv); |
| 387 | |
| 388 | /* Failed and not retryable. */ |
| 389 | si->shutr(si); |
| 390 | si->shutw(si); |
| 391 | si->ob->flags |= BF_WRITE_ERROR; |
| 392 | |
| 393 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 394 | |
| 395 | /* no session was ever accounted for this server */ |
| 396 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 397 | if (s->srv_error) |
| 398 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
| 402 | /* We are facing a retryable error, but we don't want to run a |
| 403 | * turn-around now, as the problem is likely a source port |
| 404 | * allocation problem, so we want to retry now. |
| 405 | */ |
| 406 | si->state = SI_ST_CER; |
| 407 | si->flags &= ~SI_FL_ERR; |
| 408 | sess_update_st_cer(s, si); |
| 409 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 410 | return; |
| 411 | } |
| 412 | else if (si->state == SI_ST_QUE) { |
| 413 | /* connection request was queued, check for any update */ |
| 414 | if (!s->pend_pos) { |
| 415 | /* The connection is not in the queue anymore. Either |
| 416 | * we have a server connection slot available and we |
| 417 | * go directly to the assigned state, or we need to |
| 418 | * load-balance first and go to the INI state. |
| 419 | */ |
| 420 | si->exp = TICK_ETERNITY; |
| 421 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 422 | si->state = SI_ST_REQ; |
| 423 | else { |
| 424 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 425 | si->state = SI_ST_ASS; |
| 426 | } |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | /* Connection request still in queue... */ |
| 431 | if (si->flags & SI_FL_EXP) { |
| 432 | /* ... and timeout expired */ |
| 433 | si->exp = TICK_ETERNITY; |
| 434 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 435 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 436 | s->srv->counters.failed_conns++; |
| 437 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 438 | si->shutr(si); |
| 439 | si->shutw(si); |
| 440 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 441 | if (!si->err_type) |
| 442 | si->err_type = SI_ET_QUEUE_TO; |
| 443 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 444 | if (s->srv_error) |
| 445 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
| 449 | /* Connection remains in queue, check if we have to abort it */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 450 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 451 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 452 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 453 | /* give up */ |
| 454 | si->exp = TICK_ETERNITY; |
| 455 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 456 | si->shutr(si); |
| 457 | si->shutw(si); |
| 458 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 459 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 460 | if (s->srv_error) |
| 461 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
| 465 | /* Nothing changed */ |
| 466 | return; |
| 467 | } |
| 468 | else if (si->state == SI_ST_TAR) { |
| 469 | /* Connection request might be aborted */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 470 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 471 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 472 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 473 | /* give up */ |
| 474 | si->exp = TICK_ETERNITY; |
| 475 | si->shutr(si); |
| 476 | si->shutw(si); |
| 477 | si->err_type |= SI_ET_CONN_ABRT; |
| 478 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 479 | if (s->srv_error) |
| 480 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
| 484 | if (!(si->flags & SI_FL_EXP)) |
| 485 | return; /* still in turn-around */ |
| 486 | |
| 487 | si->exp = TICK_ETERNITY; |
| 488 | |
| 489 | /* we keep trying on the same server as long as the session is |
| 490 | * marked "assigned". |
| 491 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 492 | */ |
| 493 | if (s->flags & SN_ASSIGNED) |
| 494 | si->state = SI_ST_ASS; |
| 495 | else |
| 496 | si->state = SI_ST_REQ; |
| 497 | return; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /* This function initiates a server connection request on a stream interface |
| 502 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 503 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 504 | * or SI_ST_CLO upon error. |
| 505 | */ |
| 506 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 507 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 508 | now_ms, __FUNCTION__, |
| 509 | s, |
| 510 | s->req, s->rep, |
| 511 | s->req->rex, s->rep->wex, |
| 512 | s->req->flags, s->rep->flags, |
| 513 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 514 | |
| 515 | if (si->state != SI_ST_REQ) |
| 516 | return; |
| 517 | |
| 518 | /* Try to assign a server */ |
| 519 | if (srv_redispatch_connect(s) != 0) { |
| 520 | /* We did not get a server. Either we queued the |
| 521 | * connection request, or we encountered an error. |
| 522 | */ |
| 523 | if (si->state == SI_ST_QUE) |
| 524 | return; |
| 525 | |
| 526 | /* we did not get any server, let's check the cause */ |
| 527 | si->shutr(si); |
| 528 | si->shutw(si); |
| 529 | si->ob->flags |= BF_WRITE_ERROR; |
| 530 | if (!si->err_type) |
| 531 | si->err_type = SI_ET_CONN_OTHER; |
| 532 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 533 | if (s->srv_error) |
| 534 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | |
| 538 | /* The server is assigned */ |
| 539 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 540 | si->state = SI_ST_ASS; |
| 541 | } |
| 542 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 543 | /* This stream analyser checks the switching rules and changes the backend |
| 544 | * if appropriate. The default_backend rule is also considered. |
| 545 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 546 | * either needs more data or wants to immediately abort the request. |
| 547 | */ |
| 548 | int process_switching_rules(struct session *s, struct buffer *req, int an_bit) |
| 549 | { |
| 550 | req->analysers &= ~an_bit; |
| 551 | req->analyse_exp = TICK_ETERNITY; |
| 552 | |
| 553 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 554 | now_ms, __FUNCTION__, |
| 555 | s, |
| 556 | req, |
| 557 | req->rex, req->wex, |
| 558 | req->flags, |
| 559 | req->l, |
| 560 | req->analysers); |
| 561 | |
| 562 | /* now check whether we have some switching rules for this request */ |
| 563 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 564 | struct switching_rule *rule; |
| 565 | |
| 566 | list_for_each_entry(rule, &s->fe->switching_rules, list) { |
| 567 | int ret; |
| 568 | |
| 569 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ); |
| 570 | ret = acl_pass(ret); |
| 571 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 572 | ret = !ret; |
| 573 | |
| 574 | if (ret) { |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 575 | if (!session_set_backend(s, rule->be.backend)) |
| 576 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* To ensure correct connection accounting on the backend, we |
| 582 | * have to assign one if it was not set (eg: a listen). This |
| 583 | * measure also takes care of correctly setting the default |
| 584 | * backend if any. |
| 585 | */ |
| 586 | if (!(s->flags & SN_BE_ASSIGNED)) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 587 | if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be)) |
| 588 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* we don't want to run the HTTP filters again if the backend has not changed */ |
| 592 | if (s->fe == s->be) |
| 593 | s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE; |
| 594 | |
| 595 | return 1; |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 596 | |
| 597 | sw_failed: |
| 598 | /* immediately abort this request in case of allocation failure */ |
| 599 | buffer_abort(s->req); |
| 600 | buffer_abort(s->rep); |
| 601 | |
| 602 | if (!(s->flags & SN_ERR_MASK)) |
| 603 | s->flags |= SN_ERR_RESOURCE; |
| 604 | if (!(s->flags & SN_FINST_MASK)) |
| 605 | s->flags |= SN_FINST_R; |
| 606 | |
| 607 | s->txn.status = 500; |
| 608 | s->req->analysers = 0; |
| 609 | s->req->analyse_exp = TICK_ETERNITY; |
| 610 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 613 | /* This macro is very specific to the function below. See the comments in |
| 614 | * process_session() below to understand the logic and the tests. |
| 615 | */ |
| 616 | #define UPDATE_ANALYSERS(real, list, back, flag) { \ |
| 617 | list = (((list) & ~(flag)) | ~(back)) & (real); \ |
| 618 | back = real; \ |
| 619 | if (!(list)) \ |
| 620 | break; \ |
| 621 | if (((list) ^ ((list) & ((list) - 1))) < (flag)) \ |
| 622 | continue; \ |
| 623 | } |
| 624 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 625 | /* Processes the client, server, request and response jobs of a session task, |
| 626 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 627 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 628 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 629 | * nothing too many times, the request and response buffers flags are monitored |
| 630 | * and each function is called only if at least another function has changed at |
| 631 | * least one flag it is interested in. |
| 632 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 633 | struct task *process_session(struct task *t) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 634 | { |
| 635 | struct session *s = t->context; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 636 | unsigned int rqf_last, rpf_last; |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 637 | unsigned int req_ana_back; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 638 | |
| 639 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 640 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 641 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 642 | /* This flag must explicitly be set every time */ |
| 643 | s->req->flags &= ~BF_READ_NOEXP; |
| 644 | |
| 645 | /* Keep a copy of req/rep flags so that we can detect shutdowns */ |
| 646 | rqf_last = s->req->flags; |
| 647 | rpf_last = s->rep->flags; |
| 648 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 649 | /* we don't want the stream interface functions to recursively wake us up */ |
| 650 | if (s->req->prod->owner == t) |
| 651 | s->req->prod->flags |= SI_FL_DONT_WAKE; |
| 652 | if (s->req->cons->owner == t) |
| 653 | s->req->cons->flags |= SI_FL_DONT_WAKE; |
| 654 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 655 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 656 | * stream interfaces when their timeouts have expired. |
| 657 | */ |
| 658 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 659 | stream_int_check_timeouts(&s->si[0]); |
| 660 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 661 | |
| 662 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 663 | * for future reads or writes. Note: this will also concern upper layers |
| 664 | * but we do not touch any other flag. We must be careful and correctly |
| 665 | * detect state changes when calling them. |
| 666 | */ |
| 667 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 668 | buffer_check_timeouts(s->req); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 669 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 670 | if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 671 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 672 | s->req->cons->shutw(s->req->cons); |
| 673 | } |
| 674 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 675 | if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 676 | s->req->prod->shutr(s->req->prod); |
| 677 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 678 | buffer_check_timeouts(s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 679 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 680 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 681 | s->rep->cons->flags |= SI_FL_NOLINGER; |
| 682 | s->rep->cons->shutw(s->rep->cons); |
| 683 | } |
| 684 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 685 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 686 | s->rep->prod->shutr(s->rep->prod); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 687 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 688 | |
| 689 | /* 1b: check for low-level errors reported at the stream interface. |
| 690 | * First we check if it's a retryable error (in which case we don't |
| 691 | * want to tell the buffer). Otherwise we report the error one level |
| 692 | * upper by setting flags into the buffers. Note that the side towards |
| 693 | * the client cannot have connect (hence retryable) errors. Also, the |
| 694 | * connection setup code must be able to deal with any type of abort. |
| 695 | */ |
| 696 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 697 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 698 | s->si[0].shutr(&s->si[0]); |
| 699 | s->si[0].shutw(&s->si[0]); |
| 700 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 701 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 702 | if (!(s->flags & SN_ERR_MASK)) |
| 703 | s->flags |= SN_ERR_CLICL; |
| 704 | if (!(s->flags & SN_FINST_MASK)) |
| 705 | s->flags |= SN_FINST_D; |
| 706 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 711 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 712 | s->si[1].shutr(&s->si[1]); |
| 713 | s->si[1].shutw(&s->si[1]); |
| 714 | stream_int_report_error(&s->si[1]); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 715 | s->be->counters.failed_resp++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 716 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 717 | s->srv->counters.failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 718 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 719 | if (!(s->flags & SN_ERR_MASK)) |
| 720 | s->flags |= SN_ERR_SRVCL; |
| 721 | if (!(s->flags & SN_FINST_MASK)) |
| 722 | s->flags |= SN_FINST_D; |
| 723 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 724 | } |
| 725 | /* note: maybe we should process connection errors here ? */ |
| 726 | } |
| 727 | |
| 728 | if (s->si[1].state == SI_ST_CON) { |
| 729 | /* we were trying to establish a connection on the server side, |
| 730 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 731 | */ |
| 732 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 733 | sess_update_st_cer(s, &s->si[1]); |
| 734 | else if (s->si[1].state == SI_ST_EST) |
| 735 | sess_establish(s, &s->si[1]); |
| 736 | |
| 737 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 738 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 739 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 740 | */ |
| 741 | } |
| 742 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 743 | resync_stream_interface: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 744 | /* Check for connection closure */ |
| 745 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 746 | DPRINTF(stderr, |
| 747 | "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n", |
| 748 | now_ms, __FUNCTION__, __LINE__, |
| 749 | t, |
| 750 | s, s->flags, |
| 751 | s->req, s->rep, |
| 752 | s->req->rex, s->rep->wex, |
| 753 | s->req->flags, s->rep->flags, |
| 754 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 755 | s->rep->cons->err_type, s->req->cons->err_type, |
| 756 | s->conn_retries); |
| 757 | |
| 758 | /* nothing special to be done on client side */ |
| 759 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 760 | s->req->prod->state = SI_ST_CLO; |
| 761 | |
| 762 | /* When a server-side connection is released, we have to count it and |
| 763 | * check for pending connections on this server. |
| 764 | */ |
| 765 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 766 | s->req->cons->state = SI_ST_CLO; |
| 767 | if (s->srv) { |
| 768 | if (s->flags & SN_CURR_SESS) { |
| 769 | s->flags &= ~SN_CURR_SESS; |
| 770 | s->srv->cur_sess--; |
| 771 | } |
| 772 | sess_change_server(s, NULL); |
| 773 | if (may_dequeue_tasks(s->srv, s->be)) |
| 774 | process_srv_queue(s->srv); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 780 | * at this point. |
| 781 | */ |
| 782 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 783 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 784 | /* Analyse request */ |
| 785 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 786 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 787 | unsigned int flags = s->req->flags; |
| 788 | |
| 789 | if (s->req->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 790 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 791 | unsigned int ana_list; |
| 792 | unsigned int ana_back; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 793 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 794 | /* it's up to the analysers to stop new connections, |
| 795 | * disable reading or closing. Note: if an analyser |
| 796 | * disables any of these bits, it is responsible for |
| 797 | * enabling them again when it disables itself, so |
| 798 | * that other analysers are called in similar conditions. |
| 799 | */ |
| 800 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 801 | buffer_auto_connect(s->req); |
| 802 | buffer_auto_close(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 803 | |
| 804 | /* We will call all analysers for which a bit is set in |
| 805 | * s->req->analysers, following the bit order from LSB |
| 806 | * to MSB. The analysers must remove themselves from |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 807 | * the list when not needed. Any analyser may return 0 |
| 808 | * to break out of the loop, either because of missing |
| 809 | * data to take a decision, or because it decides to |
| 810 | * kill the session. We loop at least once through each |
| 811 | * analyser, and we may loop again if other analysers |
| 812 | * are added in the middle. |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 813 | * |
| 814 | * We build a list of analysers to run. We evaluate all |
| 815 | * of these analysers in the order of the lower bit to |
| 816 | * the higher bit. This ordering is very important. |
| 817 | * An analyser will often add/remove other analysers, |
| 818 | * including itself. Any changes to itself have no effect |
| 819 | * on the loop. If it removes any other analysers, we |
| 820 | * want those analysers not to be called anymore during |
| 821 | * this loop. If it adds an analyser that is located |
| 822 | * after itself, we want it to be scheduled for being |
| 823 | * processed during the loop. If it adds an analyser |
| 824 | * which is located before it, we want it to switch to |
| 825 | * it immediately, even if it has already been called |
| 826 | * once but removed since. |
| 827 | * |
| 828 | * In order to achieve this, we compare the analyser |
| 829 | * list after the call with a copy of it before the |
| 830 | * call. The work list is fed with analyser bits that |
| 831 | * appeared during the call. Then we compare previous |
| 832 | * work list with the new one, and check the bits that |
| 833 | * appeared. If the lowest of these bits is lower than |
| 834 | * the current bit, it means we have enabled a previous |
| 835 | * analyser and must immediately loop again. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 836 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 837 | |
| 838 | ana_list = ana_back = s->req->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 839 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 840 | /* Warning! ensure that analysers are always placed in ascending order! */ |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 842 | if (ana_list & AN_REQ_INSPECT) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 843 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 844 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 845 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 846 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 847 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 848 | if (ana_list & AN_REQ_WAIT_HTTP) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 849 | if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP)) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 850 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 851 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 854 | if (ana_list & AN_REQ_HTTP_PROCESS_FE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 855 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe)) |
| 856 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 857 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 860 | if (ana_list & AN_REQ_SWITCHING_RULES) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 861 | if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES)) |
| 862 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 863 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 866 | if (ana_list & AN_REQ_HTTP_PROCESS_BE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 867 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be)) |
| 868 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 869 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 870 | } |
| 871 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 872 | if (ana_list & AN_REQ_HTTP_TARPIT) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 873 | if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT)) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 874 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 875 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 876 | } |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 877 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 878 | if (ana_list & AN_REQ_HTTP_INNER) { |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 879 | if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER)) |
| 880 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 881 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 884 | if (ana_list & AN_REQ_HTTP_BODY) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 885 | if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY)) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 886 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 887 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 888 | } |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 889 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 890 | if (ana_list & AN_REQ_PRST_RDP_COOKIE) { |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 891 | if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE)) |
| 892 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 893 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE); |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 894 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 895 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 896 | if (ana_list & AN_REQ_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 897 | if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY)) |
| 898 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 899 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 900 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 901 | break; |
| 902 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 903 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 904 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 905 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) { |
| 906 | rqf_last = s->req->flags; |
| 907 | goto resync_request; |
| 908 | } |
| 909 | } |
| 910 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 911 | /* we'll monitor the request analysers while parsing the response, |
| 912 | * because some response analysers may indirectly enable new request |
| 913 | * analysers (eg: HTTP keep-alive). |
| 914 | */ |
| 915 | req_ana_back = s->req->analysers; |
| 916 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 917 | resync_response: |
| 918 | /* Analyse response */ |
| 919 | |
| 920 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 921 | /* In inject mode, we wake up everytime something has |
| 922 | * happened on the write side of the buffer. |
| 923 | */ |
| 924 | unsigned int flags = s->rep->flags; |
| 925 | |
| 926 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 927 | !(s->rep->flags & BF_FULL)) { |
| 928 | s->rep->hijacker(s, s->rep); |
| 929 | } |
| 930 | |
| 931 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 932 | rpf_last = s->rep->flags; |
| 933 | goto resync_response; |
| 934 | } |
| 935 | } |
| 936 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 937 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 938 | unsigned int flags = s->rep->flags; |
| 939 | |
| 940 | if (s->rep->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 941 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 942 | unsigned int ana_list; |
| 943 | unsigned int ana_back; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 944 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 945 | /* it's up to the analysers to stop disable reading or |
| 946 | * closing. Note: if an analyser disables any of these |
| 947 | * bits, it is responsible for enabling them again when |
| 948 | * it disables itself, so that other analysers are called |
| 949 | * in similar conditions. |
| 950 | */ |
| 951 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 952 | buffer_auto_close(s->rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 953 | |
| 954 | /* We will call all analysers for which a bit is set in |
| 955 | * s->rep->analysers, following the bit order from LSB |
| 956 | * to MSB. The analysers must remove themselves from |
| 957 | * the list when not needed. Any analyser may return 0 |
| 958 | * to break out of the loop, either because of missing |
| 959 | * data to take a decision, or because it decides to |
| 960 | * kill the session. We loop at least once through each |
| 961 | * analyser, and we may loop again if other analysers |
| 962 | * are added in the middle. |
| 963 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 964 | |
| 965 | ana_list = ana_back = s->rep->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 966 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 967 | if (!ana_list) |
| 968 | break; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 969 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 970 | /* Warning! ensure that analysers are always placed in ascending order! */ |
| 971 | |
| 972 | if (ana_list & AN_RES_WAIT_HTTP) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 973 | if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP)) |
| 974 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 975 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 978 | if (ana_list & AN_RES_HTTP_PROCESS_BE) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 979 | if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be)) |
| 980 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 981 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 982 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 983 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 984 | if (ana_list & AN_RES_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 985 | if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY)) |
| 986 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 987 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 988 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 989 | break; |
| 990 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 994 | rpf_last = s->rep->flags; |
| 995 | goto resync_response; |
| 996 | } |
| 997 | } |
| 998 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 999 | /* maybe someone has added some request analysers, so we must check and loop */ |
| 1000 | if (s->req->analysers & ~req_ana_back) |
| 1001 | goto resync_request; |
| 1002 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1003 | /* FIXME: here we should call protocol handlers which rely on |
| 1004 | * both buffers. |
| 1005 | */ |
| 1006 | |
| 1007 | |
| 1008 | /* |
| 1009 | * Now we propagate unhandled errors to the session |
| 1010 | */ |
| 1011 | if (!(s->flags & SN_ERR_MASK)) { |
| 1012 | if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1013 | /* Report it if the client got an error or a read timeout expired */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1014 | s->req->analysers = 0; |
| 1015 | if (s->req->flags & BF_READ_ERROR) |
| 1016 | s->flags |= SN_ERR_CLICL; |
| 1017 | else if (s->req->flags & BF_READ_TIMEOUT) |
| 1018 | s->flags |= SN_ERR_CLITO; |
| 1019 | else if (s->req->flags & BF_WRITE_ERROR) |
| 1020 | s->flags |= SN_ERR_SRVCL; |
| 1021 | else |
| 1022 | s->flags |= SN_ERR_SRVTO; |
| 1023 | sess_set_term_flags(s); |
| 1024 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1025 | else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1026 | /* Report it if the server got an error or a read timeout expired */ |
| 1027 | s->rep->analysers = 0; |
| 1028 | if (s->rep->flags & BF_READ_ERROR) |
| 1029 | s->flags |= SN_ERR_SRVCL; |
| 1030 | else if (s->rep->flags & BF_READ_TIMEOUT) |
| 1031 | s->flags |= SN_ERR_SRVTO; |
| 1032 | else if (s->rep->flags & BF_WRITE_ERROR) |
| 1033 | s->flags |= SN_ERR_CLICL; |
| 1034 | else |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1035 | s->flags |= SN_ERR_CLITO; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1036 | sess_set_term_flags(s); |
| 1037 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1040 | /* |
| 1041 | * Here we take care of forwarding unhandled data. This also includes |
| 1042 | * connection establishments and shutdown requests. |
| 1043 | */ |
| 1044 | |
| 1045 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1046 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1047 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1048 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1049 | if (!s->req->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1050 | !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1051 | (s->req->prod->state >= SI_ST_EST) && |
| 1052 | (s->req->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1053 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1054 | * attached to it. If any data are left in, we'll permit them to |
| 1055 | * move. |
| 1056 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1057 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1058 | buffer_auto_connect(s->req); |
| 1059 | buffer_auto_close(s->req); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1060 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 1061 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1062 | /* If the producer is still connected, we'll enable data to flow |
| 1063 | * from the producer to the consumer (which might possibly not be |
| 1064 | * connected yet). |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1065 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1066 | if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1067 | buffer_forward(s->req, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1068 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1069 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1070 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 1071 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1072 | s->req->to_forward && |
| 1073 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1074 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1075 | (pipes_used < global.maxpipes) && |
| 1076 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 1077 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1078 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 1079 | s->req->flags |= BF_KERN_SPLICING; |
| 1080 | } |
| 1081 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1082 | /* reflect what the L7 analysers have seen last */ |
| 1083 | rqf_last = s->req->flags; |
| 1084 | |
| 1085 | /* |
| 1086 | * Now forward all shutdown requests between both sides of the buffer |
| 1087 | */ |
| 1088 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1089 | /* first, let's check if the request buffer needs to shutdown(write), which may |
| 1090 | * happen either because the input is closed or because we want to force a close |
| 1091 | * once the server has begun to respond. |
| 1092 | */ |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1093 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1094 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1095 | buffer_shutw_now(s->req); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1096 | |
| 1097 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1098 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1099 | s->req->cons->shutw(s->req->cons); |
| 1100 | |
| 1101 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1102 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 1103 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1104 | buffer_shutr_now(s->req); |
| 1105 | |
| 1106 | /* shutdown(read) pending */ |
| 1107 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1108 | s->req->prod->shutr(s->req->prod); |
| 1109 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1110 | /* it's possible that an upper layer has requested a connection setup or abort. |
| 1111 | * There are 2 situations where we decide to establish a new connection : |
| 1112 | * - there are data scheduled for emission in the buffer |
| 1113 | * - the BF_AUTO_CONNECT flag is set (active connection) |
| 1114 | */ |
| 1115 | if (s->req->cons->state == SI_ST_INI) { |
| 1116 | if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1117 | if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1118 | /* If we have a ->connect method, we need to perform a connection request, |
| 1119 | * otherwise we immediately switch to the connected state. |
| 1120 | */ |
| 1121 | if (s->req->cons->connect) |
| 1122 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
| 1123 | else |
| 1124 | s->req->cons->state = SI_ST_EST; /* connection established */ |
| 1125 | } |
Willy Tarreau | 7320122 | 2009-08-16 18:27:24 +0200 | [diff] [blame] | 1126 | } |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1127 | else { |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1128 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1129 | buffer_shutw_now(s->req); /* fix buffer flags upon abort */ |
| 1130 | buffer_shutr_now(s->rep); |
| 1131 | } |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1134 | |
| 1135 | /* we may have a pending connection request, or a connection waiting |
| 1136 | * for completion. |
| 1137 | */ |
| 1138 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 1139 | do { |
| 1140 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 1141 | * to give a chance to step 2 to perform a redirect if needed. |
| 1142 | */ |
| 1143 | if (s->si[1].state != SI_ST_REQ) |
| 1144 | sess_update_stream_int(s, &s->si[1]); |
| 1145 | if (s->si[1].state == SI_ST_REQ) |
| 1146 | sess_prepare_conn_req(s, &s->si[1]); |
| 1147 | |
| 1148 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1149 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1150 | perform_http_redirect(s, &s->si[1]); |
| 1151 | } while (s->si[1].state == SI_ST_ASS); |
| 1152 | } |
| 1153 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1154 | /* Benchmarks have shown that it's optimal to do a full resync now */ |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1155 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1156 | goto resync_stream_interface; |
| 1157 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1158 | /* otherwise wewant to check if we need to resync the req buffer or not */ |
| 1159 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1160 | goto resync_request; |
| 1161 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1162 | /* perform output updates to the response buffer */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1163 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1164 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1165 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1166 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1167 | if (!s->rep->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1168 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1169 | (s->rep->prod->state >= SI_ST_EST) && |
| 1170 | (s->rep->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1171 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1172 | * attached to it. If any data are left in, we'll permit them to |
| 1173 | * move. |
| 1174 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1175 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1176 | buffer_auto_close(s->rep); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1177 | buffer_flush(s->rep); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1178 | if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1179 | buffer_forward(s->rep, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1180 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1181 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1182 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1183 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1184 | s->rep->to_forward && |
| 1185 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1186 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1187 | (pipes_used < global.maxpipes) && |
| 1188 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1189 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1190 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1191 | s->rep->flags |= BF_KERN_SPLICING; |
| 1192 | } |
| 1193 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1194 | /* reflect what the L7 analysers have seen last */ |
| 1195 | rpf_last = s->rep->flags; |
| 1196 | |
| 1197 | /* |
| 1198 | * Now forward all shutdown requests between both sides of the buffer |
| 1199 | */ |
| 1200 | |
| 1201 | /* |
| 1202 | * FIXME: this is probably where we should produce error responses. |
| 1203 | */ |
| 1204 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1205 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1206 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1207 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1208 | buffer_shutw_now(s->rep); |
| 1209 | |
| 1210 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1211 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1212 | s->rep->cons->shutw(s->rep->cons); |
| 1213 | |
| 1214 | /* shutdown(write) done on the client side, we must stop the server too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1215 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1216 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1217 | buffer_shutr_now(s->rep); |
| 1218 | |
| 1219 | /* shutdown(read) pending */ |
| 1220 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1221 | s->rep->prod->shutr(s->rep->prod); |
| 1222 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1223 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1224 | goto resync_stream_interface; |
| 1225 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1226 | if (s->req->flags != rqf_last) |
| 1227 | goto resync_request; |
| 1228 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1229 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1230 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1232 | /* we're interested in getting wakeups again */ |
| 1233 | s->req->prod->flags &= ~SI_FL_DONT_WAKE; |
| 1234 | s->req->cons->flags &= ~SI_FL_DONT_WAKE; |
| 1235 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1236 | /* This is needed only when debugging is enabled, to indicate |
| 1237 | * client-side or server-side close. Please note that in the unlikely |
| 1238 | * event where both sides would close at once, the sequence is reported |
| 1239 | * on the server side first. |
| 1240 | */ |
| 1241 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1242 | (!(global.mode & MODE_QUIET) || |
| 1243 | (global.mode & MODE_VERBOSE)))) { |
| 1244 | int len; |
| 1245 | |
| 1246 | if (s->si[1].state == SI_ST_CLO && |
| 1247 | s->si[1].prev_state == SI_ST_EST) { |
| 1248 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1249 | s->uniq_id, s->be->id, |
| 1250 | (unsigned short)s->si[0].fd, |
| 1251 | (unsigned short)s->si[1].fd); |
| 1252 | write(1, trash, len); |
| 1253 | } |
| 1254 | |
| 1255 | if (s->si[0].state == SI_ST_CLO && |
| 1256 | s->si[0].prev_state == SI_ST_EST) { |
| 1257 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1258 | s->uniq_id, s->be->id, |
| 1259 | (unsigned short)s->si[0].fd, |
| 1260 | (unsigned short)s->si[1].fd); |
| 1261 | write(1, trash, len); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1266 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1267 | |
| 1268 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1269 | session_process_counters(s); |
| 1270 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1271 | if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1272 | s->rep->cons->update(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1273 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1274 | if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1275 | s->req->cons->update(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | ea38854 | 2009-06-21 21:45:58 +0200 | [diff] [blame] | 1277 | s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL); |
| 1278 | s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1279 | s->si[0].prev_state = s->si[0].state; |
| 1280 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1281 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1282 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1283 | |
| 1284 | /* Trick: if a request is being waiting for the server to respond, |
| 1285 | * and if we know the server can timeout, we don't want the timeout |
| 1286 | * to expire on the client side first, but we're still interested |
| 1287 | * in passing data from the client to the server (eg: POST). Thus, |
| 1288 | * we can cancel the client's request timeout if the server's |
| 1289 | * request timeout is set and the server has not yet sent a response. |
| 1290 | */ |
| 1291 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1292 | if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1293 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1294 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1295 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1296 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1297 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1298 | /* Call the second stream interface's I/O handler if it's embedded. |
| 1299 | * Note that this one may wake the task up again. |
| 1300 | */ |
| 1301 | if (s->req->cons->iohandler) { |
| 1302 | s->req->cons->iohandler(s->req->cons); |
| 1303 | if (task_in_rq(t)) { |
| 1304 | /* If we woke up, we don't want to requeue the |
| 1305 | * task to the wait queue, but rather requeue |
| 1306 | * it into the runqueue ASAP. |
| 1307 | */ |
| 1308 | t->expire = TICK_ETERNITY; |
| 1309 | return t; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1313 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1314 | tick_first(s->rep->rex, s->rep->wex)); |
| 1315 | if (s->req->analysers) |
| 1316 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1317 | |
| 1318 | if (s->si[0].exp) |
| 1319 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1320 | |
| 1321 | if (s->si[1].exp) |
| 1322 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1323 | |
| 1324 | #ifdef DEBUG_FULL |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 1325 | fprintf(stderr, |
| 1326 | "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u" |
| 1327 | " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n", |
| 1328 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, |
| 1329 | s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1330 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1331 | |
| 1332 | #ifdef DEBUG_DEV |
| 1333 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
Willy Tarreau | d0a201b | 2009-03-08 15:53:06 +0100 | [diff] [blame] | 1334 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1335 | ABORT_NOW(); |
| 1336 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1337 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | s->fe->feconn--; |
| 1341 | if (s->flags & SN_BE_ASSIGNED) |
| 1342 | s->be->beconn--; |
| 1343 | actconn--; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 1344 | s->listener->nbconn--; |
| 1345 | if (s->listener->state == LI_FULL && |
| 1346 | s->listener->nbconn < s->listener->maxconn) { |
| 1347 | /* we should reactivate the listener */ |
| 1348 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1349 | s->listener->state = LI_READY; |
| 1350 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1351 | |
| 1352 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1353 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1354 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1355 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1356 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1357 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1358 | write(1, trash, len); |
| 1359 | } |
| 1360 | |
| 1361 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1362 | session_process_counters(s); |
| 1363 | |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1364 | if (s->txn.status) { |
| 1365 | int n; |
| 1366 | |
| 1367 | n = s->txn.status / 100; |
| 1368 | if (n < 1 || n > 5) |
| 1369 | n = 0; |
| 1370 | |
| 1371 | if (s->fe->mode == PR_MODE_HTTP) |
| 1372 | s->fe->counters.p.http.rsp[n]++; |
| 1373 | |
| 1374 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 1375 | (s->be->mode == PR_MODE_HTTP)) |
| 1376 | s->be->counters.p.http.rsp[n]++; |
| 1377 | } |
| 1378 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1379 | /* let's do a final log if we need it */ |
| 1380 | if (s->logs.logwait && |
| 1381 | !(s->flags & SN_MONITOR) && |
| 1382 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1383 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1387 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1388 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1389 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1390 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1393 | /* |
| 1394 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1395 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1396 | * current connection slot. This function also notifies any LB algo which might |
| 1397 | * expect to be informed about any change in the number of active sessions on a |
| 1398 | * server. |
| 1399 | */ |
| 1400 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1401 | { |
| 1402 | if (sess->srv_conn == newsrv) |
| 1403 | return; |
| 1404 | |
| 1405 | if (sess->srv_conn) { |
| 1406 | sess->srv_conn->served--; |
| 1407 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1408 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1409 | sess->srv_conn = NULL; |
| 1410 | } |
| 1411 | |
| 1412 | if (newsrv) { |
| 1413 | newsrv->served++; |
| 1414 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1415 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1416 | sess->srv_conn = newsrv; |
| 1417 | } |
| 1418 | } |
| 1419 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1420 | /* Set correct session termination flags in case no analyser has done it. It |
| 1421 | * also counts a failed request if the server state has not reached the request |
| 1422 | * stage. |
| 1423 | */ |
| 1424 | void sess_set_term_flags(struct session *s) |
| 1425 | { |
| 1426 | if (!(s->flags & SN_FINST_MASK)) { |
| 1427 | if (s->si[1].state < SI_ST_REQ) { |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1428 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1429 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1430 | if (s->listener->counters) |
| 1431 | s->listener->counters->failed_req++; |
| 1432 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1433 | s->flags |= SN_FINST_R; |
| 1434 | } |
| 1435 | else if (s->si[1].state == SI_ST_QUE) |
| 1436 | s->flags |= SN_FINST_Q; |
| 1437 | else if (s->si[1].state < SI_ST_EST) |
| 1438 | s->flags |= SN_FINST_C; |
| 1439 | else if (s->si[1].state == SI_ST_EST) |
| 1440 | s->flags |= SN_FINST_D; |
| 1441 | else |
| 1442 | s->flags |= SN_FINST_L; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 1447 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 1448 | * session termination flags so that the caller does not have to worry about |
| 1449 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 1450 | */ |
| 1451 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 1452 | { |
| 1453 | int err_type = si->err_type; |
| 1454 | int err = 0, fin = 0; |
| 1455 | |
| 1456 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 1457 | err = SN_ERR_CLICL; |
| 1458 | fin = SN_FINST_Q; |
| 1459 | } |
| 1460 | else if (err_type & SI_ET_CONN_ABRT) { |
| 1461 | err = SN_ERR_CLICL; |
| 1462 | fin = SN_FINST_C; |
| 1463 | } |
| 1464 | else if (err_type & SI_ET_QUEUE_TO) { |
| 1465 | err = SN_ERR_SRVTO; |
| 1466 | fin = SN_FINST_Q; |
| 1467 | } |
| 1468 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 1469 | err = SN_ERR_SRVCL; |
| 1470 | fin = SN_FINST_Q; |
| 1471 | } |
| 1472 | else if (err_type & SI_ET_CONN_TO) { |
| 1473 | err = SN_ERR_SRVTO; |
| 1474 | fin = SN_FINST_C; |
| 1475 | } |
| 1476 | else if (err_type & SI_ET_CONN_ERR) { |
| 1477 | err = SN_ERR_SRVCL; |
| 1478 | fin = SN_FINST_C; |
| 1479 | } |
| 1480 | else /* SI_ET_CONN_OTHER and others */ { |
| 1481 | err = SN_ERR_INTERNAL; |
| 1482 | fin = SN_FINST_C; |
| 1483 | } |
| 1484 | |
| 1485 | if (!(s->flags & SN_ERR_MASK)) |
| 1486 | s->flags |= err; |
| 1487 | if (!(s->flags & SN_FINST_MASK)) |
| 1488 | s->flags |= fin; |
| 1489 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1490 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1491 | /* |
| 1492 | * Local variables: |
| 1493 | * c-indent-level: 8 |
| 1494 | * c-basic-offset: 8 |
| 1495 | * End: |
| 1496 | */ |