blob: a22a6f347acedb3921e828374bd969b3dcfd403f [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 Tarreau1d0dfb12009-07-07 15:10:31 +020022#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020024#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010025#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020026#include <proto/dumpstats.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010027#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020028#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010030#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010031#include <proto/proto_http.h>
32#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020033#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010035#include <proto/server.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010036#include <proto/stream_interface.h>
37#include <proto/stream_sock.h>
38#include <proto/task.h>
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
Cyril Bontébf47aeb2009-10-15 00:15:40 +020081 if (s->sessid)
82 pool_free2(apools.sessid, s->sessid);
83
Willy Tarreau92fb9832007-10-16 17:34:28 +020084 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020085 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010086
Willy Tarreau92fb9832007-10-16 17:34:28 +020087 if (txn->rsp.cap != NULL) {
88 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020089 for (h = fe->rsp_cap; h; h = h->next)
90 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020091 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020093 if (txn->req.cap != NULL) {
94 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020095 for (h = fe->req_cap; h; h = h->next)
96 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020097 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100100
101 http_end_txn(s);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100102
103 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100104 /* we have to unlink all watchers. We must not relink them if
105 * this session was the last one in the list.
106 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100107 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100108 LIST_INIT(&bref->users);
109 if (s->list.n != &sessions)
110 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100111 bref->ref = s->list.n;
112 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100113 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200114 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200115
116 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200117 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200118 pool_flush2(pool2_buffer);
119 pool_flush2(fe->hdr_idx_pool);
120 pool_flush2(pool2_requri);
121 pool_flush2(pool2_capture);
122 pool_flush2(pool2_session);
123 pool_flush2(fe->req_cap_pool);
124 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200125 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200126}
127
128
129/* perform minimal intializations, report 0 in case of error, 1 if OK. */
130int init_session()
131{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100132 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200133 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
134 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135}
136
Willy Tarreau30e71012007-11-26 20:15:35 +0100137void session_process_counters(struct session *s)
138{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 unsigned long long bytes;
140
Willy Tarreau30e71012007-11-26 20:15:35 +0100141 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 s->logs.bytes_in = s->req->total;
144 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200145 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau30e71012007-11-26 20:15:35 +0100147 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200148 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149
Willy Tarreau30e71012007-11-26 20:15:35 +0100150 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200151 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200152
153 if (s->listener->counters)
154 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100156 }
157
Willy Tarreau30e71012007-11-26 20:15:35 +0100158 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100159 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100160 s->logs.bytes_out = s->rep->total;
161 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200162 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100163
Willy Tarreau30e71012007-11-26 20:15:35 +0100164 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200165 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100166
Willy Tarreau30e71012007-11-26 20:15:35 +0100167 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200168 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200169
170 if (s->listener->counters)
171 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100172 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100173 }
174}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100176/* This function is called with (si->state == SI_ST_CON) meaning that a
177 * connection was attempted and that the file descriptor is already allocated.
178 * We must check for establishment, error and abort. Possible output states
179 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
180 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
181 * otherwise 1.
182 */
183int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
184{
185 struct buffer *req = si->ob;
186 struct buffer *rep = si->ib;
187
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100188 /* If we got an error, or if nothing happened and the connection timed
189 * out, we must give up. The CER state handler will take care of retry
190 * attempts and error reports.
191 */
192 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100193 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100194 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200195 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100196 fd_delete(si->fd);
197
198 if (si->err_type)
199 return 0;
200
201 si->err_loc = s->srv;
202 if (si->flags & SI_FL_ERR)
203 si->err_type = SI_ET_CONN_ERR;
204 else
205 si->err_type = SI_ET_CONN_TO;
206 return 0;
207 }
208
209 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200210 if (unlikely((rep->flags & BF_SHUTW) ||
211 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200212 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100213 s->be->options & PR_O_ABRT_CLOSE)))) {
214 /* give up */
215 si->shutw(si);
216 si->err_type |= SI_ET_CONN_ABRT;
217 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200218 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100219 if (s->srv_error)
220 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100221 return 1;
222 }
223
224 /* we need to wait a bit more if there was no activity either */
225 if (!(req->flags & BF_WRITE_ACTIVITY))
226 return 1;
227
228 /* OK, this means that a connection succeeded. The caller will be
229 * responsible for handling the transition from CON to EST.
230 */
231 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100232 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100233 si->state = SI_ST_EST;
234 si->err_type = SI_ET_NONE;
235 si->err_loc = NULL;
236 return 1;
237}
238
239/* This function is called with (si->state == SI_ST_CER) meaning that a
240 * previous connection attempt has failed and that the file descriptor
241 * has already been released. Possible causes include asynchronous error
242 * notification and time out. Possible output states are SI_ST_CLO when
243 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
244 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
245 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
246 * marked as in error state. It returns 0.
247 */
248int sess_update_st_cer(struct session *s, struct stream_interface *si)
249{
250 /* we probably have to release last session from the server */
251 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100252 health_adjust(s->srv, HANA_STATUS_L4_ERR);
253
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100254 if (s->flags & SN_CURR_SESS) {
255 s->flags &= ~SN_CURR_SESS;
256 s->srv->cur_sess--;
257 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100258 }
259
260 /* ensure that we have enough retries left */
261 s->conn_retries--;
262 if (s->conn_retries < 0) {
263 if (!si->err_type) {
264 si->err_type = SI_ET_CONN_ERR;
265 si->err_loc = s->srv;
266 }
267
268 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200269 s->srv->counters.failed_conns++;
270 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100271 if (may_dequeue_tasks(s->srv, s->be))
272 process_srv_queue(s->srv);
273
274 /* shutw is enough so stop a connecting socket */
275 si->shutw(si);
276 si->ob->flags |= BF_WRITE_ERROR;
277 si->ib->flags |= BF_READ_ERROR;
278
279 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100280 if (s->srv_error)
281 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100282 return 0;
283 }
284
285 /* If the "redispatch" option is set on the backend, we are allowed to
286 * retry on another server for the last retry. In order to achieve this,
287 * we must mark the session unassigned, and eventually clear the DIRECT
288 * bit to ignore any persistence cookie. We won't count a retry nor a
289 * redispatch yet, because this will depend on what server is selected.
290 */
291 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
292 if (may_dequeue_tasks(s->srv, s->be))
293 process_srv_queue(s->srv);
294
295 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
296 s->prev_srv = s->srv;
297 si->state = SI_ST_REQ;
298 } else {
299 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200300 s->srv->counters.retries++;
301 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100302 si->state = SI_ST_ASS;
303 }
304
305 if (si->flags & SI_FL_ERR) {
306 /* The error was an asynchronous connection error, and we will
307 * likely have to retry connecting to the same server, most
308 * likely leading to the same result. To avoid this, we wait
309 * one second before retrying.
310 */
311
312 if (!si->err_type)
313 si->err_type = SI_ET_CONN_ERR;
314
315 si->state = SI_ST_TAR;
316 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
317 return 0;
318 }
319 return 0;
320}
321
322/*
323 * This function handles the transition between the SI_ST_CON state and the
324 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
325 * SI_ST_EST.
326 */
327void sess_establish(struct session *s, struct stream_interface *si)
328{
329 struct buffer *req = si->ob;
330 struct buffer *rep = si->ib;
331
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100332 if (s->srv)
333 health_adjust(s->srv, HANA_STATUS_L4_OK);
334
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100335 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100336 /* if the user wants to log as soon as possible, without counting
337 * bytes from the server, then this is the right moment. */
338 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
339 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100340 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100341 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100342 }
343 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100344 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
345 /* reset hdr_idx which was already initialized by the request.
346 * right now, the http parser does it.
347 * hdr_idx_init(&s->txn.hdr_idx);
348 */
349 }
350
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200351 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100352 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
353 req->wex = TICK_ETERNITY;
354}
355
356/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
357 * Other input states are simply ignored.
358 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
359 * Flags must have previously been updated for timeouts and other conditions.
360 */
361void sess_update_stream_int(struct session *s, struct stream_interface *si)
362{
363 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",
364 now_ms, __FUNCTION__,
365 s,
366 s->req, s->rep,
367 s->req->rex, s->rep->wex,
368 s->req->flags, s->rep->flags,
369 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
370
371 if (si->state == SI_ST_ASS) {
372 /* Server assigned to connection request, we have to try to connect now */
373 int conn_err;
374
375 conn_err = connect_server(s);
376 if (conn_err == SN_ERR_NONE) {
377 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100378 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100379 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100380 return;
381 }
382
383 /* We have received a synchronous error. We might have to
384 * abort, retry immediately or redispatch.
385 */
386 if (conn_err == SN_ERR_INTERNAL) {
387 if (!si->err_type) {
388 si->err_type = SI_ET_CONN_OTHER;
389 si->err_loc = s->srv;
390 }
391
392 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100393 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100394 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200395 s->srv->counters.failed_conns++;
396 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100397
398 /* release other sessions waiting for this server */
399 if (may_dequeue_tasks(s->srv, s->be))
400 process_srv_queue(s->srv);
401
402 /* Failed and not retryable. */
403 si->shutr(si);
404 si->shutw(si);
405 si->ob->flags |= BF_WRITE_ERROR;
406
407 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
408
409 /* no session was ever accounted for this server */
410 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100411 if (s->srv_error)
412 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100413 return;
414 }
415
416 /* We are facing a retryable error, but we don't want to run a
417 * turn-around now, as the problem is likely a source port
418 * allocation problem, so we want to retry now.
419 */
420 si->state = SI_ST_CER;
421 si->flags &= ~SI_FL_ERR;
422 sess_update_st_cer(s, si);
423 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
424 return;
425 }
426 else if (si->state == SI_ST_QUE) {
427 /* connection request was queued, check for any update */
428 if (!s->pend_pos) {
429 /* The connection is not in the queue anymore. Either
430 * we have a server connection slot available and we
431 * go directly to the assigned state, or we need to
432 * load-balance first and go to the INI state.
433 */
434 si->exp = TICK_ETERNITY;
435 if (unlikely(!(s->flags & SN_ASSIGNED)))
436 si->state = SI_ST_REQ;
437 else {
438 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
439 si->state = SI_ST_ASS;
440 }
441 return;
442 }
443
444 /* Connection request still in queue... */
445 if (si->flags & SI_FL_EXP) {
446 /* ... and timeout expired */
447 si->exp = TICK_ETERNITY;
448 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
449 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200450 s->srv->counters.failed_conns++;
451 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100452 si->shutr(si);
453 si->shutw(si);
454 si->ob->flags |= BF_WRITE_TIMEOUT;
455 if (!si->err_type)
456 si->err_type = SI_ET_QUEUE_TO;
457 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100458 if (s->srv_error)
459 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100460 return;
461 }
462
463 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200464 if ((si->ob->flags & (BF_READ_ERROR)) ||
465 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200466 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100467 /* give up */
468 si->exp = TICK_ETERNITY;
469 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
470 si->shutr(si);
471 si->shutw(si);
472 si->err_type |= SI_ET_QUEUE_ABRT;
473 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100474 if (s->srv_error)
475 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100476 return;
477 }
478
479 /* Nothing changed */
480 return;
481 }
482 else if (si->state == SI_ST_TAR) {
483 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200484 if ((si->ob->flags & (BF_READ_ERROR)) ||
485 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200486 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100487 /* give up */
488 si->exp = TICK_ETERNITY;
489 si->shutr(si);
490 si->shutw(si);
491 si->err_type |= SI_ET_CONN_ABRT;
492 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100493 if (s->srv_error)
494 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100495 return;
496 }
497
498 if (!(si->flags & SI_FL_EXP))
499 return; /* still in turn-around */
500
501 si->exp = TICK_ETERNITY;
502
503 /* we keep trying on the same server as long as the session is
504 * marked "assigned".
505 * FIXME: Should we force a redispatch attempt when the server is down ?
506 */
507 if (s->flags & SN_ASSIGNED)
508 si->state = SI_ST_ASS;
509 else
510 si->state = SI_ST_REQ;
511 return;
512 }
513}
514
515/* This function initiates a server connection request on a stream interface
516 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
517 * indicating that a server has been assigned. It may also return SI_ST_QUE,
518 * or SI_ST_CLO upon error.
519 */
520static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
521 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",
522 now_ms, __FUNCTION__,
523 s,
524 s->req, s->rep,
525 s->req->rex, s->rep->wex,
526 s->req->flags, s->rep->flags,
527 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
528
529 if (si->state != SI_ST_REQ)
530 return;
531
532 /* Try to assign a server */
533 if (srv_redispatch_connect(s) != 0) {
534 /* We did not get a server. Either we queued the
535 * connection request, or we encountered an error.
536 */
537 if (si->state == SI_ST_QUE)
538 return;
539
540 /* we did not get any server, let's check the cause */
541 si->shutr(si);
542 si->shutw(si);
543 si->ob->flags |= BF_WRITE_ERROR;
544 if (!si->err_type)
545 si->err_type = SI_ET_CONN_OTHER;
546 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100547 if (s->srv_error)
548 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100549 return;
550 }
551
552 /* The server is assigned */
553 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
554 si->state = SI_ST_ASS;
555}
556
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200557/* This stream analyser checks the switching rules and changes the backend
558 * if appropriate. The default_backend rule is also considered.
559 * It returns 1 if the processing can continue on next analysers, or zero if it
560 * either needs more data or wants to immediately abort the request.
561 */
562int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
563{
564 req->analysers &= ~an_bit;
565 req->analyse_exp = TICK_ETERNITY;
566
567 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
568 now_ms, __FUNCTION__,
569 s,
570 req,
571 req->rex, req->wex,
572 req->flags,
573 req->l,
574 req->analysers);
575
576 /* now check whether we have some switching rules for this request */
577 if (!(s->flags & SN_BE_ASSIGNED)) {
578 struct switching_rule *rule;
579
580 list_for_each_entry(rule, &s->fe->switching_rules, list) {
581 int ret;
582
583 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
584 ret = acl_pass(ret);
585 if (rule->cond->pol == ACL_COND_UNLESS)
586 ret = !ret;
587
588 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200589 if (!session_set_backend(s, rule->be.backend))
590 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200591 break;
592 }
593 }
594
595 /* To ensure correct connection accounting on the backend, we
596 * have to assign one if it was not set (eg: a listen). This
597 * measure also takes care of correctly setting the default
598 * backend if any.
599 */
600 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200601 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
602 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200603 }
604
605 /* we don't want to run the HTTP filters again if the backend has not changed */
606 if (s->fe == s->be)
607 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
608
609 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200610
611 sw_failed:
612 /* immediately abort this request in case of allocation failure */
613 buffer_abort(s->req);
614 buffer_abort(s->rep);
615
616 if (!(s->flags & SN_ERR_MASK))
617 s->flags |= SN_ERR_RESOURCE;
618 if (!(s->flags & SN_FINST_MASK))
619 s->flags |= SN_FINST_R;
620
621 s->txn.status = 500;
622 s->req->analysers = 0;
623 s->req->analyse_exp = TICK_ETERNITY;
624 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200625}
626
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627/* Processes the client, server, request and response jobs of a session task,
628 * then puts it back to the wait queue in a clean state, or cleans up its
629 * resources if it must be deleted. Returns in <next> the date the task wants
630 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
631 * nothing too many times, the request and response buffers flags are monitored
632 * and each function is called only if at least another function has changed at
633 * least one flag it is interested in.
634 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100635struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636{
637 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 unsigned int rqf_last, rpf_last;
639
640 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
641 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
642
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200643 /* This flag must explicitly be set every time */
644 s->req->flags &= ~BF_READ_NOEXP;
645
646 /* Keep a copy of req/rep flags so that we can detect shutdowns */
647 rqf_last = s->req->flags;
648 rpf_last = s->rep->flags;
649
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200650 /* we don't want the stream interface functions to recursively wake us up */
651 if (s->req->prod->owner == t)
652 s->req->prod->flags |= SI_FL_DONT_WAKE;
653 if (s->req->cons->owner == t)
654 s->req->cons->flags |= SI_FL_DONT_WAKE;
655
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656 /* 1a: Check for low level timeouts if needed. We just set a flag on
657 * stream interfaces when their timeouts have expired.
658 */
659 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
660 stream_int_check_timeouts(&s->si[0]);
661 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200662
663 /* check buffer timeouts, and close the corresponding stream interfaces
664 * for future reads or writes. Note: this will also concern upper layers
665 * but we do not touch any other flag. We must be careful and correctly
666 * detect state changes when calling them.
667 */
668
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100669 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200670
Willy Tarreau14641402009-12-29 14:49:56 +0100671 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
672 s->req->cons->flags |= SI_FL_NOLINGER;
673 s->req->cons->shutw(s->req->cons);
674 }
675
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200676 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
677 s->req->prod->shutr(s->req->prod);
678
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680
Willy Tarreau14641402009-12-29 14:49:56 +0100681 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
682 s->rep->cons->flags |= SI_FL_NOLINGER;
683 s->rep->cons->shutw(s->rep->cons);
684 }
685
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200686 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
687 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200688 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689
690 /* 1b: check for low-level errors reported at the stream interface.
691 * First we check if it's a retryable error (in which case we don't
692 * want to tell the buffer). Otherwise we report the error one level
693 * upper by setting flags into the buffers. Note that the side towards
694 * the client cannot have connect (hence retryable) errors. Also, the
695 * connection setup code must be able to deal with any type of abort.
696 */
697 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
698 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
699 s->si[0].shutr(&s->si[0]);
700 s->si[0].shutw(&s->si[0]);
701 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100702 if (!(s->req->analysers) && !(s->rep->analysers)) {
703 if (!(s->flags & SN_ERR_MASK))
704 s->flags |= SN_ERR_CLICL;
705 if (!(s->flags & SN_FINST_MASK))
706 s->flags |= SN_FINST_D;
707 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 }
709 }
710
711 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
712 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
713 s->si[1].shutr(&s->si[1]);
714 s->si[1].shutw(&s->si[1]);
715 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200716 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200718 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100719 if (!(s->req->analysers) && !(s->rep->analysers)) {
720 if (!(s->flags & SN_ERR_MASK))
721 s->flags |= SN_ERR_SRVCL;
722 if (!(s->flags & SN_FINST_MASK))
723 s->flags |= SN_FINST_D;
724 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725 }
726 /* note: maybe we should process connection errors here ? */
727 }
728
729 if (s->si[1].state == SI_ST_CON) {
730 /* we were trying to establish a connection on the server side,
731 * maybe it succeeded, maybe it failed, maybe we timed out, ...
732 */
733 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
734 sess_update_st_cer(s, &s->si[1]);
735 else if (s->si[1].state == SI_ST_EST)
736 sess_establish(s, &s->si[1]);
737
738 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
739 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
740 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
741 */
742 }
743
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200744resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 /* Check for connection closure */
746
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747 DPRINTF(stderr,
748 "[%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",
749 now_ms, __FUNCTION__, __LINE__,
750 t,
751 s, s->flags,
752 s->req, s->rep,
753 s->req->rex, s->rep->wex,
754 s->req->flags, s->rep->flags,
755 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
756 s->rep->cons->err_type, s->req->cons->err_type,
757 s->conn_retries);
758
759 /* nothing special to be done on client side */
760 if (unlikely(s->req->prod->state == SI_ST_DIS))
761 s->req->prod->state = SI_ST_CLO;
762
763 /* When a server-side connection is released, we have to count it and
764 * check for pending connections on this server.
765 */
766 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
767 s->req->cons->state = SI_ST_CLO;
768 if (s->srv) {
769 if (s->flags & SN_CURR_SESS) {
770 s->flags &= ~SN_CURR_SESS;
771 s->srv->cur_sess--;
772 }
773 sess_change_server(s, NULL);
774 if (may_dequeue_tasks(s->srv, s->be))
775 process_srv_queue(s->srv);
776 }
777 }
778
779 /*
780 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
781 * at this point.
782 */
783
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100784 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100785 /* Analyse request */
786 if ((s->req->flags & BF_MASK_ANALYSER) ||
787 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
788 unsigned int flags = s->req->flags;
789
790 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200791 unsigned int last_ana = 0;
792
Willy Tarreau520d95e2009-09-19 21:04:57 +0200793 /* it's up to the analysers to stop new connections */
794 buffer_auto_connect(s->req);
795 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100796
797 /* We will call all analysers for which a bit is set in
798 * s->req->analysers, following the bit order from LSB
799 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200800 * the list when not needed. Any analyser may return 0
801 * to break out of the loop, either because of missing
802 * data to take a decision, or because it decides to
803 * kill the session. We loop at least once through each
804 * analyser, and we may loop again if other analysers
805 * are added in the middle.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100806 */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200807 while (s->req->analysers & ~last_ana) {
808 last_ana = s->req->analysers;
809
810 if (s->req->analysers & AN_REQ_INSPECT) {
811 last_ana |= AN_REQ_INSPECT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200812 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100813 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200814 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100815
Willy Tarreaud787e662009-07-07 10:14:51 +0200816 if (s->req->analysers & AN_REQ_WAIT_HTTP) {
817 last_ana |= AN_REQ_WAIT_HTTP;
Willy Tarreau3a816292009-07-07 10:55:49 +0200818 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200819 break;
820 }
821
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200822 if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) {
823 last_ana |= AN_REQ_HTTP_PROCESS_FE;
824 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
825 break;
826 }
827
828 if (s->req->analysers & AN_REQ_SWITCHING_RULES) {
829 last_ana |= AN_REQ_SWITCHING_RULES;
830 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
831 break;
Willy Tarreaud88bb6f2009-07-12 09:55:41 +0200832 /* FIXME: we mait switch from TCP to HTTP and want to
833 * immediately loop back to the top. This is a dirty way
834 * of doing it, and we should find a cleaner method relying
835 * on a circular list of function pointers.
836 */
837 if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP)
838 continue;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200839 }
840
841 if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) {
842 last_ana |= AN_REQ_HTTP_PROCESS_BE;
843 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
844 break;
845 }
846
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200847 if (s->req->analysers & AN_REQ_HTTP_TARPIT) {
848 last_ana |= AN_REQ_HTTP_TARPIT;
Willy Tarreau3a816292009-07-07 10:55:49 +0200849 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100850 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200851 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100852
Willy Tarreauc465fd72009-08-31 00:17:18 +0200853 if (s->req->analysers & AN_REQ_HTTP_INNER) {
854 last_ana |= AN_REQ_HTTP_INNER;
855 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
856 break;
857 }
858
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200859 if (s->req->analysers & AN_REQ_HTTP_BODY) {
860 last_ana |= AN_REQ_HTTP_BODY;
Willy Tarreau3a816292009-07-07 10:55:49 +0200861 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100862 break;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200863 }
Emeric Brun647caf12009-06-30 17:57:00 +0200864
865 if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) {
866 last_ana |= AN_REQ_PRST_RDP_COOKIE;
867 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
868 break;
869 }
Willy Tarreaud98cf932009-12-27 22:54:55 +0100870
871 if (s->req->analysers & AN_REQ_HTTP_XFER_BODY) {
872 last_ana |= AN_REQ_HTTP_XFER_BODY;
873 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
874 break;
875 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100876 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 }
Willy Tarreau84455332009-03-15 22:34:05 +0100878
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200879 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
880 rqf_last = s->req->flags;
881 goto resync_request;
882 }
883 }
884
885 resync_response:
886 /* Analyse response */
887
888 if (unlikely(s->rep->flags & BF_HIJACK)) {
889 /* In inject mode, we wake up everytime something has
890 * happened on the write side of the buffer.
891 */
892 unsigned int flags = s->rep->flags;
893
894 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
895 !(s->rep->flags & BF_FULL)) {
896 s->rep->hijacker(s, s->rep);
897 }
898
899 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
900 rpf_last = s->rep->flags;
901 goto resync_response;
902 }
903 }
904 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
905 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
906 unsigned int flags = s->rep->flags;
907
908 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200909 unsigned int last_ana = 0;
910
Willy Tarreau520d95e2009-09-19 21:04:57 +0200911 /* it's up to the analysers to reset auto_close */
912 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200913
914 /* We will call all analysers for which a bit is set in
915 * s->rep->analysers, following the bit order from LSB
916 * to MSB. The analysers must remove themselves from
917 * the list when not needed. Any analyser may return 0
918 * to break out of the loop, either because of missing
919 * data to take a decision, or because it decides to
920 * kill the session. We loop at least once through each
921 * analyser, and we may loop again if other analysers
922 * are added in the middle.
923 */
924 while (s->rep->analysers & ~last_ana) {
925 last_ana = s->rep->analysers;
926
927 if (s->rep->analysers & AN_RES_WAIT_HTTP) {
928 last_ana |= AN_RES_WAIT_HTTP;
929 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
930 break;
931 }
932
933 if (s->rep->analysers & AN_RES_HTTP_PROCESS_BE) {
934 last_ana |= AN_RES_HTTP_PROCESS_BE;
935 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
936 break;
937 /* FIXME: we may wait for a second response in case of a status 1xx
938 * and want to immediately loop back to the top. This is a dirty way
939 * of doing it, and we should find a cleaner method relying on a
940 * circular list of function pointers.
941 */
942 if ((s->rep->analysers & ~last_ana) & AN_RES_WAIT_HTTP)
943 continue;
944 }
Willy Tarreaud98cf932009-12-27 22:54:55 +0100945
946 if (s->rep->analysers & AN_RES_HTTP_XFER_BODY) {
947 last_ana |= AN_RES_HTTP_XFER_BODY;
948 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
949 break;
950 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200951 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200952 }
953
954 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
955 rpf_last = s->rep->flags;
956 goto resync_response;
957 }
958 }
959
960 /* FIXME: here we should call protocol handlers which rely on
961 * both buffers.
962 */
963
964
965 /*
966 * Now we propagate unhandled errors to the session
967 */
968 if (!(s->flags & SN_ERR_MASK)) {
969 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
970 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +0100971 s->req->analysers = 0;
972 if (s->req->flags & BF_READ_ERROR)
973 s->flags |= SN_ERR_CLICL;
974 else if (s->req->flags & BF_READ_TIMEOUT)
975 s->flags |= SN_ERR_CLITO;
976 else if (s->req->flags & BF_WRITE_ERROR)
977 s->flags |= SN_ERR_SRVCL;
978 else
979 s->flags |= SN_ERR_SRVTO;
980 sess_set_term_flags(s);
981 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200982 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
983 /* Report it if the server got an error or a read timeout expired */
984 s->rep->analysers = 0;
985 if (s->rep->flags & BF_READ_ERROR)
986 s->flags |= SN_ERR_SRVCL;
987 else if (s->rep->flags & BF_READ_TIMEOUT)
988 s->flags |= SN_ERR_SRVTO;
989 else if (s->rep->flags & BF_WRITE_ERROR)
990 s->flags |= SN_ERR_CLICL;
991 else
Willy Tarreau84455332009-03-15 22:34:05 +0100992 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200993 sess_set_term_flags(s);
994 }
Willy Tarreau84455332009-03-15 22:34:05 +0100995 }
996
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200997 /*
998 * Here we take care of forwarding unhandled data. This also includes
999 * connection establishments and shutdown requests.
1000 */
1001
1002
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001003 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001004 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001005 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001006 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001007 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001008 (s->req->prod->state >= SI_ST_EST) &&
1009 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001010 /* This buffer is freewheeling, there's no analyser nor hijacker
1011 * attached to it. If any data are left in, we'll permit them to
1012 * move.
1013 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001014 buffer_auto_connect(s->req);
1015 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001016 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001017
Willy Tarreau31971e52009-09-20 12:07:52 +02001018 /* If the producer is still connected, we'll enable data to flow
1019 * from the producer to the consumer (which might possibly not be
1020 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001021 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001022 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1023 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001024 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001025
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001026 /* check if it is wise to enable kernel splicing to forward request data */
1027 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1028 s->req->to_forward &&
1029 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001030 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001031 (pipes_used < global.maxpipes) &&
1032 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1033 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1034 (s->req->flags & BF_STREAMER_FAST)))) {
1035 s->req->flags |= BF_KERN_SPLICING;
1036 }
1037
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001038 /* reflect what the L7 analysers have seen last */
1039 rqf_last = s->req->flags;
1040
1041 /*
1042 * Now forward all shutdown requests between both sides of the buffer
1043 */
1044
Willy Tarreau520d95e2009-09-19 21:04:57 +02001045 /* first, let's check if the request buffer needs to shutdown(write), which may
1046 * happen either because the input is closed or because we want to force a close
1047 * once the server has begun to respond.
1048 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001049 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1050 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001051 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001052
1053 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001054 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055 s->req->cons->shutw(s->req->cons);
1056
1057 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001058 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1059 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001060 buffer_shutr_now(s->req);
1061
1062 /* shutdown(read) pending */
1063 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1064 s->req->prod->shutr(s->req->prod);
1065
Willy Tarreau520d95e2009-09-19 21:04:57 +02001066 /* it's possible that an upper layer has requested a connection setup or abort.
1067 * There are 2 situations where we decide to establish a new connection :
1068 * - there are data scheduled for emission in the buffer
1069 * - the BF_AUTO_CONNECT flag is set (active connection)
1070 */
1071 if (s->req->cons->state == SI_ST_INI) {
1072 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001073 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001074 /* If we have a ->connect method, we need to perform a connection request,
1075 * otherwise we immediately switch to the connected state.
1076 */
1077 if (s->req->cons->connect)
1078 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1079 else
1080 s->req->cons->state = SI_ST_EST; /* connection established */
1081 }
Willy Tarreau73201222009-08-16 18:27:24 +02001082 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001083 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001084 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001085 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1086 buffer_shutr_now(s->rep);
1087 }
Willy Tarreau92795622009-03-06 12:51:23 +01001088 }
1089
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001090
1091 /* we may have a pending connection request, or a connection waiting
1092 * for completion.
1093 */
1094 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1095 do {
1096 /* nb: step 1 might switch from QUE to ASS, but we first want
1097 * to give a chance to step 2 to perform a redirect if needed.
1098 */
1099 if (s->si[1].state != SI_ST_REQ)
1100 sess_update_stream_int(s, &s->si[1]);
1101 if (s->si[1].state == SI_ST_REQ)
1102 sess_prepare_conn_req(s, &s->si[1]);
1103
1104 if (s->si[1].state == SI_ST_ASS && s->srv &&
1105 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1106 perform_http_redirect(s, &s->si[1]);
1107 } while (s->si[1].state == SI_ST_ASS);
1108 }
1109
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001110 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001111 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001112 goto resync_stream_interface;
1113
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001114 /* otherwise wewant to check if we need to resync the req buffer or not */
1115 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001116 goto resync_request;
1117
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001118 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001119
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001120 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001121 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001122 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001123 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001124 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001125 (s->rep->prod->state >= SI_ST_EST) &&
1126 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001127 /* This buffer is freewheeling, there's no analyser nor hijacker
1128 * attached to it. If any data are left in, we'll permit them to
1129 * move.
1130 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001131 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001132 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001133 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1134 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001135 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001136
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001137 /* check if it is wise to enable kernel splicing to forward response data */
1138 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1139 s->rep->to_forward &&
1140 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001141 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001142 (pipes_used < global.maxpipes) &&
1143 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1144 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1145 (s->rep->flags & BF_STREAMER_FAST)))) {
1146 s->rep->flags |= BF_KERN_SPLICING;
1147 }
1148
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001149 /* reflect what the L7 analysers have seen last */
1150 rpf_last = s->rep->flags;
1151
1152 /*
1153 * Now forward all shutdown requests between both sides of the buffer
1154 */
1155
1156 /*
1157 * FIXME: this is probably where we should produce error responses.
1158 */
1159
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001160 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001161 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1162 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001163 buffer_shutw_now(s->rep);
1164
1165 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001166 if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001167 s->rep->cons->shutw(s->rep->cons);
1168
1169 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001170 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1171 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172 buffer_shutr_now(s->rep);
1173
1174 /* shutdown(read) pending */
1175 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1176 s->rep->prod->shutr(s->rep->prod);
1177
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001178 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001179 goto resync_stream_interface;
1180
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001181 if (s->req->flags != rqf_last)
1182 goto resync_request;
1183
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001184 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001185 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001186
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001187 /* we're interested in getting wakeups again */
1188 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1189 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1190
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001191 /* This is needed only when debugging is enabled, to indicate
1192 * client-side or server-side close. Please note that in the unlikely
1193 * event where both sides would close at once, the sequence is reported
1194 * on the server side first.
1195 */
1196 if (unlikely((global.mode & MODE_DEBUG) &&
1197 (!(global.mode & MODE_QUIET) ||
1198 (global.mode & MODE_VERBOSE)))) {
1199 int len;
1200
1201 if (s->si[1].state == SI_ST_CLO &&
1202 s->si[1].prev_state == SI_ST_EST) {
1203 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1204 s->uniq_id, s->be->id,
1205 (unsigned short)s->si[0].fd,
1206 (unsigned short)s->si[1].fd);
1207 write(1, trash, len);
1208 }
1209
1210 if (s->si[0].state == SI_ST_CLO &&
1211 s->si[0].prev_state == SI_ST_EST) {
1212 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1213 s->uniq_id, s->be->id,
1214 (unsigned short)s->si[0].fd,
1215 (unsigned short)s->si[1].fd);
1216 write(1, trash, len);
1217 }
1218 }
1219
1220 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1221 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1222
1223 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1224 session_process_counters(s);
1225
Willy Tarreau1accfc02009-09-05 20:57:35 +02001226 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001227 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001228
Willy Tarreau1accfc02009-09-05 20:57:35 +02001229 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001230 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001231
Willy Tarreauea388542009-06-21 21:45:58 +02001232 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1233 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001234 s->si[0].prev_state = s->si[0].state;
1235 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001236 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1237 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238
1239 /* Trick: if a request is being waiting for the server to respond,
1240 * and if we know the server can timeout, we don't want the timeout
1241 * to expire on the client side first, but we're still interested
1242 * in passing data from the client to the server (eg: POST). Thus,
1243 * we can cancel the client's request timeout if the server's
1244 * request timeout is set and the server has not yet sent a response.
1245 */
1246
Willy Tarreau520d95e2009-09-19 21:04:57 +02001247 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001248 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1249 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001250 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001251 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252
Willy Tarreau1accfc02009-09-05 20:57:35 +02001253 /* Call the second stream interface's I/O handler if it's embedded.
1254 * Note that this one may wake the task up again.
1255 */
1256 if (s->req->cons->iohandler) {
1257 s->req->cons->iohandler(s->req->cons);
1258 if (task_in_rq(t)) {
1259 /* If we woke up, we don't want to requeue the
1260 * task to the wait queue, but rather requeue
1261 * it into the runqueue ASAP.
1262 */
1263 t->expire = TICK_ETERNITY;
1264 return t;
1265 }
1266 }
1267
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001268 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1269 tick_first(s->rep->rex, s->rep->wex));
1270 if (s->req->analysers)
1271 t->expire = tick_first(t->expire, s->req->analyse_exp);
1272
1273 if (s->si[0].exp)
1274 t->expire = tick_first(t->expire, s->si[0].exp);
1275
1276 if (s->si[1].exp)
1277 t->expire = tick_first(t->expire, s->si[1].exp);
1278
1279#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001280 fprintf(stderr,
1281 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1282 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1283 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1284 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 +01001285#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001286
1287#ifdef DEBUG_DEV
1288 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001289 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001290 ABORT_NOW();
1291#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001292 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001293 }
1294
1295 s->fe->feconn--;
1296 if (s->flags & SN_BE_ASSIGNED)
1297 s->be->beconn--;
1298 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001299 s->listener->nbconn--;
1300 if (s->listener->state == LI_FULL &&
1301 s->listener->nbconn < s->listener->maxconn) {
1302 /* we should reactivate the listener */
1303 EV_FD_SET(s->listener->fd, DIR_RD);
1304 s->listener->state = LI_READY;
1305 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001306
1307 if (unlikely((global.mode & MODE_DEBUG) &&
1308 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1309 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001310 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001311 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001312 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001313 write(1, trash, len);
1314 }
1315
1316 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1317 session_process_counters(s);
1318
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001319 if (s->txn.status) {
1320 int n;
1321
1322 n = s->txn.status / 100;
1323 if (n < 1 || n > 5)
1324 n = 0;
1325
1326 if (s->fe->mode == PR_MODE_HTTP)
1327 s->fe->counters.p.http.rsp[n]++;
1328
1329 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
1330 (s->be->mode == PR_MODE_HTTP))
1331 s->be->counters.p.http.rsp[n]++;
1332 }
1333
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001334 /* let's do a final log if we need it */
1335 if (s->logs.logwait &&
1336 !(s->flags & SN_MONITOR) &&
1337 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001338 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001339 }
1340
1341 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001342 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001343 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001344 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001345 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001346}
1347
Willy Tarreau7c669d72008-06-20 15:04:11 +02001348/*
1349 * This function adjusts sess->srv_conn and maintains the previous and new
1350 * server's served session counts. Setting newsrv to NULL is enough to release
1351 * current connection slot. This function also notifies any LB algo which might
1352 * expect to be informed about any change in the number of active sessions on a
1353 * server.
1354 */
1355void sess_change_server(struct session *sess, struct server *newsrv)
1356{
1357 if (sess->srv_conn == newsrv)
1358 return;
1359
1360 if (sess->srv_conn) {
1361 sess->srv_conn->served--;
1362 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1363 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1364 sess->srv_conn = NULL;
1365 }
1366
1367 if (newsrv) {
1368 newsrv->served++;
1369 if (newsrv->proxy->lbprm.server_take_conn)
1370 newsrv->proxy->lbprm.server_take_conn(newsrv);
1371 sess->srv_conn = newsrv;
1372 }
1373}
1374
Willy Tarreau84455332009-03-15 22:34:05 +01001375/* Set correct session termination flags in case no analyser has done it. It
1376 * also counts a failed request if the server state has not reached the request
1377 * stage.
1378 */
1379void sess_set_term_flags(struct session *s)
1380{
1381 if (!(s->flags & SN_FINST_MASK)) {
1382 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001383
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001384 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001385 if (s->listener->counters)
1386 s->listener->counters->failed_req++;
1387
Willy Tarreau84455332009-03-15 22:34:05 +01001388 s->flags |= SN_FINST_R;
1389 }
1390 else if (s->si[1].state == SI_ST_QUE)
1391 s->flags |= SN_FINST_Q;
1392 else if (s->si[1].state < SI_ST_EST)
1393 s->flags |= SN_FINST_C;
1394 else if (s->si[1].state == SI_ST_EST)
1395 s->flags |= SN_FINST_D;
1396 else
1397 s->flags |= SN_FINST_L;
1398 }
1399}
1400
1401/* Handle server-side errors for default protocols. It is called whenever a a
1402 * connection setup is aborted or a request is aborted in queue. It sets the
1403 * session termination flags so that the caller does not have to worry about
1404 * them. It's installed as ->srv_error for the server-side stream_interface.
1405 */
1406void default_srv_error(struct session *s, struct stream_interface *si)
1407{
1408 int err_type = si->err_type;
1409 int err = 0, fin = 0;
1410
1411 if (err_type & SI_ET_QUEUE_ABRT) {
1412 err = SN_ERR_CLICL;
1413 fin = SN_FINST_Q;
1414 }
1415 else if (err_type & SI_ET_CONN_ABRT) {
1416 err = SN_ERR_CLICL;
1417 fin = SN_FINST_C;
1418 }
1419 else if (err_type & SI_ET_QUEUE_TO) {
1420 err = SN_ERR_SRVTO;
1421 fin = SN_FINST_Q;
1422 }
1423 else if (err_type & SI_ET_QUEUE_ERR) {
1424 err = SN_ERR_SRVCL;
1425 fin = SN_FINST_Q;
1426 }
1427 else if (err_type & SI_ET_CONN_TO) {
1428 err = SN_ERR_SRVTO;
1429 fin = SN_FINST_C;
1430 }
1431 else if (err_type & SI_ET_CONN_ERR) {
1432 err = SN_ERR_SRVCL;
1433 fin = SN_FINST_C;
1434 }
1435 else /* SI_ET_CONN_OTHER and others */ {
1436 err = SN_ERR_INTERNAL;
1437 fin = SN_FINST_C;
1438 }
1439
1440 if (!(s->flags & SN_ERR_MASK))
1441 s->flags |= err;
1442 if (!(s->flags & SN_FINST_MASK))
1443 s->flags |= fin;
1444}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001445
Willy Tarreaubaaee002006-06-26 02:48:02 +02001446/*
1447 * Local variables:
1448 * c-indent-level: 8
1449 * c-basic-offset: 8
1450 * End:
1451 */