blob: bcfa5c12e111a93e144a5e033f0331040bc3cb7f [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 Tarreau7f062c42009-03-05 18:43:00 +010031#include <proto/server.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010032#include <proto/stream_interface.h>
33#include <proto/stream_sock.h>
34#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau3ab68cf2009-01-25 16:03:28 +010036#ifdef CONFIG_HAP_TCPSPLICE
37#include <libtcpsplice.h>
38#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020040struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010041struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
43/*
44 * frees the context associated to a session. It must have been removed first.
45 */
46void session_free(struct session *s)
47{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010048 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020049 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010050 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010051
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 if (s->pend_pos)
53 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010054
Willy Tarreau1e62de62008-11-11 20:20:02 +010055 if (s->srv) { /* there may be requests left pending in queue */
56 if (s->flags & SN_CURR_SESS) {
57 s->flags &= ~SN_CURR_SESS;
58 s->srv->cur_sess--;
59 }
Willy Tarreau922a8062008-12-04 09:33:58 +010060 if (may_dequeue_tasks(s->srv, s->be))
61 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010062 }
Willy Tarreau922a8062008-12-04 09:33:58 +010063
Willy Tarreau7c669d72008-06-20 15:04:11 +020064 if (unlikely(s->srv_conn)) {
65 /* the session still has a reserved slot on a server, but
66 * it should normally be only the same as the one above,
67 * so this should not happen in fact.
68 */
69 sess_change_server(s, NULL);
70 }
71
Willy Tarreau3eba98a2009-01-25 13:56:13 +010072 if (s->req->pipe)
73 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010074
Willy Tarreau3eba98a2009-01-25 13:56:13 +010075 if (s->rep->pipe)
76 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010077
Willy Tarreau48d63db2008-08-03 17:41:33 +020078 pool_free2(pool2_buffer, s->req);
79 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
Willy Tarreau92fb9832007-10-16 17:34:28 +020081 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020082 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010083
Willy Tarreau92fb9832007-10-16 17:34:28 +020084 if (txn->rsp.cap != NULL) {
85 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020086 for (h = fe->rsp_cap; h; h = h->next)
87 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020088 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020089 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020090 if (txn->req.cap != NULL) {
91 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020092 for (h = fe->req_cap; h; h = h->next)
93 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020094 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020097 pool_free2(pool2_requri, txn->uri);
98 pool_free2(pool2_capture, txn->cli_cookie);
99 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100100
101 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100102 /* we have to unlink all watchers. We must not relink them if
103 * this session was the last one in the list.
104 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100105 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100106 LIST_INIT(&bref->users);
107 if (s->list.n != &sessions)
108 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100109 bref->ref = s->list.n;
110 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100111 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200112 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200113
114 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200115 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200116 pool_flush2(pool2_buffer);
117 pool_flush2(fe->hdr_idx_pool);
118 pool_flush2(pool2_requri);
119 pool_flush2(pool2_capture);
120 pool_flush2(pool2_session);
121 pool_flush2(fe->req_cap_pool);
122 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200123 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200124}
125
126
127/* perform minimal intializations, report 0 in case of error, 1 if OK. */
128int init_session()
129{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100130 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200131 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
132 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133}
134
Willy Tarreau30e71012007-11-26 20:15:35 +0100135void session_process_counters(struct session *s)
136{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100137 unsigned long long bytes;
138
Willy Tarreau30e71012007-11-26 20:15:35 +0100139 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100140 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100141 s->logs.bytes_in = s->req->total;
142 if (bytes) {
143 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100144
Willy Tarreau30e71012007-11-26 20:15:35 +0100145 if (s->be != s->fe)
146 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100147
Willy Tarreau30e71012007-11-26 20:15:35 +0100148 if (s->srv)
149 s->srv->bytes_in += bytes;
150 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100151 }
152
Willy Tarreau30e71012007-11-26 20:15:35 +0100153 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100154 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 s->logs.bytes_out = s->rep->total;
156 if (bytes) {
157 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100158
Willy Tarreau30e71012007-11-26 20:15:35 +0100159 if (s->be != s->fe)
160 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100161
Willy Tarreau30e71012007-11-26 20:15:35 +0100162 if (s->srv)
163 s->srv->bytes_out += bytes;
164 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100165 }
166}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100168/* This function is called with (si->state == SI_ST_CON) meaning that a
169 * connection was attempted and that the file descriptor is already allocated.
170 * We must check for establishment, error and abort. Possible output states
171 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
172 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
173 * otherwise 1.
174 */
175int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
176{
177 struct buffer *req = si->ob;
178 struct buffer *rep = si->ib;
179
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100180 /* If we got an error, or if nothing happened and the connection timed
181 * out, we must give up. The CER state handler will take care of retry
182 * attempts and error reports.
183 */
184 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
185 si->state = SI_ST_CER;
186 fd_delete(si->fd);
187
188 if (si->err_type)
189 return 0;
190
191 si->err_loc = s->srv;
192 if (si->flags & SI_FL_ERR)
193 si->err_type = SI_ET_CONN_ERR;
194 else
195 si->err_type = SI_ET_CONN_TO;
196 return 0;
197 }
198
199 /* OK, maybe we want to abort */
200 if (unlikely((req->flags & BF_SHUTW_NOW) ||
201 (rep->flags & BF_SHUTW) ||
202 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100203 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100204 s->be->options & PR_O_ABRT_CLOSE)))) {
205 /* give up */
206 si->shutw(si);
207 si->err_type |= SI_ET_CONN_ABRT;
208 si->err_loc = s->srv;
209 return 1;
210 }
211
212 /* we need to wait a bit more if there was no activity either */
213 if (!(req->flags & BF_WRITE_ACTIVITY))
214 return 1;
215
216 /* OK, this means that a connection succeeded. The caller will be
217 * responsible for handling the transition from CON to EST.
218 */
219 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
220 si->state = SI_ST_EST;
221 si->err_type = SI_ET_NONE;
222 si->err_loc = NULL;
223 return 1;
224}
225
226/* This function is called with (si->state == SI_ST_CER) meaning that a
227 * previous connection attempt has failed and that the file descriptor
228 * has already been released. Possible causes include asynchronous error
229 * notification and time out. Possible output states are SI_ST_CLO when
230 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
231 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
232 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
233 * marked as in error state. It returns 0.
234 */
235int sess_update_st_cer(struct session *s, struct stream_interface *si)
236{
237 /* we probably have to release last session from the server */
238 if (s->srv) {
239 if (s->flags & SN_CURR_SESS) {
240 s->flags &= ~SN_CURR_SESS;
241 s->srv->cur_sess--;
242 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100243 }
244
245 /* ensure that we have enough retries left */
246 s->conn_retries--;
247 if (s->conn_retries < 0) {
248 if (!si->err_type) {
249 si->err_type = SI_ET_CONN_ERR;
250 si->err_loc = s->srv;
251 }
252
253 if (s->srv)
254 s->srv->failed_conns++;
255 s->be->failed_conns++;
256 if (may_dequeue_tasks(s->srv, s->be))
257 process_srv_queue(s->srv);
258
259 /* shutw is enough so stop a connecting socket */
260 si->shutw(si);
261 si->ob->flags |= BF_WRITE_ERROR;
262 si->ib->flags |= BF_READ_ERROR;
263
264 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100265 if (s->srv_error)
266 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100267 return 0;
268 }
269
270 /* If the "redispatch" option is set on the backend, we are allowed to
271 * retry on another server for the last retry. In order to achieve this,
272 * we must mark the session unassigned, and eventually clear the DIRECT
273 * bit to ignore any persistence cookie. We won't count a retry nor a
274 * redispatch yet, because this will depend on what server is selected.
275 */
276 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
277 if (may_dequeue_tasks(s->srv, s->be))
278 process_srv_queue(s->srv);
279
280 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
281 s->prev_srv = s->srv;
282 si->state = SI_ST_REQ;
283 } else {
284 if (s->srv)
285 s->srv->retries++;
286 s->be->retries++;
287 si->state = SI_ST_ASS;
288 }
289
290 if (si->flags & SI_FL_ERR) {
291 /* The error was an asynchronous connection error, and we will
292 * likely have to retry connecting to the same server, most
293 * likely leading to the same result. To avoid this, we wait
294 * one second before retrying.
295 */
296
297 if (!si->err_type)
298 si->err_type = SI_ET_CONN_ERR;
299
300 si->state = SI_ST_TAR;
301 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
302 return 0;
303 }
304 return 0;
305}
306
307/*
308 * This function handles the transition between the SI_ST_CON state and the
309 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
310 * SI_ST_EST.
311 */
312void sess_establish(struct session *s, struct stream_interface *si)
313{
314 struct buffer *req = si->ob;
315 struct buffer *rep = si->ib;
316
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100317 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100318 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
319
320 /* if the user wants to log as soon as possible, without counting
321 * bytes from the server, then this is the right moment. */
322 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
323 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100324 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100325 }
326#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100327 if ((global.tune.options & GTUNE_USE_SPLICE) &&
328 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100329 /* TCP splicing supported by both FE and BE */
330 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
331 }
332#endif
333 }
334 else {
335 rep->analysers |= AN_RTR_HTTP_HDR;
336 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
337 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
338 /* reset hdr_idx which was already initialized by the request.
339 * right now, the http parser does it.
340 * hdr_idx_init(&s->txn.hdr_idx);
341 */
342 }
343
344 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
345 req->wex = TICK_ETERNITY;
346}
347
348/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
349 * Other input states are simply ignored.
350 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
351 * Flags must have previously been updated for timeouts and other conditions.
352 */
353void sess_update_stream_int(struct session *s, struct stream_interface *si)
354{
355 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",
356 now_ms, __FUNCTION__,
357 s,
358 s->req, s->rep,
359 s->req->rex, s->rep->wex,
360 s->req->flags, s->rep->flags,
361 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
362
363 if (si->state == SI_ST_ASS) {
364 /* Server assigned to connection request, we have to try to connect now */
365 int conn_err;
366
367 conn_err = connect_server(s);
368 if (conn_err == SN_ERR_NONE) {
369 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100370 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100371 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100372 return;
373 }
374
375 /* We have received a synchronous error. We might have to
376 * abort, retry immediately or redispatch.
377 */
378 if (conn_err == SN_ERR_INTERNAL) {
379 if (!si->err_type) {
380 si->err_type = SI_ET_CONN_OTHER;
381 si->err_loc = s->srv;
382 }
383
384 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100385 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100386 if (s->srv)
387 s->srv->failed_conns++;
388 s->be->failed_conns++;
389
390 /* release other sessions waiting for this server */
391 if (may_dequeue_tasks(s->srv, s->be))
392 process_srv_queue(s->srv);
393
394 /* Failed and not retryable. */
395 si->shutr(si);
396 si->shutw(si);
397 si->ob->flags |= BF_WRITE_ERROR;
398
399 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
400
401 /* no session was ever accounted for this server */
402 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100403 if (s->srv_error)
404 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100405 return;
406 }
407
408 /* We are facing a retryable error, but we don't want to run a
409 * turn-around now, as the problem is likely a source port
410 * allocation problem, so we want to retry now.
411 */
412 si->state = SI_ST_CER;
413 si->flags &= ~SI_FL_ERR;
414 sess_update_st_cer(s, si);
415 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
416 return;
417 }
418 else if (si->state == SI_ST_QUE) {
419 /* connection request was queued, check for any update */
420 if (!s->pend_pos) {
421 /* The connection is not in the queue anymore. Either
422 * we have a server connection slot available and we
423 * go directly to the assigned state, or we need to
424 * load-balance first and go to the INI state.
425 */
426 si->exp = TICK_ETERNITY;
427 if (unlikely(!(s->flags & SN_ASSIGNED)))
428 si->state = SI_ST_REQ;
429 else {
430 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
431 si->state = SI_ST_ASS;
432 }
433 return;
434 }
435
436 /* Connection request still in queue... */
437 if (si->flags & SI_FL_EXP) {
438 /* ... and timeout expired */
439 si->exp = TICK_ETERNITY;
440 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
441 if (s->srv)
442 s->srv->failed_conns++;
443 s->be->failed_conns++;
444 si->shutr(si);
445 si->shutw(si);
446 si->ob->flags |= BF_WRITE_TIMEOUT;
447 if (!si->err_type)
448 si->err_type = SI_ET_QUEUE_TO;
449 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100450 if (s->srv_error)
451 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100452 return;
453 }
454
455 /* Connection remains in queue, check if we have to abort it */
456 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
457 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
458 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
459 /* give up */
460 si->exp = TICK_ETERNITY;
461 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
462 si->shutr(si);
463 si->shutw(si);
464 si->err_type |= SI_ET_QUEUE_ABRT;
465 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100466 if (s->srv_error)
467 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100468 return;
469 }
470
471 /* Nothing changed */
472 return;
473 }
474 else if (si->state == SI_ST_TAR) {
475 /* Connection request might be aborted */
476 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
477 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
478 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
479 /* give up */
480 si->exp = TICK_ETERNITY;
481 si->shutr(si);
482 si->shutw(si);
483 si->err_type |= SI_ET_CONN_ABRT;
484 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100485 if (s->srv_error)
486 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100487 return;
488 }
489
490 if (!(si->flags & SI_FL_EXP))
491 return; /* still in turn-around */
492
493 si->exp = TICK_ETERNITY;
494
495 /* we keep trying on the same server as long as the session is
496 * marked "assigned".
497 * FIXME: Should we force a redispatch attempt when the server is down ?
498 */
499 if (s->flags & SN_ASSIGNED)
500 si->state = SI_ST_ASS;
501 else
502 si->state = SI_ST_REQ;
503 return;
504 }
505}
506
507/* This function initiates a server connection request on a stream interface
508 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
509 * indicating that a server has been assigned. It may also return SI_ST_QUE,
510 * or SI_ST_CLO upon error.
511 */
512static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
513 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",
514 now_ms, __FUNCTION__,
515 s,
516 s->req, s->rep,
517 s->req->rex, s->rep->wex,
518 s->req->flags, s->rep->flags,
519 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
520
521 if (si->state != SI_ST_REQ)
522 return;
523
524 /* Try to assign a server */
525 if (srv_redispatch_connect(s) != 0) {
526 /* We did not get a server. Either we queued the
527 * connection request, or we encountered an error.
528 */
529 if (si->state == SI_ST_QUE)
530 return;
531
532 /* we did not get any server, let's check the cause */
533 si->shutr(si);
534 si->shutw(si);
535 si->ob->flags |= BF_WRITE_ERROR;
536 if (!si->err_type)
537 si->err_type = SI_ET_CONN_OTHER;
538 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100539 if (s->srv_error)
540 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 return;
542 }
543
544 /* The server is assigned */
545 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
546 si->state = SI_ST_ASS;
547}
548
549/* Processes the client, server, request and response jobs of a session task,
550 * then puts it back to the wait queue in a clean state, or cleans up its
551 * resources if it must be deleted. Returns in <next> the date the task wants
552 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
553 * nothing too many times, the request and response buffers flags are monitored
554 * and each function is called only if at least another function has changed at
555 * least one flag it is interested in.
556 */
557void process_session(struct task *t, int *next)
558{
559 struct session *s = t->context;
560 int resync;
561 unsigned int rqf_last, rpf_last;
562
563 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
564 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
565
566 /* 1a: Check for low level timeouts if needed. We just set a flag on
567 * stream interfaces when their timeouts have expired.
568 */
569 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
570 stream_int_check_timeouts(&s->si[0]);
571 stream_int_check_timeouts(&s->si[1]);
572 buffer_check_timeouts(s->req);
573 buffer_check_timeouts(s->rep);
574 }
575
Willy Tarreau86491c32008-12-14 09:04:47 +0100576 s->req->flags &= ~BF_READ_NOEXP;
577
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 /* copy req/rep flags so that we can detect shutdowns */
579 rqf_last = s->req->flags;
580 rpf_last = s->rep->flags;
581
582 /* 1b: check for low-level errors reported at the stream interface.
583 * First we check if it's a retryable error (in which case we don't
584 * want to tell the buffer). Otherwise we report the error one level
585 * upper by setting flags into the buffers. Note that the side towards
586 * the client cannot have connect (hence retryable) errors. Also, the
587 * connection setup code must be able to deal with any type of abort.
588 */
589 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
590 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
591 s->si[0].shutr(&s->si[0]);
592 s->si[0].shutw(&s->si[0]);
593 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100594 if (!(s->req->analysers) && !(s->rep->analysers)) {
595 if (!(s->flags & SN_ERR_MASK))
596 s->flags |= SN_ERR_CLICL;
597 if (!(s->flags & SN_FINST_MASK))
598 s->flags |= SN_FINST_D;
599 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 }
601 }
602
603 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
604 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
605 s->si[1].shutr(&s->si[1]);
606 s->si[1].shutw(&s->si[1]);
607 stream_int_report_error(&s->si[1]);
608 s->be->failed_resp++;
609 if (s->srv)
610 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100611 if (!(s->req->analysers) && !(s->rep->analysers)) {
612 if (!(s->flags & SN_ERR_MASK))
613 s->flags |= SN_ERR_SRVCL;
614 if (!(s->flags & SN_FINST_MASK))
615 s->flags |= SN_FINST_D;
616 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 }
618 /* note: maybe we should process connection errors here ? */
619 }
620
621 if (s->si[1].state == SI_ST_CON) {
622 /* we were trying to establish a connection on the server side,
623 * maybe it succeeded, maybe it failed, maybe we timed out, ...
624 */
625 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
626 sess_update_st_cer(s, &s->si[1]);
627 else if (s->si[1].state == SI_ST_EST)
628 sess_establish(s, &s->si[1]);
629
630 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
631 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
632 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
633 */
634 }
635
636 /* check buffer timeouts, and close the corresponding stream interfaces
637 * for future reads or writes. Note: this will also concern upper layers
638 * but we do not touch any other flag. We must be careful and correctly
639 * detect state changes when calling them.
640 */
641 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
642 if (s->req->flags & BF_READ_TIMEOUT)
643 s->req->prod->shutr(s->req->prod);
644 if (s->req->flags & BF_WRITE_TIMEOUT)
645 s->req->cons->shutw(s->req->cons);
646 DPRINTF(stderr,
647 "[%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",
648 now_ms, __FUNCTION__, __LINE__,
649 t,
650 s, s->flags,
651 s->req, s->rep,
652 s->req->rex, s->rep->wex,
653 s->req->flags, s->rep->flags,
654 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
655 s->rep->cons->err_type, s->req->cons->err_type,
656 s->conn_retries);
657 }
658
659 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
660 if (s->rep->flags & BF_READ_TIMEOUT)
661 s->rep->prod->shutr(s->rep->prod);
662 if (s->rep->flags & BF_WRITE_TIMEOUT)
663 s->rep->cons->shutw(s->rep->cons);
664 DPRINTF(stderr,
665 "[%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",
666 now_ms, __FUNCTION__, __LINE__,
667 t,
668 s, s->flags,
669 s->req, s->rep,
670 s->req->rex, s->rep->wex,
671 s->req->flags, s->rep->flags,
672 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
673 s->rep->cons->err_type, s->req->cons->err_type,
674 s->conn_retries);
675 }
676
677 /* Check for connection closure */
678
679resync_stream_interface:
680 DPRINTF(stderr,
681 "[%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",
682 now_ms, __FUNCTION__, __LINE__,
683 t,
684 s, s->flags,
685 s->req, s->rep,
686 s->req->rex, s->rep->wex,
687 s->req->flags, s->rep->flags,
688 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
689 s->rep->cons->err_type, s->req->cons->err_type,
690 s->conn_retries);
691
692 /* nothing special to be done on client side */
693 if (unlikely(s->req->prod->state == SI_ST_DIS))
694 s->req->prod->state = SI_ST_CLO;
695
696 /* When a server-side connection is released, we have to count it and
697 * check for pending connections on this server.
698 */
699 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
700 s->req->cons->state = SI_ST_CLO;
701 if (s->srv) {
702 if (s->flags & SN_CURR_SESS) {
703 s->flags &= ~SN_CURR_SESS;
704 s->srv->cur_sess--;
705 }
706 sess_change_server(s, NULL);
707 if (may_dequeue_tasks(s->srv, s->be))
708 process_srv_queue(s->srv);
709 }
710 }
711
712 /*
713 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
714 * at this point.
715 */
716
717 /**** Process layer 7 below ****/
718
719 resync = 0;
720
721 /* Analyse request */
722 if ((s->req->flags & BF_MASK_ANALYSER) ||
723 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
724 unsigned int flags = s->req->flags;
725
726 if (s->req->prod->state >= SI_ST_EST) {
727 /* it's up to the analysers to reset write_ena */
728 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100729
730 /* We will call all analysers for which a bit is set in
731 * s->req->analysers, following the bit order from LSB
732 * to MSB. The analysers must remove themselves from
733 * the list when not needed. This while() loop is in
734 * fact a cleaner if().
735 */
736 while (s->req->analysers) {
737 if (s->req->analysers & AN_REQ_INSPECT)
738 if (!tcp_inspect_request(s, s->req))
739 break;
740
Willy Tarreau59234e92008-11-30 23:51:27 +0100741 if (s->req->analysers & AN_REQ_HTTP_HDR)
742 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100743 break;
744
Willy Tarreau60b85b02008-11-30 23:28:40 +0100745 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
746 if (!http_process_tarpit(s, s->req))
747 break;
748
Willy Tarreaud34af782008-11-30 23:36:37 +0100749 if (s->req->analysers & AN_REQ_HTTP_BODY)
750 if (!http_process_request_body(s, s->req))
751 break;
752
Willy Tarreauedcf6682008-11-30 23:15:34 +0100753 /* Just make sure that nobody set a wrong flag causing an endless loop */
754 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
755
756 /* we don't want to loop anyway */
757 break;
758 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 }
760 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
761 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
762 if (s->req->flags != flags)
763 resync = 1;
764 }
765
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100766 /* if noone is interested in analysing data, let's forward everything
767 * and only wake up every 1-2 MB. We still wake up when send_max is
768 * reached though.
769 */
770 if (!s->req->send_max && s->req->prod->state >= SI_ST_EST &&
771 !s->req->analysers && !(s->req->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100772 /* check if it is wise to enable kernel splicing on the request buffer */
773 if (!(s->req->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100774 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100775 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100776 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
777 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
778 (s->req->flags & BF_STREAMER_FAST))))
779 s->req->flags |= BF_KERN_SPLICING;
780
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100781 if (s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100782 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100783 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100784
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100785 /* reflect what the L7 analysers have seen last */
786 rqf_last = s->req->flags;
787
788 /*
789 * Now forward all shutdown requests between both sides of the buffer
790 */
791
792 /* first, let's check if the request buffer needs to shutdown(write) */
793 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
794 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
795 buffer_shutw_now(s->req);
796 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
797 (s->req->cons->state == SI_ST_EST) &&
798 s->be->options & PR_O_FORCE_CLO &&
799 s->rep->flags & BF_READ_ACTIVITY) {
800 /* We want to force the connection to the server to close,
801 * and the server has begun to respond. That's the right
802 * time.
803 */
804 buffer_shutw_now(s->req);
805 }
806
807 /* shutdown(write) pending */
808 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
809 s->req->cons->shutw(s->req->cons);
810
811 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100812 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
813 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 buffer_shutr_now(s->req);
815
816 /* shutdown(read) pending */
817 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
818 s->req->prod->shutr(s->req->prod);
819
Willy Tarreau92795622009-03-06 12:51:23 +0100820 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100821 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100822 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
823 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
824 s->req->cons->state = SI_ST_REQ; /* new connection requested */
825 else
826 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
827 }
828
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829
830 /* we may have a pending connection request, or a connection waiting
831 * for completion.
832 */
833 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
834 do {
835 /* nb: step 1 might switch from QUE to ASS, but we first want
836 * to give a chance to step 2 to perform a redirect if needed.
837 */
838 if (s->si[1].state != SI_ST_REQ)
839 sess_update_stream_int(s, &s->si[1]);
840 if (s->si[1].state == SI_ST_REQ)
841 sess_prepare_conn_req(s, &s->si[1]);
842
843 if (s->si[1].state == SI_ST_ASS && s->srv &&
844 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
845 perform_http_redirect(s, &s->si[1]);
846 } while (s->si[1].state == SI_ST_ASS);
847 }
848
849 /*
850 * Here we want to check if we need to resync or not.
851 */
852 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
853 resync = 1;
854
855 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
856
857 /* according to benchmarks, it makes sense to resync now */
858 if (resync)
859 goto resync_stream_interface;
860
861
862 /* Analyse response */
863
864 if (unlikely(s->rep->flags & BF_HIJACK)) {
865 /* In inject mode, we wake up everytime something has
866 * happened on the write side of the buffer.
867 */
868 unsigned int flags = s->rep->flags;
869
870 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
871 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100872 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873 }
874 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
875 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
876 if (s->rep->flags != flags)
877 resync = 1;
878 }
879 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
880 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
881 unsigned int flags = s->rep->flags;
882
883 if (s->rep->prod->state >= SI_ST_EST) {
884 /* it's up to the analysers to reset write_ena */
885 buffer_write_ena(s->rep);
886 if (s->rep->analysers)
887 process_response(s);
888 }
889 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
890 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
891 if (s->rep->flags != flags)
892 resync = 1;
893 }
894
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100895 /* if noone is interested in analysing data, let's forward everything
896 * and only wake up every 1-2 MB. We still wake up when send_max is
897 * reached though.
898 */
899 if (!s->rep->send_max && s->rep->prod->state >= SI_ST_EST &&
900 !s->rep->analysers && !(s->rep->flags & BF_HIJACK)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100901 /* check if it is wise to enable kernel splicing on the response buffer */
902 if (!(s->rep->flags & BF_KERN_SPLICING) &&
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100903 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100904 (pipes_used < global.maxpipes) &&
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100905 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
906 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
907 (s->rep->flags & BF_STREAMER_FAST))))
908 s->rep->flags |= BF_KERN_SPLICING;
909
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100910 if (s->rep->to_forward < FORWARD_DEFAULT_SIZE)
911 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100912 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100913
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 /* reflect what the L7 analysers have seen last */
915 rpf_last = s->rep->flags;
916
917 /*
918 * Now forward all shutdown requests between both sides of the buffer
919 */
920
921 /*
922 * FIXME: this is probably where we should produce error responses.
923 */
924
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100925 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
927 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
928 buffer_shutw_now(s->rep);
929
930 /* shutdown(write) pending */
931 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
932 s->rep->cons->shutw(s->rep->cons);
933
934 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100935 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
936 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937 buffer_shutr_now(s->rep);
938
939 /* shutdown(read) pending */
940 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
941 s->rep->prod->shutr(s->rep->prod);
942
943 /*
944 * Here we want to check if we need to resync or not.
945 */
946 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
947 resync = 1;
948
949 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
950
951 if (resync)
952 goto resync_stream_interface;
953
954
955 /* This is needed only when debugging is enabled, to indicate
956 * client-side or server-side close. Please note that in the unlikely
957 * event where both sides would close at once, the sequence is reported
958 * on the server side first.
959 */
960 if (unlikely((global.mode & MODE_DEBUG) &&
961 (!(global.mode & MODE_QUIET) ||
962 (global.mode & MODE_VERBOSE)))) {
963 int len;
964
965 if (s->si[1].state == SI_ST_CLO &&
966 s->si[1].prev_state == SI_ST_EST) {
967 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
968 s->uniq_id, s->be->id,
969 (unsigned short)s->si[0].fd,
970 (unsigned short)s->si[1].fd);
971 write(1, trash, len);
972 }
973
974 if (s->si[0].state == SI_ST_CLO &&
975 s->si[0].prev_state == SI_ST_EST) {
976 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
977 s->uniq_id, s->be->id,
978 (unsigned short)s->si[0].fd,
979 (unsigned short)s->si[1].fd);
980 write(1, trash, len);
981 }
982 }
983
984 if (likely((s->rep->cons->state != SI_ST_CLO) ||
985 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
986
987 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
988 session_process_counters(s);
989
990 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100991 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100992
993 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100994 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995
996 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
997 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
998 s->si[0].prev_state = s->si[0].state;
999 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001000 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1001 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001002
1003 /* Trick: if a request is being waiting for the server to respond,
1004 * and if we know the server can timeout, we don't want the timeout
1005 * to expire on the client side first, but we're still interested
1006 * in passing data from the client to the server (eg: POST). Thus,
1007 * we can cancel the client's request timeout if the server's
1008 * request timeout is set and the server has not yet sent a response.
1009 */
1010
1011 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001012 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1013 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001014 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001015 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001016
1017 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1018 tick_first(s->rep->rex, s->rep->wex));
1019 if (s->req->analysers)
1020 t->expire = tick_first(t->expire, s->req->analyse_exp);
1021
1022 if (s->si[0].exp)
1023 t->expire = tick_first(t->expire, s->si[0].exp);
1024
1025 if (s->si[1].exp)
1026 t->expire = tick_first(t->expire, s->si[1].exp);
1027
1028#ifdef DEBUG_FULL
1029 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",
1030 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);
1031#endif
1032 /* restore t to its place in the task list */
1033 task_queue(t);
1034
1035#ifdef DEBUG_DEV
1036 /* this may only happen when no timeout is set or in case of an FSM bug */
1037 if (!t->expire)
1038 ABORT_NOW();
1039#endif
1040 *next = t->expire;
1041 return; /* nothing more to do */
1042 }
1043
1044 s->fe->feconn--;
1045 if (s->flags & SN_BE_ASSIGNED)
1046 s->be->beconn--;
1047 actconn--;
1048
1049 if (unlikely((global.mode & MODE_DEBUG) &&
1050 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1051 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001052 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001053 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001054 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055 write(1, trash, len);
1056 }
1057
1058 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1059 session_process_counters(s);
1060
1061 /* let's do a final log if we need it */
1062 if (s->logs.logwait &&
1063 !(s->flags & SN_MONITOR) &&
1064 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001065 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001066 }
1067
1068 /* the task MUST not be in the run queue anymore */
1069 task_delete(t);
1070 session_free(s);
1071 task_free(t);
1072 *next = TICK_ETERNITY;
1073}
1074
Willy Tarreau7c669d72008-06-20 15:04:11 +02001075/*
1076 * This function adjusts sess->srv_conn and maintains the previous and new
1077 * server's served session counts. Setting newsrv to NULL is enough to release
1078 * current connection slot. This function also notifies any LB algo which might
1079 * expect to be informed about any change in the number of active sessions on a
1080 * server.
1081 */
1082void sess_change_server(struct session *sess, struct server *newsrv)
1083{
1084 if (sess->srv_conn == newsrv)
1085 return;
1086
1087 if (sess->srv_conn) {
1088 sess->srv_conn->served--;
1089 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1090 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1091 sess->srv_conn = NULL;
1092 }
1093
1094 if (newsrv) {
1095 newsrv->served++;
1096 if (newsrv->proxy->lbprm.server_take_conn)
1097 newsrv->proxy->lbprm.server_take_conn(newsrv);
1098 sess->srv_conn = newsrv;
1099 }
1100}
1101
1102
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103/*
1104 * Local variables:
1105 * c-indent-level: 8
1106 * c-basic-offset: 8
1107 * End:
1108 */