blob: d1bc052aa382a55542c85fab62f88829f1945659 [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 Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020023#include <proto/buffers.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010024#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020025#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010027#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010028#include <proto/proto_http.h>
29#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <proto/queue.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010031#include <proto/stream_interface.h>
32#include <proto/stream_sock.h>
33#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
Willy Tarreau3ab68cf2009-01-25 16:03:28 +010035#ifdef CONFIG_HAP_TCPSPLICE
36#include <libtcpsplice.h>
37#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020039struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010040struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42/*
43 * frees the context associated to a session. It must have been removed first.
44 */
45void session_free(struct session *s)
46{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010047 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020048 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010049 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010050
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 if (s->pend_pos)
52 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010053
Willy Tarreau1e62de62008-11-11 20:20:02 +010054 if (s->srv) { /* there may be requests left pending in queue */
55 if (s->flags & SN_CURR_SESS) {
56 s->flags &= ~SN_CURR_SESS;
57 s->srv->cur_sess--;
58 }
Willy Tarreau922a8062008-12-04 09:33:58 +010059 if (may_dequeue_tasks(s->srv, s->be))
60 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010061 }
Willy Tarreau922a8062008-12-04 09:33:58 +010062
Willy Tarreau7c669d72008-06-20 15:04:11 +020063 if (unlikely(s->srv_conn)) {
64 /* the session still has a reserved slot on a server, but
65 * it should normally be only the same as the one above,
66 * so this should not happen in fact.
67 */
68 sess_change_server(s, NULL);
69 }
70
Willy Tarreau3eba98a2009-01-25 13:56:13 +010071 if (s->req->pipe)
72 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010073
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 if (s->rep->pipe)
75 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010076
Willy Tarreau48d63db2008-08-03 17:41:33 +020077 pool_free2(pool2_buffer, s->req);
78 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Willy Tarreau92fb9832007-10-16 17:34:28 +020080 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020081 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010082
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 if (txn->rsp.cap != NULL) {
84 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020085 for (h = fe->rsp_cap; h; h = h->next)
86 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020087 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020089 if (txn->req.cap != NULL) {
90 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020091 for (h = fe->req_cap; h; h = h->next)
92 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020093 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020096 pool_free2(pool2_requri, txn->uri);
97 pool_free2(pool2_capture, txn->cli_cookie);
98 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010099
100 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
101 LIST_DEL(&bref->users);
102 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
103 bref->ref = s->list.n;
104 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100105 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200106 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200107
108 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200109 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200110 pool_flush2(pool2_buffer);
111 pool_flush2(fe->hdr_idx_pool);
112 pool_flush2(pool2_requri);
113 pool_flush2(pool2_capture);
114 pool_flush2(pool2_session);
115 pool_flush2(fe->req_cap_pool);
116 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200117 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200118}
119
120
121/* perform minimal intializations, report 0 in case of error, 1 if OK. */
122int init_session()
123{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100124 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200125 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
126 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127}
128
Willy Tarreau30e71012007-11-26 20:15:35 +0100129void session_process_counters(struct session *s)
130{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100131 unsigned long long bytes;
132
Willy Tarreau30e71012007-11-26 20:15:35 +0100133 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100134 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100135 s->logs.bytes_in = s->req->total;
136 if (bytes) {
137 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100138
Willy Tarreau30e71012007-11-26 20:15:35 +0100139 if (s->be != s->fe)
140 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100141
Willy Tarreau30e71012007-11-26 20:15:35 +0100142 if (s->srv)
143 s->srv->bytes_in += bytes;
144 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100145 }
146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100148 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100149 s->logs.bytes_out = s->rep->total;
150 if (bytes) {
151 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100152
Willy Tarreau30e71012007-11-26 20:15:35 +0100153 if (s->be != s->fe)
154 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100155
Willy Tarreau30e71012007-11-26 20:15:35 +0100156 if (s->srv)
157 s->srv->bytes_out += bytes;
158 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100159 }
160}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100162/* This function is called with (si->state == SI_ST_CON) meaning that a
163 * connection was attempted and that the file descriptor is already allocated.
164 * We must check for establishment, error and abort. Possible output states
165 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
166 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
167 * otherwise 1.
168 */
169int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
170{
171 struct buffer *req = si->ob;
172 struct buffer *rep = si->ib;
173
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100174 /* If we got an error, or if nothing happened and the connection timed
175 * out, we must give up. The CER state handler will take care of retry
176 * attempts and error reports.
177 */
178 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
179 si->state = SI_ST_CER;
180 fd_delete(si->fd);
181
182 if (si->err_type)
183 return 0;
184
185 si->err_loc = s->srv;
186 if (si->flags & SI_FL_ERR)
187 si->err_type = SI_ET_CONN_ERR;
188 else
189 si->err_type = SI_ET_CONN_TO;
190 return 0;
191 }
192
193 /* OK, maybe we want to abort */
194 if (unlikely((req->flags & BF_SHUTW_NOW) ||
195 (rep->flags & BF_SHUTW) ||
196 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100197 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100198 s->be->options & PR_O_ABRT_CLOSE)))) {
199 /* give up */
200 si->shutw(si);
201 si->err_type |= SI_ET_CONN_ABRT;
202 si->err_loc = s->srv;
203 return 1;
204 }
205
206 /* we need to wait a bit more if there was no activity either */
207 if (!(req->flags & BF_WRITE_ACTIVITY))
208 return 1;
209
210 /* OK, this means that a connection succeeded. The caller will be
211 * responsible for handling the transition from CON to EST.
212 */
213 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
214 si->state = SI_ST_EST;
215 si->err_type = SI_ET_NONE;
216 si->err_loc = NULL;
217 return 1;
218}
219
220/* This function is called with (si->state == SI_ST_CER) meaning that a
221 * previous connection attempt has failed and that the file descriptor
222 * has already been released. Possible causes include asynchronous error
223 * notification and time out. Possible output states are SI_ST_CLO when
224 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
225 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
226 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
227 * marked as in error state. It returns 0.
228 */
229int sess_update_st_cer(struct session *s, struct stream_interface *si)
230{
231 /* we probably have to release last session from the server */
232 if (s->srv) {
233 if (s->flags & SN_CURR_SESS) {
234 s->flags &= ~SN_CURR_SESS;
235 s->srv->cur_sess--;
236 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100237 }
238
239 /* ensure that we have enough retries left */
240 s->conn_retries--;
241 if (s->conn_retries < 0) {
242 if (!si->err_type) {
243 si->err_type = SI_ET_CONN_ERR;
244 si->err_loc = s->srv;
245 }
246
247 if (s->srv)
248 s->srv->failed_conns++;
249 s->be->failed_conns++;
250 if (may_dequeue_tasks(s->srv, s->be))
251 process_srv_queue(s->srv);
252
253 /* shutw is enough so stop a connecting socket */
254 si->shutw(si);
255 si->ob->flags |= BF_WRITE_ERROR;
256 si->ib->flags |= BF_READ_ERROR;
257
258 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100259 if (s->srv_error)
260 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100261 return 0;
262 }
263
264 /* If the "redispatch" option is set on the backend, we are allowed to
265 * retry on another server for the last retry. In order to achieve this,
266 * we must mark the session unassigned, and eventually clear the DIRECT
267 * bit to ignore any persistence cookie. We won't count a retry nor a
268 * redispatch yet, because this will depend on what server is selected.
269 */
270 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
271 if (may_dequeue_tasks(s->srv, s->be))
272 process_srv_queue(s->srv);
273
274 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
275 s->prev_srv = s->srv;
276 si->state = SI_ST_REQ;
277 } else {
278 if (s->srv)
279 s->srv->retries++;
280 s->be->retries++;
281 si->state = SI_ST_ASS;
282 }
283
284 if (si->flags & SI_FL_ERR) {
285 /* The error was an asynchronous connection error, and we will
286 * likely have to retry connecting to the same server, most
287 * likely leading to the same result. To avoid this, we wait
288 * one second before retrying.
289 */
290
291 if (!si->err_type)
292 si->err_type = SI_ET_CONN_ERR;
293
294 si->state = SI_ST_TAR;
295 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
296 return 0;
297 }
298 return 0;
299}
300
301/*
302 * This function handles the transition between the SI_ST_CON state and the
303 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
304 * SI_ST_EST.
305 */
306void sess_establish(struct session *s, struct stream_interface *si)
307{
308 struct buffer *req = si->ob;
309 struct buffer *rep = si->ib;
310
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100311 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100312 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
313
314 /* if the user wants to log as soon as possible, without counting
315 * bytes from the server, then this is the right moment. */
316 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
317 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100318 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100319 }
320#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100321 if ((global.tune.options & GTUNE_USE_SPLICE) &&
322 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100323 /* TCP splicing supported by both FE and BE */
324 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
325 }
326#endif
327 }
328 else {
329 rep->analysers |= AN_RTR_HTTP_HDR;
330 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
331 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
332 /* reset hdr_idx which was already initialized by the request.
333 * right now, the http parser does it.
334 * hdr_idx_init(&s->txn.hdr_idx);
335 */
336 }
337
338 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
339 req->wex = TICK_ETERNITY;
340}
341
342/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
343 * Other input states are simply ignored.
344 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
345 * Flags must have previously been updated for timeouts and other conditions.
346 */
347void sess_update_stream_int(struct session *s, struct stream_interface *si)
348{
349 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",
350 now_ms, __FUNCTION__,
351 s,
352 s->req, s->rep,
353 s->req->rex, s->rep->wex,
354 s->req->flags, s->rep->flags,
355 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
356
357 if (si->state == SI_ST_ASS) {
358 /* Server assigned to connection request, we have to try to connect now */
359 int conn_err;
360
361 conn_err = connect_server(s);
362 if (conn_err == SN_ERR_NONE) {
363 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100364 if (s->srv)
365 s->srv->cum_sess++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100366 return;
367 }
368
369 /* We have received a synchronous error. We might have to
370 * abort, retry immediately or redispatch.
371 */
372 if (conn_err == SN_ERR_INTERNAL) {
373 if (!si->err_type) {
374 si->err_type = SI_ET_CONN_OTHER;
375 si->err_loc = s->srv;
376 }
377
378 if (s->srv)
379 s->srv->cum_sess++;
380 if (s->srv)
381 s->srv->failed_conns++;
382 s->be->failed_conns++;
383
384 /* release other sessions waiting for this server */
385 if (may_dequeue_tasks(s->srv, s->be))
386 process_srv_queue(s->srv);
387
388 /* Failed and not retryable. */
389 si->shutr(si);
390 si->shutw(si);
391 si->ob->flags |= BF_WRITE_ERROR;
392
393 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
394
395 /* no session was ever accounted for this server */
396 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100397 if (s->srv_error)
398 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100399 return;
400 }
401
402 /* We are facing a retryable error, but we don't want to run a
403 * turn-around now, as the problem is likely a source port
404 * allocation problem, so we want to retry now.
405 */
406 si->state = SI_ST_CER;
407 si->flags &= ~SI_FL_ERR;
408 sess_update_st_cer(s, si);
409 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
410 return;
411 }
412 else if (si->state == SI_ST_QUE) {
413 /* connection request was queued, check for any update */
414 if (!s->pend_pos) {
415 /* The connection is not in the queue anymore. Either
416 * we have a server connection slot available and we
417 * go directly to the assigned state, or we need to
418 * load-balance first and go to the INI state.
419 */
420 si->exp = TICK_ETERNITY;
421 if (unlikely(!(s->flags & SN_ASSIGNED)))
422 si->state = SI_ST_REQ;
423 else {
424 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
425 si->state = SI_ST_ASS;
426 }
427 return;
428 }
429
430 /* Connection request still in queue... */
431 if (si->flags & SI_FL_EXP) {
432 /* ... and timeout expired */
433 si->exp = TICK_ETERNITY;
434 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
435 if (s->srv)
436 s->srv->failed_conns++;
437 s->be->failed_conns++;
438 si->shutr(si);
439 si->shutw(si);
440 si->ob->flags |= BF_WRITE_TIMEOUT;
441 if (!si->err_type)
442 si->err_type = SI_ET_QUEUE_TO;
443 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100444 if (s->srv_error)
445 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100446 return;
447 }
448
449 /* Connection remains in queue, check if we have to abort it */
450 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
451 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
452 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
453 /* give up */
454 si->exp = TICK_ETERNITY;
455 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
456 si->shutr(si);
457 si->shutw(si);
458 si->err_type |= SI_ET_QUEUE_ABRT;
459 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100460 if (s->srv_error)
461 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100462 return;
463 }
464
465 /* Nothing changed */
466 return;
467 }
468 else if (si->state == SI_ST_TAR) {
469 /* Connection request might be aborted */
470 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
471 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
472 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
473 /* give up */
474 si->exp = TICK_ETERNITY;
475 si->shutr(si);
476 si->shutw(si);
477 si->err_type |= SI_ET_CONN_ABRT;
478 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100479 if (s->srv_error)
480 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100481 return;
482 }
483
484 if (!(si->flags & SI_FL_EXP))
485 return; /* still in turn-around */
486
487 si->exp = TICK_ETERNITY;
488
489 /* we keep trying on the same server as long as the session is
490 * marked "assigned".
491 * FIXME: Should we force a redispatch attempt when the server is down ?
492 */
493 if (s->flags & SN_ASSIGNED)
494 si->state = SI_ST_ASS;
495 else
496 si->state = SI_ST_REQ;
497 return;
498 }
499}
500
501/* This function initiates a server connection request on a stream interface
502 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
503 * indicating that a server has been assigned. It may also return SI_ST_QUE,
504 * or SI_ST_CLO upon error.
505 */
506static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
507 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",
508 now_ms, __FUNCTION__,
509 s,
510 s->req, s->rep,
511 s->req->rex, s->rep->wex,
512 s->req->flags, s->rep->flags,
513 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
514
515 if (si->state != SI_ST_REQ)
516 return;
517
518 /* Try to assign a server */
519 if (srv_redispatch_connect(s) != 0) {
520 /* We did not get a server. Either we queued the
521 * connection request, or we encountered an error.
522 */
523 if (si->state == SI_ST_QUE)
524 return;
525
526 /* we did not get any server, let's check the cause */
527 si->shutr(si);
528 si->shutw(si);
529 si->ob->flags |= BF_WRITE_ERROR;
530 if (!si->err_type)
531 si->err_type = SI_ET_CONN_OTHER;
532 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100533 if (s->srv_error)
534 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 return;
536 }
537
538 /* The server is assigned */
539 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
540 si->state = SI_ST_ASS;
541}
542
543/* Processes the client, server, request and response jobs of a session task,
544 * then puts it back to the wait queue in a clean state, or cleans up its
545 * resources if it must be deleted. Returns in <next> the date the task wants
546 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
547 * nothing too many times, the request and response buffers flags are monitored
548 * and each function is called only if at least another function has changed at
549 * least one flag it is interested in.
550 */
551void process_session(struct task *t, int *next)
552{
553 struct session *s = t->context;
554 int resync;
555 unsigned int rqf_last, rpf_last;
556
557 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
558 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
559
560 /* 1a: Check for low level timeouts if needed. We just set a flag on
561 * stream interfaces when their timeouts have expired.
562 */
563 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
564 stream_int_check_timeouts(&s->si[0]);
565 stream_int_check_timeouts(&s->si[1]);
566 buffer_check_timeouts(s->req);
567 buffer_check_timeouts(s->rep);
568 }
569
Willy Tarreau86491c32008-12-14 09:04:47 +0100570 s->req->flags &= ~BF_READ_NOEXP;
571
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 /* copy req/rep flags so that we can detect shutdowns */
573 rqf_last = s->req->flags;
574 rpf_last = s->rep->flags;
575
576 /* 1b: check for low-level errors reported at the stream interface.
577 * First we check if it's a retryable error (in which case we don't
578 * want to tell the buffer). Otherwise we report the error one level
579 * upper by setting flags into the buffers. Note that the side towards
580 * the client cannot have connect (hence retryable) errors. Also, the
581 * connection setup code must be able to deal with any type of abort.
582 */
583 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
584 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
585 s->si[0].shutr(&s->si[0]);
586 s->si[0].shutw(&s->si[0]);
587 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100588 if (!(s->req->analysers) && !(s->rep->analysers)) {
589 if (!(s->flags & SN_ERR_MASK))
590 s->flags |= SN_ERR_CLICL;
591 if (!(s->flags & SN_FINST_MASK))
592 s->flags |= SN_FINST_D;
593 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 }
595 }
596
597 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
598 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
599 s->si[1].shutr(&s->si[1]);
600 s->si[1].shutw(&s->si[1]);
601 stream_int_report_error(&s->si[1]);
602 s->be->failed_resp++;
603 if (s->srv)
604 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100605 if (!(s->req->analysers) && !(s->rep->analysers)) {
606 if (!(s->flags & SN_ERR_MASK))
607 s->flags |= SN_ERR_SRVCL;
608 if (!(s->flags & SN_FINST_MASK))
609 s->flags |= SN_FINST_D;
610 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 }
612 /* note: maybe we should process connection errors here ? */
613 }
614
615 if (s->si[1].state == SI_ST_CON) {
616 /* we were trying to establish a connection on the server side,
617 * maybe it succeeded, maybe it failed, maybe we timed out, ...
618 */
619 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
620 sess_update_st_cer(s, &s->si[1]);
621 else if (s->si[1].state == SI_ST_EST)
622 sess_establish(s, &s->si[1]);
623
624 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
625 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
626 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
627 */
628 }
629
630 /* check buffer timeouts, and close the corresponding stream interfaces
631 * for future reads or writes. Note: this will also concern upper layers
632 * but we do not touch any other flag. We must be careful and correctly
633 * detect state changes when calling them.
634 */
635 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
636 if (s->req->flags & BF_READ_TIMEOUT)
637 s->req->prod->shutr(s->req->prod);
638 if (s->req->flags & BF_WRITE_TIMEOUT)
639 s->req->cons->shutw(s->req->cons);
640 DPRINTF(stderr,
641 "[%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",
642 now_ms, __FUNCTION__, __LINE__,
643 t,
644 s, s->flags,
645 s->req, s->rep,
646 s->req->rex, s->rep->wex,
647 s->req->flags, s->rep->flags,
648 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
649 s->rep->cons->err_type, s->req->cons->err_type,
650 s->conn_retries);
651 }
652
653 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
654 if (s->rep->flags & BF_READ_TIMEOUT)
655 s->rep->prod->shutr(s->rep->prod);
656 if (s->rep->flags & BF_WRITE_TIMEOUT)
657 s->rep->cons->shutw(s->rep->cons);
658 DPRINTF(stderr,
659 "[%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",
660 now_ms, __FUNCTION__, __LINE__,
661 t,
662 s, s->flags,
663 s->req, s->rep,
664 s->req->rex, s->rep->wex,
665 s->req->flags, s->rep->flags,
666 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
667 s->rep->cons->err_type, s->req->cons->err_type,
668 s->conn_retries);
669 }
670
671 /* Check for connection closure */
672
673resync_stream_interface:
674 DPRINTF(stderr,
675 "[%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",
676 now_ms, __FUNCTION__, __LINE__,
677 t,
678 s, s->flags,
679 s->req, s->rep,
680 s->req->rex, s->rep->wex,
681 s->req->flags, s->rep->flags,
682 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
683 s->rep->cons->err_type, s->req->cons->err_type,
684 s->conn_retries);
685
686 /* nothing special to be done on client side */
687 if (unlikely(s->req->prod->state == SI_ST_DIS))
688 s->req->prod->state = SI_ST_CLO;
689
690 /* When a server-side connection is released, we have to count it and
691 * check for pending connections on this server.
692 */
693 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
694 s->req->cons->state = SI_ST_CLO;
695 if (s->srv) {
696 if (s->flags & SN_CURR_SESS) {
697 s->flags &= ~SN_CURR_SESS;
698 s->srv->cur_sess--;
699 }
700 sess_change_server(s, NULL);
701 if (may_dequeue_tasks(s->srv, s->be))
702 process_srv_queue(s->srv);
703 }
704 }
705
706 /*
707 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
708 * at this point.
709 */
710
711 /**** Process layer 7 below ****/
712
713 resync = 0;
714
715 /* Analyse request */
716 if ((s->req->flags & BF_MASK_ANALYSER) ||
717 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
718 unsigned int flags = s->req->flags;
719
720 if (s->req->prod->state >= SI_ST_EST) {
721 /* it's up to the analysers to reset write_ena */
722 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100723
724 /* We will call all analysers for which a bit is set in
725 * s->req->analysers, following the bit order from LSB
726 * to MSB. The analysers must remove themselves from
727 * the list when not needed. This while() loop is in
728 * fact a cleaner if().
729 */
730 while (s->req->analysers) {
731 if (s->req->analysers & AN_REQ_INSPECT)
732 if (!tcp_inspect_request(s, s->req))
733 break;
734
Willy Tarreau59234e92008-11-30 23:51:27 +0100735 if (s->req->analysers & AN_REQ_HTTP_HDR)
736 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100737 break;
738
Willy Tarreau60b85b02008-11-30 23:28:40 +0100739 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
740 if (!http_process_tarpit(s, s->req))
741 break;
742
Willy Tarreaud34af782008-11-30 23:36:37 +0100743 if (s->req->analysers & AN_REQ_HTTP_BODY)
744 if (!http_process_request_body(s, s->req))
745 break;
746
Willy Tarreauedcf6682008-11-30 23:15:34 +0100747 /* Just make sure that nobody set a wrong flag causing an endless loop */
748 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
749
750 /* we don't want to loop anyway */
751 break;
752 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 }
754 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
755 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
756 if (s->req->flags != flags)
757 resync = 1;
758 }
759
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100760 /* if noone is interested in analysing data, let's forward everything
761 * and only wake up every 1-2 MB. We still wake up when send_max is
762 * reached though.
763 */
764 if (!s->req->send_max && s->req->prod->state >= SI_ST_EST &&
765 !s->req->analysers && !(s->req->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100766 /* check if it is wise to enable kernel splicing on the request buffer */
767 if (!(s->req->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100768 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100769 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100770 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
771 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
772 (s->req->flags & BF_STREAMER_FAST))))
773 s->req->flags |= BF_KERN_SPLICING;
774
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100775 if (s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100776 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100777 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100778
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100779 /* reflect what the L7 analysers have seen last */
780 rqf_last = s->req->flags;
781
782 /*
783 * Now forward all shutdown requests between both sides of the buffer
784 */
785
786 /* first, let's check if the request buffer needs to shutdown(write) */
787 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
788 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
789 buffer_shutw_now(s->req);
790 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
791 (s->req->cons->state == SI_ST_EST) &&
792 s->be->options & PR_O_FORCE_CLO &&
793 s->rep->flags & BF_READ_ACTIVITY) {
794 /* We want to force the connection to the server to close,
795 * and the server has begun to respond. That's the right
796 * time.
797 */
798 buffer_shutw_now(s->req);
799 }
800
801 /* shutdown(write) pending */
802 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
803 s->req->cons->shutw(s->req->cons);
804
805 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100806 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
807 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100808 buffer_shutr_now(s->req);
809
810 /* shutdown(read) pending */
811 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
812 s->req->prod->shutr(s->req->prod);
813
814 /* it's possible that an upper layer has requested a connection setup */
815 if (s->req->cons->state == SI_ST_INI &&
816 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
817 s->req->cons->state = SI_ST_REQ;
818
819 /* we may have a pending connection request, or a connection waiting
820 * for completion.
821 */
822 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
823 do {
824 /* nb: step 1 might switch from QUE to ASS, but we first want
825 * to give a chance to step 2 to perform a redirect if needed.
826 */
827 if (s->si[1].state != SI_ST_REQ)
828 sess_update_stream_int(s, &s->si[1]);
829 if (s->si[1].state == SI_ST_REQ)
830 sess_prepare_conn_req(s, &s->si[1]);
831
832 if (s->si[1].state == SI_ST_ASS && s->srv &&
833 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
834 perform_http_redirect(s, &s->si[1]);
835 } while (s->si[1].state == SI_ST_ASS);
836 }
837
838 /*
839 * Here we want to check if we need to resync or not.
840 */
841 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
842 resync = 1;
843
844 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
845
846 /* according to benchmarks, it makes sense to resync now */
847 if (resync)
848 goto resync_stream_interface;
849
850
851 /* Analyse response */
852
853 if (unlikely(s->rep->flags & BF_HIJACK)) {
854 /* In inject mode, we wake up everytime something has
855 * happened on the write side of the buffer.
856 */
857 unsigned int flags = s->rep->flags;
858
859 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
860 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100861 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 }
863 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
864 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
865 if (s->rep->flags != flags)
866 resync = 1;
867 }
868 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
869 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
870 unsigned int flags = s->rep->flags;
871
872 if (s->rep->prod->state >= SI_ST_EST) {
873 /* it's up to the analysers to reset write_ena */
874 buffer_write_ena(s->rep);
875 if (s->rep->analysers)
876 process_response(s);
877 }
878 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
879 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
880 if (s->rep->flags != flags)
881 resync = 1;
882 }
883
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100884 /* if noone is interested in analysing data, let's forward everything
885 * and only wake up every 1-2 MB. We still wake up when send_max is
886 * reached though.
887 */
888 if (!s->rep->send_max && s->rep->prod->state >= SI_ST_EST &&
889 !s->rep->analysers && !(s->rep->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100890 /* check if it is wise to enable kernel splicing on the response buffer */
891 if (!(s->rep->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100892 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100893 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100894 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
895 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
896 (s->rep->flags & BF_STREAMER_FAST))))
897 s->rep->flags |= BF_KERN_SPLICING;
898
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100899 if (s->rep->to_forward < FORWARD_DEFAULT_SIZE)
900 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100901 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100902
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 /* reflect what the L7 analysers have seen last */
904 rpf_last = s->rep->flags;
905
906 /*
907 * Now forward all shutdown requests between both sides of the buffer
908 */
909
910 /*
911 * FIXME: this is probably where we should produce error responses.
912 */
913
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100914 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100915 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
916 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
917 buffer_shutw_now(s->rep);
918
919 /* shutdown(write) pending */
920 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
921 s->rep->cons->shutw(s->rep->cons);
922
923 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100924 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
925 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926 buffer_shutr_now(s->rep);
927
928 /* shutdown(read) pending */
929 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
930 s->rep->prod->shutr(s->rep->prod);
931
932 /*
933 * Here we want to check if we need to resync or not.
934 */
935 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
936 resync = 1;
937
938 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
939
940 if (resync)
941 goto resync_stream_interface;
942
943
944 /* This is needed only when debugging is enabled, to indicate
945 * client-side or server-side close. Please note that in the unlikely
946 * event where both sides would close at once, the sequence is reported
947 * on the server side first.
948 */
949 if (unlikely((global.mode & MODE_DEBUG) &&
950 (!(global.mode & MODE_QUIET) ||
951 (global.mode & MODE_VERBOSE)))) {
952 int len;
953
954 if (s->si[1].state == SI_ST_CLO &&
955 s->si[1].prev_state == SI_ST_EST) {
956 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
957 s->uniq_id, s->be->id,
958 (unsigned short)s->si[0].fd,
959 (unsigned short)s->si[1].fd);
960 write(1, trash, len);
961 }
962
963 if (s->si[0].state == SI_ST_CLO &&
964 s->si[0].prev_state == SI_ST_EST) {
965 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
966 s->uniq_id, s->be->id,
967 (unsigned short)s->si[0].fd,
968 (unsigned short)s->si[1].fd);
969 write(1, trash, len);
970 }
971 }
972
973 if (likely((s->rep->cons->state != SI_ST_CLO) ||
974 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
975
976 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
977 session_process_counters(s);
978
979 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100980 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100981
982 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100983 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100984
985 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
986 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
987 s->si[0].prev_state = s->si[0].state;
988 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100989 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
990 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100991
992 /* Trick: if a request is being waiting for the server to respond,
993 * and if we know the server can timeout, we don't want the timeout
994 * to expire on the client side first, but we're still interested
995 * in passing data from the client to the server (eg: POST). Thus,
996 * we can cancel the client's request timeout if the server's
997 * request timeout is set and the server has not yet sent a response.
998 */
999
1000 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001001 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1002 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001003 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001004 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005
1006 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1007 tick_first(s->rep->rex, s->rep->wex));
1008 if (s->req->analysers)
1009 t->expire = tick_first(t->expire, s->req->analyse_exp);
1010
1011 if (s->si[0].exp)
1012 t->expire = tick_first(t->expire, s->si[0].exp);
1013
1014 if (s->si[1].exp)
1015 t->expire = tick_first(t->expire, s->si[1].exp);
1016
1017#ifdef DEBUG_FULL
1018 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u, cs=%d, ss=%d\n",
1019 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex, s->si[0].state, s->si[1].state);
1020#endif
1021 /* restore t to its place in the task list */
1022 task_queue(t);
1023
1024#ifdef DEBUG_DEV
1025 /* this may only happen when no timeout is set or in case of an FSM bug */
1026 if (!t->expire)
1027 ABORT_NOW();
1028#endif
1029 *next = t->expire;
1030 return; /* nothing more to do */
1031 }
1032
1033 s->fe->feconn--;
1034 if (s->flags & SN_BE_ASSIGNED)
1035 s->be->beconn--;
1036 actconn--;
1037
1038 if (unlikely((global.mode & MODE_DEBUG) &&
1039 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1040 int len;
1041 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
1042 s->uniq_id, s->be->id,
1043 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
1044 s->term_trace);
1045 write(1, trash, len);
1046 }
1047
1048 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1049 session_process_counters(s);
1050
1051 /* let's do a final log if we need it */
1052 if (s->logs.logwait &&
1053 !(s->flags & SN_MONITOR) &&
1054 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001055 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001056 }
1057
1058 /* the task MUST not be in the run queue anymore */
1059 task_delete(t);
1060 session_free(s);
1061 task_free(t);
1062 *next = TICK_ETERNITY;
1063}
1064
Willy Tarreau7c669d72008-06-20 15:04:11 +02001065/*
1066 * This function adjusts sess->srv_conn and maintains the previous and new
1067 * server's served session counts. Setting newsrv to NULL is enough to release
1068 * current connection slot. This function also notifies any LB algo which might
1069 * expect to be informed about any change in the number of active sessions on a
1070 * server.
1071 */
1072void sess_change_server(struct session *sess, struct server *newsrv)
1073{
1074 if (sess->srv_conn == newsrv)
1075 return;
1076
1077 if (sess->srv_conn) {
1078 sess->srv_conn->served--;
1079 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1080 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1081 sess->srv_conn = NULL;
1082 }
1083
1084 if (newsrv) {
1085 newsrv->served++;
1086 if (newsrv->proxy->lbprm.server_take_conn)
1087 newsrv->proxy->lbprm.server_take_conn(newsrv);
1088 sess->srv_conn = newsrv;
1089 }
1090}
1091
1092
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093/*
1094 * Local variables:
1095 * c-indent-level: 8
1096 * c-basic-offset: 8
1097 * End:
1098 */