blob: d4817ac2c380fe95d080278bddd8458c106ca548 [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) {
589 session_set_backend(s, rule->be.backend);
590 break;
591 }
592 }
593
594 /* To ensure correct connection accounting on the backend, we
595 * have to assign one if it was not set (eg: a listen). This
596 * measure also takes care of correctly setting the default
597 * backend if any.
598 */
599 if (!(s->flags & SN_BE_ASSIGNED))
600 session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be);
601 }
602
603 /* we don't want to run the HTTP filters again if the backend has not changed */
604 if (s->fe == s->be)
605 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
606
607 return 1;
608}
609
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100610/* Processes the client, server, request and response jobs of a session task,
611 * then puts it back to the wait queue in a clean state, or cleans up its
612 * resources if it must be deleted. Returns in <next> the date the task wants
613 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
614 * nothing too many times, the request and response buffers flags are monitored
615 * and each function is called only if at least another function has changed at
616 * least one flag it is interested in.
617 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100618struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619{
620 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 unsigned int rqf_last, rpf_last;
622
623 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
624 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
625
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200626 /* This flag must explicitly be set every time */
627 s->req->flags &= ~BF_READ_NOEXP;
628
629 /* Keep a copy of req/rep flags so that we can detect shutdowns */
630 rqf_last = s->req->flags;
631 rpf_last = s->rep->flags;
632
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 /* 1a: Check for low level timeouts if needed. We just set a flag on
634 * stream interfaces when their timeouts have expired.
635 */
636 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
637 stream_int_check_timeouts(&s->si[0]);
638 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200639
640 /* check buffer timeouts, and close the corresponding stream interfaces
641 * for future reads or writes. Note: this will also concern upper layers
642 * but we do not touch any other flag. We must be careful and correctly
643 * detect state changes when calling them.
644 */
645
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200647
648 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
649 s->req->prod->shutr(s->req->prod);
650
651 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
652 s->req->cons->shutw(s->req->cons);
653
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200656 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
657 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100658
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200659 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
660 s->rep->cons->shutw(s->rep->cons);
661 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100662
663 /* 1b: check for low-level errors reported at the stream interface.
664 * First we check if it's a retryable error (in which case we don't
665 * want to tell the buffer). Otherwise we report the error one level
666 * upper by setting flags into the buffers. Note that the side towards
667 * the client cannot have connect (hence retryable) errors. Also, the
668 * connection setup code must be able to deal with any type of abort.
669 */
670 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
671 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
672 s->si[0].shutr(&s->si[0]);
673 s->si[0].shutw(&s->si[0]);
674 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100675 if (!(s->req->analysers) && !(s->rep->analysers)) {
676 if (!(s->flags & SN_ERR_MASK))
677 s->flags |= SN_ERR_CLICL;
678 if (!(s->flags & SN_FINST_MASK))
679 s->flags |= SN_FINST_D;
680 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 }
682 }
683
684 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
685 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
686 s->si[1].shutr(&s->si[1]);
687 s->si[1].shutw(&s->si[1]);
688 stream_int_report_error(&s->si[1]);
689 s->be->failed_resp++;
690 if (s->srv)
691 s->srv->failed_resp++;
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_SRVCL;
695 if (!(s->flags & SN_FINST_MASK))
696 s->flags |= SN_FINST_D;
697 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 }
699 /* note: maybe we should process connection errors here ? */
700 }
701
702 if (s->si[1].state == SI_ST_CON) {
703 /* we were trying to establish a connection on the server side,
704 * maybe it succeeded, maybe it failed, maybe we timed out, ...
705 */
706 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
707 sess_update_st_cer(s, &s->si[1]);
708 else if (s->si[1].state == SI_ST_EST)
709 sess_establish(s, &s->si[1]);
710
711 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
712 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
713 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
714 */
715 }
716
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200717resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 /* Check for connection closure */
719
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 DPRINTF(stderr,
721 "[%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",
722 now_ms, __FUNCTION__, __LINE__,
723 t,
724 s, s->flags,
725 s->req, s->rep,
726 s->req->rex, s->rep->wex,
727 s->req->flags, s->rep->flags,
728 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
729 s->rep->cons->err_type, s->req->cons->err_type,
730 s->conn_retries);
731
732 /* nothing special to be done on client side */
733 if (unlikely(s->req->prod->state == SI_ST_DIS))
734 s->req->prod->state = SI_ST_CLO;
735
736 /* When a server-side connection is released, we have to count it and
737 * check for pending connections on this server.
738 */
739 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
740 s->req->cons->state = SI_ST_CLO;
741 if (s->srv) {
742 if (s->flags & SN_CURR_SESS) {
743 s->flags &= ~SN_CURR_SESS;
744 s->srv->cur_sess--;
745 }
746 sess_change_server(s, NULL);
747 if (may_dequeue_tasks(s->srv, s->be))
748 process_srv_queue(s->srv);
749 }
750 }
751
752 /*
753 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
754 * at this point.
755 */
756
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100757 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 /* Analyse request */
759 if ((s->req->flags & BF_MASK_ANALYSER) ||
760 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
761 unsigned int flags = s->req->flags;
762
763 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200764 unsigned int last_ana = 0;
765
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100766 /* it's up to the analysers to reset write_ena */
767 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100768
769 /* We will call all analysers for which a bit is set in
770 * s->req->analysers, following the bit order from LSB
771 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200772 * the list when not needed. Any analyser may return 0
773 * to break out of the loop, either because of missing
774 * data to take a decision, or because it decides to
775 * kill the session. We loop at least once through each
776 * analyser, and we may loop again if other analysers
777 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100778 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200779 while (s->req->analysers & ~last_ana) {
780 last_ana = s->req->analysers;
781
782 if (s->req->analysers & AN_REQ_INSPECT) {
783 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200784 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100785 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200786 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100787
Willy Tarreaud787e662009-07-07 10:14:51 +0200788 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
789 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200790 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200791 break;
792 }
793
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200794 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
795 last_ana |= AN_REQ_HTTP_PROCESS_FE;
796 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
797 break;
798 }
799
800 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
801 last_ana |= AN_REQ_SWITCHING_RULES;
802 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
803 break;
804 }
805
806 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
807 last_ana |= AN_REQ_HTTP_PROCESS_BE;
808 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
809 break;
810 }
811
812 if (s->req->analysers & AN_REQ_HTTP_INNER) {
813 last_ana |= AN_REQ_HTTP_INNER;
814 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100815 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200816 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100817
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200818 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
819 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200820 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100821 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200822 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100823
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200824 if (s->req->analysers & AN_REQ_HTTP_BODY) {
825 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200826 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100827 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200828 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100829 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 }
Willy Tarreau84455332009-03-15 22:34:05 +0100831
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200832 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
833 rqf_last = s->req->flags;
834 goto resync_request;
835 }
836 }
837
838 resync_response:
839 /* Analyse response */
840
841 if (unlikely(s->rep->flags & BF_HIJACK)) {
842 /* In inject mode, we wake up everytime something has
843 * happened on the write side of the buffer.
844 */
845 unsigned int flags = s->rep->flags;
846
847 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
848 !(s->rep->flags & BF_FULL)) {
849 s->rep->hijacker(s, s->rep);
850 }
851
852 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
853 rpf_last = s->rep->flags;
854 goto resync_response;
855 }
856 }
857 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
858 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
859 unsigned int flags = s->rep->flags;
860
861 if (s->rep->prod->state >= SI_ST_EST) {
862 /* it's up to the analysers to reset write_ena */
863 buffer_write_ena(s->rep);
864 if (s->rep->analysers)
865 process_response(s);
866 }
867
868 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
869 rpf_last = s->rep->flags;
870 goto resync_response;
871 }
872 }
873
874 /* FIXME: here we should call protocol handlers which rely on
875 * both buffers.
876 */
877
878
879 /*
880 * Now we propagate unhandled errors to the session
881 */
882 if (!(s->flags & SN_ERR_MASK)) {
883 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
884 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100885 s->req->analysers = 0;
886 if (s->req->flags & BF_READ_ERROR)
887 s->flags |= SN_ERR_CLICL;
888 else if (s->req->flags & BF_READ_TIMEOUT)
889 s->flags |= SN_ERR_CLITO;
890 else if (s->req->flags & BF_WRITE_ERROR)
891 s->flags |= SN_ERR_SRVCL;
892 else
893 s->flags |= SN_ERR_SRVTO;
894 sess_set_term_flags(s);
895 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200896 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
897 /* Report it if the server got an error or a read timeout expired */
898 s->rep->analysers = 0;
899 if (s->rep->flags & BF_READ_ERROR)
900 s->flags |= SN_ERR_SRVCL;
901 else if (s->rep->flags & BF_READ_TIMEOUT)
902 s->flags |= SN_ERR_SRVTO;
903 else if (s->rep->flags & BF_WRITE_ERROR)
904 s->flags |= SN_ERR_CLICL;
905 else
Willy Tarreau84455332009-03-15 22:34:05 +0100906 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200907 sess_set_term_flags(s);
908 }
Willy Tarreau84455332009-03-15 22:34:05 +0100909 }
910
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200911 /*
912 * Here we take care of forwarding unhandled data. This also includes
913 * connection establishments and shutdown requests.
914 */
915
916
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100917 /* If noone is interested in analysing data, it's time to forward
918 * everything. We will wake up from time to time when either send_max
919 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100920 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100921 if (!s->req->analysers &&
922 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
923 (s->req->prod->state >= SI_ST_EST)) {
924 /* This buffer is freewheeling, there's no analyser nor hijacker
925 * attached to it. If any data are left in, we'll permit them to
926 * move.
927 */
928 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100929
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100930 /* If the producer is still connected, we'll schedule large blocks
931 * of data to be forwarded from the producer to the consumer (which
932 * might possibly not be connected yet).
933 */
934 if (!(s->req->flags & BF_SHUTR) &&
935 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100936 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100937 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100938
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100939 /* check if it is wise to enable kernel splicing to forward request data */
940 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
941 s->req->to_forward &&
942 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200943 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100944 (pipes_used < global.maxpipes) &&
945 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
946 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
947 (s->req->flags & BF_STREAMER_FAST)))) {
948 s->req->flags |= BF_KERN_SPLICING;
949 }
950
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951 /* reflect what the L7 analysers have seen last */
952 rqf_last = s->req->flags;
953
954 /*
955 * Now forward all shutdown requests between both sides of the buffer
956 */
957
958 /* first, let's check if the request buffer needs to shutdown(write) */
959 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
960 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
961 buffer_shutw_now(s->req);
962 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
963 (s->req->cons->state == SI_ST_EST) &&
964 s->be->options & PR_O_FORCE_CLO &&
965 s->rep->flags & BF_READ_ACTIVITY) {
966 /* We want to force the connection to the server to close,
967 * and the server has begun to respond. That's the right
968 * time.
969 */
970 buffer_shutw_now(s->req);
971 }
972
973 /* shutdown(write) pending */
974 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
975 s->req->cons->shutw(s->req->cons);
976
977 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100978 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
979 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100980 buffer_shutr_now(s->req);
981
982 /* shutdown(read) pending */
983 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
984 s->req->prod->shutr(s->req->prod);
985
Willy Tarreau92795622009-03-06 12:51:23 +0100986 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100987 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100988 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
989 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
990 s->req->cons->state = SI_ST_REQ; /* new connection requested */
991 else
992 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
993 }
994
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995
996 /* we may have a pending connection request, or a connection waiting
997 * for completion.
998 */
999 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1000 do {
1001 /* nb: step 1 might switch from QUE to ASS, but we first want
1002 * to give a chance to step 2 to perform a redirect if needed.
1003 */
1004 if (s->si[1].state != SI_ST_REQ)
1005 sess_update_stream_int(s, &s->si[1]);
1006 if (s->si[1].state == SI_ST_REQ)
1007 sess_prepare_conn_req(s, &s->si[1]);
1008
1009 if (s->si[1].state == SI_ST_ASS && s->srv &&
1010 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1011 perform_http_redirect(s, &s->si[1]);
1012 } while (s->si[1].state == SI_ST_ASS);
1013 }
1014
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001015 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001016 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017 goto resync_stream_interface;
1018
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001019 /* otherwise wewant to check if we need to resync the req buffer or not */
1020 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001021 goto resync_request;
1022
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001023 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001024
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001025 /* If noone is interested in analysing data, it's time to forward
1026 * everything. We will wake up from time to time when either send_max
1027 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001028 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001029 if (!s->rep->analysers &&
1030 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
1031 (s->rep->prod->state >= SI_ST_EST)) {
1032 /* This buffer is freewheeling, there's no analyser nor hijacker
1033 * attached to it. If any data are left in, we'll permit them to
1034 * move.
1035 */
1036 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001037
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001038 /* If the producer is still connected, we'll schedule large blocks
1039 * of data to be forwarded from the producer to the consumer (which
1040 * might possibly not be connected yet).
1041 */
1042 if (!(s->rep->flags & BF_SHUTR) &&
1043 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +01001044 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001045 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001046
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001047 /* check if it is wise to enable kernel splicing to forward response data */
1048 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1049 s->rep->to_forward &&
1050 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001051 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001052 (pipes_used < global.maxpipes) &&
1053 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1054 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1055 (s->rep->flags & BF_STREAMER_FAST)))) {
1056 s->rep->flags |= BF_KERN_SPLICING;
1057 }
1058
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001059 /* reflect what the L7 analysers have seen last */
1060 rpf_last = s->rep->flags;
1061
1062 /*
1063 * Now forward all shutdown requests between both sides of the buffer
1064 */
1065
1066 /*
1067 * FIXME: this is probably where we should produce error responses.
1068 */
1069
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001070 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001071 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
1072 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
1073 buffer_shutw_now(s->rep);
1074
1075 /* shutdown(write) pending */
1076 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
1077 s->rep->cons->shutw(s->rep->cons);
1078
1079 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001080 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1081 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001082 buffer_shutr_now(s->rep);
1083
1084 /* shutdown(read) pending */
1085 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1086 s->rep->prod->shutr(s->rep->prod);
1087
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001088 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001089 goto resync_stream_interface;
1090
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001091 if (s->req->flags != rqf_last)
1092 goto resync_request;
1093
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001094 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001095 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001096
1097 /* This is needed only when debugging is enabled, to indicate
1098 * client-side or server-side close. Please note that in the unlikely
1099 * event where both sides would close at once, the sequence is reported
1100 * on the server side first.
1101 */
1102 if (unlikely((global.mode & MODE_DEBUG) &&
1103 (!(global.mode & MODE_QUIET) ||
1104 (global.mode & MODE_VERBOSE)))) {
1105 int len;
1106
1107 if (s->si[1].state == SI_ST_CLO &&
1108 s->si[1].prev_state == SI_ST_EST) {
1109 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1110 s->uniq_id, s->be->id,
1111 (unsigned short)s->si[0].fd,
1112 (unsigned short)s->si[1].fd);
1113 write(1, trash, len);
1114 }
1115
1116 if (s->si[0].state == SI_ST_CLO &&
1117 s->si[0].prev_state == SI_ST_EST) {
1118 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1119 s->uniq_id, s->be->id,
1120 (unsigned short)s->si[0].fd,
1121 (unsigned short)s->si[1].fd);
1122 write(1, trash, len);
1123 }
1124 }
1125
1126 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1127 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1128
1129 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1130 session_process_counters(s);
1131
1132 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001133 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134
1135 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001136 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001137
Willy Tarreauea388542009-06-21 21:45:58 +02001138 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1139 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001140 s->si[0].prev_state = s->si[0].state;
1141 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001142 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1143 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001144
1145 /* Trick: if a request is being waiting for the server to respond,
1146 * and if we know the server can timeout, we don't want the timeout
1147 * to expire on the client side first, but we're still interested
1148 * in passing data from the client to the server (eg: POST). Thus,
1149 * we can cancel the client's request timeout if the server's
1150 * request timeout is set and the server has not yet sent a response.
1151 */
1152
1153 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001154 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1155 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001156 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001157 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001158
1159 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1160 tick_first(s->rep->rex, s->rep->wex));
1161 if (s->req->analysers)
1162 t->expire = tick_first(t->expire, s->req->analyse_exp);
1163
1164 if (s->si[0].exp)
1165 t->expire = tick_first(t->expire, s->si[0].exp);
1166
1167 if (s->si[1].exp)
1168 t->expire = tick_first(t->expire, s->si[1].exp);
1169
1170#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001171 fprintf(stderr,
1172 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1173 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1174 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1175 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 +01001176#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001177
1178#ifdef DEBUG_DEV
1179 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001180 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001181 ABORT_NOW();
1182#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001183 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001184 }
1185
1186 s->fe->feconn--;
1187 if (s->flags & SN_BE_ASSIGNED)
1188 s->be->beconn--;
1189 actconn--;
1190
1191 if (unlikely((global.mode & MODE_DEBUG) &&
1192 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1193 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001194 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001195 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001196 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001197 write(1, trash, len);
1198 }
1199
1200 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1201 session_process_counters(s);
1202
1203 /* let's do a final log if we need it */
1204 if (s->logs.logwait &&
1205 !(s->flags & SN_MONITOR) &&
1206 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001207 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001208 }
1209
1210 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001211 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001212 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001214 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001215}
1216
Willy Tarreau7c669d72008-06-20 15:04:11 +02001217/*
1218 * This function adjusts sess->srv_conn and maintains the previous and new
1219 * server's served session counts. Setting newsrv to NULL is enough to release
1220 * current connection slot. This function also notifies any LB algo which might
1221 * expect to be informed about any change in the number of active sessions on a
1222 * server.
1223 */
1224void sess_change_server(struct session *sess, struct server *newsrv)
1225{
1226 if (sess->srv_conn == newsrv)
1227 return;
1228
1229 if (sess->srv_conn) {
1230 sess->srv_conn->served--;
1231 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1232 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1233 sess->srv_conn = NULL;
1234 }
1235
1236 if (newsrv) {
1237 newsrv->served++;
1238 if (newsrv->proxy->lbprm.server_take_conn)
1239 newsrv->proxy->lbprm.server_take_conn(newsrv);
1240 sess->srv_conn = newsrv;
1241 }
1242}
1243
Willy Tarreau84455332009-03-15 22:34:05 +01001244/* Set correct session termination flags in case no analyser has done it. It
1245 * also counts a failed request if the server state has not reached the request
1246 * stage.
1247 */
1248void sess_set_term_flags(struct session *s)
1249{
1250 if (!(s->flags & SN_FINST_MASK)) {
1251 if (s->si[1].state < SI_ST_REQ) {
1252 s->fe->failed_req++;
1253 s->flags |= SN_FINST_R;
1254 }
1255 else if (s->si[1].state == SI_ST_QUE)
1256 s->flags |= SN_FINST_Q;
1257 else if (s->si[1].state < SI_ST_EST)
1258 s->flags |= SN_FINST_C;
1259 else if (s->si[1].state == SI_ST_EST)
1260 s->flags |= SN_FINST_D;
1261 else
1262 s->flags |= SN_FINST_L;
1263 }
1264}
1265
1266/* Handle server-side errors for default protocols. It is called whenever a a
1267 * connection setup is aborted or a request is aborted in queue. It sets the
1268 * session termination flags so that the caller does not have to worry about
1269 * them. It's installed as ->srv_error for the server-side stream_interface.
1270 */
1271void default_srv_error(struct session *s, struct stream_interface *si)
1272{
1273 int err_type = si->err_type;
1274 int err = 0, fin = 0;
1275
1276 if (err_type & SI_ET_QUEUE_ABRT) {
1277 err = SN_ERR_CLICL;
1278 fin = SN_FINST_Q;
1279 }
1280 else if (err_type & SI_ET_CONN_ABRT) {
1281 err = SN_ERR_CLICL;
1282 fin = SN_FINST_C;
1283 }
1284 else if (err_type & SI_ET_QUEUE_TO) {
1285 err = SN_ERR_SRVTO;
1286 fin = SN_FINST_Q;
1287 }
1288 else if (err_type & SI_ET_QUEUE_ERR) {
1289 err = SN_ERR_SRVCL;
1290 fin = SN_FINST_Q;
1291 }
1292 else if (err_type & SI_ET_CONN_TO) {
1293 err = SN_ERR_SRVTO;
1294 fin = SN_FINST_C;
1295 }
1296 else if (err_type & SI_ET_CONN_ERR) {
1297 err = SN_ERR_SRVCL;
1298 fin = SN_FINST_C;
1299 }
1300 else /* SI_ET_CONN_OTHER and others */ {
1301 err = SN_ERR_INTERNAL;
1302 fin = SN_FINST_C;
1303 }
1304
1305 if (!(s->flags & SN_ERR_MASK))
1306 s->flags |= err;
1307 if (!(s->flags & SN_FINST_MASK))
1308 s->flags |= fin;
1309}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001310
Willy Tarreaubaaee002006-06-26 02:48:02 +02001311/*
1312 * Local variables:
1313 * c-indent-level: 8
1314 * c-basic-offset: 8
1315 * End:
1316 */