blob: 36fe50de91180f183f2ef470b304369133f7da8c [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>
Emeric Brun1d33b292010-01-04 15:47:17 +010030#include <proto/pattern.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010031#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010032#include <proto/proto_http.h>
33#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020034#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010036#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010037#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010038#include <proto/stream_interface.h>
39#include <proto/stream_sock.h>
40#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020042struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010043struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
45/*
46 * frees the context associated to a session. It must have been removed first.
47 */
48void session_free(struct session *s)
49{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010050 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020051 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010052 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010053
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 if (s->pend_pos)
55 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010056
Willy Tarreau1e62de62008-11-11 20:20:02 +010057 if (s->srv) { /* there may be requests left pending in queue */
58 if (s->flags & SN_CURR_SESS) {
59 s->flags &= ~SN_CURR_SESS;
60 s->srv->cur_sess--;
61 }
Willy Tarreau922a8062008-12-04 09:33:58 +010062 if (may_dequeue_tasks(s->srv, s->be))
63 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010064 }
Willy Tarreau922a8062008-12-04 09:33:58 +010065
Willy Tarreau7c669d72008-06-20 15:04:11 +020066 if (unlikely(s->srv_conn)) {
67 /* the session still has a reserved slot on a server, but
68 * it should normally be only the same as the one above,
69 * so this should not happen in fact.
70 */
71 sess_change_server(s, NULL);
72 }
73
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 if (s->req->pipe)
75 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010076
Willy Tarreau3eba98a2009-01-25 13:56:13 +010077 if (s->rep->pipe)
78 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +010079
Willy Tarreau48d63db2008-08-03 17:41:33 +020080 pool_free2(pool2_buffer, s->req);
81 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Willy Tarreau46023632010-01-07 22:51:47 +010083 http_end_txn(s);
84
Willy Tarreau92fb9832007-10-16 17:34:28 +020085 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020086 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +010087 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
88 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020089 }
Willy Tarreau0937bc42009-12-22 15:03:09 +010090
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010091 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +010092 /* we have to unlink all watchers. We must not relink them if
93 * this session was the last one in the list.
94 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010095 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +010096 LIST_INIT(&bref->users);
97 if (s->list.n != &sessions)
98 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010099 bref->ref = s->list.n;
100 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100101 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200102 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200103
104 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200105 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200106 pool_flush2(pool2_buffer);
107 pool_flush2(fe->hdr_idx_pool);
108 pool_flush2(pool2_requri);
109 pool_flush2(pool2_capture);
110 pool_flush2(pool2_session);
111 pool_flush2(fe->req_cap_pool);
112 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200113 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200114}
115
116
117/* perform minimal intializations, report 0 in case of error, 1 if OK. */
118int init_session()
119{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100120 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200121 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
122 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123}
124
Willy Tarreau30e71012007-11-26 20:15:35 +0100125void session_process_counters(struct session *s)
126{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100127 unsigned long long bytes;
128
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100130 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100131 s->logs.bytes_in = s->req->total;
132 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200133 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100134
Willy Tarreau30e71012007-11-26 20:15:35 +0100135 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200136 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100137
Willy Tarreau30e71012007-11-26 20:15:35 +0100138 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200139 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200140
141 if (s->listener->counters)
142 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100144 }
145
Willy Tarreau30e71012007-11-26 20:15:35 +0100146 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100147 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100148 s->logs.bytes_out = s->rep->total;
149 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200150 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100151
Willy Tarreau30e71012007-11-26 20:15:35 +0100152 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200153 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100154
Willy Tarreau30e71012007-11-26 20:15:35 +0100155 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200156 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200157
158 if (s->listener->counters)
159 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100160 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100161 }
162}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100164/* This function is called with (si->state == SI_ST_CON) meaning that a
165 * connection was attempted and that the file descriptor is already allocated.
166 * We must check for establishment, error and abort. Possible output states
167 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
168 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
169 * otherwise 1.
170 */
171int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
172{
173 struct buffer *req = si->ob;
174 struct buffer *rep = si->ib;
175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100176 /* If we got an error, or if nothing happened and the connection timed
177 * out, we must give up. The CER state handler will take care of retry
178 * attempts and error reports.
179 */
180 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100181 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100182 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200183 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100184 fd_delete(si->fd);
185
186 if (si->err_type)
187 return 0;
188
189 si->err_loc = s->srv;
190 if (si->flags & SI_FL_ERR)
191 si->err_type = SI_ET_CONN_ERR;
192 else
193 si->err_type = SI_ET_CONN_TO;
194 return 0;
195 }
196
197 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200198 if (unlikely((rep->flags & BF_SHUTW) ||
199 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200200 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100201 s->be->options & PR_O_ABRT_CLOSE)))) {
202 /* give up */
203 si->shutw(si);
204 si->err_type |= SI_ET_CONN_ABRT;
205 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200206 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100207 if (s->srv_error)
208 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100209 return 1;
210 }
211
212 /* we need to wait a bit more if there was no activity either */
213 if (!(req->flags & BF_WRITE_ACTIVITY))
214 return 1;
215
216 /* OK, this means that a connection succeeded. The caller will be
217 * responsible for handling the transition from CON to EST.
218 */
219 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100220 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100221 si->state = SI_ST_EST;
222 si->err_type = SI_ET_NONE;
223 si->err_loc = NULL;
224 return 1;
225}
226
227/* This function is called with (si->state == SI_ST_CER) meaning that a
228 * previous connection attempt has failed and that the file descriptor
229 * has already been released. Possible causes include asynchronous error
230 * notification and time out. Possible output states are SI_ST_CLO when
231 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
232 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
233 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
234 * marked as in error state. It returns 0.
235 */
236int sess_update_st_cer(struct session *s, struct stream_interface *si)
237{
238 /* we probably have to release last session from the server */
239 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100240 health_adjust(s->srv, HANA_STATUS_L4_ERR);
241
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100242 if (s->flags & SN_CURR_SESS) {
243 s->flags &= ~SN_CURR_SESS;
244 s->srv->cur_sess--;
245 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100246 }
247
248 /* ensure that we have enough retries left */
249 s->conn_retries--;
250 if (s->conn_retries < 0) {
251 if (!si->err_type) {
252 si->err_type = SI_ET_CONN_ERR;
253 si->err_loc = s->srv;
254 }
255
256 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200257 s->srv->counters.failed_conns++;
258 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100259 if (may_dequeue_tasks(s->srv, s->be))
260 process_srv_queue(s->srv);
261
262 /* shutw is enough so stop a connecting socket */
263 si->shutw(si);
264 si->ob->flags |= BF_WRITE_ERROR;
265 si->ib->flags |= BF_READ_ERROR;
266
267 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100268 if (s->srv_error)
269 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100270 return 0;
271 }
272
273 /* If the "redispatch" option is set on the backend, we are allowed to
274 * retry on another server for the last retry. In order to achieve this,
275 * we must mark the session unassigned, and eventually clear the DIRECT
276 * bit to ignore any persistence cookie. We won't count a retry nor a
277 * redispatch yet, because this will depend on what server is selected.
278 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100279 if (s->srv && s->conn_retries == 0 &&
280 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100281 if (may_dequeue_tasks(s->srv, s->be))
282 process_srv_queue(s->srv);
283
284 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
285 s->prev_srv = s->srv;
286 si->state = SI_ST_REQ;
287 } else {
288 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200289 s->srv->counters.retries++;
290 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100291 si->state = SI_ST_ASS;
292 }
293
294 if (si->flags & SI_FL_ERR) {
295 /* The error was an asynchronous connection error, and we will
296 * likely have to retry connecting to the same server, most
297 * likely leading to the same result. To avoid this, we wait
298 * one second before retrying.
299 */
300
301 if (!si->err_type)
302 si->err_type = SI_ET_CONN_ERR;
303
304 si->state = SI_ST_TAR;
305 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
306 return 0;
307 }
308 return 0;
309}
310
311/*
312 * This function handles the transition between the SI_ST_CON state and the
313 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
314 * SI_ST_EST.
315 */
316void sess_establish(struct session *s, struct stream_interface *si)
317{
318 struct buffer *req = si->ob;
319 struct buffer *rep = si->ib;
320
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100321 if (s->srv)
322 health_adjust(s->srv, HANA_STATUS_L4_OK);
323
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100324 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100325 /* if the user wants to log as soon as possible, without counting
326 * bytes from the server, then this is the right moment. */
327 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
328 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100329 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100330 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100331 }
332 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100333 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
334 /* reset hdr_idx which was already initialized by the request.
335 * right now, the http parser does it.
336 * hdr_idx_init(&s->txn.hdr_idx);
337 */
338 }
339
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200340 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100341 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
342 req->wex = TICK_ETERNITY;
343}
344
345/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
346 * Other input states are simply ignored.
347 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
348 * Flags must have previously been updated for timeouts and other conditions.
349 */
350void sess_update_stream_int(struct session *s, struct stream_interface *si)
351{
352 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",
353 now_ms, __FUNCTION__,
354 s,
355 s->req, s->rep,
356 s->req->rex, s->rep->wex,
357 s->req->flags, s->rep->flags,
358 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
359
360 if (si->state == SI_ST_ASS) {
361 /* Server assigned to connection request, we have to try to connect now */
362 int conn_err;
363
364 conn_err = connect_server(s);
365 if (conn_err == SN_ERR_NONE) {
366 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100367 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100368 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100369 return;
370 }
371
372 /* We have received a synchronous error. We might have to
373 * abort, retry immediately or redispatch.
374 */
375 if (conn_err == SN_ERR_INTERNAL) {
376 if (!si->err_type) {
377 si->err_type = SI_ET_CONN_OTHER;
378 si->err_loc = s->srv;
379 }
380
381 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100382 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100383 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200384 s->srv->counters.failed_conns++;
385 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100386
387 /* release other sessions waiting for this server */
388 if (may_dequeue_tasks(s->srv, s->be))
389 process_srv_queue(s->srv);
390
391 /* Failed and not retryable. */
392 si->shutr(si);
393 si->shutw(si);
394 si->ob->flags |= BF_WRITE_ERROR;
395
396 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
397
398 /* no session was ever accounted for this server */
399 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100400 if (s->srv_error)
401 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100402 return;
403 }
404
405 /* We are facing a retryable error, but we don't want to run a
406 * turn-around now, as the problem is likely a source port
407 * allocation problem, so we want to retry now.
408 */
409 si->state = SI_ST_CER;
410 si->flags &= ~SI_FL_ERR;
411 sess_update_st_cer(s, si);
412 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
413 return;
414 }
415 else if (si->state == SI_ST_QUE) {
416 /* connection request was queued, check for any update */
417 if (!s->pend_pos) {
418 /* The connection is not in the queue anymore. Either
419 * we have a server connection slot available and we
420 * go directly to the assigned state, or we need to
421 * load-balance first and go to the INI state.
422 */
423 si->exp = TICK_ETERNITY;
424 if (unlikely(!(s->flags & SN_ASSIGNED)))
425 si->state = SI_ST_REQ;
426 else {
427 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
428 si->state = SI_ST_ASS;
429 }
430 return;
431 }
432
433 /* Connection request still in queue... */
434 if (si->flags & SI_FL_EXP) {
435 /* ... and timeout expired */
436 si->exp = TICK_ETERNITY;
437 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
438 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200439 s->srv->counters.failed_conns++;
440 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100441 si->shutr(si);
442 si->shutw(si);
443 si->ob->flags |= BF_WRITE_TIMEOUT;
444 if (!si->err_type)
445 si->err_type = SI_ET_QUEUE_TO;
446 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100447 if (s->srv_error)
448 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100449 return;
450 }
451
452 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200453 if ((si->ob->flags & (BF_READ_ERROR)) ||
454 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200455 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100456 /* give up */
457 si->exp = TICK_ETERNITY;
458 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
459 si->shutr(si);
460 si->shutw(si);
461 si->err_type |= SI_ET_QUEUE_ABRT;
462 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100463 if (s->srv_error)
464 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100465 return;
466 }
467
468 /* Nothing changed */
469 return;
470 }
471 else if (si->state == SI_ST_TAR) {
472 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200473 if ((si->ob->flags & (BF_READ_ERROR)) ||
474 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200475 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100476 /* give up */
477 si->exp = TICK_ETERNITY;
478 si->shutr(si);
479 si->shutw(si);
480 si->err_type |= SI_ET_CONN_ABRT;
481 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100482 if (s->srv_error)
483 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100484 return;
485 }
486
487 if (!(si->flags & SI_FL_EXP))
488 return; /* still in turn-around */
489
490 si->exp = TICK_ETERNITY;
491
492 /* we keep trying on the same server as long as the session is
493 * marked "assigned".
494 * FIXME: Should we force a redispatch attempt when the server is down ?
495 */
496 if (s->flags & SN_ASSIGNED)
497 si->state = SI_ST_ASS;
498 else
499 si->state = SI_ST_REQ;
500 return;
501 }
502}
503
504/* This function initiates a server connection request on a stream interface
505 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
506 * indicating that a server has been assigned. It may also return SI_ST_QUE,
507 * or SI_ST_CLO upon error.
508 */
509static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
510 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",
511 now_ms, __FUNCTION__,
512 s,
513 s->req, s->rep,
514 s->req->rex, s->rep->wex,
515 s->req->flags, s->rep->flags,
516 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
517
518 if (si->state != SI_ST_REQ)
519 return;
520
521 /* Try to assign a server */
522 if (srv_redispatch_connect(s) != 0) {
523 /* We did not get a server. Either we queued the
524 * connection request, or we encountered an error.
525 */
526 if (si->state == SI_ST_QUE)
527 return;
528
529 /* we did not get any server, let's check the cause */
530 si->shutr(si);
531 si->shutw(si);
532 si->ob->flags |= BF_WRITE_ERROR;
533 if (!si->err_type)
534 si->err_type = SI_ET_CONN_OTHER;
535 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100536 if (s->srv_error)
537 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538 return;
539 }
540
541 /* The server is assigned */
542 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
543 si->state = SI_ST_ASS;
544}
545
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200546/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100547 * if appropriate. The default_backend rule is also considered, then the
548 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200549 * It returns 1 if the processing can continue on next analysers, or zero if it
550 * either needs more data or wants to immediately abort the request.
551 */
552int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
553{
Willy Tarreau4de91492010-01-22 19:10:05 +0100554 struct force_persist_rule *prst_rule;
555
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200556 req->analysers &= ~an_bit;
557 req->analyse_exp = TICK_ETERNITY;
558
559 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
560 now_ms, __FUNCTION__,
561 s,
562 req,
563 req->rex, req->wex,
564 req->flags,
565 req->l,
566 req->analysers);
567
568 /* now check whether we have some switching rules for this request */
569 if (!(s->flags & SN_BE_ASSIGNED)) {
570 struct switching_rule *rule;
571
572 list_for_each_entry(rule, &s->fe->switching_rules, list) {
573 int ret;
574
575 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
576 ret = acl_pass(ret);
577 if (rule->cond->pol == ACL_COND_UNLESS)
578 ret = !ret;
579
580 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200581 if (!session_set_backend(s, rule->be.backend))
582 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200583 break;
584 }
585 }
586
587 /* To ensure correct connection accounting on the backend, we
588 * have to assign one if it was not set (eg: a listen). This
589 * measure also takes care of correctly setting the default
590 * backend if any.
591 */
592 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200593 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
594 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200595 }
596
597 /* we don't want to run the HTTP filters again if the backend has not changed */
598 if (s->fe == s->be)
599 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
600
Willy Tarreau4de91492010-01-22 19:10:05 +0100601 /* as soon as we know the backend, we must check if we have a matching forced
602 * persistence rule, and report that in the session.
603 */
604 list_for_each_entry(prst_rule, &s->be->force_persist_rules, list) {
605 int ret = 1;
606
607 if (prst_rule->cond) {
608 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
609 ret = acl_pass(ret);
610 if (prst_rule->cond->pol == ACL_COND_UNLESS)
611 ret = !ret;
612 }
613
614 if (ret) {
615 /* no rule, or the rule matches */
616 s->flags |= SN_FORCE_PRST;
617 break;
618 }
619 }
620
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200621 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200622
623 sw_failed:
624 /* immediately abort this request in case of allocation failure */
625 buffer_abort(s->req);
626 buffer_abort(s->rep);
627
628 if (!(s->flags & SN_ERR_MASK))
629 s->flags |= SN_ERR_RESOURCE;
630 if (!(s->flags & SN_FINST_MASK))
631 s->flags |= SN_FINST_R;
632
633 s->txn.status = 500;
634 s->req->analysers = 0;
635 s->req->analyse_exp = TICK_ETERNITY;
636 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200637}
638
Emeric Brun1d33b292010-01-04 15:47:17 +0100639/* This stream analyser works on a request. It applies all sticking rules on
640 * it then returns 1. The data must already be present in the buffer otherwise
641 * they won't match. It always returns 1.
642 */
643int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
644{
645 struct proxy *px = s->be;
646 struct sticking_rule *rule;
647
648 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
649 now_ms, __FUNCTION__,
650 s,
651 req,
652 req->rex, req->wex,
653 req->flags,
654 req->l,
655 req->analysers);
656
657 list_for_each_entry(rule, &px->sticking_rules, list) {
658 int ret = 1 ;
659 int i;
660
661 for (i = 0; i < s->store_count; i++) {
662 if (rule->table.t == s->store[i].table)
663 break;
664 }
665
666 if (i != s->store_count)
667 continue;
668
669 if (rule->cond) {
670 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
671 ret = acl_pass(ret);
672 if (rule->cond->pol == ACL_COND_UNLESS)
673 ret = !ret;
674 }
675
676 if (ret) {
677 struct stktable_key *key;
678
679 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
680 if (!key)
681 continue;
682
683 if (rule->flags & STK_IS_MATCH) {
684 struct stksess *ts;
685
686 if ((ts = stktable_lookup(rule->table.t, key)) != NULL) {
687 if (!(s->flags & SN_ASSIGNED)) {
688 struct eb32_node *node;
689
690 /* srv found in table */
691 node = eb32_lookup(&px->conf.used_server_id, ts->sid);
692 if (node) {
693 struct server *srv;
694
695 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100696 if ((srv->state & SRV_RUNNING) ||
697 (px->options & PR_O_PERSIST) ||
698 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100699 s->flags |= SN_DIRECT | SN_ASSIGNED;
700 s->srv = srv;
701 }
702 }
703 }
704 ts->expire = tick_add(now_ms, MS_TO_TICKS(rule->table.t->expire));
705 }
706 }
707 if (rule->flags & STK_IS_STORE) {
708 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
709 struct stksess *ts;
710
711 ts = stksess_new(rule->table.t, key);
712 if (ts) {
713 s->store[s->store_count].table = rule->table.t;
714 s->store[s->store_count++].ts = ts;
715 }
716 }
717 }
718 }
719 }
720
721 req->analysers &= ~an_bit;
722 req->analyse_exp = TICK_ETERNITY;
723 return 1;
724}
725
726/* This stream analyser works on a response. It applies all store rules on it
727 * then returns 1. The data must already be present in the buffer otherwise
728 * they won't match. It always returns 1.
729 */
730int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
731{
732 struct proxy *px = s->be;
733 struct sticking_rule *rule;
734 int i;
735
736 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
737 now_ms, __FUNCTION__,
738 s,
739 req,
740 req->rex, req->wex,
741 req->flags,
742 req->l,
743 req->analysers);
744
745 list_for_each_entry(rule, &px->storersp_rules, list) {
746 int ret = 1 ;
747 int storereqidx = -1;
748
749 for (i = 0; i < s->store_count; i++) {
750 if (rule->table.t == s->store[i].table) {
751 if (!(s->store[i].flags))
752 storereqidx = i;
753 break;
754 }
755 }
756
757 if ((i != s->store_count) && (storereqidx == -1))
758 continue;
759
760 if (rule->cond) {
761 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
762 ret = acl_pass(ret);
763 if (rule->cond->pol == ACL_COND_UNLESS)
764 ret = !ret;
765 }
766
767 if (ret) {
768 struct stktable_key *key;
769
770 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
771 if (!key)
772 continue;
773
774 if (storereqidx != -1) {
775 stksess_key(s->store[storereqidx].table, s->store[storereqidx].ts, key);
776 s->store[storereqidx].flags = 1;
777 }
778 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
779 struct stksess *ts;
780
781 ts = stksess_new(rule->table.t, key);
782 if (ts) {
783 s->store[s->store_count].table = rule->table.t;
784 s->store[s->store_count].flags = 1;
785 s->store[s->store_count++].ts = ts;
786 }
787 }
788 }
789 }
790
791 /* process store request and store response */
792 for (i = 0; i < s->store_count; i++) {
793 if (stktable_store(s->store[i].table, s->store[i].ts, s->srv->puid) > 0) {
794 stksess_free(s->store[i].table, s->store[i].ts);
795 s->store[i].ts = NULL;
796 }
797 }
798
799 rep->analysers &= ~an_bit;
800 rep->analyse_exp = TICK_ETERNITY;
801 return 1;
802}
803
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100804/* This macro is very specific to the function below. See the comments in
805 * process_session() below to understand the logic and the tests.
806 */
807#define UPDATE_ANALYSERS(real, list, back, flag) { \
808 list = (((list) & ~(flag)) | ~(back)) & (real); \
809 back = real; \
810 if (!(list)) \
811 break; \
812 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
813 continue; \
814}
815
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816/* Processes the client, server, request and response jobs of a session task,
817 * then puts it back to the wait queue in a clean state, or cleans up its
818 * resources if it must be deleted. Returns in <next> the date the task wants
819 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
820 * nothing too many times, the request and response buffers flags are monitored
821 * and each function is called only if at least another function has changed at
822 * least one flag it is interested in.
823 */
Willy Tarreau26c25062009-03-08 09:38:41 +0100824struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825{
826 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +0100828 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829
830 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
831 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
832
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200833 /* This flag must explicitly be set every time */
834 s->req->flags &= ~BF_READ_NOEXP;
835
836 /* Keep a copy of req/rep flags so that we can detect shutdowns */
837 rqf_last = s->req->flags;
838 rpf_last = s->rep->flags;
839
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200840 /* we don't want the stream interface functions to recursively wake us up */
841 if (s->req->prod->owner == t)
842 s->req->prod->flags |= SI_FL_DONT_WAKE;
843 if (s->req->cons->owner == t)
844 s->req->cons->flags |= SI_FL_DONT_WAKE;
845
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 /* 1a: Check for low level timeouts if needed. We just set a flag on
847 * stream interfaces when their timeouts have expired.
848 */
849 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
850 stream_int_check_timeouts(&s->si[0]);
851 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200852
853 /* check buffer timeouts, and close the corresponding stream interfaces
854 * for future reads or writes. Note: this will also concern upper layers
855 * but we do not touch any other flag. We must be careful and correctly
856 * detect state changes when calling them.
857 */
858
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100859 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200860
Willy Tarreau14641402009-12-29 14:49:56 +0100861 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
862 s->req->cons->flags |= SI_FL_NOLINGER;
863 s->req->cons->shutw(s->req->cons);
864 }
865
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200866 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
867 s->req->prod->shutr(s->req->prod);
868
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100869 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100870
Willy Tarreau14641402009-12-29 14:49:56 +0100871 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
872 s->rep->cons->flags |= SI_FL_NOLINGER;
873 s->rep->cons->shutw(s->rep->cons);
874 }
875
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200876 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
877 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200878 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100879
880 /* 1b: check for low-level errors reported at the stream interface.
881 * First we check if it's a retryable error (in which case we don't
882 * want to tell the buffer). Otherwise we report the error one level
883 * upper by setting flags into the buffers. Note that the side towards
884 * the client cannot have connect (hence retryable) errors. Also, the
885 * connection setup code must be able to deal with any type of abort.
886 */
887 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
888 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
889 s->si[0].shutr(&s->si[0]);
890 s->si[0].shutw(&s->si[0]);
891 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100892 if (!(s->req->analysers) && !(s->rep->analysers)) {
893 if (!(s->flags & SN_ERR_MASK))
894 s->flags |= SN_ERR_CLICL;
895 if (!(s->flags & SN_FINST_MASK))
896 s->flags |= SN_FINST_D;
897 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100898 }
899 }
900
901 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
902 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
903 s->si[1].shutr(&s->si[1]);
904 s->si[1].shutw(&s->si[1]);
905 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200906 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200908 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100909 if (!(s->req->analysers) && !(s->rep->analysers)) {
910 if (!(s->flags & SN_ERR_MASK))
911 s->flags |= SN_ERR_SRVCL;
912 if (!(s->flags & SN_FINST_MASK))
913 s->flags |= SN_FINST_D;
914 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100915 }
916 /* note: maybe we should process connection errors here ? */
917 }
918
919 if (s->si[1].state == SI_ST_CON) {
920 /* we were trying to establish a connection on the server side,
921 * maybe it succeeded, maybe it failed, maybe we timed out, ...
922 */
923 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
924 sess_update_st_cer(s, &s->si[1]);
925 else if (s->si[1].state == SI_ST_EST)
926 sess_establish(s, &s->si[1]);
927
928 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
929 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
930 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
931 */
932 }
933
Willy Tarreaub67a9b82009-06-21 22:03:51 +0200934resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100935 /* Check for connection closure */
936
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937 DPRINTF(stderr,
938 "[%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",
939 now_ms, __FUNCTION__, __LINE__,
940 t,
941 s, s->flags,
942 s->req, s->rep,
943 s->req->rex, s->rep->wex,
944 s->req->flags, s->rep->flags,
945 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
946 s->rep->cons->err_type, s->req->cons->err_type,
947 s->conn_retries);
948
949 /* nothing special to be done on client side */
950 if (unlikely(s->req->prod->state == SI_ST_DIS))
951 s->req->prod->state = SI_ST_CLO;
952
953 /* When a server-side connection is released, we have to count it and
954 * check for pending connections on this server.
955 */
956 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
957 s->req->cons->state = SI_ST_CLO;
958 if (s->srv) {
959 if (s->flags & SN_CURR_SESS) {
960 s->flags &= ~SN_CURR_SESS;
961 s->srv->cur_sess--;
962 }
963 sess_change_server(s, NULL);
964 if (may_dequeue_tasks(s->srv, s->be))
965 process_srv_queue(s->srv);
966 }
967 }
968
969 /*
970 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
971 * at this point.
972 */
973
Willy Tarreau0be0ef92009-03-08 19:20:25 +0100974 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 /* Analyse request */
976 if ((s->req->flags & BF_MASK_ANALYSER) ||
977 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
978 unsigned int flags = s->req->flags;
979
980 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +0100981 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +0100982 unsigned int ana_list;
983 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200984
Willy Tarreau90deb182010-01-07 00:20:41 +0100985 /* it's up to the analysers to stop new connections,
986 * disable reading or closing. Note: if an analyser
987 * disables any of these bits, it is responsible for
988 * enabling them again when it disables itself, so
989 * that other analysers are called in similar conditions.
990 */
991 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200992 buffer_auto_connect(s->req);
993 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100994
995 /* We will call all analysers for which a bit is set in
996 * s->req->analysers, following the bit order from LSB
997 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +0200998 * the list when not needed. Any analyser may return 0
999 * to break out of the loop, either because of missing
1000 * data to take a decision, or because it decides to
1001 * kill the session. We loop at least once through each
1002 * analyser, and we may loop again if other analysers
1003 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001004 *
1005 * We build a list of analysers to run. We evaluate all
1006 * of these analysers in the order of the lower bit to
1007 * the higher bit. This ordering is very important.
1008 * An analyser will often add/remove other analysers,
1009 * including itself. Any changes to itself have no effect
1010 * on the loop. If it removes any other analysers, we
1011 * want those analysers not to be called anymore during
1012 * this loop. If it adds an analyser that is located
1013 * after itself, we want it to be scheduled for being
1014 * processed during the loop. If it adds an analyser
1015 * which is located before it, we want it to switch to
1016 * it immediately, even if it has already been called
1017 * once but removed since.
1018 *
1019 * In order to achieve this, we compare the analyser
1020 * list after the call with a copy of it before the
1021 * call. The work list is fed with analyser bits that
1022 * appeared during the call. Then we compare previous
1023 * work list with the new one, and check the bits that
1024 * appeared. If the lowest of these bits is lower than
1025 * the current bit, it means we have enabled a previous
1026 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001027 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001028
1029 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001030 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001031 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001032
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001033 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001034 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001035 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001036 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001037 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001038
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001039 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001040 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001041 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001042 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001043 }
1044
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001045 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001046 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1047 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001048 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001049 }
1050
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001051 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1053 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001054 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001055 }
1056
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001057 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001058 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1059 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001060 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001061 }
1062
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001063 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001064 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001065 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001066 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001067 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001068
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001069 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001070 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1071 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001072 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001073 }
1074
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001075 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001076 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001077 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001078 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001079 }
Emeric Brun647caf12009-06-30 17:57:00 +02001080
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001081 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001082 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1083 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001084 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001085 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001086
Emeric Brun1d33b292010-01-04 15:47:17 +01001087 if (ana_list & AN_REQ_STICKING_RULES) {
1088 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1089 break;
1090 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1091 }
1092
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001093 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001094 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1095 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001096 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001097 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001098 break;
1099 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001100 }
Willy Tarreau84455332009-03-15 22:34:05 +01001101
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001102 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
1103 rqf_last = s->req->flags;
1104 goto resync_request;
1105 }
1106 }
1107
Willy Tarreau576507f2010-01-07 00:09:04 +01001108 /* we'll monitor the request analysers while parsing the response,
1109 * because some response analysers may indirectly enable new request
1110 * analysers (eg: HTTP keep-alive).
1111 */
1112 req_ana_back = s->req->analysers;
1113
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001114 resync_response:
1115 /* Analyse response */
1116
1117 if (unlikely(s->rep->flags & BF_HIJACK)) {
1118 /* In inject mode, we wake up everytime something has
1119 * happened on the write side of the buffer.
1120 */
1121 unsigned int flags = s->rep->flags;
1122
1123 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1124 !(s->rep->flags & BF_FULL)) {
1125 s->rep->hijacker(s, s->rep);
1126 }
1127
1128 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1129 rpf_last = s->rep->flags;
1130 goto resync_response;
1131 }
1132 }
1133 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
1134 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
1135 unsigned int flags = s->rep->flags;
1136
1137 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001138 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001139 unsigned int ana_list;
1140 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001141
Willy Tarreau90deb182010-01-07 00:20:41 +01001142 /* it's up to the analysers to stop disable reading or
1143 * closing. Note: if an analyser disables any of these
1144 * bits, it is responsible for enabling them again when
1145 * it disables itself, so that other analysers are called
1146 * in similar conditions.
1147 */
1148 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001149 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001150
1151 /* We will call all analysers for which a bit is set in
1152 * s->rep->analysers, following the bit order from LSB
1153 * to MSB. The analysers must remove themselves from
1154 * the list when not needed. Any analyser may return 0
1155 * to break out of the loop, either because of missing
1156 * data to take a decision, or because it decides to
1157 * kill the session. We loop at least once through each
1158 * analyser, and we may loop again if other analysers
1159 * are added in the middle.
1160 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001161
1162 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001163 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001164 if (!ana_list)
1165 break;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001166
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001167 /* Warning! ensure that analysers are always placed in ascending order! */
1168
1169 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001170 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1171 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001172 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001173 }
1174
Emeric Brun1d33b292010-01-04 15:47:17 +01001175 if (ana_list & AN_RES_STORE_RULES) {
1176 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1177 break;
1178 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1179 }
1180
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001181 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001182 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1183 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001184 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001185 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001186
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001187 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001188 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1189 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001190 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001191 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001192 break;
1193 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001194 }
1195
1196 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1197 rpf_last = s->rep->flags;
1198 goto resync_response;
1199 }
1200 }
1201
Willy Tarreau576507f2010-01-07 00:09:04 +01001202 /* maybe someone has added some request analysers, so we must check and loop */
1203 if (s->req->analysers & ~req_ana_back)
1204 goto resync_request;
1205
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001206 /* FIXME: here we should call protocol handlers which rely on
1207 * both buffers.
1208 */
1209
1210
1211 /*
1212 * Now we propagate unhandled errors to the session
1213 */
1214 if (!(s->flags & SN_ERR_MASK)) {
1215 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1216 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001217 s->req->analysers = 0;
1218 if (s->req->flags & BF_READ_ERROR)
1219 s->flags |= SN_ERR_CLICL;
1220 else if (s->req->flags & BF_READ_TIMEOUT)
1221 s->flags |= SN_ERR_CLITO;
1222 else if (s->req->flags & BF_WRITE_ERROR)
1223 s->flags |= SN_ERR_SRVCL;
1224 else
1225 s->flags |= SN_ERR_SRVTO;
1226 sess_set_term_flags(s);
1227 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001228 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1229 /* Report it if the server got an error or a read timeout expired */
1230 s->rep->analysers = 0;
1231 if (s->rep->flags & BF_READ_ERROR)
1232 s->flags |= SN_ERR_SRVCL;
1233 else if (s->rep->flags & BF_READ_TIMEOUT)
1234 s->flags |= SN_ERR_SRVTO;
1235 else if (s->rep->flags & BF_WRITE_ERROR)
1236 s->flags |= SN_ERR_CLICL;
1237 else
Willy Tarreau84455332009-03-15 22:34:05 +01001238 s->flags |= SN_ERR_CLITO;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001239 sess_set_term_flags(s);
1240 }
Willy Tarreau84455332009-03-15 22:34:05 +01001241 }
1242
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001243 /*
1244 * Here we take care of forwarding unhandled data. This also includes
1245 * connection establishments and shutdown requests.
1246 */
1247
1248
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001249 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001250 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001251 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001252 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001253 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001254 (s->req->prod->state >= SI_ST_EST) &&
1255 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001256 /* This buffer is freewheeling, there's no analyser nor hijacker
1257 * attached to it. If any data are left in, we'll permit them to
1258 * move.
1259 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001260 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001261 buffer_auto_connect(s->req);
1262 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001263 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001264
Willy Tarreau31971e52009-09-20 12:07:52 +02001265 /* If the producer is still connected, we'll enable data to flow
1266 * from the producer to the consumer (which might possibly not be
1267 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001268 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001269 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1270 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001271 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001272
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001273 /* check if it is wise to enable kernel splicing to forward request data */
1274 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1275 s->req->to_forward &&
1276 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001277 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001278 (pipes_used < global.maxpipes) &&
1279 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1280 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1281 (s->req->flags & BF_STREAMER_FAST)))) {
1282 s->req->flags |= BF_KERN_SPLICING;
1283 }
1284
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001285 /* reflect what the L7 analysers have seen last */
1286 rqf_last = s->req->flags;
1287
1288 /*
1289 * Now forward all shutdown requests between both sides of the buffer
1290 */
1291
Willy Tarreau520d95e2009-09-19 21:04:57 +02001292 /* first, let's check if the request buffer needs to shutdown(write), which may
1293 * happen either because the input is closed or because we want to force a close
1294 * once the server has begun to respond.
1295 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001296 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1297 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001298 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001299
1300 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001301 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 +01001302 s->req->cons->shutw(s->req->cons);
1303
1304 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001305 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1306 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001307 buffer_shutr_now(s->req);
1308
1309 /* shutdown(read) pending */
1310 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1311 s->req->prod->shutr(s->req->prod);
1312
Willy Tarreau520d95e2009-09-19 21:04:57 +02001313 /* it's possible that an upper layer has requested a connection setup or abort.
1314 * There are 2 situations where we decide to establish a new connection :
1315 * - there are data scheduled for emission in the buffer
1316 * - the BF_AUTO_CONNECT flag is set (active connection)
1317 */
1318 if (s->req->cons->state == SI_ST_INI) {
1319 if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001320 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001321 /* If we have a ->connect method, we need to perform a connection request,
1322 * otherwise we immediately switch to the connected state.
1323 */
1324 if (s->req->cons->connect)
1325 s->req->cons->state = SI_ST_REQ; /* new connection requested */
1326 else
1327 s->req->cons->state = SI_ST_EST; /* connection established */
1328 }
Willy Tarreau73201222009-08-16 18:27:24 +02001329 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001330 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001331 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001332 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1333 buffer_shutr_now(s->rep);
1334 }
Willy Tarreau92795622009-03-06 12:51:23 +01001335 }
1336
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001337
1338 /* we may have a pending connection request, or a connection waiting
1339 * for completion.
1340 */
1341 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1342 do {
1343 /* nb: step 1 might switch from QUE to ASS, but we first want
1344 * to give a chance to step 2 to perform a redirect if needed.
1345 */
1346 if (s->si[1].state != SI_ST_REQ)
1347 sess_update_stream_int(s, &s->si[1]);
1348 if (s->si[1].state == SI_ST_REQ)
1349 sess_prepare_conn_req(s, &s->si[1]);
1350
1351 if (s->si[1].state == SI_ST_ASS && s->srv &&
1352 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1353 perform_http_redirect(s, &s->si[1]);
1354 } while (s->si[1].state == SI_ST_ASS);
1355 }
1356
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001357 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001358 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001359 goto resync_stream_interface;
1360
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001361 /* otherwise wewant to check if we need to resync the req buffer or not */
1362 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001363 goto resync_request;
1364
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001365 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001366
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001367 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001368 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001369 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001370 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001371 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001372 (s->rep->prod->state >= SI_ST_EST) &&
1373 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001374 /* This buffer is freewheeling, there's no analyser nor hijacker
1375 * attached to it. If any data are left in, we'll permit them to
1376 * move.
1377 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001378 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001379 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001380 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001381 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1382 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001383 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001384
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001385 /* check if it is wise to enable kernel splicing to forward response data */
1386 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1387 s->rep->to_forward &&
1388 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001389 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001390 (pipes_used < global.maxpipes) &&
1391 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1392 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1393 (s->rep->flags & BF_STREAMER_FAST)))) {
1394 s->rep->flags |= BF_KERN_SPLICING;
1395 }
1396
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001397 /* reflect what the L7 analysers have seen last */
1398 rpf_last = s->rep->flags;
1399
1400 /*
1401 * Now forward all shutdown requests between both sides of the buffer
1402 */
1403
1404 /*
1405 * FIXME: this is probably where we should produce error responses.
1406 */
1407
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001408 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001409 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1410 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001411 buffer_shutw_now(s->rep);
1412
1413 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001414 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 +01001415 s->rep->cons->shutw(s->rep->cons);
1416
1417 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001418 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1419 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001420 buffer_shutr_now(s->rep);
1421
1422 /* shutdown(read) pending */
1423 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1424 s->rep->prod->shutr(s->rep->prod);
1425
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001426 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001427 goto resync_stream_interface;
1428
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001429 if (s->req->flags != rqf_last)
1430 goto resync_request;
1431
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001432 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001433 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001434
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001435 /* we're interested in getting wakeups again */
1436 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1437 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1438
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439 /* This is needed only when debugging is enabled, to indicate
1440 * client-side or server-side close. Please note that in the unlikely
1441 * event where both sides would close at once, the sequence is reported
1442 * on the server side first.
1443 */
1444 if (unlikely((global.mode & MODE_DEBUG) &&
1445 (!(global.mode & MODE_QUIET) ||
1446 (global.mode & MODE_VERBOSE)))) {
1447 int len;
1448
1449 if (s->si[1].state == SI_ST_CLO &&
1450 s->si[1].prev_state == SI_ST_EST) {
1451 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1452 s->uniq_id, s->be->id,
1453 (unsigned short)s->si[0].fd,
1454 (unsigned short)s->si[1].fd);
1455 write(1, trash, len);
1456 }
1457
1458 if (s->si[0].state == SI_ST_CLO &&
1459 s->si[0].prev_state == SI_ST_EST) {
1460 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1461 s->uniq_id, s->be->id,
1462 (unsigned short)s->si[0].fd,
1463 (unsigned short)s->si[1].fd);
1464 write(1, trash, len);
1465 }
1466 }
1467
1468 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1469 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1470
1471 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1472 session_process_counters(s);
1473
Willy Tarreau1accfc02009-09-05 20:57:35 +02001474 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001475 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001476
Willy Tarreau1accfc02009-09-05 20:57:35 +02001477 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001478 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001479
Willy Tarreauea388542009-06-21 21:45:58 +02001480 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
1481 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001482 s->si[0].prev_state = s->si[0].state;
1483 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001484 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1485 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001486
1487 /* Trick: if a request is being waiting for the server to respond,
1488 * and if we know the server can timeout, we don't want the timeout
1489 * to expire on the client side first, but we're still interested
1490 * in passing data from the client to the server (eg: POST). Thus,
1491 * we can cancel the client's request timeout if the server's
1492 * request timeout is set and the server has not yet sent a response.
1493 */
1494
Willy Tarreau520d95e2009-09-19 21:04:57 +02001495 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001496 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1497 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001498 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001499 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001500
Willy Tarreau1accfc02009-09-05 20:57:35 +02001501 /* Call the second stream interface's I/O handler if it's embedded.
1502 * Note that this one may wake the task up again.
1503 */
1504 if (s->req->cons->iohandler) {
1505 s->req->cons->iohandler(s->req->cons);
1506 if (task_in_rq(t)) {
1507 /* If we woke up, we don't want to requeue the
1508 * task to the wait queue, but rather requeue
1509 * it into the runqueue ASAP.
1510 */
1511 t->expire = TICK_ETERNITY;
1512 return t;
1513 }
1514 }
1515
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001516 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1517 tick_first(s->rep->rex, s->rep->wex));
1518 if (s->req->analysers)
1519 t->expire = tick_first(t->expire, s->req->analyse_exp);
1520
1521 if (s->si[0].exp)
1522 t->expire = tick_first(t->expire, s->si[0].exp);
1523
1524 if (s->si[1].exp)
1525 t->expire = tick_first(t->expire, s->si[1].exp);
1526
1527#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001528 fprintf(stderr,
1529 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1530 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1531 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1532 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 +01001533#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001534
1535#ifdef DEBUG_DEV
1536 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001537 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001538 ABORT_NOW();
1539#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001540 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001541 }
1542
1543 s->fe->feconn--;
1544 if (s->flags & SN_BE_ASSIGNED)
1545 s->be->beconn--;
1546 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001547 s->listener->nbconn--;
1548 if (s->listener->state == LI_FULL &&
1549 s->listener->nbconn < s->listener->maxconn) {
1550 /* we should reactivate the listener */
1551 EV_FD_SET(s->listener->fd, DIR_RD);
1552 s->listener->state = LI_READY;
1553 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001554
1555 if (unlikely((global.mode & MODE_DEBUG) &&
1556 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1557 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001558 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001559 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001560 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001561 write(1, trash, len);
1562 }
1563
1564 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1565 session_process_counters(s);
1566
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001567 if (s->txn.status) {
1568 int n;
1569
1570 n = s->txn.status / 100;
1571 if (n < 1 || n > 5)
1572 n = 0;
1573
1574 if (s->fe->mode == PR_MODE_HTTP)
1575 s->fe->counters.p.http.rsp[n]++;
1576
1577 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
1578 (s->be->mode == PR_MODE_HTTP))
1579 s->be->counters.p.http.rsp[n]++;
1580 }
1581
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001582 /* let's do a final log if we need it */
1583 if (s->logs.logwait &&
1584 !(s->flags & SN_MONITOR) &&
1585 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001586 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001587 }
1588
1589 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001591 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001592 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001593 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001594}
1595
Willy Tarreau7c669d72008-06-20 15:04:11 +02001596/*
1597 * This function adjusts sess->srv_conn and maintains the previous and new
1598 * server's served session counts. Setting newsrv to NULL is enough to release
1599 * current connection slot. This function also notifies any LB algo which might
1600 * expect to be informed about any change in the number of active sessions on a
1601 * server.
1602 */
1603void sess_change_server(struct session *sess, struct server *newsrv)
1604{
1605 if (sess->srv_conn == newsrv)
1606 return;
1607
1608 if (sess->srv_conn) {
1609 sess->srv_conn->served--;
1610 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1611 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1612 sess->srv_conn = NULL;
1613 }
1614
1615 if (newsrv) {
1616 newsrv->served++;
1617 if (newsrv->proxy->lbprm.server_take_conn)
1618 newsrv->proxy->lbprm.server_take_conn(newsrv);
1619 sess->srv_conn = newsrv;
1620 }
1621}
1622
Willy Tarreau84455332009-03-15 22:34:05 +01001623/* Set correct session termination flags in case no analyser has done it. It
1624 * also counts a failed request if the server state has not reached the request
1625 * stage.
1626 */
1627void sess_set_term_flags(struct session *s)
1628{
1629 if (!(s->flags & SN_FINST_MASK)) {
1630 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001631
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001632 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001633 if (s->listener->counters)
1634 s->listener->counters->failed_req++;
1635
Willy Tarreau84455332009-03-15 22:34:05 +01001636 s->flags |= SN_FINST_R;
1637 }
1638 else if (s->si[1].state == SI_ST_QUE)
1639 s->flags |= SN_FINST_Q;
1640 else if (s->si[1].state < SI_ST_EST)
1641 s->flags |= SN_FINST_C;
1642 else if (s->si[1].state == SI_ST_EST)
1643 s->flags |= SN_FINST_D;
1644 else
1645 s->flags |= SN_FINST_L;
1646 }
1647}
1648
1649/* Handle server-side errors for default protocols. It is called whenever a a
1650 * connection setup is aborted or a request is aborted in queue. It sets the
1651 * session termination flags so that the caller does not have to worry about
1652 * them. It's installed as ->srv_error for the server-side stream_interface.
1653 */
1654void default_srv_error(struct session *s, struct stream_interface *si)
1655{
1656 int err_type = si->err_type;
1657 int err = 0, fin = 0;
1658
1659 if (err_type & SI_ET_QUEUE_ABRT) {
1660 err = SN_ERR_CLICL;
1661 fin = SN_FINST_Q;
1662 }
1663 else if (err_type & SI_ET_CONN_ABRT) {
1664 err = SN_ERR_CLICL;
1665 fin = SN_FINST_C;
1666 }
1667 else if (err_type & SI_ET_QUEUE_TO) {
1668 err = SN_ERR_SRVTO;
1669 fin = SN_FINST_Q;
1670 }
1671 else if (err_type & SI_ET_QUEUE_ERR) {
1672 err = SN_ERR_SRVCL;
1673 fin = SN_FINST_Q;
1674 }
1675 else if (err_type & SI_ET_CONN_TO) {
1676 err = SN_ERR_SRVTO;
1677 fin = SN_FINST_C;
1678 }
1679 else if (err_type & SI_ET_CONN_ERR) {
1680 err = SN_ERR_SRVCL;
1681 fin = SN_FINST_C;
1682 }
1683 else /* SI_ET_CONN_OTHER and others */ {
1684 err = SN_ERR_INTERNAL;
1685 fin = SN_FINST_C;
1686 }
1687
1688 if (!(s->flags & SN_ERR_MASK))
1689 s->flags |= err;
1690 if (!(s->flags & SN_FINST_MASK))
1691 s->flags |= fin;
1692}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001693
Willy Tarreaubaaee002006-06-26 02:48:02 +02001694/*
1695 * Local variables:
1696 * c-indent-level: 8
1697 * c-basic-offset: 8
1698 * End:
1699 */