blob: 3f08a6a3d78e1fc62ab379dd376f99de91cf5380 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreaue3ba5f02006-06-29 18:54:54 +020014
15#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020016#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010020#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020022#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020024#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010025#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020026#include <proto/dumpstats.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010027#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020028#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <proto/session.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010030#include <proto/pattern.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010031#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010032#include <proto/proto_http.h>
33#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020034#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010036#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010037#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010038#include <proto/stream_interface.h>
39#include <proto/stream_sock.h>
40#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020042struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010043struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
45/*
46 * frees the context associated to a session. It must have been removed first.
47 */
48void session_free(struct session *s)
49{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010050 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020051 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010052 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010053
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 if (s->pend_pos)
55 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010056
Willy Tarreau1e62de62008-11-11 20:20:02 +010057 if (s->srv) { /* there may be requests left pending in queue */
58 if (s->flags & SN_CURR_SESS) {
59 s->flags &= ~SN_CURR_SESS;
60 s->srv->cur_sess--;
61 }
Willy Tarreau922a8062008-12-04 09:33:58 +010062 if (may_dequeue_tasks(s->srv, s->be))
63 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010064 }
Willy Tarreau922a8062008-12-04 09:33:58 +010065
Willy Tarreau7c669d72008-06-20 15:04:11 +020066 if (unlikely(s->srv_conn)) {
67 /* the session still has a reserved slot on a server, but
68 * it should normally be only the same as the one above,
69 * so this should not happen in fact.
70 */
71 sess_change_server(s, NULL);
72 }
73
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 if (s->req->pipe)
75 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010076
Willy Tarreau3eba98a2009-01-25 13:56:13 +010077 if (s->rep->pipe)
78 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010079
Willy Tarreau48d63db2008-08-03 17:41:33 +020080 pool_free2(pool2_buffer, s->req);
81 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Willy Tarreau46023632010-01-07 22:51:47 +010083 http_end_txn(s);
84
Willy Tarreau92fb9832007-10-16 17:34:28 +020085 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020086 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +010087 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
88 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020089 }
Willy Tarreau0937bc42009-12-22 15:03:09 +010090
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010091 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +010092 /* we have to unlink all watchers. We must not relink them if
93 * this session was the last one in the list.
94 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010095 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +010096 LIST_INIT(&bref->users);
97 if (s->list.n != &sessions)
98 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010099 bref->ref = s->list.n;
100 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100101 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200102 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200103
104 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200105 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200106 pool_flush2(pool2_buffer);
107 pool_flush2(fe->hdr_idx_pool);
108 pool_flush2(pool2_requri);
109 pool_flush2(pool2_capture);
110 pool_flush2(pool2_session);
111 pool_flush2(fe->req_cap_pool);
112 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200113 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200114}
115
116
117/* perform minimal intializations, report 0 in case of error, 1 if OK. */
118int init_session()
119{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100120 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200121 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
122 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123}
124
Willy Tarreau30e71012007-11-26 20:15:35 +0100125void session_process_counters(struct session *s)
126{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100127 unsigned long long bytes;
128
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100130 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100131 s->logs.bytes_in = s->req->total;
132 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200133 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100134
Willy Tarreau30e71012007-11-26 20:15:35 +0100135 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200136 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100137
Willy Tarreau30e71012007-11-26 20:15:35 +0100138 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200139 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200140
141 if (s->listener->counters)
142 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100144 }
145
Willy Tarreau30e71012007-11-26 20:15:35 +0100146 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100147 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100148 s->logs.bytes_out = s->rep->total;
149 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200150 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100151
Willy Tarreau30e71012007-11-26 20:15:35 +0100152 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200153 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100154
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200156 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200157
158 if (s->listener->counters)
159 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100160 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100161 }
162}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100164/* This function is called with (si->state == SI_ST_CON) meaning that a
165 * connection was attempted and that the file descriptor is already allocated.
166 * We must check for establishment, error and abort. Possible output states
167 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
168 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
169 * otherwise 1.
170 */
171int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
172{
173 struct buffer *req = si->ob;
174 struct buffer *rep = si->ib;
175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100176 /* If we got an error, or if nothing happened and the connection timed
177 * out, we must give up. The CER state handler will take care of retry
178 * attempts and error reports.
179 */
180 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100181 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100182 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200183 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100184 fd_delete(si->fd);
185
186 if (si->err_type)
187 return 0;
188
189 si->err_loc = s->srv;
190 if (si->flags & SI_FL_ERR)
191 si->err_type = SI_ET_CONN_ERR;
192 else
193 si->err_type = SI_ET_CONN_TO;
194 return 0;
195 }
196
197 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200198 if (unlikely((rep->flags & BF_SHUTW) ||
199 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200200 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100201 s->be->options & PR_O_ABRT_CLOSE)))) {
202 /* give up */
203 si->shutw(si);
204 si->err_type |= SI_ET_CONN_ABRT;
205 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200206 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100207 if (s->srv_error)
208 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100209 return 1;
210 }
211
212 /* we need to wait a bit more if there was no activity either */
213 if (!(req->flags & BF_WRITE_ACTIVITY))
214 return 1;
215
216 /* OK, this means that a connection succeeded. The caller will be
217 * responsible for handling the transition from CON to EST.
218 */
219 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100220 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100221 si->state = SI_ST_EST;
222 si->err_type = SI_ET_NONE;
223 si->err_loc = NULL;
224 return 1;
225}
226
227/* This function is called with (si->state == SI_ST_CER) meaning that a
228 * previous connection attempt has failed and that the file descriptor
229 * has already been released. Possible causes include asynchronous error
230 * notification and time out. Possible output states are SI_ST_CLO when
231 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
232 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
233 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
234 * marked as in error state. It returns 0.
235 */
236int sess_update_st_cer(struct session *s, struct stream_interface *si)
237{
238 /* we probably have to release last session from the server */
239 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100240 health_adjust(s->srv, HANA_STATUS_L4_ERR);
241
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100242 if (s->flags & SN_CURR_SESS) {
243 s->flags &= ~SN_CURR_SESS;
244 s->srv->cur_sess--;
245 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100246 }
247
248 /* ensure that we have enough retries left */
249 s->conn_retries--;
250 if (s->conn_retries < 0) {
251 if (!si->err_type) {
252 si->err_type = SI_ET_CONN_ERR;
253 si->err_loc = s->srv;
254 }
255
256 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200257 s->srv->counters.failed_conns++;
258 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100259 if (may_dequeue_tasks(s->srv, s->be))
260 process_srv_queue(s->srv);
261
262 /* shutw is enough so stop a connecting socket */
263 si->shutw(si);
264 si->ob->flags |= BF_WRITE_ERROR;
265 si->ib->flags |= BF_READ_ERROR;
266
267 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100268 if (s->srv_error)
269 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100270 return 0;
271 }
272
273 /* If the "redispatch" option is set on the backend, we are allowed to
274 * retry on another server for the last retry. In order to achieve this,
275 * we must mark the session unassigned, and eventually clear the DIRECT
276 * bit to ignore any persistence cookie. We won't count a retry nor a
277 * redispatch yet, because this will depend on what server is selected.
278 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100279 if (s->srv && s->conn_retries == 0 &&
280 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100281 if (may_dequeue_tasks(s->srv, s->be))
282 process_srv_queue(s->srv);
283
284 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
285 s->prev_srv = s->srv;
286 si->state = SI_ST_REQ;
287 } else {
288 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200289 s->srv->counters.retries++;
290 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100291 si->state = SI_ST_ASS;
292 }
293
294 if (si->flags & SI_FL_ERR) {
295 /* The error was an asynchronous connection error, and we will
296 * likely have to retry connecting to the same server, most
297 * likely leading to the same result. To avoid this, we wait
298 * one second before retrying.
299 */
300
301 if (!si->err_type)
302 si->err_type = SI_ET_CONN_ERR;
303
304 si->state = SI_ST_TAR;
305 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
306 return 0;
307 }
308 return 0;
309}
310
311/*
312 * This function handles the transition between the SI_ST_CON state and the
313 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
314 * SI_ST_EST.
315 */
316void sess_establish(struct session *s, struct stream_interface *si)
317{
318 struct buffer *req = si->ob;
319 struct buffer *rep = si->ib;
320
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100321 if (s->srv)
322 health_adjust(s->srv, HANA_STATUS_L4_OK);
323
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100324 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100325 /* if the user wants to log as soon as possible, without counting
326 * bytes from the server, then this is the right moment. */
327 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
328 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100329 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100330 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100331 }
332 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100333 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
334 /* reset hdr_idx which was already initialized by the request.
335 * right now, the http parser does it.
336 * hdr_idx_init(&s->txn.hdr_idx);
337 */
338 }
339
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200340 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100341 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
342 req->wex = TICK_ETERNITY;
343}
344
345/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
346 * Other input states are simply ignored.
347 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
348 * Flags must have previously been updated for timeouts and other conditions.
349 */
350void sess_update_stream_int(struct session *s, struct stream_interface *si)
351{
352 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
353 now_ms, __FUNCTION__,
354 s,
355 s->req, s->rep,
356 s->req->rex, s->rep->wex,
357 s->req->flags, s->rep->flags,
358 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
359
360 if (si->state == SI_ST_ASS) {
361 /* Server assigned to connection request, we have to try to connect now */
362 int conn_err;
363
364 conn_err = connect_server(s);
365 if (conn_err == SN_ERR_NONE) {
366 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100367 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100368 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100369 return;
370 }
371
372 /* We have received a synchronous error. We might have to
373 * abort, retry immediately or redispatch.
374 */
375 if (conn_err == SN_ERR_INTERNAL) {
376 if (!si->err_type) {
377 si->err_type = SI_ET_CONN_OTHER;
378 si->err_loc = s->srv;
379 }
380
381 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100382 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100383 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200384 s->srv->counters.failed_conns++;
385 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100386
387 /* release other sessions waiting for this server */
388 if (may_dequeue_tasks(s->srv, s->be))
389 process_srv_queue(s->srv);
390
391 /* Failed and not retryable. */
392 si->shutr(si);
393 si->shutw(si);
394 si->ob->flags |= BF_WRITE_ERROR;
395
396 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
397
398 /* no session was ever accounted for this server */
399 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100400 if (s->srv_error)
401 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100402 return;
403 }
404
405 /* We are facing a retryable error, but we don't want to run a
406 * turn-around now, as the problem is likely a source port
407 * allocation problem, so we want to retry now.
408 */
409 si->state = SI_ST_CER;
410 si->flags &= ~SI_FL_ERR;
411 sess_update_st_cer(s, si);
412 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
413 return;
414 }
415 else if (si->state == SI_ST_QUE) {
416 /* connection request was queued, check for any update */
417 if (!s->pend_pos) {
418 /* The connection is not in the queue anymore. Either
419 * we have a server connection slot available and we
420 * go directly to the assigned state, or we need to
421 * load-balance first and go to the INI state.
422 */
423 si->exp = TICK_ETERNITY;
424 if (unlikely(!(s->flags & SN_ASSIGNED)))
425 si->state = SI_ST_REQ;
426 else {
427 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
428 si->state = SI_ST_ASS;
429 }
430 return;
431 }
432
433 /* Connection request still in queue... */
434 if (si->flags & SI_FL_EXP) {
435 /* ... and timeout expired */
436 si->exp = TICK_ETERNITY;
437 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
438 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200439 s->srv->counters.failed_conns++;
440 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100441 si->shutr(si);
442 si->shutw(si);
443 si->ob->flags |= BF_WRITE_TIMEOUT;
444 if (!si->err_type)
445 si->err_type = SI_ET_QUEUE_TO;
446 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100447 if (s->srv_error)
448 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100449 return;
450 }
451
452 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200453 if ((si->ob->flags & (BF_READ_ERROR)) ||
454 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200455 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100456 /* give up */
457 si->exp = TICK_ETERNITY;
458 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
459 si->shutr(si);
460 si->shutw(si);
461 si->err_type |= SI_ET_QUEUE_ABRT;
462 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100463 if (s->srv_error)
464 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100465 return;
466 }
467
468 /* Nothing changed */
469 return;
470 }
471 else if (si->state == SI_ST_TAR) {
472 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200473 if ((si->ob->flags & (BF_READ_ERROR)) ||
474 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200475 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100476 /* give up */
477 si->exp = TICK_ETERNITY;
478 si->shutr(si);
479 si->shutw(si);
480 si->err_type |= SI_ET_CONN_ABRT;
481 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100482 if (s->srv_error)
483 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100484 return;
485 }
486
487 if (!(si->flags & SI_FL_EXP))
488 return; /* still in turn-around */
489
490 si->exp = TICK_ETERNITY;
491
492 /* we keep trying on the same server as long as the session is
493 * marked "assigned".
494 * FIXME: Should we force a redispatch attempt when the server is down ?
495 */
496 if (s->flags & SN_ASSIGNED)
497 si->state = SI_ST_ASS;
498 else
499 si->state = SI_ST_REQ;
500 return;
501 }
502}
503
504/* This function initiates a server connection request on a stream interface
505 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
506 * indicating that a server has been assigned. It may also return SI_ST_QUE,
507 * or SI_ST_CLO upon error.
508 */
509static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
510 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
511 now_ms, __FUNCTION__,
512 s,
513 s->req, s->rep,
514 s->req->rex, s->rep->wex,
515 s->req->flags, s->rep->flags,
516 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
517
518 if (si->state != SI_ST_REQ)
519 return;
520
521 /* Try to assign a server */
522 if (srv_redispatch_connect(s) != 0) {
523 /* We did not get a server. Either we queued the
524 * connection request, or we encountered an error.
525 */
526 if (si->state == SI_ST_QUE)
527 return;
528
529 /* we did not get any server, let's check the cause */
530 si->shutr(si);
531 si->shutw(si);
532 si->ob->flags |= BF_WRITE_ERROR;
533 if (!si->err_type)
534 si->err_type = SI_ET_CONN_OTHER;
535 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100536 if (s->srv_error)
537 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538 return;
539 }
540
541 /* The server is assigned */
542 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
543 si->state = SI_ST_ASS;
544}
545
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200546/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100547 * if appropriate. The default_backend rule is also considered, then the
548 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200549 * It returns 1 if the processing can continue on next analysers, or zero if it
550 * either needs more data or wants to immediately abort the request.
551 */
552int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
553{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200554 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100555
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200556 req->analysers &= ~an_bit;
557 req->analyse_exp = TICK_ETERNITY;
558
559 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
560 now_ms, __FUNCTION__,
561 s,
562 req,
563 req->rex, req->wex,
564 req->flags,
565 req->l,
566 req->analysers);
567
568 /* now check whether we have some switching rules for this request */
569 if (!(s->flags & SN_BE_ASSIGNED)) {
570 struct switching_rule *rule;
571
572 list_for_each_entry(rule, &s->fe->switching_rules, list) {
573 int ret;
574
575 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
576 ret = acl_pass(ret);
577 if (rule->cond->pol == ACL_COND_UNLESS)
578 ret = !ret;
579
580 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200581 if (!session_set_backend(s, rule->be.backend))
582 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200583 break;
584 }
585 }
586
587 /* To ensure correct connection accounting on the backend, we
588 * have to assign one if it was not set (eg: a listen). This
589 * measure also takes care of correctly setting the default
590 * backend if any.
591 */
592 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200593 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
594 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200595 }
596
597 /* we don't want to run the HTTP filters again if the backend has not changed */
598 if (s->fe == s->be)
599 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
600
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200601 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +0100602 * persistence rule, and report that in the session.
603 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200604 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100605 int ret = 1;
606
607 if (prst_rule->cond) {
608 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
609 ret = acl_pass(ret);
610 if (prst_rule->cond->pol == ACL_COND_UNLESS)
611 ret = !ret;
612 }
613
614 if (ret) {
615 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200616 if (prst_rule->type == PERSIST_TYPE_FORCE) {
617 s->flags |= SN_FORCE_PRST;
618 } else {
619 s->flags |= SN_IGNORE_PRST;
620 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100621 break;
622 }
623 }
624
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200625 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200626
627 sw_failed:
628 /* immediately abort this request in case of allocation failure */
629 buffer_abort(s->req);
630 buffer_abort(s->rep);
631
632 if (!(s->flags & SN_ERR_MASK))
633 s->flags |= SN_ERR_RESOURCE;
634 if (!(s->flags & SN_FINST_MASK))
635 s->flags |= SN_FINST_R;
636
637 s->txn.status = 500;
638 s->req->analysers = 0;
639 s->req->analyse_exp = TICK_ETERNITY;
640 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200641}
642
Emeric Brun1d33b292010-01-04 15:47:17 +0100643/* This stream analyser works on a request. It applies all sticking rules on
644 * it then returns 1. The data must already be present in the buffer otherwise
645 * they won't match. It always returns 1.
646 */
647int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
648{
649 struct proxy *px = s->be;
650 struct sticking_rule *rule;
651
652 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
653 now_ms, __FUNCTION__,
654 s,
655 req,
656 req->rex, req->wex,
657 req->flags,
658 req->l,
659 req->analysers);
660
661 list_for_each_entry(rule, &px->sticking_rules, list) {
662 int ret = 1 ;
663 int i;
664
665 for (i = 0; i < s->store_count; i++) {
666 if (rule->table.t == s->store[i].table)
667 break;
668 }
669
670 if (i != s->store_count)
671 continue;
672
673 if (rule->cond) {
674 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
675 ret = acl_pass(ret);
676 if (rule->cond->pol == ACL_COND_UNLESS)
677 ret = !ret;
678 }
679
680 if (ret) {
681 struct stktable_key *key;
682
683 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
684 if (!key)
685 continue;
686
687 if (rule->flags & STK_IS_MATCH) {
688 struct stksess *ts;
689
690 if ((ts = stktable_lookup(rule->table.t, key)) != NULL) {
691 if (!(s->flags & SN_ASSIGNED)) {
692 struct eb32_node *node;
693
694 /* srv found in table */
695 node = eb32_lookup(&px->conf.used_server_id, ts->sid);
696 if (node) {
697 struct server *srv;
698
699 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100700 if ((srv->state & SRV_RUNNING) ||
701 (px->options & PR_O_PERSIST) ||
702 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100703 s->flags |= SN_DIRECT | SN_ASSIGNED;
704 s->srv = srv;
705 }
706 }
707 }
708 ts->expire = tick_add(now_ms, MS_TO_TICKS(rule->table.t->expire));
709 }
710 }
711 if (rule->flags & STK_IS_STORE) {
712 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
713 struct stksess *ts;
714
715 ts = stksess_new(rule->table.t, key);
716 if (ts) {
717 s->store[s->store_count].table = rule->table.t;
718 s->store[s->store_count++].ts = ts;
719 }
720 }
721 }
722 }
723 }
724
725 req->analysers &= ~an_bit;
726 req->analyse_exp = TICK_ETERNITY;
727 return 1;
728}
729
730/* This stream analyser works on a response. It applies all store rules on it
731 * then returns 1. The data must already be present in the buffer otherwise
732 * they won't match. It always returns 1.
733 */
734int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
735{
736 struct proxy *px = s->be;
737 struct sticking_rule *rule;
738 int i;
739
740 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
741 now_ms, __FUNCTION__,
742 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +0100743 rep,
744 rep->rex, rep->wex,
745 rep->flags,
746 rep->l,
747 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +0100748
749 list_for_each_entry(rule, &px->storersp_rules, list) {
750 int ret = 1 ;
751 int storereqidx = -1;
752
753 for (i = 0; i < s->store_count; i++) {
754 if (rule->table.t == s->store[i].table) {
755 if (!(s->store[i].flags))
756 storereqidx = i;
757 break;
758 }
759 }
760
761 if ((i != s->store_count) && (storereqidx == -1))
762 continue;
763
764 if (rule->cond) {
765 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
766 ret = acl_pass(ret);
767 if (rule->cond->pol == ACL_COND_UNLESS)
768 ret = !ret;
769 }
770
771 if (ret) {
772 struct stktable_key *key;
773
774 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
775 if (!key)
776 continue;
777
778 if (storereqidx != -1) {
779 stksess_key(s->store[storereqidx].table, s->store[storereqidx].ts, key);
780 s->store[storereqidx].flags = 1;
781 }
782 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
783 struct stksess *ts;
784
785 ts = stksess_new(rule->table.t, key);
786 if (ts) {
787 s->store[s->store_count].table = rule->table.t;
788 s->store[s->store_count].flags = 1;
789 s->store[s->store_count++].ts = ts;
790 }
791 }
792 }
793 }
794
795 /* process store request and store response */
796 for (i = 0; i < s->store_count; i++) {
797 if (stktable_store(s->store[i].table, s->store[i].ts, s->srv->puid) > 0) {
798 stksess_free(s->store[i].table, s->store[i].ts);
799 s->store[i].ts = NULL;
800 }
801 }
802
803 rep->analysers &= ~an_bit;
804 rep->analyse_exp = TICK_ETERNITY;
805 return 1;
806}
807
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100808/* This macro is very specific to the function below. See the comments in
809 * process_session() below to understand the logic and the tests.
810 */
811#define UPDATE_ANALYSERS(real, list, back, flag) { \
812 list = (((list) & ~(flag)) | ~(back)) & (real); \
813 back = real; \
814 if (!(list)) \
815 break; \
816 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
817 continue; \
818}
819
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100820/* Processes the client, server, request and response jobs of a session task,
821 * then puts it back to the wait queue in a clean state, or cleans up its
822 * resources if it must be deleted. Returns in <next> the date the task wants
823 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
824 * nothing too many times, the request and response buffers flags are monitored
825 * and each function is called only if at least another function has changed at
826 * least one flag it is interested in.
827 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100828struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829{
830 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +0100832 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833
834 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
835 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
836
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100837 /* this data may be no longer valid, clear it */
838 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
839
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200840 /* This flag must explicitly be set every time */
841 s->req->flags &= ~BF_READ_NOEXP;
842
843 /* Keep a copy of req/rep flags so that we can detect shutdowns */
844 rqf_last = s->req->flags;
845 rpf_last = s->rep->flags;
846
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200847 /* we don't want the stream interface functions to recursively wake us up */
848 if (s->req->prod->owner == t)
849 s->req->prod->flags |= SI_FL_DONT_WAKE;
850 if (s->req->cons->owner == t)
851 s->req->cons->flags |= SI_FL_DONT_WAKE;
852
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853 /* 1a: Check for low level timeouts if needed. We just set a flag on
854 * stream interfaces when their timeouts have expired.
855 */
856 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
857 stream_int_check_timeouts(&s->si[0]);
858 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200859
860 /* check buffer timeouts, and close the corresponding stream interfaces
861 * for future reads or writes. Note: this will also concern upper layers
862 * but we do not touch any other flag. We must be careful and correctly
863 * detect state changes when calling them.
864 */
865
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200867
Willy Tarreau14641402009-12-29 14:49:56 +0100868 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
869 s->req->cons->flags |= SI_FL_NOLINGER;
870 s->req->cons->shutw(s->req->cons);
871 }
872
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200873 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
874 s->req->prod->shutr(s->req->prod);
875
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100876 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877
Willy Tarreau14641402009-12-29 14:49:56 +0100878 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
879 s->rep->cons->flags |= SI_FL_NOLINGER;
880 s->rep->cons->shutw(s->rep->cons);
881 }
882
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200883 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
884 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200885 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100886
887 /* 1b: check for low-level errors reported at the stream interface.
888 * First we check if it's a retryable error (in which case we don't
889 * want to tell the buffer). Otherwise we report the error one level
890 * upper by setting flags into the buffers. Note that the side towards
891 * the client cannot have connect (hence retryable) errors. Also, the
892 * connection setup code must be able to deal with any type of abort.
893 */
894 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
895 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
896 s->si[0].shutr(&s->si[0]);
897 s->si[0].shutw(&s->si[0]);
898 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100899 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +0100900 s->be->counters.cli_aborts++;
901 if (s->srv)
902 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100903 if (!(s->flags & SN_ERR_MASK))
904 s->flags |= SN_ERR_CLICL;
905 if (!(s->flags & SN_FINST_MASK))
906 s->flags |= SN_FINST_D;
907 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 }
909 }
910
911 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
912 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
913 s->si[1].shutr(&s->si[1]);
914 s->si[1].shutw(&s->si[1]);
915 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200916 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200918 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100919 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +0100920 s->be->counters.srv_aborts++;
921 if (s->srv)
922 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100923 if (!(s->flags & SN_ERR_MASK))
924 s->flags |= SN_ERR_SRVCL;
925 if (!(s->flags & SN_FINST_MASK))
926 s->flags |= SN_FINST_D;
927 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100928 }
929 /* note: maybe we should process connection errors here ? */
930 }
931
932 if (s->si[1].state == SI_ST_CON) {
933 /* we were trying to establish a connection on the server side,
934 * maybe it succeeded, maybe it failed, maybe we timed out, ...
935 */
936 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
937 sess_update_st_cer(s, &s->si[1]);
938 else if (s->si[1].state == SI_ST_EST)
939 sess_establish(s, &s->si[1]);
940
941 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
942 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
943 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
944 */
945 }
946
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200947resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100948 /* Check for connection closure */
949
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100950 DPRINTF(stderr,
951 "[%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",
952 now_ms, __FUNCTION__, __LINE__,
953 t,
954 s, s->flags,
955 s->req, s->rep,
956 s->req->rex, s->rep->wex,
957 s->req->flags, s->rep->flags,
958 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
959 s->rep->cons->err_type, s->req->cons->err_type,
960 s->conn_retries);
961
962 /* nothing special to be done on client side */
963 if (unlikely(s->req->prod->state == SI_ST_DIS))
964 s->req->prod->state = SI_ST_CLO;
965
966 /* When a server-side connection is released, we have to count it and
967 * check for pending connections on this server.
968 */
969 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
970 s->req->cons->state = SI_ST_CLO;
971 if (s->srv) {
972 if (s->flags & SN_CURR_SESS) {
973 s->flags &= ~SN_CURR_SESS;
974 s->srv->cur_sess--;
975 }
976 sess_change_server(s, NULL);
977 if (may_dequeue_tasks(s->srv, s->be))
978 process_srv_queue(s->srv);
979 }
980 }
981
982 /*
983 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
984 * at this point.
985 */
986
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100987 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988 /* Analyse request */
989 if ((s->req->flags & BF_MASK_ANALYSER) ||
990 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
991 unsigned int flags = s->req->flags;
992
993 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +0100994 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100995 unsigned int ana_list;
996 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200997
Willy Tarreau90deb182010-01-07 00:20:41 +0100998 /* it's up to the analysers to stop new connections,
999 * disable reading or closing. Note: if an analyser
1000 * disables any of these bits, it is responsible for
1001 * enabling them again when it disables itself, so
1002 * that other analysers are called in similar conditions.
1003 */
1004 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001005 buffer_auto_connect(s->req);
1006 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001007
1008 /* We will call all analysers for which a bit is set in
1009 * s->req->analysers, following the bit order from LSB
1010 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001011 * the list when not needed. Any analyser may return 0
1012 * to break out of the loop, either because of missing
1013 * data to take a decision, or because it decides to
1014 * kill the session. We loop at least once through each
1015 * analyser, and we may loop again if other analysers
1016 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001017 *
1018 * We build a list of analysers to run. We evaluate all
1019 * of these analysers in the order of the lower bit to
1020 * the higher bit. This ordering is very important.
1021 * An analyser will often add/remove other analysers,
1022 * including itself. Any changes to itself have no effect
1023 * on the loop. If it removes any other analysers, we
1024 * want those analysers not to be called anymore during
1025 * this loop. If it adds an analyser that is located
1026 * after itself, we want it to be scheduled for being
1027 * processed during the loop. If it adds an analyser
1028 * which is located before it, we want it to switch to
1029 * it immediately, even if it has already been called
1030 * once but removed since.
1031 *
1032 * In order to achieve this, we compare the analyser
1033 * list after the call with a copy of it before the
1034 * call. The work list is fed with analyser bits that
1035 * appeared during the call. Then we compare previous
1036 * work list with the new one, and check the bits that
1037 * appeared. If the lowest of these bits is lower than
1038 * the current bit, it means we have enabled a previous
1039 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001040 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001041
1042 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001043 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001044 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001045
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001046 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001047 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001048 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001049 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001050 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001051
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001052 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001053 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001054 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001055 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001056 }
1057
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001058 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001059 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1060 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001061 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001062 }
1063
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001064 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001065 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1066 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001067 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001068 }
1069
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001070 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001071 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1072 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001073 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001074 }
1075
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001076 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001077 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001078 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001079 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001080 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001081
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001082 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001083 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1084 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001085 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001086 }
1087
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001088 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001089 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001090 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001091 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001092 }
Emeric Brun647caf12009-06-30 17:57:00 +02001093
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001094 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001095 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1096 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001097 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001098 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001099
Emeric Brun1d33b292010-01-04 15:47:17 +01001100 if (ana_list & AN_REQ_STICKING_RULES) {
1101 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1102 break;
1103 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1104 }
1105
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001106 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001107 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1108 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001109 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001110 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001111 break;
1112 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001113 }
Willy Tarreau84455332009-03-15 22:34:05 +01001114
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001115 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
1116 rqf_last = s->req->flags;
1117 goto resync_request;
1118 }
1119 }
1120
Willy Tarreau576507f2010-01-07 00:09:04 +01001121 /* we'll monitor the request analysers while parsing the response,
1122 * because some response analysers may indirectly enable new request
1123 * analysers (eg: HTTP keep-alive).
1124 */
1125 req_ana_back = s->req->analysers;
1126
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001127 resync_response:
1128 /* Analyse response */
1129
1130 if (unlikely(s->rep->flags & BF_HIJACK)) {
1131 /* In inject mode, we wake up everytime something has
1132 * happened on the write side of the buffer.
1133 */
1134 unsigned int flags = s->rep->flags;
1135
1136 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1137 !(s->rep->flags & BF_FULL)) {
1138 s->rep->hijacker(s, s->rep);
1139 }
1140
1141 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1142 rpf_last = s->rep->flags;
1143 goto resync_response;
1144 }
1145 }
1146 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
1147 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
1148 unsigned int flags = s->rep->flags;
1149
1150 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001151 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001152 unsigned int ana_list;
1153 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001154
Willy Tarreau90deb182010-01-07 00:20:41 +01001155 /* it's up to the analysers to stop disable reading or
1156 * closing. Note: if an analyser disables any of these
1157 * bits, it is responsible for enabling them again when
1158 * it disables itself, so that other analysers are called
1159 * in similar conditions.
1160 */
1161 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001162 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001163
1164 /* We will call all analysers for which a bit is set in
1165 * s->rep->analysers, following the bit order from LSB
1166 * to MSB. The analysers must remove themselves from
1167 * the list when not needed. Any analyser may return 0
1168 * to break out of the loop, either because of missing
1169 * data to take a decision, or because it decides to
1170 * kill the session. We loop at least once through each
1171 * analyser, and we may loop again if other analysers
1172 * are added in the middle.
1173 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001174
1175 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001176 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001177 /* Warning! ensure that analysers are always placed in ascending order! */
1178
1179 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001180 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1181 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001182 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001183 }
1184
Emeric Brun1d33b292010-01-04 15:47:17 +01001185 if (ana_list & AN_RES_STORE_RULES) {
1186 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1187 break;
1188 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1189 }
1190
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001191 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001192 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1193 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001194 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001195 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001196
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001197 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001198 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1199 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001200 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001201 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001202 break;
1203 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001204 }
1205
1206 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1207 rpf_last = s->rep->flags;
1208 goto resync_response;
1209 }
1210 }
1211
Willy Tarreau576507f2010-01-07 00:09:04 +01001212 /* maybe someone has added some request analysers, so we must check and loop */
1213 if (s->req->analysers & ~req_ana_back)
1214 goto resync_request;
1215
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001216 /* FIXME: here we should call protocol handlers which rely on
1217 * both buffers.
1218 */
1219
1220
1221 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001222 * Now we propagate unhandled errors to the session. Normally
1223 * we're just in a data phase here since it means we have not
1224 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001225 */
1226 if (!(s->flags & SN_ERR_MASK)) {
1227 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1228 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001229 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001230 if (s->req->flags & BF_READ_ERROR) {
1231 s->be->counters.cli_aborts++;
1232 if (s->srv)
1233 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001234 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001235 }
1236 else if (s->req->flags & BF_READ_TIMEOUT) {
1237 s->be->counters.cli_aborts++;
1238 if (s->srv)
1239 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001240 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001241 }
1242 else if (s->req->flags & BF_WRITE_ERROR) {
1243 s->be->counters.srv_aborts++;
1244 if (s->srv)
1245 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001246 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001247 }
1248 else {
1249 s->be->counters.srv_aborts++;
1250 if (s->srv)
1251 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001252 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001253 }
Willy Tarreau84455332009-03-15 22:34:05 +01001254 sess_set_term_flags(s);
1255 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001256 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1257 /* Report it if the server got an error or a read timeout expired */
1258 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001259 if (s->rep->flags & BF_READ_ERROR) {
1260 s->be->counters.srv_aborts++;
1261 if (s->srv)
1262 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001263 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001264 }
1265 else if (s->rep->flags & BF_READ_TIMEOUT) {
1266 s->be->counters.srv_aborts++;
1267 if (s->srv)
1268 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001269 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001270 }
1271 else if (s->rep->flags & BF_WRITE_ERROR) {
1272 s->be->counters.cli_aborts++;
1273 if (s->srv)
1274 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001275 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001276 }
1277 else {
1278 s->be->counters.cli_aborts++;
1279 if (s->srv)
1280 s->srv->counters.cli_aborts++;
1281 s->flags |= SN_ERR_CLITO;
1282 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001283 sess_set_term_flags(s);
1284 }
Willy Tarreau84455332009-03-15 22:34:05 +01001285 }
1286
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001287 /*
1288 * Here we take care of forwarding unhandled data. This also includes
1289 * connection establishments and shutdown requests.
1290 */
1291
1292
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001293 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001294 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001295 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001296 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001297 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001298 (s->req->prod->state >= SI_ST_EST) &&
1299 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001300 /* This buffer is freewheeling, there's no analyser nor hijacker
1301 * attached to it. If any data are left in, we'll permit them to
1302 * move.
1303 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001304 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001305 buffer_auto_connect(s->req);
1306 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001307 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001308
Willy Tarreau31971e52009-09-20 12:07:52 +02001309 /* If the producer is still connected, we'll enable data to flow
1310 * from the producer to the consumer (which might possibly not be
1311 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001312 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001313 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1314 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001315 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001316
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001317 /* check if it is wise to enable kernel splicing to forward request data */
1318 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1319 s->req->to_forward &&
1320 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001321 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001322 (pipes_used < global.maxpipes) &&
1323 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1324 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1325 (s->req->flags & BF_STREAMER_FAST)))) {
1326 s->req->flags |= BF_KERN_SPLICING;
1327 }
1328
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001329 /* reflect what the L7 analysers have seen last */
1330 rqf_last = s->req->flags;
1331
1332 /*
1333 * Now forward all shutdown requests between both sides of the buffer
1334 */
1335
Willy Tarreau520d95e2009-09-19 21:04:57 +02001336 /* first, let's check if the request buffer needs to shutdown(write), which may
1337 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001338 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001339 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001340 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001341 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001342 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001343
1344 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001345 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001346 s->req->cons->shutw(s->req->cons);
1347
1348 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001349 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1350 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001351 buffer_shutr_now(s->req);
1352
1353 /* shutdown(read) pending */
1354 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1355 s->req->prod->shutr(s->req->prod);
1356
Willy Tarreau520d95e2009-09-19 21:04:57 +02001357 /* it's possible that an upper layer has requested a connection setup or abort.
1358 * There are 2 situations where we decide to establish a new connection :
1359 * - there are data scheduled for emission in the buffer
1360 * - the BF_AUTO_CONNECT flag is set (active connection)
1361 */
1362 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001363 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001364 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001365 /* If we have a ->connect method, we need to perform a connection request,
1366 * otherwise we immediately switch to the connected state.
1367 */
1368 if (s->req->cons->connect)
1369 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1370 else
1371 s->req->cons->state = SI_ST_EST; /* connection established */
1372 }
Willy Tarreau73201222009-08-16 18:27:24 +02001373 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001374 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001375 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001376 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1377 buffer_shutr_now(s->rep);
1378 }
Willy Tarreau92795622009-03-06 12:51:23 +01001379 }
1380
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001381
1382 /* we may have a pending connection request, or a connection waiting
1383 * for completion.
1384 */
1385 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1386 do {
1387 /* nb: step 1 might switch from QUE to ASS, but we first want
1388 * to give a chance to step 2 to perform a redirect if needed.
1389 */
1390 if (s->si[1].state != SI_ST_REQ)
1391 sess_update_stream_int(s, &s->si[1]);
1392 if (s->si[1].state == SI_ST_REQ)
1393 sess_prepare_conn_req(s, &s->si[1]);
1394
1395 if (s->si[1].state == SI_ST_ASS && s->srv &&
1396 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1397 perform_http_redirect(s, &s->si[1]);
1398 } while (s->si[1].state == SI_ST_ASS);
1399 }
1400
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001401 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001402 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001403 goto resync_stream_interface;
1404
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001405 /* otherwise wewant to check if we need to resync the req buffer or not */
1406 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001407 goto resync_request;
1408
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001409 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001410
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001411 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001412 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001413 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001414 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001415 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001416 (s->rep->prod->state >= SI_ST_EST) &&
1417 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001418 /* This buffer is freewheeling, there's no analyser nor hijacker
1419 * attached to it. If any data are left in, we'll permit them to
1420 * move.
1421 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001422 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001423 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001424 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001425 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1426 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001427 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001428
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001429 /* check if it is wise to enable kernel splicing to forward response data */
1430 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1431 s->rep->to_forward &&
1432 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001433 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001434 (pipes_used < global.maxpipes) &&
1435 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1436 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1437 (s->rep->flags & BF_STREAMER_FAST)))) {
1438 s->rep->flags |= BF_KERN_SPLICING;
1439 }
1440
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001441 /* reflect what the L7 analysers have seen last */
1442 rpf_last = s->rep->flags;
1443
1444 /*
1445 * Now forward all shutdown requests between both sides of the buffer
1446 */
1447
1448 /*
1449 * FIXME: this is probably where we should produce error responses.
1450 */
1451
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001452 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001453 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1454 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001455 buffer_shutw_now(s->rep);
1456
1457 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001458 if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001459 s->rep->cons->shutw(s->rep->cons);
1460
1461 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001462 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1463 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001464 buffer_shutr_now(s->rep);
1465
1466 /* shutdown(read) pending */
1467 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1468 s->rep->prod->shutr(s->rep->prod);
1469
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001470 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001471 goto resync_stream_interface;
1472
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001473 if (s->req->flags != rqf_last)
1474 goto resync_request;
1475
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001476 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001477 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001478
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001479 /* we're interested in getting wakeups again */
1480 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1481 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1482
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001483 /* This is needed only when debugging is enabled, to indicate
1484 * client-side or server-side close. Please note that in the unlikely
1485 * event where both sides would close at once, the sequence is reported
1486 * on the server side first.
1487 */
1488 if (unlikely((global.mode & MODE_DEBUG) &&
1489 (!(global.mode & MODE_QUIET) ||
1490 (global.mode & MODE_VERBOSE)))) {
1491 int len;
1492
1493 if (s->si[1].state == SI_ST_CLO &&
1494 s->si[1].prev_state == SI_ST_EST) {
1495 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1496 s->uniq_id, s->be->id,
1497 (unsigned short)s->si[0].fd,
1498 (unsigned short)s->si[1].fd);
1499 write(1, trash, len);
1500 }
1501
1502 if (s->si[0].state == SI_ST_CLO &&
1503 s->si[0].prev_state == SI_ST_EST) {
1504 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1505 s->uniq_id, s->be->id,
1506 (unsigned short)s->si[0].fd,
1507 (unsigned short)s->si[1].fd);
1508 write(1, trash, len);
1509 }
1510 }
1511
1512 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1513 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1514
1515 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1516 session_process_counters(s);
1517
Willy Tarreau1accfc02009-09-05 20:57:35 +02001518 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001519 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001520
Willy Tarreau1accfc02009-09-05 20:57:35 +02001521 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001522 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001523
Willy Tarreauea388542009-06-21 21:45:58 +02001524 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1525 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001526 s->si[0].prev_state = s->si[0].state;
1527 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001528 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1529 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001530
1531 /* Trick: if a request is being waiting for the server to respond,
1532 * and if we know the server can timeout, we don't want the timeout
1533 * to expire on the client side first, but we're still interested
1534 * in passing data from the client to the server (eg: POST). Thus,
1535 * we can cancel the client's request timeout if the server's
1536 * request timeout is set and the server has not yet sent a response.
1537 */
1538
Willy Tarreau520d95e2009-09-19 21:04:57 +02001539 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001540 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1541 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001542 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001543 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001544
Willy Tarreau1accfc02009-09-05 20:57:35 +02001545 /* Call the second stream interface's I/O handler if it's embedded.
1546 * Note that this one may wake the task up again.
1547 */
1548 if (s->req->cons->iohandler) {
1549 s->req->cons->iohandler(s->req->cons);
1550 if (task_in_rq(t)) {
1551 /* If we woke up, we don't want to requeue the
1552 * task to the wait queue, but rather requeue
1553 * it into the runqueue ASAP.
1554 */
1555 t->expire = TICK_ETERNITY;
1556 return t;
1557 }
1558 }
1559
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001560 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1561 tick_first(s->rep->rex, s->rep->wex));
1562 if (s->req->analysers)
1563 t->expire = tick_first(t->expire, s->req->analyse_exp);
1564
1565 if (s->si[0].exp)
1566 t->expire = tick_first(t->expire, s->si[0].exp);
1567
1568 if (s->si[1].exp)
1569 t->expire = tick_first(t->expire, s->si[1].exp);
1570
1571#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001572 fprintf(stderr,
1573 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1574 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1575 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1576 s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001577#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001578
1579#ifdef DEBUG_DEV
1580 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001581 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001582 ABORT_NOW();
1583#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001584 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001585 }
1586
1587 s->fe->feconn--;
1588 if (s->flags & SN_BE_ASSIGNED)
1589 s->be->beconn--;
1590 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001591 s->listener->nbconn--;
1592 if (s->listener->state == LI_FULL &&
1593 s->listener->nbconn < s->listener->maxconn) {
1594 /* we should reactivate the listener */
1595 EV_FD_SET(s->listener->fd, DIR_RD);
1596 s->listener->state = LI_READY;
1597 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001598
1599 if (unlikely((global.mode & MODE_DEBUG) &&
1600 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1601 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001602 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001603 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001604 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605 write(1, trash, len);
1606 }
1607
1608 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1609 session_process_counters(s);
1610
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001611 if (s->txn.status) {
1612 int n;
1613
1614 n = s->txn.status / 100;
1615 if (n < 1 || n > 5)
1616 n = 0;
1617
1618 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001619 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001620
Willy Tarreau24657792010-02-26 10:30:28 +01001621 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001622 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001623 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001624 }
1625
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001626 /* let's do a final log if we need it */
1627 if (s->logs.logwait &&
1628 !(s->flags & SN_MONITOR) &&
1629 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001630 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001631 }
1632
1633 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001634 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001635 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001636 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001637 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001638}
1639
Willy Tarreau7c669d72008-06-20 15:04:11 +02001640/*
1641 * This function adjusts sess->srv_conn and maintains the previous and new
1642 * server's served session counts. Setting newsrv to NULL is enough to release
1643 * current connection slot. This function also notifies any LB algo which might
1644 * expect to be informed about any change in the number of active sessions on a
1645 * server.
1646 */
1647void sess_change_server(struct session *sess, struct server *newsrv)
1648{
1649 if (sess->srv_conn == newsrv)
1650 return;
1651
1652 if (sess->srv_conn) {
1653 sess->srv_conn->served--;
1654 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1655 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1656 sess->srv_conn = NULL;
1657 }
1658
1659 if (newsrv) {
1660 newsrv->served++;
1661 if (newsrv->proxy->lbprm.server_take_conn)
1662 newsrv->proxy->lbprm.server_take_conn(newsrv);
1663 sess->srv_conn = newsrv;
1664 }
1665}
1666
Willy Tarreau84455332009-03-15 22:34:05 +01001667/* Set correct session termination flags in case no analyser has done it. It
1668 * also counts a failed request if the server state has not reached the request
1669 * stage.
1670 */
1671void sess_set_term_flags(struct session *s)
1672{
1673 if (!(s->flags & SN_FINST_MASK)) {
1674 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001675
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001676 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001677 if (s->listener->counters)
1678 s->listener->counters->failed_req++;
1679
Willy Tarreau84455332009-03-15 22:34:05 +01001680 s->flags |= SN_FINST_R;
1681 }
1682 else if (s->si[1].state == SI_ST_QUE)
1683 s->flags |= SN_FINST_Q;
1684 else if (s->si[1].state < SI_ST_EST)
1685 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01001686 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01001687 s->flags |= SN_FINST_D;
1688 else
1689 s->flags |= SN_FINST_L;
1690 }
1691}
1692
1693/* Handle server-side errors for default protocols. It is called whenever a a
1694 * connection setup is aborted or a request is aborted in queue. It sets the
1695 * session termination flags so that the caller does not have to worry about
1696 * them. It's installed as ->srv_error for the server-side stream_interface.
1697 */
1698void default_srv_error(struct session *s, struct stream_interface *si)
1699{
1700 int err_type = si->err_type;
1701 int err = 0, fin = 0;
1702
1703 if (err_type & SI_ET_QUEUE_ABRT) {
1704 err = SN_ERR_CLICL;
1705 fin = SN_FINST_Q;
1706 }
1707 else if (err_type & SI_ET_CONN_ABRT) {
1708 err = SN_ERR_CLICL;
1709 fin = SN_FINST_C;
1710 }
1711 else if (err_type & SI_ET_QUEUE_TO) {
1712 err = SN_ERR_SRVTO;
1713 fin = SN_FINST_Q;
1714 }
1715 else if (err_type & SI_ET_QUEUE_ERR) {
1716 err = SN_ERR_SRVCL;
1717 fin = SN_FINST_Q;
1718 }
1719 else if (err_type & SI_ET_CONN_TO) {
1720 err = SN_ERR_SRVTO;
1721 fin = SN_FINST_C;
1722 }
1723 else if (err_type & SI_ET_CONN_ERR) {
1724 err = SN_ERR_SRVCL;
1725 fin = SN_FINST_C;
1726 }
1727 else /* SI_ET_CONN_OTHER and others */ {
1728 err = SN_ERR_INTERNAL;
1729 fin = SN_FINST_C;
1730 }
1731
1732 if (!(s->flags & SN_ERR_MASK))
1733 s->flags |= err;
1734 if (!(s->flags & SN_FINST_MASK))
1735 s->flags |= fin;
1736}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001737
Willy Tarreaubaaee002006-06-26 02:48:02 +02001738/*
1739 * Local variables:
1740 * c-indent-level: 8
1741 * c-basic-offset: 8
1742 * End:
1743 */