blob: 03285e25caf55508d6a7b0ff51dcf5b1da3bdc71 [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>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020025#include <proto/dumpstats.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010026#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020027#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010029#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010030#include <proto/proto_http.h>
31#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020032#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010034#include <proto/server.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/stream_interface.h>
36#include <proto/stream_sock.h>
37#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020039struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010040struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42/*
43 * frees the context associated to a session. It must have been removed first.
44 */
45void session_free(struct session *s)
46{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010047 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020048 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010049 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010050
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 if (s->pend_pos)
52 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010053
Willy Tarreau1e62de62008-11-11 20:20:02 +010054 if (s->srv) { /* there may be requests left pending in queue */
55 if (s->flags & SN_CURR_SESS) {
56 s->flags &= ~SN_CURR_SESS;
57 s->srv->cur_sess--;
58 }
Willy Tarreau922a8062008-12-04 09:33:58 +010059 if (may_dequeue_tasks(s->srv, s->be))
60 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010061 }
Willy Tarreau922a8062008-12-04 09:33:58 +010062
Willy Tarreau7c669d72008-06-20 15:04:11 +020063 if (unlikely(s->srv_conn)) {
64 /* the session still has a reserved slot on a server, but
65 * it should normally be only the same as the one above,
66 * so this should not happen in fact.
67 */
68 sess_change_server(s, NULL);
69 }
70
Willy Tarreau3eba98a2009-01-25 13:56:13 +010071 if (s->req->pipe)
72 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010073
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 if (s->rep->pipe)
75 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010076
Willy Tarreau48d63db2008-08-03 17:41:33 +020077 pool_free2(pool2_buffer, s->req);
78 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Cyril Bontébf47aeb2009-10-15 00:15:40 +020080 if (s->sessid)
81 pool_free2(apools.sessid, s->sessid);
82
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020084 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010085
Willy Tarreau92fb9832007-10-16 17:34:28 +020086 if (txn->rsp.cap != NULL) {
87 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020088 for (h = fe->rsp_cap; h; h = h->next)
89 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020090 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020092 if (txn->req.cap != NULL) {
93 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020094 for (h = fe->req_cap; h; h = h->next)
95 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020096 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020099 pool_free2(pool2_requri, txn->uri);
100 pool_free2(pool2_capture, txn->cli_cookie);
101 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100102
103 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100104 /* we have to unlink all watchers. We must not relink them if
105 * this session was the last one in the list.
106 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100107 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100108 LIST_INIT(&bref->users);
109 if (s->list.n != &sessions)
110 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100111 bref->ref = s->list.n;
112 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100113 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200114 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200115
116 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200117 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200118 pool_flush2(pool2_buffer);
119 pool_flush2(fe->hdr_idx_pool);
120 pool_flush2(pool2_requri);
121 pool_flush2(pool2_capture);
122 pool_flush2(pool2_session);
123 pool_flush2(fe->req_cap_pool);
124 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200125 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200126}
127
128
129/* perform minimal intializations, report 0 in case of error, 1 if OK. */
130int init_session()
131{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100132 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200133 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
134 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135}
136
Willy Tarreau30e71012007-11-26 20:15:35 +0100137void session_process_counters(struct session *s)
138{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 unsigned long long bytes;
140
Willy Tarreau30e71012007-11-26 20:15:35 +0100141 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 s->logs.bytes_in = s->req->total;
144 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200145 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200148 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149
Willy Tarreau30e71012007-11-26 20:15:35 +0100150 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200151 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200152
153 if (s->listener->counters)
154 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100156 }
157
Willy Tarreau30e71012007-11-26 20:15:35 +0100158 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100159 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100160 s->logs.bytes_out = s->rep->total;
161 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200162 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100163
Willy Tarreau30e71012007-11-26 20:15:35 +0100164 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200165 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100166
Willy Tarreau30e71012007-11-26 20:15:35 +0100167 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200168 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200169
170 if (s->listener->counters)
171 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100172 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100173 }
174}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100176/* This function is called with (si->state == SI_ST_CON) meaning that a
177 * connection was attempted and that the file descriptor is already allocated.
178 * We must check for establishment, error and abort. Possible output states
179 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
180 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
181 * otherwise 1.
182 */
183int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
184{
185 struct buffer *req = si->ob;
186 struct buffer *rep = si->ib;
187
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100188 /* If we got an error, or if nothing happened and the connection timed
189 * out, we must give up. The CER state handler will take care of retry
190 * attempts and error reports.
191 */
192 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100193 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100194 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200195 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100196 fd_delete(si->fd);
197
198 if (si->err_type)
199 return 0;
200
201 si->err_loc = s->srv;
202 if (si->flags & SI_FL_ERR)
203 si->err_type = SI_ET_CONN_ERR;
204 else
205 si->err_type = SI_ET_CONN_TO;
206 return 0;
207 }
208
209 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200210 if (unlikely((rep->flags & BF_SHUTW) ||
211 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200212 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100213 s->be->options & PR_O_ABRT_CLOSE)))) {
214 /* give up */
215 si->shutw(si);
216 si->err_type |= SI_ET_CONN_ABRT;
217 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200218 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100219 if (s->srv_error)
220 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100221 return 1;
222 }
223
224 /* we need to wait a bit more if there was no activity either */
225 if (!(req->flags & BF_WRITE_ACTIVITY))
226 return 1;
227
228 /* OK, this means that a connection succeeded. The caller will be
229 * responsible for handling the transition from CON to EST.
230 */
231 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100232 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100233 si->state = SI_ST_EST;
234 si->err_type = SI_ET_NONE;
235 si->err_loc = NULL;
236 return 1;
237}
238
239/* This function is called with (si->state == SI_ST_CER) meaning that a
240 * previous connection attempt has failed and that the file descriptor
241 * has already been released. Possible causes include asynchronous error
242 * notification and time out. Possible output states are SI_ST_CLO when
243 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
244 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
245 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
246 * marked as in error state. It returns 0.
247 */
248int sess_update_st_cer(struct session *s, struct stream_interface *si)
249{
250 /* we probably have to release last session from the server */
251 if (s->srv) {
252 if (s->flags & SN_CURR_SESS) {
253 s->flags &= ~SN_CURR_SESS;
254 s->srv->cur_sess--;
255 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100256 }
257
258 /* ensure that we have enough retries left */
259 s->conn_retries--;
260 if (s->conn_retries < 0) {
261 if (!si->err_type) {
262 si->err_type = SI_ET_CONN_ERR;
263 si->err_loc = s->srv;
264 }
265
266 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200267 s->srv->counters.failed_conns++;
268 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100269 if (may_dequeue_tasks(s->srv, s->be))
270 process_srv_queue(s->srv);
271
272 /* shutw is enough so stop a connecting socket */
273 si->shutw(si);
274 si->ob->flags |= BF_WRITE_ERROR;
275 si->ib->flags |= BF_READ_ERROR;
276
277 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100278 if (s->srv_error)
279 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100280 return 0;
281 }
282
283 /* If the "redispatch" option is set on the backend, we are allowed to
284 * retry on another server for the last retry. In order to achieve this,
285 * we must mark the session unassigned, and eventually clear the DIRECT
286 * bit to ignore any persistence cookie. We won't count a retry nor a
287 * redispatch yet, because this will depend on what server is selected.
288 */
289 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
290 if (may_dequeue_tasks(s->srv, s->be))
291 process_srv_queue(s->srv);
292
293 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
294 s->prev_srv = s->srv;
295 si->state = SI_ST_REQ;
296 } else {
297 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200298 s->srv->counters.retries++;
299 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100300 si->state = SI_ST_ASS;
301 }
302
303 if (si->flags & SI_FL_ERR) {
304 /* The error was an asynchronous connection error, and we will
305 * likely have to retry connecting to the same server, most
306 * likely leading to the same result. To avoid this, we wait
307 * one second before retrying.
308 */
309
310 if (!si->err_type)
311 si->err_type = SI_ET_CONN_ERR;
312
313 si->state = SI_ST_TAR;
314 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
315 return 0;
316 }
317 return 0;
318}
319
320/*
321 * This function handles the transition between the SI_ST_CON state and the
322 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
323 * SI_ST_EST.
324 */
325void sess_establish(struct session *s, struct stream_interface *si)
326{
327 struct buffer *req = si->ob;
328 struct buffer *rep = si->ib;
329
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100330 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200331 buffer_set_rlim(rep, rep->size); /* no rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100332
333 /* if the user wants to log as soon as possible, without counting
334 * bytes from the server, then this is the right moment. */
335 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
336 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100337 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100338 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100339 }
340 else {
Willy Tarreau27a674e2009-08-17 07:23:33 +0200341 buffer_set_rlim(rep, req->size - global.tune.maxrewrite); /* rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100342 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
343 /* reset hdr_idx which was already initialized by the request.
344 * right now, the http parser does it.
345 * hdr_idx_init(&s->txn.hdr_idx);
346 */
347 }
348
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200349 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100350 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
351 req->wex = TICK_ETERNITY;
352}
353
354/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
355 * Other input states are simply ignored.
356 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
357 * Flags must have previously been updated for timeouts and other conditions.
358 */
359void sess_update_stream_int(struct session *s, struct stream_interface *si)
360{
361 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",
362 now_ms, __FUNCTION__,
363 s,
364 s->req, s->rep,
365 s->req->rex, s->rep->wex,
366 s->req->flags, s->rep->flags,
367 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
368
369 if (si->state == SI_ST_ASS) {
370 /* Server assigned to connection request, we have to try to connect now */
371 int conn_err;
372
373 conn_err = connect_server(s);
374 if (conn_err == SN_ERR_NONE) {
375 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100376 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100377 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100378 return;
379 }
380
381 /* We have received a synchronous error. We might have to
382 * abort, retry immediately or redispatch.
383 */
384 if (conn_err == SN_ERR_INTERNAL) {
385 if (!si->err_type) {
386 si->err_type = SI_ET_CONN_OTHER;
387 si->err_loc = s->srv;
388 }
389
390 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100391 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100392 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200393 s->srv->counters.failed_conns++;
394 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100395
396 /* release other sessions waiting for this server */
397 if (may_dequeue_tasks(s->srv, s->be))
398 process_srv_queue(s->srv);
399
400 /* Failed and not retryable. */
401 si->shutr(si);
402 si->shutw(si);
403 si->ob->flags |= BF_WRITE_ERROR;
404
405 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
406
407 /* no session was ever accounted for this server */
408 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100409 if (s->srv_error)
410 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100411 return;
412 }
413
414 /* We are facing a retryable error, but we don't want to run a
415 * turn-around now, as the problem is likely a source port
416 * allocation problem, so we want to retry now.
417 */
418 si->state = SI_ST_CER;
419 si->flags &= ~SI_FL_ERR;
420 sess_update_st_cer(s, si);
421 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
422 return;
423 }
424 else if (si->state == SI_ST_QUE) {
425 /* connection request was queued, check for any update */
426 if (!s->pend_pos) {
427 /* The connection is not in the queue anymore. Either
428 * we have a server connection slot available and we
429 * go directly to the assigned state, or we need to
430 * load-balance first and go to the INI state.
431 */
432 si->exp = TICK_ETERNITY;
433 if (unlikely(!(s->flags & SN_ASSIGNED)))
434 si->state = SI_ST_REQ;
435 else {
436 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
437 si->state = SI_ST_ASS;
438 }
439 return;
440 }
441
442 /* Connection request still in queue... */
443 if (si->flags & SI_FL_EXP) {
444 /* ... and timeout expired */
445 si->exp = TICK_ETERNITY;
446 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
447 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200448 s->srv->counters.failed_conns++;
449 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100450 si->shutr(si);
451 si->shutw(si);
452 si->ob->flags |= BF_WRITE_TIMEOUT;
453 if (!si->err_type)
454 si->err_type = SI_ET_QUEUE_TO;
455 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100456 if (s->srv_error)
457 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100458 return;
459 }
460
461 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200462 if ((si->ob->flags & (BF_READ_ERROR)) ||
463 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200464 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100465 /* give up */
466 si->exp = TICK_ETERNITY;
467 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
468 si->shutr(si);
469 si->shutw(si);
470 si->err_type |= SI_ET_QUEUE_ABRT;
471 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100472 if (s->srv_error)
473 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100474 return;
475 }
476
477 /* Nothing changed */
478 return;
479 }
480 else if (si->state == SI_ST_TAR) {
481 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200482 if ((si->ob->flags & (BF_READ_ERROR)) ||
483 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200484 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100485 /* give up */
486 si->exp = TICK_ETERNITY;
487 si->shutr(si);
488 si->shutw(si);
489 si->err_type |= SI_ET_CONN_ABRT;
490 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100491 if (s->srv_error)
492 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100493 return;
494 }
495
496 if (!(si->flags & SI_FL_EXP))
497 return; /* still in turn-around */
498
499 si->exp = TICK_ETERNITY;
500
501 /* we keep trying on the same server as long as the session is
502 * marked "assigned".
503 * FIXME: Should we force a redispatch attempt when the server is down ?
504 */
505 if (s->flags & SN_ASSIGNED)
506 si->state = SI_ST_ASS;
507 else
508 si->state = SI_ST_REQ;
509 return;
510 }
511}
512
513/* This function initiates a server connection request on a stream interface
514 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
515 * indicating that a server has been assigned. It may also return SI_ST_QUE,
516 * or SI_ST_CLO upon error.
517 */
518static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
519 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",
520 now_ms, __FUNCTION__,
521 s,
522 s->req, s->rep,
523 s->req->rex, s->rep->wex,
524 s->req->flags, s->rep->flags,
525 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
526
527 if (si->state != SI_ST_REQ)
528 return;
529
530 /* Try to assign a server */
531 if (srv_redispatch_connect(s) != 0) {
532 /* We did not get a server. Either we queued the
533 * connection request, or we encountered an error.
534 */
535 if (si->state == SI_ST_QUE)
536 return;
537
538 /* we did not get any server, let's check the cause */
539 si->shutr(si);
540 si->shutw(si);
541 si->ob->flags |= BF_WRITE_ERROR;
542 if (!si->err_type)
543 si->err_type = SI_ET_CONN_OTHER;
544 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100545 if (s->srv_error)
546 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 return;
548 }
549
550 /* The server is assigned */
551 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
552 si->state = SI_ST_ASS;
553}
554
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200555/* This stream analyser checks the switching rules and changes the backend
556 * if appropriate. The default_backend rule is also considered.
557 * It returns 1 if the processing can continue on next analysers, or zero if it
558 * either needs more data or wants to immediately abort the request.
559 */
560int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
561{
562 req->analysers &= ~an_bit;
563 req->analyse_exp = TICK_ETERNITY;
564
565 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
566 now_ms, __FUNCTION__,
567 s,
568 req,
569 req->rex, req->wex,
570 req->flags,
571 req->l,
572 req->analysers);
573
574 /* now check whether we have some switching rules for this request */
575 if (!(s->flags & SN_BE_ASSIGNED)) {
576 struct switching_rule *rule;
577
578 list_for_each_entry(rule, &s->fe->switching_rules, list) {
579 int ret;
580
581 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
582 ret = acl_pass(ret);
583 if (rule->cond->pol == ACL_COND_UNLESS)
584 ret = !ret;
585
586 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200587 if (!session_set_backend(s, rule->be.backend))
588 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200589 break;
590 }
591 }
592
593 /* To ensure correct connection accounting on the backend, we
594 * have to assign one if it was not set (eg: a listen). This
595 * measure also takes care of correctly setting the default
596 * backend if any.
597 */
598 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200599 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
600 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200601 }
602
603 /* we don't want to run the HTTP filters again if the backend has not changed */
604 if (s->fe == s->be)
605 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
606
607 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200608
609 sw_failed:
610 /* immediately abort this request in case of allocation failure */
611 buffer_abort(s->req);
612 buffer_abort(s->rep);
613
614 if (!(s->flags & SN_ERR_MASK))
615 s->flags |= SN_ERR_RESOURCE;
616 if (!(s->flags & SN_FINST_MASK))
617 s->flags |= SN_FINST_R;
618
619 s->txn.status = 500;
620 s->req->analysers = 0;
621 s->req->analyse_exp = TICK_ETERNITY;
622 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200623}
624
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625/* Processes the client, server, request and response jobs of a session task,
626 * then puts it back to the wait queue in a clean state, or cleans up its
627 * resources if it must be deleted. Returns in <next> the date the task wants
628 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
629 * nothing too many times, the request and response buffers flags are monitored
630 * and each function is called only if at least another function has changed at
631 * least one flag it is interested in.
632 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100633struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634{
635 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 unsigned int rqf_last, rpf_last;
637
638 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
639 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
640
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200641 /* This flag must explicitly be set every time */
642 s->req->flags &= ~BF_READ_NOEXP;
643
644 /* Keep a copy of req/rep flags so that we can detect shutdowns */
645 rqf_last = s->req->flags;
646 rpf_last = s->rep->flags;
647
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200648 /* we don't want the stream interface functions to recursively wake us up */
649 if (s->req->prod->owner == t)
650 s->req->prod->flags |= SI_FL_DONT_WAKE;
651 if (s->req->cons->owner == t)
652 s->req->cons->flags |= SI_FL_DONT_WAKE;
653
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 /* 1a: Check for low level timeouts if needed. We just set a flag on
655 * stream interfaces when their timeouts have expired.
656 */
657 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
658 stream_int_check_timeouts(&s->si[0]);
659 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200660
661 /* check buffer timeouts, and close the corresponding stream interfaces
662 * for future reads or writes. Note: this will also concern upper layers
663 * but we do not touch any other flag. We must be careful and correctly
664 * detect state changes when calling them.
665 */
666
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200668
669 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
670 s->req->prod->shutr(s->req->prod);
671
672 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
673 s->req->cons->shutw(s->req->cons);
674
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200677 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
678 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100679
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200680 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
681 s->rep->cons->shutw(s->rep->cons);
682 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683
684 /* 1b: check for low-level errors reported at the stream interface.
685 * First we check if it's a retryable error (in which case we don't
686 * want to tell the buffer). Otherwise we report the error one level
687 * upper by setting flags into the buffers. Note that the side towards
688 * the client cannot have connect (hence retryable) errors. Also, the
689 * connection setup code must be able to deal with any type of abort.
690 */
691 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
692 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
693 s->si[0].shutr(&s->si[0]);
694 s->si[0].shutw(&s->si[0]);
695 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100696 if (!(s->req->analysers) && !(s->rep->analysers)) {
697 if (!(s->flags & SN_ERR_MASK))
698 s->flags |= SN_ERR_CLICL;
699 if (!(s->flags & SN_FINST_MASK))
700 s->flags |= SN_FINST_D;
701 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 }
703 }
704
705 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
706 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
707 s->si[1].shutr(&s->si[1]);
708 s->si[1].shutw(&s->si[1]);
709 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200710 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200712 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100713 if (!(s->req->analysers) && !(s->rep->analysers)) {
714 if (!(s->flags & SN_ERR_MASK))
715 s->flags |= SN_ERR_SRVCL;
716 if (!(s->flags & SN_FINST_MASK))
717 s->flags |= SN_FINST_D;
718 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719 }
720 /* note: maybe we should process connection errors here ? */
721 }
722
723 if (s->si[1].state == SI_ST_CON) {
724 /* we were trying to establish a connection on the server side,
725 * maybe it succeeded, maybe it failed, maybe we timed out, ...
726 */
727 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
728 sess_update_st_cer(s, &s->si[1]);
729 else if (s->si[1].state == SI_ST_EST)
730 sess_establish(s, &s->si[1]);
731
732 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
733 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
734 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
735 */
736 }
737
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200738resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 /* Check for connection closure */
740
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 DPRINTF(stderr,
742 "[%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",
743 now_ms, __FUNCTION__, __LINE__,
744 t,
745 s, s->flags,
746 s->req, s->rep,
747 s->req->rex, s->rep->wex,
748 s->req->flags, s->rep->flags,
749 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
750 s->rep->cons->err_type, s->req->cons->err_type,
751 s->conn_retries);
752
753 /* nothing special to be done on client side */
754 if (unlikely(s->req->prod->state == SI_ST_DIS))
755 s->req->prod->state = SI_ST_CLO;
756
757 /* When a server-side connection is released, we have to count it and
758 * check for pending connections on this server.
759 */
760 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
761 s->req->cons->state = SI_ST_CLO;
762 if (s->srv) {
763 if (s->flags & SN_CURR_SESS) {
764 s->flags &= ~SN_CURR_SESS;
765 s->srv->cur_sess--;
766 }
767 sess_change_server(s, NULL);
768 if (may_dequeue_tasks(s->srv, s->be))
769 process_srv_queue(s->srv);
770 }
771 }
772
773 /*
774 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
775 * at this point.
776 */
777
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100778 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100779 /* Analyse request */
780 if ((s->req->flags & BF_MASK_ANALYSER) ||
781 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
782 unsigned int flags = s->req->flags;
783
784 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200785 unsigned int last_ana = 0;
786
Willy Tarreau520d95e2009-09-19 21:04:57 +0200787 /* it's up to the analysers to stop new connections */
788 buffer_auto_connect(s->req);
789 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100790
791 /* We will call all analysers for which a bit is set in
792 * s->req->analysers, following the bit order from LSB
793 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200794 * the list when not needed. Any analyser may return 0
795 * to break out of the loop, either because of missing
796 * data to take a decision, or because it decides to
797 * kill the session. We loop at least once through each
798 * analyser, and we may loop again if other analysers
799 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100800 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200801 while (s->req->analysers & ~last_ana) {
802 last_ana = s->req->analysers;
803
804 if (s->req->analysers & AN_REQ_INSPECT) {
805 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200806 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100807 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200808 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100809
Willy Tarreaud787e662009-07-07 10:14:51 +0200810 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
811 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200812 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200813 break;
814 }
815
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200816 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
817 last_ana |= AN_REQ_HTTP_PROCESS_FE;
818 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
819 break;
820 }
821
822 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
823 last_ana |= AN_REQ_SWITCHING_RULES;
824 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
825 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200826 /* FIXME: we mait switch from TCP to HTTP and want to
827 * immediately loop back to the top. This is a dirty way
828 * of doing it, and we should find a cleaner method relying
829 * on a circular list of function pointers.
830 */
831 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
832 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200833 }
834
835 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
836 last_ana |= AN_REQ_HTTP_PROCESS_BE;
837 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
838 break;
839 }
840
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200841 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
842 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200843 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100844 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200845 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100846
Willy Tarreauc465fd72009-08-31 00:17:18 +0200847 if (s->req->analysers & AN_REQ_HTTP_INNER) {
848 last_ana |= AN_REQ_HTTP_INNER;
849 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
850 break;
851 }
852
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200853 if (s->req->analysers & AN_REQ_HTTP_BODY) {
854 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200855 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100856 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200857 }
Emeric Brun647caf12009-06-30 17:57:00 +0200858
859 if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) {
860 last_ana |= AN_REQ_PRST_RDP_COOKIE;
861 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
862 break;
863 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100864 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100865 }
Willy Tarreau84455332009-03-15 22:34:05 +0100866
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200867 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
868 rqf_last = s->req->flags;
869 goto resync_request;
870 }
871 }
872
873 resync_response:
874 /* Analyse response */
875
876 if (unlikely(s->rep->flags & BF_HIJACK)) {
877 /* In inject mode, we wake up everytime something has
878 * happened on the write side of the buffer.
879 */
880 unsigned int flags = s->rep->flags;
881
882 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
883 !(s->rep->flags & BF_FULL)) {
884 s->rep->hijacker(s, s->rep);
885 }
886
887 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
888 rpf_last = s->rep->flags;
889 goto resync_response;
890 }
891 }
892 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
893 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
894 unsigned int flags = s->rep->flags;
895
896 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200897 unsigned int last_ana = 0;
898
Willy Tarreau520d95e2009-09-19 21:04:57 +0200899 /* it's up to the analysers to reset auto_close */
900 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200901
902 /* We will call all analysers for which a bit is set in
903 * s->rep->analysers, following the bit order from LSB
904 * to MSB. The analysers must remove themselves from
905 * the list when not needed. Any analyser may return 0
906 * to break out of the loop, either because of missing
907 * data to take a decision, or because it decides to
908 * kill the session. We loop at least once through each
909 * analyser, and we may loop again if other analysers
910 * are added in the middle.
911 */
912 while (s->rep->analysers & ~last_ana) {
913 last_ana = s->rep->analysers;
914
915 if (s->rep->analysers & AN_RES_WAIT_HTTP) {
916 last_ana |= AN_RES_WAIT_HTTP;
917 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
918 break;
919 }
920
921 if (s->rep->analysers & AN_RES_HTTP_PROCESS_BE) {
922 last_ana |= AN_RES_HTTP_PROCESS_BE;
923 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
924 break;
925 /* FIXME: we may wait for a second response in case of a status 1xx
926 * and want to immediately loop back to the top. This is a dirty way
927 * of doing it, and we should find a cleaner method relying on a
928 * circular list of function pointers.
929 */
930 if ((s->rep->analysers & ~last_ana) & AN_RES_WAIT_HTTP)
931 continue;
932 }
933 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200934 }
935
936 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
937 rpf_last = s->rep->flags;
938 goto resync_response;
939 }
940 }
941
942 /* FIXME: here we should call protocol handlers which rely on
943 * both buffers.
944 */
945
946
947 /*
948 * Now we propagate unhandled errors to the session
949 */
950 if (!(s->flags & SN_ERR_MASK)) {
951 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
952 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100953 s->req->analysers = 0;
954 if (s->req->flags & BF_READ_ERROR)
955 s->flags |= SN_ERR_CLICL;
956 else if (s->req->flags & BF_READ_TIMEOUT)
957 s->flags |= SN_ERR_CLITO;
958 else if (s->req->flags & BF_WRITE_ERROR)
959 s->flags |= SN_ERR_SRVCL;
960 else
961 s->flags |= SN_ERR_SRVTO;
962 sess_set_term_flags(s);
963 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200964 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
965 /* Report it if the server got an error or a read timeout expired */
966 s->rep->analysers = 0;
967 if (s->rep->flags & BF_READ_ERROR)
968 s->flags |= SN_ERR_SRVCL;
969 else if (s->rep->flags & BF_READ_TIMEOUT)
970 s->flags |= SN_ERR_SRVTO;
971 else if (s->rep->flags & BF_WRITE_ERROR)
972 s->flags |= SN_ERR_CLICL;
973 else
Willy Tarreau84455332009-03-15 22:34:05 +0100974 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200975 sess_set_term_flags(s);
976 }
Willy Tarreau84455332009-03-15 22:34:05 +0100977 }
978
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200979 /*
980 * Here we take care of forwarding unhandled data. This also includes
981 * connection establishments and shutdown requests.
982 */
983
984
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100985 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +0200986 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100987 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100988 if (!s->req->analysers &&
989 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +0200990 (s->req->prod->state >= SI_ST_EST) &&
991 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100992 /* This buffer is freewheeling, there's no analyser nor hijacker
993 * attached to it. If any data are left in, we'll permit them to
994 * move.
995 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200996 buffer_auto_connect(s->req);
997 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100998 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100999
Willy Tarreau31971e52009-09-20 12:07:52 +02001000 /* If the producer is still connected, we'll enable data to flow
1001 * from the producer to the consumer (which might possibly not be
1002 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001003 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001004 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1005 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001006 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001007
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001008 /* check if it is wise to enable kernel splicing to forward request data */
1009 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1010 s->req->to_forward &&
1011 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001012 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001013 (pipes_used < global.maxpipes) &&
1014 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1015 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1016 (s->req->flags & BF_STREAMER_FAST)))) {
1017 s->req->flags |= BF_KERN_SPLICING;
1018 }
1019
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001020 /* reflect what the L7 analysers have seen last */
1021 rqf_last = s->req->flags;
1022
1023 /*
1024 * Now forward all shutdown requests between both sides of the buffer
1025 */
1026
Willy Tarreau520d95e2009-09-19 21:04:57 +02001027 /* first, let's check if the request buffer needs to shutdown(write), which may
1028 * happen either because the input is closed or because we want to force a close
1029 * once the server has begun to respond.
1030 */
1031 if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE)) == BF_AUTO_CLOSE) {
1032 if (unlikely((s->req->flags & BF_SHUTR) ||
1033 ((s->req->cons->state == SI_ST_EST) &&
1034 (s->be->options & PR_O_FORCE_CLO) &&
1035 (s->rep->flags & BF_READ_ACTIVITY))))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001036 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037 }
1038
1039 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001040 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 +01001041 s->req->cons->shutw(s->req->cons);
1042
1043 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001044 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1045 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001046 buffer_shutr_now(s->req);
1047
1048 /* shutdown(read) pending */
1049 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1050 s->req->prod->shutr(s->req->prod);
1051
Willy Tarreau520d95e2009-09-19 21:04:57 +02001052 /* it's possible that an upper layer has requested a connection setup or abort.
1053 * There are 2 situations where we decide to establish a new connection :
1054 * - there are data scheduled for emission in the buffer
1055 * - the BF_AUTO_CONNECT flag is set (active connection)
1056 */
1057 if (s->req->cons->state == SI_ST_INI) {
1058 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001059 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001060 /* If we have a ->connect method, we need to perform a connection request,
1061 * otherwise we immediately switch to the connected state.
1062 */
1063 if (s->req->cons->connect)
1064 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1065 else
1066 s->req->cons->state = SI_ST_EST; /* connection established */
1067 }
Willy Tarreau73201222009-08-16 18:27:24 +02001068 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001069 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001070 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001071 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1072 buffer_shutr_now(s->rep);
1073 }
Willy Tarreau92795622009-03-06 12:51:23 +01001074 }
1075
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001076
1077 /* we may have a pending connection request, or a connection waiting
1078 * for completion.
1079 */
1080 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1081 do {
1082 /* nb: step 1 might switch from QUE to ASS, but we first want
1083 * to give a chance to step 2 to perform a redirect if needed.
1084 */
1085 if (s->si[1].state != SI_ST_REQ)
1086 sess_update_stream_int(s, &s->si[1]);
1087 if (s->si[1].state == SI_ST_REQ)
1088 sess_prepare_conn_req(s, &s->si[1]);
1089
1090 if (s->si[1].state == SI_ST_ASS && s->srv &&
1091 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1092 perform_http_redirect(s, &s->si[1]);
1093 } while (s->si[1].state == SI_ST_ASS);
1094 }
1095
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001096 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001097 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001098 goto resync_stream_interface;
1099
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001100 /* otherwise wewant to check if we need to resync the req buffer or not */
1101 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001102 goto resync_request;
1103
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001104 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001105
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001106 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001107 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001108 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001109 if (!s->rep->analysers &&
1110 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001111 (s->rep->prod->state >= SI_ST_EST) &&
1112 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001113 /* This buffer is freewheeling, there's no analyser nor hijacker
1114 * attached to it. If any data are left in, we'll permit them to
1115 * move.
1116 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001117 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001118 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001119 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1120 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001121 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001122
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001123 /* check if it is wise to enable kernel splicing to forward response data */
1124 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1125 s->rep->to_forward &&
1126 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001127 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001128 (pipes_used < global.maxpipes) &&
1129 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1130 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1131 (s->rep->flags & BF_STREAMER_FAST)))) {
1132 s->rep->flags |= BF_KERN_SPLICING;
1133 }
1134
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001135 /* reflect what the L7 analysers have seen last */
1136 rpf_last = s->rep->flags;
1137
1138 /*
1139 * Now forward all shutdown requests between both sides of the buffer
1140 */
1141
1142 /*
1143 * FIXME: this is probably where we should produce error responses.
1144 */
1145
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001146 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001147 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1148 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001149 buffer_shutw_now(s->rep);
1150
1151 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001152 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 +01001153 s->rep->cons->shutw(s->rep->cons);
1154
1155 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001156 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1157 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001158 buffer_shutr_now(s->rep);
1159
1160 /* shutdown(read) pending */
1161 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1162 s->rep->prod->shutr(s->rep->prod);
1163
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001164 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001165 goto resync_stream_interface;
1166
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001167 if (s->req->flags != rqf_last)
1168 goto resync_request;
1169
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001170 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001171 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001173 /* we're interested in getting wakeups again */
1174 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1175 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1176
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001177 /* This is needed only when debugging is enabled, to indicate
1178 * client-side or server-side close. Please note that in the unlikely
1179 * event where both sides would close at once, the sequence is reported
1180 * on the server side first.
1181 */
1182 if (unlikely((global.mode & MODE_DEBUG) &&
1183 (!(global.mode & MODE_QUIET) ||
1184 (global.mode & MODE_VERBOSE)))) {
1185 int len;
1186
1187 if (s->si[1].state == SI_ST_CLO &&
1188 s->si[1].prev_state == SI_ST_EST) {
1189 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1190 s->uniq_id, s->be->id,
1191 (unsigned short)s->si[0].fd,
1192 (unsigned short)s->si[1].fd);
1193 write(1, trash, len);
1194 }
1195
1196 if (s->si[0].state == SI_ST_CLO &&
1197 s->si[0].prev_state == SI_ST_EST) {
1198 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1199 s->uniq_id, s->be->id,
1200 (unsigned short)s->si[0].fd,
1201 (unsigned short)s->si[1].fd);
1202 write(1, trash, len);
1203 }
1204 }
1205
1206 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1207 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1208
1209 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1210 session_process_counters(s);
1211
Willy Tarreau1accfc02009-09-05 20:57:35 +02001212 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001213 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001214
Willy Tarreau1accfc02009-09-05 20:57:35 +02001215 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001216 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217
Willy Tarreauea388542009-06-21 21:45:58 +02001218 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1219 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001220 s->si[0].prev_state = s->si[0].state;
1221 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001222 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1223 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001224
1225 /* Trick: if a request is being waiting for the server to respond,
1226 * and if we know the server can timeout, we don't want the timeout
1227 * to expire on the client side first, but we're still interested
1228 * in passing data from the client to the server (eg: POST). Thus,
1229 * we can cancel the client's request timeout if the server's
1230 * request timeout is set and the server has not yet sent a response.
1231 */
1232
Willy Tarreau520d95e2009-09-19 21:04:57 +02001233 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001234 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1235 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001236 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001237 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238
Willy Tarreau1accfc02009-09-05 20:57:35 +02001239 /* Call the second stream interface's I/O handler if it's embedded.
1240 * Note that this one may wake the task up again.
1241 */
1242 if (s->req->cons->iohandler) {
1243 s->req->cons->iohandler(s->req->cons);
1244 if (task_in_rq(t)) {
1245 /* If we woke up, we don't want to requeue the
1246 * task to the wait queue, but rather requeue
1247 * it into the runqueue ASAP.
1248 */
1249 t->expire = TICK_ETERNITY;
1250 return t;
1251 }
1252 }
1253
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001254 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1255 tick_first(s->rep->rex, s->rep->wex));
1256 if (s->req->analysers)
1257 t->expire = tick_first(t->expire, s->req->analyse_exp);
1258
1259 if (s->si[0].exp)
1260 t->expire = tick_first(t->expire, s->si[0].exp);
1261
1262 if (s->si[1].exp)
1263 t->expire = tick_first(t->expire, s->si[1].exp);
1264
1265#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001266 fprintf(stderr,
1267 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1268 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1269 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1270 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 +01001271#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001272
1273#ifdef DEBUG_DEV
1274 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001275 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001276 ABORT_NOW();
1277#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001278 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001279 }
1280
1281 s->fe->feconn--;
1282 if (s->flags & SN_BE_ASSIGNED)
1283 s->be->beconn--;
1284 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001285 s->listener->nbconn--;
1286 if (s->listener->state == LI_FULL &&
1287 s->listener->nbconn < s->listener->maxconn) {
1288 /* we should reactivate the listener */
1289 EV_FD_SET(s->listener->fd, DIR_RD);
1290 s->listener->state = LI_READY;
1291 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001292
1293 if (unlikely((global.mode & MODE_DEBUG) &&
1294 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1295 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001296 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001297 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001298 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001299 write(1, trash, len);
1300 }
1301
1302 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1303 session_process_counters(s);
1304
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001305 if (s->txn.status) {
1306 int n;
1307
1308 n = s->txn.status / 100;
1309 if (n < 1 || n > 5)
1310 n = 0;
1311
1312 if (s->fe->mode == PR_MODE_HTTP)
1313 s->fe->counters.p.http.rsp[n]++;
1314
1315 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
1316 (s->be->mode == PR_MODE_HTTP))
1317 s->be->counters.p.http.rsp[n]++;
1318 }
1319
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001320 /* let's do a final log if we need it */
1321 if (s->logs.logwait &&
1322 !(s->flags & SN_MONITOR) &&
1323 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001324 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001325 }
1326
1327 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001328 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001329 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001330 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001331 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001332}
1333
Willy Tarreau7c669d72008-06-20 15:04:11 +02001334/*
1335 * This function adjusts sess->srv_conn and maintains the previous and new
1336 * server's served session counts. Setting newsrv to NULL is enough to release
1337 * current connection slot. This function also notifies any LB algo which might
1338 * expect to be informed about any change in the number of active sessions on a
1339 * server.
1340 */
1341void sess_change_server(struct session *sess, struct server *newsrv)
1342{
1343 if (sess->srv_conn == newsrv)
1344 return;
1345
1346 if (sess->srv_conn) {
1347 sess->srv_conn->served--;
1348 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1349 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1350 sess->srv_conn = NULL;
1351 }
1352
1353 if (newsrv) {
1354 newsrv->served++;
1355 if (newsrv->proxy->lbprm.server_take_conn)
1356 newsrv->proxy->lbprm.server_take_conn(newsrv);
1357 sess->srv_conn = newsrv;
1358 }
1359}
1360
Willy Tarreau84455332009-03-15 22:34:05 +01001361/* Set correct session termination flags in case no analyser has done it. It
1362 * also counts a failed request if the server state has not reached the request
1363 * stage.
1364 */
1365void sess_set_term_flags(struct session *s)
1366{
1367 if (!(s->flags & SN_FINST_MASK)) {
1368 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001369
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001370 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001371 if (s->listener->counters)
1372 s->listener->counters->failed_req++;
1373
Willy Tarreau84455332009-03-15 22:34:05 +01001374 s->flags |= SN_FINST_R;
1375 }
1376 else if (s->si[1].state == SI_ST_QUE)
1377 s->flags |= SN_FINST_Q;
1378 else if (s->si[1].state < SI_ST_EST)
1379 s->flags |= SN_FINST_C;
1380 else if (s->si[1].state == SI_ST_EST)
1381 s->flags |= SN_FINST_D;
1382 else
1383 s->flags |= SN_FINST_L;
1384 }
1385}
1386
1387/* Handle server-side errors for default protocols. It is called whenever a a
1388 * connection setup is aborted or a request is aborted in queue. It sets the
1389 * session termination flags so that the caller does not have to worry about
1390 * them. It's installed as ->srv_error for the server-side stream_interface.
1391 */
1392void default_srv_error(struct session *s, struct stream_interface *si)
1393{
1394 int err_type = si->err_type;
1395 int err = 0, fin = 0;
1396
1397 if (err_type & SI_ET_QUEUE_ABRT) {
1398 err = SN_ERR_CLICL;
1399 fin = SN_FINST_Q;
1400 }
1401 else if (err_type & SI_ET_CONN_ABRT) {
1402 err = SN_ERR_CLICL;
1403 fin = SN_FINST_C;
1404 }
1405 else if (err_type & SI_ET_QUEUE_TO) {
1406 err = SN_ERR_SRVTO;
1407 fin = SN_FINST_Q;
1408 }
1409 else if (err_type & SI_ET_QUEUE_ERR) {
1410 err = SN_ERR_SRVCL;
1411 fin = SN_FINST_Q;
1412 }
1413 else if (err_type & SI_ET_CONN_TO) {
1414 err = SN_ERR_SRVTO;
1415 fin = SN_FINST_C;
1416 }
1417 else if (err_type & SI_ET_CONN_ERR) {
1418 err = SN_ERR_SRVCL;
1419 fin = SN_FINST_C;
1420 }
1421 else /* SI_ET_CONN_OTHER and others */ {
1422 err = SN_ERR_INTERNAL;
1423 fin = SN_FINST_C;
1424 }
1425
1426 if (!(s->flags & SN_ERR_MASK))
1427 s->flags |= err;
1428 if (!(s->flags & SN_FINST_MASK))
1429 s->flags |= fin;
1430}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001431
Willy Tarreaubaaee002006-06-26 02:48:02 +02001432/*
1433 * Local variables:
1434 * c-indent-level: 8
1435 * c-basic-offset: 8
1436 * End:
1437 */