blob: 447660e2f3372a6413addf6af6ded74d86087f55 [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
Willy Tarreau92fb9832007-10-16 17:34:28 +020080 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020081 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010082
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 if (txn->rsp.cap != NULL) {
84 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020085 for (h = fe->rsp_cap; h; h = h->next)
86 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020087 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020089 if (txn->req.cap != NULL) {
90 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020091 for (h = fe->req_cap; h; h = h->next)
92 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020093 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020096 pool_free2(pool2_requri, txn->uri);
97 pool_free2(pool2_capture, txn->cli_cookie);
98 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010099
100 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100101 /* we have to unlink all watchers. We must not relink them if
102 * this session was the last one in the list.
103 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100104 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100105 LIST_INIT(&bref->users);
106 if (s->list.n != &sessions)
107 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100108 bref->ref = s->list.n;
109 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100110 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200111 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200112
113 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200114 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200115 pool_flush2(pool2_buffer);
116 pool_flush2(fe->hdr_idx_pool);
117 pool_flush2(pool2_requri);
118 pool_flush2(pool2_capture);
119 pool_flush2(pool2_session);
120 pool_flush2(fe->req_cap_pool);
121 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200122 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200123}
124
125
126/* perform minimal intializations, report 0 in case of error, 1 if OK. */
127int init_session()
128{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100129 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200130 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
131 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132}
133
Willy Tarreau30e71012007-11-26 20:15:35 +0100134void session_process_counters(struct session *s)
135{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100136 unsigned long long bytes;
137
Willy Tarreau30e71012007-11-26 20:15:35 +0100138 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100140 s->logs.bytes_in = s->req->total;
141 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200142 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100143
Willy Tarreau30e71012007-11-26 20:15:35 +0100144 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200145 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200148 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200149
150 if (s->listener->counters)
151 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100152 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100153 }
154
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100156 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100157 s->logs.bytes_out = s->rep->total;
158 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200159 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100160
Willy Tarreau30e71012007-11-26 20:15:35 +0100161 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200162 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100163
Willy Tarreau30e71012007-11-26 20:15:35 +0100164 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200165 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200166
167 if (s->listener->counters)
168 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100169 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100170 }
171}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100173/* This function is called with (si->state == SI_ST_CON) meaning that a
174 * connection was attempted and that the file descriptor is already allocated.
175 * We must check for establishment, error and abort. Possible output states
176 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
177 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
178 * otherwise 1.
179 */
180int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
181{
182 struct buffer *req = si->ob;
183 struct buffer *rep = si->ib;
184
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100185 /* If we got an error, or if nothing happened and the connection timed
186 * out, we must give up. The CER state handler will take care of retry
187 * attempts and error reports.
188 */
189 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100190 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100191 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200192 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100193 fd_delete(si->fd);
194
195 if (si->err_type)
196 return 0;
197
198 si->err_loc = s->srv;
199 if (si->flags & SI_FL_ERR)
200 si->err_type = SI_ET_CONN_ERR;
201 else
202 si->err_type = SI_ET_CONN_TO;
203 return 0;
204 }
205
206 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200207 if (unlikely((rep->flags & BF_SHUTW) ||
208 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200209 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100210 s->be->options & PR_O_ABRT_CLOSE)))) {
211 /* give up */
212 si->shutw(si);
213 si->err_type |= SI_ET_CONN_ABRT;
214 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200215 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100216 if (s->srv_error)
217 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100218 return 1;
219 }
220
221 /* we need to wait a bit more if there was no activity either */
222 if (!(req->flags & BF_WRITE_ACTIVITY))
223 return 1;
224
225 /* OK, this means that a connection succeeded. The caller will be
226 * responsible for handling the transition from CON to EST.
227 */
228 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100229 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100230 si->state = SI_ST_EST;
231 si->err_type = SI_ET_NONE;
232 si->err_loc = NULL;
233 return 1;
234}
235
236/* This function is called with (si->state == SI_ST_CER) meaning that a
237 * previous connection attempt has failed and that the file descriptor
238 * has already been released. Possible causes include asynchronous error
239 * notification and time out. Possible output states are SI_ST_CLO when
240 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
241 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
242 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
243 * marked as in error state. It returns 0.
244 */
245int sess_update_st_cer(struct session *s, struct stream_interface *si)
246{
247 /* we probably have to release last session from the server */
248 if (s->srv) {
249 if (s->flags & SN_CURR_SESS) {
250 s->flags &= ~SN_CURR_SESS;
251 s->srv->cur_sess--;
252 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100253 }
254
255 /* ensure that we have enough retries left */
256 s->conn_retries--;
257 if (s->conn_retries < 0) {
258 if (!si->err_type) {
259 si->err_type = SI_ET_CONN_ERR;
260 si->err_loc = s->srv;
261 }
262
263 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200264 s->srv->counters.failed_conns++;
265 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100266 if (may_dequeue_tasks(s->srv, s->be))
267 process_srv_queue(s->srv);
268
269 /* shutw is enough so stop a connecting socket */
270 si->shutw(si);
271 si->ob->flags |= BF_WRITE_ERROR;
272 si->ib->flags |= BF_READ_ERROR;
273
274 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100275 if (s->srv_error)
276 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100277 return 0;
278 }
279
280 /* If the "redispatch" option is set on the backend, we are allowed to
281 * retry on another server for the last retry. In order to achieve this,
282 * we must mark the session unassigned, and eventually clear the DIRECT
283 * bit to ignore any persistence cookie. We won't count a retry nor a
284 * redispatch yet, because this will depend on what server is selected.
285 */
286 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
287 if (may_dequeue_tasks(s->srv, s->be))
288 process_srv_queue(s->srv);
289
290 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
291 s->prev_srv = s->srv;
292 si->state = SI_ST_REQ;
293 } else {
294 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200295 s->srv->counters.retries++;
296 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100297 si->state = SI_ST_ASS;
298 }
299
300 if (si->flags & SI_FL_ERR) {
301 /* The error was an asynchronous connection error, and we will
302 * likely have to retry connecting to the same server, most
303 * likely leading to the same result. To avoid this, we wait
304 * one second before retrying.
305 */
306
307 if (!si->err_type)
308 si->err_type = SI_ET_CONN_ERR;
309
310 si->state = SI_ST_TAR;
311 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
312 return 0;
313 }
314 return 0;
315}
316
317/*
318 * This function handles the transition between the SI_ST_CON state and the
319 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
320 * SI_ST_EST.
321 */
322void sess_establish(struct session *s, struct stream_interface *si)
323{
324 struct buffer *req = si->ob;
325 struct buffer *rep = si->ib;
326
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100327 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200328 buffer_set_rlim(rep, rep->size); /* no rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100329
330 /* if the user wants to log as soon as possible, without counting
331 * bytes from the server, then this is the right moment. */
332 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
333 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100334 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100335 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100336 }
337 else {
Willy Tarreau27a674e2009-08-17 07:23:33 +0200338 buffer_set_rlim(rep, req->size - global.tune.maxrewrite); /* rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100339 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
340 /* reset hdr_idx which was already initialized by the request.
341 * right now, the http parser does it.
342 * hdr_idx_init(&s->txn.hdr_idx);
343 */
344 }
345
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200346 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100347 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
348 req->wex = TICK_ETERNITY;
349}
350
351/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
352 * Other input states are simply ignored.
353 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
354 * Flags must have previously been updated for timeouts and other conditions.
355 */
356void sess_update_stream_int(struct session *s, struct stream_interface *si)
357{
358 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",
359 now_ms, __FUNCTION__,
360 s,
361 s->req, s->rep,
362 s->req->rex, s->rep->wex,
363 s->req->flags, s->rep->flags,
364 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
365
366 if (si->state == SI_ST_ASS) {
367 /* Server assigned to connection request, we have to try to connect now */
368 int conn_err;
369
370 conn_err = connect_server(s);
371 if (conn_err == SN_ERR_NONE) {
372 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100373 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100374 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100375 return;
376 }
377
378 /* We have received a synchronous error. We might have to
379 * abort, retry immediately or redispatch.
380 */
381 if (conn_err == SN_ERR_INTERNAL) {
382 if (!si->err_type) {
383 si->err_type = SI_ET_CONN_OTHER;
384 si->err_loc = s->srv;
385 }
386
387 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100388 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100389 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200390 s->srv->counters.failed_conns++;
391 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100392
393 /* release other sessions waiting for this server */
394 if (may_dequeue_tasks(s->srv, s->be))
395 process_srv_queue(s->srv);
396
397 /* Failed and not retryable. */
398 si->shutr(si);
399 si->shutw(si);
400 si->ob->flags |= BF_WRITE_ERROR;
401
402 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
403
404 /* no session was ever accounted for this server */
405 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100406 if (s->srv_error)
407 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100408 return;
409 }
410
411 /* We are facing a retryable error, but we don't want to run a
412 * turn-around now, as the problem is likely a source port
413 * allocation problem, so we want to retry now.
414 */
415 si->state = SI_ST_CER;
416 si->flags &= ~SI_FL_ERR;
417 sess_update_st_cer(s, si);
418 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
419 return;
420 }
421 else if (si->state == SI_ST_QUE) {
422 /* connection request was queued, check for any update */
423 if (!s->pend_pos) {
424 /* The connection is not in the queue anymore. Either
425 * we have a server connection slot available and we
426 * go directly to the assigned state, or we need to
427 * load-balance first and go to the INI state.
428 */
429 si->exp = TICK_ETERNITY;
430 if (unlikely(!(s->flags & SN_ASSIGNED)))
431 si->state = SI_ST_REQ;
432 else {
433 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
434 si->state = SI_ST_ASS;
435 }
436 return;
437 }
438
439 /* Connection request still in queue... */
440 if (si->flags & SI_FL_EXP) {
441 /* ... and timeout expired */
442 si->exp = TICK_ETERNITY;
443 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
444 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200445 s->srv->counters.failed_conns++;
446 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100447 si->shutr(si);
448 si->shutw(si);
449 si->ob->flags |= BF_WRITE_TIMEOUT;
450 if (!si->err_type)
451 si->err_type = SI_ET_QUEUE_TO;
452 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100453 if (s->srv_error)
454 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100455 return;
456 }
457
458 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200459 if ((si->ob->flags & (BF_READ_ERROR)) ||
460 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200461 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100462 /* give up */
463 si->exp = TICK_ETERNITY;
464 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
465 si->shutr(si);
466 si->shutw(si);
467 si->err_type |= SI_ET_QUEUE_ABRT;
468 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100469 if (s->srv_error)
470 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100471 return;
472 }
473
474 /* Nothing changed */
475 return;
476 }
477 else if (si->state == SI_ST_TAR) {
478 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200479 if ((si->ob->flags & (BF_READ_ERROR)) ||
480 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200481 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100482 /* give up */
483 si->exp = TICK_ETERNITY;
484 si->shutr(si);
485 si->shutw(si);
486 si->err_type |= SI_ET_CONN_ABRT;
487 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100488 if (s->srv_error)
489 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100490 return;
491 }
492
493 if (!(si->flags & SI_FL_EXP))
494 return; /* still in turn-around */
495
496 si->exp = TICK_ETERNITY;
497
498 /* we keep trying on the same server as long as the session is
499 * marked "assigned".
500 * FIXME: Should we force a redispatch attempt when the server is down ?
501 */
502 if (s->flags & SN_ASSIGNED)
503 si->state = SI_ST_ASS;
504 else
505 si->state = SI_ST_REQ;
506 return;
507 }
508}
509
510/* This function initiates a server connection request on a stream interface
511 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
512 * indicating that a server has been assigned. It may also return SI_ST_QUE,
513 * or SI_ST_CLO upon error.
514 */
515static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
516 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",
517 now_ms, __FUNCTION__,
518 s,
519 s->req, s->rep,
520 s->req->rex, s->rep->wex,
521 s->req->flags, s->rep->flags,
522 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
523
524 if (si->state != SI_ST_REQ)
525 return;
526
527 /* Try to assign a server */
528 if (srv_redispatch_connect(s) != 0) {
529 /* We did not get a server. Either we queued the
530 * connection request, or we encountered an error.
531 */
532 if (si->state == SI_ST_QUE)
533 return;
534
535 /* we did not get any server, let's check the cause */
536 si->shutr(si);
537 si->shutw(si);
538 si->ob->flags |= BF_WRITE_ERROR;
539 if (!si->err_type)
540 si->err_type = SI_ET_CONN_OTHER;
541 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100542 if (s->srv_error)
543 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 return;
545 }
546
547 /* The server is assigned */
548 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
549 si->state = SI_ST_ASS;
550}
551
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200552/* This stream analyser checks the switching rules and changes the backend
553 * if appropriate. The default_backend rule is also considered.
554 * It returns 1 if the processing can continue on next analysers, or zero if it
555 * either needs more data or wants to immediately abort the request.
556 */
557int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
558{
559 req->analysers &= ~an_bit;
560 req->analyse_exp = TICK_ETERNITY;
561
562 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
563 now_ms, __FUNCTION__,
564 s,
565 req,
566 req->rex, req->wex,
567 req->flags,
568 req->l,
569 req->analysers);
570
571 /* now check whether we have some switching rules for this request */
572 if (!(s->flags & SN_BE_ASSIGNED)) {
573 struct switching_rule *rule;
574
575 list_for_each_entry(rule, &s->fe->switching_rules, list) {
576 int ret;
577
578 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
579 ret = acl_pass(ret);
580 if (rule->cond->pol == ACL_COND_UNLESS)
581 ret = !ret;
582
583 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200584 if (!session_set_backend(s, rule->be.backend))
585 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200586 break;
587 }
588 }
589
590 /* To ensure correct connection accounting on the backend, we
591 * have to assign one if it was not set (eg: a listen). This
592 * measure also takes care of correctly setting the default
593 * backend if any.
594 */
595 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200596 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
597 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200598 }
599
600 /* we don't want to run the HTTP filters again if the backend has not changed */
601 if (s->fe == s->be)
602 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
603
604 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200605
606 sw_failed:
607 /* immediately abort this request in case of allocation failure */
608 buffer_abort(s->req);
609 buffer_abort(s->rep);
610
611 if (!(s->flags & SN_ERR_MASK))
612 s->flags |= SN_ERR_RESOURCE;
613 if (!(s->flags & SN_FINST_MASK))
614 s->flags |= SN_FINST_R;
615
616 s->txn.status = 500;
617 s->req->analysers = 0;
618 s->req->analyse_exp = TICK_ETERNITY;
619 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200620}
621
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622/* Processes the client, server, request and response jobs of a session task,
623 * then puts it back to the wait queue in a clean state, or cleans up its
624 * resources if it must be deleted. Returns in <next> the date the task wants
625 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
626 * nothing too many times, the request and response buffers flags are monitored
627 * and each function is called only if at least another function has changed at
628 * least one flag it is interested in.
629 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100630struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100631{
632 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 unsigned int rqf_last, rpf_last;
634
635 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
636 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
637
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200638 /* This flag must explicitly be set every time */
639 s->req->flags &= ~BF_READ_NOEXP;
640
641 /* Keep a copy of req/rep flags so that we can detect shutdowns */
642 rqf_last = s->req->flags;
643 rpf_last = s->rep->flags;
644
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200645 /* we don't want the stream interface functions to recursively wake us up */
646 if (s->req->prod->owner == t)
647 s->req->prod->flags |= SI_FL_DONT_WAKE;
648 if (s->req->cons->owner == t)
649 s->req->cons->flags |= SI_FL_DONT_WAKE;
650
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651 /* 1a: Check for low level timeouts if needed. We just set a flag on
652 * stream interfaces when their timeouts have expired.
653 */
654 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
655 stream_int_check_timeouts(&s->si[0]);
656 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200657
658 /* check buffer timeouts, and close the corresponding stream interfaces
659 * for future reads or writes. Note: this will also concern upper layers
660 * but we do not touch any other flag. We must be careful and correctly
661 * detect state changes when calling them.
662 */
663
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100664 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200665
666 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
667 s->req->prod->shutr(s->req->prod);
668
669 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
670 s->req->cons->shutw(s->req->cons);
671
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200674 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
675 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100676
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200677 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
678 s->rep->cons->shutw(s->rep->cons);
679 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680
681 /* 1b: check for low-level errors reported at the stream interface.
682 * First we check if it's a retryable error (in which case we don't
683 * want to tell the buffer). Otherwise we report the error one level
684 * upper by setting flags into the buffers. Note that the side towards
685 * the client cannot have connect (hence retryable) errors. Also, the
686 * connection setup code must be able to deal with any type of abort.
687 */
688 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
689 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
690 s->si[0].shutr(&s->si[0]);
691 s->si[0].shutw(&s->si[0]);
692 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100693 if (!(s->req->analysers) && !(s->rep->analysers)) {
694 if (!(s->flags & SN_ERR_MASK))
695 s->flags |= SN_ERR_CLICL;
696 if (!(s->flags & SN_FINST_MASK))
697 s->flags |= SN_FINST_D;
698 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 }
700 }
701
702 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
703 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
704 s->si[1].shutr(&s->si[1]);
705 s->si[1].shutw(&s->si[1]);
706 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200707 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200709 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100710 if (!(s->req->analysers) && !(s->rep->analysers)) {
711 if (!(s->flags & SN_ERR_MASK))
712 s->flags |= SN_ERR_SRVCL;
713 if (!(s->flags & SN_FINST_MASK))
714 s->flags |= SN_FINST_D;
715 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100716 }
717 /* note: maybe we should process connection errors here ? */
718 }
719
720 if (s->si[1].state == SI_ST_CON) {
721 /* we were trying to establish a connection on the server side,
722 * maybe it succeeded, maybe it failed, maybe we timed out, ...
723 */
724 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
725 sess_update_st_cer(s, &s->si[1]);
726 else if (s->si[1].state == SI_ST_EST)
727 sess_establish(s, &s->si[1]);
728
729 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
730 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
731 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
732 */
733 }
734
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200735resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 /* Check for connection closure */
737
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 DPRINTF(stderr,
739 "[%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",
740 now_ms, __FUNCTION__, __LINE__,
741 t,
742 s, s->flags,
743 s->req, s->rep,
744 s->req->rex, s->rep->wex,
745 s->req->flags, s->rep->flags,
746 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
747 s->rep->cons->err_type, s->req->cons->err_type,
748 s->conn_retries);
749
750 /* nothing special to be done on client side */
751 if (unlikely(s->req->prod->state == SI_ST_DIS))
752 s->req->prod->state = SI_ST_CLO;
753
754 /* When a server-side connection is released, we have to count it and
755 * check for pending connections on this server.
756 */
757 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
758 s->req->cons->state = SI_ST_CLO;
759 if (s->srv) {
760 if (s->flags & SN_CURR_SESS) {
761 s->flags &= ~SN_CURR_SESS;
762 s->srv->cur_sess--;
763 }
764 sess_change_server(s, NULL);
765 if (may_dequeue_tasks(s->srv, s->be))
766 process_srv_queue(s->srv);
767 }
768 }
769
770 /*
771 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
772 * at this point.
773 */
774
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100775 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776 /* Analyse request */
777 if ((s->req->flags & BF_MASK_ANALYSER) ||
778 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
779 unsigned int flags = s->req->flags;
780
781 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200782 unsigned int last_ana = 0;
783
Willy Tarreau520d95e2009-09-19 21:04:57 +0200784 /* it's up to the analysers to stop new connections */
785 buffer_auto_connect(s->req);
786 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100787
788 /* We will call all analysers for which a bit is set in
789 * s->req->analysers, following the bit order from LSB
790 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200791 * the list when not needed. Any analyser may return 0
792 * to break out of the loop, either because of missing
793 * data to take a decision, or because it decides to
794 * kill the session. We loop at least once through each
795 * analyser, and we may loop again if other analysers
796 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100797 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200798 while (s->req->analysers & ~last_ana) {
799 last_ana = s->req->analysers;
800
801 if (s->req->analysers & AN_REQ_INSPECT) {
802 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200803 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200805 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100806
Willy Tarreaud787e662009-07-07 10:14:51 +0200807 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
808 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200809 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200810 break;
811 }
812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200813 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
814 last_ana |= AN_REQ_HTTP_PROCESS_FE;
815 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
816 break;
817 }
818
819 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
820 last_ana |= AN_REQ_SWITCHING_RULES;
821 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
822 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200823 /* FIXME: we mait switch from TCP to HTTP and want to
824 * immediately loop back to the top. This is a dirty way
825 * of doing it, and we should find a cleaner method relying
826 * on a circular list of function pointers.
827 */
828 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
829 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200830 }
831
832 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
833 last_ana |= AN_REQ_HTTP_PROCESS_BE;
834 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
835 break;
836 }
837
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200838 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
839 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200840 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100841 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200842 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100843
Willy Tarreauc465fd72009-08-31 00:17:18 +0200844 if (s->req->analysers & AN_REQ_HTTP_INNER) {
845 last_ana |= AN_REQ_HTTP_INNER;
846 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
847 break;
848 }
849
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200850 if (s->req->analysers & AN_REQ_HTTP_BODY) {
851 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200852 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100853 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200854 }
Emeric Brun647caf12009-06-30 17:57:00 +0200855
856 if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) {
857 last_ana |= AN_REQ_PRST_RDP_COOKIE;
858 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
859 break;
860 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100861 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 }
Willy Tarreau84455332009-03-15 22:34:05 +0100863
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200864 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
865 rqf_last = s->req->flags;
866 goto resync_request;
867 }
868 }
869
870 resync_response:
871 /* Analyse response */
872
873 if (unlikely(s->rep->flags & BF_HIJACK)) {
874 /* In inject mode, we wake up everytime something has
875 * happened on the write side of the buffer.
876 */
877 unsigned int flags = s->rep->flags;
878
879 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
880 !(s->rep->flags & BF_FULL)) {
881 s->rep->hijacker(s, s->rep);
882 }
883
884 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
885 rpf_last = s->rep->flags;
886 goto resync_response;
887 }
888 }
889 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
890 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
891 unsigned int flags = s->rep->flags;
892
893 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200894 /* it's up to the analysers to reset auto_close */
895 buffer_auto_close(s->rep);
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200896 if (s->rep->analysers)
897 process_response(s);
898 }
899
900 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
901 rpf_last = s->rep->flags;
902 goto resync_response;
903 }
904 }
905
906 /* FIXME: here we should call protocol handlers which rely on
907 * both buffers.
908 */
909
910
911 /*
912 * Now we propagate unhandled errors to the session
913 */
914 if (!(s->flags & SN_ERR_MASK)) {
915 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
916 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100917 s->req->analysers = 0;
918 if (s->req->flags & BF_READ_ERROR)
919 s->flags |= SN_ERR_CLICL;
920 else if (s->req->flags & BF_READ_TIMEOUT)
921 s->flags |= SN_ERR_CLITO;
922 else if (s->req->flags & BF_WRITE_ERROR)
923 s->flags |= SN_ERR_SRVCL;
924 else
925 s->flags |= SN_ERR_SRVTO;
926 sess_set_term_flags(s);
927 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200928 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
929 /* Report it if the server got an error or a read timeout expired */
930 s->rep->analysers = 0;
931 if (s->rep->flags & BF_READ_ERROR)
932 s->flags |= SN_ERR_SRVCL;
933 else if (s->rep->flags & BF_READ_TIMEOUT)
934 s->flags |= SN_ERR_SRVTO;
935 else if (s->rep->flags & BF_WRITE_ERROR)
936 s->flags |= SN_ERR_CLICL;
937 else
Willy Tarreau84455332009-03-15 22:34:05 +0100938 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200939 sess_set_term_flags(s);
940 }
Willy Tarreau84455332009-03-15 22:34:05 +0100941 }
942
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200943 /*
944 * Here we take care of forwarding unhandled data. This also includes
945 * connection establishments and shutdown requests.
946 */
947
948
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100949 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +0200950 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100951 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100952 if (!s->req->analysers &&
953 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +0200954 (s->req->prod->state >= SI_ST_EST) &&
955 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100956 /* This buffer is freewheeling, there's no analyser nor hijacker
957 * attached to it. If any data are left in, we'll permit them to
958 * move.
959 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200960 buffer_auto_connect(s->req);
961 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100962 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100963
Willy Tarreau31971e52009-09-20 12:07:52 +0200964 /* If the producer is still connected, we'll enable data to flow
965 * from the producer to the consumer (which might possibly not be
966 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100967 */
Willy Tarreau31971e52009-09-20 12:07:52 +0200968 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
969 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100970 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100971
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100972 /* check if it is wise to enable kernel splicing to forward request data */
973 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
974 s->req->to_forward &&
975 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200976 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100977 (pipes_used < global.maxpipes) &&
978 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
979 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
980 (s->req->flags & BF_STREAMER_FAST)))) {
981 s->req->flags |= BF_KERN_SPLICING;
982 }
983
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100984 /* reflect what the L7 analysers have seen last */
985 rqf_last = s->req->flags;
986
987 /*
988 * Now forward all shutdown requests between both sides of the buffer
989 */
990
Willy Tarreau520d95e2009-09-19 21:04:57 +0200991 /* first, let's check if the request buffer needs to shutdown(write), which may
992 * happen either because the input is closed or because we want to force a close
993 * once the server has begun to respond.
994 */
995 if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE)) == BF_AUTO_CLOSE) {
996 if (unlikely((s->req->flags & BF_SHUTR) ||
997 ((s->req->cons->state == SI_ST_EST) &&
998 (s->be->options & PR_O_FORCE_CLO) &&
999 (s->rep->flags & BF_READ_ACTIVITY))))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001000 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001001 }
1002
1003 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001004 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 +01001005 s->req->cons->shutw(s->req->cons);
1006
1007 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001008 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1009 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010 buffer_shutr_now(s->req);
1011
1012 /* shutdown(read) pending */
1013 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1014 s->req->prod->shutr(s->req->prod);
1015
Willy Tarreau520d95e2009-09-19 21:04:57 +02001016 /* it's possible that an upper layer has requested a connection setup or abort.
1017 * There are 2 situations where we decide to establish a new connection :
1018 * - there are data scheduled for emission in the buffer
1019 * - the BF_AUTO_CONNECT flag is set (active connection)
1020 */
1021 if (s->req->cons->state == SI_ST_INI) {
1022 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001023 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001024 /* If we have a ->connect method, we need to perform a connection request,
1025 * otherwise we immediately switch to the connected state.
1026 */
1027 if (s->req->cons->connect)
1028 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1029 else
1030 s->req->cons->state = SI_ST_EST; /* connection established */
1031 }
Willy Tarreau73201222009-08-16 18:27:24 +02001032 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001033 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001034 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001035 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1036 buffer_shutr_now(s->rep);
1037 }
Willy Tarreau92795622009-03-06 12:51:23 +01001038 }
1039
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040
1041 /* we may have a pending connection request, or a connection waiting
1042 * for completion.
1043 */
1044 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1045 do {
1046 /* nb: step 1 might switch from QUE to ASS, but we first want
1047 * to give a chance to step 2 to perform a redirect if needed.
1048 */
1049 if (s->si[1].state != SI_ST_REQ)
1050 sess_update_stream_int(s, &s->si[1]);
1051 if (s->si[1].state == SI_ST_REQ)
1052 sess_prepare_conn_req(s, &s->si[1]);
1053
1054 if (s->si[1].state == SI_ST_ASS && s->srv &&
1055 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1056 perform_http_redirect(s, &s->si[1]);
1057 } while (s->si[1].state == SI_ST_ASS);
1058 }
1059
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001060 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001061 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001062 goto resync_stream_interface;
1063
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001064 /* otherwise wewant to check if we need to resync the req buffer or not */
1065 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001066 goto resync_request;
1067
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001068 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001069
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001070 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001071 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001072 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001073 if (!s->rep->analysers &&
1074 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001075 (s->rep->prod->state >= SI_ST_EST) &&
1076 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001077 /* This buffer is freewheeling, there's no analyser nor hijacker
1078 * attached to it. If any data are left in, we'll permit them to
1079 * move.
1080 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001081 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001082 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001083 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1084 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001085 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001086
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001087 /* check if it is wise to enable kernel splicing to forward response data */
1088 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1089 s->rep->to_forward &&
1090 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001091 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001092 (pipes_used < global.maxpipes) &&
1093 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1094 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1095 (s->rep->flags & BF_STREAMER_FAST)))) {
1096 s->rep->flags |= BF_KERN_SPLICING;
1097 }
1098
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001099 /* reflect what the L7 analysers have seen last */
1100 rpf_last = s->rep->flags;
1101
1102 /*
1103 * Now forward all shutdown requests between both sides of the buffer
1104 */
1105
1106 /*
1107 * FIXME: this is probably where we should produce error responses.
1108 */
1109
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001110 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001111 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1112 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001113 buffer_shutw_now(s->rep);
1114
1115 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001116 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 +01001117 s->rep->cons->shutw(s->rep->cons);
1118
1119 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001120 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1121 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001122 buffer_shutr_now(s->rep);
1123
1124 /* shutdown(read) pending */
1125 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1126 s->rep->prod->shutr(s->rep->prod);
1127
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001128 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129 goto resync_stream_interface;
1130
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001131 if (s->req->flags != rqf_last)
1132 goto resync_request;
1133
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001134 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001135 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001136
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001137 /* we're interested in getting wakeups again */
1138 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1139 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1140
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001141 /* This is needed only when debugging is enabled, to indicate
1142 * client-side or server-side close. Please note that in the unlikely
1143 * event where both sides would close at once, the sequence is reported
1144 * on the server side first.
1145 */
1146 if (unlikely((global.mode & MODE_DEBUG) &&
1147 (!(global.mode & MODE_QUIET) ||
1148 (global.mode & MODE_VERBOSE)))) {
1149 int len;
1150
1151 if (s->si[1].state == SI_ST_CLO &&
1152 s->si[1].prev_state == SI_ST_EST) {
1153 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1154 s->uniq_id, s->be->id,
1155 (unsigned short)s->si[0].fd,
1156 (unsigned short)s->si[1].fd);
1157 write(1, trash, len);
1158 }
1159
1160 if (s->si[0].state == SI_ST_CLO &&
1161 s->si[0].prev_state == SI_ST_EST) {
1162 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1163 s->uniq_id, s->be->id,
1164 (unsigned short)s->si[0].fd,
1165 (unsigned short)s->si[1].fd);
1166 write(1, trash, len);
1167 }
1168 }
1169
1170 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1171 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1172
1173 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1174 session_process_counters(s);
1175
Willy Tarreau1accfc02009-09-05 20:57:35 +02001176 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001177 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001178
Willy Tarreau1accfc02009-09-05 20:57:35 +02001179 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001180 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001181
Willy Tarreauea388542009-06-21 21:45:58 +02001182 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1183 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001184 s->si[0].prev_state = s->si[0].state;
1185 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001186 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1187 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001188
1189 /* Trick: if a request is being waiting for the server to respond,
1190 * and if we know the server can timeout, we don't want the timeout
1191 * to expire on the client side first, but we're still interested
1192 * in passing data from the client to the server (eg: POST). Thus,
1193 * we can cancel the client's request timeout if the server's
1194 * request timeout is set and the server has not yet sent a response.
1195 */
1196
Willy Tarreau520d95e2009-09-19 21:04:57 +02001197 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001198 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1199 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001200 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001201 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001202
Willy Tarreau1accfc02009-09-05 20:57:35 +02001203 /* Call the second stream interface's I/O handler if it's embedded.
1204 * Note that this one may wake the task up again.
1205 */
1206 if (s->req->cons->iohandler) {
1207 s->req->cons->iohandler(s->req->cons);
1208 if (task_in_rq(t)) {
1209 /* If we woke up, we don't want to requeue the
1210 * task to the wait queue, but rather requeue
1211 * it into the runqueue ASAP.
1212 */
1213 t->expire = TICK_ETERNITY;
1214 return t;
1215 }
1216 }
1217
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001218 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1219 tick_first(s->rep->rex, s->rep->wex));
1220 if (s->req->analysers)
1221 t->expire = tick_first(t->expire, s->req->analyse_exp);
1222
1223 if (s->si[0].exp)
1224 t->expire = tick_first(t->expire, s->si[0].exp);
1225
1226 if (s->si[1].exp)
1227 t->expire = tick_first(t->expire, s->si[1].exp);
1228
1229#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001230 fprintf(stderr,
1231 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1232 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1233 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1234 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 +01001235#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001236
1237#ifdef DEBUG_DEV
1238 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001239 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001240 ABORT_NOW();
1241#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001242 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001243 }
1244
1245 s->fe->feconn--;
1246 if (s->flags & SN_BE_ASSIGNED)
1247 s->be->beconn--;
1248 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001249 s->listener->nbconn--;
1250 if (s->listener->state == LI_FULL &&
1251 s->listener->nbconn < s->listener->maxconn) {
1252 /* we should reactivate the listener */
1253 EV_FD_SET(s->listener->fd, DIR_RD);
1254 s->listener->state = LI_READY;
1255 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001256
1257 if (unlikely((global.mode & MODE_DEBUG) &&
1258 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1259 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001260 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001261 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001262 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001263 write(1, trash, len);
1264 }
1265
1266 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1267 session_process_counters(s);
1268
1269 /* let's do a final log if we need it */
1270 if (s->logs.logwait &&
1271 !(s->flags & SN_MONITOR) &&
1272 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001273 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001274 }
1275
1276 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001277 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001278 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001279 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001280 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001281}
1282
Willy Tarreau7c669d72008-06-20 15:04:11 +02001283/*
1284 * This function adjusts sess->srv_conn and maintains the previous and new
1285 * server's served session counts. Setting newsrv to NULL is enough to release
1286 * current connection slot. This function also notifies any LB algo which might
1287 * expect to be informed about any change in the number of active sessions on a
1288 * server.
1289 */
1290void sess_change_server(struct session *sess, struct server *newsrv)
1291{
1292 if (sess->srv_conn == newsrv)
1293 return;
1294
1295 if (sess->srv_conn) {
1296 sess->srv_conn->served--;
1297 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1298 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1299 sess->srv_conn = NULL;
1300 }
1301
1302 if (newsrv) {
1303 newsrv->served++;
1304 if (newsrv->proxy->lbprm.server_take_conn)
1305 newsrv->proxy->lbprm.server_take_conn(newsrv);
1306 sess->srv_conn = newsrv;
1307 }
1308}
1309
Willy Tarreau84455332009-03-15 22:34:05 +01001310/* Set correct session termination flags in case no analyser has done it. It
1311 * also counts a failed request if the server state has not reached the request
1312 * stage.
1313 */
1314void sess_set_term_flags(struct session *s)
1315{
1316 if (!(s->flags & SN_FINST_MASK)) {
1317 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001318
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001319 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001320 if (s->listener->counters)
1321 s->listener->counters->failed_req++;
1322
Willy Tarreau84455332009-03-15 22:34:05 +01001323 s->flags |= SN_FINST_R;
1324 }
1325 else if (s->si[1].state == SI_ST_QUE)
1326 s->flags |= SN_FINST_Q;
1327 else if (s->si[1].state < SI_ST_EST)
1328 s->flags |= SN_FINST_C;
1329 else if (s->si[1].state == SI_ST_EST)
1330 s->flags |= SN_FINST_D;
1331 else
1332 s->flags |= SN_FINST_L;
1333 }
1334}
1335
1336/* Handle server-side errors for default protocols. It is called whenever a a
1337 * connection setup is aborted or a request is aborted in queue. It sets the
1338 * session termination flags so that the caller does not have to worry about
1339 * them. It's installed as ->srv_error for the server-side stream_interface.
1340 */
1341void default_srv_error(struct session *s, struct stream_interface *si)
1342{
1343 int err_type = si->err_type;
1344 int err = 0, fin = 0;
1345
1346 if (err_type & SI_ET_QUEUE_ABRT) {
1347 err = SN_ERR_CLICL;
1348 fin = SN_FINST_Q;
1349 }
1350 else if (err_type & SI_ET_CONN_ABRT) {
1351 err = SN_ERR_CLICL;
1352 fin = SN_FINST_C;
1353 }
1354 else if (err_type & SI_ET_QUEUE_TO) {
1355 err = SN_ERR_SRVTO;
1356 fin = SN_FINST_Q;
1357 }
1358 else if (err_type & SI_ET_QUEUE_ERR) {
1359 err = SN_ERR_SRVCL;
1360 fin = SN_FINST_Q;
1361 }
1362 else if (err_type & SI_ET_CONN_TO) {
1363 err = SN_ERR_SRVTO;
1364 fin = SN_FINST_C;
1365 }
1366 else if (err_type & SI_ET_CONN_ERR) {
1367 err = SN_ERR_SRVCL;
1368 fin = SN_FINST_C;
1369 }
1370 else /* SI_ET_CONN_OTHER and others */ {
1371 err = SN_ERR_INTERNAL;
1372 fin = SN_FINST_C;
1373 }
1374
1375 if (!(s->flags & SN_ERR_MASK))
1376 s->flags |= err;
1377 if (!(s->flags & SN_FINST_MASK))
1378 s->flags |= fin;
1379}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001380
Willy Tarreaubaaee002006-06-26 02:48:02 +02001381/*
1382 * Local variables:
1383 * c-indent-level: 8
1384 * c-basic-offset: 8
1385 * End:
1386 */