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