blob: 2c1d35911d8732649b4e2ef918f43e6a73435e7c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100133 if (p->feconn > p->fe_counters.conn_max)
134 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100171 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200191 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200192 s->si[1].err_loc = NULL;
193 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200194 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100195 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200196 s->si[1].shutr = stream_int_shutr;
197 s->si[1].shutw = stream_int_shutw;
198 s->si[1].exp = TICK_ETERNITY;
199 s->si[1].flags = SI_FL_NONE;
200
201 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
202 s->si[1].flags |= SI_FL_INDEP_STR;
203
Willy Tarreau827aee92011-03-10 16:55:02 +0100204 s->srv_conn = NULL;
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 */
323void session_free(struct session *s)
324{
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 */
523int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
524{
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 */
591int sess_update_st_cer(struct session *s, struct stream_interface *si)
592{
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 */
672void sess_establish(struct session *s, struct stream_interface *si)
673{
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 */
711void sess_update_stream_int(struct session *s, struct stream_interface *si)
712{
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
870/* This function initiates a server connection request on a stream interface
871 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
872 * indicating that a server has been assigned. It may also return SI_ST_QUE,
873 * or SI_ST_CLO upon error.
874 */
875static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
876 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",
877 now_ms, __FUNCTION__,
878 s,
879 s->req, s->rep,
880 s->req->rex, s->rep->wex,
881 s->req->flags, s->rep->flags,
882 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
883
884 if (si->state != SI_ST_REQ)
885 return;
886
887 /* Try to assign a server */
888 if (srv_redispatch_connect(s) != 0) {
889 /* We did not get a server. Either we queued the
890 * connection request, or we encountered an error.
891 */
892 if (si->state == SI_ST_QUE)
893 return;
894
895 /* we did not get any server, let's check the cause */
896 si->shutr(si);
897 si->shutw(si);
898 si->ob->flags |= BF_WRITE_ERROR;
899 if (!si->err_type)
900 si->err_type = SI_ET_CONN_OTHER;
901 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100902 if (s->srv_error)
903 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 return;
905 }
906
907 /* The server is assigned */
908 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
909 si->state = SI_ST_ASS;
910}
911
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200912/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100913 * if appropriate. The default_backend rule is also considered, then the
914 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200915 * It returns 1 if the processing can continue on next analysers, or zero if it
916 * either needs more data or wants to immediately abort the request.
917 */
918int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
919{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200920 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100921
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200922 req->analysers &= ~an_bit;
923 req->analyse_exp = TICK_ETERNITY;
924
925 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
926 now_ms, __FUNCTION__,
927 s,
928 req,
929 req->rex, req->wex,
930 req->flags,
931 req->l,
932 req->analysers);
933
934 /* now check whether we have some switching rules for this request */
935 if (!(s->flags & SN_BE_ASSIGNED)) {
936 struct switching_rule *rule;
937
938 list_for_each_entry(rule, &s->fe->switching_rules, list) {
939 int ret;
940
941 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
942 ret = acl_pass(ret);
943 if (rule->cond->pol == ACL_COND_UNLESS)
944 ret = !ret;
945
946 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200947 if (!session_set_backend(s, rule->be.backend))
948 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200949 break;
950 }
951 }
952
953 /* To ensure correct connection accounting on the backend, we
954 * have to assign one if it was not set (eg: a listen). This
955 * measure also takes care of correctly setting the default
956 * backend if any.
957 */
958 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200959 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
960 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200961 }
962
Willy Tarreaufb356202010-08-03 14:02:05 +0200963 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
964 if (s->fe == s->be) {
965 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200966 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200967 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200968
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200969 /* 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 +0100970 * persistence rule, and report that in the session.
971 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200972 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100973 int ret = 1;
974
975 if (prst_rule->cond) {
976 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
977 ret = acl_pass(ret);
978 if (prst_rule->cond->pol == ACL_COND_UNLESS)
979 ret = !ret;
980 }
981
982 if (ret) {
983 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200984 if (prst_rule->type == PERSIST_TYPE_FORCE) {
985 s->flags |= SN_FORCE_PRST;
986 } else {
987 s->flags |= SN_IGNORE_PRST;
988 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100989 break;
990 }
991 }
992
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200993 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200994
995 sw_failed:
996 /* immediately abort this request in case of allocation failure */
997 buffer_abort(s->req);
998 buffer_abort(s->rep);
999
1000 if (!(s->flags & SN_ERR_MASK))
1001 s->flags |= SN_ERR_RESOURCE;
1002 if (!(s->flags & SN_FINST_MASK))
1003 s->flags |= SN_FINST_R;
1004
1005 s->txn.status = 500;
1006 s->req->analysers = 0;
1007 s->req->analyse_exp = TICK_ETERNITY;
1008 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001009}
1010
Emeric Brun1d33b292010-01-04 15:47:17 +01001011/* This stream analyser works on a request. It applies all sticking rules on
1012 * it then returns 1. The data must already be present in the buffer otherwise
1013 * they won't match. It always returns 1.
1014 */
1015int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1016{
1017 struct proxy *px = s->be;
1018 struct sticking_rule *rule;
1019
1020 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1021 now_ms, __FUNCTION__,
1022 s,
1023 req,
1024 req->rex, req->wex,
1025 req->flags,
1026 req->l,
1027 req->analysers);
1028
1029 list_for_each_entry(rule, &px->sticking_rules, list) {
1030 int ret = 1 ;
1031 int i;
1032
1033 for (i = 0; i < s->store_count; i++) {
1034 if (rule->table.t == s->store[i].table)
1035 break;
1036 }
1037
1038 if (i != s->store_count)
1039 continue;
1040
1041 if (rule->cond) {
1042 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1043 ret = acl_pass(ret);
1044 if (rule->cond->pol == ACL_COND_UNLESS)
1045 ret = !ret;
1046 }
1047
1048 if (ret) {
1049 struct stktable_key *key;
1050
Emeric Brun485479d2010-09-23 18:02:19 +02001051 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001052 if (!key)
1053 continue;
1054
1055 if (rule->flags & STK_IS_MATCH) {
1056 struct stksess *ts;
1057
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001058 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001059 if (!(s->flags & SN_ASSIGNED)) {
1060 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001061 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001062
1063 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001064 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1065 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001066 if (node) {
1067 struct server *srv;
1068
1069 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001070 if ((srv->state & SRV_RUNNING) ||
1071 (px->options & PR_O_PERSIST) ||
1072 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001073 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001074 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001075 }
1076 }
1077 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001078 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001079 }
1080 }
1081 if (rule->flags & STK_IS_STORE) {
1082 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1083 struct stksess *ts;
1084
1085 ts = stksess_new(rule->table.t, key);
1086 if (ts) {
1087 s->store[s->store_count].table = rule->table.t;
1088 s->store[s->store_count++].ts = ts;
1089 }
1090 }
1091 }
1092 }
1093 }
1094
1095 req->analysers &= ~an_bit;
1096 req->analyse_exp = TICK_ETERNITY;
1097 return 1;
1098}
1099
1100/* This stream analyser works on a response. It applies all store rules on it
1101 * then returns 1. The data must already be present in the buffer otherwise
1102 * they won't match. It always returns 1.
1103 */
1104int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1105{
1106 struct proxy *px = s->be;
1107 struct sticking_rule *rule;
1108 int i;
1109
1110 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1111 now_ms, __FUNCTION__,
1112 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001113 rep,
1114 rep->rex, rep->wex,
1115 rep->flags,
1116 rep->l,
1117 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001118
1119 list_for_each_entry(rule, &px->storersp_rules, list) {
1120 int ret = 1 ;
1121 int storereqidx = -1;
1122
1123 for (i = 0; i < s->store_count; i++) {
1124 if (rule->table.t == s->store[i].table) {
1125 if (!(s->store[i].flags))
1126 storereqidx = i;
1127 break;
1128 }
1129 }
1130
1131 if ((i != s->store_count) && (storereqidx == -1))
1132 continue;
1133
1134 if (rule->cond) {
1135 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1136 ret = acl_pass(ret);
1137 if (rule->cond->pol == ACL_COND_UNLESS)
1138 ret = !ret;
1139 }
1140
1141 if (ret) {
1142 struct stktable_key *key;
1143
Emeric Brun485479d2010-09-23 18:02:19 +02001144 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001145 if (!key)
1146 continue;
1147
1148 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001149 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001150 s->store[storereqidx].flags = 1;
1151 }
1152 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1153 struct stksess *ts;
1154
1155 ts = stksess_new(rule->table.t, key);
1156 if (ts) {
1157 s->store[s->store_count].table = rule->table.t;
1158 s->store[s->store_count].flags = 1;
1159 s->store[s->store_count++].ts = ts;
1160 }
1161 }
1162 }
1163 }
1164
1165 /* process store request and store response */
1166 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001167 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001168 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001169
1170 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1171 if (ts) {
1172 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001173 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001174 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001175 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001176 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001177 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001178
1179 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001180 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001181 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001182 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001183 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001184
1185 rep->analysers &= ~an_bit;
1186 rep->analyse_exp = TICK_ETERNITY;
1187 return 1;
1188}
1189
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001190/* This macro is very specific to the function below. See the comments in
1191 * process_session() below to understand the logic and the tests.
1192 */
1193#define UPDATE_ANALYSERS(real, list, back, flag) { \
1194 list = (((list) & ~(flag)) | ~(back)) & (real); \
1195 back = real; \
1196 if (!(list)) \
1197 break; \
1198 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1199 continue; \
1200}
1201
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001202/* Processes the client, server, request and response jobs of a session task,
1203 * then puts it back to the wait queue in a clean state, or cleans up its
1204 * resources if it must be deleted. Returns in <next> the date the task wants
1205 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1206 * nothing too many times, the request and response buffers flags are monitored
1207 * and each function is called only if at least another function has changed at
1208 * least one flag it is interested in.
1209 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001210struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001211{
Willy Tarreau827aee92011-03-10 16:55:02 +01001212 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001214 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001215 unsigned int rq_prod_last, rq_cons_last;
1216 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001217 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001218
1219 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1220 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1221
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001222 /* this data may be no longer valid, clear it */
1223 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1224
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001225 /* This flag must explicitly be set every time */
1226 s->req->flags &= ~BF_READ_NOEXP;
1227
1228 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001229 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1230 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001231
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001232 /* we don't want the stream interface functions to recursively wake us up */
1233 if (s->req->prod->owner == t)
1234 s->req->prod->flags |= SI_FL_DONT_WAKE;
1235 if (s->req->cons->owner == t)
1236 s->req->cons->flags |= SI_FL_DONT_WAKE;
1237
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238 /* 1a: Check for low level timeouts if needed. We just set a flag on
1239 * stream interfaces when their timeouts have expired.
1240 */
1241 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1242 stream_int_check_timeouts(&s->si[0]);
1243 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001244
1245 /* check buffer timeouts, and close the corresponding stream interfaces
1246 * for future reads or writes. Note: this will also concern upper layers
1247 * but we do not touch any other flag. We must be careful and correctly
1248 * detect state changes when calling them.
1249 */
1250
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001251 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001252
Willy Tarreau14641402009-12-29 14:49:56 +01001253 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1254 s->req->cons->flags |= SI_FL_NOLINGER;
1255 s->req->cons->shutw(s->req->cons);
1256 }
1257
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001258 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1259 s->req->prod->shutr(s->req->prod);
1260
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001261 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262
Willy Tarreau14641402009-12-29 14:49:56 +01001263 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1264 s->rep->cons->flags |= SI_FL_NOLINGER;
1265 s->rep->cons->shutw(s->rep->cons);
1266 }
1267
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001268 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1269 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001270 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001271
1272 /* 1b: check for low-level errors reported at the stream interface.
1273 * First we check if it's a retryable error (in which case we don't
1274 * want to tell the buffer). Otherwise we report the error one level
1275 * upper by setting flags into the buffers. Note that the side towards
1276 * the client cannot have connect (hence retryable) errors. Also, the
1277 * connection setup code must be able to deal with any type of abort.
1278 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001279 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001280 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1281 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1282 s->si[0].shutr(&s->si[0]);
1283 s->si[0].shutw(&s->si[0]);
1284 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001286 s->be->be_counters.cli_aborts++;
1287 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001288 if (srv)
1289 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001290 if (!(s->flags & SN_ERR_MASK))
1291 s->flags |= SN_ERR_CLICL;
1292 if (!(s->flags & SN_FINST_MASK))
1293 s->flags |= SN_FINST_D;
1294 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001295 }
1296 }
1297
1298 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1299 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1300 s->si[1].shutr(&s->si[1]);
1301 s->si[1].shutw(&s->si[1]);
1302 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001303 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001304 if (srv)
1305 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001306 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001307 s->be->be_counters.srv_aborts++;
1308 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001309 if (srv)
1310 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001311 if (!(s->flags & SN_ERR_MASK))
1312 s->flags |= SN_ERR_SRVCL;
1313 if (!(s->flags & SN_FINST_MASK))
1314 s->flags |= SN_FINST_D;
1315 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001316 }
1317 /* note: maybe we should process connection errors here ? */
1318 }
1319
1320 if (s->si[1].state == SI_ST_CON) {
1321 /* we were trying to establish a connection on the server side,
1322 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1323 */
1324 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1325 sess_update_st_cer(s, &s->si[1]);
1326 else if (s->si[1].state == SI_ST_EST)
1327 sess_establish(s, &s->si[1]);
1328
1329 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1330 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1331 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1332 */
1333 }
1334
Willy Tarreau815a9b22010-07-27 17:15:12 +02001335 rq_prod_last = s->si[0].state;
1336 rq_cons_last = s->si[1].state;
1337 rp_cons_last = s->si[0].state;
1338 rp_prod_last = s->si[1].state;
1339
1340 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001341 /* Check for connection closure */
1342
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001343 DPRINTF(stderr,
1344 "[%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",
1345 now_ms, __FUNCTION__, __LINE__,
1346 t,
1347 s, s->flags,
1348 s->req, s->rep,
1349 s->req->rex, s->rep->wex,
1350 s->req->flags, s->rep->flags,
1351 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1352 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001353 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001354
1355 /* nothing special to be done on client side */
1356 if (unlikely(s->req->prod->state == SI_ST_DIS))
1357 s->req->prod->state = SI_ST_CLO;
1358
1359 /* When a server-side connection is released, we have to count it and
1360 * check for pending connections on this server.
1361 */
1362 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1363 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001364 srv = target_srv(&s->target);
1365 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001366 if (s->flags & SN_CURR_SESS) {
1367 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001368 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001369 }
1370 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001371 if (may_dequeue_tasks(srv, s->be))
1372 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001373 }
1374 }
1375
1376 /*
1377 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1378 * at this point.
1379 */
1380
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001381 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001382 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001383 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001384 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1385 s->si[0].state != rq_prod_last ||
1386 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001387 unsigned int flags = s->req->flags;
1388
1389 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001390 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001391 unsigned int ana_list;
1392 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001393
Willy Tarreau90deb182010-01-07 00:20:41 +01001394 /* it's up to the analysers to stop new connections,
1395 * disable reading or closing. Note: if an analyser
1396 * disables any of these bits, it is responsible for
1397 * enabling them again when it disables itself, so
1398 * that other analysers are called in similar conditions.
1399 */
1400 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001401 buffer_auto_connect(s->req);
1402 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001403
1404 /* We will call all analysers for which a bit is set in
1405 * s->req->analysers, following the bit order from LSB
1406 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001407 * the list when not needed. Any analyser may return 0
1408 * to break out of the loop, either because of missing
1409 * data to take a decision, or because it decides to
1410 * kill the session. We loop at least once through each
1411 * analyser, and we may loop again if other analysers
1412 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001413 *
1414 * We build a list of analysers to run. We evaluate all
1415 * of these analysers in the order of the lower bit to
1416 * the higher bit. This ordering is very important.
1417 * An analyser will often add/remove other analysers,
1418 * including itself. Any changes to itself have no effect
1419 * on the loop. If it removes any other analysers, we
1420 * want those analysers not to be called anymore during
1421 * this loop. If it adds an analyser that is located
1422 * after itself, we want it to be scheduled for being
1423 * processed during the loop. If it adds an analyser
1424 * which is located before it, we want it to switch to
1425 * it immediately, even if it has already been called
1426 * once but removed since.
1427 *
1428 * In order to achieve this, we compare the analyser
1429 * list after the call with a copy of it before the
1430 * call. The work list is fed with analyser bits that
1431 * appeared during the call. Then we compare previous
1432 * work list with the new one, and check the bits that
1433 * appeared. If the lowest of these bits is lower than
1434 * the current bit, it means we have enabled a previous
1435 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001436 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001437
1438 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001439 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001440 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001441
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001442 if (ana_list & AN_REQ_DECODE_PROXY) {
1443 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1444 break;
1445 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1446 }
1447
Willy Tarreaufb356202010-08-03 14:02:05 +02001448 if (ana_list & AN_REQ_INSPECT_FE) {
1449 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001450 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001451 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001452 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001453
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001454 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001455 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001456 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001458 }
1459
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001460 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001461 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1462 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001464 }
1465
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001466 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001467 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1468 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001469 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001470 }
1471
Willy Tarreaufb356202010-08-03 14:02:05 +02001472 if (ana_list & AN_REQ_INSPECT_BE) {
1473 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1474 break;
1475 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1476 }
1477
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001479 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1480 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001481 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001482 }
1483
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001484 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001485 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001486 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001487 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001488 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001489
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001490 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001491 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1492 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001494 }
1495
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001497 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001498 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001500 }
Emeric Brun647caf12009-06-30 17:57:00 +02001501
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001502 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001503 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1504 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001505 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001506 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001507
Emeric Brun1d33b292010-01-04 15:47:17 +01001508 if (ana_list & AN_REQ_STICKING_RULES) {
1509 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1510 break;
1511 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1512 }
1513
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001514 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001515 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1516 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001517 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001518 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001519 break;
1520 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001521 }
Willy Tarreau84455332009-03-15 22:34:05 +01001522
Willy Tarreau815a9b22010-07-27 17:15:12 +02001523 rq_prod_last = s->si[0].state;
1524 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001525 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001526 rqf_last = s->req->flags;
1527
1528 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001529 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001530 }
1531
Willy Tarreau576507f2010-01-07 00:09:04 +01001532 /* we'll monitor the request analysers while parsing the response,
1533 * because some response analysers may indirectly enable new request
1534 * analysers (eg: HTTP keep-alive).
1535 */
1536 req_ana_back = s->req->analysers;
1537
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001538 resync_response:
1539 /* Analyse response */
1540
1541 if (unlikely(s->rep->flags & BF_HIJACK)) {
1542 /* In inject mode, we wake up everytime something has
1543 * happened on the write side of the buffer.
1544 */
1545 unsigned int flags = s->rep->flags;
1546
1547 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1548 !(s->rep->flags & BF_FULL)) {
1549 s->rep->hijacker(s, s->rep);
1550 }
1551
1552 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1553 rpf_last = s->rep->flags;
1554 goto resync_response;
1555 }
1556 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001557 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001558 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1559 s->si[0].state != rp_cons_last ||
1560 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001561 unsigned int flags = s->rep->flags;
1562
Willy Tarreau0499e352010-12-17 07:13:42 +01001563 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1564 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1565 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1566 * for some free space in the response buffer, so we might need to call
1567 * it when something changes in the response buffer, but still we pass
1568 * it the request buffer. Note that the SI state might very well still
1569 * be zero due to us returning a flow of redirects!
1570 */
1571 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1572 s->req->flags |= BF_WAKE_ONCE;
1573 }
1574
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001575 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001576 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001577 unsigned int ana_list;
1578 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001579
Willy Tarreau90deb182010-01-07 00:20:41 +01001580 /* it's up to the analysers to stop disable reading or
1581 * closing. Note: if an analyser disables any of these
1582 * bits, it is responsible for enabling them again when
1583 * it disables itself, so that other analysers are called
1584 * in similar conditions.
1585 */
1586 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001587 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001588
1589 /* We will call all analysers for which a bit is set in
1590 * s->rep->analysers, following the bit order from LSB
1591 * to MSB. The analysers must remove themselves from
1592 * the list when not needed. Any analyser may return 0
1593 * to break out of the loop, either because of missing
1594 * data to take a decision, or because it decides to
1595 * kill the session. We loop at least once through each
1596 * analyser, and we may loop again if other analysers
1597 * are added in the middle.
1598 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001599
1600 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001601 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001602 /* Warning! ensure that analysers are always placed in ascending order! */
1603
Emeric Brun97679e72010-09-23 17:56:44 +02001604 if (ana_list & AN_RES_INSPECT) {
1605 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1606 break;
1607 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1608 }
1609
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001610 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001611 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1612 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001613 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001614 }
1615
Emeric Brun1d33b292010-01-04 15:47:17 +01001616 if (ana_list & AN_RES_STORE_RULES) {
1617 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1618 break;
1619 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1620 }
1621
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001622 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001623 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1624 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001625 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001626 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001627
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001628 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001629 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1630 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001631 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001632 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001633 break;
1634 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001635 }
1636
Willy Tarreau815a9b22010-07-27 17:15:12 +02001637 rp_cons_last = s->si[0].state;
1638 rp_prod_last = s->si[1].state;
1639 rpf_last = s->rep->flags;
1640
1641 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001642 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001643 }
1644
Willy Tarreau576507f2010-01-07 00:09:04 +01001645 /* maybe someone has added some request analysers, so we must check and loop */
1646 if (s->req->analysers & ~req_ana_back)
1647 goto resync_request;
1648
Willy Tarreau0499e352010-12-17 07:13:42 +01001649 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1650 goto resync_request;
1651
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001652 /* FIXME: here we should call protocol handlers which rely on
1653 * both buffers.
1654 */
1655
1656
1657 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001658 * Now we propagate unhandled errors to the session. Normally
1659 * we're just in a data phase here since it means we have not
1660 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001661 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001662 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001663 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001664 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1665 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001666 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001667 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001668 s->be->be_counters.cli_aborts++;
1669 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001670 if (srv)
1671 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001672 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001673 }
1674 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001675 s->be->be_counters.cli_aborts++;
1676 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001677 if (srv)
1678 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001679 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001680 }
1681 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001682 s->be->be_counters.srv_aborts++;
1683 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001684 if (srv)
1685 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001686 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001687 }
1688 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001689 s->be->be_counters.srv_aborts++;
1690 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001691 if (srv)
1692 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001693 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001694 }
Willy Tarreau84455332009-03-15 22:34:05 +01001695 sess_set_term_flags(s);
1696 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001697 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1698 /* Report it if the server got an error or a read timeout expired */
1699 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001700 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001701 s->be->be_counters.srv_aborts++;
1702 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001703 if (srv)
1704 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001705 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001706 }
1707 else if (s->rep->flags & BF_READ_TIMEOUT) {
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 Tarreau3deb3d02009-06-21 22:43:05 +02001712 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001713 }
1714 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001715 s->be->be_counters.cli_aborts++;
1716 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001717 if (srv)
1718 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001719 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001720 }
1721 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001722 s->be->be_counters.cli_aborts++;
1723 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001724 if (srv)
1725 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001726 s->flags |= SN_ERR_CLITO;
1727 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001728 sess_set_term_flags(s);
1729 }
Willy Tarreau84455332009-03-15 22:34:05 +01001730 }
1731
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001732 /*
1733 * Here we take care of forwarding unhandled data. This also includes
1734 * connection establishments and shutdown requests.
1735 */
1736
1737
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001738 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001739 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001740 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1741 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001742 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001743 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001744 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001745 (s->req->prod->state >= SI_ST_EST) &&
1746 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001747 /* This buffer is freewheeling, there's no analyser nor hijacker
1748 * attached to it. If any data are left in, we'll permit them to
1749 * move.
1750 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001751 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001752 buffer_auto_connect(s->req);
1753 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001754 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001755
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001756 /* We'll let data flow between the producer (if still connected)
1757 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001758 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001759 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001760 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001761 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001762
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001763 /* check if it is wise to enable kernel splicing to forward request data */
1764 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1765 s->req->to_forward &&
1766 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001767 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001768 (pipes_used < global.maxpipes) &&
1769 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1770 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1771 (s->req->flags & BF_STREAMER_FAST)))) {
1772 s->req->flags |= BF_KERN_SPLICING;
1773 }
1774
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001775 /* reflect what the L7 analysers have seen last */
1776 rqf_last = s->req->flags;
1777
1778 /*
1779 * Now forward all shutdown requests between both sides of the buffer
1780 */
1781
Willy Tarreau520d95e2009-09-19 21:04:57 +02001782 /* first, let's check if the request buffer needs to shutdown(write), which may
1783 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001784 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001785 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001786 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001787 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001788 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001789
1790 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001791 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 +01001792 s->req->cons->shutw(s->req->cons);
1793
1794 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001795 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1796 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001797 buffer_shutr_now(s->req);
1798
1799 /* shutdown(read) pending */
1800 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1801 s->req->prod->shutr(s->req->prod);
1802
Willy Tarreau520d95e2009-09-19 21:04:57 +02001803 /* it's possible that an upper layer has requested a connection setup or abort.
1804 * There are 2 situations where we decide to establish a new connection :
1805 * - there are data scheduled for emission in the buffer
1806 * - the BF_AUTO_CONNECT flag is set (active connection)
1807 */
1808 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001809 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001810 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001811 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001812 * switch to the connected state, otherwise we perform a connection
1813 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001814 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001815 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001816 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001817 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001818 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001819 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1820 s->req->wex = TICK_ETERNITY;
1821 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001822 }
Willy Tarreau73201222009-08-16 18:27:24 +02001823 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001824 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001825 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001826 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1827 buffer_shutr_now(s->rep);
1828 }
Willy Tarreau92795622009-03-06 12:51:23 +01001829 }
1830
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001831
1832 /* we may have a pending connection request, or a connection waiting
1833 * for completion.
1834 */
1835 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1836 do {
1837 /* nb: step 1 might switch from QUE to ASS, but we first want
1838 * to give a chance to step 2 to perform a redirect if needed.
1839 */
1840 if (s->si[1].state != SI_ST_REQ)
1841 sess_update_stream_int(s, &s->si[1]);
1842 if (s->si[1].state == SI_ST_REQ)
1843 sess_prepare_conn_req(s, &s->si[1]);
1844
Willy Tarreau827aee92011-03-10 16:55:02 +01001845 srv = target_srv(&s->target);
1846 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001847 perform_http_redirect(s, &s->si[1]);
1848 } while (s->si[1].state == SI_ST_ASS);
1849 }
1850
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001851 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001852 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001853 goto resync_stream_interface;
1854
Willy Tarreau815a9b22010-07-27 17:15:12 +02001855 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001856 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001857 goto resync_request;
1858
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001859 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001860
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001861 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001862 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001863 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1864 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001865 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001866 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001867 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001868 (s->rep->prod->state >= SI_ST_EST) &&
1869 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001870 /* This buffer is freewheeling, there's no analyser nor hijacker
1871 * attached to it. If any data are left in, we'll permit them to
1872 * move.
1873 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001874 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001875 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001876 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001877
1878 /* We'll let data flow between the producer (if still connected)
1879 * to the consumer.
1880 */
1881 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001882 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001883 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001884
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001885 /* check if it is wise to enable kernel splicing to forward response data */
1886 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1887 s->rep->to_forward &&
1888 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001889 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001890 (pipes_used < global.maxpipes) &&
1891 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1892 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1893 (s->rep->flags & BF_STREAMER_FAST)))) {
1894 s->rep->flags |= BF_KERN_SPLICING;
1895 }
1896
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001897 /* reflect what the L7 analysers have seen last */
1898 rpf_last = s->rep->flags;
1899
1900 /*
1901 * Now forward all shutdown requests between both sides of the buffer
1902 */
1903
1904 /*
1905 * FIXME: this is probably where we should produce error responses.
1906 */
1907
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001908 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001909 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1910 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001911 buffer_shutw_now(s->rep);
1912
1913 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001914 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 +01001915 s->rep->cons->shutw(s->rep->cons);
1916
1917 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001918 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1919 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001920 buffer_shutr_now(s->rep);
1921
1922 /* shutdown(read) pending */
1923 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1924 s->rep->prod->shutr(s->rep->prod);
1925
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001926 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001927 goto resync_stream_interface;
1928
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001929 if (s->req->flags != rqf_last)
1930 goto resync_request;
1931
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001932 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001933 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001935 /* we're interested in getting wakeups again */
1936 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1937 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1938
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001939 /* This is needed only when debugging is enabled, to indicate
1940 * client-side or server-side close. Please note that in the unlikely
1941 * event where both sides would close at once, the sequence is reported
1942 * on the server side first.
1943 */
1944 if (unlikely((global.mode & MODE_DEBUG) &&
1945 (!(global.mode & MODE_QUIET) ||
1946 (global.mode & MODE_VERBOSE)))) {
1947 int len;
1948
1949 if (s->si[1].state == SI_ST_CLO &&
1950 s->si[1].prev_state == SI_ST_EST) {
1951 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1952 s->uniq_id, s->be->id,
1953 (unsigned short)s->si[0].fd,
1954 (unsigned short)s->si[1].fd);
1955 write(1, trash, len);
1956 }
1957
1958 if (s->si[0].state == SI_ST_CLO &&
1959 s->si[0].prev_state == SI_ST_EST) {
1960 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1961 s->uniq_id, s->be->id,
1962 (unsigned short)s->si[0].fd,
1963 (unsigned short)s->si[1].fd);
1964 write(1, trash, len);
1965 }
1966 }
1967
1968 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1969 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1970
1971 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1972 session_process_counters(s);
1973
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001974 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001975 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001976
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001977 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001978 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001979
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001980 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1981 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 +01001982 s->si[0].prev_state = s->si[0].state;
1983 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001984 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1985 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001986
1987 /* Trick: if a request is being waiting for the server to respond,
1988 * and if we know the server can timeout, we don't want the timeout
1989 * to expire on the client side first, but we're still interested
1990 * in passing data from the client to the server (eg: POST). Thus,
1991 * we can cancel the client's request timeout if the server's
1992 * request timeout is set and the server has not yet sent a response.
1993 */
1994
Willy Tarreau520d95e2009-09-19 21:04:57 +02001995 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001996 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1997 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001998 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001999 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002000
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002001 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002002 * Note that this one may wake the task up again.
2003 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002004 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2005 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2006 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2007 s->req->cons->target.ptr.a->fct(s->req->cons);
2008 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2009 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002010 if (task_in_rq(t)) {
2011 /* If we woke up, we don't want to requeue the
2012 * task to the wait queue, but rather requeue
2013 * it into the runqueue ASAP.
2014 */
2015 t->expire = TICK_ETERNITY;
2016 return t;
2017 }
2018 }
2019
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002020 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2021 tick_first(s->rep->rex, s->rep->wex));
2022 if (s->req->analysers)
2023 t->expire = tick_first(t->expire, s->req->analyse_exp);
2024
2025 if (s->si[0].exp)
2026 t->expire = tick_first(t->expire, s->si[0].exp);
2027
2028 if (s->si[1].exp)
2029 t->expire = tick_first(t->expire, s->si[1].exp);
2030
2031#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002032 fprintf(stderr,
2033 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2034 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2035 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2036 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 +01002037#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
2039#ifdef DEBUG_DEV
2040 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002041 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002042 ABORT_NOW();
2043#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002044 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045 }
2046
2047 s->fe->feconn--;
2048 if (s->flags & SN_BE_ASSIGNED)
2049 s->be->beconn--;
2050 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002051 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002052 s->listener->nbconn--;
2053 if (s->listener->state == LI_FULL &&
2054 s->listener->nbconn < s->listener->maxconn) {
2055 /* we should reactivate the listener */
2056 EV_FD_SET(s->listener->fd, DIR_RD);
2057 s->listener->state = LI_READY;
2058 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002059
2060 if (unlikely((global.mode & MODE_DEBUG) &&
2061 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2062 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002063 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002064 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002065 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002066 write(1, trash, len);
2067 }
2068
2069 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2070 session_process_counters(s);
2071
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002072 if (s->txn.status) {
2073 int n;
2074
2075 n = s->txn.status / 100;
2076 if (n < 1 || n > 5)
2077 n = 0;
2078
2079 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002080 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002081
Willy Tarreau24657792010-02-26 10:30:28 +01002082 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002083 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002084 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002085 }
2086
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002087 /* let's do a final log if we need it */
2088 if (s->logs.logwait &&
2089 !(s->flags & SN_MONITOR) &&
2090 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002091 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002092 }
2093
2094 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002095 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002096 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002098 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002099}
2100
Willy Tarreau7c669d72008-06-20 15:04:11 +02002101/*
2102 * This function adjusts sess->srv_conn and maintains the previous and new
2103 * server's served session counts. Setting newsrv to NULL is enough to release
2104 * current connection slot. This function also notifies any LB algo which might
2105 * expect to be informed about any change in the number of active sessions on a
2106 * server.
2107 */
2108void sess_change_server(struct session *sess, struct server *newsrv)
2109{
2110 if (sess->srv_conn == newsrv)
2111 return;
2112
2113 if (sess->srv_conn) {
2114 sess->srv_conn->served--;
2115 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2116 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2117 sess->srv_conn = NULL;
2118 }
2119
2120 if (newsrv) {
2121 newsrv->served++;
2122 if (newsrv->proxy->lbprm.server_take_conn)
2123 newsrv->proxy->lbprm.server_take_conn(newsrv);
2124 sess->srv_conn = newsrv;
2125 }
2126}
2127
Willy Tarreau84455332009-03-15 22:34:05 +01002128/* Set correct session termination flags in case no analyser has done it. It
2129 * also counts a failed request if the server state has not reached the request
2130 * stage.
2131 */
2132void sess_set_term_flags(struct session *s)
2133{
2134 if (!(s->flags & SN_FINST_MASK)) {
2135 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002136
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002137 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002138 if (s->listener->counters)
2139 s->listener->counters->failed_req++;
2140
Willy Tarreau84455332009-03-15 22:34:05 +01002141 s->flags |= SN_FINST_R;
2142 }
2143 else if (s->si[1].state == SI_ST_QUE)
2144 s->flags |= SN_FINST_Q;
2145 else if (s->si[1].state < SI_ST_EST)
2146 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002147 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002148 s->flags |= SN_FINST_D;
2149 else
2150 s->flags |= SN_FINST_L;
2151 }
2152}
2153
2154/* 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 */