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