blob: e5ac4eedd50e03908441ef61c2502fcb14beef35 [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
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200204 session_init_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 Tarreau2b154922011-07-22 17:36:27 +0200313 if (ret < 0 && s->fe->mode == PR_MODE_HTTP) {
314 /* critical error, no more memory, try to emit a 500 response */
315 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
316 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
317 }
318
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100319 if (fdtab[cfd].state != FD_STCLOSE)
320 fd_delete(cfd);
321 else
322 close(cfd);
323 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200324}
325
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326/*
327 * frees the context associated to a session. It must have been removed first.
328 */
Simon Hormandec5be42011-06-08 09:19:07 +0900329static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100331 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200332 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100333 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200334 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100335
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336 if (s->pend_pos)
337 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100338
Willy Tarreau827aee92011-03-10 16:55:02 +0100339 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100340 if (s->flags & SN_CURR_SESS) {
341 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100342 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100343 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100344 if (may_dequeue_tasks(target_srv(&s->target), s->be))
345 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100346 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100347
Willy Tarreau7c669d72008-06-20 15:04:11 +0200348 if (unlikely(s->srv_conn)) {
349 /* the session still has a reserved slot on a server, but
350 * it should normally be only the same as the one above,
351 * so this should not happen in fact.
352 */
353 sess_change_server(s, NULL);
354 }
355
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100356 if (s->req->pipe)
357 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100358
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100359 if (s->rep->pipe)
360 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100361
Willy Tarreau48d63db2008-08-03 17:41:33 +0200362 pool_free2(pool2_buffer, s->req);
363 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
Willy Tarreau46023632010-01-07 22:51:47 +0100365 http_end_txn(s);
366
Willy Tarreaua4cda672010-06-06 18:28:49 +0200367 for (i = 0; i < s->store_count; i++) {
368 if (!s->store[i].ts)
369 continue;
370 stksess_free(s->store[i].table, s->store[i].ts);
371 s->store[i].ts = NULL;
372 }
373
Willy Tarreau92fb9832007-10-16 17:34:28 +0200374 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200375 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100376 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
377 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100379
Willy Tarreau56123282010-08-06 19:06:56 +0200380 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200381 session_store_counters(s);
382
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100383 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100384 /* we have to unlink all watchers. We must not relink them if
385 * this session was the last one in the list.
386 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100387 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100388 LIST_INIT(&bref->users);
389 if (s->list.n != &sessions)
390 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100391 bref->ref = s->list.n;
392 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100393 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200394 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200395
396 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200397 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200398 pool_flush2(pool2_buffer);
399 pool_flush2(fe->hdr_idx_pool);
400 pool_flush2(pool2_requri);
401 pool_flush2(pool2_capture);
402 pool_flush2(pool2_session);
403 pool_flush2(fe->req_cap_pool);
404 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200405 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200406}
407
408
409/* perform minimal intializations, report 0 in case of error, 1 if OK. */
410int init_session()
411{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100412 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200413 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
414 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415}
416
Willy Tarreau30e71012007-11-26 20:15:35 +0100417void session_process_counters(struct session *s)
418{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100419 unsigned long long bytes;
420
Willy Tarreau30e71012007-11-26 20:15:35 +0100421 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100423 s->logs.bytes_in = s->req->total;
424 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100425 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100426
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100427 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100428
Willy Tarreau827aee92011-03-10 16:55:02 +0100429 if (target_srv(&s->target))
430 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200431
432 if (s->listener->counters)
433 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200434
Willy Tarreau56123282010-08-06 19:06:56 +0200435 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200436 void *ptr;
437
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_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200441 if (ptr)
442 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200443
Willy Tarreau56123282010-08-06 19:06:56 +0200444 ptr = stktable_data_ptr(s->stkctr2_table,
445 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200446 STKTABLE_DT_BYTES_IN_RATE);
447 if (ptr)
448 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200449 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200450 }
451
Willy Tarreau56123282010-08-06 19:06:56 +0200452 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200453 void *ptr;
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_CNT);
458 if (ptr)
459 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
460
Willy Tarreau56123282010-08-06 19:06:56 +0200461 ptr = stktable_data_ptr(s->stkctr1_table,
462 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200463 STKTABLE_DT_BYTES_IN_RATE);
464 if (ptr)
465 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200466 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200467 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100468 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469 }
470
Willy Tarreau30e71012007-11-26 20:15:35 +0100471 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100473 s->logs.bytes_out = s->rep->total;
474 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100475 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100477 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100478
Willy Tarreau827aee92011-03-10 16:55:02 +0100479 if (target_srv(&s->target))
480 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200481
482 if (s->listener->counters)
483 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200484
Willy Tarreau56123282010-08-06 19:06:56 +0200485 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200486 void *ptr;
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_CNT);
491 if (ptr)
492 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
493
Willy Tarreau56123282010-08-06 19:06:56 +0200494 ptr = stktable_data_ptr(s->stkctr2_table,
495 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200496 STKTABLE_DT_BYTES_OUT_RATE);
497 if (ptr)
498 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200499 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200500 }
501
Willy Tarreau56123282010-08-06 19:06:56 +0200502 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200503 void *ptr;
504
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_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200508 if (ptr)
509 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200510
Willy Tarreau56123282010-08-06 19:06:56 +0200511 ptr = stktable_data_ptr(s->stkctr1_table,
512 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200513 STKTABLE_DT_BYTES_OUT_RATE);
514 if (ptr)
515 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200516 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200517 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100518 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100519 }
520}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100522/* This function is called with (si->state == SI_ST_CON) meaning that a
523 * connection was attempted and that the file descriptor is already allocated.
524 * We must check for establishment, error and abort. Possible output states
525 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
526 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
527 * otherwise 1.
528 */
Simon Hormandec5be42011-06-08 09:19:07 +0900529static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100530{
531 struct buffer *req = si->ob;
532 struct buffer *rep = si->ib;
533
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534 /* If we got an error, or if nothing happened and the connection timed
535 * out, we must give up. The CER state handler will take care of retry
536 * attempts and error reports.
537 */
538 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100539 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200541 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 fd_delete(si->fd);
543
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200544 if (si->release)
545 si->release(si);
546
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 if (si->err_type)
548 return 0;
549
Willy Tarreau827aee92011-03-10 16:55:02 +0100550 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551 if (si->flags & SI_FL_ERR)
552 si->err_type = SI_ET_CONN_ERR;
553 else
554 si->err_type = SI_ET_CONN_TO;
555 return 0;
556 }
557
558 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200559 if (unlikely((rep->flags & BF_SHUTW) ||
560 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200561 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100562 s->be->options & PR_O_ABRT_CLOSE)))) {
563 /* give up */
564 si->shutw(si);
565 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100566 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200567 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100568 if (s->srv_error)
569 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570 return 1;
571 }
572
573 /* we need to wait a bit more if there was no activity either */
574 if (!(req->flags & BF_WRITE_ACTIVITY))
575 return 1;
576
577 /* OK, this means that a connection succeeded. The caller will be
578 * responsible for handling the transition from CON to EST.
579 */
580 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100581 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100582 si->state = SI_ST_EST;
583 si->err_type = SI_ET_NONE;
584 si->err_loc = NULL;
585 return 1;
586}
587
588/* This function is called with (si->state == SI_ST_CER) meaning that a
589 * previous connection attempt has failed and that the file descriptor
590 * has already been released. Possible causes include asynchronous error
591 * notification and time out. Possible output states are SI_ST_CLO when
592 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
593 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
594 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
595 * marked as in error state. It returns 0.
596 */
Simon Hormandec5be42011-06-08 09:19:07 +0900597static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598{
599 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100600 if (target_srv(&s->target)) {
601 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100602
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 if (s->flags & SN_CURR_SESS) {
604 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100605 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 }
608
609 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200610 si->conn_retries--;
611 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 if (!si->err_type) {
613 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100614 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100615 }
616
Willy Tarreau827aee92011-03-10 16:55:02 +0100617 if (target_srv(&s->target))
618 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100619 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100620 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100621 if (may_dequeue_tasks(target_srv(&s->target), s->be))
622 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623
624 /* shutw is enough so stop a connecting socket */
625 si->shutw(si);
626 si->ob->flags |= BF_WRITE_ERROR;
627 si->ib->flags |= BF_READ_ERROR;
628
629 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100630 if (s->srv_error)
631 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632 return 0;
633 }
634
635 /* If the "redispatch" option is set on the backend, we are allowed to
636 * retry on another server for the last retry. In order to achieve this,
637 * we must mark the session unassigned, and eventually clear the DIRECT
638 * bit to ignore any persistence cookie. We won't count a retry nor a
639 * redispatch yet, because this will depend on what server is selected.
640 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100642 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100643 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 if (may_dequeue_tasks(target_srv(&s->target), s->be))
645 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646
647 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100648 si->state = SI_ST_REQ;
649 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100650 if (target_srv(&s->target))
651 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100652 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 si->state = SI_ST_ASS;
654 }
655
656 if (si->flags & SI_FL_ERR) {
657 /* The error was an asynchronous connection error, and we will
658 * likely have to retry connecting to the same server, most
659 * likely leading to the same result. To avoid this, we wait
660 * one second before retrying.
661 */
662
663 if (!si->err_type)
664 si->err_type = SI_ET_CONN_ERR;
665
666 si->state = SI_ST_TAR;
667 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
668 return 0;
669 }
670 return 0;
671}
672
673/*
674 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200675 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
676 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 */
Simon Hormandec5be42011-06-08 09:19:07 +0900678static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679{
680 struct buffer *req = si->ob;
681 struct buffer *rep = si->ib;
682
Willy Tarreau827aee92011-03-10 16:55:02 +0100683 if (target_srv(&s->target))
684 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100685
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 /* if the user wants to log as soon as possible, without counting
688 * bytes from the server, then this is the right moment. */
689 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
690 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100691 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
694 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
696 /* reset hdr_idx which was already initialized by the request.
697 * right now, the http parser does it.
698 * hdr_idx_init(&s->txn.hdr_idx);
699 */
700 }
701
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200702 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200704 if (si->connect) {
705 /* real connections have timeouts */
706 req->wto = s->be->timeout.server;
707 rep->rto = s->be->timeout.server;
708 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 req->wex = TICK_ETERNITY;
710}
711
712/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
713 * Other input states are simply ignored.
714 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
715 * Flags must have previously been updated for timeouts and other conditions.
716 */
Simon Hormandec5be42011-06-08 09:19:07 +0900717static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718{
Willy Tarreau827aee92011-03-10 16:55:02 +0100719 struct server *srv = target_srv(&s->target);
720
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721 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",
722 now_ms, __FUNCTION__,
723 s,
724 s->req, s->rep,
725 s->req->rex, s->rep->wex,
726 s->req->flags, s->rep->flags,
727 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
728
729 if (si->state == SI_ST_ASS) {
730 /* Server assigned to connection request, we have to try to connect now */
731 int conn_err;
732
733 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100734 srv = target_srv(&s->target);
735
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 if (conn_err == SN_ERR_NONE) {
737 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100738 if (srv)
739 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 return;
741 }
742
743 /* We have received a synchronous error. We might have to
744 * abort, retry immediately or redispatch.
745 */
746 if (conn_err == SN_ERR_INTERNAL) {
747 if (!si->err_type) {
748 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100749 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 }
751
Willy Tarreau827aee92011-03-10 16:55:02 +0100752 if (srv)
753 srv_inc_sess_ctr(srv);
754 if (srv)
755 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100756 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757
758 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100759 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 if (may_dequeue_tasks(srv, s->be))
761 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100762
763 /* Failed and not retryable. */
764 si->shutr(si);
765 si->shutw(si);
766 si->ob->flags |= BF_WRITE_ERROR;
767
768 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
769
770 /* no session was ever accounted for this server */
771 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100772 if (s->srv_error)
773 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100774 return;
775 }
776
777 /* We are facing a retryable error, but we don't want to run a
778 * turn-around now, as the problem is likely a source port
779 * allocation problem, so we want to retry now.
780 */
781 si->state = SI_ST_CER;
782 si->flags &= ~SI_FL_ERR;
783 sess_update_st_cer(s, si);
784 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
785 return;
786 }
787 else if (si->state == SI_ST_QUE) {
788 /* connection request was queued, check for any update */
789 if (!s->pend_pos) {
790 /* The connection is not in the queue anymore. Either
791 * we have a server connection slot available and we
792 * go directly to the assigned state, or we need to
793 * load-balance first and go to the INI state.
794 */
795 si->exp = TICK_ETERNITY;
796 if (unlikely(!(s->flags & SN_ASSIGNED)))
797 si->state = SI_ST_REQ;
798 else {
799 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
800 si->state = SI_ST_ASS;
801 }
802 return;
803 }
804
805 /* Connection request still in queue... */
806 if (si->flags & SI_FL_EXP) {
807 /* ... and timeout expired */
808 si->exp = TICK_ETERNITY;
809 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100810 if (srv)
811 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100812 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813 si->shutr(si);
814 si->shutw(si);
815 si->ob->flags |= BF_WRITE_TIMEOUT;
816 if (!si->err_type)
817 si->err_type = SI_ET_QUEUE_TO;
818 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819 if (s->srv_error)
820 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100821 return;
822 }
823
824 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200825 if ((si->ob->flags & (BF_READ_ERROR)) ||
826 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200827 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100828 /* give up */
829 si->exp = TICK_ETERNITY;
830 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
831 si->shutr(si);
832 si->shutw(si);
833 si->err_type |= SI_ET_QUEUE_ABRT;
834 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100835 if (s->srv_error)
836 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100837 return;
838 }
839
840 /* Nothing changed */
841 return;
842 }
843 else if (si->state == SI_ST_TAR) {
844 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200845 if ((si->ob->flags & (BF_READ_ERROR)) ||
846 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200847 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848 /* give up */
849 si->exp = TICK_ETERNITY;
850 si->shutr(si);
851 si->shutw(si);
852 si->err_type |= SI_ET_CONN_ABRT;
853 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100854 if (s->srv_error)
855 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 return;
857 }
858
859 if (!(si->flags & SI_FL_EXP))
860 return; /* still in turn-around */
861
862 si->exp = TICK_ETERNITY;
863
864 /* we keep trying on the same server as long as the session is
865 * marked "assigned".
866 * FIXME: Should we force a redispatch attempt when the server is down ?
867 */
868 if (s->flags & SN_ASSIGNED)
869 si->state = SI_ST_ASS;
870 else
871 si->state = SI_ST_REQ;
872 return;
873 }
874}
875
Simon Hormandec5be42011-06-08 09:19:07 +0900876/* Set correct session termination flags in case no analyser has done it. It
877 * also counts a failed request if the server state has not reached the request
878 * stage.
879 */
880static void sess_set_term_flags(struct session *s)
881{
882 if (!(s->flags & SN_FINST_MASK)) {
883 if (s->si[1].state < SI_ST_REQ) {
884
885 s->fe->fe_counters.failed_req++;
886 if (s->listener->counters)
887 s->listener->counters->failed_req++;
888
889 s->flags |= SN_FINST_R;
890 }
891 else if (s->si[1].state == SI_ST_QUE)
892 s->flags |= SN_FINST_Q;
893 else if (s->si[1].state < SI_ST_EST)
894 s->flags |= SN_FINST_C;
895 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
896 s->flags |= SN_FINST_D;
897 else
898 s->flags |= SN_FINST_L;
899 }
900}
901
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902/* This function initiates a server connection request on a stream interface
903 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
904 * indicating that a server has been assigned. It may also return SI_ST_QUE,
905 * or SI_ST_CLO upon error.
906 */
907static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
908 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",
909 now_ms, __FUNCTION__,
910 s,
911 s->req, s->rep,
912 s->req->rex, s->rep->wex,
913 s->req->flags, s->rep->flags,
914 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
915
916 if (si->state != SI_ST_REQ)
917 return;
918
919 /* Try to assign a server */
920 if (srv_redispatch_connect(s) != 0) {
921 /* We did not get a server. Either we queued the
922 * connection request, or we encountered an error.
923 */
924 if (si->state == SI_ST_QUE)
925 return;
926
927 /* we did not get any server, let's check the cause */
928 si->shutr(si);
929 si->shutw(si);
930 si->ob->flags |= BF_WRITE_ERROR;
931 if (!si->err_type)
932 si->err_type = SI_ET_CONN_OTHER;
933 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100934 if (s->srv_error)
935 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100936 return;
937 }
938
939 /* The server is assigned */
940 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
941 si->state = SI_ST_ASS;
942}
943
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200944/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100945 * if appropriate. The default_backend rule is also considered, then the
946 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200947 * It returns 1 if the processing can continue on next analysers, or zero if it
948 * either needs more data or wants to immediately abort the request.
949 */
Simon Hormandec5be42011-06-08 09:19:07 +0900950static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200951{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200952 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100953
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200954 req->analysers &= ~an_bit;
955 req->analyse_exp = TICK_ETERNITY;
956
957 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
958 now_ms, __FUNCTION__,
959 s,
960 req,
961 req->rex, req->wex,
962 req->flags,
963 req->l,
964 req->analysers);
965
966 /* now check whether we have some switching rules for this request */
967 if (!(s->flags & SN_BE_ASSIGNED)) {
968 struct switching_rule *rule;
969
970 list_for_each_entry(rule, &s->fe->switching_rules, list) {
971 int ret;
972
973 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
974 ret = acl_pass(ret);
975 if (rule->cond->pol == ACL_COND_UNLESS)
976 ret = !ret;
977
978 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200979 if (!session_set_backend(s, rule->be.backend))
980 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200981 break;
982 }
983 }
984
985 /* To ensure correct connection accounting on the backend, we
986 * have to assign one if it was not set (eg: a listen). This
987 * measure also takes care of correctly setting the default
988 * backend if any.
989 */
990 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200991 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
992 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200993 }
994
Willy Tarreaufb356202010-08-03 14:02:05 +0200995 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
996 if (s->fe == s->be) {
997 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200998 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200999 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001000
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001001 /* 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 +01001002 * persistence rule, and report that in the session.
1003 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001004 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001005 int ret = 1;
1006
1007 if (prst_rule->cond) {
1008 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1009 ret = acl_pass(ret);
1010 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1011 ret = !ret;
1012 }
1013
1014 if (ret) {
1015 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001016 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1017 s->flags |= SN_FORCE_PRST;
1018 } else {
1019 s->flags |= SN_IGNORE_PRST;
1020 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001021 break;
1022 }
1023 }
1024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001025 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001026
1027 sw_failed:
1028 /* immediately abort this request in case of allocation failure */
1029 buffer_abort(s->req);
1030 buffer_abort(s->rep);
1031
1032 if (!(s->flags & SN_ERR_MASK))
1033 s->flags |= SN_ERR_RESOURCE;
1034 if (!(s->flags & SN_FINST_MASK))
1035 s->flags |= SN_FINST_R;
1036
1037 s->txn.status = 500;
1038 s->req->analysers = 0;
1039 s->req->analyse_exp = TICK_ETERNITY;
1040 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001041}
1042
Emeric Brun1d33b292010-01-04 15:47:17 +01001043/* This stream analyser works on a request. It applies all sticking rules on
1044 * it then returns 1. The data must already be present in the buffer otherwise
1045 * they won't match. It always returns 1.
1046 */
Simon Hormandec5be42011-06-08 09:19:07 +09001047static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001048{
1049 struct proxy *px = s->be;
1050 struct sticking_rule *rule;
1051
1052 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1053 now_ms, __FUNCTION__,
1054 s,
1055 req,
1056 req->rex, req->wex,
1057 req->flags,
1058 req->l,
1059 req->analysers);
1060
1061 list_for_each_entry(rule, &px->sticking_rules, list) {
1062 int ret = 1 ;
1063 int i;
1064
1065 for (i = 0; i < s->store_count; i++) {
1066 if (rule->table.t == s->store[i].table)
1067 break;
1068 }
1069
1070 if (i != s->store_count)
1071 continue;
1072
1073 if (rule->cond) {
1074 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1075 ret = acl_pass(ret);
1076 if (rule->cond->pol == ACL_COND_UNLESS)
1077 ret = !ret;
1078 }
1079
1080 if (ret) {
1081 struct stktable_key *key;
1082
Emeric Brun485479d2010-09-23 18:02:19 +02001083 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001084 if (!key)
1085 continue;
1086
1087 if (rule->flags & STK_IS_MATCH) {
1088 struct stksess *ts;
1089
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001090 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001091 if (!(s->flags & SN_ASSIGNED)) {
1092 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001093 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001094
1095 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001096 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1097 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001098 if (node) {
1099 struct server *srv;
1100
1101 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001102 if ((srv->state & SRV_RUNNING) ||
1103 (px->options & PR_O_PERSIST) ||
1104 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001105 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001106 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001107 }
1108 }
1109 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001110 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001111 }
1112 }
1113 if (rule->flags & STK_IS_STORE) {
1114 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1115 struct stksess *ts;
1116
1117 ts = stksess_new(rule->table.t, key);
1118 if (ts) {
1119 s->store[s->store_count].table = rule->table.t;
1120 s->store[s->store_count++].ts = ts;
1121 }
1122 }
1123 }
1124 }
1125 }
1126
1127 req->analysers &= ~an_bit;
1128 req->analyse_exp = TICK_ETERNITY;
1129 return 1;
1130}
1131
1132/* This stream analyser works on a response. It applies all store rules on it
1133 * then returns 1. The data must already be present in the buffer otherwise
1134 * they won't match. It always returns 1.
1135 */
Simon Hormandec5be42011-06-08 09:19:07 +09001136static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001137{
1138 struct proxy *px = s->be;
1139 struct sticking_rule *rule;
1140 int i;
1141
1142 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1143 now_ms, __FUNCTION__,
1144 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001145 rep,
1146 rep->rex, rep->wex,
1147 rep->flags,
1148 rep->l,
1149 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001150
1151 list_for_each_entry(rule, &px->storersp_rules, list) {
1152 int ret = 1 ;
1153 int storereqidx = -1;
1154
1155 for (i = 0; i < s->store_count; i++) {
1156 if (rule->table.t == s->store[i].table) {
1157 if (!(s->store[i].flags))
1158 storereqidx = i;
1159 break;
1160 }
1161 }
1162
1163 if ((i != s->store_count) && (storereqidx == -1))
1164 continue;
1165
1166 if (rule->cond) {
1167 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1168 ret = acl_pass(ret);
1169 if (rule->cond->pol == ACL_COND_UNLESS)
1170 ret = !ret;
1171 }
1172
1173 if (ret) {
1174 struct stktable_key *key;
1175
Emeric Brun485479d2010-09-23 18:02:19 +02001176 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001177 if (!key)
1178 continue;
1179
1180 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001181 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001182 s->store[storereqidx].flags = 1;
1183 }
1184 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1185 struct stksess *ts;
1186
1187 ts = stksess_new(rule->table.t, key);
1188 if (ts) {
1189 s->store[s->store_count].table = rule->table.t;
1190 s->store[s->store_count].flags = 1;
1191 s->store[s->store_count++].ts = ts;
1192 }
1193 }
1194 }
1195 }
1196
1197 /* process store request and store response */
1198 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001199 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001200 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001201
Simon Hormanfa461682011-06-25 09:39:49 +09001202 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1203 stksess_free(s->store[i].table, s->store[i].ts);
1204 s->store[i].ts = NULL;
1205 continue;
1206 }
1207
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001208 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1209 if (ts) {
1210 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001211 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001212 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001213 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001214 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001215 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001216
1217 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001218 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001219 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001220 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001221 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001222
1223 rep->analysers &= ~an_bit;
1224 rep->analyse_exp = TICK_ETERNITY;
1225 return 1;
1226}
1227
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001228/* This macro is very specific to the function below. See the comments in
1229 * process_session() below to understand the logic and the tests.
1230 */
1231#define UPDATE_ANALYSERS(real, list, back, flag) { \
1232 list = (((list) & ~(flag)) | ~(back)) & (real); \
1233 back = real; \
1234 if (!(list)) \
1235 break; \
1236 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1237 continue; \
1238}
1239
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001240/* Processes the client, server, request and response jobs of a session task,
1241 * then puts it back to the wait queue in a clean state, or cleans up its
1242 * resources if it must be deleted. Returns in <next> the date the task wants
1243 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1244 * nothing too many times, the request and response buffers flags are monitored
1245 * and each function is called only if at least another function has changed at
1246 * least one flag it is interested in.
1247 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001248struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001249{
Willy Tarreau827aee92011-03-10 16:55:02 +01001250 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001251 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001253 unsigned int rq_prod_last, rq_cons_last;
1254 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001255 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001256
1257 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1258 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1259
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260 /* this data may be no longer valid, clear it */
1261 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1262
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001263 /* This flag must explicitly be set every time */
1264 s->req->flags &= ~BF_READ_NOEXP;
1265
1266 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001267 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1268 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001269
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001270 /* we don't want the stream interface functions to recursively wake us up */
1271 if (s->req->prod->owner == t)
1272 s->req->prod->flags |= SI_FL_DONT_WAKE;
1273 if (s->req->cons->owner == t)
1274 s->req->cons->flags |= SI_FL_DONT_WAKE;
1275
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001276 /* 1a: Check for low level timeouts if needed. We just set a flag on
1277 * stream interfaces when their timeouts have expired.
1278 */
1279 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1280 stream_int_check_timeouts(&s->si[0]);
1281 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001282
1283 /* check buffer timeouts, and close the corresponding stream interfaces
1284 * for future reads or writes. Note: this will also concern upper layers
1285 * but we do not touch any other flag. We must be careful and correctly
1286 * detect state changes when calling them.
1287 */
1288
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001289 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001290
Willy Tarreau14641402009-12-29 14:49:56 +01001291 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1292 s->req->cons->flags |= SI_FL_NOLINGER;
1293 s->req->cons->shutw(s->req->cons);
1294 }
1295
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001296 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1297 s->req->prod->shutr(s->req->prod);
1298
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001299 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001300
Willy Tarreau14641402009-12-29 14:49:56 +01001301 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1302 s->rep->cons->flags |= SI_FL_NOLINGER;
1303 s->rep->cons->shutw(s->rep->cons);
1304 }
1305
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001306 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1307 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001308 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001309
1310 /* 1b: check for low-level errors reported at the stream interface.
1311 * First we check if it's a retryable error (in which case we don't
1312 * want to tell the buffer). Otherwise we report the error one level
1313 * upper by setting flags into the buffers. Note that the side towards
1314 * the client cannot have connect (hence retryable) errors. Also, the
1315 * connection setup code must be able to deal with any type of abort.
1316 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001317 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001318 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1319 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1320 s->si[0].shutr(&s->si[0]);
1321 s->si[0].shutw(&s->si[0]);
1322 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001323 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001324 s->be->be_counters.cli_aborts++;
1325 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001326 if (srv)
1327 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001328 if (!(s->flags & SN_ERR_MASK))
1329 s->flags |= SN_ERR_CLICL;
1330 if (!(s->flags & SN_FINST_MASK))
1331 s->flags |= SN_FINST_D;
1332 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001333 }
1334 }
1335
1336 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1337 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1338 s->si[1].shutr(&s->si[1]);
1339 s->si[1].shutw(&s->si[1]);
1340 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001341 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001342 if (srv)
1343 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001344 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001345 s->be->be_counters.srv_aborts++;
1346 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001347 if (srv)
1348 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001349 if (!(s->flags & SN_ERR_MASK))
1350 s->flags |= SN_ERR_SRVCL;
1351 if (!(s->flags & SN_FINST_MASK))
1352 s->flags |= SN_FINST_D;
1353 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001354 }
1355 /* note: maybe we should process connection errors here ? */
1356 }
1357
1358 if (s->si[1].state == SI_ST_CON) {
1359 /* we were trying to establish a connection on the server side,
1360 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1361 */
1362 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1363 sess_update_st_cer(s, &s->si[1]);
1364 else if (s->si[1].state == SI_ST_EST)
1365 sess_establish(s, &s->si[1]);
1366
1367 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1368 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1369 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1370 */
1371 }
1372
Willy Tarreau815a9b22010-07-27 17:15:12 +02001373 rq_prod_last = s->si[0].state;
1374 rq_cons_last = s->si[1].state;
1375 rp_cons_last = s->si[0].state;
1376 rp_prod_last = s->si[1].state;
1377
1378 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001379 /* Check for connection closure */
1380
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001381 DPRINTF(stderr,
1382 "[%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",
1383 now_ms, __FUNCTION__, __LINE__,
1384 t,
1385 s, s->flags,
1386 s->req, s->rep,
1387 s->req->rex, s->rep->wex,
1388 s->req->flags, s->rep->flags,
1389 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1390 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001391 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001392
1393 /* nothing special to be done on client side */
1394 if (unlikely(s->req->prod->state == SI_ST_DIS))
1395 s->req->prod->state = SI_ST_CLO;
1396
1397 /* When a server-side connection is released, we have to count it and
1398 * check for pending connections on this server.
1399 */
1400 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1401 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001402 srv = target_srv(&s->target);
1403 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001404 if (s->flags & SN_CURR_SESS) {
1405 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001406 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001407 }
1408 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001409 if (may_dequeue_tasks(srv, s->be))
1410 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001411 }
1412 }
1413
1414 /*
1415 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1416 * at this point.
1417 */
1418
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001419 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001420 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001421 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001422 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1423 s->si[0].state != rq_prod_last ||
1424 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001425 unsigned int flags = s->req->flags;
1426
1427 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001428 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001429 unsigned int ana_list;
1430 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001431
Willy Tarreau90deb182010-01-07 00:20:41 +01001432 /* it's up to the analysers to stop new connections,
1433 * disable reading or closing. Note: if an analyser
1434 * disables any of these bits, it is responsible for
1435 * enabling them again when it disables itself, so
1436 * that other analysers are called in similar conditions.
1437 */
1438 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001439 buffer_auto_connect(s->req);
1440 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001441
1442 /* We will call all analysers for which a bit is set in
1443 * s->req->analysers, following the bit order from LSB
1444 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001445 * the list when not needed. Any analyser may return 0
1446 * to break out of the loop, either because of missing
1447 * data to take a decision, or because it decides to
1448 * kill the session. We loop at least once through each
1449 * analyser, and we may loop again if other analysers
1450 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001451 *
1452 * We build a list of analysers to run. We evaluate all
1453 * of these analysers in the order of the lower bit to
1454 * the higher bit. This ordering is very important.
1455 * An analyser will often add/remove other analysers,
1456 * including itself. Any changes to itself have no effect
1457 * on the loop. If it removes any other analysers, we
1458 * want those analysers not to be called anymore during
1459 * this loop. If it adds an analyser that is located
1460 * after itself, we want it to be scheduled for being
1461 * processed during the loop. If it adds an analyser
1462 * which is located before it, we want it to switch to
1463 * it immediately, even if it has already been called
1464 * once but removed since.
1465 *
1466 * In order to achieve this, we compare the analyser
1467 * list after the call with a copy of it before the
1468 * call. The work list is fed with analyser bits that
1469 * appeared during the call. Then we compare previous
1470 * work list with the new one, and check the bits that
1471 * appeared. If the lowest of these bits is lower than
1472 * the current bit, it means we have enabled a previous
1473 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001474 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001475
1476 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001477 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001479
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001480 if (ana_list & AN_REQ_DECODE_PROXY) {
1481 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1482 break;
1483 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1484 }
1485
Willy Tarreaufb356202010-08-03 14:02:05 +02001486 if (ana_list & AN_REQ_INSPECT_FE) {
1487 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001488 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001489 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001490 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001493 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001494 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001496 }
1497
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001498 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001499 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1500 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001501 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001502 }
1503
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001505 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1506 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001507 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001508 }
1509
Willy Tarreaufb356202010-08-03 14:02:05 +02001510 if (ana_list & AN_REQ_INSPECT_BE) {
1511 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1512 break;
1513 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1514 }
1515
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001516 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001517 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1518 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001519 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001520 }
1521
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001522 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001523 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001524 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001525 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001526 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001527
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001528 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001529 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1530 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001531 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001532 }
1533
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001534 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001535 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001536 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001537 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001538 }
Emeric Brun647caf12009-06-30 17:57:00 +02001539
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001540 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001541 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1542 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001543 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001544 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001545
Emeric Brun1d33b292010-01-04 15:47:17 +01001546 if (ana_list & AN_REQ_STICKING_RULES) {
1547 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1548 break;
1549 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1550 }
1551
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001552 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001553 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1554 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001555 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001556 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001557 break;
1558 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001559 }
Willy Tarreau84455332009-03-15 22:34:05 +01001560
Willy Tarreau815a9b22010-07-27 17:15:12 +02001561 rq_prod_last = s->si[0].state;
1562 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001563 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001564 rqf_last = s->req->flags;
1565
1566 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001567 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001568 }
1569
Willy Tarreau576507f2010-01-07 00:09:04 +01001570 /* we'll monitor the request analysers while parsing the response,
1571 * because some response analysers may indirectly enable new request
1572 * analysers (eg: HTTP keep-alive).
1573 */
1574 req_ana_back = s->req->analysers;
1575
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001576 resync_response:
1577 /* Analyse response */
1578
1579 if (unlikely(s->rep->flags & BF_HIJACK)) {
1580 /* In inject mode, we wake up everytime something has
1581 * happened on the write side of the buffer.
1582 */
1583 unsigned int flags = s->rep->flags;
1584
1585 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1586 !(s->rep->flags & BF_FULL)) {
1587 s->rep->hijacker(s, s->rep);
1588 }
1589
1590 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1591 rpf_last = s->rep->flags;
1592 goto resync_response;
1593 }
1594 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001595 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001596 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1597 s->si[0].state != rp_cons_last ||
1598 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001599 unsigned int flags = s->rep->flags;
1600
Willy Tarreau0499e352010-12-17 07:13:42 +01001601 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1602 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1603 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1604 * for some free space in the response buffer, so we might need to call
1605 * it when something changes in the response buffer, but still we pass
1606 * it the request buffer. Note that the SI state might very well still
1607 * be zero due to us returning a flow of redirects!
1608 */
1609 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1610 s->req->flags |= BF_WAKE_ONCE;
1611 }
1612
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001613 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001614 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 unsigned int ana_list;
1616 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001617
Willy Tarreau90deb182010-01-07 00:20:41 +01001618 /* it's up to the analysers to stop disable reading or
1619 * closing. Note: if an analyser disables any of these
1620 * bits, it is responsible for enabling them again when
1621 * it disables itself, so that other analysers are called
1622 * in similar conditions.
1623 */
1624 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001625 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001626
1627 /* We will call all analysers for which a bit is set in
1628 * s->rep->analysers, following the bit order from LSB
1629 * to MSB. The analysers must remove themselves from
1630 * the list when not needed. Any analyser may return 0
1631 * to break out of the loop, either because of missing
1632 * data to take a decision, or because it decides to
1633 * kill the session. We loop at least once through each
1634 * analyser, and we may loop again if other analysers
1635 * are added in the middle.
1636 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001637
1638 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001639 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001640 /* Warning! ensure that analysers are always placed in ascending order! */
1641
Emeric Brun97679e72010-09-23 17:56:44 +02001642 if (ana_list & AN_RES_INSPECT) {
1643 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1644 break;
1645 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1646 }
1647
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001648 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001649 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1650 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001651 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001652 }
1653
Emeric Brun1d33b292010-01-04 15:47:17 +01001654 if (ana_list & AN_RES_STORE_RULES) {
1655 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1656 break;
1657 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1658 }
1659
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001660 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001661 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1662 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001663 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001664 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001665
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001666 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001667 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1668 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001669 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001670 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001671 break;
1672 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001673 }
1674
Willy Tarreau815a9b22010-07-27 17:15:12 +02001675 rp_cons_last = s->si[0].state;
1676 rp_prod_last = s->si[1].state;
1677 rpf_last = s->rep->flags;
1678
1679 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001680 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001681 }
1682
Willy Tarreau576507f2010-01-07 00:09:04 +01001683 /* maybe someone has added some request analysers, so we must check and loop */
1684 if (s->req->analysers & ~req_ana_back)
1685 goto resync_request;
1686
Willy Tarreau0499e352010-12-17 07:13:42 +01001687 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1688 goto resync_request;
1689
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001690 /* FIXME: here we should call protocol handlers which rely on
1691 * both buffers.
1692 */
1693
1694
1695 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001696 * Now we propagate unhandled errors to the session. Normally
1697 * we're just in a data phase here since it means we have not
1698 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001699 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001700 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001701 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001702 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1703 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001704 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001705 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001706 s->be->be_counters.cli_aborts++;
1707 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001708 if (srv)
1709 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001710 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001711 }
1712 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001713 s->be->be_counters.cli_aborts++;
1714 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001715 if (srv)
1716 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001717 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001718 }
1719 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001720 s->be->be_counters.srv_aborts++;
1721 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001722 if (srv)
1723 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001724 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001725 }
1726 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001727 s->be->be_counters.srv_aborts++;
1728 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001729 if (srv)
1730 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001731 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001732 }
Willy Tarreau84455332009-03-15 22:34:05 +01001733 sess_set_term_flags(s);
1734 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001735 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1736 /* Report it if the server got an error or a read timeout expired */
1737 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001738 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001739 s->be->be_counters.srv_aborts++;
1740 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001741 if (srv)
1742 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001743 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001744 }
1745 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001746 s->be->be_counters.srv_aborts++;
1747 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001748 if (srv)
1749 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001750 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001751 }
1752 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001753 s->be->be_counters.cli_aborts++;
1754 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001755 if (srv)
1756 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001757 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001758 }
1759 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001760 s->be->be_counters.cli_aborts++;
1761 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001762 if (srv)
1763 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001764 s->flags |= SN_ERR_CLITO;
1765 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001766 sess_set_term_flags(s);
1767 }
Willy Tarreau84455332009-03-15 22:34:05 +01001768 }
1769
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001770 /*
1771 * Here we take care of forwarding unhandled data. This also includes
1772 * connection establishments and shutdown requests.
1773 */
1774
1775
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001776 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001777 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001778 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1779 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001780 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001781 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001782 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001783 (s->req->prod->state >= SI_ST_EST) &&
1784 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001785 /* This buffer is freewheeling, there's no analyser nor hijacker
1786 * attached to it. If any data are left in, we'll permit them to
1787 * move.
1788 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001789 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001790 buffer_auto_connect(s->req);
1791 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001792 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001793
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001794 /* We'll let data flow between the producer (if still connected)
1795 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001796 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001797 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001798 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001799 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001800
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001801 /* check if it is wise to enable kernel splicing to forward request data */
1802 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1803 s->req->to_forward &&
1804 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001805 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001806 (pipes_used < global.maxpipes) &&
1807 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1808 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1809 (s->req->flags & BF_STREAMER_FAST)))) {
1810 s->req->flags |= BF_KERN_SPLICING;
1811 }
1812
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001813 /* reflect what the L7 analysers have seen last */
1814 rqf_last = s->req->flags;
1815
1816 /*
1817 * Now forward all shutdown requests between both sides of the buffer
1818 */
1819
Willy Tarreau520d95e2009-09-19 21:04:57 +02001820 /* first, let's check if the request buffer needs to shutdown(write), which may
1821 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001822 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001823 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001824 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001825 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001826 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001827
1828 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001829 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 +01001830 s->req->cons->shutw(s->req->cons);
1831
1832 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001833 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1834 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001835 buffer_shutr_now(s->req);
1836
1837 /* shutdown(read) pending */
1838 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1839 s->req->prod->shutr(s->req->prod);
1840
Willy Tarreau520d95e2009-09-19 21:04:57 +02001841 /* it's possible that an upper layer has requested a connection setup or abort.
1842 * There are 2 situations where we decide to establish a new connection :
1843 * - there are data scheduled for emission in the buffer
1844 * - the BF_AUTO_CONNECT flag is set (active connection)
1845 */
1846 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001847 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001848 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001849 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001850 * switch to the connected state, otherwise we perform a connection
1851 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001852 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001853 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001854 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001855 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001856 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001857 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1858 s->req->wex = TICK_ETERNITY;
1859 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001860 }
Willy Tarreau73201222009-08-16 18:27:24 +02001861 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001862 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001863 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001864 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1865 buffer_shutr_now(s->rep);
1866 }
Willy Tarreau92795622009-03-06 12:51:23 +01001867 }
1868
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001869
1870 /* we may have a pending connection request, or a connection waiting
1871 * for completion.
1872 */
1873 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1874 do {
1875 /* nb: step 1 might switch from QUE to ASS, but we first want
1876 * to give a chance to step 2 to perform a redirect if needed.
1877 */
1878 if (s->si[1].state != SI_ST_REQ)
1879 sess_update_stream_int(s, &s->si[1]);
1880 if (s->si[1].state == SI_ST_REQ)
1881 sess_prepare_conn_req(s, &s->si[1]);
1882
Willy Tarreau827aee92011-03-10 16:55:02 +01001883 srv = target_srv(&s->target);
1884 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001885 perform_http_redirect(s, &s->si[1]);
1886 } while (s->si[1].state == SI_ST_ASS);
1887 }
1888
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001889 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001890 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 goto resync_stream_interface;
1892
Willy Tarreau815a9b22010-07-27 17:15:12 +02001893 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001895 goto resync_request;
1896
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001897 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001898
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001899 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001900 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001901 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1902 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001903 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001904 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001905 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001906 (s->rep->prod->state >= SI_ST_EST) &&
1907 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001908 /* This buffer is freewheeling, there's no analyser nor hijacker
1909 * attached to it. If any data are left in, we'll permit them to
1910 * move.
1911 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001912 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001913 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001914 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001915
1916 /* We'll let data flow between the producer (if still connected)
1917 * to the consumer.
1918 */
1919 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001920 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001921 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001922
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001923 /* check if it is wise to enable kernel splicing to forward response data */
1924 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1925 s->rep->to_forward &&
1926 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001927 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001928 (pipes_used < global.maxpipes) &&
1929 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1930 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1931 (s->rep->flags & BF_STREAMER_FAST)))) {
1932 s->rep->flags |= BF_KERN_SPLICING;
1933 }
1934
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001935 /* reflect what the L7 analysers have seen last */
1936 rpf_last = s->rep->flags;
1937
1938 /*
1939 * Now forward all shutdown requests between both sides of the buffer
1940 */
1941
1942 /*
1943 * FIXME: this is probably where we should produce error responses.
1944 */
1945
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001946 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001947 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1948 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001949 buffer_shutw_now(s->rep);
1950
1951 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001952 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 +01001953 s->rep->cons->shutw(s->rep->cons);
1954
1955 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001956 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1957 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001958 buffer_shutr_now(s->rep);
1959
1960 /* shutdown(read) pending */
1961 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1962 s->rep->prod->shutr(s->rep->prod);
1963
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001964 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001965 goto resync_stream_interface;
1966
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001967 if (s->req->flags != rqf_last)
1968 goto resync_request;
1969
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001971 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001972
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001973 /* we're interested in getting wakeups again */
1974 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1975 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1976
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001977 /* This is needed only when debugging is enabled, to indicate
1978 * client-side or server-side close. Please note that in the unlikely
1979 * event where both sides would close at once, the sequence is reported
1980 * on the server side first.
1981 */
1982 if (unlikely((global.mode & MODE_DEBUG) &&
1983 (!(global.mode & MODE_QUIET) ||
1984 (global.mode & MODE_VERBOSE)))) {
1985 int len;
1986
1987 if (s->si[1].state == SI_ST_CLO &&
1988 s->si[1].prev_state == SI_ST_EST) {
1989 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1990 s->uniq_id, s->be->id,
1991 (unsigned short)s->si[0].fd,
1992 (unsigned short)s->si[1].fd);
1993 write(1, trash, len);
1994 }
1995
1996 if (s->si[0].state == SI_ST_CLO &&
1997 s->si[0].prev_state == SI_ST_EST) {
1998 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1999 s->uniq_id, s->be->id,
2000 (unsigned short)s->si[0].fd,
2001 (unsigned short)s->si[1].fd);
2002 write(1, trash, len);
2003 }
2004 }
2005
2006 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2007 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2008
2009 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2010 session_process_counters(s);
2011
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002012 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002013 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002014
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002015 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002016 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002017
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002018 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2019 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 +01002020 s->si[0].prev_state = s->si[0].state;
2021 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002022 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2023 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002024
2025 /* Trick: if a request is being waiting for the server to respond,
2026 * and if we know the server can timeout, we don't want the timeout
2027 * to expire on the client side first, but we're still interested
2028 * in passing data from the client to the server (eg: POST). Thus,
2029 * we can cancel the client's request timeout if the server's
2030 * request timeout is set and the server has not yet sent a response.
2031 */
2032
Willy Tarreau520d95e2009-09-19 21:04:57 +02002033 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002034 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2035 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002036 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002037 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002039 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002040 * Note that this one may wake the task up again.
2041 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002042 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2043 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2044 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2045 s->req->cons->target.ptr.a->fct(s->req->cons);
2046 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2047 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002048 if (task_in_rq(t)) {
2049 /* If we woke up, we don't want to requeue the
2050 * task to the wait queue, but rather requeue
2051 * it into the runqueue ASAP.
2052 */
2053 t->expire = TICK_ETERNITY;
2054 return t;
2055 }
2056 }
2057
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002058 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2059 tick_first(s->rep->rex, s->rep->wex));
2060 if (s->req->analysers)
2061 t->expire = tick_first(t->expire, s->req->analyse_exp);
2062
2063 if (s->si[0].exp)
2064 t->expire = tick_first(t->expire, s->si[0].exp);
2065
2066 if (s->si[1].exp)
2067 t->expire = tick_first(t->expire, s->si[1].exp);
2068
2069#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002070 fprintf(stderr,
2071 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2072 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2073 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2074 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 +01002075#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076
2077#ifdef DEBUG_DEV
2078 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002079 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002080 ABORT_NOW();
2081#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002082 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002083 }
2084
2085 s->fe->feconn--;
2086 if (s->flags & SN_BE_ASSIGNED)
2087 s->be->beconn--;
2088 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002089 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002090 s->listener->nbconn--;
2091 if (s->listener->state == LI_FULL &&
2092 s->listener->nbconn < s->listener->maxconn) {
2093 /* we should reactivate the listener */
2094 EV_FD_SET(s->listener->fd, DIR_RD);
2095 s->listener->state = LI_READY;
2096 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097
2098 if (unlikely((global.mode & MODE_DEBUG) &&
2099 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2100 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002101 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002102 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002103 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002104 write(1, trash, len);
2105 }
2106
2107 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2108 session_process_counters(s);
2109
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002110 if (s->txn.status) {
2111 int n;
2112
2113 n = s->txn.status / 100;
2114 if (n < 1 || n > 5)
2115 n = 0;
2116
2117 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002118 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002119
Willy Tarreau24657792010-02-26 10:30:28 +01002120 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002121 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002122 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002123 }
2124
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002125 /* let's do a final log if we need it */
2126 if (s->logs.logwait &&
2127 !(s->flags & SN_MONITOR) &&
2128 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002129 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002130 }
2131
2132 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002133 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002134 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002135 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002136 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002137}
2138
Willy Tarreau7c669d72008-06-20 15:04:11 +02002139/*
2140 * This function adjusts sess->srv_conn and maintains the previous and new
2141 * server's served session counts. Setting newsrv to NULL is enough to release
2142 * current connection slot. This function also notifies any LB algo which might
2143 * expect to be informed about any change in the number of active sessions on a
2144 * server.
2145 */
2146void sess_change_server(struct session *sess, struct server *newsrv)
2147{
2148 if (sess->srv_conn == newsrv)
2149 return;
2150
2151 if (sess->srv_conn) {
2152 sess->srv_conn->served--;
2153 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2154 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002155 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002156 }
2157
2158 if (newsrv) {
2159 newsrv->served++;
2160 if (newsrv->proxy->lbprm.server_take_conn)
2161 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002162 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002163 }
2164}
2165
Willy Tarreau84455332009-03-15 22:34:05 +01002166/* Handle server-side errors for default protocols. It is called whenever a a
2167 * connection setup is aborted or a request is aborted in queue. It sets the
2168 * session termination flags so that the caller does not have to worry about
2169 * them. It's installed as ->srv_error for the server-side stream_interface.
2170 */
2171void default_srv_error(struct session *s, struct stream_interface *si)
2172{
2173 int err_type = si->err_type;
2174 int err = 0, fin = 0;
2175
2176 if (err_type & SI_ET_QUEUE_ABRT) {
2177 err = SN_ERR_CLICL;
2178 fin = SN_FINST_Q;
2179 }
2180 else if (err_type & SI_ET_CONN_ABRT) {
2181 err = SN_ERR_CLICL;
2182 fin = SN_FINST_C;
2183 }
2184 else if (err_type & SI_ET_QUEUE_TO) {
2185 err = SN_ERR_SRVTO;
2186 fin = SN_FINST_Q;
2187 }
2188 else if (err_type & SI_ET_QUEUE_ERR) {
2189 err = SN_ERR_SRVCL;
2190 fin = SN_FINST_Q;
2191 }
2192 else if (err_type & SI_ET_CONN_TO) {
2193 err = SN_ERR_SRVTO;
2194 fin = SN_FINST_C;
2195 }
2196 else if (err_type & SI_ET_CONN_ERR) {
2197 err = SN_ERR_SRVCL;
2198 fin = SN_FINST_C;
2199 }
2200 else /* SI_ET_CONN_OTHER and others */ {
2201 err = SN_ERR_INTERNAL;
2202 fin = SN_FINST_C;
2203 }
2204
2205 if (!(s->flags & SN_ERR_MASK))
2206 s->flags |= err;
2207 if (!(s->flags & SN_FINST_MASK))
2208 s->flags |= fin;
2209}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002210
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002211
Willy Tarreau8b22a712010-06-18 17:46:06 +02002212/************************************************************************/
2213/* All supported ACL keywords must be declared here. */
2214/************************************************************************/
2215
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002216/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2217static int
2218acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2219{
2220 test->flags = ACL_TEST_F_VOL_TEST;
2221 test->i = 0;
2222 if (ts != NULL) {
2223 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2224 if (!ptr)
2225 return 0; /* parameter not stored */
2226 test->i = stktable_data_cast(ptr, gpc0);
2227 }
2228 return 1;
2229}
2230
2231/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002232 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002233 */
2234static int
Willy Tarreau56123282010-08-06 19:06:56 +02002235acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2236 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002237{
Willy Tarreau56123282010-08-06 19:06:56 +02002238 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002239 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002240 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002241}
2242
2243/* set test->i to the General Purpose Counter 0 value from the session's tracked
2244 * backend counters.
2245 */
2246static int
Willy Tarreau56123282010-08-06 19:06:56 +02002247acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2248 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002249{
Willy Tarreau56123282010-08-06 19:06:56 +02002250 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002251 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002252 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002253}
2254
2255/* set test->i to the General Purpose Counter 0 value from the session's source
2256 * address in the table pointed to by expr.
2257 */
2258static int
2259acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2260 struct acl_expr *expr, struct acl_test *test)
2261{
2262 struct stktable_key *key;
2263
David du Colombier4f92d322011-03-24 11:09:31 +01002264 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002265 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002266 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002267
2268 if (expr->arg_len)
2269 px = find_stktable(expr->arg.str);
2270
2271 if (!px)
2272 return 0; /* table not found */
2273
2274 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2275}
2276
2277/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2278 * return it into test->i.
2279 */
2280static int
2281acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2282{
2283 test->flags = ACL_TEST_F_VOL_TEST;
2284 test->i = 0;
2285 if (ts != NULL) {
2286 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2287 if (!ptr)
2288 return 0; /* parameter not stored */
2289 test->i = ++stktable_data_cast(ptr, gpc0);
2290 }
2291 return 1;
2292}
2293
2294/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002295 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002296 */
2297static int
Willy Tarreau56123282010-08-06 19:06:56 +02002298acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2299 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002300{
Willy Tarreau56123282010-08-06 19:06:56 +02002301 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002302 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002303 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002304}
2305
2306/* Increment the General Purpose Counter 0 value from the session's tracked
2307 * backend counters and return it into test->i.
2308 */
2309static int
Willy Tarreau56123282010-08-06 19:06:56 +02002310acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2311 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002312{
Willy Tarreau56123282010-08-06 19:06:56 +02002313 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002314 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002315 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002316}
2317
2318/* Increment the General Purpose Counter 0 value from the session's source
2319 * address in the table pointed to by expr, and return it into test->i.
2320 */
2321static int
2322acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2323 struct acl_expr *expr, struct acl_test *test)
2324{
2325 struct stktable_key *key;
2326
David du Colombier4f92d322011-03-24 11:09:31 +01002327 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002328 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002329 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002330
2331 if (expr->arg_len)
2332 px = find_stktable(expr->arg.str);
2333
2334 if (!px)
2335 return 0; /* table not found */
2336
2337 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2338}
2339
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002340/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2341static int
2342acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2343{
2344 test->flags = ACL_TEST_F_VOL_TEST;
2345 test->i = 0;
2346 if (ts != NULL) {
2347 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2348 if (!ptr)
2349 return 0; /* parameter not stored */
2350 test->i = stktable_data_cast(ptr, conn_cnt);
2351 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002352 return 1;
2353}
2354
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002355/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002356static int
Willy Tarreau56123282010-08-06 19:06:56 +02002357acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2358 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002359{
Willy Tarreau56123282010-08-06 19:06:56 +02002360 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002361 return 0;
2362
Willy Tarreau56123282010-08-06 19:06:56 +02002363 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002364}
2365
2366/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2367static int
Willy Tarreau56123282010-08-06 19:06:56 +02002368acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2369 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002370{
Willy Tarreau56123282010-08-06 19:06:56 +02002371 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002372 return 0;
2373
Willy Tarreau56123282010-08-06 19:06:56 +02002374 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002375}
2376
2377/* set test->i to the cumulated number of connections from the session's source
2378 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002379 */
2380static int
2381acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2382 struct acl_expr *expr, struct acl_test *test)
2383{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002384 struct stktable_key *key;
2385
David du Colombier4f92d322011-03-24 11:09:31 +01002386 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002387 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002388 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002389
2390 if (expr->arg_len)
2391 px = find_stktable(expr->arg.str);
2392
2393 if (!px)
2394 return 0; /* table not found */
2395
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002396 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002397}
2398
Willy Tarreau91c43d72010-06-20 11:19:22 +02002399/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2400static int
2401acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2402{
2403 test->flags = ACL_TEST_F_VOL_TEST;
2404 test->i = 0;
2405 if (ts != NULL) {
2406 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2407 if (!ptr)
2408 return 0; /* parameter not stored */
2409 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2410 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2411 }
2412 return 1;
2413}
2414
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002415/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002416 * the configured period.
2417 */
2418static int
Willy Tarreau56123282010-08-06 19:06:56 +02002419acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2420 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002421{
Willy Tarreau56123282010-08-06 19:06:56 +02002422 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002423 return 0;
2424
Willy Tarreau56123282010-08-06 19:06:56 +02002425 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002426}
2427
2428/* set test->i to the connection rate from the session's tracked BE counters over
2429 * the configured period.
2430 */
2431static int
Willy Tarreau56123282010-08-06 19:06:56 +02002432acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2433 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002434{
Willy Tarreau56123282010-08-06 19:06:56 +02002435 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002436 return 0;
2437
Willy Tarreau56123282010-08-06 19:06:56 +02002438 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002439}
2440
2441/* set test->i to the connection rate from the session's source address in the
2442 * table pointed to by expr, over the configured period.
2443 */
2444static int
2445acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2446 struct acl_expr *expr, struct acl_test *test)
2447{
2448 struct stktable_key *key;
2449
David du Colombier4f92d322011-03-24 11:09:31 +01002450 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002451 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002452 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002453
2454 if (expr->arg_len)
2455 px = find_stktable(expr->arg.str);
2456
2457 if (!px)
2458 return 0; /* table not found */
2459
2460 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2461}
2462
Willy Tarreau8b22a712010-06-18 17:46:06 +02002463/* set test->i to the number of connections from the session's source address
2464 * in the table pointed to by expr, after updating it.
2465 */
2466static int
2467acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2468 struct acl_expr *expr, struct acl_test *test)
2469{
2470 struct stksess *ts;
2471 struct stktable_key *key;
2472 void *ptr;
2473
David du Colombier4f92d322011-03-24 11:09:31 +01002474 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002475 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002476 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002477
2478 if (expr->arg_len)
2479 px = find_stktable(expr->arg.str);
2480
2481 if (!px)
2482 return 0; /* table not found */
2483
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002484 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2485 /* entry does not exist and could not be created */
2486 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002487
2488 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2489 if (!ptr)
2490 return 0; /* parameter not stored in this table */
2491
2492 test->i = ++stktable_data_cast(ptr, conn_cnt);
2493 test->flags = ACL_TEST_F_VOL_TEST;
2494 return 1;
2495}
2496
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002497/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2498static int
2499acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2500{
2501 test->flags = ACL_TEST_F_VOL_TEST;
2502 test->i = 0;
2503
2504 if (ts != NULL) {
2505 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2506 if (!ptr)
2507 return 0; /* parameter not stored */
2508 test->i = stktable_data_cast(ptr, conn_cur);
2509 }
2510 return 1;
2511}
2512
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002513/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002514static int
Willy Tarreau56123282010-08-06 19:06:56 +02002515acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2516 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002517{
Willy Tarreau56123282010-08-06 19:06:56 +02002518 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002519 return 0;
2520
Willy Tarreau56123282010-08-06 19:06:56 +02002521 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002522}
2523
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002524/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2525static int
Willy Tarreau56123282010-08-06 19:06:56 +02002526acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2527 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002528{
Willy Tarreau56123282010-08-06 19:06:56 +02002529 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002530 return 0;
2531
Willy Tarreau56123282010-08-06 19:06:56 +02002532 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002533}
2534
Willy Tarreau38285c12010-06-18 16:35:43 +02002535/* set test->i to the number of concurrent connections from the session's source
2536 * address in the table pointed to by expr.
2537 */
2538static int
2539acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2540 struct acl_expr *expr, struct acl_test *test)
2541{
Willy Tarreau38285c12010-06-18 16:35:43 +02002542 struct stktable_key *key;
2543
David du Colombier4f92d322011-03-24 11:09:31 +01002544 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002545 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002546 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002547
2548 if (expr->arg_len)
2549 px = find_stktable(expr->arg.str);
2550
2551 if (!px)
2552 return 0; /* table not found */
2553
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002554 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002555}
2556
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002557/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2558static int
2559acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2560{
2561 test->flags = ACL_TEST_F_VOL_TEST;
2562 test->i = 0;
2563 if (ts != NULL) {
2564 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2565 if (!ptr)
2566 return 0; /* parameter not stored */
2567 test->i = stktable_data_cast(ptr, sess_cnt);
2568 }
2569 return 1;
2570}
2571
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002572/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002573static int
Willy Tarreau56123282010-08-06 19:06:56 +02002574acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2575 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002576{
Willy Tarreau56123282010-08-06 19:06:56 +02002577 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002578 return 0;
2579
Willy Tarreau56123282010-08-06 19:06:56 +02002580 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002581}
2582
2583/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2584static int
Willy Tarreau56123282010-08-06 19:06:56 +02002585acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2586 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002587{
Willy Tarreau56123282010-08-06 19:06:56 +02002588 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002589 return 0;
2590
Willy Tarreau56123282010-08-06 19:06:56 +02002591 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002592}
2593
2594/* set test->i to the cumulated number of session from the session's source
2595 * address in the table pointed to by expr.
2596 */
2597static int
2598acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2599 struct acl_expr *expr, struct acl_test *test)
2600{
2601 struct stktable_key *key;
2602
David du Colombier4f92d322011-03-24 11:09:31 +01002603 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002604 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002605 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002606
2607 if (expr->arg_len)
2608 px = find_stktable(expr->arg.str);
2609
2610 if (!px)
2611 return 0; /* table not found */
2612
2613 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2614}
2615
Willy Tarreau91c43d72010-06-20 11:19:22 +02002616/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2617static int
2618acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2619{
2620 test->flags = ACL_TEST_F_VOL_TEST;
2621 test->i = 0;
2622 if (ts != NULL) {
2623 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2624 if (!ptr)
2625 return 0; /* parameter not stored */
2626 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2627 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2628 }
2629 return 1;
2630}
2631
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002632/* set test->i to the session rate from the session's tracked FE counters over
2633 * the configured period.
2634 */
2635static int
Willy Tarreau56123282010-08-06 19:06:56 +02002636acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2637 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002638{
Willy Tarreau56123282010-08-06 19:06:56 +02002639 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002640 return 0;
2641
Willy Tarreau56123282010-08-06 19:06:56 +02002642 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002643}
2644
2645/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002646 * the configured period.
2647 */
2648static int
Willy Tarreau56123282010-08-06 19:06:56 +02002649acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2650 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002651{
Willy Tarreau56123282010-08-06 19:06:56 +02002652 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002653 return 0;
2654
Willy Tarreau56123282010-08-06 19:06:56 +02002655 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002656}
2657
2658/* set test->i to the session rate from the session's source address in the
2659 * table pointed to by expr, over the configured period.
2660 */
2661static int
2662acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2663 struct acl_expr *expr, struct acl_test *test)
2664{
2665 struct stktable_key *key;
2666
David du Colombier4f92d322011-03-24 11:09:31 +01002667 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002668 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002669 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002670
2671 if (expr->arg_len)
2672 px = find_stktable(expr->arg.str);
2673
2674 if (!px)
2675 return 0; /* table not found */
2676
2677 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2678}
2679
Willy Tarreauda7ff642010-06-23 11:44:09 +02002680/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2681static int
2682acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2683{
2684 test->flags = ACL_TEST_F_VOL_TEST;
2685 test->i = 0;
2686 if (ts != NULL) {
2687 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2688 if (!ptr)
2689 return 0; /* parameter not stored */
2690 test->i = stktable_data_cast(ptr, http_req_cnt);
2691 }
2692 return 1;
2693}
2694
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002695/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002696static int
Willy Tarreau56123282010-08-06 19:06:56 +02002697acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2698 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002699{
Willy Tarreau56123282010-08-06 19:06:56 +02002700 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002701 return 0;
2702
Willy Tarreau56123282010-08-06 19:06:56 +02002703 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002704}
2705
2706/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2707static int
Willy Tarreau56123282010-08-06 19:06:56 +02002708acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2709 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002710{
Willy Tarreau56123282010-08-06 19:06:56 +02002711 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002712 return 0;
2713
Willy Tarreau56123282010-08-06 19:06:56 +02002714 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002715}
2716
2717/* set test->i to the cumulated number of session from the session's source
2718 * address in the table pointed to by expr.
2719 */
2720static int
2721acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002722 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002723{
2724 struct stktable_key *key;
2725
David du Colombier4f92d322011-03-24 11:09:31 +01002726 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002727 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002728 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002729
2730 if (expr->arg_len)
2731 px = find_stktable(expr->arg.str);
2732
2733 if (!px)
2734 return 0; /* table not found */
2735
2736 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2737}
2738
2739/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2740static int
2741acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2742{
2743 test->flags = ACL_TEST_F_VOL_TEST;
2744 test->i = 0;
2745 if (ts != NULL) {
2746 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2747 if (!ptr)
2748 return 0; /* parameter not stored */
2749 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2750 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2751 }
2752 return 1;
2753}
2754
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002755/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002756 * the configured period.
2757 */
2758static int
Willy Tarreau56123282010-08-06 19:06:56 +02002759acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2760 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002761{
Willy Tarreau56123282010-08-06 19:06:56 +02002762 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002763 return 0;
2764
Willy Tarreau56123282010-08-06 19:06:56 +02002765 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002766}
2767
2768/* set test->i to the session rate from the session's tracked BE counters over
2769 * the configured period.
2770 */
2771static int
Willy Tarreau56123282010-08-06 19:06:56 +02002772acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2773 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002774{
Willy Tarreau56123282010-08-06 19:06:56 +02002775 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002776 return 0;
2777
Willy Tarreau56123282010-08-06 19:06:56 +02002778 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002779}
2780
2781/* set test->i to the session rate from the session's source address in the
2782 * table pointed to by expr, over the configured period.
2783 */
2784static int
2785acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002786 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002787{
2788 struct stktable_key *key;
2789
David du Colombier4f92d322011-03-24 11:09:31 +01002790 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002791 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002792 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002793
2794 if (expr->arg_len)
2795 px = find_stktable(expr->arg.str);
2796
2797 if (!px)
2798 return 0; /* table not found */
2799
2800 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2801}
2802
2803/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2804static int
2805acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2806{
2807 test->flags = ACL_TEST_F_VOL_TEST;
2808 test->i = 0;
2809 if (ts != NULL) {
2810 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2811 if (!ptr)
2812 return 0; /* parameter not stored */
2813 test->i = stktable_data_cast(ptr, http_err_cnt);
2814 }
2815 return 1;
2816}
2817
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002818/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002819static int
Willy Tarreau56123282010-08-06 19:06:56 +02002820acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2821 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002822{
Willy Tarreau56123282010-08-06 19:06:56 +02002823 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002824 return 0;
2825
Willy Tarreau56123282010-08-06 19:06:56 +02002826 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002827}
2828
2829/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2830static int
Willy Tarreau56123282010-08-06 19:06:56 +02002831acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2832 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002833{
Willy Tarreau56123282010-08-06 19:06:56 +02002834 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835 return 0;
2836
Willy Tarreau56123282010-08-06 19:06:56 +02002837 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838}
2839
2840/* set test->i to the cumulated number of session from the session's source
2841 * address in the table pointed to by expr.
2842 */
2843static int
2844acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002845 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002846{
2847 struct stktable_key *key;
2848
David du Colombier4f92d322011-03-24 11:09:31 +01002849 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002850 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002851 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002852
2853 if (expr->arg_len)
2854 px = find_stktable(expr->arg.str);
2855
2856 if (!px)
2857 return 0; /* table not found */
2858
2859 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2860}
2861
2862/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2863static int
2864acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2865{
2866 test->flags = ACL_TEST_F_VOL_TEST;
2867 test->i = 0;
2868 if (ts != NULL) {
2869 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2870 if (!ptr)
2871 return 0; /* parameter not stored */
2872 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2873 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2874 }
2875 return 1;
2876}
2877
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002878/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002879 * the configured period.
2880 */
2881static int
Willy Tarreau56123282010-08-06 19:06:56 +02002882acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2883 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002884{
Willy Tarreau56123282010-08-06 19:06:56 +02002885 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002886 return 0;
2887
Willy Tarreau56123282010-08-06 19:06:56 +02002888 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002889}
2890
2891/* set test->i to the session rate from the session's tracked BE counters over
2892 * the configured period.
2893 */
2894static int
Willy Tarreau56123282010-08-06 19:06:56 +02002895acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2896 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002897{
Willy Tarreau56123282010-08-06 19:06:56 +02002898 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002899 return 0;
2900
Willy Tarreau56123282010-08-06 19:06:56 +02002901 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002902}
2903
2904/* set test->i to the session rate from the session's source address in the
2905 * table pointed to by expr, over the configured period.
2906 */
2907static int
2908acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2909 struct acl_expr *expr, struct acl_test *test)
2910{
2911 struct stktable_key *key;
2912
David du Colombier4f92d322011-03-24 11:09:31 +01002913 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002914 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002915 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002916
2917 if (expr->arg_len)
2918 px = find_stktable(expr->arg.str);
2919
2920 if (!px)
2921 return 0; /* table not found */
2922
2923 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2924}
2925
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002926/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2927static int
2928acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2929{
2930 test->flags = ACL_TEST_F_VOL_TEST;
2931 test->i = 0;
2932
2933 if (ts != NULL) {
2934 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2935 if (!ptr)
2936 return 0; /* parameter not stored */
2937 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2938 }
2939 return 1;
2940}
2941
2942/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002943 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002944 */
2945static int
Willy Tarreau56123282010-08-06 19:06:56 +02002946acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2947 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002948{
Willy Tarreau56123282010-08-06 19:06:56 +02002949 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002950 return 0;
2951
Willy Tarreau56123282010-08-06 19:06:56 +02002952 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002953}
2954
2955/* set test->i to the number of kbytes received from clients according to the
2956 * session's tracked BE counters.
2957 */
2958static int
Willy Tarreau56123282010-08-06 19:06:56 +02002959acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2960 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002961{
Willy Tarreau56123282010-08-06 19:06:56 +02002962 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002963 return 0;
2964
Willy Tarreau56123282010-08-06 19:06:56 +02002965 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002966}
2967
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002968/* set test->i to the number of kbytes received from the session's source
2969 * address in the table pointed to by expr.
2970 */
2971static int
2972acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2973 struct acl_expr *expr, struct acl_test *test)
2974{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002975 struct stktable_key *key;
2976
David du Colombier4f92d322011-03-24 11:09:31 +01002977 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002978 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002979 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002980
2981 if (expr->arg_len)
2982 px = find_stktable(expr->arg.str);
2983
2984 if (!px)
2985 return 0; /* table not found */
2986
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002987 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2988}
2989
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002990/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2991 * configured period.
2992 */
2993static int
2994acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2995{
2996 test->flags = ACL_TEST_F_VOL_TEST;
2997 test->i = 0;
2998 if (ts != NULL) {
2999 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3000 if (!ptr)
3001 return 0; /* parameter not stored */
3002 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
3003 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3004 }
3005 return 1;
3006}
3007
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003008/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003009 * counters over the configured period.
3010 */
3011static int
Willy Tarreau56123282010-08-06 19:06:56 +02003012acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3013 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003014{
Willy Tarreau56123282010-08-06 19:06:56 +02003015 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003016 return 0;
3017
Willy Tarreau56123282010-08-06 19:06:56 +02003018 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003019}
3020
3021/* set test->i to the bytes rate from clients from the session's tracked BE
3022 * counters over the configured period.
3023 */
3024static int
Willy Tarreau56123282010-08-06 19:06:56 +02003025acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3026 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003027{
Willy Tarreau56123282010-08-06 19:06:56 +02003028 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003029 return 0;
3030
Willy Tarreau56123282010-08-06 19:06:56 +02003031 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003032}
3033
3034/* set test->i to the bytes rate from clients from the session's source address
3035 * in the table pointed to by expr, over the configured period.
3036 */
3037static int
3038acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003039 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003040{
3041 struct stktable_key *key;
3042
David du Colombier4f92d322011-03-24 11:09:31 +01003043 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003044 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003045 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003046
3047 if (expr->arg_len)
3048 px = find_stktable(expr->arg.str);
3049
3050 if (!px)
3051 return 0; /* table not found */
3052
3053 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3054}
3055
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003056/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3057static int
3058acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3059{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003060 test->flags = ACL_TEST_F_VOL_TEST;
3061 test->i = 0;
3062
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003063 if (ts != NULL) {
3064 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003065 if (!ptr)
3066 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003067 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003068 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003069 return 1;
3070}
3071
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003072/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003073 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003074 */
3075static int
Willy Tarreau56123282010-08-06 19:06:56 +02003076acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3077 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003078{
Willy Tarreau56123282010-08-06 19:06:56 +02003079 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003080 return 0;
3081
Willy Tarreau56123282010-08-06 19:06:56 +02003082 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003083}
3084
3085/* set test->i to the number of kbytes sent to clients according to the session's
3086 * tracked BE counters.
3087 */
3088static int
Willy Tarreau56123282010-08-06 19:06:56 +02003089acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3090 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003091{
Willy Tarreau56123282010-08-06 19:06:56 +02003092 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003093 return 0;
3094
Willy Tarreau56123282010-08-06 19:06:56 +02003095 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003096}
3097
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003098/* set test->i to the number of kbytes sent to the session's source address in
3099 * the table pointed to by expr.
3100 */
3101static int
3102acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3103 struct acl_expr *expr, struct acl_test *test)
3104{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003105 struct stktable_key *key;
3106
David du Colombier4f92d322011-03-24 11:09:31 +01003107 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003108 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003109 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003110
3111 if (expr->arg_len)
3112 px = find_stktable(expr->arg.str);
3113
3114 if (!px)
3115 return 0; /* table not found */
3116
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003117 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003118}
3119
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003120/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3121 * configured period.
3122 */
3123static int
3124acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3125{
3126 test->flags = ACL_TEST_F_VOL_TEST;
3127 test->i = 0;
3128 if (ts != NULL) {
3129 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3130 if (!ptr)
3131 return 0; /* parameter not stored */
3132 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3133 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3134 }
3135 return 1;
3136}
3137
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003138/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003139 * over the configured period.
3140 */
3141static int
Willy Tarreau56123282010-08-06 19:06:56 +02003142acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3143 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003144{
Willy Tarreau56123282010-08-06 19:06:56 +02003145 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003146 return 0;
3147
Willy Tarreau56123282010-08-06 19:06:56 +02003148 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003149}
3150
3151/* set test->i to the bytes rate to clients from the session's tracked BE counters
3152 * over the configured period.
3153 */
3154static int
Willy Tarreau56123282010-08-06 19:06:56 +02003155acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3156 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003157{
Willy Tarreau56123282010-08-06 19:06:56 +02003158 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003159 return 0;
3160
Willy Tarreau56123282010-08-06 19:06:56 +02003161 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003162}
3163
3164/* set test->i to the bytes rate to client from the session's source address in
3165 * the table pointed to by expr, over the configured period.
3166 */
3167static int
3168acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003169 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003170{
3171 struct stktable_key *key;
3172
David du Colombier4f92d322011-03-24 11:09:31 +01003173 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003174 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003175 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003176
3177 if (expr->arg_len)
3178 px = find_stktable(expr->arg.str);
3179
3180 if (!px)
3181 return 0; /* table not found */
3182
3183 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3184}
3185
Willy Tarreauc735a072011-03-29 00:57:02 +02003186/* set test->i to the number of used entries in the table pointed to by expr. */
3187static int
3188acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3189 struct acl_expr *expr, struct acl_test *test)
3190{
3191 if (expr->arg_len)
3192 px = find_stktable(expr->arg.str);
3193
3194 if (!px)
3195 return 0; /* table not found */
3196
3197 test->flags = ACL_TEST_F_VOL_TEST;
3198 test->i = px->table.current;
3199 return 1;
3200}
3201
3202/* set test->i to the number of free entries in the table pointed to by expr. */
3203static int
3204acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3205 struct acl_expr *expr, struct acl_test *test)
3206{
3207 if (expr->arg_len)
3208 px = find_stktable(expr->arg.str);
3209
3210 if (!px)
3211 return 0; /* table not found */
3212
3213 test->flags = ACL_TEST_F_VOL_TEST;
3214 test->i = px->table.size - px->table.current;
3215 return 1;
3216}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003217
3218/* Note: must not be declared <const> as its list will be overwritten */
3219static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003220 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3221 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3222 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3223 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3224 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3225 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3226 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3227 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3228 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3229 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3230 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3231 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3232 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3233 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3234 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3235 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3236 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3237 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3238 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3239 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3240 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3241 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3242 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3243 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3244 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3245 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3246 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3247 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3248 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3249 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3250 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3251 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3252 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3253 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3254 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3255 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3256 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3257 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3258 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3259 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3260 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3261 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3262 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3263 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3264 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3265 { "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 +02003266 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3267 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003268 { NULL, NULL, NULL, NULL },
3269}};
3270
3271
Willy Tarreau56123282010-08-06 19:06:56 +02003272/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003273 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3274 * <prm> struct is fed with the table name if any. If unspecified, the caller
3275 * will assume that the current proxy's table is used.
3276 */
3277int parse_track_counters(char **args, int *arg,
3278 int section_type, struct proxy *curpx,
3279 struct track_ctr_prm *prm,
3280 struct proxy *defpx, char *err, int errlen)
3281{
3282 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003283 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003284
Willy Tarreau56123282010-08-06 19:06:56 +02003285 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003286 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003287 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003288 */
3289 while (args[*arg]) {
3290 if (strcmp(args[*arg], "src") == 0) {
3291 prm->type = STKTABLE_TYPE_IP;
3292 pattern_type = 1;
3293 }
3294 else if (strcmp(args[*arg], "table") == 0) {
3295 if (!args[*arg + 1]) {
3296 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003297 "missing table for %s in %s '%s'.",
3298 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003299 return -1;
3300 }
3301 /* we copy the table name for now, it will be resolved later */
3302 prm->table.n = strdup(args[*arg + 1]);
3303 (*arg)++;
3304 }
3305 else {
3306 /* unhandled keywords are handled by the caller */
3307 break;
3308 }
3309 (*arg)++;
3310 }
3311
3312 if (!pattern_type) {
3313 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003314 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3315 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003316 return -1;
3317 }
3318
3319 return 0;
3320}
3321
Willy Tarreau8b22a712010-06-18 17:46:06 +02003322__attribute__((constructor))
3323static void __session_init(void)
3324{
3325 acl_register_keywords(&acl_kws);
3326}
3327
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328/*
3329 * Local variables:
3330 * c-indent-level: 8
3331 * c-basic-offset: 8
3332 * End:
3333 */