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 | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 22 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 23 | #include <proto/buffers.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 24 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 25 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 27 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <proto/queue.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/stream_sock.h> |
| 33 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | |
| 35 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 36 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 37 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * frees the context associated to a session. It must have been removed first. |
| 41 | */ |
| 42 | void session_free(struct session *s) |
| 43 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 44 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 45 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 46 | struct bref *bref, *back; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 47 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | if (s->pend_pos) |
| 49 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 51 | if (s->srv) { /* there may be requests left pending in queue */ |
| 52 | if (s->flags & SN_CURR_SESS) { |
| 53 | s->flags &= ~SN_CURR_SESS; |
| 54 | s->srv->cur_sess--; |
| 55 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 56 | if (may_dequeue_tasks(s->srv, s->be)) |
| 57 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 58 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 59 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 60 | if (unlikely(s->srv_conn)) { |
| 61 | /* the session still has a reserved slot on a server, but |
| 62 | * it should normally be only the same as the one above, |
| 63 | * so this should not happen in fact. |
| 64 | */ |
| 65 | sess_change_server(s, NULL); |
| 66 | } |
| 67 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 68 | if (s->req->pipe) |
| 69 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 70 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 71 | if (s->rep->pipe) |
| 72 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 73 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 74 | pool_free2(pool2_buffer, s->req); |
| 75 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 77 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 78 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 79 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 80 | if (txn->rsp.cap != NULL) { |
| 81 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 82 | for (h = fe->rsp_cap; h; h = h->next) |
| 83 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 84 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 86 | if (txn->req.cap != NULL) { |
| 87 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 88 | for (h = fe->req_cap; h; h = h->next) |
| 89 | pool_free2(h->pool, txn->req.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 90 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | } |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 93 | pool_free2(pool2_requri, txn->uri); |
| 94 | pool_free2(pool2_capture, txn->cli_cookie); |
| 95 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 96 | |
| 97 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
| 98 | LIST_DEL(&bref->users); |
| 99 | LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users); |
| 100 | bref->ref = s->list.n; |
| 101 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 102 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 103 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 104 | |
| 105 | /* 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] | 106 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 107 | pool_flush2(pool2_buffer); |
| 108 | pool_flush2(fe->hdr_idx_pool); |
| 109 | pool_flush2(pool2_requri); |
| 110 | pool_flush2(pool2_capture); |
| 111 | pool_flush2(pool2_session); |
| 112 | pool_flush2(fe->req_cap_pool); |
| 113 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 114 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 119 | int init_session() |
| 120 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 121 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 122 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 123 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 126 | void session_process_counters(struct session *s) |
| 127 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 128 | unsigned long long bytes; |
| 129 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 130 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 131 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 132 | s->logs.bytes_in = s->req->total; |
| 133 | if (bytes) { |
| 134 | s->fe->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->be != s->fe) |
| 137 | s->be->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 138 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 139 | if (s->srv) |
| 140 | s->srv->bytes_in += bytes; |
| 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) { |
| 148 | s->fe->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) |
| 151 | s->be->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) |
| 154 | s->srv->bytes_out += bytes; |
| 155 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 159 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 160 | * connection was attempted and that the file descriptor is already allocated. |
| 161 | * We must check for establishment, error and abort. Possible output states |
| 162 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 163 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 164 | * otherwise 1. |
| 165 | */ |
| 166 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 167 | { |
| 168 | struct buffer *req = si->ob; |
| 169 | struct buffer *rep = si->ib; |
| 170 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 171 | /* If we got an error, or if nothing happened and the connection timed |
| 172 | * out, we must give up. The CER state handler will take care of retry |
| 173 | * attempts and error reports. |
| 174 | */ |
| 175 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
| 176 | si->state = SI_ST_CER; |
| 177 | fd_delete(si->fd); |
| 178 | |
| 179 | if (si->err_type) |
| 180 | return 0; |
| 181 | |
| 182 | si->err_loc = s->srv; |
| 183 | if (si->flags & SI_FL_ERR) |
| 184 | si->err_type = SI_ET_CONN_ERR; |
| 185 | else |
| 186 | si->err_type = SI_ET_CONN_TO; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* OK, maybe we want to abort */ |
| 191 | if (unlikely((req->flags & BF_SHUTW_NOW) || |
| 192 | (rep->flags & BF_SHUTW) || |
| 193 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | efc612c | 2009-01-09 12:18:24 +0100 | [diff] [blame] | 194 | (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 195 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 196 | /* give up */ |
| 197 | si->shutw(si); |
| 198 | si->err_type |= SI_ET_CONN_ABRT; |
| 199 | si->err_loc = s->srv; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* we need to wait a bit more if there was no activity either */ |
| 204 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 205 | return 1; |
| 206 | |
| 207 | /* OK, this means that a connection succeeded. The caller will be |
| 208 | * responsible for handling the transition from CON to EST. |
| 209 | */ |
| 210 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 211 | si->state = SI_ST_EST; |
| 212 | si->err_type = SI_ET_NONE; |
| 213 | si->err_loc = NULL; |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 218 | * previous connection attempt has failed and that the file descriptor |
| 219 | * has already been released. Possible causes include asynchronous error |
| 220 | * notification and time out. Possible output states are SI_ST_CLO when |
| 221 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 222 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 223 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 224 | * marked as in error state. It returns 0. |
| 225 | */ |
| 226 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 227 | { |
| 228 | /* we probably have to release last session from the server */ |
| 229 | if (s->srv) { |
| 230 | if (s->flags & SN_CURR_SESS) { |
| 231 | s->flags &= ~SN_CURR_SESS; |
| 232 | s->srv->cur_sess--; |
| 233 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* ensure that we have enough retries left */ |
| 237 | s->conn_retries--; |
| 238 | if (s->conn_retries < 0) { |
| 239 | if (!si->err_type) { |
| 240 | si->err_type = SI_ET_CONN_ERR; |
| 241 | si->err_loc = s->srv; |
| 242 | } |
| 243 | |
| 244 | if (s->srv) |
| 245 | s->srv->failed_conns++; |
| 246 | s->be->failed_conns++; |
| 247 | if (may_dequeue_tasks(s->srv, s->be)) |
| 248 | process_srv_queue(s->srv); |
| 249 | |
| 250 | /* shutw is enough so stop a connecting socket */ |
| 251 | si->shutw(si); |
| 252 | si->ob->flags |= BF_WRITE_ERROR; |
| 253 | si->ib->flags |= BF_READ_ERROR; |
| 254 | |
| 255 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 256 | if (s->srv_error) |
| 257 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 262 | * retry on another server for the last retry. In order to achieve this, |
| 263 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 264 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 265 | * redispatch yet, because this will depend on what server is selected. |
| 266 | */ |
| 267 | if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) { |
| 268 | if (may_dequeue_tasks(s->srv, s->be)) |
| 269 | process_srv_queue(s->srv); |
| 270 | |
| 271 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 272 | s->prev_srv = s->srv; |
| 273 | si->state = SI_ST_REQ; |
| 274 | } else { |
| 275 | if (s->srv) |
| 276 | s->srv->retries++; |
| 277 | s->be->retries++; |
| 278 | si->state = SI_ST_ASS; |
| 279 | } |
| 280 | |
| 281 | if (si->flags & SI_FL_ERR) { |
| 282 | /* The error was an asynchronous connection error, and we will |
| 283 | * likely have to retry connecting to the same server, most |
| 284 | * likely leading to the same result. To avoid this, we wait |
| 285 | * one second before retrying. |
| 286 | */ |
| 287 | |
| 288 | if (!si->err_type) |
| 289 | si->err_type = SI_ET_CONN_ERR; |
| 290 | |
| 291 | si->state = SI_ST_TAR; |
| 292 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 293 | return 0; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * This function handles the transition between the SI_ST_CON state and the |
| 300 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON to |
| 301 | * SI_ST_EST. |
| 302 | */ |
| 303 | void sess_establish(struct session *s, struct stream_interface *si) |
| 304 | { |
| 305 | struct buffer *req = si->ob; |
| 306 | struct buffer *rep = si->ib; |
| 307 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 308 | 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] | 309 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 310 | |
| 311 | /* if the user wants to log as soon as possible, without counting |
| 312 | * bytes from the server, then this is the right moment. */ |
| 313 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 314 | 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] | 315 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 316 | } |
| 317 | #ifdef CONFIG_HAP_TCPSPLICE |
| 318 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
| 319 | /* TCP splicing supported by both FE and BE */ |
| 320 | tcp_splice_splicefd(req->prod->fd, si->fd, 0); |
| 321 | } |
| 322 | #endif |
| 323 | } |
| 324 | else { |
| 325 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 326 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 327 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 328 | /* reset hdr_idx which was already initialized by the request. |
| 329 | * right now, the http parser does it. |
| 330 | * hdr_idx_init(&s->txn.hdr_idx); |
| 331 | */ |
| 332 | } |
| 333 | |
| 334 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 335 | req->wex = TICK_ETERNITY; |
| 336 | } |
| 337 | |
| 338 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 339 | * Other input states are simply ignored. |
| 340 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 341 | * Flags must have previously been updated for timeouts and other conditions. |
| 342 | */ |
| 343 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 344 | { |
| 345 | 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", |
| 346 | now_ms, __FUNCTION__, |
| 347 | s, |
| 348 | s->req, s->rep, |
| 349 | s->req->rex, s->rep->wex, |
| 350 | s->req->flags, s->rep->flags, |
| 351 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 352 | |
| 353 | if (si->state == SI_ST_ASS) { |
| 354 | /* Server assigned to connection request, we have to try to connect now */ |
| 355 | int conn_err; |
| 356 | |
| 357 | conn_err = connect_server(s); |
| 358 | if (conn_err == SN_ERR_NONE) { |
| 359 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 360 | if (s->srv) |
| 361 | s->srv->cum_sess++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
| 365 | /* We have received a synchronous error. We might have to |
| 366 | * abort, retry immediately or redispatch. |
| 367 | */ |
| 368 | if (conn_err == SN_ERR_INTERNAL) { |
| 369 | if (!si->err_type) { |
| 370 | si->err_type = SI_ET_CONN_OTHER; |
| 371 | si->err_loc = s->srv; |
| 372 | } |
| 373 | |
| 374 | if (s->srv) |
| 375 | s->srv->cum_sess++; |
| 376 | if (s->srv) |
| 377 | s->srv->failed_conns++; |
| 378 | s->be->failed_conns++; |
| 379 | |
| 380 | /* release other sessions waiting for this server */ |
| 381 | if (may_dequeue_tasks(s->srv, s->be)) |
| 382 | process_srv_queue(s->srv); |
| 383 | |
| 384 | /* Failed and not retryable. */ |
| 385 | si->shutr(si); |
| 386 | si->shutw(si); |
| 387 | si->ob->flags |= BF_WRITE_ERROR; |
| 388 | |
| 389 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 390 | |
| 391 | /* no session was ever accounted for this server */ |
| 392 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 393 | if (s->srv_error) |
| 394 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
| 398 | /* We are facing a retryable error, but we don't want to run a |
| 399 | * turn-around now, as the problem is likely a source port |
| 400 | * allocation problem, so we want to retry now. |
| 401 | */ |
| 402 | si->state = SI_ST_CER; |
| 403 | si->flags &= ~SI_FL_ERR; |
| 404 | sess_update_st_cer(s, si); |
| 405 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 406 | return; |
| 407 | } |
| 408 | else if (si->state == SI_ST_QUE) { |
| 409 | /* connection request was queued, check for any update */ |
| 410 | if (!s->pend_pos) { |
| 411 | /* The connection is not in the queue anymore. Either |
| 412 | * we have a server connection slot available and we |
| 413 | * go directly to the assigned state, or we need to |
| 414 | * load-balance first and go to the INI state. |
| 415 | */ |
| 416 | si->exp = TICK_ETERNITY; |
| 417 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 418 | si->state = SI_ST_REQ; |
| 419 | else { |
| 420 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 421 | si->state = SI_ST_ASS; |
| 422 | } |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | /* Connection request still in queue... */ |
| 427 | if (si->flags & SI_FL_EXP) { |
| 428 | /* ... and timeout expired */ |
| 429 | si->exp = TICK_ETERNITY; |
| 430 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 431 | if (s->srv) |
| 432 | s->srv->failed_conns++; |
| 433 | s->be->failed_conns++; |
| 434 | si->shutr(si); |
| 435 | si->shutw(si); |
| 436 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 437 | if (!si->err_type) |
| 438 | si->err_type = SI_ET_QUEUE_TO; |
| 439 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 440 | if (s->srv_error) |
| 441 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
| 445 | /* Connection remains in queue, check if we have to abort it */ |
| 446 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 447 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 448 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
| 449 | /* give up */ |
| 450 | si->exp = TICK_ETERNITY; |
| 451 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 452 | si->shutr(si); |
| 453 | si->shutw(si); |
| 454 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 455 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 456 | if (s->srv_error) |
| 457 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
| 461 | /* Nothing changed */ |
| 462 | return; |
| 463 | } |
| 464 | else if (si->state == SI_ST_TAR) { |
| 465 | /* Connection request might be aborted */ |
| 466 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 467 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 468 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
| 469 | /* give up */ |
| 470 | si->exp = TICK_ETERNITY; |
| 471 | si->shutr(si); |
| 472 | si->shutw(si); |
| 473 | si->err_type |= SI_ET_CONN_ABRT; |
| 474 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 475 | if (s->srv_error) |
| 476 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 477 | return; |
| 478 | } |
| 479 | |
| 480 | if (!(si->flags & SI_FL_EXP)) |
| 481 | return; /* still in turn-around */ |
| 482 | |
| 483 | si->exp = TICK_ETERNITY; |
| 484 | |
| 485 | /* we keep trying on the same server as long as the session is |
| 486 | * marked "assigned". |
| 487 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 488 | */ |
| 489 | if (s->flags & SN_ASSIGNED) |
| 490 | si->state = SI_ST_ASS; |
| 491 | else |
| 492 | si->state = SI_ST_REQ; |
| 493 | return; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* This function initiates a server connection request on a stream interface |
| 498 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 499 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 500 | * or SI_ST_CLO upon error. |
| 501 | */ |
| 502 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 503 | 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", |
| 504 | now_ms, __FUNCTION__, |
| 505 | s, |
| 506 | s->req, s->rep, |
| 507 | s->req->rex, s->rep->wex, |
| 508 | s->req->flags, s->rep->flags, |
| 509 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 510 | |
| 511 | if (si->state != SI_ST_REQ) |
| 512 | return; |
| 513 | |
| 514 | /* Try to assign a server */ |
| 515 | if (srv_redispatch_connect(s) != 0) { |
| 516 | /* We did not get a server. Either we queued the |
| 517 | * connection request, or we encountered an error. |
| 518 | */ |
| 519 | if (si->state == SI_ST_QUE) |
| 520 | return; |
| 521 | |
| 522 | /* we did not get any server, let's check the cause */ |
| 523 | si->shutr(si); |
| 524 | si->shutw(si); |
| 525 | si->ob->flags |= BF_WRITE_ERROR; |
| 526 | if (!si->err_type) |
| 527 | si->err_type = SI_ET_CONN_OTHER; |
| 528 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 529 | if (s->srv_error) |
| 530 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
| 534 | /* The server is assigned */ |
| 535 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 536 | si->state = SI_ST_ASS; |
| 537 | } |
| 538 | |
| 539 | /* Processes the client, server, request and response jobs of a session task, |
| 540 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 541 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 542 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 543 | * nothing too many times, the request and response buffers flags are monitored |
| 544 | * and each function is called only if at least another function has changed at |
| 545 | * least one flag it is interested in. |
| 546 | */ |
| 547 | void process_session(struct task *t, int *next) |
| 548 | { |
| 549 | struct session *s = t->context; |
| 550 | int resync; |
| 551 | unsigned int rqf_last, rpf_last; |
| 552 | |
| 553 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 554 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 555 | |
| 556 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 557 | * stream interfaces when their timeouts have expired. |
| 558 | */ |
| 559 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 560 | stream_int_check_timeouts(&s->si[0]); |
| 561 | stream_int_check_timeouts(&s->si[1]); |
| 562 | buffer_check_timeouts(s->req); |
| 563 | buffer_check_timeouts(s->rep); |
| 564 | } |
| 565 | |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 566 | s->req->flags &= ~BF_READ_NOEXP; |
| 567 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 568 | /* copy req/rep flags so that we can detect shutdowns */ |
| 569 | rqf_last = s->req->flags; |
| 570 | rpf_last = s->rep->flags; |
| 571 | |
| 572 | /* 1b: check for low-level errors reported at the stream interface. |
| 573 | * First we check if it's a retryable error (in which case we don't |
| 574 | * want to tell the buffer). Otherwise we report the error one level |
| 575 | * upper by setting flags into the buffers. Note that the side towards |
| 576 | * the client cannot have connect (hence retryable) errors. Also, the |
| 577 | * connection setup code must be able to deal with any type of abort. |
| 578 | */ |
| 579 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 580 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 581 | s->si[0].shutr(&s->si[0]); |
| 582 | s->si[0].shutw(&s->si[0]); |
| 583 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 584 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 585 | if (!(s->flags & SN_ERR_MASK)) |
| 586 | s->flags |= SN_ERR_CLICL; |
| 587 | if (!(s->flags & SN_FINST_MASK)) |
| 588 | s->flags |= SN_FINST_D; |
| 589 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 594 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 595 | s->si[1].shutr(&s->si[1]); |
| 596 | s->si[1].shutw(&s->si[1]); |
| 597 | stream_int_report_error(&s->si[1]); |
| 598 | s->be->failed_resp++; |
| 599 | if (s->srv) |
| 600 | s->srv->failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 601 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 602 | if (!(s->flags & SN_ERR_MASK)) |
| 603 | s->flags |= SN_ERR_SRVCL; |
| 604 | if (!(s->flags & SN_FINST_MASK)) |
| 605 | s->flags |= SN_FINST_D; |
| 606 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 607 | } |
| 608 | /* note: maybe we should process connection errors here ? */ |
| 609 | } |
| 610 | |
| 611 | if (s->si[1].state == SI_ST_CON) { |
| 612 | /* we were trying to establish a connection on the server side, |
| 613 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 614 | */ |
| 615 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 616 | sess_update_st_cer(s, &s->si[1]); |
| 617 | else if (s->si[1].state == SI_ST_EST) |
| 618 | sess_establish(s, &s->si[1]); |
| 619 | |
| 620 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 621 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 622 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 623 | */ |
| 624 | } |
| 625 | |
| 626 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 627 | * for future reads or writes. Note: this will also concern upper layers |
| 628 | * but we do not touch any other flag. We must be careful and correctly |
| 629 | * detect state changes when calling them. |
| 630 | */ |
| 631 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 632 | if (s->req->flags & BF_READ_TIMEOUT) |
| 633 | s->req->prod->shutr(s->req->prod); |
| 634 | if (s->req->flags & BF_WRITE_TIMEOUT) |
| 635 | s->req->cons->shutw(s->req->cons); |
| 636 | DPRINTF(stderr, |
| 637 | "[%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", |
| 638 | now_ms, __FUNCTION__, __LINE__, |
| 639 | t, |
| 640 | s, s->flags, |
| 641 | s->req, s->rep, |
| 642 | s->req->rex, s->rep->wex, |
| 643 | s->req->flags, s->rep->flags, |
| 644 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 645 | s->rep->cons->err_type, s->req->cons->err_type, |
| 646 | s->conn_retries); |
| 647 | } |
| 648 | |
| 649 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 650 | if (s->rep->flags & BF_READ_TIMEOUT) |
| 651 | s->rep->prod->shutr(s->rep->prod); |
| 652 | if (s->rep->flags & BF_WRITE_TIMEOUT) |
| 653 | s->rep->cons->shutw(s->rep->cons); |
| 654 | DPRINTF(stderr, |
| 655 | "[%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", |
| 656 | now_ms, __FUNCTION__, __LINE__, |
| 657 | t, |
| 658 | s, s->flags, |
| 659 | s->req, s->rep, |
| 660 | s->req->rex, s->rep->wex, |
| 661 | s->req->flags, s->rep->flags, |
| 662 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 663 | s->rep->cons->err_type, s->req->cons->err_type, |
| 664 | s->conn_retries); |
| 665 | } |
| 666 | |
| 667 | /* Check for connection closure */ |
| 668 | |
| 669 | resync_stream_interface: |
| 670 | DPRINTF(stderr, |
| 671 | "[%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", |
| 672 | now_ms, __FUNCTION__, __LINE__, |
| 673 | t, |
| 674 | s, s->flags, |
| 675 | s->req, s->rep, |
| 676 | s->req->rex, s->rep->wex, |
| 677 | s->req->flags, s->rep->flags, |
| 678 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 679 | s->rep->cons->err_type, s->req->cons->err_type, |
| 680 | s->conn_retries); |
| 681 | |
| 682 | /* nothing special to be done on client side */ |
| 683 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 684 | s->req->prod->state = SI_ST_CLO; |
| 685 | |
| 686 | /* When a server-side connection is released, we have to count it and |
| 687 | * check for pending connections on this server. |
| 688 | */ |
| 689 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 690 | s->req->cons->state = SI_ST_CLO; |
| 691 | if (s->srv) { |
| 692 | if (s->flags & SN_CURR_SESS) { |
| 693 | s->flags &= ~SN_CURR_SESS; |
| 694 | s->srv->cur_sess--; |
| 695 | } |
| 696 | sess_change_server(s, NULL); |
| 697 | if (may_dequeue_tasks(s->srv, s->be)) |
| 698 | process_srv_queue(s->srv); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 704 | * at this point. |
| 705 | */ |
| 706 | |
| 707 | /**** Process layer 7 below ****/ |
| 708 | |
| 709 | resync = 0; |
| 710 | |
| 711 | /* Analyse request */ |
| 712 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 713 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 714 | unsigned int flags = s->req->flags; |
| 715 | |
| 716 | if (s->req->prod->state >= SI_ST_EST) { |
| 717 | /* it's up to the analysers to reset write_ena */ |
| 718 | buffer_write_ena(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 719 | |
| 720 | /* We will call all analysers for which a bit is set in |
| 721 | * s->req->analysers, following the bit order from LSB |
| 722 | * to MSB. The analysers must remove themselves from |
| 723 | * the list when not needed. This while() loop is in |
| 724 | * fact a cleaner if(). |
| 725 | */ |
| 726 | while (s->req->analysers) { |
| 727 | if (s->req->analysers & AN_REQ_INSPECT) |
| 728 | if (!tcp_inspect_request(s, s->req)) |
| 729 | break; |
| 730 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 731 | if (s->req->analysers & AN_REQ_HTTP_HDR) |
| 732 | if (!http_process_request(s, s->req)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 733 | break; |
| 734 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 735 | if (s->req->analysers & AN_REQ_HTTP_TARPIT) |
| 736 | if (!http_process_tarpit(s, s->req)) |
| 737 | break; |
| 738 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 739 | if (s->req->analysers & AN_REQ_HTTP_BODY) |
| 740 | if (!http_process_request_body(s, s->req)) |
| 741 | break; |
| 742 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 743 | /* Just make sure that nobody set a wrong flag causing an endless loop */ |
| 744 | s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
| 745 | |
| 746 | /* we don't want to loop anyway */ |
| 747 | break; |
| 748 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 749 | } |
| 750 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 751 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 752 | if (s->req->flags != flags) |
| 753 | resync = 1; |
| 754 | } |
| 755 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 756 | /* if noone is interested in analysing data, let's forward everything |
| 757 | * and only wake up every 1-2 MB. We still wake up when send_max is |
| 758 | * reached though. |
| 759 | */ |
| 760 | if (!s->req->send_max && s->req->prod->state >= SI_ST_EST && |
| 761 | !s->req->analysers && !(s->req->flags & BF_HIJACK)) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 762 | /* check if it is wise to enable kernel splicing on the request buffer */ |
| 763 | if (!(s->req->flags & BF_KERN_SPLICING) && |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 764 | (pipes_used < global.maxpipes) && |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 765 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 766 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 767 | (s->req->flags & BF_STREAMER_FAST)))) |
| 768 | s->req->flags |= BF_KERN_SPLICING; |
| 769 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 770 | if (s->req->to_forward < FORWARD_DEFAULT_SIZE) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 771 | buffer_forward(s->req, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 772 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 773 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 774 | /* reflect what the L7 analysers have seen last */ |
| 775 | rqf_last = s->req->flags; |
| 776 | |
| 777 | /* |
| 778 | * Now forward all shutdown requests between both sides of the buffer |
| 779 | */ |
| 780 | |
| 781 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 782 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 783 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 784 | buffer_shutw_now(s->req); |
| 785 | else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) && |
| 786 | (s->req->cons->state == SI_ST_EST) && |
| 787 | s->be->options & PR_O_FORCE_CLO && |
| 788 | s->rep->flags & BF_READ_ACTIVITY) { |
| 789 | /* We want to force the connection to the server to close, |
| 790 | * and the server has begun to respond. That's the right |
| 791 | * time. |
| 792 | */ |
| 793 | buffer_shutw_now(s->req); |
| 794 | } |
| 795 | |
| 796 | /* shutdown(write) pending */ |
| 797 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 798 | s->req->cons->shutw(s->req->cons); |
| 799 | |
| 800 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 801 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 802 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 803 | buffer_shutr_now(s->req); |
| 804 | |
| 805 | /* shutdown(read) pending */ |
| 806 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 807 | s->req->prod->shutr(s->req->prod); |
| 808 | |
| 809 | /* it's possible that an upper layer has requested a connection setup */ |
| 810 | if (s->req->cons->state == SI_ST_INI && |
| 811 | (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA) |
| 812 | s->req->cons->state = SI_ST_REQ; |
| 813 | |
| 814 | /* we may have a pending connection request, or a connection waiting |
| 815 | * for completion. |
| 816 | */ |
| 817 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 818 | do { |
| 819 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 820 | * to give a chance to step 2 to perform a redirect if needed. |
| 821 | */ |
| 822 | if (s->si[1].state != SI_ST_REQ) |
| 823 | sess_update_stream_int(s, &s->si[1]); |
| 824 | if (s->si[1].state == SI_ST_REQ) |
| 825 | sess_prepare_conn_req(s, &s->si[1]); |
| 826 | |
| 827 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 828 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 829 | perform_http_redirect(s, &s->si[1]); |
| 830 | } while (s->si[1].state == SI_ST_ASS); |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * Here we want to check if we need to resync or not. |
| 835 | */ |
| 836 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
| 837 | resync = 1; |
| 838 | |
| 839 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 840 | |
| 841 | /* according to benchmarks, it makes sense to resync now */ |
| 842 | if (resync) |
| 843 | goto resync_stream_interface; |
| 844 | |
| 845 | |
| 846 | /* Analyse response */ |
| 847 | |
| 848 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 849 | /* In inject mode, we wake up everytime something has |
| 850 | * happened on the write side of the buffer. |
| 851 | */ |
| 852 | unsigned int flags = s->rep->flags; |
| 853 | |
| 854 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 855 | !(s->rep->flags & BF_FULL)) { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 856 | s->rep->hijacker(s, s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 857 | } |
| 858 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 859 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 860 | if (s->rep->flags != flags) |
| 861 | resync = 1; |
| 862 | } |
| 863 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 864 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 865 | unsigned int flags = s->rep->flags; |
| 866 | |
| 867 | if (s->rep->prod->state >= SI_ST_EST) { |
| 868 | /* it's up to the analysers to reset write_ena */ |
| 869 | buffer_write_ena(s->rep); |
| 870 | if (s->rep->analysers) |
| 871 | process_response(s); |
| 872 | } |
| 873 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 874 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 875 | if (s->rep->flags != flags) |
| 876 | resync = 1; |
| 877 | } |
| 878 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 879 | /* if noone is interested in analysing data, let's forward everything |
| 880 | * and only wake up every 1-2 MB. We still wake up when send_max is |
| 881 | * reached though. |
| 882 | */ |
| 883 | if (!s->rep->send_max && s->rep->prod->state >= SI_ST_EST && |
| 884 | !s->rep->analysers && !(s->rep->flags & BF_HIJACK)) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 885 | /* check if it is wise to enable kernel splicing on the response buffer */ |
| 886 | if (!(s->rep->flags & BF_KERN_SPLICING) && |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 887 | (pipes_used < global.maxpipes) && |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 888 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 889 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 890 | (s->rep->flags & BF_STREAMER_FAST)))) |
| 891 | s->rep->flags |= BF_KERN_SPLICING; |
| 892 | |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 893 | if (s->rep->to_forward < FORWARD_DEFAULT_SIZE) |
| 894 | buffer_forward(s->rep, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 895 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 896 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 897 | /* reflect what the L7 analysers have seen last */ |
| 898 | rpf_last = s->rep->flags; |
| 899 | |
| 900 | /* |
| 901 | * Now forward all shutdown requests between both sides of the buffer |
| 902 | */ |
| 903 | |
| 904 | /* |
| 905 | * FIXME: this is probably where we should produce error responses. |
| 906 | */ |
| 907 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 908 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 909 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 910 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 911 | buffer_shutw_now(s->rep); |
| 912 | |
| 913 | /* shutdown(write) pending */ |
| 914 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 915 | s->rep->cons->shutw(s->rep->cons); |
| 916 | |
| 917 | /* 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] | 918 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 919 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 920 | buffer_shutr_now(s->rep); |
| 921 | |
| 922 | /* shutdown(read) pending */ |
| 923 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 924 | s->rep->prod->shutr(s->rep->prod); |
| 925 | |
| 926 | /* |
| 927 | * Here we want to check if we need to resync or not. |
| 928 | */ |
| 929 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
| 930 | resync = 1; |
| 931 | |
| 932 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 933 | |
| 934 | if (resync) |
| 935 | goto resync_stream_interface; |
| 936 | |
| 937 | |
| 938 | /* This is needed only when debugging is enabled, to indicate |
| 939 | * client-side or server-side close. Please note that in the unlikely |
| 940 | * event where both sides would close at once, the sequence is reported |
| 941 | * on the server side first. |
| 942 | */ |
| 943 | if (unlikely((global.mode & MODE_DEBUG) && |
| 944 | (!(global.mode & MODE_QUIET) || |
| 945 | (global.mode & MODE_VERBOSE)))) { |
| 946 | int len; |
| 947 | |
| 948 | if (s->si[1].state == SI_ST_CLO && |
| 949 | s->si[1].prev_state == SI_ST_EST) { |
| 950 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 951 | s->uniq_id, s->be->id, |
| 952 | (unsigned short)s->si[0].fd, |
| 953 | (unsigned short)s->si[1].fd); |
| 954 | write(1, trash, len); |
| 955 | } |
| 956 | |
| 957 | if (s->si[0].state == SI_ST_CLO && |
| 958 | s->si[0].prev_state == SI_ST_EST) { |
| 959 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 960 | s->uniq_id, s->be->id, |
| 961 | (unsigned short)s->si[0].fd, |
| 962 | (unsigned short)s->si[1].fd); |
| 963 | write(1, trash, len); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 968 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 969 | |
| 970 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 971 | session_process_counters(s); |
| 972 | |
| 973 | if (s->rep->cons->state == SI_ST_EST) |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 974 | stream_sock_data_finish(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 975 | |
| 976 | if (s->req->cons->state == SI_ST_EST) |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 977 | stream_sock_data_finish(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 978 | |
| 979 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 980 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 981 | s->si[0].prev_state = s->si[0].state; |
| 982 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 983 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 984 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 985 | |
| 986 | /* Trick: if a request is being waiting for the server to respond, |
| 987 | * and if we know the server can timeout, we don't want the timeout |
| 988 | * to expire on the client side first, but we're still interested |
| 989 | * in passing data from the client to the server (eg: POST). Thus, |
| 990 | * we can cancel the client's request timeout if the server's |
| 991 | * request timeout is set and the server has not yet sent a response. |
| 992 | */ |
| 993 | |
| 994 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 995 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 996 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 997 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 998 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 999 | |
| 1000 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1001 | tick_first(s->rep->rex, s->rep->wex)); |
| 1002 | if (s->req->analysers) |
| 1003 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1004 | |
| 1005 | if (s->si[0].exp) |
| 1006 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1007 | |
| 1008 | if (s->si[1].exp) |
| 1009 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1010 | |
| 1011 | #ifdef DEBUG_FULL |
| 1012 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u, cs=%d, ss=%d\n", |
| 1013 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex, s->si[0].state, s->si[1].state); |
| 1014 | #endif |
| 1015 | /* restore t to its place in the task list */ |
| 1016 | task_queue(t); |
| 1017 | |
| 1018 | #ifdef DEBUG_DEV |
| 1019 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 1020 | if (!t->expire) |
| 1021 | ABORT_NOW(); |
| 1022 | #endif |
| 1023 | *next = t->expire; |
| 1024 | return; /* nothing more to do */ |
| 1025 | } |
| 1026 | |
| 1027 | s->fe->feconn--; |
| 1028 | if (s->flags & SN_BE_ASSIGNED) |
| 1029 | s->be->beconn--; |
| 1030 | actconn--; |
| 1031 | |
| 1032 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1033 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1034 | int len; |
| 1035 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n", |
| 1036 | s->uniq_id, s->be->id, |
| 1037 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
| 1038 | s->term_trace); |
| 1039 | write(1, trash, len); |
| 1040 | } |
| 1041 | |
| 1042 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1043 | session_process_counters(s); |
| 1044 | |
| 1045 | /* let's do a final log if we need it */ |
| 1046 | if (s->logs.logwait && |
| 1047 | !(s->flags & SN_MONITOR) && |
| 1048 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1049 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* the task MUST not be in the run queue anymore */ |
| 1053 | task_delete(t); |
| 1054 | session_free(s); |
| 1055 | task_free(t); |
| 1056 | *next = TICK_ETERNITY; |
| 1057 | } |
| 1058 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1059 | /* |
| 1060 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1061 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1062 | * current connection slot. This function also notifies any LB algo which might |
| 1063 | * expect to be informed about any change in the number of active sessions on a |
| 1064 | * server. |
| 1065 | */ |
| 1066 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1067 | { |
| 1068 | if (sess->srv_conn == newsrv) |
| 1069 | return; |
| 1070 | |
| 1071 | if (sess->srv_conn) { |
| 1072 | sess->srv_conn->served--; |
| 1073 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1074 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1075 | sess->srv_conn = NULL; |
| 1076 | } |
| 1077 | |
| 1078 | if (newsrv) { |
| 1079 | newsrv->served++; |
| 1080 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1081 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1082 | sess->srv_conn = newsrv; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1087 | /* |
| 1088 | * Local variables: |
| 1089 | * c-indent-level: 8 |
| 1090 | * c-basic-offset: 8 |
| 1091 | * End: |
| 1092 | */ |