blob: 137bbd330d161a18c6940ee5a2b2c8931b0cb9ad [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>
Emeric Brun1d33b292010-01-04 15:47:17 +010032#include <proto/pattern.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010033#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010034#include <proto/proto_http.h>
35#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020036#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010038#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010039#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010040#include <proto/stream_interface.h>
41#include <proto/stream_sock.h>
42#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020044struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010045struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau81f9aa32010-06-01 17:45:26 +020047/* This function is called from the protocol layer accept() in order to instanciate
48 * a new session on behalf of a given listener and frontend. It returns a positive
49 * value upon success, 0 if the connection needs to be closed and ignored, or a
50 * negative value upon critical failure.
51 */
52int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
53{
54 struct proxy *p = l->frontend;
55 struct session *s;
56 struct http_txn *txn;
57 struct task *t;
58
59 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
60 Alert("out of memory in event_accept().\n");
61 goto out_close;
62 }
63
64 /* minimum session initialization required for monitor mode below */
65 s->flags = 0;
66 s->logs.logwait = p->to_log;
67
68 /* if this session comes from a known monitoring system, we want to ignore
69 * it as soon as possible, which means closing it immediately for TCP, but
70 * cleanly.
71 */
72 if (unlikely((l->options & LI_O_CHK_MONNET) &&
73 addr->ss_family == AF_INET &&
74 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
75 if (p->mode == PR_MODE_TCP) {
76 pool_free2(pool2_session, s);
77 return 0;
78 }
79 s->flags |= SN_MONITOR;
80 s->logs.logwait = 0;
81 }
82
83 /* OK, we're keeping the session, so let's properly initialize the session */
84 LIST_ADDQ(&sessions, &s->list);
85 LIST_INIT(&s->back_refs);
86
87 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
88 Alert("out of memory in event_accept().\n");
89 goto out_free_session;
90 }
91
92 s->term_trace = 0;
93 s->cli_addr = *addr;
94 s->logs.accept_date = date; /* user-visible date for logging */
95 s->logs.tv_accept = now; /* corrected date for internal use */
96 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +020097 p->feconn++; /* beconn will be increased once assigned */
98
Willy Tarreaub36b4242010-06-04 20:59:39 +020099 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200100
101 t->process = l->handler;
102 t->context = s;
103 t->nice = l->nice;
104 t->expire = TICK_ETERNITY;
105
106 s->task = t;
107 s->listener = l;
108
109 /* Note: initially, the session's backend points to the frontend.
110 * This changes later when switching rules are executed or
111 * when the default backend is assigned.
112 */
113 s->be = s->fe = p;
114 s->req = s->rep = NULL; /* will be allocated later */
115
116 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
117 * to abort right here as soon as possible, we check the rules before
118 * even initializing the stream interfaces.
119 */
120 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
121 task_free(t);
122 LIST_DEL(&s->list);
123 pool_free2(pool2_session, s);
124 /* let's do a no-linger now to close with a single RST. */
125 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200126 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 return 0;
128 }
129
Willy Tarreaub36b4242010-06-04 20:59:39 +0200130 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200131 if (p->feconn > p->counters.feconn_max)
132 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200133 proxy_inc_fe_sess_ctr(l, p);
134
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200135 /* this part should be common with other protocols */
136 s->si[0].fd = cfd;
137 s->si[0].owner = t;
138 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
139 s->si[0].err_type = SI_ET_NONE;
140 s->si[0].err_loc = NULL;
141 s->si[0].connect = NULL;
142 s->si[0].iohandler = NULL;
143 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;
165 s->si[1].shutr = stream_int_shutr;
166 s->si[1].shutw = stream_int_shutw;
167 s->si[1].exp = TICK_ETERNITY;
168 s->si[1].flags = SI_FL_NONE;
169
170 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
171 s->si[1].flags |= SI_FL_INDEP_STR;
172
173 s->srv = s->prev_srv = s->srv_conn = NULL;
174 s->pend_pos = NULL;
175
176 /* init store persistence */
177 s->store_count = 0;
178
179 /* Adjust some socket options */
180 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
181 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
182 goto out_free_task;
183 }
184
185 txn = &s->txn;
186 /* Those variables will be checked and freed if non-NULL in
187 * session.c:session_free(). It is important that they are
188 * properly initialized.
189 */
190 txn->sessid = NULL;
191 txn->srv_cookie = NULL;
192 txn->cli_cookie = NULL;
193 txn->uri = NULL;
194 txn->req.cap = NULL;
195 txn->rsp.cap = NULL;
196 txn->hdr_idx.v = NULL;
197 txn->hdr_idx.size = txn->hdr_idx.used = 0;
198
199 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
200 goto out_free_task; /* no memory */
201
202 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
203 goto out_free_req; /* no memory */
204
205 /* initialize the request buffer */
206 s->req->size = global.tune.bufsize;
207 buffer_init(s->req);
208 s->req->prod = &s->si[0];
209 s->req->cons = &s->si[1];
210 s->si[0].ib = s->si[1].ob = s->req;
211 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
212
213 /* activate default analysers enabled for this listener */
214 s->req->analysers = l->analysers;
215
216 s->req->wto = TICK_ETERNITY;
217 s->req->rto = TICK_ETERNITY;
218 s->req->rex = TICK_ETERNITY;
219 s->req->wex = TICK_ETERNITY;
220 s->req->analyse_exp = TICK_ETERNITY;
221
222 /* initialize response buffer */
223 s->rep->size = global.tune.bufsize;
224 buffer_init(s->rep);
225 s->rep->prod = &s->si[1];
226 s->rep->cons = &s->si[0];
227 s->si[0].ob = s->si[1].ib = s->rep;
228 s->rep->analysers = 0;
229
230 s->rep->rto = TICK_ETERNITY;
231 s->rep->wto = TICK_ETERNITY;
232 s->rep->rex = TICK_ETERNITY;
233 s->rep->wex = TICK_ETERNITY;
234 s->rep->analyse_exp = TICK_ETERNITY;
235
236 /* finish initialization of the accepted file descriptor */
237 fd_insert(cfd);
238 fdtab[cfd].owner = &s->si[0];
239 fdtab[cfd].state = FD_STREADY;
240 fdtab[cfd].flags = 0;
241 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
242 fdtab[cfd].cb[DIR_RD].b = s->req;
243 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
244 fdtab[cfd].cb[DIR_WR].b = s->rep;
245 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
246 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
247 EV_FD_SET(cfd, DIR_RD);
248
249 if (p->accept) {
250 int ret = p->accept(s);
251 if (unlikely(ret < 0))
252 goto out_free_rep;
253
254 if (unlikely(ret == 0)) {
255 /* work is finished, we can release everything (eg: monitoring) */
256 pool_free2(pool2_buffer, s->rep);
257 pool_free2(pool2_buffer, s->req);
258 task_free(t);
259 LIST_DEL(&s->list);
260 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200261 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200262 return 0;
263 }
264 }
265
266 /* it is important not to call the wakeup function directly but to
267 * pass through task_wakeup(), because this one knows how to apply
268 * priorities to tasks.
269 */
270 task_wakeup(t, TASK_WOKEN_INIT);
271 return 1;
272
273 /* Error unrolling */
274 out_free_rep:
275 pool_free2(pool2_buffer, s->rep);
276 out_free_req:
277 pool_free2(pool2_buffer, s->req);
278 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200279 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200280 task_free(t);
281 out_free_session:
282 LIST_DEL(&s->list);
283 pool_free2(pool2_session, s);
284 out_close:
285 return -1;
286}
287
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288/*
289 * frees the context associated to a session. It must have been removed first.
290 */
291void session_free(struct session *s)
292{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100293 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200294 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100295 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200296 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100297
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 if (s->pend_pos)
299 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100300
Willy Tarreau1e62de62008-11-11 20:20:02 +0100301 if (s->srv) { /* there may be requests left pending in queue */
302 if (s->flags & SN_CURR_SESS) {
303 s->flags &= ~SN_CURR_SESS;
304 s->srv->cur_sess--;
305 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100306 if (may_dequeue_tasks(s->srv, s->be))
307 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100308 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100309
Willy Tarreau7c669d72008-06-20 15:04:11 +0200310 if (unlikely(s->srv_conn)) {
311 /* the session still has a reserved slot on a server, but
312 * it should normally be only the same as the one above,
313 * so this should not happen in fact.
314 */
315 sess_change_server(s, NULL);
316 }
317
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100318 if (s->req->pipe)
319 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100320
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100321 if (s->rep->pipe)
322 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100323
Willy Tarreau48d63db2008-08-03 17:41:33 +0200324 pool_free2(pool2_buffer, s->req);
325 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326
Willy Tarreau46023632010-01-07 22:51:47 +0100327 http_end_txn(s);
328
Willy Tarreaua4cda672010-06-06 18:28:49 +0200329 for (i = 0; i < s->store_count; i++) {
330 if (!s->store[i].ts)
331 continue;
332 stksess_free(s->store[i].table, s->store[i].ts);
333 s->store[i].ts = NULL;
334 }
335
Willy Tarreau92fb9832007-10-16 17:34:28 +0200336 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200337 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100338 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
339 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100341
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100342 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100343 /* we have to unlink all watchers. We must not relink them if
344 * this session was the last one in the list.
345 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100346 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100347 LIST_INIT(&bref->users);
348 if (s->list.n != &sessions)
349 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100350 bref->ref = s->list.n;
351 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100352 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200353 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200354
355 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200356 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200357 pool_flush2(pool2_buffer);
358 pool_flush2(fe->hdr_idx_pool);
359 pool_flush2(pool2_requri);
360 pool_flush2(pool2_capture);
361 pool_flush2(pool2_session);
362 pool_flush2(fe->req_cap_pool);
363 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200364 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200365}
366
367
368/* perform minimal intializations, report 0 in case of error, 1 if OK. */
369int init_session()
370{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100371 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200372 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
373 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374}
375
Willy Tarreau30e71012007-11-26 20:15:35 +0100376void session_process_counters(struct session *s)
377{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100378 unsigned long long bytes;
379
Willy Tarreau30e71012007-11-26 20:15:35 +0100380 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100381 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100382 s->logs.bytes_in = s->req->total;
383 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200384 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100385
Willy Tarreau30e71012007-11-26 20:15:35 +0100386 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200387 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100388
Willy Tarreau30e71012007-11-26 20:15:35 +0100389 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200390 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200391
392 if (s->listener->counters)
393 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100394 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100395 }
396
Willy Tarreau30e71012007-11-26 20:15:35 +0100397 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100398 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100399 s->logs.bytes_out = s->rep->total;
400 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200401 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100402
Willy Tarreau30e71012007-11-26 20:15:35 +0100403 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200404 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100405
Willy Tarreau30e71012007-11-26 20:15:35 +0100406 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200407 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200408
409 if (s->listener->counters)
410 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100411 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100412 }
413}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100415/* This function is called with (si->state == SI_ST_CON) meaning that a
416 * connection was attempted and that the file descriptor is already allocated.
417 * We must check for establishment, error and abort. Possible output states
418 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
419 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
420 * otherwise 1.
421 */
422int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
423{
424 struct buffer *req = si->ob;
425 struct buffer *rep = si->ib;
426
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100427 /* If we got an error, or if nothing happened and the connection timed
428 * out, we must give up. The CER state handler will take care of retry
429 * attempts and error reports.
430 */
431 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100432 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100433 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200434 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100435 fd_delete(si->fd);
436
437 if (si->err_type)
438 return 0;
439
440 si->err_loc = s->srv;
441 if (si->flags & SI_FL_ERR)
442 si->err_type = SI_ET_CONN_ERR;
443 else
444 si->err_type = SI_ET_CONN_TO;
445 return 0;
446 }
447
448 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200449 if (unlikely((rep->flags & BF_SHUTW) ||
450 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200451 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100452 s->be->options & PR_O_ABRT_CLOSE)))) {
453 /* give up */
454 si->shutw(si);
455 si->err_type |= SI_ET_CONN_ABRT;
456 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200457 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100458 if (s->srv_error)
459 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100460 return 1;
461 }
462
463 /* we need to wait a bit more if there was no activity either */
464 if (!(req->flags & BF_WRITE_ACTIVITY))
465 return 1;
466
467 /* OK, this means that a connection succeeded. The caller will be
468 * responsible for handling the transition from CON to EST.
469 */
470 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100471 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100472 si->state = SI_ST_EST;
473 si->err_type = SI_ET_NONE;
474 si->err_loc = NULL;
475 return 1;
476}
477
478/* This function is called with (si->state == SI_ST_CER) meaning that a
479 * previous connection attempt has failed and that the file descriptor
480 * has already been released. Possible causes include asynchronous error
481 * notification and time out. Possible output states are SI_ST_CLO when
482 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
483 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
484 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
485 * marked as in error state. It returns 0.
486 */
487int sess_update_st_cer(struct session *s, struct stream_interface *si)
488{
489 /* we probably have to release last session from the server */
490 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100491 health_adjust(s->srv, HANA_STATUS_L4_ERR);
492
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100493 if (s->flags & SN_CURR_SESS) {
494 s->flags &= ~SN_CURR_SESS;
495 s->srv->cur_sess--;
496 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100497 }
498
499 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200500 si->conn_retries--;
501 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100502 if (!si->err_type) {
503 si->err_type = SI_ET_CONN_ERR;
504 si->err_loc = s->srv;
505 }
506
507 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200508 s->srv->counters.failed_conns++;
509 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100510 if (may_dequeue_tasks(s->srv, s->be))
511 process_srv_queue(s->srv);
512
513 /* shutw is enough so stop a connecting socket */
514 si->shutw(si);
515 si->ob->flags |= BF_WRITE_ERROR;
516 si->ib->flags |= BF_READ_ERROR;
517
518 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100519 if (s->srv_error)
520 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100521 return 0;
522 }
523
524 /* If the "redispatch" option is set on the backend, we are allowed to
525 * retry on another server for the last retry. In order to achieve this,
526 * we must mark the session unassigned, and eventually clear the DIRECT
527 * bit to ignore any persistence cookie. We won't count a retry nor a
528 * redispatch yet, because this will depend on what server is selected.
529 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200530 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100531 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100532 if (may_dequeue_tasks(s->srv, s->be))
533 process_srv_queue(s->srv);
534
535 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
536 s->prev_srv = s->srv;
537 si->state = SI_ST_REQ;
538 } else {
539 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200540 s->srv->counters.retries++;
541 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 si->state = SI_ST_ASS;
543 }
544
545 if (si->flags & SI_FL_ERR) {
546 /* The error was an asynchronous connection error, and we will
547 * likely have to retry connecting to the same server, most
548 * likely leading to the same result. To avoid this, we wait
549 * one second before retrying.
550 */
551
552 if (!si->err_type)
553 si->err_type = SI_ET_CONN_ERR;
554
555 si->state = SI_ST_TAR;
556 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
557 return 0;
558 }
559 return 0;
560}
561
562/*
563 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200564 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
565 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100566 */
567void sess_establish(struct session *s, struct stream_interface *si)
568{
569 struct buffer *req = si->ob;
570 struct buffer *rep = si->ib;
571
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100572 if (s->srv)
573 health_adjust(s->srv, HANA_STATUS_L4_OK);
574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100576 /* if the user wants to log as soon as possible, without counting
577 * bytes from the server, then this is the right moment. */
578 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
579 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100580 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100582 }
583 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
585 /* reset hdr_idx which was already initialized by the request.
586 * right now, the http parser does it.
587 * hdr_idx_init(&s->txn.hdr_idx);
588 */
589 }
590
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200591 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200593 if (si->connect) {
594 /* real connections have timeouts */
595 req->wto = s->be->timeout.server;
596 rep->rto = s->be->timeout.server;
597 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 req->wex = TICK_ETERNITY;
599}
600
601/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
602 * Other input states are simply ignored.
603 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
604 * Flags must have previously been updated for timeouts and other conditions.
605 */
606void sess_update_stream_int(struct session *s, struct stream_interface *si)
607{
608 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",
609 now_ms, __FUNCTION__,
610 s,
611 s->req, s->rep,
612 s->req->rex, s->rep->wex,
613 s->req->flags, s->rep->flags,
614 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
615
616 if (si->state == SI_ST_ASS) {
617 /* Server assigned to connection request, we have to try to connect now */
618 int conn_err;
619
620 conn_err = connect_server(s);
621 if (conn_err == SN_ERR_NONE) {
622 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100623 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100624 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625 return;
626 }
627
628 /* We have received a synchronous error. We might have to
629 * abort, retry immediately or redispatch.
630 */
631 if (conn_err == SN_ERR_INTERNAL) {
632 if (!si->err_type) {
633 si->err_type = SI_ET_CONN_OTHER;
634 si->err_loc = s->srv;
635 }
636
637 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100638 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100639 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200640 s->srv->counters.failed_conns++;
641 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642
643 /* release other sessions waiting for this server */
644 if (may_dequeue_tasks(s->srv, s->be))
645 process_srv_queue(s->srv);
646
647 /* Failed and not retryable. */
648 si->shutr(si);
649 si->shutw(si);
650 si->ob->flags |= BF_WRITE_ERROR;
651
652 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
653
654 /* no session was ever accounted for this server */
655 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100656 if (s->srv_error)
657 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658 return;
659 }
660
661 /* We are facing a retryable error, but we don't want to run a
662 * turn-around now, as the problem is likely a source port
663 * allocation problem, so we want to retry now.
664 */
665 si->state = SI_ST_CER;
666 si->flags &= ~SI_FL_ERR;
667 sess_update_st_cer(s, si);
668 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
669 return;
670 }
671 else if (si->state == SI_ST_QUE) {
672 /* connection request was queued, check for any update */
673 if (!s->pend_pos) {
674 /* The connection is not in the queue anymore. Either
675 * we have a server connection slot available and we
676 * go directly to the assigned state, or we need to
677 * load-balance first and go to the INI state.
678 */
679 si->exp = TICK_ETERNITY;
680 if (unlikely(!(s->flags & SN_ASSIGNED)))
681 si->state = SI_ST_REQ;
682 else {
683 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
684 si->state = SI_ST_ASS;
685 }
686 return;
687 }
688
689 /* Connection request still in queue... */
690 if (si->flags & SI_FL_EXP) {
691 /* ... and timeout expired */
692 si->exp = TICK_ETERNITY;
693 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
694 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200695 s->srv->counters.failed_conns++;
696 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 si->shutr(si);
698 si->shutw(si);
699 si->ob->flags |= BF_WRITE_TIMEOUT;
700 if (!si->err_type)
701 si->err_type = SI_ET_QUEUE_TO;
702 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100703 if (s->srv_error)
704 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 return;
706 }
707
708 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200709 if ((si->ob->flags & (BF_READ_ERROR)) ||
710 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200711 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 /* give up */
713 si->exp = TICK_ETERNITY;
714 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
715 si->shutr(si);
716 si->shutw(si);
717 si->err_type |= SI_ET_QUEUE_ABRT;
718 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100719 if (s->srv_error)
720 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721 return;
722 }
723
724 /* Nothing changed */
725 return;
726 }
727 else if (si->state == SI_ST_TAR) {
728 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200729 if ((si->ob->flags & (BF_READ_ERROR)) ||
730 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200731 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 /* give up */
733 si->exp = TICK_ETERNITY;
734 si->shutr(si);
735 si->shutw(si);
736 si->err_type |= SI_ET_CONN_ABRT;
737 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100738 if (s->srv_error)
739 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 return;
741 }
742
743 if (!(si->flags & SI_FL_EXP))
744 return; /* still in turn-around */
745
746 si->exp = TICK_ETERNITY;
747
748 /* we keep trying on the same server as long as the session is
749 * marked "assigned".
750 * FIXME: Should we force a redispatch attempt when the server is down ?
751 */
752 if (s->flags & SN_ASSIGNED)
753 si->state = SI_ST_ASS;
754 else
755 si->state = SI_ST_REQ;
756 return;
757 }
758}
759
760/* This function initiates a server connection request on a stream interface
761 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
762 * indicating that a server has been assigned. It may also return SI_ST_QUE,
763 * or SI_ST_CLO upon error.
764 */
765static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
766 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",
767 now_ms, __FUNCTION__,
768 s,
769 s->req, s->rep,
770 s->req->rex, s->rep->wex,
771 s->req->flags, s->rep->flags,
772 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
773
774 if (si->state != SI_ST_REQ)
775 return;
776
777 /* Try to assign a server */
778 if (srv_redispatch_connect(s) != 0) {
779 /* We did not get a server. Either we queued the
780 * connection request, or we encountered an error.
781 */
782 if (si->state == SI_ST_QUE)
783 return;
784
785 /* we did not get any server, let's check the cause */
786 si->shutr(si);
787 si->shutw(si);
788 si->ob->flags |= BF_WRITE_ERROR;
789 if (!si->err_type)
790 si->err_type = SI_ET_CONN_OTHER;
791 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100792 if (s->srv_error)
793 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100794 return;
795 }
796
797 /* The server is assigned */
798 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
799 si->state = SI_ST_ASS;
800}
801
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200802/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100803 * if appropriate. The default_backend rule is also considered, then the
804 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200805 * It returns 1 if the processing can continue on next analysers, or zero if it
806 * either needs more data or wants to immediately abort the request.
807 */
808int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
809{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200810 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100811
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200812 req->analysers &= ~an_bit;
813 req->analyse_exp = TICK_ETERNITY;
814
815 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
816 now_ms, __FUNCTION__,
817 s,
818 req,
819 req->rex, req->wex,
820 req->flags,
821 req->l,
822 req->analysers);
823
824 /* now check whether we have some switching rules for this request */
825 if (!(s->flags & SN_BE_ASSIGNED)) {
826 struct switching_rule *rule;
827
828 list_for_each_entry(rule, &s->fe->switching_rules, list) {
829 int ret;
830
831 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
832 ret = acl_pass(ret);
833 if (rule->cond->pol == ACL_COND_UNLESS)
834 ret = !ret;
835
836 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200837 if (!session_set_backend(s, rule->be.backend))
838 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200839 break;
840 }
841 }
842
843 /* To ensure correct connection accounting on the backend, we
844 * have to assign one if it was not set (eg: a listen). This
845 * measure also takes care of correctly setting the default
846 * backend if any.
847 */
848 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200849 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
850 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200851 }
852
853 /* we don't want to run the HTTP filters again if the backend has not changed */
854 if (s->fe == s->be)
855 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
856
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200857 /* 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 +0100858 * persistence rule, and report that in the session.
859 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200860 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100861 int ret = 1;
862
863 if (prst_rule->cond) {
864 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
865 ret = acl_pass(ret);
866 if (prst_rule->cond->pol == ACL_COND_UNLESS)
867 ret = !ret;
868 }
869
870 if (ret) {
871 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200872 if (prst_rule->type == PERSIST_TYPE_FORCE) {
873 s->flags |= SN_FORCE_PRST;
874 } else {
875 s->flags |= SN_IGNORE_PRST;
876 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100877 break;
878 }
879 }
880
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200881 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200882
883 sw_failed:
884 /* immediately abort this request in case of allocation failure */
885 buffer_abort(s->req);
886 buffer_abort(s->rep);
887
888 if (!(s->flags & SN_ERR_MASK))
889 s->flags |= SN_ERR_RESOURCE;
890 if (!(s->flags & SN_FINST_MASK))
891 s->flags |= SN_FINST_R;
892
893 s->txn.status = 500;
894 s->req->analysers = 0;
895 s->req->analyse_exp = TICK_ETERNITY;
896 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200897}
898
Emeric Brun1d33b292010-01-04 15:47:17 +0100899/* This stream analyser works on a request. It applies all sticking rules on
900 * it then returns 1. The data must already be present in the buffer otherwise
901 * they won't match. It always returns 1.
902 */
903int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
904{
905 struct proxy *px = s->be;
906 struct sticking_rule *rule;
907
908 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
909 now_ms, __FUNCTION__,
910 s,
911 req,
912 req->rex, req->wex,
913 req->flags,
914 req->l,
915 req->analysers);
916
917 list_for_each_entry(rule, &px->sticking_rules, list) {
918 int ret = 1 ;
919 int i;
920
921 for (i = 0; i < s->store_count; i++) {
922 if (rule->table.t == s->store[i].table)
923 break;
924 }
925
926 if (i != s->store_count)
927 continue;
928
929 if (rule->cond) {
930 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
931 ret = acl_pass(ret);
932 if (rule->cond->pol == ACL_COND_UNLESS)
933 ret = !ret;
934 }
935
936 if (ret) {
937 struct stktable_key *key;
938
939 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
940 if (!key)
941 continue;
942
943 if (rule->flags & STK_IS_MATCH) {
944 struct stksess *ts;
945
946 if ((ts = stktable_lookup(rule->table.t, key)) != NULL) {
947 if (!(s->flags & SN_ASSIGNED)) {
948 struct eb32_node *node;
949
950 /* srv found in table */
951 node = eb32_lookup(&px->conf.used_server_id, ts->sid);
952 if (node) {
953 struct server *srv;
954
955 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100956 if ((srv->state & SRV_RUNNING) ||
957 (px->options & PR_O_PERSIST) ||
958 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100959 s->flags |= SN_DIRECT | SN_ASSIGNED;
960 s->srv = srv;
961 }
962 }
963 }
964 ts->expire = tick_add(now_ms, MS_TO_TICKS(rule->table.t->expire));
965 }
966 }
967 if (rule->flags & STK_IS_STORE) {
968 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
969 struct stksess *ts;
970
971 ts = stksess_new(rule->table.t, key);
972 if (ts) {
973 s->store[s->store_count].table = rule->table.t;
974 s->store[s->store_count++].ts = ts;
975 }
976 }
977 }
978 }
979 }
980
981 req->analysers &= ~an_bit;
982 req->analyse_exp = TICK_ETERNITY;
983 return 1;
984}
985
986/* This stream analyser works on a response. It applies all store rules on it
987 * then returns 1. The data must already be present in the buffer otherwise
988 * they won't match. It always returns 1.
989 */
990int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
991{
992 struct proxy *px = s->be;
993 struct sticking_rule *rule;
994 int i;
995
996 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
997 now_ms, __FUNCTION__,
998 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +0100999 rep,
1000 rep->rex, rep->wex,
1001 rep->flags,
1002 rep->l,
1003 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001004
1005 list_for_each_entry(rule, &px->storersp_rules, list) {
1006 int ret = 1 ;
1007 int storereqidx = -1;
1008
1009 for (i = 0; i < s->store_count; i++) {
1010 if (rule->table.t == s->store[i].table) {
1011 if (!(s->store[i].flags))
1012 storereqidx = i;
1013 break;
1014 }
1015 }
1016
1017 if ((i != s->store_count) && (storereqidx == -1))
1018 continue;
1019
1020 if (rule->cond) {
1021 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1022 ret = acl_pass(ret);
1023 if (rule->cond->pol == ACL_COND_UNLESS)
1024 ret = !ret;
1025 }
1026
1027 if (ret) {
1028 struct stktable_key *key;
1029
1030 key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
1031 if (!key)
1032 continue;
1033
1034 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001035 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001036 s->store[storereqidx].flags = 1;
1037 }
1038 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1039 struct stksess *ts;
1040
1041 ts = stksess_new(rule->table.t, key);
1042 if (ts) {
1043 s->store[s->store_count].table = rule->table.t;
1044 s->store[s->store_count].flags = 1;
1045 s->store[s->store_count++].ts = ts;
1046 }
1047 }
1048 }
1049 }
1050
1051 /* process store request and store response */
1052 for (i = 0; i < s->store_count; i++) {
1053 if (stktable_store(s->store[i].table, s->store[i].ts, s->srv->puid) > 0) {
1054 stksess_free(s->store[i].table, s->store[i].ts);
1055 s->store[i].ts = NULL;
1056 }
1057 }
1058
1059 rep->analysers &= ~an_bit;
1060 rep->analyse_exp = TICK_ETERNITY;
1061 return 1;
1062}
1063
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001064/* This macro is very specific to the function below. See the comments in
1065 * process_session() below to understand the logic and the tests.
1066 */
1067#define UPDATE_ANALYSERS(real, list, back, flag) { \
1068 list = (((list) & ~(flag)) | ~(back)) & (real); \
1069 back = real; \
1070 if (!(list)) \
1071 break; \
1072 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1073 continue; \
1074}
1075
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001076/* Processes the client, server, request and response jobs of a session task,
1077 * then puts it back to the wait queue in a clean state, or cleans up its
1078 * resources if it must be deleted. Returns in <next> the date the task wants
1079 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1080 * nothing too many times, the request and response buffers flags are monitored
1081 * and each function is called only if at least another function has changed at
1082 * least one flag it is interested in.
1083 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001084struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001085{
1086 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001087 unsigned int rqf_last, rpf_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001088 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001089
1090 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1091 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1092
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001093 /* this data may be no longer valid, clear it */
1094 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1095
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001096 /* This flag must explicitly be set every time */
1097 s->req->flags &= ~BF_READ_NOEXP;
1098
1099 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1100 rqf_last = s->req->flags;
1101 rpf_last = s->rep->flags;
1102
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001103 /* we don't want the stream interface functions to recursively wake us up */
1104 if (s->req->prod->owner == t)
1105 s->req->prod->flags |= SI_FL_DONT_WAKE;
1106 if (s->req->cons->owner == t)
1107 s->req->cons->flags |= SI_FL_DONT_WAKE;
1108
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001109 /* 1a: Check for low level timeouts if needed. We just set a flag on
1110 * stream interfaces when their timeouts have expired.
1111 */
1112 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1113 stream_int_check_timeouts(&s->si[0]);
1114 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001115
1116 /* check buffer timeouts, and close the corresponding stream interfaces
1117 * for future reads or writes. Note: this will also concern upper layers
1118 * but we do not touch any other flag. We must be careful and correctly
1119 * detect state changes when calling them.
1120 */
1121
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001122 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001123
Willy Tarreau14641402009-12-29 14:49:56 +01001124 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1125 s->req->cons->flags |= SI_FL_NOLINGER;
1126 s->req->cons->shutw(s->req->cons);
1127 }
1128
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001129 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1130 s->req->prod->shutr(s->req->prod);
1131
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001132 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001133
Willy Tarreau14641402009-12-29 14:49:56 +01001134 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1135 s->rep->cons->flags |= SI_FL_NOLINGER;
1136 s->rep->cons->shutw(s->rep->cons);
1137 }
1138
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001139 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1140 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001141 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001142
1143 /* 1b: check for low-level errors reported at the stream interface.
1144 * First we check if it's a retryable error (in which case we don't
1145 * want to tell the buffer). Otherwise we report the error one level
1146 * upper by setting flags into the buffers. Note that the side towards
1147 * the client cannot have connect (hence retryable) errors. Also, the
1148 * connection setup code must be able to deal with any type of abort.
1149 */
1150 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1151 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1152 s->si[0].shutr(&s->si[0]);
1153 s->si[0].shutw(&s->si[0]);
1154 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001155 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001156 s->be->counters.cli_aborts++;
1157 if (s->srv)
1158 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001159 if (!(s->flags & SN_ERR_MASK))
1160 s->flags |= SN_ERR_CLICL;
1161 if (!(s->flags & SN_FINST_MASK))
1162 s->flags |= SN_FINST_D;
1163 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001164 }
1165 }
1166
1167 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1168 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1169 s->si[1].shutr(&s->si[1]);
1170 s->si[1].shutw(&s->si[1]);
1171 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001172 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001173 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001174 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001175 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001176 s->be->counters.srv_aborts++;
1177 if (s->srv)
1178 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001179 if (!(s->flags & SN_ERR_MASK))
1180 s->flags |= SN_ERR_SRVCL;
1181 if (!(s->flags & SN_FINST_MASK))
1182 s->flags |= SN_FINST_D;
1183 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001184 }
1185 /* note: maybe we should process connection errors here ? */
1186 }
1187
1188 if (s->si[1].state == SI_ST_CON) {
1189 /* we were trying to establish a connection on the server side,
1190 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1191 */
1192 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1193 sess_update_st_cer(s, &s->si[1]);
1194 else if (s->si[1].state == SI_ST_EST)
1195 sess_establish(s, &s->si[1]);
1196
1197 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1198 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1199 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1200 */
1201 }
1202
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001203resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001204 /* Check for connection closure */
1205
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001206 DPRINTF(stderr,
1207 "[%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",
1208 now_ms, __FUNCTION__, __LINE__,
1209 t,
1210 s, s->flags,
1211 s->req, s->rep,
1212 s->req->rex, s->rep->wex,
1213 s->req->flags, s->rep->flags,
1214 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1215 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001216 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217
1218 /* nothing special to be done on client side */
1219 if (unlikely(s->req->prod->state == SI_ST_DIS))
1220 s->req->prod->state = SI_ST_CLO;
1221
1222 /* When a server-side connection is released, we have to count it and
1223 * check for pending connections on this server.
1224 */
1225 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1226 s->req->cons->state = SI_ST_CLO;
1227 if (s->srv) {
1228 if (s->flags & SN_CURR_SESS) {
1229 s->flags &= ~SN_CURR_SESS;
1230 s->srv->cur_sess--;
1231 }
1232 sess_change_server(s, NULL);
1233 if (may_dequeue_tasks(s->srv, s->be))
1234 process_srv_queue(s->srv);
1235 }
1236 }
1237
1238 /*
1239 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1240 * at this point.
1241 */
1242
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001243 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001244 /* Analyse request */
1245 if ((s->req->flags & BF_MASK_ANALYSER) ||
1246 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
1247 unsigned int flags = s->req->flags;
1248
1249 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001250 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001251 unsigned int ana_list;
1252 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001253
Willy Tarreau90deb182010-01-07 00:20:41 +01001254 /* it's up to the analysers to stop new connections,
1255 * disable reading or closing. Note: if an analyser
1256 * disables any of these bits, it is responsible for
1257 * enabling them again when it disables itself, so
1258 * that other analysers are called in similar conditions.
1259 */
1260 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001261 buffer_auto_connect(s->req);
1262 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001263
1264 /* We will call all analysers for which a bit is set in
1265 * s->req->analysers, following the bit order from LSB
1266 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001267 * the list when not needed. Any analyser may return 0
1268 * to break out of the loop, either because of missing
1269 * data to take a decision, or because it decides to
1270 * kill the session. We loop at least once through each
1271 * analyser, and we may loop again if other analysers
1272 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001273 *
1274 * We build a list of analysers to run. We evaluate all
1275 * of these analysers in the order of the lower bit to
1276 * the higher bit. This ordering is very important.
1277 * An analyser will often add/remove other analysers,
1278 * including itself. Any changes to itself have no effect
1279 * on the loop. If it removes any other analysers, we
1280 * want those analysers not to be called anymore during
1281 * this loop. If it adds an analyser that is located
1282 * after itself, we want it to be scheduled for being
1283 * processed during the loop. If it adds an analyser
1284 * which is located before it, we want it to switch to
1285 * it immediately, even if it has already been called
1286 * once but removed since.
1287 *
1288 * In order to achieve this, we compare the analyser
1289 * list after the call with a copy of it before the
1290 * call. The work list is fed with analyser bits that
1291 * appeared during the call. Then we compare previous
1292 * work list with the new one, and check the bits that
1293 * appeared. If the lowest of these bits is lower than
1294 * the current bit, it means we have enabled a previous
1295 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001296 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001297
1298 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001299 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001300 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001301
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001302 if (ana_list & AN_REQ_INSPECT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001303 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001304 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001305 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001306 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001307
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001308 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001309 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001310 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001311 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001312 }
1313
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001314 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001315 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1316 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001317 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001318 }
1319
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001320 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001321 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1322 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001323 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001324 }
1325
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001326 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001327 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1328 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001329 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001330 }
1331
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001332 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001333 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001334 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001335 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001336 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001337
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001338 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001339 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1340 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001341 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001342 }
1343
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001344 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001345 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001346 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001347 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001348 }
Emeric Brun647caf12009-06-30 17:57:00 +02001349
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001350 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001351 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1352 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001353 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001354 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001355
Emeric Brun1d33b292010-01-04 15:47:17 +01001356 if (ana_list & AN_REQ_STICKING_RULES) {
1357 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1358 break;
1359 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1360 }
1361
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001362 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001363 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1364 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001365 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001366 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001367 break;
1368 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001369 }
Willy Tarreau84455332009-03-15 22:34:05 +01001370
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001371 if ((s->req->flags ^ flags) & BF_MASK_STATIC) {
1372 rqf_last = s->req->flags;
1373 goto resync_request;
1374 }
1375 }
1376
Willy Tarreau576507f2010-01-07 00:09:04 +01001377 /* we'll monitor the request analysers while parsing the response,
1378 * because some response analysers may indirectly enable new request
1379 * analysers (eg: HTTP keep-alive).
1380 */
1381 req_ana_back = s->req->analysers;
1382
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001383 resync_response:
1384 /* Analyse response */
1385
1386 if (unlikely(s->rep->flags & BF_HIJACK)) {
1387 /* In inject mode, we wake up everytime something has
1388 * happened on the write side of the buffer.
1389 */
1390 unsigned int flags = s->rep->flags;
1391
1392 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1393 !(s->rep->flags & BF_FULL)) {
1394 s->rep->hijacker(s, s->rep);
1395 }
1396
1397 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1398 rpf_last = s->rep->flags;
1399 goto resync_response;
1400 }
1401 }
1402 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
1403 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
1404 unsigned int flags = s->rep->flags;
1405
1406 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001407 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001408 unsigned int ana_list;
1409 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001410
Willy Tarreau90deb182010-01-07 00:20:41 +01001411 /* it's up to the analysers to stop disable reading or
1412 * closing. Note: if an analyser disables any of these
1413 * bits, it is responsible for enabling them again when
1414 * it disables itself, so that other analysers are called
1415 * in similar conditions.
1416 */
1417 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001418 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001419
1420 /* We will call all analysers for which a bit is set in
1421 * s->rep->analysers, following the bit order from LSB
1422 * to MSB. The analysers must remove themselves from
1423 * the list when not needed. Any analyser may return 0
1424 * to break out of the loop, either because of missing
1425 * data to take a decision, or because it decides to
1426 * kill the session. We loop at least once through each
1427 * analyser, and we may loop again if other analysers
1428 * are added in the middle.
1429 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001430
1431 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001432 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001433 /* Warning! ensure that analysers are always placed in ascending order! */
1434
1435 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001436 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1437 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001438 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001439 }
1440
Emeric Brun1d33b292010-01-04 15:47:17 +01001441 if (ana_list & AN_RES_STORE_RULES) {
1442 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1443 break;
1444 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1445 }
1446
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001447 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001448 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1449 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001450 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001451 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001452
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001453 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001454 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1455 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001456 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001457 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001458 break;
1459 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001460 }
1461
1462 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1463 rpf_last = s->rep->flags;
1464 goto resync_response;
1465 }
1466 }
1467
Willy Tarreau576507f2010-01-07 00:09:04 +01001468 /* maybe someone has added some request analysers, so we must check and loop */
1469 if (s->req->analysers & ~req_ana_back)
1470 goto resync_request;
1471
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001472 /* FIXME: here we should call protocol handlers which rely on
1473 * both buffers.
1474 */
1475
1476
1477 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001478 * Now we propagate unhandled errors to the session. Normally
1479 * we're just in a data phase here since it means we have not
1480 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001481 */
1482 if (!(s->flags & SN_ERR_MASK)) {
1483 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1484 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001485 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001486 if (s->req->flags & BF_READ_ERROR) {
1487 s->be->counters.cli_aborts++;
1488 if (s->srv)
1489 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001490 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001491 }
1492 else if (s->req->flags & BF_READ_TIMEOUT) {
1493 s->be->counters.cli_aborts++;
1494 if (s->srv)
1495 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001496 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001497 }
1498 else if (s->req->flags & BF_WRITE_ERROR) {
1499 s->be->counters.srv_aborts++;
1500 if (s->srv)
1501 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001502 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001503 }
1504 else {
1505 s->be->counters.srv_aborts++;
1506 if (s->srv)
1507 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001508 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001509 }
Willy Tarreau84455332009-03-15 22:34:05 +01001510 sess_set_term_flags(s);
1511 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001512 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1513 /* Report it if the server got an error or a read timeout expired */
1514 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001515 if (s->rep->flags & BF_READ_ERROR) {
1516 s->be->counters.srv_aborts++;
1517 if (s->srv)
1518 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001519 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001520 }
1521 else if (s->rep->flags & BF_READ_TIMEOUT) {
1522 s->be->counters.srv_aborts++;
1523 if (s->srv)
1524 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001525 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001526 }
1527 else if (s->rep->flags & BF_WRITE_ERROR) {
1528 s->be->counters.cli_aborts++;
1529 if (s->srv)
1530 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001531 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001532 }
1533 else {
1534 s->be->counters.cli_aborts++;
1535 if (s->srv)
1536 s->srv->counters.cli_aborts++;
1537 s->flags |= SN_ERR_CLITO;
1538 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001539 sess_set_term_flags(s);
1540 }
Willy Tarreau84455332009-03-15 22:34:05 +01001541 }
1542
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001543 /*
1544 * Here we take care of forwarding unhandled data. This also includes
1545 * connection establishments and shutdown requests.
1546 */
1547
1548
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001549 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001550 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001551 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001552 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001553 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001554 (s->req->prod->state >= SI_ST_EST) &&
1555 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001556 /* This buffer is freewheeling, there's no analyser nor hijacker
1557 * attached to it. If any data are left in, we'll permit them to
1558 * move.
1559 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001560 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001561 buffer_auto_connect(s->req);
1562 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001563 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001564
Willy Tarreau31971e52009-09-20 12:07:52 +02001565 /* If the producer is still connected, we'll enable data to flow
1566 * from the producer to the consumer (which might possibly not be
1567 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001568 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001569 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1570 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001571 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001572
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001573 /* check if it is wise to enable kernel splicing to forward request data */
1574 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1575 s->req->to_forward &&
1576 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001577 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001578 (pipes_used < global.maxpipes) &&
1579 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1580 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1581 (s->req->flags & BF_STREAMER_FAST)))) {
1582 s->req->flags |= BF_KERN_SPLICING;
1583 }
1584
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001585 /* reflect what the L7 analysers have seen last */
1586 rqf_last = s->req->flags;
1587
1588 /*
1589 * Now forward all shutdown requests between both sides of the buffer
1590 */
1591
Willy Tarreau520d95e2009-09-19 21:04:57 +02001592 /* first, let's check if the request buffer needs to shutdown(write), which may
1593 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001594 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001595 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001596 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001597 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001598 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001599
1600 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001601 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 +01001602 s->req->cons->shutw(s->req->cons);
1603
1604 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001605 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1606 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001607 buffer_shutr_now(s->req);
1608
1609 /* shutdown(read) pending */
1610 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1611 s->req->prod->shutr(s->req->prod);
1612
Willy Tarreau520d95e2009-09-19 21:04:57 +02001613 /* it's possible that an upper layer has requested a connection setup or abort.
1614 * There are 2 situations where we decide to establish a new connection :
1615 * - there are data scheduled for emission in the buffer
1616 * - the BF_AUTO_CONNECT flag is set (active connection)
1617 */
1618 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001619 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001620 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001621 /* If we have an iohandler without a connect method, we immediately
1622 * switch to the connected state, otherwise we perform a connection
1623 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001624 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001625 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001626 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001627 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001628 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001629 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1630 s->req->wex = TICK_ETERNITY;
1631 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001632 }
Willy Tarreau73201222009-08-16 18:27:24 +02001633 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001634 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001635 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001636 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1637 buffer_shutr_now(s->rep);
1638 }
Willy Tarreau92795622009-03-06 12:51:23 +01001639 }
1640
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001641
1642 /* we may have a pending connection request, or a connection waiting
1643 * for completion.
1644 */
1645 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1646 do {
1647 /* nb: step 1 might switch from QUE to ASS, but we first want
1648 * to give a chance to step 2 to perform a redirect if needed.
1649 */
1650 if (s->si[1].state != SI_ST_REQ)
1651 sess_update_stream_int(s, &s->si[1]);
1652 if (s->si[1].state == SI_ST_REQ)
1653 sess_prepare_conn_req(s, &s->si[1]);
1654
1655 if (s->si[1].state == SI_ST_ASS && s->srv &&
1656 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1657 perform_http_redirect(s, &s->si[1]);
1658 } while (s->si[1].state == SI_ST_ASS);
1659 }
1660
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001661 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001662 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001663 goto resync_stream_interface;
1664
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001665 /* otherwise wewant to check if we need to resync the req buffer or not */
1666 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001667 goto resync_request;
1668
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001669 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001670
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001671 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001672 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001673 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001674 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001675 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001676 (s->rep->prod->state >= SI_ST_EST) &&
1677 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001678 /* This buffer is freewheeling, there's no analyser nor hijacker
1679 * attached to it. If any data are left in, we'll permit them to
1680 * move.
1681 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001682 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001683 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001684 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001685 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1686 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001687 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001688
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001689 /* check if it is wise to enable kernel splicing to forward response data */
1690 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1691 s->rep->to_forward &&
1692 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001693 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001694 (pipes_used < global.maxpipes) &&
1695 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1696 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1697 (s->rep->flags & BF_STREAMER_FAST)))) {
1698 s->rep->flags |= BF_KERN_SPLICING;
1699 }
1700
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001701 /* reflect what the L7 analysers have seen last */
1702 rpf_last = s->rep->flags;
1703
1704 /*
1705 * Now forward all shutdown requests between both sides of the buffer
1706 */
1707
1708 /*
1709 * FIXME: this is probably where we should produce error responses.
1710 */
1711
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001712 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001713 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1714 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001715 buffer_shutw_now(s->rep);
1716
1717 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001718 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 +01001719 s->rep->cons->shutw(s->rep->cons);
1720
1721 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001722 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1723 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001724 buffer_shutr_now(s->rep);
1725
1726 /* shutdown(read) pending */
1727 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1728 s->rep->prod->shutr(s->rep->prod);
1729
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001730 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001731 goto resync_stream_interface;
1732
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001733 if (s->req->flags != rqf_last)
1734 goto resync_request;
1735
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001736 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001737 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001738
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001739 /* we're interested in getting wakeups again */
1740 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1741 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1742
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001743 /* This is needed only when debugging is enabled, to indicate
1744 * client-side or server-side close. Please note that in the unlikely
1745 * event where both sides would close at once, the sequence is reported
1746 * on the server side first.
1747 */
1748 if (unlikely((global.mode & MODE_DEBUG) &&
1749 (!(global.mode & MODE_QUIET) ||
1750 (global.mode & MODE_VERBOSE)))) {
1751 int len;
1752
1753 if (s->si[1].state == SI_ST_CLO &&
1754 s->si[1].prev_state == SI_ST_EST) {
1755 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1756 s->uniq_id, s->be->id,
1757 (unsigned short)s->si[0].fd,
1758 (unsigned short)s->si[1].fd);
1759 write(1, trash, len);
1760 }
1761
1762 if (s->si[0].state == SI_ST_CLO &&
1763 s->si[0].prev_state == SI_ST_EST) {
1764 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1765 s->uniq_id, s->be->id,
1766 (unsigned short)s->si[0].fd,
1767 (unsigned short)s->si[1].fd);
1768 write(1, trash, len);
1769 }
1770 }
1771
1772 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1773 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1774
1775 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1776 session_process_counters(s);
1777
Willy Tarreau1accfc02009-09-05 20:57:35 +02001778 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001779 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001780
Willy Tarreau1accfc02009-09-05 20:57:35 +02001781 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001782 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001784 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1785 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 +01001786 s->si[0].prev_state = s->si[0].state;
1787 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001788 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1789 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001790
1791 /* Trick: if a request is being waiting for the server to respond,
1792 * and if we know the server can timeout, we don't want the timeout
1793 * to expire on the client side first, but we're still interested
1794 * in passing data from the client to the server (eg: POST). Thus,
1795 * we can cancel the client's request timeout if the server's
1796 * request timeout is set and the server has not yet sent a response.
1797 */
1798
Willy Tarreau520d95e2009-09-19 21:04:57 +02001799 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001800 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1801 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001802 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001803 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001804
Willy Tarreau1accfc02009-09-05 20:57:35 +02001805 /* Call the second stream interface's I/O handler if it's embedded.
1806 * Note that this one may wake the task up again.
1807 */
1808 if (s->req->cons->iohandler) {
1809 s->req->cons->iohandler(s->req->cons);
1810 if (task_in_rq(t)) {
1811 /* If we woke up, we don't want to requeue the
1812 * task to the wait queue, but rather requeue
1813 * it into the runqueue ASAP.
1814 */
1815 t->expire = TICK_ETERNITY;
1816 return t;
1817 }
1818 }
1819
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001820 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1821 tick_first(s->rep->rex, s->rep->wex));
1822 if (s->req->analysers)
1823 t->expire = tick_first(t->expire, s->req->analyse_exp);
1824
1825 if (s->si[0].exp)
1826 t->expire = tick_first(t->expire, s->si[0].exp);
1827
1828 if (s->si[1].exp)
1829 t->expire = tick_first(t->expire, s->si[1].exp);
1830
1831#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001832 fprintf(stderr,
1833 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1834 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1835 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1836 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 +01001837#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001838
1839#ifdef DEBUG_DEV
1840 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001841 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001842 ABORT_NOW();
1843#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001844 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001845 }
1846
1847 s->fe->feconn--;
1848 if (s->flags & SN_BE_ASSIGNED)
1849 s->be->beconn--;
1850 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001851 s->listener->nbconn--;
1852 if (s->listener->state == LI_FULL &&
1853 s->listener->nbconn < s->listener->maxconn) {
1854 /* we should reactivate the listener */
1855 EV_FD_SET(s->listener->fd, DIR_RD);
1856 s->listener->state = LI_READY;
1857 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001858
1859 if (unlikely((global.mode & MODE_DEBUG) &&
1860 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1861 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001862 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001864 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001865 write(1, trash, len);
1866 }
1867
1868 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1869 session_process_counters(s);
1870
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001871 if (s->txn.status) {
1872 int n;
1873
1874 n = s->txn.status / 100;
1875 if (n < 1 || n > 5)
1876 n = 0;
1877
1878 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001879 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001880
Willy Tarreau24657792010-02-26 10:30:28 +01001881 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001882 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001883 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001884 }
1885
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886 /* let's do a final log if we need it */
1887 if (s->logs.logwait &&
1888 !(s->flags & SN_MONITOR) &&
1889 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001890 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 }
1892
1893 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001894 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001895 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001896 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001897 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001898}
1899
Willy Tarreau7c669d72008-06-20 15:04:11 +02001900/*
1901 * This function adjusts sess->srv_conn and maintains the previous and new
1902 * server's served session counts. Setting newsrv to NULL is enough to release
1903 * current connection slot. This function also notifies any LB algo which might
1904 * expect to be informed about any change in the number of active sessions on a
1905 * server.
1906 */
1907void sess_change_server(struct session *sess, struct server *newsrv)
1908{
1909 if (sess->srv_conn == newsrv)
1910 return;
1911
1912 if (sess->srv_conn) {
1913 sess->srv_conn->served--;
1914 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1915 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1916 sess->srv_conn = NULL;
1917 }
1918
1919 if (newsrv) {
1920 newsrv->served++;
1921 if (newsrv->proxy->lbprm.server_take_conn)
1922 newsrv->proxy->lbprm.server_take_conn(newsrv);
1923 sess->srv_conn = newsrv;
1924 }
1925}
1926
Willy Tarreau84455332009-03-15 22:34:05 +01001927/* Set correct session termination flags in case no analyser has done it. It
1928 * also counts a failed request if the server state has not reached the request
1929 * stage.
1930 */
1931void sess_set_term_flags(struct session *s)
1932{
1933 if (!(s->flags & SN_FINST_MASK)) {
1934 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001935
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001936 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001937 if (s->listener->counters)
1938 s->listener->counters->failed_req++;
1939
Willy Tarreau84455332009-03-15 22:34:05 +01001940 s->flags |= SN_FINST_R;
1941 }
1942 else if (s->si[1].state == SI_ST_QUE)
1943 s->flags |= SN_FINST_Q;
1944 else if (s->si[1].state < SI_ST_EST)
1945 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01001946 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01001947 s->flags |= SN_FINST_D;
1948 else
1949 s->flags |= SN_FINST_L;
1950 }
1951}
1952
1953/* Handle server-side errors for default protocols. It is called whenever a a
1954 * connection setup is aborted or a request is aborted in queue. It sets the
1955 * session termination flags so that the caller does not have to worry about
1956 * them. It's installed as ->srv_error for the server-side stream_interface.
1957 */
1958void default_srv_error(struct session *s, struct stream_interface *si)
1959{
1960 int err_type = si->err_type;
1961 int err = 0, fin = 0;
1962
1963 if (err_type & SI_ET_QUEUE_ABRT) {
1964 err = SN_ERR_CLICL;
1965 fin = SN_FINST_Q;
1966 }
1967 else if (err_type & SI_ET_CONN_ABRT) {
1968 err = SN_ERR_CLICL;
1969 fin = SN_FINST_C;
1970 }
1971 else if (err_type & SI_ET_QUEUE_TO) {
1972 err = SN_ERR_SRVTO;
1973 fin = SN_FINST_Q;
1974 }
1975 else if (err_type & SI_ET_QUEUE_ERR) {
1976 err = SN_ERR_SRVCL;
1977 fin = SN_FINST_Q;
1978 }
1979 else if (err_type & SI_ET_CONN_TO) {
1980 err = SN_ERR_SRVTO;
1981 fin = SN_FINST_C;
1982 }
1983 else if (err_type & SI_ET_CONN_ERR) {
1984 err = SN_ERR_SRVCL;
1985 fin = SN_FINST_C;
1986 }
1987 else /* SI_ET_CONN_OTHER and others */ {
1988 err = SN_ERR_INTERNAL;
1989 fin = SN_FINST_C;
1990 }
1991
1992 if (!(s->flags & SN_ERR_MASK))
1993 s->flags |= err;
1994 if (!(s->flags & SN_FINST_MASK))
1995 s->flags |= fin;
1996}
Willy Tarreau7c669d72008-06-20 15:04:11 +02001997
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998/*
1999 * Local variables:
2000 * c-indent-level: 8
2001 * c-basic-offset: 8
2002 * End:
2003 */