blob: 9f3ccec0579fea59e9bd82e92ae1bb41c4f882ed [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 Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
50 * value upon success, 0 if the connection needs to be closed and ignored, or a
51 * negative value upon critical failure.
52 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
59
60 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
61 Alert("out of memory in event_accept().\n");
62 goto out_close;
63 }
64
65 /* minimum session initialization required for monitor mode below */
66 s->flags = 0;
67 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020068 s->stkctr1_entry = NULL;
69 s->stkctr2_entry = NULL;
70 s->stkctr1_table = NULL;
71 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020072
73 /* if this session comes from a known monitoring system, we want to ignore
74 * it as soon as possible, which means closing it immediately for TCP, but
75 * cleanly.
76 */
77 if (unlikely((l->options & LI_O_CHK_MONNET) &&
78 addr->ss_family == AF_INET &&
79 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
80 if (p->mode == PR_MODE_TCP) {
81 pool_free2(pool2_session, s);
82 return 0;
83 }
84 s->flags |= SN_MONITOR;
85 s->logs.logwait = 0;
86 }
87
88 /* OK, we're keeping the session, so let's properly initialize the session */
89 LIST_ADDQ(&sessions, &s->list);
90 LIST_INIT(&s->back_refs);
91
92 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
93 Alert("out of memory in event_accept().\n");
94 goto out_free_session;
95 }
96
97 s->term_trace = 0;
98 s->cli_addr = *addr;
99 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau56123282010-08-06 19:06:56 +0200126 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200127 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200128 task_free(t);
129 LIST_DEL(&s->list);
130 pool_free2(pool2_session, s);
131 /* let's do a no-linger now to close with a single RST. */
132 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200133 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 return 0;
135 }
136
Willy Tarreaub36b4242010-06-04 20:59:39 +0200137 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200138 if (p->feconn > p->counters.feconn_max)
139 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200140 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200141 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200142 void *ptr;
143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200145 if (ptr)
146 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200147
Willy Tarreau56123282010-08-06 19:06:56 +0200148 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200149 if (ptr)
150 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200151 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200152 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200153
Willy Tarreau56123282010-08-06 19:06:56 +0200154 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200155 void *ptr;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 stktable_data_cast(ptr, sess_cnt)++;
160
Willy Tarreau56123282010-08-06 19:06:56 +0200161 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200162 if (ptr)
163 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200164 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200165 }
166
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 /* this part should be common with other protocols */
168 s->si[0].fd = cfd;
169 s->si[0].owner = t;
170 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
171 s->si[0].err_type = SI_ET_NONE;
172 s->si[0].err_loc = NULL;
173 s->si[0].connect = NULL;
174 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200175 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200176 s->si[0].exp = TICK_ETERNITY;
177 s->si[0].flags = SI_FL_NONE;
178
179 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
180 s->si[0].flags |= SI_FL_INDEP_STR;
181
182 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
183 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
184
185 /* add the various callbacks */
186 stream_sock_prepare_interface(&s->si[0]);
187
188 /* pre-initialize the other side's stream interface to an INIT state. The
189 * callbacks will be initialized before attempting to connect.
190 */
191 s->si[1].fd = -1; /* just to help with debugging */
192 s->si[1].owner = t;
193 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
194 s->si[1].err_type = SI_ET_NONE;
195 s->si[1].err_loc = NULL;
196 s->si[1].connect = NULL;
197 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200198 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200199 s->si[1].shutr = stream_int_shutr;
200 s->si[1].shutw = stream_int_shutw;
201 s->si[1].exp = TICK_ETERNITY;
202 s->si[1].flags = SI_FL_NONE;
203
204 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
205 s->si[1].flags |= SI_FL_INDEP_STR;
206
207 s->srv = s->prev_srv = s->srv_conn = NULL;
208 s->pend_pos = NULL;
209
210 /* init store persistence */
211 s->store_count = 0;
212
213 /* Adjust some socket options */
214 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
215 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
216 goto out_free_task;
217 }
218
219 txn = &s->txn;
220 /* Those variables will be checked and freed if non-NULL in
221 * session.c:session_free(). It is important that they are
222 * properly initialized.
223 */
224 txn->sessid = NULL;
225 txn->srv_cookie = NULL;
226 txn->cli_cookie = NULL;
227 txn->uri = NULL;
228 txn->req.cap = NULL;
229 txn->rsp.cap = NULL;
230 txn->hdr_idx.v = NULL;
231 txn->hdr_idx.size = txn->hdr_idx.used = 0;
232
233 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
234 goto out_free_task; /* no memory */
235
236 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
237 goto out_free_req; /* no memory */
238
239 /* initialize the request buffer */
240 s->req->size = global.tune.bufsize;
241 buffer_init(s->req);
242 s->req->prod = &s->si[0];
243 s->req->cons = &s->si[1];
244 s->si[0].ib = s->si[1].ob = s->req;
245 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
246
247 /* activate default analysers enabled for this listener */
248 s->req->analysers = l->analysers;
249
250 s->req->wto = TICK_ETERNITY;
251 s->req->rto = TICK_ETERNITY;
252 s->req->rex = TICK_ETERNITY;
253 s->req->wex = TICK_ETERNITY;
254 s->req->analyse_exp = TICK_ETERNITY;
255
256 /* initialize response buffer */
257 s->rep->size = global.tune.bufsize;
258 buffer_init(s->rep);
259 s->rep->prod = &s->si[1];
260 s->rep->cons = &s->si[0];
261 s->si[0].ob = s->si[1].ib = s->rep;
262 s->rep->analysers = 0;
263
264 s->rep->rto = TICK_ETERNITY;
265 s->rep->wto = TICK_ETERNITY;
266 s->rep->rex = TICK_ETERNITY;
267 s->rep->wex = TICK_ETERNITY;
268 s->rep->analyse_exp = TICK_ETERNITY;
269
270 /* finish initialization of the accepted file descriptor */
271 fd_insert(cfd);
272 fdtab[cfd].owner = &s->si[0];
273 fdtab[cfd].state = FD_STREADY;
274 fdtab[cfd].flags = 0;
275 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
276 fdtab[cfd].cb[DIR_RD].b = s->req;
277 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
278 fdtab[cfd].cb[DIR_WR].b = s->rep;
279 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
280 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
281 EV_FD_SET(cfd, DIR_RD);
282
283 if (p->accept) {
284 int ret = p->accept(s);
285 if (unlikely(ret < 0))
286 goto out_free_rep;
287
288 if (unlikely(ret == 0)) {
289 /* work is finished, we can release everything (eg: monitoring) */
290 pool_free2(pool2_buffer, s->rep);
291 pool_free2(pool2_buffer, s->req);
Willy Tarreau56123282010-08-06 19:06:56 +0200292 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200293 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200294 task_free(t);
295 LIST_DEL(&s->list);
296 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200297 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200298 return 0;
299 }
300 }
301
302 /* it is important not to call the wakeup function directly but to
303 * pass through task_wakeup(), because this one knows how to apply
304 * priorities to tasks.
305 */
306 task_wakeup(t, TASK_WOKEN_INIT);
307 return 1;
308
309 /* Error unrolling */
310 out_free_rep:
311 pool_free2(pool2_buffer, s->rep);
312 out_free_req:
313 pool_free2(pool2_buffer, s->req);
314 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200315 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200316 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200317 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200318 task_free(t);
319 out_free_session:
320 LIST_DEL(&s->list);
321 pool_free2(pool2_session, s);
322 out_close:
323 return -1;
324}
325
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326/*
327 * frees the context associated to a session. It must have been removed first.
328 */
329void session_free(struct session *s)
330{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100331 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200332 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100333 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200334 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100335
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336 if (s->pend_pos)
337 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100338
Willy Tarreau1e62de62008-11-11 20:20:02 +0100339 if (s->srv) { /* there may be requests left pending in queue */
340 if (s->flags & SN_CURR_SESS) {
341 s->flags &= ~SN_CURR_SESS;
342 s->srv->cur_sess--;
343 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100344 if (may_dequeue_tasks(s->srv, s->be))
345 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100346 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100347
Willy Tarreau7c669d72008-06-20 15:04:11 +0200348 if (unlikely(s->srv_conn)) {
349 /* the session still has a reserved slot on a server, but
350 * it should normally be only the same as the one above,
351 * so this should not happen in fact.
352 */
353 sess_change_server(s, NULL);
354 }
355
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100356 if (s->req->pipe)
357 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100358
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100359 if (s->rep->pipe)
360 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100361
Willy Tarreau48d63db2008-08-03 17:41:33 +0200362 pool_free2(pool2_buffer, s->req);
363 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
Willy Tarreau46023632010-01-07 22:51:47 +0100365 http_end_txn(s);
366
Willy Tarreaua4cda672010-06-06 18:28:49 +0200367 for (i = 0; i < s->store_count; i++) {
368 if (!s->store[i].ts)
369 continue;
370 stksess_free(s->store[i].table, s->store[i].ts);
371 s->store[i].ts = NULL;
372 }
373
Willy Tarreau92fb9832007-10-16 17:34:28 +0200374 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200375 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100376 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
377 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100379
Willy Tarreau56123282010-08-06 19:06:56 +0200380 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200381 session_store_counters(s);
382
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100383 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100384 /* we have to unlink all watchers. We must not relink them if
385 * this session was the last one in the list.
386 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100387 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100388 LIST_INIT(&bref->users);
389 if (s->list.n != &sessions)
390 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100391 bref->ref = s->list.n;
392 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100393 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200394 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200395
396 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200397 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200398 pool_flush2(pool2_buffer);
399 pool_flush2(fe->hdr_idx_pool);
400 pool_flush2(pool2_requri);
401 pool_flush2(pool2_capture);
402 pool_flush2(pool2_session);
403 pool_flush2(fe->req_cap_pool);
404 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200405 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200406}
407
408
409/* perform minimal intializations, report 0 in case of error, 1 if OK. */
410int init_session()
411{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100412 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200413 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
414 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415}
416
Willy Tarreau30e71012007-11-26 20:15:35 +0100417void session_process_counters(struct session *s)
418{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100419 unsigned long long bytes;
420
Willy Tarreau30e71012007-11-26 20:15:35 +0100421 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100423 s->logs.bytes_in = s->req->total;
424 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200425 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100426
Willy Tarreau30e71012007-11-26 20:15:35 +0100427 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200428 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100429
Willy Tarreau30e71012007-11-26 20:15:35 +0100430 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200431 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200432
433 if (s->listener->counters)
434 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435
Willy Tarreau56123282010-08-06 19:06:56 +0200436 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200437 void *ptr;
438
Willy Tarreau56123282010-08-06 19:06:56 +0200439 ptr = stktable_data_ptr(s->stkctr2_table,
440 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200441 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200442 if (ptr)
443 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200444
Willy Tarreau56123282010-08-06 19:06:56 +0200445 ptr = stktable_data_ptr(s->stkctr2_table,
446 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200447 STKTABLE_DT_BYTES_IN_RATE);
448 if (ptr)
449 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200450 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 }
452
Willy Tarreau56123282010-08-06 19:06:56 +0200453 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200454 void *ptr;
455
Willy Tarreau56123282010-08-06 19:06:56 +0200456 ptr = stktable_data_ptr(s->stkctr1_table,
457 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200458 STKTABLE_DT_BYTES_IN_CNT);
459 if (ptr)
460 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
461
Willy Tarreau56123282010-08-06 19:06:56 +0200462 ptr = stktable_data_ptr(s->stkctr1_table,
463 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200464 STKTABLE_DT_BYTES_IN_RATE);
465 if (ptr)
466 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200467 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200468 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100469 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470 }
471
Willy Tarreau30e71012007-11-26 20:15:35 +0100472 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100473 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100474 s->logs.bytes_out = s->rep->total;
475 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200476 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau30e71012007-11-26 20:15:35 +0100478 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200479 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100480
Willy Tarreau30e71012007-11-26 20:15:35 +0100481 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200482 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200483
484 if (s->listener->counters)
485 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200486
Willy Tarreau56123282010-08-06 19:06:56 +0200487 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200488 void *ptr;
489
Willy Tarreau56123282010-08-06 19:06:56 +0200490 ptr = stktable_data_ptr(s->stkctr2_table,
491 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200492 STKTABLE_DT_BYTES_OUT_CNT);
493 if (ptr)
494 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
495
Willy Tarreau56123282010-08-06 19:06:56 +0200496 ptr = stktable_data_ptr(s->stkctr2_table,
497 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200498 STKTABLE_DT_BYTES_OUT_RATE);
499 if (ptr)
500 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200501 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200502 }
503
Willy Tarreau56123282010-08-06 19:06:56 +0200504 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200505 void *ptr;
506
Willy Tarreau56123282010-08-06 19:06:56 +0200507 ptr = stktable_data_ptr(s->stkctr1_table,
508 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200509 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200510 if (ptr)
511 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200512
Willy Tarreau56123282010-08-06 19:06:56 +0200513 ptr = stktable_data_ptr(s->stkctr1_table,
514 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515 STKTABLE_DT_BYTES_OUT_RATE);
516 if (ptr)
517 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200518 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200519 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100520 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100521 }
522}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524/* This function is called with (si->state == SI_ST_CON) meaning that a
525 * connection was attempted and that the file descriptor is already allocated.
526 * We must check for establishment, error and abort. Possible output states
527 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
528 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
529 * otherwise 1.
530 */
531int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
532{
533 struct buffer *req = si->ob;
534 struct buffer *rep = si->ib;
535
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 /* If we got an error, or if nothing happened and the connection timed
537 * out, we must give up. The CER state handler will take care of retry
538 * attempts and error reports.
539 */
540 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100541 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200543 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 fd_delete(si->fd);
545
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200546 if (si->release)
547 si->release(si);
548
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100549 if (si->err_type)
550 return 0;
551
552 si->err_loc = s->srv;
553 if (si->flags & SI_FL_ERR)
554 si->err_type = SI_ET_CONN_ERR;
555 else
556 si->err_type = SI_ET_CONN_TO;
557 return 0;
558 }
559
560 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200561 if (unlikely((rep->flags & BF_SHUTW) ||
562 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200563 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 s->be->options & PR_O_ABRT_CLOSE)))) {
565 /* give up */
566 si->shutw(si);
567 si->err_type |= SI_ET_CONN_ABRT;
568 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200569 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100570 if (s->srv_error)
571 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 return 1;
573 }
574
575 /* we need to wait a bit more if there was no activity either */
576 if (!(req->flags & BF_WRITE_ACTIVITY))
577 return 1;
578
579 /* OK, this means that a connection succeeded. The caller will be
580 * responsible for handling the transition from CON to EST.
581 */
582 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100583 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 si->state = SI_ST_EST;
585 si->err_type = SI_ET_NONE;
586 si->err_loc = NULL;
587 return 1;
588}
589
590/* This function is called with (si->state == SI_ST_CER) meaning that a
591 * previous connection attempt has failed and that the file descriptor
592 * has already been released. Possible causes include asynchronous error
593 * notification and time out. Possible output states are SI_ST_CLO when
594 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
595 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
596 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
597 * marked as in error state. It returns 0.
598 */
599int sess_update_st_cer(struct session *s, struct stream_interface *si)
600{
601 /* we probably have to release last session from the server */
602 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100603 health_adjust(s->srv, HANA_STATUS_L4_ERR);
604
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 if (s->flags & SN_CURR_SESS) {
606 s->flags &= ~SN_CURR_SESS;
607 s->srv->cur_sess--;
608 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 }
610
611 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200612 si->conn_retries--;
613 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 if (!si->err_type) {
615 si->err_type = SI_ET_CONN_ERR;
616 si->err_loc = s->srv;
617 }
618
619 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200620 s->srv->counters.failed_conns++;
621 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 if (may_dequeue_tasks(s->srv, s->be))
623 process_srv_queue(s->srv);
624
625 /* shutw is enough so stop a connecting socket */
626 si->shutw(si);
627 si->ob->flags |= BF_WRITE_ERROR;
628 si->ib->flags |= BF_READ_ERROR;
629
630 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100631 if (s->srv_error)
632 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 return 0;
634 }
635
636 /* If the "redispatch" option is set on the backend, we are allowed to
637 * retry on another server for the last retry. In order to achieve this,
638 * we must mark the session unassigned, and eventually clear the DIRECT
639 * bit to ignore any persistence cookie. We won't count a retry nor a
640 * redispatch yet, because this will depend on what server is selected.
641 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200642 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100643 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 if (may_dequeue_tasks(s->srv, s->be))
645 process_srv_queue(s->srv);
646
647 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
648 s->prev_srv = s->srv;
649 si->state = SI_ST_REQ;
650 } else {
651 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200652 s->srv->counters.retries++;
653 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 si->state = SI_ST_ASS;
655 }
656
657 if (si->flags & SI_FL_ERR) {
658 /* The error was an asynchronous connection error, and we will
659 * likely have to retry connecting to the same server, most
660 * likely leading to the same result. To avoid this, we wait
661 * one second before retrying.
662 */
663
664 if (!si->err_type)
665 si->err_type = SI_ET_CONN_ERR;
666
667 si->state = SI_ST_TAR;
668 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
669 return 0;
670 }
671 return 0;
672}
673
674/*
675 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200676 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
677 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 */
679void sess_establish(struct session *s, struct stream_interface *si)
680{
681 struct buffer *req = si->ob;
682 struct buffer *rep = si->ib;
683
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100684 if (s->srv)
685 health_adjust(s->srv, HANA_STATUS_L4_OK);
686
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688 /* if the user wants to log as soon as possible, without counting
689 * bytes from the server, then this is the right moment. */
690 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
691 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100692 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 }
695 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
697 /* reset hdr_idx which was already initialized by the request.
698 * right now, the http parser does it.
699 * hdr_idx_init(&s->txn.hdr_idx);
700 */
701 }
702
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200703 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200705 if (si->connect) {
706 /* real connections have timeouts */
707 req->wto = s->be->timeout.server;
708 rep->rto = s->be->timeout.server;
709 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 req->wex = TICK_ETERNITY;
711}
712
713/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
714 * Other input states are simply ignored.
715 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
716 * Flags must have previously been updated for timeouts and other conditions.
717 */
718void sess_update_stream_int(struct session *s, struct stream_interface *si)
719{
720 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",
721 now_ms, __FUNCTION__,
722 s,
723 s->req, s->rep,
724 s->req->rex, s->rep->wex,
725 s->req->flags, s->rep->flags,
726 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
727
728 if (si->state == SI_ST_ASS) {
729 /* Server assigned to connection request, we have to try to connect now */
730 int conn_err;
731
732 conn_err = connect_server(s);
733 if (conn_err == SN_ERR_NONE) {
734 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100735 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100736 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 return;
738 }
739
740 /* We have received a synchronous error. We might have to
741 * abort, retry immediately or redispatch.
742 */
743 if (conn_err == SN_ERR_INTERNAL) {
744 if (!si->err_type) {
745 si->err_type = SI_ET_CONN_OTHER;
746 si->err_loc = s->srv;
747 }
748
749 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100750 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200752 s->srv->counters.failed_conns++;
753 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754
755 /* release other sessions waiting for this server */
756 if (may_dequeue_tasks(s->srv, s->be))
757 process_srv_queue(s->srv);
758
759 /* Failed and not retryable. */
760 si->shutr(si);
761 si->shutw(si);
762 si->ob->flags |= BF_WRITE_ERROR;
763
764 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
765
766 /* no session was ever accounted for this server */
767 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100768 if (s->srv_error)
769 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 return;
771 }
772
773 /* We are facing a retryable error, but we don't want to run a
774 * turn-around now, as the problem is likely a source port
775 * allocation problem, so we want to retry now.
776 */
777 si->state = SI_ST_CER;
778 si->flags &= ~SI_FL_ERR;
779 sess_update_st_cer(s, si);
780 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
781 return;
782 }
783 else if (si->state == SI_ST_QUE) {
784 /* connection request was queued, check for any update */
785 if (!s->pend_pos) {
786 /* The connection is not in the queue anymore. Either
787 * we have a server connection slot available and we
788 * go directly to the assigned state, or we need to
789 * load-balance first and go to the INI state.
790 */
791 si->exp = TICK_ETERNITY;
792 if (unlikely(!(s->flags & SN_ASSIGNED)))
793 si->state = SI_ST_REQ;
794 else {
795 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
796 si->state = SI_ST_ASS;
797 }
798 return;
799 }
800
801 /* Connection request still in queue... */
802 if (si->flags & SI_FL_EXP) {
803 /* ... and timeout expired */
804 si->exp = TICK_ETERNITY;
805 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
806 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200807 s->srv->counters.failed_conns++;
808 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809 si->shutr(si);
810 si->shutw(si);
811 si->ob->flags |= BF_WRITE_TIMEOUT;
812 if (!si->err_type)
813 si->err_type = SI_ET_QUEUE_TO;
814 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100815 if (s->srv_error)
816 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 return;
818 }
819
820 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200821 if ((si->ob->flags & (BF_READ_ERROR)) ||
822 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200823 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 /* give up */
825 si->exp = TICK_ETERNITY;
826 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
827 si->shutr(si);
828 si->shutw(si);
829 si->err_type |= SI_ET_QUEUE_ABRT;
830 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100831 if (s->srv_error)
832 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833 return;
834 }
835
836 /* Nothing changed */
837 return;
838 }
839 else if (si->state == SI_ST_TAR) {
840 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200841 if ((si->ob->flags & (BF_READ_ERROR)) ||
842 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200843 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 /* give up */
845 si->exp = TICK_ETERNITY;
846 si->shutr(si);
847 si->shutw(si);
848 si->err_type |= SI_ET_CONN_ABRT;
849 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100850 if (s->srv_error)
851 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852 return;
853 }
854
855 if (!(si->flags & SI_FL_EXP))
856 return; /* still in turn-around */
857
858 si->exp = TICK_ETERNITY;
859
860 /* we keep trying on the same server as long as the session is
861 * marked "assigned".
862 * FIXME: Should we force a redispatch attempt when the server is down ?
863 */
864 if (s->flags & SN_ASSIGNED)
865 si->state = SI_ST_ASS;
866 else
867 si->state = SI_ST_REQ;
868 return;
869 }
870}
871
872/* This function initiates a server connection request on a stream interface
873 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
874 * indicating that a server has been assigned. It may also return SI_ST_QUE,
875 * or SI_ST_CLO upon error.
876 */
877static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
878 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",
879 now_ms, __FUNCTION__,
880 s,
881 s->req, s->rep,
882 s->req->rex, s->rep->wex,
883 s->req->flags, s->rep->flags,
884 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
885
886 if (si->state != SI_ST_REQ)
887 return;
888
889 /* Try to assign a server */
890 if (srv_redispatch_connect(s) != 0) {
891 /* We did not get a server. Either we queued the
892 * connection request, or we encountered an error.
893 */
894 if (si->state == SI_ST_QUE)
895 return;
896
897 /* we did not get any server, let's check the cause */
898 si->shutr(si);
899 si->shutw(si);
900 si->ob->flags |= BF_WRITE_ERROR;
901 if (!si->err_type)
902 si->err_type = SI_ET_CONN_OTHER;
903 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100904 if (s->srv_error)
905 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906 return;
907 }
908
909 /* The server is assigned */
910 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
911 si->state = SI_ST_ASS;
912}
913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200914/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100915 * if appropriate. The default_backend rule is also considered, then the
916 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200917 * It returns 1 if the processing can continue on next analysers, or zero if it
918 * either needs more data or wants to immediately abort the request.
919 */
920int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
921{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200922 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100923
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200924 req->analysers &= ~an_bit;
925 req->analyse_exp = TICK_ETERNITY;
926
927 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
928 now_ms, __FUNCTION__,
929 s,
930 req,
931 req->rex, req->wex,
932 req->flags,
933 req->l,
934 req->analysers);
935
936 /* now check whether we have some switching rules for this request */
937 if (!(s->flags & SN_BE_ASSIGNED)) {
938 struct switching_rule *rule;
939
940 list_for_each_entry(rule, &s->fe->switching_rules, list) {
941 int ret;
942
943 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
944 ret = acl_pass(ret);
945 if (rule->cond->pol == ACL_COND_UNLESS)
946 ret = !ret;
947
948 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200949 if (!session_set_backend(s, rule->be.backend))
950 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200951 break;
952 }
953 }
954
955 /* To ensure correct connection accounting on the backend, we
956 * have to assign one if it was not set (eg: a listen). This
957 * measure also takes care of correctly setting the default
958 * backend if any.
959 */
960 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200961 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
962 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200963 }
964
Willy Tarreaufb356202010-08-03 14:02:05 +0200965 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
966 if (s->fe == s->be) {
967 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200968 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200969 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200970
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200971 /* 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 +0100972 * persistence rule, and report that in the session.
973 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200974 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100975 int ret = 1;
976
977 if (prst_rule->cond) {
978 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
979 ret = acl_pass(ret);
980 if (prst_rule->cond->pol == ACL_COND_UNLESS)
981 ret = !ret;
982 }
983
984 if (ret) {
985 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200986 if (prst_rule->type == PERSIST_TYPE_FORCE) {
987 s->flags |= SN_FORCE_PRST;
988 } else {
989 s->flags |= SN_IGNORE_PRST;
990 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100991 break;
992 }
993 }
994
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200995 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200996
997 sw_failed:
998 /* immediately abort this request in case of allocation failure */
999 buffer_abort(s->req);
1000 buffer_abort(s->rep);
1001
1002 if (!(s->flags & SN_ERR_MASK))
1003 s->flags |= SN_ERR_RESOURCE;
1004 if (!(s->flags & SN_FINST_MASK))
1005 s->flags |= SN_FINST_R;
1006
1007 s->txn.status = 500;
1008 s->req->analysers = 0;
1009 s->req->analyse_exp = TICK_ETERNITY;
1010 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001011}
1012
Emeric Brun1d33b292010-01-04 15:47:17 +01001013/* This stream analyser works on a request. It applies all sticking rules on
1014 * it then returns 1. The data must already be present in the buffer otherwise
1015 * they won't match. It always returns 1.
1016 */
1017int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1018{
1019 struct proxy *px = s->be;
1020 struct sticking_rule *rule;
1021
1022 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1023 now_ms, __FUNCTION__,
1024 s,
1025 req,
1026 req->rex, req->wex,
1027 req->flags,
1028 req->l,
1029 req->analysers);
1030
1031 list_for_each_entry(rule, &px->sticking_rules, list) {
1032 int ret = 1 ;
1033 int i;
1034
1035 for (i = 0; i < s->store_count; i++) {
1036 if (rule->table.t == s->store[i].table)
1037 break;
1038 }
1039
1040 if (i != s->store_count)
1041 continue;
1042
1043 if (rule->cond) {
1044 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1045 ret = acl_pass(ret);
1046 if (rule->cond->pol == ACL_COND_UNLESS)
1047 ret = !ret;
1048 }
1049
1050 if (ret) {
1051 struct stktable_key *key;
1052
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001053 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001054 if (!key)
1055 continue;
1056
1057 if (rule->flags & STK_IS_MATCH) {
1058 struct stksess *ts;
1059
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001060 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001061 if (!(s->flags & SN_ASSIGNED)) {
1062 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001063 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001064
1065 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001066 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1067 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001068 if (node) {
1069 struct server *srv;
1070
1071 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001072 if ((srv->state & SRV_RUNNING) ||
1073 (px->options & PR_O_PERSIST) ||
1074 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001075 s->flags |= SN_DIRECT | SN_ASSIGNED;
1076 s->srv = srv;
1077 }
1078 }
1079 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001080 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001081 }
1082 }
1083 if (rule->flags & STK_IS_STORE) {
1084 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1085 struct stksess *ts;
1086
1087 ts = stksess_new(rule->table.t, key);
1088 if (ts) {
1089 s->store[s->store_count].table = rule->table.t;
1090 s->store[s->store_count++].ts = ts;
1091 }
1092 }
1093 }
1094 }
1095 }
1096
1097 req->analysers &= ~an_bit;
1098 req->analyse_exp = TICK_ETERNITY;
1099 return 1;
1100}
1101
1102/* This stream analyser works on a response. It applies all store rules on it
1103 * then returns 1. The data must already be present in the buffer otherwise
1104 * they won't match. It always returns 1.
1105 */
1106int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1107{
1108 struct proxy *px = s->be;
1109 struct sticking_rule *rule;
1110 int i;
1111
1112 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1113 now_ms, __FUNCTION__,
1114 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001115 rep,
1116 rep->rex, rep->wex,
1117 rep->flags,
1118 rep->l,
1119 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001120
1121 list_for_each_entry(rule, &px->storersp_rules, list) {
1122 int ret = 1 ;
1123 int storereqidx = -1;
1124
1125 for (i = 0; i < s->store_count; i++) {
1126 if (rule->table.t == s->store[i].table) {
1127 if (!(s->store[i].flags))
1128 storereqidx = i;
1129 break;
1130 }
1131 }
1132
1133 if ((i != s->store_count) && (storereqidx == -1))
1134 continue;
1135
1136 if (rule->cond) {
1137 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1138 ret = acl_pass(ret);
1139 if (rule->cond->pol == ACL_COND_UNLESS)
1140 ret = !ret;
1141 }
1142
1143 if (ret) {
1144 struct stktable_key *key;
1145
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001146 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001147 if (!key)
1148 continue;
1149
1150 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001151 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001152 s->store[storereqidx].flags = 1;
1153 }
1154 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1155 struct stksess *ts;
1156
1157 ts = stksess_new(rule->table.t, key);
1158 if (ts) {
1159 s->store[s->store_count].table = rule->table.t;
1160 s->store[s->store_count].flags = 1;
1161 s->store[s->store_count++].ts = ts;
1162 }
1163 }
1164 }
1165 }
1166
1167 /* process store request and store response */
1168 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001169 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001170 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001171
1172 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1173 if (ts) {
1174 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001175 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001177 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001178 else
1179 ts = stktable_store(s->store[i].table, s->store[i].ts);
1180
1181 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001182 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1183 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001184 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001185 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001186
1187 rep->analysers &= ~an_bit;
1188 rep->analyse_exp = TICK_ETERNITY;
1189 return 1;
1190}
1191
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001192/* This macro is very specific to the function below. See the comments in
1193 * process_session() below to understand the logic and the tests.
1194 */
1195#define UPDATE_ANALYSERS(real, list, back, flag) { \
1196 list = (((list) & ~(flag)) | ~(back)) & (real); \
1197 back = real; \
1198 if (!(list)) \
1199 break; \
1200 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1201 continue; \
1202}
1203
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001204/* Processes the client, server, request and response jobs of a session task,
1205 * then puts it back to the wait queue in a clean state, or cleans up its
1206 * resources if it must be deleted. Returns in <next> the date the task wants
1207 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1208 * nothing too many times, the request and response buffers flags are monitored
1209 * and each function is called only if at least another function has changed at
1210 * least one flag it is interested in.
1211 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001212struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213{
1214 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001215 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001216 unsigned int rq_prod_last, rq_cons_last;
1217 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001218 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001219
1220 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1221 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1222
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001223 /* this data may be no longer valid, clear it */
1224 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1225
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001226 /* This flag must explicitly be set every time */
1227 s->req->flags &= ~BF_READ_NOEXP;
1228
1229 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1230 rqf_last = s->req->flags;
1231 rpf_last = s->rep->flags;
1232
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001233 /* we don't want the stream interface functions to recursively wake us up */
1234 if (s->req->prod->owner == t)
1235 s->req->prod->flags |= SI_FL_DONT_WAKE;
1236 if (s->req->cons->owner == t)
1237 s->req->cons->flags |= SI_FL_DONT_WAKE;
1238
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001239 /* 1a: Check for low level timeouts if needed. We just set a flag on
1240 * stream interfaces when their timeouts have expired.
1241 */
1242 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1243 stream_int_check_timeouts(&s->si[0]);
1244 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001245
1246 /* check buffer timeouts, and close the corresponding stream interfaces
1247 * for future reads or writes. Note: this will also concern upper layers
1248 * but we do not touch any other flag. We must be careful and correctly
1249 * detect state changes when calling them.
1250 */
1251
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001253
Willy Tarreau14641402009-12-29 14:49:56 +01001254 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1255 s->req->cons->flags |= SI_FL_NOLINGER;
1256 s->req->cons->shutw(s->req->cons);
1257 }
1258
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001259 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1260 s->req->prod->shutr(s->req->prod);
1261
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001263
Willy Tarreau14641402009-12-29 14:49:56 +01001264 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1265 s->rep->cons->flags |= SI_FL_NOLINGER;
1266 s->rep->cons->shutw(s->rep->cons);
1267 }
1268
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001269 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1270 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001271 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001272
1273 /* 1b: check for low-level errors reported at the stream interface.
1274 * First we check if it's a retryable error (in which case we don't
1275 * want to tell the buffer). Otherwise we report the error one level
1276 * upper by setting flags into the buffers. Note that the side towards
1277 * the client cannot have connect (hence retryable) errors. Also, the
1278 * connection setup code must be able to deal with any type of abort.
1279 */
1280 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1281 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1282 s->si[0].shutr(&s->si[0]);
1283 s->si[0].shutw(&s->si[0]);
1284 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001286 s->be->counters.cli_aborts++;
1287 if (s->srv)
1288 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001289 if (!(s->flags & SN_ERR_MASK))
1290 s->flags |= SN_ERR_CLICL;
1291 if (!(s->flags & SN_FINST_MASK))
1292 s->flags |= SN_FINST_D;
1293 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001294 }
1295 }
1296
1297 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1298 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1299 s->si[1].shutr(&s->si[1]);
1300 s->si[1].shutw(&s->si[1]);
1301 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001302 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001304 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001305 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001306 s->be->counters.srv_aborts++;
1307 if (s->srv)
1308 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001309 if (!(s->flags & SN_ERR_MASK))
1310 s->flags |= SN_ERR_SRVCL;
1311 if (!(s->flags & SN_FINST_MASK))
1312 s->flags |= SN_FINST_D;
1313 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001314 }
1315 /* note: maybe we should process connection errors here ? */
1316 }
1317
1318 if (s->si[1].state == SI_ST_CON) {
1319 /* we were trying to establish a connection on the server side,
1320 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1321 */
1322 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1323 sess_update_st_cer(s, &s->si[1]);
1324 else if (s->si[1].state == SI_ST_EST)
1325 sess_establish(s, &s->si[1]);
1326
1327 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1328 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1329 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1330 */
1331 }
1332
Willy Tarreau815a9b22010-07-27 17:15:12 +02001333 rq_prod_last = s->si[0].state;
1334 rq_cons_last = s->si[1].state;
1335 rp_cons_last = s->si[0].state;
1336 rp_prod_last = s->si[1].state;
1337
1338 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001339 /* Check for connection closure */
1340
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001341 DPRINTF(stderr,
1342 "[%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",
1343 now_ms, __FUNCTION__, __LINE__,
1344 t,
1345 s, s->flags,
1346 s->req, s->rep,
1347 s->req->rex, s->rep->wex,
1348 s->req->flags, s->rep->flags,
1349 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1350 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001351 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001352
1353 /* nothing special to be done on client side */
1354 if (unlikely(s->req->prod->state == SI_ST_DIS))
1355 s->req->prod->state = SI_ST_CLO;
1356
1357 /* When a server-side connection is released, we have to count it and
1358 * check for pending connections on this server.
1359 */
1360 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1361 s->req->cons->state = SI_ST_CLO;
1362 if (s->srv) {
1363 if (s->flags & SN_CURR_SESS) {
1364 s->flags &= ~SN_CURR_SESS;
1365 s->srv->cur_sess--;
1366 }
1367 sess_change_server(s, NULL);
1368 if (may_dequeue_tasks(s->srv, s->be))
1369 process_srv_queue(s->srv);
1370 }
1371 }
1372
1373 /*
1374 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1375 * at this point.
1376 */
1377
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001378 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001379 /* Analyse request */
1380 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001381 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1382 s->si[0].state != rq_prod_last ||
1383 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001384 unsigned int flags = s->req->flags;
1385
1386 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001387 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001388 unsigned int ana_list;
1389 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001390
Willy Tarreau90deb182010-01-07 00:20:41 +01001391 /* it's up to the analysers to stop new connections,
1392 * disable reading or closing. Note: if an analyser
1393 * disables any of these bits, it is responsible for
1394 * enabling them again when it disables itself, so
1395 * that other analysers are called in similar conditions.
1396 */
1397 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001398 buffer_auto_connect(s->req);
1399 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001400
1401 /* We will call all analysers for which a bit is set in
1402 * s->req->analysers, following the bit order from LSB
1403 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001404 * the list when not needed. Any analyser may return 0
1405 * to break out of the loop, either because of missing
1406 * data to take a decision, or because it decides to
1407 * kill the session. We loop at least once through each
1408 * analyser, and we may loop again if other analysers
1409 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001410 *
1411 * We build a list of analysers to run. We evaluate all
1412 * of these analysers in the order of the lower bit to
1413 * the higher bit. This ordering is very important.
1414 * An analyser will often add/remove other analysers,
1415 * including itself. Any changes to itself have no effect
1416 * on the loop. If it removes any other analysers, we
1417 * want those analysers not to be called anymore during
1418 * this loop. If it adds an analyser that is located
1419 * after itself, we want it to be scheduled for being
1420 * processed during the loop. If it adds an analyser
1421 * which is located before it, we want it to switch to
1422 * it immediately, even if it has already been called
1423 * once but removed since.
1424 *
1425 * In order to achieve this, we compare the analyser
1426 * list after the call with a copy of it before the
1427 * call. The work list is fed with analyser bits that
1428 * appeared during the call. Then we compare previous
1429 * work list with the new one, and check the bits that
1430 * appeared. If the lowest of these bits is lower than
1431 * the current bit, it means we have enabled a previous
1432 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001433 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001434
1435 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001436 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001437 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001438
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001439 if (ana_list & AN_REQ_DECODE_PROXY) {
1440 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1441 break;
1442 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1443 }
1444
Willy Tarreaufb356202010-08-03 14:02:05 +02001445 if (ana_list & AN_REQ_INSPECT_FE) {
1446 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001447 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001448 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001449 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001450
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001451 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001452 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001453 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001454 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001455 }
1456
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001458 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1459 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001460 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001461 }
1462
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001464 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1465 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001466 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001467 }
1468
Willy Tarreaufb356202010-08-03 14:02:05 +02001469 if (ana_list & AN_REQ_INSPECT_BE) {
1470 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1471 break;
1472 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1473 }
1474
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001475 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001476 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1477 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001479 }
1480
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001481 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001482 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001483 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001484 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001485 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001486
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001487 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001488 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1489 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001490 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001491 }
1492
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001494 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001495 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001497 }
Emeric Brun647caf12009-06-30 17:57:00 +02001498
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001500 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1501 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001502 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001503 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001504
Emeric Brun1d33b292010-01-04 15:47:17 +01001505 if (ana_list & AN_REQ_STICKING_RULES) {
1506 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1507 break;
1508 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1509 }
1510
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001511 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001512 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1513 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001514 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001515 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001516 break;
1517 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001518 }
Willy Tarreau84455332009-03-15 22:34:05 +01001519
Willy Tarreau815a9b22010-07-27 17:15:12 +02001520 rq_prod_last = s->si[0].state;
1521 rq_cons_last = s->si[1].state;
1522 rqf_last = s->req->flags;
1523
1524 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001525 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001526 }
1527
Willy Tarreau576507f2010-01-07 00:09:04 +01001528 /* we'll monitor the request analysers while parsing the response,
1529 * because some response analysers may indirectly enable new request
1530 * analysers (eg: HTTP keep-alive).
1531 */
1532 req_ana_back = s->req->analysers;
1533
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001534 resync_response:
1535 /* Analyse response */
1536
1537 if (unlikely(s->rep->flags & BF_HIJACK)) {
1538 /* In inject mode, we wake up everytime something has
1539 * happened on the write side of the buffer.
1540 */
1541 unsigned int flags = s->rep->flags;
1542
1543 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1544 !(s->rep->flags & BF_FULL)) {
1545 s->rep->hijacker(s, s->rep);
1546 }
1547
1548 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1549 rpf_last = s->rep->flags;
1550 goto resync_response;
1551 }
1552 }
1553 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001554 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1555 s->si[0].state != rp_cons_last ||
1556 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001557 unsigned int flags = s->rep->flags;
1558
1559 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001560 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001561 unsigned int ana_list;
1562 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001563
Willy Tarreau90deb182010-01-07 00:20:41 +01001564 /* it's up to the analysers to stop disable reading or
1565 * closing. Note: if an analyser disables any of these
1566 * bits, it is responsible for enabling them again when
1567 * it disables itself, so that other analysers are called
1568 * in similar conditions.
1569 */
1570 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001571 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001572
1573 /* We will call all analysers for which a bit is set in
1574 * s->rep->analysers, following the bit order from LSB
1575 * to MSB. The analysers must remove themselves from
1576 * the list when not needed. Any analyser may return 0
1577 * to break out of the loop, either because of missing
1578 * data to take a decision, or because it decides to
1579 * kill the session. We loop at least once through each
1580 * analyser, and we may loop again if other analysers
1581 * are added in the middle.
1582 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001583
1584 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001585 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001586 /* Warning! ensure that analysers are always placed in ascending order! */
1587
1588 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001589 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1590 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001591 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001592 }
1593
Emeric Brun1d33b292010-01-04 15:47:17 +01001594 if (ana_list & AN_RES_STORE_RULES) {
1595 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1596 break;
1597 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1598 }
1599
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001600 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001601 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1602 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001603 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001604 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001605
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001606 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001607 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1608 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001609 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001610 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001611 break;
1612 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001613 }
1614
Willy Tarreau815a9b22010-07-27 17:15:12 +02001615 rp_cons_last = s->si[0].state;
1616 rp_prod_last = s->si[1].state;
1617 rpf_last = s->rep->flags;
1618
1619 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001620 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001621 }
1622
Willy Tarreau576507f2010-01-07 00:09:04 +01001623 /* maybe someone has added some request analysers, so we must check and loop */
1624 if (s->req->analysers & ~req_ana_back)
1625 goto resync_request;
1626
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001627 /* FIXME: here we should call protocol handlers which rely on
1628 * both buffers.
1629 */
1630
1631
1632 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001633 * Now we propagate unhandled errors to the session. Normally
1634 * we're just in a data phase here since it means we have not
1635 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001636 */
1637 if (!(s->flags & SN_ERR_MASK)) {
1638 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1639 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001640 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001641 if (s->req->flags & BF_READ_ERROR) {
1642 s->be->counters.cli_aborts++;
1643 if (s->srv)
1644 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001645 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001646 }
1647 else if (s->req->flags & BF_READ_TIMEOUT) {
1648 s->be->counters.cli_aborts++;
1649 if (s->srv)
1650 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001651 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001652 }
1653 else if (s->req->flags & BF_WRITE_ERROR) {
1654 s->be->counters.srv_aborts++;
1655 if (s->srv)
1656 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001657 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001658 }
1659 else {
1660 s->be->counters.srv_aborts++;
1661 if (s->srv)
1662 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001663 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001664 }
Willy Tarreau84455332009-03-15 22:34:05 +01001665 sess_set_term_flags(s);
1666 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001667 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1668 /* Report it if the server got an error or a read timeout expired */
1669 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001670 if (s->rep->flags & BF_READ_ERROR) {
1671 s->be->counters.srv_aborts++;
1672 if (s->srv)
1673 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001674 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001675 }
1676 else if (s->rep->flags & BF_READ_TIMEOUT) {
1677 s->be->counters.srv_aborts++;
1678 if (s->srv)
1679 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001680 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001681 }
1682 else if (s->rep->flags & BF_WRITE_ERROR) {
1683 s->be->counters.cli_aborts++;
1684 if (s->srv)
1685 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001686 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001687 }
1688 else {
1689 s->be->counters.cli_aborts++;
1690 if (s->srv)
1691 s->srv->counters.cli_aborts++;
1692 s->flags |= SN_ERR_CLITO;
1693 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001694 sess_set_term_flags(s);
1695 }
Willy Tarreau84455332009-03-15 22:34:05 +01001696 }
1697
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001698 /*
1699 * Here we take care of forwarding unhandled data. This also includes
1700 * connection establishments and shutdown requests.
1701 */
1702
1703
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001704 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001705 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001706 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001707 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001708 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001709 (s->req->prod->state >= SI_ST_EST) &&
1710 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001711 /* This buffer is freewheeling, there's no analyser nor hijacker
1712 * attached to it. If any data are left in, we'll permit them to
1713 * move.
1714 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001715 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001716 buffer_auto_connect(s->req);
1717 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001718 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001719
Willy Tarreau31971e52009-09-20 12:07:52 +02001720 /* If the producer is still connected, we'll enable data to flow
1721 * from the producer to the consumer (which might possibly not be
1722 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001723 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001724 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1725 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001726 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001727
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001728 /* check if it is wise to enable kernel splicing to forward request data */
1729 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1730 s->req->to_forward &&
1731 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001732 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001733 (pipes_used < global.maxpipes) &&
1734 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1735 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1736 (s->req->flags & BF_STREAMER_FAST)))) {
1737 s->req->flags |= BF_KERN_SPLICING;
1738 }
1739
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001740 /* reflect what the L7 analysers have seen last */
1741 rqf_last = s->req->flags;
1742
1743 /*
1744 * Now forward all shutdown requests between both sides of the buffer
1745 */
1746
Willy Tarreau520d95e2009-09-19 21:04:57 +02001747 /* first, let's check if the request buffer needs to shutdown(write), which may
1748 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001749 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001750 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001751 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001752 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001753 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001754
1755 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001756 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 +01001757 s->req->cons->shutw(s->req->cons);
1758
1759 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001760 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1761 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001762 buffer_shutr_now(s->req);
1763
1764 /* shutdown(read) pending */
1765 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1766 s->req->prod->shutr(s->req->prod);
1767
Willy Tarreau520d95e2009-09-19 21:04:57 +02001768 /* it's possible that an upper layer has requested a connection setup or abort.
1769 * There are 2 situations where we decide to establish a new connection :
1770 * - there are data scheduled for emission in the buffer
1771 * - the BF_AUTO_CONNECT flag is set (active connection)
1772 */
1773 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001774 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001775 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001776 /* If we have an iohandler without a connect method, we immediately
1777 * switch to the connected state, otherwise we perform a connection
1778 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001779 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001780 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001781 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001782 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001783 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001784 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1785 s->req->wex = TICK_ETERNITY;
1786 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001787 }
Willy Tarreau73201222009-08-16 18:27:24 +02001788 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001789 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001790 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001791 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1792 buffer_shutr_now(s->rep);
1793 }
Willy Tarreau92795622009-03-06 12:51:23 +01001794 }
1795
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001796
1797 /* we may have a pending connection request, or a connection waiting
1798 * for completion.
1799 */
1800 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1801 do {
1802 /* nb: step 1 might switch from QUE to ASS, but we first want
1803 * to give a chance to step 2 to perform a redirect if needed.
1804 */
1805 if (s->si[1].state != SI_ST_REQ)
1806 sess_update_stream_int(s, &s->si[1]);
1807 if (s->si[1].state == SI_ST_REQ)
1808 sess_prepare_conn_req(s, &s->si[1]);
1809
1810 if (s->si[1].state == SI_ST_ASS && s->srv &&
1811 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1812 perform_http_redirect(s, &s->si[1]);
1813 } while (s->si[1].state == SI_ST_ASS);
1814 }
1815
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001816 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001817 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001818 goto resync_stream_interface;
1819
Willy Tarreau815a9b22010-07-27 17:15:12 +02001820 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001821 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001822 goto resync_request;
1823
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001824 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001825
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001826 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001827 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001828 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001829 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001830 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001831 (s->rep->prod->state >= SI_ST_EST) &&
1832 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001833 /* This buffer is freewheeling, there's no analyser nor hijacker
1834 * attached to it. If any data are left in, we'll permit them to
1835 * move.
1836 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001837 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001838 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001839 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001840 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1841 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001842 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001843
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001844 /* check if it is wise to enable kernel splicing to forward response data */
1845 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1846 s->rep->to_forward &&
1847 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001848 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001849 (pipes_used < global.maxpipes) &&
1850 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1851 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1852 (s->rep->flags & BF_STREAMER_FAST)))) {
1853 s->rep->flags |= BF_KERN_SPLICING;
1854 }
1855
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001856 /* reflect what the L7 analysers have seen last */
1857 rpf_last = s->rep->flags;
1858
1859 /*
1860 * Now forward all shutdown requests between both sides of the buffer
1861 */
1862
1863 /*
1864 * FIXME: this is probably where we should produce error responses.
1865 */
1866
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001867 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001868 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1869 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001870 buffer_shutw_now(s->rep);
1871
1872 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001873 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 +01001874 s->rep->cons->shutw(s->rep->cons);
1875
1876 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001877 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1878 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879 buffer_shutr_now(s->rep);
1880
1881 /* shutdown(read) pending */
1882 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1883 s->rep->prod->shutr(s->rep->prod);
1884
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001885 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886 goto resync_stream_interface;
1887
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001888 if (s->req->flags != rqf_last)
1889 goto resync_request;
1890
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001891 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001892 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001893
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001894 /* we're interested in getting wakeups again */
1895 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1896 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1897
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001898 /* This is needed only when debugging is enabled, to indicate
1899 * client-side or server-side close. Please note that in the unlikely
1900 * event where both sides would close at once, the sequence is reported
1901 * on the server side first.
1902 */
1903 if (unlikely((global.mode & MODE_DEBUG) &&
1904 (!(global.mode & MODE_QUIET) ||
1905 (global.mode & MODE_VERBOSE)))) {
1906 int len;
1907
1908 if (s->si[1].state == SI_ST_CLO &&
1909 s->si[1].prev_state == SI_ST_EST) {
1910 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1911 s->uniq_id, s->be->id,
1912 (unsigned short)s->si[0].fd,
1913 (unsigned short)s->si[1].fd);
1914 write(1, trash, len);
1915 }
1916
1917 if (s->si[0].state == SI_ST_CLO &&
1918 s->si[0].prev_state == SI_ST_EST) {
1919 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1920 s->uniq_id, s->be->id,
1921 (unsigned short)s->si[0].fd,
1922 (unsigned short)s->si[1].fd);
1923 write(1, trash, len);
1924 }
1925 }
1926
1927 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1928 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1929
1930 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1931 session_process_counters(s);
1932
Willy Tarreau1accfc02009-09-05 20:57:35 +02001933 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001934 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001935
Willy Tarreau1accfc02009-09-05 20:57:35 +02001936 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001937 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001938
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001939 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1940 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 +01001941 s->si[0].prev_state = s->si[0].state;
1942 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001943 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1944 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001945
1946 /* Trick: if a request is being waiting for the server to respond,
1947 * and if we know the server can timeout, we don't want the timeout
1948 * to expire on the client side first, but we're still interested
1949 * in passing data from the client to the server (eg: POST). Thus,
1950 * we can cancel the client's request timeout if the server's
1951 * request timeout is set and the server has not yet sent a response.
1952 */
1953
Willy Tarreau520d95e2009-09-19 21:04:57 +02001954 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001955 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1956 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001957 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001958 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001959
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001960 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001961 * Note that this one may wake the task up again.
1962 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001963 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1964 if (s->req->cons->iohandler)
1965 s->req->cons->iohandler(s->req->cons);
1966 if (s->rep->cons->iohandler)
1967 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001968 if (task_in_rq(t)) {
1969 /* If we woke up, we don't want to requeue the
1970 * task to the wait queue, but rather requeue
1971 * it into the runqueue ASAP.
1972 */
1973 t->expire = TICK_ETERNITY;
1974 return t;
1975 }
1976 }
1977
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001978 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1979 tick_first(s->rep->rex, s->rep->wex));
1980 if (s->req->analysers)
1981 t->expire = tick_first(t->expire, s->req->analyse_exp);
1982
1983 if (s->si[0].exp)
1984 t->expire = tick_first(t->expire, s->si[0].exp);
1985
1986 if (s->si[1].exp)
1987 t->expire = tick_first(t->expire, s->si[1].exp);
1988
1989#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001990 fprintf(stderr,
1991 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1992 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1993 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1994 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 +01001995#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001996
1997#ifdef DEBUG_DEV
1998 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001999 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002000 ABORT_NOW();
2001#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002002 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002003 }
2004
2005 s->fe->feconn--;
2006 if (s->flags & SN_BE_ASSIGNED)
2007 s->be->beconn--;
2008 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002009 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002010 s->listener->nbconn--;
2011 if (s->listener->state == LI_FULL &&
2012 s->listener->nbconn < s->listener->maxconn) {
2013 /* we should reactivate the listener */
2014 EV_FD_SET(s->listener->fd, DIR_RD);
2015 s->listener->state = LI_READY;
2016 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002017
2018 if (unlikely((global.mode & MODE_DEBUG) &&
2019 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2020 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002021 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002023 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002024 write(1, trash, len);
2025 }
2026
2027 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2028 session_process_counters(s);
2029
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002030 if (s->txn.status) {
2031 int n;
2032
2033 n = s->txn.status / 100;
2034 if (n < 1 || n > 5)
2035 n = 0;
2036
2037 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002038 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002039
Willy Tarreau24657792010-02-26 10:30:28 +01002040 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002041 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002042 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002043 }
2044
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045 /* let's do a final log if we need it */
2046 if (s->logs.logwait &&
2047 !(s->flags & SN_MONITOR) &&
2048 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002049 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 }
2051
2052 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002053 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002054 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002055 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002056 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002057}
2058
Willy Tarreau7c669d72008-06-20 15:04:11 +02002059/*
2060 * This function adjusts sess->srv_conn and maintains the previous and new
2061 * server's served session counts. Setting newsrv to NULL is enough to release
2062 * current connection slot. This function also notifies any LB algo which might
2063 * expect to be informed about any change in the number of active sessions on a
2064 * server.
2065 */
2066void sess_change_server(struct session *sess, struct server *newsrv)
2067{
2068 if (sess->srv_conn == newsrv)
2069 return;
2070
2071 if (sess->srv_conn) {
2072 sess->srv_conn->served--;
2073 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2074 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2075 sess->srv_conn = NULL;
2076 }
2077
2078 if (newsrv) {
2079 newsrv->served++;
2080 if (newsrv->proxy->lbprm.server_take_conn)
2081 newsrv->proxy->lbprm.server_take_conn(newsrv);
2082 sess->srv_conn = newsrv;
2083 }
2084}
2085
Willy Tarreau84455332009-03-15 22:34:05 +01002086/* Set correct session termination flags in case no analyser has done it. It
2087 * also counts a failed request if the server state has not reached the request
2088 * stage.
2089 */
2090void sess_set_term_flags(struct session *s)
2091{
2092 if (!(s->flags & SN_FINST_MASK)) {
2093 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002094
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002095 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002096 if (s->listener->counters)
2097 s->listener->counters->failed_req++;
2098
Willy Tarreau84455332009-03-15 22:34:05 +01002099 s->flags |= SN_FINST_R;
2100 }
2101 else if (s->si[1].state == SI_ST_QUE)
2102 s->flags |= SN_FINST_Q;
2103 else if (s->si[1].state < SI_ST_EST)
2104 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002105 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002106 s->flags |= SN_FINST_D;
2107 else
2108 s->flags |= SN_FINST_L;
2109 }
2110}
2111
2112/* Handle server-side errors for default protocols. It is called whenever a a
2113 * connection setup is aborted or a request is aborted in queue. It sets the
2114 * session termination flags so that the caller does not have to worry about
2115 * them. It's installed as ->srv_error for the server-side stream_interface.
2116 */
2117void default_srv_error(struct session *s, struct stream_interface *si)
2118{
2119 int err_type = si->err_type;
2120 int err = 0, fin = 0;
2121
2122 if (err_type & SI_ET_QUEUE_ABRT) {
2123 err = SN_ERR_CLICL;
2124 fin = SN_FINST_Q;
2125 }
2126 else if (err_type & SI_ET_CONN_ABRT) {
2127 err = SN_ERR_CLICL;
2128 fin = SN_FINST_C;
2129 }
2130 else if (err_type & SI_ET_QUEUE_TO) {
2131 err = SN_ERR_SRVTO;
2132 fin = SN_FINST_Q;
2133 }
2134 else if (err_type & SI_ET_QUEUE_ERR) {
2135 err = SN_ERR_SRVCL;
2136 fin = SN_FINST_Q;
2137 }
2138 else if (err_type & SI_ET_CONN_TO) {
2139 err = SN_ERR_SRVTO;
2140 fin = SN_FINST_C;
2141 }
2142 else if (err_type & SI_ET_CONN_ERR) {
2143 err = SN_ERR_SRVCL;
2144 fin = SN_FINST_C;
2145 }
2146 else /* SI_ET_CONN_OTHER and others */ {
2147 err = SN_ERR_INTERNAL;
2148 fin = SN_FINST_C;
2149 }
2150
2151 if (!(s->flags & SN_ERR_MASK))
2152 s->flags |= err;
2153 if (!(s->flags & SN_FINST_MASK))
2154 s->flags |= fin;
2155}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002156
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002157
Willy Tarreau8b22a712010-06-18 17:46:06 +02002158/************************************************************************/
2159/* All supported ACL keywords must be declared here. */
2160/************************************************************************/
2161
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002162/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2163static int
2164acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2165{
2166 test->flags = ACL_TEST_F_VOL_TEST;
2167 test->i = 0;
2168 if (ts != NULL) {
2169 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2170 if (!ptr)
2171 return 0; /* parameter not stored */
2172 test->i = stktable_data_cast(ptr, gpc0);
2173 }
2174 return 1;
2175}
2176
2177/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002178 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002179 */
2180static int
Willy Tarreau56123282010-08-06 19:06:56 +02002181acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2182 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002183{
Willy Tarreau56123282010-08-06 19:06:56 +02002184 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002185 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002186 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002187}
2188
2189/* set test->i to the General Purpose Counter 0 value from the session's tracked
2190 * backend counters.
2191 */
2192static int
Willy Tarreau56123282010-08-06 19:06:56 +02002193acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2194 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002195{
Willy Tarreau56123282010-08-06 19:06:56 +02002196 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002197 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002198 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002199}
2200
2201/* set test->i to the General Purpose Counter 0 value from the session's source
2202 * address in the table pointed to by expr.
2203 */
2204static int
2205acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2206 struct acl_expr *expr, struct acl_test *test)
2207{
2208 struct stktable_key *key;
2209
2210 key = tcpv4_src_to_stktable_key(l4);
2211 if (!key)
2212 return 0; /* only TCPv4 is supported right now */
2213
2214 if (expr->arg_len)
2215 px = find_stktable(expr->arg.str);
2216
2217 if (!px)
2218 return 0; /* table not found */
2219
2220 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2221}
2222
2223/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2224 * return it into test->i.
2225 */
2226static int
2227acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2228{
2229 test->flags = ACL_TEST_F_VOL_TEST;
2230 test->i = 0;
2231 if (ts != NULL) {
2232 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2233 if (!ptr)
2234 return 0; /* parameter not stored */
2235 test->i = ++stktable_data_cast(ptr, gpc0);
2236 }
2237 return 1;
2238}
2239
2240/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002241 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002242 */
2243static int
Willy Tarreau56123282010-08-06 19:06:56 +02002244acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2245 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002246{
Willy Tarreau56123282010-08-06 19:06:56 +02002247 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002248 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002249 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002250}
2251
2252/* Increment the General Purpose Counter 0 value from the session's tracked
2253 * backend counters and return it into test->i.
2254 */
2255static int
Willy Tarreau56123282010-08-06 19:06:56 +02002256acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2257 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002258{
Willy Tarreau56123282010-08-06 19:06:56 +02002259 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002260 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002261 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002262}
2263
2264/* Increment the General Purpose Counter 0 value from the session's source
2265 * address in the table pointed to by expr, and return it into test->i.
2266 */
2267static int
2268acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2269 struct acl_expr *expr, struct acl_test *test)
2270{
2271 struct stktable_key *key;
2272
2273 key = tcpv4_src_to_stktable_key(l4);
2274 if (!key)
2275 return 0; /* only TCPv4 is supported right now */
2276
2277 if (expr->arg_len)
2278 px = find_stktable(expr->arg.str);
2279
2280 if (!px)
2281 return 0; /* table not found */
2282
2283 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2284}
2285
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002286/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2287static int
2288acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2289{
2290 test->flags = ACL_TEST_F_VOL_TEST;
2291 test->i = 0;
2292 if (ts != NULL) {
2293 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2294 if (!ptr)
2295 return 0; /* parameter not stored */
2296 test->i = stktable_data_cast(ptr, conn_cnt);
2297 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002298 return 1;
2299}
2300
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002301/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002302static int
Willy Tarreau56123282010-08-06 19:06:56 +02002303acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2304 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002305{
Willy Tarreau56123282010-08-06 19:06:56 +02002306 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002307 return 0;
2308
Willy Tarreau56123282010-08-06 19:06:56 +02002309 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002310}
2311
2312/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2313static int
Willy Tarreau56123282010-08-06 19:06:56 +02002314acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2315 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002316{
Willy Tarreau56123282010-08-06 19:06:56 +02002317 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002318 return 0;
2319
Willy Tarreau56123282010-08-06 19:06:56 +02002320 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002321}
2322
2323/* set test->i to the cumulated number of connections from the session's source
2324 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002325 */
2326static int
2327acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2328 struct acl_expr *expr, struct acl_test *test)
2329{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002330 struct stktable_key *key;
2331
2332 key = tcpv4_src_to_stktable_key(l4);
2333 if (!key)
2334 return 0; /* only TCPv4 is supported right now */
2335
2336 if (expr->arg_len)
2337 px = find_stktable(expr->arg.str);
2338
2339 if (!px)
2340 return 0; /* table not found */
2341
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002342 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002343}
2344
Willy Tarreau91c43d72010-06-20 11:19:22 +02002345/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2346static int
2347acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2348{
2349 test->flags = ACL_TEST_F_VOL_TEST;
2350 test->i = 0;
2351 if (ts != NULL) {
2352 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2353 if (!ptr)
2354 return 0; /* parameter not stored */
2355 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2356 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2357 }
2358 return 1;
2359}
2360
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002361/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002362 * the configured period.
2363 */
2364static int
Willy Tarreau56123282010-08-06 19:06:56 +02002365acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2366 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002367{
Willy Tarreau56123282010-08-06 19:06:56 +02002368 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002369 return 0;
2370
Willy Tarreau56123282010-08-06 19:06:56 +02002371 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002372}
2373
2374/* set test->i to the connection rate from the session's tracked BE counters over
2375 * the configured period.
2376 */
2377static int
Willy Tarreau56123282010-08-06 19:06:56 +02002378acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2379 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002380{
Willy Tarreau56123282010-08-06 19:06:56 +02002381 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002382 return 0;
2383
Willy Tarreau56123282010-08-06 19:06:56 +02002384 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002385}
2386
2387/* set test->i to the connection rate from the session's source address in the
2388 * table pointed to by expr, over the configured period.
2389 */
2390static int
2391acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2392 struct acl_expr *expr, struct acl_test *test)
2393{
2394 struct stktable_key *key;
2395
2396 key = tcpv4_src_to_stktable_key(l4);
2397 if (!key)
2398 return 0; /* only TCPv4 is supported right now */
2399
2400 if (expr->arg_len)
2401 px = find_stktable(expr->arg.str);
2402
2403 if (!px)
2404 return 0; /* table not found */
2405
2406 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2407}
2408
Willy Tarreau8b22a712010-06-18 17:46:06 +02002409/* set test->i to the number of connections from the session's source address
2410 * in the table pointed to by expr, after updating it.
2411 */
2412static int
2413acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2414 struct acl_expr *expr, struct acl_test *test)
2415{
2416 struct stksess *ts;
2417 struct stktable_key *key;
2418 void *ptr;
2419
2420 key = tcpv4_src_to_stktable_key(l4);
2421 if (!key)
2422 return 0; /* only TCPv4 is supported right now */
2423
2424 if (expr->arg_len)
2425 px = find_stktable(expr->arg.str);
2426
2427 if (!px)
2428 return 0; /* table not found */
2429
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002430 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2431 /* entry does not exist and could not be created */
2432 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002433
2434 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2435 if (!ptr)
2436 return 0; /* parameter not stored in this table */
2437
2438 test->i = ++stktable_data_cast(ptr, conn_cnt);
2439 test->flags = ACL_TEST_F_VOL_TEST;
2440 return 1;
2441}
2442
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002443/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2444static int
2445acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2446{
2447 test->flags = ACL_TEST_F_VOL_TEST;
2448 test->i = 0;
2449
2450 if (ts != NULL) {
2451 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2452 if (!ptr)
2453 return 0; /* parameter not stored */
2454 test->i = stktable_data_cast(ptr, conn_cur);
2455 }
2456 return 1;
2457}
2458
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002459/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002460static int
Willy Tarreau56123282010-08-06 19:06:56 +02002461acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2462 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002463{
Willy Tarreau56123282010-08-06 19:06:56 +02002464 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002465 return 0;
2466
Willy Tarreau56123282010-08-06 19:06:56 +02002467 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002468}
2469
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002470/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2471static int
Willy Tarreau56123282010-08-06 19:06:56 +02002472acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2473 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002474{
Willy Tarreau56123282010-08-06 19:06:56 +02002475 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002476 return 0;
2477
Willy Tarreau56123282010-08-06 19:06:56 +02002478 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002479}
2480
Willy Tarreau38285c12010-06-18 16:35:43 +02002481/* set test->i to the number of concurrent connections from the session's source
2482 * address in the table pointed to by expr.
2483 */
2484static int
2485acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2486 struct acl_expr *expr, struct acl_test *test)
2487{
Willy Tarreau38285c12010-06-18 16:35:43 +02002488 struct stktable_key *key;
2489
2490 key = tcpv4_src_to_stktable_key(l4);
2491 if (!key)
2492 return 0; /* only TCPv4 is supported right now */
2493
2494 if (expr->arg_len)
2495 px = find_stktable(expr->arg.str);
2496
2497 if (!px)
2498 return 0; /* table not found */
2499
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002500 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002501}
2502
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002503/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2504static int
2505acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2506{
2507 test->flags = ACL_TEST_F_VOL_TEST;
2508 test->i = 0;
2509 if (ts != NULL) {
2510 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2511 if (!ptr)
2512 return 0; /* parameter not stored */
2513 test->i = stktable_data_cast(ptr, sess_cnt);
2514 }
2515 return 1;
2516}
2517
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002518/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002519static int
Willy Tarreau56123282010-08-06 19:06:56 +02002520acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2521 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002522{
Willy Tarreau56123282010-08-06 19:06:56 +02002523 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002524 return 0;
2525
Willy Tarreau56123282010-08-06 19:06:56 +02002526 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002527}
2528
2529/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2530static int
Willy Tarreau56123282010-08-06 19:06:56 +02002531acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2532 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002533{
Willy Tarreau56123282010-08-06 19:06:56 +02002534 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002535 return 0;
2536
Willy Tarreau56123282010-08-06 19:06:56 +02002537 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002538}
2539
2540/* set test->i to the cumulated number of session from the session's source
2541 * address in the table pointed to by expr.
2542 */
2543static int
2544acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2545 struct acl_expr *expr, struct acl_test *test)
2546{
2547 struct stktable_key *key;
2548
2549 key = tcpv4_src_to_stktable_key(l4);
2550 if (!key)
2551 return 0; /* only TCPv4 is supported right now */
2552
2553 if (expr->arg_len)
2554 px = find_stktable(expr->arg.str);
2555
2556 if (!px)
2557 return 0; /* table not found */
2558
2559 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2560}
2561
Willy Tarreau91c43d72010-06-20 11:19:22 +02002562/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2563static int
2564acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2565{
2566 test->flags = ACL_TEST_F_VOL_TEST;
2567 test->i = 0;
2568 if (ts != NULL) {
2569 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2570 if (!ptr)
2571 return 0; /* parameter not stored */
2572 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2573 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2574 }
2575 return 1;
2576}
2577
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002578/* set test->i to the session rate from the session's tracked FE counters over
2579 * the configured period.
2580 */
2581static int
Willy Tarreau56123282010-08-06 19:06:56 +02002582acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2583 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002584{
Willy Tarreau56123282010-08-06 19:06:56 +02002585 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002586 return 0;
2587
Willy Tarreau56123282010-08-06 19:06:56 +02002588 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002589}
2590
2591/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002592 * the configured period.
2593 */
2594static int
Willy Tarreau56123282010-08-06 19:06:56 +02002595acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2596 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002597{
Willy Tarreau56123282010-08-06 19:06:56 +02002598 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002599 return 0;
2600
Willy Tarreau56123282010-08-06 19:06:56 +02002601 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002602}
2603
2604/* set test->i to the session rate from the session's source address in the
2605 * table pointed to by expr, over the configured period.
2606 */
2607static int
2608acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2609 struct acl_expr *expr, struct acl_test *test)
2610{
2611 struct stktable_key *key;
2612
2613 key = tcpv4_src_to_stktable_key(l4);
2614 if (!key)
2615 return 0; /* only TCPv4 is supported right now */
2616
2617 if (expr->arg_len)
2618 px = find_stktable(expr->arg.str);
2619
2620 if (!px)
2621 return 0; /* table not found */
2622
2623 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2624}
2625
Willy Tarreauda7ff642010-06-23 11:44:09 +02002626/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2627static int
2628acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2629{
2630 test->flags = ACL_TEST_F_VOL_TEST;
2631 test->i = 0;
2632 if (ts != NULL) {
2633 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2634 if (!ptr)
2635 return 0; /* parameter not stored */
2636 test->i = stktable_data_cast(ptr, http_req_cnt);
2637 }
2638 return 1;
2639}
2640
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002641/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002642static int
Willy Tarreau56123282010-08-06 19:06:56 +02002643acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2644 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002645{
Willy Tarreau56123282010-08-06 19:06:56 +02002646 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002647 return 0;
2648
Willy Tarreau56123282010-08-06 19:06:56 +02002649 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002650}
2651
2652/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2653static int
Willy Tarreau56123282010-08-06 19:06:56 +02002654acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2655 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002656{
Willy Tarreau56123282010-08-06 19:06:56 +02002657 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002658 return 0;
2659
Willy Tarreau56123282010-08-06 19:06:56 +02002660 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002661}
2662
2663/* set test->i to the cumulated number of session from the session's source
2664 * address in the table pointed to by expr.
2665 */
2666static int
2667acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002668 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002669{
2670 struct stktable_key *key;
2671
2672 key = tcpv4_src_to_stktable_key(l4);
2673 if (!key)
2674 return 0; /* only TCPv4 is supported right now */
2675
2676 if (expr->arg_len)
2677 px = find_stktable(expr->arg.str);
2678
2679 if (!px)
2680 return 0; /* table not found */
2681
2682 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2683}
2684
2685/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2686static int
2687acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2688{
2689 test->flags = ACL_TEST_F_VOL_TEST;
2690 test->i = 0;
2691 if (ts != NULL) {
2692 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2693 if (!ptr)
2694 return 0; /* parameter not stored */
2695 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2696 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2697 }
2698 return 1;
2699}
2700
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002701/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002702 * the configured period.
2703 */
2704static int
Willy Tarreau56123282010-08-06 19:06:56 +02002705acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2706 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002707{
Willy Tarreau56123282010-08-06 19:06:56 +02002708 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002709 return 0;
2710
Willy Tarreau56123282010-08-06 19:06:56 +02002711 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002712}
2713
2714/* set test->i to the session rate from the session's tracked BE counters over
2715 * the configured period.
2716 */
2717static int
Willy Tarreau56123282010-08-06 19:06:56 +02002718acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2719 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002720{
Willy Tarreau56123282010-08-06 19:06:56 +02002721 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002722 return 0;
2723
Willy Tarreau56123282010-08-06 19:06:56 +02002724 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002725}
2726
2727/* set test->i to the session rate from the session's source address in the
2728 * table pointed to by expr, over the configured period.
2729 */
2730static int
2731acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002732 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002733{
2734 struct stktable_key *key;
2735
2736 key = tcpv4_src_to_stktable_key(l4);
2737 if (!key)
2738 return 0; /* only TCPv4 is supported right now */
2739
2740 if (expr->arg_len)
2741 px = find_stktable(expr->arg.str);
2742
2743 if (!px)
2744 return 0; /* table not found */
2745
2746 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2747}
2748
2749/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2750static int
2751acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2752{
2753 test->flags = ACL_TEST_F_VOL_TEST;
2754 test->i = 0;
2755 if (ts != NULL) {
2756 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2757 if (!ptr)
2758 return 0; /* parameter not stored */
2759 test->i = stktable_data_cast(ptr, http_err_cnt);
2760 }
2761 return 1;
2762}
2763
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002764/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002765static int
Willy Tarreau56123282010-08-06 19:06:56 +02002766acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2767 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002768{
Willy Tarreau56123282010-08-06 19:06:56 +02002769 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002770 return 0;
2771
Willy Tarreau56123282010-08-06 19:06:56 +02002772 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002773}
2774
2775/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2776static int
Willy Tarreau56123282010-08-06 19:06:56 +02002777acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2778 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002779{
Willy Tarreau56123282010-08-06 19:06:56 +02002780 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002781 return 0;
2782
Willy Tarreau56123282010-08-06 19:06:56 +02002783 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002784}
2785
2786/* set test->i to the cumulated number of session from the session's source
2787 * address in the table pointed to by expr.
2788 */
2789static int
2790acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002791 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002792{
2793 struct stktable_key *key;
2794
2795 key = tcpv4_src_to_stktable_key(l4);
2796 if (!key)
2797 return 0; /* only TCPv4 is supported right now */
2798
2799 if (expr->arg_len)
2800 px = find_stktable(expr->arg.str);
2801
2802 if (!px)
2803 return 0; /* table not found */
2804
2805 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2806}
2807
2808/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2809static int
2810acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2811{
2812 test->flags = ACL_TEST_F_VOL_TEST;
2813 test->i = 0;
2814 if (ts != NULL) {
2815 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2816 if (!ptr)
2817 return 0; /* parameter not stored */
2818 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2819 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2820 }
2821 return 1;
2822}
2823
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002824/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002825 * the configured period.
2826 */
2827static int
Willy Tarreau56123282010-08-06 19:06:56 +02002828acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2829 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002830{
Willy Tarreau56123282010-08-06 19:06:56 +02002831 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002832 return 0;
2833
Willy Tarreau56123282010-08-06 19:06:56 +02002834 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002835}
2836
2837/* set test->i to the session rate from the session's tracked BE counters over
2838 * the configured period.
2839 */
2840static int
Willy Tarreau56123282010-08-06 19:06:56 +02002841acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2842 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002843{
Willy Tarreau56123282010-08-06 19:06:56 +02002844 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002845 return 0;
2846
Willy Tarreau56123282010-08-06 19:06:56 +02002847 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002848}
2849
2850/* set test->i to the session rate from the session's source address in the
2851 * table pointed to by expr, over the configured period.
2852 */
2853static int
2854acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2855 struct acl_expr *expr, struct acl_test *test)
2856{
2857 struct stktable_key *key;
2858
2859 key = tcpv4_src_to_stktable_key(l4);
2860 if (!key)
2861 return 0; /* only TCPv4 is supported right now */
2862
2863 if (expr->arg_len)
2864 px = find_stktable(expr->arg.str);
2865
2866 if (!px)
2867 return 0; /* table not found */
2868
2869 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2870}
2871
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002872/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2873static int
2874acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2875{
2876 test->flags = ACL_TEST_F_VOL_TEST;
2877 test->i = 0;
2878
2879 if (ts != NULL) {
2880 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2881 if (!ptr)
2882 return 0; /* parameter not stored */
2883 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2884 }
2885 return 1;
2886}
2887
2888/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002889 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002890 */
2891static int
Willy Tarreau56123282010-08-06 19:06:56 +02002892acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2893 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002894{
Willy Tarreau56123282010-08-06 19:06:56 +02002895 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002896 return 0;
2897
Willy Tarreau56123282010-08-06 19:06:56 +02002898 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002899}
2900
2901/* set test->i to the number of kbytes received from clients according to the
2902 * session's tracked BE counters.
2903 */
2904static int
Willy Tarreau56123282010-08-06 19:06:56 +02002905acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2906 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002907{
Willy Tarreau56123282010-08-06 19:06:56 +02002908 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002909 return 0;
2910
Willy Tarreau56123282010-08-06 19:06:56 +02002911 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002912}
2913
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002914/* set test->i to the number of kbytes received from the session's source
2915 * address in the table pointed to by expr.
2916 */
2917static int
2918acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2919 struct acl_expr *expr, struct acl_test *test)
2920{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002921 struct stktable_key *key;
2922
2923 key = tcpv4_src_to_stktable_key(l4);
2924 if (!key)
2925 return 0; /* only TCPv4 is supported right now */
2926
2927 if (expr->arg_len)
2928 px = find_stktable(expr->arg.str);
2929
2930 if (!px)
2931 return 0; /* table not found */
2932
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002933 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2934}
2935
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002936/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2937 * configured period.
2938 */
2939static int
2940acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2941{
2942 test->flags = ACL_TEST_F_VOL_TEST;
2943 test->i = 0;
2944 if (ts != NULL) {
2945 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2946 if (!ptr)
2947 return 0; /* parameter not stored */
2948 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2949 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2950 }
2951 return 1;
2952}
2953
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002954/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002955 * counters over the configured period.
2956 */
2957static int
Willy Tarreau56123282010-08-06 19:06:56 +02002958acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2959 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002960{
Willy Tarreau56123282010-08-06 19:06:56 +02002961 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002962 return 0;
2963
Willy Tarreau56123282010-08-06 19:06:56 +02002964 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002965}
2966
2967/* set test->i to the bytes rate from clients from the session's tracked BE
2968 * counters over the configured period.
2969 */
2970static int
Willy Tarreau56123282010-08-06 19:06:56 +02002971acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2972 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002973{
Willy Tarreau56123282010-08-06 19:06:56 +02002974 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002975 return 0;
2976
Willy Tarreau56123282010-08-06 19:06:56 +02002977 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002978}
2979
2980/* set test->i to the bytes rate from clients from the session's source address
2981 * in the table pointed to by expr, over the configured period.
2982 */
2983static int
2984acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002985 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002986{
2987 struct stktable_key *key;
2988
2989 key = tcpv4_src_to_stktable_key(l4);
2990 if (!key)
2991 return 0; /* only TCPv4 is supported right now */
2992
2993 if (expr->arg_len)
2994 px = find_stktable(expr->arg.str);
2995
2996 if (!px)
2997 return 0; /* table not found */
2998
2999 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3000}
3001
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003002/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3003static int
3004acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3005{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003006 test->flags = ACL_TEST_F_VOL_TEST;
3007 test->i = 0;
3008
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003009 if (ts != NULL) {
3010 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003011 if (!ptr)
3012 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003013 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003014 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003015 return 1;
3016}
3017
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003018/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003019 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003020 */
3021static int
Willy Tarreau56123282010-08-06 19:06:56 +02003022acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3023 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003024{
Willy Tarreau56123282010-08-06 19:06:56 +02003025 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003026 return 0;
3027
Willy Tarreau56123282010-08-06 19:06:56 +02003028 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003029}
3030
3031/* set test->i to the number of kbytes sent to clients according to the session's
3032 * tracked BE counters.
3033 */
3034static int
Willy Tarreau56123282010-08-06 19:06:56 +02003035acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3036 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003037{
Willy Tarreau56123282010-08-06 19:06:56 +02003038 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003039 return 0;
3040
Willy Tarreau56123282010-08-06 19:06:56 +02003041 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003042}
3043
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003044/* set test->i to the number of kbytes sent to the session's source address in
3045 * the table pointed to by expr.
3046 */
3047static int
3048acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3049 struct acl_expr *expr, struct acl_test *test)
3050{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003051 struct stktable_key *key;
3052
3053 key = tcpv4_src_to_stktable_key(l4);
3054 if (!key)
3055 return 0; /* only TCPv4 is supported right now */
3056
3057 if (expr->arg_len)
3058 px = find_stktable(expr->arg.str);
3059
3060 if (!px)
3061 return 0; /* table not found */
3062
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003063 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003064}
3065
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003066/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3067 * configured period.
3068 */
3069static int
3070acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3071{
3072 test->flags = ACL_TEST_F_VOL_TEST;
3073 test->i = 0;
3074 if (ts != NULL) {
3075 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3076 if (!ptr)
3077 return 0; /* parameter not stored */
3078 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3079 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3080 }
3081 return 1;
3082}
3083
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003084/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003085 * over the configured period.
3086 */
3087static int
Willy Tarreau56123282010-08-06 19:06:56 +02003088acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3089 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003090{
Willy Tarreau56123282010-08-06 19:06:56 +02003091 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003092 return 0;
3093
Willy Tarreau56123282010-08-06 19:06:56 +02003094 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003095}
3096
3097/* set test->i to the bytes rate to clients from the session's tracked BE counters
3098 * over the configured period.
3099 */
3100static int
Willy Tarreau56123282010-08-06 19:06:56 +02003101acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3102 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003103{
Willy Tarreau56123282010-08-06 19:06:56 +02003104 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003105 return 0;
3106
Willy Tarreau56123282010-08-06 19:06:56 +02003107 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003108}
3109
3110/* set test->i to the bytes rate to client from the session's source address in
3111 * the table pointed to by expr, over the configured period.
3112 */
3113static int
3114acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003115 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003116{
3117 struct stktable_key *key;
3118
3119 key = tcpv4_src_to_stktable_key(l4);
3120 if (!key)
3121 return 0; /* only TCPv4 is supported right now */
3122
3123 if (expr->arg_len)
3124 px = find_stktable(expr->arg.str);
3125
3126 if (!px)
3127 return 0; /* table not found */
3128
3129 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3130}
3131
Willy Tarreau8b22a712010-06-18 17:46:06 +02003132
3133/* Note: must not be declared <const> as its list will be overwritten */
3134static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003135 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3136 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3137 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3138 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3139 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3140 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3141 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3142 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3143 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3144 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3145 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3146 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3147 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3148 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3149 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3150 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3151 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3152 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3153 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3154 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3155 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3156 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3157 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3158 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3159 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3160 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3161 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3162 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3163 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3164 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3165 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3166 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3167 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3168 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3169 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3171 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3172 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3173 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3174 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3175 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3176 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3177 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3178 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3179 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3180 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003181 { NULL, NULL, NULL, NULL },
3182}};
3183
3184
Willy Tarreau56123282010-08-06 19:06:56 +02003185/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003186 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3187 * <prm> struct is fed with the table name if any. If unspecified, the caller
3188 * will assume that the current proxy's table is used.
3189 */
3190int parse_track_counters(char **args, int *arg,
3191 int section_type, struct proxy *curpx,
3192 struct track_ctr_prm *prm,
3193 struct proxy *defpx, char *err, int errlen)
3194{
3195 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003196 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003197
Willy Tarreau56123282010-08-06 19:06:56 +02003198 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003199 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003200 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003201 */
3202 while (args[*arg]) {
3203 if (strcmp(args[*arg], "src") == 0) {
3204 prm->type = STKTABLE_TYPE_IP;
3205 pattern_type = 1;
3206 }
3207 else if (strcmp(args[*arg], "table") == 0) {
3208 if (!args[*arg + 1]) {
3209 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003210 "missing table for %s in %s '%s'.",
3211 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003212 return -1;
3213 }
3214 /* we copy the table name for now, it will be resolved later */
3215 prm->table.n = strdup(args[*arg + 1]);
3216 (*arg)++;
3217 }
3218 else {
3219 /* unhandled keywords are handled by the caller */
3220 break;
3221 }
3222 (*arg)++;
3223 }
3224
3225 if (!pattern_type) {
3226 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003227 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3228 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003229 return -1;
3230 }
3231
3232 return 0;
3233}
3234
Willy Tarreau8b22a712010-06-18 17:46:06 +02003235__attribute__((constructor))
3236static void __session_init(void)
3237{
3238 acl_register_keywords(&acl_kws);
3239}
3240
Willy Tarreaubaaee002006-06-26 02:48:02 +02003241/*
3242 * Local variables:
3243 * c-indent-level: 8
3244 * c-basic-offset: 8
3245 * End:
3246 */