blob: b5cb81ede566c3f3de8e193d44cfd2c4f08879de [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 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100557struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558{
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
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100717 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 /**** Process layer 7 below ****/
719
720 resync = 0;
721
722 /* Analyse request */
723 if ((s->req->flags & BF_MASK_ANALYSER) ||
724 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
725 unsigned int flags = s->req->flags;
726
727 if (s->req->prod->state >= SI_ST_EST) {
728 /* it's up to the analysers to reset write_ena */
729 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100730
731 /* We will call all analysers for which a bit is set in
732 * s->req->analysers, following the bit order from LSB
733 * to MSB. The analysers must remove themselves from
734 * the list when not needed. This while() loop is in
735 * fact a cleaner if().
736 */
737 while (s->req->analysers) {
738 if (s->req->analysers & AN_REQ_INSPECT)
739 if (!tcp_inspect_request(s, s->req))
740 break;
741
Willy Tarreau59234e92008-11-30 23:51:27 +0100742 if (s->req->analysers & AN_REQ_HTTP_HDR)
743 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100744 break;
745
Willy Tarreau60b85b02008-11-30 23:28:40 +0100746 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
747 if (!http_process_tarpit(s, s->req))
748 break;
749
Willy Tarreaud34af782008-11-30 23:36:37 +0100750 if (s->req->analysers & AN_REQ_HTTP_BODY)
751 if (!http_process_request_body(s, s->req))
752 break;
753
Willy Tarreauedcf6682008-11-30 23:15:34 +0100754 /* Just make sure that nobody set a wrong flag causing an endless loop */
755 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
756
757 /* we don't want to loop anyway */
758 break;
759 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 }
761 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
762 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
763 if (s->req->flags != flags)
764 resync = 1;
765 }
766
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100767 /* If noone is interested in analysing data, it's time to forward
768 * everything. We will wake up from time to time when either send_max
769 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100770 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100771 if (!s->req->analysers &&
772 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
773 (s->req->prod->state >= SI_ST_EST)) {
774 /* This buffer is freewheeling, there's no analyser nor hijacker
775 * attached to it. If any data are left in, we'll permit them to
776 * move.
777 */
778 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100779
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100780 /* If the producer is still connected, we'll schedule large blocks
781 * of data to be forwarded from the producer to the consumer (which
782 * might possibly not be connected yet).
783 */
784 if (!(s->req->flags & BF_SHUTR) &&
785 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100786 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100787 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100788
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100789 /* check if it is wise to enable kernel splicing to forward request data */
790 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
791 s->req->to_forward &&
792 (global.tune.options & GTUNE_USE_SPLICE) &&
793 (pipes_used < global.maxpipes) &&
794 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
795 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
796 (s->req->flags & BF_STREAMER_FAST)))) {
797 s->req->flags |= BF_KERN_SPLICING;
798 }
799
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100800 /* reflect what the L7 analysers have seen last */
801 rqf_last = s->req->flags;
802
803 /*
804 * Now forward all shutdown requests between both sides of the buffer
805 */
806
807 /* first, let's check if the request buffer needs to shutdown(write) */
808 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
809 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
810 buffer_shutw_now(s->req);
811 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
812 (s->req->cons->state == SI_ST_EST) &&
813 s->be->options & PR_O_FORCE_CLO &&
814 s->rep->flags & BF_READ_ACTIVITY) {
815 /* We want to force the connection to the server to close,
816 * and the server has begun to respond. That's the right
817 * time.
818 */
819 buffer_shutw_now(s->req);
820 }
821
822 /* shutdown(write) pending */
823 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
824 s->req->cons->shutw(s->req->cons);
825
826 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100827 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
828 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 buffer_shutr_now(s->req);
830
831 /* shutdown(read) pending */
832 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
833 s->req->prod->shutr(s->req->prod);
834
Willy Tarreau92795622009-03-06 12:51:23 +0100835 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100837 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
838 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
839 s->req->cons->state = SI_ST_REQ; /* new connection requested */
840 else
841 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
842 }
843
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844
845 /* we may have a pending connection request, or a connection waiting
846 * for completion.
847 */
848 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
849 do {
850 /* nb: step 1 might switch from QUE to ASS, but we first want
851 * to give a chance to step 2 to perform a redirect if needed.
852 */
853 if (s->si[1].state != SI_ST_REQ)
854 sess_update_stream_int(s, &s->si[1]);
855 if (s->si[1].state == SI_ST_REQ)
856 sess_prepare_conn_req(s, &s->si[1]);
857
858 if (s->si[1].state == SI_ST_ASS && s->srv &&
859 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
860 perform_http_redirect(s, &s->si[1]);
861 } while (s->si[1].state == SI_ST_ASS);
862 }
863
864 /*
865 * Here we want to check if we need to resync or not.
866 */
867 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
868 resync = 1;
869
870 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
871
872 /* according to benchmarks, it makes sense to resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100873 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100874 goto resync_stream_interface;
875
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100876 if (resync)
877 goto resync_request;
878
879 resync_response:
880 resync = 0;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100881
882 /* Analyse response */
883
884 if (unlikely(s->rep->flags & BF_HIJACK)) {
885 /* In inject mode, we wake up everytime something has
886 * happened on the write side of the buffer.
887 */
888 unsigned int flags = s->rep->flags;
889
890 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
891 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100892 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893 }
894 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
895 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
896 if (s->rep->flags != flags)
897 resync = 1;
898 }
899 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
900 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
901 unsigned int flags = s->rep->flags;
902
903 if (s->rep->prod->state >= SI_ST_EST) {
904 /* it's up to the analysers to reset write_ena */
905 buffer_write_ena(s->rep);
906 if (s->rep->analysers)
907 process_response(s);
908 }
909 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
910 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
911 if (s->rep->flags != flags)
912 resync = 1;
913 }
914
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100915 /* If noone is interested in analysing data, it's time to forward
916 * everything. We will wake up from time to time when either send_max
917 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100918 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100919 if (!s->rep->analysers &&
920 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
921 (s->rep->prod->state >= SI_ST_EST)) {
922 /* This buffer is freewheeling, there's no analyser nor hijacker
923 * attached to it. If any data are left in, we'll permit them to
924 * move.
925 */
926 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100927
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100928 /* If the producer is still connected, we'll schedule large blocks
929 * of data to be forwarded from the producer to the consumer (which
930 * might possibly not be connected yet).
931 */
932 if (!(s->rep->flags & BF_SHUTR) &&
933 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100934 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100935 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100936
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100937 /* check if it is wise to enable kernel splicing to forward response data */
938 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
939 s->rep->to_forward &&
940 (global.tune.options & GTUNE_USE_SPLICE) &&
941 (pipes_used < global.maxpipes) &&
942 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
943 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
944 (s->rep->flags & BF_STREAMER_FAST)))) {
945 s->rep->flags |= BF_KERN_SPLICING;
946 }
947
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100948 /* reflect what the L7 analysers have seen last */
949 rpf_last = s->rep->flags;
950
951 /*
952 * Now forward all shutdown requests between both sides of the buffer
953 */
954
955 /*
956 * FIXME: this is probably where we should produce error responses.
957 */
958
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100959 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100960 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
961 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
962 buffer_shutw_now(s->rep);
963
964 /* shutdown(write) pending */
965 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
966 s->rep->cons->shutw(s->rep->cons);
967
968 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100969 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
970 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100971 buffer_shutr_now(s->rep);
972
973 /* shutdown(read) pending */
974 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
975 s->rep->prod->shutr(s->rep->prod);
976
977 /*
978 * Here we want to check if we need to resync or not.
979 */
980 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
981 resync = 1;
982
983 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
984
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100985 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986 goto resync_stream_interface;
987
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100988 if (s->req->flags != rqf_last)
989 goto resync_request;
990
991 if (resync)
992 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100993
994 /* This is needed only when debugging is enabled, to indicate
995 * client-side or server-side close. Please note that in the unlikely
996 * event where both sides would close at once, the sequence is reported
997 * on the server side first.
998 */
999 if (unlikely((global.mode & MODE_DEBUG) &&
1000 (!(global.mode & MODE_QUIET) ||
1001 (global.mode & MODE_VERBOSE)))) {
1002 int len;
1003
1004 if (s->si[1].state == SI_ST_CLO &&
1005 s->si[1].prev_state == SI_ST_EST) {
1006 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1007 s->uniq_id, s->be->id,
1008 (unsigned short)s->si[0].fd,
1009 (unsigned short)s->si[1].fd);
1010 write(1, trash, len);
1011 }
1012
1013 if (s->si[0].state == SI_ST_CLO &&
1014 s->si[0].prev_state == SI_ST_EST) {
1015 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1016 s->uniq_id, s->be->id,
1017 (unsigned short)s->si[0].fd,
1018 (unsigned short)s->si[1].fd);
1019 write(1, trash, len);
1020 }
1021 }
1022
1023 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1024 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1025
1026 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1027 session_process_counters(s);
1028
1029 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001030 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001031
1032 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001033 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001034
1035 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1036 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1037 s->si[0].prev_state = s->si[0].state;
1038 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001039 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1040 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041
1042 /* Trick: if a request is being waiting for the server to respond,
1043 * and if we know the server can timeout, we don't want the timeout
1044 * to expire on the client side first, but we're still interested
1045 * in passing data from the client to the server (eg: POST). Thus,
1046 * we can cancel the client's request timeout if the server's
1047 * request timeout is set and the server has not yet sent a response.
1048 */
1049
1050 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001051 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1052 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001053 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001054 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055
1056 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1057 tick_first(s->rep->rex, s->rep->wex));
1058 if (s->req->analysers)
1059 t->expire = tick_first(t->expire, s->req->analyse_exp);
1060
1061 if (s->si[0].exp)
1062 t->expire = tick_first(t->expire, s->si[0].exp);
1063
1064 if (s->si[1].exp)
1065 t->expire = tick_first(t->expire, s->si[1].exp);
1066
1067#ifdef DEBUG_FULL
1068 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",
1069 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);
1070#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001071
1072#ifdef DEBUG_DEV
1073 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001074 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001075 ABORT_NOW();
1076#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001077 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001078 }
1079
1080 s->fe->feconn--;
1081 if (s->flags & SN_BE_ASSIGNED)
1082 s->be->beconn--;
1083 actconn--;
1084
1085 if (unlikely((global.mode & MODE_DEBUG) &&
1086 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1087 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001088 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001089 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001090 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001091 write(1, trash, len);
1092 }
1093
1094 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1095 session_process_counters(s);
1096
1097 /* let's do a final log if we need it */
1098 if (s->logs.logwait &&
1099 !(s->flags & SN_MONITOR) &&
1100 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001101 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001102 }
1103
1104 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001105 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001106 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001108 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001109}
1110
Willy Tarreau7c669d72008-06-20 15:04:11 +02001111/*
1112 * This function adjusts sess->srv_conn and maintains the previous and new
1113 * server's served session counts. Setting newsrv to NULL is enough to release
1114 * current connection slot. This function also notifies any LB algo which might
1115 * expect to be informed about any change in the number of active sessions on a
1116 * server.
1117 */
1118void sess_change_server(struct session *sess, struct server *newsrv)
1119{
1120 if (sess->srv_conn == newsrv)
1121 return;
1122
1123 if (sess->srv_conn) {
1124 sess->srv_conn->served--;
1125 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1126 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1127 sess->srv_conn = NULL;
1128 }
1129
1130 if (newsrv) {
1131 newsrv->served++;
1132 if (newsrv->proxy->lbprm.server_take_conn)
1133 newsrv->proxy->lbprm.server_take_conn(newsrv);
1134 sess->srv_conn = newsrv;
1135 }
1136}
1137
1138
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139/*
1140 * Local variables:
1141 * c-indent-level: 8
1142 * c-basic-offset: 8
1143 * End:
1144 */