blob: d3f3710c90aeac8183a849996ba4e82c814966b3 [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 Tarreau1a52dbd2009-06-28 19:37:53 +0200733 if (s->req->analysers & AN_REQ_HTTP_HDR) {
734 last_ana |= AN_REQ_HTTP_HDR;
Willy Tarreau59234e92008-11-30 23:51:27 +0100735 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100736 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200737 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100738
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200739 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
740 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau60b85b02008-11-30 23:28:40 +0100741 if (!http_process_tarpit(s, s->req))
742 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200743 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100744
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200745 if (s->req->analysers & AN_REQ_HTTP_BODY) {
746 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreaud34af782008-11-30 23:36:37 +0100747 if (!http_process_request_body(s, s->req))
748 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200749 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100750 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 }
Willy Tarreau84455332009-03-15 22:34:05 +0100752
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200753 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
754 rqf_last = s->req->flags;
755 goto resync_request;
756 }
757 }
758
759 resync_response:
760 /* Analyse response */
761
762 if (unlikely(s->rep->flags & BF_HIJACK)) {
763 /* In inject mode, we wake up everytime something has
764 * happened on the write side of the buffer.
765 */
766 unsigned int flags = s->rep->flags;
767
768 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
769 !(s->rep->flags & BF_FULL)) {
770 s->rep->hijacker(s, s->rep);
771 }
772
773 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
774 rpf_last = s->rep->flags;
775 goto resync_response;
776 }
777 }
778 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
779 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
780 unsigned int flags = s->rep->flags;
781
782 if (s->rep->prod->state >= SI_ST_EST) {
783 /* it's up to the analysers to reset write_ena */
784 buffer_write_ena(s->rep);
785 if (s->rep->analysers)
786 process_response(s);
787 }
788
789 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
790 rpf_last = s->rep->flags;
791 goto resync_response;
792 }
793 }
794
795 /* FIXME: here we should call protocol handlers which rely on
796 * both buffers.
797 */
798
799
800 /*
801 * Now we propagate unhandled errors to the session
802 */
803 if (!(s->flags & SN_ERR_MASK)) {
804 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
805 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100806 s->req->analysers = 0;
807 if (s->req->flags & BF_READ_ERROR)
808 s->flags |= SN_ERR_CLICL;
809 else if (s->req->flags & BF_READ_TIMEOUT)
810 s->flags |= SN_ERR_CLITO;
811 else if (s->req->flags & BF_WRITE_ERROR)
812 s->flags |= SN_ERR_SRVCL;
813 else
814 s->flags |= SN_ERR_SRVTO;
815 sess_set_term_flags(s);
816 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200817 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
818 /* Report it if the server got an error or a read timeout expired */
819 s->rep->analysers = 0;
820 if (s->rep->flags & BF_READ_ERROR)
821 s->flags |= SN_ERR_SRVCL;
822 else if (s->rep->flags & BF_READ_TIMEOUT)
823 s->flags |= SN_ERR_SRVTO;
824 else if (s->rep->flags & BF_WRITE_ERROR)
825 s->flags |= SN_ERR_CLICL;
826 else
Willy Tarreau84455332009-03-15 22:34:05 +0100827 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200828 sess_set_term_flags(s);
829 }
Willy Tarreau84455332009-03-15 22:34:05 +0100830 }
831
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200832 /*
833 * Here we take care of forwarding unhandled data. This also includes
834 * connection establishments and shutdown requests.
835 */
836
837
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100838 /* If noone is interested in analysing data, it's time to forward
839 * everything. We will wake up from time to time when either send_max
840 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100841 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100842 if (!s->req->analysers &&
843 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
844 (s->req->prod->state >= SI_ST_EST)) {
845 /* This buffer is freewheeling, there's no analyser nor hijacker
846 * attached to it. If any data are left in, we'll permit them to
847 * move.
848 */
849 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100850
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100851 /* If the producer is still connected, we'll schedule large blocks
852 * of data to be forwarded from the producer to the consumer (which
853 * might possibly not be connected yet).
854 */
855 if (!(s->req->flags & BF_SHUTR) &&
856 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100857 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100858 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100859
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100860 /* check if it is wise to enable kernel splicing to forward request data */
861 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
862 s->req->to_forward &&
863 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200864 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100865 (pipes_used < global.maxpipes) &&
866 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
867 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
868 (s->req->flags & BF_STREAMER_FAST)))) {
869 s->req->flags |= BF_KERN_SPLICING;
870 }
871
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 /* reflect what the L7 analysers have seen last */
873 rqf_last = s->req->flags;
874
875 /*
876 * Now forward all shutdown requests between both sides of the buffer
877 */
878
879 /* first, let's check if the request buffer needs to shutdown(write) */
880 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
881 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
882 buffer_shutw_now(s->req);
883 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
884 (s->req->cons->state == SI_ST_EST) &&
885 s->be->options & PR_O_FORCE_CLO &&
886 s->rep->flags & BF_READ_ACTIVITY) {
887 /* We want to force the connection to the server to close,
888 * and the server has begun to respond. That's the right
889 * time.
890 */
891 buffer_shutw_now(s->req);
892 }
893
894 /* shutdown(write) pending */
895 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
896 s->req->cons->shutw(s->req->cons);
897
898 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100899 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
900 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100901 buffer_shutr_now(s->req);
902
903 /* shutdown(read) pending */
904 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
905 s->req->prod->shutr(s->req->prod);
906
Willy Tarreau92795622009-03-06 12:51:23 +0100907 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100909 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
910 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
911 s->req->cons->state = SI_ST_REQ; /* new connection requested */
912 else
913 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
914 }
915
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916
917 /* we may have a pending connection request, or a connection waiting
918 * for completion.
919 */
920 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
921 do {
922 /* nb: step 1 might switch from QUE to ASS, but we first want
923 * to give a chance to step 2 to perform a redirect if needed.
924 */
925 if (s->si[1].state != SI_ST_REQ)
926 sess_update_stream_int(s, &s->si[1]);
927 if (s->si[1].state == SI_ST_REQ)
928 sess_prepare_conn_req(s, &s->si[1]);
929
930 if (s->si[1].state == SI_ST_ASS && s->srv &&
931 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
932 perform_http_redirect(s, &s->si[1]);
933 } while (s->si[1].state == SI_ST_ASS);
934 }
935
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200936 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100937 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938 goto resync_stream_interface;
939
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200940 /* otherwise wewant to check if we need to resync the req buffer or not */
941 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100942 goto resync_request;
943
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200944 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +0100945
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100946 /* If noone is interested in analysing data, it's time to forward
947 * everything. We will wake up from time to time when either send_max
948 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100949 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100950 if (!s->rep->analysers &&
951 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
952 (s->rep->prod->state >= SI_ST_EST)) {
953 /* This buffer is freewheeling, there's no analyser nor hijacker
954 * attached to it. If any data are left in, we'll permit them to
955 * move.
956 */
957 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100958
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100959 /* If the producer is still connected, we'll schedule large blocks
960 * of data to be forwarded from the producer to the consumer (which
961 * might possibly not be connected yet).
962 */
963 if (!(s->rep->flags & BF_SHUTR) &&
964 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100965 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100966 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100967
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100968 /* check if it is wise to enable kernel splicing to forward response data */
969 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
970 s->rep->to_forward &&
971 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +0200972 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100973 (pipes_used < global.maxpipes) &&
974 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
975 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
976 (s->rep->flags & BF_STREAMER_FAST)))) {
977 s->rep->flags |= BF_KERN_SPLICING;
978 }
979
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100980 /* reflect what the L7 analysers have seen last */
981 rpf_last = s->rep->flags;
982
983 /*
984 * Now forward all shutdown requests between both sides of the buffer
985 */
986
987 /*
988 * FIXME: this is probably where we should produce error responses.
989 */
990
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100991 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100992 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
993 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
994 buffer_shutw_now(s->rep);
995
996 /* shutdown(write) pending */
997 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
998 s->rep->cons->shutw(s->rep->cons);
999
1000 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001001 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1002 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001003 buffer_shutr_now(s->rep);
1004
1005 /* shutdown(read) pending */
1006 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1007 s->rep->prod->shutr(s->rep->prod);
1008
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001009 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010 goto resync_stream_interface;
1011
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001012 if (s->req->flags != rqf_last)
1013 goto resync_request;
1014
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001015 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001016 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017
1018 /* This is needed only when debugging is enabled, to indicate
1019 * client-side or server-side close. Please note that in the unlikely
1020 * event where both sides would close at once, the sequence is reported
1021 * on the server side first.
1022 */
1023 if (unlikely((global.mode & MODE_DEBUG) &&
1024 (!(global.mode & MODE_QUIET) ||
1025 (global.mode & MODE_VERBOSE)))) {
1026 int len;
1027
1028 if (s->si[1].state == SI_ST_CLO &&
1029 s->si[1].prev_state == SI_ST_EST) {
1030 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1031 s->uniq_id, s->be->id,
1032 (unsigned short)s->si[0].fd,
1033 (unsigned short)s->si[1].fd);
1034 write(1, trash, len);
1035 }
1036
1037 if (s->si[0].state == SI_ST_CLO &&
1038 s->si[0].prev_state == SI_ST_EST) {
1039 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1040 s->uniq_id, s->be->id,
1041 (unsigned short)s->si[0].fd,
1042 (unsigned short)s->si[1].fd);
1043 write(1, trash, len);
1044 }
1045 }
1046
1047 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1048 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1049
1050 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1051 session_process_counters(s);
1052
1053 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001054 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055
1056 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001057 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058
Willy Tarreauea388542009-06-21 21:45:58 +02001059 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1060 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001061 s->si[0].prev_state = s->si[0].state;
1062 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001063 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1064 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001065
1066 /* Trick: if a request is being waiting for the server to respond,
1067 * and if we know the server can timeout, we don't want the timeout
1068 * to expire on the client side first, but we're still interested
1069 * in passing data from the client to the server (eg: POST). Thus,
1070 * we can cancel the client's request timeout if the server's
1071 * request timeout is set and the server has not yet sent a response.
1072 */
1073
1074 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001075 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1076 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001077 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001078 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001079
1080 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1081 tick_first(s->rep->rex, s->rep->wex));
1082 if (s->req->analysers)
1083 t->expire = tick_first(t->expire, s->req->analyse_exp);
1084
1085 if (s->si[0].exp)
1086 t->expire = tick_first(t->expire, s->si[0].exp);
1087
1088 if (s->si[1].exp)
1089 t->expire = tick_first(t->expire, s->si[1].exp);
1090
1091#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001092 fprintf(stderr,
1093 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1094 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1095 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1096 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 +01001097#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001098
1099#ifdef DEBUG_DEV
1100 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001101 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001102 ABORT_NOW();
1103#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001104 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001105 }
1106
1107 s->fe->feconn--;
1108 if (s->flags & SN_BE_ASSIGNED)
1109 s->be->beconn--;
1110 actconn--;
1111
1112 if (unlikely((global.mode & MODE_DEBUG) &&
1113 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1114 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001115 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001116 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001117 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001118 write(1, trash, len);
1119 }
1120
1121 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1122 session_process_counters(s);
1123
1124 /* let's do a final log if we need it */
1125 if (s->logs.logwait &&
1126 !(s->flags & SN_MONITOR) &&
1127 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001128 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129 }
1130
1131 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001132 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001133 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001135 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001136}
1137
Willy Tarreau7c669d72008-06-20 15:04:11 +02001138/*
1139 * This function adjusts sess->srv_conn and maintains the previous and new
1140 * server's served session counts. Setting newsrv to NULL is enough to release
1141 * current connection slot. This function also notifies any LB algo which might
1142 * expect to be informed about any change in the number of active sessions on a
1143 * server.
1144 */
1145void sess_change_server(struct session *sess, struct server *newsrv)
1146{
1147 if (sess->srv_conn == newsrv)
1148 return;
1149
1150 if (sess->srv_conn) {
1151 sess->srv_conn->served--;
1152 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1153 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1154 sess->srv_conn = NULL;
1155 }
1156
1157 if (newsrv) {
1158 newsrv->served++;
1159 if (newsrv->proxy->lbprm.server_take_conn)
1160 newsrv->proxy->lbprm.server_take_conn(newsrv);
1161 sess->srv_conn = newsrv;
1162 }
1163}
1164
Willy Tarreau84455332009-03-15 22:34:05 +01001165/* Set correct session termination flags in case no analyser has done it. It
1166 * also counts a failed request if the server state has not reached the request
1167 * stage.
1168 */
1169void sess_set_term_flags(struct session *s)
1170{
1171 if (!(s->flags & SN_FINST_MASK)) {
1172 if (s->si[1].state < SI_ST_REQ) {
1173 s->fe->failed_req++;
1174 s->flags |= SN_FINST_R;
1175 }
1176 else if (s->si[1].state == SI_ST_QUE)
1177 s->flags |= SN_FINST_Q;
1178 else if (s->si[1].state < SI_ST_EST)
1179 s->flags |= SN_FINST_C;
1180 else if (s->si[1].state == SI_ST_EST)
1181 s->flags |= SN_FINST_D;
1182 else
1183 s->flags |= SN_FINST_L;
1184 }
1185}
1186
1187/* Handle server-side errors for default protocols. It is called whenever a a
1188 * connection setup is aborted or a request is aborted in queue. It sets the
1189 * session termination flags so that the caller does not have to worry about
1190 * them. It's installed as ->srv_error for the server-side stream_interface.
1191 */
1192void default_srv_error(struct session *s, struct stream_interface *si)
1193{
1194 int err_type = si->err_type;
1195 int err = 0, fin = 0;
1196
1197 if (err_type & SI_ET_QUEUE_ABRT) {
1198 err = SN_ERR_CLICL;
1199 fin = SN_FINST_Q;
1200 }
1201 else if (err_type & SI_ET_CONN_ABRT) {
1202 err = SN_ERR_CLICL;
1203 fin = SN_FINST_C;
1204 }
1205 else if (err_type & SI_ET_QUEUE_TO) {
1206 err = SN_ERR_SRVTO;
1207 fin = SN_FINST_Q;
1208 }
1209 else if (err_type & SI_ET_QUEUE_ERR) {
1210 err = SN_ERR_SRVCL;
1211 fin = SN_FINST_Q;
1212 }
1213 else if (err_type & SI_ET_CONN_TO) {
1214 err = SN_ERR_SRVTO;
1215 fin = SN_FINST_C;
1216 }
1217 else if (err_type & SI_ET_CONN_ERR) {
1218 err = SN_ERR_SRVCL;
1219 fin = SN_FINST_C;
1220 }
1221 else /* SI_ET_CONN_OTHER and others */ {
1222 err = SN_ERR_INTERNAL;
1223 fin = SN_FINST_C;
1224 }
1225
1226 if (!(s->flags & SN_ERR_MASK))
1227 s->flags |= err;
1228 if (!(s->flags & SN_FINST_MASK))
1229 s->flags |= fin;
1230}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001231
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232/*
1233 * Local variables:
1234 * c-indent-level: 8
1235 * c-basic-offset: 8
1236 * End:
1237 */