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