Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdlib.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 16 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <types/capture.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 20 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 22 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 23 | #include <proto/buffers.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 24 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 25 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 27 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 31 | #include <proto/server.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 32 | #include <proto/stream_interface.h> |
| 33 | #include <proto/stream_sock.h> |
| 34 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 36 | #ifdef CONFIG_HAP_TCPSPLICE |
| 37 | #include <libtcpsplice.h> |
| 38 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 40 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 41 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * frees the context associated to a session. It must have been removed first. |
| 45 | */ |
| 46 | void session_free(struct session *s) |
| 47 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 48 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 49 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 50 | struct bref *bref, *back; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | if (s->pend_pos) |
| 53 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 55 | if (s->srv) { /* there may be requests left pending in queue */ |
| 56 | if (s->flags & SN_CURR_SESS) { |
| 57 | s->flags &= ~SN_CURR_SESS; |
| 58 | s->srv->cur_sess--; |
| 59 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 60 | if (may_dequeue_tasks(s->srv, s->be)) |
| 61 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 62 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 63 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 64 | if (unlikely(s->srv_conn)) { |
| 65 | /* the session still has a reserved slot on a server, but |
| 66 | * it should normally be only the same as the one above, |
| 67 | * so this should not happen in fact. |
| 68 | */ |
| 69 | sess_change_server(s, NULL); |
| 70 | } |
| 71 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 72 | if (s->req->pipe) |
| 73 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 74 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 75 | if (s->rep->pipe) |
| 76 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 78 | pool_free2(pool2_buffer, s->req); |
| 79 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 81 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 82 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 83 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 84 | if (txn->rsp.cap != NULL) { |
| 85 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 86 | for (h = fe->rsp_cap; h; h = h->next) |
| 87 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 88 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 90 | if (txn->req.cap != NULL) { |
| 91 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 92 | for (h = fe->req_cap; h; h = h->next) |
| 93 | pool_free2(h->pool, txn->req.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 94 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | } |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 97 | pool_free2(pool2_requri, txn->uri); |
| 98 | pool_free2(pool2_capture, txn->cli_cookie); |
| 99 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 100 | |
| 101 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 102 | /* we have to unlink all watchers. We must not relink them if |
| 103 | * this session was the last one in the list. |
| 104 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 105 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 106 | LIST_INIT(&bref->users); |
| 107 | if (s->list.n != &sessions) |
| 108 | LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 109 | bref->ref = s->list.n; |
| 110 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 111 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 112 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 113 | |
| 114 | /* 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] | 115 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 116 | pool_flush2(pool2_buffer); |
| 117 | pool_flush2(fe->hdr_idx_pool); |
| 118 | pool_flush2(pool2_requri); |
| 119 | pool_flush2(pool2_capture); |
| 120 | pool_flush2(pool2_session); |
| 121 | pool_flush2(fe->req_cap_pool); |
| 122 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 123 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 128 | int init_session() |
| 129 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 130 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 131 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 132 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 135 | void session_process_counters(struct session *s) |
| 136 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 137 | unsigned long long bytes; |
| 138 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 139 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 140 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 141 | s->logs.bytes_in = s->req->total; |
| 142 | if (bytes) { |
| 143 | s->fe->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 144 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 145 | if (s->be != s->fe) |
| 146 | s->be->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 147 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 148 | if (s->srv) |
| 149 | s->srv->bytes_in += bytes; |
| 150 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 153 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 154 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 155 | s->logs.bytes_out = s->rep->total; |
| 156 | if (bytes) { |
| 157 | s->fe->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 158 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 159 | if (s->be != s->fe) |
| 160 | s->be->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 161 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 162 | if (s->srv) |
| 163 | s->srv->bytes_out += bytes; |
| 164 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 168 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 169 | * connection was attempted and that the file descriptor is already allocated. |
| 170 | * We must check for establishment, error and abort. Possible output states |
| 171 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 172 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 173 | * otherwise 1. |
| 174 | */ |
| 175 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 176 | { |
| 177 | struct buffer *req = si->ob; |
| 178 | struct buffer *rep = si->ib; |
| 179 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 180 | /* If we got an error, or if nothing happened and the connection timed |
| 181 | * out, we must give up. The CER state handler will take care of retry |
| 182 | * attempts and error reports. |
| 183 | */ |
| 184 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
| 185 | si->state = SI_ST_CER; |
| 186 | fd_delete(si->fd); |
| 187 | |
| 188 | if (si->err_type) |
| 189 | return 0; |
| 190 | |
| 191 | si->err_loc = s->srv; |
| 192 | if (si->flags & SI_FL_ERR) |
| 193 | si->err_type = SI_ET_CONN_ERR; |
| 194 | else |
| 195 | si->err_type = SI_ET_CONN_TO; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* OK, maybe we want to abort */ |
| 200 | if (unlikely((req->flags & BF_SHUTW_NOW) || |
| 201 | (rep->flags & BF_SHUTW) || |
| 202 | ((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] | 203 | (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 204 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 205 | /* give up */ |
| 206 | si->shutw(si); |
| 207 | si->err_type |= SI_ET_CONN_ABRT; |
| 208 | si->err_loc = s->srv; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 209 | if (s->srv_error) |
| 210 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 |
Willy Tarreau | 3ab68cf | 2009-01-25 16:03:28 +0100 | [diff] [blame] | 329 | if ((global.tune.options & GTUNE_USE_SPLICE) && |
| 330 | (s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 331 | /* TCP splicing supported by both FE and BE */ |
| 332 | tcp_splice_splicefd(req->prod->fd, si->fd, 0); |
| 333 | } |
| 334 | #endif |
| 335 | } |
| 336 | else { |
| 337 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 338 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 339 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 340 | /* reset hdr_idx which was already initialized by the request. |
| 341 | * right now, the http parser does it. |
| 342 | * hdr_idx_init(&s->txn.hdr_idx); |
| 343 | */ |
| 344 | } |
| 345 | |
| 346 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 347 | req->wex = TICK_ETERNITY; |
| 348 | } |
| 349 | |
| 350 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 351 | * Other input states are simply ignored. |
| 352 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 353 | * Flags must have previously been updated for timeouts and other conditions. |
| 354 | */ |
| 355 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 356 | { |
| 357 | 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", |
| 358 | now_ms, __FUNCTION__, |
| 359 | s, |
| 360 | s->req, s->rep, |
| 361 | s->req->rex, s->rep->wex, |
| 362 | s->req->flags, s->rep->flags, |
| 363 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 364 | |
| 365 | if (si->state == SI_ST_ASS) { |
| 366 | /* Server assigned to connection request, we have to try to connect now */ |
| 367 | int conn_err; |
| 368 | |
| 369 | conn_err = connect_server(s); |
| 370 | if (conn_err == SN_ERR_NONE) { |
| 371 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 372 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 373 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | /* We have received a synchronous error. We might have to |
| 378 | * abort, retry immediately or redispatch. |
| 379 | */ |
| 380 | if (conn_err == SN_ERR_INTERNAL) { |
| 381 | if (!si->err_type) { |
| 382 | si->err_type = SI_ET_CONN_OTHER; |
| 383 | si->err_loc = s->srv; |
| 384 | } |
| 385 | |
| 386 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 387 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 388 | if (s->srv) |
| 389 | s->srv->failed_conns++; |
| 390 | s->be->failed_conns++; |
| 391 | |
| 392 | /* release other sessions waiting for this server */ |
| 393 | if (may_dequeue_tasks(s->srv, s->be)) |
| 394 | process_srv_queue(s->srv); |
| 395 | |
| 396 | /* Failed and not retryable. */ |
| 397 | si->shutr(si); |
| 398 | si->shutw(si); |
| 399 | si->ob->flags |= BF_WRITE_ERROR; |
| 400 | |
| 401 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 402 | |
| 403 | /* no session was ever accounted for this server */ |
| 404 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 405 | if (s->srv_error) |
| 406 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* We are facing a retryable error, but we don't want to run a |
| 411 | * turn-around now, as the problem is likely a source port |
| 412 | * allocation problem, so we want to retry now. |
| 413 | */ |
| 414 | si->state = SI_ST_CER; |
| 415 | si->flags &= ~SI_FL_ERR; |
| 416 | sess_update_st_cer(s, si); |
| 417 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 418 | return; |
| 419 | } |
| 420 | else if (si->state == SI_ST_QUE) { |
| 421 | /* connection request was queued, check for any update */ |
| 422 | if (!s->pend_pos) { |
| 423 | /* The connection is not in the queue anymore. Either |
| 424 | * we have a server connection slot available and we |
| 425 | * go directly to the assigned state, or we need to |
| 426 | * load-balance first and go to the INI state. |
| 427 | */ |
| 428 | si->exp = TICK_ETERNITY; |
| 429 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 430 | si->state = SI_ST_REQ; |
| 431 | else { |
| 432 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 433 | si->state = SI_ST_ASS; |
| 434 | } |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | /* Connection request still in queue... */ |
| 439 | if (si->flags & SI_FL_EXP) { |
| 440 | /* ... and timeout expired */ |
| 441 | si->exp = TICK_ETERNITY; |
| 442 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 443 | if (s->srv) |
| 444 | s->srv->failed_conns++; |
| 445 | s->be->failed_conns++; |
| 446 | si->shutr(si); |
| 447 | si->shutw(si); |
| 448 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 449 | if (!si->err_type) |
| 450 | si->err_type = SI_ET_QUEUE_TO; |
| 451 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 452 | if (s->srv_error) |
| 453 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | /* Connection remains in queue, check if we have to abort it */ |
| 458 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 459 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 460 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
| 461 | /* give up */ |
| 462 | si->exp = TICK_ETERNITY; |
| 463 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 464 | si->shutr(si); |
| 465 | si->shutw(si); |
| 466 | si->err_type |= SI_ET_QUEUE_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 | /* Nothing changed */ |
| 474 | return; |
| 475 | } |
| 476 | else if (si->state == SI_ST_TAR) { |
| 477 | /* Connection request might be aborted */ |
| 478 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 479 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 480 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
| 481 | /* give up */ |
| 482 | si->exp = TICK_ETERNITY; |
| 483 | si->shutr(si); |
| 484 | si->shutw(si); |
| 485 | si->err_type |= SI_ET_CONN_ABRT; |
| 486 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 487 | if (s->srv_error) |
| 488 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
| 492 | if (!(si->flags & SI_FL_EXP)) |
| 493 | return; /* still in turn-around */ |
| 494 | |
| 495 | si->exp = TICK_ETERNITY; |
| 496 | |
| 497 | /* we keep trying on the same server as long as the session is |
| 498 | * marked "assigned". |
| 499 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 500 | */ |
| 501 | if (s->flags & SN_ASSIGNED) |
| 502 | si->state = SI_ST_ASS; |
| 503 | else |
| 504 | si->state = SI_ST_REQ; |
| 505 | return; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* This function initiates a server connection request on a stream interface |
| 510 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 511 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 512 | * or SI_ST_CLO upon error. |
| 513 | */ |
| 514 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 515 | 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", |
| 516 | now_ms, __FUNCTION__, |
| 517 | s, |
| 518 | s->req, s->rep, |
| 519 | s->req->rex, s->rep->wex, |
| 520 | s->req->flags, s->rep->flags, |
| 521 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 522 | |
| 523 | if (si->state != SI_ST_REQ) |
| 524 | return; |
| 525 | |
| 526 | /* Try to assign a server */ |
| 527 | if (srv_redispatch_connect(s) != 0) { |
| 528 | /* We did not get a server. Either we queued the |
| 529 | * connection request, or we encountered an error. |
| 530 | */ |
| 531 | if (si->state == SI_ST_QUE) |
| 532 | return; |
| 533 | |
| 534 | /* we did not get any server, let's check the cause */ |
| 535 | si->shutr(si); |
| 536 | si->shutw(si); |
| 537 | si->ob->flags |= BF_WRITE_ERROR; |
| 538 | if (!si->err_type) |
| 539 | si->err_type = SI_ET_CONN_OTHER; |
| 540 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 541 | if (s->srv_error) |
| 542 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
| 546 | /* The server is assigned */ |
| 547 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 548 | si->state = SI_ST_ASS; |
| 549 | } |
| 550 | |
| 551 | /* Processes the client, server, request and response jobs of a session task, |
| 552 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 553 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 554 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 555 | * nothing too many times, the request and response buffers flags are monitored |
| 556 | * and each function is called only if at least another function has changed at |
| 557 | * least one flag it is interested in. |
| 558 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 559 | struct task *process_session(struct task *t) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 560 | { |
| 561 | struct session *s = t->context; |
| 562 | int resync; |
| 563 | unsigned int rqf_last, rpf_last; |
| 564 | |
| 565 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 566 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 567 | |
| 568 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 569 | * stream interfaces when their timeouts have expired. |
| 570 | */ |
| 571 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 572 | stream_int_check_timeouts(&s->si[0]); |
| 573 | stream_int_check_timeouts(&s->si[1]); |
| 574 | buffer_check_timeouts(s->req); |
| 575 | buffer_check_timeouts(s->rep); |
| 576 | } |
| 577 | |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 578 | s->req->flags &= ~BF_READ_NOEXP; |
| 579 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 580 | /* copy req/rep flags so that we can detect shutdowns */ |
| 581 | rqf_last = s->req->flags; |
| 582 | rpf_last = s->rep->flags; |
| 583 | |
| 584 | /* 1b: check for low-level errors reported at the stream interface. |
| 585 | * First we check if it's a retryable error (in which case we don't |
| 586 | * want to tell the buffer). Otherwise we report the error one level |
| 587 | * upper by setting flags into the buffers. Note that the side towards |
| 588 | * the client cannot have connect (hence retryable) errors. Also, the |
| 589 | * connection setup code must be able to deal with any type of abort. |
| 590 | */ |
| 591 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 592 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 593 | s->si[0].shutr(&s->si[0]); |
| 594 | s->si[0].shutw(&s->si[0]); |
| 595 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 596 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 597 | if (!(s->flags & SN_ERR_MASK)) |
| 598 | s->flags |= SN_ERR_CLICL; |
| 599 | if (!(s->flags & SN_FINST_MASK)) |
| 600 | s->flags |= SN_FINST_D; |
| 601 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| 605 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 606 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 607 | s->si[1].shutr(&s->si[1]); |
| 608 | s->si[1].shutw(&s->si[1]); |
| 609 | stream_int_report_error(&s->si[1]); |
| 610 | s->be->failed_resp++; |
| 611 | if (s->srv) |
| 612 | s->srv->failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 613 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 614 | if (!(s->flags & SN_ERR_MASK)) |
| 615 | s->flags |= SN_ERR_SRVCL; |
| 616 | if (!(s->flags & SN_FINST_MASK)) |
| 617 | s->flags |= SN_FINST_D; |
| 618 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 619 | } |
| 620 | /* note: maybe we should process connection errors here ? */ |
| 621 | } |
| 622 | |
| 623 | if (s->si[1].state == SI_ST_CON) { |
| 624 | /* we were trying to establish a connection on the server side, |
| 625 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 626 | */ |
| 627 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 628 | sess_update_st_cer(s, &s->si[1]); |
| 629 | else if (s->si[1].state == SI_ST_EST) |
| 630 | sess_establish(s, &s->si[1]); |
| 631 | |
| 632 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 633 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 634 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 635 | */ |
| 636 | } |
| 637 | |
| 638 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 639 | * for future reads or writes. Note: this will also concern upper layers |
| 640 | * but we do not touch any other flag. We must be careful and correctly |
| 641 | * detect state changes when calling them. |
| 642 | */ |
| 643 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 644 | if (s->req->flags & BF_READ_TIMEOUT) |
| 645 | s->req->prod->shutr(s->req->prod); |
| 646 | if (s->req->flags & BF_WRITE_TIMEOUT) |
| 647 | s->req->cons->shutw(s->req->cons); |
| 648 | DPRINTF(stderr, |
| 649 | "[%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", |
| 650 | now_ms, __FUNCTION__, __LINE__, |
| 651 | t, |
| 652 | s, s->flags, |
| 653 | s->req, s->rep, |
| 654 | s->req->rex, s->rep->wex, |
| 655 | s->req->flags, s->rep->flags, |
| 656 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 657 | s->rep->cons->err_type, s->req->cons->err_type, |
| 658 | s->conn_retries); |
| 659 | } |
| 660 | |
| 661 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 662 | if (s->rep->flags & BF_READ_TIMEOUT) |
| 663 | s->rep->prod->shutr(s->rep->prod); |
| 664 | if (s->rep->flags & BF_WRITE_TIMEOUT) |
| 665 | s->rep->cons->shutw(s->rep->cons); |
| 666 | DPRINTF(stderr, |
| 667 | "[%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", |
| 668 | now_ms, __FUNCTION__, __LINE__, |
| 669 | t, |
| 670 | s, s->flags, |
| 671 | s->req, s->rep, |
| 672 | s->req->rex, s->rep->wex, |
| 673 | s->req->flags, s->rep->flags, |
| 674 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 675 | s->rep->cons->err_type, s->req->cons->err_type, |
| 676 | s->conn_retries); |
| 677 | } |
| 678 | |
| 679 | /* Check for connection closure */ |
| 680 | |
| 681 | resync_stream_interface: |
| 682 | DPRINTF(stderr, |
| 683 | "[%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", |
| 684 | now_ms, __FUNCTION__, __LINE__, |
| 685 | t, |
| 686 | s, s->flags, |
| 687 | s->req, s->rep, |
| 688 | s->req->rex, s->rep->wex, |
| 689 | s->req->flags, s->rep->flags, |
| 690 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 691 | s->rep->cons->err_type, s->req->cons->err_type, |
| 692 | s->conn_retries); |
| 693 | |
| 694 | /* nothing special to be done on client side */ |
| 695 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 696 | s->req->prod->state = SI_ST_CLO; |
| 697 | |
| 698 | /* When a server-side connection is released, we have to count it and |
| 699 | * check for pending connections on this server. |
| 700 | */ |
| 701 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 702 | s->req->cons->state = SI_ST_CLO; |
| 703 | if (s->srv) { |
| 704 | if (s->flags & SN_CURR_SESS) { |
| 705 | s->flags &= ~SN_CURR_SESS; |
| 706 | s->srv->cur_sess--; |
| 707 | } |
| 708 | sess_change_server(s, NULL); |
| 709 | if (may_dequeue_tasks(s->srv, s->be)) |
| 710 | process_srv_queue(s->srv); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 716 | * at this point. |
| 717 | */ |
| 718 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 719 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 720 | /**** Process layer 7 below ****/ |
| 721 | |
| 722 | resync = 0; |
| 723 | |
| 724 | /* Analyse request */ |
| 725 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 726 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 727 | unsigned int flags = s->req->flags; |
| 728 | |
| 729 | if (s->req->prod->state >= SI_ST_EST) { |
| 730 | /* it's up to the analysers to reset write_ena */ |
| 731 | buffer_write_ena(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 732 | |
| 733 | /* We will call all analysers for which a bit is set in |
| 734 | * s->req->analysers, following the bit order from LSB |
| 735 | * to MSB. The analysers must remove themselves from |
| 736 | * the list when not needed. This while() loop is in |
| 737 | * fact a cleaner if(). |
| 738 | */ |
| 739 | while (s->req->analysers) { |
| 740 | if (s->req->analysers & AN_REQ_INSPECT) |
| 741 | if (!tcp_inspect_request(s, s->req)) |
| 742 | break; |
| 743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 744 | if (s->req->analysers & AN_REQ_HTTP_HDR) |
| 745 | if (!http_process_request(s, s->req)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 746 | break; |
| 747 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 748 | if (s->req->analysers & AN_REQ_HTTP_TARPIT) |
| 749 | if (!http_process_tarpit(s, s->req)) |
| 750 | break; |
| 751 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 752 | if (s->req->analysers & AN_REQ_HTTP_BODY) |
| 753 | if (!http_process_request_body(s, s->req)) |
| 754 | break; |
| 755 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 756 | /* Just make sure that nobody set a wrong flag causing an endless loop */ |
| 757 | s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
| 758 | |
| 759 | /* we don't want to loop anyway */ |
| 760 | break; |
| 761 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 762 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 763 | |
| 764 | /* Report it if the client got an error or a read timeout expired */ |
| 765 | if (unlikely(s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) && |
| 766 | !(s->flags & SN_ERR_MASK)) { |
| 767 | s->req->analysers = 0; |
| 768 | if (s->req->flags & BF_READ_ERROR) |
| 769 | s->flags |= SN_ERR_CLICL; |
| 770 | else if (s->req->flags & BF_READ_TIMEOUT) |
| 771 | s->flags |= SN_ERR_CLITO; |
| 772 | else if (s->req->flags & BF_WRITE_ERROR) |
| 773 | s->flags |= SN_ERR_SRVCL; |
| 774 | else |
| 775 | s->flags |= SN_ERR_SRVTO; |
| 776 | sess_set_term_flags(s); |
| 777 | } |
| 778 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 779 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 780 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 781 | if (s->req->flags != flags) |
| 782 | resync = 1; |
| 783 | } |
| 784 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 785 | /* Report it if the client got an error or a read timeout expired */ |
| 786 | if (unlikely(s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) && |
| 787 | !(s->flags & SN_ERR_MASK)) { |
| 788 | if (s->req->flags & BF_READ_ERROR) |
| 789 | s->flags |= SN_ERR_CLICL; |
| 790 | else if (s->req->flags & BF_READ_TIMEOUT) |
| 791 | s->flags |= SN_ERR_CLITO; |
| 792 | else if (s->req->flags & BF_WRITE_ERROR) |
| 793 | s->flags |= SN_ERR_SRVCL; |
| 794 | else |
| 795 | s->flags |= SN_ERR_SRVTO; |
| 796 | sess_set_term_flags(s); |
| 797 | } |
| 798 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 799 | /* If noone is interested in analysing data, it's time to forward |
| 800 | * everything. We will wake up from time to time when either send_max |
| 801 | * or to_forward are reached. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 802 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 803 | if (!s->req->analysers && |
| 804 | !(s->req->flags & (BF_HIJACK|BF_SHUTW)) && |
| 805 | (s->req->prod->state >= SI_ST_EST)) { |
| 806 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 807 | * attached to it. If any data are left in, we'll permit them to |
| 808 | * move. |
| 809 | */ |
| 810 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 811 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 812 | /* If the producer is still connected, we'll schedule large blocks |
| 813 | * of data to be forwarded from the producer to the consumer (which |
| 814 | * might possibly not be connected yet). |
| 815 | */ |
| 816 | if (!(s->req->flags & BF_SHUTR) && |
| 817 | s->req->to_forward < FORWARD_DEFAULT_SIZE) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 818 | buffer_forward(s->req, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 819 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 820 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 821 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 822 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 823 | s->req->to_forward && |
| 824 | (global.tune.options & GTUNE_USE_SPLICE) && |
| 825 | (pipes_used < global.maxpipes) && |
| 826 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 827 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 828 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 829 | s->req->flags |= BF_KERN_SPLICING; |
| 830 | } |
| 831 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 832 | /* reflect what the L7 analysers have seen last */ |
| 833 | rqf_last = s->req->flags; |
| 834 | |
| 835 | /* |
| 836 | * Now forward all shutdown requests between both sides of the buffer |
| 837 | */ |
| 838 | |
| 839 | /* first, let's check if the request buffer needs to shutdown(write) */ |
| 840 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 841 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 842 | buffer_shutw_now(s->req); |
| 843 | else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) && |
| 844 | (s->req->cons->state == SI_ST_EST) && |
| 845 | s->be->options & PR_O_FORCE_CLO && |
| 846 | s->rep->flags & BF_READ_ACTIVITY) { |
| 847 | /* We want to force the connection to the server to close, |
| 848 | * and the server has begun to respond. That's the right |
| 849 | * time. |
| 850 | */ |
| 851 | buffer_shutw_now(s->req); |
| 852 | } |
| 853 | |
| 854 | /* shutdown(write) pending */ |
| 855 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 856 | s->req->cons->shutw(s->req->cons); |
| 857 | |
| 858 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 859 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 860 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 861 | buffer_shutr_now(s->req); |
| 862 | |
| 863 | /* shutdown(read) pending */ |
| 864 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 865 | s->req->prod->shutr(s->req->prod); |
| 866 | |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 867 | /* it's possible that an upper layer has requested a connection setup or abort */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 868 | if (s->req->cons->state == SI_ST_INI && |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 869 | (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) { |
| 870 | if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA) |
| 871 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
| 872 | else |
| 873 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
| 874 | } |
| 875 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 876 | |
| 877 | /* we may have a pending connection request, or a connection waiting |
| 878 | * for completion. |
| 879 | */ |
| 880 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 881 | do { |
| 882 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 883 | * to give a chance to step 2 to perform a redirect if needed. |
| 884 | */ |
| 885 | if (s->si[1].state != SI_ST_REQ) |
| 886 | sess_update_stream_int(s, &s->si[1]); |
| 887 | if (s->si[1].state == SI_ST_REQ) |
| 888 | sess_prepare_conn_req(s, &s->si[1]); |
| 889 | |
| 890 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 891 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 892 | perform_http_redirect(s, &s->si[1]); |
| 893 | } while (s->si[1].state == SI_ST_ASS); |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Here we want to check if we need to resync or not. |
| 898 | */ |
| 899 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
| 900 | resync = 1; |
| 901 | |
| 902 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 903 | |
| 904 | /* according to benchmarks, it makes sense to resync now */ |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 905 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 906 | goto resync_stream_interface; |
| 907 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 908 | if (resync) |
| 909 | goto resync_request; |
| 910 | |
| 911 | resync_response: |
| 912 | resync = 0; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 913 | |
| 914 | /* Analyse response */ |
| 915 | |
| 916 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 917 | /* In inject mode, we wake up everytime something has |
| 918 | * happened on the write side of the buffer. |
| 919 | */ |
| 920 | unsigned int flags = s->rep->flags; |
| 921 | |
| 922 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 923 | !(s->rep->flags & BF_FULL)) { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 924 | s->rep->hijacker(s, s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 925 | } |
| 926 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 927 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 928 | if (s->rep->flags != flags) |
| 929 | resync = 1; |
| 930 | } |
| 931 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 932 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 933 | unsigned int flags = s->rep->flags; |
| 934 | |
| 935 | if (s->rep->prod->state >= SI_ST_EST) { |
| 936 | /* it's up to the analysers to reset write_ena */ |
| 937 | buffer_write_ena(s->rep); |
| 938 | if (s->rep->analysers) |
| 939 | process_response(s); |
| 940 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 941 | |
| 942 | /* Report it if the server got an error or a read timeout expired */ |
| 943 | if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) && |
| 944 | !(s->flags & SN_ERR_MASK)) { |
| 945 | s->rep->analysers = 0; |
| 946 | if (s->rep->flags & BF_READ_ERROR) |
| 947 | s->flags |= SN_ERR_SRVCL; |
| 948 | else if (s->rep->flags & BF_READ_TIMEOUT) |
| 949 | s->flags |= SN_ERR_SRVTO; |
| 950 | else if (s->rep->flags & BF_WRITE_ERROR) |
| 951 | s->flags |= SN_ERR_CLICL; |
| 952 | else |
| 953 | s->flags |= SN_ERR_CLITO; |
| 954 | sess_set_term_flags(s); |
| 955 | } |
| 956 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 957 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 958 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 959 | if (s->rep->flags != flags) |
| 960 | resync = 1; |
| 961 | } |
| 962 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 963 | /* Report it if the server got an error or a read timeout expired */ |
| 964 | if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) && |
| 965 | !(s->flags & SN_ERR_MASK)) { |
| 966 | if (s->rep->flags & BF_READ_ERROR) |
| 967 | s->flags |= SN_ERR_SRVCL; |
| 968 | else if (s->rep->flags & BF_READ_TIMEOUT) |
| 969 | s->flags |= SN_ERR_SRVTO; |
| 970 | else if (s->rep->flags & BF_WRITE_ERROR) |
| 971 | s->flags |= SN_ERR_CLICL; |
| 972 | else |
| 973 | s->flags |= SN_ERR_CLITO; |
| 974 | sess_set_term_flags(s); |
| 975 | } |
| 976 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 977 | /* If noone is interested in analysing data, it's time to forward |
| 978 | * everything. We will wake up from time to time when either send_max |
| 979 | * or to_forward are reached. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 980 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 981 | if (!s->rep->analysers && |
| 982 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) && |
| 983 | (s->rep->prod->state >= SI_ST_EST)) { |
| 984 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 985 | * attached to it. If any data are left in, we'll permit them to |
| 986 | * move. |
| 987 | */ |
| 988 | buffer_flush(s->rep); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 989 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 990 | /* If the producer is still connected, we'll schedule large blocks |
| 991 | * of data to be forwarded from the producer to the consumer (which |
| 992 | * might possibly not be connected yet). |
| 993 | */ |
| 994 | if (!(s->rep->flags & BF_SHUTR) && |
| 995 | s->rep->to_forward < FORWARD_DEFAULT_SIZE) |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 996 | buffer_forward(s->rep, FORWARD_DEFAULT_SIZE); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 997 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 998 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 999 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1000 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1001 | s->rep->to_forward && |
| 1002 | (global.tune.options & GTUNE_USE_SPLICE) && |
| 1003 | (pipes_used < global.maxpipes) && |
| 1004 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1005 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1006 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1007 | s->rep->flags |= BF_KERN_SPLICING; |
| 1008 | } |
| 1009 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1010 | /* reflect what the L7 analysers have seen last */ |
| 1011 | rpf_last = s->rep->flags; |
| 1012 | |
| 1013 | /* |
| 1014 | * Now forward all shutdown requests between both sides of the buffer |
| 1015 | */ |
| 1016 | |
| 1017 | /* |
| 1018 | * FIXME: this is probably where we should produce error responses. |
| 1019 | */ |
| 1020 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1021 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1022 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 1023 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) |
| 1024 | buffer_shutw_now(s->rep); |
| 1025 | |
| 1026 | /* shutdown(write) pending */ |
| 1027 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) |
| 1028 | s->rep->cons->shutw(s->rep->cons); |
| 1029 | |
| 1030 | /* 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] | 1031 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1032 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1033 | buffer_shutr_now(s->rep); |
| 1034 | |
| 1035 | /* shutdown(read) pending */ |
| 1036 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1037 | s->rep->prod->shutr(s->rep->prod); |
| 1038 | |
| 1039 | /* |
| 1040 | * Here we want to check if we need to resync or not. |
| 1041 | */ |
| 1042 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
| 1043 | resync = 1; |
| 1044 | |
| 1045 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1046 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1047 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1048 | goto resync_stream_interface; |
| 1049 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1050 | if (s->req->flags != rqf_last) |
| 1051 | goto resync_request; |
| 1052 | |
| 1053 | if (resync) |
| 1054 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* This is needed only when debugging is enabled, to indicate |
| 1057 | * client-side or server-side close. Please note that in the unlikely |
| 1058 | * event where both sides would close at once, the sequence is reported |
| 1059 | * on the server side first. |
| 1060 | */ |
| 1061 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1062 | (!(global.mode & MODE_QUIET) || |
| 1063 | (global.mode & MODE_VERBOSE)))) { |
| 1064 | int len; |
| 1065 | |
| 1066 | if (s->si[1].state == SI_ST_CLO && |
| 1067 | s->si[1].prev_state == SI_ST_EST) { |
| 1068 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1069 | s->uniq_id, s->be->id, |
| 1070 | (unsigned short)s->si[0].fd, |
| 1071 | (unsigned short)s->si[1].fd); |
| 1072 | write(1, trash, len); |
| 1073 | } |
| 1074 | |
| 1075 | if (s->si[0].state == SI_ST_CLO && |
| 1076 | s->si[0].prev_state == SI_ST_EST) { |
| 1077 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1078 | s->uniq_id, s->be->id, |
| 1079 | (unsigned short)s->si[0].fd, |
| 1080 | (unsigned short)s->si[1].fd); |
| 1081 | write(1, trash, len); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1086 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1087 | |
| 1088 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1089 | session_process_counters(s); |
| 1090 | |
| 1091 | if (s->rep->cons->state == SI_ST_EST) |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 1092 | stream_sock_data_finish(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1093 | |
| 1094 | if (s->req->cons->state == SI_ST_EST) |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 1095 | stream_sock_data_finish(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1096 | |
| 1097 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1098 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1099 | s->si[0].prev_state = s->si[0].state; |
| 1100 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1101 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1102 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* Trick: if a request is being waiting for the server to respond, |
| 1105 | * and if we know the server can timeout, we don't want the timeout |
| 1106 | * to expire on the client side first, but we're still interested |
| 1107 | * in passing data from the client to the server (eg: POST). Thus, |
| 1108 | * we can cancel the client's request timeout if the server's |
| 1109 | * request timeout is set and the server has not yet sent a response. |
| 1110 | */ |
| 1111 | |
| 1112 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1113 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1114 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1115 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1116 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1117 | |
| 1118 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1119 | tick_first(s->rep->rex, s->rep->wex)); |
| 1120 | if (s->req->analysers) |
| 1121 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1122 | |
| 1123 | if (s->si[0].exp) |
| 1124 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1125 | |
| 1126 | if (s->si[1].exp) |
| 1127 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1128 | |
| 1129 | #ifdef DEBUG_FULL |
| 1130 | 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", |
| 1131 | 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); |
| 1132 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1133 | |
| 1134 | #ifdef DEBUG_DEV |
| 1135 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
Willy Tarreau | d0a201b | 2009-03-08 15:53:06 +0100 | [diff] [blame] | 1136 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1137 | ABORT_NOW(); |
| 1138 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1139 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | s->fe->feconn--; |
| 1143 | if (s->flags & SN_BE_ASSIGNED) |
| 1144 | s->be->beconn--; |
| 1145 | actconn--; |
| 1146 | |
| 1147 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1148 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1149 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1150 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1151 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1152 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1153 | write(1, trash, len); |
| 1154 | } |
| 1155 | |
| 1156 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1157 | session_process_counters(s); |
| 1158 | |
| 1159 | /* let's do a final log if we need it */ |
| 1160 | if (s->logs.logwait && |
| 1161 | !(s->flags & SN_MONITOR) && |
| 1162 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1163 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1167 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1168 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1169 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1170 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1173 | /* |
| 1174 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1175 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1176 | * current connection slot. This function also notifies any LB algo which might |
| 1177 | * expect to be informed about any change in the number of active sessions on a |
| 1178 | * server. |
| 1179 | */ |
| 1180 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1181 | { |
| 1182 | if (sess->srv_conn == newsrv) |
| 1183 | return; |
| 1184 | |
| 1185 | if (sess->srv_conn) { |
| 1186 | sess->srv_conn->served--; |
| 1187 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1188 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1189 | sess->srv_conn = NULL; |
| 1190 | } |
| 1191 | |
| 1192 | if (newsrv) { |
| 1193 | newsrv->served++; |
| 1194 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1195 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1196 | sess->srv_conn = newsrv; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1200 | /* Set correct session termination flags in case no analyser has done it. It |
| 1201 | * also counts a failed request if the server state has not reached the request |
| 1202 | * stage. |
| 1203 | */ |
| 1204 | void sess_set_term_flags(struct session *s) |
| 1205 | { |
| 1206 | if (!(s->flags & SN_FINST_MASK)) { |
| 1207 | if (s->si[1].state < SI_ST_REQ) { |
| 1208 | s->fe->failed_req++; |
| 1209 | s->flags |= SN_FINST_R; |
| 1210 | } |
| 1211 | else if (s->si[1].state == SI_ST_QUE) |
| 1212 | s->flags |= SN_FINST_Q; |
| 1213 | else if (s->si[1].state < SI_ST_EST) |
| 1214 | s->flags |= SN_FINST_C; |
| 1215 | else if (s->si[1].state == SI_ST_EST) |
| 1216 | s->flags |= SN_FINST_D; |
| 1217 | else |
| 1218 | s->flags |= SN_FINST_L; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 1223 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 1224 | * session termination flags so that the caller does not have to worry about |
| 1225 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 1226 | */ |
| 1227 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 1228 | { |
| 1229 | int err_type = si->err_type; |
| 1230 | int err = 0, fin = 0; |
| 1231 | |
| 1232 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 1233 | err = SN_ERR_CLICL; |
| 1234 | fin = SN_FINST_Q; |
| 1235 | } |
| 1236 | else if (err_type & SI_ET_CONN_ABRT) { |
| 1237 | err = SN_ERR_CLICL; |
| 1238 | fin = SN_FINST_C; |
| 1239 | } |
| 1240 | else if (err_type & SI_ET_QUEUE_TO) { |
| 1241 | err = SN_ERR_SRVTO; |
| 1242 | fin = SN_FINST_Q; |
| 1243 | } |
| 1244 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 1245 | err = SN_ERR_SRVCL; |
| 1246 | fin = SN_FINST_Q; |
| 1247 | } |
| 1248 | else if (err_type & SI_ET_CONN_TO) { |
| 1249 | err = SN_ERR_SRVTO; |
| 1250 | fin = SN_FINST_C; |
| 1251 | } |
| 1252 | else if (err_type & SI_ET_CONN_ERR) { |
| 1253 | err = SN_ERR_SRVCL; |
| 1254 | fin = SN_FINST_C; |
| 1255 | } |
| 1256 | else /* SI_ET_CONN_OTHER and others */ { |
| 1257 | err = SN_ERR_INTERNAL; |
| 1258 | fin = SN_FINST_C; |
| 1259 | } |
| 1260 | |
| 1261 | if (!(s->flags & SN_ERR_MASK)) |
| 1262 | s->flags |= err; |
| 1263 | if (!(s->flags & SN_FINST_MASK)) |
| 1264 | s->flags |= fin; |
| 1265 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1266 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1267 | /* |
| 1268 | * Local variables: |
| 1269 | * c-indent-level: 8 |
| 1270 | * c-basic-offset: 8 |
| 1271 | * End: |
| 1272 | */ |