blob: 56d0c8f8bf6b7376c9ad82b634fff71893c09575 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100133 if (p->feconn > p->fe_counters.conn_max)
134 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100171 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200191 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200192 s->si[1].err_loc = NULL;
193 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200194 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100195 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200196 s->si[1].shutr = stream_int_shutr;
197 s->si[1].shutw = stream_int_shutw;
198 s->si[1].exp = TICK_ETERNITY;
199 s->si[1].flags = SI_FL_NONE;
200
201 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
202 s->si[1].flags |= SI_FL_INDEP_STR;
203
Simon Hormanaf514952011-06-21 14:34:57 +0900204 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100205 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206 s->pend_pos = NULL;
207
208 /* init store persistence */
209 s->store_count = 0;
210
211 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100212 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214
215 txn = &s->txn;
216 /* Those variables will be checked and freed if non-NULL in
217 * session.c:session_free(). It is important that they are
218 * properly initialized.
219 */
220 txn->sessid = NULL;
221 txn->srv_cookie = NULL;
222 txn->cli_cookie = NULL;
223 txn->uri = NULL;
224 txn->req.cap = NULL;
225 txn->rsp.cap = NULL;
226 txn->hdr_idx.v = NULL;
227 txn->hdr_idx.size = txn->hdr_idx.used = 0;
228
229 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
230 goto out_free_task; /* no memory */
231
232 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
233 goto out_free_req; /* no memory */
234
235 /* initialize the request buffer */
236 s->req->size = global.tune.bufsize;
237 buffer_init(s->req);
238 s->req->prod = &s->si[0];
239 s->req->cons = &s->si[1];
240 s->si[0].ib = s->si[1].ob = s->req;
241 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
242
243 /* activate default analysers enabled for this listener */
244 s->req->analysers = l->analysers;
245
246 s->req->wto = TICK_ETERNITY;
247 s->req->rto = TICK_ETERNITY;
248 s->req->rex = TICK_ETERNITY;
249 s->req->wex = TICK_ETERNITY;
250 s->req->analyse_exp = TICK_ETERNITY;
251
252 /* initialize response buffer */
253 s->rep->size = global.tune.bufsize;
254 buffer_init(s->rep);
255 s->rep->prod = &s->si[1];
256 s->rep->cons = &s->si[0];
257 s->si[0].ob = s->si[1].ib = s->rep;
258 s->rep->analysers = 0;
259
Willy Tarreau96e31212011-05-30 18:10:30 +0200260 if (s->fe->options2 & PR_O2_NODELAY) {
261 s->req->flags |= BF_NEVER_WAIT;
262 s->rep->flags |= BF_NEVER_WAIT;
263 }
264
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200265 s->rep->rto = TICK_ETERNITY;
266 s->rep->wto = TICK_ETERNITY;
267 s->rep->rex = TICK_ETERNITY;
268 s->rep->wex = TICK_ETERNITY;
269 s->rep->analyse_exp = TICK_ETERNITY;
270
271 /* finish initialization of the accepted file descriptor */
272 fd_insert(cfd);
273 fdtab[cfd].owner = &s->si[0];
274 fdtab[cfd].state = FD_STREADY;
275 fdtab[cfd].flags = 0;
276 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
277 fdtab[cfd].cb[DIR_RD].b = s->req;
278 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
279 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100280 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.c.from;
281 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.c.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200282 EV_FD_SET(cfd, DIR_RD);
283
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100284 if (p->accept && (ret = p->accept(s)) <= 0) {
285 /* Either we had an unrecoverable error (<0) or work is
286 * finished (=0, eg: monitoring), in both situations,
287 * we can release everything and close.
288 */
289 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200290 }
291
292 /* it is important not to call the wakeup function directly but to
293 * pass through task_wakeup(), because this one knows how to apply
294 * priorities to tasks.
295 */
296 task_wakeup(t, TASK_WOKEN_INIT);
297 return 1;
298
299 /* Error unrolling */
300 out_free_rep:
301 pool_free2(pool2_buffer, s->rep);
302 out_free_req:
303 pool_free2(pool2_buffer, s->req);
304 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200305 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200306 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200307 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200308 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200309 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100310 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200311 pool_free2(pool2_session, s);
312 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100313 if (fdtab[cfd].state != FD_STCLOSE)
314 fd_delete(cfd);
315 else
316 close(cfd);
317 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200318}
319
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320/*
321 * frees the context associated to a session. It must have been removed first.
322 */
Simon Hormandec5be42011-06-08 09:19:07 +0900323static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100325 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200326 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100327 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200328 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100329
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 if (s->pend_pos)
331 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100332
Willy Tarreau827aee92011-03-10 16:55:02 +0100333 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100334 if (s->flags & SN_CURR_SESS) {
335 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100336 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100337 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100338 if (may_dequeue_tasks(target_srv(&s->target), s->be))
339 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100340 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100341
Willy Tarreau7c669d72008-06-20 15:04:11 +0200342 if (unlikely(s->srv_conn)) {
343 /* the session still has a reserved slot on a server, but
344 * it should normally be only the same as the one above,
345 * so this should not happen in fact.
346 */
347 sess_change_server(s, NULL);
348 }
349
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100350 if (s->req->pipe)
351 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100352
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100353 if (s->rep->pipe)
354 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100355
Willy Tarreau48d63db2008-08-03 17:41:33 +0200356 pool_free2(pool2_buffer, s->req);
357 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
Willy Tarreau46023632010-01-07 22:51:47 +0100359 http_end_txn(s);
360
Willy Tarreaua4cda672010-06-06 18:28:49 +0200361 for (i = 0; i < s->store_count; i++) {
362 if (!s->store[i].ts)
363 continue;
364 stksess_free(s->store[i].table, s->store[i].ts);
365 s->store[i].ts = NULL;
366 }
367
Willy Tarreau92fb9832007-10-16 17:34:28 +0200368 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200369 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100370 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
371 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100373
Willy Tarreau56123282010-08-06 19:06:56 +0200374 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200375 session_store_counters(s);
376
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100377 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100378 /* we have to unlink all watchers. We must not relink them if
379 * this session was the last one in the list.
380 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100381 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100382 LIST_INIT(&bref->users);
383 if (s->list.n != &sessions)
384 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100385 bref->ref = s->list.n;
386 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100387 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200388 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200389
390 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200391 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200392 pool_flush2(pool2_buffer);
393 pool_flush2(fe->hdr_idx_pool);
394 pool_flush2(pool2_requri);
395 pool_flush2(pool2_capture);
396 pool_flush2(pool2_session);
397 pool_flush2(fe->req_cap_pool);
398 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200399 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200400}
401
402
403/* perform minimal intializations, report 0 in case of error, 1 if OK. */
404int init_session()
405{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100406 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200407 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
408 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409}
410
Willy Tarreau30e71012007-11-26 20:15:35 +0100411void session_process_counters(struct session *s)
412{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100413 unsigned long long bytes;
414
Willy Tarreau30e71012007-11-26 20:15:35 +0100415 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100416 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100417 s->logs.bytes_in = s->req->total;
418 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100419 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100420
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100421 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422
Willy Tarreau827aee92011-03-10 16:55:02 +0100423 if (target_srv(&s->target))
424 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200425
426 if (s->listener->counters)
427 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200428
Willy Tarreau56123282010-08-06 19:06:56 +0200429 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200430 void *ptr;
431
Willy Tarreau56123282010-08-06 19:06:56 +0200432 ptr = stktable_data_ptr(s->stkctr2_table,
433 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200434 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435 if (ptr)
436 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200437
Willy Tarreau56123282010-08-06 19:06:56 +0200438 ptr = stktable_data_ptr(s->stkctr2_table,
439 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200440 STKTABLE_DT_BYTES_IN_RATE);
441 if (ptr)
442 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200443 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200444 }
445
Willy Tarreau56123282010-08-06 19:06:56 +0200446 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200447 void *ptr;
448
Willy Tarreau56123282010-08-06 19:06:56 +0200449 ptr = stktable_data_ptr(s->stkctr1_table,
450 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 STKTABLE_DT_BYTES_IN_CNT);
452 if (ptr)
453 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
454
Willy Tarreau56123282010-08-06 19:06:56 +0200455 ptr = stktable_data_ptr(s->stkctr1_table,
456 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200457 STKTABLE_DT_BYTES_IN_RATE);
458 if (ptr)
459 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200460 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200461 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100462 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100463 }
464
Willy Tarreau30e71012007-11-26 20:15:35 +0100465 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100466 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100467 s->logs.bytes_out = s->rep->total;
468 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100469 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100471 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472
Willy Tarreau827aee92011-03-10 16:55:02 +0100473 if (target_srv(&s->target))
474 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200475
476 if (s->listener->counters)
477 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200478
Willy Tarreau56123282010-08-06 19:06:56 +0200479 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200480 void *ptr;
481
Willy Tarreau56123282010-08-06 19:06:56 +0200482 ptr = stktable_data_ptr(s->stkctr2_table,
483 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200484 STKTABLE_DT_BYTES_OUT_CNT);
485 if (ptr)
486 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
487
Willy Tarreau56123282010-08-06 19:06:56 +0200488 ptr = stktable_data_ptr(s->stkctr2_table,
489 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200490 STKTABLE_DT_BYTES_OUT_RATE);
491 if (ptr)
492 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200493 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200494 }
495
Willy Tarreau56123282010-08-06 19:06:56 +0200496 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200497 void *ptr;
498
Willy Tarreau56123282010-08-06 19:06:56 +0200499 ptr = stktable_data_ptr(s->stkctr1_table,
500 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200501 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200502 if (ptr)
503 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504
Willy Tarreau56123282010-08-06 19:06:56 +0200505 ptr = stktable_data_ptr(s->stkctr1_table,
506 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200507 STKTABLE_DT_BYTES_OUT_RATE);
508 if (ptr)
509 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200510 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200511 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100512 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100513 }
514}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100516/* This function is called with (si->state == SI_ST_CON) meaning that a
517 * connection was attempted and that the file descriptor is already allocated.
518 * We must check for establishment, error and abort. Possible output states
519 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
520 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
521 * otherwise 1.
522 */
Simon Hormandec5be42011-06-08 09:19:07 +0900523static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524{
525 struct buffer *req = si->ob;
526 struct buffer *rep = si->ib;
527
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100528 /* If we got an error, or if nothing happened and the connection timed
529 * out, we must give up. The CER state handler will take care of retry
530 * attempts and error reports.
531 */
532 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100533 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200535 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 fd_delete(si->fd);
537
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200538 if (si->release)
539 si->release(si);
540
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 if (si->err_type)
542 return 0;
543
Willy Tarreau827aee92011-03-10 16:55:02 +0100544 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100545 if (si->flags & SI_FL_ERR)
546 si->err_type = SI_ET_CONN_ERR;
547 else
548 si->err_type = SI_ET_CONN_TO;
549 return 0;
550 }
551
552 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200553 if (unlikely((rep->flags & BF_SHUTW) ||
554 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200555 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100556 s->be->options & PR_O_ABRT_CLOSE)))) {
557 /* give up */
558 si->shutw(si);
559 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100560 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200561 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100562 if (s->srv_error)
563 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 return 1;
565 }
566
567 /* we need to wait a bit more if there was no activity either */
568 if (!(req->flags & BF_WRITE_ACTIVITY))
569 return 1;
570
571 /* OK, this means that a connection succeeded. The caller will be
572 * responsible for handling the transition from CON to EST.
573 */
574 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100575 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100576 si->state = SI_ST_EST;
577 si->err_type = SI_ET_NONE;
578 si->err_loc = NULL;
579 return 1;
580}
581
582/* This function is called with (si->state == SI_ST_CER) meaning that a
583 * previous connection attempt has failed and that the file descriptor
584 * has already been released. Possible causes include asynchronous error
585 * notification and time out. Possible output states are SI_ST_CLO when
586 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
587 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
588 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
589 * marked as in error state. It returns 0.
590 */
Simon Hormandec5be42011-06-08 09:19:07 +0900591static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592{
593 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 if (target_srv(&s->target)) {
595 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100596
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 if (s->flags & SN_CURR_SESS) {
598 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100599 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 }
602
603 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200604 si->conn_retries--;
605 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 if (!si->err_type) {
607 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 }
610
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 if (target_srv(&s->target))
612 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100613 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100614 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 if (may_dequeue_tasks(target_srv(&s->target), s->be))
616 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617
618 /* shutw is enough so stop a connecting socket */
619 si->shutw(si);
620 si->ob->flags |= BF_WRITE_ERROR;
621 si->ib->flags |= BF_READ_ERROR;
622
623 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100624 if (s->srv_error)
625 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100626 return 0;
627 }
628
629 /* If the "redispatch" option is set on the backend, we are allowed to
630 * retry on another server for the last retry. In order to achieve this,
631 * we must mark the session unassigned, and eventually clear the DIRECT
632 * bit to ignore any persistence cookie. We won't count a retry nor a
633 * redispatch yet, because this will depend on what server is selected.
634 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100636 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100637 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100638 if (may_dequeue_tasks(target_srv(&s->target), s->be))
639 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640
641 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 si->state = SI_ST_REQ;
643 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 if (target_srv(&s->target))
645 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100646 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 si->state = SI_ST_ASS;
648 }
649
650 if (si->flags & SI_FL_ERR) {
651 /* The error was an asynchronous connection error, and we will
652 * likely have to retry connecting to the same server, most
653 * likely leading to the same result. To avoid this, we wait
654 * one second before retrying.
655 */
656
657 if (!si->err_type)
658 si->err_type = SI_ET_CONN_ERR;
659
660 si->state = SI_ST_TAR;
661 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
662 return 0;
663 }
664 return 0;
665}
666
667/*
668 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200669 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
670 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 */
Simon Hormandec5be42011-06-08 09:19:07 +0900672static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673{
674 struct buffer *req = si->ob;
675 struct buffer *rep = si->ib;
676
Willy Tarreau827aee92011-03-10 16:55:02 +0100677 if (target_srv(&s->target))
678 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100679
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 /* if the user wants to log as soon as possible, without counting
682 * bytes from the server, then this is the right moment. */
683 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
684 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100685 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 }
688 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
690 /* reset hdr_idx which was already initialized by the request.
691 * right now, the http parser does it.
692 * hdr_idx_init(&s->txn.hdr_idx);
693 */
694 }
695
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200696 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200698 if (si->connect) {
699 /* real connections have timeouts */
700 req->wto = s->be->timeout.server;
701 rep->rto = s->be->timeout.server;
702 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 req->wex = TICK_ETERNITY;
704}
705
706/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
707 * Other input states are simply ignored.
708 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
709 * Flags must have previously been updated for timeouts and other conditions.
710 */
Simon Hormandec5be42011-06-08 09:19:07 +0900711static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712{
Willy Tarreau827aee92011-03-10 16:55:02 +0100713 struct server *srv = target_srv(&s->target);
714
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
716 now_ms, __FUNCTION__,
717 s,
718 s->req, s->rep,
719 s->req->rex, s->rep->wex,
720 s->req->flags, s->rep->flags,
721 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
722
723 if (si->state == SI_ST_ASS) {
724 /* Server assigned to connection request, we have to try to connect now */
725 int conn_err;
726
727 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100728 srv = target_srv(&s->target);
729
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 if (conn_err == SN_ERR_NONE) {
731 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100732 if (srv)
733 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100734 return;
735 }
736
737 /* We have received a synchronous error. We might have to
738 * abort, retry immediately or redispatch.
739 */
740 if (conn_err == SN_ERR_INTERNAL) {
741 if (!si->err_type) {
742 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
745
Willy Tarreau827aee92011-03-10 16:55:02 +0100746 if (srv)
747 srv_inc_sess_ctr(srv);
748 if (srv)
749 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100750 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751
752 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100753 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100754 if (may_dequeue_tasks(srv, s->be))
755 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756
757 /* Failed and not retryable. */
758 si->shutr(si);
759 si->shutw(si);
760 si->ob->flags |= BF_WRITE_ERROR;
761
762 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
763
764 /* no session was ever accounted for this server */
765 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100766 if (s->srv_error)
767 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768 return;
769 }
770
771 /* We are facing a retryable error, but we don't want to run a
772 * turn-around now, as the problem is likely a source port
773 * allocation problem, so we want to retry now.
774 */
775 si->state = SI_ST_CER;
776 si->flags &= ~SI_FL_ERR;
777 sess_update_st_cer(s, si);
778 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
779 return;
780 }
781 else if (si->state == SI_ST_QUE) {
782 /* connection request was queued, check for any update */
783 if (!s->pend_pos) {
784 /* The connection is not in the queue anymore. Either
785 * we have a server connection slot available and we
786 * go directly to the assigned state, or we need to
787 * load-balance first and go to the INI state.
788 */
789 si->exp = TICK_ETERNITY;
790 if (unlikely(!(s->flags & SN_ASSIGNED)))
791 si->state = SI_ST_REQ;
792 else {
793 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
794 si->state = SI_ST_ASS;
795 }
796 return;
797 }
798
799 /* Connection request still in queue... */
800 if (si->flags & SI_FL_EXP) {
801 /* ... and timeout expired */
802 si->exp = TICK_ETERNITY;
803 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100804 if (srv)
805 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100806 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100807 si->shutr(si);
808 si->shutw(si);
809 si->ob->flags |= BF_WRITE_TIMEOUT;
810 if (!si->err_type)
811 si->err_type = SI_ET_QUEUE_TO;
812 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100813 if (s->srv_error)
814 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100815 return;
816 }
817
818 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200819 if ((si->ob->flags & (BF_READ_ERROR)) ||
820 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200821 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 /* give up */
823 si->exp = TICK_ETERNITY;
824 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
825 si->shutr(si);
826 si->shutw(si);
827 si->err_type |= SI_ET_QUEUE_ABRT;
828 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829 if (s->srv_error)
830 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 return;
832 }
833
834 /* Nothing changed */
835 return;
836 }
837 else if (si->state == SI_ST_TAR) {
838 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200839 if ((si->ob->flags & (BF_READ_ERROR)) ||
840 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200841 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 /* give up */
843 si->exp = TICK_ETERNITY;
844 si->shutr(si);
845 si->shutw(si);
846 si->err_type |= SI_ET_CONN_ABRT;
847 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100848 if (s->srv_error)
849 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850 return;
851 }
852
853 if (!(si->flags & SI_FL_EXP))
854 return; /* still in turn-around */
855
856 si->exp = TICK_ETERNITY;
857
858 /* we keep trying on the same server as long as the session is
859 * marked "assigned".
860 * FIXME: Should we force a redispatch attempt when the server is down ?
861 */
862 if (s->flags & SN_ASSIGNED)
863 si->state = SI_ST_ASS;
864 else
865 si->state = SI_ST_REQ;
866 return;
867 }
868}
869
Simon Hormandec5be42011-06-08 09:19:07 +0900870/* Set correct session termination flags in case no analyser has done it. It
871 * also counts a failed request if the server state has not reached the request
872 * stage.
873 */
874static void sess_set_term_flags(struct session *s)
875{
876 if (!(s->flags & SN_FINST_MASK)) {
877 if (s->si[1].state < SI_ST_REQ) {
878
879 s->fe->fe_counters.failed_req++;
880 if (s->listener->counters)
881 s->listener->counters->failed_req++;
882
883 s->flags |= SN_FINST_R;
884 }
885 else if (s->si[1].state == SI_ST_QUE)
886 s->flags |= SN_FINST_Q;
887 else if (s->si[1].state < SI_ST_EST)
888 s->flags |= SN_FINST_C;
889 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
890 s->flags |= SN_FINST_D;
891 else
892 s->flags |= SN_FINST_L;
893 }
894}
895
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100896/* This function initiates a server connection request on a stream interface
897 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
898 * indicating that a server has been assigned. It may also return SI_ST_QUE,
899 * or SI_ST_CLO upon error.
900 */
901static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
902 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
903 now_ms, __FUNCTION__,
904 s,
905 s->req, s->rep,
906 s->req->rex, s->rep->wex,
907 s->req->flags, s->rep->flags,
908 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
909
910 if (si->state != SI_ST_REQ)
911 return;
912
913 /* Try to assign a server */
914 if (srv_redispatch_connect(s) != 0) {
915 /* We did not get a server. Either we queued the
916 * connection request, or we encountered an error.
917 */
918 if (si->state == SI_ST_QUE)
919 return;
920
921 /* we did not get any server, let's check the cause */
922 si->shutr(si);
923 si->shutw(si);
924 si->ob->flags |= BF_WRITE_ERROR;
925 if (!si->err_type)
926 si->err_type = SI_ET_CONN_OTHER;
927 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100928 if (s->srv_error)
929 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 return;
931 }
932
933 /* The server is assigned */
934 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
935 si->state = SI_ST_ASS;
936}
937
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200938/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100939 * if appropriate. The default_backend rule is also considered, then the
940 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200941 * It returns 1 if the processing can continue on next analysers, or zero if it
942 * either needs more data or wants to immediately abort the request.
943 */
Simon Hormandec5be42011-06-08 09:19:07 +0900944static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200946 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100947
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200948 req->analysers &= ~an_bit;
949 req->analyse_exp = TICK_ETERNITY;
950
951 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
952 now_ms, __FUNCTION__,
953 s,
954 req,
955 req->rex, req->wex,
956 req->flags,
957 req->l,
958 req->analysers);
959
960 /* now check whether we have some switching rules for this request */
961 if (!(s->flags & SN_BE_ASSIGNED)) {
962 struct switching_rule *rule;
963
964 list_for_each_entry(rule, &s->fe->switching_rules, list) {
965 int ret;
966
967 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
968 ret = acl_pass(ret);
969 if (rule->cond->pol == ACL_COND_UNLESS)
970 ret = !ret;
971
972 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200973 if (!session_set_backend(s, rule->be.backend))
974 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200975 break;
976 }
977 }
978
979 /* To ensure correct connection accounting on the backend, we
980 * have to assign one if it was not set (eg: a listen). This
981 * measure also takes care of correctly setting the default
982 * backend if any.
983 */
984 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200985 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
986 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200987 }
988
Willy Tarreaufb356202010-08-03 14:02:05 +0200989 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
990 if (s->fe == s->be) {
991 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200992 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200993 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200994
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200995 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +0100996 * persistence rule, and report that in the session.
997 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200998 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100999 int ret = 1;
1000
1001 if (prst_rule->cond) {
1002 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1003 ret = acl_pass(ret);
1004 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1005 ret = !ret;
1006 }
1007
1008 if (ret) {
1009 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001010 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1011 s->flags |= SN_FORCE_PRST;
1012 } else {
1013 s->flags |= SN_IGNORE_PRST;
1014 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001015 break;
1016 }
1017 }
1018
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001019 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001020
1021 sw_failed:
1022 /* immediately abort this request in case of allocation failure */
1023 buffer_abort(s->req);
1024 buffer_abort(s->rep);
1025
1026 if (!(s->flags & SN_ERR_MASK))
1027 s->flags |= SN_ERR_RESOURCE;
1028 if (!(s->flags & SN_FINST_MASK))
1029 s->flags |= SN_FINST_R;
1030
1031 s->txn.status = 500;
1032 s->req->analysers = 0;
1033 s->req->analyse_exp = TICK_ETERNITY;
1034 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001035}
1036
Emeric Brun1d33b292010-01-04 15:47:17 +01001037/* This stream analyser works on a request. It applies all sticking rules on
1038 * it then returns 1. The data must already be present in the buffer otherwise
1039 * they won't match. It always returns 1.
1040 */
Simon Hormandec5be42011-06-08 09:19:07 +09001041static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001042{
1043 struct proxy *px = s->be;
1044 struct sticking_rule *rule;
1045
1046 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1047 now_ms, __FUNCTION__,
1048 s,
1049 req,
1050 req->rex, req->wex,
1051 req->flags,
1052 req->l,
1053 req->analysers);
1054
1055 list_for_each_entry(rule, &px->sticking_rules, list) {
1056 int ret = 1 ;
1057 int i;
1058
1059 for (i = 0; i < s->store_count; i++) {
1060 if (rule->table.t == s->store[i].table)
1061 break;
1062 }
1063
1064 if (i != s->store_count)
1065 continue;
1066
1067 if (rule->cond) {
1068 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1069 ret = acl_pass(ret);
1070 if (rule->cond->pol == ACL_COND_UNLESS)
1071 ret = !ret;
1072 }
1073
1074 if (ret) {
1075 struct stktable_key *key;
1076
Emeric Brun485479d2010-09-23 18:02:19 +02001077 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001078 if (!key)
1079 continue;
1080
1081 if (rule->flags & STK_IS_MATCH) {
1082 struct stksess *ts;
1083
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001084 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001085 if (!(s->flags & SN_ASSIGNED)) {
1086 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001087 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001088
1089 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001090 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1091 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001092 if (node) {
1093 struct server *srv;
1094
1095 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001096 if ((srv->state & SRV_RUNNING) ||
1097 (px->options & PR_O_PERSIST) ||
1098 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001099 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001100 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001101 }
1102 }
1103 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001104 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001105 }
1106 }
1107 if (rule->flags & STK_IS_STORE) {
1108 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1109 struct stksess *ts;
1110
1111 ts = stksess_new(rule->table.t, key);
1112 if (ts) {
1113 s->store[s->store_count].table = rule->table.t;
1114 s->store[s->store_count++].ts = ts;
1115 }
1116 }
1117 }
1118 }
1119 }
1120
1121 req->analysers &= ~an_bit;
1122 req->analyse_exp = TICK_ETERNITY;
1123 return 1;
1124}
1125
1126/* This stream analyser works on a response. It applies all store rules on it
1127 * then returns 1. The data must already be present in the buffer otherwise
1128 * they won't match. It always returns 1.
1129 */
Simon Hormandec5be42011-06-08 09:19:07 +09001130static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001131{
1132 struct proxy *px = s->be;
1133 struct sticking_rule *rule;
1134 int i;
1135
1136 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1137 now_ms, __FUNCTION__,
1138 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001139 rep,
1140 rep->rex, rep->wex,
1141 rep->flags,
1142 rep->l,
1143 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001144
1145 list_for_each_entry(rule, &px->storersp_rules, list) {
1146 int ret = 1 ;
1147 int storereqidx = -1;
1148
1149 for (i = 0; i < s->store_count; i++) {
1150 if (rule->table.t == s->store[i].table) {
1151 if (!(s->store[i].flags))
1152 storereqidx = i;
1153 break;
1154 }
1155 }
1156
1157 if ((i != s->store_count) && (storereqidx == -1))
1158 continue;
1159
1160 if (rule->cond) {
1161 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1162 ret = acl_pass(ret);
1163 if (rule->cond->pol == ACL_COND_UNLESS)
1164 ret = !ret;
1165 }
1166
1167 if (ret) {
1168 struct stktable_key *key;
1169
Emeric Brun485479d2010-09-23 18:02:19 +02001170 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001171 if (!key)
1172 continue;
1173
1174 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001175 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 s->store[storereqidx].flags = 1;
1177 }
1178 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1179 struct stksess *ts;
1180
1181 ts = stksess_new(rule->table.t, key);
1182 if (ts) {
1183 s->store[s->store_count].table = rule->table.t;
1184 s->store[s->store_count].flags = 1;
1185 s->store[s->store_count++].ts = ts;
1186 }
1187 }
1188 }
1189 }
1190
1191 /* process store request and store response */
1192 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001193 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001194 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001195
1196 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1197 if (ts) {
1198 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001199 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001200 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001201 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001202 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001203 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001204
1205 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001206 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001207 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001208 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001209 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001210
1211 rep->analysers &= ~an_bit;
1212 rep->analyse_exp = TICK_ETERNITY;
1213 return 1;
1214}
1215
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001216/* This macro is very specific to the function below. See the comments in
1217 * process_session() below to understand the logic and the tests.
1218 */
1219#define UPDATE_ANALYSERS(real, list, back, flag) { \
1220 list = (((list) & ~(flag)) | ~(back)) & (real); \
1221 back = real; \
1222 if (!(list)) \
1223 break; \
1224 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1225 continue; \
1226}
1227
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001228/* Processes the client, server, request and response jobs of a session task,
1229 * then puts it back to the wait queue in a clean state, or cleans up its
1230 * resources if it must be deleted. Returns in <next> the date the task wants
1231 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1232 * nothing too many times, the request and response buffers flags are monitored
1233 * and each function is called only if at least another function has changed at
1234 * least one flag it is interested in.
1235 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001236struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001237{
Willy Tarreau827aee92011-03-10 16:55:02 +01001238 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001239 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001240 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001241 unsigned int rq_prod_last, rq_cons_last;
1242 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001243 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001244
1245 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1246 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1247
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 /* this data may be no longer valid, clear it */
1249 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1250
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001251 /* This flag must explicitly be set every time */
1252 s->req->flags &= ~BF_READ_NOEXP;
1253
1254 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001255 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1256 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001257
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001258 /* we don't want the stream interface functions to recursively wake us up */
1259 if (s->req->prod->owner == t)
1260 s->req->prod->flags |= SI_FL_DONT_WAKE;
1261 if (s->req->cons->owner == t)
1262 s->req->cons->flags |= SI_FL_DONT_WAKE;
1263
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001264 /* 1a: Check for low level timeouts if needed. We just set a flag on
1265 * stream interfaces when their timeouts have expired.
1266 */
1267 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1268 stream_int_check_timeouts(&s->si[0]);
1269 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001270
1271 /* check buffer timeouts, and close the corresponding stream interfaces
1272 * for future reads or writes. Note: this will also concern upper layers
1273 * but we do not touch any other flag. We must be careful and correctly
1274 * detect state changes when calling them.
1275 */
1276
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001277 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001278
Willy Tarreau14641402009-12-29 14:49:56 +01001279 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1280 s->req->cons->flags |= SI_FL_NOLINGER;
1281 s->req->cons->shutw(s->req->cons);
1282 }
1283
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001284 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1285 s->req->prod->shutr(s->req->prod);
1286
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001287 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001288
Willy Tarreau14641402009-12-29 14:49:56 +01001289 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1290 s->rep->cons->flags |= SI_FL_NOLINGER;
1291 s->rep->cons->shutw(s->rep->cons);
1292 }
1293
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001294 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1295 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001296 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001297
1298 /* 1b: check for low-level errors reported at the stream interface.
1299 * First we check if it's a retryable error (in which case we don't
1300 * want to tell the buffer). Otherwise we report the error one level
1301 * upper by setting flags into the buffers. Note that the side towards
1302 * the client cannot have connect (hence retryable) errors. Also, the
1303 * connection setup code must be able to deal with any type of abort.
1304 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001305 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001306 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1307 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1308 s->si[0].shutr(&s->si[0]);
1309 s->si[0].shutw(&s->si[0]);
1310 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001311 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001312 s->be->be_counters.cli_aborts++;
1313 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001314 if (srv)
1315 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001316 if (!(s->flags & SN_ERR_MASK))
1317 s->flags |= SN_ERR_CLICL;
1318 if (!(s->flags & SN_FINST_MASK))
1319 s->flags |= SN_FINST_D;
1320 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001321 }
1322 }
1323
1324 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1325 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1326 s->si[1].shutr(&s->si[1]);
1327 s->si[1].shutw(&s->si[1]);
1328 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001329 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001330 if (srv)
1331 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001332 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001333 s->be->be_counters.srv_aborts++;
1334 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001335 if (srv)
1336 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001337 if (!(s->flags & SN_ERR_MASK))
1338 s->flags |= SN_ERR_SRVCL;
1339 if (!(s->flags & SN_FINST_MASK))
1340 s->flags |= SN_FINST_D;
1341 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001342 }
1343 /* note: maybe we should process connection errors here ? */
1344 }
1345
1346 if (s->si[1].state == SI_ST_CON) {
1347 /* we were trying to establish a connection on the server side,
1348 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1349 */
1350 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1351 sess_update_st_cer(s, &s->si[1]);
1352 else if (s->si[1].state == SI_ST_EST)
1353 sess_establish(s, &s->si[1]);
1354
1355 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1356 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1357 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1358 */
1359 }
1360
Willy Tarreau815a9b22010-07-27 17:15:12 +02001361 rq_prod_last = s->si[0].state;
1362 rq_cons_last = s->si[1].state;
1363 rp_cons_last = s->si[0].state;
1364 rp_prod_last = s->si[1].state;
1365
1366 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001367 /* Check for connection closure */
1368
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001369 DPRINTF(stderr,
1370 "[%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",
1371 now_ms, __FUNCTION__, __LINE__,
1372 t,
1373 s, s->flags,
1374 s->req, s->rep,
1375 s->req->rex, s->rep->wex,
1376 s->req->flags, s->rep->flags,
1377 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1378 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001379 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001380
1381 /* nothing special to be done on client side */
1382 if (unlikely(s->req->prod->state == SI_ST_DIS))
1383 s->req->prod->state = SI_ST_CLO;
1384
1385 /* When a server-side connection is released, we have to count it and
1386 * check for pending connections on this server.
1387 */
1388 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1389 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001390 srv = target_srv(&s->target);
1391 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001392 if (s->flags & SN_CURR_SESS) {
1393 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001394 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001395 }
1396 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001397 if (may_dequeue_tasks(srv, s->be))
1398 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001399 }
1400 }
1401
1402 /*
1403 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1404 * at this point.
1405 */
1406
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001407 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001408 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001409 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001410 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1411 s->si[0].state != rq_prod_last ||
1412 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001413 unsigned int flags = s->req->flags;
1414
1415 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001416 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001417 unsigned int ana_list;
1418 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001419
Willy Tarreau90deb182010-01-07 00:20:41 +01001420 /* it's up to the analysers to stop new connections,
1421 * disable reading or closing. Note: if an analyser
1422 * disables any of these bits, it is responsible for
1423 * enabling them again when it disables itself, so
1424 * that other analysers are called in similar conditions.
1425 */
1426 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001427 buffer_auto_connect(s->req);
1428 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001429
1430 /* We will call all analysers for which a bit is set in
1431 * s->req->analysers, following the bit order from LSB
1432 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001433 * the list when not needed. Any analyser may return 0
1434 * to break out of the loop, either because of missing
1435 * data to take a decision, or because it decides to
1436 * kill the session. We loop at least once through each
1437 * analyser, and we may loop again if other analysers
1438 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001439 *
1440 * We build a list of analysers to run. We evaluate all
1441 * of these analysers in the order of the lower bit to
1442 * the higher bit. This ordering is very important.
1443 * An analyser will often add/remove other analysers,
1444 * including itself. Any changes to itself have no effect
1445 * on the loop. If it removes any other analysers, we
1446 * want those analysers not to be called anymore during
1447 * this loop. If it adds an analyser that is located
1448 * after itself, we want it to be scheduled for being
1449 * processed during the loop. If it adds an analyser
1450 * which is located before it, we want it to switch to
1451 * it immediately, even if it has already been called
1452 * once but removed since.
1453 *
1454 * In order to achieve this, we compare the analyser
1455 * list after the call with a copy of it before the
1456 * call. The work list is fed with analyser bits that
1457 * appeared during the call. Then we compare previous
1458 * work list with the new one, and check the bits that
1459 * appeared. If the lowest of these bits is lower than
1460 * the current bit, it means we have enabled a previous
1461 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001462 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463
1464 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001465 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001466 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001467
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001468 if (ana_list & AN_REQ_DECODE_PROXY) {
1469 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1470 break;
1471 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1472 }
1473
Willy Tarreaufb356202010-08-03 14:02:05 +02001474 if (ana_list & AN_REQ_INSPECT_FE) {
1475 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001476 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001477 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001478 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001479
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001481 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001482 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001484 }
1485
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001487 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1488 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001490 }
1491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001493 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1494 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001496 }
1497
Willy Tarreaufb356202010-08-03 14:02:05 +02001498 if (ana_list & AN_REQ_INSPECT_BE) {
1499 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1500 break;
1501 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1502 }
1503
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001505 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1506 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001507 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001508 }
1509
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001510 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001511 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001512 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001513 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001514 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001515
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001516 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001517 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1518 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001519 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001520 }
1521
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001522 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001523 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001524 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001525 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001526 }
Emeric Brun647caf12009-06-30 17:57:00 +02001527
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001528 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001529 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1530 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001531 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001532 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001533
Emeric Brun1d33b292010-01-04 15:47:17 +01001534 if (ana_list & AN_REQ_STICKING_RULES) {
1535 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1536 break;
1537 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1538 }
1539
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001540 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001541 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1542 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001543 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001544 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001545 break;
1546 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001547 }
Willy Tarreau84455332009-03-15 22:34:05 +01001548
Willy Tarreau815a9b22010-07-27 17:15:12 +02001549 rq_prod_last = s->si[0].state;
1550 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001551 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001552 rqf_last = s->req->flags;
1553
1554 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001555 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001556 }
1557
Willy Tarreau576507f2010-01-07 00:09:04 +01001558 /* we'll monitor the request analysers while parsing the response,
1559 * because some response analysers may indirectly enable new request
1560 * analysers (eg: HTTP keep-alive).
1561 */
1562 req_ana_back = s->req->analysers;
1563
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001564 resync_response:
1565 /* Analyse response */
1566
1567 if (unlikely(s->rep->flags & BF_HIJACK)) {
1568 /* In inject mode, we wake up everytime something has
1569 * happened on the write side of the buffer.
1570 */
1571 unsigned int flags = s->rep->flags;
1572
1573 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1574 !(s->rep->flags & BF_FULL)) {
1575 s->rep->hijacker(s, s->rep);
1576 }
1577
1578 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1579 rpf_last = s->rep->flags;
1580 goto resync_response;
1581 }
1582 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001583 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001584 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1585 s->si[0].state != rp_cons_last ||
1586 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001587 unsigned int flags = s->rep->flags;
1588
Willy Tarreau0499e352010-12-17 07:13:42 +01001589 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1590 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1591 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1592 * for some free space in the response buffer, so we might need to call
1593 * it when something changes in the response buffer, but still we pass
1594 * it the request buffer. Note that the SI state might very well still
1595 * be zero due to us returning a flow of redirects!
1596 */
1597 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1598 s->req->flags |= BF_WAKE_ONCE;
1599 }
1600
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001601 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001602 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001603 unsigned int ana_list;
1604 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001605
Willy Tarreau90deb182010-01-07 00:20:41 +01001606 /* it's up to the analysers to stop disable reading or
1607 * closing. Note: if an analyser disables any of these
1608 * bits, it is responsible for enabling them again when
1609 * it disables itself, so that other analysers are called
1610 * in similar conditions.
1611 */
1612 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001613 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001614
1615 /* We will call all analysers for which a bit is set in
1616 * s->rep->analysers, following the bit order from LSB
1617 * to MSB. The analysers must remove themselves from
1618 * the list when not needed. Any analyser may return 0
1619 * to break out of the loop, either because of missing
1620 * data to take a decision, or because it decides to
1621 * kill the session. We loop at least once through each
1622 * analyser, and we may loop again if other analysers
1623 * are added in the middle.
1624 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001625
1626 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001627 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001628 /* Warning! ensure that analysers are always placed in ascending order! */
1629
Emeric Brun97679e72010-09-23 17:56:44 +02001630 if (ana_list & AN_RES_INSPECT) {
1631 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1632 break;
1633 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1634 }
1635
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001636 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001637 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1638 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001639 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001640 }
1641
Emeric Brun1d33b292010-01-04 15:47:17 +01001642 if (ana_list & AN_RES_STORE_RULES) {
1643 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1644 break;
1645 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1646 }
1647
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001648 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001649 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1650 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001651 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001652 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001653
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001654 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001655 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1656 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001657 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001658 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001659 break;
1660 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001661 }
1662
Willy Tarreau815a9b22010-07-27 17:15:12 +02001663 rp_cons_last = s->si[0].state;
1664 rp_prod_last = s->si[1].state;
1665 rpf_last = s->rep->flags;
1666
1667 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001668 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001669 }
1670
Willy Tarreau576507f2010-01-07 00:09:04 +01001671 /* maybe someone has added some request analysers, so we must check and loop */
1672 if (s->req->analysers & ~req_ana_back)
1673 goto resync_request;
1674
Willy Tarreau0499e352010-12-17 07:13:42 +01001675 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1676 goto resync_request;
1677
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001678 /* FIXME: here we should call protocol handlers which rely on
1679 * both buffers.
1680 */
1681
1682
1683 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001684 * Now we propagate unhandled errors to the session. Normally
1685 * we're just in a data phase here since it means we have not
1686 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001687 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001688 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001689 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001690 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1691 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001692 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001693 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001694 s->be->be_counters.cli_aborts++;
1695 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001696 if (srv)
1697 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001698 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001699 }
1700 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001701 s->be->be_counters.cli_aborts++;
1702 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001703 if (srv)
1704 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001705 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001706 }
1707 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001708 s->be->be_counters.srv_aborts++;
1709 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001710 if (srv)
1711 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001712 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001713 }
1714 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001715 s->be->be_counters.srv_aborts++;
1716 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001717 if (srv)
1718 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001719 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001720 }
Willy Tarreau84455332009-03-15 22:34:05 +01001721 sess_set_term_flags(s);
1722 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001723 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1724 /* Report it if the server got an error or a read timeout expired */
1725 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001726 if (s->rep->flags & BF_READ_ERROR) {
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 Tarreau3deb3d02009-06-21 22:43:05 +02001731 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001732 }
1733 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001734 s->be->be_counters.srv_aborts++;
1735 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001736 if (srv)
1737 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001738 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001739 }
1740 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001741 s->be->be_counters.cli_aborts++;
1742 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001743 if (srv)
1744 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001745 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001746 }
1747 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001748 s->be->be_counters.cli_aborts++;
1749 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001750 if (srv)
1751 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001752 s->flags |= SN_ERR_CLITO;
1753 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001754 sess_set_term_flags(s);
1755 }
Willy Tarreau84455332009-03-15 22:34:05 +01001756 }
1757
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001758 /*
1759 * Here we take care of forwarding unhandled data. This also includes
1760 * connection establishments and shutdown requests.
1761 */
1762
1763
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001764 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001765 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001766 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1767 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001768 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001769 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001770 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001771 (s->req->prod->state >= SI_ST_EST) &&
1772 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001773 /* This buffer is freewheeling, there's no analyser nor hijacker
1774 * attached to it. If any data are left in, we'll permit them to
1775 * move.
1776 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001777 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001778 buffer_auto_connect(s->req);
1779 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001780 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001781
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001782 /* We'll let data flow between the producer (if still connected)
1783 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001784 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001785 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001786 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001787 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001788
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001789 /* check if it is wise to enable kernel splicing to forward request data */
1790 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1791 s->req->to_forward &&
1792 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001793 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001794 (pipes_used < global.maxpipes) &&
1795 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1796 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1797 (s->req->flags & BF_STREAMER_FAST)))) {
1798 s->req->flags |= BF_KERN_SPLICING;
1799 }
1800
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001801 /* reflect what the L7 analysers have seen last */
1802 rqf_last = s->req->flags;
1803
1804 /*
1805 * Now forward all shutdown requests between both sides of the buffer
1806 */
1807
Willy Tarreau520d95e2009-09-19 21:04:57 +02001808 /* first, let's check if the request buffer needs to shutdown(write), which may
1809 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001810 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001811 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001812 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001813 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001814 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001815
1816 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001817 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 +01001818 s->req->cons->shutw(s->req->cons);
1819
1820 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001821 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1822 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001823 buffer_shutr_now(s->req);
1824
1825 /* shutdown(read) pending */
1826 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1827 s->req->prod->shutr(s->req->prod);
1828
Willy Tarreau520d95e2009-09-19 21:04:57 +02001829 /* it's possible that an upper layer has requested a connection setup or abort.
1830 * There are 2 situations where we decide to establish a new connection :
1831 * - there are data scheduled for emission in the buffer
1832 * - the BF_AUTO_CONNECT flag is set (active connection)
1833 */
1834 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001835 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001836 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001837 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001838 * switch to the connected state, otherwise we perform a connection
1839 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001840 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001841 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001842 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001843 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001844 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001845 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1846 s->req->wex = TICK_ETERNITY;
1847 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001848 }
Willy Tarreau73201222009-08-16 18:27:24 +02001849 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001850 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001851 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001852 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1853 buffer_shutr_now(s->rep);
1854 }
Willy Tarreau92795622009-03-06 12:51:23 +01001855 }
1856
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001857
1858 /* we may have a pending connection request, or a connection waiting
1859 * for completion.
1860 */
1861 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1862 do {
1863 /* nb: step 1 might switch from QUE to ASS, but we first want
1864 * to give a chance to step 2 to perform a redirect if needed.
1865 */
1866 if (s->si[1].state != SI_ST_REQ)
1867 sess_update_stream_int(s, &s->si[1]);
1868 if (s->si[1].state == SI_ST_REQ)
1869 sess_prepare_conn_req(s, &s->si[1]);
1870
Willy Tarreau827aee92011-03-10 16:55:02 +01001871 srv = target_srv(&s->target);
1872 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001873 perform_http_redirect(s, &s->si[1]);
1874 } while (s->si[1].state == SI_ST_ASS);
1875 }
1876
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001877 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001878 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879 goto resync_stream_interface;
1880
Willy Tarreau815a9b22010-07-27 17:15:12 +02001881 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001882 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001883 goto resync_request;
1884
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001885 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001886
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001887 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001888 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001889 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1890 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001891 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001892 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001893 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001894 (s->rep->prod->state >= SI_ST_EST) &&
1895 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001896 /* This buffer is freewheeling, there's no analyser nor hijacker
1897 * attached to it. If any data are left in, we'll permit them to
1898 * move.
1899 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001900 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001901 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001902 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001903
1904 /* We'll let data flow between the producer (if still connected)
1905 * to the consumer.
1906 */
1907 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001908 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001909 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001910
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001911 /* check if it is wise to enable kernel splicing to forward response data */
1912 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1913 s->rep->to_forward &&
1914 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001915 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001916 (pipes_used < global.maxpipes) &&
1917 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1918 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1919 (s->rep->flags & BF_STREAMER_FAST)))) {
1920 s->rep->flags |= BF_KERN_SPLICING;
1921 }
1922
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001923 /* reflect what the L7 analysers have seen last */
1924 rpf_last = s->rep->flags;
1925
1926 /*
1927 * Now forward all shutdown requests between both sides of the buffer
1928 */
1929
1930 /*
1931 * FIXME: this is probably where we should produce error responses.
1932 */
1933
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001934 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001935 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1936 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001937 buffer_shutw_now(s->rep);
1938
1939 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001940 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 +01001941 s->rep->cons->shutw(s->rep->cons);
1942
1943 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001944 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1945 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001946 buffer_shutr_now(s->rep);
1947
1948 /* shutdown(read) pending */
1949 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1950 s->rep->prod->shutr(s->rep->prod);
1951
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001952 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001953 goto resync_stream_interface;
1954
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001955 if (s->req->flags != rqf_last)
1956 goto resync_request;
1957
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001958 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001959 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001961 /* we're interested in getting wakeups again */
1962 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1963 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1964
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001965 /* This is needed only when debugging is enabled, to indicate
1966 * client-side or server-side close. Please note that in the unlikely
1967 * event where both sides would close at once, the sequence is reported
1968 * on the server side first.
1969 */
1970 if (unlikely((global.mode & MODE_DEBUG) &&
1971 (!(global.mode & MODE_QUIET) ||
1972 (global.mode & MODE_VERBOSE)))) {
1973 int len;
1974
1975 if (s->si[1].state == SI_ST_CLO &&
1976 s->si[1].prev_state == SI_ST_EST) {
1977 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1978 s->uniq_id, s->be->id,
1979 (unsigned short)s->si[0].fd,
1980 (unsigned short)s->si[1].fd);
1981 write(1, trash, len);
1982 }
1983
1984 if (s->si[0].state == SI_ST_CLO &&
1985 s->si[0].prev_state == SI_ST_EST) {
1986 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1987 s->uniq_id, s->be->id,
1988 (unsigned short)s->si[0].fd,
1989 (unsigned short)s->si[1].fd);
1990 write(1, trash, len);
1991 }
1992 }
1993
1994 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1995 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1996
1997 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1998 session_process_counters(s);
1999
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002000 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002001 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002002
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002003 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002004 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002005
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002006 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2007 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 +01002008 s->si[0].prev_state = s->si[0].state;
2009 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002010 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2011 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002012
2013 /* Trick: if a request is being waiting for the server to respond,
2014 * and if we know the server can timeout, we don't want the timeout
2015 * to expire on the client side first, but we're still interested
2016 * in passing data from the client to the server (eg: POST). Thus,
2017 * we can cancel the client's request timeout if the server's
2018 * request timeout is set and the server has not yet sent a response.
2019 */
2020
Willy Tarreau520d95e2009-09-19 21:04:57 +02002021 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002022 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2023 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002024 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002025 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002026
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002027 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002028 * Note that this one may wake the task up again.
2029 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002030 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2031 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2032 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2033 s->req->cons->target.ptr.a->fct(s->req->cons);
2034 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2035 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002036 if (task_in_rq(t)) {
2037 /* If we woke up, we don't want to requeue the
2038 * task to the wait queue, but rather requeue
2039 * it into the runqueue ASAP.
2040 */
2041 t->expire = TICK_ETERNITY;
2042 return t;
2043 }
2044 }
2045
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002046 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2047 tick_first(s->rep->rex, s->rep->wex));
2048 if (s->req->analysers)
2049 t->expire = tick_first(t->expire, s->req->analyse_exp);
2050
2051 if (s->si[0].exp)
2052 t->expire = tick_first(t->expire, s->si[0].exp);
2053
2054 if (s->si[1].exp)
2055 t->expire = tick_first(t->expire, s->si[1].exp);
2056
2057#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002058 fprintf(stderr,
2059 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2060 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2061 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2062 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 +01002063#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002064
2065#ifdef DEBUG_DEV
2066 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002067 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068 ABORT_NOW();
2069#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002070 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002071 }
2072
2073 s->fe->feconn--;
2074 if (s->flags & SN_BE_ASSIGNED)
2075 s->be->beconn--;
2076 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002077 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002078 s->listener->nbconn--;
2079 if (s->listener->state == LI_FULL &&
2080 s->listener->nbconn < s->listener->maxconn) {
2081 /* we should reactivate the listener */
2082 EV_FD_SET(s->listener->fd, DIR_RD);
2083 s->listener->state = LI_READY;
2084 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002085
2086 if (unlikely((global.mode & MODE_DEBUG) &&
2087 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2088 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002089 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002091 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002092 write(1, trash, len);
2093 }
2094
2095 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2096 session_process_counters(s);
2097
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002098 if (s->txn.status) {
2099 int n;
2100
2101 n = s->txn.status / 100;
2102 if (n < 1 || n > 5)
2103 n = 0;
2104
2105 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002106 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002107
Willy Tarreau24657792010-02-26 10:30:28 +01002108 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002109 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002110 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002111 }
2112
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002113 /* let's do a final log if we need it */
2114 if (s->logs.logwait &&
2115 !(s->flags & SN_MONITOR) &&
2116 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002117 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002118 }
2119
2120 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002121 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002122 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002123 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002124 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002125}
2126
Willy Tarreau7c669d72008-06-20 15:04:11 +02002127/*
2128 * This function adjusts sess->srv_conn and maintains the previous and new
2129 * server's served session counts. Setting newsrv to NULL is enough to release
2130 * current connection slot. This function also notifies any LB algo which might
2131 * expect to be informed about any change in the number of active sessions on a
2132 * server.
2133 */
2134void sess_change_server(struct session *sess, struct server *newsrv)
2135{
2136 if (sess->srv_conn == newsrv)
2137 return;
2138
2139 if (sess->srv_conn) {
2140 sess->srv_conn->served--;
2141 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2142 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002143 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002144 }
2145
2146 if (newsrv) {
2147 newsrv->served++;
2148 if (newsrv->proxy->lbprm.server_take_conn)
2149 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002150 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002151 }
2152}
2153
Willy Tarreau84455332009-03-15 22:34:05 +01002154/* Handle server-side errors for default protocols. It is called whenever a a
2155 * connection setup is aborted or a request is aborted in queue. It sets the
2156 * session termination flags so that the caller does not have to worry about
2157 * them. It's installed as ->srv_error for the server-side stream_interface.
2158 */
2159void default_srv_error(struct session *s, struct stream_interface *si)
2160{
2161 int err_type = si->err_type;
2162 int err = 0, fin = 0;
2163
2164 if (err_type & SI_ET_QUEUE_ABRT) {
2165 err = SN_ERR_CLICL;
2166 fin = SN_FINST_Q;
2167 }
2168 else if (err_type & SI_ET_CONN_ABRT) {
2169 err = SN_ERR_CLICL;
2170 fin = SN_FINST_C;
2171 }
2172 else if (err_type & SI_ET_QUEUE_TO) {
2173 err = SN_ERR_SRVTO;
2174 fin = SN_FINST_Q;
2175 }
2176 else if (err_type & SI_ET_QUEUE_ERR) {
2177 err = SN_ERR_SRVCL;
2178 fin = SN_FINST_Q;
2179 }
2180 else if (err_type & SI_ET_CONN_TO) {
2181 err = SN_ERR_SRVTO;
2182 fin = SN_FINST_C;
2183 }
2184 else if (err_type & SI_ET_CONN_ERR) {
2185 err = SN_ERR_SRVCL;
2186 fin = SN_FINST_C;
2187 }
2188 else /* SI_ET_CONN_OTHER and others */ {
2189 err = SN_ERR_INTERNAL;
2190 fin = SN_FINST_C;
2191 }
2192
2193 if (!(s->flags & SN_ERR_MASK))
2194 s->flags |= err;
2195 if (!(s->flags & SN_FINST_MASK))
2196 s->flags |= fin;
2197}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002198
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002199
Willy Tarreau8b22a712010-06-18 17:46:06 +02002200/************************************************************************/
2201/* All supported ACL keywords must be declared here. */
2202/************************************************************************/
2203
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002204/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2205static int
2206acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2207{
2208 test->flags = ACL_TEST_F_VOL_TEST;
2209 test->i = 0;
2210 if (ts != NULL) {
2211 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2212 if (!ptr)
2213 return 0; /* parameter not stored */
2214 test->i = stktable_data_cast(ptr, gpc0);
2215 }
2216 return 1;
2217}
2218
2219/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002220 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002221 */
2222static int
Willy Tarreau56123282010-08-06 19:06:56 +02002223acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2224 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002225{
Willy Tarreau56123282010-08-06 19:06:56 +02002226 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002227 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002228 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002229}
2230
2231/* set test->i to the General Purpose Counter 0 value from the session's tracked
2232 * backend counters.
2233 */
2234static int
Willy Tarreau56123282010-08-06 19:06:56 +02002235acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2236 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002237{
Willy Tarreau56123282010-08-06 19:06:56 +02002238 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002239 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002240 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002241}
2242
2243/* set test->i to the General Purpose Counter 0 value from the session's source
2244 * address in the table pointed to by expr.
2245 */
2246static int
2247acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2248 struct acl_expr *expr, struct acl_test *test)
2249{
2250 struct stktable_key *key;
2251
David du Colombier4f92d322011-03-24 11:09:31 +01002252 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002253 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002254 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002255
2256 if (expr->arg_len)
2257 px = find_stktable(expr->arg.str);
2258
2259 if (!px)
2260 return 0; /* table not found */
2261
2262 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2263}
2264
2265/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2266 * return it into test->i.
2267 */
2268static int
2269acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2270{
2271 test->flags = ACL_TEST_F_VOL_TEST;
2272 test->i = 0;
2273 if (ts != NULL) {
2274 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2275 if (!ptr)
2276 return 0; /* parameter not stored */
2277 test->i = ++stktable_data_cast(ptr, gpc0);
2278 }
2279 return 1;
2280}
2281
2282/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002283 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002284 */
2285static int
Willy Tarreau56123282010-08-06 19:06:56 +02002286acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2287 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002288{
Willy Tarreau56123282010-08-06 19:06:56 +02002289 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002290 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002291 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002292}
2293
2294/* Increment the General Purpose Counter 0 value from the session's tracked
2295 * backend counters and return it into test->i.
2296 */
2297static int
Willy Tarreau56123282010-08-06 19:06:56 +02002298acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2299 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002300{
Willy Tarreau56123282010-08-06 19:06:56 +02002301 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002302 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002303 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002304}
2305
2306/* Increment the General Purpose Counter 0 value from the session's source
2307 * address in the table pointed to by expr, and return it into test->i.
2308 */
2309static int
2310acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2311 struct acl_expr *expr, struct acl_test *test)
2312{
2313 struct stktable_key *key;
2314
David du Colombier4f92d322011-03-24 11:09:31 +01002315 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002316 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002317 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002318
2319 if (expr->arg_len)
2320 px = find_stktable(expr->arg.str);
2321
2322 if (!px)
2323 return 0; /* table not found */
2324
2325 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2326}
2327
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002328/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2329static int
2330acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2331{
2332 test->flags = ACL_TEST_F_VOL_TEST;
2333 test->i = 0;
2334 if (ts != NULL) {
2335 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2336 if (!ptr)
2337 return 0; /* parameter not stored */
2338 test->i = stktable_data_cast(ptr, conn_cnt);
2339 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002340 return 1;
2341}
2342
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002343/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002344static int
Willy Tarreau56123282010-08-06 19:06:56 +02002345acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2346 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002347{
Willy Tarreau56123282010-08-06 19:06:56 +02002348 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002349 return 0;
2350
Willy Tarreau56123282010-08-06 19:06:56 +02002351 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002352}
2353
2354/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2355static int
Willy Tarreau56123282010-08-06 19:06:56 +02002356acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2357 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002358{
Willy Tarreau56123282010-08-06 19:06:56 +02002359 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002360 return 0;
2361
Willy Tarreau56123282010-08-06 19:06:56 +02002362 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002363}
2364
2365/* set test->i to the cumulated number of connections from the session's source
2366 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002367 */
2368static int
2369acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2370 struct acl_expr *expr, struct acl_test *test)
2371{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002372 struct stktable_key *key;
2373
David du Colombier4f92d322011-03-24 11:09:31 +01002374 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002375 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002376 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002377
2378 if (expr->arg_len)
2379 px = find_stktable(expr->arg.str);
2380
2381 if (!px)
2382 return 0; /* table not found */
2383
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002384 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002385}
2386
Willy Tarreau91c43d72010-06-20 11:19:22 +02002387/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2388static int
2389acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2390{
2391 test->flags = ACL_TEST_F_VOL_TEST;
2392 test->i = 0;
2393 if (ts != NULL) {
2394 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2395 if (!ptr)
2396 return 0; /* parameter not stored */
2397 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2398 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2399 }
2400 return 1;
2401}
2402
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002403/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002404 * the configured period.
2405 */
2406static int
Willy Tarreau56123282010-08-06 19:06:56 +02002407acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2408 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002409{
Willy Tarreau56123282010-08-06 19:06:56 +02002410 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002411 return 0;
2412
Willy Tarreau56123282010-08-06 19:06:56 +02002413 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002414}
2415
2416/* set test->i to the connection rate from the session's tracked BE counters over
2417 * the configured period.
2418 */
2419static int
Willy Tarreau56123282010-08-06 19:06:56 +02002420acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2421 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002422{
Willy Tarreau56123282010-08-06 19:06:56 +02002423 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002424 return 0;
2425
Willy Tarreau56123282010-08-06 19:06:56 +02002426 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002427}
2428
2429/* set test->i to the connection rate from the session's source address in the
2430 * table pointed to by expr, over the configured period.
2431 */
2432static int
2433acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2434 struct acl_expr *expr, struct acl_test *test)
2435{
2436 struct stktable_key *key;
2437
David du Colombier4f92d322011-03-24 11:09:31 +01002438 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002439 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002440 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002441
2442 if (expr->arg_len)
2443 px = find_stktable(expr->arg.str);
2444
2445 if (!px)
2446 return 0; /* table not found */
2447
2448 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2449}
2450
Willy Tarreau8b22a712010-06-18 17:46:06 +02002451/* set test->i to the number of connections from the session's source address
2452 * in the table pointed to by expr, after updating it.
2453 */
2454static int
2455acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2456 struct acl_expr *expr, struct acl_test *test)
2457{
2458 struct stksess *ts;
2459 struct stktable_key *key;
2460 void *ptr;
2461
David du Colombier4f92d322011-03-24 11:09:31 +01002462 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002463 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002464 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002465
2466 if (expr->arg_len)
2467 px = find_stktable(expr->arg.str);
2468
2469 if (!px)
2470 return 0; /* table not found */
2471
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002472 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2473 /* entry does not exist and could not be created */
2474 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002475
2476 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2477 if (!ptr)
2478 return 0; /* parameter not stored in this table */
2479
2480 test->i = ++stktable_data_cast(ptr, conn_cnt);
2481 test->flags = ACL_TEST_F_VOL_TEST;
2482 return 1;
2483}
2484
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002485/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2486static int
2487acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2488{
2489 test->flags = ACL_TEST_F_VOL_TEST;
2490 test->i = 0;
2491
2492 if (ts != NULL) {
2493 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2494 if (!ptr)
2495 return 0; /* parameter not stored */
2496 test->i = stktable_data_cast(ptr, conn_cur);
2497 }
2498 return 1;
2499}
2500
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002501/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002502static int
Willy Tarreau56123282010-08-06 19:06:56 +02002503acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2504 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002505{
Willy Tarreau56123282010-08-06 19:06:56 +02002506 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002507 return 0;
2508
Willy Tarreau56123282010-08-06 19:06:56 +02002509 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002510}
2511
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002512/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2513static int
Willy Tarreau56123282010-08-06 19:06:56 +02002514acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2515 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002516{
Willy Tarreau56123282010-08-06 19:06:56 +02002517 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002518 return 0;
2519
Willy Tarreau56123282010-08-06 19:06:56 +02002520 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002521}
2522
Willy Tarreau38285c12010-06-18 16:35:43 +02002523/* set test->i to the number of concurrent connections from the session's source
2524 * address in the table pointed to by expr.
2525 */
2526static int
2527acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2528 struct acl_expr *expr, struct acl_test *test)
2529{
Willy Tarreau38285c12010-06-18 16:35:43 +02002530 struct stktable_key *key;
2531
David du Colombier4f92d322011-03-24 11:09:31 +01002532 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002533 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002534 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002535
2536 if (expr->arg_len)
2537 px = find_stktable(expr->arg.str);
2538
2539 if (!px)
2540 return 0; /* table not found */
2541
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002542 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002543}
2544
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002545/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2546static int
2547acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2548{
2549 test->flags = ACL_TEST_F_VOL_TEST;
2550 test->i = 0;
2551 if (ts != NULL) {
2552 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2553 if (!ptr)
2554 return 0; /* parameter not stored */
2555 test->i = stktable_data_cast(ptr, sess_cnt);
2556 }
2557 return 1;
2558}
2559
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002560/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002561static int
Willy Tarreau56123282010-08-06 19:06:56 +02002562acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2563 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002564{
Willy Tarreau56123282010-08-06 19:06:56 +02002565 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002566 return 0;
2567
Willy Tarreau56123282010-08-06 19:06:56 +02002568 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002569}
2570
2571/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2572static int
Willy Tarreau56123282010-08-06 19:06:56 +02002573acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2574 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002575{
Willy Tarreau56123282010-08-06 19:06:56 +02002576 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002577 return 0;
2578
Willy Tarreau56123282010-08-06 19:06:56 +02002579 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002580}
2581
2582/* set test->i to the cumulated number of session from the session's source
2583 * address in the table pointed to by expr.
2584 */
2585static int
2586acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2587 struct acl_expr *expr, struct acl_test *test)
2588{
2589 struct stktable_key *key;
2590
David du Colombier4f92d322011-03-24 11:09:31 +01002591 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002592 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002593 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002594
2595 if (expr->arg_len)
2596 px = find_stktable(expr->arg.str);
2597
2598 if (!px)
2599 return 0; /* table not found */
2600
2601 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2602}
2603
Willy Tarreau91c43d72010-06-20 11:19:22 +02002604/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2605static int
2606acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2607{
2608 test->flags = ACL_TEST_F_VOL_TEST;
2609 test->i = 0;
2610 if (ts != NULL) {
2611 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2612 if (!ptr)
2613 return 0; /* parameter not stored */
2614 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2615 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2616 }
2617 return 1;
2618}
2619
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002620/* set test->i to the session rate from the session's tracked FE counters over
2621 * the configured period.
2622 */
2623static int
Willy Tarreau56123282010-08-06 19:06:56 +02002624acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2625 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002626{
Willy Tarreau56123282010-08-06 19:06:56 +02002627 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002628 return 0;
2629
Willy Tarreau56123282010-08-06 19:06:56 +02002630 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002631}
2632
2633/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002634 * the configured period.
2635 */
2636static int
Willy Tarreau56123282010-08-06 19:06:56 +02002637acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2638 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002639{
Willy Tarreau56123282010-08-06 19:06:56 +02002640 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002641 return 0;
2642
Willy Tarreau56123282010-08-06 19:06:56 +02002643 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002644}
2645
2646/* set test->i to the session rate from the session's source address in the
2647 * table pointed to by expr, over the configured period.
2648 */
2649static int
2650acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2651 struct acl_expr *expr, struct acl_test *test)
2652{
2653 struct stktable_key *key;
2654
David du Colombier4f92d322011-03-24 11:09:31 +01002655 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002656 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002657 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002658
2659 if (expr->arg_len)
2660 px = find_stktable(expr->arg.str);
2661
2662 if (!px)
2663 return 0; /* table not found */
2664
2665 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2666}
2667
Willy Tarreauda7ff642010-06-23 11:44:09 +02002668/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2669static int
2670acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2671{
2672 test->flags = ACL_TEST_F_VOL_TEST;
2673 test->i = 0;
2674 if (ts != NULL) {
2675 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2676 if (!ptr)
2677 return 0; /* parameter not stored */
2678 test->i = stktable_data_cast(ptr, http_req_cnt);
2679 }
2680 return 1;
2681}
2682
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002683/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002684static int
Willy Tarreau56123282010-08-06 19:06:56 +02002685acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2686 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002687{
Willy Tarreau56123282010-08-06 19:06:56 +02002688 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002689 return 0;
2690
Willy Tarreau56123282010-08-06 19:06:56 +02002691 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002692}
2693
2694/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2695static int
Willy Tarreau56123282010-08-06 19:06:56 +02002696acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2697 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002698{
Willy Tarreau56123282010-08-06 19:06:56 +02002699 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002700 return 0;
2701
Willy Tarreau56123282010-08-06 19:06:56 +02002702 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002703}
2704
2705/* set test->i to the cumulated number of session from the session's source
2706 * address in the table pointed to by expr.
2707 */
2708static int
2709acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002710 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002711{
2712 struct stktable_key *key;
2713
David du Colombier4f92d322011-03-24 11:09:31 +01002714 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002715 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002716 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717
2718 if (expr->arg_len)
2719 px = find_stktable(expr->arg.str);
2720
2721 if (!px)
2722 return 0; /* table not found */
2723
2724 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2725}
2726
2727/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2728static int
2729acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2730{
2731 test->flags = ACL_TEST_F_VOL_TEST;
2732 test->i = 0;
2733 if (ts != NULL) {
2734 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2735 if (!ptr)
2736 return 0; /* parameter not stored */
2737 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2738 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2739 }
2740 return 1;
2741}
2742
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002743/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002744 * the configured period.
2745 */
2746static int
Willy Tarreau56123282010-08-06 19:06:56 +02002747acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2748 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002749{
Willy Tarreau56123282010-08-06 19:06:56 +02002750 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002751 return 0;
2752
Willy Tarreau56123282010-08-06 19:06:56 +02002753 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002754}
2755
2756/* set test->i to the session rate from the session's tracked BE counters over
2757 * the configured period.
2758 */
2759static int
Willy Tarreau56123282010-08-06 19:06:56 +02002760acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2761 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762{
Willy Tarreau56123282010-08-06 19:06:56 +02002763 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002764 return 0;
2765
Willy Tarreau56123282010-08-06 19:06:56 +02002766 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002767}
2768
2769/* set test->i to the session rate from the session's source address in the
2770 * table pointed to by expr, over the configured period.
2771 */
2772static int
2773acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002774 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002775{
2776 struct stktable_key *key;
2777
David du Colombier4f92d322011-03-24 11:09:31 +01002778 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002779 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002780 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002781
2782 if (expr->arg_len)
2783 px = find_stktable(expr->arg.str);
2784
2785 if (!px)
2786 return 0; /* table not found */
2787
2788 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2789}
2790
2791/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2792static int
2793acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2794{
2795 test->flags = ACL_TEST_F_VOL_TEST;
2796 test->i = 0;
2797 if (ts != NULL) {
2798 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2799 if (!ptr)
2800 return 0; /* parameter not stored */
2801 test->i = stktable_data_cast(ptr, http_err_cnt);
2802 }
2803 return 1;
2804}
2805
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002806/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002807static int
Willy Tarreau56123282010-08-06 19:06:56 +02002808acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2809 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002810{
Willy Tarreau56123282010-08-06 19:06:56 +02002811 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002812 return 0;
2813
Willy Tarreau56123282010-08-06 19:06:56 +02002814 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002815}
2816
2817/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2818static int
Willy Tarreau56123282010-08-06 19:06:56 +02002819acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2820 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002821{
Willy Tarreau56123282010-08-06 19:06:56 +02002822 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002823 return 0;
2824
Willy Tarreau56123282010-08-06 19:06:56 +02002825 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826}
2827
2828/* set test->i to the cumulated number of session from the session's source
2829 * address in the table pointed to by expr.
2830 */
2831static int
2832acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002833 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834{
2835 struct stktable_key *key;
2836
David du Colombier4f92d322011-03-24 11:09:31 +01002837 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002839 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840
2841 if (expr->arg_len)
2842 px = find_stktable(expr->arg.str);
2843
2844 if (!px)
2845 return 0; /* table not found */
2846
2847 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2848}
2849
2850/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2851static int
2852acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2853{
2854 test->flags = ACL_TEST_F_VOL_TEST;
2855 test->i = 0;
2856 if (ts != NULL) {
2857 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2858 if (!ptr)
2859 return 0; /* parameter not stored */
2860 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2861 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2862 }
2863 return 1;
2864}
2865
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002866/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002867 * the configured period.
2868 */
2869static int
Willy Tarreau56123282010-08-06 19:06:56 +02002870acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2871 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002872{
Willy Tarreau56123282010-08-06 19:06:56 +02002873 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002874 return 0;
2875
Willy Tarreau56123282010-08-06 19:06:56 +02002876 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002877}
2878
2879/* set test->i to the session rate from the session's tracked BE counters over
2880 * the configured period.
2881 */
2882static int
Willy Tarreau56123282010-08-06 19:06:56 +02002883acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2884 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002885{
Willy Tarreau56123282010-08-06 19:06:56 +02002886 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002887 return 0;
2888
Willy Tarreau56123282010-08-06 19:06:56 +02002889 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002890}
2891
2892/* set test->i to the session rate from the session's source address in the
2893 * table pointed to by expr, over the configured period.
2894 */
2895static int
2896acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2897 struct acl_expr *expr, struct acl_test *test)
2898{
2899 struct stktable_key *key;
2900
David du Colombier4f92d322011-03-24 11:09:31 +01002901 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002902 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002903 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002904
2905 if (expr->arg_len)
2906 px = find_stktable(expr->arg.str);
2907
2908 if (!px)
2909 return 0; /* table not found */
2910
2911 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2912}
2913
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002914/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2915static int
2916acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2917{
2918 test->flags = ACL_TEST_F_VOL_TEST;
2919 test->i = 0;
2920
2921 if (ts != NULL) {
2922 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2923 if (!ptr)
2924 return 0; /* parameter not stored */
2925 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2926 }
2927 return 1;
2928}
2929
2930/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002931 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002932 */
2933static int
Willy Tarreau56123282010-08-06 19:06:56 +02002934acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2935 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002936{
Willy Tarreau56123282010-08-06 19:06:56 +02002937 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002938 return 0;
2939
Willy Tarreau56123282010-08-06 19:06:56 +02002940 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002941}
2942
2943/* set test->i to the number of kbytes received from clients according to the
2944 * session's tracked BE counters.
2945 */
2946static int
Willy Tarreau56123282010-08-06 19:06:56 +02002947acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2948 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002949{
Willy Tarreau56123282010-08-06 19:06:56 +02002950 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002951 return 0;
2952
Willy Tarreau56123282010-08-06 19:06:56 +02002953 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002954}
2955
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002956/* set test->i to the number of kbytes received from the session's source
2957 * address in the table pointed to by expr.
2958 */
2959static int
2960acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2961 struct acl_expr *expr, struct acl_test *test)
2962{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002963 struct stktable_key *key;
2964
David du Colombier4f92d322011-03-24 11:09:31 +01002965 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002966 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002967 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002968
2969 if (expr->arg_len)
2970 px = find_stktable(expr->arg.str);
2971
2972 if (!px)
2973 return 0; /* table not found */
2974
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002975 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2976}
2977
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002978/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2979 * configured period.
2980 */
2981static int
2982acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2983{
2984 test->flags = ACL_TEST_F_VOL_TEST;
2985 test->i = 0;
2986 if (ts != NULL) {
2987 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2988 if (!ptr)
2989 return 0; /* parameter not stored */
2990 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2991 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2992 }
2993 return 1;
2994}
2995
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002996/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002997 * counters over the configured period.
2998 */
2999static int
Willy Tarreau56123282010-08-06 19:06:56 +02003000acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3001 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003002{
Willy Tarreau56123282010-08-06 19:06:56 +02003003 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003004 return 0;
3005
Willy Tarreau56123282010-08-06 19:06:56 +02003006 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003007}
3008
3009/* set test->i to the bytes rate from clients from the session's tracked BE
3010 * counters over the configured period.
3011 */
3012static int
Willy Tarreau56123282010-08-06 19:06:56 +02003013acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3014 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003015{
Willy Tarreau56123282010-08-06 19:06:56 +02003016 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003017 return 0;
3018
Willy Tarreau56123282010-08-06 19:06:56 +02003019 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003020}
3021
3022/* set test->i to the bytes rate from clients from the session's source address
3023 * in the table pointed to by expr, over the configured period.
3024 */
3025static int
3026acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003027 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003028{
3029 struct stktable_key *key;
3030
David du Colombier4f92d322011-03-24 11:09:31 +01003031 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003032 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003033 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003034
3035 if (expr->arg_len)
3036 px = find_stktable(expr->arg.str);
3037
3038 if (!px)
3039 return 0; /* table not found */
3040
3041 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3042}
3043
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003044/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3045static int
3046acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3047{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003048 test->flags = ACL_TEST_F_VOL_TEST;
3049 test->i = 0;
3050
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003051 if (ts != NULL) {
3052 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003053 if (!ptr)
3054 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003055 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003056 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003057 return 1;
3058}
3059
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003060/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003061 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003062 */
3063static int
Willy Tarreau56123282010-08-06 19:06:56 +02003064acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3065 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003066{
Willy Tarreau56123282010-08-06 19:06:56 +02003067 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003068 return 0;
3069
Willy Tarreau56123282010-08-06 19:06:56 +02003070 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003071}
3072
3073/* set test->i to the number of kbytes sent to clients according to the session's
3074 * tracked BE counters.
3075 */
3076static int
Willy Tarreau56123282010-08-06 19:06:56 +02003077acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3078 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003079{
Willy Tarreau56123282010-08-06 19:06:56 +02003080 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003081 return 0;
3082
Willy Tarreau56123282010-08-06 19:06:56 +02003083 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003084}
3085
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003086/* set test->i to the number of kbytes sent to the session's source address in
3087 * the table pointed to by expr.
3088 */
3089static int
3090acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3091 struct acl_expr *expr, struct acl_test *test)
3092{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003093 struct stktable_key *key;
3094
David du Colombier4f92d322011-03-24 11:09:31 +01003095 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003096 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003097 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003098
3099 if (expr->arg_len)
3100 px = find_stktable(expr->arg.str);
3101
3102 if (!px)
3103 return 0; /* table not found */
3104
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003105 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003106}
3107
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003108/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3109 * configured period.
3110 */
3111static int
3112acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3113{
3114 test->flags = ACL_TEST_F_VOL_TEST;
3115 test->i = 0;
3116 if (ts != NULL) {
3117 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3118 if (!ptr)
3119 return 0; /* parameter not stored */
3120 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3121 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3122 }
3123 return 1;
3124}
3125
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003126/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003127 * over the configured period.
3128 */
3129static int
Willy Tarreau56123282010-08-06 19:06:56 +02003130acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3131 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003132{
Willy Tarreau56123282010-08-06 19:06:56 +02003133 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003134 return 0;
3135
Willy Tarreau56123282010-08-06 19:06:56 +02003136 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003137}
3138
3139/* set test->i to the bytes rate to clients from the session's tracked BE counters
3140 * over the configured period.
3141 */
3142static int
Willy Tarreau56123282010-08-06 19:06:56 +02003143acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3144 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003145{
Willy Tarreau56123282010-08-06 19:06:56 +02003146 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003147 return 0;
3148
Willy Tarreau56123282010-08-06 19:06:56 +02003149 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003150}
3151
3152/* set test->i to the bytes rate to client from the session's source address in
3153 * the table pointed to by expr, over the configured period.
3154 */
3155static int
3156acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003157 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003158{
3159 struct stktable_key *key;
3160
David du Colombier4f92d322011-03-24 11:09:31 +01003161 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003162 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003163 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003164
3165 if (expr->arg_len)
3166 px = find_stktable(expr->arg.str);
3167
3168 if (!px)
3169 return 0; /* table not found */
3170
3171 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3172}
3173
Willy Tarreauc735a072011-03-29 00:57:02 +02003174/* set test->i to the number of used entries in the table pointed to by expr. */
3175static int
3176acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3177 struct acl_expr *expr, struct acl_test *test)
3178{
3179 if (expr->arg_len)
3180 px = find_stktable(expr->arg.str);
3181
3182 if (!px)
3183 return 0; /* table not found */
3184
3185 test->flags = ACL_TEST_F_VOL_TEST;
3186 test->i = px->table.current;
3187 return 1;
3188}
3189
3190/* set test->i to the number of free entries in the table pointed to by expr. */
3191static int
3192acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3193 struct acl_expr *expr, struct acl_test *test)
3194{
3195 if (expr->arg_len)
3196 px = find_stktable(expr->arg.str);
3197
3198 if (!px)
3199 return 0; /* table not found */
3200
3201 test->flags = ACL_TEST_F_VOL_TEST;
3202 test->i = px->table.size - px->table.current;
3203 return 1;
3204}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003205
3206/* Note: must not be declared <const> as its list will be overwritten */
3207static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003208 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3209 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3210 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3211 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3212 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3213 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3214 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3215 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3216 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3217 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3218 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3219 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3220 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3221 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3222 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3223 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3224 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3225 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3226 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3227 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3228 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3229 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3230 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3231 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3232 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3233 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3234 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3235 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3236 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3237 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3238 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3239 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3240 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3241 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3242 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3243 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3244 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3245 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3246 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3247 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3248 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3249 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3250 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3251 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3252 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3253 { "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 +02003254 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3255 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003256 { NULL, NULL, NULL, NULL },
3257}};
3258
3259
Willy Tarreau56123282010-08-06 19:06:56 +02003260/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003261 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3262 * <prm> struct is fed with the table name if any. If unspecified, the caller
3263 * will assume that the current proxy's table is used.
3264 */
3265int parse_track_counters(char **args, int *arg,
3266 int section_type, struct proxy *curpx,
3267 struct track_ctr_prm *prm,
3268 struct proxy *defpx, char *err, int errlen)
3269{
3270 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003271 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003272
Willy Tarreau56123282010-08-06 19:06:56 +02003273 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003274 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003275 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003276 */
3277 while (args[*arg]) {
3278 if (strcmp(args[*arg], "src") == 0) {
3279 prm->type = STKTABLE_TYPE_IP;
3280 pattern_type = 1;
3281 }
3282 else if (strcmp(args[*arg], "table") == 0) {
3283 if (!args[*arg + 1]) {
3284 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003285 "missing table for %s in %s '%s'.",
3286 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003287 return -1;
3288 }
3289 /* we copy the table name for now, it will be resolved later */
3290 prm->table.n = strdup(args[*arg + 1]);
3291 (*arg)++;
3292 }
3293 else {
3294 /* unhandled keywords are handled by the caller */
3295 break;
3296 }
3297 (*arg)++;
3298 }
3299
3300 if (!pattern_type) {
3301 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003302 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3303 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003304 return -1;
3305 }
3306
3307 return 0;
3308}
3309
Willy Tarreau8b22a712010-06-18 17:46:06 +02003310__attribute__((constructor))
3311static void __session_init(void)
3312{
3313 acl_register_keywords(&acl_kws);
3314}
3315
Willy Tarreaubaaee002006-06-26 02:48:02 +02003316/*
3317 * Local variables:
3318 * c-indent-level: 8
3319 * c-basic-offset: 8
3320 * End:
3321 */