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