blob: e359eb1aa0ba946b72bb58b8b1d2d1f37cc945da [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) {
142 s->fe->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)
145 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->srv)
148 s->srv->bytes_in += bytes;
149 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100150 }
151
Willy Tarreau30e71012007-11-26 20:15:35 +0100152 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100153 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100154 s->logs.bytes_out = s->rep->total;
155 if (bytes) {
156 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100157
Willy Tarreau30e71012007-11-26 20:15:35 +0100158 if (s->be != s->fe)
159 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100160
Willy Tarreau30e71012007-11-26 20:15:35 +0100161 if (s->srv)
162 s->srv->bytes_out += bytes;
163 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100164 }
165}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100167/* This function is called with (si->state == SI_ST_CON) meaning that a
168 * connection was attempted and that the file descriptor is already allocated.
169 * We must check for establishment, error and abort. Possible output states
170 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
171 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
172 * otherwise 1.
173 */
174int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
175{
176 struct buffer *req = si->ob;
177 struct buffer *rep = si->ib;
178
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100179 /* If we got an error, or if nothing happened and the connection timed
180 * out, we must give up. The CER state handler will take care of retry
181 * attempts and error reports.
182 */
183 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100184 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100185 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200186 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100187 fd_delete(si->fd);
188
189 if (si->err_type)
190 return 0;
191
192 si->err_loc = s->srv;
193 if (si->flags & SI_FL_ERR)
194 si->err_type = SI_ET_CONN_ERR;
195 else
196 si->err_type = SI_ET_CONN_TO;
197 return 0;
198 }
199
200 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200201 if (unlikely((rep->flags & BF_SHUTW) ||
202 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200203 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100204 s->be->options & PR_O_ABRT_CLOSE)))) {
205 /* give up */
206 si->shutw(si);
207 si->err_type |= SI_ET_CONN_ABRT;
208 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200209 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100210 if (s->srv_error)
211 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100212 return 1;
213 }
214
215 /* we need to wait a bit more if there was no activity either */
216 if (!(req->flags & BF_WRITE_ACTIVITY))
217 return 1;
218
219 /* OK, this means that a connection succeeded. The caller will be
220 * responsible for handling the transition from CON to EST.
221 */
222 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100223 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100224 si->state = SI_ST_EST;
225 si->err_type = SI_ET_NONE;
226 si->err_loc = NULL;
227 return 1;
228}
229
230/* This function is called with (si->state == SI_ST_CER) meaning that a
231 * previous connection attempt has failed and that the file descriptor
232 * has already been released. Possible causes include asynchronous error
233 * notification and time out. Possible output states are SI_ST_CLO when
234 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
235 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
236 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
237 * marked as in error state. It returns 0.
238 */
239int sess_update_st_cer(struct session *s, struct stream_interface *si)
240{
241 /* we probably have to release last session from the server */
242 if (s->srv) {
243 if (s->flags & SN_CURR_SESS) {
244 s->flags &= ~SN_CURR_SESS;
245 s->srv->cur_sess--;
246 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100247 }
248
249 /* ensure that we have enough retries left */
250 s->conn_retries--;
251 if (s->conn_retries < 0) {
252 if (!si->err_type) {
253 si->err_type = SI_ET_CONN_ERR;
254 si->err_loc = s->srv;
255 }
256
257 if (s->srv)
258 s->srv->failed_conns++;
259 s->be->failed_conns++;
260 if (may_dequeue_tasks(s->srv, s->be))
261 process_srv_queue(s->srv);
262
263 /* shutw is enough so stop a connecting socket */
264 si->shutw(si);
265 si->ob->flags |= BF_WRITE_ERROR;
266 si->ib->flags |= BF_READ_ERROR;
267
268 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100269 if (s->srv_error)
270 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100271 return 0;
272 }
273
274 /* If the "redispatch" option is set on the backend, we are allowed to
275 * retry on another server for the last retry. In order to achieve this,
276 * we must mark the session unassigned, and eventually clear the DIRECT
277 * bit to ignore any persistence cookie. We won't count a retry nor a
278 * redispatch yet, because this will depend on what server is selected.
279 */
280 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
281 if (may_dequeue_tasks(s->srv, s->be))
282 process_srv_queue(s->srv);
283
284 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
285 s->prev_srv = s->srv;
286 si->state = SI_ST_REQ;
287 } else {
288 if (s->srv)
289 s->srv->retries++;
290 s->be->retries++;
291 si->state = SI_ST_ASS;
292 }
293
294 if (si->flags & SI_FL_ERR) {
295 /* The error was an asynchronous connection error, and we will
296 * likely have to retry connecting to the same server, most
297 * likely leading to the same result. To avoid this, we wait
298 * one second before retrying.
299 */
300
301 if (!si->err_type)
302 si->err_type = SI_ET_CONN_ERR;
303
304 si->state = SI_ST_TAR;
305 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
306 return 0;
307 }
308 return 0;
309}
310
311/*
312 * This function handles the transition between the SI_ST_CON state and the
313 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
314 * SI_ST_EST.
315 */
316void sess_establish(struct session *s, struct stream_interface *si)
317{
318 struct buffer *req = si->ob;
319 struct buffer *rep = si->ib;
320
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100321 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200322 buffer_set_rlim(rep, rep->size); /* no rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100323
324 /* if the user wants to log as soon as possible, without counting
325 * bytes from the server, then this is the right moment. */
326 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
327 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100328 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100329 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100330 }
331 else {
Willy Tarreau27a674e2009-08-17 07:23:33 +0200332 buffer_set_rlim(rep, req->size - global.tune.maxrewrite); /* rewrite needed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100333 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
334 /* reset hdr_idx which was already initialized by the request.
335 * right now, the http parser does it.
336 * hdr_idx_init(&s->txn.hdr_idx);
337 */
338 }
339
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200340 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100341 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
342 req->wex = TICK_ETERNITY;
343}
344
345/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
346 * Other input states are simply ignored.
347 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
348 * Flags must have previously been updated for timeouts and other conditions.
349 */
350void sess_update_stream_int(struct session *s, struct stream_interface *si)
351{
352 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
353 now_ms, __FUNCTION__,
354 s,
355 s->req, s->rep,
356 s->req->rex, s->rep->wex,
357 s->req->flags, s->rep->flags,
358 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
359
360 if (si->state == SI_ST_ASS) {
361 /* Server assigned to connection request, we have to try to connect now */
362 int conn_err;
363
364 conn_err = connect_server(s);
365 if (conn_err == SN_ERR_NONE) {
366 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100367 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100368 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100369 return;
370 }
371
372 /* We have received a synchronous error. We might have to
373 * abort, retry immediately or redispatch.
374 */
375 if (conn_err == SN_ERR_INTERNAL) {
376 if (!si->err_type) {
377 si->err_type = SI_ET_CONN_OTHER;
378 si->err_loc = s->srv;
379 }
380
381 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100382 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100383 if (s->srv)
384 s->srv->failed_conns++;
385 s->be->failed_conns++;
386
387 /* release other sessions waiting for this server */
388 if (may_dequeue_tasks(s->srv, s->be))
389 process_srv_queue(s->srv);
390
391 /* Failed and not retryable. */
392 si->shutr(si);
393 si->shutw(si);
394 si->ob->flags |= BF_WRITE_ERROR;
395
396 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
397
398 /* no session was ever accounted for this server */
399 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100400 if (s->srv_error)
401 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100402 return;
403 }
404
405 /* We are facing a retryable error, but we don't want to run a
406 * turn-around now, as the problem is likely a source port
407 * allocation problem, so we want to retry now.
408 */
409 si->state = SI_ST_CER;
410 si->flags &= ~SI_FL_ERR;
411 sess_update_st_cer(s, si);
412 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
413 return;
414 }
415 else if (si->state == SI_ST_QUE) {
416 /* connection request was queued, check for any update */
417 if (!s->pend_pos) {
418 /* The connection is not in the queue anymore. Either
419 * we have a server connection slot available and we
420 * go directly to the assigned state, or we need to
421 * load-balance first and go to the INI state.
422 */
423 si->exp = TICK_ETERNITY;
424 if (unlikely(!(s->flags & SN_ASSIGNED)))
425 si->state = SI_ST_REQ;
426 else {
427 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
428 si->state = SI_ST_ASS;
429 }
430 return;
431 }
432
433 /* Connection request still in queue... */
434 if (si->flags & SI_FL_EXP) {
435 /* ... and timeout expired */
436 si->exp = TICK_ETERNITY;
437 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
438 if (s->srv)
439 s->srv->failed_conns++;
440 s->be->failed_conns++;
441 si->shutr(si);
442 si->shutw(si);
443 si->ob->flags |= BF_WRITE_TIMEOUT;
444 if (!si->err_type)
445 si->err_type = SI_ET_QUEUE_TO;
446 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100447 if (s->srv_error)
448 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100449 return;
450 }
451
452 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200453 if ((si->ob->flags & (BF_READ_ERROR)) ||
454 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200455 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100456 /* give up */
457 si->exp = TICK_ETERNITY;
458 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
459 si->shutr(si);
460 si->shutw(si);
461 si->err_type |= SI_ET_QUEUE_ABRT;
462 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100463 if (s->srv_error)
464 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100465 return;
466 }
467
468 /* Nothing changed */
469 return;
470 }
471 else if (si->state == SI_ST_TAR) {
472 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200473 if ((si->ob->flags & (BF_READ_ERROR)) ||
474 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200475 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100476 /* give up */
477 si->exp = TICK_ETERNITY;
478 si->shutr(si);
479 si->shutw(si);
480 si->err_type |= SI_ET_CONN_ABRT;
481 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100482 if (s->srv_error)
483 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100484 return;
485 }
486
487 if (!(si->flags & SI_FL_EXP))
488 return; /* still in turn-around */
489
490 si->exp = TICK_ETERNITY;
491
492 /* we keep trying on the same server as long as the session is
493 * marked "assigned".
494 * FIXME: Should we force a redispatch attempt when the server is down ?
495 */
496 if (s->flags & SN_ASSIGNED)
497 si->state = SI_ST_ASS;
498 else
499 si->state = SI_ST_REQ;
500 return;
501 }
502}
503
504/* This function initiates a server connection request on a stream interface
505 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
506 * indicating that a server has been assigned. It may also return SI_ST_QUE,
507 * or SI_ST_CLO upon error.
508 */
509static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
510 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
511 now_ms, __FUNCTION__,
512 s,
513 s->req, s->rep,
514 s->req->rex, s->rep->wex,
515 s->req->flags, s->rep->flags,
516 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
517
518 if (si->state != SI_ST_REQ)
519 return;
520
521 /* Try to assign a server */
522 if (srv_redispatch_connect(s) != 0) {
523 /* We did not get a server. Either we queued the
524 * connection request, or we encountered an error.
525 */
526 if (si->state == SI_ST_QUE)
527 return;
528
529 /* we did not get any server, let's check the cause */
530 si->shutr(si);
531 si->shutw(si);
532 si->ob->flags |= BF_WRITE_ERROR;
533 if (!si->err_type)
534 si->err_type = SI_ET_CONN_OTHER;
535 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100536 if (s->srv_error)
537 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538 return;
539 }
540
541 /* The server is assigned */
542 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
543 si->state = SI_ST_ASS;
544}
545
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200546/* This stream analyser checks the switching rules and changes the backend
547 * if appropriate. The default_backend rule is also considered.
548 * It returns 1 if the processing can continue on next analysers, or zero if it
549 * either needs more data or wants to immediately abort the request.
550 */
551int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
552{
553 req->analysers &= ~an_bit;
554 req->analyse_exp = TICK_ETERNITY;
555
556 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
557 now_ms, __FUNCTION__,
558 s,
559 req,
560 req->rex, req->wex,
561 req->flags,
562 req->l,
563 req->analysers);
564
565 /* now check whether we have some switching rules for this request */
566 if (!(s->flags & SN_BE_ASSIGNED)) {
567 struct switching_rule *rule;
568
569 list_for_each_entry(rule, &s->fe->switching_rules, list) {
570 int ret;
571
572 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
573 ret = acl_pass(ret);
574 if (rule->cond->pol == ACL_COND_UNLESS)
575 ret = !ret;
576
577 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200578 if (!session_set_backend(s, rule->be.backend))
579 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200580 break;
581 }
582 }
583
584 /* To ensure correct connection accounting on the backend, we
585 * have to assign one if it was not set (eg: a listen). This
586 * measure also takes care of correctly setting the default
587 * backend if any.
588 */
589 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200590 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
591 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200592 }
593
594 /* we don't want to run the HTTP filters again if the backend has not changed */
595 if (s->fe == s->be)
596 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
597
598 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200599
600 sw_failed:
601 /* immediately abort this request in case of allocation failure */
602 buffer_abort(s->req);
603 buffer_abort(s->rep);
604
605 if (!(s->flags & SN_ERR_MASK))
606 s->flags |= SN_ERR_RESOURCE;
607 if (!(s->flags & SN_FINST_MASK))
608 s->flags |= SN_FINST_R;
609
610 s->txn.status = 500;
611 s->req->analysers = 0;
612 s->req->analyse_exp = TICK_ETERNITY;
613 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200614}
615
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616/* Processes the client, server, request and response jobs of a session task,
617 * then puts it back to the wait queue in a clean state, or cleans up its
618 * resources if it must be deleted. Returns in <next> the date the task wants
619 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
620 * nothing too many times, the request and response buffers flags are monitored
621 * and each function is called only if at least another function has changed at
622 * least one flag it is interested in.
623 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100624struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625{
626 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627 unsigned int rqf_last, rpf_last;
628
629 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
630 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
631
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200632 /* This flag must explicitly be set every time */
633 s->req->flags &= ~BF_READ_NOEXP;
634
635 /* Keep a copy of req/rep flags so that we can detect shutdowns */
636 rqf_last = s->req->flags;
637 rpf_last = s->rep->flags;
638
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200639 /* we don't want the stream interface functions to recursively wake us up */
640 if (s->req->prod->owner == t)
641 s->req->prod->flags |= SI_FL_DONT_WAKE;
642 if (s->req->cons->owner == t)
643 s->req->cons->flags |= SI_FL_DONT_WAKE;
644
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100645 /* 1a: Check for low level timeouts if needed. We just set a flag on
646 * stream interfaces when their timeouts have expired.
647 */
648 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
649 stream_int_check_timeouts(&s->si[0]);
650 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200651
652 /* check buffer timeouts, and close the corresponding stream interfaces
653 * for future reads or writes. Note: this will also concern upper layers
654 * but we do not touch any other flag. We must be careful and correctly
655 * detect state changes when calling them.
656 */
657
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200659
660 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
661 s->req->prod->shutr(s->req->prod);
662
663 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
664 s->req->cons->shutw(s->req->cons);
665
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200668 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
669 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100670
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200671 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
672 s->rep->cons->shutw(s->rep->cons);
673 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674
675 /* 1b: check for low-level errors reported at the stream interface.
676 * First we check if it's a retryable error (in which case we don't
677 * want to tell the buffer). Otherwise we report the error one level
678 * upper by setting flags into the buffers. Note that the side towards
679 * the client cannot have connect (hence retryable) errors. Also, the
680 * connection setup code must be able to deal with any type of abort.
681 */
682 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
683 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
684 s->si[0].shutr(&s->si[0]);
685 s->si[0].shutw(&s->si[0]);
686 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100687 if (!(s->req->analysers) && !(s->rep->analysers)) {
688 if (!(s->flags & SN_ERR_MASK))
689 s->flags |= SN_ERR_CLICL;
690 if (!(s->flags & SN_FINST_MASK))
691 s->flags |= SN_FINST_D;
692 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
694 }
695
696 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
697 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
698 s->si[1].shutr(&s->si[1]);
699 s->si[1].shutw(&s->si[1]);
700 stream_int_report_error(&s->si[1]);
701 s->be->failed_resp++;
702 if (s->srv)
703 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100704 if (!(s->req->analysers) && !(s->rep->analysers)) {
705 if (!(s->flags & SN_ERR_MASK))
706 s->flags |= SN_ERR_SRVCL;
707 if (!(s->flags & SN_FINST_MASK))
708 s->flags |= SN_FINST_D;
709 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 }
711 /* note: maybe we should process connection errors here ? */
712 }
713
714 if (s->si[1].state == SI_ST_CON) {
715 /* we were trying to establish a connection on the server side,
716 * maybe it succeeded, maybe it failed, maybe we timed out, ...
717 */
718 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
719 sess_update_st_cer(s, &s->si[1]);
720 else if (s->si[1].state == SI_ST_EST)
721 sess_establish(s, &s->si[1]);
722
723 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
724 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
725 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
726 */
727 }
728
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200729resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 /* Check for connection closure */
731
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 DPRINTF(stderr,
733 "[%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",
734 now_ms, __FUNCTION__, __LINE__,
735 t,
736 s, s->flags,
737 s->req, s->rep,
738 s->req->rex, s->rep->wex,
739 s->req->flags, s->rep->flags,
740 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
741 s->rep->cons->err_type, s->req->cons->err_type,
742 s->conn_retries);
743
744 /* nothing special to be done on client side */
745 if (unlikely(s->req->prod->state == SI_ST_DIS))
746 s->req->prod->state = SI_ST_CLO;
747
748 /* When a server-side connection is released, we have to count it and
749 * check for pending connections on this server.
750 */
751 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
752 s->req->cons->state = SI_ST_CLO;
753 if (s->srv) {
754 if (s->flags & SN_CURR_SESS) {
755 s->flags &= ~SN_CURR_SESS;
756 s->srv->cur_sess--;
757 }
758 sess_change_server(s, NULL);
759 if (may_dequeue_tasks(s->srv, s->be))
760 process_srv_queue(s->srv);
761 }
762 }
763
764 /*
765 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
766 * at this point.
767 */
768
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100769 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 /* Analyse request */
771 if ((s->req->flags & BF_MASK_ANALYSER) ||
772 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
773 unsigned int flags = s->req->flags;
774
775 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200776 unsigned int last_ana = 0;
777
Willy Tarreau520d95e2009-09-19 21:04:57 +0200778 /* it's up to the analysers to stop new connections */
779 buffer_auto_connect(s->req);
780 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100781
782 /* We will call all analysers for which a bit is set in
783 * s->req->analysers, following the bit order from LSB
784 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200785 * the list when not needed. Any analyser may return 0
786 * to break out of the loop, either because of missing
787 * data to take a decision, or because it decides to
788 * kill the session. We loop at least once through each
789 * analyser, and we may loop again if other analysers
790 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100791 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200792 while (s->req->analysers & ~last_ana) {
793 last_ana = s->req->analysers;
794
795 if (s->req->analysers & AN_REQ_INSPECT) {
796 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200797 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100798 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200799 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100800
Willy Tarreaud787e662009-07-07 10:14:51 +0200801 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
802 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200803 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200804 break;
805 }
806
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200807 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
808 last_ana |= AN_REQ_HTTP_PROCESS_FE;
809 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
810 break;
811 }
812
813 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
814 last_ana |= AN_REQ_SWITCHING_RULES;
815 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
816 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200817 /* FIXME: we mait switch from TCP to HTTP and want to
818 * immediately loop back to the top. This is a dirty way
819 * of doing it, and we should find a cleaner method relying
820 * on a circular list of function pointers.
821 */
822 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
823 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200824 }
825
826 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
827 last_ana |= AN_REQ_HTTP_PROCESS_BE;
828 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
829 break;
830 }
831
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200832 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
833 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200834 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100835 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200836 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100837
Willy Tarreauc465fd72009-08-31 00:17:18 +0200838 if (s->req->analysers & AN_REQ_HTTP_INNER) {
839 last_ana |= AN_REQ_HTTP_INNER;
840 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
841 break;
842 }
843
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200844 if (s->req->analysers & AN_REQ_HTTP_BODY) {
845 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200846 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100847 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200848 }
Emeric Brun647caf12009-06-30 17:57:00 +0200849
850 if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) {
851 last_ana |= AN_REQ_PRST_RDP_COOKIE;
852 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
853 break;
854 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100855 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 }
Willy Tarreau84455332009-03-15 22:34:05 +0100857
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200858 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
859 rqf_last = s->req->flags;
860 goto resync_request;
861 }
862 }
863
864 resync_response:
865 /* Analyse response */
866
867 if (unlikely(s->rep->flags & BF_HIJACK)) {
868 /* In inject mode, we wake up everytime something has
869 * happened on the write side of the buffer.
870 */
871 unsigned int flags = s->rep->flags;
872
873 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
874 !(s->rep->flags & BF_FULL)) {
875 s->rep->hijacker(s, s->rep);
876 }
877
878 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
879 rpf_last = s->rep->flags;
880 goto resync_response;
881 }
882 }
883 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
884 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
885 unsigned int flags = s->rep->flags;
886
887 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200888 /* it's up to the analysers to reset auto_close */
889 buffer_auto_close(s->rep);
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200890 if (s->rep->analysers)
891 process_response(s);
892 }
893
894 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
895 rpf_last = s->rep->flags;
896 goto resync_response;
897 }
898 }
899
900 /* FIXME: here we should call protocol handlers which rely on
901 * both buffers.
902 */
903
904
905 /*
906 * Now we propagate unhandled errors to the session
907 */
908 if (!(s->flags & SN_ERR_MASK)) {
909 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
910 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100911 s->req->analysers = 0;
912 if (s->req->flags & BF_READ_ERROR)
913 s->flags |= SN_ERR_CLICL;
914 else if (s->req->flags & BF_READ_TIMEOUT)
915 s->flags |= SN_ERR_CLITO;
916 else if (s->req->flags & BF_WRITE_ERROR)
917 s->flags |= SN_ERR_SRVCL;
918 else
919 s->flags |= SN_ERR_SRVTO;
920 sess_set_term_flags(s);
921 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200922 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
923 /* Report it if the server got an error or a read timeout expired */
924 s->rep->analysers = 0;
925 if (s->rep->flags & BF_READ_ERROR)
926 s->flags |= SN_ERR_SRVCL;
927 else if (s->rep->flags & BF_READ_TIMEOUT)
928 s->flags |= SN_ERR_SRVTO;
929 else if (s->rep->flags & BF_WRITE_ERROR)
930 s->flags |= SN_ERR_CLICL;
931 else
Willy Tarreau84455332009-03-15 22:34:05 +0100932 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200933 sess_set_term_flags(s);
934 }
Willy Tarreau84455332009-03-15 22:34:05 +0100935 }
936
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200937 /*
938 * Here we take care of forwarding unhandled data. This also includes
939 * connection establishments and shutdown requests.
940 */
941
942
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100943 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +0200944 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100945 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100946 if (!s->req->analysers &&
947 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +0200948 (s->req->prod->state >= SI_ST_EST) &&
949 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100950 /* This buffer is freewheeling, there's no analyser nor hijacker
951 * attached to it. If any data are left in, we'll permit them to
952 * move.
953 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200954 buffer_auto_connect(s->req);
955 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100956 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100957
Willy Tarreau31971e52009-09-20 12:07:52 +0200958 /* If the producer is still connected, we'll enable data to flow
959 * from the producer to the consumer (which might possibly not be
960 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100961 */
Willy Tarreau31971e52009-09-20 12:07:52 +0200962 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
963 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100964 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100965
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100966 /* check if it is wise to enable kernel splicing to forward request data */
967 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
968 s->req->to_forward &&
969 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200970 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100971 (pipes_used < global.maxpipes) &&
972 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
973 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
974 (s->req->flags & BF_STREAMER_FAST)))) {
975 s->req->flags |= BF_KERN_SPLICING;
976 }
977
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100978 /* reflect what the L7 analysers have seen last */
979 rqf_last = s->req->flags;
980
981 /*
982 * Now forward all shutdown requests between both sides of the buffer
983 */
984
Willy Tarreau520d95e2009-09-19 21:04:57 +0200985 /* first, let's check if the request buffer needs to shutdown(write), which may
986 * happen either because the input is closed or because we want to force a close
987 * once the server has begun to respond.
988 */
989 if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE)) == BF_AUTO_CLOSE) {
990 if (unlikely((s->req->flags & BF_SHUTR) ||
991 ((s->req->cons->state == SI_ST_EST) &&
992 (s->be->options & PR_O_FORCE_CLO) &&
993 (s->rep->flags & BF_READ_ACTIVITY))))
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200994 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995 }
996
997 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200998 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 +0100999 s->req->cons->shutw(s->req->cons);
1000
1001 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001002 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1003 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001004 buffer_shutr_now(s->req);
1005
1006 /* shutdown(read) pending */
1007 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1008 s->req->prod->shutr(s->req->prod);
1009
Willy Tarreau520d95e2009-09-19 21:04:57 +02001010 /* it's possible that an upper layer has requested a connection setup or abort.
1011 * There are 2 situations where we decide to establish a new connection :
1012 * - there are data scheduled for emission in the buffer
1013 * - the BF_AUTO_CONNECT flag is set (active connection)
1014 */
1015 if (s->req->cons->state == SI_ST_INI) {
1016 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001017 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001018 /* If we have a ->connect method, we need to perform a connection request,
1019 * otherwise we immediately switch to the connected state.
1020 */
1021 if (s->req->cons->connect)
1022 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1023 else
1024 s->req->cons->state = SI_ST_EST; /* connection established */
1025 }
Willy Tarreau73201222009-08-16 18:27:24 +02001026 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001027 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001028 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001029 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1030 buffer_shutr_now(s->rep);
1031 }
Willy Tarreau92795622009-03-06 12:51:23 +01001032 }
1033
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001034
1035 /* we may have a pending connection request, or a connection waiting
1036 * for completion.
1037 */
1038 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1039 do {
1040 /* nb: step 1 might switch from QUE to ASS, but we first want
1041 * to give a chance to step 2 to perform a redirect if needed.
1042 */
1043 if (s->si[1].state != SI_ST_REQ)
1044 sess_update_stream_int(s, &s->si[1]);
1045 if (s->si[1].state == SI_ST_REQ)
1046 sess_prepare_conn_req(s, &s->si[1]);
1047
1048 if (s->si[1].state == SI_ST_ASS && s->srv &&
1049 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1050 perform_http_redirect(s, &s->si[1]);
1051 } while (s->si[1].state == SI_ST_ASS);
1052 }
1053
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001054 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001055 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001056 goto resync_stream_interface;
1057
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001058 /* otherwise wewant to check if we need to resync the req buffer or not */
1059 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001060 goto resync_request;
1061
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001062 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001063
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001064 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001065 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001066 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001067 if (!s->rep->analysers &&
1068 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001069 (s->rep->prod->state >= SI_ST_EST) &&
1070 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001071 /* This buffer is freewheeling, there's no analyser nor hijacker
1072 * attached to it. If any data are left in, we'll permit them to
1073 * move.
1074 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001075 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001076 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001077 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1078 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001079 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001080
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001081 /* check if it is wise to enable kernel splicing to forward response data */
1082 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1083 s->rep->to_forward &&
1084 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001085 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001086 (pipes_used < global.maxpipes) &&
1087 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1088 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1089 (s->rep->flags & BF_STREAMER_FAST)))) {
1090 s->rep->flags |= BF_KERN_SPLICING;
1091 }
1092
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001093 /* reflect what the L7 analysers have seen last */
1094 rpf_last = s->rep->flags;
1095
1096 /*
1097 * Now forward all shutdown requests between both sides of the buffer
1098 */
1099
1100 /*
1101 * FIXME: this is probably where we should produce error responses.
1102 */
1103
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001104 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001105 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1106 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107 buffer_shutw_now(s->rep);
1108
1109 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001110 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 +01001111 s->rep->cons->shutw(s->rep->cons);
1112
1113 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001114 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1115 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001116 buffer_shutr_now(s->rep);
1117
1118 /* shutdown(read) pending */
1119 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1120 s->rep->prod->shutr(s->rep->prod);
1121
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001122 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001123 goto resync_stream_interface;
1124
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001125 if (s->req->flags != rqf_last)
1126 goto resync_request;
1127
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001128 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001129 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001130
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001131 /* we're interested in getting wakeups again */
1132 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1133 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1134
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001135 /* This is needed only when debugging is enabled, to indicate
1136 * client-side or server-side close. Please note that in the unlikely
1137 * event where both sides would close at once, the sequence is reported
1138 * on the server side first.
1139 */
1140 if (unlikely((global.mode & MODE_DEBUG) &&
1141 (!(global.mode & MODE_QUIET) ||
1142 (global.mode & MODE_VERBOSE)))) {
1143 int len;
1144
1145 if (s->si[1].state == SI_ST_CLO &&
1146 s->si[1].prev_state == SI_ST_EST) {
1147 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1148 s->uniq_id, s->be->id,
1149 (unsigned short)s->si[0].fd,
1150 (unsigned short)s->si[1].fd);
1151 write(1, trash, len);
1152 }
1153
1154 if (s->si[0].state == SI_ST_CLO &&
1155 s->si[0].prev_state == SI_ST_EST) {
1156 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1157 s->uniq_id, s->be->id,
1158 (unsigned short)s->si[0].fd,
1159 (unsigned short)s->si[1].fd);
1160 write(1, trash, len);
1161 }
1162 }
1163
1164 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1165 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1166
1167 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1168 session_process_counters(s);
1169
Willy Tarreau1accfc02009-09-05 20:57:35 +02001170 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001171 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172
Willy Tarreau1accfc02009-09-05 20:57:35 +02001173 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001174 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001175
Willy Tarreauea388542009-06-21 21:45:58 +02001176 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1177 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001178 s->si[0].prev_state = s->si[0].state;
1179 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001180 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1181 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001182
1183 /* Trick: if a request is being waiting for the server to respond,
1184 * and if we know the server can timeout, we don't want the timeout
1185 * to expire on the client side first, but we're still interested
1186 * in passing data from the client to the server (eg: POST). Thus,
1187 * we can cancel the client's request timeout if the server's
1188 * request timeout is set and the server has not yet sent a response.
1189 */
1190
Willy Tarreau520d95e2009-09-19 21:04:57 +02001191 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001192 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1193 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001194 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001195 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001196
Willy Tarreau1accfc02009-09-05 20:57:35 +02001197 /* Call the second stream interface's I/O handler if it's embedded.
1198 * Note that this one may wake the task up again.
1199 */
1200 if (s->req->cons->iohandler) {
1201 s->req->cons->iohandler(s->req->cons);
1202 if (task_in_rq(t)) {
1203 /* If we woke up, we don't want to requeue the
1204 * task to the wait queue, but rather requeue
1205 * it into the runqueue ASAP.
1206 */
1207 t->expire = TICK_ETERNITY;
1208 return t;
1209 }
1210 }
1211
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1213 tick_first(s->rep->rex, s->rep->wex));
1214 if (s->req->analysers)
1215 t->expire = tick_first(t->expire, s->req->analyse_exp);
1216
1217 if (s->si[0].exp)
1218 t->expire = tick_first(t->expire, s->si[0].exp);
1219
1220 if (s->si[1].exp)
1221 t->expire = tick_first(t->expire, s->si[1].exp);
1222
1223#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001224 fprintf(stderr,
1225 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1226 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1227 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1228 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 +01001229#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001230
1231#ifdef DEBUG_DEV
1232 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001233 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001234 ABORT_NOW();
1235#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001236 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001237 }
1238
1239 s->fe->feconn--;
1240 if (s->flags & SN_BE_ASSIGNED)
1241 s->be->beconn--;
1242 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001243 s->listener->nbconn--;
1244 if (s->listener->state == LI_FULL &&
1245 s->listener->nbconn < s->listener->maxconn) {
1246 /* we should reactivate the listener */
1247 EV_FD_SET(s->listener->fd, DIR_RD);
1248 s->listener->state = LI_READY;
1249 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001250
1251 if (unlikely((global.mode & MODE_DEBUG) &&
1252 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1253 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001254 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001255 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001256 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257 write(1, trash, len);
1258 }
1259
1260 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1261 session_process_counters(s);
1262
1263 /* let's do a final log if we need it */
1264 if (s->logs.logwait &&
1265 !(s->flags & SN_MONITOR) &&
1266 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001267 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001268 }
1269
1270 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001271 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001272 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001273 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001274 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001275}
1276
Willy Tarreau7c669d72008-06-20 15:04:11 +02001277/*
1278 * This function adjusts sess->srv_conn and maintains the previous and new
1279 * server's served session counts. Setting newsrv to NULL is enough to release
1280 * current connection slot. This function also notifies any LB algo which might
1281 * expect to be informed about any change in the number of active sessions on a
1282 * server.
1283 */
1284void sess_change_server(struct session *sess, struct server *newsrv)
1285{
1286 if (sess->srv_conn == newsrv)
1287 return;
1288
1289 if (sess->srv_conn) {
1290 sess->srv_conn->served--;
1291 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1292 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1293 sess->srv_conn = NULL;
1294 }
1295
1296 if (newsrv) {
1297 newsrv->served++;
1298 if (newsrv->proxy->lbprm.server_take_conn)
1299 newsrv->proxy->lbprm.server_take_conn(newsrv);
1300 sess->srv_conn = newsrv;
1301 }
1302}
1303
Willy Tarreau84455332009-03-15 22:34:05 +01001304/* Set correct session termination flags in case no analyser has done it. It
1305 * also counts a failed request if the server state has not reached the request
1306 * stage.
1307 */
1308void sess_set_term_flags(struct session *s)
1309{
1310 if (!(s->flags & SN_FINST_MASK)) {
1311 if (s->si[1].state < SI_ST_REQ) {
1312 s->fe->failed_req++;
1313 s->flags |= SN_FINST_R;
1314 }
1315 else if (s->si[1].state == SI_ST_QUE)
1316 s->flags |= SN_FINST_Q;
1317 else if (s->si[1].state < SI_ST_EST)
1318 s->flags |= SN_FINST_C;
1319 else if (s->si[1].state == SI_ST_EST)
1320 s->flags |= SN_FINST_D;
1321 else
1322 s->flags |= SN_FINST_L;
1323 }
1324}
1325
1326/* Handle server-side errors for default protocols. It is called whenever a a
1327 * connection setup is aborted or a request is aborted in queue. It sets the
1328 * session termination flags so that the caller does not have to worry about
1329 * them. It's installed as ->srv_error for the server-side stream_interface.
1330 */
1331void default_srv_error(struct session *s, struct stream_interface *si)
1332{
1333 int err_type = si->err_type;
1334 int err = 0, fin = 0;
1335
1336 if (err_type & SI_ET_QUEUE_ABRT) {
1337 err = SN_ERR_CLICL;
1338 fin = SN_FINST_Q;
1339 }
1340 else if (err_type & SI_ET_CONN_ABRT) {
1341 err = SN_ERR_CLICL;
1342 fin = SN_FINST_C;
1343 }
1344 else if (err_type & SI_ET_QUEUE_TO) {
1345 err = SN_ERR_SRVTO;
1346 fin = SN_FINST_Q;
1347 }
1348 else if (err_type & SI_ET_QUEUE_ERR) {
1349 err = SN_ERR_SRVCL;
1350 fin = SN_FINST_Q;
1351 }
1352 else if (err_type & SI_ET_CONN_TO) {
1353 err = SN_ERR_SRVTO;
1354 fin = SN_FINST_C;
1355 }
1356 else if (err_type & SI_ET_CONN_ERR) {
1357 err = SN_ERR_SRVCL;
1358 fin = SN_FINST_C;
1359 }
1360 else /* SI_ET_CONN_OTHER and others */ {
1361 err = SN_ERR_INTERNAL;
1362 fin = SN_FINST_C;
1363 }
1364
1365 if (!(s->flags & SN_ERR_MASK))
1366 s->flags |= err;
1367 if (!(s->flags & SN_FINST_MASK))
1368 s->flags |= fin;
1369}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001370
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371/*
1372 * Local variables:
1373 * c-indent-level: 8
1374 * c-basic-offset: 8
1375 * End:
1376 */