blob: 659dadb5be3e3908e34ebbf87ffd2ab70f8bb493 [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 Tarreau55a8d0e2008-11-30 18:47:21 +0100781 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
782 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreau06bea942009-03-21 22:09:29 +0100783 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784 resync = 1;
785 }
786
Willy Tarreau84455332009-03-15 22:34:05 +0100787 /* Report it if the client got an error or a read timeout expired */
788 if (unlikely(s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
789 !(s->flags & SN_ERR_MASK)) {
790 if (s->req->flags & BF_READ_ERROR)
791 s->flags |= SN_ERR_CLICL;
792 else if (s->req->flags & BF_READ_TIMEOUT)
793 s->flags |= SN_ERR_CLITO;
794 else if (s->req->flags & BF_WRITE_ERROR)
795 s->flags |= SN_ERR_SRVCL;
796 else
797 s->flags |= SN_ERR_SRVTO;
798 sess_set_term_flags(s);
799 }
800
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100801 /* If noone is interested in analysing data, it's time to forward
802 * everything. We will wake up from time to time when either send_max
803 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100804 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100805 if (!s->req->analysers &&
806 !(s->req->flags & (BF_HIJACK|BF_SHUTW)) &&
807 (s->req->prod->state >= SI_ST_EST)) {
808 /* This buffer is freewheeling, there's no analyser nor hijacker
809 * attached to it. If any data are left in, we'll permit them to
810 * move.
811 */
812 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100813
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100814 /* If the producer is still connected, we'll schedule large blocks
815 * of data to be forwarded from the producer to the consumer (which
816 * might possibly not be connected yet).
817 */
818 if (!(s->req->flags & BF_SHUTR) &&
819 s->req->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100820 buffer_forward(s->req, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100821 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100822
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100823 /* check if it is wise to enable kernel splicing to forward request data */
824 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
825 s->req->to_forward &&
826 (global.tune.options & GTUNE_USE_SPLICE) &&
827 (pipes_used < global.maxpipes) &&
828 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
829 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
830 (s->req->flags & BF_STREAMER_FAST)))) {
831 s->req->flags |= BF_KERN_SPLICING;
832 }
833
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834 /* reflect what the L7 analysers have seen last */
835 rqf_last = s->req->flags;
836
837 /*
838 * Now forward all shutdown requests between both sides of the buffer
839 */
840
841 /* first, let's check if the request buffer needs to shutdown(write) */
842 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
843 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
844 buffer_shutw_now(s->req);
845 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
846 (s->req->cons->state == SI_ST_EST) &&
847 s->be->options & PR_O_FORCE_CLO &&
848 s->rep->flags & BF_READ_ACTIVITY) {
849 /* We want to force the connection to the server to close,
850 * and the server has begun to respond. That's the right
851 * time.
852 */
853 buffer_shutw_now(s->req);
854 }
855
856 /* shutdown(write) pending */
857 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
858 s->req->cons->shutw(s->req->cons);
859
860 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100861 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
862 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 buffer_shutr_now(s->req);
864
865 /* shutdown(read) pending */
866 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
867 s->req->prod->shutr(s->req->prod);
868
Willy Tarreau92795622009-03-06 12:51:23 +0100869 /* it's possible that an upper layer has requested a connection setup or abort */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100870 if (s->req->cons->state == SI_ST_INI &&
Willy Tarreau92795622009-03-06 12:51:23 +0100871 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW))) {
872 if ((s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
873 s->req->cons->state = SI_ST_REQ; /* new connection requested */
874 else
875 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
876 }
877
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878
879 /* we may have a pending connection request, or a connection waiting
880 * for completion.
881 */
882 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
883 do {
884 /* nb: step 1 might switch from QUE to ASS, but we first want
885 * to give a chance to step 2 to perform a redirect if needed.
886 */
887 if (s->si[1].state != SI_ST_REQ)
888 sess_update_stream_int(s, &s->si[1]);
889 if (s->si[1].state == SI_ST_REQ)
890 sess_prepare_conn_req(s, &s->si[1]);
891
892 if (s->si[1].state == SI_ST_ASS && s->srv &&
893 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
894 perform_http_redirect(s, &s->si[1]);
895 } while (s->si[1].state == SI_ST_ASS);
896 }
897
898 /*
899 * Here we want to check if we need to resync or not.
900 */
901 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
902 resync = 1;
903
904 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
905
906 /* according to benchmarks, it makes sense to resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100907 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 goto resync_stream_interface;
909
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100910 if (resync)
911 goto resync_request;
912
913 resync_response:
914 resync = 0;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100915
916 /* Analyse response */
917
918 if (unlikely(s->rep->flags & BF_HIJACK)) {
919 /* In inject mode, we wake up everytime something has
920 * happened on the write side of the buffer.
921 */
922 unsigned int flags = s->rep->flags;
923
924 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
925 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100926 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100927 }
928 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
929 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreau06bea942009-03-21 22:09:29 +0100930 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100931 resync = 1;
932 }
933 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
934 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
935 unsigned int flags = s->rep->flags;
936
937 if (s->rep->prod->state >= SI_ST_EST) {
938 /* it's up to the analysers to reset write_ena */
939 buffer_write_ena(s->rep);
940 if (s->rep->analysers)
941 process_response(s);
942 }
Willy Tarreau84455332009-03-15 22:34:05 +0100943
944 /* Report it if the server got an error or a read timeout expired */
945 if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
946 !(s->flags & SN_ERR_MASK)) {
947 s->rep->analysers = 0;
948 if (s->rep->flags & BF_READ_ERROR)
949 s->flags |= SN_ERR_SRVCL;
950 else if (s->rep->flags & BF_READ_TIMEOUT)
951 s->flags |= SN_ERR_SRVTO;
952 else if (s->rep->flags & BF_WRITE_ERROR)
953 s->flags |= SN_ERR_CLICL;
954 else
955 s->flags |= SN_ERR_CLITO;
956 sess_set_term_flags(s);
957 }
958
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100959 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
960 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
Willy Tarreau06bea942009-03-21 22:09:29 +0100961 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100962 resync = 1;
963 }
964
Willy Tarreau84455332009-03-15 22:34:05 +0100965 /* Report it if the server got an error or a read timeout expired */
966 if (unlikely(s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) &&
967 !(s->flags & SN_ERR_MASK)) {
968 if (s->rep->flags & BF_READ_ERROR)
969 s->flags |= SN_ERR_SRVCL;
970 else if (s->rep->flags & BF_READ_TIMEOUT)
971 s->flags |= SN_ERR_SRVTO;
972 else if (s->rep->flags & BF_WRITE_ERROR)
973 s->flags |= SN_ERR_CLICL;
974 else
975 s->flags |= SN_ERR_CLITO;
976 sess_set_term_flags(s);
977 }
978
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100979 /* If noone is interested in analysing data, it's time to forward
980 * everything. We will wake up from time to time when either send_max
981 * or to_forward are reached.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100982 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100983 if (!s->rep->analysers &&
984 !(s->rep->flags & (BF_HIJACK|BF_SHUTW)) &&
985 (s->rep->prod->state >= SI_ST_EST)) {
986 /* This buffer is freewheeling, there's no analyser nor hijacker
987 * attached to it. If any data are left in, we'll permit them to
988 * move.
989 */
990 buffer_flush(s->rep);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100991
Willy Tarreau7c84bab2009-03-08 21:38:23 +0100992 /* If the producer is still connected, we'll schedule large blocks
993 * of data to be forwarded from the producer to the consumer (which
994 * might possibly not be connected yet).
995 */
996 if (!(s->rep->flags & BF_SHUTR) &&
997 s->rep->to_forward < FORWARD_DEFAULT_SIZE)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100998 buffer_forward(s->rep, FORWARD_DEFAULT_SIZE);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100999 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001000
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001001 /* check if it is wise to enable kernel splicing to forward response data */
1002 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1003 s->rep->to_forward &&
1004 (global.tune.options & GTUNE_USE_SPLICE) &&
1005 (pipes_used < global.maxpipes) &&
1006 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1007 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1008 (s->rep->flags & BF_STREAMER_FAST)))) {
1009 s->rep->flags |= BF_KERN_SPLICING;
1010 }
1011
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001012 /* reflect what the L7 analysers have seen last */
1013 rpf_last = s->rep->flags;
1014
1015 /*
1016 * Now forward all shutdown requests between both sides of the buffer
1017 */
1018
1019 /*
1020 * FIXME: this is probably where we should produce error responses.
1021 */
1022
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001023 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001024 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
1025 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
1026 buffer_shutw_now(s->rep);
1027
1028 /* shutdown(write) pending */
1029 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
1030 s->rep->cons->shutw(s->rep->cons);
1031
1032 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001033 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1034 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001035 buffer_shutr_now(s->rep);
1036
1037 /* shutdown(read) pending */
1038 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1039 s->rep->prod->shutr(s->rep->prod);
1040
1041 /*
1042 * Here we want to check if we need to resync or not.
1043 */
1044 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
1045 resync = 1;
1046
1047 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1048
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001049 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001050 goto resync_stream_interface;
1051
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001052 if (s->req->flags != rqf_last)
1053 goto resync_request;
1054
1055 if (resync)
1056 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001057
1058 /* This is needed only when debugging is enabled, to indicate
1059 * client-side or server-side close. Please note that in the unlikely
1060 * event where both sides would close at once, the sequence is reported
1061 * on the server side first.
1062 */
1063 if (unlikely((global.mode & MODE_DEBUG) &&
1064 (!(global.mode & MODE_QUIET) ||
1065 (global.mode & MODE_VERBOSE)))) {
1066 int len;
1067
1068 if (s->si[1].state == SI_ST_CLO &&
1069 s->si[1].prev_state == SI_ST_EST) {
1070 len = sprintf(trash, "%08x:%s.srvcls[%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 if (s->si[0].state == SI_ST_CLO &&
1078 s->si[0].prev_state == SI_ST_EST) {
1079 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1080 s->uniq_id, s->be->id,
1081 (unsigned short)s->si[0].fd,
1082 (unsigned short)s->si[1].fd);
1083 write(1, trash, len);
1084 }
1085 }
1086
1087 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1088 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1089
1090 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1091 session_process_counters(s);
1092
1093 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001094 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001095
1096 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +01001097 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001098
1099 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1100 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
1101 s->si[0].prev_state = s->si[0].state;
1102 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001103 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1104 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001105
1106 /* Trick: if a request is being waiting for the server to respond,
1107 * and if we know the server can timeout, we don't want the timeout
1108 * to expire on the client side first, but we're still interested
1109 * in passing data from the client to the server (eg: POST). Thus,
1110 * we can cancel the client's request timeout if the server's
1111 * request timeout is set and the server has not yet sent a response.
1112 */
1113
1114 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001115 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1116 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001117 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001118 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001119
1120 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1121 tick_first(s->rep->rex, s->rep->wex));
1122 if (s->req->analysers)
1123 t->expire = tick_first(t->expire, s->req->analyse_exp);
1124
1125 if (s->si[0].exp)
1126 t->expire = tick_first(t->expire, s->si[0].exp);
1127
1128 if (s->si[1].exp)
1129 t->expire = tick_first(t->expire, s->si[1].exp);
1130
1131#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001132 fprintf(stderr,
1133 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1134 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1135 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1136 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 +01001137#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001138
1139#ifdef DEBUG_DEV
1140 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001141 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001142 ABORT_NOW();
1143#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001144 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001145 }
1146
1147 s->fe->feconn--;
1148 if (s->flags & SN_BE_ASSIGNED)
1149 s->be->beconn--;
1150 actconn--;
1151
1152 if (unlikely((global.mode & MODE_DEBUG) &&
1153 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1154 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001155 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001156 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001157 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001158 write(1, trash, len);
1159 }
1160
1161 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1162 session_process_counters(s);
1163
1164 /* let's do a final log if we need it */
1165 if (s->logs.logwait &&
1166 !(s->flags & SN_MONITOR) &&
1167 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001168 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001169 }
1170
1171 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001173 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001174 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001175 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001176}
1177
Willy Tarreau7c669d72008-06-20 15:04:11 +02001178/*
1179 * This function adjusts sess->srv_conn and maintains the previous and new
1180 * server's served session counts. Setting newsrv to NULL is enough to release
1181 * current connection slot. This function also notifies any LB algo which might
1182 * expect to be informed about any change in the number of active sessions on a
1183 * server.
1184 */
1185void sess_change_server(struct session *sess, struct server *newsrv)
1186{
1187 if (sess->srv_conn == newsrv)
1188 return;
1189
1190 if (sess->srv_conn) {
1191 sess->srv_conn->served--;
1192 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1193 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1194 sess->srv_conn = NULL;
1195 }
1196
1197 if (newsrv) {
1198 newsrv->served++;
1199 if (newsrv->proxy->lbprm.server_take_conn)
1200 newsrv->proxy->lbprm.server_take_conn(newsrv);
1201 sess->srv_conn = newsrv;
1202 }
1203}
1204
Willy Tarreau84455332009-03-15 22:34:05 +01001205/* Set correct session termination flags in case no analyser has done it. It
1206 * also counts a failed request if the server state has not reached the request
1207 * stage.
1208 */
1209void sess_set_term_flags(struct session *s)
1210{
1211 if (!(s->flags & SN_FINST_MASK)) {
1212 if (s->si[1].state < SI_ST_REQ) {
1213 s->fe->failed_req++;
1214 s->flags |= SN_FINST_R;
1215 }
1216 else if (s->si[1].state == SI_ST_QUE)
1217 s->flags |= SN_FINST_Q;
1218 else if (s->si[1].state < SI_ST_EST)
1219 s->flags |= SN_FINST_C;
1220 else if (s->si[1].state == SI_ST_EST)
1221 s->flags |= SN_FINST_D;
1222 else
1223 s->flags |= SN_FINST_L;
1224 }
1225}
1226
1227/* Handle server-side errors for default protocols. It is called whenever a a
1228 * connection setup is aborted or a request is aborted in queue. It sets the
1229 * session termination flags so that the caller does not have to worry about
1230 * them. It's installed as ->srv_error for the server-side stream_interface.
1231 */
1232void default_srv_error(struct session *s, struct stream_interface *si)
1233{
1234 int err_type = si->err_type;
1235 int err = 0, fin = 0;
1236
1237 if (err_type & SI_ET_QUEUE_ABRT) {
1238 err = SN_ERR_CLICL;
1239 fin = SN_FINST_Q;
1240 }
1241 else if (err_type & SI_ET_CONN_ABRT) {
1242 err = SN_ERR_CLICL;
1243 fin = SN_FINST_C;
1244 }
1245 else if (err_type & SI_ET_QUEUE_TO) {
1246 err = SN_ERR_SRVTO;
1247 fin = SN_FINST_Q;
1248 }
1249 else if (err_type & SI_ET_QUEUE_ERR) {
1250 err = SN_ERR_SRVCL;
1251 fin = SN_FINST_Q;
1252 }
1253 else if (err_type & SI_ET_CONN_TO) {
1254 err = SN_ERR_SRVTO;
1255 fin = SN_FINST_C;
1256 }
1257 else if (err_type & SI_ET_CONN_ERR) {
1258 err = SN_ERR_SRVCL;
1259 fin = SN_FINST_C;
1260 }
1261 else /* SI_ET_CONN_OTHER and others */ {
1262 err = SN_ERR_INTERNAL;
1263 fin = SN_FINST_C;
1264 }
1265
1266 if (!(s->flags & SN_ERR_MASK))
1267 s->flags |= err;
1268 if (!(s->flags & SN_FINST_MASK))
1269 s->flags |= fin;
1270}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001271
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272/*
1273 * Local variables:
1274 * c-indent-level: 8
1275 * c-basic-offset: 8
1276 * End:
1277 */