blob: 873201c68ddfedae8d88a657003b7b41b230d310 [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;
142 s->si[0].exp = TICK_ETERNITY;
143 s->si[0].flags = SI_FL_NONE;
144
145 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
146 s->si[0].flags |= SI_FL_INDEP_STR;
147
148 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
149 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
150
151 /* add the various callbacks */
152 stream_sock_prepare_interface(&s->si[0]);
153
154 /* pre-initialize the other side's stream interface to an INIT state. The
155 * callbacks will be initialized before attempting to connect.
156 */
157 s->si[1].fd = -1; /* just to help with debugging */
158 s->si[1].owner = t;
159 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
160 s->si[1].err_type = SI_ET_NONE;
161 s->si[1].err_loc = NULL;
162 s->si[1].connect = NULL;
163 s->si[1].iohandler = NULL;
164 s->si[1].shutr = stream_int_shutr;
165 s->si[1].shutw = stream_int_shutw;
166 s->si[1].exp = TICK_ETERNITY;
167 s->si[1].flags = SI_FL_NONE;
168
169 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
170 s->si[1].flags |= SI_FL_INDEP_STR;
171
172 s->srv = s->prev_srv = s->srv_conn = NULL;
173 s->pend_pos = NULL;
174
175 /* init store persistence */
176 s->store_count = 0;
177
178 /* Adjust some socket options */
179 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
180 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
181 goto out_free_task;
182 }
183
184 txn = &s->txn;
185 /* Those variables will be checked and freed if non-NULL in
186 * session.c:session_free(). It is important that they are
187 * properly initialized.
188 */
189 txn->sessid = NULL;
190 txn->srv_cookie = NULL;
191 txn->cli_cookie = NULL;
192 txn->uri = NULL;
193 txn->req.cap = NULL;
194 txn->rsp.cap = NULL;
195 txn->hdr_idx.v = NULL;
196 txn->hdr_idx.size = txn->hdr_idx.used = 0;
197
198 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
199 goto out_free_task; /* no memory */
200
201 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
202 goto out_free_req; /* no memory */
203
204 /* initialize the request buffer */
205 s->req->size = global.tune.bufsize;
206 buffer_init(s->req);
207 s->req->prod = &s->si[0];
208 s->req->cons = &s->si[1];
209 s->si[0].ib = s->si[1].ob = s->req;
210 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
211
212 /* activate default analysers enabled for this listener */
213 s->req->analysers = l->analysers;
214
215 s->req->wto = TICK_ETERNITY;
216 s->req->rto = TICK_ETERNITY;
217 s->req->rex = TICK_ETERNITY;
218 s->req->wex = TICK_ETERNITY;
219 s->req->analyse_exp = TICK_ETERNITY;
220
221 /* initialize response buffer */
222 s->rep->size = global.tune.bufsize;
223 buffer_init(s->rep);
224 s->rep->prod = &s->si[1];
225 s->rep->cons = &s->si[0];
226 s->si[0].ob = s->si[1].ib = s->rep;
227 s->rep->analysers = 0;
228
229 s->rep->rto = TICK_ETERNITY;
230 s->rep->wto = TICK_ETERNITY;
231 s->rep->rex = TICK_ETERNITY;
232 s->rep->wex = TICK_ETERNITY;
233 s->rep->analyse_exp = TICK_ETERNITY;
234
235 /* finish initialization of the accepted file descriptor */
236 fd_insert(cfd);
237 fdtab[cfd].owner = &s->si[0];
238 fdtab[cfd].state = FD_STREADY;
239 fdtab[cfd].flags = 0;
240 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
241 fdtab[cfd].cb[DIR_RD].b = s->req;
242 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
243 fdtab[cfd].cb[DIR_WR].b = s->rep;
244 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
245 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
246 EV_FD_SET(cfd, DIR_RD);
247
248 if (p->accept) {
249 int ret = p->accept(s);
250 if (unlikely(ret < 0))
251 goto out_free_rep;
252
253 if (unlikely(ret == 0)) {
254 /* work is finished, we can release everything (eg: monitoring) */
255 pool_free2(pool2_buffer, s->rep);
256 pool_free2(pool2_buffer, s->req);
257 task_free(t);
258 LIST_DEL(&s->list);
259 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200260 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200261 return 0;
262 }
263 }
264
265 /* it is important not to call the wakeup function directly but to
266 * pass through task_wakeup(), because this one knows how to apply
267 * priorities to tasks.
268 */
269 task_wakeup(t, TASK_WOKEN_INIT);
270 return 1;
271
272 /* Error unrolling */
273 out_free_rep:
274 pool_free2(pool2_buffer, s->rep);
275 out_free_req:
276 pool_free2(pool2_buffer, s->req);
277 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200278 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200279 task_free(t);
280 out_free_session:
281 LIST_DEL(&s->list);
282 pool_free2(pool2_session, s);
283 out_close:
284 return -1;
285}
286
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287/*
288 * frees the context associated to a session. It must have been removed first.
289 */
290void session_free(struct session *s)
291{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100292 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200293 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100294 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200295 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100296
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297 if (s->pend_pos)
298 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100299
Willy Tarreau1e62de62008-11-11 20:20:02 +0100300 if (s->srv) { /* there may be requests left pending in queue */
301 if (s->flags & SN_CURR_SESS) {
302 s->flags &= ~SN_CURR_SESS;
303 s->srv->cur_sess--;
304 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100305 if (may_dequeue_tasks(s->srv, s->be))
306 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100307 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100308
Willy Tarreau7c669d72008-06-20 15:04:11 +0200309 if (unlikely(s->srv_conn)) {
310 /* the session still has a reserved slot on a server, but
311 * it should normally be only the same as the one above,
312 * so this should not happen in fact.
313 */
314 sess_change_server(s, NULL);
315 }
316
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100317 if (s->req->pipe)
318 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100319
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100320 if (s->rep->pipe)
321 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100322
Willy Tarreau48d63db2008-08-03 17:41:33 +0200323 pool_free2(pool2_buffer, s->req);
324 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325
Willy Tarreau46023632010-01-07 22:51:47 +0100326 http_end_txn(s);
327
Willy Tarreaua4cda672010-06-06 18:28:49 +0200328 for (i = 0; i < s->store_count; i++) {
329 if (!s->store[i].ts)
330 continue;
331 stksess_free(s->store[i].table, s->store[i].ts);
332 s->store[i].ts = NULL;
333 }
334
Willy Tarreau92fb9832007-10-16 17:34:28 +0200335 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200336 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100337 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
338 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100340
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100341 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100342 /* we have to unlink all watchers. We must not relink them if
343 * this session was the last one in the list.
344 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100345 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100346 LIST_INIT(&bref->users);
347 if (s->list.n != &sessions)
348 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100349 bref->ref = s->list.n;
350 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100351 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200352 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200353
354 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200355 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200356 pool_flush2(pool2_buffer);
357 pool_flush2(fe->hdr_idx_pool);
358 pool_flush2(pool2_requri);
359 pool_flush2(pool2_capture);
360 pool_flush2(pool2_session);
361 pool_flush2(fe->req_cap_pool);
362 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200363 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200364}
365
366
367/* perform minimal intializations, report 0 in case of error, 1 if OK. */
368int init_session()
369{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100370 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200371 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
372 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373}
374
Willy Tarreau30e71012007-11-26 20:15:35 +0100375void session_process_counters(struct session *s)
376{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100377 unsigned long long bytes;
378
Willy Tarreau30e71012007-11-26 20:15:35 +0100379 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100380 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100381 s->logs.bytes_in = s->req->total;
382 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200383 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100384
Willy Tarreau30e71012007-11-26 20:15:35 +0100385 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200386 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100387
Willy Tarreau30e71012007-11-26 20:15:35 +0100388 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200389 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200390
391 if (s->listener->counters)
392 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100393 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100394 }
395
Willy Tarreau30e71012007-11-26 20:15:35 +0100396 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100397 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100398 s->logs.bytes_out = s->rep->total;
399 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200400 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100401
Willy Tarreau30e71012007-11-26 20:15:35 +0100402 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200403 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100404
Willy Tarreau30e71012007-11-26 20:15:35 +0100405 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200406 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200407
408 if (s->listener->counters)
409 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100410 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100411 }
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100414/* This function is called with (si->state == SI_ST_CON) meaning that a
415 * connection was attempted and that the file descriptor is already allocated.
416 * We must check for establishment, error and abort. Possible output states
417 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
418 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
419 * otherwise 1.
420 */
421int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
422{
423 struct buffer *req = si->ob;
424 struct buffer *rep = si->ib;
425
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100426 /* If we got an error, or if nothing happened and the connection timed
427 * out, we must give up. The CER state handler will take care of retry
428 * attempts and error reports.
429 */
430 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100431 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100432 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200433 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100434 fd_delete(si->fd);
435
436 if (si->err_type)
437 return 0;
438
439 si->err_loc = s->srv;
440 if (si->flags & SI_FL_ERR)
441 si->err_type = SI_ET_CONN_ERR;
442 else
443 si->err_type = SI_ET_CONN_TO;
444 return 0;
445 }
446
447 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200448 if (unlikely((rep->flags & BF_SHUTW) ||
449 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200450 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100451 s->be->options & PR_O_ABRT_CLOSE)))) {
452 /* give up */
453 si->shutw(si);
454 si->err_type |= SI_ET_CONN_ABRT;
455 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200456 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100457 if (s->srv_error)
458 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100459 return 1;
460 }
461
462 /* we need to wait a bit more if there was no activity either */
463 if (!(req->flags & BF_WRITE_ACTIVITY))
464 return 1;
465
466 /* OK, this means that a connection succeeded. The caller will be
467 * responsible for handling the transition from CON to EST.
468 */
469 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100470 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100471 si->state = SI_ST_EST;
472 si->err_type = SI_ET_NONE;
473 si->err_loc = NULL;
474 return 1;
475}
476
477/* This function is called with (si->state == SI_ST_CER) meaning that a
478 * previous connection attempt has failed and that the file descriptor
479 * has already been released. Possible causes include asynchronous error
480 * notification and time out. Possible output states are SI_ST_CLO when
481 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
482 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
483 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
484 * marked as in error state. It returns 0.
485 */
486int sess_update_st_cer(struct session *s, struct stream_interface *si)
487{
488 /* we probably have to release last session from the server */
489 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100490 health_adjust(s->srv, HANA_STATUS_L4_ERR);
491
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100492 if (s->flags & SN_CURR_SESS) {
493 s->flags &= ~SN_CURR_SESS;
494 s->srv->cur_sess--;
495 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100496 }
497
498 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200499 si->conn_retries--;
500 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100501 if (!si->err_type) {
502 si->err_type = SI_ET_CONN_ERR;
503 si->err_loc = s->srv;
504 }
505
506 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200507 s->srv->counters.failed_conns++;
508 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100509 if (may_dequeue_tasks(s->srv, s->be))
510 process_srv_queue(s->srv);
511
512 /* shutw is enough so stop a connecting socket */
513 si->shutw(si);
514 si->ob->flags |= BF_WRITE_ERROR;
515 si->ib->flags |= BF_READ_ERROR;
516
517 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100518 if (s->srv_error)
519 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100520 return 0;
521 }
522
523 /* If the "redispatch" option is set on the backend, we are allowed to
524 * retry on another server for the last retry. In order to achieve this,
525 * we must mark the session unassigned, and eventually clear the DIRECT
526 * bit to ignore any persistence cookie. We won't count a retry nor a
527 * redispatch yet, because this will depend on what server is selected.
528 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200529 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100530 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100531 if (may_dequeue_tasks(s->srv, s->be))
532 process_srv_queue(s->srv);
533
534 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
535 s->prev_srv = s->srv;
536 si->state = SI_ST_REQ;
537 } else {
538 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200539 s->srv->counters.retries++;
540 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 si->state = SI_ST_ASS;
542 }
543
544 if (si->flags & SI_FL_ERR) {
545 /* The error was an asynchronous connection error, and we will
546 * likely have to retry connecting to the same server, most
547 * likely leading to the same result. To avoid this, we wait
548 * one second before retrying.
549 */
550
551 if (!si->err_type)
552 si->err_type = SI_ET_CONN_ERR;
553
554 si->state = SI_ST_TAR;
555 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
556 return 0;
557 }
558 return 0;
559}
560
561/*
562 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200563 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
564 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100565 */
566void sess_establish(struct session *s, struct stream_interface *si)
567{
568 struct buffer *req = si->ob;
569 struct buffer *rep = si->ib;
570
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100571 if (s->srv)
572 health_adjust(s->srv, HANA_STATUS_L4_OK);
573
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100574 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 /* if the user wants to log as soon as possible, without counting
576 * bytes from the server, then this is the right moment. */
577 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
578 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100579 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 }
582 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
584 /* reset hdr_idx which was already initialized by the request.
585 * right now, the http parser does it.
586 * hdr_idx_init(&s->txn.hdr_idx);
587 */
588 }
589
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200590 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100591 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200592 if (si->connect) {
593 /* real connections have timeouts */
594 req->wto = s->be->timeout.server;
595 rep->rto = s->be->timeout.server;
596 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 req->wex = TICK_ETERNITY;
598}
599
600/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
601 * Other input states are simply ignored.
602 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
603 * Flags must have previously been updated for timeouts and other conditions.
604 */
605void sess_update_stream_int(struct session *s, struct stream_interface *si)
606{
607 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",
608 now_ms, __FUNCTION__,
609 s,
610 s->req, s->rep,
611 s->req->rex, s->rep->wex,
612 s->req->flags, s->rep->flags,
613 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
614
615 if (si->state == SI_ST_ASS) {
616 /* Server assigned to connection request, we have to try to connect now */
617 int conn_err;
618
619 conn_err = connect_server(s);
620 if (conn_err == SN_ERR_NONE) {
621 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100622 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100623 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100624 return;
625 }
626
627 /* We have received a synchronous error. We might have to
628 * abort, retry immediately or redispatch.
629 */
630 if (conn_err == SN_ERR_INTERNAL) {
631 if (!si->err_type) {
632 si->err_type = SI_ET_CONN_OTHER;
633 si->err_loc = s->srv;
634 }
635
636 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100637 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200639 s->srv->counters.failed_conns++;
640 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100641
642 /* release other sessions waiting for this server */
643 if (may_dequeue_tasks(s->srv, s->be))
644 process_srv_queue(s->srv);
645
646 /* Failed and not retryable. */
647 si->shutr(si);
648 si->shutw(si);
649 si->ob->flags |= BF_WRITE_ERROR;
650
651 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
652
653 /* no session was ever accounted for this server */
654 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100655 if (s->srv_error)
656 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657 return;
658 }
659
660 /* We are facing a retryable error, but we don't want to run a
661 * turn-around now, as the problem is likely a source port
662 * allocation problem, so we want to retry now.
663 */
664 si->state = SI_ST_CER;
665 si->flags &= ~SI_FL_ERR;
666 sess_update_st_cer(s, si);
667 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
668 return;
669 }
670 else if (si->state == SI_ST_QUE) {
671 /* connection request was queued, check for any update */
672 if (!s->pend_pos) {
673 /* The connection is not in the queue anymore. Either
674 * we have a server connection slot available and we
675 * go directly to the assigned state, or we need to
676 * load-balance first and go to the INI state.
677 */
678 si->exp = TICK_ETERNITY;
679 if (unlikely(!(s->flags & SN_ASSIGNED)))
680 si->state = SI_ST_REQ;
681 else {
682 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
683 si->state = SI_ST_ASS;
684 }
685 return;
686 }
687
688 /* Connection request still in queue... */
689 if (si->flags & SI_FL_EXP) {
690 /* ... and timeout expired */
691 si->exp = TICK_ETERNITY;
692 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
693 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200694 s->srv->counters.failed_conns++;
695 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 si->shutr(si);
697 si->shutw(si);
698 si->ob->flags |= BF_WRITE_TIMEOUT;
699 if (!si->err_type)
700 si->err_type = SI_ET_QUEUE_TO;
701 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100702 if (s->srv_error)
703 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 return;
705 }
706
707 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200708 if ((si->ob->flags & (BF_READ_ERROR)) ||
709 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200710 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 /* give up */
712 si->exp = TICK_ETERNITY;
713 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
714 si->shutr(si);
715 si->shutw(si);
716 si->err_type |= SI_ET_QUEUE_ABRT;
717 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100718 if (s->srv_error)
719 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 return;
721 }
722
723 /* Nothing changed */
724 return;
725 }
726 else if (si->state == SI_ST_TAR) {
727 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200728 if ((si->ob->flags & (BF_READ_ERROR)) ||
729 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200730 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100731 /* give up */
732 si->exp = TICK_ETERNITY;
733 si->shutr(si);
734 si->shutw(si);
735 si->err_type |= SI_ET_CONN_ABRT;
736 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100737 if (s->srv_error)
738 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 return;
740 }
741
742 if (!(si->flags & SI_FL_EXP))
743 return; /* still in turn-around */
744
745 si->exp = TICK_ETERNITY;
746
747 /* we keep trying on the same server as long as the session is
748 * marked "assigned".
749 * FIXME: Should we force a redispatch attempt when the server is down ?
750 */
751 if (s->flags & SN_ASSIGNED)
752 si->state = SI_ST_ASS;
753 else
754 si->state = SI_ST_REQ;
755 return;
756 }
757}
758
759/* This function initiates a server connection request on a stream interface
760 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
761 * indicating that a server has been assigned. It may also return SI_ST_QUE,
762 * or SI_ST_CLO upon error.
763 */
764static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
765 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",
766 now_ms, __FUNCTION__,
767 s,
768 s->req, s->rep,
769 s->req->rex, s->rep->wex,
770 s->req->flags, s->rep->flags,
771 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
772
773 if (si->state != SI_ST_REQ)
774 return;
775
776 /* Try to assign a server */
777 if (srv_redispatch_connect(s) != 0) {
778 /* We did not get a server. Either we queued the
779 * connection request, or we encountered an error.
780 */
781 if (si->state == SI_ST_QUE)
782 return;
783
784 /* we did not get any server, let's check the cause */
785 si->shutr(si);
786 si->shutw(si);
787 si->ob->flags |= BF_WRITE_ERROR;
788 if (!si->err_type)
789 si->err_type = SI_ET_CONN_OTHER;
790 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100791 if (s->srv_error)
792 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100793 return;
794 }
795
796 /* The server is assigned */
797 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
798 si->state = SI_ST_ASS;
799}
800
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200801/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100802 * if appropriate. The default_backend rule is also considered, then the
803 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200804 * It returns 1 if the processing can continue on next analysers, or zero if it
805 * either needs more data or wants to immediately abort the request.
806 */
807int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
808{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200809 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200811 req->analysers &= ~an_bit;
812 req->analyse_exp = TICK_ETERNITY;
813
814 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
815 now_ms, __FUNCTION__,
816 s,
817 req,
818 req->rex, req->wex,
819 req->flags,
820 req->l,
821 req->analysers);
822
823 /* now check whether we have some switching rules for this request */
824 if (!(s->flags & SN_BE_ASSIGNED)) {
825 struct switching_rule *rule;
826
827 list_for_each_entry(rule, &s->fe->switching_rules, list) {
828 int ret;
829
830 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
831 ret = acl_pass(ret);
832 if (rule->cond->pol == ACL_COND_UNLESS)
833 ret = !ret;
834
835 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200836 if (!session_set_backend(s, rule->be.backend))
837 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200838 break;
839 }
840 }
841
842 /* To ensure correct connection accounting on the backend, we
843 * have to assign one if it was not set (eg: a listen). This
844 * measure also takes care of correctly setting the default
845 * backend if any.
846 */
847 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200848 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
849 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200850 }
851
852 /* we don't want to run the HTTP filters again if the backend has not changed */
853 if (s->fe == s->be)
854 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
855
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200856 /* 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 +0100857 * persistence rule, and report that in the session.
858 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200859 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100860 int ret = 1;
861
862 if (prst_rule->cond) {
863 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
864 ret = acl_pass(ret);
865 if (prst_rule->cond->pol == ACL_COND_UNLESS)
866 ret = !ret;
867 }
868
869 if (ret) {
870 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200871 if (prst_rule->type == PERSIST_TYPE_FORCE) {
872 s->flags |= SN_FORCE_PRST;
873 } else {
874 s->flags |= SN_IGNORE_PRST;
875 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100876 break;
877 }
878 }
879
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200880 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200881
882 sw_failed:
883 /* immediately abort this request in case of allocation failure */
884 buffer_abort(s->req);
885 buffer_abort(s->rep);
886
887 if (!(s->flags & SN_ERR_MASK))
888 s->flags |= SN_ERR_RESOURCE;
889 if (!(s->flags & SN_FINST_MASK))
890 s->flags |= SN_FINST_R;
891
892 s->txn.status = 500;
893 s->req->analysers = 0;
894 s->req->analyse_exp = TICK_ETERNITY;
895 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200896}
897
Emeric Brun1d33b292010-01-04 15:47:17 +0100898/* This stream analyser works on a request. It applies all sticking rules on
899 * it then returns 1. The data must already be present in the buffer otherwise
900 * they won't match. It always returns 1.
901 */
902int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
903{
904 struct proxy *px = s->be;
905 struct sticking_rule *rule;
906
907 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
908 now_ms, __FUNCTION__,
909 s,
910 req,
911 req->rex, req->wex,
912 req->flags,
913 req->l,
914 req->analysers);
915
916 list_for_each_entry(rule, &px->sticking_rules, list) {
917 int ret = 1 ;
918 int i;
919
920 for (i = 0; i < s->store_count; i++) {
921 if (rule->table.t == s->store[i].table)
922 break;
923 }
924
925 if (i != s->store_count)
926 continue;
927
928 if (rule->cond) {
929 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
930 ret = acl_pass(ret);
931 if (rule->cond->pol == ACL_COND_UNLESS)
932 ret = !ret;
933 }
934
935 if (ret) {
936 struct stktable_key *key;
937
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200938 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +0100939 if (!key)
940 continue;
941
942 if (rule->flags & STK_IS_MATCH) {
943 struct stksess *ts;
944
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200945 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100946 if (!(s->flags & SN_ASSIGNED)) {
947 struct eb32_node *node;
948
949 /* srv found in table */
950 node = eb32_lookup(&px->conf.used_server_id, ts->sid);
951 if (node) {
952 struct server *srv;
953
954 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100955 if ((srv->state & SRV_RUNNING) ||
956 (px->options & PR_O_PERSIST) ||
957 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100958 s->flags |= SN_DIRECT | SN_ASSIGNED;
959 s->srv = srv;
960 }
961 }
962 }
963 ts->expire = tick_add(now_ms, MS_TO_TICKS(rule->table.t->expire));
964 }
965 }
966 if (rule->flags & STK_IS_STORE) {
967 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
968 struct stksess *ts;
969
970 ts = stksess_new(rule->table.t, key);
971 if (ts) {
972 s->store[s->store_count].table = rule->table.t;
973 s->store[s->store_count++].ts = ts;
974 }
975 }
976 }
977 }
978 }
979
980 req->analysers &= ~an_bit;
981 req->analyse_exp = TICK_ETERNITY;
982 return 1;
983}
984
985/* This stream analyser works on a response. It applies all store rules on it
986 * then returns 1. The data must already be present in the buffer otherwise
987 * they won't match. It always returns 1.
988 */
989int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
990{
991 struct proxy *px = s->be;
992 struct sticking_rule *rule;
993 int i;
994
995 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
996 now_ms, __FUNCTION__,
997 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +0100998 rep,
999 rep->rex, rep->wex,
1000 rep->flags,
1001 rep->l,
1002 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001003
1004 list_for_each_entry(rule, &px->storersp_rules, list) {
1005 int ret = 1 ;
1006 int storereqidx = -1;
1007
1008 for (i = 0; i < s->store_count; i++) {
1009 if (rule->table.t == s->store[i].table) {
1010 if (!(s->store[i].flags))
1011 storereqidx = i;
1012 break;
1013 }
1014 }
1015
1016 if ((i != s->store_count) && (storereqidx == -1))
1017 continue;
1018
1019 if (rule->cond) {
1020 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1021 ret = acl_pass(ret);
1022 if (rule->cond->pol == ACL_COND_UNLESS)
1023 ret = !ret;
1024 }
1025
1026 if (ret) {
1027 struct stktable_key *key;
1028
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001029 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001030 if (!key)
1031 continue;
1032
1033 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001034 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001035 s->store[storereqidx].flags = 1;
1036 }
1037 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1038 struct stksess *ts;
1039
1040 ts = stksess_new(rule->table.t, key);
1041 if (ts) {
1042 s->store[s->store_count].table = rule->table.t;
1043 s->store[s->store_count].flags = 1;
1044 s->store[s->store_count++].ts = ts;
1045 }
1046 }
1047 }
1048 }
1049
1050 /* process store request and store response */
1051 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001052 struct stksess *ts;
1053
1054 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1055 if (ts) {
1056 /* the entry already existed, we can free ours */
Emeric Brun1d33b292010-01-04 15:47:17 +01001057 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001058 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001059 else
1060 ts = stktable_store(s->store[i].table, s->store[i].ts);
1061
1062 s->store[i].ts = NULL;
1063 ts->sid = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001064 }
1065
1066 rep->analysers &= ~an_bit;
1067 rep->analyse_exp = TICK_ETERNITY;
1068 return 1;
1069}
1070
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001071/* This macro is very specific to the function below. See the comments in
1072 * process_session() below to understand the logic and the tests.
1073 */
1074#define UPDATE_ANALYSERS(real, list, back, flag) { \
1075 list = (((list) & ~(flag)) | ~(back)) & (real); \
1076 back = real; \
1077 if (!(list)) \
1078 break; \
1079 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1080 continue; \
1081}
1082
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083/* Processes the client, server, request and response jobs of a session task,
1084 * then puts it back to the wait queue in a clean state, or cleans up its
1085 * resources if it must be deleted. Returns in <next> the date the task wants
1086 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1087 * nothing too many times, the request and response buffers flags are monitored
1088 * and each function is called only if at least another function has changed at
1089 * least one flag it is interested in.
1090 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001091struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001092{
1093 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001094 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001095 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001096
1097 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1098 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1099
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001100 /* this data may be no longer valid, clear it */
1101 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1102
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001103 /* This flag must explicitly be set every time */
1104 s->req->flags &= ~BF_READ_NOEXP;
1105
1106 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1107 rqf_last = s->req->flags;
1108 rpf_last = s->rep->flags;
1109
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001110 /* we don't want the stream interface functions to recursively wake us up */
1111 if (s->req->prod->owner == t)
1112 s->req->prod->flags |= SI_FL_DONT_WAKE;
1113 if (s->req->cons->owner == t)
1114 s->req->cons->flags |= SI_FL_DONT_WAKE;
1115
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001116 /* 1a: Check for low level timeouts if needed. We just set a flag on
1117 * stream interfaces when their timeouts have expired.
1118 */
1119 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1120 stream_int_check_timeouts(&s->si[0]);
1121 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001122
1123 /* check buffer timeouts, and close the corresponding stream interfaces
1124 * for future reads or writes. Note: this will also concern upper layers
1125 * but we do not touch any other flag. We must be careful and correctly
1126 * detect state changes when calling them.
1127 */
1128
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001130
Willy Tarreau14641402009-12-29 14:49:56 +01001131 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1132 s->req->cons->flags |= SI_FL_NOLINGER;
1133 s->req->cons->shutw(s->req->cons);
1134 }
1135
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001136 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1137 s->req->prod->shutr(s->req->prod);
1138
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001139 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001140
Willy Tarreau14641402009-12-29 14:49:56 +01001141 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1142 s->rep->cons->flags |= SI_FL_NOLINGER;
1143 s->rep->cons->shutw(s->rep->cons);
1144 }
1145
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001146 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1147 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001148 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001149
1150 /* 1b: check for low-level errors reported at the stream interface.
1151 * First we check if it's a retryable error (in which case we don't
1152 * want to tell the buffer). Otherwise we report the error one level
1153 * upper by setting flags into the buffers. Note that the side towards
1154 * the client cannot have connect (hence retryable) errors. Also, the
1155 * connection setup code must be able to deal with any type of abort.
1156 */
1157 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1158 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1159 s->si[0].shutr(&s->si[0]);
1160 s->si[0].shutw(&s->si[0]);
1161 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001162 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001163 s->be->counters.cli_aborts++;
1164 if (s->srv)
1165 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001166 if (!(s->flags & SN_ERR_MASK))
1167 s->flags |= SN_ERR_CLICL;
1168 if (!(s->flags & SN_FINST_MASK))
1169 s->flags |= SN_FINST_D;
1170 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001171 }
1172 }
1173
1174 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1175 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1176 s->si[1].shutr(&s->si[1]);
1177 s->si[1].shutw(&s->si[1]);
1178 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001179 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001180 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001181 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001182 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001183 s->be->counters.srv_aborts++;
1184 if (s->srv)
1185 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001186 if (!(s->flags & SN_ERR_MASK))
1187 s->flags |= SN_ERR_SRVCL;
1188 if (!(s->flags & SN_FINST_MASK))
1189 s->flags |= SN_FINST_D;
1190 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001191 }
1192 /* note: maybe we should process connection errors here ? */
1193 }
1194
1195 if (s->si[1].state == SI_ST_CON) {
1196 /* we were trying to establish a connection on the server side,
1197 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1198 */
1199 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1200 sess_update_st_cer(s, &s->si[1]);
1201 else if (s->si[1].state == SI_ST_EST)
1202 sess_establish(s, &s->si[1]);
1203
1204 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1205 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1206 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1207 */
1208 }
1209
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001210resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001211 /* Check for connection closure */
1212
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213 DPRINTF(stderr,
1214 "[%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",
1215 now_ms, __FUNCTION__, __LINE__,
1216 t,
1217 s, s->flags,
1218 s->req, s->rep,
1219 s->req->rex, s->rep->wex,
1220 s->req->flags, s->rep->flags,
1221 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1222 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001223 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001224
1225 /* nothing special to be done on client side */
1226 if (unlikely(s->req->prod->state == SI_ST_DIS))
1227 s->req->prod->state = SI_ST_CLO;
1228
1229 /* When a server-side connection is released, we have to count it and
1230 * check for pending connections on this server.
1231 */
1232 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1233 s->req->cons->state = SI_ST_CLO;
1234 if (s->srv) {
1235 if (s->flags & SN_CURR_SESS) {
1236 s->flags &= ~SN_CURR_SESS;
1237 s->srv->cur_sess--;
1238 }
1239 sess_change_server(s, NULL);
1240 if (may_dequeue_tasks(s->srv, s->be))
1241 process_srv_queue(s->srv);
1242 }
1243 }
1244
1245 /*
1246 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1247 * at this point.
1248 */
1249
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001250 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001251 /* Analyse request */
1252 if ((s->req->flags & BF_MASK_ANALYSER) ||
1253 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
1254 unsigned int flags = s->req->flags;
1255
1256 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001257 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001258 unsigned int ana_list;
1259 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001260
Willy Tarreau90deb182010-01-07 00:20:41 +01001261 /* it's up to the analysers to stop new connections,
1262 * disable reading or closing. Note: if an analyser
1263 * disables any of these bits, it is responsible for
1264 * enabling them again when it disables itself, so
1265 * that other analysers are called in similar conditions.
1266 */
1267 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001268 buffer_auto_connect(s->req);
1269 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001270
1271 /* We will call all analysers for which a bit is set in
1272 * s->req->analysers, following the bit order from LSB
1273 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001274 * the list when not needed. Any analyser may return 0
1275 * to break out of the loop, either because of missing
1276 * data to take a decision, or because it decides to
1277 * kill the session. We loop at least once through each
1278 * analyser, and we may loop again if other analysers
1279 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001280 *
1281 * We build a list of analysers to run. We evaluate all
1282 * of these analysers in the order of the lower bit to
1283 * the higher bit. This ordering is very important.
1284 * An analyser will often add/remove other analysers,
1285 * including itself. Any changes to itself have no effect
1286 * on the loop. If it removes any other analysers, we
1287 * want those analysers not to be called anymore during
1288 * this loop. If it adds an analyser that is located
1289 * after itself, we want it to be scheduled for being
1290 * processed during the loop. If it adds an analyser
1291 * which is located before it, we want it to switch to
1292 * it immediately, even if it has already been called
1293 * once but removed since.
1294 *
1295 * In order to achieve this, we compare the analyser
1296 * list after the call with a copy of it before the
1297 * call. The work list is fed with analyser bits that
1298 * appeared during the call. Then we compare previous
1299 * work list with the new one, and check the bits that
1300 * appeared. If the lowest of these bits is lower than
1301 * the current bit, it means we have enabled a previous
1302 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001303 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001304
1305 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001306 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001307 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001308
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001309 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001310 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001311 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001312 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001313 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001314
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001315 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001316 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001317 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001318 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001319 }
1320
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001321 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001322 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1323 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001324 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001325 }
1326
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001327 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001328 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1329 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001330 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001331 }
1332
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001333 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001334 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1335 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001336 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001337 }
1338
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001339 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001340 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001341 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001342 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001343 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001344
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001345 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001346 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1347 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001348 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001349 }
1350
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001351 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001352 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001353 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001354 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001355 }
Emeric Brun647caf12009-06-30 17:57:00 +02001356
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001357 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001358 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1359 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001360 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001361 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001362
Emeric Brun1d33b292010-01-04 15:47:17 +01001363 if (ana_list & AN_REQ_STICKING_RULES) {
1364 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1365 break;
1366 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1367 }
1368
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001369 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001370 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1371 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001372 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001373 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001374 break;
1375 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001376 }
Willy Tarreau84455332009-03-15 22:34:05 +01001377
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001378 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
1379 rqf_last = s->req->flags;
1380 goto resync_request;
1381 }
1382 }
1383
Willy Tarreau576507f2010-01-07 00:09:04 +01001384 /* we'll monitor the request analysers while parsing the response,
1385 * because some response analysers may indirectly enable new request
1386 * analysers (eg: HTTP keep-alive).
1387 */
1388 req_ana_back = s->req->analysers;
1389
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001390 resync_response:
1391 /* Analyse response */
1392
1393 if (unlikely(s->rep->flags & BF_HIJACK)) {
1394 /* In inject mode, we wake up everytime something has
1395 * happened on the write side of the buffer.
1396 */
1397 unsigned int flags = s->rep->flags;
1398
1399 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1400 !(s->rep->flags & BF_FULL)) {
1401 s->rep->hijacker(s, s->rep);
1402 }
1403
1404 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1405 rpf_last = s->rep->flags;
1406 goto resync_response;
1407 }
1408 }
1409 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
1410 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
1411 unsigned int flags = s->rep->flags;
1412
1413 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001414 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001415 unsigned int ana_list;
1416 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001417
Willy Tarreau90deb182010-01-07 00:20:41 +01001418 /* it's up to the analysers to stop disable reading or
1419 * closing. Note: if an analyser disables any of these
1420 * bits, it is responsible for enabling them again when
1421 * it disables itself, so that other analysers are called
1422 * in similar conditions.
1423 */
1424 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001425 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001426
1427 /* We will call all analysers for which a bit is set in
1428 * s->rep->analysers, following the bit order from LSB
1429 * to MSB. The analysers must remove themselves from
1430 * the list when not needed. Any analyser may return 0
1431 * to break out of the loop, either because of missing
1432 * data to take a decision, or because it decides to
1433 * kill the session. We loop at least once through each
1434 * analyser, and we may loop again if other analysers
1435 * are added in the middle.
1436 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001437
1438 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001439 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001440 /* Warning! ensure that analysers are always placed in ascending order! */
1441
1442 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001443 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1444 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001445 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001446 }
1447
Emeric Brun1d33b292010-01-04 15:47:17 +01001448 if (ana_list & AN_RES_STORE_RULES) {
1449 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1450 break;
1451 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1452 }
1453
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001454 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001455 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1456 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001458 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001459
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001460 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001461 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1462 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001464 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001465 break;
1466 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001467 }
1468
1469 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1470 rpf_last = s->rep->flags;
1471 goto resync_response;
1472 }
1473 }
1474
Willy Tarreau576507f2010-01-07 00:09:04 +01001475 /* maybe someone has added some request analysers, so we must check and loop */
1476 if (s->req->analysers & ~req_ana_back)
1477 goto resync_request;
1478
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001479 /* FIXME: here we should call protocol handlers which rely on
1480 * both buffers.
1481 */
1482
1483
1484 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001485 * Now we propagate unhandled errors to the session. Normally
1486 * we're just in a data phase here since it means we have not
1487 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001488 */
1489 if (!(s->flags & SN_ERR_MASK)) {
1490 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1491 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001492 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001493 if (s->req->flags & BF_READ_ERROR) {
1494 s->be->counters.cli_aborts++;
1495 if (s->srv)
1496 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001497 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001498 }
1499 else if (s->req->flags & BF_READ_TIMEOUT) {
1500 s->be->counters.cli_aborts++;
1501 if (s->srv)
1502 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001503 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001504 }
1505 else if (s->req->flags & BF_WRITE_ERROR) {
1506 s->be->counters.srv_aborts++;
1507 if (s->srv)
1508 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001509 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001510 }
1511 else {
1512 s->be->counters.srv_aborts++;
1513 if (s->srv)
1514 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001515 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001516 }
Willy Tarreau84455332009-03-15 22:34:05 +01001517 sess_set_term_flags(s);
1518 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001519 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1520 /* Report it if the server got an error or a read timeout expired */
1521 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001522 if (s->rep->flags & BF_READ_ERROR) {
1523 s->be->counters.srv_aborts++;
1524 if (s->srv)
1525 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001526 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001527 }
1528 else if (s->rep->flags & BF_READ_TIMEOUT) {
1529 s->be->counters.srv_aborts++;
1530 if (s->srv)
1531 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001532 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001533 }
1534 else if (s->rep->flags & BF_WRITE_ERROR) {
1535 s->be->counters.cli_aborts++;
1536 if (s->srv)
1537 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001538 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001539 }
1540 else {
1541 s->be->counters.cli_aborts++;
1542 if (s->srv)
1543 s->srv->counters.cli_aborts++;
1544 s->flags |= SN_ERR_CLITO;
1545 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001546 sess_set_term_flags(s);
1547 }
Willy Tarreau84455332009-03-15 22:34:05 +01001548 }
1549
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001550 /*
1551 * Here we take care of forwarding unhandled data. This also includes
1552 * connection establishments and shutdown requests.
1553 */
1554
1555
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001556 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001557 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001558 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001559 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001560 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001561 (s->req->prod->state >= SI_ST_EST) &&
1562 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001563 /* This buffer is freewheeling, there's no analyser nor hijacker
1564 * attached to it. If any data are left in, we'll permit them to
1565 * move.
1566 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001567 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001568 buffer_auto_connect(s->req);
1569 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001570 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001571
Willy Tarreau31971e52009-09-20 12:07:52 +02001572 /* If the producer is still connected, we'll enable data to flow
1573 * from the producer to the consumer (which might possibly not be
1574 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001575 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001576 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1577 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001578 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001579
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001580 /* check if it is wise to enable kernel splicing to forward request data */
1581 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1582 s->req->to_forward &&
1583 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001584 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001585 (pipes_used < global.maxpipes) &&
1586 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1587 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1588 (s->req->flags & BF_STREAMER_FAST)))) {
1589 s->req->flags |= BF_KERN_SPLICING;
1590 }
1591
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001592 /* reflect what the L7 analysers have seen last */
1593 rqf_last = s->req->flags;
1594
1595 /*
1596 * Now forward all shutdown requests between both sides of the buffer
1597 */
1598
Willy Tarreau520d95e2009-09-19 21:04:57 +02001599 /* first, let's check if the request buffer needs to shutdown(write), which may
1600 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001601 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001602 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001603 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001604 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001605 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001606
1607 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001608 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 +01001609 s->req->cons->shutw(s->req->cons);
1610
1611 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001612 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1613 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614 buffer_shutr_now(s->req);
1615
1616 /* shutdown(read) pending */
1617 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1618 s->req->prod->shutr(s->req->prod);
1619
Willy Tarreau520d95e2009-09-19 21:04:57 +02001620 /* it's possible that an upper layer has requested a connection setup or abort.
1621 * There are 2 situations where we decide to establish a new connection :
1622 * - there are data scheduled for emission in the buffer
1623 * - the BF_AUTO_CONNECT flag is set (active connection)
1624 */
1625 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001626 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001627 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001628 /* If we have an iohandler without a connect method, we immediately
1629 * switch to the connected state, otherwise we perform a connection
1630 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001631 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001632 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001633 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001634 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001635 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001636 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1637 s->req->wex = TICK_ETERNITY;
1638 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001639 }
Willy Tarreau73201222009-08-16 18:27:24 +02001640 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001641 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001642 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001643 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1644 buffer_shutr_now(s->rep);
1645 }
Willy Tarreau92795622009-03-06 12:51:23 +01001646 }
1647
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001648
1649 /* we may have a pending connection request, or a connection waiting
1650 * for completion.
1651 */
1652 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1653 do {
1654 /* nb: step 1 might switch from QUE to ASS, but we first want
1655 * to give a chance to step 2 to perform a redirect if needed.
1656 */
1657 if (s->si[1].state != SI_ST_REQ)
1658 sess_update_stream_int(s, &s->si[1]);
1659 if (s->si[1].state == SI_ST_REQ)
1660 sess_prepare_conn_req(s, &s->si[1]);
1661
1662 if (s->si[1].state == SI_ST_ASS && s->srv &&
1663 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1664 perform_http_redirect(s, &s->si[1]);
1665 } while (s->si[1].state == SI_ST_ASS);
1666 }
1667
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001668 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001669 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001670 goto resync_stream_interface;
1671
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001672 /* otherwise wewant to check if we need to resync the req buffer or not */
1673 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001674 goto resync_request;
1675
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001676 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001677
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001678 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001679 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001680 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001681 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001682 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001683 (s->rep->prod->state >= SI_ST_EST) &&
1684 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001685 /* This buffer is freewheeling, there's no analyser nor hijacker
1686 * attached to it. If any data are left in, we'll permit them to
1687 * move.
1688 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001689 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001690 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001691 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001692 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1693 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001694 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001695
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001696 /* check if it is wise to enable kernel splicing to forward response data */
1697 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1698 s->rep->to_forward &&
1699 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001700 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001701 (pipes_used < global.maxpipes) &&
1702 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1703 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1704 (s->rep->flags & BF_STREAMER_FAST)))) {
1705 s->rep->flags |= BF_KERN_SPLICING;
1706 }
1707
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001708 /* reflect what the L7 analysers have seen last */
1709 rpf_last = s->rep->flags;
1710
1711 /*
1712 * Now forward all shutdown requests between both sides of the buffer
1713 */
1714
1715 /*
1716 * FIXME: this is probably where we should produce error responses.
1717 */
1718
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001719 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001720 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1721 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001722 buffer_shutw_now(s->rep);
1723
1724 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001725 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 +01001726 s->rep->cons->shutw(s->rep->cons);
1727
1728 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001729 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1730 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001731 buffer_shutr_now(s->rep);
1732
1733 /* shutdown(read) pending */
1734 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1735 s->rep->prod->shutr(s->rep->prod);
1736
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001737 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001738 goto resync_stream_interface;
1739
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001740 if (s->req->flags != rqf_last)
1741 goto resync_request;
1742
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001743 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001744 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001745
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001746 /* we're interested in getting wakeups again */
1747 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1748 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1749
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001750 /* This is needed only when debugging is enabled, to indicate
1751 * client-side or server-side close. Please note that in the unlikely
1752 * event where both sides would close at once, the sequence is reported
1753 * on the server side first.
1754 */
1755 if (unlikely((global.mode & MODE_DEBUG) &&
1756 (!(global.mode & MODE_QUIET) ||
1757 (global.mode & MODE_VERBOSE)))) {
1758 int len;
1759
1760 if (s->si[1].state == SI_ST_CLO &&
1761 s->si[1].prev_state == SI_ST_EST) {
1762 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1763 s->uniq_id, s->be->id,
1764 (unsigned short)s->si[0].fd,
1765 (unsigned short)s->si[1].fd);
1766 write(1, trash, len);
1767 }
1768
1769 if (s->si[0].state == SI_ST_CLO &&
1770 s->si[0].prev_state == SI_ST_EST) {
1771 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1772 s->uniq_id, s->be->id,
1773 (unsigned short)s->si[0].fd,
1774 (unsigned short)s->si[1].fd);
1775 write(1, trash, len);
1776 }
1777 }
1778
1779 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1780 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1781
1782 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1783 session_process_counters(s);
1784
Willy Tarreau1accfc02009-09-05 20:57:35 +02001785 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001786 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001787
Willy Tarreau1accfc02009-09-05 20:57:35 +02001788 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001789 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001790
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001791 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1792 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 +01001793 s->si[0].prev_state = s->si[0].state;
1794 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001795 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1796 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001797
1798 /* Trick: if a request is being waiting for the server to respond,
1799 * and if we know the server can timeout, we don't want the timeout
1800 * to expire on the client side first, but we're still interested
1801 * in passing data from the client to the server (eg: POST). Thus,
1802 * we can cancel the client's request timeout if the server's
1803 * request timeout is set and the server has not yet sent a response.
1804 */
1805
Willy Tarreau520d95e2009-09-19 21:04:57 +02001806 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001807 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1808 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001809 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001810 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001811
Willy Tarreau1accfc02009-09-05 20:57:35 +02001812 /* Call the second stream interface's I/O handler if it's embedded.
1813 * Note that this one may wake the task up again.
1814 */
1815 if (s->req->cons->iohandler) {
1816 s->req->cons->iohandler(s->req->cons);
1817 if (task_in_rq(t)) {
1818 /* If we woke up, we don't want to requeue the
1819 * task to the wait queue, but rather requeue
1820 * it into the runqueue ASAP.
1821 */
1822 t->expire = TICK_ETERNITY;
1823 return t;
1824 }
1825 }
1826
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001827 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1828 tick_first(s->rep->rex, s->rep->wex));
1829 if (s->req->analysers)
1830 t->expire = tick_first(t->expire, s->req->analyse_exp);
1831
1832 if (s->si[0].exp)
1833 t->expire = tick_first(t->expire, s->si[0].exp);
1834
1835 if (s->si[1].exp)
1836 t->expire = tick_first(t->expire, s->si[1].exp);
1837
1838#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001839 fprintf(stderr,
1840 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1841 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1842 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1843 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 +01001844#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001845
1846#ifdef DEBUG_DEV
1847 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001848 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001849 ABORT_NOW();
1850#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001851 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001852 }
1853
1854 s->fe->feconn--;
1855 if (s->flags & SN_BE_ASSIGNED)
1856 s->be->beconn--;
1857 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001858 s->listener->nbconn--;
1859 if (s->listener->state == LI_FULL &&
1860 s->listener->nbconn < s->listener->maxconn) {
1861 /* we should reactivate the listener */
1862 EV_FD_SET(s->listener->fd, DIR_RD);
1863 s->listener->state = LI_READY;
1864 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001865
1866 if (unlikely((global.mode & MODE_DEBUG) &&
1867 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1868 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001869 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001870 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001871 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001872 write(1, trash, len);
1873 }
1874
1875 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1876 session_process_counters(s);
1877
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001878 if (s->txn.status) {
1879 int n;
1880
1881 n = s->txn.status / 100;
1882 if (n < 1 || n > 5)
1883 n = 0;
1884
1885 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001886 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001887
Willy Tarreau24657792010-02-26 10:30:28 +01001888 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001889 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001890 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001891 }
1892
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001893 /* let's do a final log if we need it */
1894 if (s->logs.logwait &&
1895 !(s->flags & SN_MONITOR) &&
1896 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001897 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001898 }
1899
1900 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001901 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001902 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001903 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001904 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905}
1906
Willy Tarreau7c669d72008-06-20 15:04:11 +02001907/*
1908 * This function adjusts sess->srv_conn and maintains the previous and new
1909 * server's served session counts. Setting newsrv to NULL is enough to release
1910 * current connection slot. This function also notifies any LB algo which might
1911 * expect to be informed about any change in the number of active sessions on a
1912 * server.
1913 */
1914void sess_change_server(struct session *sess, struct server *newsrv)
1915{
1916 if (sess->srv_conn == newsrv)
1917 return;
1918
1919 if (sess->srv_conn) {
1920 sess->srv_conn->served--;
1921 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1922 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1923 sess->srv_conn = NULL;
1924 }
1925
1926 if (newsrv) {
1927 newsrv->served++;
1928 if (newsrv->proxy->lbprm.server_take_conn)
1929 newsrv->proxy->lbprm.server_take_conn(newsrv);
1930 sess->srv_conn = newsrv;
1931 }
1932}
1933
Willy Tarreau84455332009-03-15 22:34:05 +01001934/* Set correct session termination flags in case no analyser has done it. It
1935 * also counts a failed request if the server state has not reached the request
1936 * stage.
1937 */
1938void sess_set_term_flags(struct session *s)
1939{
1940 if (!(s->flags & SN_FINST_MASK)) {
1941 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001942
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001943 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001944 if (s->listener->counters)
1945 s->listener->counters->failed_req++;
1946
Willy Tarreau84455332009-03-15 22:34:05 +01001947 s->flags |= SN_FINST_R;
1948 }
1949 else if (s->si[1].state == SI_ST_QUE)
1950 s->flags |= SN_FINST_Q;
1951 else if (s->si[1].state < SI_ST_EST)
1952 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01001953 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01001954 s->flags |= SN_FINST_D;
1955 else
1956 s->flags |= SN_FINST_L;
1957 }
1958}
1959
1960/* Handle server-side errors for default protocols. It is called whenever a a
1961 * connection setup is aborted or a request is aborted in queue. It sets the
1962 * session termination flags so that the caller does not have to worry about
1963 * them. It's installed as ->srv_error for the server-side stream_interface.
1964 */
1965void default_srv_error(struct session *s, struct stream_interface *si)
1966{
1967 int err_type = si->err_type;
1968 int err = 0, fin = 0;
1969
1970 if (err_type & SI_ET_QUEUE_ABRT) {
1971 err = SN_ERR_CLICL;
1972 fin = SN_FINST_Q;
1973 }
1974 else if (err_type & SI_ET_CONN_ABRT) {
1975 err = SN_ERR_CLICL;
1976 fin = SN_FINST_C;
1977 }
1978 else if (err_type & SI_ET_QUEUE_TO) {
1979 err = SN_ERR_SRVTO;
1980 fin = SN_FINST_Q;
1981 }
1982 else if (err_type & SI_ET_QUEUE_ERR) {
1983 err = SN_ERR_SRVCL;
1984 fin = SN_FINST_Q;
1985 }
1986 else if (err_type & SI_ET_CONN_TO) {
1987 err = SN_ERR_SRVTO;
1988 fin = SN_FINST_C;
1989 }
1990 else if (err_type & SI_ET_CONN_ERR) {
1991 err = SN_ERR_SRVCL;
1992 fin = SN_FINST_C;
1993 }
1994 else /* SI_ET_CONN_OTHER and others */ {
1995 err = SN_ERR_INTERNAL;
1996 fin = SN_FINST_C;
1997 }
1998
1999 if (!(s->flags & SN_ERR_MASK))
2000 s->flags |= err;
2001 if (!(s->flags & SN_FINST_MASK))
2002 s->flags |= fin;
2003}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002004
Willy Tarreaubaaee002006-06-26 02:48:02 +02002005/*
2006 * Local variables:
2007 * c-indent-level: 8
2008 * c-basic-offset: 8
2009 * End:
2010 */