blob: 017ac2672c2d5365c3382733a01ac26c25088878 [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;
233
234 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
235 goto out_free_task; /* no memory */
236
237 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
238 goto out_free_req; /* no memory */
239
240 /* initialize the request buffer */
241 s->req->size = global.tune.bufsize;
242 buffer_init(s->req);
243 s->req->prod = &s->si[0];
244 s->req->cons = &s->si[1];
245 s->si[0].ib = s->si[1].ob = s->req;
246 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
247
248 /* activate default analysers enabled for this listener */
249 s->req->analysers = l->analysers;
250
251 s->req->wto = TICK_ETERNITY;
252 s->req->rto = TICK_ETERNITY;
253 s->req->rex = TICK_ETERNITY;
254 s->req->wex = TICK_ETERNITY;
255 s->req->analyse_exp = TICK_ETERNITY;
256
257 /* initialize response buffer */
258 s->rep->size = global.tune.bufsize;
259 buffer_init(s->rep);
260 s->rep->prod = &s->si[1];
261 s->rep->cons = &s->si[0];
262 s->si[0].ob = s->si[1].ib = s->rep;
263 s->rep->analysers = 0;
264
Willy Tarreau96e31212011-05-30 18:10:30 +0200265 if (s->fe->options2 & PR_O2_NODELAY) {
266 s->req->flags |= BF_NEVER_WAIT;
267 s->rep->flags |= BF_NEVER_WAIT;
268 }
269
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200270 s->rep->rto = TICK_ETERNITY;
271 s->rep->wto = TICK_ETERNITY;
272 s->rep->rex = TICK_ETERNITY;
273 s->rep->wex = TICK_ETERNITY;
274 s->rep->analyse_exp = TICK_ETERNITY;
275
276 /* finish initialization of the accepted file descriptor */
277 fd_insert(cfd);
278 fdtab[cfd].owner = &s->si[0];
279 fdtab[cfd].state = FD_STREADY;
280 fdtab[cfd].flags = 0;
281 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
282 fdtab[cfd].cb[DIR_RD].b = s->req;
283 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
284 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200285 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.from;
286 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200287 EV_FD_SET(cfd, DIR_RD);
288
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100289 if (p->accept && (ret = p->accept(s)) <= 0) {
290 /* Either we had an unrecoverable error (<0) or work is
291 * finished (=0, eg: monitoring), in both situations,
292 * we can release everything and close.
293 */
294 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200295 }
296
297 /* it is important not to call the wakeup function directly but to
298 * pass through task_wakeup(), because this one knows how to apply
299 * priorities to tasks.
300 */
301 task_wakeup(t, TASK_WOKEN_INIT);
302 return 1;
303
304 /* Error unrolling */
305 out_free_rep:
306 pool_free2(pool2_buffer, s->rep);
307 out_free_req:
308 pool_free2(pool2_buffer, s->req);
309 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200310 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200311 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200312 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200313 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200314 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100315 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200316 pool_free2(pool2_session, s);
317 out_close:
Willy Tarreau2b154922011-07-22 17:36:27 +0200318 if (ret < 0 && s->fe->mode == PR_MODE_HTTP) {
319 /* critical error, no more memory, try to emit a 500 response */
320 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
321 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
322 }
323
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100324 if (fdtab[cfd].state != FD_STCLOSE)
325 fd_delete(cfd);
326 else
327 close(cfd);
328 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200329}
330
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331/*
332 * frees the context associated to a session. It must have been removed first.
333 */
Simon Hormandec5be42011-06-08 09:19:07 +0900334static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100336 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200337 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100338 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200339 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100340
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 if (s->pend_pos)
342 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100343
Willy Tarreau827aee92011-03-10 16:55:02 +0100344 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100345 if (s->flags & SN_CURR_SESS) {
346 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100347 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100348 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100349 if (may_dequeue_tasks(target_srv(&s->target), s->be))
350 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100351 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100352
Willy Tarreau7c669d72008-06-20 15:04:11 +0200353 if (unlikely(s->srv_conn)) {
354 /* the session still has a reserved slot on a server, but
355 * it should normally be only the same as the one above,
356 * so this should not happen in fact.
357 */
358 sess_change_server(s, NULL);
359 }
360
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100361 if (s->req->pipe)
362 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100363
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100364 if (s->rep->pipe)
365 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100366
Willy Tarreau48d63db2008-08-03 17:41:33 +0200367 pool_free2(pool2_buffer, s->req);
368 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369
Willy Tarreau46023632010-01-07 22:51:47 +0100370 http_end_txn(s);
371
Willy Tarreaua4cda672010-06-06 18:28:49 +0200372 for (i = 0; i < s->store_count; i++) {
373 if (!s->store[i].ts)
374 continue;
375 stksess_free(s->store[i].table, s->store[i].ts);
376 s->store[i].ts = NULL;
377 }
378
Willy Tarreau34eb6712011-10-24 18:15:04 +0200379 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200380 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100381 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
382 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100384
Willy Tarreau56123282010-08-06 19:06:56 +0200385 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200386 session_store_counters(s);
387
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100388 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100389 /* we have to unlink all watchers. We must not relink them if
390 * this session was the last one in the list.
391 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100392 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100393 LIST_INIT(&bref->users);
394 if (s->list.n != &sessions)
395 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100396 bref->ref = s->list.n;
397 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100398 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200399 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200400
401 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200402 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200403 pool_flush2(pool2_buffer);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200404 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200405 pool_flush2(pool2_requri);
406 pool_flush2(pool2_capture);
407 pool_flush2(pool2_session);
408 pool_flush2(fe->req_cap_pool);
409 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200410 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200411}
412
413
414/* perform minimal intializations, report 0 in case of error, 1 if OK. */
415int init_session()
416{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100417 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200418 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
419 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420}
421
Willy Tarreau30e71012007-11-26 20:15:35 +0100422void session_process_counters(struct session *s)
423{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100424 unsigned long long bytes;
425
Willy Tarreau30e71012007-11-26 20:15:35 +0100426 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100427 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100428 s->logs.bytes_in = s->req->total;
429 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100430 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100431
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100432 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100433
Willy Tarreau827aee92011-03-10 16:55:02 +0100434 if (target_srv(&s->target))
435 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200436
437 if (s->listener->counters)
438 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200439
Willy Tarreau56123282010-08-06 19:06:56 +0200440 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200441 void *ptr;
442
Willy Tarreau56123282010-08-06 19:06:56 +0200443 ptr = stktable_data_ptr(s->stkctr2_table,
444 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200445 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200446 if (ptr)
447 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200448
Willy Tarreau56123282010-08-06 19:06:56 +0200449 ptr = stktable_data_ptr(s->stkctr2_table,
450 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200451 STKTABLE_DT_BYTES_IN_RATE);
452 if (ptr)
453 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200454 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200455 }
456
Willy Tarreau56123282010-08-06 19:06:56 +0200457 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200458 void *ptr;
459
Willy Tarreau56123282010-08-06 19:06:56 +0200460 ptr = stktable_data_ptr(s->stkctr1_table,
461 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200462 STKTABLE_DT_BYTES_IN_CNT);
463 if (ptr)
464 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
465
Willy Tarreau56123282010-08-06 19:06:56 +0200466 ptr = stktable_data_ptr(s->stkctr1_table,
467 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200468 STKTABLE_DT_BYTES_IN_RATE);
469 if (ptr)
470 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200471 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200472 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100473 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100474 }
475
Willy Tarreau30e71012007-11-26 20:15:35 +0100476 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100478 s->logs.bytes_out = s->rep->total;
479 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100480 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100481
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100482 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100483
Willy Tarreau827aee92011-03-10 16:55:02 +0100484 if (target_srv(&s->target))
485 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200486
487 if (s->listener->counters)
488 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200489
Willy Tarreau56123282010-08-06 19:06:56 +0200490 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200491 void *ptr;
492
Willy Tarreau56123282010-08-06 19:06:56 +0200493 ptr = stktable_data_ptr(s->stkctr2_table,
494 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200495 STKTABLE_DT_BYTES_OUT_CNT);
496 if (ptr)
497 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
498
Willy Tarreau56123282010-08-06 19:06:56 +0200499 ptr = stktable_data_ptr(s->stkctr2_table,
500 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200501 STKTABLE_DT_BYTES_OUT_RATE);
502 if (ptr)
503 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200504 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200505 }
506
Willy Tarreau56123282010-08-06 19:06:56 +0200507 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200508 void *ptr;
509
Willy Tarreau56123282010-08-06 19:06:56 +0200510 ptr = stktable_data_ptr(s->stkctr1_table,
511 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200512 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200513 if (ptr)
514 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515
Willy Tarreau56123282010-08-06 19:06:56 +0200516 ptr = stktable_data_ptr(s->stkctr1_table,
517 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200518 STKTABLE_DT_BYTES_OUT_RATE);
519 if (ptr)
520 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200521 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200522 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100523 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100524 }
525}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100527/* This function is called with (si->state == SI_ST_CON) meaning that a
528 * connection was attempted and that the file descriptor is already allocated.
529 * We must check for establishment, error and abort. Possible output states
530 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
531 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
532 * otherwise 1.
533 */
Simon Hormandec5be42011-06-08 09:19:07 +0900534static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535{
536 struct buffer *req = si->ob;
537 struct buffer *rep = si->ib;
538
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100539 /* If we got an error, or if nothing happened and the connection timed
540 * out, we must give up. The CER state handler will take care of retry
541 * attempts and error reports.
542 */
543 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100544 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100545 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200546 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 fd_delete(si->fd);
548
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200549 if (si->release)
550 si->release(si);
551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 if (si->err_type)
553 return 0;
554
Willy Tarreau827aee92011-03-10 16:55:02 +0100555 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100556 if (si->flags & SI_FL_ERR)
557 si->err_type = SI_ET_CONN_ERR;
558 else
559 si->err_type = SI_ET_CONN_TO;
560 return 0;
561 }
562
563 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200564 if (unlikely((rep->flags & BF_SHUTW) ||
565 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200566 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100567 s->be->options & PR_O_ABRT_CLOSE)))) {
568 /* give up */
569 si->shutw(si);
570 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100571 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200572 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100573 if (s->srv_error)
574 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 return 1;
576 }
577
578 /* we need to wait a bit more if there was no activity either */
579 if (!(req->flags & BF_WRITE_ACTIVITY))
580 return 1;
581
582 /* OK, this means that a connection succeeded. The caller will be
583 * responsible for handling the transition from CON to EST.
584 */
585 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100586 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 si->state = SI_ST_EST;
588 si->err_type = SI_ET_NONE;
589 si->err_loc = NULL;
590 return 1;
591}
592
593/* This function is called with (si->state == SI_ST_CER) meaning that a
594 * previous connection attempt has failed and that the file descriptor
595 * has already been released. Possible causes include asynchronous error
596 * notification and time out. Possible output states are SI_ST_CLO when
597 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
598 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
599 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
600 * marked as in error state. It returns 0.
601 */
Simon Hormandec5be42011-06-08 09:19:07 +0900602static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603{
604 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100605 if (target_srv(&s->target)) {
606 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100607
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 if (s->flags & SN_CURR_SESS) {
609 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100610 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 }
613
614 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200615 si->conn_retries--;
616 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 if (!si->err_type) {
618 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100619 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 }
621
Willy Tarreau827aee92011-03-10 16:55:02 +0100622 if (target_srv(&s->target))
623 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100624 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100625 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100626 if (may_dequeue_tasks(target_srv(&s->target), s->be))
627 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628
629 /* shutw is enough so stop a connecting socket */
630 si->shutw(si);
631 si->ob->flags |= BF_WRITE_ERROR;
632 si->ib->flags |= BF_READ_ERROR;
633
634 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100635 if (s->srv_error)
636 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 return 0;
638 }
639
640 /* If the "redispatch" option is set on the backend, we are allowed to
641 * retry on another server for the last retry. In order to achieve this,
642 * we must mark the session unassigned, and eventually clear the DIRECT
643 * bit to ignore any persistence cookie. We won't count a retry nor a
644 * redispatch yet, because this will depend on what server is selected.
645 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100646 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100647 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100648 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100649 if (may_dequeue_tasks(target_srv(&s->target), s->be))
650 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651
652 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 si->state = SI_ST_REQ;
654 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100655 if (target_srv(&s->target))
656 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100657 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658 si->state = SI_ST_ASS;
659 }
660
661 if (si->flags & SI_FL_ERR) {
662 /* The error was an asynchronous connection error, and we will
663 * likely have to retry connecting to the same server, most
664 * likely leading to the same result. To avoid this, we wait
665 * one second before retrying.
666 */
667
668 if (!si->err_type)
669 si->err_type = SI_ET_CONN_ERR;
670
671 si->state = SI_ST_TAR;
672 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
673 return 0;
674 }
675 return 0;
676}
677
678/*
679 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200680 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
681 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 */
Simon Hormandec5be42011-06-08 09:19:07 +0900683static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684{
685 struct buffer *req = si->ob;
686 struct buffer *rep = si->ib;
687
Willy Tarreau827aee92011-03-10 16:55:02 +0100688 if (target_srv(&s->target))
689 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100690
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 /* if the user wants to log as soon as possible, without counting
693 * bytes from the server, then this is the right moment. */
694 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
695 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100696 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 }
699 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
701 /* reset hdr_idx which was already initialized by the request.
702 * right now, the http parser does it.
703 * hdr_idx_init(&s->txn.hdr_idx);
704 */
705 }
706
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200707 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200709 if (si->connect) {
710 /* real connections have timeouts */
711 req->wto = s->be->timeout.server;
712 rep->rto = s->be->timeout.server;
713 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 req->wex = TICK_ETERNITY;
715}
716
717/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
718 * Other input states are simply ignored.
719 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
720 * Flags must have previously been updated for timeouts and other conditions.
721 */
Simon Hormandec5be42011-06-08 09:19:07 +0900722static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100723{
Willy Tarreau827aee92011-03-10 16:55:02 +0100724 struct server *srv = target_srv(&s->target);
725
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
727 now_ms, __FUNCTION__,
728 s,
729 s->req, s->rep,
730 s->req->rex, s->rep->wex,
731 s->req->flags, s->rep->flags,
732 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
733
734 if (si->state == SI_ST_ASS) {
735 /* Server assigned to connection request, we have to try to connect now */
736 int conn_err;
737
738 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100739 srv = target_srv(&s->target);
740
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 if (conn_err == SN_ERR_NONE) {
742 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 if (srv)
744 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 return;
746 }
747
748 /* We have received a synchronous error. We might have to
749 * abort, retry immediately or redispatch.
750 */
751 if (conn_err == SN_ERR_INTERNAL) {
752 if (!si->err_type) {
753 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100754 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 }
756
Willy Tarreau827aee92011-03-10 16:55:02 +0100757 if (srv)
758 srv_inc_sess_ctr(srv);
759 if (srv)
760 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100761 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100762
763 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100764 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100765 if (may_dequeue_tasks(srv, s->be))
766 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767
768 /* Failed and not retryable. */
769 si->shutr(si);
770 si->shutw(si);
771 si->ob->flags |= BF_WRITE_ERROR;
772
773 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
774
775 /* no session was ever accounted for this server */
776 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100777 if (s->srv_error)
778 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100779 return;
780 }
781
782 /* We are facing a retryable error, but we don't want to run a
783 * turn-around now, as the problem is likely a source port
784 * allocation problem, so we want to retry now.
785 */
786 si->state = SI_ST_CER;
787 si->flags &= ~SI_FL_ERR;
788 sess_update_st_cer(s, si);
789 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
790 return;
791 }
792 else if (si->state == SI_ST_QUE) {
793 /* connection request was queued, check for any update */
794 if (!s->pend_pos) {
795 /* The connection is not in the queue anymore. Either
796 * we have a server connection slot available and we
797 * go directly to the assigned state, or we need to
798 * load-balance first and go to the INI state.
799 */
800 si->exp = TICK_ETERNITY;
801 if (unlikely(!(s->flags & SN_ASSIGNED)))
802 si->state = SI_ST_REQ;
803 else {
804 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
805 si->state = SI_ST_ASS;
806 }
807 return;
808 }
809
810 /* Connection request still in queue... */
811 if (si->flags & SI_FL_EXP) {
812 /* ... and timeout expired */
813 si->exp = TICK_ETERNITY;
814 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (srv)
816 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100817 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 si->shutr(si);
819 si->shutw(si);
820 si->ob->flags |= BF_WRITE_TIMEOUT;
821 if (!si->err_type)
822 si->err_type = SI_ET_QUEUE_TO;
823 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824 if (s->srv_error)
825 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826 return;
827 }
828
829 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200830 if ((si->ob->flags & (BF_READ_ERROR)) ||
831 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200832 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833 /* give up */
834 si->exp = TICK_ETERNITY;
835 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
836 si->shutr(si);
837 si->shutw(si);
838 si->err_type |= SI_ET_QUEUE_ABRT;
839 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 if (s->srv_error)
841 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 return;
843 }
844
845 /* Nothing changed */
846 return;
847 }
848 else if (si->state == SI_ST_TAR) {
849 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200850 if ((si->ob->flags & (BF_READ_ERROR)) ||
851 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200852 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853 /* give up */
854 si->exp = TICK_ETERNITY;
855 si->shutr(si);
856 si->shutw(si);
857 si->err_type |= SI_ET_CONN_ABRT;
858 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100859 if (s->srv_error)
860 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100861 return;
862 }
863
864 if (!(si->flags & SI_FL_EXP))
865 return; /* still in turn-around */
866
867 si->exp = TICK_ETERNITY;
868
869 /* we keep trying on the same server as long as the session is
870 * marked "assigned".
871 * FIXME: Should we force a redispatch attempt when the server is down ?
872 */
873 if (s->flags & SN_ASSIGNED)
874 si->state = SI_ST_ASS;
875 else
876 si->state = SI_ST_REQ;
877 return;
878 }
879}
880
Simon Hormandec5be42011-06-08 09:19:07 +0900881/* Set correct session termination flags in case no analyser has done it. It
882 * also counts a failed request if the server state has not reached the request
883 * stage.
884 */
885static void sess_set_term_flags(struct session *s)
886{
887 if (!(s->flags & SN_FINST_MASK)) {
888 if (s->si[1].state < SI_ST_REQ) {
889
890 s->fe->fe_counters.failed_req++;
891 if (s->listener->counters)
892 s->listener->counters->failed_req++;
893
894 s->flags |= SN_FINST_R;
895 }
896 else if (s->si[1].state == SI_ST_QUE)
897 s->flags |= SN_FINST_Q;
898 else if (s->si[1].state < SI_ST_EST)
899 s->flags |= SN_FINST_C;
900 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
901 s->flags |= SN_FINST_D;
902 else
903 s->flags |= SN_FINST_L;
904 }
905}
906
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907/* This function initiates a server connection request on a stream interface
908 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
909 * indicating that a server has been assigned. It may also return SI_ST_QUE,
910 * or SI_ST_CLO upon error.
911 */
912static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
913 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
914 now_ms, __FUNCTION__,
915 s,
916 s->req, s->rep,
917 s->req->rex, s->rep->wex,
918 s->req->flags, s->rep->flags,
919 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
920
921 if (si->state != SI_ST_REQ)
922 return;
923
924 /* Try to assign a server */
925 if (srv_redispatch_connect(s) != 0) {
926 /* We did not get a server. Either we queued the
927 * connection request, or we encountered an error.
928 */
929 if (si->state == SI_ST_QUE)
930 return;
931
932 /* we did not get any server, let's check the cause */
933 si->shutr(si);
934 si->shutw(si);
935 si->ob->flags |= BF_WRITE_ERROR;
936 if (!si->err_type)
937 si->err_type = SI_ET_CONN_OTHER;
938 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100939 if (s->srv_error)
940 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100941 return;
942 }
943
944 /* The server is assigned */
945 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
946 si->state = SI_ST_ASS;
947}
948
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200949/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100950 * if appropriate. The default_backend rule is also considered, then the
951 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200952 * It returns 1 if the processing can continue on next analysers, or zero if it
953 * either needs more data or wants to immediately abort the request.
954 */
Simon Hormandec5be42011-06-08 09:19:07 +0900955static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200956{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200957 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200959 req->analysers &= ~an_bit;
960 req->analyse_exp = TICK_ETERNITY;
961
962 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
963 now_ms, __FUNCTION__,
964 s,
965 req,
966 req->rex, req->wex,
967 req->flags,
968 req->l,
969 req->analysers);
970
971 /* now check whether we have some switching rules for this request */
972 if (!(s->flags & SN_BE_ASSIGNED)) {
973 struct switching_rule *rule;
974
975 list_for_each_entry(rule, &s->fe->switching_rules, list) {
976 int ret;
977
978 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
979 ret = acl_pass(ret);
980 if (rule->cond->pol == ACL_COND_UNLESS)
981 ret = !ret;
982
983 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200984 if (!session_set_backend(s, rule->be.backend))
985 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200986 break;
987 }
988 }
989
990 /* To ensure correct connection accounting on the backend, we
991 * have to assign one if it was not set (eg: a listen). This
992 * measure also takes care of correctly setting the default
993 * backend if any.
994 */
995 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200996 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
997 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200998 }
999
Willy Tarreaufb356202010-08-03 14:02:05 +02001000 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1001 if (s->fe == s->be) {
1002 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001003 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001004 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001005
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001006 /* 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 +01001007 * persistence rule, and report that in the session.
1008 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001009 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001010 int ret = 1;
1011
1012 if (prst_rule->cond) {
1013 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1014 ret = acl_pass(ret);
1015 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1016 ret = !ret;
1017 }
1018
1019 if (ret) {
1020 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001021 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1022 s->flags |= SN_FORCE_PRST;
1023 } else {
1024 s->flags |= SN_IGNORE_PRST;
1025 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001026 break;
1027 }
1028 }
1029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001030 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001031
1032 sw_failed:
1033 /* immediately abort this request in case of allocation failure */
1034 buffer_abort(s->req);
1035 buffer_abort(s->rep);
1036
1037 if (!(s->flags & SN_ERR_MASK))
1038 s->flags |= SN_ERR_RESOURCE;
1039 if (!(s->flags & SN_FINST_MASK))
1040 s->flags |= SN_FINST_R;
1041
1042 s->txn.status = 500;
1043 s->req->analysers = 0;
1044 s->req->analyse_exp = TICK_ETERNITY;
1045 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001046}
1047
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001048/* This stream analyser works on a request. It applies all use-server rules on
1049 * it then returns 1. The data must already be present in the buffer otherwise
1050 * they won't match. It always returns 1.
1051 */
1052static int process_server_rules(struct session *s, struct buffer *req, int an_bit)
1053{
1054 struct proxy *px = s->be;
1055 struct server_rule *rule;
1056
1057 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1058 now_ms, __FUNCTION__,
1059 s,
1060 req,
1061 req->rex, req->wex,
1062 req->flags,
1063 req->l,
1064 req->analysers);
1065
1066 if (!(s->flags & SN_ASSIGNED)) {
1067 list_for_each_entry(rule, &px->server_rules, list) {
1068 int ret;
1069
1070 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1071 ret = acl_pass(ret);
1072 if (rule->cond->pol == ACL_COND_UNLESS)
1073 ret = !ret;
1074
1075 if (ret) {
1076 struct server *srv = rule->srv.ptr;
1077
1078 if ((srv->state & SRV_RUNNING) ||
1079 (px->options & PR_O_PERSIST) ||
1080 (s->flags & SN_FORCE_PRST)) {
1081 s->flags |= SN_DIRECT | SN_ASSIGNED;
1082 set_target_server(&s->target, srv);
1083 break;
1084 }
1085 /* if the server is not UP, let's go on with next rules
1086 * just in case another one is suited.
1087 */
1088 }
1089 }
1090 }
1091
1092 req->analysers &= ~an_bit;
1093 req->analyse_exp = TICK_ETERNITY;
1094 return 1;
1095}
1096
Emeric Brun1d33b292010-01-04 15:47:17 +01001097/* This stream analyser works on a request. It applies all sticking rules on
1098 * it then returns 1. The data must already be present in the buffer otherwise
1099 * they won't match. It always returns 1.
1100 */
Simon Hormandec5be42011-06-08 09:19:07 +09001101static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001102{
1103 struct proxy *px = s->be;
1104 struct sticking_rule *rule;
1105
1106 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1107 now_ms, __FUNCTION__,
1108 s,
1109 req,
1110 req->rex, req->wex,
1111 req->flags,
1112 req->l,
1113 req->analysers);
1114
1115 list_for_each_entry(rule, &px->sticking_rules, list) {
1116 int ret = 1 ;
1117 int i;
1118
1119 for (i = 0; i < s->store_count; i++) {
1120 if (rule->table.t == s->store[i].table)
1121 break;
1122 }
1123
1124 if (i != s->store_count)
1125 continue;
1126
1127 if (rule->cond) {
1128 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1129 ret = acl_pass(ret);
1130 if (rule->cond->pol == ACL_COND_UNLESS)
1131 ret = !ret;
1132 }
1133
1134 if (ret) {
1135 struct stktable_key *key;
1136
Emeric Brun485479d2010-09-23 18:02:19 +02001137 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001138 if (!key)
1139 continue;
1140
1141 if (rule->flags & STK_IS_MATCH) {
1142 struct stksess *ts;
1143
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001144 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001145 if (!(s->flags & SN_ASSIGNED)) {
1146 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001147 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001148
1149 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001150 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1151 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001152 if (node) {
1153 struct server *srv;
1154
1155 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001156 if ((srv->state & SRV_RUNNING) ||
1157 (px->options & PR_O_PERSIST) ||
1158 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001159 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001160 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001161 }
1162 }
1163 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001164 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001165 }
1166 }
1167 if (rule->flags & STK_IS_STORE) {
1168 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1169 struct stksess *ts;
1170
1171 ts = stksess_new(rule->table.t, key);
1172 if (ts) {
1173 s->store[s->store_count].table = rule->table.t;
1174 s->store[s->store_count++].ts = ts;
1175 }
1176 }
1177 }
1178 }
1179 }
1180
1181 req->analysers &= ~an_bit;
1182 req->analyse_exp = TICK_ETERNITY;
1183 return 1;
1184}
1185
1186/* This stream analyser works on a response. It applies all store rules on it
1187 * then returns 1. The data must already be present in the buffer otherwise
1188 * they won't match. It always returns 1.
1189 */
Simon Hormandec5be42011-06-08 09:19:07 +09001190static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001191{
1192 struct proxy *px = s->be;
1193 struct sticking_rule *rule;
1194 int i;
1195
1196 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1197 now_ms, __FUNCTION__,
1198 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001199 rep,
1200 rep->rex, rep->wex,
1201 rep->flags,
1202 rep->l,
1203 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001204
1205 list_for_each_entry(rule, &px->storersp_rules, list) {
1206 int ret = 1 ;
1207 int storereqidx = -1;
1208
1209 for (i = 0; i < s->store_count; i++) {
1210 if (rule->table.t == s->store[i].table) {
1211 if (!(s->store[i].flags))
1212 storereqidx = i;
1213 break;
1214 }
1215 }
1216
1217 if ((i != s->store_count) && (storereqidx == -1))
1218 continue;
1219
1220 if (rule->cond) {
1221 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1222 ret = acl_pass(ret);
1223 if (rule->cond->pol == ACL_COND_UNLESS)
1224 ret = !ret;
1225 }
1226
1227 if (ret) {
1228 struct stktable_key *key;
1229
Emeric Brun485479d2010-09-23 18:02:19 +02001230 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001231 if (!key)
1232 continue;
1233
1234 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001235 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001236 s->store[storereqidx].flags = 1;
1237 }
1238 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1239 struct stksess *ts;
1240
1241 ts = stksess_new(rule->table.t, key);
1242 if (ts) {
1243 s->store[s->store_count].table = rule->table.t;
1244 s->store[s->store_count].flags = 1;
1245 s->store[s->store_count++].ts = ts;
1246 }
1247 }
1248 }
1249 }
1250
1251 /* process store request and store response */
1252 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001253 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001254 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001255
Simon Hormanfa461682011-06-25 09:39:49 +09001256 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1257 stksess_free(s->store[i].table, s->store[i].ts);
1258 s->store[i].ts = NULL;
1259 continue;
1260 }
1261
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001262 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1263 if (ts) {
1264 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001265 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001266 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001267 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001268 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001269 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001270
1271 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001272 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001273 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001274 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001275 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001276
1277 rep->analysers &= ~an_bit;
1278 rep->analyse_exp = TICK_ETERNITY;
1279 return 1;
1280}
1281
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001282/* This macro is very specific to the function below. See the comments in
1283 * process_session() below to understand the logic and the tests.
1284 */
1285#define UPDATE_ANALYSERS(real, list, back, flag) { \
1286 list = (((list) & ~(flag)) | ~(back)) & (real); \
1287 back = real; \
1288 if (!(list)) \
1289 break; \
1290 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1291 continue; \
1292}
1293
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001294/* Processes the client, server, request and response jobs of a session task,
1295 * then puts it back to the wait queue in a clean state, or cleans up its
1296 * resources if it must be deleted. Returns in <next> the date the task wants
1297 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1298 * nothing too many times, the request and response buffers flags are monitored
1299 * and each function is called only if at least another function has changed at
1300 * least one flag it is interested in.
1301 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001302struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303{
Willy Tarreau827aee92011-03-10 16:55:02 +01001304 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001305 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001306 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001307 unsigned int rq_prod_last, rq_cons_last;
1308 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001309 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310
1311 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1312 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1313
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001314 /* this data may be no longer valid, clear it */
1315 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1316
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001317 /* This flag must explicitly be set every time */
1318 s->req->flags &= ~BF_READ_NOEXP;
1319
1320 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001321 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1322 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001323
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001324 /* we don't want the stream interface functions to recursively wake us up */
1325 if (s->req->prod->owner == t)
1326 s->req->prod->flags |= SI_FL_DONT_WAKE;
1327 if (s->req->cons->owner == t)
1328 s->req->cons->flags |= SI_FL_DONT_WAKE;
1329
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001330 /* 1a: Check for low level timeouts if needed. We just set a flag on
1331 * stream interfaces when their timeouts have expired.
1332 */
1333 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1334 stream_int_check_timeouts(&s->si[0]);
1335 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001336
1337 /* check buffer timeouts, and close the corresponding stream interfaces
1338 * for future reads or writes. Note: this will also concern upper layers
1339 * but we do not touch any other flag. We must be careful and correctly
1340 * detect state changes when calling them.
1341 */
1342
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001343 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001344
Willy Tarreau14641402009-12-29 14:49:56 +01001345 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1346 s->req->cons->flags |= SI_FL_NOLINGER;
1347 s->req->cons->shutw(s->req->cons);
1348 }
1349
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001350 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1351 s->req->prod->shutr(s->req->prod);
1352
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001353 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001354
Willy Tarreau14641402009-12-29 14:49:56 +01001355 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1356 s->rep->cons->flags |= SI_FL_NOLINGER;
1357 s->rep->cons->shutw(s->rep->cons);
1358 }
1359
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001360 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1361 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001362 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001363
1364 /* 1b: check for low-level errors reported at the stream interface.
1365 * First we check if it's a retryable error (in which case we don't
1366 * want to tell the buffer). Otherwise we report the error one level
1367 * upper by setting flags into the buffers. Note that the side towards
1368 * the client cannot have connect (hence retryable) errors. Also, the
1369 * connection setup code must be able to deal with any type of abort.
1370 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001371 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001372 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1373 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1374 s->si[0].shutr(&s->si[0]);
1375 s->si[0].shutw(&s->si[0]);
1376 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001377 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001378 s->be->be_counters.cli_aborts++;
1379 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001380 if (srv)
1381 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001382 if (!(s->flags & SN_ERR_MASK))
1383 s->flags |= SN_ERR_CLICL;
1384 if (!(s->flags & SN_FINST_MASK))
1385 s->flags |= SN_FINST_D;
1386 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001387 }
1388 }
1389
1390 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1391 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1392 s->si[1].shutr(&s->si[1]);
1393 s->si[1].shutw(&s->si[1]);
1394 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001395 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001396 if (srv)
1397 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001398 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001399 s->be->be_counters.srv_aborts++;
1400 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001401 if (srv)
1402 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001403 if (!(s->flags & SN_ERR_MASK))
1404 s->flags |= SN_ERR_SRVCL;
1405 if (!(s->flags & SN_FINST_MASK))
1406 s->flags |= SN_FINST_D;
1407 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001408 }
1409 /* note: maybe we should process connection errors here ? */
1410 }
1411
1412 if (s->si[1].state == SI_ST_CON) {
1413 /* we were trying to establish a connection on the server side,
1414 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1415 */
1416 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1417 sess_update_st_cer(s, &s->si[1]);
1418 else if (s->si[1].state == SI_ST_EST)
1419 sess_establish(s, &s->si[1]);
1420
1421 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1422 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1423 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1424 */
1425 }
1426
Willy Tarreau815a9b22010-07-27 17:15:12 +02001427 rq_prod_last = s->si[0].state;
1428 rq_cons_last = s->si[1].state;
1429 rp_cons_last = s->si[0].state;
1430 rp_prod_last = s->si[1].state;
1431
1432 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001433 /* Check for connection closure */
1434
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001435 DPRINTF(stderr,
1436 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
1437 now_ms, __FUNCTION__, __LINE__,
1438 t,
1439 s, s->flags,
1440 s->req, s->rep,
1441 s->req->rex, s->rep->wex,
1442 s->req->flags, s->rep->flags,
1443 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1444 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001445 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001446
1447 /* nothing special to be done on client side */
1448 if (unlikely(s->req->prod->state == SI_ST_DIS))
1449 s->req->prod->state = SI_ST_CLO;
1450
1451 /* When a server-side connection is released, we have to count it and
1452 * check for pending connections on this server.
1453 */
1454 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1455 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001456 srv = target_srv(&s->target);
1457 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001458 if (s->flags & SN_CURR_SESS) {
1459 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001460 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001461 }
1462 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001463 if (may_dequeue_tasks(srv, s->be))
1464 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001465 }
1466 }
1467
1468 /*
1469 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1470 * at this point.
1471 */
1472
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001473 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001474 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001475 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001476 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1477 s->si[0].state != rq_prod_last ||
1478 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001479 unsigned int flags = s->req->flags;
1480
1481 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001482 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 unsigned int ana_list;
1484 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001485
Willy Tarreau90deb182010-01-07 00:20:41 +01001486 /* it's up to the analysers to stop new connections,
1487 * disable reading or closing. Note: if an analyser
1488 * disables any of these bits, it is responsible for
1489 * enabling them again when it disables itself, so
1490 * that other analysers are called in similar conditions.
1491 */
1492 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001493 buffer_auto_connect(s->req);
1494 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001495
1496 /* We will call all analysers for which a bit is set in
1497 * s->req->analysers, following the bit order from LSB
1498 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001499 * the list when not needed. Any analyser may return 0
1500 * to break out of the loop, either because of missing
1501 * data to take a decision, or because it decides to
1502 * kill the session. We loop at least once through each
1503 * analyser, and we may loop again if other analysers
1504 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001505 *
1506 * We build a list of analysers to run. We evaluate all
1507 * of these analysers in the order of the lower bit to
1508 * the higher bit. This ordering is very important.
1509 * An analyser will often add/remove other analysers,
1510 * including itself. Any changes to itself have no effect
1511 * on the loop. If it removes any other analysers, we
1512 * want those analysers not to be called anymore during
1513 * this loop. If it adds an analyser that is located
1514 * after itself, we want it to be scheduled for being
1515 * processed during the loop. If it adds an analyser
1516 * which is located before it, we want it to switch to
1517 * it immediately, even if it has already been called
1518 * once but removed since.
1519 *
1520 * In order to achieve this, we compare the analyser
1521 * list after the call with a copy of it before the
1522 * call. The work list is fed with analyser bits that
1523 * appeared during the call. Then we compare previous
1524 * work list with the new one, and check the bits that
1525 * appeared. If the lowest of these bits is lower than
1526 * the current bit, it means we have enabled a previous
1527 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001528 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001529
1530 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001531 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001532 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001533
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001534 if (ana_list & AN_REQ_DECODE_PROXY) {
1535 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1536 break;
1537 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1538 }
1539
Willy Tarreaufb356202010-08-03 14:02:05 +02001540 if (ana_list & AN_REQ_INSPECT_FE) {
1541 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001542 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001543 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001544 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001545
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001546 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001547 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001548 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001549 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001550 }
1551
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001552 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001553 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1554 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001555 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001556 }
1557
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001558 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001559 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1560 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001561 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001562 }
1563
Willy Tarreaufb356202010-08-03 14:02:05 +02001564 if (ana_list & AN_REQ_INSPECT_BE) {
1565 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1566 break;
1567 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1568 }
1569
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001570 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001571 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1572 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001573 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001574 }
1575
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001576 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001577 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001578 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001579 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001580 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001581
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001582 if (ana_list & AN_REQ_SRV_RULES) {
1583 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1584 break;
1585 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1586 }
1587
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001588 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001589 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1590 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001591 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001592 }
1593
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001594 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001595 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001596 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001597 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001598 }
Emeric Brun647caf12009-06-30 17:57:00 +02001599
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001600 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001601 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1602 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001603 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001604 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001605
Emeric Brun1d33b292010-01-04 15:47:17 +01001606 if (ana_list & AN_REQ_STICKING_RULES) {
1607 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1608 break;
1609 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1610 }
1611
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001612 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001613 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1614 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001616 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001617 break;
1618 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001619 }
Willy Tarreau84455332009-03-15 22:34:05 +01001620
Willy Tarreau815a9b22010-07-27 17:15:12 +02001621 rq_prod_last = s->si[0].state;
1622 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001623 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001624 rqf_last = s->req->flags;
1625
1626 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001627 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001628 }
1629
Willy Tarreau576507f2010-01-07 00:09:04 +01001630 /* we'll monitor the request analysers while parsing the response,
1631 * because some response analysers may indirectly enable new request
1632 * analysers (eg: HTTP keep-alive).
1633 */
1634 req_ana_back = s->req->analysers;
1635
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001636 resync_response:
1637 /* Analyse response */
1638
1639 if (unlikely(s->rep->flags & BF_HIJACK)) {
1640 /* In inject mode, we wake up everytime something has
1641 * happened on the write side of the buffer.
1642 */
1643 unsigned int flags = s->rep->flags;
1644
1645 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1646 !(s->rep->flags & BF_FULL)) {
1647 s->rep->hijacker(s, s->rep);
1648 }
1649
1650 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1651 rpf_last = s->rep->flags;
1652 goto resync_response;
1653 }
1654 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001655 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001656 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1657 s->si[0].state != rp_cons_last ||
1658 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001659 unsigned int flags = s->rep->flags;
1660
Willy Tarreau0499e352010-12-17 07:13:42 +01001661 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1662 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1663 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1664 * for some free space in the response buffer, so we might need to call
1665 * it when something changes in the response buffer, but still we pass
1666 * it the request buffer. Note that the SI state might very well still
1667 * be zero due to us returning a flow of redirects!
1668 */
1669 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1670 s->req->flags |= BF_WAKE_ONCE;
1671 }
1672
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001673 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001674 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001675 unsigned int ana_list;
1676 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001677
Willy Tarreau90deb182010-01-07 00:20:41 +01001678 /* it's up to the analysers to stop disable reading or
1679 * closing. Note: if an analyser disables any of these
1680 * bits, it is responsible for enabling them again when
1681 * it disables itself, so that other analysers are called
1682 * in similar conditions.
1683 */
1684 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001685 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001686
1687 /* We will call all analysers for which a bit is set in
1688 * s->rep->analysers, following the bit order from LSB
1689 * to MSB. The analysers must remove themselves from
1690 * the list when not needed. Any analyser may return 0
1691 * to break out of the loop, either because of missing
1692 * data to take a decision, or because it decides to
1693 * kill the session. We loop at least once through each
1694 * analyser, and we may loop again if other analysers
1695 * are added in the middle.
1696 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001697
1698 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001699 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001700 /* Warning! ensure that analysers are always placed in ascending order! */
1701
Emeric Brun97679e72010-09-23 17:56:44 +02001702 if (ana_list & AN_RES_INSPECT) {
1703 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1704 break;
1705 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1706 }
1707
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001708 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001709 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1710 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001712 }
1713
Emeric Brun1d33b292010-01-04 15:47:17 +01001714 if (ana_list & AN_RES_STORE_RULES) {
1715 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1716 break;
1717 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1718 }
1719
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001720 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001721 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1722 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001724 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001725
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001726 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001727 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1728 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001729 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001730 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001731 break;
1732 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001733 }
1734
Willy Tarreau815a9b22010-07-27 17:15:12 +02001735 rp_cons_last = s->si[0].state;
1736 rp_prod_last = s->si[1].state;
1737 rpf_last = s->rep->flags;
1738
1739 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001740 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001741 }
1742
Willy Tarreau576507f2010-01-07 00:09:04 +01001743 /* maybe someone has added some request analysers, so we must check and loop */
1744 if (s->req->analysers & ~req_ana_back)
1745 goto resync_request;
1746
Willy Tarreau0499e352010-12-17 07:13:42 +01001747 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1748 goto resync_request;
1749
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001750 /* FIXME: here we should call protocol handlers which rely on
1751 * both buffers.
1752 */
1753
1754
1755 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001756 * Now we propagate unhandled errors to the session. Normally
1757 * we're just in a data phase here since it means we have not
1758 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001759 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001760 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001761 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001762 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1763 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001764 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001765 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001766 s->be->be_counters.cli_aborts++;
1767 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001768 if (srv)
1769 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001770 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001771 }
1772 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001773 s->be->be_counters.cli_aborts++;
1774 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001775 if (srv)
1776 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001777 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001778 }
1779 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001780 s->be->be_counters.srv_aborts++;
1781 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001782 if (srv)
1783 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001784 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001785 }
1786 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001787 s->be->be_counters.srv_aborts++;
1788 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001789 if (srv)
1790 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001791 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001792 }
Willy Tarreau84455332009-03-15 22:34:05 +01001793 sess_set_term_flags(s);
1794 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001795 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1796 /* Report it if the server got an error or a read timeout expired */
1797 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001798 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001799 s->be->be_counters.srv_aborts++;
1800 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001801 if (srv)
1802 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001803 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001804 }
1805 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001806 s->be->be_counters.srv_aborts++;
1807 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001808 if (srv)
1809 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001810 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001811 }
1812 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001813 s->be->be_counters.cli_aborts++;
1814 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001815 if (srv)
1816 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001817 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001818 }
1819 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001820 s->be->be_counters.cli_aborts++;
1821 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001822 if (srv)
1823 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001824 s->flags |= SN_ERR_CLITO;
1825 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001826 sess_set_term_flags(s);
1827 }
Willy Tarreau84455332009-03-15 22:34:05 +01001828 }
1829
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001830 /*
1831 * Here we take care of forwarding unhandled data. This also includes
1832 * connection establishments and shutdown requests.
1833 */
1834
1835
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001836 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001837 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001838 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1839 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001840 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001841 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001842 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001843 (s->req->prod->state >= SI_ST_EST) &&
1844 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001845 /* This buffer is freewheeling, there's no analyser nor hijacker
1846 * attached to it. If any data are left in, we'll permit them to
1847 * move.
1848 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001849 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001850 buffer_auto_connect(s->req);
1851 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001852 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001853
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001854 /* We'll let data flow between the producer (if still connected)
1855 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001856 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001857 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001858 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001859 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001860
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001861 /* check if it is wise to enable kernel splicing to forward request data */
1862 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1863 s->req->to_forward &&
1864 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001865 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001866 (pipes_used < global.maxpipes) &&
1867 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1868 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1869 (s->req->flags & BF_STREAMER_FAST)))) {
1870 s->req->flags |= BF_KERN_SPLICING;
1871 }
1872
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001873 /* reflect what the L7 analysers have seen last */
1874 rqf_last = s->req->flags;
1875
1876 /*
1877 * Now forward all shutdown requests between both sides of the buffer
1878 */
1879
Willy Tarreau520d95e2009-09-19 21:04:57 +02001880 /* first, let's check if the request buffer needs to shutdown(write), which may
1881 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001882 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001883 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001884 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001885 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001886 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001887
1888 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001889 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 +01001890 s->req->cons->shutw(s->req->cons);
1891
1892 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001893 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1894 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001895 buffer_shutr_now(s->req);
1896
1897 /* shutdown(read) pending */
1898 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1899 s->req->prod->shutr(s->req->prod);
1900
Willy Tarreau520d95e2009-09-19 21:04:57 +02001901 /* it's possible that an upper layer has requested a connection setup or abort.
1902 * There are 2 situations where we decide to establish a new connection :
1903 * - there are data scheduled for emission in the buffer
1904 * - the BF_AUTO_CONNECT flag is set (active connection)
1905 */
1906 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001907 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001908 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001909 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001910 * switch to the connected state, otherwise we perform a connection
1911 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001912 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001913 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001914 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001915 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001916 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001917 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1918 s->req->wex = TICK_ETERNITY;
1919 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001920 }
Willy Tarreau73201222009-08-16 18:27:24 +02001921 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001922 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001923 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001924 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1925 buffer_shutr_now(s->rep);
1926 }
Willy Tarreau92795622009-03-06 12:51:23 +01001927 }
1928
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001929
1930 /* we may have a pending connection request, or a connection waiting
1931 * for completion.
1932 */
1933 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1934 do {
1935 /* nb: step 1 might switch from QUE to ASS, but we first want
1936 * to give a chance to step 2 to perform a redirect if needed.
1937 */
1938 if (s->si[1].state != SI_ST_REQ)
1939 sess_update_stream_int(s, &s->si[1]);
1940 if (s->si[1].state == SI_ST_REQ)
1941 sess_prepare_conn_req(s, &s->si[1]);
1942
Willy Tarreau827aee92011-03-10 16:55:02 +01001943 srv = target_srv(&s->target);
1944 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001945 perform_http_redirect(s, &s->si[1]);
1946 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001947
1948 /* Now we can add the server name to a header (if requested) */
1949 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01001950 if ((s->flags & SN_BE_ASSIGNED) &&
Mark Lamourinec2247f02012-01-04 13:02:01 -05001951 (s->be->mode == PR_MODE_HTTP) &&
1952 (s->be->server_id_hdr_name != NULL)) {
1953
1954 http_send_name_header(&s->txn,
1955 &s->txn.req,
1956 s->req,
1957 s->be,
1958 target_srv(&s->target)->id);
1959 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960 }
1961
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001962 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001963 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001964 goto resync_stream_interface;
1965
Willy Tarreau815a9b22010-07-27 17:15:12 +02001966 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001967 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001968 goto resync_request;
1969
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001971
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001972 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001973 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001974 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1975 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001976 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001977 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001978 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001979 (s->rep->prod->state >= SI_ST_EST) &&
1980 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 /* This buffer is freewheeling, there's no analyser nor hijacker
1982 * attached to it. If any data are left in, we'll permit them to
1983 * move.
1984 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001985 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001986 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001987 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001988
1989 /* We'll let data flow between the producer (if still connected)
1990 * to the consumer.
1991 */
1992 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001993 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001994 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001995
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 /* check if it is wise to enable kernel splicing to forward response data */
1997 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1998 s->rep->to_forward &&
1999 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02002000 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002001 (pipes_used < global.maxpipes) &&
2002 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2003 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
2004 (s->rep->flags & BF_STREAMER_FAST)))) {
2005 s->rep->flags |= BF_KERN_SPLICING;
2006 }
2007
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002008 /* reflect what the L7 analysers have seen last */
2009 rpf_last = s->rep->flags;
2010
2011 /*
2012 * Now forward all shutdown requests between both sides of the buffer
2013 */
2014
2015 /*
2016 * FIXME: this is probably where we should produce error responses.
2017 */
2018
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002019 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002020 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
2021 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022 buffer_shutw_now(s->rep);
2023
2024 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02002025 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 +01002026 s->rep->cons->shutw(s->rep->cons);
2027
2028 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002029 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
2030 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031 buffer_shutr_now(s->rep);
2032
2033 /* shutdown(read) pending */
2034 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
2035 s->rep->prod->shutr(s->rep->prod);
2036
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002037 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038 goto resync_stream_interface;
2039
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002040 if (s->req->flags != rqf_last)
2041 goto resync_request;
2042
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002043 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002044 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002046 /* we're interested in getting wakeups again */
2047 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2048 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2049
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 /* This is needed only when debugging is enabled, to indicate
2051 * client-side or server-side close. Please note that in the unlikely
2052 * event where both sides would close at once, the sequence is reported
2053 * on the server side first.
2054 */
2055 if (unlikely((global.mode & MODE_DEBUG) &&
2056 (!(global.mode & MODE_QUIET) ||
2057 (global.mode & MODE_VERBOSE)))) {
2058 int len;
2059
2060 if (s->si[1].state == SI_ST_CLO &&
2061 s->si[1].prev_state == SI_ST_EST) {
2062 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2063 s->uniq_id, s->be->id,
2064 (unsigned short)s->si[0].fd,
2065 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002066 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002067 }
2068
2069 if (s->si[0].state == SI_ST_CLO &&
2070 s->si[0].prev_state == SI_ST_EST) {
2071 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2072 s->uniq_id, s->be->id,
2073 (unsigned short)s->si[0].fd,
2074 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002075 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076 }
2077 }
2078
2079 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2080 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2081
2082 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2083 session_process_counters(s);
2084
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002085 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002086 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002087
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002088 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002089 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002091 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2092 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 +01002093 s->si[0].prev_state = s->si[0].state;
2094 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002095 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2096 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097
2098 /* Trick: if a request is being waiting for the server to respond,
2099 * and if we know the server can timeout, we don't want the timeout
2100 * to expire on the client side first, but we're still interested
2101 * in passing data from the client to the server (eg: POST). Thus,
2102 * we can cancel the client's request timeout if the server's
2103 * request timeout is set and the server has not yet sent a response.
2104 */
2105
Willy Tarreau520d95e2009-09-19 21:04:57 +02002106 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002107 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2108 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002109 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002110 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002111
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002112 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002113 * Note that this one may wake the task up again.
2114 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002115 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2116 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2117 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2118 s->req->cons->target.ptr.a->fct(s->req->cons);
2119 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2120 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002121 if (task_in_rq(t)) {
2122 /* If we woke up, we don't want to requeue the
2123 * task to the wait queue, but rather requeue
2124 * it into the runqueue ASAP.
2125 */
2126 t->expire = TICK_ETERNITY;
2127 return t;
2128 }
2129 }
2130
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002131 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2132 tick_first(s->rep->rex, s->rep->wex));
2133 if (s->req->analysers)
2134 t->expire = tick_first(t->expire, s->req->analyse_exp);
2135
2136 if (s->si[0].exp)
2137 t->expire = tick_first(t->expire, s->si[0].exp);
2138
2139 if (s->si[1].exp)
2140 t->expire = tick_first(t->expire, s->si[1].exp);
2141
2142#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002143 fprintf(stderr,
2144 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2145 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2146 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2147 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 +01002148#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002149
2150#ifdef DEBUG_DEV
2151 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002152 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002153 ABORT_NOW();
2154#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002155 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156 }
2157
2158 s->fe->feconn--;
2159 if (s->flags & SN_BE_ASSIGNED)
2160 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002161 if (!(s->listener->options & LI_O_UNLIMITED))
2162 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002163 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002164 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002165 if (s->listener->state == LI_FULL)
2166 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002167
Willy Tarreau08ceb102011-07-24 22:58:00 +02002168 /* Dequeues all of the listeners waiting for a resource */
2169 if (!LIST_ISEMPTY(&global_listener_queue))
2170 dequeue_all_listeners(&global_listener_queue);
2171
Willy Tarreaub32907b2011-07-25 08:37:44 +02002172 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2173 (!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 +02002174 dequeue_all_listeners(&s->fe->listener_queue);
2175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002176 if (unlikely((global.mode & MODE_DEBUG) &&
2177 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2178 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002179 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002180 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002181 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002182 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002183 }
2184
2185 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2186 session_process_counters(s);
2187
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002188 if (s->txn.status) {
2189 int n;
2190
2191 n = s->txn.status / 100;
2192 if (n < 1 || n > 5)
2193 n = 0;
2194
2195 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002196 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002197
Willy Tarreau24657792010-02-26 10:30:28 +01002198 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002199 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002200 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002201 }
2202
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002203 /* let's do a final log if we need it */
2204 if (s->logs.logwait &&
2205 !(s->flags & SN_MONITOR) &&
2206 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002207 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002208 }
2209
2210 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002211 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002212 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002213 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002214 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002215}
2216
Willy Tarreau7c669d72008-06-20 15:04:11 +02002217/*
2218 * This function adjusts sess->srv_conn and maintains the previous and new
2219 * server's served session counts. Setting newsrv to NULL is enough to release
2220 * current connection slot. This function also notifies any LB algo which might
2221 * expect to be informed about any change in the number of active sessions on a
2222 * server.
2223 */
2224void sess_change_server(struct session *sess, struct server *newsrv)
2225{
2226 if (sess->srv_conn == newsrv)
2227 return;
2228
2229 if (sess->srv_conn) {
2230 sess->srv_conn->served--;
2231 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2232 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002233 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002234 }
2235
2236 if (newsrv) {
2237 newsrv->served++;
2238 if (newsrv->proxy->lbprm.server_take_conn)
2239 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002240 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002241 }
2242}
2243
Willy Tarreau84455332009-03-15 22:34:05 +01002244/* Handle server-side errors for default protocols. It is called whenever a a
2245 * connection setup is aborted or a request is aborted in queue. It sets the
2246 * session termination flags so that the caller does not have to worry about
2247 * them. It's installed as ->srv_error for the server-side stream_interface.
2248 */
2249void default_srv_error(struct session *s, struct stream_interface *si)
2250{
2251 int err_type = si->err_type;
2252 int err = 0, fin = 0;
2253
2254 if (err_type & SI_ET_QUEUE_ABRT) {
2255 err = SN_ERR_CLICL;
2256 fin = SN_FINST_Q;
2257 }
2258 else if (err_type & SI_ET_CONN_ABRT) {
2259 err = SN_ERR_CLICL;
2260 fin = SN_FINST_C;
2261 }
2262 else if (err_type & SI_ET_QUEUE_TO) {
2263 err = SN_ERR_SRVTO;
2264 fin = SN_FINST_Q;
2265 }
2266 else if (err_type & SI_ET_QUEUE_ERR) {
2267 err = SN_ERR_SRVCL;
2268 fin = SN_FINST_Q;
2269 }
2270 else if (err_type & SI_ET_CONN_TO) {
2271 err = SN_ERR_SRVTO;
2272 fin = SN_FINST_C;
2273 }
2274 else if (err_type & SI_ET_CONN_ERR) {
2275 err = SN_ERR_SRVCL;
2276 fin = SN_FINST_C;
2277 }
2278 else /* SI_ET_CONN_OTHER and others */ {
2279 err = SN_ERR_INTERNAL;
2280 fin = SN_FINST_C;
2281 }
2282
2283 if (!(s->flags & SN_ERR_MASK))
2284 s->flags |= err;
2285 if (!(s->flags & SN_FINST_MASK))
2286 s->flags |= fin;
2287}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002288
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002289/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2290void session_shutdown(struct session *session, int why)
2291{
2292 if (session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))
2293 return;
2294
2295 buffer_shutw_now(session->req);
2296 buffer_shutr_now(session->rep);
2297 session->task->nice = 1024;
2298 if (!(session->flags & SN_ERR_MASK))
2299 session->flags |= why;
2300 task_wakeup(session->task, TASK_WOKEN_OTHER);
2301}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002302
Willy Tarreau8b22a712010-06-18 17:46:06 +02002303/************************************************************************/
2304/* All supported ACL keywords must be declared here. */
2305/************************************************************************/
2306
Willy Tarreaua5e37562011-12-16 17:06:15 +01002307/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002308static int
2309acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2310{
2311 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002312 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002313 if (ts != NULL) {
2314 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2315 if (!ptr)
2316 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002317 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002318 }
2319 return 1;
2320}
2321
Willy Tarreaua5e37562011-12-16 17:06:15 +01002322/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002323 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002324 */
2325static int
Willy Tarreau56123282010-08-06 19:06:56 +02002326acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2327 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002328{
Willy Tarreau56123282010-08-06 19:06:56 +02002329 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002330 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002331 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002332}
2333
Willy Tarreaua5e37562011-12-16 17:06:15 +01002334/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002335 * backend counters.
2336 */
2337static int
Willy Tarreau56123282010-08-06 19:06:56 +02002338acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2339 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002340{
Willy Tarreau56123282010-08-06 19:06:56 +02002341 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002342 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002343 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002344}
2345
Willy Tarreaua5e37562011-12-16 17:06:15 +01002346/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002347 * address in the table pointed to by expr.
2348 */
2349static int
2350acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2351 struct acl_expr *expr, struct acl_test *test)
2352{
2353 struct stktable_key *key;
2354
David du Colombier4f92d322011-03-24 11:09:31 +01002355 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002356 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002357 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002358
2359 if (expr->arg_len)
2360 px = find_stktable(expr->arg.str);
2361
2362 if (!px)
2363 return 0; /* table not found */
2364
2365 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2366}
2367
2368/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002369 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002370 */
2371static int
2372acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2373{
2374 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002375 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002376 if (ts != NULL) {
2377 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2378 if (!ptr)
2379 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002380 temp_pattern.data.integer = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002381 }
2382 return 1;
2383}
2384
2385/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002386 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002387 */
2388static int
Willy Tarreau56123282010-08-06 19:06:56 +02002389acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2390 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002391{
Willy Tarreau56123282010-08-06 19:06:56 +02002392 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002393 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002394 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002395}
2396
2397/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002398 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002399 */
2400static int
Willy Tarreau56123282010-08-06 19:06:56 +02002401acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2402 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002403{
Willy Tarreau56123282010-08-06 19:06:56 +02002404 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002405 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002406 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002407}
2408
2409/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002410 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002411 */
2412static int
2413acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2414 struct acl_expr *expr, struct acl_test *test)
2415{
2416 struct stktable_key *key;
2417
David du Colombier4f92d322011-03-24 11:09:31 +01002418 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002419 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002420 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002421
2422 if (expr->arg_len)
2423 px = find_stktable(expr->arg.str);
2424
2425 if (!px)
2426 return 0; /* table not found */
2427
2428 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2429}
2430
Willy Tarreauf73cd112011-08-13 01:45:16 +02002431/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002432 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002433 */
2434static int
2435acl_fetch_clr_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2436{
2437 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002438 temp_pattern.data.integer = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002439 if (ts != NULL) {
2440 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2441 if (!ptr)
2442 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002443 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002444 stktable_data_cast(ptr, gpc0) = 0;
2445 }
2446 return 1;
2447}
2448
2449/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002450 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002451 */
2452static int
2453acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2454 struct acl_expr *expr, struct acl_test *test)
2455{
2456 if (!l4->stkctr1_entry)
2457 return 0;
2458 return acl_fetch_clr_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
2459}
2460
2461/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002462 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002463 */
2464static int
2465acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2466 struct acl_expr *expr, struct acl_test *test)
2467{
2468 if (!l4->stkctr2_entry)
2469 return 0;
2470 return acl_fetch_clr_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
2471}
2472
2473/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002474 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002475 */
2476static int
2477acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2478 struct acl_expr *expr, struct acl_test *test)
2479{
2480 struct stktable_key *key;
2481
2482 key = tcp_src_to_stktable_key(l4);
2483 if (!key)
2484 return 0;
2485
2486 if (expr->arg_len)
2487 px = find_stktable(expr->arg.str);
2488
2489 if (!px)
2490 return 0; /* table not found */
2491
2492 return acl_fetch_clr_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2493}
2494
Willy Tarreaua5e37562011-12-16 17:06:15 +01002495/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002496static int
2497acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2498{
2499 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002500 temp_pattern.data.integer = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002501 if (ts != NULL) {
2502 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2503 if (!ptr)
2504 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002505 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002506 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002507 return 1;
2508}
2509
Willy Tarreaua5e37562011-12-16 17:06:15 +01002510/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002511static int
Willy Tarreau56123282010-08-06 19:06:56 +02002512acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2513 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002514{
Willy Tarreau56123282010-08-06 19:06:56 +02002515 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002516 return 0;
2517
Willy Tarreau56123282010-08-06 19:06:56 +02002518 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002519}
2520
Willy Tarreaua5e37562011-12-16 17:06:15 +01002521/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002522static int
Willy Tarreau56123282010-08-06 19:06:56 +02002523acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2524 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002525{
Willy Tarreau56123282010-08-06 19:06:56 +02002526 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002527 return 0;
2528
Willy Tarreau56123282010-08-06 19:06:56 +02002529 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002530}
2531
Willy Tarreaua5e37562011-12-16 17:06:15 +01002532/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002533 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002534 */
2535static int
2536acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2537 struct acl_expr *expr, struct acl_test *test)
2538{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002539 struct stktable_key *key;
2540
David du Colombier4f92d322011-03-24 11:09:31 +01002541 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002542 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002543 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002544
2545 if (expr->arg_len)
2546 px = find_stktable(expr->arg.str);
2547
2548 if (!px)
2549 return 0; /* table not found */
2550
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002551 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002552}
2553
Willy Tarreaua5e37562011-12-16 17:06:15 +01002554/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002555static int
2556acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2557{
2558 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002559 temp_pattern.data.integer = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002560 if (ts != NULL) {
2561 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2562 if (!ptr)
2563 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002564 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002565 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2566 }
2567 return 1;
2568}
2569
Willy Tarreaua5e37562011-12-16 17:06:15 +01002570/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002571 * the configured period.
2572 */
2573static int
Willy Tarreau56123282010-08-06 19:06:56 +02002574acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2575 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002576{
Willy Tarreau56123282010-08-06 19:06:56 +02002577 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002578 return 0;
2579
Willy Tarreau56123282010-08-06 19:06:56 +02002580 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002581}
2582
Willy Tarreaua5e37562011-12-16 17:06:15 +01002583/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002584 * the configured period.
2585 */
2586static int
Willy Tarreau56123282010-08-06 19:06:56 +02002587acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2588 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002589{
Willy Tarreau56123282010-08-06 19:06:56 +02002590 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002591 return 0;
2592
Willy Tarreau56123282010-08-06 19:06:56 +02002593 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002594}
2595
Willy Tarreaua5e37562011-12-16 17:06:15 +01002596/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002597 * table pointed to by expr, over the configured period.
2598 */
2599static int
2600acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2601 struct acl_expr *expr, struct acl_test *test)
2602{
2603 struct stktable_key *key;
2604
David du Colombier4f92d322011-03-24 11:09:31 +01002605 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002606 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002607 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002608
2609 if (expr->arg_len)
2610 px = find_stktable(expr->arg.str);
2611
2612 if (!px)
2613 return 0; /* table not found */
2614
2615 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2616}
2617
Willy Tarreaua5e37562011-12-16 17:06:15 +01002618/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002619 * in the table pointed to by expr, after updating it.
2620 */
2621static int
2622acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2623 struct acl_expr *expr, struct acl_test *test)
2624{
2625 struct stksess *ts;
2626 struct stktable_key *key;
2627 void *ptr;
2628
David du Colombier4f92d322011-03-24 11:09:31 +01002629 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002630 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002631 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002632
2633 if (expr->arg_len)
2634 px = find_stktable(expr->arg.str);
2635
2636 if (!px)
2637 return 0; /* table not found */
2638
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002639 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2640 /* entry does not exist and could not be created */
2641 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002642
2643 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2644 if (!ptr)
2645 return 0; /* parameter not stored in this table */
2646
Willy Tarreaua5e37562011-12-16 17:06:15 +01002647 temp_pattern.data.integer = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002648 test->flags = ACL_TEST_F_VOL_TEST;
2649 return 1;
2650}
2651
Willy Tarreaua5e37562011-12-16 17:06:15 +01002652/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002653static int
2654acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2655{
2656 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002657 temp_pattern.data.integer = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002658
2659 if (ts != NULL) {
2660 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2661 if (!ptr)
2662 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002663 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002664 }
2665 return 1;
2666}
2667
Willy Tarreaua5e37562011-12-16 17:06:15 +01002668/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002669static int
Willy Tarreau56123282010-08-06 19:06:56 +02002670acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2671 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002672{
Willy Tarreau56123282010-08-06 19:06:56 +02002673 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002674 return 0;
2675
Willy Tarreau56123282010-08-06 19:06:56 +02002676 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002677}
2678
Willy Tarreaua5e37562011-12-16 17:06:15 +01002679/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002680static int
Willy Tarreau56123282010-08-06 19:06:56 +02002681acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2682 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002683{
Willy Tarreau56123282010-08-06 19:06:56 +02002684 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002685 return 0;
2686
Willy Tarreau56123282010-08-06 19:06:56 +02002687 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002688}
2689
Willy Tarreaua5e37562011-12-16 17:06:15 +01002690/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002691 * address in the table pointed to by expr.
2692 */
2693static int
2694acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2695 struct acl_expr *expr, struct acl_test *test)
2696{
Willy Tarreau38285c12010-06-18 16:35:43 +02002697 struct stktable_key *key;
2698
David du Colombier4f92d322011-03-24 11:09:31 +01002699 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002700 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002701 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002702
2703 if (expr->arg_len)
2704 px = find_stktable(expr->arg.str);
2705
2706 if (!px)
2707 return 0; /* table not found */
2708
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002709 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002710}
2711
Willy Tarreaua5e37562011-12-16 17:06:15 +01002712/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002713static int
2714acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2715{
2716 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002717 temp_pattern.data.integer = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002718 if (ts != NULL) {
2719 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2720 if (!ptr)
2721 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002722 temp_pattern.data.integer = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002723 }
2724 return 1;
2725}
2726
Willy Tarreaua5e37562011-12-16 17:06:15 +01002727/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002728static int
Willy Tarreau56123282010-08-06 19:06:56 +02002729acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2730 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002731{
Willy Tarreau56123282010-08-06 19:06:56 +02002732 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002733 return 0;
2734
Willy Tarreau56123282010-08-06 19:06:56 +02002735 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002736}
2737
Willy Tarreaua5e37562011-12-16 17:06:15 +01002738/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002739static int
Willy Tarreau56123282010-08-06 19:06:56 +02002740acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2741 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002742{
Willy Tarreau56123282010-08-06 19:06:56 +02002743 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002744 return 0;
2745
Willy Tarreau56123282010-08-06 19:06:56 +02002746 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002747}
2748
Willy Tarreaua5e37562011-12-16 17:06:15 +01002749/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002750 * address in the table pointed to by expr.
2751 */
2752static int
2753acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2754 struct acl_expr *expr, struct acl_test *test)
2755{
2756 struct stktable_key *key;
2757
David du Colombier4f92d322011-03-24 11:09:31 +01002758 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002759 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002760 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002761
2762 if (expr->arg_len)
2763 px = find_stktable(expr->arg.str);
2764
2765 if (!px)
2766 return 0; /* table not found */
2767
2768 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2769}
2770
Willy Tarreaua5e37562011-12-16 17:06:15 +01002771/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002772static int
2773acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2774{
2775 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002776 temp_pattern.data.integer = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002777 if (ts != NULL) {
2778 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2779 if (!ptr)
2780 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002781 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002782 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2783 }
2784 return 1;
2785}
2786
Willy Tarreaua5e37562011-12-16 17:06:15 +01002787/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002788 * the configured period.
2789 */
2790static int
Willy Tarreau56123282010-08-06 19:06:56 +02002791acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2792 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002793{
Willy Tarreau56123282010-08-06 19:06:56 +02002794 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002795 return 0;
2796
Willy Tarreau56123282010-08-06 19:06:56 +02002797 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002798}
2799
Willy Tarreaua5e37562011-12-16 17:06:15 +01002800/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002801 * the configured period.
2802 */
2803static int
Willy Tarreau56123282010-08-06 19:06:56 +02002804acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2805 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002806{
Willy Tarreau56123282010-08-06 19:06:56 +02002807 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808 return 0;
2809
Willy Tarreau56123282010-08-06 19:06:56 +02002810 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002811}
2812
Willy Tarreaua5e37562011-12-16 17:06:15 +01002813/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002814 * table pointed to by expr, over the configured period.
2815 */
2816static int
2817acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2818 struct acl_expr *expr, struct acl_test *test)
2819{
2820 struct stktable_key *key;
2821
David du Colombier4f92d322011-03-24 11:09:31 +01002822 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002823 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002824 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002825
2826 if (expr->arg_len)
2827 px = find_stktable(expr->arg.str);
2828
2829 if (!px)
2830 return 0; /* table not found */
2831
2832 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2833}
2834
Willy Tarreaua5e37562011-12-16 17:06:15 +01002835/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002836static int
2837acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2838{
2839 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002840 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002841 if (ts != NULL) {
2842 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2843 if (!ptr)
2844 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002845 temp_pattern.data.integer = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002846 }
2847 return 1;
2848}
2849
Willy Tarreaua5e37562011-12-16 17:06:15 +01002850/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002851static int
Willy Tarreau56123282010-08-06 19:06:56 +02002852acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2853 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002854{
Willy Tarreau56123282010-08-06 19:06:56 +02002855 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002856 return 0;
2857
Willy Tarreau56123282010-08-06 19:06:56 +02002858 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002859}
2860
Willy Tarreaua5e37562011-12-16 17:06:15 +01002861/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002862static int
Willy Tarreau56123282010-08-06 19:06:56 +02002863acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2864 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002865{
Willy Tarreau56123282010-08-06 19:06:56 +02002866 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002867 return 0;
2868
Willy Tarreau56123282010-08-06 19:06:56 +02002869 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002870}
2871
Willy Tarreaua5e37562011-12-16 17:06:15 +01002872/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002873 * address in the table pointed to by expr.
2874 */
2875static int
2876acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002877 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002878{
2879 struct stktable_key *key;
2880
David du Colombier4f92d322011-03-24 11:09:31 +01002881 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002882 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002883 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002884
2885 if (expr->arg_len)
2886 px = find_stktable(expr->arg.str);
2887
2888 if (!px)
2889 return 0; /* table not found */
2890
2891 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2892}
2893
Willy Tarreaua5e37562011-12-16 17:06:15 +01002894/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002895static int
2896acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2897{
2898 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002899 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002900 if (ts != NULL) {
2901 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2902 if (!ptr)
2903 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002904 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002905 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2906 }
2907 return 1;
2908}
2909
Willy Tarreaua5e37562011-12-16 17:06:15 +01002910/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002911 * the configured period.
2912 */
2913static int
Willy Tarreau56123282010-08-06 19:06:56 +02002914acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2915 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002916{
Willy Tarreau56123282010-08-06 19:06:56 +02002917 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002918 return 0;
2919
Willy Tarreau56123282010-08-06 19:06:56 +02002920 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002921}
2922
Willy Tarreaua5e37562011-12-16 17:06:15 +01002923/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002924 * the configured period.
2925 */
2926static int
Willy Tarreau56123282010-08-06 19:06:56 +02002927acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2928 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002929{
Willy Tarreau56123282010-08-06 19:06:56 +02002930 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002931 return 0;
2932
Willy Tarreau56123282010-08-06 19:06:56 +02002933 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002934}
2935
Willy Tarreaua5e37562011-12-16 17:06:15 +01002936/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937 * table pointed to by expr, over the configured period.
2938 */
2939static int
2940acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002941 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002942{
2943 struct stktable_key *key;
2944
David du Colombier4f92d322011-03-24 11:09:31 +01002945 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002946 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002947 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002948
2949 if (expr->arg_len)
2950 px = find_stktable(expr->arg.str);
2951
2952 if (!px)
2953 return 0; /* table not found */
2954
2955 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2956}
2957
Willy Tarreaua5e37562011-12-16 17:06:15 +01002958/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959static int
2960acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2961{
2962 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002963 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002964 if (ts != NULL) {
2965 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2966 if (!ptr)
2967 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002968 temp_pattern.data.integer = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002969 }
2970 return 1;
2971}
2972
Willy Tarreaua5e37562011-12-16 17:06:15 +01002973/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002974static int
Willy Tarreau56123282010-08-06 19:06:56 +02002975acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2976 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002977{
Willy Tarreau56123282010-08-06 19:06:56 +02002978 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002979 return 0;
2980
Willy Tarreau56123282010-08-06 19:06:56 +02002981 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002982}
2983
Willy Tarreaua5e37562011-12-16 17:06:15 +01002984/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002985static int
Willy Tarreau56123282010-08-06 19:06:56 +02002986acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2987 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002988{
Willy Tarreau56123282010-08-06 19:06:56 +02002989 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002990 return 0;
2991
Willy Tarreau56123282010-08-06 19:06:56 +02002992 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993}
2994
Willy Tarreaua5e37562011-12-16 17:06:15 +01002995/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002996 * address in the table pointed to by expr.
2997 */
2998static int
2999acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003000 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001{
3002 struct stktable_key *key;
3003
David du Colombier4f92d322011-03-24 11:09:31 +01003004 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003005 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003006 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007
3008 if (expr->arg_len)
3009 px = find_stktable(expr->arg.str);
3010
3011 if (!px)
3012 return 0; /* table not found */
3013
3014 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
3015}
3016
Willy Tarreaua5e37562011-12-16 17:06:15 +01003017/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003018static int
3019acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3020{
3021 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003022 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003023 if (ts != NULL) {
3024 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3025 if (!ptr)
3026 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003027 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003028 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3029 }
3030 return 1;
3031}
3032
Willy Tarreaua5e37562011-12-16 17:06:15 +01003033/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003034 * the configured period.
3035 */
3036static int
Willy Tarreau56123282010-08-06 19:06:56 +02003037acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3038 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003039{
Willy Tarreau56123282010-08-06 19:06:56 +02003040 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003041 return 0;
3042
Willy Tarreau56123282010-08-06 19:06:56 +02003043 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003044}
3045
Willy Tarreaua5e37562011-12-16 17:06:15 +01003046/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003047 * the configured period.
3048 */
3049static int
Willy Tarreau56123282010-08-06 19:06:56 +02003050acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3051 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003052{
Willy Tarreau56123282010-08-06 19:06:56 +02003053 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003054 return 0;
3055
Willy Tarreau56123282010-08-06 19:06:56 +02003056 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003057}
3058
Willy Tarreaua5e37562011-12-16 17:06:15 +01003059/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060 * table pointed to by expr, over the configured period.
3061 */
3062static int
3063acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3064 struct acl_expr *expr, struct acl_test *test)
3065{
3066 struct stktable_key *key;
3067
David du Colombier4f92d322011-03-24 11:09:31 +01003068 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003069 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003070 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003071
3072 if (expr->arg_len)
3073 px = find_stktable(expr->arg.str);
3074
3075 if (!px)
3076 return 0; /* table not found */
3077
3078 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3079}
3080
Willy Tarreaua5e37562011-12-16 17:06:15 +01003081/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003082static int
3083acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
3084{
3085 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003086 temp_pattern.data.integer = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003087
3088 if (ts != NULL) {
3089 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3090 if (!ptr)
3091 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003092 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003093 }
3094 return 1;
3095}
3096
Willy Tarreaua5e37562011-12-16 17:06:15 +01003097/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003098 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003099 */
3100static int
Willy Tarreau56123282010-08-06 19:06:56 +02003101acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3102 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003103{
Willy Tarreau56123282010-08-06 19:06:56 +02003104 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003105 return 0;
3106
Willy Tarreau56123282010-08-06 19:06:56 +02003107 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003108}
3109
Willy Tarreaua5e37562011-12-16 17:06:15 +01003110/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003111 * session's tracked BE counters.
3112 */
3113static int
Willy Tarreau56123282010-08-06 19:06:56 +02003114acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3115 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003116{
Willy Tarreau56123282010-08-06 19:06:56 +02003117 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003118 return 0;
3119
Willy Tarreau56123282010-08-06 19:06:56 +02003120 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003121}
3122
Willy Tarreaua5e37562011-12-16 17:06:15 +01003123/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003124 * address in the table pointed to by expr.
3125 */
3126static int
3127acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3128 struct acl_expr *expr, struct acl_test *test)
3129{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003130 struct stktable_key *key;
3131
David du Colombier4f92d322011-03-24 11:09:31 +01003132 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003133 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003134 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003135
3136 if (expr->arg_len)
3137 px = find_stktable(expr->arg.str);
3138
3139 if (!px)
3140 return 0; /* table not found */
3141
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003142 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
3143}
3144
Willy Tarreaua5e37562011-12-16 17:06:15 +01003145/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003146 * configured period.
3147 */
3148static int
3149acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3150{
3151 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003152 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003153 if (ts != NULL) {
3154 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3155 if (!ptr)
3156 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003157 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003158 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3159 }
3160 return 1;
3161}
3162
Willy Tarreaua5e37562011-12-16 17:06:15 +01003163/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003164 * counters over the configured period.
3165 */
3166static int
Willy Tarreau56123282010-08-06 19:06:56 +02003167acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3168 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003169{
Willy Tarreau56123282010-08-06 19:06:56 +02003170 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003171 return 0;
3172
Willy Tarreau56123282010-08-06 19:06:56 +02003173 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003174}
3175
Willy Tarreaua5e37562011-12-16 17:06:15 +01003176/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003177 * counters over the configured period.
3178 */
3179static int
Willy Tarreau56123282010-08-06 19:06:56 +02003180acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3181 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003182{
Willy Tarreau56123282010-08-06 19:06:56 +02003183 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003184 return 0;
3185
Willy Tarreau56123282010-08-06 19:06:56 +02003186 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003187}
3188
Willy Tarreaua5e37562011-12-16 17:06:15 +01003189/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003190 * in the table pointed to by expr, over the configured period.
3191 */
3192static int
3193acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003194 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003195{
3196 struct stktable_key *key;
3197
David du Colombier4f92d322011-03-24 11:09:31 +01003198 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003199 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003200 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003201
3202 if (expr->arg_len)
3203 px = find_stktable(expr->arg.str);
3204
3205 if (!px)
3206 return 0; /* table not found */
3207
3208 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3209}
3210
Willy Tarreaua5e37562011-12-16 17:06:15 +01003211/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003212static int
3213acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3214{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003215 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003216 temp_pattern.data.integer = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003217
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003218 if (ts != NULL) {
3219 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003220 if (!ptr)
3221 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003222 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003224 return 1;
3225}
3226
Willy Tarreaua5e37562011-12-16 17:06:15 +01003227/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003228 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003229 */
3230static int
Willy Tarreau56123282010-08-06 19:06:56 +02003231acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3232 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003233{
Willy Tarreau56123282010-08-06 19:06:56 +02003234 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003235 return 0;
3236
Willy Tarreau56123282010-08-06 19:06:56 +02003237 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003238}
3239
Willy Tarreaua5e37562011-12-16 17:06:15 +01003240/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003241 * tracked BE counters.
3242 */
3243static int
Willy Tarreau56123282010-08-06 19:06:56 +02003244acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3245 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003246{
Willy Tarreau56123282010-08-06 19:06:56 +02003247 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003248 return 0;
3249
Willy Tarreau56123282010-08-06 19:06:56 +02003250 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003251}
3252
Willy Tarreaua5e37562011-12-16 17:06:15 +01003253/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003254 * the table pointed to by expr.
3255 */
3256static int
3257acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3258 struct acl_expr *expr, struct acl_test *test)
3259{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003260 struct stktable_key *key;
3261
David du Colombier4f92d322011-03-24 11:09:31 +01003262 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003263 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003264 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003265
3266 if (expr->arg_len)
3267 px = find_stktable(expr->arg.str);
3268
3269 if (!px)
3270 return 0; /* table not found */
3271
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003272 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003273}
3274
Willy Tarreaua5e37562011-12-16 17:06:15 +01003275/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003276 * configured period.
3277 */
3278static int
3279acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3280{
3281 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003282 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003283 if (ts != NULL) {
3284 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3285 if (!ptr)
3286 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003287 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003288 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3289 }
3290 return 1;
3291}
3292
Willy Tarreaua5e37562011-12-16 17:06:15 +01003293/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003294 * over the configured period.
3295 */
3296static int
Willy Tarreau56123282010-08-06 19:06:56 +02003297acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3298 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003299{
Willy Tarreau56123282010-08-06 19:06:56 +02003300 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003301 return 0;
3302
Willy Tarreau56123282010-08-06 19:06:56 +02003303 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003304}
3305
Willy Tarreaua5e37562011-12-16 17:06:15 +01003306/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003307 * over the configured period.
3308 */
3309static int
Willy Tarreau56123282010-08-06 19:06:56 +02003310acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3311 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003312{
Willy Tarreau56123282010-08-06 19:06:56 +02003313 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003314 return 0;
3315
Willy Tarreau56123282010-08-06 19:06:56 +02003316 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003317}
3318
Willy Tarreaua5e37562011-12-16 17:06:15 +01003319/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003320 * the table pointed to by expr, over the configured period.
3321 */
3322static int
3323acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003324 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003325{
3326 struct stktable_key *key;
3327
David du Colombier4f92d322011-03-24 11:09:31 +01003328 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003329 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003330 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003331
3332 if (expr->arg_len)
3333 px = find_stktable(expr->arg.str);
3334
3335 if (!px)
3336 return 0; /* table not found */
3337
3338 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3339}
3340
Willy Tarreaua5e37562011-12-16 17:06:15 +01003341/* set temp integer to the number of used entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003342static int
3343acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3344 struct acl_expr *expr, struct acl_test *test)
3345{
3346 if (expr->arg_len)
3347 px = find_stktable(expr->arg.str);
3348
3349 if (!px)
3350 return 0; /* table not found */
3351
3352 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003353 temp_pattern.data.integer = px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003354 return 1;
3355}
3356
Willy Tarreaua5e37562011-12-16 17:06:15 +01003357/* set temp integer to the number of free entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003358static int
3359acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3360 struct acl_expr *expr, struct acl_test *test)
3361{
3362 if (expr->arg_len)
3363 px = find_stktable(expr->arg.str);
3364
3365 if (!px)
3366 return 0; /* table not found */
3367
3368 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003369 temp_pattern.data.integer = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003370 return 1;
3371}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003372
3373/* Note: must not be declared <const> as its list will be overwritten */
3374static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003375 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3376 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3377 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3378 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3379 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3380 { "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 +02003381 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3382 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3383 { "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 +02003384 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3385 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3386 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3387 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3388 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3389 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3390 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3391 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3392 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3393 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3394 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3395 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3396 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3397 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3398 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3399 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3400 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3401 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3402 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3403 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3404 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3405 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3406 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3407 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3408 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3409 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3410 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3411 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3412 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3413 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3414 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3415 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3416 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3417 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3418 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3419 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3420 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3421 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3422 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3423 { "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 +02003424 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3425 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003426 { NULL, NULL, NULL, NULL },
3427}};
3428
3429
Willy Tarreau56123282010-08-06 19:06:56 +02003430/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003431 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3432 * <prm> struct is fed with the table name if any. If unspecified, the caller
3433 * will assume that the current proxy's table is used.
3434 */
3435int parse_track_counters(char **args, int *arg,
3436 int section_type, struct proxy *curpx,
3437 struct track_ctr_prm *prm,
3438 struct proxy *defpx, char *err, int errlen)
3439{
3440 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003441 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003442
Willy Tarreau56123282010-08-06 19:06:56 +02003443 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003444 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003445 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003446 */
3447 while (args[*arg]) {
3448 if (strcmp(args[*arg], "src") == 0) {
3449 prm->type = STKTABLE_TYPE_IP;
3450 pattern_type = 1;
3451 }
3452 else if (strcmp(args[*arg], "table") == 0) {
3453 if (!args[*arg + 1]) {
3454 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003455 "missing table for %s in %s '%s'.",
3456 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003457 return -1;
3458 }
3459 /* we copy the table name for now, it will be resolved later */
3460 prm->table.n = strdup(args[*arg + 1]);
3461 (*arg)++;
3462 }
3463 else {
3464 /* unhandled keywords are handled by the caller */
3465 break;
3466 }
3467 (*arg)++;
3468 }
3469
3470 if (!pattern_type) {
3471 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003472 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3473 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003474 return -1;
3475 }
3476
3477 return 0;
3478}
3479
Willy Tarreau8b22a712010-06-18 17:46:06 +02003480__attribute__((constructor))
3481static void __session_init(void)
3482{
3483 acl_register_keywords(&acl_kws);
3484}
3485
Willy Tarreaubaaee002006-06-26 02:48:02 +02003486/*
3487 * Local variables:
3488 * c-indent-level: 8
3489 * c-basic-offset: 8
3490 * End:
3491 */