blob: 0f82639b8505d5d336abbaa3e087351e5e85e47c [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))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100185 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100186 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200187 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100188 fd_delete(si->fd);
189
190 if (si->err_type)
191 return 0;
192
193 si->err_loc = s->srv;
194 if (si->flags & SI_FL_ERR)
195 si->err_type = SI_ET_CONN_ERR;
196 else
197 si->err_type = SI_ET_CONN_TO;
198 return 0;
199 }
200
201 /* OK, maybe we want to abort */
202 if (unlikely((req->flags & BF_SHUTW_NOW) ||
203 (rep->flags & BF_SHUTW) ||
204 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100205 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100206 s->be->options & PR_O_ABRT_CLOSE)))) {
207 /* give up */
208 si->shutw(si);
209 si->err_type |= SI_ET_CONN_ABRT;
210 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200211 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100212 if (s->srv_error)
213 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100214 return 1;
215 }
216
217 /* we need to wait a bit more if there was no activity either */
218 if (!(req->flags & BF_WRITE_ACTIVITY))
219 return 1;
220
221 /* OK, this means that a connection succeeded. The caller will be
222 * responsible for handling the transition from CON to EST.
223 */
224 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100225 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100226 si->state = SI_ST_EST;
227 si->err_type = SI_ET_NONE;
228 si->err_loc = NULL;
229 return 1;
230}
231
232/* This function is called with (si->state == SI_ST_CER) meaning that a
233 * previous connection attempt has failed and that the file descriptor
234 * has already been released. Possible causes include asynchronous error
235 * notification and time out. Possible output states are SI_ST_CLO when
236 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
237 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
238 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
239 * marked as in error state. It returns 0.
240 */
241int sess_update_st_cer(struct session *s, struct stream_interface *si)
242{
243 /* we probably have to release last session from the server */
244 if (s->srv) {
245 if (s->flags & SN_CURR_SESS) {
246 s->flags &= ~SN_CURR_SESS;
247 s->srv->cur_sess--;
248 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100249 }
250
251 /* ensure that we have enough retries left */
252 s->conn_retries--;
253 if (s->conn_retries < 0) {
254 if (!si->err_type) {
255 si->err_type = SI_ET_CONN_ERR;
256 si->err_loc = s->srv;
257 }
258
259 if (s->srv)
260 s->srv->failed_conns++;
261 s->be->failed_conns++;
262 if (may_dequeue_tasks(s->srv, s->be))
263 process_srv_queue(s->srv);
264
265 /* shutw is enough so stop a connecting socket */
266 si->shutw(si);
267 si->ob->flags |= BF_WRITE_ERROR;
268 si->ib->flags |= BF_READ_ERROR;
269
270 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100271 if (s->srv_error)
272 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100273 return 0;
274 }
275
276 /* If the "redispatch" option is set on the backend, we are allowed to
277 * retry on another server for the last retry. In order to achieve this,
278 * we must mark the session unassigned, and eventually clear the DIRECT
279 * bit to ignore any persistence cookie. We won't count a retry nor a
280 * redispatch yet, because this will depend on what server is selected.
281 */
282 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
283 if (may_dequeue_tasks(s->srv, s->be))
284 process_srv_queue(s->srv);
285
286 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
287 s->prev_srv = s->srv;
288 si->state = SI_ST_REQ;
289 } else {
290 if (s->srv)
291 s->srv->retries++;
292 s->be->retries++;
293 si->state = SI_ST_ASS;
294 }
295
296 if (si->flags & SI_FL_ERR) {
297 /* The error was an asynchronous connection error, and we will
298 * likely have to retry connecting to the same server, most
299 * likely leading to the same result. To avoid this, we wait
300 * one second before retrying.
301 */
302
303 if (!si->err_type)
304 si->err_type = SI_ET_CONN_ERR;
305
306 si->state = SI_ST_TAR;
307 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
308 return 0;
309 }
310 return 0;
311}
312
313/*
314 * This function handles the transition between the SI_ST_CON state and the
315 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
316 * SI_ST_EST.
317 */
318void sess_establish(struct session *s, struct stream_interface *si)
319{
320 struct buffer *req = si->ob;
321 struct buffer *rep = si->ib;
322
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100323 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100324 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
325
326 /* if the user wants to log as soon as possible, without counting
327 * bytes from the server, then this is the right moment. */
328 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
329 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100330 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100331 }
332#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100333 if ((global.tune.options & GTUNE_USE_SPLICE) &&
334 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100335 /* TCP splicing supported by both FE and BE */
336 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
337 }
338#endif
339 }
340 else {
341 rep->analysers |= AN_RTR_HTTP_HDR;
342 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
343 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
344 /* reset hdr_idx which was already initialized by the request.
345 * right now, the http parser does it.
346 * hdr_idx_init(&s->txn.hdr_idx);
347 */
348 }
349
350 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
351 req->wex = TICK_ETERNITY;
352}
353
354/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
355 * Other input states are simply ignored.
356 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
357 * Flags must have previously been updated for timeouts and other conditions.
358 */
359void sess_update_stream_int(struct session *s, struct stream_interface *si)
360{
361 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",
362 now_ms, __FUNCTION__,
363 s,
364 s->req, s->rep,
365 s->req->rex, s->rep->wex,
366 s->req->flags, s->rep->flags,
367 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
368
369 if (si->state == SI_ST_ASS) {
370 /* Server assigned to connection request, we have to try to connect now */
371 int conn_err;
372
373 conn_err = connect_server(s);
374 if (conn_err == SN_ERR_NONE) {
375 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100376 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100377 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100378 return;
379 }
380
381 /* We have received a synchronous error. We might have to
382 * abort, retry immediately or redispatch.
383 */
384 if (conn_err == SN_ERR_INTERNAL) {
385 if (!si->err_type) {
386 si->err_type = SI_ET_CONN_OTHER;
387 si->err_loc = s->srv;
388 }
389
390 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100391 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100392 if (s->srv)
393 s->srv->failed_conns++;
394 s->be->failed_conns++;
395
396 /* release other sessions waiting for this server */
397 if (may_dequeue_tasks(s->srv, s->be))
398 process_srv_queue(s->srv);
399
400 /* Failed and not retryable. */
401 si->shutr(si);
402 si->shutw(si);
403 si->ob->flags |= BF_WRITE_ERROR;
404
405 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
406
407 /* no session was ever accounted for this server */
408 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100409 if (s->srv_error)
410 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100411 return;
412 }
413
414 /* We are facing a retryable error, but we don't want to run a
415 * turn-around now, as the problem is likely a source port
416 * allocation problem, so we want to retry now.
417 */
418 si->state = SI_ST_CER;
419 si->flags &= ~SI_FL_ERR;
420 sess_update_st_cer(s, si);
421 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
422 return;
423 }
424 else if (si->state == SI_ST_QUE) {
425 /* connection request was queued, check for any update */
426 if (!s->pend_pos) {
427 /* The connection is not in the queue anymore. Either
428 * we have a server connection slot available and we
429 * go directly to the assigned state, or we need to
430 * load-balance first and go to the INI state.
431 */
432 si->exp = TICK_ETERNITY;
433 if (unlikely(!(s->flags & SN_ASSIGNED)))
434 si->state = SI_ST_REQ;
435 else {
436 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
437 si->state = SI_ST_ASS;
438 }
439 return;
440 }
441
442 /* Connection request still in queue... */
443 if (si->flags & SI_FL_EXP) {
444 /* ... and timeout expired */
445 si->exp = TICK_ETERNITY;
446 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
447 if (s->srv)
448 s->srv->failed_conns++;
449 s->be->failed_conns++;
450 si->shutr(si);
451 si->shutw(si);
452 si->ob->flags |= BF_WRITE_TIMEOUT;
453 if (!si->err_type)
454 si->err_type = SI_ET_QUEUE_TO;
455 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100456 if (s->srv_error)
457 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100458 return;
459 }
460
461 /* Connection remains in queue, check if we have to abort it */
462 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
463 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
464 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
465 /* give up */
466 si->exp = TICK_ETERNITY;
467 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
468 si->shutr(si);
469 si->shutw(si);
470 si->err_type |= SI_ET_QUEUE_ABRT;
471 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100472 if (s->srv_error)
473 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100474 return;
475 }
476
477 /* Nothing changed */
478 return;
479 }
480 else if (si->state == SI_ST_TAR) {
481 /* Connection request might be aborted */
482 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
483 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
484 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
485 /* give up */
486 si->exp = TICK_ETERNITY;
487 si->shutr(si);
488 si->shutw(si);
489 si->err_type |= SI_ET_CONN_ABRT;
490 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100491 if (s->srv_error)
492 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100493 return;
494 }
495
496 if (!(si->flags & SI_FL_EXP))
497 return; /* still in turn-around */
498
499 si->exp = TICK_ETERNITY;
500
501 /* we keep trying on the same server as long as the session is
502 * marked "assigned".
503 * FIXME: Should we force a redispatch attempt when the server is down ?
504 */
505 if (s->flags & SN_ASSIGNED)
506 si->state = SI_ST_ASS;
507 else
508 si->state = SI_ST_REQ;
509 return;
510 }
511}
512
513/* This function initiates a server connection request on a stream interface
514 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
515 * indicating that a server has been assigned. It may also return SI_ST_QUE,
516 * or SI_ST_CLO upon error.
517 */
518static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
519 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",
520 now_ms, __FUNCTION__,
521 s,
522 s->req, s->rep,
523 s->req->rex, s->rep->wex,
524 s->req->flags, s->rep->flags,
525 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
526
527 if (si->state != SI_ST_REQ)
528 return;
529
530 /* Try to assign a server */
531 if (srv_redispatch_connect(s) != 0) {
532 /* We did not get a server. Either we queued the
533 * connection request, or we encountered an error.
534 */
535 if (si->state == SI_ST_QUE)
536 return;
537
538 /* we did not get any server, let's check the cause */
539 si->shutr(si);
540 si->shutw(si);
541 si->ob->flags |= BF_WRITE_ERROR;
542 if (!si->err_type)
543 si->err_type = SI_ET_CONN_OTHER;
544 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100545 if (s->srv_error)
546 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 return;
548 }
549
550 /* The server is assigned */
551 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
552 si->state = SI_ST_ASS;
553}
554
555/* Processes the client, server, request and response jobs of a session task,
556 * then puts it back to the wait queue in a clean state, or cleans up its
557 * resources if it must be deleted. Returns in <next> the date the task wants
558 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
559 * nothing too many times, the request and response buffers flags are monitored
560 * and each function is called only if at least another function has changed at
561 * least one flag it is interested in.
562 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100563struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564{
565 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100566 unsigned int rqf_last, rpf_last;
567
568 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
569 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
570
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200571 /* This flag must explicitly be set every time */
572 s->req->flags &= ~BF_READ_NOEXP;
573
574 /* Keep a copy of req/rep flags so that we can detect shutdowns */
575 rqf_last = s->req->flags;
576 rpf_last = s->rep->flags;
577
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 /* 1a: Check for low level timeouts if needed. We just set a flag on
579 * stream interfaces when their timeouts have expired.
580 */
581 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
582 stream_int_check_timeouts(&s->si[0]);
583 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200584
585 /* check buffer timeouts, and close the corresponding stream interfaces
586 * for future reads or writes. Note: this will also concern upper layers
587 * but we do not touch any other flag. We must be careful and correctly
588 * detect state changes when calling them.
589 */
590
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100591 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200592
593 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
594 s->req->prod->shutr(s->req->prod);
595
596 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
597 s->req->cons->shutw(s->req->cons);
598
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200601 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
602 s->rep->prod->shutr(s->rep->prod);
Willy Tarreau86491c32008-12-14 09:04:47 +0100603
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200604 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT))
605 s->rep->cons->shutw(s->rep->cons);
606 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607
608 /* 1b: check for low-level errors reported at the stream interface.
609 * First we check if it's a retryable error (in which case we don't
610 * want to tell the buffer). Otherwise we report the error one level
611 * upper by setting flags into the buffers. Note that the side towards
612 * the client cannot have connect (hence retryable) errors. Also, the
613 * connection setup code must be able to deal with any type of abort.
614 */
615 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
616 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
617 s->si[0].shutr(&s->si[0]);
618 s->si[0].shutw(&s->si[0]);
619 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100620 if (!(s->req->analysers) && !(s->rep->analysers)) {
621 if (!(s->flags & SN_ERR_MASK))
622 s->flags |= SN_ERR_CLICL;
623 if (!(s->flags & SN_FINST_MASK))
624 s->flags |= SN_FINST_D;
625 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100626 }
627 }
628
629 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
630 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
631 s->si[1].shutr(&s->si[1]);
632 s->si[1].shutw(&s->si[1]);
633 stream_int_report_error(&s->si[1]);
634 s->be->failed_resp++;
635 if (s->srv)
636 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100637 if (!(s->req->analysers) && !(s->rep->analysers)) {
638 if (!(s->flags & SN_ERR_MASK))
639 s->flags |= SN_ERR_SRVCL;
640 if (!(s->flags & SN_FINST_MASK))
641 s->flags |= SN_FINST_D;
642 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 }
644 /* note: maybe we should process connection errors here ? */
645 }
646
647 if (s->si[1].state == SI_ST_CON) {
648 /* we were trying to establish a connection on the server side,
649 * maybe it succeeded, maybe it failed, maybe we timed out, ...
650 */
651 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
652 sess_update_st_cer(s, &s->si[1]);
653 else if (s->si[1].state == SI_ST_EST)
654 sess_establish(s, &s->si[1]);
655
656 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
657 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
658 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
659 */
660 }
661
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200662resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663 /* Check for connection closure */
664
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 DPRINTF(stderr,
666 "[%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",
667 now_ms, __FUNCTION__, __LINE__,
668 t,
669 s, s->flags,
670 s->req, s->rep,
671 s->req->rex, s->rep->wex,
672 s->req->flags, s->rep->flags,
673 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
674 s->rep->cons->err_type, s->req->cons->err_type,
675 s->conn_retries);
676
677 /* nothing special to be done on client side */
678 if (unlikely(s->req->prod->state == SI_ST_DIS))
679 s->req->prod->state = SI_ST_CLO;
680
681 /* When a server-side connection is released, we have to count it and
682 * check for pending connections on this server.
683 */
684 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
685 s->req->cons->state = SI_ST_CLO;
686 if (s->srv) {
687 if (s->flags & SN_CURR_SESS) {
688 s->flags &= ~SN_CURR_SESS;
689 s->srv->cur_sess--;
690 }
691 sess_change_server(s, NULL);
692 if (may_dequeue_tasks(s->srv, s->be))
693 process_srv_queue(s->srv);
694 }
695 }
696
697 /*
698 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
699 * at this point.
700 */
701
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100702 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 /* Analyse request */
704 if ((s->req->flags & BF_MASK_ANALYSER) ||
705 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
706 unsigned int flags = s->req->flags;
707
708 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200709 unsigned int last_ana = 0;
710
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 /* it's up to the analysers to reset write_ena */
712 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100713
714 /* We will call all analysers for which a bit is set in
715 * s->req->analysers, following the bit order from LSB
716 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200717 * the list when not needed. Any analyser may return 0
718 * to break out of the loop, either because of missing
719 * data to take a decision, or because it decides to
720 * kill the session. We loop at least once through each
721 * analyser, and we may loop again if other analysers
722 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100723 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200724 while (s->req->analysers & ~last_ana) {
725 last_ana = s->req->analysers;
726
727 if (s->req->analysers & AN_REQ_INSPECT) {
728 last_ana |= AN_REQ_INSPECT;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100729 if (!tcp_inspect_request(s, s->req))
730 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200731 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100732
Willy Tarreaud787e662009-07-07 10:14:51 +0200733 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
734 last_ana |= AN_REQ_WAIT_HTTP;
735 if (!http_wait_for_request(s, s->req))
736 break;
737 }
738
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200739 if (s->req->analysers & AN_REQ_HTTP_HDR) {
740 last_ana |= AN_REQ_HTTP_HDR;
Willy Tarreau59234e92008-11-30 23:51:27 +0100741 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100742 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200743 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100744
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200745 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
746 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau60b85b02008-11-30 23:28:40 +0100747 if (!http_process_tarpit(s, s->req))
748 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200749 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100750
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200751 if (s->req->analysers & AN_REQ_HTTP_BODY) {
752 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreaud34af782008-11-30 23:36:37 +0100753 if (!http_process_request_body(s, s->req))
754 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200755 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100756 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 }
Willy Tarreau84455332009-03-15 22:34:05 +0100758
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200759 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
760 rqf_last = s->req->flags;
761 goto resync_request;
762 }
763 }
764
765 resync_response:
766 /* Analyse response */
767
768 if (unlikely(s->rep->flags & BF_HIJACK)) {
769 /* In inject mode, we wake up everytime something has
770 * happened on the write side of the buffer.
771 */
772 unsigned int flags = s->rep->flags;
773
774 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
775 !(s->rep->flags & BF_FULL)) {
776 s->rep->hijacker(s, s->rep);
777 }
778
779 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
780 rpf_last = s->rep->flags;
781 goto resync_response;
782 }
783 }
784 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
785 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
786 unsigned int flags = s->rep->flags;
787
788 if (s->rep->prod->state >= SI_ST_EST) {
789 /* it's up to the analysers to reset write_ena */
790 buffer_write_ena(s->rep);
791 if (s->rep->analysers)
792 process_response(s);
793 }
794
795 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
796 rpf_last = s->rep->flags;
797 goto resync_response;
798 }
799 }
800
801 /* FIXME: here we should call protocol handlers which rely on
802 * both buffers.
803 */
804
805
806 /*
807 * Now we propagate unhandled errors to the session
808 */
809 if (!(s->flags & SN_ERR_MASK)) {
810 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
811 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100812 s->req->analysers = 0;
813 if (s->req->flags & BF_READ_ERROR)
814 s->flags |= SN_ERR_CLICL;
815 else if (s->req->flags & BF_READ_TIMEOUT)
816 s->flags |= SN_ERR_CLITO;
817 else if (s->req->flags & BF_WRITE_ERROR)
818 s->flags |= SN_ERR_SRVCL;
819 else
820 s->flags |= SN_ERR_SRVTO;
821 sess_set_term_flags(s);
822 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200823 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
824 /* Report it if the server got an error or a read timeout expired */
825 s->rep->analysers = 0;
826 if (s->rep->flags & BF_READ_ERROR)
827 s->flags |= SN_ERR_SRVCL;
828 else if (s->rep->flags & BF_READ_TIMEOUT)
829 s->flags |= SN_ERR_SRVTO;
830 else if (s->rep->flags & BF_WRITE_ERROR)
831 s->flags |= SN_ERR_CLICL;
832 else
Willy Tarreau84455332009-03-15 22:34:05 +0100833 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200834 sess_set_term_flags(s);
835 }
Willy Tarreau84455332009-03-15 22:34:05 +0100836 }
837
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200838 /*
839 * Here we take care of forwarding unhandled data. This also includes
840 * connection establishments and shutdown requests.
841 */
842
843
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100844 /* If noone is interested in analysing data, it's time to forward
845 * everything. We will wake up from time to time when either send_max
846 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100847 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100848 if (!s->req->analysers &&
849 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
850 (s->req->prod->state >= SI_ST_EST)) {
851 /* This buffer is freewheeling, there's no analyser nor hijacker
852 * attached to it. If any data are left in, we'll permit them to
853 * move.
854 */
855 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100856
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100857 /* If the producer is still connected, we'll schedule large blocks
858 * of data to be forwarded from the producer to the consumer (which
859 * might possibly not be connected yet).
860 */
861 if (!(s->req->flags & BF_SHUTR) &&
862 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100863 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100864 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100865
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100866 /* check if it is wise to enable kernel splicing to forward request data */
867 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
868 s->req->to_forward &&
869 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200870 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100871 (pipes_used < global.maxpipes) &&
872 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
873 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
874 (s->req->flags & BF_STREAMER_FAST)))) {
875 s->req->flags |= BF_KERN_SPLICING;
876 }
877
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878 /* reflect what the L7 analysers have seen last */
879 rqf_last = s->req->flags;
880
881 /*
882 * Now forward all shutdown requests between both sides of the buffer
883 */
884
885 /* first, let's check if the request buffer needs to shutdown(write) */
886 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
887 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
888 buffer_shutw_now(s->req);
889 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
890 (s->req->cons->state == SI_ST_EST) &&
891 s->be->options & PR_O_FORCE_CLO &&
892 s->rep->flags & BF_READ_ACTIVITY) {
893 /* We want to force the connection to the server to close,
894 * and the server has begun to respond. That's the right
895 * time.
896 */
897 buffer_shutw_now(s->req);
898 }
899
900 /* shutdown(write) pending */
901 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
902 s->req->cons->shutw(s->req->cons);
903
904 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100905 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
906 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 buffer_shutr_now(s->req);
908
909 /* shutdown(read) pending */
910 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
911 s->req->prod->shutr(s->req->prod);
912
Willy Tarreau92795622009-03-06 12:51:23 +0100913 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100915 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
916 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
917 s->req->cons->state = SI_ST_REQ; /* new connection requested */
918 else
919 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
920 }
921
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100922
923 /* we may have a pending connection request, or a connection waiting
924 * for completion.
925 */
926 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
927 do {
928 /* nb: step 1 might switch from QUE to ASS, but we first want
929 * to give a chance to step 2 to perform a redirect if needed.
930 */
931 if (s->si[1].state != SI_ST_REQ)
932 sess_update_stream_int(s, &s->si[1]);
933 if (s->si[1].state == SI_ST_REQ)
934 sess_prepare_conn_req(s, &s->si[1]);
935
936 if (s->si[1].state == SI_ST_ASS && s->srv &&
937 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
938 perform_http_redirect(s, &s->si[1]);
939 } while (s->si[1].state == SI_ST_ASS);
940 }
941
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200942 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100943 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100944 goto resync_stream_interface;
945
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200946 /* otherwise wewant to check if we need to resync the req buffer or not */
947 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100948 goto resync_request;
949
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200950 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +0100951
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100952 /* If noone is interested in analysing data, it's time to forward
953 * everything. We will wake up from time to time when either send_max
954 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100955 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100956 if (!s->rep->analysers &&
957 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
958 (s->rep->prod->state >= SI_ST_EST)) {
959 /* This buffer is freewheeling, there's no analyser nor hijacker
960 * attached to it. If any data are left in, we'll permit them to
961 * move.
962 */
963 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100964
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100965 /* If the producer is still connected, we'll schedule large blocks
966 * of data to be forwarded from the producer to the consumer (which
967 * might possibly not be connected yet).
968 */
969 if (!(s->rep->flags & BF_SHUTR) &&
970 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100971 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100972 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100973
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100974 /* check if it is wise to enable kernel splicing to forward response data */
975 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
976 s->rep->to_forward &&
977 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200978 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100979 (pipes_used < global.maxpipes) &&
980 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
981 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
982 (s->rep->flags & BF_STREAMER_FAST)))) {
983 s->rep->flags |= BF_KERN_SPLICING;
984 }
985
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986 /* reflect what the L7 analysers have seen last */
987 rpf_last = s->rep->flags;
988
989 /*
990 * Now forward all shutdown requests between both sides of the buffer
991 */
992
993 /*
994 * FIXME: this is probably where we should produce error responses.
995 */
996
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100997 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100998 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
999 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
1000 buffer_shutw_now(s->rep);
1001
1002 /* shutdown(write) pending */
1003 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
1004 s->rep->cons->shutw(s->rep->cons);
1005
1006 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001007 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1008 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001009 buffer_shutr_now(s->rep);
1010
1011 /* shutdown(read) pending */
1012 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1013 s->rep->prod->shutr(s->rep->prod);
1014
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001015 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001016 goto resync_stream_interface;
1017
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001018 if (s->req->flags != rqf_last)
1019 goto resync_request;
1020
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001021 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001022 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023
1024 /* This is needed only when debugging is enabled, to indicate
1025 * client-side or server-side close. Please note that in the unlikely
1026 * event where both sides would close at once, the sequence is reported
1027 * on the server side first.
1028 */
1029 if (unlikely((global.mode & MODE_DEBUG) &&
1030 (!(global.mode & MODE_QUIET) ||
1031 (global.mode & MODE_VERBOSE)))) {
1032 int len;
1033
1034 if (s->si[1].state == SI_ST_CLO &&
1035 s->si[1].prev_state == SI_ST_EST) {
1036 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1037 s->uniq_id, s->be->id,
1038 (unsigned short)s->si[0].fd,
1039 (unsigned short)s->si[1].fd);
1040 write(1, trash, len);
1041 }
1042
1043 if (s->si[0].state == SI_ST_CLO &&
1044 s->si[0].prev_state == SI_ST_EST) {
1045 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1046 s->uniq_id, s->be->id,
1047 (unsigned short)s->si[0].fd,
1048 (unsigned short)s->si[1].fd);
1049 write(1, trash, len);
1050 }
1051 }
1052
1053 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1054 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1055
1056 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1057 session_process_counters(s);
1058
1059 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001060 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001061
1062 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001063 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001064
Willy Tarreauea388542009-06-21 21:45:58 +02001065 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1066 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001067 s->si[0].prev_state = s->si[0].state;
1068 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001069 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1070 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001071
1072 /* Trick: if a request is being waiting for the server to respond,
1073 * and if we know the server can timeout, we don't want the timeout
1074 * to expire on the client side first, but we're still interested
1075 * in passing data from the client to the server (eg: POST). Thus,
1076 * we can cancel the client's request timeout if the server's
1077 * request timeout is set and the server has not yet sent a response.
1078 */
1079
1080 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001081 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1082 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001084 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001085
1086 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1087 tick_first(s->rep->rex, s->rep->wex));
1088 if (s->req->analysers)
1089 t->expire = tick_first(t->expire, s->req->analyse_exp);
1090
1091 if (s->si[0].exp)
1092 t->expire = tick_first(t->expire, s->si[0].exp);
1093
1094 if (s->si[1].exp)
1095 t->expire = tick_first(t->expire, s->si[1].exp);
1096
1097#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001098 fprintf(stderr,
1099 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1100 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1101 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1102 s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001103#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001104
1105#ifdef DEBUG_DEV
1106 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001107 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001108 ABORT_NOW();
1109#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001110 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001111 }
1112
1113 s->fe->feconn--;
1114 if (s->flags & SN_BE_ASSIGNED)
1115 s->be->beconn--;
1116 actconn--;
1117
1118 if (unlikely((global.mode & MODE_DEBUG) &&
1119 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1120 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001121 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001122 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001123 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001124 write(1, trash, len);
1125 }
1126
1127 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1128 session_process_counters(s);
1129
1130 /* let's do a final log if we need it */
1131 if (s->logs.logwait &&
1132 !(s->flags & SN_MONITOR) &&
1133 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001134 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001135 }
1136
1137 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001138 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001139 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001140 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001141 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001142}
1143
Willy Tarreau7c669d72008-06-20 15:04:11 +02001144/*
1145 * This function adjusts sess->srv_conn and maintains the previous and new
1146 * server's served session counts. Setting newsrv to NULL is enough to release
1147 * current connection slot. This function also notifies any LB algo which might
1148 * expect to be informed about any change in the number of active sessions on a
1149 * server.
1150 */
1151void sess_change_server(struct session *sess, struct server *newsrv)
1152{
1153 if (sess->srv_conn == newsrv)
1154 return;
1155
1156 if (sess->srv_conn) {
1157 sess->srv_conn->served--;
1158 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1159 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1160 sess->srv_conn = NULL;
1161 }
1162
1163 if (newsrv) {
1164 newsrv->served++;
1165 if (newsrv->proxy->lbprm.server_take_conn)
1166 newsrv->proxy->lbprm.server_take_conn(newsrv);
1167 sess->srv_conn = newsrv;
1168 }
1169}
1170
Willy Tarreau84455332009-03-15 22:34:05 +01001171/* Set correct session termination flags in case no analyser has done it. It
1172 * also counts a failed request if the server state has not reached the request
1173 * stage.
1174 */
1175void sess_set_term_flags(struct session *s)
1176{
1177 if (!(s->flags & SN_FINST_MASK)) {
1178 if (s->si[1].state < SI_ST_REQ) {
1179 s->fe->failed_req++;
1180 s->flags |= SN_FINST_R;
1181 }
1182 else if (s->si[1].state == SI_ST_QUE)
1183 s->flags |= SN_FINST_Q;
1184 else if (s->si[1].state < SI_ST_EST)
1185 s->flags |= SN_FINST_C;
1186 else if (s->si[1].state == SI_ST_EST)
1187 s->flags |= SN_FINST_D;
1188 else
1189 s->flags |= SN_FINST_L;
1190 }
1191}
1192
1193/* Handle server-side errors for default protocols. It is called whenever a a
1194 * connection setup is aborted or a request is aborted in queue. It sets the
1195 * session termination flags so that the caller does not have to worry about
1196 * them. It's installed as ->srv_error for the server-side stream_interface.
1197 */
1198void default_srv_error(struct session *s, struct stream_interface *si)
1199{
1200 int err_type = si->err_type;
1201 int err = 0, fin = 0;
1202
1203 if (err_type & SI_ET_QUEUE_ABRT) {
1204 err = SN_ERR_CLICL;
1205 fin = SN_FINST_Q;
1206 }
1207 else if (err_type & SI_ET_CONN_ABRT) {
1208 err = SN_ERR_CLICL;
1209 fin = SN_FINST_C;
1210 }
1211 else if (err_type & SI_ET_QUEUE_TO) {
1212 err = SN_ERR_SRVTO;
1213 fin = SN_FINST_Q;
1214 }
1215 else if (err_type & SI_ET_QUEUE_ERR) {
1216 err = SN_ERR_SRVCL;
1217 fin = SN_FINST_Q;
1218 }
1219 else if (err_type & SI_ET_CONN_TO) {
1220 err = SN_ERR_SRVTO;
1221 fin = SN_FINST_C;
1222 }
1223 else if (err_type & SI_ET_CONN_ERR) {
1224 err = SN_ERR_SRVCL;
1225 fin = SN_FINST_C;
1226 }
1227 else /* SI_ET_CONN_OTHER and others */ {
1228 err = SN_ERR_INTERNAL;
1229 fin = SN_FINST_C;
1230 }
1231
1232 if (!(s->flags & SN_ERR_MASK))
1233 s->flags |= err;
1234 if (!(s->flags & SN_FINST_MASK))
1235 s->flags |= fin;
1236}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001237
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238/*
1239 * Local variables:
1240 * c-indent-level: 8
1241 * c-basic-offset: 8
1242 * End:
1243 */