blob: 10c7f13d613a52576ca1a3a2f135717d01e1ebc6 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 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 Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010029#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020030#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010032#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010033#include <proto/proto_http.h>
34#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020035#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010037#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010038#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010039#include <proto/stream_interface.h>
40#include <proto/stream_sock.h>
41#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020043struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010044struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau81f9aa32010-06-01 17:45:26 +020046/* This function is called from the protocol layer accept() in order to instanciate
47 * a new session on behalf of a given listener and frontend. It returns a positive
48 * value upon success, 0 if the connection needs to be closed and ignored, or a
49 * negative value upon critical failure.
50 */
51int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
52{
53 struct proxy *p = l->frontend;
54 struct session *s;
55 struct http_txn *txn;
56 struct task *t;
57
58 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
59 Alert("out of memory in event_accept().\n");
60 goto out_close;
61 }
62
63 /* minimum session initialization required for monitor mode below */
64 s->flags = 0;
65 s->logs.logwait = p->to_log;
66
67 /* if this session comes from a known monitoring system, we want to ignore
68 * it as soon as possible, which means closing it immediately for TCP, but
69 * cleanly.
70 */
71 if (unlikely((l->options & LI_O_CHK_MONNET) &&
72 addr->ss_family == AF_INET &&
73 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
74 if (p->mode == PR_MODE_TCP) {
75 pool_free2(pool2_session, s);
76 return 0;
77 }
78 s->flags |= SN_MONITOR;
79 s->logs.logwait = 0;
80 }
81
82 /* OK, we're keeping the session, so let's properly initialize the session */
83 LIST_ADDQ(&sessions, &s->list);
84 LIST_INIT(&s->back_refs);
85
86 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
87 Alert("out of memory in event_accept().\n");
88 goto out_free_session;
89 }
90
91 s->term_trace = 0;
92 s->cli_addr = *addr;
93 s->logs.accept_date = date; /* user-visible date for logging */
94 s->logs.tv_accept = now; /* corrected date for internal use */
95 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +020096 p->feconn++; /* beconn will be increased once assigned */
97
Willy Tarreaub36b4242010-06-04 20:59:39 +020098 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099
100 t->process = l->handler;
101 t->context = s;
102 t->nice = l->nice;
103 t->expire = TICK_ETERNITY;
104
105 s->task = t;
106 s->listener = l;
107
108 /* Note: initially, the session's backend points to the frontend.
109 * This changes later when switching rules are executed or
110 * when the default backend is assigned.
111 */
112 s->be = s->fe = p;
113 s->req = s->rep = NULL; /* will be allocated later */
114
115 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
116 * to abort right here as soon as possible, we check the rules before
117 * even initializing the stream interfaces.
118 */
119 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
120 task_free(t);
121 LIST_DEL(&s->list);
122 pool_free2(pool2_session, s);
123 /* let's do a no-linger now to close with a single RST. */
124 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200125 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 return 0;
127 }
128
Willy Tarreaub36b4242010-06-04 20:59:39 +0200129 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200130 if (p->feconn > p->counters.feconn_max)
131 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 proxy_inc_fe_sess_ctr(l, p);
133
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 /* this part should be common with other protocols */
135 s->si[0].fd = cfd;
136 s->si[0].owner = t;
137 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
138 s->si[0].err_type = SI_ET_NONE;
139 s->si[0].err_loc = NULL;
140 s->si[0].connect = NULL;
141 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200142 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200143 s->si[0].exp = TICK_ETERNITY;
144 s->si[0].flags = SI_FL_NONE;
145
146 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
147 s->si[0].flags |= SI_FL_INDEP_STR;
148
149 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
150 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
151
152 /* add the various callbacks */
153 stream_sock_prepare_interface(&s->si[0]);
154
155 /* pre-initialize the other side's stream interface to an INIT state. The
156 * callbacks will be initialized before attempting to connect.
157 */
158 s->si[1].fd = -1; /* just to help with debugging */
159 s->si[1].owner = t;
160 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
161 s->si[1].err_type = SI_ET_NONE;
162 s->si[1].err_loc = NULL;
163 s->si[1].connect = NULL;
164 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200165 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 s->si[1].shutr = stream_int_shutr;
167 s->si[1].shutw = stream_int_shutw;
168 s->si[1].exp = TICK_ETERNITY;
169 s->si[1].flags = SI_FL_NONE;
170
171 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
172 s->si[1].flags |= SI_FL_INDEP_STR;
173
174 s->srv = s->prev_srv = s->srv_conn = NULL;
175 s->pend_pos = NULL;
176
177 /* init store persistence */
178 s->store_count = 0;
Willy Tarreau5214be12010-06-14 15:32:18 +0200179 s->tracked_src_counters = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180
181 /* Adjust some socket options */
182 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
183 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
184 goto out_free_task;
185 }
186
187 txn = &s->txn;
188 /* Those variables will be checked and freed if non-NULL in
189 * session.c:session_free(). It is important that they are
190 * properly initialized.
191 */
192 txn->sessid = NULL;
193 txn->srv_cookie = NULL;
194 txn->cli_cookie = NULL;
195 txn->uri = NULL;
196 txn->req.cap = NULL;
197 txn->rsp.cap = NULL;
198 txn->hdr_idx.v = NULL;
199 txn->hdr_idx.size = txn->hdr_idx.used = 0;
200
201 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
202 goto out_free_task; /* no memory */
203
204 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
205 goto out_free_req; /* no memory */
206
207 /* initialize the request buffer */
208 s->req->size = global.tune.bufsize;
209 buffer_init(s->req);
210 s->req->prod = &s->si[0];
211 s->req->cons = &s->si[1];
212 s->si[0].ib = s->si[1].ob = s->req;
213 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
214
215 /* activate default analysers enabled for this listener */
216 s->req->analysers = l->analysers;
217
218 s->req->wto = TICK_ETERNITY;
219 s->req->rto = TICK_ETERNITY;
220 s->req->rex = TICK_ETERNITY;
221 s->req->wex = TICK_ETERNITY;
222 s->req->analyse_exp = TICK_ETERNITY;
223
224 /* initialize response buffer */
225 s->rep->size = global.tune.bufsize;
226 buffer_init(s->rep);
227 s->rep->prod = &s->si[1];
228 s->rep->cons = &s->si[0];
229 s->si[0].ob = s->si[1].ib = s->rep;
230 s->rep->analysers = 0;
231
232 s->rep->rto = TICK_ETERNITY;
233 s->rep->wto = TICK_ETERNITY;
234 s->rep->rex = TICK_ETERNITY;
235 s->rep->wex = TICK_ETERNITY;
236 s->rep->analyse_exp = TICK_ETERNITY;
237
238 /* finish initialization of the accepted file descriptor */
239 fd_insert(cfd);
240 fdtab[cfd].owner = &s->si[0];
241 fdtab[cfd].state = FD_STREADY;
242 fdtab[cfd].flags = 0;
243 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
244 fdtab[cfd].cb[DIR_RD].b = s->req;
245 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
246 fdtab[cfd].cb[DIR_WR].b = s->rep;
247 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
248 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
249 EV_FD_SET(cfd, DIR_RD);
250
251 if (p->accept) {
252 int ret = p->accept(s);
253 if (unlikely(ret < 0))
254 goto out_free_rep;
255
256 if (unlikely(ret == 0)) {
257 /* work is finished, we can release everything (eg: monitoring) */
258 pool_free2(pool2_buffer, s->rep);
259 pool_free2(pool2_buffer, s->req);
260 task_free(t);
261 LIST_DEL(&s->list);
262 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200263 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200264 return 0;
265 }
266 }
267
268 /* it is important not to call the wakeup function directly but to
269 * pass through task_wakeup(), because this one knows how to apply
270 * priorities to tasks.
271 */
272 task_wakeup(t, TASK_WOKEN_INIT);
273 return 1;
274
275 /* Error unrolling */
276 out_free_rep:
277 pool_free2(pool2_buffer, s->rep);
278 out_free_req:
279 pool_free2(pool2_buffer, s->req);
280 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200281 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200282 task_free(t);
283 out_free_session:
284 LIST_DEL(&s->list);
285 pool_free2(pool2_session, s);
286 out_close:
287 return -1;
288}
289
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290/*
291 * frees the context associated to a session. It must have been removed first.
292 */
293void session_free(struct session *s)
294{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100295 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200296 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100297 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200298 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100299
Willy Tarreaubaaee002006-06-26 02:48:02 +0200300 if (s->pend_pos)
301 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100302
Willy Tarreau1e62de62008-11-11 20:20:02 +0100303 if (s->srv) { /* there may be requests left pending in queue */
304 if (s->flags & SN_CURR_SESS) {
305 s->flags &= ~SN_CURR_SESS;
306 s->srv->cur_sess--;
307 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100308 if (may_dequeue_tasks(s->srv, s->be))
309 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100310 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100311
Willy Tarreau7c669d72008-06-20 15:04:11 +0200312 if (unlikely(s->srv_conn)) {
313 /* the session still has a reserved slot on a server, but
314 * it should normally be only the same as the one above,
315 * so this should not happen in fact.
316 */
317 sess_change_server(s, NULL);
318 }
319
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100320 if (s->req->pipe)
321 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100322
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100323 if (s->rep->pipe)
324 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100325
Willy Tarreau48d63db2008-08-03 17:41:33 +0200326 pool_free2(pool2_buffer, s->req);
327 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328
Willy Tarreau46023632010-01-07 22:51:47 +0100329 http_end_txn(s);
330
Willy Tarreaua4cda672010-06-06 18:28:49 +0200331 for (i = 0; i < s->store_count; i++) {
332 if (!s->store[i].ts)
333 continue;
334 stksess_free(s->store[i].table, s->store[i].ts);
335 s->store[i].ts = NULL;
336 }
337
Willy Tarreau92fb9832007-10-16 17:34:28 +0200338 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200339 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100340 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
341 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100343
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100344 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100345 /* we have to unlink all watchers. We must not relink them if
346 * this session was the last one in the list.
347 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100348 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100349 LIST_INIT(&bref->users);
350 if (s->list.n != &sessions)
351 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100352 bref->ref = s->list.n;
353 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100354 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200355 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200356
357 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200358 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200359 pool_flush2(pool2_buffer);
360 pool_flush2(fe->hdr_idx_pool);
361 pool_flush2(pool2_requri);
362 pool_flush2(pool2_capture);
363 pool_flush2(pool2_session);
364 pool_flush2(fe->req_cap_pool);
365 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200366 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200367}
368
369
370/* perform minimal intializations, report 0 in case of error, 1 if OK. */
371int init_session()
372{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100373 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200374 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
375 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376}
377
Willy Tarreau30e71012007-11-26 20:15:35 +0100378void session_process_counters(struct session *s)
379{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100380 unsigned long long bytes;
381
Willy Tarreau30e71012007-11-26 20:15:35 +0100382 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100383 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100384 s->logs.bytes_in = s->req->total;
385 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200386 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100387
Willy Tarreau30e71012007-11-26 20:15:35 +0100388 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200389 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100390
Willy Tarreau30e71012007-11-26 20:15:35 +0100391 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200392 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200393
394 if (s->listener->counters)
395 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100396 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100397 }
398
Willy Tarreau30e71012007-11-26 20:15:35 +0100399 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100400 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100401 s->logs.bytes_out = s->rep->total;
402 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200403 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100404
Willy Tarreau30e71012007-11-26 20:15:35 +0100405 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200406 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100407
Willy Tarreau30e71012007-11-26 20:15:35 +0100408 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200409 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200410
411 if (s->listener->counters)
412 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100413 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100414 }
415}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100417/* This function is called with (si->state == SI_ST_CON) meaning that a
418 * connection was attempted and that the file descriptor is already allocated.
419 * We must check for establishment, error and abort. Possible output states
420 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
421 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
422 * otherwise 1.
423 */
424int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
425{
426 struct buffer *req = si->ob;
427 struct buffer *rep = si->ib;
428
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100429 /* If we got an error, or if nothing happened and the connection timed
430 * out, we must give up. The CER state handler will take care of retry
431 * attempts and error reports.
432 */
433 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100434 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100435 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200436 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100437 fd_delete(si->fd);
438
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200439 if (si->release)
440 si->release(si);
441
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100442 if (si->err_type)
443 return 0;
444
445 si->err_loc = s->srv;
446 if (si->flags & SI_FL_ERR)
447 si->err_type = SI_ET_CONN_ERR;
448 else
449 si->err_type = SI_ET_CONN_TO;
450 return 0;
451 }
452
453 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200454 if (unlikely((rep->flags & BF_SHUTW) ||
455 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200456 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100457 s->be->options & PR_O_ABRT_CLOSE)))) {
458 /* give up */
459 si->shutw(si);
460 si->err_type |= SI_ET_CONN_ABRT;
461 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200462 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100463 if (s->srv_error)
464 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100465 return 1;
466 }
467
468 /* we need to wait a bit more if there was no activity either */
469 if (!(req->flags & BF_WRITE_ACTIVITY))
470 return 1;
471
472 /* OK, this means that a connection succeeded. The caller will be
473 * responsible for handling the transition from CON to EST.
474 */
475 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100476 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100477 si->state = SI_ST_EST;
478 si->err_type = SI_ET_NONE;
479 si->err_loc = NULL;
480 return 1;
481}
482
483/* This function is called with (si->state == SI_ST_CER) meaning that a
484 * previous connection attempt has failed and that the file descriptor
485 * has already been released. Possible causes include asynchronous error
486 * notification and time out. Possible output states are SI_ST_CLO when
487 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
488 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
489 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
490 * marked as in error state. It returns 0.
491 */
492int sess_update_st_cer(struct session *s, struct stream_interface *si)
493{
494 /* we probably have to release last session from the server */
495 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100496 health_adjust(s->srv, HANA_STATUS_L4_ERR);
497
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100498 if (s->flags & SN_CURR_SESS) {
499 s->flags &= ~SN_CURR_SESS;
500 s->srv->cur_sess--;
501 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100502 }
503
504 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200505 si->conn_retries--;
506 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100507 if (!si->err_type) {
508 si->err_type = SI_ET_CONN_ERR;
509 si->err_loc = s->srv;
510 }
511
512 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200513 s->srv->counters.failed_conns++;
514 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100515 if (may_dequeue_tasks(s->srv, s->be))
516 process_srv_queue(s->srv);
517
518 /* shutw is enough so stop a connecting socket */
519 si->shutw(si);
520 si->ob->flags |= BF_WRITE_ERROR;
521 si->ib->flags |= BF_READ_ERROR;
522
523 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100524 if (s->srv_error)
525 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100526 return 0;
527 }
528
529 /* If the "redispatch" option is set on the backend, we are allowed to
530 * retry on another server for the last retry. In order to achieve this,
531 * we must mark the session unassigned, and eventually clear the DIRECT
532 * bit to ignore any persistence cookie. We won't count a retry nor a
533 * redispatch yet, because this will depend on what server is selected.
534 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200535 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100536 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100537 if (may_dequeue_tasks(s->srv, s->be))
538 process_srv_queue(s->srv);
539
540 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
541 s->prev_srv = s->srv;
542 si->state = SI_ST_REQ;
543 } else {
544 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200545 s->srv->counters.retries++;
546 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 si->state = SI_ST_ASS;
548 }
549
550 if (si->flags & SI_FL_ERR) {
551 /* The error was an asynchronous connection error, and we will
552 * likely have to retry connecting to the same server, most
553 * likely leading to the same result. To avoid this, we wait
554 * one second before retrying.
555 */
556
557 if (!si->err_type)
558 si->err_type = SI_ET_CONN_ERR;
559
560 si->state = SI_ST_TAR;
561 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
562 return 0;
563 }
564 return 0;
565}
566
567/*
568 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200569 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
570 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 */
572void sess_establish(struct session *s, struct stream_interface *si)
573{
574 struct buffer *req = si->ob;
575 struct buffer *rep = si->ib;
576
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100577 if (s->srv)
578 health_adjust(s->srv, HANA_STATUS_L4_OK);
579
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 /* if the user wants to log as soon as possible, without counting
582 * bytes from the server, then this is the right moment. */
583 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
584 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100585 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100586 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 }
588 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
590 /* reset hdr_idx which was already initialized by the request.
591 * right now, the http parser does it.
592 * hdr_idx_init(&s->txn.hdr_idx);
593 */
594 }
595
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200596 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200598 if (si->connect) {
599 /* real connections have timeouts */
600 req->wto = s->be->timeout.server;
601 rep->rto = s->be->timeout.server;
602 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 req->wex = TICK_ETERNITY;
604}
605
606/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
607 * Other input states are simply ignored.
608 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
609 * Flags must have previously been updated for timeouts and other conditions.
610 */
611void sess_update_stream_int(struct session *s, struct stream_interface *si)
612{
613 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",
614 now_ms, __FUNCTION__,
615 s,
616 s->req, s->rep,
617 s->req->rex, s->rep->wex,
618 s->req->flags, s->rep->flags,
619 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
620
621 if (si->state == SI_ST_ASS) {
622 /* Server assigned to connection request, we have to try to connect now */
623 int conn_err;
624
625 conn_err = connect_server(s);
626 if (conn_err == SN_ERR_NONE) {
627 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100628 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100629 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630 return;
631 }
632
633 /* We have received a synchronous error. We might have to
634 * abort, retry immediately or redispatch.
635 */
636 if (conn_err == SN_ERR_INTERNAL) {
637 if (!si->err_type) {
638 si->err_type = SI_ET_CONN_OTHER;
639 si->err_loc = s->srv;
640 }
641
642 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100643 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200645 s->srv->counters.failed_conns++;
646 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647
648 /* release other sessions waiting for this server */
649 if (may_dequeue_tasks(s->srv, s->be))
650 process_srv_queue(s->srv);
651
652 /* Failed and not retryable. */
653 si->shutr(si);
654 si->shutw(si);
655 si->ob->flags |= BF_WRITE_ERROR;
656
657 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
658
659 /* no session was ever accounted for this server */
660 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100661 if (s->srv_error)
662 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663 return;
664 }
665
666 /* We are facing a retryable error, but we don't want to run a
667 * turn-around now, as the problem is likely a source port
668 * allocation problem, so we want to retry now.
669 */
670 si->state = SI_ST_CER;
671 si->flags &= ~SI_FL_ERR;
672 sess_update_st_cer(s, si);
673 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
674 return;
675 }
676 else if (si->state == SI_ST_QUE) {
677 /* connection request was queued, check for any update */
678 if (!s->pend_pos) {
679 /* The connection is not in the queue anymore. Either
680 * we have a server connection slot available and we
681 * go directly to the assigned state, or we need to
682 * load-balance first and go to the INI state.
683 */
684 si->exp = TICK_ETERNITY;
685 if (unlikely(!(s->flags & SN_ASSIGNED)))
686 si->state = SI_ST_REQ;
687 else {
688 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
689 si->state = SI_ST_ASS;
690 }
691 return;
692 }
693
694 /* Connection request still in queue... */
695 if (si->flags & SI_FL_EXP) {
696 /* ... and timeout expired */
697 si->exp = TICK_ETERNITY;
698 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
699 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200700 s->srv->counters.failed_conns++;
701 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 si->shutr(si);
703 si->shutw(si);
704 si->ob->flags |= BF_WRITE_TIMEOUT;
705 if (!si->err_type)
706 si->err_type = SI_ET_QUEUE_TO;
707 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100708 if (s->srv_error)
709 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 return;
711 }
712
713 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200714 if ((si->ob->flags & (BF_READ_ERROR)) ||
715 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200716 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 /* give up */
718 si->exp = TICK_ETERNITY;
719 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
720 si->shutr(si);
721 si->shutw(si);
722 si->err_type |= SI_ET_QUEUE_ABRT;
723 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100724 if (s->srv_error)
725 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 return;
727 }
728
729 /* Nothing changed */
730 return;
731 }
732 else if (si->state == SI_ST_TAR) {
733 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200734 if ((si->ob->flags & (BF_READ_ERROR)) ||
735 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200736 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 /* give up */
738 si->exp = TICK_ETERNITY;
739 si->shutr(si);
740 si->shutw(si);
741 si->err_type |= SI_ET_CONN_ABRT;
742 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100743 if (s->srv_error)
744 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 return;
746 }
747
748 if (!(si->flags & SI_FL_EXP))
749 return; /* still in turn-around */
750
751 si->exp = TICK_ETERNITY;
752
753 /* we keep trying on the same server as long as the session is
754 * marked "assigned".
755 * FIXME: Should we force a redispatch attempt when the server is down ?
756 */
757 if (s->flags & SN_ASSIGNED)
758 si->state = SI_ST_ASS;
759 else
760 si->state = SI_ST_REQ;
761 return;
762 }
763}
764
765/* This function initiates a server connection request on a stream interface
766 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
767 * indicating that a server has been assigned. It may also return SI_ST_QUE,
768 * or SI_ST_CLO upon error.
769 */
770static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
771 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",
772 now_ms, __FUNCTION__,
773 s,
774 s->req, s->rep,
775 s->req->rex, s->rep->wex,
776 s->req->flags, s->rep->flags,
777 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
778
779 if (si->state != SI_ST_REQ)
780 return;
781
782 /* Try to assign a server */
783 if (srv_redispatch_connect(s) != 0) {
784 /* We did not get a server. Either we queued the
785 * connection request, or we encountered an error.
786 */
787 if (si->state == SI_ST_QUE)
788 return;
789
790 /* we did not get any server, let's check the cause */
791 si->shutr(si);
792 si->shutw(si);
793 si->ob->flags |= BF_WRITE_ERROR;
794 if (!si->err_type)
795 si->err_type = SI_ET_CONN_OTHER;
796 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100797 if (s->srv_error)
798 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100799 return;
800 }
801
802 /* The server is assigned */
803 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
804 si->state = SI_ST_ASS;
805}
806
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200807/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100808 * if appropriate. The default_backend rule is also considered, then the
809 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200810 * It returns 1 if the processing can continue on next analysers, or zero if it
811 * either needs more data or wants to immediately abort the request.
812 */
813int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
814{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200815 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100816
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200817 req->analysers &= ~an_bit;
818 req->analyse_exp = TICK_ETERNITY;
819
820 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
821 now_ms, __FUNCTION__,
822 s,
823 req,
824 req->rex, req->wex,
825 req->flags,
826 req->l,
827 req->analysers);
828
829 /* now check whether we have some switching rules for this request */
830 if (!(s->flags & SN_BE_ASSIGNED)) {
831 struct switching_rule *rule;
832
833 list_for_each_entry(rule, &s->fe->switching_rules, list) {
834 int ret;
835
836 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
837 ret = acl_pass(ret);
838 if (rule->cond->pol == ACL_COND_UNLESS)
839 ret = !ret;
840
841 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200842 if (!session_set_backend(s, rule->be.backend))
843 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200844 break;
845 }
846 }
847
848 /* To ensure correct connection accounting on the backend, we
849 * have to assign one if it was not set (eg: a listen). This
850 * measure also takes care of correctly setting the default
851 * backend if any.
852 */
853 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200854 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
855 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200856 }
857
858 /* we don't want to run the HTTP filters again if the backend has not changed */
859 if (s->fe == s->be)
860 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
861
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200862 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +0100863 * persistence rule, and report that in the session.
864 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200865 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100866 int ret = 1;
867
868 if (prst_rule->cond) {
869 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
870 ret = acl_pass(ret);
871 if (prst_rule->cond->pol == ACL_COND_UNLESS)
872 ret = !ret;
873 }
874
875 if (ret) {
876 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200877 if (prst_rule->type == PERSIST_TYPE_FORCE) {
878 s->flags |= SN_FORCE_PRST;
879 } else {
880 s->flags |= SN_IGNORE_PRST;
881 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100882 break;
883 }
884 }
885
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200886 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200887
888 sw_failed:
889 /* immediately abort this request in case of allocation failure */
890 buffer_abort(s->req);
891 buffer_abort(s->rep);
892
893 if (!(s->flags & SN_ERR_MASK))
894 s->flags |= SN_ERR_RESOURCE;
895 if (!(s->flags & SN_FINST_MASK))
896 s->flags |= SN_FINST_R;
897
898 s->txn.status = 500;
899 s->req->analysers = 0;
900 s->req->analyse_exp = TICK_ETERNITY;
901 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200902}
903
Emeric Brun1d33b292010-01-04 15:47:17 +0100904/* This stream analyser works on a request. It applies all sticking rules on
905 * it then returns 1. The data must already be present in the buffer otherwise
906 * they won't match. It always returns 1.
907 */
908int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
909{
910 struct proxy *px = s->be;
911 struct sticking_rule *rule;
912
913 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
914 now_ms, __FUNCTION__,
915 s,
916 req,
917 req->rex, req->wex,
918 req->flags,
919 req->l,
920 req->analysers);
921
922 list_for_each_entry(rule, &px->sticking_rules, list) {
923 int ret = 1 ;
924 int i;
925
926 for (i = 0; i < s->store_count; i++) {
927 if (rule->table.t == s->store[i].table)
928 break;
929 }
930
931 if (i != s->store_count)
932 continue;
933
934 if (rule->cond) {
935 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
936 ret = acl_pass(ret);
937 if (rule->cond->pol == ACL_COND_UNLESS)
938 ret = !ret;
939 }
940
941 if (ret) {
942 struct stktable_key *key;
943
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200944 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +0100945 if (!key)
946 continue;
947
948 if (rule->flags & STK_IS_MATCH) {
949 struct stksess *ts;
950
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200951 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100952 if (!(s->flags & SN_ASSIGNED)) {
953 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +0200954 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +0100955
956 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +0200957 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
958 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +0100959 if (node) {
960 struct server *srv;
961
962 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100963 if ((srv->state & SRV_RUNNING) ||
964 (px->options & PR_O_PERSIST) ||
965 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100966 s->flags |= SN_DIRECT | SN_ASSIGNED;
967 s->srv = srv;
968 }
969 }
970 }
Willy Tarreaue8f63382010-07-13 15:20:24 +0200971 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +0100972 }
973 }
974 if (rule->flags & STK_IS_STORE) {
975 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
976 struct stksess *ts;
977
978 ts = stksess_new(rule->table.t, key);
979 if (ts) {
980 s->store[s->store_count].table = rule->table.t;
981 s->store[s->store_count++].ts = ts;
982 }
983 }
984 }
985 }
986 }
987
988 req->analysers &= ~an_bit;
989 req->analyse_exp = TICK_ETERNITY;
990 return 1;
991}
992
993/* This stream analyser works on a response. It applies all store rules on it
994 * then returns 1. The data must already be present in the buffer otherwise
995 * they won't match. It always returns 1.
996 */
997int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
998{
999 struct proxy *px = s->be;
1000 struct sticking_rule *rule;
1001 int i;
1002
1003 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1004 now_ms, __FUNCTION__,
1005 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001006 rep,
1007 rep->rex, rep->wex,
1008 rep->flags,
1009 rep->l,
1010 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001011
1012 list_for_each_entry(rule, &px->storersp_rules, list) {
1013 int ret = 1 ;
1014 int storereqidx = -1;
1015
1016 for (i = 0; i < s->store_count; i++) {
1017 if (rule->table.t == s->store[i].table) {
1018 if (!(s->store[i].flags))
1019 storereqidx = i;
1020 break;
1021 }
1022 }
1023
1024 if ((i != s->store_count) && (storereqidx == -1))
1025 continue;
1026
1027 if (rule->cond) {
1028 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1029 ret = acl_pass(ret);
1030 if (rule->cond->pol == ACL_COND_UNLESS)
1031 ret = !ret;
1032 }
1033
1034 if (ret) {
1035 struct stktable_key *key;
1036
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001037 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001038 if (!key)
1039 continue;
1040
1041 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001042 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001043 s->store[storereqidx].flags = 1;
1044 }
1045 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1046 struct stksess *ts;
1047
1048 ts = stksess_new(rule->table.t, key);
1049 if (ts) {
1050 s->store[s->store_count].table = rule->table.t;
1051 s->store[s->store_count].flags = 1;
1052 s->store[s->store_count++].ts = ts;
1053 }
1054 }
1055 }
1056 }
1057
1058 /* process store request and store response */
1059 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001060 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001061 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001062
1063 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1064 if (ts) {
1065 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001066 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001067 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001068 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001069 else
1070 ts = stktable_store(s->store[i].table, s->store[i].ts);
1071
1072 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001073 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1074 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001075 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001076 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001077
1078 rep->analysers &= ~an_bit;
1079 rep->analyse_exp = TICK_ETERNITY;
1080 return 1;
1081}
1082
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001083/* This macro is very specific to the function below. See the comments in
1084 * process_session() below to understand the logic and the tests.
1085 */
1086#define UPDATE_ANALYSERS(real, list, back, flag) { \
1087 list = (((list) & ~(flag)) | ~(back)) & (real); \
1088 back = real; \
1089 if (!(list)) \
1090 break; \
1091 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1092 continue; \
1093}
1094
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001095/* Processes the client, server, request and response jobs of a session task,
1096 * then puts it back to the wait queue in a clean state, or cleans up its
1097 * resources if it must be deleted. Returns in <next> the date the task wants
1098 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1099 * nothing too many times, the request and response buffers flags are monitored
1100 * and each function is called only if at least another function has changed at
1101 * least one flag it is interested in.
1102 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001103struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001104{
1105 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001106 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001107 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001108
1109 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1110 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1111
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001112 /* this data may be no longer valid, clear it */
1113 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1114
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001115 /* This flag must explicitly be set every time */
1116 s->req->flags &= ~BF_READ_NOEXP;
1117
1118 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1119 rqf_last = s->req->flags;
1120 rpf_last = s->rep->flags;
1121
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001122 /* we don't want the stream interface functions to recursively wake us up */
1123 if (s->req->prod->owner == t)
1124 s->req->prod->flags |= SI_FL_DONT_WAKE;
1125 if (s->req->cons->owner == t)
1126 s->req->cons->flags |= SI_FL_DONT_WAKE;
1127
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001128 /* 1a: Check for low level timeouts if needed. We just set a flag on
1129 * stream interfaces when their timeouts have expired.
1130 */
1131 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1132 stream_int_check_timeouts(&s->si[0]);
1133 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001134
1135 /* check buffer timeouts, and close the corresponding stream interfaces
1136 * for future reads or writes. Note: this will also concern upper layers
1137 * but we do not touch any other flag. We must be careful and correctly
1138 * detect state changes when calling them.
1139 */
1140
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001141 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001142
Willy Tarreau14641402009-12-29 14:49:56 +01001143 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1144 s->req->cons->flags |= SI_FL_NOLINGER;
1145 s->req->cons->shutw(s->req->cons);
1146 }
1147
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001148 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1149 s->req->prod->shutr(s->req->prod);
1150
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001151 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001152
Willy Tarreau14641402009-12-29 14:49:56 +01001153 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1154 s->rep->cons->flags |= SI_FL_NOLINGER;
1155 s->rep->cons->shutw(s->rep->cons);
1156 }
1157
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001158 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1159 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001160 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001161
1162 /* 1b: check for low-level errors reported at the stream interface.
1163 * First we check if it's a retryable error (in which case we don't
1164 * want to tell the buffer). Otherwise we report the error one level
1165 * upper by setting flags into the buffers. Note that the side towards
1166 * the client cannot have connect (hence retryable) errors. Also, the
1167 * connection setup code must be able to deal with any type of abort.
1168 */
1169 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1170 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1171 s->si[0].shutr(&s->si[0]);
1172 s->si[0].shutw(&s->si[0]);
1173 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001174 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001175 s->be->counters.cli_aborts++;
1176 if (s->srv)
1177 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001178 if (!(s->flags & SN_ERR_MASK))
1179 s->flags |= SN_ERR_CLICL;
1180 if (!(s->flags & SN_FINST_MASK))
1181 s->flags |= SN_FINST_D;
1182 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001183 }
1184 }
1185
1186 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1187 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1188 s->si[1].shutr(&s->si[1]);
1189 s->si[1].shutw(&s->si[1]);
1190 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001191 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001192 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001193 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001194 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001195 s->be->counters.srv_aborts++;
1196 if (s->srv)
1197 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001198 if (!(s->flags & SN_ERR_MASK))
1199 s->flags |= SN_ERR_SRVCL;
1200 if (!(s->flags & SN_FINST_MASK))
1201 s->flags |= SN_FINST_D;
1202 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001203 }
1204 /* note: maybe we should process connection errors here ? */
1205 }
1206
1207 if (s->si[1].state == SI_ST_CON) {
1208 /* we were trying to establish a connection on the server side,
1209 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1210 */
1211 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1212 sess_update_st_cer(s, &s->si[1]);
1213 else if (s->si[1].state == SI_ST_EST)
1214 sess_establish(s, &s->si[1]);
1215
1216 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1217 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1218 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1219 */
1220 }
1221
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001222resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001223 /* Check for connection closure */
1224
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001225 DPRINTF(stderr,
1226 "[%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",
1227 now_ms, __FUNCTION__, __LINE__,
1228 t,
1229 s, s->flags,
1230 s->req, s->rep,
1231 s->req->rex, s->rep->wex,
1232 s->req->flags, s->rep->flags,
1233 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1234 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001235 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001236
1237 /* nothing special to be done on client side */
1238 if (unlikely(s->req->prod->state == SI_ST_DIS))
1239 s->req->prod->state = SI_ST_CLO;
1240
1241 /* When a server-side connection is released, we have to count it and
1242 * check for pending connections on this server.
1243 */
1244 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1245 s->req->cons->state = SI_ST_CLO;
1246 if (s->srv) {
1247 if (s->flags & SN_CURR_SESS) {
1248 s->flags &= ~SN_CURR_SESS;
1249 s->srv->cur_sess--;
1250 }
1251 sess_change_server(s, NULL);
1252 if (may_dequeue_tasks(s->srv, s->be))
1253 process_srv_queue(s->srv);
1254 }
1255 }
1256
1257 /*
1258 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1259 * at this point.
1260 */
1261
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001262 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001263 /* Analyse request */
1264 if ((s->req->flags & BF_MASK_ANALYSER) ||
1265 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
1266 unsigned int flags = s->req->flags;
1267
1268 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001269 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001270 unsigned int ana_list;
1271 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001272
Willy Tarreau90deb182010-01-07 00:20:41 +01001273 /* it's up to the analysers to stop new connections,
1274 * disable reading or closing. Note: if an analyser
1275 * disables any of these bits, it is responsible for
1276 * enabling them again when it disables itself, so
1277 * that other analysers are called in similar conditions.
1278 */
1279 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001280 buffer_auto_connect(s->req);
1281 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001282
1283 /* We will call all analysers for which a bit is set in
1284 * s->req->analysers, following the bit order from LSB
1285 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001286 * the list when not needed. Any analyser may return 0
1287 * to break out of the loop, either because of missing
1288 * data to take a decision, or because it decides to
1289 * kill the session. We loop at least once through each
1290 * analyser, and we may loop again if other analysers
1291 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001292 *
1293 * We build a list of analysers to run. We evaluate all
1294 * of these analysers in the order of the lower bit to
1295 * the higher bit. This ordering is very important.
1296 * An analyser will often add/remove other analysers,
1297 * including itself. Any changes to itself have no effect
1298 * on the loop. If it removes any other analysers, we
1299 * want those analysers not to be called anymore during
1300 * this loop. If it adds an analyser that is located
1301 * after itself, we want it to be scheduled for being
1302 * processed during the loop. If it adds an analyser
1303 * which is located before it, we want it to switch to
1304 * it immediately, even if it has already been called
1305 * once but removed since.
1306 *
1307 * In order to achieve this, we compare the analyser
1308 * list after the call with a copy of it before the
1309 * call. The work list is fed with analyser bits that
1310 * appeared during the call. Then we compare previous
1311 * work list with the new one, and check the bits that
1312 * appeared. If the lowest of these bits is lower than
1313 * the current bit, it means we have enabled a previous
1314 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001315 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001316
1317 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001318 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001319 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001320
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001321 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001322 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001323 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001324 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001325 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001326
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001327 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001328 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001329 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001330 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001331 }
1332
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001333 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001334 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1335 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001336 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001337 }
1338
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001339 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001340 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1341 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001342 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001343 }
1344
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001345 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001346 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1347 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001348 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001349 }
1350
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001351 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001352 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001353 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001354 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001355 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001356
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001357 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001358 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1359 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001360 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001361 }
1362
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001363 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001364 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001365 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001366 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001367 }
Emeric Brun647caf12009-06-30 17:57:00 +02001368
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001369 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001370 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1371 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001372 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001373 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001374
Emeric Brun1d33b292010-01-04 15:47:17 +01001375 if (ana_list & AN_REQ_STICKING_RULES) {
1376 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1377 break;
1378 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1379 }
1380
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001381 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001382 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1383 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001384 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001385 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001386 break;
1387 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001388 }
Willy Tarreau84455332009-03-15 22:34:05 +01001389
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001390 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
1391 rqf_last = s->req->flags;
1392 goto resync_request;
1393 }
1394 }
1395
Willy Tarreau576507f2010-01-07 00:09:04 +01001396 /* we'll monitor the request analysers while parsing the response,
1397 * because some response analysers may indirectly enable new request
1398 * analysers (eg: HTTP keep-alive).
1399 */
1400 req_ana_back = s->req->analysers;
1401
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001402 resync_response:
1403 /* Analyse response */
1404
1405 if (unlikely(s->rep->flags & BF_HIJACK)) {
1406 /* In inject mode, we wake up everytime something has
1407 * happened on the write side of the buffer.
1408 */
1409 unsigned int flags = s->rep->flags;
1410
1411 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1412 !(s->rep->flags & BF_FULL)) {
1413 s->rep->hijacker(s, s->rep);
1414 }
1415
1416 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1417 rpf_last = s->rep->flags;
1418 goto resync_response;
1419 }
1420 }
1421 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
1422 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
1423 unsigned int flags = s->rep->flags;
1424
1425 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001426 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001427 unsigned int ana_list;
1428 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001429
Willy Tarreau90deb182010-01-07 00:20:41 +01001430 /* it's up to the analysers to stop disable reading or
1431 * closing. Note: if an analyser disables any of these
1432 * bits, it is responsible for enabling them again when
1433 * it disables itself, so that other analysers are called
1434 * in similar conditions.
1435 */
1436 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001437 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001438
1439 /* We will call all analysers for which a bit is set in
1440 * s->rep->analysers, following the bit order from LSB
1441 * to MSB. The analysers must remove themselves from
1442 * the list when not needed. Any analyser may return 0
1443 * to break out of the loop, either because of missing
1444 * data to take a decision, or because it decides to
1445 * kill the session. We loop at least once through each
1446 * analyser, and we may loop again if other analysers
1447 * are added in the middle.
1448 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001449
1450 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001451 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001452 /* Warning! ensure that analysers are always placed in ascending order! */
1453
1454 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001455 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1456 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001458 }
1459
Emeric Brun1d33b292010-01-04 15:47:17 +01001460 if (ana_list & AN_RES_STORE_RULES) {
1461 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1462 break;
1463 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1464 }
1465
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001466 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001467 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1468 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001469 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001470 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001471
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001472 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001473 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1474 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001475 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001476 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001477 break;
1478 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001479 }
1480
1481 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1482 rpf_last = s->rep->flags;
1483 goto resync_response;
1484 }
1485 }
1486
Willy Tarreau576507f2010-01-07 00:09:04 +01001487 /* maybe someone has added some request analysers, so we must check and loop */
1488 if (s->req->analysers & ~req_ana_back)
1489 goto resync_request;
1490
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001491 /* FIXME: here we should call protocol handlers which rely on
1492 * both buffers.
1493 */
1494
1495
1496 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001497 * Now we propagate unhandled errors to the session. Normally
1498 * we're just in a data phase here since it means we have not
1499 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001500 */
1501 if (!(s->flags & SN_ERR_MASK)) {
1502 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1503 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001504 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001505 if (s->req->flags & BF_READ_ERROR) {
1506 s->be->counters.cli_aborts++;
1507 if (s->srv)
1508 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001509 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001510 }
1511 else if (s->req->flags & BF_READ_TIMEOUT) {
1512 s->be->counters.cli_aborts++;
1513 if (s->srv)
1514 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001515 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001516 }
1517 else if (s->req->flags & BF_WRITE_ERROR) {
1518 s->be->counters.srv_aborts++;
1519 if (s->srv)
1520 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001521 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001522 }
1523 else {
1524 s->be->counters.srv_aborts++;
1525 if (s->srv)
1526 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001527 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001528 }
Willy Tarreau84455332009-03-15 22:34:05 +01001529 sess_set_term_flags(s);
1530 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001531 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1532 /* Report it if the server got an error or a read timeout expired */
1533 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001534 if (s->rep->flags & BF_READ_ERROR) {
1535 s->be->counters.srv_aborts++;
1536 if (s->srv)
1537 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001538 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001539 }
1540 else if (s->rep->flags & BF_READ_TIMEOUT) {
1541 s->be->counters.srv_aborts++;
1542 if (s->srv)
1543 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001544 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001545 }
1546 else if (s->rep->flags & BF_WRITE_ERROR) {
1547 s->be->counters.cli_aborts++;
1548 if (s->srv)
1549 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001550 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001551 }
1552 else {
1553 s->be->counters.cli_aborts++;
1554 if (s->srv)
1555 s->srv->counters.cli_aborts++;
1556 s->flags |= SN_ERR_CLITO;
1557 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001558 sess_set_term_flags(s);
1559 }
Willy Tarreau84455332009-03-15 22:34:05 +01001560 }
1561
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001562 /*
1563 * Here we take care of forwarding unhandled data. This also includes
1564 * connection establishments and shutdown requests.
1565 */
1566
1567
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001568 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001569 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001570 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001571 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001572 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001573 (s->req->prod->state >= SI_ST_EST) &&
1574 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001575 /* This buffer is freewheeling, there's no analyser nor hijacker
1576 * attached to it. If any data are left in, we'll permit them to
1577 * move.
1578 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001579 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001580 buffer_auto_connect(s->req);
1581 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001582 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001583
Willy Tarreau31971e52009-09-20 12:07:52 +02001584 /* If the producer is still connected, we'll enable data to flow
1585 * from the producer to the consumer (which might possibly not be
1586 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001587 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001588 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1589 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001590 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001591
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001592 /* check if it is wise to enable kernel splicing to forward request data */
1593 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1594 s->req->to_forward &&
1595 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001596 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001597 (pipes_used < global.maxpipes) &&
1598 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1599 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1600 (s->req->flags & BF_STREAMER_FAST)))) {
1601 s->req->flags |= BF_KERN_SPLICING;
1602 }
1603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001604 /* reflect what the L7 analysers have seen last */
1605 rqf_last = s->req->flags;
1606
1607 /*
1608 * Now forward all shutdown requests between both sides of the buffer
1609 */
1610
Willy Tarreau520d95e2009-09-19 21:04:57 +02001611 /* first, let's check if the request buffer needs to shutdown(write), which may
1612 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001613 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001614 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001615 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001616 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001617 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001618
1619 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001620 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 +01001621 s->req->cons->shutw(s->req->cons);
1622
1623 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001624 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1625 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001626 buffer_shutr_now(s->req);
1627
1628 /* shutdown(read) pending */
1629 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1630 s->req->prod->shutr(s->req->prod);
1631
Willy Tarreau520d95e2009-09-19 21:04:57 +02001632 /* it's possible that an upper layer has requested a connection setup or abort.
1633 * There are 2 situations where we decide to establish a new connection :
1634 * - there are data scheduled for emission in the buffer
1635 * - the BF_AUTO_CONNECT flag is set (active connection)
1636 */
1637 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001638 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001639 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001640 /* If we have an iohandler without a connect method, we immediately
1641 * switch to the connected state, otherwise we perform a connection
1642 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001643 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001644 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001645 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001646 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001647 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001648 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1649 s->req->wex = TICK_ETERNITY;
1650 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001651 }
Willy Tarreau73201222009-08-16 18:27:24 +02001652 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001653 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001654 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001655 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1656 buffer_shutr_now(s->rep);
1657 }
Willy Tarreau92795622009-03-06 12:51:23 +01001658 }
1659
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001660
1661 /* we may have a pending connection request, or a connection waiting
1662 * for completion.
1663 */
1664 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1665 do {
1666 /* nb: step 1 might switch from QUE to ASS, but we first want
1667 * to give a chance to step 2 to perform a redirect if needed.
1668 */
1669 if (s->si[1].state != SI_ST_REQ)
1670 sess_update_stream_int(s, &s->si[1]);
1671 if (s->si[1].state == SI_ST_REQ)
1672 sess_prepare_conn_req(s, &s->si[1]);
1673
1674 if (s->si[1].state == SI_ST_ASS && s->srv &&
1675 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1676 perform_http_redirect(s, &s->si[1]);
1677 } while (s->si[1].state == SI_ST_ASS);
1678 }
1679
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001680 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001681 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001682 goto resync_stream_interface;
1683
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001684 /* otherwise wewant to check if we need to resync the req buffer or not */
1685 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001686 goto resync_request;
1687
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001688 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001689
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001690 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001691 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001692 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001693 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001694 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001695 (s->rep->prod->state >= SI_ST_EST) &&
1696 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001697 /* This buffer is freewheeling, there's no analyser nor hijacker
1698 * attached to it. If any data are left in, we'll permit them to
1699 * move.
1700 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001701 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001702 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001703 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001704 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1705 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001706 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001707
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001708 /* check if it is wise to enable kernel splicing to forward response data */
1709 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1710 s->rep->to_forward &&
1711 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001712 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001713 (pipes_used < global.maxpipes) &&
1714 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1715 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1716 (s->rep->flags & BF_STREAMER_FAST)))) {
1717 s->rep->flags |= BF_KERN_SPLICING;
1718 }
1719
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001720 /* reflect what the L7 analysers have seen last */
1721 rpf_last = s->rep->flags;
1722
1723 /*
1724 * Now forward all shutdown requests between both sides of the buffer
1725 */
1726
1727 /*
1728 * FIXME: this is probably where we should produce error responses.
1729 */
1730
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001731 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001732 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1733 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001734 buffer_shutw_now(s->rep);
1735
1736 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001737 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 +01001738 s->rep->cons->shutw(s->rep->cons);
1739
1740 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001741 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1742 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001743 buffer_shutr_now(s->rep);
1744
1745 /* shutdown(read) pending */
1746 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1747 s->rep->prod->shutr(s->rep->prod);
1748
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001749 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001750 goto resync_stream_interface;
1751
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001752 if (s->req->flags != rqf_last)
1753 goto resync_request;
1754
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001755 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001756 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001757
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001758 /* we're interested in getting wakeups again */
1759 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1760 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1761
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001762 /* This is needed only when debugging is enabled, to indicate
1763 * client-side or server-side close. Please note that in the unlikely
1764 * event where both sides would close at once, the sequence is reported
1765 * on the server side first.
1766 */
1767 if (unlikely((global.mode & MODE_DEBUG) &&
1768 (!(global.mode & MODE_QUIET) ||
1769 (global.mode & MODE_VERBOSE)))) {
1770 int len;
1771
1772 if (s->si[1].state == SI_ST_CLO &&
1773 s->si[1].prev_state == SI_ST_EST) {
1774 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1775 s->uniq_id, s->be->id,
1776 (unsigned short)s->si[0].fd,
1777 (unsigned short)s->si[1].fd);
1778 write(1, trash, len);
1779 }
1780
1781 if (s->si[0].state == SI_ST_CLO &&
1782 s->si[0].prev_state == SI_ST_EST) {
1783 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1784 s->uniq_id, s->be->id,
1785 (unsigned short)s->si[0].fd,
1786 (unsigned short)s->si[1].fd);
1787 write(1, trash, len);
1788 }
1789 }
1790
1791 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1792 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1793
1794 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1795 session_process_counters(s);
1796
Willy Tarreau1accfc02009-09-05 20:57:35 +02001797 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001798 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001799
Willy Tarreau1accfc02009-09-05 20:57:35 +02001800 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001801 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001802
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001803 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1804 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001805 s->si[0].prev_state = s->si[0].state;
1806 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001807 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1808 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001809
1810 /* Trick: if a request is being waiting for the server to respond,
1811 * and if we know the server can timeout, we don't want the timeout
1812 * to expire on the client side first, but we're still interested
1813 * in passing data from the client to the server (eg: POST). Thus,
1814 * we can cancel the client's request timeout if the server's
1815 * request timeout is set and the server has not yet sent a response.
1816 */
1817
Willy Tarreau520d95e2009-09-19 21:04:57 +02001818 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001819 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1820 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001821 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001822 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001823
Willy Tarreau1accfc02009-09-05 20:57:35 +02001824 /* Call the second stream interface's I/O handler if it's embedded.
1825 * Note that this one may wake the task up again.
1826 */
1827 if (s->req->cons->iohandler) {
1828 s->req->cons->iohandler(s->req->cons);
1829 if (task_in_rq(t)) {
1830 /* If we woke up, we don't want to requeue the
1831 * task to the wait queue, but rather requeue
1832 * it into the runqueue ASAP.
1833 */
1834 t->expire = TICK_ETERNITY;
1835 return t;
1836 }
1837 }
1838
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001839 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1840 tick_first(s->rep->rex, s->rep->wex));
1841 if (s->req->analysers)
1842 t->expire = tick_first(t->expire, s->req->analyse_exp);
1843
1844 if (s->si[0].exp)
1845 t->expire = tick_first(t->expire, s->si[0].exp);
1846
1847 if (s->si[1].exp)
1848 t->expire = tick_first(t->expire, s->si[1].exp);
1849
1850#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001851 fprintf(stderr,
1852 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1853 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1854 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1855 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 +01001856#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001857
1858#ifdef DEBUG_DEV
1859 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001860 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001861 ABORT_NOW();
1862#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001863 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001864 }
1865
1866 s->fe->feconn--;
1867 if (s->flags & SN_BE_ASSIGNED)
1868 s->be->beconn--;
1869 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001870 s->listener->nbconn--;
1871 if (s->listener->state == LI_FULL &&
1872 s->listener->nbconn < s->listener->maxconn) {
1873 /* we should reactivate the listener */
1874 EV_FD_SET(s->listener->fd, DIR_RD);
1875 s->listener->state = LI_READY;
1876 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001877
1878 if (unlikely((global.mode & MODE_DEBUG) &&
1879 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1880 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001881 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001882 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001883 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001884 write(1, trash, len);
1885 }
1886
1887 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1888 session_process_counters(s);
1889
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001890 if (s->txn.status) {
1891 int n;
1892
1893 n = s->txn.status / 100;
1894 if (n < 1 || n > 5)
1895 n = 0;
1896
1897 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001898 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001899
Willy Tarreau24657792010-02-26 10:30:28 +01001900 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001901 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001902 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001903 }
1904
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 /* let's do a final log if we need it */
1906 if (s->logs.logwait &&
1907 !(s->flags & SN_MONITOR) &&
1908 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001909 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001910 }
1911
1912 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001913 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001914 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001915 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001916 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001917}
1918
Willy Tarreau7c669d72008-06-20 15:04:11 +02001919/*
1920 * This function adjusts sess->srv_conn and maintains the previous and new
1921 * server's served session counts. Setting newsrv to NULL is enough to release
1922 * current connection slot. This function also notifies any LB algo which might
1923 * expect to be informed about any change in the number of active sessions on a
1924 * server.
1925 */
1926void sess_change_server(struct session *sess, struct server *newsrv)
1927{
1928 if (sess->srv_conn == newsrv)
1929 return;
1930
1931 if (sess->srv_conn) {
1932 sess->srv_conn->served--;
1933 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1934 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1935 sess->srv_conn = NULL;
1936 }
1937
1938 if (newsrv) {
1939 newsrv->served++;
1940 if (newsrv->proxy->lbprm.server_take_conn)
1941 newsrv->proxy->lbprm.server_take_conn(newsrv);
1942 sess->srv_conn = newsrv;
1943 }
1944}
1945
Willy Tarreau84455332009-03-15 22:34:05 +01001946/* Set correct session termination flags in case no analyser has done it. It
1947 * also counts a failed request if the server state has not reached the request
1948 * stage.
1949 */
1950void sess_set_term_flags(struct session *s)
1951{
1952 if (!(s->flags & SN_FINST_MASK)) {
1953 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001954
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001955 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001956 if (s->listener->counters)
1957 s->listener->counters->failed_req++;
1958
Willy Tarreau84455332009-03-15 22:34:05 +01001959 s->flags |= SN_FINST_R;
1960 }
1961 else if (s->si[1].state == SI_ST_QUE)
1962 s->flags |= SN_FINST_Q;
1963 else if (s->si[1].state < SI_ST_EST)
1964 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01001965 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01001966 s->flags |= SN_FINST_D;
1967 else
1968 s->flags |= SN_FINST_L;
1969 }
1970}
1971
1972/* Handle server-side errors for default protocols. It is called whenever a a
1973 * connection setup is aborted or a request is aborted in queue. It sets the
1974 * session termination flags so that the caller does not have to worry about
1975 * them. It's installed as ->srv_error for the server-side stream_interface.
1976 */
1977void default_srv_error(struct session *s, struct stream_interface *si)
1978{
1979 int err_type = si->err_type;
1980 int err = 0, fin = 0;
1981
1982 if (err_type & SI_ET_QUEUE_ABRT) {
1983 err = SN_ERR_CLICL;
1984 fin = SN_FINST_Q;
1985 }
1986 else if (err_type & SI_ET_CONN_ABRT) {
1987 err = SN_ERR_CLICL;
1988 fin = SN_FINST_C;
1989 }
1990 else if (err_type & SI_ET_QUEUE_TO) {
1991 err = SN_ERR_SRVTO;
1992 fin = SN_FINST_Q;
1993 }
1994 else if (err_type & SI_ET_QUEUE_ERR) {
1995 err = SN_ERR_SRVCL;
1996 fin = SN_FINST_Q;
1997 }
1998 else if (err_type & SI_ET_CONN_TO) {
1999 err = SN_ERR_SRVTO;
2000 fin = SN_FINST_C;
2001 }
2002 else if (err_type & SI_ET_CONN_ERR) {
2003 err = SN_ERR_SRVCL;
2004 fin = SN_FINST_C;
2005 }
2006 else /* SI_ET_CONN_OTHER and others */ {
2007 err = SN_ERR_INTERNAL;
2008 fin = SN_FINST_C;
2009 }
2010
2011 if (!(s->flags & SN_ERR_MASK))
2012 s->flags |= err;
2013 if (!(s->flags & SN_FINST_MASK))
2014 s->flags |= fin;
2015}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002016
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017/*
2018 * Local variables:
2019 * c-indent-level: 8
2020 * c-basic-offset: 8
2021 * End:
2022 */