blob: 86aa28c7cbd444a94cf9fcae257eef0c13f62141 [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 Tarreau62793712011-07-24 19:23:38 +020035#include <proto/protocols.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010036#include <proto/proto_http.h>
37#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020038#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010040#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010041#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010042#include <proto/stream_interface.h>
43#include <proto/stream_sock.h>
44#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020046struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010047struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau81f9aa32010-06-01 17:45:26 +020049/* This function is called from the protocol layer accept() in order to instanciate
50 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010051 * value upon success, 0 if the connection can be ignored, or a negative value upon
52 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020053 */
54int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
55{
56 struct proxy *p = l->frontend;
57 struct session *s;
58 struct http_txn *txn;
59 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010060 int ret;
61
62
63 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020064
Willy Tarreaufffe1322010-11-11 09:48:16 +010065 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020067
68 /* minimum session initialization required for monitor mode below */
69 s->flags = 0;
70 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020071 s->stkctr1_entry = NULL;
72 s->stkctr2_entry = NULL;
73 s->stkctr1_table = NULL;
74 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020075
76 /* if this session comes from a known monitoring system, we want to ignore
77 * it as soon as possible, which means closing it immediately for TCP, but
78 * cleanly.
79 */
80 if (unlikely((l->options & LI_O_CHK_MONNET) &&
81 addr->ss_family == AF_INET &&
82 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
83 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010084 ret = 0; /* successful termination */
85 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020086 }
87 s->flags |= SN_MONITOR;
88 s->logs.logwait = 0;
89 }
90
Willy Tarreauabe8ea52010-11-11 10:56:04 +010091 if (unlikely((t = task_new()) == NULL))
92 goto out_free_session;
93
Willy Tarreau81f9aa32010-06-01 17:45:26 +020094 /* OK, we're keeping the session, so let's properly initialize the session */
95 LIST_ADDQ(&sessions, &s->list);
96 LIST_INIT(&s->back_refs);
97
Willy Tarreau81f9aa32010-06-01 17:45:26 +020098 s->term_trace = 0;
Willy Tarreau6471afb2011-09-23 10:54:59 +020099 s->si[0].addr.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200100 s->logs.accept_date = date; /* user-visible date for logging */
101 s->logs.tv_accept = now; /* corrected date for internal use */
102 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200103 p->feconn++; /* beconn will be increased once assigned */
104
Willy Tarreaub36b4242010-06-04 20:59:39 +0200105 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200106
107 t->process = l->handler;
108 t->context = s;
109 t->nice = l->nice;
110 t->expire = TICK_ETERNITY;
111
112 s->task = t;
113 s->listener = l;
114
115 /* Note: initially, the session's backend points to the frontend.
116 * This changes later when switching rules are executed or
117 * when the default backend is assigned.
118 */
119 s->be = s->fe = p;
120 s->req = s->rep = NULL; /* will be allocated later */
121
122 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
123 * to abort right here as soon as possible, we check the rules before
124 * even initializing the stream interfaces.
125 */
126 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 /* let's do a no-linger now to close with a single RST. */
128 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100129 ret = 0; /* successful termination */
130 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200131 }
132
Willy Tarreaub36b4242010-06-04 20:59:39 +0200133 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100134 if (p->feconn > p->fe_counters.conn_max)
135 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100136
Willy Tarreaub36b4242010-06-04 20:59:39 +0200137 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200138 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200139 void *ptr;
140
Willy Tarreau56123282010-08-06 19:06:56 +0200141 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200142 if (ptr)
143 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200144
Willy Tarreau56123282010-08-06 19:06:56 +0200145 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200146 if (ptr)
147 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200148 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200149 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200150
Willy Tarreau56123282010-08-06 19:06:56 +0200151 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200152 void *ptr;
153
Willy Tarreau56123282010-08-06 19:06:56 +0200154 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200155 if (ptr)
156 stktable_data_cast(ptr, sess_cnt)++;
157
Willy Tarreau56123282010-08-06 19:06:56 +0200158 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200159 if (ptr)
160 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200161 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200162 }
163
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200164 /* this part should be common with other protocols */
165 s->si[0].fd = cfd;
166 s->si[0].owner = t;
167 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
168 s->si[0].err_type = SI_ET_NONE;
169 s->si[0].err_loc = NULL;
170 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200171 s->si[0].release = NULL;
Willy Tarreau9b061e32012-04-07 18:03:52 +0200172 s->si[0].get_src = getpeername;
173 s->si[0].get_dst = getsockname;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100174 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200175 s->si[0].exp = TICK_ETERNITY;
176 s->si[0].flags = SI_FL_NONE;
177
178 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
179 s->si[0].flags |= SI_FL_INDEP_STR;
180
181 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
182 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
183
184 /* add the various callbacks */
185 stream_sock_prepare_interface(&s->si[0]);
186
187 /* pre-initialize the other side's stream interface to an INIT state. The
188 * callbacks will be initialized before attempting to connect.
189 */
190 s->si[1].fd = -1; /* just to help with debugging */
191 s->si[1].owner = t;
192 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
193 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200194 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200195 s->si[1].err_loc = NULL;
196 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200197 s->si[1].release = NULL;
Willy Tarreau9b061e32012-04-07 18:03:52 +0200198 s->si[1].get_src = NULL;
199 s->si[1].get_dst = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100200 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201 s->si[1].shutr = stream_int_shutr;
202 s->si[1].shutw = stream_int_shutw;
203 s->si[1].exp = TICK_ETERNITY;
204 s->si[1].flags = SI_FL_NONE;
205
206 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
207 s->si[1].flags |= SI_FL_INDEP_STR;
208
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200209 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100210 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200211 s->pend_pos = NULL;
212
213 /* init store persistence */
214 s->store_count = 0;
215
216 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100217 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200219
220 txn = &s->txn;
221 /* Those variables will be checked and freed if non-NULL in
222 * session.c:session_free(). It is important that they are
223 * properly initialized.
224 */
225 txn->sessid = NULL;
226 txn->srv_cookie = NULL;
227 txn->cli_cookie = NULL;
228 txn->uri = NULL;
229 txn->req.cap = NULL;
230 txn->rsp.cap = NULL;
231 txn->hdr_idx.v = NULL;
232 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100233 txn->req.flags = 0;
234 txn->rsp.flags = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235
236 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
237 goto out_free_task; /* no memory */
238
239 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
240 goto out_free_req; /* no memory */
241
242 /* initialize the request buffer */
243 s->req->size = global.tune.bufsize;
244 buffer_init(s->req);
245 s->req->prod = &s->si[0];
246 s->req->cons = &s->si[1];
247 s->si[0].ib = s->si[1].ob = s->req;
248 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
249
250 /* activate default analysers enabled for this listener */
251 s->req->analysers = l->analysers;
252
253 s->req->wto = TICK_ETERNITY;
254 s->req->rto = TICK_ETERNITY;
255 s->req->rex = TICK_ETERNITY;
256 s->req->wex = TICK_ETERNITY;
257 s->req->analyse_exp = TICK_ETERNITY;
258
259 /* initialize response buffer */
260 s->rep->size = global.tune.bufsize;
261 buffer_init(s->rep);
262 s->rep->prod = &s->si[1];
263 s->rep->cons = &s->si[0];
264 s->si[0].ob = s->si[1].ib = s->rep;
265 s->rep->analysers = 0;
266
Willy Tarreau96e31212011-05-30 18:10:30 +0200267 if (s->fe->options2 & PR_O2_NODELAY) {
268 s->req->flags |= BF_NEVER_WAIT;
269 s->rep->flags |= BF_NEVER_WAIT;
270 }
271
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200272 s->rep->rto = TICK_ETERNITY;
273 s->rep->wto = TICK_ETERNITY;
274 s->rep->rex = TICK_ETERNITY;
275 s->rep->wex = TICK_ETERNITY;
276 s->rep->analyse_exp = TICK_ETERNITY;
277
278 /* finish initialization of the accepted file descriptor */
279 fd_insert(cfd);
280 fdtab[cfd].owner = &s->si[0];
281 fdtab[cfd].state = FD_STREADY;
282 fdtab[cfd].flags = 0;
283 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
284 fdtab[cfd].cb[DIR_RD].b = s->req;
285 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
286 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200287 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.from;
288 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200289 EV_FD_SET(cfd, DIR_RD);
290
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100291 if (p->accept && (ret = p->accept(s)) <= 0) {
292 /* Either we had an unrecoverable error (<0) or work is
293 * finished (=0, eg: monitoring), in both situations,
294 * we can release everything and close.
295 */
296 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200297 }
298
299 /* it is important not to call the wakeup function directly but to
300 * pass through task_wakeup(), because this one knows how to apply
301 * priorities to tasks.
302 */
303 task_wakeup(t, TASK_WOKEN_INIT);
304 return 1;
305
306 /* Error unrolling */
307 out_free_rep:
308 pool_free2(pool2_buffer, s->rep);
309 out_free_req:
310 pool_free2(pool2_buffer, s->req);
311 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200312 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200313 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200314 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200315 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200316 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100317 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200318 pool_free2(pool2_session, s);
319 out_close:
Willy Tarreau2b154922011-07-22 17:36:27 +0200320 if (ret < 0 && s->fe->mode == PR_MODE_HTTP) {
321 /* critical error, no more memory, try to emit a 500 response */
322 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
323 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
324 }
325
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100326 if (fdtab[cfd].state != FD_STCLOSE)
327 fd_delete(cfd);
328 else
329 close(cfd);
330 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200331}
332
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333/*
334 * frees the context associated to a session. It must have been removed first.
335 */
Simon Hormandec5be42011-06-08 09:19:07 +0900336static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100338 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200339 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100340 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200341 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100342
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 if (s->pend_pos)
344 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100345
Willy Tarreau827aee92011-03-10 16:55:02 +0100346 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100347 if (s->flags & SN_CURR_SESS) {
348 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100349 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100350 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100351 if (may_dequeue_tasks(target_srv(&s->target), s->be))
352 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100353 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100354
Willy Tarreau7c669d72008-06-20 15:04:11 +0200355 if (unlikely(s->srv_conn)) {
356 /* the session still has a reserved slot on a server, but
357 * it should normally be only the same as the one above,
358 * so this should not happen in fact.
359 */
360 sess_change_server(s, NULL);
361 }
362
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100363 if (s->req->pipe)
364 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100365
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100366 if (s->rep->pipe)
367 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100368
Willy Tarreau48d63db2008-08-03 17:41:33 +0200369 pool_free2(pool2_buffer, s->req);
370 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371
Willy Tarreau46023632010-01-07 22:51:47 +0100372 http_end_txn(s);
373
Willy Tarreaua4cda672010-06-06 18:28:49 +0200374 for (i = 0; i < s->store_count; i++) {
375 if (!s->store[i].ts)
376 continue;
377 stksess_free(s->store[i].table, s->store[i].ts);
378 s->store[i].ts = NULL;
379 }
380
Willy Tarreau34eb6712011-10-24 18:15:04 +0200381 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200382 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100383 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
384 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100386
Willy Tarreau56123282010-08-06 19:06:56 +0200387 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200388 session_store_counters(s);
389
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100390 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100391 /* we have to unlink all watchers. We must not relink them if
392 * this session was the last one in the list.
393 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100394 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100395 LIST_INIT(&bref->users);
396 if (s->list.n != &sessions)
397 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100398 bref->ref = s->list.n;
399 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100400 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200401 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200402
403 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200404 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200405 pool_flush2(pool2_buffer);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200406 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200407 pool_flush2(pool2_requri);
408 pool_flush2(pool2_capture);
409 pool_flush2(pool2_session);
410 pool_flush2(fe->req_cap_pool);
411 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200412 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200413}
414
415
416/* perform minimal intializations, report 0 in case of error, 1 if OK. */
417int init_session()
418{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100419 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200420 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
421 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422}
423
Willy Tarreau30e71012007-11-26 20:15:35 +0100424void session_process_counters(struct session *s)
425{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100426 unsigned long long bytes;
427
Willy Tarreau30e71012007-11-26 20:15:35 +0100428 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100429 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100430 s->logs.bytes_in = s->req->total;
431 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100432 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100433
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100434 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100435
Willy Tarreau827aee92011-03-10 16:55:02 +0100436 if (target_srv(&s->target))
437 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200438
439 if (s->listener->counters)
440 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200441
Willy Tarreau56123282010-08-06 19:06:56 +0200442 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200443 void *ptr;
444
Willy Tarreau56123282010-08-06 19:06:56 +0200445 ptr = stktable_data_ptr(s->stkctr2_table,
446 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200447 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200448 if (ptr)
449 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200450
Willy Tarreau56123282010-08-06 19:06:56 +0200451 ptr = stktable_data_ptr(s->stkctr2_table,
452 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200453 STKTABLE_DT_BYTES_IN_RATE);
454 if (ptr)
455 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200456 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200457 }
458
Willy Tarreau56123282010-08-06 19:06:56 +0200459 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200460 void *ptr;
461
Willy Tarreau56123282010-08-06 19:06:56 +0200462 ptr = stktable_data_ptr(s->stkctr1_table,
463 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200464 STKTABLE_DT_BYTES_IN_CNT);
465 if (ptr)
466 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
467
Willy Tarreau56123282010-08-06 19:06:56 +0200468 ptr = stktable_data_ptr(s->stkctr1_table,
469 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200470 STKTABLE_DT_BYTES_IN_RATE);
471 if (ptr)
472 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200473 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200474 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100475 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476 }
477
Willy Tarreau30e71012007-11-26 20:15:35 +0100478 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100480 s->logs.bytes_out = s->rep->total;
481 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100482 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100483
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100484 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100485
Willy Tarreau827aee92011-03-10 16:55:02 +0100486 if (target_srv(&s->target))
487 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200488
489 if (s->listener->counters)
490 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200491
Willy Tarreau56123282010-08-06 19:06:56 +0200492 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200493 void *ptr;
494
Willy Tarreau56123282010-08-06 19:06:56 +0200495 ptr = stktable_data_ptr(s->stkctr2_table,
496 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200497 STKTABLE_DT_BYTES_OUT_CNT);
498 if (ptr)
499 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
500
Willy Tarreau56123282010-08-06 19:06:56 +0200501 ptr = stktable_data_ptr(s->stkctr2_table,
502 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200503 STKTABLE_DT_BYTES_OUT_RATE);
504 if (ptr)
505 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200506 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200507 }
508
Willy Tarreau56123282010-08-06 19:06:56 +0200509 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200510 void *ptr;
511
Willy Tarreau56123282010-08-06 19:06:56 +0200512 ptr = stktable_data_ptr(s->stkctr1_table,
513 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200515 if (ptr)
516 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200517
Willy Tarreau56123282010-08-06 19:06:56 +0200518 ptr = stktable_data_ptr(s->stkctr1_table,
519 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200520 STKTABLE_DT_BYTES_OUT_RATE);
521 if (ptr)
522 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200523 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200524 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100525 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100526 }
527}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529/* This function is called with (si->state == SI_ST_CON) meaning that a
530 * connection was attempted and that the file descriptor is already allocated.
531 * We must check for establishment, error and abort. Possible output states
532 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
533 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
534 * otherwise 1.
535 */
Simon Hormandec5be42011-06-08 09:19:07 +0900536static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100537{
538 struct buffer *req = si->ob;
539 struct buffer *rep = si->ib;
540
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 /* If we got an error, or if nothing happened and the connection timed
542 * out, we must give up. The CER state handler will take care of retry
543 * attempts and error reports.
544 */
545 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100546 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200548 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100549 fd_delete(si->fd);
550
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200551 if (si->release)
552 si->release(si);
553
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100554 if (si->err_type)
555 return 0;
556
Willy Tarreau827aee92011-03-10 16:55:02 +0100557 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558 if (si->flags & SI_FL_ERR)
559 si->err_type = SI_ET_CONN_ERR;
560 else
561 si->err_type = SI_ET_CONN_TO;
562 return 0;
563 }
564
565 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200566 if (unlikely((rep->flags & BF_SHUTW) ||
567 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200568 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100569 s->be->options & PR_O_ABRT_CLOSE)))) {
570 /* give up */
571 si->shutw(si);
572 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100573 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200574 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100575 if (s->srv_error)
576 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577 return 1;
578 }
579
580 /* we need to wait a bit more if there was no activity either */
581 if (!(req->flags & BF_WRITE_ACTIVITY))
582 return 1;
583
584 /* OK, this means that a connection succeeded. The caller will be
585 * responsible for handling the transition from CON to EST.
586 */
587 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100588 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 si->state = SI_ST_EST;
590 si->err_type = SI_ET_NONE;
591 si->err_loc = NULL;
592 return 1;
593}
594
595/* This function is called with (si->state == SI_ST_CER) meaning that a
596 * previous connection attempt has failed and that the file descriptor
597 * has already been released. Possible causes include asynchronous error
598 * notification and time out. Possible output states are SI_ST_CLO when
599 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
600 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
601 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
602 * marked as in error state. It returns 0.
603 */
Simon Hormandec5be42011-06-08 09:19:07 +0900604static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605{
606 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 if (target_srv(&s->target)) {
608 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100609
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100610 if (s->flags & SN_CURR_SESS) {
611 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100612 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 }
615
616 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200617 si->conn_retries--;
618 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 if (!si->err_type) {
620 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100621 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 }
623
Willy Tarreau827aee92011-03-10 16:55:02 +0100624 if (target_srv(&s->target))
625 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100626 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100627 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100628 if (may_dequeue_tasks(target_srv(&s->target), s->be))
629 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630
631 /* shutw is enough so stop a connecting socket */
632 si->shutw(si);
633 si->ob->flags |= BF_WRITE_ERROR;
634 si->ib->flags |= BF_READ_ERROR;
635
636 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100637 if (s->srv_error)
638 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100639 return 0;
640 }
641
642 /* If the "redispatch" option is set on the backend, we are allowed to
643 * retry on another server for the last retry. In order to achieve this,
644 * we must mark the session unassigned, and eventually clear the DIRECT
645 * bit to ignore any persistence cookie. We won't count a retry nor a
646 * redispatch yet, because this will depend on what server is selected.
647 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100648 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100649 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100650 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 if (may_dequeue_tasks(target_srv(&s->target), s->be))
652 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653
654 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655 si->state = SI_ST_REQ;
656 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100657 if (target_srv(&s->target))
658 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100659 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100660 si->state = SI_ST_ASS;
661 }
662
663 if (si->flags & SI_FL_ERR) {
664 /* The error was an asynchronous connection error, and we will
665 * likely have to retry connecting to the same server, most
666 * likely leading to the same result. To avoid this, we wait
667 * one second before retrying.
668 */
669
670 if (!si->err_type)
671 si->err_type = SI_ET_CONN_ERR;
672
673 si->state = SI_ST_TAR;
674 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
675 return 0;
676 }
677 return 0;
678}
679
680/*
681 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200682 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
683 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684 */
Simon Hormandec5be42011-06-08 09:19:07 +0900685static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686{
687 struct buffer *req = si->ob;
688 struct buffer *rep = si->ib;
689
Willy Tarreau827aee92011-03-10 16:55:02 +0100690 if (target_srv(&s->target))
691 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100692
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 /* if the user wants to log as soon as possible, without counting
695 * bytes from the server, then this is the right moment. */
696 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
697 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100698 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 }
701 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
703 /* reset hdr_idx which was already initialized by the request.
704 * right now, the http parser does it.
705 * hdr_idx_init(&s->txn.hdr_idx);
706 */
707 }
708
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200709 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200711 if (si->connect) {
712 /* real connections have timeouts */
713 req->wto = s->be->timeout.server;
714 rep->rto = s->be->timeout.server;
715 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100716 req->wex = TICK_ETERNITY;
717}
718
719/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
720 * Other input states are simply ignored.
721 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
722 * Flags must have previously been updated for timeouts and other conditions.
723 */
Simon Hormandec5be42011-06-08 09:19:07 +0900724static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725{
Willy Tarreau827aee92011-03-10 16:55:02 +0100726 struct server *srv = target_srv(&s->target);
727
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100728 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729 now_ms, __FUNCTION__,
730 s,
731 s->req, s->rep,
732 s->req->rex, s->rep->wex,
733 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100734 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735
736 if (si->state == SI_ST_ASS) {
737 /* Server assigned to connection request, we have to try to connect now */
738 int conn_err;
739
740 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100741 srv = target_srv(&s->target);
742
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 if (conn_err == SN_ERR_NONE) {
744 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100745 if (srv)
746 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747 return;
748 }
749
750 /* We have received a synchronous error. We might have to
751 * abort, retry immediately or redispatch.
752 */
753 if (conn_err == SN_ERR_INTERNAL) {
754 if (!si->err_type) {
755 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100756 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 }
758
Willy Tarreau827aee92011-03-10 16:55:02 +0100759 if (srv)
760 srv_inc_sess_ctr(srv);
761 if (srv)
762 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100763 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764
765 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100766 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 if (may_dequeue_tasks(srv, s->be))
768 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769
770 /* Failed and not retryable. */
771 si->shutr(si);
772 si->shutw(si);
773 si->ob->flags |= BF_WRITE_ERROR;
774
775 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
776
777 /* no session was ever accounted for this server */
778 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100779 if (s->srv_error)
780 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100781 return;
782 }
783
784 /* We are facing a retryable error, but we don't want to run a
785 * turn-around now, as the problem is likely a source port
786 * allocation problem, so we want to retry now.
787 */
788 si->state = SI_ST_CER;
789 si->flags &= ~SI_FL_ERR;
790 sess_update_st_cer(s, si);
791 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
792 return;
793 }
794 else if (si->state == SI_ST_QUE) {
795 /* connection request was queued, check for any update */
796 if (!s->pend_pos) {
797 /* The connection is not in the queue anymore. Either
798 * we have a server connection slot available and we
799 * go directly to the assigned state, or we need to
800 * load-balance first and go to the INI state.
801 */
802 si->exp = TICK_ETERNITY;
803 if (unlikely(!(s->flags & SN_ASSIGNED)))
804 si->state = SI_ST_REQ;
805 else {
806 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
807 si->state = SI_ST_ASS;
808 }
809 return;
810 }
811
812 /* Connection request still in queue... */
813 if (si->flags & SI_FL_EXP) {
814 /* ... and timeout expired */
815 si->exp = TICK_ETERNITY;
816 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (srv)
818 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100819 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100820 si->shutr(si);
821 si->shutw(si);
822 si->ob->flags |= BF_WRITE_TIMEOUT;
823 if (!si->err_type)
824 si->err_type = SI_ET_QUEUE_TO;
825 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100826 if (s->srv_error)
827 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100828 return;
829 }
830
831 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200832 if ((si->ob->flags & (BF_READ_ERROR)) ||
833 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200834 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100835 /* give up */
836 si->exp = TICK_ETERNITY;
837 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
838 si->shutr(si);
839 si->shutw(si);
840 si->err_type |= SI_ET_QUEUE_ABRT;
841 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100842 if (s->srv_error)
843 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 return;
845 }
846
847 /* Nothing changed */
848 return;
849 }
850 else if (si->state == SI_ST_TAR) {
851 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200852 if ((si->ob->flags & (BF_READ_ERROR)) ||
853 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200854 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855 /* give up */
856 si->exp = TICK_ETERNITY;
857 si->shutr(si);
858 si->shutw(si);
859 si->err_type |= SI_ET_CONN_ABRT;
860 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100861 if (s->srv_error)
862 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 return;
864 }
865
866 if (!(si->flags & SI_FL_EXP))
867 return; /* still in turn-around */
868
869 si->exp = TICK_ETERNITY;
870
871 /* we keep trying on the same server as long as the session is
872 * marked "assigned".
873 * FIXME: Should we force a redispatch attempt when the server is down ?
874 */
875 if (s->flags & SN_ASSIGNED)
876 si->state = SI_ST_ASS;
877 else
878 si->state = SI_ST_REQ;
879 return;
880 }
881}
882
Simon Hormandec5be42011-06-08 09:19:07 +0900883/* Set correct session termination flags in case no analyser has done it. It
884 * also counts a failed request if the server state has not reached the request
885 * stage.
886 */
887static void sess_set_term_flags(struct session *s)
888{
889 if (!(s->flags & SN_FINST_MASK)) {
890 if (s->si[1].state < SI_ST_REQ) {
891
892 s->fe->fe_counters.failed_req++;
893 if (s->listener->counters)
894 s->listener->counters->failed_req++;
895
896 s->flags |= SN_FINST_R;
897 }
898 else if (s->si[1].state == SI_ST_QUE)
899 s->flags |= SN_FINST_Q;
900 else if (s->si[1].state < SI_ST_EST)
901 s->flags |= SN_FINST_C;
902 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
903 s->flags |= SN_FINST_D;
904 else
905 s->flags |= SN_FINST_L;
906 }
907}
908
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909/* This function initiates a server connection request on a stream interface
910 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
911 * indicating that a server has been assigned. It may also return SI_ST_QUE,
912 * or SI_ST_CLO upon error.
913 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100914static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
915{
916 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 now_ms, __FUNCTION__,
918 s,
919 s->req, s->rep,
920 s->req->rex, s->rep->wex,
921 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100922 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923
924 if (si->state != SI_ST_REQ)
925 return;
926
927 /* Try to assign a server */
928 if (srv_redispatch_connect(s) != 0) {
929 /* We did not get a server. Either we queued the
930 * connection request, or we encountered an error.
931 */
932 if (si->state == SI_ST_QUE)
933 return;
934
935 /* we did not get any server, let's check the cause */
936 si->shutr(si);
937 si->shutw(si);
938 si->ob->flags |= BF_WRITE_ERROR;
939 if (!si->err_type)
940 si->err_type = SI_ET_CONN_OTHER;
941 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100942 if (s->srv_error)
943 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100944 return;
945 }
946
947 /* The server is assigned */
948 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
949 si->state = SI_ST_ASS;
950}
951
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200952/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100953 * if appropriate. The default_backend rule is also considered, then the
954 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200955 * It returns 1 if the processing can continue on next analysers, or zero if it
956 * either needs more data or wants to immediately abort the request.
957 */
Simon Hormandec5be42011-06-08 09:19:07 +0900958static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200959{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200960 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100961
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962 req->analysers &= ~an_bit;
963 req->analyse_exp = TICK_ETERNITY;
964
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100965 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200966 now_ms, __FUNCTION__,
967 s,
968 req,
969 req->rex, req->wex,
970 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100971 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200972 req->analysers);
973
974 /* now check whether we have some switching rules for this request */
975 if (!(s->flags & SN_BE_ASSIGNED)) {
976 struct switching_rule *rule;
977
978 list_for_each_entry(rule, &s->fe->switching_rules, list) {
979 int ret;
980
981 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
982 ret = acl_pass(ret);
983 if (rule->cond->pol == ACL_COND_UNLESS)
984 ret = !ret;
985
986 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200987 if (!session_set_backend(s, rule->be.backend))
988 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200989 break;
990 }
991 }
992
993 /* To ensure correct connection accounting on the backend, we
994 * have to assign one if it was not set (eg: a listen). This
995 * measure also takes care of correctly setting the default
996 * backend if any.
997 */
998 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200999 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1000 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001001 }
1002
Willy Tarreaufb356202010-08-03 14:02:05 +02001003 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1004 if (s->fe == s->be) {
1005 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001006 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001007 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001008
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001009 /* 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 +01001010 * persistence rule, and report that in the session.
1011 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001012 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001013 int ret = 1;
1014
1015 if (prst_rule->cond) {
1016 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1017 ret = acl_pass(ret);
1018 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1019 ret = !ret;
1020 }
1021
1022 if (ret) {
1023 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001024 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1025 s->flags |= SN_FORCE_PRST;
1026 } else {
1027 s->flags |= SN_IGNORE_PRST;
1028 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001029 break;
1030 }
1031 }
1032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001033 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001034
1035 sw_failed:
1036 /* immediately abort this request in case of allocation failure */
1037 buffer_abort(s->req);
1038 buffer_abort(s->rep);
1039
1040 if (!(s->flags & SN_ERR_MASK))
1041 s->flags |= SN_ERR_RESOURCE;
1042 if (!(s->flags & SN_FINST_MASK))
1043 s->flags |= SN_FINST_R;
1044
1045 s->txn.status = 500;
1046 s->req->analysers = 0;
1047 s->req->analyse_exp = TICK_ETERNITY;
1048 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001049}
1050
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001051/* This stream analyser works on a request. It applies all use-server rules on
1052 * it then returns 1. The data must already be present in the buffer otherwise
1053 * they won't match. It always returns 1.
1054 */
1055static int process_server_rules(struct session *s, struct buffer *req, int an_bit)
1056{
1057 struct proxy *px = s->be;
1058 struct server_rule *rule;
1059
1060 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1061 now_ms, __FUNCTION__,
1062 s,
1063 req,
1064 req->rex, req->wex,
1065 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001066 req->i + req->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001067 req->analysers);
1068
1069 if (!(s->flags & SN_ASSIGNED)) {
1070 list_for_each_entry(rule, &px->server_rules, list) {
1071 int ret;
1072
1073 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1074 ret = acl_pass(ret);
1075 if (rule->cond->pol == ACL_COND_UNLESS)
1076 ret = !ret;
1077
1078 if (ret) {
1079 struct server *srv = rule->srv.ptr;
1080
1081 if ((srv->state & SRV_RUNNING) ||
1082 (px->options & PR_O_PERSIST) ||
1083 (s->flags & SN_FORCE_PRST)) {
1084 s->flags |= SN_DIRECT | SN_ASSIGNED;
1085 set_target_server(&s->target, srv);
1086 break;
1087 }
1088 /* if the server is not UP, let's go on with next rules
1089 * just in case another one is suited.
1090 */
1091 }
1092 }
1093 }
1094
1095 req->analysers &= ~an_bit;
1096 req->analyse_exp = TICK_ETERNITY;
1097 return 1;
1098}
1099
Emeric Brun1d33b292010-01-04 15:47:17 +01001100/* This stream analyser works on a request. It applies all sticking rules on
1101 * it then returns 1. The data must already be present in the buffer otherwise
1102 * they won't match. It always returns 1.
1103 */
Simon Hormandec5be42011-06-08 09:19:07 +09001104static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001105{
1106 struct proxy *px = s->be;
1107 struct sticking_rule *rule;
1108
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001109 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001110 now_ms, __FUNCTION__,
1111 s,
1112 req,
1113 req->rex, req->wex,
1114 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001115 req->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001116 req->analysers);
1117
1118 list_for_each_entry(rule, &px->sticking_rules, list) {
1119 int ret = 1 ;
1120 int i;
1121
1122 for (i = 0; i < s->store_count; i++) {
1123 if (rule->table.t == s->store[i].table)
1124 break;
1125 }
1126
1127 if (i != s->store_count)
1128 continue;
1129
1130 if (rule->cond) {
1131 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1132 ret = acl_pass(ret);
1133 if (rule->cond->pol == ACL_COND_UNLESS)
1134 ret = !ret;
1135 }
1136
1137 if (ret) {
1138 struct stktable_key *key;
1139
Emeric Brun485479d2010-09-23 18:02:19 +02001140 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001141 if (!key)
1142 continue;
1143
1144 if (rule->flags & STK_IS_MATCH) {
1145 struct stksess *ts;
1146
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001147 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001148 if (!(s->flags & SN_ASSIGNED)) {
1149 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001150 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001151
1152 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001153 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1154 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001155 if (node) {
1156 struct server *srv;
1157
1158 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001159 if ((srv->state & SRV_RUNNING) ||
1160 (px->options & PR_O_PERSIST) ||
1161 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001162 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001163 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001164 }
1165 }
1166 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001167 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001168 }
1169 }
1170 if (rule->flags & STK_IS_STORE) {
1171 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1172 struct stksess *ts;
1173
1174 ts = stksess_new(rule->table.t, key);
1175 if (ts) {
1176 s->store[s->store_count].table = rule->table.t;
1177 s->store[s->store_count++].ts = ts;
1178 }
1179 }
1180 }
1181 }
1182 }
1183
1184 req->analysers &= ~an_bit;
1185 req->analyse_exp = TICK_ETERNITY;
1186 return 1;
1187}
1188
1189/* This stream analyser works on a response. It applies all store rules on it
1190 * then returns 1. The data must already be present in the buffer otherwise
1191 * they won't match. It always returns 1.
1192 */
Simon Hormandec5be42011-06-08 09:19:07 +09001193static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001194{
1195 struct proxy *px = s->be;
1196 struct sticking_rule *rule;
1197 int i;
1198
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001199 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001200 now_ms, __FUNCTION__,
1201 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001202 rep,
1203 rep->rex, rep->wex,
1204 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001205 rep->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001206 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001207
1208 list_for_each_entry(rule, &px->storersp_rules, list) {
1209 int ret = 1 ;
1210 int storereqidx = -1;
1211
1212 for (i = 0; i < s->store_count; i++) {
1213 if (rule->table.t == s->store[i].table) {
1214 if (!(s->store[i].flags))
1215 storereqidx = i;
1216 break;
1217 }
1218 }
1219
1220 if ((i != s->store_count) && (storereqidx == -1))
1221 continue;
1222
1223 if (rule->cond) {
1224 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1225 ret = acl_pass(ret);
1226 if (rule->cond->pol == ACL_COND_UNLESS)
1227 ret = !ret;
1228 }
1229
1230 if (ret) {
1231 struct stktable_key *key;
1232
Emeric Brun485479d2010-09-23 18:02:19 +02001233 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001234 if (!key)
1235 continue;
1236
1237 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001238 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001239 s->store[storereqidx].flags = 1;
1240 }
1241 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1242 struct stksess *ts;
1243
1244 ts = stksess_new(rule->table.t, key);
1245 if (ts) {
1246 s->store[s->store_count].table = rule->table.t;
1247 s->store[s->store_count].flags = 1;
1248 s->store[s->store_count++].ts = ts;
1249 }
1250 }
1251 }
1252 }
1253
1254 /* process store request and store response */
1255 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001256 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001257 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001258
Simon Hormanfa461682011-06-25 09:39:49 +09001259 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1260 stksess_free(s->store[i].table, s->store[i].ts);
1261 s->store[i].ts = NULL;
1262 continue;
1263 }
1264
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001265 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1266 if (ts) {
1267 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001268 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001269 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001270 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001271 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001272 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001273
1274 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001275 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001276 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001277 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001278 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001279
1280 rep->analysers &= ~an_bit;
1281 rep->analyse_exp = TICK_ETERNITY;
1282 return 1;
1283}
1284
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001285/* This macro is very specific to the function below. See the comments in
1286 * process_session() below to understand the logic and the tests.
1287 */
1288#define UPDATE_ANALYSERS(real, list, back, flag) { \
1289 list = (((list) & ~(flag)) | ~(back)) & (real); \
1290 back = real; \
1291 if (!(list)) \
1292 break; \
1293 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1294 continue; \
1295}
1296
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001297/* Processes the client, server, request and response jobs of a session task,
1298 * then puts it back to the wait queue in a clean state, or cleans up its
1299 * resources if it must be deleted. Returns in <next> the date the task wants
1300 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1301 * nothing too many times, the request and response buffers flags are monitored
1302 * and each function is called only if at least another function has changed at
1303 * least one flag it is interested in.
1304 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001305struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001306{
Willy Tarreau827aee92011-03-10 16:55:02 +01001307 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001308 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001309 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001310 unsigned int rq_prod_last, rq_cons_last;
1311 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001312 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001313
1314 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1315 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1316
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 /* this data may be no longer valid, clear it */
1318 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1319
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001320 /* This flag must explicitly be set every time */
1321 s->req->flags &= ~BF_READ_NOEXP;
1322
1323 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001324 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1325 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001326
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001327 /* we don't want the stream interface functions to recursively wake us up */
1328 if (s->req->prod->owner == t)
1329 s->req->prod->flags |= SI_FL_DONT_WAKE;
1330 if (s->req->cons->owner == t)
1331 s->req->cons->flags |= SI_FL_DONT_WAKE;
1332
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001333 /* 1a: Check for low level timeouts if needed. We just set a flag on
1334 * stream interfaces when their timeouts have expired.
1335 */
1336 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1337 stream_int_check_timeouts(&s->si[0]);
1338 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001339
1340 /* check buffer timeouts, and close the corresponding stream interfaces
1341 * for future reads or writes. Note: this will also concern upper layers
1342 * but we do not touch any other flag. We must be careful and correctly
1343 * detect state changes when calling them.
1344 */
1345
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001346 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001347
Willy Tarreau14641402009-12-29 14:49:56 +01001348 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1349 s->req->cons->flags |= SI_FL_NOLINGER;
1350 s->req->cons->shutw(s->req->cons);
1351 }
1352
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001353 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1354 s->req->prod->shutr(s->req->prod);
1355
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001356 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001357
Willy Tarreau14641402009-12-29 14:49:56 +01001358 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1359 s->rep->cons->flags |= SI_FL_NOLINGER;
1360 s->rep->cons->shutw(s->rep->cons);
1361 }
1362
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001363 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1364 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001365 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001366
1367 /* 1b: check for low-level errors reported at the stream interface.
1368 * First we check if it's a retryable error (in which case we don't
1369 * want to tell the buffer). Otherwise we report the error one level
1370 * upper by setting flags into the buffers. Note that the side towards
1371 * the client cannot have connect (hence retryable) errors. Also, the
1372 * connection setup code must be able to deal with any type of abort.
1373 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001374 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001375 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1376 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1377 s->si[0].shutr(&s->si[0]);
1378 s->si[0].shutw(&s->si[0]);
1379 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001380 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001381 s->be->be_counters.cli_aborts++;
1382 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001383 if (srv)
1384 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001385 if (!(s->flags & SN_ERR_MASK))
1386 s->flags |= SN_ERR_CLICL;
1387 if (!(s->flags & SN_FINST_MASK))
1388 s->flags |= SN_FINST_D;
1389 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001390 }
1391 }
1392
1393 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1394 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1395 s->si[1].shutr(&s->si[1]);
1396 s->si[1].shutw(&s->si[1]);
1397 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001398 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001399 if (srv)
1400 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001401 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001402 s->be->be_counters.srv_aborts++;
1403 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001404 if (srv)
1405 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001406 if (!(s->flags & SN_ERR_MASK))
1407 s->flags |= SN_ERR_SRVCL;
1408 if (!(s->flags & SN_FINST_MASK))
1409 s->flags |= SN_FINST_D;
1410 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001411 }
1412 /* note: maybe we should process connection errors here ? */
1413 }
1414
1415 if (s->si[1].state == SI_ST_CON) {
1416 /* we were trying to establish a connection on the server side,
1417 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1418 */
1419 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1420 sess_update_st_cer(s, &s->si[1]);
1421 else if (s->si[1].state == SI_ST_EST)
1422 sess_establish(s, &s->si[1]);
1423
1424 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1425 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1426 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1427 */
1428 }
1429
Willy Tarreau815a9b22010-07-27 17:15:12 +02001430 rq_prod_last = s->si[0].state;
1431 rq_cons_last = s->si[1].state;
1432 rp_cons_last = s->si[0].state;
1433 rp_prod_last = s->si[1].state;
1434
1435 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001436 /* Check for connection closure */
1437
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001438 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001439 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440 now_ms, __FUNCTION__, __LINE__,
1441 t,
1442 s, s->flags,
1443 s->req, s->rep,
1444 s->req->rex, s->rep->wex,
1445 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001446 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001447 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001448 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001449
1450 /* nothing special to be done on client side */
1451 if (unlikely(s->req->prod->state == SI_ST_DIS))
1452 s->req->prod->state = SI_ST_CLO;
1453
1454 /* When a server-side connection is released, we have to count it and
1455 * check for pending connections on this server.
1456 */
1457 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1458 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001459 srv = target_srv(&s->target);
1460 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001461 if (s->flags & SN_CURR_SESS) {
1462 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001463 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001464 }
1465 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001466 if (may_dequeue_tasks(srv, s->be))
1467 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001468 }
1469 }
1470
1471 /*
1472 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1473 * at this point.
1474 */
1475
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001476 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001477 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001478 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001479 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1480 s->si[0].state != rq_prod_last ||
1481 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001482 unsigned int flags = s->req->flags;
1483
1484 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001485 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 unsigned int ana_list;
1487 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001488
Willy Tarreau90deb182010-01-07 00:20:41 +01001489 /* it's up to the analysers to stop new connections,
1490 * disable reading or closing. Note: if an analyser
1491 * disables any of these bits, it is responsible for
1492 * enabling them again when it disables itself, so
1493 * that other analysers are called in similar conditions.
1494 */
1495 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001496 buffer_auto_connect(s->req);
1497 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001498
1499 /* We will call all analysers for which a bit is set in
1500 * s->req->analysers, following the bit order from LSB
1501 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001502 * the list when not needed. Any analyser may return 0
1503 * to break out of the loop, either because of missing
1504 * data to take a decision, or because it decides to
1505 * kill the session. We loop at least once through each
1506 * analyser, and we may loop again if other analysers
1507 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001508 *
1509 * We build a list of analysers to run. We evaluate all
1510 * of these analysers in the order of the lower bit to
1511 * the higher bit. This ordering is very important.
1512 * An analyser will often add/remove other analysers,
1513 * including itself. Any changes to itself have no effect
1514 * on the loop. If it removes any other analysers, we
1515 * want those analysers not to be called anymore during
1516 * this loop. If it adds an analyser that is located
1517 * after itself, we want it to be scheduled for being
1518 * processed during the loop. If it adds an analyser
1519 * which is located before it, we want it to switch to
1520 * it immediately, even if it has already been called
1521 * once but removed since.
1522 *
1523 * In order to achieve this, we compare the analyser
1524 * list after the call with a copy of it before the
1525 * call. The work list is fed with analyser bits that
1526 * appeared during the call. Then we compare previous
1527 * work list with the new one, and check the bits that
1528 * appeared. If the lowest of these bits is lower than
1529 * the current bit, it means we have enabled a previous
1530 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001531 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001532
1533 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001534 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001535 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001536
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001537 if (ana_list & AN_REQ_DECODE_PROXY) {
1538 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1539 break;
1540 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1541 }
1542
Willy Tarreaufb356202010-08-03 14:02:05 +02001543 if (ana_list & AN_REQ_INSPECT_FE) {
1544 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001545 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001546 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001547 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001548
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001549 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001550 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001551 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001552 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001553 }
1554
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001555 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001556 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1557 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001558 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001559 }
1560
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001561 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001562 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1563 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001564 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001565 }
1566
Willy Tarreaufb356202010-08-03 14:02:05 +02001567 if (ana_list & AN_REQ_INSPECT_BE) {
1568 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1569 break;
1570 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1571 }
1572
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001573 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001574 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1575 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001576 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001577 }
1578
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001579 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001580 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001581 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001582 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001583 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001584
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001585 if (ana_list & AN_REQ_SRV_RULES) {
1586 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1587 break;
1588 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1589 }
1590
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001591 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001592 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1593 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001594 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001595 }
1596
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001597 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001598 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001599 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001600 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001601 }
Emeric Brun647caf12009-06-30 17:57:00 +02001602
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001603 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001604 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1605 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001606 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001607 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001608
Emeric Brun1d33b292010-01-04 15:47:17 +01001609 if (ana_list & AN_REQ_STICKING_RULES) {
1610 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1611 break;
1612 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1613 }
1614
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001616 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1617 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001618 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001619 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001620 break;
1621 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001622 }
Willy Tarreau84455332009-03-15 22:34:05 +01001623
Willy Tarreau815a9b22010-07-27 17:15:12 +02001624 rq_prod_last = s->si[0].state;
1625 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001626 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001627 rqf_last = s->req->flags;
1628
1629 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001630 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001631 }
1632
Willy Tarreau576507f2010-01-07 00:09:04 +01001633 /* we'll monitor the request analysers while parsing the response,
1634 * because some response analysers may indirectly enable new request
1635 * analysers (eg: HTTP keep-alive).
1636 */
1637 req_ana_back = s->req->analysers;
1638
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001639 resync_response:
1640 /* Analyse response */
1641
1642 if (unlikely(s->rep->flags & BF_HIJACK)) {
1643 /* In inject mode, we wake up everytime something has
1644 * happened on the write side of the buffer.
1645 */
1646 unsigned int flags = s->rep->flags;
1647
1648 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1649 !(s->rep->flags & BF_FULL)) {
1650 s->rep->hijacker(s, s->rep);
1651 }
1652
1653 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1654 rpf_last = s->rep->flags;
1655 goto resync_response;
1656 }
1657 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001658 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001659 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1660 s->si[0].state != rp_cons_last ||
1661 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001662 unsigned int flags = s->rep->flags;
1663
Willy Tarreau0499e352010-12-17 07:13:42 +01001664 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1665 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1666 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1667 * for some free space in the response buffer, so we might need to call
1668 * it when something changes in the response buffer, but still we pass
1669 * it the request buffer. Note that the SI state might very well still
1670 * be zero due to us returning a flow of redirects!
1671 */
1672 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1673 s->req->flags |= BF_WAKE_ONCE;
1674 }
1675
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001676 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001677 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001678 unsigned int ana_list;
1679 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001680
Willy Tarreau90deb182010-01-07 00:20:41 +01001681 /* it's up to the analysers to stop disable reading or
1682 * closing. Note: if an analyser disables any of these
1683 * bits, it is responsible for enabling them again when
1684 * it disables itself, so that other analysers are called
1685 * in similar conditions.
1686 */
1687 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001688 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001689
1690 /* We will call all analysers for which a bit is set in
1691 * s->rep->analysers, following the bit order from LSB
1692 * to MSB. The analysers must remove themselves from
1693 * the list when not needed. Any analyser may return 0
1694 * to break out of the loop, either because of missing
1695 * data to take a decision, or because it decides to
1696 * kill the session. We loop at least once through each
1697 * analyser, and we may loop again if other analysers
1698 * are added in the middle.
1699 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001700
1701 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001702 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001703 /* Warning! ensure that analysers are always placed in ascending order! */
1704
Emeric Brun97679e72010-09-23 17:56:44 +02001705 if (ana_list & AN_RES_INSPECT) {
1706 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1707 break;
1708 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1709 }
1710
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001712 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1713 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001714 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001715 }
1716
Emeric Brun1d33b292010-01-04 15:47:17 +01001717 if (ana_list & AN_RES_STORE_RULES) {
1718 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1719 break;
1720 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1721 }
1722
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001724 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1725 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001726 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001727 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001728
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001729 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001730 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1731 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001732 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001733 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001734 break;
1735 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001736 }
1737
Willy Tarreau815a9b22010-07-27 17:15:12 +02001738 rp_cons_last = s->si[0].state;
1739 rp_prod_last = s->si[1].state;
1740 rpf_last = s->rep->flags;
1741
1742 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001743 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001744 }
1745
Willy Tarreau576507f2010-01-07 00:09:04 +01001746 /* maybe someone has added some request analysers, so we must check and loop */
1747 if (s->req->analysers & ~req_ana_back)
1748 goto resync_request;
1749
Willy Tarreau0499e352010-12-17 07:13:42 +01001750 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1751 goto resync_request;
1752
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001753 /* FIXME: here we should call protocol handlers which rely on
1754 * both buffers.
1755 */
1756
1757
1758 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001759 * Now we propagate unhandled errors to the session. Normally
1760 * we're just in a data phase here since it means we have not
1761 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001762 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001763 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001764 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001765 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1766 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001767 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001768 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001769 s->be->be_counters.cli_aborts++;
1770 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001771 if (srv)
1772 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001773 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001774 }
1775 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001776 s->be->be_counters.cli_aborts++;
1777 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001778 if (srv)
1779 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001780 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001781 }
1782 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001783 s->be->be_counters.srv_aborts++;
1784 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001785 if (srv)
1786 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001787 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001788 }
1789 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001790 s->be->be_counters.srv_aborts++;
1791 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001792 if (srv)
1793 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001794 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001795 }
Willy Tarreau84455332009-03-15 22:34:05 +01001796 sess_set_term_flags(s);
1797 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001798 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1799 /* Report it if the server got an error or a read timeout expired */
1800 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001801 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001802 s->be->be_counters.srv_aborts++;
1803 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001804 if (srv)
1805 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001806 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001807 }
1808 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001809 s->be->be_counters.srv_aborts++;
1810 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001811 if (srv)
1812 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001813 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001814 }
1815 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001816 s->be->be_counters.cli_aborts++;
1817 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001818 if (srv)
1819 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001820 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001821 }
1822 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001823 s->be->be_counters.cli_aborts++;
1824 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001825 if (srv)
1826 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001827 s->flags |= SN_ERR_CLITO;
1828 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001829 sess_set_term_flags(s);
1830 }
Willy Tarreau84455332009-03-15 22:34:05 +01001831 }
1832
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001833 /*
1834 * Here we take care of forwarding unhandled data. This also includes
1835 * connection establishments and shutdown requests.
1836 */
1837
1838
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001839 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001840 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001841 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1842 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001843 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001844 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001845 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001846 (s->req->prod->state >= SI_ST_EST) &&
1847 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001848 /* This buffer is freewheeling, there's no analyser nor hijacker
1849 * attached to it. If any data are left in, we'll permit them to
1850 * move.
1851 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001852 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001853 buffer_auto_connect(s->req);
1854 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001855 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001856
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001857 /* We'll let data flow between the producer (if still connected)
1858 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001859 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001860 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001861 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001862 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001863
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001864 /* check if it is wise to enable kernel splicing to forward request data */
1865 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1866 s->req->to_forward &&
1867 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001868 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001869 (pipes_used < global.maxpipes) &&
1870 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1871 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1872 (s->req->flags & BF_STREAMER_FAST)))) {
1873 s->req->flags |= BF_KERN_SPLICING;
1874 }
1875
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001876 /* reflect what the L7 analysers have seen last */
1877 rqf_last = s->req->flags;
1878
1879 /*
1880 * Now forward all shutdown requests between both sides of the buffer
1881 */
1882
Willy Tarreau520d95e2009-09-19 21:04:57 +02001883 /* first, let's check if the request buffer needs to shutdown(write), which may
1884 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001885 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001886 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001887 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001888 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001889 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001890
1891 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001892 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 +01001893 s->req->cons->shutw(s->req->cons);
1894
1895 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001896 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1897 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001898 buffer_shutr_now(s->req);
1899
1900 /* shutdown(read) pending */
1901 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1902 s->req->prod->shutr(s->req->prod);
1903
Willy Tarreau520d95e2009-09-19 21:04:57 +02001904 /* it's possible that an upper layer has requested a connection setup or abort.
1905 * There are 2 situations where we decide to establish a new connection :
1906 * - there are data scheduled for emission in the buffer
1907 * - the BF_AUTO_CONNECT flag is set (active connection)
1908 */
1909 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001910 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001911 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001912 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001913 * switch to the connected state, otherwise we perform a connection
1914 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001915 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001916 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001917 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001918 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001919 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001920 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1921 s->req->wex = TICK_ETERNITY;
1922 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001923 }
Willy Tarreau73201222009-08-16 18:27:24 +02001924 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001925 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001926 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001927 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1928 buffer_shutr_now(s->rep);
1929 }
Willy Tarreau92795622009-03-06 12:51:23 +01001930 }
1931
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001932
1933 /* we may have a pending connection request, or a connection waiting
1934 * for completion.
1935 */
1936 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1937 do {
1938 /* nb: step 1 might switch from QUE to ASS, but we first want
1939 * to give a chance to step 2 to perform a redirect if needed.
1940 */
1941 if (s->si[1].state != SI_ST_REQ)
1942 sess_update_stream_int(s, &s->si[1]);
1943 if (s->si[1].state == SI_ST_REQ)
1944 sess_prepare_conn_req(s, &s->si[1]);
1945
Willy Tarreau827aee92011-03-10 16:55:02 +01001946 srv = target_srv(&s->target);
1947 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001948 perform_http_redirect(s, &s->si[1]);
1949 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001950
1951 /* Now we can add the server name to a header (if requested) */
1952 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01001953 if ((s->flags & SN_BE_ASSIGNED) &&
Mark Lamourinec2247f02012-01-04 13:02:01 -05001954 (s->be->mode == PR_MODE_HTTP) &&
1955 (s->be->server_id_hdr_name != NULL)) {
1956
1957 http_send_name_header(&s->txn,
1958 &s->txn.req,
1959 s->req,
1960 s->be,
1961 target_srv(&s->target)->id);
1962 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001963 }
1964
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001965 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001966 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001967 goto resync_stream_interface;
1968
Willy Tarreau815a9b22010-07-27 17:15:12 +02001969 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001971 goto resync_request;
1972
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001973 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001974
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001975 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001976 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001977 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1978 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001979 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001980 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001981 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001982 (s->rep->prod->state >= SI_ST_EST) &&
1983 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001984 /* This buffer is freewheeling, there's no analyser nor hijacker
1985 * attached to it. If any data are left in, we'll permit them to
1986 * move.
1987 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001988 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001989 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001990 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001991
1992 /* We'll let data flow between the producer (if still connected)
1993 * to the consumer.
1994 */
1995 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001996 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001997 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001998
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001999 /* check if it is wise to enable kernel splicing to forward response data */
2000 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
2001 s->rep->to_forward &&
2002 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02002003 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002004 (pipes_used < global.maxpipes) &&
2005 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2006 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
2007 (s->rep->flags & BF_STREAMER_FAST)))) {
2008 s->rep->flags |= BF_KERN_SPLICING;
2009 }
2010
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002011 /* reflect what the L7 analysers have seen last */
2012 rpf_last = s->rep->flags;
2013
2014 /*
2015 * Now forward all shutdown requests between both sides of the buffer
2016 */
2017
2018 /*
2019 * FIXME: this is probably where we should produce error responses.
2020 */
2021
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002022 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002023 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
2024 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002025 buffer_shutw_now(s->rep);
2026
2027 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02002028 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 +01002029 s->rep->cons->shutw(s->rep->cons);
2030
2031 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002032 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
2033 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002034 buffer_shutr_now(s->rep);
2035
2036 /* shutdown(read) pending */
2037 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
2038 s->rep->prod->shutr(s->rep->prod);
2039
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002040 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002041 goto resync_stream_interface;
2042
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002043 if (s->req->flags != rqf_last)
2044 goto resync_request;
2045
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002046 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002047 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002048
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002049 /* we're interested in getting wakeups again */
2050 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2051 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2052
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002053 /* This is needed only when debugging is enabled, to indicate
2054 * client-side or server-side close. Please note that in the unlikely
2055 * event where both sides would close at once, the sequence is reported
2056 * on the server side first.
2057 */
2058 if (unlikely((global.mode & MODE_DEBUG) &&
2059 (!(global.mode & MODE_QUIET) ||
2060 (global.mode & MODE_VERBOSE)))) {
2061 int len;
2062
2063 if (s->si[1].state == SI_ST_CLO &&
2064 s->si[1].prev_state == SI_ST_EST) {
2065 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2066 s->uniq_id, s->be->id,
2067 (unsigned short)s->si[0].fd,
2068 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002069 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002070 }
2071
2072 if (s->si[0].state == SI_ST_CLO &&
2073 s->si[0].prev_state == SI_ST_EST) {
2074 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2075 s->uniq_id, s->be->id,
2076 (unsigned short)s->si[0].fd,
2077 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002078 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079 }
2080 }
2081
2082 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2083 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2084
2085 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2086 session_process_counters(s);
2087
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002088 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002089 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002091 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002092 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002093
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002094 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2095 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 +01002096 s->si[0].prev_state = s->si[0].state;
2097 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002098 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2099 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002100
2101 /* Trick: if a request is being waiting for the server to respond,
2102 * and if we know the server can timeout, we don't want the timeout
2103 * to expire on the client side first, but we're still interested
2104 * in passing data from the client to the server (eg: POST). Thus,
2105 * we can cancel the client's request timeout if the server's
2106 * request timeout is set and the server has not yet sent a response.
2107 */
2108
Willy Tarreau520d95e2009-09-19 21:04:57 +02002109 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002110 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2111 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002113 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002114
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002115 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002116 * Note that this one may wake the task up again.
2117 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002118 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2119 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2120 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2121 s->req->cons->target.ptr.a->fct(s->req->cons);
2122 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2123 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002124 if (task_in_rq(t)) {
2125 /* If we woke up, we don't want to requeue the
2126 * task to the wait queue, but rather requeue
2127 * it into the runqueue ASAP.
2128 */
2129 t->expire = TICK_ETERNITY;
2130 return t;
2131 }
2132 }
2133
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002134 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2135 tick_first(s->rep->rex, s->rep->wex));
2136 if (s->req->analysers)
2137 t->expire = tick_first(t->expire, s->req->analyse_exp);
2138
2139 if (s->si[0].exp)
2140 t->expire = tick_first(t->expire, s->si[0].exp);
2141
2142 if (s->si[1].exp)
2143 t->expire = tick_first(t->expire, s->si[1].exp);
2144
2145#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002146 fprintf(stderr,
2147 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2148 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2149 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2150 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 +01002151#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002152
2153#ifdef DEBUG_DEV
2154 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002155 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156 ABORT_NOW();
2157#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002158 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002159 }
2160
2161 s->fe->feconn--;
2162 if (s->flags & SN_BE_ASSIGNED)
2163 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002164 if (!(s->listener->options & LI_O_UNLIMITED))
2165 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002166 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002167 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002168 if (s->listener->state == LI_FULL)
2169 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002170
Willy Tarreau08ceb102011-07-24 22:58:00 +02002171 /* Dequeues all of the listeners waiting for a resource */
2172 if (!LIST_ISEMPTY(&global_listener_queue))
2173 dequeue_all_listeners(&global_listener_queue);
2174
Willy Tarreaub32907b2011-07-25 08:37:44 +02002175 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2176 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
Willy Tarreau07687c12011-07-24 23:55:06 +02002177 dequeue_all_listeners(&s->fe->listener_queue);
2178
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002179 if (unlikely((global.mode & MODE_DEBUG) &&
2180 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2181 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002182 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002183 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002184 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002185 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002186 }
2187
2188 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2189 session_process_counters(s);
2190
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002191 if (s->txn.status) {
2192 int n;
2193
2194 n = s->txn.status / 100;
2195 if (n < 1 || n > 5)
2196 n = 0;
2197
2198 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002199 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002200
Willy Tarreau24657792010-02-26 10:30:28 +01002201 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002202 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002203 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002204 }
2205
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206 /* let's do a final log if we need it */
2207 if (s->logs.logwait &&
2208 !(s->flags & SN_MONITOR) &&
2209 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002210 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002211 }
2212
2213 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002214 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002215 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002216 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002217 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002218}
2219
Willy Tarreau7c669d72008-06-20 15:04:11 +02002220/*
2221 * This function adjusts sess->srv_conn and maintains the previous and new
2222 * server's served session counts. Setting newsrv to NULL is enough to release
2223 * current connection slot. This function also notifies any LB algo which might
2224 * expect to be informed about any change in the number of active sessions on a
2225 * server.
2226 */
2227void sess_change_server(struct session *sess, struct server *newsrv)
2228{
2229 if (sess->srv_conn == newsrv)
2230 return;
2231
2232 if (sess->srv_conn) {
2233 sess->srv_conn->served--;
2234 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2235 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002236 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002237 }
2238
2239 if (newsrv) {
2240 newsrv->served++;
2241 if (newsrv->proxy->lbprm.server_take_conn)
2242 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002243 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002244 }
2245}
2246
Willy Tarreau84455332009-03-15 22:34:05 +01002247/* Handle server-side errors for default protocols. It is called whenever a a
2248 * connection setup is aborted or a request is aborted in queue. It sets the
2249 * session termination flags so that the caller does not have to worry about
2250 * them. It's installed as ->srv_error for the server-side stream_interface.
2251 */
2252void default_srv_error(struct session *s, struct stream_interface *si)
2253{
2254 int err_type = si->err_type;
2255 int err = 0, fin = 0;
2256
2257 if (err_type & SI_ET_QUEUE_ABRT) {
2258 err = SN_ERR_CLICL;
2259 fin = SN_FINST_Q;
2260 }
2261 else if (err_type & SI_ET_CONN_ABRT) {
2262 err = SN_ERR_CLICL;
2263 fin = SN_FINST_C;
2264 }
2265 else if (err_type & SI_ET_QUEUE_TO) {
2266 err = SN_ERR_SRVTO;
2267 fin = SN_FINST_Q;
2268 }
2269 else if (err_type & SI_ET_QUEUE_ERR) {
2270 err = SN_ERR_SRVCL;
2271 fin = SN_FINST_Q;
2272 }
2273 else if (err_type & SI_ET_CONN_TO) {
2274 err = SN_ERR_SRVTO;
2275 fin = SN_FINST_C;
2276 }
2277 else if (err_type & SI_ET_CONN_ERR) {
2278 err = SN_ERR_SRVCL;
2279 fin = SN_FINST_C;
2280 }
2281 else /* SI_ET_CONN_OTHER and others */ {
2282 err = SN_ERR_INTERNAL;
2283 fin = SN_FINST_C;
2284 }
2285
2286 if (!(s->flags & SN_ERR_MASK))
2287 s->flags |= err;
2288 if (!(s->flags & SN_FINST_MASK))
2289 s->flags |= fin;
2290}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002291
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002292/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2293void session_shutdown(struct session *session, int why)
2294{
2295 if (session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))
2296 return;
2297
2298 buffer_shutw_now(session->req);
2299 buffer_shutr_now(session->rep);
2300 session->task->nice = 1024;
2301 if (!(session->flags & SN_ERR_MASK))
2302 session->flags |= why;
2303 task_wakeup(session->task, TASK_WOKEN_OTHER);
2304}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002305
Willy Tarreau8b22a712010-06-18 17:46:06 +02002306/************************************************************************/
2307/* All supported ACL keywords must be declared here. */
2308/************************************************************************/
2309
Willy Tarreaua5e37562011-12-16 17:06:15 +01002310/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002311static int
2312acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2313{
2314 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002315 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002316 if (ts != NULL) {
2317 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2318 if (!ptr)
2319 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002320 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002321 }
2322 return 1;
2323}
2324
Willy Tarreaua5e37562011-12-16 17:06:15 +01002325/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002326 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002327 */
2328static int
Willy Tarreau56123282010-08-06 19:06:56 +02002329acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2330 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002331{
Willy Tarreau56123282010-08-06 19:06:56 +02002332 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002333 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002334 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002335}
2336
Willy Tarreaua5e37562011-12-16 17:06:15 +01002337/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002338 * backend counters.
2339 */
2340static int
Willy Tarreau56123282010-08-06 19:06:56 +02002341acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2342 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002343{
Willy Tarreau56123282010-08-06 19:06:56 +02002344 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002345 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002346 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002347}
2348
Willy Tarreaua5e37562011-12-16 17:06:15 +01002349/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002350 * address in the table pointed to by expr.
2351 */
2352static int
2353acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2354 struct acl_expr *expr, struct acl_test *test)
2355{
2356 struct stktable_key *key;
2357
David du Colombier4f92d322011-03-24 11:09:31 +01002358 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002359 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002360 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002361
2362 if (expr->arg_len)
2363 px = find_stktable(expr->arg.str);
2364
2365 if (!px)
2366 return 0; /* table not found */
2367
2368 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2369}
2370
2371/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002372 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002373 */
2374static int
2375acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2376{
2377 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002378 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002379 if (ts != NULL) {
2380 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2381 if (!ptr)
2382 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002383 temp_pattern.data.integer = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002384 }
2385 return 1;
2386}
2387
2388/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002389 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002390 */
2391static int
Willy Tarreau56123282010-08-06 19:06:56 +02002392acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2393 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002394{
Willy Tarreau56123282010-08-06 19:06:56 +02002395 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002396 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002397 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002398}
2399
2400/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002401 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002402 */
2403static int
Willy Tarreau56123282010-08-06 19:06:56 +02002404acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2405 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002406{
Willy Tarreau56123282010-08-06 19:06:56 +02002407 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002408 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002409 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002410}
2411
2412/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002413 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002414 */
2415static int
2416acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2417 struct acl_expr *expr, struct acl_test *test)
2418{
2419 struct stktable_key *key;
2420
David du Colombier4f92d322011-03-24 11:09:31 +01002421 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002422 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002423 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002424
2425 if (expr->arg_len)
2426 px = find_stktable(expr->arg.str);
2427
2428 if (!px)
2429 return 0; /* table not found */
2430
2431 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2432}
2433
Willy Tarreauf73cd112011-08-13 01:45:16 +02002434/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002435 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002436 */
2437static int
2438acl_fetch_clr_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2439{
2440 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002441 temp_pattern.data.integer = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002442 if (ts != NULL) {
2443 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2444 if (!ptr)
2445 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002446 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002447 stktable_data_cast(ptr, gpc0) = 0;
2448 }
2449 return 1;
2450}
2451
2452/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002453 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002454 */
2455static int
2456acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2457 struct acl_expr *expr, struct acl_test *test)
2458{
2459 if (!l4->stkctr1_entry)
2460 return 0;
2461 return acl_fetch_clr_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
2462}
2463
2464/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002465 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002466 */
2467static int
2468acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2469 struct acl_expr *expr, struct acl_test *test)
2470{
2471 if (!l4->stkctr2_entry)
2472 return 0;
2473 return acl_fetch_clr_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
2474}
2475
2476/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002477 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002478 */
2479static int
2480acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2481 struct acl_expr *expr, struct acl_test *test)
2482{
2483 struct stktable_key *key;
2484
2485 key = tcp_src_to_stktable_key(l4);
2486 if (!key)
2487 return 0;
2488
2489 if (expr->arg_len)
2490 px = find_stktable(expr->arg.str);
2491
2492 if (!px)
2493 return 0; /* table not found */
2494
2495 return acl_fetch_clr_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2496}
2497
Willy Tarreaua5e37562011-12-16 17:06:15 +01002498/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002499static int
2500acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2501{
2502 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002503 temp_pattern.data.integer = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002504 if (ts != NULL) {
2505 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2506 if (!ptr)
2507 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002508 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002509 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002510 return 1;
2511}
2512
Willy Tarreaua5e37562011-12-16 17:06:15 +01002513/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002514static int
Willy Tarreau56123282010-08-06 19:06:56 +02002515acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2516 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002517{
Willy Tarreau56123282010-08-06 19:06:56 +02002518 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002519 return 0;
2520
Willy Tarreau56123282010-08-06 19:06:56 +02002521 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002522}
2523
Willy Tarreaua5e37562011-12-16 17:06:15 +01002524/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002525static int
Willy Tarreau56123282010-08-06 19:06:56 +02002526acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2527 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002528{
Willy Tarreau56123282010-08-06 19:06:56 +02002529 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002530 return 0;
2531
Willy Tarreau56123282010-08-06 19:06:56 +02002532 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002533}
2534
Willy Tarreaua5e37562011-12-16 17:06:15 +01002535/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002536 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002537 */
2538static int
2539acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2540 struct acl_expr *expr, struct acl_test *test)
2541{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002542 struct stktable_key *key;
2543
David du Colombier4f92d322011-03-24 11:09:31 +01002544 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002545 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002546 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002547
2548 if (expr->arg_len)
2549 px = find_stktable(expr->arg.str);
2550
2551 if (!px)
2552 return 0; /* table not found */
2553
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002554 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002555}
2556
Willy Tarreaua5e37562011-12-16 17:06:15 +01002557/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002558static int
2559acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2560{
2561 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002562 temp_pattern.data.integer = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002563 if (ts != NULL) {
2564 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2565 if (!ptr)
2566 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002567 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002568 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2569 }
2570 return 1;
2571}
2572
Willy Tarreaua5e37562011-12-16 17:06:15 +01002573/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002574 * the configured period.
2575 */
2576static int
Willy Tarreau56123282010-08-06 19:06:56 +02002577acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2578 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002579{
Willy Tarreau56123282010-08-06 19:06:56 +02002580 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002581 return 0;
2582
Willy Tarreau56123282010-08-06 19:06:56 +02002583 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002584}
2585
Willy Tarreaua5e37562011-12-16 17:06:15 +01002586/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002587 * the configured period.
2588 */
2589static int
Willy Tarreau56123282010-08-06 19:06:56 +02002590acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2591 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002592{
Willy Tarreau56123282010-08-06 19:06:56 +02002593 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002594 return 0;
2595
Willy Tarreau56123282010-08-06 19:06:56 +02002596 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002597}
2598
Willy Tarreaua5e37562011-12-16 17:06:15 +01002599/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002600 * table pointed to by expr, over the configured period.
2601 */
2602static int
2603acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2604 struct acl_expr *expr, struct acl_test *test)
2605{
2606 struct stktable_key *key;
2607
David du Colombier4f92d322011-03-24 11:09:31 +01002608 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002609 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002610 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002611
2612 if (expr->arg_len)
2613 px = find_stktable(expr->arg.str);
2614
2615 if (!px)
2616 return 0; /* table not found */
2617
2618 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2619}
2620
Willy Tarreaua5e37562011-12-16 17:06:15 +01002621/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002622 * in the table pointed to by expr, after updating it.
2623 */
2624static int
2625acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2626 struct acl_expr *expr, struct acl_test *test)
2627{
2628 struct stksess *ts;
2629 struct stktable_key *key;
2630 void *ptr;
2631
David du Colombier4f92d322011-03-24 11:09:31 +01002632 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002633 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002634 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002635
2636 if (expr->arg_len)
2637 px = find_stktable(expr->arg.str);
2638
2639 if (!px)
2640 return 0; /* table not found */
2641
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002642 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2643 /* entry does not exist and could not be created */
2644 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002645
2646 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2647 if (!ptr)
2648 return 0; /* parameter not stored in this table */
2649
Willy Tarreaua5e37562011-12-16 17:06:15 +01002650 temp_pattern.data.integer = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002651 test->flags = ACL_TEST_F_VOL_TEST;
2652 return 1;
2653}
2654
Willy Tarreaua5e37562011-12-16 17:06:15 +01002655/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002656static int
2657acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2658{
2659 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002660 temp_pattern.data.integer = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002661
2662 if (ts != NULL) {
2663 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2664 if (!ptr)
2665 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002666 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002667 }
2668 return 1;
2669}
2670
Willy Tarreaua5e37562011-12-16 17:06:15 +01002671/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002672static int
Willy Tarreau56123282010-08-06 19:06:56 +02002673acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2674 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002675{
Willy Tarreau56123282010-08-06 19:06:56 +02002676 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002677 return 0;
2678
Willy Tarreau56123282010-08-06 19:06:56 +02002679 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002680}
2681
Willy Tarreaua5e37562011-12-16 17:06:15 +01002682/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002683static int
Willy Tarreau56123282010-08-06 19:06:56 +02002684acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2685 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002686{
Willy Tarreau56123282010-08-06 19:06:56 +02002687 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002688 return 0;
2689
Willy Tarreau56123282010-08-06 19:06:56 +02002690 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002691}
2692
Willy Tarreaua5e37562011-12-16 17:06:15 +01002693/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002694 * address in the table pointed to by expr.
2695 */
2696static int
2697acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2698 struct acl_expr *expr, struct acl_test *test)
2699{
Willy Tarreau38285c12010-06-18 16:35:43 +02002700 struct stktable_key *key;
2701
David du Colombier4f92d322011-03-24 11:09:31 +01002702 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002703 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002704 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002705
2706 if (expr->arg_len)
2707 px = find_stktable(expr->arg.str);
2708
2709 if (!px)
2710 return 0; /* table not found */
2711
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002712 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002713}
2714
Willy Tarreaua5e37562011-12-16 17:06:15 +01002715/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002716static int
2717acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2718{
2719 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002720 temp_pattern.data.integer = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002721 if (ts != NULL) {
2722 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2723 if (!ptr)
2724 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002725 temp_pattern.data.integer = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002726 }
2727 return 1;
2728}
2729
Willy Tarreaua5e37562011-12-16 17:06:15 +01002730/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002731static int
Willy Tarreau56123282010-08-06 19:06:56 +02002732acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2733 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002734{
Willy Tarreau56123282010-08-06 19:06:56 +02002735 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002736 return 0;
2737
Willy Tarreau56123282010-08-06 19:06:56 +02002738 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002739}
2740
Willy Tarreaua5e37562011-12-16 17:06:15 +01002741/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002742static int
Willy Tarreau56123282010-08-06 19:06:56 +02002743acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2744 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002745{
Willy Tarreau56123282010-08-06 19:06:56 +02002746 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002747 return 0;
2748
Willy Tarreau56123282010-08-06 19:06:56 +02002749 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002750}
2751
Willy Tarreaua5e37562011-12-16 17:06:15 +01002752/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002753 * address in the table pointed to by expr.
2754 */
2755static int
2756acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2757 struct acl_expr *expr, struct acl_test *test)
2758{
2759 struct stktable_key *key;
2760
David du Colombier4f92d322011-03-24 11:09:31 +01002761 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002762 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002763 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002764
2765 if (expr->arg_len)
2766 px = find_stktable(expr->arg.str);
2767
2768 if (!px)
2769 return 0; /* table not found */
2770
2771 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2772}
2773
Willy Tarreaua5e37562011-12-16 17:06:15 +01002774/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002775static int
2776acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2777{
2778 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002779 temp_pattern.data.integer = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002780 if (ts != NULL) {
2781 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2782 if (!ptr)
2783 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002784 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002785 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2786 }
2787 return 1;
2788}
2789
Willy Tarreaua5e37562011-12-16 17:06:15 +01002790/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002791 * the configured period.
2792 */
2793static int
Willy Tarreau56123282010-08-06 19:06:56 +02002794acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2795 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002796{
Willy Tarreau56123282010-08-06 19:06:56 +02002797 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002798 return 0;
2799
Willy Tarreau56123282010-08-06 19:06:56 +02002800 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002801}
2802
Willy Tarreaua5e37562011-12-16 17:06:15 +01002803/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002804 * the configured period.
2805 */
2806static int
Willy Tarreau56123282010-08-06 19:06:56 +02002807acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2808 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002809{
Willy Tarreau56123282010-08-06 19:06:56 +02002810 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002811 return 0;
2812
Willy Tarreau56123282010-08-06 19:06:56 +02002813 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002814}
2815
Willy Tarreaua5e37562011-12-16 17:06:15 +01002816/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002817 * table pointed to by expr, over the configured period.
2818 */
2819static int
2820acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2821 struct acl_expr *expr, struct acl_test *test)
2822{
2823 struct stktable_key *key;
2824
David du Colombier4f92d322011-03-24 11:09:31 +01002825 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002826 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002827 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002828
2829 if (expr->arg_len)
2830 px = find_stktable(expr->arg.str);
2831
2832 if (!px)
2833 return 0; /* table not found */
2834
2835 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2836}
2837
Willy Tarreaua5e37562011-12-16 17:06:15 +01002838/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002839static int
2840acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2841{
2842 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002843 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002844 if (ts != NULL) {
2845 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2846 if (!ptr)
2847 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002848 temp_pattern.data.integer = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002849 }
2850 return 1;
2851}
2852
Willy Tarreaua5e37562011-12-16 17:06:15 +01002853/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002854static int
Willy Tarreau56123282010-08-06 19:06:56 +02002855acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2856 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002857{
Willy Tarreau56123282010-08-06 19:06:56 +02002858 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002859 return 0;
2860
Willy Tarreau56123282010-08-06 19:06:56 +02002861 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002862}
2863
Willy Tarreaua5e37562011-12-16 17:06:15 +01002864/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002865static int
Willy Tarreau56123282010-08-06 19:06:56 +02002866acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2867 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002868{
Willy Tarreau56123282010-08-06 19:06:56 +02002869 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002870 return 0;
2871
Willy Tarreau56123282010-08-06 19:06:56 +02002872 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002873}
2874
Willy Tarreaua5e37562011-12-16 17:06:15 +01002875/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002876 * address in the table pointed to by expr.
2877 */
2878static int
2879acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002880 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002881{
2882 struct stktable_key *key;
2883
David du Colombier4f92d322011-03-24 11:09:31 +01002884 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002885 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002886 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002887
2888 if (expr->arg_len)
2889 px = find_stktable(expr->arg.str);
2890
2891 if (!px)
2892 return 0; /* table not found */
2893
2894 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2895}
2896
Willy Tarreaua5e37562011-12-16 17:06:15 +01002897/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002898static int
2899acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2900{
2901 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002902 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002903 if (ts != NULL) {
2904 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2905 if (!ptr)
2906 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002907 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002908 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2909 }
2910 return 1;
2911}
2912
Willy Tarreaua5e37562011-12-16 17:06:15 +01002913/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002914 * the configured period.
2915 */
2916static int
Willy Tarreau56123282010-08-06 19:06:56 +02002917acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2918 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002919{
Willy Tarreau56123282010-08-06 19:06:56 +02002920 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002921 return 0;
2922
Willy Tarreau56123282010-08-06 19:06:56 +02002923 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002924}
2925
Willy Tarreaua5e37562011-12-16 17:06:15 +01002926/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002927 * the configured period.
2928 */
2929static int
Willy Tarreau56123282010-08-06 19:06:56 +02002930acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2931 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002932{
Willy Tarreau56123282010-08-06 19:06:56 +02002933 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002934 return 0;
2935
Willy Tarreau56123282010-08-06 19:06:56 +02002936 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937}
2938
Willy Tarreaua5e37562011-12-16 17:06:15 +01002939/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02002940 * table pointed to by expr, over the configured period.
2941 */
2942static int
2943acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002944 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002945{
2946 struct stktable_key *key;
2947
David du Colombier4f92d322011-03-24 11:09:31 +01002948 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002949 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002950 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002951
2952 if (expr->arg_len)
2953 px = find_stktable(expr->arg.str);
2954
2955 if (!px)
2956 return 0; /* table not found */
2957
2958 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2959}
2960
Willy Tarreaua5e37562011-12-16 17:06:15 +01002961/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962static int
2963acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2964{
2965 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002966 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002967 if (ts != NULL) {
2968 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2969 if (!ptr)
2970 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002971 temp_pattern.data.integer = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002972 }
2973 return 1;
2974}
2975
Willy Tarreaua5e37562011-12-16 17:06:15 +01002976/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002977static int
Willy Tarreau56123282010-08-06 19:06:56 +02002978acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2979 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002980{
Willy Tarreau56123282010-08-06 19:06:56 +02002981 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002982 return 0;
2983
Willy Tarreau56123282010-08-06 19:06:56 +02002984 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002985}
2986
Willy Tarreaua5e37562011-12-16 17:06:15 +01002987/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002988static int
Willy Tarreau56123282010-08-06 19:06:56 +02002989acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2990 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002991{
Willy Tarreau56123282010-08-06 19:06:56 +02002992 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993 return 0;
2994
Willy Tarreau56123282010-08-06 19:06:56 +02002995 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002996}
2997
Willy Tarreaua5e37562011-12-16 17:06:15 +01002998/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002999 * address in the table pointed to by expr.
3000 */
3001static int
3002acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003003 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003004{
3005 struct stktable_key *key;
3006
David du Colombier4f92d322011-03-24 11:09:31 +01003007 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003008 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003009 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010
3011 if (expr->arg_len)
3012 px = find_stktable(expr->arg.str);
3013
3014 if (!px)
3015 return 0; /* table not found */
3016
3017 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
3018}
3019
Willy Tarreaua5e37562011-12-16 17:06:15 +01003020/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003021static int
3022acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3023{
3024 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003025 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003026 if (ts != NULL) {
3027 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3028 if (!ptr)
3029 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003030 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003031 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3032 }
3033 return 1;
3034}
3035
Willy Tarreaua5e37562011-12-16 17:06:15 +01003036/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003037 * the configured period.
3038 */
3039static int
Willy Tarreau56123282010-08-06 19:06:56 +02003040acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3041 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003042{
Willy Tarreau56123282010-08-06 19:06:56 +02003043 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003044 return 0;
3045
Willy Tarreau56123282010-08-06 19:06:56 +02003046 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003047}
3048
Willy Tarreaua5e37562011-12-16 17:06:15 +01003049/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003050 * the configured period.
3051 */
3052static int
Willy Tarreau56123282010-08-06 19:06:56 +02003053acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3054 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003055{
Willy Tarreau56123282010-08-06 19:06:56 +02003056 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003057 return 0;
3058
Willy Tarreau56123282010-08-06 19:06:56 +02003059 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060}
3061
Willy Tarreaua5e37562011-12-16 17:06:15 +01003062/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003063 * table pointed to by expr, over the configured period.
3064 */
3065static int
3066acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3067 struct acl_expr *expr, struct acl_test *test)
3068{
3069 struct stktable_key *key;
3070
David du Colombier4f92d322011-03-24 11:09:31 +01003071 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003072 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003073 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003074
3075 if (expr->arg_len)
3076 px = find_stktable(expr->arg.str);
3077
3078 if (!px)
3079 return 0; /* table not found */
3080
3081 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3082}
3083
Willy Tarreaua5e37562011-12-16 17:06:15 +01003084/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003085static int
3086acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
3087{
3088 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003089 temp_pattern.data.integer = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003090
3091 if (ts != NULL) {
3092 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3093 if (!ptr)
3094 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003095 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003096 }
3097 return 1;
3098}
3099
Willy Tarreaua5e37562011-12-16 17:06:15 +01003100/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003101 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003102 */
3103static int
Willy Tarreau56123282010-08-06 19:06:56 +02003104acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3105 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003106{
Willy Tarreau56123282010-08-06 19:06:56 +02003107 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003108 return 0;
3109
Willy Tarreau56123282010-08-06 19:06:56 +02003110 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003111}
3112
Willy Tarreaua5e37562011-12-16 17:06:15 +01003113/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003114 * session's tracked BE counters.
3115 */
3116static int
Willy Tarreau56123282010-08-06 19:06:56 +02003117acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3118 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003119{
Willy Tarreau56123282010-08-06 19:06:56 +02003120 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003121 return 0;
3122
Willy Tarreau56123282010-08-06 19:06:56 +02003123 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003124}
3125
Willy Tarreaua5e37562011-12-16 17:06:15 +01003126/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003127 * address in the table pointed to by expr.
3128 */
3129static int
3130acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3131 struct acl_expr *expr, struct acl_test *test)
3132{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003133 struct stktable_key *key;
3134
David du Colombier4f92d322011-03-24 11:09:31 +01003135 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003136 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003137 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003138
3139 if (expr->arg_len)
3140 px = find_stktable(expr->arg.str);
3141
3142 if (!px)
3143 return 0; /* table not found */
3144
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003145 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
3146}
3147
Willy Tarreaua5e37562011-12-16 17:06:15 +01003148/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003149 * configured period.
3150 */
3151static int
3152acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3153{
3154 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003155 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003156 if (ts != NULL) {
3157 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3158 if (!ptr)
3159 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003160 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003161 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3162 }
3163 return 1;
3164}
3165
Willy Tarreaua5e37562011-12-16 17:06:15 +01003166/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003167 * counters over the configured period.
3168 */
3169static int
Willy Tarreau56123282010-08-06 19:06:56 +02003170acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3171 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003172{
Willy Tarreau56123282010-08-06 19:06:56 +02003173 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003174 return 0;
3175
Willy Tarreau56123282010-08-06 19:06:56 +02003176 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003177}
3178
Willy Tarreaua5e37562011-12-16 17:06:15 +01003179/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003180 * counters over the configured period.
3181 */
3182static int
Willy Tarreau56123282010-08-06 19:06:56 +02003183acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3184 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003185{
Willy Tarreau56123282010-08-06 19:06:56 +02003186 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003187 return 0;
3188
Willy Tarreau56123282010-08-06 19:06:56 +02003189 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003190}
3191
Willy Tarreaua5e37562011-12-16 17:06:15 +01003192/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003193 * in the table pointed to by expr, over the configured period.
3194 */
3195static int
3196acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003197 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003198{
3199 struct stktable_key *key;
3200
David du Colombier4f92d322011-03-24 11:09:31 +01003201 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003202 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003203 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003204
3205 if (expr->arg_len)
3206 px = find_stktable(expr->arg.str);
3207
3208 if (!px)
3209 return 0; /* table not found */
3210
3211 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3212}
3213
Willy Tarreaua5e37562011-12-16 17:06:15 +01003214/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003215static int
3216acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3217{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003218 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003219 temp_pattern.data.integer = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003220
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003221 if (ts != NULL) {
3222 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223 if (!ptr)
3224 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003225 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003226 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003227 return 1;
3228}
3229
Willy Tarreaua5e37562011-12-16 17:06:15 +01003230/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003231 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003232 */
3233static int
Willy Tarreau56123282010-08-06 19:06:56 +02003234acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3235 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003236{
Willy Tarreau56123282010-08-06 19:06:56 +02003237 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003238 return 0;
3239
Willy Tarreau56123282010-08-06 19:06:56 +02003240 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003241}
3242
Willy Tarreaua5e37562011-12-16 17:06:15 +01003243/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003244 * tracked BE counters.
3245 */
3246static int
Willy Tarreau56123282010-08-06 19:06:56 +02003247acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3248 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003249{
Willy Tarreau56123282010-08-06 19:06:56 +02003250 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003251 return 0;
3252
Willy Tarreau56123282010-08-06 19:06:56 +02003253 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003254}
3255
Willy Tarreaua5e37562011-12-16 17:06:15 +01003256/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003257 * the table pointed to by expr.
3258 */
3259static int
3260acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3261 struct acl_expr *expr, struct acl_test *test)
3262{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003263 struct stktable_key *key;
3264
David du Colombier4f92d322011-03-24 11:09:31 +01003265 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003266 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003267 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003268
3269 if (expr->arg_len)
3270 px = find_stktable(expr->arg.str);
3271
3272 if (!px)
3273 return 0; /* table not found */
3274
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003275 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003276}
3277
Willy Tarreaua5e37562011-12-16 17:06:15 +01003278/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003279 * configured period.
3280 */
3281static int
3282acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3283{
3284 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003285 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003286 if (ts != NULL) {
3287 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3288 if (!ptr)
3289 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003290 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003291 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3292 }
3293 return 1;
3294}
3295
Willy Tarreaua5e37562011-12-16 17:06:15 +01003296/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003297 * over the configured period.
3298 */
3299static int
Willy Tarreau56123282010-08-06 19:06:56 +02003300acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3301 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003302{
Willy Tarreau56123282010-08-06 19:06:56 +02003303 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003304 return 0;
3305
Willy Tarreau56123282010-08-06 19:06:56 +02003306 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003307}
3308
Willy Tarreaua5e37562011-12-16 17:06:15 +01003309/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003310 * over the configured period.
3311 */
3312static int
Willy Tarreau56123282010-08-06 19:06:56 +02003313acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3314 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003315{
Willy Tarreau56123282010-08-06 19:06:56 +02003316 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003317 return 0;
3318
Willy Tarreau56123282010-08-06 19:06:56 +02003319 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003320}
3321
Willy Tarreaua5e37562011-12-16 17:06:15 +01003322/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003323 * the table pointed to by expr, over the configured period.
3324 */
3325static int
3326acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003327 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003328{
3329 struct stktable_key *key;
3330
David du Colombier4f92d322011-03-24 11:09:31 +01003331 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003332 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003333 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003334
3335 if (expr->arg_len)
3336 px = find_stktable(expr->arg.str);
3337
3338 if (!px)
3339 return 0; /* table not found */
3340
3341 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3342}
3343
Willy Tarreaua5e37562011-12-16 17:06:15 +01003344/* set temp integer to the number of used entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003345static int
3346acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3347 struct acl_expr *expr, struct acl_test *test)
3348{
3349 if (expr->arg_len)
3350 px = find_stktable(expr->arg.str);
3351
3352 if (!px)
3353 return 0; /* table not found */
3354
3355 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003356 temp_pattern.data.integer = px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003357 return 1;
3358}
3359
Willy Tarreaua5e37562011-12-16 17:06:15 +01003360/* set temp integer to the number of free entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003361static int
3362acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3363 struct acl_expr *expr, struct acl_test *test)
3364{
3365 if (expr->arg_len)
3366 px = find_stktable(expr->arg.str);
3367
3368 if (!px)
3369 return 0; /* table not found */
3370
3371 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003372 temp_pattern.data.integer = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003373 return 1;
3374}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003375
3376/* Note: must not be declared <const> as its list will be overwritten */
3377static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003378 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3379 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3380 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3381 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3382 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3383 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreauf73cd112011-08-13 01:45:16 +02003384 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3385 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3386 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau56123282010-08-06 19:06:56 +02003387 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3388 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3389 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3390 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3391 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3392 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3393 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3394 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3395 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3396 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3397 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3398 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3399 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3400 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3401 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3402 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3403 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3404 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3405 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3406 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3407 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3408 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3409 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3410 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3411 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3412 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3413 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3414 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3415 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3416 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3417 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3418 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3419 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3420 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3421 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3422 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3423 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3424 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3425 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3426 { "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 +02003427 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3428 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003429 { NULL, NULL, NULL, NULL },
3430}};
3431
3432
Willy Tarreau56123282010-08-06 19:06:56 +02003433/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003434 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3435 * <prm> struct is fed with the table name if any. If unspecified, the caller
3436 * will assume that the current proxy's table is used.
3437 */
3438int parse_track_counters(char **args, int *arg,
3439 int section_type, struct proxy *curpx,
3440 struct track_ctr_prm *prm,
3441 struct proxy *defpx, char *err, int errlen)
3442{
3443 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003444 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003445
Willy Tarreau56123282010-08-06 19:06:56 +02003446 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003447 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003448 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003449 */
3450 while (args[*arg]) {
3451 if (strcmp(args[*arg], "src") == 0) {
3452 prm->type = STKTABLE_TYPE_IP;
3453 pattern_type = 1;
3454 }
3455 else if (strcmp(args[*arg], "table") == 0) {
3456 if (!args[*arg + 1]) {
3457 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003458 "missing table for %s in %s '%s'.",
3459 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003460 return -1;
3461 }
3462 /* we copy the table name for now, it will be resolved later */
3463 prm->table.n = strdup(args[*arg + 1]);
3464 (*arg)++;
3465 }
3466 else {
3467 /* unhandled keywords are handled by the caller */
3468 break;
3469 }
3470 (*arg)++;
3471 }
3472
3473 if (!pattern_type) {
3474 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003475 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3476 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003477 return -1;
3478 }
3479
3480 return 0;
3481}
3482
Willy Tarreau8b22a712010-06-18 17:46:06 +02003483__attribute__((constructor))
3484static void __session_init(void)
3485{
3486 acl_register_keywords(&acl_kws);
3487}
3488
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489/*
3490 * Local variables:
3491 * c-indent-level: 8
3492 * c-basic-offset: 8
3493 * End:
3494 */