blob: 65e22f601d0c0c10caf1493903db38310a7458db [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
Willy Tarreau46023632010-01-07 22:51:47 +010081 http_end_txn(s);
82
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020084 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +010085 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
86 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020087 }
Willy Tarreau0937bc42009-12-22 15:03:09 +010088
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010089 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +010090 /* we have to unlink all watchers. We must not relink them if
91 * this session was the last one in the list.
92 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010093 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +010094 LIST_INIT(&bref->users);
95 if (s->list.n != &sessions)
96 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010097 bref->ref = s->list.n;
98 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010099 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200100 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200101
102 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200103 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200104 pool_flush2(pool2_buffer);
105 pool_flush2(fe->hdr_idx_pool);
106 pool_flush2(pool2_requri);
107 pool_flush2(pool2_capture);
108 pool_flush2(pool2_session);
109 pool_flush2(fe->req_cap_pool);
110 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200111 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200112}
113
114
115/* perform minimal intializations, report 0 in case of error, 1 if OK. */
116int init_session()
117{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100118 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200119 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
120 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121}
122
Willy Tarreau30e71012007-11-26 20:15:35 +0100123void session_process_counters(struct session *s)
124{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100125 unsigned long long bytes;
126
Willy Tarreau30e71012007-11-26 20:15:35 +0100127 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100128 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 s->logs.bytes_in = s->req->total;
130 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200131 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100132
Willy Tarreau30e71012007-11-26 20:15:35 +0100133 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200134 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100135
Willy Tarreau30e71012007-11-26 20:15:35 +0100136 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200137 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200138
139 if (s->listener->counters)
140 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100141 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142 }
143
Willy Tarreau30e71012007-11-26 20:15:35 +0100144 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100145 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100146 s->logs.bytes_out = s->rep->total;
147 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200148 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149
Willy Tarreau30e71012007-11-26 20:15:35 +0100150 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200151 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100152
Willy Tarreau30e71012007-11-26 20:15:35 +0100153 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200154 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200155
156 if (s->listener->counters)
157 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100158 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100159 }
160}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100162/* This function is called with (si->state == SI_ST_CON) meaning that a
163 * connection was attempted and that the file descriptor is already allocated.
164 * We must check for establishment, error and abort. Possible output states
165 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
166 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
167 * otherwise 1.
168 */
169int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
170{
171 struct buffer *req = si->ob;
172 struct buffer *rep = si->ib;
173
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100174 /* If we got an error, or if nothing happened and the connection timed
175 * out, we must give up. The CER state handler will take care of retry
176 * attempts and error reports.
177 */
178 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100179 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100180 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200181 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100182 fd_delete(si->fd);
183
184 if (si->err_type)
185 return 0;
186
187 si->err_loc = s->srv;
188 if (si->flags & SI_FL_ERR)
189 si->err_type = SI_ET_CONN_ERR;
190 else
191 si->err_type = SI_ET_CONN_TO;
192 return 0;
193 }
194
195 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200196 if (unlikely((rep->flags & BF_SHUTW) ||
197 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200198 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100199 s->be->options & PR_O_ABRT_CLOSE)))) {
200 /* give up */
201 si->shutw(si);
202 si->err_type |= SI_ET_CONN_ABRT;
203 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200204 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100205 if (s->srv_error)
206 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100207 return 1;
208 }
209
210 /* we need to wait a bit more if there was no activity either */
211 if (!(req->flags & BF_WRITE_ACTIVITY))
212 return 1;
213
214 /* OK, this means that a connection succeeded. The caller will be
215 * responsible for handling the transition from CON to EST.
216 */
217 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100218 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100219 si->state = SI_ST_EST;
220 si->err_type = SI_ET_NONE;
221 si->err_loc = NULL;
222 return 1;
223}
224
225/* This function is called with (si->state == SI_ST_CER) meaning that a
226 * previous connection attempt has failed and that the file descriptor
227 * has already been released. Possible causes include asynchronous error
228 * notification and time out. Possible output states are SI_ST_CLO when
229 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
230 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
231 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
232 * marked as in error state. It returns 0.
233 */
234int sess_update_st_cer(struct session *s, struct stream_interface *si)
235{
236 /* we probably have to release last session from the server */
237 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100238 health_adjust(s->srv, HANA_STATUS_L4_ERR);
239
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100240 if (s->flags & SN_CURR_SESS) {
241 s->flags &= ~SN_CURR_SESS;
242 s->srv->cur_sess--;
243 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100244 }
245
246 /* ensure that we have enough retries left */
247 s->conn_retries--;
248 if (s->conn_retries < 0) {
249 if (!si->err_type) {
250 si->err_type = SI_ET_CONN_ERR;
251 si->err_loc = s->srv;
252 }
253
254 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200255 s->srv->counters.failed_conns++;
256 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100257 if (may_dequeue_tasks(s->srv, s->be))
258 process_srv_queue(s->srv);
259
260 /* shutw is enough so stop a connecting socket */
261 si->shutw(si);
262 si->ob->flags |= BF_WRITE_ERROR;
263 si->ib->flags |= BF_READ_ERROR;
264
265 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100266 if (s->srv_error)
267 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100268 return 0;
269 }
270
271 /* If the "redispatch" option is set on the backend, we are allowed to
272 * retry on another server for the last retry. In order to achieve this,
273 * we must mark the session unassigned, and eventually clear the DIRECT
274 * bit to ignore any persistence cookie. We won't count a retry nor a
275 * redispatch yet, because this will depend on what server is selected.
276 */
277 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
278 if (may_dequeue_tasks(s->srv, s->be))
279 process_srv_queue(s->srv);
280
281 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
282 s->prev_srv = s->srv;
283 si->state = SI_ST_REQ;
284 } else {
285 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200286 s->srv->counters.retries++;
287 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100288 si->state = SI_ST_ASS;
289 }
290
291 if (si->flags & SI_FL_ERR) {
292 /* The error was an asynchronous connection error, and we will
293 * likely have to retry connecting to the same server, most
294 * likely leading to the same result. To avoid this, we wait
295 * one second before retrying.
296 */
297
298 if (!si->err_type)
299 si->err_type = SI_ET_CONN_ERR;
300
301 si->state = SI_ST_TAR;
302 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
303 return 0;
304 }
305 return 0;
306}
307
308/*
309 * This function handles the transition between the SI_ST_CON state and the
310 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
311 * SI_ST_EST.
312 */
313void sess_establish(struct session *s, struct stream_interface *si)
314{
315 struct buffer *req = si->ob;
316 struct buffer *rep = si->ib;
317
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100318 if (s->srv)
319 health_adjust(s->srv, HANA_STATUS_L4_OK);
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 /* if the user wants to log as soon as possible, without counting
323 * bytes from the server, then this is the right moment. */
324 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
325 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100326 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100327 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100328 }
329 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100330 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
331 /* reset hdr_idx which was already initialized by the request.
332 * right now, the http parser does it.
333 * hdr_idx_init(&s->txn.hdr_idx);
334 */
335 }
336
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200337 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100338 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
339 req->wex = TICK_ETERNITY;
340}
341
342/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
343 * Other input states are simply ignored.
344 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
345 * Flags must have previously been updated for timeouts and other conditions.
346 */
347void sess_update_stream_int(struct session *s, struct stream_interface *si)
348{
349 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",
350 now_ms, __FUNCTION__,
351 s,
352 s->req, s->rep,
353 s->req->rex, s->rep->wex,
354 s->req->flags, s->rep->flags,
355 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
356
357 if (si->state == SI_ST_ASS) {
358 /* Server assigned to connection request, we have to try to connect now */
359 int conn_err;
360
361 conn_err = connect_server(s);
362 if (conn_err == SN_ERR_NONE) {
363 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100364 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100365 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100366 return;
367 }
368
369 /* We have received a synchronous error. We might have to
370 * abort, retry immediately or redispatch.
371 */
372 if (conn_err == SN_ERR_INTERNAL) {
373 if (!si->err_type) {
374 si->err_type = SI_ET_CONN_OTHER;
375 si->err_loc = s->srv;
376 }
377
378 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 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200381 s->srv->counters.failed_conns++;
382 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100383
384 /* release other sessions waiting for this server */
385 if (may_dequeue_tasks(s->srv, s->be))
386 process_srv_queue(s->srv);
387
388 /* Failed and not retryable. */
389 si->shutr(si);
390 si->shutw(si);
391 si->ob->flags |= BF_WRITE_ERROR;
392
393 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
394
395 /* no session was ever accounted for this server */
396 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100397 if (s->srv_error)
398 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100399 return;
400 }
401
402 /* We are facing a retryable error, but we don't want to run a
403 * turn-around now, as the problem is likely a source port
404 * allocation problem, so we want to retry now.
405 */
406 si->state = SI_ST_CER;
407 si->flags &= ~SI_FL_ERR;
408 sess_update_st_cer(s, si);
409 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
410 return;
411 }
412 else if (si->state == SI_ST_QUE) {
413 /* connection request was queued, check for any update */
414 if (!s->pend_pos) {
415 /* The connection is not in the queue anymore. Either
416 * we have a server connection slot available and we
417 * go directly to the assigned state, or we need to
418 * load-balance first and go to the INI state.
419 */
420 si->exp = TICK_ETERNITY;
421 if (unlikely(!(s->flags & SN_ASSIGNED)))
422 si->state = SI_ST_REQ;
423 else {
424 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
425 si->state = SI_ST_ASS;
426 }
427 return;
428 }
429
430 /* Connection request still in queue... */
431 if (si->flags & SI_FL_EXP) {
432 /* ... and timeout expired */
433 si->exp = TICK_ETERNITY;
434 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
435 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200436 s->srv->counters.failed_conns++;
437 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100438 si->shutr(si);
439 si->shutw(si);
440 si->ob->flags |= BF_WRITE_TIMEOUT;
441 if (!si->err_type)
442 si->err_type = SI_ET_QUEUE_TO;
443 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100444 if (s->srv_error)
445 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100446 return;
447 }
448
449 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200450 if ((si->ob->flags & (BF_READ_ERROR)) ||
451 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200452 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100453 /* give up */
454 si->exp = TICK_ETERNITY;
455 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
456 si->shutr(si);
457 si->shutw(si);
458 si->err_type |= SI_ET_QUEUE_ABRT;
459 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100460 if (s->srv_error)
461 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100462 return;
463 }
464
465 /* Nothing changed */
466 return;
467 }
468 else if (si->state == SI_ST_TAR) {
469 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200470 if ((si->ob->flags & (BF_READ_ERROR)) ||
471 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200472 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100473 /* give up */
474 si->exp = TICK_ETERNITY;
475 si->shutr(si);
476 si->shutw(si);
477 si->err_type |= SI_ET_CONN_ABRT;
478 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100479 if (s->srv_error)
480 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100481 return;
482 }
483
484 if (!(si->flags & SI_FL_EXP))
485 return; /* still in turn-around */
486
487 si->exp = TICK_ETERNITY;
488
489 /* we keep trying on the same server as long as the session is
490 * marked "assigned".
491 * FIXME: Should we force a redispatch attempt when the server is down ?
492 */
493 if (s->flags & SN_ASSIGNED)
494 si->state = SI_ST_ASS;
495 else
496 si->state = SI_ST_REQ;
497 return;
498 }
499}
500
501/* This function initiates a server connection request on a stream interface
502 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
503 * indicating that a server has been assigned. It may also return SI_ST_QUE,
504 * or SI_ST_CLO upon error.
505 */
506static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
507 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",
508 now_ms, __FUNCTION__,
509 s,
510 s->req, s->rep,
511 s->req->rex, s->rep->wex,
512 s->req->flags, s->rep->flags,
513 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
514
515 if (si->state != SI_ST_REQ)
516 return;
517
518 /* Try to assign a server */
519 if (srv_redispatch_connect(s) != 0) {
520 /* We did not get a server. Either we queued the
521 * connection request, or we encountered an error.
522 */
523 if (si->state == SI_ST_QUE)
524 return;
525
526 /* we did not get any server, let's check the cause */
527 si->shutr(si);
528 si->shutw(si);
529 si->ob->flags |= BF_WRITE_ERROR;
530 if (!si->err_type)
531 si->err_type = SI_ET_CONN_OTHER;
532 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100533 if (s->srv_error)
534 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 return;
536 }
537
538 /* The server is assigned */
539 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
540 si->state = SI_ST_ASS;
541}
542
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200543/* This stream analyser checks the switching rules and changes the backend
544 * if appropriate. The default_backend rule is also considered.
545 * It returns 1 if the processing can continue on next analysers, or zero if it
546 * either needs more data or wants to immediately abort the request.
547 */
548int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
549{
550 req->analysers &= ~an_bit;
551 req->analyse_exp = TICK_ETERNITY;
552
553 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
554 now_ms, __FUNCTION__,
555 s,
556 req,
557 req->rex, req->wex,
558 req->flags,
559 req->l,
560 req->analysers);
561
562 /* now check whether we have some switching rules for this request */
563 if (!(s->flags & SN_BE_ASSIGNED)) {
564 struct switching_rule *rule;
565
566 list_for_each_entry(rule, &s->fe->switching_rules, list) {
567 int ret;
568
569 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
570 ret = acl_pass(ret);
571 if (rule->cond->pol == ACL_COND_UNLESS)
572 ret = !ret;
573
574 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200575 if (!session_set_backend(s, rule->be.backend))
576 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200577 break;
578 }
579 }
580
581 /* To ensure correct connection accounting on the backend, we
582 * have to assign one if it was not set (eg: a listen). This
583 * measure also takes care of correctly setting the default
584 * backend if any.
585 */
586 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200587 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
588 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200589 }
590
591 /* we don't want to run the HTTP filters again if the backend has not changed */
592 if (s->fe == s->be)
593 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
594
595 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200596
597 sw_failed:
598 /* immediately abort this request in case of allocation failure */
599 buffer_abort(s->req);
600 buffer_abort(s->rep);
601
602 if (!(s->flags & SN_ERR_MASK))
603 s->flags |= SN_ERR_RESOURCE;
604 if (!(s->flags & SN_FINST_MASK))
605 s->flags |= SN_FINST_R;
606
607 s->txn.status = 500;
608 s->req->analysers = 0;
609 s->req->analyse_exp = TICK_ETERNITY;
610 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200611}
612
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100613/* This macro is very specific to the function below. See the comments in
614 * process_session() below to understand the logic and the tests.
615 */
616#define UPDATE_ANALYSERS(real, list, back, flag) { \
617 list = (((list) & ~(flag)) | ~(back)) & (real); \
618 back = real; \
619 if (!(list)) \
620 break; \
621 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
622 continue; \
623}
624
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625/* Processes the client, server, request and response jobs of a session task,
626 * then puts it back to the wait queue in a clean state, or cleans up its
627 * resources if it must be deleted. Returns in <next> the date the task wants
628 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
629 * nothing too many times, the request and response buffers flags are monitored
630 * and each function is called only if at least another function has changed at
631 * least one flag it is interested in.
632 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100633struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634{
635 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +0100637 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638
639 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
640 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
641
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200642 /* This flag must explicitly be set every time */
643 s->req->flags &= ~BF_READ_NOEXP;
644
645 /* Keep a copy of req/rep flags so that we can detect shutdowns */
646 rqf_last = s->req->flags;
647 rpf_last = s->rep->flags;
648
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200649 /* we don't want the stream interface functions to recursively wake us up */
650 if (s->req->prod->owner == t)
651 s->req->prod->flags |= SI_FL_DONT_WAKE;
652 if (s->req->cons->owner == t)
653 s->req->cons->flags |= SI_FL_DONT_WAKE;
654
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655 /* 1a: Check for low level timeouts if needed. We just set a flag on
656 * stream interfaces when their timeouts have expired.
657 */
658 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
659 stream_int_check_timeouts(&s->si[0]);
660 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200661
662 /* check buffer timeouts, and close the corresponding stream interfaces
663 * for future reads or writes. Note: this will also concern upper layers
664 * but we do not touch any other flag. We must be careful and correctly
665 * detect state changes when calling them.
666 */
667
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100668 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200669
Willy Tarreau14641402009-12-29 14:49:56 +0100670 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
671 s->req->cons->flags |= SI_FL_NOLINGER;
672 s->req->cons->shutw(s->req->cons);
673 }
674
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200675 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
676 s->req->prod->shutr(s->req->prod);
677
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679
Willy Tarreau14641402009-12-29 14:49:56 +0100680 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
681 s->rep->cons->flags |= SI_FL_NOLINGER;
682 s->rep->cons->shutw(s->rep->cons);
683 }
684
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200685 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
686 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200687 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688
689 /* 1b: check for low-level errors reported at the stream interface.
690 * First we check if it's a retryable error (in which case we don't
691 * want to tell the buffer). Otherwise we report the error one level
692 * upper by setting flags into the buffers. Note that the side towards
693 * the client cannot have connect (hence retryable) errors. Also, the
694 * connection setup code must be able to deal with any type of abort.
695 */
696 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
697 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
698 s->si[0].shutr(&s->si[0]);
699 s->si[0].shutw(&s->si[0]);
700 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100701 if (!(s->req->analysers) && !(s->rep->analysers)) {
702 if (!(s->flags & SN_ERR_MASK))
703 s->flags |= SN_ERR_CLICL;
704 if (!(s->flags & SN_FINST_MASK))
705 s->flags |= SN_FINST_D;
706 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100707 }
708 }
709
710 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
711 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
712 s->si[1].shutr(&s->si[1]);
713 s->si[1].shutw(&s->si[1]);
714 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200715 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100716 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200717 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100718 if (!(s->req->analysers) && !(s->rep->analysers)) {
719 if (!(s->flags & SN_ERR_MASK))
720 s->flags |= SN_ERR_SRVCL;
721 if (!(s->flags & SN_FINST_MASK))
722 s->flags |= SN_FINST_D;
723 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724 }
725 /* note: maybe we should process connection errors here ? */
726 }
727
728 if (s->si[1].state == SI_ST_CON) {
729 /* we were trying to establish a connection on the server side,
730 * maybe it succeeded, maybe it failed, maybe we timed out, ...
731 */
732 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
733 sess_update_st_cer(s, &s->si[1]);
734 else if (s->si[1].state == SI_ST_EST)
735 sess_establish(s, &s->si[1]);
736
737 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
738 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
739 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
740 */
741 }
742
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200743resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 /* Check for connection closure */
745
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746 DPRINTF(stderr,
747 "[%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",
748 now_ms, __FUNCTION__, __LINE__,
749 t,
750 s, s->flags,
751 s->req, s->rep,
752 s->req->rex, s->rep->wex,
753 s->req->flags, s->rep->flags,
754 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
755 s->rep->cons->err_type, s->req->cons->err_type,
756 s->conn_retries);
757
758 /* nothing special to be done on client side */
759 if (unlikely(s->req->prod->state == SI_ST_DIS))
760 s->req->prod->state = SI_ST_CLO;
761
762 /* When a server-side connection is released, we have to count it and
763 * check for pending connections on this server.
764 */
765 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
766 s->req->cons->state = SI_ST_CLO;
767 if (s->srv) {
768 if (s->flags & SN_CURR_SESS) {
769 s->flags &= ~SN_CURR_SESS;
770 s->srv->cur_sess--;
771 }
772 sess_change_server(s, NULL);
773 if (may_dequeue_tasks(s->srv, s->be))
774 process_srv_queue(s->srv);
775 }
776 }
777
778 /*
779 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
780 * at this point.
781 */
782
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100783 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784 /* Analyse request */
785 if ((s->req->flags & BF_MASK_ANALYSER) ||
786 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
787 unsigned int flags = s->req->flags;
788
789 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +0100790 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100791 unsigned int ana_list;
792 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200793
Willy Tarreau90deb182010-01-07 00:20:41 +0100794 /* it's up to the analysers to stop new connections,
795 * disable reading or closing. Note: if an analyser
796 * disables any of these bits, it is responsible for
797 * enabling them again when it disables itself, so
798 * that other analysers are called in similar conditions.
799 */
800 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200801 buffer_auto_connect(s->req);
802 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803
804 /* We will call all analysers for which a bit is set in
805 * s->req->analysers, following the bit order from LSB
806 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200807 * the list when not needed. Any analyser may return 0
808 * to break out of the loop, either because of missing
809 * data to take a decision, or because it decides to
810 * kill the session. We loop at least once through each
811 * analyser, and we may loop again if other analysers
812 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100813 *
814 * We build a list of analysers to run. We evaluate all
815 * of these analysers in the order of the lower bit to
816 * the higher bit. This ordering is very important.
817 * An analyser will often add/remove other analysers,
818 * including itself. Any changes to itself have no effect
819 * on the loop. If it removes any other analysers, we
820 * want those analysers not to be called anymore during
821 * this loop. If it adds an analyser that is located
822 * after itself, we want it to be scheduled for being
823 * processed during the loop. If it adds an analyser
824 * which is located before it, we want it to switch to
825 * it immediately, even if it has already been called
826 * once but removed since.
827 *
828 * In order to achieve this, we compare the analyser
829 * list after the call with a copy of it before the
830 * call. The work list is fed with analyser bits that
831 * appeared during the call. Then we compare previous
832 * work list with the new one, and check the bits that
833 * appeared. If the lowest of these bits is lower than
834 * the current bit, it means we have enabled a previous
835 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100836 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100837
838 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +0100839 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100840 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200841
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100842 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +0200843 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100844 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100845 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200846 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100847
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100848 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +0200849 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +0200850 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100851 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +0200852 }
853
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100854 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200855 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
856 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100857 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200858 }
859
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100860 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200861 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
862 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100863 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200864 }
865
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100866 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200867 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
868 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100869 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200870 }
871
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100872 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +0200873 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +0100874 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100875 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200876 }
Willy Tarreau60b85b02008-11-30 23:28:40 +0100877
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100878 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +0200879 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
880 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100881 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +0200882 }
883
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100884 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +0200885 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +0100886 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100887 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200888 }
Emeric Brun647caf12009-06-30 17:57:00 +0200889
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100890 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +0200891 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
892 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100893 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +0200894 }
Willy Tarreaud98cf932009-12-27 22:54:55 +0100895
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100896 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +0100897 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
898 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100899 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +0100900 }
Willy Tarreaue34070e2010-01-08 00:32:27 +0100901 break;
902 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 }
Willy Tarreau84455332009-03-15 22:34:05 +0100904
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200905 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
906 rqf_last = s->req->flags;
907 goto resync_request;
908 }
909 }
910
Willy Tarreau576507f2010-01-07 00:09:04 +0100911 /* we'll monitor the request analysers while parsing the response,
912 * because some response analysers may indirectly enable new request
913 * analysers (eg: HTTP keep-alive).
914 */
915 req_ana_back = s->req->analysers;
916
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200917 resync_response:
918 /* Analyse response */
919
920 if (unlikely(s->rep->flags & BF_HIJACK)) {
921 /* In inject mode, we wake up everytime something has
922 * happened on the write side of the buffer.
923 */
924 unsigned int flags = s->rep->flags;
925
926 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
927 !(s->rep->flags & BF_FULL)) {
928 s->rep->hijacker(s, s->rep);
929 }
930
931 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
932 rpf_last = s->rep->flags;
933 goto resync_response;
934 }
935 }
936 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
937 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
938 unsigned int flags = s->rep->flags;
939
940 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +0100941 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100942 unsigned int ana_list;
943 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200944
Willy Tarreau90deb182010-01-07 00:20:41 +0100945 /* it's up to the analysers to stop disable reading or
946 * closing. Note: if an analyser disables any of these
947 * bits, it is responsible for enabling them again when
948 * it disables itself, so that other analysers are called
949 * in similar conditions.
950 */
951 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200952 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200953
954 /* We will call all analysers for which a bit is set in
955 * s->rep->analysers, following the bit order from LSB
956 * to MSB. The analysers must remove themselves from
957 * the list when not needed. Any analyser may return 0
958 * to break out of the loop, either because of missing
959 * data to take a decision, or because it decides to
960 * kill the session. We loop at least once through each
961 * analyser, and we may loop again if other analysers
962 * are added in the middle.
963 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100964
965 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +0100966 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100967 if (!ana_list)
968 break;
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200969
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100970 /* Warning! ensure that analysers are always placed in ascending order! */
971
972 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200973 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
974 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100975 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200976 }
977
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100978 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200979 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
980 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100981 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +0200982 }
Willy Tarreaud98cf932009-12-27 22:54:55 +0100983
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100984 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +0100985 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
986 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100987 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +0100988 }
Willy Tarreaue34070e2010-01-08 00:32:27 +0100989 break;
990 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +0200991 }
992
993 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
994 rpf_last = s->rep->flags;
995 goto resync_response;
996 }
997 }
998
Willy Tarreau576507f2010-01-07 00:09:04 +0100999 /* maybe someone has added some request analysers, so we must check and loop */
1000 if (s->req->analysers & ~req_ana_back)
1001 goto resync_request;
1002
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001003 /* FIXME: here we should call protocol handlers which rely on
1004 * both buffers.
1005 */
1006
1007
1008 /*
1009 * Now we propagate unhandled errors to the session
1010 */
1011 if (!(s->flags & SN_ERR_MASK)) {
1012 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1013 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001014 s->req->analysers = 0;
1015 if (s->req->flags & BF_READ_ERROR)
1016 s->flags |= SN_ERR_CLICL;
1017 else if (s->req->flags & BF_READ_TIMEOUT)
1018 s->flags |= SN_ERR_CLITO;
1019 else if (s->req->flags & BF_WRITE_ERROR)
1020 s->flags |= SN_ERR_SRVCL;
1021 else
1022 s->flags |= SN_ERR_SRVTO;
1023 sess_set_term_flags(s);
1024 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001025 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1026 /* Report it if the server got an error or a read timeout expired */
1027 s->rep->analysers = 0;
1028 if (s->rep->flags & BF_READ_ERROR)
1029 s->flags |= SN_ERR_SRVCL;
1030 else if (s->rep->flags & BF_READ_TIMEOUT)
1031 s->flags |= SN_ERR_SRVTO;
1032 else if (s->rep->flags & BF_WRITE_ERROR)
1033 s->flags |= SN_ERR_CLICL;
1034 else
Willy Tarreau84455332009-03-15 22:34:05 +01001035 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001036 sess_set_term_flags(s);
1037 }
Willy Tarreau84455332009-03-15 22:34:05 +01001038 }
1039
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001040 /*
1041 * Here we take care of forwarding unhandled data. This also includes
1042 * connection establishments and shutdown requests.
1043 */
1044
1045
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001046 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001047 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001048 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001049 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001050 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001051 (s->req->prod->state >= SI_ST_EST) &&
1052 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001053 /* This buffer is freewheeling, there's no analyser nor hijacker
1054 * attached to it. If any data are left in, we'll permit them to
1055 * move.
1056 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001057 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001058 buffer_auto_connect(s->req);
1059 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001060 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001061
Willy Tarreau31971e52009-09-20 12:07:52 +02001062 /* If the producer is still connected, we'll enable data to flow
1063 * from the producer to the consumer (which might possibly not be
1064 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001065 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001066 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1067 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001068 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001069
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001070 /* check if it is wise to enable kernel splicing to forward request data */
1071 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1072 s->req->to_forward &&
1073 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001074 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001075 (pipes_used < global.maxpipes) &&
1076 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1077 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1078 (s->req->flags & BF_STREAMER_FAST)))) {
1079 s->req->flags |= BF_KERN_SPLICING;
1080 }
1081
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001082 /* reflect what the L7 analysers have seen last */
1083 rqf_last = s->req->flags;
1084
1085 /*
1086 * Now forward all shutdown requests between both sides of the buffer
1087 */
1088
Willy Tarreau520d95e2009-09-19 21:04:57 +02001089 /* first, let's check if the request buffer needs to shutdown(write), which may
1090 * happen either because the input is closed or because we want to force a close
1091 * once the server has begun to respond.
1092 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001093 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1094 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001095 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001096
1097 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001098 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 +01001099 s->req->cons->shutw(s->req->cons);
1100
1101 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001102 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1103 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001104 buffer_shutr_now(s->req);
1105
1106 /* shutdown(read) pending */
1107 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1108 s->req->prod->shutr(s->req->prod);
1109
Willy Tarreau520d95e2009-09-19 21:04:57 +02001110 /* it's possible that an upper layer has requested a connection setup or abort.
1111 * There are 2 situations where we decide to establish a new connection :
1112 * - there are data scheduled for emission in the buffer
1113 * - the BF_AUTO_CONNECT flag is set (active connection)
1114 */
1115 if (s->req->cons->state == SI_ST_INI) {
1116 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001117 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001118 /* If we have a ->connect method, we need to perform a connection request,
1119 * otherwise we immediately switch to the connected state.
1120 */
1121 if (s->req->cons->connect)
1122 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1123 else
1124 s->req->cons->state = SI_ST_EST; /* connection established */
1125 }
Willy Tarreau73201222009-08-16 18:27:24 +02001126 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001127 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001128 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001129 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1130 buffer_shutr_now(s->rep);
1131 }
Willy Tarreau92795622009-03-06 12:51:23 +01001132 }
1133
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134
1135 /* we may have a pending connection request, or a connection waiting
1136 * for completion.
1137 */
1138 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1139 do {
1140 /* nb: step 1 might switch from QUE to ASS, but we first want
1141 * to give a chance to step 2 to perform a redirect if needed.
1142 */
1143 if (s->si[1].state != SI_ST_REQ)
1144 sess_update_stream_int(s, &s->si[1]);
1145 if (s->si[1].state == SI_ST_REQ)
1146 sess_prepare_conn_req(s, &s->si[1]);
1147
1148 if (s->si[1].state == SI_ST_ASS && s->srv &&
1149 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1150 perform_http_redirect(s, &s->si[1]);
1151 } while (s->si[1].state == SI_ST_ASS);
1152 }
1153
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001154 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001155 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001156 goto resync_stream_interface;
1157
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001158 /* otherwise wewant to check if we need to resync the req buffer or not */
1159 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001160 goto resync_request;
1161
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001162 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001163
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001164 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001165 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001166 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001167 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001168 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001169 (s->rep->prod->state >= SI_ST_EST) &&
1170 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001171 /* This buffer is freewheeling, there's no analyser nor hijacker
1172 * attached to it. If any data are left in, we'll permit them to
1173 * move.
1174 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001175 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001176 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001177 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001178 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1179 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001180 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001181
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001182 /* check if it is wise to enable kernel splicing to forward response data */
1183 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1184 s->rep->to_forward &&
1185 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001186 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001187 (pipes_used < global.maxpipes) &&
1188 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1189 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1190 (s->rep->flags & BF_STREAMER_FAST)))) {
1191 s->rep->flags |= BF_KERN_SPLICING;
1192 }
1193
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001194 /* reflect what the L7 analysers have seen last */
1195 rpf_last = s->rep->flags;
1196
1197 /*
1198 * Now forward all shutdown requests between both sides of the buffer
1199 */
1200
1201 /*
1202 * FIXME: this is probably where we should produce error responses.
1203 */
1204
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001205 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001206 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1207 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001208 buffer_shutw_now(s->rep);
1209
1210 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001211 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 +01001212 s->rep->cons->shutw(s->rep->cons);
1213
1214 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001215 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1216 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217 buffer_shutr_now(s->rep);
1218
1219 /* shutdown(read) pending */
1220 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1221 s->rep->prod->shutr(s->rep->prod);
1222
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001223 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001224 goto resync_stream_interface;
1225
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001226 if (s->req->flags != rqf_last)
1227 goto resync_request;
1228
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001229 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001230 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001231
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001232 /* we're interested in getting wakeups again */
1233 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1234 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1235
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001236 /* This is needed only when debugging is enabled, to indicate
1237 * client-side or server-side close. Please note that in the unlikely
1238 * event where both sides would close at once, the sequence is reported
1239 * on the server side first.
1240 */
1241 if (unlikely((global.mode & MODE_DEBUG) &&
1242 (!(global.mode & MODE_QUIET) ||
1243 (global.mode & MODE_VERBOSE)))) {
1244 int len;
1245
1246 if (s->si[1].state == SI_ST_CLO &&
1247 s->si[1].prev_state == SI_ST_EST) {
1248 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1249 s->uniq_id, s->be->id,
1250 (unsigned short)s->si[0].fd,
1251 (unsigned short)s->si[1].fd);
1252 write(1, trash, len);
1253 }
1254
1255 if (s->si[0].state == SI_ST_CLO &&
1256 s->si[0].prev_state == SI_ST_EST) {
1257 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1258 s->uniq_id, s->be->id,
1259 (unsigned short)s->si[0].fd,
1260 (unsigned short)s->si[1].fd);
1261 write(1, trash, len);
1262 }
1263 }
1264
1265 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1266 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1267
1268 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1269 session_process_counters(s);
1270
Willy Tarreau1accfc02009-09-05 20:57:35 +02001271 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001272 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001273
Willy Tarreau1accfc02009-09-05 20:57:35 +02001274 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001275 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001276
Willy Tarreauea388542009-06-21 21:45:58 +02001277 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1278 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001279 s->si[0].prev_state = s->si[0].state;
1280 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001281 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1282 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001283
1284 /* Trick: if a request is being waiting for the server to respond,
1285 * and if we know the server can timeout, we don't want the timeout
1286 * to expire on the client side first, but we're still interested
1287 * in passing data from the client to the server (eg: POST). Thus,
1288 * we can cancel the client's request timeout if the server's
1289 * request timeout is set and the server has not yet sent a response.
1290 */
1291
Willy Tarreau520d95e2009-09-19 21:04:57 +02001292 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001293 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1294 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001295 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001296 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001297
Willy Tarreau1accfc02009-09-05 20:57:35 +02001298 /* Call the second stream interface's I/O handler if it's embedded.
1299 * Note that this one may wake the task up again.
1300 */
1301 if (s->req->cons->iohandler) {
1302 s->req->cons->iohandler(s->req->cons);
1303 if (task_in_rq(t)) {
1304 /* If we woke up, we don't want to requeue the
1305 * task to the wait queue, but rather requeue
1306 * it into the runqueue ASAP.
1307 */
1308 t->expire = TICK_ETERNITY;
1309 return t;
1310 }
1311 }
1312
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001313 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1314 tick_first(s->rep->rex, s->rep->wex));
1315 if (s->req->analysers)
1316 t->expire = tick_first(t->expire, s->req->analyse_exp);
1317
1318 if (s->si[0].exp)
1319 t->expire = tick_first(t->expire, s->si[0].exp);
1320
1321 if (s->si[1].exp)
1322 t->expire = tick_first(t->expire, s->si[1].exp);
1323
1324#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001325 fprintf(stderr,
1326 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1327 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1328 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1329 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 +01001330#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001331
1332#ifdef DEBUG_DEV
1333 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001334 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001335 ABORT_NOW();
1336#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001337 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001338 }
1339
1340 s->fe->feconn--;
1341 if (s->flags & SN_BE_ASSIGNED)
1342 s->be->beconn--;
1343 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001344 s->listener->nbconn--;
1345 if (s->listener->state == LI_FULL &&
1346 s->listener->nbconn < s->listener->maxconn) {
1347 /* we should reactivate the listener */
1348 EV_FD_SET(s->listener->fd, DIR_RD);
1349 s->listener->state = LI_READY;
1350 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001351
1352 if (unlikely((global.mode & MODE_DEBUG) &&
1353 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1354 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001355 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001356 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001357 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001358 write(1, trash, len);
1359 }
1360
1361 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1362 session_process_counters(s);
1363
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001364 if (s->txn.status) {
1365 int n;
1366
1367 n = s->txn.status / 100;
1368 if (n < 1 || n > 5)
1369 n = 0;
1370
1371 if (s->fe->mode == PR_MODE_HTTP)
1372 s->fe->counters.p.http.rsp[n]++;
1373
1374 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
1375 (s->be->mode == PR_MODE_HTTP))
1376 s->be->counters.p.http.rsp[n]++;
1377 }
1378
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001379 /* let's do a final log if we need it */
1380 if (s->logs.logwait &&
1381 !(s->flags & SN_MONITOR) &&
1382 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001383 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001384 }
1385
1386 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001387 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001388 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001389 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001390 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001391}
1392
Willy Tarreau7c669d72008-06-20 15:04:11 +02001393/*
1394 * This function adjusts sess->srv_conn and maintains the previous and new
1395 * server's served session counts. Setting newsrv to NULL is enough to release
1396 * current connection slot. This function also notifies any LB algo which might
1397 * expect to be informed about any change in the number of active sessions on a
1398 * server.
1399 */
1400void sess_change_server(struct session *sess, struct server *newsrv)
1401{
1402 if (sess->srv_conn == newsrv)
1403 return;
1404
1405 if (sess->srv_conn) {
1406 sess->srv_conn->served--;
1407 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1408 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1409 sess->srv_conn = NULL;
1410 }
1411
1412 if (newsrv) {
1413 newsrv->served++;
1414 if (newsrv->proxy->lbprm.server_take_conn)
1415 newsrv->proxy->lbprm.server_take_conn(newsrv);
1416 sess->srv_conn = newsrv;
1417 }
1418}
1419
Willy Tarreau84455332009-03-15 22:34:05 +01001420/* Set correct session termination flags in case no analyser has done it. It
1421 * also counts a failed request if the server state has not reached the request
1422 * stage.
1423 */
1424void sess_set_term_flags(struct session *s)
1425{
1426 if (!(s->flags & SN_FINST_MASK)) {
1427 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001428
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001429 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001430 if (s->listener->counters)
1431 s->listener->counters->failed_req++;
1432
Willy Tarreau84455332009-03-15 22:34:05 +01001433 s->flags |= SN_FINST_R;
1434 }
1435 else if (s->si[1].state == SI_ST_QUE)
1436 s->flags |= SN_FINST_Q;
1437 else if (s->si[1].state < SI_ST_EST)
1438 s->flags |= SN_FINST_C;
1439 else if (s->si[1].state == SI_ST_EST)
1440 s->flags |= SN_FINST_D;
1441 else
1442 s->flags |= SN_FINST_L;
1443 }
1444}
1445
1446/* Handle server-side errors for default protocols. It is called whenever a a
1447 * connection setup is aborted or a request is aborted in queue. It sets the
1448 * session termination flags so that the caller does not have to worry about
1449 * them. It's installed as ->srv_error for the server-side stream_interface.
1450 */
1451void default_srv_error(struct session *s, struct stream_interface *si)
1452{
1453 int err_type = si->err_type;
1454 int err = 0, fin = 0;
1455
1456 if (err_type & SI_ET_QUEUE_ABRT) {
1457 err = SN_ERR_CLICL;
1458 fin = SN_FINST_Q;
1459 }
1460 else if (err_type & SI_ET_CONN_ABRT) {
1461 err = SN_ERR_CLICL;
1462 fin = SN_FINST_C;
1463 }
1464 else if (err_type & SI_ET_QUEUE_TO) {
1465 err = SN_ERR_SRVTO;
1466 fin = SN_FINST_Q;
1467 }
1468 else if (err_type & SI_ET_QUEUE_ERR) {
1469 err = SN_ERR_SRVCL;
1470 fin = SN_FINST_Q;
1471 }
1472 else if (err_type & SI_ET_CONN_TO) {
1473 err = SN_ERR_SRVTO;
1474 fin = SN_FINST_C;
1475 }
1476 else if (err_type & SI_ET_CONN_ERR) {
1477 err = SN_ERR_SRVCL;
1478 fin = SN_FINST_C;
1479 }
1480 else /* SI_ET_CONN_OTHER and others */ {
1481 err = SN_ERR_INTERNAL;
1482 fin = SN_FINST_C;
1483 }
1484
1485 if (!(s->flags & SN_ERR_MASK))
1486 s->flags |= err;
1487 if (!(s->flags & SN_FINST_MASK))
1488 s->flags |= fin;
1489}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001490
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491/*
1492 * Local variables:
1493 * c-indent-level: 8
1494 * c-basic-offset: 8
1495 * End:
1496 */