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