blob: 71a4cb02ace366aa999592639716b20be7aa6b9e [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;
187 fd_delete(si->fd);
188
189 if (si->err_type)
190 return 0;
191
192 si->err_loc = s->srv;
193 if (si->flags & SI_FL_ERR)
194 si->err_type = SI_ET_CONN_ERR;
195 else
196 si->err_type = SI_ET_CONN_TO;
197 return 0;
198 }
199
200 /* OK, maybe we want to abort */
201 if (unlikely((req->flags & BF_SHUTW_NOW) ||
202 (rep->flags & BF_SHUTW) ||
203 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100204 (((req->flags & (BF_EMPTY|BF_WRITE_ACTIVITY)) == BF_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100205 s->be->options & PR_O_ABRT_CLOSE)))) {
206 /* give up */
207 si->shutw(si);
208 si->err_type |= SI_ET_CONN_ABRT;
209 si->err_loc = s->srv;
Willy Tarreau84455332009-03-15 22:34:05 +0100210 if (s->srv_error)
211 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100212 return 1;
213 }
214
215 /* we need to wait a bit more if there was no activity either */
216 if (!(req->flags & BF_WRITE_ACTIVITY))
217 return 1;
218
219 /* OK, this means that a connection succeeded. The caller will be
220 * responsible for handling the transition from CON to EST.
221 */
222 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100223 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100224 si->state = SI_ST_EST;
225 si->err_type = SI_ET_NONE;
226 si->err_loc = NULL;
227 return 1;
228}
229
230/* This function is called with (si->state == SI_ST_CER) meaning that a
231 * previous connection attempt has failed and that the file descriptor
232 * has already been released. Possible causes include asynchronous error
233 * notification and time out. Possible output states are SI_ST_CLO when
234 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
235 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
236 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
237 * marked as in error state. It returns 0.
238 */
239int sess_update_st_cer(struct session *s, struct stream_interface *si)
240{
241 /* we probably have to release last session from the server */
242 if (s->srv) {
243 if (s->flags & SN_CURR_SESS) {
244 s->flags &= ~SN_CURR_SESS;
245 s->srv->cur_sess--;
246 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100247 }
248
249 /* ensure that we have enough retries left */
250 s->conn_retries--;
251 if (s->conn_retries < 0) {
252 if (!si->err_type) {
253 si->err_type = SI_ET_CONN_ERR;
254 si->err_loc = s->srv;
255 }
256
257 if (s->srv)
258 s->srv->failed_conns++;
259 s->be->failed_conns++;
260 if (may_dequeue_tasks(s->srv, s->be))
261 process_srv_queue(s->srv);
262
263 /* shutw is enough so stop a connecting socket */
264 si->shutw(si);
265 si->ob->flags |= BF_WRITE_ERROR;
266 si->ib->flags |= BF_READ_ERROR;
267
268 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100269 if (s->srv_error)
270 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100271 return 0;
272 }
273
274 /* If the "redispatch" option is set on the backend, we are allowed to
275 * retry on another server for the last retry. In order to achieve this,
276 * we must mark the session unassigned, and eventually clear the DIRECT
277 * bit to ignore any persistence cookie. We won't count a retry nor a
278 * redispatch yet, because this will depend on what server is selected.
279 */
280 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
281 if (may_dequeue_tasks(s->srv, s->be))
282 process_srv_queue(s->srv);
283
284 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
285 s->prev_srv = s->srv;
286 si->state = SI_ST_REQ;
287 } else {
288 if (s->srv)
289 s->srv->retries++;
290 s->be->retries++;
291 si->state = SI_ST_ASS;
292 }
293
294 if (si->flags & SI_FL_ERR) {
295 /* The error was an asynchronous connection error, and we will
296 * likely have to retry connecting to the same server, most
297 * likely leading to the same result. To avoid this, we wait
298 * one second before retrying.
299 */
300
301 if (!si->err_type)
302 si->err_type = SI_ET_CONN_ERR;
303
304 si->state = SI_ST_TAR;
305 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
306 return 0;
307 }
308 return 0;
309}
310
311/*
312 * This function handles the transition between the SI_ST_CON state and the
313 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
314 * SI_ST_EST.
315 */
316void sess_establish(struct session *s, struct stream_interface *si)
317{
318 struct buffer *req = si->ob;
319 struct buffer *rep = si->ib;
320
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100321 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100322 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
323
324 /* if the user wants to log as soon as possible, without counting
325 * bytes from the server, then this is the right moment. */
326 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
327 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100328 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100329 }
330#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100331 if ((global.tune.options & GTUNE_USE_SPLICE) &&
332 (s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100333 /* TCP splicing supported by both FE and BE */
334 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
335 }
336#endif
337 }
338 else {
339 rep->analysers |= AN_RTR_HTTP_HDR;
340 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
341 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
342 /* reset hdr_idx which was already initialized by the request.
343 * right now, the http parser does it.
344 * hdr_idx_init(&s->txn.hdr_idx);
345 */
346 }
347
348 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
349 req->wex = TICK_ETERNITY;
350}
351
352/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
353 * Other input states are simply ignored.
354 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
355 * Flags must have previously been updated for timeouts and other conditions.
356 */
357void sess_update_stream_int(struct session *s, struct stream_interface *si)
358{
359 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",
360 now_ms, __FUNCTION__,
361 s,
362 s->req, s->rep,
363 s->req->rex, s->rep->wex,
364 s->req->flags, s->rep->flags,
365 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
366
367 if (si->state == SI_ST_ASS) {
368 /* Server assigned to connection request, we have to try to connect now */
369 int conn_err;
370
371 conn_err = connect_server(s);
372 if (conn_err == SN_ERR_NONE) {
373 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100374 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100375 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100376 return;
377 }
378
379 /* We have received a synchronous error. We might have to
380 * abort, retry immediately or redispatch.
381 */
382 if (conn_err == SN_ERR_INTERNAL) {
383 if (!si->err_type) {
384 si->err_type = SI_ET_CONN_OTHER;
385 si->err_loc = s->srv;
386 }
387
388 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100389 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100390 if (s->srv)
391 s->srv->failed_conns++;
392 s->be->failed_conns++;
393
394 /* release other sessions waiting for this server */
395 if (may_dequeue_tasks(s->srv, s->be))
396 process_srv_queue(s->srv);
397
398 /* Failed and not retryable. */
399 si->shutr(si);
400 si->shutw(si);
401 si->ob->flags |= BF_WRITE_ERROR;
402
403 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
404
405 /* no session was ever accounted for this server */
406 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100407 if (s->srv_error)
408 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100409 return;
410 }
411
412 /* We are facing a retryable error, but we don't want to run a
413 * turn-around now, as the problem is likely a source port
414 * allocation problem, so we want to retry now.
415 */
416 si->state = SI_ST_CER;
417 si->flags &= ~SI_FL_ERR;
418 sess_update_st_cer(s, si);
419 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
420 return;
421 }
422 else if (si->state == SI_ST_QUE) {
423 /* connection request was queued, check for any update */
424 if (!s->pend_pos) {
425 /* The connection is not in the queue anymore. Either
426 * we have a server connection slot available and we
427 * go directly to the assigned state, or we need to
428 * load-balance first and go to the INI state.
429 */
430 si->exp = TICK_ETERNITY;
431 if (unlikely(!(s->flags & SN_ASSIGNED)))
432 si->state = SI_ST_REQ;
433 else {
434 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
435 si->state = SI_ST_ASS;
436 }
437 return;
438 }
439
440 /* Connection request still in queue... */
441 if (si->flags & SI_FL_EXP) {
442 /* ... and timeout expired */
443 si->exp = TICK_ETERNITY;
444 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
445 if (s->srv)
446 s->srv->failed_conns++;
447 s->be->failed_conns++;
448 si->shutr(si);
449 si->shutw(si);
450 si->ob->flags |= BF_WRITE_TIMEOUT;
451 if (!si->err_type)
452 si->err_type = SI_ET_QUEUE_TO;
453 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100454 if (s->srv_error)
455 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100456 return;
457 }
458
459 /* Connection remains in queue, check if we have to abort it */
460 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
461 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
462 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
463 /* give up */
464 si->exp = TICK_ETERNITY;
465 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
466 si->shutr(si);
467 si->shutw(si);
468 si->err_type |= SI_ET_QUEUE_ABRT;
469 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100470 if (s->srv_error)
471 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100472 return;
473 }
474
475 /* Nothing changed */
476 return;
477 }
478 else if (si->state == SI_ST_TAR) {
479 /* Connection request might be aborted */
480 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
481 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
482 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
483 /* give up */
484 si->exp = TICK_ETERNITY;
485 si->shutr(si);
486 si->shutw(si);
487 si->err_type |= SI_ET_CONN_ABRT;
488 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100489 if (s->srv_error)
490 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100491 return;
492 }
493
494 if (!(si->flags & SI_FL_EXP))
495 return; /* still in turn-around */
496
497 si->exp = TICK_ETERNITY;
498
499 /* we keep trying on the same server as long as the session is
500 * marked "assigned".
501 * FIXME: Should we force a redispatch attempt when the server is down ?
502 */
503 if (s->flags & SN_ASSIGNED)
504 si->state = SI_ST_ASS;
505 else
506 si->state = SI_ST_REQ;
507 return;
508 }
509}
510
511/* This function initiates a server connection request on a stream interface
512 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
513 * indicating that a server has been assigned. It may also return SI_ST_QUE,
514 * or SI_ST_CLO upon error.
515 */
516static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
517 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",
518 now_ms, __FUNCTION__,
519 s,
520 s->req, s->rep,
521 s->req->rex, s->rep->wex,
522 s->req->flags, s->rep->flags,
523 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
524
525 if (si->state != SI_ST_REQ)
526 return;
527
528 /* Try to assign a server */
529 if (srv_redispatch_connect(s) != 0) {
530 /* We did not get a server. Either we queued the
531 * connection request, or we encountered an error.
532 */
533 if (si->state == SI_ST_QUE)
534 return;
535
536 /* we did not get any server, let's check the cause */
537 si->shutr(si);
538 si->shutw(si);
539 si->ob->flags |= BF_WRITE_ERROR;
540 if (!si->err_type)
541 si->err_type = SI_ET_CONN_OTHER;
542 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100543 if (s->srv_error)
544 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100545 return;
546 }
547
548 /* The server is assigned */
549 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
550 si->state = SI_ST_ASS;
551}
552
553/* Processes the client, server, request and response jobs of a session task,
554 * then puts it back to the wait queue in a clean state, or cleans up its
555 * resources if it must be deleted. Returns in <next> the date the task wants
556 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
557 * nothing too many times, the request and response buffers flags are monitored
558 * and each function is called only if at least another function has changed at
559 * least one flag it is interested in.
560 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100561struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100562{
563 struct session *s = t->context;
564 int resync;
565 unsigned int rqf_last, rpf_last;
566
567 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
568 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
569
570 /* 1a: Check for low level timeouts if needed. We just set a flag on
571 * stream interfaces when their timeouts have expired.
572 */
573 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
574 stream_int_check_timeouts(&s->si[0]);
575 stream_int_check_timeouts(&s->si[1]);
576 buffer_check_timeouts(s->req);
577 buffer_check_timeouts(s->rep);
578 }
579
Willy Tarreau86491c32008-12-14 09:04:47 +0100580 s->req->flags &= ~BF_READ_NOEXP;
581
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100582 /* copy req/rep flags so that we can detect shutdowns */
583 rqf_last = s->req->flags;
584 rpf_last = s->rep->flags;
585
586 /* 1b: check for low-level errors reported at the stream interface.
587 * First we check if it's a retryable error (in which case we don't
588 * want to tell the buffer). Otherwise we report the error one level
589 * upper by setting flags into the buffers. Note that the side towards
590 * the client cannot have connect (hence retryable) errors. Also, the
591 * connection setup code must be able to deal with any type of abort.
592 */
593 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
594 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
595 s->si[0].shutr(&s->si[0]);
596 s->si[0].shutw(&s->si[0]);
597 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100598 if (!(s->req->analysers) && !(s->rep->analysers)) {
599 if (!(s->flags & SN_ERR_MASK))
600 s->flags |= SN_ERR_CLICL;
601 if (!(s->flags & SN_FINST_MASK))
602 s->flags |= SN_FINST_D;
603 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 }
605 }
606
607 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
608 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
609 s->si[1].shutr(&s->si[1]);
610 s->si[1].shutw(&s->si[1]);
611 stream_int_report_error(&s->si[1]);
612 s->be->failed_resp++;
613 if (s->srv)
614 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100615 if (!(s->req->analysers) && !(s->rep->analysers)) {
616 if (!(s->flags & SN_ERR_MASK))
617 s->flags |= SN_ERR_SRVCL;
618 if (!(s->flags & SN_FINST_MASK))
619 s->flags |= SN_FINST_D;
620 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 }
622 /* note: maybe we should process connection errors here ? */
623 }
624
625 if (s->si[1].state == SI_ST_CON) {
626 /* we were trying to establish a connection on the server side,
627 * maybe it succeeded, maybe it failed, maybe we timed out, ...
628 */
629 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
630 sess_update_st_cer(s, &s->si[1]);
631 else if (s->si[1].state == SI_ST_EST)
632 sess_establish(s, &s->si[1]);
633
634 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
635 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
636 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
637 */
638 }
639
640 /* check buffer timeouts, and close the corresponding stream interfaces
641 * for future reads or writes. Note: this will also concern upper layers
642 * but we do not touch any other flag. We must be careful and correctly
643 * detect state changes when calling them.
644 */
645 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
646 if (s->req->flags & BF_READ_TIMEOUT)
647 s->req->prod->shutr(s->req->prod);
648 if (s->req->flags & BF_WRITE_TIMEOUT)
649 s->req->cons->shutw(s->req->cons);
650 DPRINTF(stderr,
651 "[%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",
652 now_ms, __FUNCTION__, __LINE__,
653 t,
654 s, s->flags,
655 s->req, s->rep,
656 s->req->rex, s->rep->wex,
657 s->req->flags, s->rep->flags,
658 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
659 s->rep->cons->err_type, s->req->cons->err_type,
660 s->conn_retries);
661 }
662
663 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
664 if (s->rep->flags & BF_READ_TIMEOUT)
665 s->rep->prod->shutr(s->rep->prod);
666 if (s->rep->flags & BF_WRITE_TIMEOUT)
667 s->rep->cons->shutw(s->rep->cons);
668 DPRINTF(stderr,
669 "[%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",
670 now_ms, __FUNCTION__, __LINE__,
671 t,
672 s, s->flags,
673 s->req, s->rep,
674 s->req->rex, s->rep->wex,
675 s->req->flags, s->rep->flags,
676 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
677 s->rep->cons->err_type, s->req->cons->err_type,
678 s->conn_retries);
679 }
680
681 /* Check for connection closure */
682
683resync_stream_interface:
684 DPRINTF(stderr,
685 "[%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",
686 now_ms, __FUNCTION__, __LINE__,
687 t,
688 s, s->flags,
689 s->req, s->rep,
690 s->req->rex, s->rep->wex,
691 s->req->flags, s->rep->flags,
692 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
693 s->rep->cons->err_type, s->req->cons->err_type,
694 s->conn_retries);
695
696 /* nothing special to be done on client side */
697 if (unlikely(s->req->prod->state == SI_ST_DIS))
698 s->req->prod->state = SI_ST_CLO;
699
700 /* When a server-side connection is released, we have to count it and
701 * check for pending connections on this server.
702 */
703 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
704 s->req->cons->state = SI_ST_CLO;
705 if (s->srv) {
706 if (s->flags & SN_CURR_SESS) {
707 s->flags &= ~SN_CURR_SESS;
708 s->srv->cur_sess--;
709 }
710 sess_change_server(s, NULL);
711 if (may_dequeue_tasks(s->srv, s->be))
712 process_srv_queue(s->srv);
713 }
714 }
715
716 /*
717 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
718 * at this point.
719 */
720
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100721 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 /**** Process layer 7 below ****/
723
724 resync = 0;
725
726 /* Analyse request */
727 if ((s->req->flags & BF_MASK_ANALYSER) ||
728 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
729 unsigned int flags = s->req->flags;
730
731 if (s->req->prod->state >= SI_ST_EST) {
732 /* it's up to the analysers to reset write_ena */
733 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100734
735 /* We will call all analysers for which a bit is set in
736 * s->req->analysers, following the bit order from LSB
737 * to MSB. The analysers must remove themselves from
738 * the list when not needed. This while() loop is in
739 * fact a cleaner if().
740 */
741 while (s->req->analysers) {
742 if (s->req->analysers & AN_REQ_INSPECT)
743 if (!tcp_inspect_request(s, s->req))
744 break;
745
Willy Tarreau59234e92008-11-30 23:51:27 +0100746 if (s->req->analysers & AN_REQ_HTTP_HDR)
747 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100748 break;
749
Willy Tarreau60b85b02008-11-30 23:28:40 +0100750 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
751 if (!http_process_tarpit(s, s->req))
752 break;
753
Willy Tarreaud34af782008-11-30 23:36:37 +0100754 if (s->req->analysers & AN_REQ_HTTP_BODY)
755 if (!http_process_request_body(s, s->req))
756 break;
757
Willy Tarreauedcf6682008-11-30 23:15:34 +0100758 /* Just make sure that nobody set a wrong flag causing an endless loop */
759 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
760
761 /* we don't want to loop anyway */
762 break;
763 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764 }
Willy Tarreau84455332009-03-15 22:34:05 +0100765
766 /* Report it if the client got an error or a read timeout expired */
767 if (unlikely(s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
768 !(s->flags & SN_ERR_MASK)) {
769 s->req->analysers = 0;
770 if (s->req->flags & BF_READ_ERROR)
771 s->flags |= SN_ERR_CLICL;
772 else if (s->req->flags & BF_READ_TIMEOUT)
773 s->flags |= SN_ERR_CLITO;
774 else if (s->req->flags & BF_WRITE_ERROR)
775 s->flags |= SN_ERR_SRVCL;
776 else
777 s->flags |= SN_ERR_SRVTO;
778 sess_set_term_flags(s);
779 }
780
Willy Tarreau06bea942009-03-21 22:09:29 +0100781 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 resync = 1;
783 }
784
Willy Tarreau84455332009-03-15 22:34:05 +0100785 /* Report it if the client got an error or a read timeout expired */
786 if (unlikely(s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
787 !(s->flags & SN_ERR_MASK)) {
788 if (s->req->flags & BF_READ_ERROR)
789 s->flags |= SN_ERR_CLICL;
790 else if (s->req->flags & BF_READ_TIMEOUT)
791 s->flags |= SN_ERR_CLITO;
792 else if (s->req->flags & BF_WRITE_ERROR)
793 s->flags |= SN_ERR_SRVCL;
794 else
795 s->flags |= SN_ERR_SRVTO;
796 sess_set_term_flags(s);
797 }
798
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100799 /* If noone is interested in analysing data, it's time to forward
800 * everything. We will wake up from time to time when either send_max
801 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100802 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100803 if (!s->req->analysers &&
804 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
805 (s->req->prod->state >= SI_ST_EST)) {
806 /* This buffer is freewheeling, there's no analyser nor hijacker
807 * attached to it. If any data are left in, we'll permit them to
808 * move.
809 */
810 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100811
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100812 /* If the producer is still connected, we'll schedule large blocks
813 * of data to be forwarded from the producer to the consumer (which
814 * might possibly not be connected yet).
815 */
816 if (!(s->req->flags & BF_SHUTR) &&
817 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100818 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100819 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100820
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100821 /* check if it is wise to enable kernel splicing to forward request data */
822 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
823 s->req->to_forward &&
824 (global.tune.options & GTUNE_USE_SPLICE) &&
825 (pipes_used < global.maxpipes) &&
826 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
827 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
828 (s->req->flags & BF_STREAMER_FAST)))) {
829 s->req->flags |= BF_KERN_SPLICING;
830 }
831
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 /* reflect what the L7 analysers have seen last */
833 rqf_last = s->req->flags;
834
835 /*
836 * Now forward all shutdown requests between both sides of the buffer
837 */
838
839 /* first, let's check if the request buffer needs to shutdown(write) */
840 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
841 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
842 buffer_shutw_now(s->req);
843 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
844 (s->req->cons->state == SI_ST_EST) &&
845 s->be->options & PR_O_FORCE_CLO &&
846 s->rep->flags & BF_READ_ACTIVITY) {
847 /* We want to force the connection to the server to close,
848 * and the server has begun to respond. That's the right
849 * time.
850 */
851 buffer_shutw_now(s->req);
852 }
853
854 /* shutdown(write) pending */
855 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
856 s->req->cons->shutw(s->req->cons);
857
858 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100859 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
860 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100861 buffer_shutr_now(s->req);
862
863 /* shutdown(read) pending */
864 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
865 s->req->prod->shutr(s->req->prod);
866
Willy Tarreau92795622009-03-06 12:51:23 +0100867 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100869 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
870 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
871 s->req->cons->state = SI_ST_REQ; /* new connection requested */
872 else
873 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
874 }
875
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100876
877 /* we may have a pending connection request, or a connection waiting
878 * for completion.
879 */
880 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
881 do {
882 /* nb: step 1 might switch from QUE to ASS, but we first want
883 * to give a chance to step 2 to perform a redirect if needed.
884 */
885 if (s->si[1].state != SI_ST_REQ)
886 sess_update_stream_int(s, &s->si[1]);
887 if (s->si[1].state == SI_ST_REQ)
888 sess_prepare_conn_req(s, &s->si[1]);
889
890 if (s->si[1].state == SI_ST_ASS && s->srv &&
891 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
892 perform_http_redirect(s, &s->si[1]);
893 } while (s->si[1].state == SI_ST_ASS);
894 }
895
896 /*
897 * Here we want to check if we need to resync or not.
898 */
899 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
900 resync = 1;
901
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 /* according to benchmarks, it makes sense to resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100903 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 goto resync_stream_interface;
905
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100906 if (resync)
907 goto resync_request;
908
909 resync_response:
910 resync = 0;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911
912 /* Analyse response */
913
914 if (unlikely(s->rep->flags & BF_HIJACK)) {
915 /* In inject mode, we wake up everytime something has
916 * happened on the write side of the buffer.
917 */
918 unsigned int flags = s->rep->flags;
919
920 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
921 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100922 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923 }
Willy Tarreauea388542009-06-21 21:45:58 +0200924
Willy Tarreau06bea942009-03-21 22:09:29 +0100925 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926 resync = 1;
927 }
928 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
929 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
930 unsigned int flags = s->rep->flags;
931
932 if (s->rep->prod->state >= SI_ST_EST) {
933 /* it's up to the analysers to reset write_ena */
934 buffer_write_ena(s->rep);
935 if (s->rep->analysers)
936 process_response(s);
937 }
Willy Tarreau84455332009-03-15 22:34:05 +0100938
939 /* Report it if the server got an error or a read timeout expired */
940 if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
941 !(s->flags & SN_ERR_MASK)) {
942 s->rep->analysers = 0;
943 if (s->rep->flags & BF_READ_ERROR)
944 s->flags |= SN_ERR_SRVCL;
945 else if (s->rep->flags & BF_READ_TIMEOUT)
946 s->flags |= SN_ERR_SRVTO;
947 else if (s->rep->flags & BF_WRITE_ERROR)
948 s->flags |= SN_ERR_CLICL;
949 else
950 s->flags |= SN_ERR_CLITO;
951 sess_set_term_flags(s);
952 }
953
Willy Tarreau06bea942009-03-21 22:09:29 +0100954 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100955 resync = 1;
956 }
957
Willy Tarreau84455332009-03-15 22:34:05 +0100958 /* Report it if the server got an error or a read timeout expired */
959 if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
960 !(s->flags & SN_ERR_MASK)) {
961 if (s->rep->flags & BF_READ_ERROR)
962 s->flags |= SN_ERR_SRVCL;
963 else if (s->rep->flags & BF_READ_TIMEOUT)
964 s->flags |= SN_ERR_SRVTO;
965 else if (s->rep->flags & BF_WRITE_ERROR)
966 s->flags |= SN_ERR_CLICL;
967 else
968 s->flags |= SN_ERR_CLITO;
969 sess_set_term_flags(s);
970 }
971
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100972 /* If noone is interested in analysing data, it's time to forward
973 * everything. We will wake up from time to time when either send_max
974 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100975 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100976 if (!s->rep->analysers &&
977 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
978 (s->rep->prod->state >= SI_ST_EST)) {
979 /* This buffer is freewheeling, there's no analyser nor hijacker
980 * attached to it. If any data are left in, we'll permit them to
981 * move.
982 */
983 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100984
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100985 /* If the producer is still connected, we'll schedule large blocks
986 * of data to be forwarded from the producer to the consumer (which
987 * might possibly not be connected yet).
988 */
989 if (!(s->rep->flags & BF_SHUTR) &&
990 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100991 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100992 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100993
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100994 /* check if it is wise to enable kernel splicing to forward response data */
995 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
996 s->rep->to_forward &&
997 (global.tune.options & GTUNE_USE_SPLICE) &&
998 (pipes_used < global.maxpipes) &&
999 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1000 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1001 (s->rep->flags & BF_STREAMER_FAST)))) {
1002 s->rep->flags |= BF_KERN_SPLICING;
1003 }
1004
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005 /* reflect what the L7 analysers have seen last */
1006 rpf_last = s->rep->flags;
1007
1008 /*
1009 * Now forward all shutdown requests between both sides of the buffer
1010 */
1011
1012 /*
1013 * FIXME: this is probably where we should produce error responses.
1014 */
1015
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001016 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
1018 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
1019 buffer_shutw_now(s->rep);
1020
1021 /* shutdown(write) pending */
1022 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
1023 s->rep->cons->shutw(s->rep->cons);
1024
1025 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001026 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1027 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001028 buffer_shutr_now(s->rep);
1029
1030 /* shutdown(read) pending */
1031 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1032 s->rep->prod->shutr(s->rep->prod);
1033
1034 /*
1035 * Here we want to check if we need to resync or not.
1036 */
1037 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
1038 resync = 1;
1039
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001040 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041 goto resync_stream_interface;
1042
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001043 if (s->req->flags != rqf_last)
1044 goto resync_request;
1045
1046 if (resync)
1047 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001048
1049 /* This is needed only when debugging is enabled, to indicate
1050 * client-side or server-side close. Please note that in the unlikely
1051 * event where both sides would close at once, the sequence is reported
1052 * on the server side first.
1053 */
1054 if (unlikely((global.mode & MODE_DEBUG) &&
1055 (!(global.mode & MODE_QUIET) ||
1056 (global.mode & MODE_VERBOSE)))) {
1057 int len;
1058
1059 if (s->si[1].state == SI_ST_CLO &&
1060 s->si[1].prev_state == SI_ST_EST) {
1061 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1062 s->uniq_id, s->be->id,
1063 (unsigned short)s->si[0].fd,
1064 (unsigned short)s->si[1].fd);
1065 write(1, trash, len);
1066 }
1067
1068 if (s->si[0].state == SI_ST_CLO &&
1069 s->si[0].prev_state == SI_ST_EST) {
1070 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1071 s->uniq_id, s->be->id,
1072 (unsigned short)s->si[0].fd,
1073 (unsigned short)s->si[1].fd);
1074 write(1, trash, len);
1075 }
1076 }
1077
1078 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1079 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1080
1081 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1082 session_process_counters(s);
1083
1084 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001085 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001086
1087 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001088 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001089
Willy Tarreauea388542009-06-21 21:45:58 +02001090 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1091 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001092 s->si[0].prev_state = s->si[0].state;
1093 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001094 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1095 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001096
1097 /* Trick: if a request is being waiting for the server to respond,
1098 * and if we know the server can timeout, we don't want the timeout
1099 * to expire on the client side first, but we're still interested
1100 * in passing data from the client to the server (eg: POST). Thus,
1101 * we can cancel the client's request timeout if the server's
1102 * request timeout is set and the server has not yet sent a response.
1103 */
1104
1105 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001106 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1107 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001108 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001109 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001110
1111 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1112 tick_first(s->rep->rex, s->rep->wex));
1113 if (s->req->analysers)
1114 t->expire = tick_first(t->expire, s->req->analyse_exp);
1115
1116 if (s->si[0].exp)
1117 t->expire = tick_first(t->expire, s->si[0].exp);
1118
1119 if (s->si[1].exp)
1120 t->expire = tick_first(t->expire, s->si[1].exp);
1121
1122#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001123 fprintf(stderr,
1124 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1125 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1126 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1127 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 +01001128#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129
1130#ifdef DEBUG_DEV
1131 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001132 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001133 ABORT_NOW();
1134#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001135 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001136 }
1137
1138 s->fe->feconn--;
1139 if (s->flags & SN_BE_ASSIGNED)
1140 s->be->beconn--;
1141 actconn--;
1142
1143 if (unlikely((global.mode & MODE_DEBUG) &&
1144 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1145 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001146 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001147 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001148 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001149 write(1, trash, len);
1150 }
1151
1152 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1153 session_process_counters(s);
1154
1155 /* let's do a final log if we need it */
1156 if (s->logs.logwait &&
1157 !(s->flags & SN_MONITOR) &&
1158 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001159 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001160 }
1161
1162 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001163 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001164 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001165 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001166 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001167}
1168
Willy Tarreau7c669d72008-06-20 15:04:11 +02001169/*
1170 * This function adjusts sess->srv_conn and maintains the previous and new
1171 * server's served session counts. Setting newsrv to NULL is enough to release
1172 * current connection slot. This function also notifies any LB algo which might
1173 * expect to be informed about any change in the number of active sessions on a
1174 * server.
1175 */
1176void sess_change_server(struct session *sess, struct server *newsrv)
1177{
1178 if (sess->srv_conn == newsrv)
1179 return;
1180
1181 if (sess->srv_conn) {
1182 sess->srv_conn->served--;
1183 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1184 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1185 sess->srv_conn = NULL;
1186 }
1187
1188 if (newsrv) {
1189 newsrv->served++;
1190 if (newsrv->proxy->lbprm.server_take_conn)
1191 newsrv->proxy->lbprm.server_take_conn(newsrv);
1192 sess->srv_conn = newsrv;
1193 }
1194}
1195
Willy Tarreau84455332009-03-15 22:34:05 +01001196/* Set correct session termination flags in case no analyser has done it. It
1197 * also counts a failed request if the server state has not reached the request
1198 * stage.
1199 */
1200void sess_set_term_flags(struct session *s)
1201{
1202 if (!(s->flags & SN_FINST_MASK)) {
1203 if (s->si[1].state < SI_ST_REQ) {
1204 s->fe->failed_req++;
1205 s->flags |= SN_FINST_R;
1206 }
1207 else if (s->si[1].state == SI_ST_QUE)
1208 s->flags |= SN_FINST_Q;
1209 else if (s->si[1].state < SI_ST_EST)
1210 s->flags |= SN_FINST_C;
1211 else if (s->si[1].state == SI_ST_EST)
1212 s->flags |= SN_FINST_D;
1213 else
1214 s->flags |= SN_FINST_L;
1215 }
1216}
1217
1218/* Handle server-side errors for default protocols. It is called whenever a a
1219 * connection setup is aborted or a request is aborted in queue. It sets the
1220 * session termination flags so that the caller does not have to worry about
1221 * them. It's installed as ->srv_error for the server-side stream_interface.
1222 */
1223void default_srv_error(struct session *s, struct stream_interface *si)
1224{
1225 int err_type = si->err_type;
1226 int err = 0, fin = 0;
1227
1228 if (err_type & SI_ET_QUEUE_ABRT) {
1229 err = SN_ERR_CLICL;
1230 fin = SN_FINST_Q;
1231 }
1232 else if (err_type & SI_ET_CONN_ABRT) {
1233 err = SN_ERR_CLICL;
1234 fin = SN_FINST_C;
1235 }
1236 else if (err_type & SI_ET_QUEUE_TO) {
1237 err = SN_ERR_SRVTO;
1238 fin = SN_FINST_Q;
1239 }
1240 else if (err_type & SI_ET_QUEUE_ERR) {
1241 err = SN_ERR_SRVCL;
1242 fin = SN_FINST_Q;
1243 }
1244 else if (err_type & SI_ET_CONN_TO) {
1245 err = SN_ERR_SRVTO;
1246 fin = SN_FINST_C;
1247 }
1248 else if (err_type & SI_ET_CONN_ERR) {
1249 err = SN_ERR_SRVCL;
1250 fin = SN_FINST_C;
1251 }
1252 else /* SI_ET_CONN_OTHER and others */ {
1253 err = SN_ERR_INTERNAL;
1254 fin = SN_FINST_C;
1255 }
1256
1257 if (!(s->flags & SN_ERR_MASK))
1258 s->flags |= err;
1259 if (!(s->flags & SN_FINST_MASK))
1260 s->flags |= fin;
1261}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001262
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263/*
1264 * Local variables:
1265 * c-indent-level: 8
1266 * c-basic-offset: 8
1267 * End:
1268 */