blob: af6061f59822ea84e4abbcc3e5a7f20942d898a6 [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 Tarreau8d5d7f22007-01-21 19:16:41 +010025#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020026#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010028#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010029#include <proto/proto_http.h>
30#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020031#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010033#include <proto/server.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010034#include <proto/stream_interface.h>
35#include <proto/stream_sock.h>
36#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreau3ab68cf2009-01-25 16:03:28 +010038#ifdef CONFIG_HAP_TCPSPLICE
39#include <libtcpsplice.h>
40#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020042struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010043struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
45/*
46 * frees the context associated to a session. It must have been removed first.
47 */
48void session_free(struct session *s)
49{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010050 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020051 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010052 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010053
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 if (s->pend_pos)
55 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010056
Willy Tarreau1e62de62008-11-11 20:20:02 +010057 if (s->srv) { /* there may be requests left pending in queue */
58 if (s->flags & SN_CURR_SESS) {
59 s->flags &= ~SN_CURR_SESS;
60 s->srv->cur_sess--;
61 }
Willy Tarreau922a8062008-12-04 09:33:58 +010062 if (may_dequeue_tasks(s->srv, s->be))
63 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010064 }
Willy Tarreau922a8062008-12-04 09:33:58 +010065
Willy Tarreau7c669d72008-06-20 15:04:11 +020066 if (unlikely(s->srv_conn)) {
67 /* the session still has a reserved slot on a server, but
68 * it should normally be only the same as the one above,
69 * so this should not happen in fact.
70 */
71 sess_change_server(s, NULL);
72 }
73
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 if (s->req->pipe)
75 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010076
Willy Tarreau3eba98a2009-01-25 13:56:13 +010077 if (s->rep->pipe)
78 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010079
Willy Tarreau48d63db2008-08-03 17:41:33 +020080 pool_free2(pool2_buffer, s->req);
81 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020084 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010085
Willy Tarreau92fb9832007-10-16 17:34:28 +020086 if (txn->rsp.cap != NULL) {
87 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020088 for (h = fe->rsp_cap; h; h = h->next)
89 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020090 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020092 if (txn->req.cap != NULL) {
93 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020094 for (h = fe->req_cap; h; h = h->next)
95 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020096 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020099 pool_free2(pool2_requri, txn->uri);
100 pool_free2(pool2_capture, txn->cli_cookie);
101 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100102
103 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100104 /* we have to unlink all watchers. We must not relink them if
105 * this session was the last one in the list.
106 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100107 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100108 LIST_INIT(&bref->users);
109 if (s->list.n != &sessions)
110 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100111 bref->ref = s->list.n;
112 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100113 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200114 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200115
116 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200117 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200118 pool_flush2(pool2_buffer);
119 pool_flush2(fe->hdr_idx_pool);
120 pool_flush2(pool2_requri);
121 pool_flush2(pool2_capture);
122 pool_flush2(pool2_session);
123 pool_flush2(fe->req_cap_pool);
124 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200125 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200126}
127
128
129/* perform minimal intializations, report 0 in case of error, 1 if OK. */
130int init_session()
131{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100132 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200133 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
134 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135}
136
Willy Tarreau30e71012007-11-26 20:15:35 +0100137void session_process_counters(struct session *s)
138{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 unsigned long long bytes;
140
Willy Tarreau30e71012007-11-26 20:15:35 +0100141 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 s->logs.bytes_in = s->req->total;
144 if (bytes) {
145 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->be != s->fe)
148 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149
Willy Tarreau30e71012007-11-26 20:15:35 +0100150 if (s->srv)
151 s->srv->bytes_in += bytes;
152 }
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) {
159 s->fe->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)
162 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100163
Willy Tarreau30e71012007-11-26 20:15:35 +0100164 if (s->srv)
165 s->srv->bytes_out += bytes;
166 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100167 }
168}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100170/* This function is called with (si->state == SI_ST_CON) meaning that a
171 * connection was attempted and that the file descriptor is already allocated.
172 * We must check for establishment, error and abort. Possible output states
173 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
174 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
175 * otherwise 1.
176 */
177int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
178{
179 struct buffer *req = si->ob;
180 struct buffer *rep = si->ib;
181
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100182 /* If we got an error, or if nothing happened and the connection timed
183 * out, we must give up. The CER state handler will take care of retry
184 * attempts and error reports.
185 */
186 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100187 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100188 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200189 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100190 fd_delete(si->fd);
191
192 if (si->err_type)
193 return 0;
194
195 si->err_loc = s->srv;
196 if (si->flags & SI_FL_ERR)
197 si->err_type = SI_ET_CONN_ERR;
198 else
199 si->err_type = SI_ET_CONN_TO;
200 return 0;
201 }
202
203 /* OK, maybe we want to abort */
204 if (unlikely((req->flags & BF_SHUTW_NOW) ||
205 (rep->flags & BF_SHUTW) ||
206 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100207 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100208 s->be->options & PR_O_ABRT_CLOSE)))) {
209 /* give up */
210 si->shutw(si);
211 si->err_type |= SI_ET_CONN_ABRT;
212 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200213 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100214 if (s->srv_error)
215 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100216 return 1;
217 }
218
219 /* we need to wait a bit more if there was no activity either */
220 if (!(req->flags & BF_WRITE_ACTIVITY))
221 return 1;
222
223 /* OK, this means that a connection succeeded. The caller will be
224 * responsible for handling the transition from CON to EST.
225 */
226 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100227 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100228 si->state = SI_ST_EST;
229 si->err_type = SI_ET_NONE;
230 si->err_loc = NULL;
231 return 1;
232}
233
234/* This function is called with (si->state == SI_ST_CER) meaning that a
235 * previous connection attempt has failed and that the file descriptor
236 * has already been released. Possible causes include asynchronous error
237 * notification and time out. Possible output states are SI_ST_CLO when
238 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
239 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
240 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
241 * marked as in error state. It returns 0.
242 */
243int sess_update_st_cer(struct session *s, struct stream_interface *si)
244{
245 /* we probably have to release last session from the server */
246 if (s->srv) {
247 if (s->flags & SN_CURR_SESS) {
248 s->flags &= ~SN_CURR_SESS;
249 s->srv->cur_sess--;
250 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100251 }
252
253 /* ensure that we have enough retries left */
254 s->conn_retries--;
255 if (s->conn_retries < 0) {
256 if (!si->err_type) {
257 si->err_type = SI_ET_CONN_ERR;
258 si->err_loc = s->srv;
259 }
260
261 if (s->srv)
262 s->srv->failed_conns++;
263 s->be->failed_conns++;
264 if (may_dequeue_tasks(s->srv, s->be))
265 process_srv_queue(s->srv);
266
267 /* shutw is enough so stop a connecting socket */
268 si->shutw(si);
269 si->ob->flags |= BF_WRITE_ERROR;
270 si->ib->flags |= BF_READ_ERROR;
271
272 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100273 if (s->srv_error)
274 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100275 return 0;
276 }
277
278 /* If the "redispatch" option is set on the backend, we are allowed to
279 * retry on another server for the last retry. In order to achieve this,
280 * we must mark the session unassigned, and eventually clear the DIRECT
281 * bit to ignore any persistence cookie. We won't count a retry nor a
282 * redispatch yet, because this will depend on what server is selected.
283 */
284 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
285 if (may_dequeue_tasks(s->srv, s->be))
286 process_srv_queue(s->srv);
287
288 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
289 s->prev_srv = s->srv;
290 si->state = SI_ST_REQ;
291 } else {
292 if (s->srv)
293 s->srv->retries++;
294 s->be->retries++;
295 si->state = SI_ST_ASS;
296 }
297
298 if (si->flags & SI_FL_ERR) {
299 /* The error was an asynchronous connection error, and we will
300 * likely have to retry connecting to the same server, most
301 * likely leading to the same result. To avoid this, we wait
302 * one second before retrying.
303 */
304
305 if (!si->err_type)
306 si->err_type = SI_ET_CONN_ERR;
307
308 si->state = SI_ST_TAR;
309 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
310 return 0;
311 }
312 return 0;
313}
314
315/*
316 * This function handles the transition between the SI_ST_CON state and the
317 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
318 * SI_ST_EST.
319 */
320void sess_establish(struct session *s, struct stream_interface *si)
321{
322 struct buffer *req = si->ob;
323 struct buffer *rep = si->ib;
324
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100325 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100326 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
327
328 /* if the user wants to log as soon as possible, without counting
329 * bytes from the server, then this is the right moment. */
330 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
331 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100332 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100333 }
334#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100335 if ((global.tune.options & GTUNE_USE_SPLICE) &&
336 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100337 /* TCP splicing supported by both FE and BE */
338 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
339 }
340#endif
341 }
342 else {
343 rep->analysers |= AN_RTR_HTTP_HDR;
344 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
345 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
346 /* reset hdr_idx which was already initialized by the request.
347 * right now, the http parser does it.
348 * hdr_idx_init(&s->txn.hdr_idx);
349 */
350 }
351
352 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
353 req->wex = TICK_ETERNITY;
354}
355
356/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
357 * Other input states are simply ignored.
358 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
359 * Flags must have previously been updated for timeouts and other conditions.
360 */
361void sess_update_stream_int(struct session *s, struct stream_interface *si)
362{
363 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",
364 now_ms, __FUNCTION__,
365 s,
366 s->req, s->rep,
367 s->req->rex, s->rep->wex,
368 s->req->flags, s->rep->flags,
369 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
370
371 if (si->state == SI_ST_ASS) {
372 /* Server assigned to connection request, we have to try to connect now */
373 int conn_err;
374
375 conn_err = connect_server(s);
376 if (conn_err == SN_ERR_NONE) {
377 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100378 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100379 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100380 return;
381 }
382
383 /* We have received a synchronous error. We might have to
384 * abort, retry immediately or redispatch.
385 */
386 if (conn_err == SN_ERR_INTERNAL) {
387 if (!si->err_type) {
388 si->err_type = SI_ET_CONN_OTHER;
389 si->err_loc = s->srv;
390 }
391
392 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100393 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100394 if (s->srv)
395 s->srv->failed_conns++;
396 s->be->failed_conns++;
397
398 /* release other sessions waiting for this server */
399 if (may_dequeue_tasks(s->srv, s->be))
400 process_srv_queue(s->srv);
401
402 /* Failed and not retryable. */
403 si->shutr(si);
404 si->shutw(si);
405 si->ob->flags |= BF_WRITE_ERROR;
406
407 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
408
409 /* no session was ever accounted for this server */
410 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100411 if (s->srv_error)
412 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100413 return;
414 }
415
416 /* We are facing a retryable error, but we don't want to run a
417 * turn-around now, as the problem is likely a source port
418 * allocation problem, so we want to retry now.
419 */
420 si->state = SI_ST_CER;
421 si->flags &= ~SI_FL_ERR;
422 sess_update_st_cer(s, si);
423 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
424 return;
425 }
426 else if (si->state == SI_ST_QUE) {
427 /* connection request was queued, check for any update */
428 if (!s->pend_pos) {
429 /* The connection is not in the queue anymore. Either
430 * we have a server connection slot available and we
431 * go directly to the assigned state, or we need to
432 * load-balance first and go to the INI state.
433 */
434 si->exp = TICK_ETERNITY;
435 if (unlikely(!(s->flags & SN_ASSIGNED)))
436 si->state = SI_ST_REQ;
437 else {
438 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
439 si->state = SI_ST_ASS;
440 }
441 return;
442 }
443
444 /* Connection request still in queue... */
445 if (si->flags & SI_FL_EXP) {
446 /* ... and timeout expired */
447 si->exp = TICK_ETERNITY;
448 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
449 if (s->srv)
450 s->srv->failed_conns++;
451 s->be->failed_conns++;
452 si->shutr(si);
453 si->shutw(si);
454 si->ob->flags |= BF_WRITE_TIMEOUT;
455 if (!si->err_type)
456 si->err_type = SI_ET_QUEUE_TO;
457 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100458 if (s->srv_error)
459 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100460 return;
461 }
462
463 /* Connection remains in queue, check if we have to abort it */
464 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
465 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
466 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
467 /* give up */
468 si->exp = TICK_ETERNITY;
469 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
470 si->shutr(si);
471 si->shutw(si);
472 si->err_type |= SI_ET_QUEUE_ABRT;
473 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100474 if (s->srv_error)
475 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100476 return;
477 }
478
479 /* Nothing changed */
480 return;
481 }
482 else if (si->state == SI_ST_TAR) {
483 /* Connection request might be aborted */
484 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
485 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
486 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
487 /* give up */
488 si->exp = TICK_ETERNITY;
489 si->shutr(si);
490 si->shutw(si);
491 si->err_type |= SI_ET_CONN_ABRT;
492 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100493 if (s->srv_error)
494 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100495 return;
496 }
497
498 if (!(si->flags & SI_FL_EXP))
499 return; /* still in turn-around */
500
501 si->exp = TICK_ETERNITY;
502
503 /* we keep trying on the same server as long as the session is
504 * marked "assigned".
505 * FIXME: Should we force a redispatch attempt when the server is down ?
506 */
507 if (s->flags & SN_ASSIGNED)
508 si->state = SI_ST_ASS;
509 else
510 si->state = SI_ST_REQ;
511 return;
512 }
513}
514
515/* This function initiates a server connection request on a stream interface
516 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
517 * indicating that a server has been assigned. It may also return SI_ST_QUE,
518 * or SI_ST_CLO upon error.
519 */
520static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
521 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",
522 now_ms, __FUNCTION__,
523 s,
524 s->req, s->rep,
525 s->req->rex, s->rep->wex,
526 s->req->flags, s->rep->flags,
527 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
528
529 if (si->state != SI_ST_REQ)
530 return;
531
532 /* Try to assign a server */
533 if (srv_redispatch_connect(s) != 0) {
534 /* We did not get a server. Either we queued the
535 * connection request, or we encountered an error.
536 */
537 if (si->state == SI_ST_QUE)
538 return;
539
540 /* we did not get any server, let's check the cause */
541 si->shutr(si);
542 si->shutw(si);
543 si->ob->flags |= BF_WRITE_ERROR;
544 if (!si->err_type)
545 si->err_type = SI_ET_CONN_OTHER;
546 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100547 if (s->srv_error)
548 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100549 return;
550 }
551
552 /* The server is assigned */
553 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
554 si->state = SI_ST_ASS;
555}
556
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200557/* This stream analyser checks the switching rules and changes the backend
558 * if appropriate. The default_backend rule is also considered.
559 * It returns 1 if the processing can continue on next analysers, or zero if it
560 * either needs more data or wants to immediately abort the request.
561 */
562int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
563{
564 req->analysers &= ~an_bit;
565 req->analyse_exp = TICK_ETERNITY;
566
567 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
568 now_ms, __FUNCTION__,
569 s,
570 req,
571 req->rex, req->wex,
572 req->flags,
573 req->l,
574 req->analysers);
575
576 /* now check whether we have some switching rules for this request */
577 if (!(s->flags & SN_BE_ASSIGNED)) {
578 struct switching_rule *rule;
579
580 list_for_each_entry(rule, &s->fe->switching_rules, list) {
581 int ret;
582
583 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
584 ret = acl_pass(ret);
585 if (rule->cond->pol == ACL_COND_UNLESS)
586 ret = !ret;
587
588 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200589 if (!session_set_backend(s, rule->be.backend))
590 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200591 break;
592 }
593 }
594
595 /* To ensure correct connection accounting on the backend, we
596 * have to assign one if it was not set (eg: a listen). This
597 * measure also takes care of correctly setting the default
598 * backend if any.
599 */
600 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200601 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
602 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200603 }
604
605 /* we don't want to run the HTTP filters again if the backend has not changed */
606 if (s->fe == s->be)
607 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
608
609 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200610
611 sw_failed:
612 /* immediately abort this request in case of allocation failure */
613 buffer_abort(s->req);
614 buffer_abort(s->rep);
615
616 if (!(s->flags & SN_ERR_MASK))
617 s->flags |= SN_ERR_RESOURCE;
618 if (!(s->flags & SN_FINST_MASK))
619 s->flags |= SN_FINST_R;
620
621 s->txn.status = 500;
622 s->req->analysers = 0;
623 s->req->analyse_exp = TICK_ETERNITY;
624 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200625}
626
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627/* Processes the client, server, request and response jobs of a session task,
628 * then puts it back to the wait queue in a clean state, or cleans up its
629 * resources if it must be deleted. Returns in <next> the date the task wants
630 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
631 * nothing too many times, the request and response buffers flags are monitored
632 * and each function is called only if at least another function has changed at
633 * least one flag it is interested in.
634 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100635struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636{
637 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 unsigned int rqf_last, rpf_last;
639
640 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
641 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
642
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200643 /* This flag must explicitly be set every time */
644 s->req->flags &= ~BF_READ_NOEXP;
645
646 /* Keep a copy of req/rep flags so that we can detect shutdowns */
647 rqf_last = s->req->flags;
648 rpf_last = s->rep->flags;
649
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650 /* 1a: Check for low level timeouts if needed. We just set a flag on
651 * stream interfaces when their timeouts have expired.
652 */
653 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
654 stream_int_check_timeouts(&s->si[0]);
655 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200656
657 /* check buffer timeouts, and close the corresponding stream interfaces
658 * for future reads or writes. Note: this will also concern upper layers
659 * but we do not touch any other flag. We must be careful and correctly
660 * detect state changes when calling them.
661 */
662
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200664
665 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
666 s->req->prod->shutr(s->req->prod);
667
668 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
669 s->req->cons->shutw(s->req->cons);
670
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200673 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
674 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100675
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200676 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
677 s->rep->cons->shutw(s->rep->cons);
678 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679
680 /* 1b: check for low-level errors reported at the stream interface.
681 * First we check if it's a retryable error (in which case we don't
682 * want to tell the buffer). Otherwise we report the error one level
683 * upper by setting flags into the buffers. Note that the side towards
684 * the client cannot have connect (hence retryable) errors. Also, the
685 * connection setup code must be able to deal with any type of abort.
686 */
687 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
688 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
689 s->si[0].shutr(&s->si[0]);
690 s->si[0].shutw(&s->si[0]);
691 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100692 if (!(s->req->analysers) && !(s->rep->analysers)) {
693 if (!(s->flags & SN_ERR_MASK))
694 s->flags |= SN_ERR_CLICL;
695 if (!(s->flags & SN_FINST_MASK))
696 s->flags |= SN_FINST_D;
697 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 }
699 }
700
701 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
702 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
703 s->si[1].shutr(&s->si[1]);
704 s->si[1].shutw(&s->si[1]);
705 stream_int_report_error(&s->si[1]);
706 s->be->failed_resp++;
707 if (s->srv)
708 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100709 if (!(s->req->analysers) && !(s->rep->analysers)) {
710 if (!(s->flags & SN_ERR_MASK))
711 s->flags |= SN_ERR_SRVCL;
712 if (!(s->flags & SN_FINST_MASK))
713 s->flags |= SN_FINST_D;
714 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 }
716 /* note: maybe we should process connection errors here ? */
717 }
718
719 if (s->si[1].state == SI_ST_CON) {
720 /* we were trying to establish a connection on the server side,
721 * maybe it succeeded, maybe it failed, maybe we timed out, ...
722 */
723 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
724 sess_update_st_cer(s, &s->si[1]);
725 else if (s->si[1].state == SI_ST_EST)
726 sess_establish(s, &s->si[1]);
727
728 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
729 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
730 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
731 */
732 }
733
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200734resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 /* Check for connection closure */
736
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 DPRINTF(stderr,
738 "[%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",
739 now_ms, __FUNCTION__, __LINE__,
740 t,
741 s, s->flags,
742 s->req, s->rep,
743 s->req->rex, s->rep->wex,
744 s->req->flags, s->rep->flags,
745 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
746 s->rep->cons->err_type, s->req->cons->err_type,
747 s->conn_retries);
748
749 /* nothing special to be done on client side */
750 if (unlikely(s->req->prod->state == SI_ST_DIS))
751 s->req->prod->state = SI_ST_CLO;
752
753 /* When a server-side connection is released, we have to count it and
754 * check for pending connections on this server.
755 */
756 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
757 s->req->cons->state = SI_ST_CLO;
758 if (s->srv) {
759 if (s->flags & SN_CURR_SESS) {
760 s->flags &= ~SN_CURR_SESS;
761 s->srv->cur_sess--;
762 }
763 sess_change_server(s, NULL);
764 if (may_dequeue_tasks(s->srv, s->be))
765 process_srv_queue(s->srv);
766 }
767 }
768
769 /*
770 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
771 * at this point.
772 */
773
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100774 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100775 /* Analyse request */
776 if ((s->req->flags & BF_MASK_ANALYSER) ||
777 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
778 unsigned int flags = s->req->flags;
779
780 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200781 unsigned int last_ana = 0;
782
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100783 /* it's up to the analysers to reset write_ena */
784 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100785
786 /* We will call all analysers for which a bit is set in
787 * s->req->analysers, following the bit order from LSB
788 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200789 * the list when not needed. Any analyser may return 0
790 * to break out of the loop, either because of missing
791 * data to take a decision, or because it decides to
792 * kill the session. We loop at least once through each
793 * analyser, and we may loop again if other analysers
794 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100795 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200796 while (s->req->analysers & ~last_ana) {
797 last_ana = s->req->analysers;
798
799 if (s->req->analysers & AN_REQ_INSPECT) {
800 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200801 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100802 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200803 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804
Willy Tarreaud787e662009-07-07 10:14:51 +0200805 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
806 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200807 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200808 break;
809 }
810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200811 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
812 last_ana |= AN_REQ_HTTP_PROCESS_FE;
813 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
814 break;
815 }
816
817 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
818 last_ana |= AN_REQ_SWITCHING_RULES;
819 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
820 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200821 /* FIXME: we mait switch from TCP to HTTP and want to
822 * immediately loop back to the top. This is a dirty way
823 * of doing it, and we should find a cleaner method relying
824 * on a circular list of function pointers.
825 */
826 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
827 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200828 }
829
830 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
831 last_ana |= AN_REQ_HTTP_PROCESS_BE;
832 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
833 break;
834 }
835
836 if (s->req->analysers & AN_REQ_HTTP_INNER) {
837 last_ana |= AN_REQ_HTTP_INNER;
838 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100839 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200840 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100841
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200842 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
843 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200844 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100845 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200846 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100847
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200848 if (s->req->analysers & AN_REQ_HTTP_BODY) {
849 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200850 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100851 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200852 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100853 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100854 }
Willy Tarreau84455332009-03-15 22:34:05 +0100855
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200856 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
857 rqf_last = s->req->flags;
858 goto resync_request;
859 }
860 }
861
862 resync_response:
863 /* Analyse response */
864
865 if (unlikely(s->rep->flags & BF_HIJACK)) {
866 /* In inject mode, we wake up everytime something has
867 * happened on the write side of the buffer.
868 */
869 unsigned int flags = s->rep->flags;
870
871 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
872 !(s->rep->flags & BF_FULL)) {
873 s->rep->hijacker(s, s->rep);
874 }
875
876 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
877 rpf_last = s->rep->flags;
878 goto resync_response;
879 }
880 }
881 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
882 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
883 unsigned int flags = s->rep->flags;
884
885 if (s->rep->prod->state >= SI_ST_EST) {
886 /* it's up to the analysers to reset write_ena */
887 buffer_write_ena(s->rep);
888 if (s->rep->analysers)
889 process_response(s);
890 }
891
892 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
893 rpf_last = s->rep->flags;
894 goto resync_response;
895 }
896 }
897
898 /* FIXME: here we should call protocol handlers which rely on
899 * both buffers.
900 */
901
902
903 /*
904 * Now we propagate unhandled errors to the session
905 */
906 if (!(s->flags & SN_ERR_MASK)) {
907 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
908 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100909 s->req->analysers = 0;
910 if (s->req->flags & BF_READ_ERROR)
911 s->flags |= SN_ERR_CLICL;
912 else if (s->req->flags & BF_READ_TIMEOUT)
913 s->flags |= SN_ERR_CLITO;
914 else if (s->req->flags & BF_WRITE_ERROR)
915 s->flags |= SN_ERR_SRVCL;
916 else
917 s->flags |= SN_ERR_SRVTO;
918 sess_set_term_flags(s);
919 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200920 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
921 /* Report it if the server got an error or a read timeout expired */
922 s->rep->analysers = 0;
923 if (s->rep->flags & BF_READ_ERROR)
924 s->flags |= SN_ERR_SRVCL;
925 else if (s->rep->flags & BF_READ_TIMEOUT)
926 s->flags |= SN_ERR_SRVTO;
927 else if (s->rep->flags & BF_WRITE_ERROR)
928 s->flags |= SN_ERR_CLICL;
929 else
Willy Tarreau84455332009-03-15 22:34:05 +0100930 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200931 sess_set_term_flags(s);
932 }
Willy Tarreau84455332009-03-15 22:34:05 +0100933 }
934
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200935 /*
936 * Here we take care of forwarding unhandled data. This also includes
937 * connection establishments and shutdown requests.
938 */
939
940
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100941 /* If noone is interested in analysing data, it's time to forward
942 * everything. We will wake up from time to time when either send_max
943 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100944 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100945 if (!s->req->analysers &&
946 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
947 (s->req->prod->state >= SI_ST_EST)) {
948 /* This buffer is freewheeling, there's no analyser nor hijacker
949 * attached to it. If any data are left in, we'll permit them to
950 * move.
951 */
952 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100953
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100954 /* If the producer is still connected, we'll schedule large blocks
955 * of data to be forwarded from the producer to the consumer (which
956 * might possibly not be connected yet).
957 */
958 if (!(s->req->flags & BF_SHUTR) &&
959 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100960 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100961 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100962
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100963 /* check if it is wise to enable kernel splicing to forward request data */
964 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
965 s->req->to_forward &&
966 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200967 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100968 (pipes_used < global.maxpipes) &&
969 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
970 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
971 (s->req->flags & BF_STREAMER_FAST)))) {
972 s->req->flags |= BF_KERN_SPLICING;
973 }
974
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 /* reflect what the L7 analysers have seen last */
976 rqf_last = s->req->flags;
977
978 /*
979 * Now forward all shutdown requests between both sides of the buffer
980 */
981
982 /* first, let's check if the request buffer needs to shutdown(write) */
983 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
984 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
985 buffer_shutw_now(s->req);
986 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
987 (s->req->cons->state == SI_ST_EST) &&
988 s->be->options & PR_O_FORCE_CLO &&
989 s->rep->flags & BF_READ_ACTIVITY) {
990 /* We want to force the connection to the server to close,
991 * and the server has begun to respond. That's the right
992 * time.
993 */
994 buffer_shutw_now(s->req);
995 }
996
997 /* shutdown(write) pending */
998 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
999 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 Tarreau92795622009-03-06 12:51:23 +01001010 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001011 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +01001012 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
1013 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
1014 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1015 else
1016 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
1017 }
1018
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001019
1020 /* we may have a pending connection request, or a connection waiting
1021 * for completion.
1022 */
1023 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1024 do {
1025 /* nb: step 1 might switch from QUE to ASS, but we first want
1026 * to give a chance to step 2 to perform a redirect if needed.
1027 */
1028 if (s->si[1].state != SI_ST_REQ)
1029 sess_update_stream_int(s, &s->si[1]);
1030 if (s->si[1].state == SI_ST_REQ)
1031 sess_prepare_conn_req(s, &s->si[1]);
1032
1033 if (s->si[1].state == SI_ST_ASS && s->srv &&
1034 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1035 perform_http_redirect(s, &s->si[1]);
1036 } while (s->si[1].state == SI_ST_ASS);
1037 }
1038
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001039 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001040 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041 goto resync_stream_interface;
1042
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001043 /* otherwise wewant to check if we need to resync the req buffer or not */
1044 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001045 goto resync_request;
1046
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001047 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001048
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001049 /* If noone is interested in analysing data, it's time to forward
1050 * everything. We will wake up from time to time when either send_max
1051 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001052 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001053 if (!s->rep->analysers &&
1054 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
1055 (s->rep->prod->state >= SI_ST_EST)) {
1056 /* This buffer is freewheeling, there's no analyser nor hijacker
1057 * attached to it. If any data are left in, we'll permit them to
1058 * move.
1059 */
1060 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001061
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001062 /* If the producer is still connected, we'll schedule large blocks
1063 * of data to be forwarded from the producer to the consumer (which
1064 * might possibly not be connected yet).
1065 */
1066 if (!(s->rep->flags & BF_SHUTR) &&
1067 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +01001068 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001069 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001070
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001071 /* check if it is wise to enable kernel splicing to forward response data */
1072 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1073 s->rep->to_forward &&
1074 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001075 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001076 (pipes_used < global.maxpipes) &&
1077 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1078 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1079 (s->rep->flags & BF_STREAMER_FAST)))) {
1080 s->rep->flags |= BF_KERN_SPLICING;
1081 }
1082
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083 /* reflect what the L7 analysers have seen last */
1084 rpf_last = s->rep->flags;
1085
1086 /*
1087 * Now forward all shutdown requests between both sides of the buffer
1088 */
1089
1090 /*
1091 * FIXME: this is probably where we should produce error responses.
1092 */
1093
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001094 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001095 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
1096 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
1097 buffer_shutw_now(s->rep);
1098
1099 /* shutdown(write) pending */
1100 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
1101 s->rep->cons->shutw(s->rep->cons);
1102
1103 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001104 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1105 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001106 buffer_shutr_now(s->rep);
1107
1108 /* shutdown(read) pending */
1109 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1110 s->rep->prod->shutr(s->rep->prod);
1111
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001112 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001113 goto resync_stream_interface;
1114
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001115 if (s->req->flags != rqf_last)
1116 goto resync_request;
1117
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001118 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001119 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001120
1121 /* This is needed only when debugging is enabled, to indicate
1122 * client-side or server-side close. Please note that in the unlikely
1123 * event where both sides would close at once, the sequence is reported
1124 * on the server side first.
1125 */
1126 if (unlikely((global.mode & MODE_DEBUG) &&
1127 (!(global.mode & MODE_QUIET) ||
1128 (global.mode & MODE_VERBOSE)))) {
1129 int len;
1130
1131 if (s->si[1].state == SI_ST_CLO &&
1132 s->si[1].prev_state == SI_ST_EST) {
1133 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1134 s->uniq_id, s->be->id,
1135 (unsigned short)s->si[0].fd,
1136 (unsigned short)s->si[1].fd);
1137 write(1, trash, len);
1138 }
1139
1140 if (s->si[0].state == SI_ST_CLO &&
1141 s->si[0].prev_state == SI_ST_EST) {
1142 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1143 s->uniq_id, s->be->id,
1144 (unsigned short)s->si[0].fd,
1145 (unsigned short)s->si[1].fd);
1146 write(1, trash, len);
1147 }
1148 }
1149
1150 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1151 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1152
1153 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1154 session_process_counters(s);
1155
1156 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001157 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001158
1159 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001160 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001161
Willy Tarreauea388542009-06-21 21:45:58 +02001162 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1163 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001164 s->si[0].prev_state = s->si[0].state;
1165 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001166 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1167 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001168
1169 /* Trick: if a request is being waiting for the server to respond,
1170 * and if we know the server can timeout, we don't want the timeout
1171 * to expire on the client side first, but we're still interested
1172 * in passing data from the client to the server (eg: POST). Thus,
1173 * we can cancel the client's request timeout if the server's
1174 * request timeout is set and the server has not yet sent a response.
1175 */
1176
1177 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001178 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1179 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001180 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001181 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001182
1183 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1184 tick_first(s->rep->rex, s->rep->wex));
1185 if (s->req->analysers)
1186 t->expire = tick_first(t->expire, s->req->analyse_exp);
1187
1188 if (s->si[0].exp)
1189 t->expire = tick_first(t->expire, s->si[0].exp);
1190
1191 if (s->si[1].exp)
1192 t->expire = tick_first(t->expire, s->si[1].exp);
1193
1194#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001195 fprintf(stderr,
1196 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1197 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1198 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1199 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 +01001200#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001201
1202#ifdef DEBUG_DEV
1203 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001204 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001205 ABORT_NOW();
1206#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001207 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001208 }
1209
1210 s->fe->feconn--;
1211 if (s->flags & SN_BE_ASSIGNED)
1212 s->be->beconn--;
1213 actconn--;
1214
1215 if (unlikely((global.mode & MODE_DEBUG) &&
1216 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1217 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001218 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001219 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001220 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001221 write(1, trash, len);
1222 }
1223
1224 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1225 session_process_counters(s);
1226
1227 /* let's do a final log if we need it */
1228 if (s->logs.logwait &&
1229 !(s->flags & SN_MONITOR) &&
1230 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001231 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001232 }
1233
1234 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001235 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001236 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001237 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001238 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001239}
1240
Willy Tarreau7c669d72008-06-20 15:04:11 +02001241/*
1242 * This function adjusts sess->srv_conn and maintains the previous and new
1243 * server's served session counts. Setting newsrv to NULL is enough to release
1244 * current connection slot. This function also notifies any LB algo which might
1245 * expect to be informed about any change in the number of active sessions on a
1246 * server.
1247 */
1248void sess_change_server(struct session *sess, struct server *newsrv)
1249{
1250 if (sess->srv_conn == newsrv)
1251 return;
1252
1253 if (sess->srv_conn) {
1254 sess->srv_conn->served--;
1255 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1256 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1257 sess->srv_conn = NULL;
1258 }
1259
1260 if (newsrv) {
1261 newsrv->served++;
1262 if (newsrv->proxy->lbprm.server_take_conn)
1263 newsrv->proxy->lbprm.server_take_conn(newsrv);
1264 sess->srv_conn = newsrv;
1265 }
1266}
1267
Willy Tarreau84455332009-03-15 22:34:05 +01001268/* Set correct session termination flags in case no analyser has done it. It
1269 * also counts a failed request if the server state has not reached the request
1270 * stage.
1271 */
1272void sess_set_term_flags(struct session *s)
1273{
1274 if (!(s->flags & SN_FINST_MASK)) {
1275 if (s->si[1].state < SI_ST_REQ) {
1276 s->fe->failed_req++;
1277 s->flags |= SN_FINST_R;
1278 }
1279 else if (s->si[1].state == SI_ST_QUE)
1280 s->flags |= SN_FINST_Q;
1281 else if (s->si[1].state < SI_ST_EST)
1282 s->flags |= SN_FINST_C;
1283 else if (s->si[1].state == SI_ST_EST)
1284 s->flags |= SN_FINST_D;
1285 else
1286 s->flags |= SN_FINST_L;
1287 }
1288}
1289
1290/* Handle server-side errors for default protocols. It is called whenever a a
1291 * connection setup is aborted or a request is aborted in queue. It sets the
1292 * session termination flags so that the caller does not have to worry about
1293 * them. It's installed as ->srv_error for the server-side stream_interface.
1294 */
1295void default_srv_error(struct session *s, struct stream_interface *si)
1296{
1297 int err_type = si->err_type;
1298 int err = 0, fin = 0;
1299
1300 if (err_type & SI_ET_QUEUE_ABRT) {
1301 err = SN_ERR_CLICL;
1302 fin = SN_FINST_Q;
1303 }
1304 else if (err_type & SI_ET_CONN_ABRT) {
1305 err = SN_ERR_CLICL;
1306 fin = SN_FINST_C;
1307 }
1308 else if (err_type & SI_ET_QUEUE_TO) {
1309 err = SN_ERR_SRVTO;
1310 fin = SN_FINST_Q;
1311 }
1312 else if (err_type & SI_ET_QUEUE_ERR) {
1313 err = SN_ERR_SRVCL;
1314 fin = SN_FINST_Q;
1315 }
1316 else if (err_type & SI_ET_CONN_TO) {
1317 err = SN_ERR_SRVTO;
1318 fin = SN_FINST_C;
1319 }
1320 else if (err_type & SI_ET_CONN_ERR) {
1321 err = SN_ERR_SRVCL;
1322 fin = SN_FINST_C;
1323 }
1324 else /* SI_ET_CONN_OTHER and others */ {
1325 err = SN_ERR_INTERNAL;
1326 fin = SN_FINST_C;
1327 }
1328
1329 if (!(s->flags & SN_ERR_MASK))
1330 s->flags |= err;
1331 if (!(s->flags & SN_FINST_MASK))
1332 s->flags |= fin;
1333}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001334
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335/*
1336 * Local variables:
1337 * c-indent-level: 8
1338 * c-basic-offset: 8
1339 * End:
1340 */