blob: b86526ac84b552f57f86f335f29b8a913cf9e6fe [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>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010030#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010031#include <proto/proto_http.h>
32#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020033#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010035#include <proto/server.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010036#include <proto/stream_interface.h>
37#include <proto/stream_sock.h>
38#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020040struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010041struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
43/*
44 * frees the context associated to a session. It must have been removed first.
45 */
46void session_free(struct session *s)
47{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010048 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020049 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010050 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010051
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 if (s->pend_pos)
53 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010054
Willy Tarreau1e62de62008-11-11 20:20:02 +010055 if (s->srv) { /* there may be requests left pending in queue */
56 if (s->flags & SN_CURR_SESS) {
57 s->flags &= ~SN_CURR_SESS;
58 s->srv->cur_sess--;
59 }
Willy Tarreau922a8062008-12-04 09:33:58 +010060 if (may_dequeue_tasks(s->srv, s->be))
61 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010062 }
Willy Tarreau922a8062008-12-04 09:33:58 +010063
Willy Tarreau7c669d72008-06-20 15:04:11 +020064 if (unlikely(s->srv_conn)) {
65 /* the session still has a reserved slot on a server, but
66 * it should normally be only the same as the one above,
67 * so this should not happen in fact.
68 */
69 sess_change_server(s, NULL);
70 }
71
Willy Tarreau3eba98a2009-01-25 13:56:13 +010072 if (s->req->pipe)
73 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010074
Willy Tarreau3eba98a2009-01-25 13:56:13 +010075 if (s->rep->pipe)
76 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010077
Willy Tarreau48d63db2008-08-03 17:41:33 +020078 pool_free2(pool2_buffer, s->req);
79 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
Cyril Bontébf47aeb2009-10-15 00:15:40 +020081 if (s->sessid)
82 pool_free2(apools.sessid, s->sessid);
83
Willy Tarreau92fb9832007-10-16 17:34:28 +020084 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020085 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010086
Willy Tarreau92fb9832007-10-16 17:34:28 +020087 if (txn->rsp.cap != NULL) {
88 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020089 for (h = fe->rsp_cap; h; h = h->next)
90 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020091 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020093 if (txn->req.cap != NULL) {
94 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020095 for (h = fe->req_cap; h; h = h->next)
96 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020097 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 }
Willy Tarreau48d63db2008-08-03 17:41:33 +0200100 pool_free2(pool2_requri, txn->uri);
101 pool_free2(pool2_capture, txn->cli_cookie);
102 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100103
104 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100105 /* we have to unlink all watchers. We must not relink them if
106 * this session was the last one in the list.
107 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100108 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100109 LIST_INIT(&bref->users);
110 if (s->list.n != &sessions)
111 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100112 bref->ref = s->list.n;
113 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100114 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200115 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200116
117 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200118 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200119 pool_flush2(pool2_buffer);
120 pool_flush2(fe->hdr_idx_pool);
121 pool_flush2(pool2_requri);
122 pool_flush2(pool2_capture);
123 pool_flush2(pool2_session);
124 pool_flush2(fe->req_cap_pool);
125 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200126 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200127}
128
129
130/* perform minimal intializations, report 0 in case of error, 1 if OK. */
131int init_session()
132{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100133 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200134 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
135 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136}
137
Willy Tarreau30e71012007-11-26 20:15:35 +0100138void session_process_counters(struct session *s)
139{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100140 unsigned long long bytes;
141
Willy Tarreau30e71012007-11-26 20:15:35 +0100142 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100143 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100144 s->logs.bytes_in = s->req->total;
145 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200146 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100147
Willy Tarreau30e71012007-11-26 20:15:35 +0100148 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200149 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100150
Willy Tarreau30e71012007-11-26 20:15:35 +0100151 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200152 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200153
154 if (s->listener->counters)
155 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100156 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100157 }
158
Willy Tarreau30e71012007-11-26 20:15:35 +0100159 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100160 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100161 s->logs.bytes_out = s->rep->total;
162 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200163 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100164
Willy Tarreau30e71012007-11-26 20:15:35 +0100165 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200166 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100167
Willy Tarreau30e71012007-11-26 20:15:35 +0100168 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200169 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200170
171 if (s->listener->counters)
172 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100173 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100174 }
175}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100177/* This function is called with (si->state == SI_ST_CON) meaning that a
178 * connection was attempted and that the file descriptor is already allocated.
179 * We must check for establishment, error and abort. Possible output states
180 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
181 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
182 * otherwise 1.
183 */
184int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
185{
186 struct buffer *req = si->ob;
187 struct buffer *rep = si->ib;
188
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100189 /* If we got an error, or if nothing happened and the connection timed
190 * out, we must give up. The CER state handler will take care of retry
191 * attempts and error reports.
192 */
193 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100194 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100195 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200196 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100197 fd_delete(si->fd);
198
199 if (si->err_type)
200 return 0;
201
202 si->err_loc = s->srv;
203 if (si->flags & SI_FL_ERR)
204 si->err_type = SI_ET_CONN_ERR;
205 else
206 si->err_type = SI_ET_CONN_TO;
207 return 0;
208 }
209
210 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200211 if (unlikely((rep->flags & BF_SHUTW) ||
212 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200213 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100214 s->be->options & PR_O_ABRT_CLOSE)))) {
215 /* give up */
216 si->shutw(si);
217 si->err_type |= SI_ET_CONN_ABRT;
218 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200219 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100220 if (s->srv_error)
221 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100222 return 1;
223 }
224
225 /* we need to wait a bit more if there was no activity either */
226 if (!(req->flags & BF_WRITE_ACTIVITY))
227 return 1;
228
229 /* OK, this means that a connection succeeded. The caller will be
230 * responsible for handling the transition from CON to EST.
231 */
232 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100233 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100234 si->state = SI_ST_EST;
235 si->err_type = SI_ET_NONE;
236 si->err_loc = NULL;
237 return 1;
238}
239
240/* This function is called with (si->state == SI_ST_CER) meaning that a
241 * previous connection attempt has failed and that the file descriptor
242 * has already been released. Possible causes include asynchronous error
243 * notification and time out. Possible output states are SI_ST_CLO when
244 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
245 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
246 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
247 * marked as in error state. It returns 0.
248 */
249int sess_update_st_cer(struct session *s, struct stream_interface *si)
250{
251 /* we probably have to release last session from the server */
252 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100253 health_adjust(s->srv, HANA_STATUS_L4_ERR);
254
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100255 if (s->flags & SN_CURR_SESS) {
256 s->flags &= ~SN_CURR_SESS;
257 s->srv->cur_sess--;
258 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100259 }
260
261 /* ensure that we have enough retries left */
262 s->conn_retries--;
263 if (s->conn_retries < 0) {
264 if (!si->err_type) {
265 si->err_type = SI_ET_CONN_ERR;
266 si->err_loc = s->srv;
267 }
268
269 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200270 s->srv->counters.failed_conns++;
271 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100272 if (may_dequeue_tasks(s->srv, s->be))
273 process_srv_queue(s->srv);
274
275 /* shutw is enough so stop a connecting socket */
276 si->shutw(si);
277 si->ob->flags |= BF_WRITE_ERROR;
278 si->ib->flags |= BF_READ_ERROR;
279
280 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100281 if (s->srv_error)
282 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100283 return 0;
284 }
285
286 /* If the "redispatch" option is set on the backend, we are allowed to
287 * retry on another server for the last retry. In order to achieve this,
288 * we must mark the session unassigned, and eventually clear the DIRECT
289 * bit to ignore any persistence cookie. We won't count a retry nor a
290 * redispatch yet, because this will depend on what server is selected.
291 */
292 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
293 if (may_dequeue_tasks(s->srv, s->be))
294 process_srv_queue(s->srv);
295
296 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
297 s->prev_srv = s->srv;
298 si->state = SI_ST_REQ;
299 } else {
300 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200301 s->srv->counters.retries++;
302 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100303 si->state = SI_ST_ASS;
304 }
305
306 if (si->flags & SI_FL_ERR) {
307 /* The error was an asynchronous connection error, and we will
308 * likely have to retry connecting to the same server, most
309 * likely leading to the same result. To avoid this, we wait
310 * one second before retrying.
311 */
312
313 if (!si->err_type)
314 si->err_type = SI_ET_CONN_ERR;
315
316 si->state = SI_ST_TAR;
317 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
318 return 0;
319 }
320 return 0;
321}
322
323/*
324 * This function handles the transition between the SI_ST_CON state and the
325 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
326 * SI_ST_EST.
327 */
328void sess_establish(struct session *s, struct stream_interface *si)
329{
330 struct buffer *req = si->ob;
331 struct buffer *rep = si->ib;
332
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100333 if (s->srv)
334 health_adjust(s->srv, HANA_STATUS_L4_OK);
335
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100336 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100337 /* if the user wants to log as soon as possible, without counting
338 * bytes from the server, then this is the right moment. */
339 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
340 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100341 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100342 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100343 }
344 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100345 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
346 /* reset hdr_idx which was already initialized by the request.
347 * right now, the http parser does it.
348 * hdr_idx_init(&s->txn.hdr_idx);
349 */
350 }
351
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200352 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100353 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
354 req->wex = TICK_ETERNITY;
355}
356
357/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
358 * Other input states are simply ignored.
359 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
360 * Flags must have previously been updated for timeouts and other conditions.
361 */
362void sess_update_stream_int(struct session *s, struct stream_interface *si)
363{
364 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",
365 now_ms, __FUNCTION__,
366 s,
367 s->req, s->rep,
368 s->req->rex, s->rep->wex,
369 s->req->flags, s->rep->flags,
370 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
371
372 if (si->state == SI_ST_ASS) {
373 /* Server assigned to connection request, we have to try to connect now */
374 int conn_err;
375
376 conn_err = connect_server(s);
377 if (conn_err == SN_ERR_NONE) {
378 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100379 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100380 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100381 return;
382 }
383
384 /* We have received a synchronous error. We might have to
385 * abort, retry immediately or redispatch.
386 */
387 if (conn_err == SN_ERR_INTERNAL) {
388 if (!si->err_type) {
389 si->err_type = SI_ET_CONN_OTHER;
390 si->err_loc = s->srv;
391 }
392
393 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100394 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100395 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200396 s->srv->counters.failed_conns++;
397 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100398
399 /* release other sessions waiting for this server */
400 if (may_dequeue_tasks(s->srv, s->be))
401 process_srv_queue(s->srv);
402
403 /* Failed and not retryable. */
404 si->shutr(si);
405 si->shutw(si);
406 si->ob->flags |= BF_WRITE_ERROR;
407
408 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
409
410 /* no session was ever accounted for this server */
411 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100412 if (s->srv_error)
413 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100414 return;
415 }
416
417 /* We are facing a retryable error, but we don't want to run a
418 * turn-around now, as the problem is likely a source port
419 * allocation problem, so we want to retry now.
420 */
421 si->state = SI_ST_CER;
422 si->flags &= ~SI_FL_ERR;
423 sess_update_st_cer(s, si);
424 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
425 return;
426 }
427 else if (si->state == SI_ST_QUE) {
428 /* connection request was queued, check for any update */
429 if (!s->pend_pos) {
430 /* The connection is not in the queue anymore. Either
431 * we have a server connection slot available and we
432 * go directly to the assigned state, or we need to
433 * load-balance first and go to the INI state.
434 */
435 si->exp = TICK_ETERNITY;
436 if (unlikely(!(s->flags & SN_ASSIGNED)))
437 si->state = SI_ST_REQ;
438 else {
439 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
440 si->state = SI_ST_ASS;
441 }
442 return;
443 }
444
445 /* Connection request still in queue... */
446 if (si->flags & SI_FL_EXP) {
447 /* ... and timeout expired */
448 si->exp = TICK_ETERNITY;
449 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
450 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200451 s->srv->counters.failed_conns++;
452 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100453 si->shutr(si);
454 si->shutw(si);
455 si->ob->flags |= BF_WRITE_TIMEOUT;
456 if (!si->err_type)
457 si->err_type = SI_ET_QUEUE_TO;
458 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100459 if (s->srv_error)
460 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100461 return;
462 }
463
464 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200465 if ((si->ob->flags & (BF_READ_ERROR)) ||
466 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200467 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100468 /* give up */
469 si->exp = TICK_ETERNITY;
470 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
471 si->shutr(si);
472 si->shutw(si);
473 si->err_type |= SI_ET_QUEUE_ABRT;
474 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100475 if (s->srv_error)
476 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100477 return;
478 }
479
480 /* Nothing changed */
481 return;
482 }
483 else if (si->state == SI_ST_TAR) {
484 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200485 if ((si->ob->flags & (BF_READ_ERROR)) ||
486 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200487 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100488 /* give up */
489 si->exp = TICK_ETERNITY;
490 si->shutr(si);
491 si->shutw(si);
492 si->err_type |= SI_ET_CONN_ABRT;
493 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100494 if (s->srv_error)
495 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100496 return;
497 }
498
499 if (!(si->flags & SI_FL_EXP))
500 return; /* still in turn-around */
501
502 si->exp = TICK_ETERNITY;
503
504 /* we keep trying on the same server as long as the session is
505 * marked "assigned".
506 * FIXME: Should we force a redispatch attempt when the server is down ?
507 */
508 if (s->flags & SN_ASSIGNED)
509 si->state = SI_ST_ASS;
510 else
511 si->state = SI_ST_REQ;
512 return;
513 }
514}
515
516/* This function initiates a server connection request on a stream interface
517 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
518 * indicating that a server has been assigned. It may also return SI_ST_QUE,
519 * or SI_ST_CLO upon error.
520 */
521static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
522 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",
523 now_ms, __FUNCTION__,
524 s,
525 s->req, s->rep,
526 s->req->rex, s->rep->wex,
527 s->req->flags, s->rep->flags,
528 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
529
530 if (si->state != SI_ST_REQ)
531 return;
532
533 /* Try to assign a server */
534 if (srv_redispatch_connect(s) != 0) {
535 /* We did not get a server. Either we queued the
536 * connection request, or we encountered an error.
537 */
538 if (si->state == SI_ST_QUE)
539 return;
540
541 /* we did not get any server, let's check the cause */
542 si->shutr(si);
543 si->shutw(si);
544 si->ob->flags |= BF_WRITE_ERROR;
545 if (!si->err_type)
546 si->err_type = SI_ET_CONN_OTHER;
547 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100548 if (s->srv_error)
549 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100550 return;
551 }
552
553 /* The server is assigned */
554 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
555 si->state = SI_ST_ASS;
556}
557
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200558/* This stream analyser checks the switching rules and changes the backend
559 * if appropriate. The default_backend rule is also considered.
560 * It returns 1 if the processing can continue on next analysers, or zero if it
561 * either needs more data or wants to immediately abort the request.
562 */
563int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
564{
565 req->analysers &= ~an_bit;
566 req->analyse_exp = TICK_ETERNITY;
567
568 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
569 now_ms, __FUNCTION__,
570 s,
571 req,
572 req->rex, req->wex,
573 req->flags,
574 req->l,
575 req->analysers);
576
577 /* now check whether we have some switching rules for this request */
578 if (!(s->flags & SN_BE_ASSIGNED)) {
579 struct switching_rule *rule;
580
581 list_for_each_entry(rule, &s->fe->switching_rules, list) {
582 int ret;
583
584 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
585 ret = acl_pass(ret);
586 if (rule->cond->pol == ACL_COND_UNLESS)
587 ret = !ret;
588
589 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200590 if (!session_set_backend(s, rule->be.backend))
591 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200592 break;
593 }
594 }
595
596 /* To ensure correct connection accounting on the backend, we
597 * have to assign one if it was not set (eg: a listen). This
598 * measure also takes care of correctly setting the default
599 * backend if any.
600 */
601 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200602 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
603 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200604 }
605
606 /* we don't want to run the HTTP filters again if the backend has not changed */
607 if (s->fe == s->be)
608 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
609
610 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200611
612 sw_failed:
613 /* immediately abort this request in case of allocation failure */
614 buffer_abort(s->req);
615 buffer_abort(s->rep);
616
617 if (!(s->flags & SN_ERR_MASK))
618 s->flags |= SN_ERR_RESOURCE;
619 if (!(s->flags & SN_FINST_MASK))
620 s->flags |= SN_FINST_R;
621
622 s->txn.status = 500;
623 s->req->analysers = 0;
624 s->req->analyse_exp = TICK_ETERNITY;
625 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200626}
627
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628/* Processes the client, server, request and response jobs of a session task,
629 * then puts it back to the wait queue in a clean state, or cleans up its
630 * resources if it must be deleted. Returns in <next> the date the task wants
631 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
632 * nothing too many times, the request and response buffers flags are monitored
633 * and each function is called only if at least another function has changed at
634 * least one flag it is interested in.
635 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100636struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637{
638 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100639 unsigned int rqf_last, rpf_last;
640
641 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
642 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
643
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200644 /* This flag must explicitly be set every time */
645 s->req->flags &= ~BF_READ_NOEXP;
646
647 /* Keep a copy of req/rep flags so that we can detect shutdowns */
648 rqf_last = s->req->flags;
649 rpf_last = s->rep->flags;
650
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200651 /* we don't want the stream interface functions to recursively wake us up */
652 if (s->req->prod->owner == t)
653 s->req->prod->flags |= SI_FL_DONT_WAKE;
654 if (s->req->cons->owner == t)
655 s->req->cons->flags |= SI_FL_DONT_WAKE;
656
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657 /* 1a: Check for low level timeouts if needed. We just set a flag on
658 * stream interfaces when their timeouts have expired.
659 */
660 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
661 stream_int_check_timeouts(&s->si[0]);
662 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200663
664 /* check buffer timeouts, and close the corresponding stream interfaces
665 * for future reads or writes. Note: this will also concern upper layers
666 * but we do not touch any other flag. We must be careful and correctly
667 * detect state changes when calling them.
668 */
669
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100670 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200671
672 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
673 s->req->prod->shutr(s->req->prod);
674
675 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
676 s->req->cons->shutw(s->req->cons);
677
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200680 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
681 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100682
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200683 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
684 s->rep->cons->shutw(s->rep->cons);
685 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686
687 /* 1b: check for low-level errors reported at the stream interface.
688 * First we check if it's a retryable error (in which case we don't
689 * want to tell the buffer). Otherwise we report the error one level
690 * upper by setting flags into the buffers. Note that the side towards
691 * the client cannot have connect (hence retryable) errors. Also, the
692 * connection setup code must be able to deal with any type of abort.
693 */
694 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
695 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
696 s->si[0].shutr(&s->si[0]);
697 s->si[0].shutw(&s->si[0]);
698 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100699 if (!(s->req->analysers) && !(s->rep->analysers)) {
700 if (!(s->flags & SN_ERR_MASK))
701 s->flags |= SN_ERR_CLICL;
702 if (!(s->flags & SN_FINST_MASK))
703 s->flags |= SN_FINST_D;
704 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 }
706 }
707
708 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
709 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
710 s->si[1].shutr(&s->si[1]);
711 s->si[1].shutw(&s->si[1]);
712 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200713 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200715 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100716 if (!(s->req->analysers) && !(s->rep->analysers)) {
717 if (!(s->flags & SN_ERR_MASK))
718 s->flags |= SN_ERR_SRVCL;
719 if (!(s->flags & SN_FINST_MASK))
720 s->flags |= SN_FINST_D;
721 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 }
723 /* note: maybe we should process connection errors here ? */
724 }
725
726 if (s->si[1].state == SI_ST_CON) {
727 /* we were trying to establish a connection on the server side,
728 * maybe it succeeded, maybe it failed, maybe we timed out, ...
729 */
730 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
731 sess_update_st_cer(s, &s->si[1]);
732 else if (s->si[1].state == SI_ST_EST)
733 sess_establish(s, &s->si[1]);
734
735 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
736 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
737 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
738 */
739 }
740
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200741resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 /* Check for connection closure */
743
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 DPRINTF(stderr,
745 "[%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",
746 now_ms, __FUNCTION__, __LINE__,
747 t,
748 s, s->flags,
749 s->req, s->rep,
750 s->req->rex, s->rep->wex,
751 s->req->flags, s->rep->flags,
752 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
753 s->rep->cons->err_type, s->req->cons->err_type,
754 s->conn_retries);
755
756 /* nothing special to be done on client side */
757 if (unlikely(s->req->prod->state == SI_ST_DIS))
758 s->req->prod->state = SI_ST_CLO;
759
760 /* When a server-side connection is released, we have to count it and
761 * check for pending connections on this server.
762 */
763 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
764 s->req->cons->state = SI_ST_CLO;
765 if (s->srv) {
766 if (s->flags & SN_CURR_SESS) {
767 s->flags &= ~SN_CURR_SESS;
768 s->srv->cur_sess--;
769 }
770 sess_change_server(s, NULL);
771 if (may_dequeue_tasks(s->srv, s->be))
772 process_srv_queue(s->srv);
773 }
774 }
775
776 /*
777 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
778 * at this point.
779 */
780
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100781 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 /* Analyse request */
783 if ((s->req->flags & BF_MASK_ANALYSER) ||
784 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
785 unsigned int flags = s->req->flags;
786
787 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200788 unsigned int last_ana = 0;
789
Willy Tarreau520d95e2009-09-19 21:04:57 +0200790 /* it's up to the analysers to stop new connections */
791 buffer_auto_connect(s->req);
792 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100793
794 /* We will call all analysers for which a bit is set in
795 * s->req->analysers, following the bit order from LSB
796 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200797 * the list when not needed. Any analyser may return 0
798 * to break out of the loop, either because of missing
799 * data to take a decision, or because it decides to
800 * kill the session. We loop at least once through each
801 * analyser, and we may loop again if other analysers
802 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200804 while (s->req->analysers & ~last_ana) {
805 last_ana = s->req->analysers;
806
807 if (s->req->analysers & AN_REQ_INSPECT) {
808 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200809 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100810 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200811 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100812
Willy Tarreaud787e662009-07-07 10:14:51 +0200813 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
814 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200815 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200816 break;
817 }
818
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200819 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
820 last_ana |= AN_REQ_HTTP_PROCESS_FE;
821 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
822 break;
823 }
824
825 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
826 last_ana |= AN_REQ_SWITCHING_RULES;
827 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
828 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200829 /* FIXME: we mait switch from TCP to HTTP and want to
830 * immediately loop back to the top. This is a dirty way
831 * of doing it, and we should find a cleaner method relying
832 * on a circular list of function pointers.
833 */
834 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
835 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200836 }
837
838 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
839 last_ana |= AN_REQ_HTTP_PROCESS_BE;
840 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
841 break;
842 }
843
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200844 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
845 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200846 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100847 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200848 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100849
Willy Tarreauc465fd72009-08-31 00:17:18 +0200850 if (s->req->analysers & AN_REQ_HTTP_INNER) {
851 last_ana |= AN_REQ_HTTP_INNER;
852 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
853 break;
854 }
855
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200856 if (s->req->analysers & AN_REQ_HTTP_BODY) {
857 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200858 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100859 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200860 }
Emeric Brun647caf12009-06-30 17:57:00 +0200861
862 if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) {
863 last_ana |= AN_REQ_PRST_RDP_COOKIE;
864 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
865 break;
866 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868 }
Willy Tarreau84455332009-03-15 22:34:05 +0100869
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200870 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
871 rqf_last = s->req->flags;
872 goto resync_request;
873 }
874 }
875
876 resync_response:
877 /* Analyse response */
878
879 if (unlikely(s->rep->flags & BF_HIJACK)) {
880 /* In inject mode, we wake up everytime something has
881 * happened on the write side of the buffer.
882 */
883 unsigned int flags = s->rep->flags;
884
885 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
886 !(s->rep->flags & BF_FULL)) {
887 s->rep->hijacker(s, s->rep);
888 }
889
890 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
891 rpf_last = s->rep->flags;
892 goto resync_response;
893 }
894 }
895 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
896 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
897 unsigned int flags = s->rep->flags;
898
899 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200900 unsigned int last_ana = 0;
901
Willy Tarreau520d95e2009-09-19 21:04:57 +0200902 /* it's up to the analysers to reset auto_close */
903 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200904
905 /* We will call all analysers for which a bit is set in
906 * s->rep->analysers, following the bit order from LSB
907 * to MSB. The analysers must remove themselves from
908 * the list when not needed. Any analyser may return 0
909 * to break out of the loop, either because of missing
910 * data to take a decision, or because it decides to
911 * kill the session. We loop at least once through each
912 * analyser, and we may loop again if other analysers
913 * are added in the middle.
914 */
915 while (s->rep->analysers & ~last_ana) {
916 last_ana = s->rep->analysers;
917
918 if (s->rep->analysers & AN_RES_WAIT_HTTP) {
919 last_ana |= AN_RES_WAIT_HTTP;
920 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
921 break;
922 }
923
924 if (s->rep->analysers & AN_RES_HTTP_PROCESS_BE) {
925 last_ana |= AN_RES_HTTP_PROCESS_BE;
926 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
927 break;
928 /* FIXME: we may wait for a second response in case of a status 1xx
929 * and want to immediately loop back to the top. This is a dirty way
930 * of doing it, and we should find a cleaner method relying on a
931 * circular list of function pointers.
932 */
933 if ((s->rep->analysers & ~last_ana) & AN_RES_WAIT_HTTP)
934 continue;
935 }
936 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200937 }
938
939 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
940 rpf_last = s->rep->flags;
941 goto resync_response;
942 }
943 }
944
945 /* FIXME: here we should call protocol handlers which rely on
946 * both buffers.
947 */
948
949
950 /*
951 * Now we propagate unhandled errors to the session
952 */
953 if (!(s->flags & SN_ERR_MASK)) {
954 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
955 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100956 s->req->analysers = 0;
957 if (s->req->flags & BF_READ_ERROR)
958 s->flags |= SN_ERR_CLICL;
959 else if (s->req->flags & BF_READ_TIMEOUT)
960 s->flags |= SN_ERR_CLITO;
961 else if (s->req->flags & BF_WRITE_ERROR)
962 s->flags |= SN_ERR_SRVCL;
963 else
964 s->flags |= SN_ERR_SRVTO;
965 sess_set_term_flags(s);
966 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200967 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
968 /* Report it if the server got an error or a read timeout expired */
969 s->rep->analysers = 0;
970 if (s->rep->flags & BF_READ_ERROR)
971 s->flags |= SN_ERR_SRVCL;
972 else if (s->rep->flags & BF_READ_TIMEOUT)
973 s->flags |= SN_ERR_SRVTO;
974 else if (s->rep->flags & BF_WRITE_ERROR)
975 s->flags |= SN_ERR_CLICL;
976 else
Willy Tarreau84455332009-03-15 22:34:05 +0100977 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200978 sess_set_term_flags(s);
979 }
Willy Tarreau84455332009-03-15 22:34:05 +0100980 }
981
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200982 /*
983 * Here we take care of forwarding unhandled data. This also includes
984 * connection establishments and shutdown requests.
985 */
986
987
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100988 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +0200989 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100990 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100991 if (!s->req->analysers &&
992 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +0200993 (s->req->prod->state >= SI_ST_EST) &&
994 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100995 /* This buffer is freewheeling, there's no analyser nor hijacker
996 * attached to it. If any data are left in, we'll permit them to
997 * move.
998 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200999 buffer_auto_connect(s->req);
1000 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001001 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001002
Willy Tarreau31971e52009-09-20 12:07:52 +02001003 /* If the producer is still connected, we'll enable data to flow
1004 * from the producer to the consumer (which might possibly not be
1005 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001006 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001007 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1008 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001009 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001010
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001011 /* check if it is wise to enable kernel splicing to forward request data */
1012 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1013 s->req->to_forward &&
1014 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001015 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001016 (pipes_used < global.maxpipes) &&
1017 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1018 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1019 (s->req->flags & BF_STREAMER_FAST)))) {
1020 s->req->flags |= BF_KERN_SPLICING;
1021 }
1022
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023 /* reflect what the L7 analysers have seen last */
1024 rqf_last = s->req->flags;
1025
1026 /*
1027 * Now forward all shutdown requests between both sides of the buffer
1028 */
1029
Willy Tarreau520d95e2009-09-19 21:04:57 +02001030 /* first, let's check if the request buffer needs to shutdown(write), which may
1031 * happen either because the input is closed or because we want to force a close
1032 * once the server has begun to respond.
1033 */
1034 if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE)) == BF_AUTO_CLOSE) {
1035 if (unlikely((s->req->flags & BF_SHUTR) ||
1036 ((s->req->cons->state == SI_ST_EST) &&
1037 (s->be->options & PR_O_FORCE_CLO) &&
1038 (s->rep->flags & BF_READ_ACTIVITY))))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001039 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040 }
1041
1042 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001043 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 +01001044 s->req->cons->shutw(s->req->cons);
1045
1046 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001047 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1048 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001049 buffer_shutr_now(s->req);
1050
1051 /* shutdown(read) pending */
1052 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1053 s->req->prod->shutr(s->req->prod);
1054
Willy Tarreau520d95e2009-09-19 21:04:57 +02001055 /* it's possible that an upper layer has requested a connection setup or abort.
1056 * There are 2 situations where we decide to establish a new connection :
1057 * - there are data scheduled for emission in the buffer
1058 * - the BF_AUTO_CONNECT flag is set (active connection)
1059 */
1060 if (s->req->cons->state == SI_ST_INI) {
1061 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001062 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001063 /* If we have a ->connect method, we need to perform a connection request,
1064 * otherwise we immediately switch to the connected state.
1065 */
1066 if (s->req->cons->connect)
1067 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1068 else
1069 s->req->cons->state = SI_ST_EST; /* connection established */
1070 }
Willy Tarreau73201222009-08-16 18:27:24 +02001071 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001072 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001073 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001074 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1075 buffer_shutr_now(s->rep);
1076 }
Willy Tarreau92795622009-03-06 12:51:23 +01001077 }
1078
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001079
1080 /* we may have a pending connection request, or a connection waiting
1081 * for completion.
1082 */
1083 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1084 do {
1085 /* nb: step 1 might switch from QUE to ASS, but we first want
1086 * to give a chance to step 2 to perform a redirect if needed.
1087 */
1088 if (s->si[1].state != SI_ST_REQ)
1089 sess_update_stream_int(s, &s->si[1]);
1090 if (s->si[1].state == SI_ST_REQ)
1091 sess_prepare_conn_req(s, &s->si[1]);
1092
1093 if (s->si[1].state == SI_ST_ASS && s->srv &&
1094 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1095 perform_http_redirect(s, &s->si[1]);
1096 } while (s->si[1].state == SI_ST_ASS);
1097 }
1098
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001099 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001100 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001101 goto resync_stream_interface;
1102
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001103 /* otherwise wewant to check if we need to resync the req buffer or not */
1104 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001105 goto resync_request;
1106
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001107 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001108
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001109 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001110 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001111 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001112 if (!s->rep->analysers &&
1113 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001114 (s->rep->prod->state >= SI_ST_EST) &&
1115 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001116 /* This buffer is freewheeling, there's no analyser nor hijacker
1117 * attached to it. If any data are left in, we'll permit them to
1118 * move.
1119 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001120 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001121 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001122 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1123 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001124 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001125
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001126 /* check if it is wise to enable kernel splicing to forward response data */
1127 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1128 s->rep->to_forward &&
1129 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001130 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001131 (pipes_used < global.maxpipes) &&
1132 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1133 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1134 (s->rep->flags & BF_STREAMER_FAST)))) {
1135 s->rep->flags |= BF_KERN_SPLICING;
1136 }
1137
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001138 /* reflect what the L7 analysers have seen last */
1139 rpf_last = s->rep->flags;
1140
1141 /*
1142 * Now forward all shutdown requests between both sides of the buffer
1143 */
1144
1145 /*
1146 * FIXME: this is probably where we should produce error responses.
1147 */
1148
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001149 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001150 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1151 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001152 buffer_shutw_now(s->rep);
1153
1154 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001155 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 +01001156 s->rep->cons->shutw(s->rep->cons);
1157
1158 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001159 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1160 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001161 buffer_shutr_now(s->rep);
1162
1163 /* shutdown(read) pending */
1164 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1165 s->rep->prod->shutr(s->rep->prod);
1166
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001167 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001168 goto resync_stream_interface;
1169
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001170 if (s->req->flags != rqf_last)
1171 goto resync_request;
1172
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001173 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001174 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001175
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001176 /* we're interested in getting wakeups again */
1177 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1178 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1179
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001180 /* This is needed only when debugging is enabled, to indicate
1181 * client-side or server-side close. Please note that in the unlikely
1182 * event where both sides would close at once, the sequence is reported
1183 * on the server side first.
1184 */
1185 if (unlikely((global.mode & MODE_DEBUG) &&
1186 (!(global.mode & MODE_QUIET) ||
1187 (global.mode & MODE_VERBOSE)))) {
1188 int len;
1189
1190 if (s->si[1].state == SI_ST_CLO &&
1191 s->si[1].prev_state == SI_ST_EST) {
1192 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1193 s->uniq_id, s->be->id,
1194 (unsigned short)s->si[0].fd,
1195 (unsigned short)s->si[1].fd);
1196 write(1, trash, len);
1197 }
1198
1199 if (s->si[0].state == SI_ST_CLO &&
1200 s->si[0].prev_state == SI_ST_EST) {
1201 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1202 s->uniq_id, s->be->id,
1203 (unsigned short)s->si[0].fd,
1204 (unsigned short)s->si[1].fd);
1205 write(1, trash, len);
1206 }
1207 }
1208
1209 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1210 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1211
1212 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1213 session_process_counters(s);
1214
Willy Tarreau1accfc02009-09-05 20:57:35 +02001215 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001216 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217
Willy Tarreau1accfc02009-09-05 20:57:35 +02001218 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001219 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001220
Willy Tarreauea388542009-06-21 21:45:58 +02001221 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1222 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001223 s->si[0].prev_state = s->si[0].state;
1224 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001225 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1226 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001227
1228 /* Trick: if a request is being waiting for the server to respond,
1229 * and if we know the server can timeout, we don't want the timeout
1230 * to expire on the client side first, but we're still interested
1231 * in passing data from the client to the server (eg: POST). Thus,
1232 * we can cancel the client's request timeout if the server's
1233 * request timeout is set and the server has not yet sent a response.
1234 */
1235
Willy Tarreau520d95e2009-09-19 21:04:57 +02001236 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001237 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1238 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001239 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001240 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001241
Willy Tarreau1accfc02009-09-05 20:57:35 +02001242 /* Call the second stream interface's I/O handler if it's embedded.
1243 * Note that this one may wake the task up again.
1244 */
1245 if (s->req->cons->iohandler) {
1246 s->req->cons->iohandler(s->req->cons);
1247 if (task_in_rq(t)) {
1248 /* If we woke up, we don't want to requeue the
1249 * task to the wait queue, but rather requeue
1250 * it into the runqueue ASAP.
1251 */
1252 t->expire = TICK_ETERNITY;
1253 return t;
1254 }
1255 }
1256
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1258 tick_first(s->rep->rex, s->rep->wex));
1259 if (s->req->analysers)
1260 t->expire = tick_first(t->expire, s->req->analyse_exp);
1261
1262 if (s->si[0].exp)
1263 t->expire = tick_first(t->expire, s->si[0].exp);
1264
1265 if (s->si[1].exp)
1266 t->expire = tick_first(t->expire, s->si[1].exp);
1267
1268#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001269 fprintf(stderr,
1270 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1271 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1272 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1273 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 +01001274#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001275
1276#ifdef DEBUG_DEV
1277 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001278 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001279 ABORT_NOW();
1280#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001281 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001282 }
1283
1284 s->fe->feconn--;
1285 if (s->flags & SN_BE_ASSIGNED)
1286 s->be->beconn--;
1287 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001288 s->listener->nbconn--;
1289 if (s->listener->state == LI_FULL &&
1290 s->listener->nbconn < s->listener->maxconn) {
1291 /* we should reactivate the listener */
1292 EV_FD_SET(s->listener->fd, DIR_RD);
1293 s->listener->state = LI_READY;
1294 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001295
1296 if (unlikely((global.mode & MODE_DEBUG) &&
1297 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1298 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001299 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001300 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001301 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001302 write(1, trash, len);
1303 }
1304
1305 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1306 session_process_counters(s);
1307
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001308 if (s->txn.status) {
1309 int n;
1310
1311 n = s->txn.status / 100;
1312 if (n < 1 || n > 5)
1313 n = 0;
1314
1315 if (s->fe->mode == PR_MODE_HTTP)
1316 s->fe->counters.p.http.rsp[n]++;
1317
1318 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
1319 (s->be->mode == PR_MODE_HTTP))
1320 s->be->counters.p.http.rsp[n]++;
1321 }
1322
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001323 /* let's do a final log if we need it */
1324 if (s->logs.logwait &&
1325 !(s->flags & SN_MONITOR) &&
1326 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001327 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001328 }
1329
1330 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001331 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001332 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001333 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001334 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001335}
1336
Willy Tarreau7c669d72008-06-20 15:04:11 +02001337/*
1338 * This function adjusts sess->srv_conn and maintains the previous and new
1339 * server's served session counts. Setting newsrv to NULL is enough to release
1340 * current connection slot. This function also notifies any LB algo which might
1341 * expect to be informed about any change in the number of active sessions on a
1342 * server.
1343 */
1344void sess_change_server(struct session *sess, struct server *newsrv)
1345{
1346 if (sess->srv_conn == newsrv)
1347 return;
1348
1349 if (sess->srv_conn) {
1350 sess->srv_conn->served--;
1351 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1352 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1353 sess->srv_conn = NULL;
1354 }
1355
1356 if (newsrv) {
1357 newsrv->served++;
1358 if (newsrv->proxy->lbprm.server_take_conn)
1359 newsrv->proxy->lbprm.server_take_conn(newsrv);
1360 sess->srv_conn = newsrv;
1361 }
1362}
1363
Willy Tarreau84455332009-03-15 22:34:05 +01001364/* Set correct session termination flags in case no analyser has done it. It
1365 * also counts a failed request if the server state has not reached the request
1366 * stage.
1367 */
1368void sess_set_term_flags(struct session *s)
1369{
1370 if (!(s->flags & SN_FINST_MASK)) {
1371 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001372
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001373 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001374 if (s->listener->counters)
1375 s->listener->counters->failed_req++;
1376
Willy Tarreau84455332009-03-15 22:34:05 +01001377 s->flags |= SN_FINST_R;
1378 }
1379 else if (s->si[1].state == SI_ST_QUE)
1380 s->flags |= SN_FINST_Q;
1381 else if (s->si[1].state < SI_ST_EST)
1382 s->flags |= SN_FINST_C;
1383 else if (s->si[1].state == SI_ST_EST)
1384 s->flags |= SN_FINST_D;
1385 else
1386 s->flags |= SN_FINST_L;
1387 }
1388}
1389
1390/* Handle server-side errors for default protocols. It is called whenever a a
1391 * connection setup is aborted or a request is aborted in queue. It sets the
1392 * session termination flags so that the caller does not have to worry about
1393 * them. It's installed as ->srv_error for the server-side stream_interface.
1394 */
1395void default_srv_error(struct session *s, struct stream_interface *si)
1396{
1397 int err_type = si->err_type;
1398 int err = 0, fin = 0;
1399
1400 if (err_type & SI_ET_QUEUE_ABRT) {
1401 err = SN_ERR_CLICL;
1402 fin = SN_FINST_Q;
1403 }
1404 else if (err_type & SI_ET_CONN_ABRT) {
1405 err = SN_ERR_CLICL;
1406 fin = SN_FINST_C;
1407 }
1408 else if (err_type & SI_ET_QUEUE_TO) {
1409 err = SN_ERR_SRVTO;
1410 fin = SN_FINST_Q;
1411 }
1412 else if (err_type & SI_ET_QUEUE_ERR) {
1413 err = SN_ERR_SRVCL;
1414 fin = SN_FINST_Q;
1415 }
1416 else if (err_type & SI_ET_CONN_TO) {
1417 err = SN_ERR_SRVTO;
1418 fin = SN_FINST_C;
1419 }
1420 else if (err_type & SI_ET_CONN_ERR) {
1421 err = SN_ERR_SRVCL;
1422 fin = SN_FINST_C;
1423 }
1424 else /* SI_ET_CONN_OTHER and others */ {
1425 err = SN_ERR_INTERNAL;
1426 fin = SN_FINST_C;
1427 }
1428
1429 if (!(s->flags & SN_ERR_MASK))
1430 s->flags |= err;
1431 if (!(s->flags & SN_FINST_MASK))
1432 s->flags |= fin;
1433}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001434
Willy Tarreaubaaee002006-06-26 02:48:02 +02001435/*
1436 * Local variables:
1437 * c-indent-level: 8
1438 * c-basic-offset: 8
1439 * End:
1440 */