blob: b09caaeee92b7dd40dda01964b2217e7dd26770c [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;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020066 s->tracked_counters = NULL;
67 s->tracked_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020068
69 /* if this session comes from a known monitoring system, we want to ignore
70 * it as soon as possible, which means closing it immediately for TCP, but
71 * cleanly.
72 */
73 if (unlikely((l->options & LI_O_CHK_MONNET) &&
74 addr->ss_family == AF_INET &&
75 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
76 if (p->mode == PR_MODE_TCP) {
77 pool_free2(pool2_session, s);
78 return 0;
79 }
80 s->flags |= SN_MONITOR;
81 s->logs.logwait = 0;
82 }
83
84 /* OK, we're keeping the session, so let's properly initialize the session */
85 LIST_ADDQ(&sessions, &s->list);
86 LIST_INIT(&s->back_refs);
87
88 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
89 Alert("out of memory in event_accept().\n");
90 goto out_free_session;
91 }
92
93 s->term_trace = 0;
94 s->cli_addr = *addr;
95 s->logs.accept_date = date; /* user-visible date for logging */
96 s->logs.tv_accept = now; /* corrected date for internal use */
97 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +020098 p->feconn++; /* beconn will be increased once assigned */
99
Willy Tarreaub36b4242010-06-04 20:59:39 +0200100 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200101
102 t->process = l->handler;
103 t->context = s;
104 t->nice = l->nice;
105 t->expire = TICK_ETERNITY;
106
107 s->task = t;
108 s->listener = l;
109
110 /* Note: initially, the session's backend points to the frontend.
111 * This changes later when switching rules are executed or
112 * when the default backend is assigned.
113 */
114 s->be = s->fe = p;
115 s->req = s->rep = NULL; /* will be allocated later */
116
117 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
118 * to abort right here as soon as possible, we check the rules before
119 * even initializing the stream interfaces.
120 */
121 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200122 if (s->tracked_counters)
123 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200124 task_free(t);
125 LIST_DEL(&s->list);
126 pool_free2(pool2_session, s);
127 /* let's do a no-linger now to close with a single RST. */
128 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200129 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 return 0;
131 }
132
Willy Tarreaub36b4242010-06-04 20:59:39 +0200133 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200134 if (p->feconn > p->counters.feconn_max)
135 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
137
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200138 /* this part should be common with other protocols */
139 s->si[0].fd = cfd;
140 s->si[0].owner = t;
141 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
142 s->si[0].err_type = SI_ET_NONE;
143 s->si[0].err_loc = NULL;
144 s->si[0].connect = NULL;
145 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200146 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200147 s->si[0].exp = TICK_ETERNITY;
148 s->si[0].flags = SI_FL_NONE;
149
150 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
151 s->si[0].flags |= SI_FL_INDEP_STR;
152
153 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
154 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
155
156 /* add the various callbacks */
157 stream_sock_prepare_interface(&s->si[0]);
158
159 /* pre-initialize the other side's stream interface to an INIT state. The
160 * callbacks will be initialized before attempting to connect.
161 */
162 s->si[1].fd = -1; /* just to help with debugging */
163 s->si[1].owner = t;
164 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
165 s->si[1].err_type = SI_ET_NONE;
166 s->si[1].err_loc = NULL;
167 s->si[1].connect = NULL;
168 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200169 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->si[1].shutr = stream_int_shutr;
171 s->si[1].shutw = stream_int_shutw;
172 s->si[1].exp = TICK_ETERNITY;
173 s->si[1].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[1].flags |= SI_FL_INDEP_STR;
177
178 s->srv = s->prev_srv = s->srv_conn = NULL;
179 s->pend_pos = NULL;
180
181 /* init store persistence */
182 s->store_count = 0;
183
184 /* Adjust some socket options */
185 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
186 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
187 goto out_free_task;
188 }
189
190 txn = &s->txn;
191 /* Those variables will be checked and freed if non-NULL in
192 * session.c:session_free(). It is important that they are
193 * properly initialized.
194 */
195 txn->sessid = NULL;
196 txn->srv_cookie = NULL;
197 txn->cli_cookie = NULL;
198 txn->uri = NULL;
199 txn->req.cap = NULL;
200 txn->rsp.cap = NULL;
201 txn->hdr_idx.v = NULL;
202 txn->hdr_idx.size = txn->hdr_idx.used = 0;
203
204 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
205 goto out_free_task; /* no memory */
206
207 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
208 goto out_free_req; /* no memory */
209
210 /* initialize the request buffer */
211 s->req->size = global.tune.bufsize;
212 buffer_init(s->req);
213 s->req->prod = &s->si[0];
214 s->req->cons = &s->si[1];
215 s->si[0].ib = s->si[1].ob = s->req;
216 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
217
218 /* activate default analysers enabled for this listener */
219 s->req->analysers = l->analysers;
220
221 s->req->wto = TICK_ETERNITY;
222 s->req->rto = TICK_ETERNITY;
223 s->req->rex = TICK_ETERNITY;
224 s->req->wex = TICK_ETERNITY;
225 s->req->analyse_exp = TICK_ETERNITY;
226
227 /* initialize response buffer */
228 s->rep->size = global.tune.bufsize;
229 buffer_init(s->rep);
230 s->rep->prod = &s->si[1];
231 s->rep->cons = &s->si[0];
232 s->si[0].ob = s->si[1].ib = s->rep;
233 s->rep->analysers = 0;
234
235 s->rep->rto = TICK_ETERNITY;
236 s->rep->wto = TICK_ETERNITY;
237 s->rep->rex = TICK_ETERNITY;
238 s->rep->wex = TICK_ETERNITY;
239 s->rep->analyse_exp = TICK_ETERNITY;
240
241 /* finish initialization of the accepted file descriptor */
242 fd_insert(cfd);
243 fdtab[cfd].owner = &s->si[0];
244 fdtab[cfd].state = FD_STREADY;
245 fdtab[cfd].flags = 0;
246 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
247 fdtab[cfd].cb[DIR_RD].b = s->req;
248 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
249 fdtab[cfd].cb[DIR_WR].b = s->rep;
250 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
251 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
252 EV_FD_SET(cfd, DIR_RD);
253
254 if (p->accept) {
255 int ret = p->accept(s);
256 if (unlikely(ret < 0))
257 goto out_free_rep;
258
259 if (unlikely(ret == 0)) {
260 /* work is finished, we can release everything (eg: monitoring) */
261 pool_free2(pool2_buffer, s->rep);
262 pool_free2(pool2_buffer, s->req);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200263 if (s->tracked_counters)
264 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200265 task_free(t);
266 LIST_DEL(&s->list);
267 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200268 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200269 return 0;
270 }
271 }
272
273 /* it is important not to call the wakeup function directly but to
274 * pass through task_wakeup(), because this one knows how to apply
275 * priorities to tasks.
276 */
277 task_wakeup(t, TASK_WOKEN_INIT);
278 return 1;
279
280 /* Error unrolling */
281 out_free_rep:
282 pool_free2(pool2_buffer, s->rep);
283 out_free_req:
284 pool_free2(pool2_buffer, s->req);
285 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200286 p->feconn--;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200287 if (s->tracked_counters)
288 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200289 task_free(t);
290 out_free_session:
291 LIST_DEL(&s->list);
292 pool_free2(pool2_session, s);
293 out_close:
294 return -1;
295}
296
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297/*
298 * frees the context associated to a session. It must have been removed first.
299 */
300void session_free(struct session *s)
301{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100302 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200303 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100304 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200305 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100306
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 if (s->pend_pos)
308 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100309
Willy Tarreau1e62de62008-11-11 20:20:02 +0100310 if (s->srv) { /* there may be requests left pending in queue */
311 if (s->flags & SN_CURR_SESS) {
312 s->flags &= ~SN_CURR_SESS;
313 s->srv->cur_sess--;
314 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100315 if (may_dequeue_tasks(s->srv, s->be))
316 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100317 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100318
Willy Tarreau7c669d72008-06-20 15:04:11 +0200319 if (unlikely(s->srv_conn)) {
320 /* the session still has a reserved slot on a server, but
321 * it should normally be only the same as the one above,
322 * so this should not happen in fact.
323 */
324 sess_change_server(s, NULL);
325 }
326
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100327 if (s->req->pipe)
328 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100329
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100330 if (s->rep->pipe)
331 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100332
Willy Tarreau48d63db2008-08-03 17:41:33 +0200333 pool_free2(pool2_buffer, s->req);
334 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335
Willy Tarreau46023632010-01-07 22:51:47 +0100336 http_end_txn(s);
337
Willy Tarreaua4cda672010-06-06 18:28:49 +0200338 for (i = 0; i < s->store_count; i++) {
339 if (!s->store[i].ts)
340 continue;
341 stksess_free(s->store[i].table, s->store[i].ts);
342 s->store[i].ts = NULL;
343 }
344
Willy Tarreau92fb9832007-10-16 17:34:28 +0200345 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200346 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100347 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
348 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100350
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200351 if (s->tracked_counters)
352 session_store_counters(s);
353
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100354 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100355 /* we have to unlink all watchers. We must not relink them if
356 * this session was the last one in the list.
357 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100358 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100359 LIST_INIT(&bref->users);
360 if (s->list.n != &sessions)
361 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100362 bref->ref = s->list.n;
363 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100364 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200365 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200366
367 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200368 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200369 pool_flush2(pool2_buffer);
370 pool_flush2(fe->hdr_idx_pool);
371 pool_flush2(pool2_requri);
372 pool_flush2(pool2_capture);
373 pool_flush2(pool2_session);
374 pool_flush2(fe->req_cap_pool);
375 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200376 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200377}
378
379
380/* perform minimal intializations, report 0 in case of error, 1 if OK. */
381int init_session()
382{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100383 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200384 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
385 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386}
387
Willy Tarreau30e71012007-11-26 20:15:35 +0100388void session_process_counters(struct session *s)
389{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100390 unsigned long long bytes;
391
Willy Tarreau30e71012007-11-26 20:15:35 +0100392 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100393 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100394 s->logs.bytes_in = s->req->total;
395 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200396 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100397
Willy Tarreau30e71012007-11-26 20:15:35 +0100398 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200399 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100400
Willy Tarreau30e71012007-11-26 20:15:35 +0100401 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200402 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200403
404 if (s->listener->counters)
405 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200406
407 if (s->tracked_counters) {
408 void *ptr = stktable_data_ptr(s->tracked_table,
409 s->tracked_counters,
410 STKTABLE_DT_BYTES_IN_CNT);
411 if (ptr)
412 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
413 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100414 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100415 }
416
Willy Tarreau30e71012007-11-26 20:15:35 +0100417 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100418 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100419 s->logs.bytes_out = s->rep->total;
420 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200421 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422
Willy Tarreau30e71012007-11-26 20:15:35 +0100423 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200424 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100425
Willy Tarreau30e71012007-11-26 20:15:35 +0100426 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200427 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200428
429 if (s->listener->counters)
430 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200431
432 if (s->tracked_counters) {
433 void *ptr = stktable_data_ptr(s->tracked_table,
434 s->tracked_counters,
435 STKTABLE_DT_BYTES_OUT_CNT);
436 if (ptr)
437 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
438 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100439 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100440 }
441}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100443/* This function is called with (si->state == SI_ST_CON) meaning that a
444 * connection was attempted and that the file descriptor is already allocated.
445 * We must check for establishment, error and abort. Possible output states
446 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
447 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
448 * otherwise 1.
449 */
450int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
451{
452 struct buffer *req = si->ob;
453 struct buffer *rep = si->ib;
454
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100455 /* If we got an error, or if nothing happened and the connection timed
456 * out, we must give up. The CER state handler will take care of retry
457 * attempts and error reports.
458 */
459 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100460 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100461 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200462 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100463 fd_delete(si->fd);
464
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200465 if (si->release)
466 si->release(si);
467
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100468 if (si->err_type)
469 return 0;
470
471 si->err_loc = s->srv;
472 if (si->flags & SI_FL_ERR)
473 si->err_type = SI_ET_CONN_ERR;
474 else
475 si->err_type = SI_ET_CONN_TO;
476 return 0;
477 }
478
479 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200480 if (unlikely((rep->flags & BF_SHUTW) ||
481 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200482 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100483 s->be->options & PR_O_ABRT_CLOSE)))) {
484 /* give up */
485 si->shutw(si);
486 si->err_type |= SI_ET_CONN_ABRT;
487 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200488 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100489 if (s->srv_error)
490 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100491 return 1;
492 }
493
494 /* we need to wait a bit more if there was no activity either */
495 if (!(req->flags & BF_WRITE_ACTIVITY))
496 return 1;
497
498 /* OK, this means that a connection succeeded. The caller will be
499 * responsible for handling the transition from CON to EST.
500 */
501 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100502 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100503 si->state = SI_ST_EST;
504 si->err_type = SI_ET_NONE;
505 si->err_loc = NULL;
506 return 1;
507}
508
509/* This function is called with (si->state == SI_ST_CER) meaning that a
510 * previous connection attempt has failed and that the file descriptor
511 * has already been released. Possible causes include asynchronous error
512 * notification and time out. Possible output states are SI_ST_CLO when
513 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
514 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
515 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
516 * marked as in error state. It returns 0.
517 */
518int sess_update_st_cer(struct session *s, struct stream_interface *si)
519{
520 /* we probably have to release last session from the server */
521 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100522 health_adjust(s->srv, HANA_STATUS_L4_ERR);
523
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524 if (s->flags & SN_CURR_SESS) {
525 s->flags &= ~SN_CURR_SESS;
526 s->srv->cur_sess--;
527 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100528 }
529
530 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200531 si->conn_retries--;
532 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100533 if (!si->err_type) {
534 si->err_type = SI_ET_CONN_ERR;
535 si->err_loc = s->srv;
536 }
537
538 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200539 s->srv->counters.failed_conns++;
540 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 if (may_dequeue_tasks(s->srv, s->be))
542 process_srv_queue(s->srv);
543
544 /* shutw is enough so stop a connecting socket */
545 si->shutw(si);
546 si->ob->flags |= BF_WRITE_ERROR;
547 si->ib->flags |= BF_READ_ERROR;
548
549 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100550 if (s->srv_error)
551 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 return 0;
553 }
554
555 /* If the "redispatch" option is set on the backend, we are allowed to
556 * retry on another server for the last retry. In order to achieve this,
557 * we must mark the session unassigned, and eventually clear the DIRECT
558 * bit to ignore any persistence cookie. We won't count a retry nor a
559 * redispatch yet, because this will depend on what server is selected.
560 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200561 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100562 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 if (may_dequeue_tasks(s->srv, s->be))
564 process_srv_queue(s->srv);
565
566 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
567 s->prev_srv = s->srv;
568 si->state = SI_ST_REQ;
569 } else {
570 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200571 s->srv->counters.retries++;
572 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100573 si->state = SI_ST_ASS;
574 }
575
576 if (si->flags & SI_FL_ERR) {
577 /* The error was an asynchronous connection error, and we will
578 * likely have to retry connecting to the same server, most
579 * likely leading to the same result. To avoid this, we wait
580 * one second before retrying.
581 */
582
583 if (!si->err_type)
584 si->err_type = SI_ET_CONN_ERR;
585
586 si->state = SI_ST_TAR;
587 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
588 return 0;
589 }
590 return 0;
591}
592
593/*
594 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200595 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
596 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 */
598void sess_establish(struct session *s, struct stream_interface *si)
599{
600 struct buffer *req = si->ob;
601 struct buffer *rep = si->ib;
602
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100603 if (s->srv)
604 health_adjust(s->srv, HANA_STATUS_L4_OK);
605
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 /* if the user wants to log as soon as possible, without counting
608 * bytes from the server, then this is the right moment. */
609 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
610 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100611 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 }
614 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100615 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
616 /* reset hdr_idx which was already initialized by the request.
617 * right now, the http parser does it.
618 * hdr_idx_init(&s->txn.hdr_idx);
619 */
620 }
621
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200622 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200624 if (si->connect) {
625 /* real connections have timeouts */
626 req->wto = s->be->timeout.server;
627 rep->rto = s->be->timeout.server;
628 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629 req->wex = TICK_ETERNITY;
630}
631
632/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
633 * Other input states are simply ignored.
634 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
635 * Flags must have previously been updated for timeouts and other conditions.
636 */
637void sess_update_stream_int(struct session *s, struct stream_interface *si)
638{
639 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",
640 now_ms, __FUNCTION__,
641 s,
642 s->req, s->rep,
643 s->req->rex, s->rep->wex,
644 s->req->flags, s->rep->flags,
645 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
646
647 if (si->state == SI_ST_ASS) {
648 /* Server assigned to connection request, we have to try to connect now */
649 int conn_err;
650
651 conn_err = connect_server(s);
652 if (conn_err == SN_ERR_NONE) {
653 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100654 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100655 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656 return;
657 }
658
659 /* We have received a synchronous error. We might have to
660 * abort, retry immediately or redispatch.
661 */
662 if (conn_err == SN_ERR_INTERNAL) {
663 if (!si->err_type) {
664 si->err_type = SI_ET_CONN_OTHER;
665 si->err_loc = s->srv;
666 }
667
668 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100669 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100670 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200671 s->srv->counters.failed_conns++;
672 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673
674 /* release other sessions waiting for this server */
675 if (may_dequeue_tasks(s->srv, s->be))
676 process_srv_queue(s->srv);
677
678 /* Failed and not retryable. */
679 si->shutr(si);
680 si->shutw(si);
681 si->ob->flags |= BF_WRITE_ERROR;
682
683 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
684
685 /* no session was ever accounted for this server */
686 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100687 if (s->srv_error)
688 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 return;
690 }
691
692 /* We are facing a retryable error, but we don't want to run a
693 * turn-around now, as the problem is likely a source port
694 * allocation problem, so we want to retry now.
695 */
696 si->state = SI_ST_CER;
697 si->flags &= ~SI_FL_ERR;
698 sess_update_st_cer(s, si);
699 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
700 return;
701 }
702 else if (si->state == SI_ST_QUE) {
703 /* connection request was queued, check for any update */
704 if (!s->pend_pos) {
705 /* The connection is not in the queue anymore. Either
706 * we have a server connection slot available and we
707 * go directly to the assigned state, or we need to
708 * load-balance first and go to the INI state.
709 */
710 si->exp = TICK_ETERNITY;
711 if (unlikely(!(s->flags & SN_ASSIGNED)))
712 si->state = SI_ST_REQ;
713 else {
714 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
715 si->state = SI_ST_ASS;
716 }
717 return;
718 }
719
720 /* Connection request still in queue... */
721 if (si->flags & SI_FL_EXP) {
722 /* ... and timeout expired */
723 si->exp = TICK_ETERNITY;
724 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
725 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200726 s->srv->counters.failed_conns++;
727 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 si->shutr(si);
729 si->shutw(si);
730 si->ob->flags |= BF_WRITE_TIMEOUT;
731 if (!si->err_type)
732 si->err_type = SI_ET_QUEUE_TO;
733 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100734 if (s->srv_error)
735 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 return;
737 }
738
739 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200740 if ((si->ob->flags & (BF_READ_ERROR)) ||
741 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200742 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 /* give up */
744 si->exp = TICK_ETERNITY;
745 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
746 si->shutr(si);
747 si->shutw(si);
748 si->err_type |= SI_ET_QUEUE_ABRT;
749 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100750 if (s->srv_error)
751 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 return;
753 }
754
755 /* Nothing changed */
756 return;
757 }
758 else if (si->state == SI_ST_TAR) {
759 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200760 if ((si->ob->flags & (BF_READ_ERROR)) ||
761 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200762 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763 /* give up */
764 si->exp = TICK_ETERNITY;
765 si->shutr(si);
766 si->shutw(si);
767 si->err_type |= SI_ET_CONN_ABRT;
768 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100769 if (s->srv_error)
770 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100771 return;
772 }
773
774 if (!(si->flags & SI_FL_EXP))
775 return; /* still in turn-around */
776
777 si->exp = TICK_ETERNITY;
778
779 /* we keep trying on the same server as long as the session is
780 * marked "assigned".
781 * FIXME: Should we force a redispatch attempt when the server is down ?
782 */
783 if (s->flags & SN_ASSIGNED)
784 si->state = SI_ST_ASS;
785 else
786 si->state = SI_ST_REQ;
787 return;
788 }
789}
790
791/* This function initiates a server connection request on a stream interface
792 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
793 * indicating that a server has been assigned. It may also return SI_ST_QUE,
794 * or SI_ST_CLO upon error.
795 */
796static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
797 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",
798 now_ms, __FUNCTION__,
799 s,
800 s->req, s->rep,
801 s->req->rex, s->rep->wex,
802 s->req->flags, s->rep->flags,
803 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
804
805 if (si->state != SI_ST_REQ)
806 return;
807
808 /* Try to assign a server */
809 if (srv_redispatch_connect(s) != 0) {
810 /* We did not get a server. Either we queued the
811 * connection request, or we encountered an error.
812 */
813 if (si->state == SI_ST_QUE)
814 return;
815
816 /* we did not get any server, let's check the cause */
817 si->shutr(si);
818 si->shutw(si);
819 si->ob->flags |= BF_WRITE_ERROR;
820 if (!si->err_type)
821 si->err_type = SI_ET_CONN_OTHER;
822 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100823 if (s->srv_error)
824 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 return;
826 }
827
828 /* The server is assigned */
829 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
830 si->state = SI_ST_ASS;
831}
832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200833/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100834 * if appropriate. The default_backend rule is also considered, then the
835 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200836 * It returns 1 if the processing can continue on next analysers, or zero if it
837 * either needs more data or wants to immediately abort the request.
838 */
839int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
840{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200841 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200843 req->analysers &= ~an_bit;
844 req->analyse_exp = TICK_ETERNITY;
845
846 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
847 now_ms, __FUNCTION__,
848 s,
849 req,
850 req->rex, req->wex,
851 req->flags,
852 req->l,
853 req->analysers);
854
855 /* now check whether we have some switching rules for this request */
856 if (!(s->flags & SN_BE_ASSIGNED)) {
857 struct switching_rule *rule;
858
859 list_for_each_entry(rule, &s->fe->switching_rules, list) {
860 int ret;
861
862 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
863 ret = acl_pass(ret);
864 if (rule->cond->pol == ACL_COND_UNLESS)
865 ret = !ret;
866
867 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200868 if (!session_set_backend(s, rule->be.backend))
869 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200870 break;
871 }
872 }
873
874 /* To ensure correct connection accounting on the backend, we
875 * have to assign one if it was not set (eg: a listen). This
876 * measure also takes care of correctly setting the default
877 * backend if any.
878 */
879 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200880 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
881 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200882 }
883
Willy Tarreaufb356202010-08-03 14:02:05 +0200884 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
885 if (s->fe == s->be) {
886 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200887 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200888 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200889
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200890 /* 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 +0100891 * persistence rule, and report that in the session.
892 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200893 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100894 int ret = 1;
895
896 if (prst_rule->cond) {
897 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
898 ret = acl_pass(ret);
899 if (prst_rule->cond->pol == ACL_COND_UNLESS)
900 ret = !ret;
901 }
902
903 if (ret) {
904 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200905 if (prst_rule->type == PERSIST_TYPE_FORCE) {
906 s->flags |= SN_FORCE_PRST;
907 } else {
908 s->flags |= SN_IGNORE_PRST;
909 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100910 break;
911 }
912 }
913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200914 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200915
916 sw_failed:
917 /* immediately abort this request in case of allocation failure */
918 buffer_abort(s->req);
919 buffer_abort(s->rep);
920
921 if (!(s->flags & SN_ERR_MASK))
922 s->flags |= SN_ERR_RESOURCE;
923 if (!(s->flags & SN_FINST_MASK))
924 s->flags |= SN_FINST_R;
925
926 s->txn.status = 500;
927 s->req->analysers = 0;
928 s->req->analyse_exp = TICK_ETERNITY;
929 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200930}
931
Emeric Brun1d33b292010-01-04 15:47:17 +0100932/* This stream analyser works on a request. It applies all sticking rules on
933 * it then returns 1. The data must already be present in the buffer otherwise
934 * they won't match. It always returns 1.
935 */
936int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
937{
938 struct proxy *px = s->be;
939 struct sticking_rule *rule;
940
941 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
942 now_ms, __FUNCTION__,
943 s,
944 req,
945 req->rex, req->wex,
946 req->flags,
947 req->l,
948 req->analysers);
949
950 list_for_each_entry(rule, &px->sticking_rules, list) {
951 int ret = 1 ;
952 int i;
953
954 for (i = 0; i < s->store_count; i++) {
955 if (rule->table.t == s->store[i].table)
956 break;
957 }
958
959 if (i != s->store_count)
960 continue;
961
962 if (rule->cond) {
963 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
964 ret = acl_pass(ret);
965 if (rule->cond->pol == ACL_COND_UNLESS)
966 ret = !ret;
967 }
968
969 if (ret) {
970 struct stktable_key *key;
971
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200972 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +0100973 if (!key)
974 continue;
975
976 if (rule->flags & STK_IS_MATCH) {
977 struct stksess *ts;
978
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200979 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100980 if (!(s->flags & SN_ASSIGNED)) {
981 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +0200982 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +0100983
984 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +0200985 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
986 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +0100987 if (node) {
988 struct server *srv;
989
990 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100991 if ((srv->state & SRV_RUNNING) ||
992 (px->options & PR_O_PERSIST) ||
993 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100994 s->flags |= SN_DIRECT | SN_ASSIGNED;
995 s->srv = srv;
996 }
997 }
998 }
Willy Tarreaue8f63382010-07-13 15:20:24 +0200999 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001000 }
1001 }
1002 if (rule->flags & STK_IS_STORE) {
1003 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1004 struct stksess *ts;
1005
1006 ts = stksess_new(rule->table.t, key);
1007 if (ts) {
1008 s->store[s->store_count].table = rule->table.t;
1009 s->store[s->store_count++].ts = ts;
1010 }
1011 }
1012 }
1013 }
1014 }
1015
1016 req->analysers &= ~an_bit;
1017 req->analyse_exp = TICK_ETERNITY;
1018 return 1;
1019}
1020
1021/* This stream analyser works on a response. It applies all store rules on it
1022 * then returns 1. The data must already be present in the buffer otherwise
1023 * they won't match. It always returns 1.
1024 */
1025int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1026{
1027 struct proxy *px = s->be;
1028 struct sticking_rule *rule;
1029 int i;
1030
1031 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1032 now_ms, __FUNCTION__,
1033 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001034 rep,
1035 rep->rex, rep->wex,
1036 rep->flags,
1037 rep->l,
1038 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001039
1040 list_for_each_entry(rule, &px->storersp_rules, list) {
1041 int ret = 1 ;
1042 int storereqidx = -1;
1043
1044 for (i = 0; i < s->store_count; i++) {
1045 if (rule->table.t == s->store[i].table) {
1046 if (!(s->store[i].flags))
1047 storereqidx = i;
1048 break;
1049 }
1050 }
1051
1052 if ((i != s->store_count) && (storereqidx == -1))
1053 continue;
1054
1055 if (rule->cond) {
1056 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1057 ret = acl_pass(ret);
1058 if (rule->cond->pol == ACL_COND_UNLESS)
1059 ret = !ret;
1060 }
1061
1062 if (ret) {
1063 struct stktable_key *key;
1064
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001065 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001066 if (!key)
1067 continue;
1068
1069 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001070 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001071 s->store[storereqidx].flags = 1;
1072 }
1073 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1074 struct stksess *ts;
1075
1076 ts = stksess_new(rule->table.t, key);
1077 if (ts) {
1078 s->store[s->store_count].table = rule->table.t;
1079 s->store[s->store_count].flags = 1;
1080 s->store[s->store_count++].ts = ts;
1081 }
1082 }
1083 }
1084 }
1085
1086 /* process store request and store response */
1087 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001088 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001089 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001090
1091 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1092 if (ts) {
1093 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001094 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001095 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001096 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001097 else
1098 ts = stktable_store(s->store[i].table, s->store[i].ts);
1099
1100 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001101 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1102 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001103 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001104 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001105
1106 rep->analysers &= ~an_bit;
1107 rep->analyse_exp = TICK_ETERNITY;
1108 return 1;
1109}
1110
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001111/* This macro is very specific to the function below. See the comments in
1112 * process_session() below to understand the logic and the tests.
1113 */
1114#define UPDATE_ANALYSERS(real, list, back, flag) { \
1115 list = (((list) & ~(flag)) | ~(back)) & (real); \
1116 back = real; \
1117 if (!(list)) \
1118 break; \
1119 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1120 continue; \
1121}
1122
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001123/* Processes the client, server, request and response jobs of a session task,
1124 * then puts it back to the wait queue in a clean state, or cleans up its
1125 * resources if it must be deleted. Returns in <next> the date the task wants
1126 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1127 * nothing too many times, the request and response buffers flags are monitored
1128 * and each function is called only if at least another function has changed at
1129 * least one flag it is interested in.
1130 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001131struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001132{
1133 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001135 unsigned int rq_prod_last, rq_cons_last;
1136 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001137 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001138
1139 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1140 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1141
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001142 /* this data may be no longer valid, clear it */
1143 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1144
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001145 /* This flag must explicitly be set every time */
1146 s->req->flags &= ~BF_READ_NOEXP;
1147
1148 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1149 rqf_last = s->req->flags;
1150 rpf_last = s->rep->flags;
1151
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001152 /* we don't want the stream interface functions to recursively wake us up */
1153 if (s->req->prod->owner == t)
1154 s->req->prod->flags |= SI_FL_DONT_WAKE;
1155 if (s->req->cons->owner == t)
1156 s->req->cons->flags |= SI_FL_DONT_WAKE;
1157
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001158 /* 1a: Check for low level timeouts if needed. We just set a flag on
1159 * stream interfaces when their timeouts have expired.
1160 */
1161 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1162 stream_int_check_timeouts(&s->si[0]);
1163 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001164
1165 /* check buffer timeouts, and close the corresponding stream interfaces
1166 * for future reads or writes. Note: this will also concern upper layers
1167 * but we do not touch any other flag. We must be careful and correctly
1168 * detect state changes when calling them.
1169 */
1170
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001171 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001172
Willy Tarreau14641402009-12-29 14:49:56 +01001173 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1174 s->req->cons->flags |= SI_FL_NOLINGER;
1175 s->req->cons->shutw(s->req->cons);
1176 }
1177
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001178 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1179 s->req->prod->shutr(s->req->prod);
1180
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001181 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001182
Willy Tarreau14641402009-12-29 14:49:56 +01001183 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1184 s->rep->cons->flags |= SI_FL_NOLINGER;
1185 s->rep->cons->shutw(s->rep->cons);
1186 }
1187
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001188 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1189 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001190 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001191
1192 /* 1b: check for low-level errors reported at the stream interface.
1193 * First we check if it's a retryable error (in which case we don't
1194 * want to tell the buffer). Otherwise we report the error one level
1195 * upper by setting flags into the buffers. Note that the side towards
1196 * the client cannot have connect (hence retryable) errors. Also, the
1197 * connection setup code must be able to deal with any type of abort.
1198 */
1199 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1200 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1201 s->si[0].shutr(&s->si[0]);
1202 s->si[0].shutw(&s->si[0]);
1203 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001204 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001205 s->be->counters.cli_aborts++;
1206 if (s->srv)
1207 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001208 if (!(s->flags & SN_ERR_MASK))
1209 s->flags |= SN_ERR_CLICL;
1210 if (!(s->flags & SN_FINST_MASK))
1211 s->flags |= SN_FINST_D;
1212 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213 }
1214 }
1215
1216 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1217 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1218 s->si[1].shutr(&s->si[1]);
1219 s->si[1].shutw(&s->si[1]);
1220 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001221 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001222 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001223 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001224 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001225 s->be->counters.srv_aborts++;
1226 if (s->srv)
1227 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001228 if (!(s->flags & SN_ERR_MASK))
1229 s->flags |= SN_ERR_SRVCL;
1230 if (!(s->flags & SN_FINST_MASK))
1231 s->flags |= SN_FINST_D;
1232 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001233 }
1234 /* note: maybe we should process connection errors here ? */
1235 }
1236
1237 if (s->si[1].state == SI_ST_CON) {
1238 /* we were trying to establish a connection on the server side,
1239 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1240 */
1241 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1242 sess_update_st_cer(s, &s->si[1]);
1243 else if (s->si[1].state == SI_ST_EST)
1244 sess_establish(s, &s->si[1]);
1245
1246 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1247 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1248 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1249 */
1250 }
1251
Willy Tarreau815a9b22010-07-27 17:15:12 +02001252 rq_prod_last = s->si[0].state;
1253 rq_cons_last = s->si[1].state;
1254 rp_cons_last = s->si[0].state;
1255 rp_prod_last = s->si[1].state;
1256
1257 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001258 /* Check for connection closure */
1259
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001260 DPRINTF(stderr,
1261 "[%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",
1262 now_ms, __FUNCTION__, __LINE__,
1263 t,
1264 s, s->flags,
1265 s->req, s->rep,
1266 s->req->rex, s->rep->wex,
1267 s->req->flags, s->rep->flags,
1268 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1269 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001270 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001271
1272 /* nothing special to be done on client side */
1273 if (unlikely(s->req->prod->state == SI_ST_DIS))
1274 s->req->prod->state = SI_ST_CLO;
1275
1276 /* When a server-side connection is released, we have to count it and
1277 * check for pending connections on this server.
1278 */
1279 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1280 s->req->cons->state = SI_ST_CLO;
1281 if (s->srv) {
1282 if (s->flags & SN_CURR_SESS) {
1283 s->flags &= ~SN_CURR_SESS;
1284 s->srv->cur_sess--;
1285 }
1286 sess_change_server(s, NULL);
1287 if (may_dequeue_tasks(s->srv, s->be))
1288 process_srv_queue(s->srv);
1289 }
1290 }
1291
1292 /*
1293 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1294 * at this point.
1295 */
1296
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001297 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001298 /* Analyse request */
1299 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001300 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1301 s->si[0].state != rq_prod_last ||
1302 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303 unsigned int flags = s->req->flags;
1304
1305 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001306 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001307 unsigned int ana_list;
1308 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001309
Willy Tarreau90deb182010-01-07 00:20:41 +01001310 /* it's up to the analysers to stop new connections,
1311 * disable reading or closing. Note: if an analyser
1312 * disables any of these bits, it is responsible for
1313 * enabling them again when it disables itself, so
1314 * that other analysers are called in similar conditions.
1315 */
1316 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001317 buffer_auto_connect(s->req);
1318 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001319
1320 /* We will call all analysers for which a bit is set in
1321 * s->req->analysers, following the bit order from LSB
1322 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001323 * the list when not needed. Any analyser may return 0
1324 * to break out of the loop, either because of missing
1325 * data to take a decision, or because it decides to
1326 * kill the session. We loop at least once through each
1327 * analyser, and we may loop again if other analysers
1328 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001329 *
1330 * We build a list of analysers to run. We evaluate all
1331 * of these analysers in the order of the lower bit to
1332 * the higher bit. This ordering is very important.
1333 * An analyser will often add/remove other analysers,
1334 * including itself. Any changes to itself have no effect
1335 * on the loop. If it removes any other analysers, we
1336 * want those analysers not to be called anymore during
1337 * this loop. If it adds an analyser that is located
1338 * after itself, we want it to be scheduled for being
1339 * processed during the loop. If it adds an analyser
1340 * which is located before it, we want it to switch to
1341 * it immediately, even if it has already been called
1342 * once but removed since.
1343 *
1344 * In order to achieve this, we compare the analyser
1345 * list after the call with a copy of it before the
1346 * call. The work list is fed with analyser bits that
1347 * appeared during the call. Then we compare previous
1348 * work list with the new one, and check the bits that
1349 * appeared. If the lowest of these bits is lower than
1350 * the current bit, it means we have enabled a previous
1351 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001352 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001353
1354 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001355 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001356 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001357
Willy Tarreaufb356202010-08-03 14:02:05 +02001358 if (ana_list & AN_REQ_INSPECT_FE) {
1359 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001360 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001361 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001362 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001363
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001364 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001365 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001366 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001367 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001368 }
1369
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001370 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001371 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1372 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001373 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001374 }
1375
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001376 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001377 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1378 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001379 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001380 }
1381
Willy Tarreaufb356202010-08-03 14:02:05 +02001382 if (ana_list & AN_REQ_INSPECT_BE) {
1383 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1384 break;
1385 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1386 }
1387
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001388 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001389 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1390 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001391 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001392 }
1393
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001394 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001395 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001396 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001397 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001398 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001399
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001400 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001401 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1402 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001403 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001404 }
1405
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001406 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001407 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001408 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001409 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001410 }
Emeric Brun647caf12009-06-30 17:57:00 +02001411
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001412 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001413 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1414 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001415 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001416 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001417
Emeric Brun1d33b292010-01-04 15:47:17 +01001418 if (ana_list & AN_REQ_STICKING_RULES) {
1419 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1420 break;
1421 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1422 }
1423
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001424 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001425 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1426 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001427 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001428 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001429 break;
1430 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001431 }
Willy Tarreau84455332009-03-15 22:34:05 +01001432
Willy Tarreau815a9b22010-07-27 17:15:12 +02001433 rq_prod_last = s->si[0].state;
1434 rq_cons_last = s->si[1].state;
1435 rqf_last = s->req->flags;
1436
1437 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001438 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001439 }
1440
Willy Tarreau576507f2010-01-07 00:09:04 +01001441 /* we'll monitor the request analysers while parsing the response,
1442 * because some response analysers may indirectly enable new request
1443 * analysers (eg: HTTP keep-alive).
1444 */
1445 req_ana_back = s->req->analysers;
1446
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001447 resync_response:
1448 /* Analyse response */
1449
1450 if (unlikely(s->rep->flags & BF_HIJACK)) {
1451 /* In inject mode, we wake up everytime something has
1452 * happened on the write side of the buffer.
1453 */
1454 unsigned int flags = s->rep->flags;
1455
1456 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1457 !(s->rep->flags & BF_FULL)) {
1458 s->rep->hijacker(s, s->rep);
1459 }
1460
1461 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1462 rpf_last = s->rep->flags;
1463 goto resync_response;
1464 }
1465 }
1466 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001467 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1468 s->si[0].state != rp_cons_last ||
1469 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001470 unsigned int flags = s->rep->flags;
1471
1472 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001473 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 unsigned int ana_list;
1475 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001476
Willy Tarreau90deb182010-01-07 00:20:41 +01001477 /* it's up to the analysers to stop disable reading or
1478 * closing. Note: if an analyser disables any of these
1479 * bits, it is responsible for enabling them again when
1480 * it disables itself, so that other analysers are called
1481 * in similar conditions.
1482 */
1483 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001484 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001485
1486 /* We will call all analysers for which a bit is set in
1487 * s->rep->analysers, following the bit order from LSB
1488 * to MSB. The analysers must remove themselves from
1489 * the list when not needed. Any analyser may return 0
1490 * to break out of the loop, either because of missing
1491 * data to take a decision, or because it decides to
1492 * kill the session. We loop at least once through each
1493 * analyser, and we may loop again if other analysers
1494 * are added in the middle.
1495 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496
1497 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001498 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 /* Warning! ensure that analysers are always placed in ascending order! */
1500
1501 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001502 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1503 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001505 }
1506
Emeric Brun1d33b292010-01-04 15:47:17 +01001507 if (ana_list & AN_RES_STORE_RULES) {
1508 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1509 break;
1510 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1511 }
1512
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001513 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001514 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1515 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001516 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001517 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001518
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001519 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001520 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1521 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001522 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001523 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001524 break;
1525 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001526 }
1527
Willy Tarreau815a9b22010-07-27 17:15:12 +02001528 rp_cons_last = s->si[0].state;
1529 rp_prod_last = s->si[1].state;
1530 rpf_last = s->rep->flags;
1531
1532 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001533 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001534 }
1535
Willy Tarreau576507f2010-01-07 00:09:04 +01001536 /* maybe someone has added some request analysers, so we must check and loop */
1537 if (s->req->analysers & ~req_ana_back)
1538 goto resync_request;
1539
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001540 /* FIXME: here we should call protocol handlers which rely on
1541 * both buffers.
1542 */
1543
1544
1545 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001546 * Now we propagate unhandled errors to the session. Normally
1547 * we're just in a data phase here since it means we have not
1548 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001549 */
1550 if (!(s->flags & SN_ERR_MASK)) {
1551 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1552 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001553 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001554 if (s->req->flags & BF_READ_ERROR) {
1555 s->be->counters.cli_aborts++;
1556 if (s->srv)
1557 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001558 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001559 }
1560 else if (s->req->flags & BF_READ_TIMEOUT) {
1561 s->be->counters.cli_aborts++;
1562 if (s->srv)
1563 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001564 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001565 }
1566 else if (s->req->flags & BF_WRITE_ERROR) {
1567 s->be->counters.srv_aborts++;
1568 if (s->srv)
1569 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001570 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001571 }
1572 else {
1573 s->be->counters.srv_aborts++;
1574 if (s->srv)
1575 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001576 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001577 }
Willy Tarreau84455332009-03-15 22:34:05 +01001578 sess_set_term_flags(s);
1579 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001580 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1581 /* Report it if the server got an error or a read timeout expired */
1582 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001583 if (s->rep->flags & BF_READ_ERROR) {
1584 s->be->counters.srv_aborts++;
1585 if (s->srv)
1586 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001587 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001588 }
1589 else if (s->rep->flags & BF_READ_TIMEOUT) {
1590 s->be->counters.srv_aborts++;
1591 if (s->srv)
1592 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001593 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001594 }
1595 else if (s->rep->flags & BF_WRITE_ERROR) {
1596 s->be->counters.cli_aborts++;
1597 if (s->srv)
1598 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001599 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001600 }
1601 else {
1602 s->be->counters.cli_aborts++;
1603 if (s->srv)
1604 s->srv->counters.cli_aborts++;
1605 s->flags |= SN_ERR_CLITO;
1606 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001607 sess_set_term_flags(s);
1608 }
Willy Tarreau84455332009-03-15 22:34:05 +01001609 }
1610
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001611 /*
1612 * Here we take care of forwarding unhandled data. This also includes
1613 * connection establishments and shutdown requests.
1614 */
1615
1616
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001617 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001618 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001619 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001620 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001621 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001622 (s->req->prod->state >= SI_ST_EST) &&
1623 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001624 /* This buffer is freewheeling, there's no analyser nor hijacker
1625 * attached to it. If any data are left in, we'll permit them to
1626 * move.
1627 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001628 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001629 buffer_auto_connect(s->req);
1630 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001631 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001632
Willy Tarreau31971e52009-09-20 12:07:52 +02001633 /* If the producer is still connected, we'll enable data to flow
1634 * from the producer to the consumer (which might possibly not be
1635 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001636 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001637 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1638 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001639 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001640
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001641 /* check if it is wise to enable kernel splicing to forward request data */
1642 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1643 s->req->to_forward &&
1644 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001645 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001646 (pipes_used < global.maxpipes) &&
1647 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1648 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1649 (s->req->flags & BF_STREAMER_FAST)))) {
1650 s->req->flags |= BF_KERN_SPLICING;
1651 }
1652
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001653 /* reflect what the L7 analysers have seen last */
1654 rqf_last = s->req->flags;
1655
1656 /*
1657 * Now forward all shutdown requests between both sides of the buffer
1658 */
1659
Willy Tarreau520d95e2009-09-19 21:04:57 +02001660 /* first, let's check if the request buffer needs to shutdown(write), which may
1661 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001662 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001663 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001664 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001665 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001666 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001667
1668 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001669 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 +01001670 s->req->cons->shutw(s->req->cons);
1671
1672 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001673 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1674 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001675 buffer_shutr_now(s->req);
1676
1677 /* shutdown(read) pending */
1678 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1679 s->req->prod->shutr(s->req->prod);
1680
Willy Tarreau520d95e2009-09-19 21:04:57 +02001681 /* it's possible that an upper layer has requested a connection setup or abort.
1682 * There are 2 situations where we decide to establish a new connection :
1683 * - there are data scheduled for emission in the buffer
1684 * - the BF_AUTO_CONNECT flag is set (active connection)
1685 */
1686 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001687 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001688 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001689 /* If we have an iohandler without a connect method, we immediately
1690 * switch to the connected state, otherwise we perform a connection
1691 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001692 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001693 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001694 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001695 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001696 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001697 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1698 s->req->wex = TICK_ETERNITY;
1699 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001700 }
Willy Tarreau73201222009-08-16 18:27:24 +02001701 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001702 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001703 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001704 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1705 buffer_shutr_now(s->rep);
1706 }
Willy Tarreau92795622009-03-06 12:51:23 +01001707 }
1708
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001709
1710 /* we may have a pending connection request, or a connection waiting
1711 * for completion.
1712 */
1713 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1714 do {
1715 /* nb: step 1 might switch from QUE to ASS, but we first want
1716 * to give a chance to step 2 to perform a redirect if needed.
1717 */
1718 if (s->si[1].state != SI_ST_REQ)
1719 sess_update_stream_int(s, &s->si[1]);
1720 if (s->si[1].state == SI_ST_REQ)
1721 sess_prepare_conn_req(s, &s->si[1]);
1722
1723 if (s->si[1].state == SI_ST_ASS && s->srv &&
1724 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1725 perform_http_redirect(s, &s->si[1]);
1726 } while (s->si[1].state == SI_ST_ASS);
1727 }
1728
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001729 /* Benchmarks have shown that it's optimal to do a full resync now */
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 Tarreau815a9b22010-07-27 17:15:12 +02001733 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001734 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001735 goto resync_request;
1736
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001737 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001738
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001739 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001740 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001741 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001742 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001743 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001744 (s->rep->prod->state >= SI_ST_EST) &&
1745 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001746 /* This buffer is freewheeling, there's no analyser nor hijacker
1747 * attached to it. If any data are left in, we'll permit them to
1748 * move.
1749 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001750 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001751 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001752 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001753 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1754 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001755 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001756
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001757 /* check if it is wise to enable kernel splicing to forward response data */
1758 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1759 s->rep->to_forward &&
1760 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001761 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001762 (pipes_used < global.maxpipes) &&
1763 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1764 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1765 (s->rep->flags & BF_STREAMER_FAST)))) {
1766 s->rep->flags |= BF_KERN_SPLICING;
1767 }
1768
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001769 /* reflect what the L7 analysers have seen last */
1770 rpf_last = s->rep->flags;
1771
1772 /*
1773 * Now forward all shutdown requests between both sides of the buffer
1774 */
1775
1776 /*
1777 * FIXME: this is probably where we should produce error responses.
1778 */
1779
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001780 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001781 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1782 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783 buffer_shutw_now(s->rep);
1784
1785 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001786 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 +01001787 s->rep->cons->shutw(s->rep->cons);
1788
1789 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001790 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1791 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001792 buffer_shutr_now(s->rep);
1793
1794 /* shutdown(read) pending */
1795 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1796 s->rep->prod->shutr(s->rep->prod);
1797
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001798 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001799 goto resync_stream_interface;
1800
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001801 if (s->req->flags != rqf_last)
1802 goto resync_request;
1803
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001804 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001805 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001806
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001807 /* we're interested in getting wakeups again */
1808 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1809 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1810
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001811 /* This is needed only when debugging is enabled, to indicate
1812 * client-side or server-side close. Please note that in the unlikely
1813 * event where both sides would close at once, the sequence is reported
1814 * on the server side first.
1815 */
1816 if (unlikely((global.mode & MODE_DEBUG) &&
1817 (!(global.mode & MODE_QUIET) ||
1818 (global.mode & MODE_VERBOSE)))) {
1819 int len;
1820
1821 if (s->si[1].state == SI_ST_CLO &&
1822 s->si[1].prev_state == SI_ST_EST) {
1823 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1824 s->uniq_id, s->be->id,
1825 (unsigned short)s->si[0].fd,
1826 (unsigned short)s->si[1].fd);
1827 write(1, trash, len);
1828 }
1829
1830 if (s->si[0].state == SI_ST_CLO &&
1831 s->si[0].prev_state == SI_ST_EST) {
1832 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1833 s->uniq_id, s->be->id,
1834 (unsigned short)s->si[0].fd,
1835 (unsigned short)s->si[1].fd);
1836 write(1, trash, len);
1837 }
1838 }
1839
1840 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1841 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1842
1843 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1844 session_process_counters(s);
1845
Willy Tarreau1accfc02009-09-05 20:57:35 +02001846 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001847 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001848
Willy Tarreau1accfc02009-09-05 20:57:35 +02001849 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001850 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001851
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001852 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1853 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 +01001854 s->si[0].prev_state = s->si[0].state;
1855 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001856 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1857 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001858
1859 /* Trick: if a request is being waiting for the server to respond,
1860 * and if we know the server can timeout, we don't want the timeout
1861 * to expire on the client side first, but we're still interested
1862 * in passing data from the client to the server (eg: POST). Thus,
1863 * we can cancel the client's request timeout if the server's
1864 * request timeout is set and the server has not yet sent a response.
1865 */
1866
Willy Tarreau520d95e2009-09-19 21:04:57 +02001867 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001868 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1869 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001870 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001871 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001872
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001873 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001874 * Note that this one may wake the task up again.
1875 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001876 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1877 if (s->req->cons->iohandler)
1878 s->req->cons->iohandler(s->req->cons);
1879 if (s->rep->cons->iohandler)
1880 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001881 if (task_in_rq(t)) {
1882 /* If we woke up, we don't want to requeue the
1883 * task to the wait queue, but rather requeue
1884 * it into the runqueue ASAP.
1885 */
1886 t->expire = TICK_ETERNITY;
1887 return t;
1888 }
1889 }
1890
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1892 tick_first(s->rep->rex, s->rep->wex));
1893 if (s->req->analysers)
1894 t->expire = tick_first(t->expire, s->req->analyse_exp);
1895
1896 if (s->si[0].exp)
1897 t->expire = tick_first(t->expire, s->si[0].exp);
1898
1899 if (s->si[1].exp)
1900 t->expire = tick_first(t->expire, s->si[1].exp);
1901
1902#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001903 fprintf(stderr,
1904 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1905 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1906 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1907 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 +01001908#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001909
1910#ifdef DEBUG_DEV
1911 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001912 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001913 ABORT_NOW();
1914#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001915 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001916 }
1917
1918 s->fe->feconn--;
1919 if (s->flags & SN_BE_ASSIGNED)
1920 s->be->beconn--;
1921 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001922 s->listener->nbconn--;
1923 if (s->listener->state == LI_FULL &&
1924 s->listener->nbconn < s->listener->maxconn) {
1925 /* we should reactivate the listener */
1926 EV_FD_SET(s->listener->fd, DIR_RD);
1927 s->listener->state = LI_READY;
1928 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001929
1930 if (unlikely((global.mode & MODE_DEBUG) &&
1931 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1932 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001933 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001935 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001936 write(1, trash, len);
1937 }
1938
1939 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1940 session_process_counters(s);
1941
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001942 if (s->txn.status) {
1943 int n;
1944
1945 n = s->txn.status / 100;
1946 if (n < 1 || n > 5)
1947 n = 0;
1948
1949 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001950 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001951
Willy Tarreau24657792010-02-26 10:30:28 +01001952 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001953 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001954 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001955 }
1956
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001957 /* let's do a final log if we need it */
1958 if (s->logs.logwait &&
1959 !(s->flags & SN_MONITOR) &&
1960 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001961 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001962 }
1963
1964 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001965 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001966 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001967 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001968 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001969}
1970
Willy Tarreau7c669d72008-06-20 15:04:11 +02001971/*
1972 * This function adjusts sess->srv_conn and maintains the previous and new
1973 * server's served session counts. Setting newsrv to NULL is enough to release
1974 * current connection slot. This function also notifies any LB algo which might
1975 * expect to be informed about any change in the number of active sessions on a
1976 * server.
1977 */
1978void sess_change_server(struct session *sess, struct server *newsrv)
1979{
1980 if (sess->srv_conn == newsrv)
1981 return;
1982
1983 if (sess->srv_conn) {
1984 sess->srv_conn->served--;
1985 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1986 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1987 sess->srv_conn = NULL;
1988 }
1989
1990 if (newsrv) {
1991 newsrv->served++;
1992 if (newsrv->proxy->lbprm.server_take_conn)
1993 newsrv->proxy->lbprm.server_take_conn(newsrv);
1994 sess->srv_conn = newsrv;
1995 }
1996}
1997
Willy Tarreau84455332009-03-15 22:34:05 +01001998/* Set correct session termination flags in case no analyser has done it. It
1999 * also counts a failed request if the server state has not reached the request
2000 * stage.
2001 */
2002void sess_set_term_flags(struct session *s)
2003{
2004 if (!(s->flags & SN_FINST_MASK)) {
2005 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002006
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002007 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002008 if (s->listener->counters)
2009 s->listener->counters->failed_req++;
2010
Willy Tarreau84455332009-03-15 22:34:05 +01002011 s->flags |= SN_FINST_R;
2012 }
2013 else if (s->si[1].state == SI_ST_QUE)
2014 s->flags |= SN_FINST_Q;
2015 else if (s->si[1].state < SI_ST_EST)
2016 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002017 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002018 s->flags |= SN_FINST_D;
2019 else
2020 s->flags |= SN_FINST_L;
2021 }
2022}
2023
2024/* Handle server-side errors for default protocols. It is called whenever a a
2025 * connection setup is aborted or a request is aborted in queue. It sets the
2026 * session termination flags so that the caller does not have to worry about
2027 * them. It's installed as ->srv_error for the server-side stream_interface.
2028 */
2029void default_srv_error(struct session *s, struct stream_interface *si)
2030{
2031 int err_type = si->err_type;
2032 int err = 0, fin = 0;
2033
2034 if (err_type & SI_ET_QUEUE_ABRT) {
2035 err = SN_ERR_CLICL;
2036 fin = SN_FINST_Q;
2037 }
2038 else if (err_type & SI_ET_CONN_ABRT) {
2039 err = SN_ERR_CLICL;
2040 fin = SN_FINST_C;
2041 }
2042 else if (err_type & SI_ET_QUEUE_TO) {
2043 err = SN_ERR_SRVTO;
2044 fin = SN_FINST_Q;
2045 }
2046 else if (err_type & SI_ET_QUEUE_ERR) {
2047 err = SN_ERR_SRVCL;
2048 fin = SN_FINST_Q;
2049 }
2050 else if (err_type & SI_ET_CONN_TO) {
2051 err = SN_ERR_SRVTO;
2052 fin = SN_FINST_C;
2053 }
2054 else if (err_type & SI_ET_CONN_ERR) {
2055 err = SN_ERR_SRVCL;
2056 fin = SN_FINST_C;
2057 }
2058 else /* SI_ET_CONN_OTHER and others */ {
2059 err = SN_ERR_INTERNAL;
2060 fin = SN_FINST_C;
2061 }
2062
2063 if (!(s->flags & SN_ERR_MASK))
2064 s->flags |= err;
2065 if (!(s->flags & SN_FINST_MASK))
2066 s->flags |= fin;
2067}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002068
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002069
Willy Tarreau8b22a712010-06-18 17:46:06 +02002070/************************************************************************/
2071/* All supported ACL keywords must be declared here. */
2072/************************************************************************/
2073
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002074/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2075static int
2076acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2077{
2078 test->flags = ACL_TEST_F_VOL_TEST;
2079 test->i = 0;
2080 if (ts != NULL) {
2081 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2082 if (!ptr)
2083 return 0; /* parameter not stored */
2084 test->i = stktable_data_cast(ptr, conn_cnt);
2085 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002086 return 1;
2087}
2088
2089/* set test->i to the cumulated number of connections from the session's tracked counters */
2090static int
2091acl_fetch_trk_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2092 struct acl_expr *expr, struct acl_test *test)
2093{
2094 if (!l4->tracked_counters)
2095 return 0;
2096
2097 return acl_fetch_conn_cnt(l4->tracked_table, test, l4->tracked_counters);
2098}
2099
2100/* set test->i to the cumulated number of connections from the session's source
2101 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002102 */
2103static int
2104acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2105 struct acl_expr *expr, struct acl_test *test)
2106{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002107 struct stktable_key *key;
2108
2109 key = tcpv4_src_to_stktable_key(l4);
2110 if (!key)
2111 return 0; /* only TCPv4 is supported right now */
2112
2113 if (expr->arg_len)
2114 px = find_stktable(expr->arg.str);
2115
2116 if (!px)
2117 return 0; /* table not found */
2118
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002119 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002120}
2121
2122/* set test->i to the number of connections from the session's source address
2123 * in the table pointed to by expr, after updating it.
2124 */
2125static int
2126acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2127 struct acl_expr *expr, struct acl_test *test)
2128{
2129 struct stksess *ts;
2130 struct stktable_key *key;
2131 void *ptr;
2132
2133 key = tcpv4_src_to_stktable_key(l4);
2134 if (!key)
2135 return 0; /* only TCPv4 is supported right now */
2136
2137 if (expr->arg_len)
2138 px = find_stktable(expr->arg.str);
2139
2140 if (!px)
2141 return 0; /* table not found */
2142
2143 if ((ts = stktable_lookup_key(&px->table, key)) == NULL) {
2144 /* entry does not exist, initialize a new one */
2145 ts = stksess_new(&px->table, key);
2146 if (!ts)
2147 return 0;
2148 stktable_store(&px->table, ts);
2149 }
2150 else
2151 stktable_touch(&px->table, ts);
2152
2153 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2154 if (!ptr)
2155 return 0; /* parameter not stored in this table */
2156
2157 test->i = ++stktable_data_cast(ptr, conn_cnt);
2158 test->flags = ACL_TEST_F_VOL_TEST;
2159 return 1;
2160}
2161
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002162/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2163static int
2164acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2165{
2166 test->flags = ACL_TEST_F_VOL_TEST;
2167 test->i = 0;
2168
2169 if (ts != NULL) {
2170 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2171 if (!ptr)
2172 return 0; /* parameter not stored */
2173 test->i = stktable_data_cast(ptr, conn_cur);
2174 }
2175 return 1;
2176}
2177
2178/* set test->i to the number of concurrent connections from the session's tracked counters */
2179static int
2180acl_fetch_trk_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2181 struct acl_expr *expr, struct acl_test *test)
2182{
2183 if (!l4->tracked_counters)
2184 return 0;
2185
2186 return acl_fetch_conn_cur(l4->tracked_table, test, l4->tracked_counters);
2187}
2188
Willy Tarreau38285c12010-06-18 16:35:43 +02002189/* set test->i to the number of concurrent connections from the session's source
2190 * address in the table pointed to by expr.
2191 */
2192static int
2193acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2194 struct acl_expr *expr, struct acl_test *test)
2195{
Willy Tarreau38285c12010-06-18 16:35:43 +02002196 struct stktable_key *key;
2197
2198 key = tcpv4_src_to_stktable_key(l4);
2199 if (!key)
2200 return 0; /* only TCPv4 is supported right now */
2201
2202 if (expr->arg_len)
2203 px = find_stktable(expr->arg.str);
2204
2205 if (!px)
2206 return 0; /* table not found */
2207
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002208 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002209}
2210
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002211/* set test->i to the number of kbytes received from the session's source
2212 * address in the table pointed to by expr.
2213 */
2214static int
2215acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2216 struct acl_expr *expr, struct acl_test *test)
2217{
2218 struct stksess *ts;
2219 struct stktable_key *key;
2220
2221 key = tcpv4_src_to_stktable_key(l4);
2222 if (!key)
2223 return 0; /* only TCPv4 is supported right now */
2224
2225 if (expr->arg_len)
2226 px = find_stktable(expr->arg.str);
2227
2228 if (!px)
2229 return 0; /* table not found */
2230
2231 test->flags = ACL_TEST_F_VOL_TEST;
2232 test->i = 0;
2233
2234 if ((ts = stktable_lookup_key(&px->table, key)) != NULL) {
2235 void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_BYTES_IN_CNT);
2236 if (!ptr)
2237 return 0; /* parameter not stored */
2238 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2239 }
2240
2241 return 1;
2242}
2243
2244/* set test->i to the number of kbytes sent to the session's source address in
2245 * the table pointed to by expr.
2246 */
2247static int
2248acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
2249 struct acl_expr *expr, struct acl_test *test)
2250{
2251 struct stksess *ts;
2252 struct stktable_key *key;
2253
2254 key = tcpv4_src_to_stktable_key(l4);
2255 if (!key)
2256 return 0; /* only TCPv4 is supported right now */
2257
2258 if (expr->arg_len)
2259 px = find_stktable(expr->arg.str);
2260
2261 if (!px)
2262 return 0; /* table not found */
2263
2264 test->flags = ACL_TEST_F_VOL_TEST;
2265 test->i = 0;
2266
2267 if ((ts = stktable_lookup_key(&px->table, key)) != NULL) {
2268 void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
2269 if (!ptr)
2270 return 0; /* parameter not stored */
2271 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
2272 }
2273
2274 return 1;
2275}
2276
Willy Tarreau8b22a712010-06-18 17:46:06 +02002277
2278/* Note: must not be declared <const> as its list will be overwritten */
2279static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002280 { "trk_conn_cnt", acl_parse_int, acl_fetch_trk_conn_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002281 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
2282 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002283 { "trk_conn_cur", acl_parse_int, acl_fetch_trk_conn_cur, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau38285c12010-06-18 16:35:43 +02002284 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002285 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
2286 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002287 { NULL, NULL, NULL, NULL },
2288}};
2289
2290
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002291/* Parse a "track-counters" line starting with "track-counters" in args[arg-1].
2292 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
2293 * <prm> struct is fed with the table name if any. If unspecified, the caller
2294 * will assume that the current proxy's table is used.
2295 */
2296int parse_track_counters(char **args, int *arg,
2297 int section_type, struct proxy *curpx,
2298 struct track_ctr_prm *prm,
2299 struct proxy *defpx, char *err, int errlen)
2300{
2301 int pattern_type = 0;
2302
2303 /* parse the arguments of "track-counters" before the condition in the
2304 * following form :
2305 * track-counters src [ table xxx ] [ if|unless ... ]
2306 */
2307 while (args[*arg]) {
2308 if (strcmp(args[*arg], "src") == 0) {
2309 prm->type = STKTABLE_TYPE_IP;
2310 pattern_type = 1;
2311 }
2312 else if (strcmp(args[*arg], "table") == 0) {
2313 if (!args[*arg + 1]) {
2314 snprintf(err, errlen,
2315 "missing table for track-counter in %s '%s'.",
2316 proxy_type_str(curpx), curpx->id);
2317 return -1;
2318 }
2319 /* we copy the table name for now, it will be resolved later */
2320 prm->table.n = strdup(args[*arg + 1]);
2321 (*arg)++;
2322 }
2323 else {
2324 /* unhandled keywords are handled by the caller */
2325 break;
2326 }
2327 (*arg)++;
2328 }
2329
2330 if (!pattern_type) {
2331 snprintf(err, errlen,
2332 "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).",
2333 proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
2334 return -1;
2335 }
2336
2337 return 0;
2338}
2339
Willy Tarreau8b22a712010-06-18 17:46:06 +02002340__attribute__((constructor))
2341static void __session_init(void)
2342{
2343 acl_register_keywords(&acl_kws);
2344}
2345
Willy Tarreaubaaee002006-06-26 02:48:02 +02002346/*
2347 * Local variables:
2348 * c-indent-level: 8
2349 * c-basic-offset: 8
2350 * End:
2351 */