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 | 5ca791d | 2009-08-16 19:06:42 +0200 | [diff] [blame] | 25 | #include <proto/dumpstats.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 26 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 29 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 30 | #include <proto/proto_http.h> |
| 31 | #include <proto/proto_tcp.h> |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 32 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 34 | #include <proto/server.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 35 | #include <proto/stream_interface.h> |
| 36 | #include <proto/stream_sock.h> |
| 37 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 39 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 40 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * frees the context associated to a session. It must have been removed first. |
| 44 | */ |
| 45 | void session_free(struct session *s) |
| 46 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 47 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 48 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 49 | struct bref *bref, *back; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | if (s->pend_pos) |
| 52 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 54 | if (s->srv) { /* there may be requests left pending in queue */ |
| 55 | if (s->flags & SN_CURR_SESS) { |
| 56 | s->flags &= ~SN_CURR_SESS; |
| 57 | s->srv->cur_sess--; |
| 58 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 59 | if (may_dequeue_tasks(s->srv, s->be)) |
| 60 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 61 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 62 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 63 | if (unlikely(s->srv_conn)) { |
| 64 | /* the session still has a reserved slot on a server, but |
| 65 | * it should normally be only the same as the one above, |
| 66 | * so this should not happen in fact. |
| 67 | */ |
| 68 | sess_change_server(s, NULL); |
| 69 | } |
| 70 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 71 | if (s->req->pipe) |
| 72 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 73 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 74 | if (s->rep->pipe) |
| 75 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 76 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 77 | pool_free2(pool2_buffer, s->req); |
| 78 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 80 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 81 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 82 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 83 | if (txn->rsp.cap != NULL) { |
| 84 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 85 | for (h = fe->rsp_cap; h; h = h->next) |
| 86 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 87 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 89 | if (txn->req.cap != NULL) { |
| 90 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 91 | for (h = fe->req_cap; h; h = h->next) |
| 92 | pool_free2(h->pool, txn->req.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 93 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | } |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 96 | pool_free2(pool2_requri, txn->uri); |
| 97 | pool_free2(pool2_capture, txn->cli_cookie); |
| 98 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 99 | |
| 100 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 101 | /* we have to unlink all watchers. We must not relink them if |
| 102 | * this session was the last one in the list. |
| 103 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 104 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 105 | LIST_INIT(&bref->users); |
| 106 | if (s->list.n != &sessions) |
| 107 | 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] | 108 | bref->ref = s->list.n; |
| 109 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 110 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 111 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 112 | |
| 113 | /* 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] | 114 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 115 | pool_flush2(pool2_buffer); |
| 116 | pool_flush2(fe->hdr_idx_pool); |
| 117 | pool_flush2(pool2_requri); |
| 118 | pool_flush2(pool2_capture); |
| 119 | pool_flush2(pool2_session); |
| 120 | pool_flush2(fe->req_cap_pool); |
| 121 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 122 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 127 | int init_session() |
| 128 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 129 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 130 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 131 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 134 | void session_process_counters(struct session *s) |
| 135 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 136 | unsigned long long bytes; |
| 137 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 138 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 139 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 140 | s->logs.bytes_in = s->req->total; |
| 141 | if (bytes) { |
| 142 | s->fe->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 144 | if (s->be != s->fe) |
| 145 | s->be->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->srv) |
| 148 | s->srv->bytes_in += bytes; |
| 149 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 152 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 153 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 154 | s->logs.bytes_out = s->rep->total; |
| 155 | if (bytes) { |
| 156 | s->fe->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 157 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 158 | if (s->be != s->fe) |
| 159 | s->be->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->srv) |
| 162 | s->srv->bytes_out += bytes; |
| 163 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 167 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 168 | * connection was attempted and that the file descriptor is already allocated. |
| 169 | * We must check for establishment, error and abort. Possible output states |
| 170 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 171 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 172 | * otherwise 1. |
| 173 | */ |
| 174 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 175 | { |
| 176 | struct buffer *req = si->ob; |
| 177 | struct buffer *rep = si->ib; |
| 178 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 179 | /* If we got an error, or if nothing happened and the connection timed |
| 180 | * out, we must give up. The CER state handler will take care of retry |
| 181 | * attempts and error reports. |
| 182 | */ |
| 183 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 184 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 185 | si->state = SI_ST_CER; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 186 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 187 | fd_delete(si->fd); |
| 188 | |
| 189 | if (si->err_type) |
| 190 | return 0; |
| 191 | |
| 192 | si->err_loc = s->srv; |
| 193 | if (si->flags & SI_FL_ERR) |
| 194 | si->err_type = SI_ET_CONN_ERR; |
| 195 | else |
| 196 | si->err_type = SI_ET_CONN_TO; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* OK, maybe we want to abort */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 201 | if (unlikely((rep->flags & BF_SHUTW) || |
| 202 | ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 203 | (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_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 | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 322 | buffer_set_rlim(rep, rep->size); /* no rewrite needed */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 { |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 332 | buffer_set_rlim(rep, req->size - global.tune.maxrewrite); /* rewrite needed */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 333 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 334 | /* reset hdr_idx which was already initialized by the request. |
| 335 | * right now, the http parser does it. |
| 336 | * hdr_idx_init(&s->txn.hdr_idx); |
| 337 | */ |
| 338 | } |
| 339 | |
Willy Tarreau | 4e5b828 | 2009-08-16 22:57:50 +0200 | [diff] [blame] | 340 | rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 453 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 454 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 455 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 473 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 474 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 475 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 639 | /* we don't want the stream interface functions to recursively wake us up */ |
| 640 | if (s->req->prod->owner == t) |
| 641 | s->req->prod->flags |= SI_FL_DONT_WAKE; |
| 642 | if (s->req->cons->owner == t) |
| 643 | s->req->cons->flags |= SI_FL_DONT_WAKE; |
| 644 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 645 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 646 | * stream interfaces when their timeouts have expired. |
| 647 | */ |
| 648 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 649 | stream_int_check_timeouts(&s->si[0]); |
| 650 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 651 | |
| 652 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 653 | * for future reads or writes. Note: this will also concern upper layers |
| 654 | * but we do not touch any other flag. We must be careful and correctly |
| 655 | * detect state changes when calling them. |
| 656 | */ |
| 657 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 658 | buffer_check_timeouts(s->req); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 659 | |
| 660 | if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 661 | s->req->prod->shutr(s->req->prod); |
| 662 | |
| 663 | if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) |
| 664 | s->req->cons->shutw(s->req->cons); |
| 665 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 666 | buffer_check_timeouts(s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 667 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 668 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 669 | s->rep->prod->shutr(s->rep->prod); |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 670 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 671 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) |
| 672 | s->rep->cons->shutw(s->rep->cons); |
| 673 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 674 | |
| 675 | /* 1b: check for low-level errors reported at the stream interface. |
| 676 | * First we check if it's a retryable error (in which case we don't |
| 677 | * want to tell the buffer). Otherwise we report the error one level |
| 678 | * upper by setting flags into the buffers. Note that the side towards |
| 679 | * the client cannot have connect (hence retryable) errors. Also, the |
| 680 | * connection setup code must be able to deal with any type of abort. |
| 681 | */ |
| 682 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 683 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 684 | s->si[0].shutr(&s->si[0]); |
| 685 | s->si[0].shutw(&s->si[0]); |
| 686 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 687 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 688 | if (!(s->flags & SN_ERR_MASK)) |
| 689 | s->flags |= SN_ERR_CLICL; |
| 690 | if (!(s->flags & SN_FINST_MASK)) |
| 691 | s->flags |= SN_FINST_D; |
| 692 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 697 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 698 | s->si[1].shutr(&s->si[1]); |
| 699 | s->si[1].shutw(&s->si[1]); |
| 700 | stream_int_report_error(&s->si[1]); |
| 701 | s->be->failed_resp++; |
| 702 | if (s->srv) |
| 703 | s->srv->failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 704 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 705 | if (!(s->flags & SN_ERR_MASK)) |
| 706 | s->flags |= SN_ERR_SRVCL; |
| 707 | if (!(s->flags & SN_FINST_MASK)) |
| 708 | s->flags |= SN_FINST_D; |
| 709 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 710 | } |
| 711 | /* note: maybe we should process connection errors here ? */ |
| 712 | } |
| 713 | |
| 714 | if (s->si[1].state == SI_ST_CON) { |
| 715 | /* we were trying to establish a connection on the server side, |
| 716 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 717 | */ |
| 718 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 719 | sess_update_st_cer(s, &s->si[1]); |
| 720 | else if (s->si[1].state == SI_ST_EST) |
| 721 | sess_establish(s, &s->si[1]); |
| 722 | |
| 723 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 724 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 725 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 726 | */ |
| 727 | } |
| 728 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 729 | resync_stream_interface: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 730 | /* Check for connection closure */ |
| 731 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 732 | DPRINTF(stderr, |
| 733 | "[%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", |
| 734 | now_ms, __FUNCTION__, __LINE__, |
| 735 | t, |
| 736 | s, s->flags, |
| 737 | s->req, s->rep, |
| 738 | s->req->rex, s->rep->wex, |
| 739 | s->req->flags, s->rep->flags, |
| 740 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 741 | s->rep->cons->err_type, s->req->cons->err_type, |
| 742 | s->conn_retries); |
| 743 | |
| 744 | /* nothing special to be done on client side */ |
| 745 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 746 | s->req->prod->state = SI_ST_CLO; |
| 747 | |
| 748 | /* When a server-side connection is released, we have to count it and |
| 749 | * check for pending connections on this server. |
| 750 | */ |
| 751 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 752 | s->req->cons->state = SI_ST_CLO; |
| 753 | if (s->srv) { |
| 754 | if (s->flags & SN_CURR_SESS) { |
| 755 | s->flags &= ~SN_CURR_SESS; |
| 756 | s->srv->cur_sess--; |
| 757 | } |
| 758 | sess_change_server(s, NULL); |
| 759 | if (may_dequeue_tasks(s->srv, s->be)) |
| 760 | process_srv_queue(s->srv); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 766 | * at this point. |
| 767 | */ |
| 768 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 769 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 770 | /* Analyse request */ |
| 771 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 772 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 773 | unsigned int flags = s->req->flags; |
| 774 | |
| 775 | if (s->req->prod->state >= SI_ST_EST) { |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 776 | unsigned int last_ana = 0; |
| 777 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 778 | /* it's up to the analysers to stop new connections */ |
| 779 | buffer_auto_connect(s->req); |
| 780 | buffer_auto_close(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 781 | |
| 782 | /* We will call all analysers for which a bit is set in |
| 783 | * s->req->analysers, following the bit order from LSB |
| 784 | * to MSB. The analysers must remove themselves from |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 785 | * the list when not needed. Any analyser may return 0 |
| 786 | * to break out of the loop, either because of missing |
| 787 | * data to take a decision, or because it decides to |
| 788 | * kill the session. We loop at least once through each |
| 789 | * analyser, and we may loop again if other analysers |
| 790 | * are added in the middle. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 791 | */ |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 792 | while (s->req->analysers & ~last_ana) { |
| 793 | last_ana = s->req->analysers; |
| 794 | |
| 795 | if (s->req->analysers & AN_REQ_INSPECT) { |
| 796 | last_ana |= AN_REQ_INSPECT; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 797 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 798 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 799 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 800 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 801 | if (s->req->analysers & AN_REQ_WAIT_HTTP) { |
| 802 | last_ana |= AN_REQ_WAIT_HTTP; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 803 | if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP)) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 804 | break; |
| 805 | } |
| 806 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 807 | if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) { |
| 808 | last_ana |= AN_REQ_HTTP_PROCESS_FE; |
| 809 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe)) |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | if (s->req->analysers & AN_REQ_SWITCHING_RULES) { |
| 814 | last_ana |= AN_REQ_SWITCHING_RULES; |
| 815 | if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES)) |
| 816 | break; |
Willy Tarreau | d88bb6f | 2009-07-12 09:55:41 +0200 | [diff] [blame] | 817 | /* FIXME: we mait switch from TCP to HTTP and want to |
| 818 | * immediately loop back to the top. This is a dirty way |
| 819 | * of doing it, and we should find a cleaner method relying |
| 820 | * on a circular list of function pointers. |
| 821 | */ |
| 822 | if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP) |
| 823 | continue; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) { |
| 827 | last_ana |= AN_REQ_HTTP_PROCESS_BE; |
| 828 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be)) |
| 829 | break; |
| 830 | } |
| 831 | |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 832 | if (s->req->analysers & AN_REQ_HTTP_TARPIT) { |
| 833 | last_ana |= AN_REQ_HTTP_TARPIT; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 834 | if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT)) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 835 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 836 | } |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 837 | |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 838 | if (s->req->analysers & AN_REQ_HTTP_INNER) { |
| 839 | last_ana |= AN_REQ_HTTP_INNER; |
| 840 | if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER)) |
| 841 | break; |
| 842 | } |
| 843 | |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 844 | if (s->req->analysers & AN_REQ_HTTP_BODY) { |
| 845 | last_ana |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 846 | if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY)) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 847 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 848 | } |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 849 | |
| 850 | if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) { |
| 851 | last_ana |= AN_REQ_PRST_RDP_COOKIE; |
| 852 | if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE)) |
| 853 | break; |
| 854 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 855 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 856 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 857 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 858 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) { |
| 859 | rqf_last = s->req->flags; |
| 860 | goto resync_request; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | resync_response: |
| 865 | /* Analyse response */ |
| 866 | |
| 867 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 868 | /* In inject mode, we wake up everytime something has |
| 869 | * happened on the write side of the buffer. |
| 870 | */ |
| 871 | unsigned int flags = s->rep->flags; |
| 872 | |
| 873 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 874 | !(s->rep->flags & BF_FULL)) { |
| 875 | s->rep->hijacker(s, s->rep); |
| 876 | } |
| 877 | |
| 878 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 879 | rpf_last = s->rep->flags; |
| 880 | goto resync_response; |
| 881 | } |
| 882 | } |
| 883 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 884 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 885 | unsigned int flags = s->rep->flags; |
| 886 | |
| 887 | if (s->rep->prod->state >= SI_ST_EST) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 888 | /* it's up to the analysers to reset auto_close */ |
| 889 | buffer_auto_close(s->rep); |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 890 | if (s->rep->analysers) |
| 891 | process_response(s); |
| 892 | } |
| 893 | |
| 894 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 895 | rpf_last = s->rep->flags; |
| 896 | goto resync_response; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* FIXME: here we should call protocol handlers which rely on |
| 901 | * both buffers. |
| 902 | */ |
| 903 | |
| 904 | |
| 905 | /* |
| 906 | * Now we propagate unhandled errors to the session |
| 907 | */ |
| 908 | if (!(s->flags & SN_ERR_MASK)) { |
| 909 | if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 910 | /* 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] | 911 | s->req->analysers = 0; |
| 912 | if (s->req->flags & BF_READ_ERROR) |
| 913 | s->flags |= SN_ERR_CLICL; |
| 914 | else if (s->req->flags & BF_READ_TIMEOUT) |
| 915 | s->flags |= SN_ERR_CLITO; |
| 916 | else if (s->req->flags & BF_WRITE_ERROR) |
| 917 | s->flags |= SN_ERR_SRVCL; |
| 918 | else |
| 919 | s->flags |= SN_ERR_SRVTO; |
| 920 | sess_set_term_flags(s); |
| 921 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 922 | else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 923 | /* Report it if the server got an error or a read timeout expired */ |
| 924 | s->rep->analysers = 0; |
| 925 | if (s->rep->flags & BF_READ_ERROR) |
| 926 | s->flags |= SN_ERR_SRVCL; |
| 927 | else if (s->rep->flags & BF_READ_TIMEOUT) |
| 928 | s->flags |= SN_ERR_SRVTO; |
| 929 | else if (s->rep->flags & BF_WRITE_ERROR) |
| 930 | s->flags |= SN_ERR_CLICL; |
| 931 | else |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 932 | s->flags |= SN_ERR_CLITO; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 933 | sess_set_term_flags(s); |
| 934 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 937 | /* |
| 938 | * Here we take care of forwarding unhandled data. This also includes |
| 939 | * connection establishments and shutdown requests. |
| 940 | */ |
| 941 | |
| 942 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 943 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 944 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 945 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 946 | if (!s->req->analysers && |
| 947 | !(s->req->flags & (BF_HIJACK|BF_SHUTW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 948 | (s->req->prod->state >= SI_ST_EST) && |
| 949 | (s->req->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 950 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 951 | * attached to it. If any data are left in, we'll permit them to |
| 952 | * move. |
| 953 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 954 | buffer_auto_connect(s->req); |
| 955 | buffer_auto_close(s->req); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 956 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 957 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 958 | /* If the producer is still connected, we'll enable data to flow |
| 959 | * from the producer to the consumer (which might possibly not be |
| 960 | * connected yet). |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 961 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 962 | if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 963 | buffer_forward(s->req, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 964 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 965 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 966 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 967 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 968 | s->req->to_forward && |
| 969 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 970 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 971 | (pipes_used < global.maxpipes) && |
| 972 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 973 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 974 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 975 | s->req->flags |= BF_KERN_SPLICING; |
| 976 | } |
| 977 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 978 | /* reflect what the L7 analysers have seen last */ |
| 979 | rqf_last = s->req->flags; |
| 980 | |
| 981 | /* |
| 982 | * Now forward all shutdown requests between both sides of the buffer |
| 983 | */ |
| 984 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 985 | /* first, let's check if the request buffer needs to shutdown(write), which may |
| 986 | * happen either because the input is closed or because we want to force a close |
| 987 | * once the server has begun to respond. |
| 988 | */ |
| 989 | if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE)) == BF_AUTO_CLOSE) { |
| 990 | if (unlikely((s->req->flags & BF_SHUTR) || |
| 991 | ((s->req->cons->state == SI_ST_EST) && |
| 992 | (s->be->options & PR_O_FORCE_CLO) && |
| 993 | (s->rep->flags & BF_READ_ACTIVITY)))) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 994 | buffer_shutw_now(s->req); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 998 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 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 | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1010 | /* it's possible that an upper layer has requested a connection setup or abort. |
| 1011 | * There are 2 situations where we decide to establish a new connection : |
| 1012 | * - there are data scheduled for emission in the buffer |
| 1013 | * - the BF_AUTO_CONNECT flag is set (active connection) |
| 1014 | */ |
| 1015 | if (s->req->cons->state == SI_ST_INI) { |
| 1016 | if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1017 | if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1018 | /* If we have a ->connect method, we need to perform a connection request, |
| 1019 | * otherwise we immediately switch to the connected state. |
| 1020 | */ |
| 1021 | if (s->req->cons->connect) |
| 1022 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
| 1023 | else |
| 1024 | s->req->cons->state = SI_ST_EST; /* connection established */ |
| 1025 | } |
Willy Tarreau | 7320122 | 2009-08-16 18:27:24 +0200 | [diff] [blame] | 1026 | } |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1027 | else { |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1028 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1029 | buffer_shutw_now(s->req); /* fix buffer flags upon abort */ |
| 1030 | buffer_shutr_now(s->rep); |
| 1031 | } |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1034 | |
| 1035 | /* we may have a pending connection request, or a connection waiting |
| 1036 | * for completion. |
| 1037 | */ |
| 1038 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 1039 | do { |
| 1040 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 1041 | * to give a chance to step 2 to perform a redirect if needed. |
| 1042 | */ |
| 1043 | if (s->si[1].state != SI_ST_REQ) |
| 1044 | sess_update_stream_int(s, &s->si[1]); |
| 1045 | if (s->si[1].state == SI_ST_REQ) |
| 1046 | sess_prepare_conn_req(s, &s->si[1]); |
| 1047 | |
| 1048 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1049 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1050 | perform_http_redirect(s, &s->si[1]); |
| 1051 | } while (s->si[1].state == SI_ST_ASS); |
| 1052 | } |
| 1053 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1054 | /* 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] | 1055 | 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] | 1056 | goto resync_stream_interface; |
| 1057 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1058 | /* otherwise wewant to check if we need to resync the req buffer or not */ |
| 1059 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1060 | goto resync_request; |
| 1061 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1062 | /* perform output updates to the response buffer */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1063 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1064 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1065 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1066 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1067 | if (!s->rep->analysers && |
| 1068 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1069 | (s->rep->prod->state >= SI_ST_EST) && |
| 1070 | (s->rep->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1071 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1072 | * attached to it. If any data are left in, we'll permit them to |
| 1073 | * move. |
| 1074 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1075 | buffer_auto_close(s->rep); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1076 | buffer_flush(s->rep); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1077 | if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1078 | buffer_forward(s->rep, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1079 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1080 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1081 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1082 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1083 | s->rep->to_forward && |
| 1084 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1085 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1086 | (pipes_used < global.maxpipes) && |
| 1087 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1088 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1089 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1090 | s->rep->flags |= BF_KERN_SPLICING; |
| 1091 | } |
| 1092 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1093 | /* reflect what the L7 analysers have seen last */ |
| 1094 | rpf_last = s->rep->flags; |
| 1095 | |
| 1096 | /* |
| 1097 | * Now forward all shutdown requests between both sides of the buffer |
| 1098 | */ |
| 1099 | |
| 1100 | /* |
| 1101 | * FIXME: this is probably where we should produce error responses. |
| 1102 | */ |
| 1103 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1104 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1105 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1106 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1107 | buffer_shutw_now(s->rep); |
| 1108 | |
| 1109 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1110 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1111 | s->rep->cons->shutw(s->rep->cons); |
| 1112 | |
| 1113 | /* 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] | 1114 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1115 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1116 | buffer_shutr_now(s->rep); |
| 1117 | |
| 1118 | /* shutdown(read) pending */ |
| 1119 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1120 | s->rep->prod->shutr(s->rep->prod); |
| 1121 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1122 | 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] | 1123 | goto resync_stream_interface; |
| 1124 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1125 | if (s->req->flags != rqf_last) |
| 1126 | goto resync_request; |
| 1127 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1128 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1129 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1130 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1131 | /* we're interested in getting wakeups again */ |
| 1132 | s->req->prod->flags &= ~SI_FL_DONT_WAKE; |
| 1133 | s->req->cons->flags &= ~SI_FL_DONT_WAKE; |
| 1134 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1135 | /* This is needed only when debugging is enabled, to indicate |
| 1136 | * client-side or server-side close. Please note that in the unlikely |
| 1137 | * event where both sides would close at once, the sequence is reported |
| 1138 | * on the server side first. |
| 1139 | */ |
| 1140 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1141 | (!(global.mode & MODE_QUIET) || |
| 1142 | (global.mode & MODE_VERBOSE)))) { |
| 1143 | int len; |
| 1144 | |
| 1145 | if (s->si[1].state == SI_ST_CLO && |
| 1146 | s->si[1].prev_state == SI_ST_EST) { |
| 1147 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1148 | s->uniq_id, s->be->id, |
| 1149 | (unsigned short)s->si[0].fd, |
| 1150 | (unsigned short)s->si[1].fd); |
| 1151 | write(1, trash, len); |
| 1152 | } |
| 1153 | |
| 1154 | if (s->si[0].state == SI_ST_CLO && |
| 1155 | s->si[0].prev_state == SI_ST_EST) { |
| 1156 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1157 | s->uniq_id, s->be->id, |
| 1158 | (unsigned short)s->si[0].fd, |
| 1159 | (unsigned short)s->si[1].fd); |
| 1160 | write(1, trash, len); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1165 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1166 | |
| 1167 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1168 | session_process_counters(s); |
| 1169 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1170 | if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1171 | s->rep->cons->update(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1172 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1173 | if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1174 | s->req->cons->update(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1175 | |
Willy Tarreau | ea38854 | 2009-06-21 21:45:58 +0200 | [diff] [blame] | 1176 | s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL); |
| 1177 | 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] | 1178 | s->si[0].prev_state = s->si[0].state; |
| 1179 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1180 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1181 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1182 | |
| 1183 | /* Trick: if a request is being waiting for the server to respond, |
| 1184 | * and if we know the server can timeout, we don't want the timeout |
| 1185 | * to expire on the client side first, but we're still interested |
| 1186 | * in passing data from the client to the server (eg: POST). Thus, |
| 1187 | * we can cancel the client's request timeout if the server's |
| 1188 | * request timeout is set and the server has not yet sent a response. |
| 1189 | */ |
| 1190 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1191 | if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1192 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1193 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1194 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1195 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1196 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1197 | /* Call the second stream interface's I/O handler if it's embedded. |
| 1198 | * Note that this one may wake the task up again. |
| 1199 | */ |
| 1200 | if (s->req->cons->iohandler) { |
| 1201 | s->req->cons->iohandler(s->req->cons); |
| 1202 | if (task_in_rq(t)) { |
| 1203 | /* If we woke up, we don't want to requeue the |
| 1204 | * task to the wait queue, but rather requeue |
| 1205 | * it into the runqueue ASAP. |
| 1206 | */ |
| 1207 | t->expire = TICK_ETERNITY; |
| 1208 | return t; |
| 1209 | } |
| 1210 | } |
| 1211 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1212 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1213 | tick_first(s->rep->rex, s->rep->wex)); |
| 1214 | if (s->req->analysers) |
| 1215 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1216 | |
| 1217 | if (s->si[0].exp) |
| 1218 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1219 | |
| 1220 | if (s->si[1].exp) |
| 1221 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1222 | |
| 1223 | #ifdef DEBUG_FULL |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 1224 | fprintf(stderr, |
| 1225 | "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u" |
| 1226 | " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n", |
| 1227 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, |
| 1228 | 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] | 1229 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1230 | |
| 1231 | #ifdef DEBUG_DEV |
| 1232 | /* 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] | 1233 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1234 | ABORT_NOW(); |
| 1235 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1236 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | s->fe->feconn--; |
| 1240 | if (s->flags & SN_BE_ASSIGNED) |
| 1241 | s->be->beconn--; |
| 1242 | actconn--; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 1243 | s->listener->nbconn--; |
| 1244 | if (s->listener->state == LI_FULL && |
| 1245 | s->listener->nbconn < s->listener->maxconn) { |
| 1246 | /* we should reactivate the listener */ |
| 1247 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1248 | s->listener->state = LI_READY; |
| 1249 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1250 | |
| 1251 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1252 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1253 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1254 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1255 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1256 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1257 | write(1, trash, len); |
| 1258 | } |
| 1259 | |
| 1260 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1261 | session_process_counters(s); |
| 1262 | |
| 1263 | /* let's do a final log if we need it */ |
| 1264 | if (s->logs.logwait && |
| 1265 | !(s->flags & SN_MONITOR) && |
| 1266 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1267 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1271 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1272 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1273 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1274 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1277 | /* |
| 1278 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1279 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1280 | * current connection slot. This function also notifies any LB algo which might |
| 1281 | * expect to be informed about any change in the number of active sessions on a |
| 1282 | * server. |
| 1283 | */ |
| 1284 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1285 | { |
| 1286 | if (sess->srv_conn == newsrv) |
| 1287 | return; |
| 1288 | |
| 1289 | if (sess->srv_conn) { |
| 1290 | sess->srv_conn->served--; |
| 1291 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1292 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1293 | sess->srv_conn = NULL; |
| 1294 | } |
| 1295 | |
| 1296 | if (newsrv) { |
| 1297 | newsrv->served++; |
| 1298 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1299 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1300 | sess->srv_conn = newsrv; |
| 1301 | } |
| 1302 | } |
| 1303 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1304 | /* Set correct session termination flags in case no analyser has done it. It |
| 1305 | * also counts a failed request if the server state has not reached the request |
| 1306 | * stage. |
| 1307 | */ |
| 1308 | void sess_set_term_flags(struct session *s) |
| 1309 | { |
| 1310 | if (!(s->flags & SN_FINST_MASK)) { |
| 1311 | if (s->si[1].state < SI_ST_REQ) { |
| 1312 | s->fe->failed_req++; |
| 1313 | s->flags |= SN_FINST_R; |
| 1314 | } |
| 1315 | else if (s->si[1].state == SI_ST_QUE) |
| 1316 | s->flags |= SN_FINST_Q; |
| 1317 | else if (s->si[1].state < SI_ST_EST) |
| 1318 | s->flags |= SN_FINST_C; |
| 1319 | else if (s->si[1].state == SI_ST_EST) |
| 1320 | s->flags |= SN_FINST_D; |
| 1321 | else |
| 1322 | s->flags |= SN_FINST_L; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 1327 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 1328 | * session termination flags so that the caller does not have to worry about |
| 1329 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 1330 | */ |
| 1331 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 1332 | { |
| 1333 | int err_type = si->err_type; |
| 1334 | int err = 0, fin = 0; |
| 1335 | |
| 1336 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 1337 | err = SN_ERR_CLICL; |
| 1338 | fin = SN_FINST_Q; |
| 1339 | } |
| 1340 | else if (err_type & SI_ET_CONN_ABRT) { |
| 1341 | err = SN_ERR_CLICL; |
| 1342 | fin = SN_FINST_C; |
| 1343 | } |
| 1344 | else if (err_type & SI_ET_QUEUE_TO) { |
| 1345 | err = SN_ERR_SRVTO; |
| 1346 | fin = SN_FINST_Q; |
| 1347 | } |
| 1348 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 1349 | err = SN_ERR_SRVCL; |
| 1350 | fin = SN_FINST_Q; |
| 1351 | } |
| 1352 | else if (err_type & SI_ET_CONN_TO) { |
| 1353 | err = SN_ERR_SRVTO; |
| 1354 | fin = SN_FINST_C; |
| 1355 | } |
| 1356 | else if (err_type & SI_ET_CONN_ERR) { |
| 1357 | err = SN_ERR_SRVCL; |
| 1358 | fin = SN_FINST_C; |
| 1359 | } |
| 1360 | else /* SI_ET_CONN_OTHER and others */ { |
| 1361 | err = SN_ERR_INTERNAL; |
| 1362 | fin = SN_FINST_C; |
| 1363 | } |
| 1364 | |
| 1365 | if (!(s->flags & SN_ERR_MASK)) |
| 1366 | s->flags |= err; |
| 1367 | if (!(s->flags & SN_FINST_MASK)) |
| 1368 | s->flags |= fin; |
| 1369 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1370 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1371 | /* |
| 1372 | * Local variables: |
| 1373 | * c-indent-level: 8 |
| 1374 | * c-basic-offset: 8 |
| 1375 | * End: |
| 1376 | */ |