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