Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 2 | * Session management functions. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | #include <fcntl.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 18 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <types/capture.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 22 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 24 | #include <proto/acl.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 25 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 26 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 27 | #include <proto/checks.h> |
Willy Tarreau | 5ca791d | 2009-08-16 19:06:42 +0200 | [diff] [blame] | 28 | #include <proto/dumpstats.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 29 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 30 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <proto/session.h> |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 32 | #include <proto/pattern.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 33 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 34 | #include <proto/proto_http.h> |
| 35 | #include <proto/proto_tcp.h> |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 36 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 38 | #include <proto/server.h> |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 39 | #include <proto/stick_table.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 40 | #include <proto/stream_interface.h> |
| 41 | #include <proto/stream_sock.h> |
| 42 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 44 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 45 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 47 | /* This function is called from the protocol layer accept() in order to instanciate |
| 48 | * a new session on behalf of a given listener and frontend. It returns a positive |
| 49 | * value upon success, 0 if the connection needs to be closed and ignored, or a |
| 50 | * negative value upon critical failure. |
| 51 | */ |
| 52 | int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr) |
| 53 | { |
| 54 | struct proxy *p = l->frontend; |
| 55 | struct session *s; |
| 56 | struct http_txn *txn; |
| 57 | struct task *t; |
| 58 | |
| 59 | if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) { |
| 60 | Alert("out of memory in event_accept().\n"); |
| 61 | goto out_close; |
| 62 | } |
| 63 | |
| 64 | /* minimum session initialization required for monitor mode below */ |
| 65 | s->flags = 0; |
| 66 | s->logs.logwait = p->to_log; |
| 67 | |
| 68 | /* if this session comes from a known monitoring system, we want to ignore |
| 69 | * it as soon as possible, which means closing it immediately for TCP, but |
| 70 | * cleanly. |
| 71 | */ |
| 72 | if (unlikely((l->options & LI_O_CHK_MONNET) && |
| 73 | addr->ss_family == AF_INET && |
| 74 | (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) { |
| 75 | if (p->mode == PR_MODE_TCP) { |
| 76 | pool_free2(pool2_session, s); |
| 77 | return 0; |
| 78 | } |
| 79 | s->flags |= SN_MONITOR; |
| 80 | s->logs.logwait = 0; |
| 81 | } |
| 82 | |
| 83 | /* OK, we're keeping the session, so let's properly initialize the session */ |
| 84 | LIST_ADDQ(&sessions, &s->list); |
| 85 | LIST_INIT(&s->back_refs); |
| 86 | |
| 87 | if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */ |
| 88 | Alert("out of memory in event_accept().\n"); |
| 89 | goto out_free_session; |
| 90 | } |
| 91 | |
| 92 | s->term_trace = 0; |
| 93 | s->cli_addr = *addr; |
| 94 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 95 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 96 | s->uniq_id = totalconn; |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 97 | p->feconn++; /* beconn will be increased once assigned */ |
| 98 | |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 99 | proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */ |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 100 | |
| 101 | t->process = l->handler; |
| 102 | t->context = s; |
| 103 | t->nice = l->nice; |
| 104 | t->expire = TICK_ETERNITY; |
| 105 | |
| 106 | s->task = t; |
| 107 | s->listener = l; |
| 108 | |
| 109 | /* Note: initially, the session's backend points to the frontend. |
| 110 | * This changes later when switching rules are executed or |
| 111 | * when the default backend is assigned. |
| 112 | */ |
| 113 | s->be = s->fe = p; |
| 114 | s->req = s->rep = NULL; /* will be allocated later */ |
| 115 | |
| 116 | /* now evaluate the tcp-request layer4 rules. Since we expect to be able |
| 117 | * to abort right here as soon as possible, we check the rules before |
| 118 | * even initializing the stream interfaces. |
| 119 | */ |
| 120 | if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) { |
| 121 | task_free(t); |
| 122 | LIST_DEL(&s->list); |
| 123 | pool_free2(pool2_session, s); |
| 124 | /* let's do a no-linger now to close with a single RST. */ |
| 125 | setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 126 | p->feconn--; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 130 | /* This session was accepted, count it now */ |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 131 | if (p->feconn > p->counters.feconn_max) |
| 132 | p->counters.feconn_max = p->feconn; |
Willy Tarreau | b36b424 | 2010-06-04 20:59:39 +0200 | [diff] [blame] | 133 | proxy_inc_fe_sess_ctr(l, p); |
| 134 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 135 | /* this part should be common with other protocols */ |
| 136 | s->si[0].fd = cfd; |
| 137 | s->si[0].owner = t; |
| 138 | s->si[0].state = s->si[0].prev_state = SI_ST_EST; |
| 139 | s->si[0].err_type = SI_ET_NONE; |
| 140 | s->si[0].err_loc = NULL; |
| 141 | s->si[0].connect = NULL; |
| 142 | s->si[0].iohandler = NULL; |
| 143 | s->si[0].exp = TICK_ETERNITY; |
| 144 | s->si[0].flags = SI_FL_NONE; |
| 145 | |
| 146 | if (likely(s->fe->options2 & PR_O2_INDEPSTR)) |
| 147 | s->si[0].flags |= SI_FL_INDEP_STR; |
| 148 | |
| 149 | if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6) |
| 150 | s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */ |
| 151 | |
| 152 | /* add the various callbacks */ |
| 153 | stream_sock_prepare_interface(&s->si[0]); |
| 154 | |
| 155 | /* pre-initialize the other side's stream interface to an INIT state. The |
| 156 | * callbacks will be initialized before attempting to connect. |
| 157 | */ |
| 158 | s->si[1].fd = -1; /* just to help with debugging */ |
| 159 | s->si[1].owner = t; |
| 160 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 161 | s->si[1].err_type = SI_ET_NONE; |
| 162 | s->si[1].err_loc = NULL; |
| 163 | s->si[1].connect = NULL; |
| 164 | s->si[1].iohandler = NULL; |
| 165 | s->si[1].shutr = stream_int_shutr; |
| 166 | s->si[1].shutw = stream_int_shutw; |
| 167 | s->si[1].exp = TICK_ETERNITY; |
| 168 | s->si[1].flags = SI_FL_NONE; |
| 169 | |
| 170 | if (likely(s->fe->options2 & PR_O2_INDEPSTR)) |
| 171 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 172 | |
| 173 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 174 | s->pend_pos = NULL; |
| 175 | |
| 176 | /* init store persistence */ |
| 177 | s->store_count = 0; |
| 178 | |
| 179 | /* Adjust some socket options */ |
| 180 | if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) { |
| 181 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 182 | goto out_free_task; |
| 183 | } |
| 184 | |
| 185 | txn = &s->txn; |
| 186 | /* Those variables will be checked and freed if non-NULL in |
| 187 | * session.c:session_free(). It is important that they are |
| 188 | * properly initialized. |
| 189 | */ |
| 190 | txn->sessid = NULL; |
| 191 | txn->srv_cookie = NULL; |
| 192 | txn->cli_cookie = NULL; |
| 193 | txn->uri = NULL; |
| 194 | txn->req.cap = NULL; |
| 195 | txn->rsp.cap = NULL; |
| 196 | txn->hdr_idx.v = NULL; |
| 197 | txn->hdr_idx.size = txn->hdr_idx.used = 0; |
| 198 | |
| 199 | if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL)) |
| 200 | goto out_free_task; /* no memory */ |
| 201 | |
| 202 | if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL)) |
| 203 | goto out_free_req; /* no memory */ |
| 204 | |
| 205 | /* initialize the request buffer */ |
| 206 | s->req->size = global.tune.bufsize; |
| 207 | buffer_init(s->req); |
| 208 | s->req->prod = &s->si[0]; |
| 209 | s->req->cons = &s->si[1]; |
| 210 | s->si[0].ib = s->si[1].ob = s->req; |
| 211 | s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */ |
| 212 | |
| 213 | /* activate default analysers enabled for this listener */ |
| 214 | s->req->analysers = l->analysers; |
| 215 | |
| 216 | s->req->wto = TICK_ETERNITY; |
| 217 | s->req->rto = TICK_ETERNITY; |
| 218 | s->req->rex = TICK_ETERNITY; |
| 219 | s->req->wex = TICK_ETERNITY; |
| 220 | s->req->analyse_exp = TICK_ETERNITY; |
| 221 | |
| 222 | /* initialize response buffer */ |
| 223 | s->rep->size = global.tune.bufsize; |
| 224 | buffer_init(s->rep); |
| 225 | s->rep->prod = &s->si[1]; |
| 226 | s->rep->cons = &s->si[0]; |
| 227 | s->si[0].ob = s->si[1].ib = s->rep; |
| 228 | s->rep->analysers = 0; |
| 229 | |
| 230 | s->rep->rto = TICK_ETERNITY; |
| 231 | s->rep->wto = TICK_ETERNITY; |
| 232 | s->rep->rex = TICK_ETERNITY; |
| 233 | s->rep->wex = TICK_ETERNITY; |
| 234 | s->rep->analyse_exp = TICK_ETERNITY; |
| 235 | |
| 236 | /* finish initialization of the accepted file descriptor */ |
| 237 | fd_insert(cfd); |
| 238 | fdtab[cfd].owner = &s->si[0]; |
| 239 | fdtab[cfd].state = FD_STREADY; |
| 240 | fdtab[cfd].flags = 0; |
| 241 | fdtab[cfd].cb[DIR_RD].f = l->proto->read; |
| 242 | fdtab[cfd].cb[DIR_RD].b = s->req; |
| 243 | fdtab[cfd].cb[DIR_WR].f = l->proto->write; |
| 244 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
| 245 | fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr; |
| 246 | fdinfo[cfd].peerlen = sizeof(s->cli_addr); |
| 247 | EV_FD_SET(cfd, DIR_RD); |
| 248 | |
| 249 | if (p->accept) { |
| 250 | int ret = p->accept(s); |
| 251 | if (unlikely(ret < 0)) |
| 252 | goto out_free_rep; |
| 253 | |
| 254 | if (unlikely(ret == 0)) { |
| 255 | /* work is finished, we can release everything (eg: monitoring) */ |
| 256 | pool_free2(pool2_buffer, s->rep); |
| 257 | pool_free2(pool2_buffer, s->req); |
| 258 | task_free(t); |
| 259 | LIST_DEL(&s->list); |
| 260 | pool_free2(pool2_session, s); |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 261 | p->feconn--; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* it is important not to call the wakeup function directly but to |
| 267 | * pass through task_wakeup(), because this one knows how to apply |
| 268 | * priorities to tasks. |
| 269 | */ |
| 270 | task_wakeup(t, TASK_WOKEN_INIT); |
| 271 | return 1; |
| 272 | |
| 273 | /* Error unrolling */ |
| 274 | out_free_rep: |
| 275 | pool_free2(pool2_buffer, s->rep); |
| 276 | out_free_req: |
| 277 | pool_free2(pool2_buffer, s->req); |
| 278 | out_free_task: |
Willy Tarreau | 24dcaf3 | 2010-06-05 10:49:41 +0200 | [diff] [blame] | 279 | p->feconn--; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 280 | task_free(t); |
| 281 | out_free_session: |
| 282 | LIST_DEL(&s->list); |
| 283 | pool_free2(pool2_session, s); |
| 284 | out_close: |
| 285 | return -1; |
| 286 | } |
| 287 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 288 | /* |
| 289 | * frees the context associated to a session. It must have been removed first. |
| 290 | */ |
| 291 | void session_free(struct session *s) |
| 292 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 293 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 294 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 295 | struct bref *bref, *back; |
Willy Tarreau | a4cda67 | 2010-06-06 18:28:49 +0200 | [diff] [blame] | 296 | int i; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 297 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | if (s->pend_pos) |
| 299 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 300 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 301 | if (s->srv) { /* there may be requests left pending in queue */ |
| 302 | if (s->flags & SN_CURR_SESS) { |
| 303 | s->flags &= ~SN_CURR_SESS; |
| 304 | s->srv->cur_sess--; |
| 305 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 306 | if (may_dequeue_tasks(s->srv, s->be)) |
| 307 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 308 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 309 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 310 | if (unlikely(s->srv_conn)) { |
| 311 | /* the session still has a reserved slot on a server, but |
| 312 | * it should normally be only the same as the one above, |
| 313 | * so this should not happen in fact. |
| 314 | */ |
| 315 | sess_change_server(s, NULL); |
| 316 | } |
| 317 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 318 | if (s->req->pipe) |
| 319 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 320 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 321 | if (s->rep->pipe) |
| 322 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 323 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 324 | pool_free2(pool2_buffer, s->req); |
| 325 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 327 | http_end_txn(s); |
| 328 | |
Willy Tarreau | a4cda67 | 2010-06-06 18:28:49 +0200 | [diff] [blame] | 329 | for (i = 0; i < s->store_count; i++) { |
| 330 | if (!s->store[i].ts) |
| 331 | continue; |
| 332 | stksess_free(s->store[i].table, s->store[i].ts); |
| 333 | s->store[i].ts = NULL; |
| 334 | } |
| 335 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 336 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 337 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 338 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
| 339 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 340 | } |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 341 | |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 342 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 343 | /* we have to unlink all watchers. We must not relink them if |
| 344 | * this session was the last one in the list. |
| 345 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 346 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 347 | LIST_INIT(&bref->users); |
| 348 | if (s->list.n != &sessions) |
| 349 | 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] | 350 | bref->ref = s->list.n; |
| 351 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 352 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 353 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 354 | |
| 355 | /* 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] | 356 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 357 | pool_flush2(pool2_buffer); |
| 358 | pool_flush2(fe->hdr_idx_pool); |
| 359 | pool_flush2(pool2_requri); |
| 360 | pool_flush2(pool2_capture); |
| 361 | pool_flush2(pool2_session); |
| 362 | pool_flush2(fe->req_cap_pool); |
| 363 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 364 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 369 | int init_session() |
| 370 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 371 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 372 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 373 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 376 | void session_process_counters(struct session *s) |
| 377 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 378 | unsigned long long bytes; |
| 379 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 380 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 381 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 382 | s->logs.bytes_in = s->req->total; |
| 383 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 384 | s->fe->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 385 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 386 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 387 | s->be->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 388 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 389 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 390 | s->srv->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 391 | |
| 392 | if (s->listener->counters) |
| 393 | s->listener->counters->bytes_in += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 394 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 397 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 398 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 399 | s->logs.bytes_out = s->rep->total; |
| 400 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 401 | s->fe->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 402 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 403 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 404 | s->be->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 405 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 406 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 407 | s->srv->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 408 | |
| 409 | if (s->listener->counters) |
| 410 | s->listener->counters->bytes_out += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 411 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 412 | } |
| 413 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 414 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 415 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 416 | * connection was attempted and that the file descriptor is already allocated. |
| 417 | * We must check for establishment, error and abort. Possible output states |
| 418 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 419 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 420 | * otherwise 1. |
| 421 | */ |
| 422 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 423 | { |
| 424 | struct buffer *req = si->ob; |
| 425 | struct buffer *rep = si->ib; |
| 426 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 427 | /* If we got an error, or if nothing happened and the connection timed |
| 428 | * out, we must give up. The CER state handler will take care of retry |
| 429 | * attempts and error reports. |
| 430 | */ |
| 431 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 432 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 433 | si->state = SI_ST_CER; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 434 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 435 | fd_delete(si->fd); |
| 436 | |
| 437 | if (si->err_type) |
| 438 | return 0; |
| 439 | |
| 440 | si->err_loc = s->srv; |
| 441 | if (si->flags & SI_FL_ERR) |
| 442 | si->err_type = SI_ET_CONN_ERR; |
| 443 | else |
| 444 | si->err_type = SI_ET_CONN_TO; |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /* OK, maybe we want to abort */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 449 | if (unlikely((rep->flags & BF_SHUTW) || |
| 450 | ((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] | 451 | (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 452 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 453 | /* give up */ |
| 454 | si->shutw(si); |
| 455 | si->err_type |= SI_ET_CONN_ABRT; |
| 456 | si->err_loc = s->srv; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 457 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +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 1; |
| 461 | } |
| 462 | |
| 463 | /* we need to wait a bit more if there was no activity either */ |
| 464 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 465 | return 1; |
| 466 | |
| 467 | /* OK, this means that a connection succeeded. The caller will be |
| 468 | * responsible for handling the transition from CON to EST. |
| 469 | */ |
| 470 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 471 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 472 | si->state = SI_ST_EST; |
| 473 | si->err_type = SI_ET_NONE; |
| 474 | si->err_loc = NULL; |
| 475 | return 1; |
| 476 | } |
| 477 | |
| 478 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 479 | * previous connection attempt has failed and that the file descriptor |
| 480 | * has already been released. Possible causes include asynchronous error |
| 481 | * notification and time out. Possible output states are SI_ST_CLO when |
| 482 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 483 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 484 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 485 | * marked as in error state. It returns 0. |
| 486 | */ |
| 487 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 488 | { |
| 489 | /* we probably have to release last session from the server */ |
| 490 | if (s->srv) { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 491 | health_adjust(s->srv, HANA_STATUS_L4_ERR); |
| 492 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 493 | if (s->flags & SN_CURR_SESS) { |
| 494 | s->flags &= ~SN_CURR_SESS; |
| 495 | s->srv->cur_sess--; |
| 496 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* ensure that we have enough retries left */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 500 | si->conn_retries--; |
| 501 | if (si->conn_retries < 0) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 502 | if (!si->err_type) { |
| 503 | si->err_type = SI_ET_CONN_ERR; |
| 504 | si->err_loc = s->srv; |
| 505 | } |
| 506 | |
| 507 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 508 | s->srv->counters.failed_conns++; |
| 509 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 510 | if (may_dequeue_tasks(s->srv, s->be)) |
| 511 | process_srv_queue(s->srv); |
| 512 | |
| 513 | /* shutw is enough so stop a connecting socket */ |
| 514 | si->shutw(si); |
| 515 | si->ob->flags |= BF_WRITE_ERROR; |
| 516 | si->ib->flags |= BF_READ_ERROR; |
| 517 | |
| 518 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 519 | if (s->srv_error) |
| 520 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 525 | * retry on another server for the last retry. In order to achieve this, |
| 526 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 527 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 528 | * redispatch yet, because this will depend on what server is selected. |
| 529 | */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 530 | if (s->srv && si->conn_retries == 0 && |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 531 | s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 532 | if (may_dequeue_tasks(s->srv, s->be)) |
| 533 | process_srv_queue(s->srv); |
| 534 | |
| 535 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 536 | s->prev_srv = s->srv; |
| 537 | si->state = SI_ST_REQ; |
| 538 | } else { |
| 539 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 540 | s->srv->counters.retries++; |
| 541 | s->be->counters.retries++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 542 | si->state = SI_ST_ASS; |
| 543 | } |
| 544 | |
| 545 | if (si->flags & SI_FL_ERR) { |
| 546 | /* The error was an asynchronous connection error, and we will |
| 547 | * likely have to retry connecting to the same server, most |
| 548 | * likely leading to the same result. To avoid this, we wait |
| 549 | * one second before retrying. |
| 550 | */ |
| 551 | |
| 552 | if (!si->err_type) |
| 553 | si->err_type = SI_ET_CONN_ERR; |
| 554 | |
| 555 | si->state = SI_ST_TAR; |
| 556 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 557 | return 0; |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * This function handles the transition between the SI_ST_CON state and the |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 564 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or |
| 565 | * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined. |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 566 | */ |
| 567 | void sess_establish(struct session *s, struct stream_interface *si) |
| 568 | { |
| 569 | struct buffer *req = si->ob; |
| 570 | struct buffer *rep = si->ib; |
| 571 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 572 | if (s->srv) |
| 573 | health_adjust(s->srv, HANA_STATUS_L4_OK); |
| 574 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 575 | 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] | 576 | /* if the user wants to log as soon as possible, without counting |
| 577 | * bytes from the server, then this is the right moment. */ |
| 578 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 579 | 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] | 580 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 581 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 582 | } |
| 583 | else { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 584 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 585 | /* reset hdr_idx which was already initialized by the request. |
| 586 | * right now, the http parser does it. |
| 587 | * hdr_idx_init(&s->txn.hdr_idx); |
| 588 | */ |
| 589 | } |
| 590 | |
Willy Tarreau | 4e5b828 | 2009-08-16 22:57:50 +0200 | [diff] [blame] | 591 | rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 592 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 593 | if (si->connect) { |
| 594 | /* real connections have timeouts */ |
| 595 | req->wto = s->be->timeout.server; |
| 596 | rep->rto = s->be->timeout.server; |
| 597 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 598 | req->wex = TICK_ETERNITY; |
| 599 | } |
| 600 | |
| 601 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 602 | * Other input states are simply ignored. |
| 603 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 604 | * Flags must have previously been updated for timeouts and other conditions. |
| 605 | */ |
| 606 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 607 | { |
| 608 | 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", |
| 609 | now_ms, __FUNCTION__, |
| 610 | s, |
| 611 | s->req, s->rep, |
| 612 | s->req->rex, s->rep->wex, |
| 613 | s->req->flags, s->rep->flags, |
| 614 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 615 | |
| 616 | if (si->state == SI_ST_ASS) { |
| 617 | /* Server assigned to connection request, we have to try to connect now */ |
| 618 | int conn_err; |
| 619 | |
| 620 | conn_err = connect_server(s); |
| 621 | if (conn_err == SN_ERR_NONE) { |
| 622 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 623 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 624 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
| 628 | /* We have received a synchronous error. We might have to |
| 629 | * abort, retry immediately or redispatch. |
| 630 | */ |
| 631 | if (conn_err == SN_ERR_INTERNAL) { |
| 632 | if (!si->err_type) { |
| 633 | si->err_type = SI_ET_CONN_OTHER; |
| 634 | si->err_loc = s->srv; |
| 635 | } |
| 636 | |
| 637 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 638 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 639 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 640 | s->srv->counters.failed_conns++; |
| 641 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 642 | |
| 643 | /* release other sessions waiting for this server */ |
| 644 | if (may_dequeue_tasks(s->srv, s->be)) |
| 645 | process_srv_queue(s->srv); |
| 646 | |
| 647 | /* Failed and not retryable. */ |
| 648 | si->shutr(si); |
| 649 | si->shutw(si); |
| 650 | si->ob->flags |= BF_WRITE_ERROR; |
| 651 | |
| 652 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 653 | |
| 654 | /* no session was ever accounted for this server */ |
| 655 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 656 | if (s->srv_error) |
| 657 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
| 661 | /* We are facing a retryable error, but we don't want to run a |
| 662 | * turn-around now, as the problem is likely a source port |
| 663 | * allocation problem, so we want to retry now. |
| 664 | */ |
| 665 | si->state = SI_ST_CER; |
| 666 | si->flags &= ~SI_FL_ERR; |
| 667 | sess_update_st_cer(s, si); |
| 668 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 669 | return; |
| 670 | } |
| 671 | else if (si->state == SI_ST_QUE) { |
| 672 | /* connection request was queued, check for any update */ |
| 673 | if (!s->pend_pos) { |
| 674 | /* The connection is not in the queue anymore. Either |
| 675 | * we have a server connection slot available and we |
| 676 | * go directly to the assigned state, or we need to |
| 677 | * load-balance first and go to the INI state. |
| 678 | */ |
| 679 | si->exp = TICK_ETERNITY; |
| 680 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 681 | si->state = SI_ST_REQ; |
| 682 | else { |
| 683 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 684 | si->state = SI_ST_ASS; |
| 685 | } |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | /* Connection request still in queue... */ |
| 690 | if (si->flags & SI_FL_EXP) { |
| 691 | /* ... and timeout expired */ |
| 692 | si->exp = TICK_ETERNITY; |
| 693 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 694 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 695 | s->srv->counters.failed_conns++; |
| 696 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 697 | si->shutr(si); |
| 698 | si->shutw(si); |
| 699 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 700 | if (!si->err_type) |
| 701 | si->err_type = SI_ET_QUEUE_TO; |
| 702 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 703 | if (s->srv_error) |
| 704 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
| 708 | /* Connection remains in queue, check if we have to abort it */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 709 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 710 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 711 | (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] | 712 | /* give up */ |
| 713 | si->exp = TICK_ETERNITY; |
| 714 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 715 | si->shutr(si); |
| 716 | si->shutw(si); |
| 717 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 718 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 719 | if (s->srv_error) |
| 720 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | /* Nothing changed */ |
| 725 | return; |
| 726 | } |
| 727 | else if (si->state == SI_ST_TAR) { |
| 728 | /* Connection request might be aborted */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 729 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 730 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 731 | (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] | 732 | /* give up */ |
| 733 | si->exp = TICK_ETERNITY; |
| 734 | si->shutr(si); |
| 735 | si->shutw(si); |
| 736 | si->err_type |= SI_ET_CONN_ABRT; |
| 737 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 738 | if (s->srv_error) |
| 739 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
| 743 | if (!(si->flags & SI_FL_EXP)) |
| 744 | return; /* still in turn-around */ |
| 745 | |
| 746 | si->exp = TICK_ETERNITY; |
| 747 | |
| 748 | /* we keep trying on the same server as long as the session is |
| 749 | * marked "assigned". |
| 750 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 751 | */ |
| 752 | if (s->flags & SN_ASSIGNED) |
| 753 | si->state = SI_ST_ASS; |
| 754 | else |
| 755 | si->state = SI_ST_REQ; |
| 756 | return; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* This function initiates a server connection request on a stream interface |
| 761 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 762 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 763 | * or SI_ST_CLO upon error. |
| 764 | */ |
| 765 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 766 | 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", |
| 767 | now_ms, __FUNCTION__, |
| 768 | s, |
| 769 | s->req, s->rep, |
| 770 | s->req->rex, s->rep->wex, |
| 771 | s->req->flags, s->rep->flags, |
| 772 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 773 | |
| 774 | if (si->state != SI_ST_REQ) |
| 775 | return; |
| 776 | |
| 777 | /* Try to assign a server */ |
| 778 | if (srv_redispatch_connect(s) != 0) { |
| 779 | /* We did not get a server. Either we queued the |
| 780 | * connection request, or we encountered an error. |
| 781 | */ |
| 782 | if (si->state == SI_ST_QUE) |
| 783 | return; |
| 784 | |
| 785 | /* we did not get any server, let's check the cause */ |
| 786 | si->shutr(si); |
| 787 | si->shutw(si); |
| 788 | si->ob->flags |= BF_WRITE_ERROR; |
| 789 | if (!si->err_type) |
| 790 | si->err_type = SI_ET_CONN_OTHER; |
| 791 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 792 | if (s->srv_error) |
| 793 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
| 797 | /* The server is assigned */ |
| 798 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 799 | si->state = SI_ST_ASS; |
| 800 | } |
| 801 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 802 | /* This stream analyser checks the switching rules and changes the backend |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 803 | * if appropriate. The default_backend rule is also considered, then the |
| 804 | * target backend's forced persistence rules are also evaluated last if any. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 805 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 806 | * either needs more data or wants to immediately abort the request. |
| 807 | */ |
| 808 | int process_switching_rules(struct session *s, struct buffer *req, int an_bit) |
| 809 | { |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 810 | struct persist_rule *prst_rule; |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 811 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 812 | req->analysers &= ~an_bit; |
| 813 | req->analyse_exp = TICK_ETERNITY; |
| 814 | |
| 815 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 816 | now_ms, __FUNCTION__, |
| 817 | s, |
| 818 | req, |
| 819 | req->rex, req->wex, |
| 820 | req->flags, |
| 821 | req->l, |
| 822 | req->analysers); |
| 823 | |
| 824 | /* now check whether we have some switching rules for this request */ |
| 825 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 826 | struct switching_rule *rule; |
| 827 | |
| 828 | list_for_each_entry(rule, &s->fe->switching_rules, list) { |
| 829 | int ret; |
| 830 | |
| 831 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ); |
| 832 | ret = acl_pass(ret); |
| 833 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 834 | ret = !ret; |
| 835 | |
| 836 | if (ret) { |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 837 | if (!session_set_backend(s, rule->be.backend)) |
| 838 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* To ensure correct connection accounting on the backend, we |
| 844 | * have to assign one if it was not set (eg: a listen). This |
| 845 | * measure also takes care of correctly setting the default |
| 846 | * backend if any. |
| 847 | */ |
| 848 | if (!(s->flags & SN_BE_ASSIGNED)) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 849 | if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be)) |
| 850 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* we don't want to run the HTTP filters again if the backend has not changed */ |
| 854 | if (s->fe == s->be) |
| 855 | s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE; |
| 856 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 857 | /* as soon as we know the backend, we must check if we have a matching forced or ignored |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 858 | * persistence rule, and report that in the session. |
| 859 | */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 860 | list_for_each_entry(prst_rule, &s->be->persist_rules, list) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 861 | int ret = 1; |
| 862 | |
| 863 | if (prst_rule->cond) { |
| 864 | ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 865 | ret = acl_pass(ret); |
| 866 | if (prst_rule->cond->pol == ACL_COND_UNLESS) |
| 867 | ret = !ret; |
| 868 | } |
| 869 | |
| 870 | if (ret) { |
| 871 | /* no rule, or the rule matches */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 872 | if (prst_rule->type == PERSIST_TYPE_FORCE) { |
| 873 | s->flags |= SN_FORCE_PRST; |
| 874 | } else { |
| 875 | s->flags |= SN_IGNORE_PRST; |
| 876 | } |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 881 | return 1; |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 882 | |
| 883 | sw_failed: |
| 884 | /* immediately abort this request in case of allocation failure */ |
| 885 | buffer_abort(s->req); |
| 886 | buffer_abort(s->rep); |
| 887 | |
| 888 | if (!(s->flags & SN_ERR_MASK)) |
| 889 | s->flags |= SN_ERR_RESOURCE; |
| 890 | if (!(s->flags & SN_FINST_MASK)) |
| 891 | s->flags |= SN_FINST_R; |
| 892 | |
| 893 | s->txn.status = 500; |
| 894 | s->req->analysers = 0; |
| 895 | s->req->analyse_exp = TICK_ETERNITY; |
| 896 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 899 | /* This stream analyser works on a request. It applies all sticking rules on |
| 900 | * it then returns 1. The data must already be present in the buffer otherwise |
| 901 | * they won't match. It always returns 1. |
| 902 | */ |
| 903 | int process_sticking_rules(struct session *s, struct buffer *req, int an_bit) |
| 904 | { |
| 905 | struct proxy *px = s->be; |
| 906 | struct sticking_rule *rule; |
| 907 | |
| 908 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 909 | now_ms, __FUNCTION__, |
| 910 | s, |
| 911 | req, |
| 912 | req->rex, req->wex, |
| 913 | req->flags, |
| 914 | req->l, |
| 915 | req->analysers); |
| 916 | |
| 917 | list_for_each_entry(rule, &px->sticking_rules, list) { |
| 918 | int ret = 1 ; |
| 919 | int i; |
| 920 | |
| 921 | for (i = 0; i < s->store_count; i++) { |
| 922 | if (rule->table.t == s->store[i].table) |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | if (i != s->store_count) |
| 927 | continue; |
| 928 | |
| 929 | if (rule->cond) { |
| 930 | ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ); |
| 931 | ret = acl_pass(ret); |
| 932 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 933 | ret = !ret; |
| 934 | } |
| 935 | |
| 936 | if (ret) { |
| 937 | struct stktable_key *key; |
| 938 | |
| 939 | key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type); |
| 940 | if (!key) |
| 941 | continue; |
| 942 | |
| 943 | if (rule->flags & STK_IS_MATCH) { |
| 944 | struct stksess *ts; |
| 945 | |
| 946 | if ((ts = stktable_lookup(rule->table.t, key)) != NULL) { |
| 947 | if (!(s->flags & SN_ASSIGNED)) { |
| 948 | struct eb32_node *node; |
| 949 | |
| 950 | /* srv found in table */ |
| 951 | node = eb32_lookup(&px->conf.used_server_id, ts->sid); |
| 952 | if (node) { |
| 953 | struct server *srv; |
| 954 | |
| 955 | srv = container_of(node, struct server, conf.id); |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 956 | if ((srv->state & SRV_RUNNING) || |
| 957 | (px->options & PR_O_PERSIST) || |
| 958 | (s->flags & SN_FORCE_PRST)) { |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 959 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 960 | s->srv = srv; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | ts->expire = tick_add(now_ms, MS_TO_TICKS(rule->table.t->expire)); |
| 965 | } |
| 966 | } |
| 967 | if (rule->flags & STK_IS_STORE) { |
| 968 | if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) { |
| 969 | struct stksess *ts; |
| 970 | |
| 971 | ts = stksess_new(rule->table.t, key); |
| 972 | if (ts) { |
| 973 | s->store[s->store_count].table = rule->table.t; |
| 974 | s->store[s->store_count++].ts = ts; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | req->analysers &= ~an_bit; |
| 982 | req->analyse_exp = TICK_ETERNITY; |
| 983 | return 1; |
| 984 | } |
| 985 | |
| 986 | /* This stream analyser works on a response. It applies all store rules on it |
| 987 | * then returns 1. The data must already be present in the buffer otherwise |
| 988 | * they won't match. It always returns 1. |
| 989 | */ |
| 990 | int process_store_rules(struct session *s, struct buffer *rep, int an_bit) |
| 991 | { |
| 992 | struct proxy *px = s->be; |
| 993 | struct sticking_rule *rule; |
| 994 | int i; |
| 995 | |
| 996 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 997 | now_ms, __FUNCTION__, |
| 998 | s, |
Willy Tarreau | 2e2b3eb | 2010-02-09 20:55:44 +0100 | [diff] [blame] | 999 | rep, |
| 1000 | rep->rex, rep->wex, |
| 1001 | rep->flags, |
| 1002 | rep->l, |
| 1003 | rep->analysers); |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1004 | |
| 1005 | list_for_each_entry(rule, &px->storersp_rules, list) { |
| 1006 | int ret = 1 ; |
| 1007 | int storereqidx = -1; |
| 1008 | |
| 1009 | for (i = 0; i < s->store_count; i++) { |
| 1010 | if (rule->table.t == s->store[i].table) { |
| 1011 | if (!(s->store[i].flags)) |
| 1012 | storereqidx = i; |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if ((i != s->store_count) && (storereqidx == -1)) |
| 1018 | continue; |
| 1019 | |
| 1020 | if (rule->cond) { |
| 1021 | ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR); |
| 1022 | ret = acl_pass(ret); |
| 1023 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1024 | ret = !ret; |
| 1025 | } |
| 1026 | |
| 1027 | if (ret) { |
| 1028 | struct stktable_key *key; |
| 1029 | |
| 1030 | key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type); |
| 1031 | if (!key) |
| 1032 | continue; |
| 1033 | |
| 1034 | if (storereqidx != -1) { |
| 1035 | stksess_key(s->store[storereqidx].table, s->store[storereqidx].ts, key); |
| 1036 | s->store[storereqidx].flags = 1; |
| 1037 | } |
| 1038 | else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) { |
| 1039 | struct stksess *ts; |
| 1040 | |
| 1041 | ts = stksess_new(rule->table.t, key); |
| 1042 | if (ts) { |
| 1043 | s->store[s->store_count].table = rule->table.t; |
| 1044 | s->store[s->store_count].flags = 1; |
| 1045 | s->store[s->store_count++].ts = ts; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /* process store request and store response */ |
| 1052 | for (i = 0; i < s->store_count; i++) { |
| 1053 | if (stktable_store(s->store[i].table, s->store[i].ts, s->srv->puid) > 0) { |
| 1054 | stksess_free(s->store[i].table, s->store[i].ts); |
| 1055 | s->store[i].ts = NULL; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | rep->analysers &= ~an_bit; |
| 1060 | rep->analyse_exp = TICK_ETERNITY; |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1064 | /* This macro is very specific to the function below. See the comments in |
| 1065 | * process_session() below to understand the logic and the tests. |
| 1066 | */ |
| 1067 | #define UPDATE_ANALYSERS(real, list, back, flag) { \ |
| 1068 | list = (((list) & ~(flag)) | ~(back)) & (real); \ |
| 1069 | back = real; \ |
| 1070 | if (!(list)) \ |
| 1071 | break; \ |
| 1072 | if (((list) ^ ((list) & ((list) - 1))) < (flag)) \ |
| 1073 | continue; \ |
| 1074 | } |
| 1075 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1076 | /* Processes the client, server, request and response jobs of a session task, |
| 1077 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 1078 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 1079 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 1080 | * nothing too many times, the request and response buffers flags are monitored |
| 1081 | * and each function is called only if at least another function has changed at |
| 1082 | * least one flag it is interested in. |
| 1083 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1084 | struct task *process_session(struct task *t) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1085 | { |
| 1086 | struct session *s = t->context; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1087 | unsigned int rqf_last, rpf_last; |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1088 | unsigned int req_ana_back; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1089 | |
| 1090 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 1091 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 1092 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1093 | /* this data may be no longer valid, clear it */ |
| 1094 | memset(&s->txn.auth, 0, sizeof(s->txn.auth)); |
| 1095 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1096 | /* This flag must explicitly be set every time */ |
| 1097 | s->req->flags &= ~BF_READ_NOEXP; |
| 1098 | |
| 1099 | /* Keep a copy of req/rep flags so that we can detect shutdowns */ |
| 1100 | rqf_last = s->req->flags; |
| 1101 | rpf_last = s->rep->flags; |
| 1102 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1103 | /* we don't want the stream interface functions to recursively wake us up */ |
| 1104 | if (s->req->prod->owner == t) |
| 1105 | s->req->prod->flags |= SI_FL_DONT_WAKE; |
| 1106 | if (s->req->cons->owner == t) |
| 1107 | s->req->cons->flags |= SI_FL_DONT_WAKE; |
| 1108 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1109 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 1110 | * stream interfaces when their timeouts have expired. |
| 1111 | */ |
| 1112 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 1113 | stream_int_check_timeouts(&s->si[0]); |
| 1114 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 1117 | * for future reads or writes. Note: this will also concern upper layers |
| 1118 | * but we do not touch any other flag. We must be careful and correctly |
| 1119 | * detect state changes when calling them. |
| 1120 | */ |
| 1121 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1122 | buffer_check_timeouts(s->req); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1123 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 1124 | if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 1125 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 1126 | s->req->cons->shutw(s->req->cons); |
| 1127 | } |
| 1128 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1129 | if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 1130 | s->req->prod->shutr(s->req->prod); |
| 1131 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1132 | buffer_check_timeouts(s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1133 | |
Willy Tarreau | 1464140 | 2009-12-29 14:49:56 +0100 | [diff] [blame] | 1134 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) { |
| 1135 | s->rep->cons->flags |= SI_FL_NOLINGER; |
| 1136 | s->rep->cons->shutw(s->rep->cons); |
| 1137 | } |
| 1138 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1139 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 1140 | s->rep->prod->shutr(s->rep->prod); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1141 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1142 | |
| 1143 | /* 1b: check for low-level errors reported at the stream interface. |
| 1144 | * First we check if it's a retryable error (in which case we don't |
| 1145 | * want to tell the buffer). Otherwise we report the error one level |
| 1146 | * upper by setting flags into the buffers. Note that the side towards |
| 1147 | * the client cannot have connect (hence retryable) errors. Also, the |
| 1148 | * connection setup code must be able to deal with any type of abort. |
| 1149 | */ |
| 1150 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 1151 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 1152 | s->si[0].shutr(&s->si[0]); |
| 1153 | s->si[0].shutw(&s->si[0]); |
| 1154 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1155 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1156 | s->be->counters.cli_aborts++; |
| 1157 | if (s->srv) |
| 1158 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1159 | if (!(s->flags & SN_ERR_MASK)) |
| 1160 | s->flags |= SN_ERR_CLICL; |
| 1161 | if (!(s->flags & SN_FINST_MASK)) |
| 1162 | s->flags |= SN_FINST_D; |
| 1163 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 1168 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 1169 | s->si[1].shutr(&s->si[1]); |
| 1170 | s->si[1].shutw(&s->si[1]); |
| 1171 | stream_int_report_error(&s->si[1]); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1172 | s->be->counters.failed_resp++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1173 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1174 | s->srv->counters.failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1175 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1176 | s->be->counters.srv_aborts++; |
| 1177 | if (s->srv) |
| 1178 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 1179 | if (!(s->flags & SN_ERR_MASK)) |
| 1180 | s->flags |= SN_ERR_SRVCL; |
| 1181 | if (!(s->flags & SN_FINST_MASK)) |
| 1182 | s->flags |= SN_FINST_D; |
| 1183 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1184 | } |
| 1185 | /* note: maybe we should process connection errors here ? */ |
| 1186 | } |
| 1187 | |
| 1188 | if (s->si[1].state == SI_ST_CON) { |
| 1189 | /* we were trying to establish a connection on the server side, |
| 1190 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 1191 | */ |
| 1192 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 1193 | sess_update_st_cer(s, &s->si[1]); |
| 1194 | else if (s->si[1].state == SI_ST_EST) |
| 1195 | sess_establish(s, &s->si[1]); |
| 1196 | |
| 1197 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 1198 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 1199 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 1200 | */ |
| 1201 | } |
| 1202 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 1203 | resync_stream_interface: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1204 | /* Check for connection closure */ |
| 1205 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1206 | DPRINTF(stderr, |
| 1207 | "[%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", |
| 1208 | now_ms, __FUNCTION__, __LINE__, |
| 1209 | t, |
| 1210 | s, s->flags, |
| 1211 | s->req, s->rep, |
| 1212 | s->req->rex, s->rep->wex, |
| 1213 | s->req->flags, s->rep->flags, |
| 1214 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 1215 | s->rep->cons->err_type, s->req->cons->err_type, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1216 | s->req->cons->conn_retries); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1217 | |
| 1218 | /* nothing special to be done on client side */ |
| 1219 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 1220 | s->req->prod->state = SI_ST_CLO; |
| 1221 | |
| 1222 | /* When a server-side connection is released, we have to count it and |
| 1223 | * check for pending connections on this server. |
| 1224 | */ |
| 1225 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 1226 | s->req->cons->state = SI_ST_CLO; |
| 1227 | if (s->srv) { |
| 1228 | if (s->flags & SN_CURR_SESS) { |
| 1229 | s->flags &= ~SN_CURR_SESS; |
| 1230 | s->srv->cur_sess--; |
| 1231 | } |
| 1232 | sess_change_server(s, NULL); |
| 1233 | if (may_dequeue_tasks(s->srv, s->be)) |
| 1234 | process_srv_queue(s->srv); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 1240 | * at this point. |
| 1241 | */ |
| 1242 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1243 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1244 | /* Analyse request */ |
| 1245 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 1246 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 1247 | unsigned int flags = s->req->flags; |
| 1248 | |
| 1249 | if (s->req->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1250 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1251 | unsigned int ana_list; |
| 1252 | unsigned int ana_back; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1253 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1254 | /* it's up to the analysers to stop new connections, |
| 1255 | * disable reading or closing. Note: if an analyser |
| 1256 | * disables any of these bits, it is responsible for |
| 1257 | * enabling them again when it disables itself, so |
| 1258 | * that other analysers are called in similar conditions. |
| 1259 | */ |
| 1260 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1261 | buffer_auto_connect(s->req); |
| 1262 | buffer_auto_close(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1263 | |
| 1264 | /* We will call all analysers for which a bit is set in |
| 1265 | * s->req->analysers, following the bit order from LSB |
| 1266 | * to MSB. The analysers must remove themselves from |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1267 | * the list when not needed. Any analyser may return 0 |
| 1268 | * to break out of the loop, either because of missing |
| 1269 | * data to take a decision, or because it decides to |
| 1270 | * kill the session. We loop at least once through each |
| 1271 | * analyser, and we may loop again if other analysers |
| 1272 | * are added in the middle. |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1273 | * |
| 1274 | * We build a list of analysers to run. We evaluate all |
| 1275 | * of these analysers in the order of the lower bit to |
| 1276 | * the higher bit. This ordering is very important. |
| 1277 | * An analyser will often add/remove other analysers, |
| 1278 | * including itself. Any changes to itself have no effect |
| 1279 | * on the loop. If it removes any other analysers, we |
| 1280 | * want those analysers not to be called anymore during |
| 1281 | * this loop. If it adds an analyser that is located |
| 1282 | * after itself, we want it to be scheduled for being |
| 1283 | * processed during the loop. If it adds an analyser |
| 1284 | * which is located before it, we want it to switch to |
| 1285 | * it immediately, even if it has already been called |
| 1286 | * once but removed since. |
| 1287 | * |
| 1288 | * In order to achieve this, we compare the analyser |
| 1289 | * list after the call with a copy of it before the |
| 1290 | * call. The work list is fed with analyser bits that |
| 1291 | * appeared during the call. Then we compare previous |
| 1292 | * work list with the new one, and check the bits that |
| 1293 | * appeared. If the lowest of these bits is lower than |
| 1294 | * the current bit, it means we have enabled a previous |
| 1295 | * analyser and must immediately loop again. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1296 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1297 | |
| 1298 | ana_list = ana_back = s->req->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1299 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1300 | /* Warning! ensure that analysers are always placed in ascending order! */ |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1301 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1302 | if (ana_list & AN_REQ_INSPECT) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1303 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1304 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1305 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1306 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1307 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1308 | if (ana_list & AN_REQ_WAIT_HTTP) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1309 | if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP)) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1310 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1311 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1314 | if (ana_list & AN_REQ_HTTP_PROCESS_FE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1315 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe)) |
| 1316 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1317 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1320 | if (ana_list & AN_REQ_SWITCHING_RULES) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1321 | if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES)) |
| 1322 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1323 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1326 | if (ana_list & AN_REQ_HTTP_PROCESS_BE) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1327 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be)) |
| 1328 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1329 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1332 | if (ana_list & AN_REQ_HTTP_TARPIT) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1333 | if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT)) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 1334 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1335 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1336 | } |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 1337 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1338 | if (ana_list & AN_REQ_HTTP_INNER) { |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 1339 | if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER)) |
| 1340 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1341 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1344 | if (ana_list & AN_REQ_HTTP_BODY) { |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1345 | if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY)) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 1346 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1347 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY); |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 1348 | } |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1349 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1350 | if (ana_list & AN_REQ_PRST_RDP_COOKIE) { |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1351 | if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE)) |
| 1352 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1353 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE); |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 1354 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1355 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1356 | if (ana_list & AN_REQ_STICKING_RULES) { |
| 1357 | if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES)) |
| 1358 | break; |
| 1359 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES); |
| 1360 | } |
| 1361 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1362 | if (ana_list & AN_REQ_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1363 | if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY)) |
| 1364 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1365 | UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1366 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1367 | break; |
| 1368 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1369 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1370 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1371 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) { |
| 1372 | rqf_last = s->req->flags; |
| 1373 | goto resync_request; |
| 1374 | } |
| 1375 | } |
| 1376 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1377 | /* we'll monitor the request analysers while parsing the response, |
| 1378 | * because some response analysers may indirectly enable new request |
| 1379 | * analysers (eg: HTTP keep-alive). |
| 1380 | */ |
| 1381 | req_ana_back = s->req->analysers; |
| 1382 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1383 | resync_response: |
| 1384 | /* Analyse response */ |
| 1385 | |
| 1386 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 1387 | /* In inject mode, we wake up everytime something has |
| 1388 | * happened on the write side of the buffer. |
| 1389 | */ |
| 1390 | unsigned int flags = s->rep->flags; |
| 1391 | |
| 1392 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 1393 | !(s->rep->flags & BF_FULL)) { |
| 1394 | s->rep->hijacker(s, s->rep); |
| 1395 | } |
| 1396 | |
| 1397 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 1398 | rpf_last = s->rep->flags; |
| 1399 | goto resync_response; |
| 1400 | } |
| 1401 | } |
| 1402 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 1403 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 1404 | unsigned int flags = s->rep->flags; |
| 1405 | |
| 1406 | if (s->rep->prod->state >= SI_ST_EST) { |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1407 | int max_loops = global.tune.maxpollevents; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1408 | unsigned int ana_list; |
| 1409 | unsigned int ana_back; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1410 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1411 | /* it's up to the analysers to stop disable reading or |
| 1412 | * closing. Note: if an analyser disables any of these |
| 1413 | * bits, it is responsible for enabling them again when |
| 1414 | * it disables itself, so that other analysers are called |
| 1415 | * in similar conditions. |
| 1416 | */ |
| 1417 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1418 | buffer_auto_close(s->rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1419 | |
| 1420 | /* We will call all analysers for which a bit is set in |
| 1421 | * s->rep->analysers, following the bit order from LSB |
| 1422 | * to MSB. The analysers must remove themselves from |
| 1423 | * the list when not needed. Any analyser may return 0 |
| 1424 | * to break out of the loop, either because of missing |
| 1425 | * data to take a decision, or because it decides to |
| 1426 | * kill the session. We loop at least once through each |
| 1427 | * analyser, and we may loop again if other analysers |
| 1428 | * are added in the middle. |
| 1429 | */ |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1430 | |
| 1431 | ana_list = ana_back = s->rep->analysers; |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1432 | while (ana_list && max_loops--) { |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1433 | /* Warning! ensure that analysers are always placed in ascending order! */ |
| 1434 | |
| 1435 | if (ana_list & AN_RES_WAIT_HTTP) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1436 | if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP)) |
| 1437 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1438 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
Emeric Brun | 1d33b29 | 2010-01-04 15:47:17 +0100 | [diff] [blame] | 1441 | if (ana_list & AN_RES_STORE_RULES) { |
| 1442 | if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES)) |
| 1443 | break; |
| 1444 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES); |
| 1445 | } |
| 1446 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1447 | if (ana_list & AN_RES_HTTP_PROCESS_BE) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1448 | if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be)) |
| 1449 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1450 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 1451 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1452 | |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1453 | if (ana_list & AN_RES_HTTP_XFER_BODY) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1454 | if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY)) |
| 1455 | break; |
Willy Tarreau | 1e0bbaf | 2010-01-06 23:53:24 +0100 | [diff] [blame] | 1456 | UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1457 | } |
Willy Tarreau | e34070e | 2010-01-08 00:32:27 +0100 | [diff] [blame] | 1458 | break; |
| 1459 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 1463 | rpf_last = s->rep->flags; |
| 1464 | goto resync_response; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Willy Tarreau | 576507f | 2010-01-07 00:09:04 +0100 | [diff] [blame] | 1468 | /* maybe someone has added some request analysers, so we must check and loop */ |
| 1469 | if (s->req->analysers & ~req_ana_back) |
| 1470 | goto resync_request; |
| 1471 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1472 | /* FIXME: here we should call protocol handlers which rely on |
| 1473 | * both buffers. |
| 1474 | */ |
| 1475 | |
| 1476 | |
| 1477 | /* |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1478 | * Now we propagate unhandled errors to the session. Normally |
| 1479 | * we're just in a data phase here since it means we have not |
| 1480 | * seen any analyser who could set an error status. |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1481 | */ |
| 1482 | if (!(s->flags & SN_ERR_MASK)) { |
| 1483 | if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1484 | /* 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] | 1485 | s->req->analysers = 0; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1486 | if (s->req->flags & BF_READ_ERROR) { |
| 1487 | s->be->counters.cli_aborts++; |
| 1488 | if (s->srv) |
| 1489 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1490 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1491 | } |
| 1492 | else if (s->req->flags & BF_READ_TIMEOUT) { |
| 1493 | s->be->counters.cli_aborts++; |
| 1494 | if (s->srv) |
| 1495 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1496 | s->flags |= SN_ERR_CLITO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1497 | } |
| 1498 | else if (s->req->flags & BF_WRITE_ERROR) { |
| 1499 | s->be->counters.srv_aborts++; |
| 1500 | if (s->srv) |
| 1501 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1502 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1503 | } |
| 1504 | else { |
| 1505 | s->be->counters.srv_aborts++; |
| 1506 | if (s->srv) |
| 1507 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1508 | s->flags |= SN_ERR_SRVTO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1509 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1510 | sess_set_term_flags(s); |
| 1511 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1512 | else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 1513 | /* Report it if the server got an error or a read timeout expired */ |
| 1514 | s->rep->analysers = 0; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1515 | if (s->rep->flags & BF_READ_ERROR) { |
| 1516 | s->be->counters.srv_aborts++; |
| 1517 | if (s->srv) |
| 1518 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1519 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1520 | } |
| 1521 | else if (s->rep->flags & BF_READ_TIMEOUT) { |
| 1522 | s->be->counters.srv_aborts++; |
| 1523 | if (s->srv) |
| 1524 | s->srv->counters.srv_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1525 | s->flags |= SN_ERR_SRVTO; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1526 | } |
| 1527 | else if (s->rep->flags & BF_WRITE_ERROR) { |
| 1528 | s->be->counters.cli_aborts++; |
| 1529 | if (s->srv) |
| 1530 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1531 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 1532 | } |
| 1533 | else { |
| 1534 | s->be->counters.cli_aborts++; |
| 1535 | if (s->srv) |
| 1536 | s->srv->counters.cli_aborts++; |
| 1537 | s->flags |= SN_ERR_CLITO; |
| 1538 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1539 | sess_set_term_flags(s); |
| 1540 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1543 | /* |
| 1544 | * Here we take care of forwarding unhandled data. This also includes |
| 1545 | * connection establishments and shutdown requests. |
| 1546 | */ |
| 1547 | |
| 1548 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1549 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1550 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1551 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1552 | if (!s->req->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1553 | !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1554 | (s->req->prod->state >= SI_ST_EST) && |
| 1555 | (s->req->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1556 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1557 | * attached to it. If any data are left in, we'll permit them to |
| 1558 | * move. |
| 1559 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1560 | buffer_auto_read(s->req); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1561 | buffer_auto_connect(s->req); |
| 1562 | buffer_auto_close(s->req); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1563 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 1564 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1565 | /* If the producer is still connected, we'll enable data to flow |
| 1566 | * from the producer to the consumer (which might possibly not be |
| 1567 | * connected yet). |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1568 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1569 | if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1570 | buffer_forward(s->req, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1571 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1572 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1573 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 1574 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1575 | s->req->to_forward && |
| 1576 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1577 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1578 | (pipes_used < global.maxpipes) && |
| 1579 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 1580 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1581 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 1582 | s->req->flags |= BF_KERN_SPLICING; |
| 1583 | } |
| 1584 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1585 | /* reflect what the L7 analysers have seen last */ |
| 1586 | rqf_last = s->req->flags; |
| 1587 | |
| 1588 | /* |
| 1589 | * Now forward all shutdown requests between both sides of the buffer |
| 1590 | */ |
| 1591 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1592 | /* first, let's check if the request buffer needs to shutdown(write), which may |
| 1593 | * happen either because the input is closed or because we want to force a close |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1594 | * once the server has begun to respond. |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1595 | */ |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1596 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1597 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1598 | buffer_shutw_now(s->req); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1599 | |
| 1600 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1601 | 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] | 1602 | s->req->cons->shutw(s->req->cons); |
| 1603 | |
| 1604 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1605 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 1606 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1607 | buffer_shutr_now(s->req); |
| 1608 | |
| 1609 | /* shutdown(read) pending */ |
| 1610 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1611 | s->req->prod->shutr(s->req->prod); |
| 1612 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1613 | /* it's possible that an upper layer has requested a connection setup or abort. |
| 1614 | * There are 2 situations where we decide to establish a new connection : |
| 1615 | * - there are data scheduled for emission in the buffer |
| 1616 | * - the BF_AUTO_CONNECT flag is set (active connection) |
| 1617 | */ |
| 1618 | if (s->req->cons->state == SI_ST_INI) { |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 1619 | if (!(s->req->flags & BF_SHUTW)) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1620 | if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) { |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1621 | /* If we have an iohandler without a connect method, we immediately |
| 1622 | * switch to the connected state, otherwise we perform a connection |
| 1623 | * request. |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1624 | */ |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1625 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
Willy Tarreau | 070ceb6 | 2010-06-01 10:36:43 +0200 | [diff] [blame] | 1626 | s->req->cons->conn_retries = s->be->conn_retries; |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1627 | if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1628 | s->req->cons->state = SI_ST_EST; /* connection established */ |
Willy Tarreau | 85e7d00 | 2010-05-31 11:57:51 +0200 | [diff] [blame] | 1629 | s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 1630 | s->req->wex = TICK_ETERNITY; |
| 1631 | } |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1632 | } |
Willy Tarreau | 7320122 | 2009-08-16 18:27:24 +0200 | [diff] [blame] | 1633 | } |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1634 | else { |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1635 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1636 | buffer_shutw_now(s->req); /* fix buffer flags upon abort */ |
| 1637 | buffer_shutr_now(s->rep); |
| 1638 | } |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1639 | } |
| 1640 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1641 | |
| 1642 | /* we may have a pending connection request, or a connection waiting |
| 1643 | * for completion. |
| 1644 | */ |
| 1645 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 1646 | do { |
| 1647 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 1648 | * to give a chance to step 2 to perform a redirect if needed. |
| 1649 | */ |
| 1650 | if (s->si[1].state != SI_ST_REQ) |
| 1651 | sess_update_stream_int(s, &s->si[1]); |
| 1652 | if (s->si[1].state == SI_ST_REQ) |
| 1653 | sess_prepare_conn_req(s, &s->si[1]); |
| 1654 | |
| 1655 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1656 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1657 | perform_http_redirect(s, &s->si[1]); |
| 1658 | } while (s->si[1].state == SI_ST_ASS); |
| 1659 | } |
| 1660 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1661 | /* 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] | 1662 | 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] | 1663 | goto resync_stream_interface; |
| 1664 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1665 | /* otherwise wewant to check if we need to resync the req buffer or not */ |
| 1666 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1667 | goto resync_request; |
| 1668 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1669 | /* perform output updates to the response buffer */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1670 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1671 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1672 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1673 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1674 | if (!s->rep->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 1675 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1676 | (s->rep->prod->state >= SI_ST_EST) && |
| 1677 | (s->rep->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1678 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1679 | * attached to it. If any data are left in, we'll permit them to |
| 1680 | * move. |
| 1681 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 1682 | buffer_auto_read(s->rep); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1683 | buffer_auto_close(s->rep); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1684 | buffer_flush(s->rep); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1685 | if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1686 | buffer_forward(s->rep, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1687 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1689 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1690 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1691 | s->rep->to_forward && |
| 1692 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1693 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1694 | (pipes_used < global.maxpipes) && |
| 1695 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1696 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1697 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1698 | s->rep->flags |= BF_KERN_SPLICING; |
| 1699 | } |
| 1700 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1701 | /* reflect what the L7 analysers have seen last */ |
| 1702 | rpf_last = s->rep->flags; |
| 1703 | |
| 1704 | /* |
| 1705 | * Now forward all shutdown requests between both sides of the buffer |
| 1706 | */ |
| 1707 | |
| 1708 | /* |
| 1709 | * FIXME: this is probably where we should produce error responses. |
| 1710 | */ |
| 1711 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1712 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1713 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1714 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1715 | buffer_shutw_now(s->rep); |
| 1716 | |
| 1717 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1718 | 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] | 1719 | s->rep->cons->shutw(s->rep->cons); |
| 1720 | |
| 1721 | /* 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] | 1722 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1723 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1724 | buffer_shutr_now(s->rep); |
| 1725 | |
| 1726 | /* shutdown(read) pending */ |
| 1727 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1728 | s->rep->prod->shutr(s->rep->prod); |
| 1729 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1730 | 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] | 1731 | goto resync_stream_interface; |
| 1732 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1733 | if (s->req->flags != rqf_last) |
| 1734 | goto resync_request; |
| 1735 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1736 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1737 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1738 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1739 | /* we're interested in getting wakeups again */ |
| 1740 | s->req->prod->flags &= ~SI_FL_DONT_WAKE; |
| 1741 | s->req->cons->flags &= ~SI_FL_DONT_WAKE; |
| 1742 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1743 | /* This is needed only when debugging is enabled, to indicate |
| 1744 | * client-side or server-side close. Please note that in the unlikely |
| 1745 | * event where both sides would close at once, the sequence is reported |
| 1746 | * on the server side first. |
| 1747 | */ |
| 1748 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1749 | (!(global.mode & MODE_QUIET) || |
| 1750 | (global.mode & MODE_VERBOSE)))) { |
| 1751 | int len; |
| 1752 | |
| 1753 | if (s->si[1].state == SI_ST_CLO && |
| 1754 | s->si[1].prev_state == SI_ST_EST) { |
| 1755 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1756 | s->uniq_id, s->be->id, |
| 1757 | (unsigned short)s->si[0].fd, |
| 1758 | (unsigned short)s->si[1].fd); |
| 1759 | write(1, trash, len); |
| 1760 | } |
| 1761 | |
| 1762 | if (s->si[0].state == SI_ST_CLO && |
| 1763 | s->si[0].prev_state == SI_ST_EST) { |
| 1764 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1765 | s->uniq_id, s->be->id, |
| 1766 | (unsigned short)s->si[0].fd, |
| 1767 | (unsigned short)s->si[1].fd); |
| 1768 | write(1, trash, len); |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1773 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1774 | |
| 1775 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1776 | session_process_counters(s); |
| 1777 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1778 | if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1779 | s->rep->cons->update(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1781 | if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1782 | s->req->cons->update(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1783 | |
Willy Tarreau | a6eebb3 | 2010-06-04 11:40:20 +0200 | [diff] [blame] | 1784 | s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED); |
| 1785 | s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1786 | s->si[0].prev_state = s->si[0].state; |
| 1787 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1788 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1789 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* Trick: if a request is being waiting for the server to respond, |
| 1792 | * and if we know the server can timeout, we don't want the timeout |
| 1793 | * to expire on the client side first, but we're still interested |
| 1794 | * in passing data from the client to the server (eg: POST). Thus, |
| 1795 | * we can cancel the client's request timeout if the server's |
| 1796 | * request timeout is set and the server has not yet sent a response. |
| 1797 | */ |
| 1798 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1799 | if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1800 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1801 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1802 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1803 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1804 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1805 | /* Call the second stream interface's I/O handler if it's embedded. |
| 1806 | * Note that this one may wake the task up again. |
| 1807 | */ |
| 1808 | if (s->req->cons->iohandler) { |
| 1809 | s->req->cons->iohandler(s->req->cons); |
| 1810 | if (task_in_rq(t)) { |
| 1811 | /* If we woke up, we don't want to requeue the |
| 1812 | * task to the wait queue, but rather requeue |
| 1813 | * it into the runqueue ASAP. |
| 1814 | */ |
| 1815 | t->expire = TICK_ETERNITY; |
| 1816 | return t; |
| 1817 | } |
| 1818 | } |
| 1819 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1820 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1821 | tick_first(s->rep->rex, s->rep->wex)); |
| 1822 | if (s->req->analysers) |
| 1823 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1824 | |
| 1825 | if (s->si[0].exp) |
| 1826 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1827 | |
| 1828 | if (s->si[1].exp) |
| 1829 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1830 | |
| 1831 | #ifdef DEBUG_FULL |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 1832 | fprintf(stderr, |
| 1833 | "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u" |
| 1834 | " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n", |
| 1835 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, |
| 1836 | 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] | 1837 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1838 | |
| 1839 | #ifdef DEBUG_DEV |
| 1840 | /* 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] | 1841 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1842 | ABORT_NOW(); |
| 1843 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1844 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | s->fe->feconn--; |
| 1848 | if (s->flags & SN_BE_ASSIGNED) |
| 1849 | s->be->beconn--; |
| 1850 | actconn--; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 1851 | s->listener->nbconn--; |
| 1852 | if (s->listener->state == LI_FULL && |
| 1853 | s->listener->nbconn < s->listener->maxconn) { |
| 1854 | /* we should reactivate the listener */ |
| 1855 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1856 | s->listener->state = LI_READY; |
| 1857 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1858 | |
| 1859 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1860 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1861 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1862 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1863 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1864 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1865 | write(1, trash, len); |
| 1866 | } |
| 1867 | |
| 1868 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1869 | session_process_counters(s); |
| 1870 | |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1871 | if (s->txn.status) { |
| 1872 | int n; |
| 1873 | |
| 1874 | n = s->txn.status / 100; |
| 1875 | if (n < 1 || n > 5) |
| 1876 | n = 0; |
| 1877 | |
| 1878 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1879 | s->fe->counters.fe.http.rsp[n]++; |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1880 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1881 | if ((s->flags & SN_BE_ASSIGNED) && |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1882 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 1883 | s->be->counters.be.http.rsp[n]++; |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1884 | } |
| 1885 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1886 | /* let's do a final log if we need it */ |
| 1887 | if (s->logs.logwait && |
| 1888 | !(s->flags & SN_MONITOR) && |
| 1889 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1890 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1894 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1895 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1896 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1897 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1898 | } |
| 1899 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1900 | /* |
| 1901 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1902 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1903 | * current connection slot. This function also notifies any LB algo which might |
| 1904 | * expect to be informed about any change in the number of active sessions on a |
| 1905 | * server. |
| 1906 | */ |
| 1907 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1908 | { |
| 1909 | if (sess->srv_conn == newsrv) |
| 1910 | return; |
| 1911 | |
| 1912 | if (sess->srv_conn) { |
| 1913 | sess->srv_conn->served--; |
| 1914 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1915 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1916 | sess->srv_conn = NULL; |
| 1917 | } |
| 1918 | |
| 1919 | if (newsrv) { |
| 1920 | newsrv->served++; |
| 1921 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1922 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1923 | sess->srv_conn = newsrv; |
| 1924 | } |
| 1925 | } |
| 1926 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1927 | /* Set correct session termination flags in case no analyser has done it. It |
| 1928 | * also counts a failed request if the server state has not reached the request |
| 1929 | * stage. |
| 1930 | */ |
| 1931 | void sess_set_term_flags(struct session *s) |
| 1932 | { |
| 1933 | if (!(s->flags & SN_FINST_MASK)) { |
| 1934 | if (s->si[1].state < SI_ST_REQ) { |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1935 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1936 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1937 | if (s->listener->counters) |
| 1938 | s->listener->counters->failed_req++; |
| 1939 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1940 | s->flags |= SN_FINST_R; |
| 1941 | } |
| 1942 | else if (s->si[1].state == SI_ST_QUE) |
| 1943 | s->flags |= SN_FINST_Q; |
| 1944 | else if (s->si[1].state < SI_ST_EST) |
| 1945 | s->flags |= SN_FINST_C; |
Willy Tarreau | 033b2db | 2010-03-04 17:54:21 +0100 | [diff] [blame] | 1946 | else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST) |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1947 | s->flags |= SN_FINST_D; |
| 1948 | else |
| 1949 | s->flags |= SN_FINST_L; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 1954 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 1955 | * session termination flags so that the caller does not have to worry about |
| 1956 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 1957 | */ |
| 1958 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 1959 | { |
| 1960 | int err_type = si->err_type; |
| 1961 | int err = 0, fin = 0; |
| 1962 | |
| 1963 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 1964 | err = SN_ERR_CLICL; |
| 1965 | fin = SN_FINST_Q; |
| 1966 | } |
| 1967 | else if (err_type & SI_ET_CONN_ABRT) { |
| 1968 | err = SN_ERR_CLICL; |
| 1969 | fin = SN_FINST_C; |
| 1970 | } |
| 1971 | else if (err_type & SI_ET_QUEUE_TO) { |
| 1972 | err = SN_ERR_SRVTO; |
| 1973 | fin = SN_FINST_Q; |
| 1974 | } |
| 1975 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 1976 | err = SN_ERR_SRVCL; |
| 1977 | fin = SN_FINST_Q; |
| 1978 | } |
| 1979 | else if (err_type & SI_ET_CONN_TO) { |
| 1980 | err = SN_ERR_SRVTO; |
| 1981 | fin = SN_FINST_C; |
| 1982 | } |
| 1983 | else if (err_type & SI_ET_CONN_ERR) { |
| 1984 | err = SN_ERR_SRVCL; |
| 1985 | fin = SN_FINST_C; |
| 1986 | } |
| 1987 | else /* SI_ET_CONN_OTHER and others */ { |
| 1988 | err = SN_ERR_INTERNAL; |
| 1989 | fin = SN_FINST_C; |
| 1990 | } |
| 1991 | |
| 1992 | if (!(s->flags & SN_ERR_MASK)) |
| 1993 | s->flags |= err; |
| 1994 | if (!(s->flags & SN_FINST_MASK)) |
| 1995 | s->flags |= fin; |
| 1996 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1997 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1998 | /* |
| 1999 | * Local variables: |
| 2000 | * c-indent-level: 8 |
| 2001 | * c-basic-offset: 8 |
| 2002 | * End: |
| 2003 | */ |