blob: ae720cf34a928e9092e305086b57b030b5771e17 [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
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
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;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 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 Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100133 if (p->feconn > p->fe_counters.conn_max)
134 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100171 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200191 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200192 s->si[1].err_loc = NULL;
193 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200194 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100195 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200196 s->si[1].shutr = stream_int_shutr;
197 s->si[1].shutw = stream_int_shutw;
198 s->si[1].exp = TICK_ETERNITY;
199 s->si[1].flags = SI_FL_NONE;
200
201 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
202 s->si[1].flags |= SI_FL_INDEP_STR;
203
Simon Hormanaf514952011-06-21 14:34:57 +0900204 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100205 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206 s->pend_pos = NULL;
207
208 /* init store persistence */
209 s->store_count = 0;
210
211 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100212 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214
215 txn = &s->txn;
216 /* Those variables will be checked and freed if non-NULL in
217 * session.c:session_free(). It is important that they are
218 * properly initialized.
219 */
220 txn->sessid = NULL;
221 txn->srv_cookie = NULL;
222 txn->cli_cookie = NULL;
223 txn->uri = NULL;
224 txn->req.cap = NULL;
225 txn->rsp.cap = NULL;
226 txn->hdr_idx.v = NULL;
227 txn->hdr_idx.size = txn->hdr_idx.used = 0;
228
229 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
230 goto out_free_task; /* no memory */
231
232 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
233 goto out_free_req; /* no memory */
234
235 /* initialize the request buffer */
236 s->req->size = global.tune.bufsize;
237 buffer_init(s->req);
238 s->req->prod = &s->si[0];
239 s->req->cons = &s->si[1];
240 s->si[0].ib = s->si[1].ob = s->req;
241 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
242
243 /* activate default analysers enabled for this listener */
244 s->req->analysers = l->analysers;
245
246 s->req->wto = TICK_ETERNITY;
247 s->req->rto = TICK_ETERNITY;
248 s->req->rex = TICK_ETERNITY;
249 s->req->wex = TICK_ETERNITY;
250 s->req->analyse_exp = TICK_ETERNITY;
251
252 /* initialize response buffer */
253 s->rep->size = global.tune.bufsize;
254 buffer_init(s->rep);
255 s->rep->prod = &s->si[1];
256 s->rep->cons = &s->si[0];
257 s->si[0].ob = s->si[1].ib = s->rep;
258 s->rep->analysers = 0;
259
Willy Tarreau96e31212011-05-30 18:10:30 +0200260 if (s->fe->options2 & PR_O2_NODELAY) {
261 s->req->flags |= BF_NEVER_WAIT;
262 s->rep->flags |= BF_NEVER_WAIT;
263 }
264
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200265 s->rep->rto = TICK_ETERNITY;
266 s->rep->wto = TICK_ETERNITY;
267 s->rep->rex = TICK_ETERNITY;
268 s->rep->wex = TICK_ETERNITY;
269 s->rep->analyse_exp = TICK_ETERNITY;
270
271 /* finish initialization of the accepted file descriptor */
272 fd_insert(cfd);
273 fdtab[cfd].owner = &s->si[0];
274 fdtab[cfd].state = FD_STREADY;
275 fdtab[cfd].flags = 0;
276 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
277 fdtab[cfd].cb[DIR_RD].b = s->req;
278 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
279 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100280 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.c.from;
281 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.c.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200282 EV_FD_SET(cfd, DIR_RD);
283
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100284 if (p->accept && (ret = p->accept(s)) <= 0) {
285 /* Either we had an unrecoverable error (<0) or work is
286 * finished (=0, eg: monitoring), in both situations,
287 * we can release everything and close.
288 */
289 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200290 }
291
292 /* it is important not to call the wakeup function directly but to
293 * pass through task_wakeup(), because this one knows how to apply
294 * priorities to tasks.
295 */
296 task_wakeup(t, TASK_WOKEN_INIT);
297 return 1;
298
299 /* Error unrolling */
300 out_free_rep:
301 pool_free2(pool2_buffer, s->rep);
302 out_free_req:
303 pool_free2(pool2_buffer, s->req);
304 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200305 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200306 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200307 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200308 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200309 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100310 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200311 pool_free2(pool2_session, s);
312 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100313 if (fdtab[cfd].state != FD_STCLOSE)
314 fd_delete(cfd);
315 else
316 close(cfd);
317 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200318}
319
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320/*
321 * frees the context associated to a session. It must have been removed first.
322 */
Simon Hormandec5be42011-06-08 09:19:07 +0900323static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100325 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200326 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100327 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200328 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100329
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 if (s->pend_pos)
331 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100332
Willy Tarreau827aee92011-03-10 16:55:02 +0100333 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100334 if (s->flags & SN_CURR_SESS) {
335 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100336 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100337 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100338 if (may_dequeue_tasks(target_srv(&s->target), s->be))
339 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100340 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100341
Willy Tarreau7c669d72008-06-20 15:04:11 +0200342 if (unlikely(s->srv_conn)) {
343 /* the session still has a reserved slot on a server, but
344 * it should normally be only the same as the one above,
345 * so this should not happen in fact.
346 */
347 sess_change_server(s, NULL);
348 }
349
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100350 if (s->req->pipe)
351 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100352
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100353 if (s->rep->pipe)
354 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100355
Willy Tarreau48d63db2008-08-03 17:41:33 +0200356 pool_free2(pool2_buffer, s->req);
357 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
Willy Tarreau46023632010-01-07 22:51:47 +0100359 http_end_txn(s);
360
Willy Tarreaua4cda672010-06-06 18:28:49 +0200361 for (i = 0; i < s->store_count; i++) {
362 if (!s->store[i].ts)
363 continue;
364 stksess_free(s->store[i].table, s->store[i].ts);
365 s->store[i].ts = NULL;
366 }
367
Willy Tarreau92fb9832007-10-16 17:34:28 +0200368 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200369 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100370 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
371 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100373
Willy Tarreau56123282010-08-06 19:06:56 +0200374 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200375 session_store_counters(s);
376
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100377 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100378 /* we have to unlink all watchers. We must not relink them if
379 * this session was the last one in the list.
380 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100381 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100382 LIST_INIT(&bref->users);
383 if (s->list.n != &sessions)
384 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100385 bref->ref = s->list.n;
386 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100387 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200388 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200389
390 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200391 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200392 pool_flush2(pool2_buffer);
393 pool_flush2(fe->hdr_idx_pool);
394 pool_flush2(pool2_requri);
395 pool_flush2(pool2_capture);
396 pool_flush2(pool2_session);
397 pool_flush2(fe->req_cap_pool);
398 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200399 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200400}
401
402
403/* perform minimal intializations, report 0 in case of error, 1 if OK. */
404int init_session()
405{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100406 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200407 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
408 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409}
410
Willy Tarreau30e71012007-11-26 20:15:35 +0100411void session_process_counters(struct session *s)
412{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100413 unsigned long long bytes;
414
Willy Tarreau30e71012007-11-26 20:15:35 +0100415 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100416 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100417 s->logs.bytes_in = s->req->total;
418 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100419 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100420
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100421 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422
Willy Tarreau827aee92011-03-10 16:55:02 +0100423 if (target_srv(&s->target))
424 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200425
426 if (s->listener->counters)
427 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200428
Willy Tarreau56123282010-08-06 19:06:56 +0200429 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200430 void *ptr;
431
Willy Tarreau56123282010-08-06 19:06:56 +0200432 ptr = stktable_data_ptr(s->stkctr2_table,
433 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200434 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435 if (ptr)
436 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200437
Willy Tarreau56123282010-08-06 19:06:56 +0200438 ptr = stktable_data_ptr(s->stkctr2_table,
439 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200440 STKTABLE_DT_BYTES_IN_RATE);
441 if (ptr)
442 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200443 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200444 }
445
Willy Tarreau56123282010-08-06 19:06:56 +0200446 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200447 void *ptr;
448
Willy Tarreau56123282010-08-06 19:06:56 +0200449 ptr = stktable_data_ptr(s->stkctr1_table,
450 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 STKTABLE_DT_BYTES_IN_CNT);
452 if (ptr)
453 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
454
Willy Tarreau56123282010-08-06 19:06:56 +0200455 ptr = stktable_data_ptr(s->stkctr1_table,
456 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200457 STKTABLE_DT_BYTES_IN_RATE);
458 if (ptr)
459 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200460 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200461 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100462 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100463 }
464
Willy Tarreau30e71012007-11-26 20:15:35 +0100465 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100466 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100467 s->logs.bytes_out = s->rep->total;
468 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100469 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100471 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472
Willy Tarreau827aee92011-03-10 16:55:02 +0100473 if (target_srv(&s->target))
474 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200475
476 if (s->listener->counters)
477 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200478
Willy Tarreau56123282010-08-06 19:06:56 +0200479 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200480 void *ptr;
481
Willy Tarreau56123282010-08-06 19:06:56 +0200482 ptr = stktable_data_ptr(s->stkctr2_table,
483 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200484 STKTABLE_DT_BYTES_OUT_CNT);
485 if (ptr)
486 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
487
Willy Tarreau56123282010-08-06 19:06:56 +0200488 ptr = stktable_data_ptr(s->stkctr2_table,
489 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200490 STKTABLE_DT_BYTES_OUT_RATE);
491 if (ptr)
492 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200493 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200494 }
495
Willy Tarreau56123282010-08-06 19:06:56 +0200496 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200497 void *ptr;
498
Willy Tarreau56123282010-08-06 19:06:56 +0200499 ptr = stktable_data_ptr(s->stkctr1_table,
500 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200501 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200502 if (ptr)
503 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504
Willy Tarreau56123282010-08-06 19:06:56 +0200505 ptr = stktable_data_ptr(s->stkctr1_table,
506 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200507 STKTABLE_DT_BYTES_OUT_RATE);
508 if (ptr)
509 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200510 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200511 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100512 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100513 }
514}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100516/* This function is called with (si->state == SI_ST_CON) meaning that a
517 * connection was attempted and that the file descriptor is already allocated.
518 * We must check for establishment, error and abort. Possible output states
519 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
520 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
521 * otherwise 1.
522 */
Simon Hormandec5be42011-06-08 09:19:07 +0900523static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524{
525 struct buffer *req = si->ob;
526 struct buffer *rep = si->ib;
527
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100528 /* If we got an error, or if nothing happened and the connection timed
529 * out, we must give up. The CER state handler will take care of retry
530 * attempts and error reports.
531 */
532 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100533 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200535 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 fd_delete(si->fd);
537
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200538 if (si->release)
539 si->release(si);
540
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 if (si->err_type)
542 return 0;
543
Willy Tarreau827aee92011-03-10 16:55:02 +0100544 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100545 if (si->flags & SI_FL_ERR)
546 si->err_type = SI_ET_CONN_ERR;
547 else
548 si->err_type = SI_ET_CONN_TO;
549 return 0;
550 }
551
552 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200553 if (unlikely((rep->flags & BF_SHUTW) ||
554 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200555 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100556 s->be->options & PR_O_ABRT_CLOSE)))) {
557 /* give up */
558 si->shutw(si);
559 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100560 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200561 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100562 if (s->srv_error)
563 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 return 1;
565 }
566
567 /* we need to wait a bit more if there was no activity either */
568 if (!(req->flags & BF_WRITE_ACTIVITY))
569 return 1;
570
571 /* OK, this means that a connection succeeded. The caller will be
572 * responsible for handling the transition from CON to EST.
573 */
574 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100575 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100576 si->state = SI_ST_EST;
577 si->err_type = SI_ET_NONE;
578 si->err_loc = NULL;
579 return 1;
580}
581
582/* This function is called with (si->state == SI_ST_CER) meaning that a
583 * previous connection attempt has failed and that the file descriptor
584 * has already been released. Possible causes include asynchronous error
585 * notification and time out. Possible output states are SI_ST_CLO when
586 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
587 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
588 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
589 * marked as in error state. It returns 0.
590 */
Simon Hormandec5be42011-06-08 09:19:07 +0900591static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592{
593 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 if (target_srv(&s->target)) {
595 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100596
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 if (s->flags & SN_CURR_SESS) {
598 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100599 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 }
602
603 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200604 si->conn_retries--;
605 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 if (!si->err_type) {
607 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 }
610
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 if (target_srv(&s->target))
612 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100613 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100614 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 if (may_dequeue_tasks(target_srv(&s->target), s->be))
616 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617
618 /* shutw is enough so stop a connecting socket */
619 si->shutw(si);
620 si->ob->flags |= BF_WRITE_ERROR;
621 si->ib->flags |= BF_READ_ERROR;
622
623 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100624 if (s->srv_error)
625 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100626 return 0;
627 }
628
629 /* If the "redispatch" option is set on the backend, we are allowed to
630 * retry on another server for the last retry. In order to achieve this,
631 * we must mark the session unassigned, and eventually clear the DIRECT
632 * bit to ignore any persistence cookie. We won't count a retry nor a
633 * redispatch yet, because this will depend on what server is selected.
634 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100636 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100637 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100638 if (may_dequeue_tasks(target_srv(&s->target), s->be))
639 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640
641 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 si->state = SI_ST_REQ;
643 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 if (target_srv(&s->target))
645 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100646 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 si->state = SI_ST_ASS;
648 }
649
650 if (si->flags & SI_FL_ERR) {
651 /* The error was an asynchronous connection error, and we will
652 * likely have to retry connecting to the same server, most
653 * likely leading to the same result. To avoid this, we wait
654 * one second before retrying.
655 */
656
657 if (!si->err_type)
658 si->err_type = SI_ET_CONN_ERR;
659
660 si->state = SI_ST_TAR;
661 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
662 return 0;
663 }
664 return 0;
665}
666
667/*
668 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200669 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
670 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 */
Simon Hormandec5be42011-06-08 09:19:07 +0900672static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673{
674 struct buffer *req = si->ob;
675 struct buffer *rep = si->ib;
676
Willy Tarreau827aee92011-03-10 16:55:02 +0100677 if (target_srv(&s->target))
678 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100679
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 /* if the user wants to log as soon as possible, without counting
682 * bytes from the server, then this is the right moment. */
683 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
684 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100685 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 }
688 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
690 /* reset hdr_idx which was already initialized by the request.
691 * right now, the http parser does it.
692 * hdr_idx_init(&s->txn.hdr_idx);
693 */
694 }
695
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200696 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200698 if (si->connect) {
699 /* real connections have timeouts */
700 req->wto = s->be->timeout.server;
701 rep->rto = s->be->timeout.server;
702 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 req->wex = TICK_ETERNITY;
704}
705
706/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
707 * Other input states are simply ignored.
708 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
709 * Flags must have previously been updated for timeouts and other conditions.
710 */
Simon Hormandec5be42011-06-08 09:19:07 +0900711static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712{
Willy Tarreau827aee92011-03-10 16:55:02 +0100713 struct server *srv = target_srv(&s->target);
714
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 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",
716 now_ms, __FUNCTION__,
717 s,
718 s->req, s->rep,
719 s->req->rex, s->rep->wex,
720 s->req->flags, s->rep->flags,
721 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
722
723 if (si->state == SI_ST_ASS) {
724 /* Server assigned to connection request, we have to try to connect now */
725 int conn_err;
726
727 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100728 srv = target_srv(&s->target);
729
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 if (conn_err == SN_ERR_NONE) {
731 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100732 if (srv)
733 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100734 return;
735 }
736
737 /* We have received a synchronous error. We might have to
738 * abort, retry immediately or redispatch.
739 */
740 if (conn_err == SN_ERR_INTERNAL) {
741 if (!si->err_type) {
742 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
745
Willy Tarreau827aee92011-03-10 16:55:02 +0100746 if (srv)
747 srv_inc_sess_ctr(srv);
748 if (srv)
749 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100750 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751
752 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100753 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100754 if (may_dequeue_tasks(srv, s->be))
755 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756
757 /* Failed and not retryable. */
758 si->shutr(si);
759 si->shutw(si);
760 si->ob->flags |= BF_WRITE_ERROR;
761
762 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
763
764 /* no session was ever accounted for this server */
765 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100766 if (s->srv_error)
767 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768 return;
769 }
770
771 /* We are facing a retryable error, but we don't want to run a
772 * turn-around now, as the problem is likely a source port
773 * allocation problem, so we want to retry now.
774 */
775 si->state = SI_ST_CER;
776 si->flags &= ~SI_FL_ERR;
777 sess_update_st_cer(s, si);
778 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
779 return;
780 }
781 else if (si->state == SI_ST_QUE) {
782 /* connection request was queued, check for any update */
783 if (!s->pend_pos) {
784 /* The connection is not in the queue anymore. Either
785 * we have a server connection slot available and we
786 * go directly to the assigned state, or we need to
787 * load-balance first and go to the INI state.
788 */
789 si->exp = TICK_ETERNITY;
790 if (unlikely(!(s->flags & SN_ASSIGNED)))
791 si->state = SI_ST_REQ;
792 else {
793 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
794 si->state = SI_ST_ASS;
795 }
796 return;
797 }
798
799 /* Connection request still in queue... */
800 if (si->flags & SI_FL_EXP) {
801 /* ... and timeout expired */
802 si->exp = TICK_ETERNITY;
803 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100804 if (srv)
805 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100806 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100807 si->shutr(si);
808 si->shutw(si);
809 si->ob->flags |= BF_WRITE_TIMEOUT;
810 if (!si->err_type)
811 si->err_type = SI_ET_QUEUE_TO;
812 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100813 if (s->srv_error)
814 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100815 return;
816 }
817
818 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200819 if ((si->ob->flags & (BF_READ_ERROR)) ||
820 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200821 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 /* give up */
823 si->exp = TICK_ETERNITY;
824 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
825 si->shutr(si);
826 si->shutw(si);
827 si->err_type |= SI_ET_QUEUE_ABRT;
828 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829 if (s->srv_error)
830 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 return;
832 }
833
834 /* Nothing changed */
835 return;
836 }
837 else if (si->state == SI_ST_TAR) {
838 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200839 if ((si->ob->flags & (BF_READ_ERROR)) ||
840 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200841 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 /* give up */
843 si->exp = TICK_ETERNITY;
844 si->shutr(si);
845 si->shutw(si);
846 si->err_type |= SI_ET_CONN_ABRT;
847 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100848 if (s->srv_error)
849 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850 return;
851 }
852
853 if (!(si->flags & SI_FL_EXP))
854 return; /* still in turn-around */
855
856 si->exp = TICK_ETERNITY;
857
858 /* we keep trying on the same server as long as the session is
859 * marked "assigned".
860 * FIXME: Should we force a redispatch attempt when the server is down ?
861 */
862 if (s->flags & SN_ASSIGNED)
863 si->state = SI_ST_ASS;
864 else
865 si->state = SI_ST_REQ;
866 return;
867 }
868}
869
Simon Hormandec5be42011-06-08 09:19:07 +0900870/* Set correct session termination flags in case no analyser has done it. It
871 * also counts a failed request if the server state has not reached the request
872 * stage.
873 */
874static void sess_set_term_flags(struct session *s)
875{
876 if (!(s->flags & SN_FINST_MASK)) {
877 if (s->si[1].state < SI_ST_REQ) {
878
879 s->fe->fe_counters.failed_req++;
880 if (s->listener->counters)
881 s->listener->counters->failed_req++;
882
883 s->flags |= SN_FINST_R;
884 }
885 else if (s->si[1].state == SI_ST_QUE)
886 s->flags |= SN_FINST_Q;
887 else if (s->si[1].state < SI_ST_EST)
888 s->flags |= SN_FINST_C;
889 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
890 s->flags |= SN_FINST_D;
891 else
892 s->flags |= SN_FINST_L;
893 }
894}
895
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100896/* This function initiates a server connection request on a stream interface
897 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
898 * indicating that a server has been assigned. It may also return SI_ST_QUE,
899 * or SI_ST_CLO upon error.
900 */
901static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
902 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",
903 now_ms, __FUNCTION__,
904 s,
905 s->req, s->rep,
906 s->req->rex, s->rep->wex,
907 s->req->flags, s->rep->flags,
908 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
909
910 if (si->state != SI_ST_REQ)
911 return;
912
913 /* Try to assign a server */
914 if (srv_redispatch_connect(s) != 0) {
915 /* We did not get a server. Either we queued the
916 * connection request, or we encountered an error.
917 */
918 if (si->state == SI_ST_QUE)
919 return;
920
921 /* we did not get any server, let's check the cause */
922 si->shutr(si);
923 si->shutw(si);
924 si->ob->flags |= BF_WRITE_ERROR;
925 if (!si->err_type)
926 si->err_type = SI_ET_CONN_OTHER;
927 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100928 if (s->srv_error)
929 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 return;
931 }
932
933 /* The server is assigned */
934 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
935 si->state = SI_ST_ASS;
936}
937
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200938/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100939 * if appropriate. The default_backend rule is also considered, then the
940 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200941 * It returns 1 if the processing can continue on next analysers, or zero if it
942 * either needs more data or wants to immediately abort the request.
943 */
Simon Hormandec5be42011-06-08 09:19:07 +0900944static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200946 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100947
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200948 req->analysers &= ~an_bit;
949 req->analyse_exp = TICK_ETERNITY;
950
951 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
952 now_ms, __FUNCTION__,
953 s,
954 req,
955 req->rex, req->wex,
956 req->flags,
957 req->l,
958 req->analysers);
959
960 /* now check whether we have some switching rules for this request */
961 if (!(s->flags & SN_BE_ASSIGNED)) {
962 struct switching_rule *rule;
963
964 list_for_each_entry(rule, &s->fe->switching_rules, list) {
965 int ret;
966
967 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
968 ret = acl_pass(ret);
969 if (rule->cond->pol == ACL_COND_UNLESS)
970 ret = !ret;
971
972 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200973 if (!session_set_backend(s, rule->be.backend))
974 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200975 break;
976 }
977 }
978
979 /* To ensure correct connection accounting on the backend, we
980 * have to assign one if it was not set (eg: a listen). This
981 * measure also takes care of correctly setting the default
982 * backend if any.
983 */
984 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200985 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
986 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200987 }
988
Willy Tarreaufb356202010-08-03 14:02:05 +0200989 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
990 if (s->fe == s->be) {
991 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200992 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200993 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200994
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200995 /* 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 +0100996 * persistence rule, and report that in the session.
997 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200998 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100999 int ret = 1;
1000
1001 if (prst_rule->cond) {
1002 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1003 ret = acl_pass(ret);
1004 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1005 ret = !ret;
1006 }
1007
1008 if (ret) {
1009 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001010 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1011 s->flags |= SN_FORCE_PRST;
1012 } else {
1013 s->flags |= SN_IGNORE_PRST;
1014 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001015 break;
1016 }
1017 }
1018
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001019 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001020
1021 sw_failed:
1022 /* immediately abort this request in case of allocation failure */
1023 buffer_abort(s->req);
1024 buffer_abort(s->rep);
1025
1026 if (!(s->flags & SN_ERR_MASK))
1027 s->flags |= SN_ERR_RESOURCE;
1028 if (!(s->flags & SN_FINST_MASK))
1029 s->flags |= SN_FINST_R;
1030
1031 s->txn.status = 500;
1032 s->req->analysers = 0;
1033 s->req->analyse_exp = TICK_ETERNITY;
1034 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001035}
1036
Emeric Brun1d33b292010-01-04 15:47:17 +01001037/* This stream analyser works on a request. It applies all sticking rules on
1038 * it then returns 1. The data must already be present in the buffer otherwise
1039 * they won't match. It always returns 1.
1040 */
Simon Hormandec5be42011-06-08 09:19:07 +09001041static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001042{
1043 struct proxy *px = s->be;
1044 struct sticking_rule *rule;
1045
1046 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1047 now_ms, __FUNCTION__,
1048 s,
1049 req,
1050 req->rex, req->wex,
1051 req->flags,
1052 req->l,
1053 req->analysers);
1054
1055 list_for_each_entry(rule, &px->sticking_rules, list) {
1056 int ret = 1 ;
1057 int i;
1058
1059 for (i = 0; i < s->store_count; i++) {
1060 if (rule->table.t == s->store[i].table)
1061 break;
1062 }
1063
1064 if (i != s->store_count)
1065 continue;
1066
1067 if (rule->cond) {
1068 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1069 ret = acl_pass(ret);
1070 if (rule->cond->pol == ACL_COND_UNLESS)
1071 ret = !ret;
1072 }
1073
1074 if (ret) {
1075 struct stktable_key *key;
1076
Emeric Brun485479d2010-09-23 18:02:19 +02001077 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001078 if (!key)
1079 continue;
1080
1081 if (rule->flags & STK_IS_MATCH) {
1082 struct stksess *ts;
1083
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001084 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001085 if (!(s->flags & SN_ASSIGNED)) {
1086 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001087 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001088
1089 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001090 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1091 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001092 if (node) {
1093 struct server *srv;
1094
1095 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001096 if ((srv->state & SRV_RUNNING) ||
1097 (px->options & PR_O_PERSIST) ||
1098 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001099 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001100 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001101 }
1102 }
1103 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001104 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001105 }
1106 }
1107 if (rule->flags & STK_IS_STORE) {
1108 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1109 struct stksess *ts;
1110
1111 ts = stksess_new(rule->table.t, key);
1112 if (ts) {
1113 s->store[s->store_count].table = rule->table.t;
1114 s->store[s->store_count++].ts = ts;
1115 }
1116 }
1117 }
1118 }
1119 }
1120
1121 req->analysers &= ~an_bit;
1122 req->analyse_exp = TICK_ETERNITY;
1123 return 1;
1124}
1125
1126/* This stream analyser works on a response. It applies all store rules on it
1127 * then returns 1. The data must already be present in the buffer otherwise
1128 * they won't match. It always returns 1.
1129 */
Simon Hormandec5be42011-06-08 09:19:07 +09001130static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001131{
1132 struct proxy *px = s->be;
1133 struct sticking_rule *rule;
1134 int i;
1135
1136 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1137 now_ms, __FUNCTION__,
1138 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001139 rep,
1140 rep->rex, rep->wex,
1141 rep->flags,
1142 rep->l,
1143 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001144
1145 list_for_each_entry(rule, &px->storersp_rules, list) {
1146 int ret = 1 ;
1147 int storereqidx = -1;
1148
1149 for (i = 0; i < s->store_count; i++) {
1150 if (rule->table.t == s->store[i].table) {
1151 if (!(s->store[i].flags))
1152 storereqidx = i;
1153 break;
1154 }
1155 }
1156
1157 if ((i != s->store_count) && (storereqidx == -1))
1158 continue;
1159
1160 if (rule->cond) {
1161 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1162 ret = acl_pass(ret);
1163 if (rule->cond->pol == ACL_COND_UNLESS)
1164 ret = !ret;
1165 }
1166
1167 if (ret) {
1168 struct stktable_key *key;
1169
Emeric Brun485479d2010-09-23 18:02:19 +02001170 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001171 if (!key)
1172 continue;
1173
1174 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001175 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 s->store[storereqidx].flags = 1;
1177 }
1178 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1179 struct stksess *ts;
1180
1181 ts = stksess_new(rule->table.t, key);
1182 if (ts) {
1183 s->store[s->store_count].table = rule->table.t;
1184 s->store[s->store_count].flags = 1;
1185 s->store[s->store_count++].ts = ts;
1186 }
1187 }
1188 }
1189 }
1190
1191 /* process store request and store response */
1192 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001193 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001194 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001195
Simon Hormanfa461682011-06-25 09:39:49 +09001196 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1197 stksess_free(s->store[i].table, s->store[i].ts);
1198 s->store[i].ts = NULL;
1199 continue;
1200 }
1201
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001202 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1203 if (ts) {
1204 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001205 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001206 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001207 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001208 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001209 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001210
1211 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001212 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001213 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001214 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001215 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001216
1217 rep->analysers &= ~an_bit;
1218 rep->analyse_exp = TICK_ETERNITY;
1219 return 1;
1220}
1221
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001222/* This macro is very specific to the function below. See the comments in
1223 * process_session() below to understand the logic and the tests.
1224 */
1225#define UPDATE_ANALYSERS(real, list, back, flag) { \
1226 list = (((list) & ~(flag)) | ~(back)) & (real); \
1227 back = real; \
1228 if (!(list)) \
1229 break; \
1230 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1231 continue; \
1232}
1233
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001234/* Processes the client, server, request and response jobs of a session task,
1235 * then puts it back to the wait queue in a clean state, or cleans up its
1236 * resources if it must be deleted. Returns in <next> the date the task wants
1237 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1238 * nothing too many times, the request and response buffers flags are monitored
1239 * and each function is called only if at least another function has changed at
1240 * least one flag it is interested in.
1241 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001242struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001243{
Willy Tarreau827aee92011-03-10 16:55:02 +01001244 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001245 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001246 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001247 unsigned int rq_prod_last, rq_cons_last;
1248 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001249 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001250
1251 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1252 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1253
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001254 /* this data may be no longer valid, clear it */
1255 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1256
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001257 /* This flag must explicitly be set every time */
1258 s->req->flags &= ~BF_READ_NOEXP;
1259
1260 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001261 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1262 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001263
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001264 /* we don't want the stream interface functions to recursively wake us up */
1265 if (s->req->prod->owner == t)
1266 s->req->prod->flags |= SI_FL_DONT_WAKE;
1267 if (s->req->cons->owner == t)
1268 s->req->cons->flags |= SI_FL_DONT_WAKE;
1269
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001270 /* 1a: Check for low level timeouts if needed. We just set a flag on
1271 * stream interfaces when their timeouts have expired.
1272 */
1273 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1274 stream_int_check_timeouts(&s->si[0]);
1275 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001276
1277 /* check buffer timeouts, and close the corresponding stream interfaces
1278 * for future reads or writes. Note: this will also concern upper layers
1279 * but we do not touch any other flag. We must be careful and correctly
1280 * detect state changes when calling them.
1281 */
1282
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001283 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001284
Willy Tarreau14641402009-12-29 14:49:56 +01001285 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1286 s->req->cons->flags |= SI_FL_NOLINGER;
1287 s->req->cons->shutw(s->req->cons);
1288 }
1289
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001290 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1291 s->req->prod->shutr(s->req->prod);
1292
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001293 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001294
Willy Tarreau14641402009-12-29 14:49:56 +01001295 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1296 s->rep->cons->flags |= SI_FL_NOLINGER;
1297 s->rep->cons->shutw(s->rep->cons);
1298 }
1299
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001300 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1301 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001302 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303
1304 /* 1b: check for low-level errors reported at the stream interface.
1305 * First we check if it's a retryable error (in which case we don't
1306 * want to tell the buffer). Otherwise we report the error one level
1307 * upper by setting flags into the buffers. Note that the side towards
1308 * the client cannot have connect (hence retryable) errors. Also, the
1309 * connection setup code must be able to deal with any type of abort.
1310 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001311 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001312 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1313 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1314 s->si[0].shutr(&s->si[0]);
1315 s->si[0].shutw(&s->si[0]);
1316 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001317 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001318 s->be->be_counters.cli_aborts++;
1319 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001320 if (srv)
1321 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001322 if (!(s->flags & SN_ERR_MASK))
1323 s->flags |= SN_ERR_CLICL;
1324 if (!(s->flags & SN_FINST_MASK))
1325 s->flags |= SN_FINST_D;
1326 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001327 }
1328 }
1329
1330 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1331 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1332 s->si[1].shutr(&s->si[1]);
1333 s->si[1].shutw(&s->si[1]);
1334 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001335 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001336 if (srv)
1337 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001338 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001339 s->be->be_counters.srv_aborts++;
1340 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001341 if (srv)
1342 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001343 if (!(s->flags & SN_ERR_MASK))
1344 s->flags |= SN_ERR_SRVCL;
1345 if (!(s->flags & SN_FINST_MASK))
1346 s->flags |= SN_FINST_D;
1347 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001348 }
1349 /* note: maybe we should process connection errors here ? */
1350 }
1351
1352 if (s->si[1].state == SI_ST_CON) {
1353 /* we were trying to establish a connection on the server side,
1354 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1355 */
1356 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1357 sess_update_st_cer(s, &s->si[1]);
1358 else if (s->si[1].state == SI_ST_EST)
1359 sess_establish(s, &s->si[1]);
1360
1361 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1362 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1363 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1364 */
1365 }
1366
Willy Tarreau815a9b22010-07-27 17:15:12 +02001367 rq_prod_last = s->si[0].state;
1368 rq_cons_last = s->si[1].state;
1369 rp_cons_last = s->si[0].state;
1370 rp_prod_last = s->si[1].state;
1371
1372 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001373 /* Check for connection closure */
1374
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001375 DPRINTF(stderr,
1376 "[%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",
1377 now_ms, __FUNCTION__, __LINE__,
1378 t,
1379 s, s->flags,
1380 s->req, s->rep,
1381 s->req->rex, s->rep->wex,
1382 s->req->flags, s->rep->flags,
1383 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1384 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001385 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001386
1387 /* nothing special to be done on client side */
1388 if (unlikely(s->req->prod->state == SI_ST_DIS))
1389 s->req->prod->state = SI_ST_CLO;
1390
1391 /* When a server-side connection is released, we have to count it and
1392 * check for pending connections on this server.
1393 */
1394 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1395 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001396 srv = target_srv(&s->target);
1397 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001398 if (s->flags & SN_CURR_SESS) {
1399 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001400 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001401 }
1402 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001403 if (may_dequeue_tasks(srv, s->be))
1404 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001405 }
1406 }
1407
1408 /*
1409 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1410 * at this point.
1411 */
1412
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001413 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001414 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001415 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001416 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1417 s->si[0].state != rq_prod_last ||
1418 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001419 unsigned int flags = s->req->flags;
1420
1421 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001422 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001423 unsigned int ana_list;
1424 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001425
Willy Tarreau90deb182010-01-07 00:20:41 +01001426 /* it's up to the analysers to stop new connections,
1427 * disable reading or closing. Note: if an analyser
1428 * disables any of these bits, it is responsible for
1429 * enabling them again when it disables itself, so
1430 * that other analysers are called in similar conditions.
1431 */
1432 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001433 buffer_auto_connect(s->req);
1434 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001435
1436 /* We will call all analysers for which a bit is set in
1437 * s->req->analysers, following the bit order from LSB
1438 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001439 * the list when not needed. Any analyser may return 0
1440 * to break out of the loop, either because of missing
1441 * data to take a decision, or because it decides to
1442 * kill the session. We loop at least once through each
1443 * analyser, and we may loop again if other analysers
1444 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001445 *
1446 * We build a list of analysers to run. We evaluate all
1447 * of these analysers in the order of the lower bit to
1448 * the higher bit. This ordering is very important.
1449 * An analyser will often add/remove other analysers,
1450 * including itself. Any changes to itself have no effect
1451 * on the loop. If it removes any other analysers, we
1452 * want those analysers not to be called anymore during
1453 * this loop. If it adds an analyser that is located
1454 * after itself, we want it to be scheduled for being
1455 * processed during the loop. If it adds an analyser
1456 * which is located before it, we want it to switch to
1457 * it immediately, even if it has already been called
1458 * once but removed since.
1459 *
1460 * In order to achieve this, we compare the analyser
1461 * list after the call with a copy of it before the
1462 * call. The work list is fed with analyser bits that
1463 * appeared during the call. Then we compare previous
1464 * work list with the new one, and check the bits that
1465 * appeared. If the lowest of these bits is lower than
1466 * the current bit, it means we have enabled a previous
1467 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001468 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001469
1470 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001471 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001472 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001473
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001474 if (ana_list & AN_REQ_DECODE_PROXY) {
1475 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1476 break;
1477 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1478 }
1479
Willy Tarreaufb356202010-08-03 14:02:05 +02001480 if (ana_list & AN_REQ_INSPECT_FE) {
1481 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001482 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001483 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001484 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001485
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001487 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001488 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001490 }
1491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001493 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1494 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001496 }
1497
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001498 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001499 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1500 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001501 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001502 }
1503
Willy Tarreaufb356202010-08-03 14:02:05 +02001504 if (ana_list & AN_REQ_INSPECT_BE) {
1505 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1506 break;
1507 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1508 }
1509
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001510 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001511 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1512 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001513 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001514 }
1515
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001516 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001517 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001518 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001519 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001520 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001521
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001522 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001523 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1524 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001525 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001526 }
1527
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001528 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001529 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001530 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001531 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001532 }
Emeric Brun647caf12009-06-30 17:57:00 +02001533
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001534 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001535 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1536 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001537 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001538 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001539
Emeric Brun1d33b292010-01-04 15:47:17 +01001540 if (ana_list & AN_REQ_STICKING_RULES) {
1541 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1542 break;
1543 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1544 }
1545
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001546 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001547 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1548 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001549 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001550 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001551 break;
1552 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001553 }
Willy Tarreau84455332009-03-15 22:34:05 +01001554
Willy Tarreau815a9b22010-07-27 17:15:12 +02001555 rq_prod_last = s->si[0].state;
1556 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001557 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001558 rqf_last = s->req->flags;
1559
1560 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001561 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001562 }
1563
Willy Tarreau576507f2010-01-07 00:09:04 +01001564 /* we'll monitor the request analysers while parsing the response,
1565 * because some response analysers may indirectly enable new request
1566 * analysers (eg: HTTP keep-alive).
1567 */
1568 req_ana_back = s->req->analysers;
1569
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001570 resync_response:
1571 /* Analyse response */
1572
1573 if (unlikely(s->rep->flags & BF_HIJACK)) {
1574 /* In inject mode, we wake up everytime something has
1575 * happened on the write side of the buffer.
1576 */
1577 unsigned int flags = s->rep->flags;
1578
1579 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1580 !(s->rep->flags & BF_FULL)) {
1581 s->rep->hijacker(s, s->rep);
1582 }
1583
1584 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1585 rpf_last = s->rep->flags;
1586 goto resync_response;
1587 }
1588 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001589 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001590 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1591 s->si[0].state != rp_cons_last ||
1592 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001593 unsigned int flags = s->rep->flags;
1594
Willy Tarreau0499e352010-12-17 07:13:42 +01001595 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1596 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1597 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1598 * for some free space in the response buffer, so we might need to call
1599 * it when something changes in the response buffer, but still we pass
1600 * it the request buffer. Note that the SI state might very well still
1601 * be zero due to us returning a flow of redirects!
1602 */
1603 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1604 s->req->flags |= BF_WAKE_ONCE;
1605 }
1606
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001607 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001608 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001609 unsigned int ana_list;
1610 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001611
Willy Tarreau90deb182010-01-07 00:20:41 +01001612 /* it's up to the analysers to stop disable reading or
1613 * closing. Note: if an analyser disables any of these
1614 * bits, it is responsible for enabling them again when
1615 * it disables itself, so that other analysers are called
1616 * in similar conditions.
1617 */
1618 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001619 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001620
1621 /* We will call all analysers for which a bit is set in
1622 * s->rep->analysers, following the bit order from LSB
1623 * to MSB. The analysers must remove themselves from
1624 * the list when not needed. Any analyser may return 0
1625 * to break out of the loop, either because of missing
1626 * data to take a decision, or because it decides to
1627 * kill the session. We loop at least once through each
1628 * analyser, and we may loop again if other analysers
1629 * are added in the middle.
1630 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001631
1632 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001633 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001634 /* Warning! ensure that analysers are always placed in ascending order! */
1635
Emeric Brun97679e72010-09-23 17:56:44 +02001636 if (ana_list & AN_RES_INSPECT) {
1637 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1638 break;
1639 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1640 }
1641
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001642 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001643 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1644 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001645 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001646 }
1647
Emeric Brun1d33b292010-01-04 15:47:17 +01001648 if (ana_list & AN_RES_STORE_RULES) {
1649 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1650 break;
1651 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1652 }
1653
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001654 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001655 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1656 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001657 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001658 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001659
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001660 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001661 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1662 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001663 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001664 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001665 break;
1666 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001667 }
1668
Willy Tarreau815a9b22010-07-27 17:15:12 +02001669 rp_cons_last = s->si[0].state;
1670 rp_prod_last = s->si[1].state;
1671 rpf_last = s->rep->flags;
1672
1673 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001674 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001675 }
1676
Willy Tarreau576507f2010-01-07 00:09:04 +01001677 /* maybe someone has added some request analysers, so we must check and loop */
1678 if (s->req->analysers & ~req_ana_back)
1679 goto resync_request;
1680
Willy Tarreau0499e352010-12-17 07:13:42 +01001681 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1682 goto resync_request;
1683
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001684 /* FIXME: here we should call protocol handlers which rely on
1685 * both buffers.
1686 */
1687
1688
1689 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001690 * Now we propagate unhandled errors to the session. Normally
1691 * we're just in a data phase here since it means we have not
1692 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001693 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001694 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001695 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001696 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1697 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001698 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001699 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001700 s->be->be_counters.cli_aborts++;
1701 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001702 if (srv)
1703 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001704 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001705 }
1706 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001707 s->be->be_counters.cli_aborts++;
1708 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001709 if (srv)
1710 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001711 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001712 }
1713 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001714 s->be->be_counters.srv_aborts++;
1715 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001716 if (srv)
1717 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001718 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001719 }
1720 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001721 s->be->be_counters.srv_aborts++;
1722 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001723 if (srv)
1724 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001725 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001726 }
Willy Tarreau84455332009-03-15 22:34:05 +01001727 sess_set_term_flags(s);
1728 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001729 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1730 /* Report it if the server got an error or a read timeout expired */
1731 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001732 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001733 s->be->be_counters.srv_aborts++;
1734 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001735 if (srv)
1736 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001737 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001738 }
1739 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001740 s->be->be_counters.srv_aborts++;
1741 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001742 if (srv)
1743 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001744 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001745 }
1746 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001747 s->be->be_counters.cli_aborts++;
1748 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001749 if (srv)
1750 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001751 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001752 }
1753 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001754 s->be->be_counters.cli_aborts++;
1755 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001756 if (srv)
1757 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001758 s->flags |= SN_ERR_CLITO;
1759 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001760 sess_set_term_flags(s);
1761 }
Willy Tarreau84455332009-03-15 22:34:05 +01001762 }
1763
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001764 /*
1765 * Here we take care of forwarding unhandled data. This also includes
1766 * connection establishments and shutdown requests.
1767 */
1768
1769
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001770 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001771 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001772 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1773 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001774 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001775 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001776 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001777 (s->req->prod->state >= SI_ST_EST) &&
1778 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001779 /* This buffer is freewheeling, there's no analyser nor hijacker
1780 * attached to it. If any data are left in, we'll permit them to
1781 * move.
1782 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001783 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001784 buffer_auto_connect(s->req);
1785 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001786 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001787
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001788 /* We'll let data flow between the producer (if still connected)
1789 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001790 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001791 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001792 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001793 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001794
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001795 /* check if it is wise to enable kernel splicing to forward request data */
1796 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1797 s->req->to_forward &&
1798 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001799 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001800 (pipes_used < global.maxpipes) &&
1801 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1802 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1803 (s->req->flags & BF_STREAMER_FAST)))) {
1804 s->req->flags |= BF_KERN_SPLICING;
1805 }
1806
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001807 /* reflect what the L7 analysers have seen last */
1808 rqf_last = s->req->flags;
1809
1810 /*
1811 * Now forward all shutdown requests between both sides of the buffer
1812 */
1813
Willy Tarreau520d95e2009-09-19 21:04:57 +02001814 /* first, let's check if the request buffer needs to shutdown(write), which may
1815 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001816 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001817 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001818 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001819 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001820 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001821
1822 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001823 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 +01001824 s->req->cons->shutw(s->req->cons);
1825
1826 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001827 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1828 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001829 buffer_shutr_now(s->req);
1830
1831 /* shutdown(read) pending */
1832 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1833 s->req->prod->shutr(s->req->prod);
1834
Willy Tarreau520d95e2009-09-19 21:04:57 +02001835 /* it's possible that an upper layer has requested a connection setup or abort.
1836 * There are 2 situations where we decide to establish a new connection :
1837 * - there are data scheduled for emission in the buffer
1838 * - the BF_AUTO_CONNECT flag is set (active connection)
1839 */
1840 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001841 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001842 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001843 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001844 * switch to the connected state, otherwise we perform a connection
1845 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001846 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001847 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001848 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001849 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001850 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001851 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1852 s->req->wex = TICK_ETERNITY;
1853 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001854 }
Willy Tarreau73201222009-08-16 18:27:24 +02001855 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001856 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001857 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001858 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1859 buffer_shutr_now(s->rep);
1860 }
Willy Tarreau92795622009-03-06 12:51:23 +01001861 }
1862
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863
1864 /* we may have a pending connection request, or a connection waiting
1865 * for completion.
1866 */
1867 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1868 do {
1869 /* nb: step 1 might switch from QUE to ASS, but we first want
1870 * to give a chance to step 2 to perform a redirect if needed.
1871 */
1872 if (s->si[1].state != SI_ST_REQ)
1873 sess_update_stream_int(s, &s->si[1]);
1874 if (s->si[1].state == SI_ST_REQ)
1875 sess_prepare_conn_req(s, &s->si[1]);
1876
Willy Tarreau827aee92011-03-10 16:55:02 +01001877 srv = target_srv(&s->target);
1878 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879 perform_http_redirect(s, &s->si[1]);
1880 } while (s->si[1].state == SI_ST_ASS);
1881 }
1882
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001883 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001884 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001885 goto resync_stream_interface;
1886
Willy Tarreau815a9b22010-07-27 17:15:12 +02001887 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001888 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001889 goto resync_request;
1890
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001891 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001892
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001893 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001894 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001895 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1896 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001897 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001898 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001899 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001900 (s->rep->prod->state >= SI_ST_EST) &&
1901 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001902 /* This buffer is freewheeling, there's no analyser nor hijacker
1903 * attached to it. If any data are left in, we'll permit them to
1904 * move.
1905 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001906 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001907 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001908 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001909
1910 /* We'll let data flow between the producer (if still connected)
1911 * to the consumer.
1912 */
1913 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001914 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001915 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001916
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001917 /* check if it is wise to enable kernel splicing to forward response data */
1918 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1919 s->rep->to_forward &&
1920 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001921 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001922 (pipes_used < global.maxpipes) &&
1923 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1924 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1925 (s->rep->flags & BF_STREAMER_FAST)))) {
1926 s->rep->flags |= BF_KERN_SPLICING;
1927 }
1928
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001929 /* reflect what the L7 analysers have seen last */
1930 rpf_last = s->rep->flags;
1931
1932 /*
1933 * Now forward all shutdown requests between both sides of the buffer
1934 */
1935
1936 /*
1937 * FIXME: this is probably where we should produce error responses.
1938 */
1939
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001940 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001941 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1942 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001943 buffer_shutw_now(s->rep);
1944
1945 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001946 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 +01001947 s->rep->cons->shutw(s->rep->cons);
1948
1949 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001950 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1951 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001952 buffer_shutr_now(s->rep);
1953
1954 /* shutdown(read) pending */
1955 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1956 s->rep->prod->shutr(s->rep->prod);
1957
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001958 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001959 goto resync_stream_interface;
1960
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001961 if (s->req->flags != rqf_last)
1962 goto resync_request;
1963
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001964 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001965 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001966
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001967 /* we're interested in getting wakeups again */
1968 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1969 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1970
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001971 /* This is needed only when debugging is enabled, to indicate
1972 * client-side or server-side close. Please note that in the unlikely
1973 * event where both sides would close at once, the sequence is reported
1974 * on the server side first.
1975 */
1976 if (unlikely((global.mode & MODE_DEBUG) &&
1977 (!(global.mode & MODE_QUIET) ||
1978 (global.mode & MODE_VERBOSE)))) {
1979 int len;
1980
1981 if (s->si[1].state == SI_ST_CLO &&
1982 s->si[1].prev_state == SI_ST_EST) {
1983 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1984 s->uniq_id, s->be->id,
1985 (unsigned short)s->si[0].fd,
1986 (unsigned short)s->si[1].fd);
1987 write(1, trash, len);
1988 }
1989
1990 if (s->si[0].state == SI_ST_CLO &&
1991 s->si[0].prev_state == SI_ST_EST) {
1992 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1993 s->uniq_id, s->be->id,
1994 (unsigned short)s->si[0].fd,
1995 (unsigned short)s->si[1].fd);
1996 write(1, trash, len);
1997 }
1998 }
1999
2000 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2001 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2002
2003 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2004 session_process_counters(s);
2005
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002006 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002007 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002008
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002009 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002010 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002011
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002012 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2013 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 +01002014 s->si[0].prev_state = s->si[0].state;
2015 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002016 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2017 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002018
2019 /* Trick: if a request is being waiting for the server to respond,
2020 * and if we know the server can timeout, we don't want the timeout
2021 * to expire on the client side first, but we're still interested
2022 * in passing data from the client to the server (eg: POST). Thus,
2023 * we can cancel the client's request timeout if the server's
2024 * request timeout is set and the server has not yet sent a response.
2025 */
2026
Willy Tarreau520d95e2009-09-19 21:04:57 +02002027 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002028 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2029 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002030 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002031 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002032
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002033 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002034 * Note that this one may wake the task up again.
2035 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002036 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2037 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2038 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2039 s->req->cons->target.ptr.a->fct(s->req->cons);
2040 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2041 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002042 if (task_in_rq(t)) {
2043 /* If we woke up, we don't want to requeue the
2044 * task to the wait queue, but rather requeue
2045 * it into the runqueue ASAP.
2046 */
2047 t->expire = TICK_ETERNITY;
2048 return t;
2049 }
2050 }
2051
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002052 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2053 tick_first(s->rep->rex, s->rep->wex));
2054 if (s->req->analysers)
2055 t->expire = tick_first(t->expire, s->req->analyse_exp);
2056
2057 if (s->si[0].exp)
2058 t->expire = tick_first(t->expire, s->si[0].exp);
2059
2060 if (s->si[1].exp)
2061 t->expire = tick_first(t->expire, s->si[1].exp);
2062
2063#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002064 fprintf(stderr,
2065 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2066 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2067 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2068 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 +01002069#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002070
2071#ifdef DEBUG_DEV
2072 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002073 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002074 ABORT_NOW();
2075#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002076 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002077 }
2078
2079 s->fe->feconn--;
2080 if (s->flags & SN_BE_ASSIGNED)
2081 s->be->beconn--;
2082 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002083 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002084 s->listener->nbconn--;
2085 if (s->listener->state == LI_FULL &&
2086 s->listener->nbconn < s->listener->maxconn) {
2087 /* we should reactivate the listener */
2088 EV_FD_SET(s->listener->fd, DIR_RD);
2089 s->listener->state = LI_READY;
2090 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002091
2092 if (unlikely((global.mode & MODE_DEBUG) &&
2093 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2094 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002095 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002096 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002097 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002098 write(1, trash, len);
2099 }
2100
2101 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2102 session_process_counters(s);
2103
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002104 if (s->txn.status) {
2105 int n;
2106
2107 n = s->txn.status / 100;
2108 if (n < 1 || n > 5)
2109 n = 0;
2110
2111 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002112 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002113
Willy Tarreau24657792010-02-26 10:30:28 +01002114 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002115 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002116 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002117 }
2118
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002119 /* let's do a final log if we need it */
2120 if (s->logs.logwait &&
2121 !(s->flags & SN_MONITOR) &&
2122 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002123 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002124 }
2125
2126 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002127 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002128 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002129 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002130 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002131}
2132
Willy Tarreau7c669d72008-06-20 15:04:11 +02002133/*
2134 * This function adjusts sess->srv_conn and maintains the previous and new
2135 * server's served session counts. Setting newsrv to NULL is enough to release
2136 * current connection slot. This function also notifies any LB algo which might
2137 * expect to be informed about any change in the number of active sessions on a
2138 * server.
2139 */
2140void sess_change_server(struct session *sess, struct server *newsrv)
2141{
2142 if (sess->srv_conn == newsrv)
2143 return;
2144
2145 if (sess->srv_conn) {
2146 sess->srv_conn->served--;
2147 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2148 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002149 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002150 }
2151
2152 if (newsrv) {
2153 newsrv->served++;
2154 if (newsrv->proxy->lbprm.server_take_conn)
2155 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002156 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002157 }
2158}
2159
Willy Tarreau84455332009-03-15 22:34:05 +01002160/* Handle server-side errors for default protocols. It is called whenever a a
2161 * connection setup is aborted or a request is aborted in queue. It sets the
2162 * session termination flags so that the caller does not have to worry about
2163 * them. It's installed as ->srv_error for the server-side stream_interface.
2164 */
2165void default_srv_error(struct session *s, struct stream_interface *si)
2166{
2167 int err_type = si->err_type;
2168 int err = 0, fin = 0;
2169
2170 if (err_type & SI_ET_QUEUE_ABRT) {
2171 err = SN_ERR_CLICL;
2172 fin = SN_FINST_Q;
2173 }
2174 else if (err_type & SI_ET_CONN_ABRT) {
2175 err = SN_ERR_CLICL;
2176 fin = SN_FINST_C;
2177 }
2178 else if (err_type & SI_ET_QUEUE_TO) {
2179 err = SN_ERR_SRVTO;
2180 fin = SN_FINST_Q;
2181 }
2182 else if (err_type & SI_ET_QUEUE_ERR) {
2183 err = SN_ERR_SRVCL;
2184 fin = SN_FINST_Q;
2185 }
2186 else if (err_type & SI_ET_CONN_TO) {
2187 err = SN_ERR_SRVTO;
2188 fin = SN_FINST_C;
2189 }
2190 else if (err_type & SI_ET_CONN_ERR) {
2191 err = SN_ERR_SRVCL;
2192 fin = SN_FINST_C;
2193 }
2194 else /* SI_ET_CONN_OTHER and others */ {
2195 err = SN_ERR_INTERNAL;
2196 fin = SN_FINST_C;
2197 }
2198
2199 if (!(s->flags & SN_ERR_MASK))
2200 s->flags |= err;
2201 if (!(s->flags & SN_FINST_MASK))
2202 s->flags |= fin;
2203}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002204
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002205
Willy Tarreau8b22a712010-06-18 17:46:06 +02002206/************************************************************************/
2207/* All supported ACL keywords must be declared here. */
2208/************************************************************************/
2209
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002210/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2211static int
2212acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2213{
2214 test->flags = ACL_TEST_F_VOL_TEST;
2215 test->i = 0;
2216 if (ts != NULL) {
2217 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2218 if (!ptr)
2219 return 0; /* parameter not stored */
2220 test->i = stktable_data_cast(ptr, gpc0);
2221 }
2222 return 1;
2223}
2224
2225/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002226 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002227 */
2228static int
Willy Tarreau56123282010-08-06 19:06:56 +02002229acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2230 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002231{
Willy Tarreau56123282010-08-06 19:06:56 +02002232 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002233 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002234 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002235}
2236
2237/* set test->i to the General Purpose Counter 0 value from the session's tracked
2238 * backend counters.
2239 */
2240static int
Willy Tarreau56123282010-08-06 19:06:56 +02002241acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2242 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002243{
Willy Tarreau56123282010-08-06 19:06:56 +02002244 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002245 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002246 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002247}
2248
2249/* set test->i to the General Purpose Counter 0 value from the session's source
2250 * address in the table pointed to by expr.
2251 */
2252static int
2253acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2254 struct acl_expr *expr, struct acl_test *test)
2255{
2256 struct stktable_key *key;
2257
David du Colombier4f92d322011-03-24 11:09:31 +01002258 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002259 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002260 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002261
2262 if (expr->arg_len)
2263 px = find_stktable(expr->arg.str);
2264
2265 if (!px)
2266 return 0; /* table not found */
2267
2268 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2269}
2270
2271/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2272 * return it into test->i.
2273 */
2274static int
2275acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2276{
2277 test->flags = ACL_TEST_F_VOL_TEST;
2278 test->i = 0;
2279 if (ts != NULL) {
2280 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2281 if (!ptr)
2282 return 0; /* parameter not stored */
2283 test->i = ++stktable_data_cast(ptr, gpc0);
2284 }
2285 return 1;
2286}
2287
2288/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002289 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002290 */
2291static int
Willy Tarreau56123282010-08-06 19:06:56 +02002292acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2293 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002294{
Willy Tarreau56123282010-08-06 19:06:56 +02002295 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002296 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002297 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002298}
2299
2300/* Increment the General Purpose Counter 0 value from the session's tracked
2301 * backend counters and return it into test->i.
2302 */
2303static int
Willy Tarreau56123282010-08-06 19:06:56 +02002304acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2305 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002306{
Willy Tarreau56123282010-08-06 19:06:56 +02002307 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002308 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002309 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002310}
2311
2312/* Increment the General Purpose Counter 0 value from the session's source
2313 * address in the table pointed to by expr, and return it into test->i.
2314 */
2315static int
2316acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2317 struct acl_expr *expr, struct acl_test *test)
2318{
2319 struct stktable_key *key;
2320
David du Colombier4f92d322011-03-24 11:09:31 +01002321 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002322 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002323 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002324
2325 if (expr->arg_len)
2326 px = find_stktable(expr->arg.str);
2327
2328 if (!px)
2329 return 0; /* table not found */
2330
2331 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2332}
2333
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002334/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2335static int
2336acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2337{
2338 test->flags = ACL_TEST_F_VOL_TEST;
2339 test->i = 0;
2340 if (ts != NULL) {
2341 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2342 if (!ptr)
2343 return 0; /* parameter not stored */
2344 test->i = stktable_data_cast(ptr, conn_cnt);
2345 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002346 return 1;
2347}
2348
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002349/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002350static int
Willy Tarreau56123282010-08-06 19:06:56 +02002351acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2352 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002353{
Willy Tarreau56123282010-08-06 19:06:56 +02002354 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002355 return 0;
2356
Willy Tarreau56123282010-08-06 19:06:56 +02002357 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002358}
2359
2360/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2361static int
Willy Tarreau56123282010-08-06 19:06:56 +02002362acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2363 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002364{
Willy Tarreau56123282010-08-06 19:06:56 +02002365 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002366 return 0;
2367
Willy Tarreau56123282010-08-06 19:06:56 +02002368 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002369}
2370
2371/* set test->i to the cumulated number of connections from the session's source
2372 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002373 */
2374static int
2375acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2376 struct acl_expr *expr, struct acl_test *test)
2377{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002378 struct stktable_key *key;
2379
David du Colombier4f92d322011-03-24 11:09:31 +01002380 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002381 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002382 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002383
2384 if (expr->arg_len)
2385 px = find_stktable(expr->arg.str);
2386
2387 if (!px)
2388 return 0; /* table not found */
2389
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002390 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002391}
2392
Willy Tarreau91c43d72010-06-20 11:19:22 +02002393/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2394static int
2395acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2396{
2397 test->flags = ACL_TEST_F_VOL_TEST;
2398 test->i = 0;
2399 if (ts != NULL) {
2400 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2401 if (!ptr)
2402 return 0; /* parameter not stored */
2403 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2404 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2405 }
2406 return 1;
2407}
2408
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002409/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002410 * the configured period.
2411 */
2412static int
Willy Tarreau56123282010-08-06 19:06:56 +02002413acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2414 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002415{
Willy Tarreau56123282010-08-06 19:06:56 +02002416 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002417 return 0;
2418
Willy Tarreau56123282010-08-06 19:06:56 +02002419 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002420}
2421
2422/* set test->i to the connection rate from the session's tracked BE counters over
2423 * the configured period.
2424 */
2425static int
Willy Tarreau56123282010-08-06 19:06:56 +02002426acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2427 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002428{
Willy Tarreau56123282010-08-06 19:06:56 +02002429 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002430 return 0;
2431
Willy Tarreau56123282010-08-06 19:06:56 +02002432 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002433}
2434
2435/* set test->i to the connection rate from the session's source address in the
2436 * table pointed to by expr, over the configured period.
2437 */
2438static int
2439acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2440 struct acl_expr *expr, struct acl_test *test)
2441{
2442 struct stktable_key *key;
2443
David du Colombier4f92d322011-03-24 11:09:31 +01002444 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002445 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002446 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002447
2448 if (expr->arg_len)
2449 px = find_stktable(expr->arg.str);
2450
2451 if (!px)
2452 return 0; /* table not found */
2453
2454 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2455}
2456
Willy Tarreau8b22a712010-06-18 17:46:06 +02002457/* set test->i to the number of connections from the session's source address
2458 * in the table pointed to by expr, after updating it.
2459 */
2460static int
2461acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2462 struct acl_expr *expr, struct acl_test *test)
2463{
2464 struct stksess *ts;
2465 struct stktable_key *key;
2466 void *ptr;
2467
David du Colombier4f92d322011-03-24 11:09:31 +01002468 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002469 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002470 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002471
2472 if (expr->arg_len)
2473 px = find_stktable(expr->arg.str);
2474
2475 if (!px)
2476 return 0; /* table not found */
2477
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002478 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2479 /* entry does not exist and could not be created */
2480 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002481
2482 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2483 if (!ptr)
2484 return 0; /* parameter not stored in this table */
2485
2486 test->i = ++stktable_data_cast(ptr, conn_cnt);
2487 test->flags = ACL_TEST_F_VOL_TEST;
2488 return 1;
2489}
2490
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002491/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2492static int
2493acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2494{
2495 test->flags = ACL_TEST_F_VOL_TEST;
2496 test->i = 0;
2497
2498 if (ts != NULL) {
2499 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2500 if (!ptr)
2501 return 0; /* parameter not stored */
2502 test->i = stktable_data_cast(ptr, conn_cur);
2503 }
2504 return 1;
2505}
2506
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002507/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002508static int
Willy Tarreau56123282010-08-06 19:06:56 +02002509acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2510 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002511{
Willy Tarreau56123282010-08-06 19:06:56 +02002512 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002513 return 0;
2514
Willy Tarreau56123282010-08-06 19:06:56 +02002515 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002516}
2517
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002518/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2519static int
Willy Tarreau56123282010-08-06 19:06:56 +02002520acl_fetch_sc2_conn_cur(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->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002524 return 0;
2525
Willy Tarreau56123282010-08-06 19:06:56 +02002526 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002527}
2528
Willy Tarreau38285c12010-06-18 16:35:43 +02002529/* set test->i to the number of concurrent connections from the session's source
2530 * address in the table pointed to by expr.
2531 */
2532static int
2533acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2534 struct acl_expr *expr, struct acl_test *test)
2535{
Willy Tarreau38285c12010-06-18 16:35:43 +02002536 struct stktable_key *key;
2537
David du Colombier4f92d322011-03-24 11:09:31 +01002538 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002539 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002540 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002541
2542 if (expr->arg_len)
2543 px = find_stktable(expr->arg.str);
2544
2545 if (!px)
2546 return 0; /* table not found */
2547
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002548 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002549}
2550
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002551/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2552static int
2553acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2554{
2555 test->flags = ACL_TEST_F_VOL_TEST;
2556 test->i = 0;
2557 if (ts != NULL) {
2558 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2559 if (!ptr)
2560 return 0; /* parameter not stored */
2561 test->i = stktable_data_cast(ptr, sess_cnt);
2562 }
2563 return 1;
2564}
2565
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002566/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002567static int
Willy Tarreau56123282010-08-06 19:06:56 +02002568acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2569 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002570{
Willy Tarreau56123282010-08-06 19:06:56 +02002571 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002572 return 0;
2573
Willy Tarreau56123282010-08-06 19:06:56 +02002574 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002575}
2576
2577/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2578static int
Willy Tarreau56123282010-08-06 19:06:56 +02002579acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2580 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002581{
Willy Tarreau56123282010-08-06 19:06:56 +02002582 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002583 return 0;
2584
Willy Tarreau56123282010-08-06 19:06:56 +02002585 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002586}
2587
2588/* set test->i to the cumulated number of session from the session's source
2589 * address in the table pointed to by expr.
2590 */
2591static int
2592acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2593 struct acl_expr *expr, struct acl_test *test)
2594{
2595 struct stktable_key *key;
2596
David du Colombier4f92d322011-03-24 11:09:31 +01002597 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002598 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002599 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002600
2601 if (expr->arg_len)
2602 px = find_stktable(expr->arg.str);
2603
2604 if (!px)
2605 return 0; /* table not found */
2606
2607 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2608}
2609
Willy Tarreau91c43d72010-06-20 11:19:22 +02002610/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2611static int
2612acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2613{
2614 test->flags = ACL_TEST_F_VOL_TEST;
2615 test->i = 0;
2616 if (ts != NULL) {
2617 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2618 if (!ptr)
2619 return 0; /* parameter not stored */
2620 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2621 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2622 }
2623 return 1;
2624}
2625
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002626/* set test->i to the session rate from the session's tracked FE counters over
2627 * the configured period.
2628 */
2629static int
Willy Tarreau56123282010-08-06 19:06:56 +02002630acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2631 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002632{
Willy Tarreau56123282010-08-06 19:06:56 +02002633 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002634 return 0;
2635
Willy Tarreau56123282010-08-06 19:06:56 +02002636 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002637}
2638
2639/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002640 * the configured period.
2641 */
2642static int
Willy Tarreau56123282010-08-06 19:06:56 +02002643acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2644 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002645{
Willy Tarreau56123282010-08-06 19:06:56 +02002646 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002647 return 0;
2648
Willy Tarreau56123282010-08-06 19:06:56 +02002649 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002650}
2651
2652/* set test->i to the session rate from the session's source address in the
2653 * table pointed to by expr, over the configured period.
2654 */
2655static int
2656acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2657 struct acl_expr *expr, struct acl_test *test)
2658{
2659 struct stktable_key *key;
2660
David du Colombier4f92d322011-03-24 11:09:31 +01002661 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002662 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002663 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002664
2665 if (expr->arg_len)
2666 px = find_stktable(expr->arg.str);
2667
2668 if (!px)
2669 return 0; /* table not found */
2670
2671 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2672}
2673
Willy Tarreauda7ff642010-06-23 11:44:09 +02002674/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2675static int
2676acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2677{
2678 test->flags = ACL_TEST_F_VOL_TEST;
2679 test->i = 0;
2680 if (ts != NULL) {
2681 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2682 if (!ptr)
2683 return 0; /* parameter not stored */
2684 test->i = stktable_data_cast(ptr, http_req_cnt);
2685 }
2686 return 1;
2687}
2688
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002689/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002690static int
Willy Tarreau56123282010-08-06 19:06:56 +02002691acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2692 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002693{
Willy Tarreau56123282010-08-06 19:06:56 +02002694 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002695 return 0;
2696
Willy Tarreau56123282010-08-06 19:06:56 +02002697 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002698}
2699
2700/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2701static int
Willy Tarreau56123282010-08-06 19:06:56 +02002702acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2703 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002704{
Willy Tarreau56123282010-08-06 19:06:56 +02002705 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002706 return 0;
2707
Willy Tarreau56123282010-08-06 19:06:56 +02002708 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002709}
2710
2711/* set test->i to the cumulated number of session from the session's source
2712 * address in the table pointed to by expr.
2713 */
2714static int
2715acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002716 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717{
2718 struct stktable_key *key;
2719
David du Colombier4f92d322011-03-24 11:09:31 +01002720 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002721 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002722 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002723
2724 if (expr->arg_len)
2725 px = find_stktable(expr->arg.str);
2726
2727 if (!px)
2728 return 0; /* table not found */
2729
2730 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2731}
2732
2733/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2734static int
2735acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2736{
2737 test->flags = ACL_TEST_F_VOL_TEST;
2738 test->i = 0;
2739 if (ts != NULL) {
2740 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2741 if (!ptr)
2742 return 0; /* parameter not stored */
2743 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2744 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2745 }
2746 return 1;
2747}
2748
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002749/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002750 * the configured period.
2751 */
2752static int
Willy Tarreau56123282010-08-06 19:06:56 +02002753acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2754 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002755{
Willy Tarreau56123282010-08-06 19:06:56 +02002756 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002757 return 0;
2758
Willy Tarreau56123282010-08-06 19:06:56 +02002759 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002760}
2761
2762/* set test->i to the session rate from the session's tracked BE counters over
2763 * the configured period.
2764 */
2765static int
Willy Tarreau56123282010-08-06 19:06:56 +02002766acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2767 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002768{
Willy Tarreau56123282010-08-06 19:06:56 +02002769 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002770 return 0;
2771
Willy Tarreau56123282010-08-06 19:06:56 +02002772 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002773}
2774
2775/* set test->i to the session rate from the session's source address in the
2776 * table pointed to by expr, over the configured period.
2777 */
2778static int
2779acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002780 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002781{
2782 struct stktable_key *key;
2783
David du Colombier4f92d322011-03-24 11:09:31 +01002784 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002785 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002786 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002787
2788 if (expr->arg_len)
2789 px = find_stktable(expr->arg.str);
2790
2791 if (!px)
2792 return 0; /* table not found */
2793
2794 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2795}
2796
2797/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2798static int
2799acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2800{
2801 test->flags = ACL_TEST_F_VOL_TEST;
2802 test->i = 0;
2803 if (ts != NULL) {
2804 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2805 if (!ptr)
2806 return 0; /* parameter not stored */
2807 test->i = stktable_data_cast(ptr, http_err_cnt);
2808 }
2809 return 1;
2810}
2811
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002812/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002813static int
Willy Tarreau56123282010-08-06 19:06:56 +02002814acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2815 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002816{
Willy Tarreau56123282010-08-06 19:06:56 +02002817 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002818 return 0;
2819
Willy Tarreau56123282010-08-06 19:06:56 +02002820 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002821}
2822
2823/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2824static int
Willy Tarreau56123282010-08-06 19:06:56 +02002825acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2826 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002827{
Willy Tarreau56123282010-08-06 19:06:56 +02002828 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002829 return 0;
2830
Willy Tarreau56123282010-08-06 19:06:56 +02002831 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002832}
2833
2834/* set test->i to the cumulated number of session from the session's source
2835 * address in the table pointed to by expr.
2836 */
2837static int
2838acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002839 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840{
2841 struct stktable_key *key;
2842
David du Colombier4f92d322011-03-24 11:09:31 +01002843 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002844 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002845 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002846
2847 if (expr->arg_len)
2848 px = find_stktable(expr->arg.str);
2849
2850 if (!px)
2851 return 0; /* table not found */
2852
2853 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2854}
2855
2856/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2857static int
2858acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2859{
2860 test->flags = ACL_TEST_F_VOL_TEST;
2861 test->i = 0;
2862 if (ts != NULL) {
2863 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2864 if (!ptr)
2865 return 0; /* parameter not stored */
2866 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2867 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2868 }
2869 return 1;
2870}
2871
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002872/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002873 * the configured period.
2874 */
2875static int
Willy Tarreau56123282010-08-06 19:06:56 +02002876acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2877 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002878{
Willy Tarreau56123282010-08-06 19:06:56 +02002879 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002880 return 0;
2881
Willy Tarreau56123282010-08-06 19:06:56 +02002882 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002883}
2884
2885/* set test->i to the session rate from the session's tracked BE counters over
2886 * the configured period.
2887 */
2888static int
Willy Tarreau56123282010-08-06 19:06:56 +02002889acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2890 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002891{
Willy Tarreau56123282010-08-06 19:06:56 +02002892 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002893 return 0;
2894
Willy Tarreau56123282010-08-06 19:06:56 +02002895 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002896}
2897
2898/* set test->i to the session rate from the session's source address in the
2899 * table pointed to by expr, over the configured period.
2900 */
2901static int
2902acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2903 struct acl_expr *expr, struct acl_test *test)
2904{
2905 struct stktable_key *key;
2906
David du Colombier4f92d322011-03-24 11:09:31 +01002907 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002908 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002909 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002910
2911 if (expr->arg_len)
2912 px = find_stktable(expr->arg.str);
2913
2914 if (!px)
2915 return 0; /* table not found */
2916
2917 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2918}
2919
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002920/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2921static int
2922acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2923{
2924 test->flags = ACL_TEST_F_VOL_TEST;
2925 test->i = 0;
2926
2927 if (ts != NULL) {
2928 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2929 if (!ptr)
2930 return 0; /* parameter not stored */
2931 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2932 }
2933 return 1;
2934}
2935
2936/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002937 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002938 */
2939static int
Willy Tarreau56123282010-08-06 19:06:56 +02002940acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2941 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002942{
Willy Tarreau56123282010-08-06 19:06:56 +02002943 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002944 return 0;
2945
Willy Tarreau56123282010-08-06 19:06:56 +02002946 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002947}
2948
2949/* set test->i to the number of kbytes received from clients according to the
2950 * session's tracked BE counters.
2951 */
2952static int
Willy Tarreau56123282010-08-06 19:06:56 +02002953acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2954 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002955{
Willy Tarreau56123282010-08-06 19:06:56 +02002956 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002957 return 0;
2958
Willy Tarreau56123282010-08-06 19:06:56 +02002959 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002960}
2961
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002962/* set test->i to the number of kbytes received from the session's source
2963 * address in the table pointed to by expr.
2964 */
2965static int
2966acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2967 struct acl_expr *expr, struct acl_test *test)
2968{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002969 struct stktable_key *key;
2970
David du Colombier4f92d322011-03-24 11:09:31 +01002971 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002972 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002973 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002974
2975 if (expr->arg_len)
2976 px = find_stktable(expr->arg.str);
2977
2978 if (!px)
2979 return 0; /* table not found */
2980
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002981 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2982}
2983
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002984/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2985 * configured period.
2986 */
2987static int
2988acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2989{
2990 test->flags = ACL_TEST_F_VOL_TEST;
2991 test->i = 0;
2992 if (ts != NULL) {
2993 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2994 if (!ptr)
2995 return 0; /* parameter not stored */
2996 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2997 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2998 }
2999 return 1;
3000}
3001
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003002/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003003 * counters over the configured period.
3004 */
3005static int
Willy Tarreau56123282010-08-06 19:06:56 +02003006acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3007 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003008{
Willy Tarreau56123282010-08-06 19:06:56 +02003009 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003010 return 0;
3011
Willy Tarreau56123282010-08-06 19:06:56 +02003012 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003013}
3014
3015/* set test->i to the bytes rate from clients from the session's tracked BE
3016 * counters over the configured period.
3017 */
3018static int
Willy Tarreau56123282010-08-06 19:06:56 +02003019acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3020 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003021{
Willy Tarreau56123282010-08-06 19:06:56 +02003022 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003023 return 0;
3024
Willy Tarreau56123282010-08-06 19:06:56 +02003025 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003026}
3027
3028/* set test->i to the bytes rate from clients from the session's source address
3029 * in the table pointed to by expr, over the configured period.
3030 */
3031static int
3032acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003033 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003034{
3035 struct stktable_key *key;
3036
David du Colombier4f92d322011-03-24 11:09:31 +01003037 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003038 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003039 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003040
3041 if (expr->arg_len)
3042 px = find_stktable(expr->arg.str);
3043
3044 if (!px)
3045 return 0; /* table not found */
3046
3047 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3048}
3049
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003050/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3051static int
3052acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3053{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003054 test->flags = ACL_TEST_F_VOL_TEST;
3055 test->i = 0;
3056
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003057 if (ts != NULL) {
3058 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003059 if (!ptr)
3060 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003061 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003062 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003063 return 1;
3064}
3065
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003066/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003067 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003068 */
3069static int
Willy Tarreau56123282010-08-06 19:06:56 +02003070acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3071 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003072{
Willy Tarreau56123282010-08-06 19:06:56 +02003073 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003074 return 0;
3075
Willy Tarreau56123282010-08-06 19:06:56 +02003076 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003077}
3078
3079/* set test->i to the number of kbytes sent to clients according to the session's
3080 * tracked BE counters.
3081 */
3082static int
Willy Tarreau56123282010-08-06 19:06:56 +02003083acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3084 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003085{
Willy Tarreau56123282010-08-06 19:06:56 +02003086 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003087 return 0;
3088
Willy Tarreau56123282010-08-06 19:06:56 +02003089 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003090}
3091
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003092/* set test->i to the number of kbytes sent to the session's source address in
3093 * the table pointed to by expr.
3094 */
3095static int
3096acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3097 struct acl_expr *expr, struct acl_test *test)
3098{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003099 struct stktable_key *key;
3100
David du Colombier4f92d322011-03-24 11:09:31 +01003101 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003102 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003103 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003104
3105 if (expr->arg_len)
3106 px = find_stktable(expr->arg.str);
3107
3108 if (!px)
3109 return 0; /* table not found */
3110
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003111 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003112}
3113
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003114/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3115 * configured period.
3116 */
3117static int
3118acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3119{
3120 test->flags = ACL_TEST_F_VOL_TEST;
3121 test->i = 0;
3122 if (ts != NULL) {
3123 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3124 if (!ptr)
3125 return 0; /* parameter not stored */
3126 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3127 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3128 }
3129 return 1;
3130}
3131
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003132/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003133 * over the configured period.
3134 */
3135static int
Willy Tarreau56123282010-08-06 19:06:56 +02003136acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3137 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003138{
Willy Tarreau56123282010-08-06 19:06:56 +02003139 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003140 return 0;
3141
Willy Tarreau56123282010-08-06 19:06:56 +02003142 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003143}
3144
3145/* set test->i to the bytes rate to clients from the session's tracked BE counters
3146 * over the configured period.
3147 */
3148static int
Willy Tarreau56123282010-08-06 19:06:56 +02003149acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3150 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003151{
Willy Tarreau56123282010-08-06 19:06:56 +02003152 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003153 return 0;
3154
Willy Tarreau56123282010-08-06 19:06:56 +02003155 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003156}
3157
3158/* set test->i to the bytes rate to client from the session's source address in
3159 * the table pointed to by expr, over the configured period.
3160 */
3161static int
3162acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003163 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003164{
3165 struct stktable_key *key;
3166
David du Colombier4f92d322011-03-24 11:09:31 +01003167 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003168 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003169 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003170
3171 if (expr->arg_len)
3172 px = find_stktable(expr->arg.str);
3173
3174 if (!px)
3175 return 0; /* table not found */
3176
3177 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3178}
3179
Willy Tarreauc735a072011-03-29 00:57:02 +02003180/* set test->i to the number of used entries in the table pointed to by expr. */
3181static int
3182acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3183 struct acl_expr *expr, struct acl_test *test)
3184{
3185 if (expr->arg_len)
3186 px = find_stktable(expr->arg.str);
3187
3188 if (!px)
3189 return 0; /* table not found */
3190
3191 test->flags = ACL_TEST_F_VOL_TEST;
3192 test->i = px->table.current;
3193 return 1;
3194}
3195
3196/* set test->i to the number of free entries in the table pointed to by expr. */
3197static int
3198acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3199 struct acl_expr *expr, struct acl_test *test)
3200{
3201 if (expr->arg_len)
3202 px = find_stktable(expr->arg.str);
3203
3204 if (!px)
3205 return 0; /* table not found */
3206
3207 test->flags = ACL_TEST_F_VOL_TEST;
3208 test->i = px->table.size - px->table.current;
3209 return 1;
3210}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003211
3212/* Note: must not be declared <const> as its list will be overwritten */
3213static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003214 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3215 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3216 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3217 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3218 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3219 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3220 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3221 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3222 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3223 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3224 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3225 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3226 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3227 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3228 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3229 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3230 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3231 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3232 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3233 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3234 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3235 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3236 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3237 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3238 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3239 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3240 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3241 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3242 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3243 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3244 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3245 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3246 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3247 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3248 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3249 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3250 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3251 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3252 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3253 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3254 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3255 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3256 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3257 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3258 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3259 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreauc735a072011-03-29 00:57:02 +02003260 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3261 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003262 { NULL, NULL, NULL, NULL },
3263}};
3264
3265
Willy Tarreau56123282010-08-06 19:06:56 +02003266/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003267 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3268 * <prm> struct is fed with the table name if any. If unspecified, the caller
3269 * will assume that the current proxy's table is used.
3270 */
3271int parse_track_counters(char **args, int *arg,
3272 int section_type, struct proxy *curpx,
3273 struct track_ctr_prm *prm,
3274 struct proxy *defpx, char *err, int errlen)
3275{
3276 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003277 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003278
Willy Tarreau56123282010-08-06 19:06:56 +02003279 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003280 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003281 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003282 */
3283 while (args[*arg]) {
3284 if (strcmp(args[*arg], "src") == 0) {
3285 prm->type = STKTABLE_TYPE_IP;
3286 pattern_type = 1;
3287 }
3288 else if (strcmp(args[*arg], "table") == 0) {
3289 if (!args[*arg + 1]) {
3290 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003291 "missing table for %s in %s '%s'.",
3292 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003293 return -1;
3294 }
3295 /* we copy the table name for now, it will be resolved later */
3296 prm->table.n = strdup(args[*arg + 1]);
3297 (*arg)++;
3298 }
3299 else {
3300 /* unhandled keywords are handled by the caller */
3301 break;
3302 }
3303 (*arg)++;
3304 }
3305
3306 if (!pattern_type) {
3307 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003308 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3309 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003310 return -1;
3311 }
3312
3313 return 0;
3314}
3315
Willy Tarreau8b22a712010-06-18 17:46:06 +02003316__attribute__((constructor))
3317static void __session_init(void)
3318{
3319 acl_register_keywords(&acl_kws);
3320}
3321
Willy Tarreaubaaee002006-06-26 02:48:02 +02003322/*
3323 * Local variables:
3324 * c-indent-level: 8
3325 * c-basic-offset: 8
3326 * End:
3327 */