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