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