blob: 8ced9b8bf30b8987a3fbe62ec71b7d9fc14a0afe [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) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100101 /* we have to unlink all watchers. We must not relink them if
102 * this session was the last one in the list.
103 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100104 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100105 LIST_INIT(&bref->users);
106 if (s->list.n != &sessions)
107 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100108 bref->ref = s->list.n;
109 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100110 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200111 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200112
113 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200114 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200115 pool_flush2(pool2_buffer);
116 pool_flush2(fe->hdr_idx_pool);
117 pool_flush2(pool2_requri);
118 pool_flush2(pool2_capture);
119 pool_flush2(pool2_session);
120 pool_flush2(fe->req_cap_pool);
121 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200122 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200123}
124
125
126/* perform minimal intializations, report 0 in case of error, 1 if OK. */
127int init_session()
128{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100129 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200130 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
131 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132}
133
Willy Tarreau30e71012007-11-26 20:15:35 +0100134void session_process_counters(struct session *s)
135{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100136 unsigned long long bytes;
137
Willy Tarreau30e71012007-11-26 20:15:35 +0100138 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100140 s->logs.bytes_in = s->req->total;
141 if (bytes) {
142 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100143
Willy Tarreau30e71012007-11-26 20:15:35 +0100144 if (s->be != s->fe)
145 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->srv)
148 s->srv->bytes_in += bytes;
149 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100150 }
151
Willy Tarreau30e71012007-11-26 20:15:35 +0100152 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100153 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100154 s->logs.bytes_out = s->rep->total;
155 if (bytes) {
156 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100157
Willy Tarreau30e71012007-11-26 20:15:35 +0100158 if (s->be != s->fe)
159 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100160
Willy Tarreau30e71012007-11-26 20:15:35 +0100161 if (s->srv)
162 s->srv->bytes_out += bytes;
163 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100164 }
165}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100167/* This function is called with (si->state == SI_ST_CON) meaning that a
168 * connection was attempted and that the file descriptor is already allocated.
169 * We must check for establishment, error and abort. Possible output states
170 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
171 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
172 * otherwise 1.
173 */
174int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
175{
176 struct buffer *req = si->ob;
177 struct buffer *rep = si->ib;
178
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100179 /* If we got an error, or if nothing happened and the connection timed
180 * out, we must give up. The CER state handler will take care of retry
181 * attempts and error reports.
182 */
183 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
184 si->state = SI_ST_CER;
185 fd_delete(si->fd);
186
187 if (si->err_type)
188 return 0;
189
190 si->err_loc = s->srv;
191 if (si->flags & SI_FL_ERR)
192 si->err_type = SI_ET_CONN_ERR;
193 else
194 si->err_type = SI_ET_CONN_TO;
195 return 0;
196 }
197
198 /* OK, maybe we want to abort */
199 if (unlikely((req->flags & BF_SHUTW_NOW) ||
200 (rep->flags & BF_SHUTW) ||
201 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100202 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100203 s->be->options & PR_O_ABRT_CLOSE)))) {
204 /* give up */
205 si->shutw(si);
206 si->err_type |= SI_ET_CONN_ABRT;
207 si->err_loc = s->srv;
208 return 1;
209 }
210
211 /* we need to wait a bit more if there was no activity either */
212 if (!(req->flags & BF_WRITE_ACTIVITY))
213 return 1;
214
215 /* OK, this means that a connection succeeded. The caller will be
216 * responsible for handling the transition from CON to EST.
217 */
218 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
219 si->state = SI_ST_EST;
220 si->err_type = SI_ET_NONE;
221 si->err_loc = NULL;
222 return 1;
223}
224
225/* This function is called with (si->state == SI_ST_CER) meaning that a
226 * previous connection attempt has failed and that the file descriptor
227 * has already been released. Possible causes include asynchronous error
228 * notification and time out. Possible output states are SI_ST_CLO when
229 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
230 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
231 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
232 * marked as in error state. It returns 0.
233 */
234int sess_update_st_cer(struct session *s, struct stream_interface *si)
235{
236 /* we probably have to release last session from the server */
237 if (s->srv) {
238 if (s->flags & SN_CURR_SESS) {
239 s->flags &= ~SN_CURR_SESS;
240 s->srv->cur_sess--;
241 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100242 }
243
244 /* ensure that we have enough retries left */
245 s->conn_retries--;
246 if (s->conn_retries < 0) {
247 if (!si->err_type) {
248 si->err_type = SI_ET_CONN_ERR;
249 si->err_loc = s->srv;
250 }
251
252 if (s->srv)
253 s->srv->failed_conns++;
254 s->be->failed_conns++;
255 if (may_dequeue_tasks(s->srv, s->be))
256 process_srv_queue(s->srv);
257
258 /* shutw is enough so stop a connecting socket */
259 si->shutw(si);
260 si->ob->flags |= BF_WRITE_ERROR;
261 si->ib->flags |= BF_READ_ERROR;
262
263 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100264 if (s->srv_error)
265 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100266 return 0;
267 }
268
269 /* If the "redispatch" option is set on the backend, we are allowed to
270 * retry on another server for the last retry. In order to achieve this,
271 * we must mark the session unassigned, and eventually clear the DIRECT
272 * bit to ignore any persistence cookie. We won't count a retry nor a
273 * redispatch yet, because this will depend on what server is selected.
274 */
275 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
276 if (may_dequeue_tasks(s->srv, s->be))
277 process_srv_queue(s->srv);
278
279 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
280 s->prev_srv = s->srv;
281 si->state = SI_ST_REQ;
282 } else {
283 if (s->srv)
284 s->srv->retries++;
285 s->be->retries++;
286 si->state = SI_ST_ASS;
287 }
288
289 if (si->flags & SI_FL_ERR) {
290 /* The error was an asynchronous connection error, and we will
291 * likely have to retry connecting to the same server, most
292 * likely leading to the same result. To avoid this, we wait
293 * one second before retrying.
294 */
295
296 if (!si->err_type)
297 si->err_type = SI_ET_CONN_ERR;
298
299 si->state = SI_ST_TAR;
300 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
301 return 0;
302 }
303 return 0;
304}
305
306/*
307 * This function handles the transition between the SI_ST_CON state and the
308 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
309 * SI_ST_EST.
310 */
311void sess_establish(struct session *s, struct stream_interface *si)
312{
313 struct buffer *req = si->ob;
314 struct buffer *rep = si->ib;
315
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100316 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100317 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
318
319 /* if the user wants to log as soon as possible, without counting
320 * bytes from the server, then this is the right moment. */
321 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
322 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100323 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100324 }
325#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100326 if ((global.tune.options & GTUNE_USE_SPLICE) &&
327 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100328 /* TCP splicing supported by both FE and BE */
329 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
330 }
331#endif
332 }
333 else {
334 rep->analysers |= AN_RTR_HTTP_HDR;
335 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
336 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
337 /* reset hdr_idx which was already initialized by the request.
338 * right now, the http parser does it.
339 * hdr_idx_init(&s->txn.hdr_idx);
340 */
341 }
342
343 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
344 req->wex = TICK_ETERNITY;
345}
346
347/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
348 * Other input states are simply ignored.
349 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
350 * Flags must have previously been updated for timeouts and other conditions.
351 */
352void sess_update_stream_int(struct session *s, struct stream_interface *si)
353{
354 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",
355 now_ms, __FUNCTION__,
356 s,
357 s->req, s->rep,
358 s->req->rex, s->rep->wex,
359 s->req->flags, s->rep->flags,
360 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
361
362 if (si->state == SI_ST_ASS) {
363 /* Server assigned to connection request, we have to try to connect now */
364 int conn_err;
365
366 conn_err = connect_server(s);
367 if (conn_err == SN_ERR_NONE) {
368 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100369 if (s->srv)
370 s->srv->cum_sess++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100371 return;
372 }
373
374 /* We have received a synchronous error. We might have to
375 * abort, retry immediately or redispatch.
376 */
377 if (conn_err == SN_ERR_INTERNAL) {
378 if (!si->err_type) {
379 si->err_type = SI_ET_CONN_OTHER;
380 si->err_loc = s->srv;
381 }
382
383 if (s->srv)
384 s->srv->cum_sess++;
385 if (s->srv)
386 s->srv->failed_conns++;
387 s->be->failed_conns++;
388
389 /* release other sessions waiting for this server */
390 if (may_dequeue_tasks(s->srv, s->be))
391 process_srv_queue(s->srv);
392
393 /* Failed and not retryable. */
394 si->shutr(si);
395 si->shutw(si);
396 si->ob->flags |= BF_WRITE_ERROR;
397
398 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
399
400 /* no session was ever accounted for this server */
401 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100402 if (s->srv_error)
403 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100404 return;
405 }
406
407 /* We are facing a retryable error, but we don't want to run a
408 * turn-around now, as the problem is likely a source port
409 * allocation problem, so we want to retry now.
410 */
411 si->state = SI_ST_CER;
412 si->flags &= ~SI_FL_ERR;
413 sess_update_st_cer(s, si);
414 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
415 return;
416 }
417 else if (si->state == SI_ST_QUE) {
418 /* connection request was queued, check for any update */
419 if (!s->pend_pos) {
420 /* The connection is not in the queue anymore. Either
421 * we have a server connection slot available and we
422 * go directly to the assigned state, or we need to
423 * load-balance first and go to the INI state.
424 */
425 si->exp = TICK_ETERNITY;
426 if (unlikely(!(s->flags & SN_ASSIGNED)))
427 si->state = SI_ST_REQ;
428 else {
429 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
430 si->state = SI_ST_ASS;
431 }
432 return;
433 }
434
435 /* Connection request still in queue... */
436 if (si->flags & SI_FL_EXP) {
437 /* ... and timeout expired */
438 si->exp = TICK_ETERNITY;
439 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
440 if (s->srv)
441 s->srv->failed_conns++;
442 s->be->failed_conns++;
443 si->shutr(si);
444 si->shutw(si);
445 si->ob->flags |= BF_WRITE_TIMEOUT;
446 if (!si->err_type)
447 si->err_type = SI_ET_QUEUE_TO;
448 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100449 if (s->srv_error)
450 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100451 return;
452 }
453
454 /* Connection remains in queue, check if we have to abort it */
455 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
456 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
457 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
458 /* give up */
459 si->exp = TICK_ETERNITY;
460 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
461 si->shutr(si);
462 si->shutw(si);
463 si->err_type |= SI_ET_QUEUE_ABRT;
464 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100465 if (s->srv_error)
466 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100467 return;
468 }
469
470 /* Nothing changed */
471 return;
472 }
473 else if (si->state == SI_ST_TAR) {
474 /* Connection request might be aborted */
475 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
476 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
477 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
478 /* give up */
479 si->exp = TICK_ETERNITY;
480 si->shutr(si);
481 si->shutw(si);
482 si->err_type |= SI_ET_CONN_ABRT;
483 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100484 if (s->srv_error)
485 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100486 return;
487 }
488
489 if (!(si->flags & SI_FL_EXP))
490 return; /* still in turn-around */
491
492 si->exp = TICK_ETERNITY;
493
494 /* we keep trying on the same server as long as the session is
495 * marked "assigned".
496 * FIXME: Should we force a redispatch attempt when the server is down ?
497 */
498 if (s->flags & SN_ASSIGNED)
499 si->state = SI_ST_ASS;
500 else
501 si->state = SI_ST_REQ;
502 return;
503 }
504}
505
506/* This function initiates a server connection request on a stream interface
507 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
508 * indicating that a server has been assigned. It may also return SI_ST_QUE,
509 * or SI_ST_CLO upon error.
510 */
511static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
512 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",
513 now_ms, __FUNCTION__,
514 s,
515 s->req, s->rep,
516 s->req->rex, s->rep->wex,
517 s->req->flags, s->rep->flags,
518 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
519
520 if (si->state != SI_ST_REQ)
521 return;
522
523 /* Try to assign a server */
524 if (srv_redispatch_connect(s) != 0) {
525 /* We did not get a server. Either we queued the
526 * connection request, or we encountered an error.
527 */
528 if (si->state == SI_ST_QUE)
529 return;
530
531 /* we did not get any server, let's check the cause */
532 si->shutr(si);
533 si->shutw(si);
534 si->ob->flags |= BF_WRITE_ERROR;
535 if (!si->err_type)
536 si->err_type = SI_ET_CONN_OTHER;
537 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100538 if (s->srv_error)
539 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540 return;
541 }
542
543 /* The server is assigned */
544 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
545 si->state = SI_ST_ASS;
546}
547
548/* Processes the client, server, request and response jobs of a session task,
549 * then puts it back to the wait queue in a clean state, or cleans up its
550 * resources if it must be deleted. Returns in <next> the date the task wants
551 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
552 * nothing too many times, the request and response buffers flags are monitored
553 * and each function is called only if at least another function has changed at
554 * least one flag it is interested in.
555 */
556void process_session(struct task *t, int *next)
557{
558 struct session *s = t->context;
559 int resync;
560 unsigned int rqf_last, rpf_last;
561
562 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
563 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
564
565 /* 1a: Check for low level timeouts if needed. We just set a flag on
566 * stream interfaces when their timeouts have expired.
567 */
568 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
569 stream_int_check_timeouts(&s->si[0]);
570 stream_int_check_timeouts(&s->si[1]);
571 buffer_check_timeouts(s->req);
572 buffer_check_timeouts(s->rep);
573 }
574
Willy Tarreau86491c32008-12-14 09:04:47 +0100575 s->req->flags &= ~BF_READ_NOEXP;
576
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577 /* copy req/rep flags so that we can detect shutdowns */
578 rqf_last = s->req->flags;
579 rpf_last = s->rep->flags;
580
581 /* 1b: check for low-level errors reported at the stream interface.
582 * First we check if it's a retryable error (in which case we don't
583 * want to tell the buffer). Otherwise we report the error one level
584 * upper by setting flags into the buffers. Note that the side towards
585 * the client cannot have connect (hence retryable) errors. Also, the
586 * connection setup code must be able to deal with any type of abort.
587 */
588 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
589 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
590 s->si[0].shutr(&s->si[0]);
591 s->si[0].shutw(&s->si[0]);
592 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100593 if (!(s->req->analysers) && !(s->rep->analysers)) {
594 if (!(s->flags & SN_ERR_MASK))
595 s->flags |= SN_ERR_CLICL;
596 if (!(s->flags & SN_FINST_MASK))
597 s->flags |= SN_FINST_D;
598 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 }
600 }
601
602 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
603 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
604 s->si[1].shutr(&s->si[1]);
605 s->si[1].shutw(&s->si[1]);
606 stream_int_report_error(&s->si[1]);
607 s->be->failed_resp++;
608 if (s->srv)
609 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100610 if (!(s->req->analysers) && !(s->rep->analysers)) {
611 if (!(s->flags & SN_ERR_MASK))
612 s->flags |= SN_ERR_SRVCL;
613 if (!(s->flags & SN_FINST_MASK))
614 s->flags |= SN_FINST_D;
615 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616 }
617 /* note: maybe we should process connection errors here ? */
618 }
619
620 if (s->si[1].state == SI_ST_CON) {
621 /* we were trying to establish a connection on the server side,
622 * maybe it succeeded, maybe it failed, maybe we timed out, ...
623 */
624 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
625 sess_update_st_cer(s, &s->si[1]);
626 else if (s->si[1].state == SI_ST_EST)
627 sess_establish(s, &s->si[1]);
628
629 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
630 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
631 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
632 */
633 }
634
635 /* check buffer timeouts, and close the corresponding stream interfaces
636 * for future reads or writes. Note: this will also concern upper layers
637 * but we do not touch any other flag. We must be careful and correctly
638 * detect state changes when calling them.
639 */
640 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
641 if (s->req->flags & BF_READ_TIMEOUT)
642 s->req->prod->shutr(s->req->prod);
643 if (s->req->flags & BF_WRITE_TIMEOUT)
644 s->req->cons->shutw(s->req->cons);
645 DPRINTF(stderr,
646 "[%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",
647 now_ms, __FUNCTION__, __LINE__,
648 t,
649 s, s->flags,
650 s->req, s->rep,
651 s->req->rex, s->rep->wex,
652 s->req->flags, s->rep->flags,
653 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
654 s->rep->cons->err_type, s->req->cons->err_type,
655 s->conn_retries);
656 }
657
658 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
659 if (s->rep->flags & BF_READ_TIMEOUT)
660 s->rep->prod->shutr(s->rep->prod);
661 if (s->rep->flags & BF_WRITE_TIMEOUT)
662 s->rep->cons->shutw(s->rep->cons);
663 DPRINTF(stderr,
664 "[%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",
665 now_ms, __FUNCTION__, __LINE__,
666 t,
667 s, s->flags,
668 s->req, s->rep,
669 s->req->rex, s->rep->wex,
670 s->req->flags, s->rep->flags,
671 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
672 s->rep->cons->err_type, s->req->cons->err_type,
673 s->conn_retries);
674 }
675
676 /* Check for connection closure */
677
678resync_stream_interface:
679 DPRINTF(stderr,
680 "[%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",
681 now_ms, __FUNCTION__, __LINE__,
682 t,
683 s, s->flags,
684 s->req, s->rep,
685 s->req->rex, s->rep->wex,
686 s->req->flags, s->rep->flags,
687 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
688 s->rep->cons->err_type, s->req->cons->err_type,
689 s->conn_retries);
690
691 /* nothing special to be done on client side */
692 if (unlikely(s->req->prod->state == SI_ST_DIS))
693 s->req->prod->state = SI_ST_CLO;
694
695 /* When a server-side connection is released, we have to count it and
696 * check for pending connections on this server.
697 */
698 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
699 s->req->cons->state = SI_ST_CLO;
700 if (s->srv) {
701 if (s->flags & SN_CURR_SESS) {
702 s->flags &= ~SN_CURR_SESS;
703 s->srv->cur_sess--;
704 }
705 sess_change_server(s, NULL);
706 if (may_dequeue_tasks(s->srv, s->be))
707 process_srv_queue(s->srv);
708 }
709 }
710
711 /*
712 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
713 * at this point.
714 */
715
716 /**** Process layer 7 below ****/
717
718 resync = 0;
719
720 /* Analyse request */
721 if ((s->req->flags & BF_MASK_ANALYSER) ||
722 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
723 unsigned int flags = s->req->flags;
724
725 if (s->req->prod->state >= SI_ST_EST) {
726 /* it's up to the analysers to reset write_ena */
727 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100728
729 /* We will call all analysers for which a bit is set in
730 * s->req->analysers, following the bit order from LSB
731 * to MSB. The analysers must remove themselves from
732 * the list when not needed. This while() loop is in
733 * fact a cleaner if().
734 */
735 while (s->req->analysers) {
736 if (s->req->analysers & AN_REQ_INSPECT)
737 if (!tcp_inspect_request(s, s->req))
738 break;
739
Willy Tarreau59234e92008-11-30 23:51:27 +0100740 if (s->req->analysers & AN_REQ_HTTP_HDR)
741 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100742 break;
743
Willy Tarreau60b85b02008-11-30 23:28:40 +0100744 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
745 if (!http_process_tarpit(s, s->req))
746 break;
747
Willy Tarreaud34af782008-11-30 23:36:37 +0100748 if (s->req->analysers & AN_REQ_HTTP_BODY)
749 if (!http_process_request_body(s, s->req))
750 break;
751
Willy Tarreauedcf6682008-11-30 23:15:34 +0100752 /* Just make sure that nobody set a wrong flag causing an endless loop */
753 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
754
755 /* we don't want to loop anyway */
756 break;
757 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 }
759 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
760 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
761 if (s->req->flags != flags)
762 resync = 1;
763 }
764
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100765 /* if noone is interested in analysing data, let's forward everything
766 * and only wake up every 1-2 MB. We still wake up when send_max is
767 * reached though.
768 */
769 if (!s->req->send_max && s->req->prod->state >= SI_ST_EST &&
770 !s->req->analysers && !(s->req->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100771 /* check if it is wise to enable kernel splicing on the request buffer */
772 if (!(s->req->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100773 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100774 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100775 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
776 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
777 (s->req->flags & BF_STREAMER_FAST))))
778 s->req->flags |= BF_KERN_SPLICING;
779
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100780 if (s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100781 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100782 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100783
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784 /* reflect what the L7 analysers have seen last */
785 rqf_last = s->req->flags;
786
787 /*
788 * Now forward all shutdown requests between both sides of the buffer
789 */
790
791 /* first, let's check if the request buffer needs to shutdown(write) */
792 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
793 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
794 buffer_shutw_now(s->req);
795 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
796 (s->req->cons->state == SI_ST_EST) &&
797 s->be->options & PR_O_FORCE_CLO &&
798 s->rep->flags & BF_READ_ACTIVITY) {
799 /* We want to force the connection to the server to close,
800 * and the server has begun to respond. That's the right
801 * time.
802 */
803 buffer_shutw_now(s->req);
804 }
805
806 /* shutdown(write) pending */
807 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
808 s->req->cons->shutw(s->req->cons);
809
810 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100811 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
812 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813 buffer_shutr_now(s->req);
814
815 /* shutdown(read) pending */
816 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
817 s->req->prod->shutr(s->req->prod);
818
819 /* it's possible that an upper layer has requested a connection setup */
820 if (s->req->cons->state == SI_ST_INI &&
821 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
822 s->req->cons->state = SI_ST_REQ;
823
824 /* we may have a pending connection request, or a connection waiting
825 * for completion.
826 */
827 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
828 do {
829 /* nb: step 1 might switch from QUE to ASS, but we first want
830 * to give a chance to step 2 to perform a redirect if needed.
831 */
832 if (s->si[1].state != SI_ST_REQ)
833 sess_update_stream_int(s, &s->si[1]);
834 if (s->si[1].state == SI_ST_REQ)
835 sess_prepare_conn_req(s, &s->si[1]);
836
837 if (s->si[1].state == SI_ST_ASS && s->srv &&
838 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
839 perform_http_redirect(s, &s->si[1]);
840 } while (s->si[1].state == SI_ST_ASS);
841 }
842
843 /*
844 * Here we want to check if we need to resync or not.
845 */
846 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
847 resync = 1;
848
849 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
850
851 /* according to benchmarks, it makes sense to resync now */
852 if (resync)
853 goto resync_stream_interface;
854
855
856 /* Analyse response */
857
858 if (unlikely(s->rep->flags & BF_HIJACK)) {
859 /* In inject mode, we wake up everytime something has
860 * happened on the write side of the buffer.
861 */
862 unsigned int flags = s->rep->flags;
863
864 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
865 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100866 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100867 }
868 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
869 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
870 if (s->rep->flags != flags)
871 resync = 1;
872 }
873 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
874 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
875 unsigned int flags = s->rep->flags;
876
877 if (s->rep->prod->state >= SI_ST_EST) {
878 /* it's up to the analysers to reset write_ena */
879 buffer_write_ena(s->rep);
880 if (s->rep->analysers)
881 process_response(s);
882 }
883 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
884 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
885 if (s->rep->flags != flags)
886 resync = 1;
887 }
888
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100889 /* if noone is interested in analysing data, let's forward everything
890 * and only wake up every 1-2 MB. We still wake up when send_max is
891 * reached though.
892 */
893 if (!s->rep->send_max && s->rep->prod->state >= SI_ST_EST &&
894 !s->rep->analysers && !(s->rep->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100895 /* check if it is wise to enable kernel splicing on the response buffer */
896 if (!(s->rep->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100897 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100898 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100899 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
900 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
901 (s->rep->flags & BF_STREAMER_FAST))))
902 s->rep->flags |= BF_KERN_SPLICING;
903
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100904 if (s->rep->to_forward < FORWARD_DEFAULT_SIZE)
905 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100906 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100907
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 /* reflect what the L7 analysers have seen last */
909 rpf_last = s->rep->flags;
910
911 /*
912 * Now forward all shutdown requests between both sides of the buffer
913 */
914
915 /*
916 * FIXME: this is probably where we should produce error responses.
917 */
918
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100919 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
921 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
922 buffer_shutw_now(s->rep);
923
924 /* shutdown(write) pending */
925 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
926 s->rep->cons->shutw(s->rep->cons);
927
928 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100929 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
930 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100931 buffer_shutr_now(s->rep);
932
933 /* shutdown(read) pending */
934 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
935 s->rep->prod->shutr(s->rep->prod);
936
937 /*
938 * Here we want to check if we need to resync or not.
939 */
940 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
941 resync = 1;
942
943 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
944
945 if (resync)
946 goto resync_stream_interface;
947
948
949 /* This is needed only when debugging is enabled, to indicate
950 * client-side or server-side close. Please note that in the unlikely
951 * event where both sides would close at once, the sequence is reported
952 * on the server side first.
953 */
954 if (unlikely((global.mode & MODE_DEBUG) &&
955 (!(global.mode & MODE_QUIET) ||
956 (global.mode & MODE_VERBOSE)))) {
957 int len;
958
959 if (s->si[1].state == SI_ST_CLO &&
960 s->si[1].prev_state == SI_ST_EST) {
961 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
962 s->uniq_id, s->be->id,
963 (unsigned short)s->si[0].fd,
964 (unsigned short)s->si[1].fd);
965 write(1, trash, len);
966 }
967
968 if (s->si[0].state == SI_ST_CLO &&
969 s->si[0].prev_state == SI_ST_EST) {
970 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
971 s->uniq_id, s->be->id,
972 (unsigned short)s->si[0].fd,
973 (unsigned short)s->si[1].fd);
974 write(1, trash, len);
975 }
976 }
977
978 if (likely((s->rep->cons->state != SI_ST_CLO) ||
979 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
980
981 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
982 session_process_counters(s);
983
984 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100985 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986
987 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100988 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989
990 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
991 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
992 s->si[0].prev_state = s->si[0].state;
993 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100994 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
995 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100996
997 /* Trick: if a request is being waiting for the server to respond,
998 * and if we know the server can timeout, we don't want the timeout
999 * to expire on the client side first, but we're still interested
1000 * in passing data from the client to the server (eg: POST). Thus,
1001 * we can cancel the client's request timeout if the server's
1002 * request timeout is set and the server has not yet sent a response.
1003 */
1004
1005 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001006 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1007 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001008 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001009 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010
1011 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1012 tick_first(s->rep->rex, s->rep->wex));
1013 if (s->req->analysers)
1014 t->expire = tick_first(t->expire, s->req->analyse_exp);
1015
1016 if (s->si[0].exp)
1017 t->expire = tick_first(t->expire, s->si[0].exp);
1018
1019 if (s->si[1].exp)
1020 t->expire = tick_first(t->expire, s->si[1].exp);
1021
1022#ifdef DEBUG_FULL
1023 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",
1024 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);
1025#endif
1026 /* restore t to its place in the task list */
1027 task_queue(t);
1028
1029#ifdef DEBUG_DEV
1030 /* this may only happen when no timeout is set or in case of an FSM bug */
1031 if (!t->expire)
1032 ABORT_NOW();
1033#endif
1034 *next = t->expire;
1035 return; /* nothing more to do */
1036 }
1037
1038 s->fe->feconn--;
1039 if (s->flags & SN_BE_ASSIGNED)
1040 s->be->beconn--;
1041 actconn--;
1042
1043 if (unlikely((global.mode & MODE_DEBUG) &&
1044 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1045 int len;
1046 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
1047 s->uniq_id, s->be->id,
1048 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
1049 s->term_trace);
1050 write(1, trash, len);
1051 }
1052
1053 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1054 session_process_counters(s);
1055
1056 /* let's do a final log if we need it */
1057 if (s->logs.logwait &&
1058 !(s->flags & SN_MONITOR) &&
1059 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001060 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001061 }
1062
1063 /* the task MUST not be in the run queue anymore */
1064 task_delete(t);
1065 session_free(s);
1066 task_free(t);
1067 *next = TICK_ETERNITY;
1068}
1069
Willy Tarreau7c669d72008-06-20 15:04:11 +02001070/*
1071 * This function adjusts sess->srv_conn and maintains the previous and new
1072 * server's served session counts. Setting newsrv to NULL is enough to release
1073 * current connection slot. This function also notifies any LB algo which might
1074 * expect to be informed about any change in the number of active sessions on a
1075 * server.
1076 */
1077void sess_change_server(struct session *sess, struct server *newsrv)
1078{
1079 if (sess->srv_conn == newsrv)
1080 return;
1081
1082 if (sess->srv_conn) {
1083 sess->srv_conn->served--;
1084 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1085 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1086 sess->srv_conn = NULL;
1087 }
1088
1089 if (newsrv) {
1090 newsrv->served++;
1091 if (newsrv->proxy->lbprm.server_take_conn)
1092 newsrv->proxy->lbprm.server_take_conn(newsrv);
1093 sess->srv_conn = newsrv;
1094 }
1095}
1096
1097
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098/*
1099 * Local variables:
1100 * c-indent-level: 8
1101 * c-basic-offset: 8
1102 * End:
1103 */