blob: e5b76eb3a222441b9c59419cdd7b4836f4d7e869 [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 Tarreau9e000c62011-03-10 14:03:36 +0100172 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200173 s->si[0].exp = TICK_ETERNITY;
174 s->si[0].flags = SI_FL_NONE;
175
176 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
177 s->si[0].flags |= SI_FL_INDEP_STR;
178
179 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
180 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
181
182 /* add the various callbacks */
183 stream_sock_prepare_interface(&s->si[0]);
184
185 /* pre-initialize the other side's stream interface to an INIT state. The
186 * callbacks will be initialized before attempting to connect.
187 */
188 s->si[1].fd = -1; /* just to help with debugging */
189 s->si[1].owner = t;
190 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
191 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200192 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200193 s->si[1].err_loc = NULL;
194 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200195 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100196 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197 s->si[1].shutr = stream_int_shutr;
198 s->si[1].shutw = stream_int_shutw;
199 s->si[1].exp = TICK_ETERNITY;
200 s->si[1].flags = SI_FL_NONE;
201
202 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
203 s->si[1].flags |= SI_FL_INDEP_STR;
204
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200205 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100206 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 s->pend_pos = NULL;
208
209 /* init store persistence */
210 s->store_count = 0;
211
212 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100213 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215
216 txn = &s->txn;
217 /* Those variables will be checked and freed if non-NULL in
218 * session.c:session_free(). It is important that they are
219 * properly initialized.
220 */
221 txn->sessid = NULL;
222 txn->srv_cookie = NULL;
223 txn->cli_cookie = NULL;
224 txn->uri = NULL;
225 txn->req.cap = NULL;
226 txn->rsp.cap = NULL;
227 txn->hdr_idx.v = NULL;
228 txn->hdr_idx.size = txn->hdr_idx.used = 0;
229
230 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
231 goto out_free_task; /* no memory */
232
233 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
234 goto out_free_req; /* no memory */
235
236 /* initialize the request buffer */
237 s->req->size = global.tune.bufsize;
238 buffer_init(s->req);
239 s->req->prod = &s->si[0];
240 s->req->cons = &s->si[1];
241 s->si[0].ib = s->si[1].ob = s->req;
242 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
243
244 /* activate default analysers enabled for this listener */
245 s->req->analysers = l->analysers;
246
247 s->req->wto = TICK_ETERNITY;
248 s->req->rto = TICK_ETERNITY;
249 s->req->rex = TICK_ETERNITY;
250 s->req->wex = TICK_ETERNITY;
251 s->req->analyse_exp = TICK_ETERNITY;
252
253 /* initialize response buffer */
254 s->rep->size = global.tune.bufsize;
255 buffer_init(s->rep);
256 s->rep->prod = &s->si[1];
257 s->rep->cons = &s->si[0];
258 s->si[0].ob = s->si[1].ib = s->rep;
259 s->rep->analysers = 0;
260
Willy Tarreau96e31212011-05-30 18:10:30 +0200261 if (s->fe->options2 & PR_O2_NODELAY) {
262 s->req->flags |= BF_NEVER_WAIT;
263 s->rep->flags |= BF_NEVER_WAIT;
264 }
265
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200266 s->rep->rto = TICK_ETERNITY;
267 s->rep->wto = TICK_ETERNITY;
268 s->rep->rex = TICK_ETERNITY;
269 s->rep->wex = TICK_ETERNITY;
270 s->rep->analyse_exp = TICK_ETERNITY;
271
272 /* finish initialization of the accepted file descriptor */
273 fd_insert(cfd);
274 fdtab[cfd].owner = &s->si[0];
275 fdtab[cfd].state = FD_STREADY;
276 fdtab[cfd].flags = 0;
277 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
278 fdtab[cfd].cb[DIR_RD].b = s->req;
279 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
280 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200281 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.from;
282 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200283 EV_FD_SET(cfd, DIR_RD);
284
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100285 if (p->accept && (ret = p->accept(s)) <= 0) {
286 /* Either we had an unrecoverable error (<0) or work is
287 * finished (=0, eg: monitoring), in both situations,
288 * we can release everything and close.
289 */
290 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200291 }
292
293 /* it is important not to call the wakeup function directly but to
294 * pass through task_wakeup(), because this one knows how to apply
295 * priorities to tasks.
296 */
297 task_wakeup(t, TASK_WOKEN_INIT);
298 return 1;
299
300 /* Error unrolling */
301 out_free_rep:
302 pool_free2(pool2_buffer, s->rep);
303 out_free_req:
304 pool_free2(pool2_buffer, s->req);
305 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200306 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200307 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200308 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200309 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200310 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100311 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200312 pool_free2(pool2_session, s);
313 out_close:
Willy Tarreau2b154922011-07-22 17:36:27 +0200314 if (ret < 0 && s->fe->mode == PR_MODE_HTTP) {
315 /* critical error, no more memory, try to emit a 500 response */
316 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
317 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
318 }
319
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100320 if (fdtab[cfd].state != FD_STCLOSE)
321 fd_delete(cfd);
322 else
323 close(cfd);
324 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200325}
326
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327/*
328 * frees the context associated to a session. It must have been removed first.
329 */
Simon Hormandec5be42011-06-08 09:19:07 +0900330static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100332 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200333 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100334 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200335 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100336
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337 if (s->pend_pos)
338 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100339
Willy Tarreau827aee92011-03-10 16:55:02 +0100340 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100341 if (s->flags & SN_CURR_SESS) {
342 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100343 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100344 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100345 if (may_dequeue_tasks(target_srv(&s->target), s->be))
346 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100347 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100348
Willy Tarreau7c669d72008-06-20 15:04:11 +0200349 if (unlikely(s->srv_conn)) {
350 /* the session still has a reserved slot on a server, but
351 * it should normally be only the same as the one above,
352 * so this should not happen in fact.
353 */
354 sess_change_server(s, NULL);
355 }
356
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100357 if (s->req->pipe)
358 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100359
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100360 if (s->rep->pipe)
361 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100362
Willy Tarreau48d63db2008-08-03 17:41:33 +0200363 pool_free2(pool2_buffer, s->req);
364 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
Willy Tarreau46023632010-01-07 22:51:47 +0100366 http_end_txn(s);
367
Willy Tarreaua4cda672010-06-06 18:28:49 +0200368 for (i = 0; i < s->store_count; i++) {
369 if (!s->store[i].ts)
370 continue;
371 stksess_free(s->store[i].table, s->store[i].ts);
372 s->store[i].ts = NULL;
373 }
374
Willy Tarreau34eb6712011-10-24 18:15:04 +0200375 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200376 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100377 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
378 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100380
Willy Tarreau56123282010-08-06 19:06:56 +0200381 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200382 session_store_counters(s);
383
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100384 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100385 /* we have to unlink all watchers. We must not relink them if
386 * this session was the last one in the list.
387 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100388 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100389 LIST_INIT(&bref->users);
390 if (s->list.n != &sessions)
391 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100392 bref->ref = s->list.n;
393 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100394 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200395 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200396
397 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200398 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200399 pool_flush2(pool2_buffer);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200400 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200401 pool_flush2(pool2_requri);
402 pool_flush2(pool2_capture);
403 pool_flush2(pool2_session);
404 pool_flush2(fe->req_cap_pool);
405 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200406 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200407}
408
409
410/* perform minimal intializations, report 0 in case of error, 1 if OK. */
411int init_session()
412{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100413 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200414 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
415 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416}
417
Willy Tarreau30e71012007-11-26 20:15:35 +0100418void session_process_counters(struct session *s)
419{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100420 unsigned long long bytes;
421
Willy Tarreau30e71012007-11-26 20:15:35 +0100422 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100423 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100424 s->logs.bytes_in = s->req->total;
425 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100426 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100427
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100428 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100429
Willy Tarreau827aee92011-03-10 16:55:02 +0100430 if (target_srv(&s->target))
431 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200432
433 if (s->listener->counters)
434 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435
Willy Tarreau56123282010-08-06 19:06:56 +0200436 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200437 void *ptr;
438
Willy Tarreau56123282010-08-06 19:06:56 +0200439 ptr = stktable_data_ptr(s->stkctr2_table,
440 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200441 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200442 if (ptr)
443 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200444
Willy Tarreau56123282010-08-06 19:06:56 +0200445 ptr = stktable_data_ptr(s->stkctr2_table,
446 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200447 STKTABLE_DT_BYTES_IN_RATE);
448 if (ptr)
449 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200450 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 }
452
Willy Tarreau56123282010-08-06 19:06:56 +0200453 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200454 void *ptr;
455
Willy Tarreau56123282010-08-06 19:06:56 +0200456 ptr = stktable_data_ptr(s->stkctr1_table,
457 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200458 STKTABLE_DT_BYTES_IN_CNT);
459 if (ptr)
460 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
461
Willy Tarreau56123282010-08-06 19:06:56 +0200462 ptr = stktable_data_ptr(s->stkctr1_table,
463 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200464 STKTABLE_DT_BYTES_IN_RATE);
465 if (ptr)
466 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200467 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200468 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100469 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470 }
471
Willy Tarreau30e71012007-11-26 20:15:35 +0100472 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100473 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100474 s->logs.bytes_out = s->rep->total;
475 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100476 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100478 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479
Willy Tarreau827aee92011-03-10 16:55:02 +0100480 if (target_srv(&s->target))
481 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200482
483 if (s->listener->counters)
484 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200485
Willy Tarreau56123282010-08-06 19:06:56 +0200486 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200487 void *ptr;
488
Willy Tarreau56123282010-08-06 19:06:56 +0200489 ptr = stktable_data_ptr(s->stkctr2_table,
490 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200491 STKTABLE_DT_BYTES_OUT_CNT);
492 if (ptr)
493 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
494
Willy Tarreau56123282010-08-06 19:06:56 +0200495 ptr = stktable_data_ptr(s->stkctr2_table,
496 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200497 STKTABLE_DT_BYTES_OUT_RATE);
498 if (ptr)
499 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200500 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200501 }
502
Willy Tarreau56123282010-08-06 19:06:56 +0200503 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504 void *ptr;
505
Willy Tarreau56123282010-08-06 19:06:56 +0200506 ptr = stktable_data_ptr(s->stkctr1_table,
507 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200508 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200509 if (ptr)
510 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511
Willy Tarreau56123282010-08-06 19:06:56 +0200512 ptr = stktable_data_ptr(s->stkctr1_table,
513 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514 STKTABLE_DT_BYTES_OUT_RATE);
515 if (ptr)
516 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200517 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200518 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100519 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520 }
521}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523/* This function is called with (si->state == SI_ST_CON) meaning that a
524 * connection was attempted and that the file descriptor is already allocated.
525 * We must check for establishment, error and abort. Possible output states
526 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
527 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
528 * otherwise 1.
529 */
Simon Hormandec5be42011-06-08 09:19:07 +0900530static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100531{
532 struct buffer *req = si->ob;
533 struct buffer *rep = si->ib;
534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 /* If we got an error, or if nothing happened and the connection timed
536 * out, we must give up. The CER state handler will take care of retry
537 * attempts and error reports.
538 */
539 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100540 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200542 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100543 fd_delete(si->fd);
544
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200545 if (si->release)
546 si->release(si);
547
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548 if (si->err_type)
549 return 0;
550
Willy Tarreau827aee92011-03-10 16:55:02 +0100551 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 if (si->flags & SI_FL_ERR)
553 si->err_type = SI_ET_CONN_ERR;
554 else
555 si->err_type = SI_ET_CONN_TO;
556 return 0;
557 }
558
559 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200560 if (unlikely((rep->flags & BF_SHUTW) ||
561 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200562 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 s->be->options & PR_O_ABRT_CLOSE)))) {
564 /* give up */
565 si->shutw(si);
566 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100567 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200568 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100569 if (s->srv_error)
570 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 return 1;
572 }
573
574 /* we need to wait a bit more if there was no activity either */
575 if (!(req->flags & BF_WRITE_ACTIVITY))
576 return 1;
577
578 /* OK, this means that a connection succeeded. The caller will be
579 * responsible for handling the transition from CON to EST.
580 */
581 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100582 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 si->state = SI_ST_EST;
584 si->err_type = SI_ET_NONE;
585 si->err_loc = NULL;
586 return 1;
587}
588
589/* This function is called with (si->state == SI_ST_CER) meaning that a
590 * previous connection attempt has failed and that the file descriptor
591 * has already been released. Possible causes include asynchronous error
592 * notification and time out. Possible output states are SI_ST_CLO when
593 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
594 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
595 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
596 * marked as in error state. It returns 0.
597 */
Simon Hormandec5be42011-06-08 09:19:07 +0900598static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599{
600 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100601 if (target_srv(&s->target)) {
602 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 if (s->flags & SN_CURR_SESS) {
605 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 }
609
610 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200611 si->conn_retries--;
612 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 if (!si->err_type) {
614 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616 }
617
Willy Tarreau827aee92011-03-10 16:55:02 +0100618 if (target_srv(&s->target))
619 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100620 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100621 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100622 if (may_dequeue_tasks(target_srv(&s->target), s->be))
623 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100624
625 /* shutw is enough so stop a connecting socket */
626 si->shutw(si);
627 si->ob->flags |= BF_WRITE_ERROR;
628 si->ib->flags |= BF_READ_ERROR;
629
630 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100631 if (s->srv_error)
632 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 return 0;
634 }
635
636 /* If the "redispatch" option is set on the backend, we are allowed to
637 * retry on another server for the last retry. In order to achieve this,
638 * we must mark the session unassigned, and eventually clear the DIRECT
639 * bit to ignore any persistence cookie. We won't count a retry nor a
640 * redispatch yet, because this will depend on what server is selected.
641 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100642 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100643 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100644 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100645 if (may_dequeue_tasks(target_srv(&s->target), s->be))
646 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647
648 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649 si->state = SI_ST_REQ;
650 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 if (target_srv(&s->target))
652 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100653 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 si->state = SI_ST_ASS;
655 }
656
657 if (si->flags & SI_FL_ERR) {
658 /* The error was an asynchronous connection error, and we will
659 * likely have to retry connecting to the same server, most
660 * likely leading to the same result. To avoid this, we wait
661 * one second before retrying.
662 */
663
664 if (!si->err_type)
665 si->err_type = SI_ET_CONN_ERR;
666
667 si->state = SI_ST_TAR;
668 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
669 return 0;
670 }
671 return 0;
672}
673
674/*
675 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200676 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
677 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 */
Simon Hormandec5be42011-06-08 09:19:07 +0900679static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680{
681 struct buffer *req = si->ob;
682 struct buffer *rep = si->ib;
683
Willy Tarreau827aee92011-03-10 16:55:02 +0100684 if (target_srv(&s->target))
685 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100686
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688 /* if the user wants to log as soon as possible, without counting
689 * bytes from the server, then this is the right moment. */
690 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
691 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100692 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 }
695 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
697 /* reset hdr_idx which was already initialized by the request.
698 * right now, the http parser does it.
699 * hdr_idx_init(&s->txn.hdr_idx);
700 */
701 }
702
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200703 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200705 if (si->connect) {
706 /* real connections have timeouts */
707 req->wto = s->be->timeout.server;
708 rep->rto = s->be->timeout.server;
709 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 req->wex = TICK_ETERNITY;
711}
712
713/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
714 * Other input states are simply ignored.
715 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
716 * Flags must have previously been updated for timeouts and other conditions.
717 */
Simon Hormandec5be42011-06-08 09:19:07 +0900718static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719{
Willy Tarreau827aee92011-03-10 16:55:02 +0100720 struct server *srv = target_srv(&s->target);
721
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 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",
723 now_ms, __FUNCTION__,
724 s,
725 s->req, s->rep,
726 s->req->rex, s->rep->wex,
727 s->req->flags, s->rep->flags,
728 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
729
730 if (si->state == SI_ST_ASS) {
731 /* Server assigned to connection request, we have to try to connect now */
732 int conn_err;
733
734 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100735 srv = target_srv(&s->target);
736
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 if (conn_err == SN_ERR_NONE) {
738 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100739 if (srv)
740 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 return;
742 }
743
744 /* We have received a synchronous error. We might have to
745 * abort, retry immediately or redispatch.
746 */
747 if (conn_err == SN_ERR_INTERNAL) {
748 if (!si->err_type) {
749 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100750 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 }
752
Willy Tarreau827aee92011-03-10 16:55:02 +0100753 if (srv)
754 srv_inc_sess_ctr(srv);
755 if (srv)
756 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100757 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758
759 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100760 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 if (may_dequeue_tasks(srv, s->be))
762 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763
764 /* Failed and not retryable. */
765 si->shutr(si);
766 si->shutw(si);
767 si->ob->flags |= BF_WRITE_ERROR;
768
769 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
770
771 /* no session was ever accounted for this server */
772 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100773 if (s->srv_error)
774 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100775 return;
776 }
777
778 /* We are facing a retryable error, but we don't want to run a
779 * turn-around now, as the problem is likely a source port
780 * allocation problem, so we want to retry now.
781 */
782 si->state = SI_ST_CER;
783 si->flags &= ~SI_FL_ERR;
784 sess_update_st_cer(s, si);
785 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
786 return;
787 }
788 else if (si->state == SI_ST_QUE) {
789 /* connection request was queued, check for any update */
790 if (!s->pend_pos) {
791 /* The connection is not in the queue anymore. Either
792 * we have a server connection slot available and we
793 * go directly to the assigned state, or we need to
794 * load-balance first and go to the INI state.
795 */
796 si->exp = TICK_ETERNITY;
797 if (unlikely(!(s->flags & SN_ASSIGNED)))
798 si->state = SI_ST_REQ;
799 else {
800 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
801 si->state = SI_ST_ASS;
802 }
803 return;
804 }
805
806 /* Connection request still in queue... */
807 if (si->flags & SI_FL_EXP) {
808 /* ... and timeout expired */
809 si->exp = TICK_ETERNITY;
810 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 if (srv)
812 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100813 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 si->shutr(si);
815 si->shutw(si);
816 si->ob->flags |= BF_WRITE_TIMEOUT;
817 if (!si->err_type)
818 si->err_type = SI_ET_QUEUE_TO;
819 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100820 if (s->srv_error)
821 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 return;
823 }
824
825 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200826 if ((si->ob->flags & (BF_READ_ERROR)) ||
827 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200828 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 /* give up */
830 si->exp = TICK_ETERNITY;
831 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
832 si->shutr(si);
833 si->shutw(si);
834 si->err_type |= SI_ET_QUEUE_ABRT;
835 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100836 if (s->srv_error)
837 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 return;
839 }
840
841 /* Nothing changed */
842 return;
843 }
844 else if (si->state == SI_ST_TAR) {
845 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200846 if ((si->ob->flags & (BF_READ_ERROR)) ||
847 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200848 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100849 /* give up */
850 si->exp = TICK_ETERNITY;
851 si->shutr(si);
852 si->shutw(si);
853 si->err_type |= SI_ET_CONN_ABRT;
854 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100855 if (s->srv_error)
856 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100857 return;
858 }
859
860 if (!(si->flags & SI_FL_EXP))
861 return; /* still in turn-around */
862
863 si->exp = TICK_ETERNITY;
864
865 /* we keep trying on the same server as long as the session is
866 * marked "assigned".
867 * FIXME: Should we force a redispatch attempt when the server is down ?
868 */
869 if (s->flags & SN_ASSIGNED)
870 si->state = SI_ST_ASS;
871 else
872 si->state = SI_ST_REQ;
873 return;
874 }
875}
876
Simon Hormandec5be42011-06-08 09:19:07 +0900877/* Set correct session termination flags in case no analyser has done it. It
878 * also counts a failed request if the server state has not reached the request
879 * stage.
880 */
881static void sess_set_term_flags(struct session *s)
882{
883 if (!(s->flags & SN_FINST_MASK)) {
884 if (s->si[1].state < SI_ST_REQ) {
885
886 s->fe->fe_counters.failed_req++;
887 if (s->listener->counters)
888 s->listener->counters->failed_req++;
889
890 s->flags |= SN_FINST_R;
891 }
892 else if (s->si[1].state == SI_ST_QUE)
893 s->flags |= SN_FINST_Q;
894 else if (s->si[1].state < SI_ST_EST)
895 s->flags |= SN_FINST_C;
896 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
897 s->flags |= SN_FINST_D;
898 else
899 s->flags |= SN_FINST_L;
900 }
901}
902
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903/* This function initiates a server connection request on a stream interface
904 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
905 * indicating that a server has been assigned. It may also return SI_ST_QUE,
906 * or SI_ST_CLO upon error.
907 */
908static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
909 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",
910 now_ms, __FUNCTION__,
911 s,
912 s->req, s->rep,
913 s->req->rex, s->rep->wex,
914 s->req->flags, s->rep->flags,
915 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
916
917 if (si->state != SI_ST_REQ)
918 return;
919
920 /* Try to assign a server */
921 if (srv_redispatch_connect(s) != 0) {
922 /* We did not get a server. Either we queued the
923 * connection request, or we encountered an error.
924 */
925 if (si->state == SI_ST_QUE)
926 return;
927
928 /* we did not get any server, let's check the cause */
929 si->shutr(si);
930 si->shutw(si);
931 si->ob->flags |= BF_WRITE_ERROR;
932 if (!si->err_type)
933 si->err_type = SI_ET_CONN_OTHER;
934 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100935 if (s->srv_error)
936 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937 return;
938 }
939
940 /* The server is assigned */
941 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
942 si->state = SI_ST_ASS;
943}
944
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100946 * if appropriate. The default_backend rule is also considered, then the
947 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200948 * It returns 1 if the processing can continue on next analysers, or zero if it
949 * either needs more data or wants to immediately abort the request.
950 */
Simon Hormandec5be42011-06-08 09:19:07 +0900951static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200952{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200953 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100954
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200955 req->analysers &= ~an_bit;
956 req->analyse_exp = TICK_ETERNITY;
957
958 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
959 now_ms, __FUNCTION__,
960 s,
961 req,
962 req->rex, req->wex,
963 req->flags,
964 req->l,
965 req->analysers);
966
967 /* now check whether we have some switching rules for this request */
968 if (!(s->flags & SN_BE_ASSIGNED)) {
969 struct switching_rule *rule;
970
971 list_for_each_entry(rule, &s->fe->switching_rules, list) {
972 int ret;
973
974 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
975 ret = acl_pass(ret);
976 if (rule->cond->pol == ACL_COND_UNLESS)
977 ret = !ret;
978
979 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200980 if (!session_set_backend(s, rule->be.backend))
981 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200982 break;
983 }
984 }
985
986 /* To ensure correct connection accounting on the backend, we
987 * have to assign one if it was not set (eg: a listen). This
988 * measure also takes care of correctly setting the default
989 * backend if any.
990 */
991 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200992 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
993 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200994 }
995
Willy Tarreaufb356202010-08-03 14:02:05 +0200996 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
997 if (s->fe == s->be) {
998 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200999 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001000 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001001
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001002 /* 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 +01001003 * persistence rule, and report that in the session.
1004 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001005 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001006 int ret = 1;
1007
1008 if (prst_rule->cond) {
1009 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
1010 ret = acl_pass(ret);
1011 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1012 ret = !ret;
1013 }
1014
1015 if (ret) {
1016 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001017 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1018 s->flags |= SN_FORCE_PRST;
1019 } else {
1020 s->flags |= SN_IGNORE_PRST;
1021 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001022 break;
1023 }
1024 }
1025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001026 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001027
1028 sw_failed:
1029 /* immediately abort this request in case of allocation failure */
1030 buffer_abort(s->req);
1031 buffer_abort(s->rep);
1032
1033 if (!(s->flags & SN_ERR_MASK))
1034 s->flags |= SN_ERR_RESOURCE;
1035 if (!(s->flags & SN_FINST_MASK))
1036 s->flags |= SN_FINST_R;
1037
1038 s->txn.status = 500;
1039 s->req->analysers = 0;
1040 s->req->analyse_exp = TICK_ETERNITY;
1041 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001042}
1043
Emeric Brun1d33b292010-01-04 15:47:17 +01001044/* This stream analyser works on a request. It applies all sticking rules on
1045 * it then returns 1. The data must already be present in the buffer otherwise
1046 * they won't match. It always returns 1.
1047 */
Simon Hormandec5be42011-06-08 09:19:07 +09001048static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001049{
1050 struct proxy *px = s->be;
1051 struct sticking_rule *rule;
1052
1053 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1054 now_ms, __FUNCTION__,
1055 s,
1056 req,
1057 req->rex, req->wex,
1058 req->flags,
1059 req->l,
1060 req->analysers);
1061
1062 list_for_each_entry(rule, &px->sticking_rules, list) {
1063 int ret = 1 ;
1064 int i;
1065
1066 for (i = 0; i < s->store_count; i++) {
1067 if (rule->table.t == s->store[i].table)
1068 break;
1069 }
1070
1071 if (i != s->store_count)
1072 continue;
1073
1074 if (rule->cond) {
1075 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1076 ret = acl_pass(ret);
1077 if (rule->cond->pol == ACL_COND_UNLESS)
1078 ret = !ret;
1079 }
1080
1081 if (ret) {
1082 struct stktable_key *key;
1083
Emeric Brun485479d2010-09-23 18:02:19 +02001084 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001085 if (!key)
1086 continue;
1087
1088 if (rule->flags & STK_IS_MATCH) {
1089 struct stksess *ts;
1090
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001091 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001092 if (!(s->flags & SN_ASSIGNED)) {
1093 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001094 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001095
1096 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001097 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1098 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001099 if (node) {
1100 struct server *srv;
1101
1102 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001103 if ((srv->state & SRV_RUNNING) ||
1104 (px->options & PR_O_PERSIST) ||
1105 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001106 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001107 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001108 }
1109 }
1110 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001111 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001112 }
1113 }
1114 if (rule->flags & STK_IS_STORE) {
1115 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1116 struct stksess *ts;
1117
1118 ts = stksess_new(rule->table.t, key);
1119 if (ts) {
1120 s->store[s->store_count].table = rule->table.t;
1121 s->store[s->store_count++].ts = ts;
1122 }
1123 }
1124 }
1125 }
1126 }
1127
1128 req->analysers &= ~an_bit;
1129 req->analyse_exp = TICK_ETERNITY;
1130 return 1;
1131}
1132
1133/* This stream analyser works on a response. It applies all store rules on it
1134 * then returns 1. The data must already be present in the buffer otherwise
1135 * they won't match. It always returns 1.
1136 */
Simon Hormandec5be42011-06-08 09:19:07 +09001137static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001138{
1139 struct proxy *px = s->be;
1140 struct sticking_rule *rule;
1141 int i;
1142
1143 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1144 now_ms, __FUNCTION__,
1145 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001146 rep,
1147 rep->rex, rep->wex,
1148 rep->flags,
1149 rep->l,
1150 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001151
1152 list_for_each_entry(rule, &px->storersp_rules, list) {
1153 int ret = 1 ;
1154 int storereqidx = -1;
1155
1156 for (i = 0; i < s->store_count; i++) {
1157 if (rule->table.t == s->store[i].table) {
1158 if (!(s->store[i].flags))
1159 storereqidx = i;
1160 break;
1161 }
1162 }
1163
1164 if ((i != s->store_count) && (storereqidx == -1))
1165 continue;
1166
1167 if (rule->cond) {
1168 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1169 ret = acl_pass(ret);
1170 if (rule->cond->pol == ACL_COND_UNLESS)
1171 ret = !ret;
1172 }
1173
1174 if (ret) {
1175 struct stktable_key *key;
1176
Emeric Brun485479d2010-09-23 18:02:19 +02001177 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001178 if (!key)
1179 continue;
1180
1181 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001182 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001183 s->store[storereqidx].flags = 1;
1184 }
1185 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1186 struct stksess *ts;
1187
1188 ts = stksess_new(rule->table.t, key);
1189 if (ts) {
1190 s->store[s->store_count].table = rule->table.t;
1191 s->store[s->store_count].flags = 1;
1192 s->store[s->store_count++].ts = ts;
1193 }
1194 }
1195 }
1196 }
1197
1198 /* process store request and store response */
1199 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001200 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001201 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001202
Simon Hormanfa461682011-06-25 09:39:49 +09001203 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1204 stksess_free(s->store[i].table, s->store[i].ts);
1205 s->store[i].ts = NULL;
1206 continue;
1207 }
1208
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001209 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1210 if (ts) {
1211 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001212 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001213 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001214 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001215 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001216 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001217
1218 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001219 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001220 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001221 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001222 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001223
1224 rep->analysers &= ~an_bit;
1225 rep->analyse_exp = TICK_ETERNITY;
1226 return 1;
1227}
1228
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001229/* This macro is very specific to the function below. See the comments in
1230 * process_session() below to understand the logic and the tests.
1231 */
1232#define UPDATE_ANALYSERS(real, list, back, flag) { \
1233 list = (((list) & ~(flag)) | ~(back)) & (real); \
1234 back = real; \
1235 if (!(list)) \
1236 break; \
1237 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1238 continue; \
1239}
1240
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001241/* Processes the client, server, request and response jobs of a session task,
1242 * then puts it back to the wait queue in a clean state, or cleans up its
1243 * resources if it must be deleted. Returns in <next> the date the task wants
1244 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1245 * nothing too many times, the request and response buffers flags are monitored
1246 * and each function is called only if at least another function has changed at
1247 * least one flag it is interested in.
1248 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001249struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001250{
Willy Tarreau827aee92011-03-10 16:55:02 +01001251 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001253 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001254 unsigned int rq_prod_last, rq_cons_last;
1255 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001256 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257
1258 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1259 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1260
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001261 /* this data may be no longer valid, clear it */
1262 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1263
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001264 /* This flag must explicitly be set every time */
1265 s->req->flags &= ~BF_READ_NOEXP;
1266
1267 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001268 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1269 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001270
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001271 /* we don't want the stream interface functions to recursively wake us up */
1272 if (s->req->prod->owner == t)
1273 s->req->prod->flags |= SI_FL_DONT_WAKE;
1274 if (s->req->cons->owner == t)
1275 s->req->cons->flags |= SI_FL_DONT_WAKE;
1276
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001277 /* 1a: Check for low level timeouts if needed. We just set a flag on
1278 * stream interfaces when their timeouts have expired.
1279 */
1280 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1281 stream_int_check_timeouts(&s->si[0]);
1282 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001283
1284 /* check buffer timeouts, and close the corresponding stream interfaces
1285 * for future reads or writes. Note: this will also concern upper layers
1286 * but we do not touch any other flag. We must be careful and correctly
1287 * detect state changes when calling them.
1288 */
1289
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001290 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001291
Willy Tarreau14641402009-12-29 14:49:56 +01001292 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1293 s->req->cons->flags |= SI_FL_NOLINGER;
1294 s->req->cons->shutw(s->req->cons);
1295 }
1296
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001297 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1298 s->req->prod->shutr(s->req->prod);
1299
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001300 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001301
Willy Tarreau14641402009-12-29 14:49:56 +01001302 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1303 s->rep->cons->flags |= SI_FL_NOLINGER;
1304 s->rep->cons->shutw(s->rep->cons);
1305 }
1306
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001307 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1308 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001309 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310
1311 /* 1b: check for low-level errors reported at the stream interface.
1312 * First we check if it's a retryable error (in which case we don't
1313 * want to tell the buffer). Otherwise we report the error one level
1314 * upper by setting flags into the buffers. Note that the side towards
1315 * the client cannot have connect (hence retryable) errors. Also, the
1316 * connection setup code must be able to deal with any type of abort.
1317 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001318 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001319 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1320 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1321 s->si[0].shutr(&s->si[0]);
1322 s->si[0].shutw(&s->si[0]);
1323 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001324 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001325 s->be->be_counters.cli_aborts++;
1326 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001327 if (srv)
1328 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001329 if (!(s->flags & SN_ERR_MASK))
1330 s->flags |= SN_ERR_CLICL;
1331 if (!(s->flags & SN_FINST_MASK))
1332 s->flags |= SN_FINST_D;
1333 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001334 }
1335 }
1336
1337 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1338 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1339 s->si[1].shutr(&s->si[1]);
1340 s->si[1].shutw(&s->si[1]);
1341 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001342 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001343 if (srv)
1344 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001345 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001346 s->be->be_counters.srv_aborts++;
1347 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001348 if (srv)
1349 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001350 if (!(s->flags & SN_ERR_MASK))
1351 s->flags |= SN_ERR_SRVCL;
1352 if (!(s->flags & SN_FINST_MASK))
1353 s->flags |= SN_FINST_D;
1354 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001355 }
1356 /* note: maybe we should process connection errors here ? */
1357 }
1358
1359 if (s->si[1].state == SI_ST_CON) {
1360 /* we were trying to establish a connection on the server side,
1361 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1362 */
1363 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1364 sess_update_st_cer(s, &s->si[1]);
1365 else if (s->si[1].state == SI_ST_EST)
1366 sess_establish(s, &s->si[1]);
1367
1368 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1369 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1370 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1371 */
1372 }
1373
Willy Tarreau815a9b22010-07-27 17:15:12 +02001374 rq_prod_last = s->si[0].state;
1375 rq_cons_last = s->si[1].state;
1376 rp_cons_last = s->si[0].state;
1377 rp_prod_last = s->si[1].state;
1378
1379 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001380 /* Check for connection closure */
1381
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001382 DPRINTF(stderr,
1383 "[%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",
1384 now_ms, __FUNCTION__, __LINE__,
1385 t,
1386 s, s->flags,
1387 s->req, s->rep,
1388 s->req->rex, s->rep->wex,
1389 s->req->flags, s->rep->flags,
1390 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1391 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001392 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001393
1394 /* nothing special to be done on client side */
1395 if (unlikely(s->req->prod->state == SI_ST_DIS))
1396 s->req->prod->state = SI_ST_CLO;
1397
1398 /* When a server-side connection is released, we have to count it and
1399 * check for pending connections on this server.
1400 */
1401 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1402 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001403 srv = target_srv(&s->target);
1404 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001405 if (s->flags & SN_CURR_SESS) {
1406 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001407 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001408 }
1409 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001410 if (may_dequeue_tasks(srv, s->be))
1411 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001412 }
1413 }
1414
1415 /*
1416 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1417 * at this point.
1418 */
1419
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001420 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001421 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001422 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001423 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1424 s->si[0].state != rq_prod_last ||
1425 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001426 unsigned int flags = s->req->flags;
1427
1428 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001429 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001430 unsigned int ana_list;
1431 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001432
Willy Tarreau90deb182010-01-07 00:20:41 +01001433 /* it's up to the analysers to stop new connections,
1434 * disable reading or closing. Note: if an analyser
1435 * disables any of these bits, it is responsible for
1436 * enabling them again when it disables itself, so
1437 * that other analysers are called in similar conditions.
1438 */
1439 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001440 buffer_auto_connect(s->req);
1441 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001442
1443 /* We will call all analysers for which a bit is set in
1444 * s->req->analysers, following the bit order from LSB
1445 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001446 * the list when not needed. Any analyser may return 0
1447 * to break out of the loop, either because of missing
1448 * data to take a decision, or because it decides to
1449 * kill the session. We loop at least once through each
1450 * analyser, and we may loop again if other analysers
1451 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001452 *
1453 * We build a list of analysers to run. We evaluate all
1454 * of these analysers in the order of the lower bit to
1455 * the higher bit. This ordering is very important.
1456 * An analyser will often add/remove other analysers,
1457 * including itself. Any changes to itself have no effect
1458 * on the loop. If it removes any other analysers, we
1459 * want those analysers not to be called anymore during
1460 * this loop. If it adds an analyser that is located
1461 * after itself, we want it to be scheduled for being
1462 * processed during the loop. If it adds an analyser
1463 * which is located before it, we want it to switch to
1464 * it immediately, even if it has already been called
1465 * once but removed since.
1466 *
1467 * In order to achieve this, we compare the analyser
1468 * list after the call with a copy of it before the
1469 * call. The work list is fed with analyser bits that
1470 * appeared during the call. Then we compare previous
1471 * work list with the new one, and check the bits that
1472 * appeared. If the lowest of these bits is lower than
1473 * the current bit, it means we have enabled a previous
1474 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001475 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001476
1477 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001478 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001479 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001480
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001481 if (ana_list & AN_REQ_DECODE_PROXY) {
1482 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1483 break;
1484 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1485 }
1486
Willy Tarreaufb356202010-08-03 14:02:05 +02001487 if (ana_list & AN_REQ_INSPECT_FE) {
1488 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001489 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001490 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001491 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001492
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001494 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001495 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001497 }
1498
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001500 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1501 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001502 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001503 }
1504
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001505 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001506 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1507 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001508 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001509 }
1510
Willy Tarreaufb356202010-08-03 14:02:05 +02001511 if (ana_list & AN_REQ_INSPECT_BE) {
1512 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1513 break;
1514 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1515 }
1516
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001517 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001518 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1519 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001520 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001521 }
1522
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001523 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001524 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001525 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001526 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001527 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001528
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001529 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001530 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1531 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001532 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001533 }
1534
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001535 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001536 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001537 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001538 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001539 }
Emeric Brun647caf12009-06-30 17:57:00 +02001540
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001541 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001542 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1543 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001544 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001545 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001546
Emeric Brun1d33b292010-01-04 15:47:17 +01001547 if (ana_list & AN_REQ_STICKING_RULES) {
1548 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1549 break;
1550 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1551 }
1552
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001553 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001554 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1555 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001556 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001557 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001558 break;
1559 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001560 }
Willy Tarreau84455332009-03-15 22:34:05 +01001561
Willy Tarreau815a9b22010-07-27 17:15:12 +02001562 rq_prod_last = s->si[0].state;
1563 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001564 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001565 rqf_last = s->req->flags;
1566
1567 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001568 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001569 }
1570
Willy Tarreau576507f2010-01-07 00:09:04 +01001571 /* we'll monitor the request analysers while parsing the response,
1572 * because some response analysers may indirectly enable new request
1573 * analysers (eg: HTTP keep-alive).
1574 */
1575 req_ana_back = s->req->analysers;
1576
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001577 resync_response:
1578 /* Analyse response */
1579
1580 if (unlikely(s->rep->flags & BF_HIJACK)) {
1581 /* In inject mode, we wake up everytime something has
1582 * happened on the write side of the buffer.
1583 */
1584 unsigned int flags = s->rep->flags;
1585
1586 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1587 !(s->rep->flags & BF_FULL)) {
1588 s->rep->hijacker(s, s->rep);
1589 }
1590
1591 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1592 rpf_last = s->rep->flags;
1593 goto resync_response;
1594 }
1595 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001596 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001597 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1598 s->si[0].state != rp_cons_last ||
1599 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001600 unsigned int flags = s->rep->flags;
1601
Willy Tarreau0499e352010-12-17 07:13:42 +01001602 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1603 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1604 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1605 * for some free space in the response buffer, so we might need to call
1606 * it when something changes in the response buffer, but still we pass
1607 * it the request buffer. Note that the SI state might very well still
1608 * be zero due to us returning a flow of redirects!
1609 */
1610 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1611 s->req->flags |= BF_WAKE_ONCE;
1612 }
1613
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001614 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001615 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001616 unsigned int ana_list;
1617 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001618
Willy Tarreau90deb182010-01-07 00:20:41 +01001619 /* it's up to the analysers to stop disable reading or
1620 * closing. Note: if an analyser disables any of these
1621 * bits, it is responsible for enabling them again when
1622 * it disables itself, so that other analysers are called
1623 * in similar conditions.
1624 */
1625 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001626 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001627
1628 /* We will call all analysers for which a bit is set in
1629 * s->rep->analysers, following the bit order from LSB
1630 * to MSB. The analysers must remove themselves from
1631 * the list when not needed. Any analyser may return 0
1632 * to break out of the loop, either because of missing
1633 * data to take a decision, or because it decides to
1634 * kill the session. We loop at least once through each
1635 * analyser, and we may loop again if other analysers
1636 * are added in the middle.
1637 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001638
1639 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001640 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001641 /* Warning! ensure that analysers are always placed in ascending order! */
1642
Emeric Brun97679e72010-09-23 17:56:44 +02001643 if (ana_list & AN_RES_INSPECT) {
1644 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1645 break;
1646 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1647 }
1648
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001649 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001650 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1651 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001652 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001653 }
1654
Emeric Brun1d33b292010-01-04 15:47:17 +01001655 if (ana_list & AN_RES_STORE_RULES) {
1656 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1657 break;
1658 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1659 }
1660
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001661 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001662 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1663 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001664 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001665 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001666
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001667 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001668 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1669 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001670 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001671 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001672 break;
1673 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001674 }
1675
Willy Tarreau815a9b22010-07-27 17:15:12 +02001676 rp_cons_last = s->si[0].state;
1677 rp_prod_last = s->si[1].state;
1678 rpf_last = s->rep->flags;
1679
1680 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001681 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001682 }
1683
Willy Tarreau576507f2010-01-07 00:09:04 +01001684 /* maybe someone has added some request analysers, so we must check and loop */
1685 if (s->req->analysers & ~req_ana_back)
1686 goto resync_request;
1687
Willy Tarreau0499e352010-12-17 07:13:42 +01001688 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1689 goto resync_request;
1690
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001691 /* FIXME: here we should call protocol handlers which rely on
1692 * both buffers.
1693 */
1694
1695
1696 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001697 * Now we propagate unhandled errors to the session. Normally
1698 * we're just in a data phase here since it means we have not
1699 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001700 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001701 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001702 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001703 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1704 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001705 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001706 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001707 s->be->be_counters.cli_aborts++;
1708 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001709 if (srv)
1710 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001711 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001712 }
1713 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001714 s->be->be_counters.cli_aborts++;
1715 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001716 if (srv)
1717 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001718 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001719 }
1720 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001721 s->be->be_counters.srv_aborts++;
1722 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001723 if (srv)
1724 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001725 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001726 }
1727 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001728 s->be->be_counters.srv_aborts++;
1729 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001730 if (srv)
1731 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001732 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001733 }
Willy Tarreau84455332009-03-15 22:34:05 +01001734 sess_set_term_flags(s);
1735 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001736 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1737 /* Report it if the server got an error or a read timeout expired */
1738 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001739 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001740 s->be->be_counters.srv_aborts++;
1741 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001742 if (srv)
1743 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001744 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001745 }
1746 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001747 s->be->be_counters.srv_aborts++;
1748 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001749 if (srv)
1750 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001751 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001752 }
1753 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001754 s->be->be_counters.cli_aborts++;
1755 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001756 if (srv)
1757 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001758 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001759 }
1760 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001761 s->be->be_counters.cli_aborts++;
1762 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001763 if (srv)
1764 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001765 s->flags |= SN_ERR_CLITO;
1766 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001767 sess_set_term_flags(s);
1768 }
Willy Tarreau84455332009-03-15 22:34:05 +01001769 }
1770
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001771 /*
1772 * Here we take care of forwarding unhandled data. This also includes
1773 * connection establishments and shutdown requests.
1774 */
1775
1776
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001777 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001778 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001779 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1780 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001781 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001782 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001783 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001784 (s->req->prod->state >= SI_ST_EST) &&
1785 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001786 /* This buffer is freewheeling, there's no analyser nor hijacker
1787 * attached to it. If any data are left in, we'll permit them to
1788 * move.
1789 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001790 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001791 buffer_auto_connect(s->req);
1792 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001793 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001794
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001795 /* We'll let data flow between the producer (if still connected)
1796 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001797 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001798 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001799 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001800 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001801
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001802 /* check if it is wise to enable kernel splicing to forward request data */
1803 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1804 s->req->to_forward &&
1805 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001806 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001807 (pipes_used < global.maxpipes) &&
1808 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1809 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1810 (s->req->flags & BF_STREAMER_FAST)))) {
1811 s->req->flags |= BF_KERN_SPLICING;
1812 }
1813
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001814 /* reflect what the L7 analysers have seen last */
1815 rqf_last = s->req->flags;
1816
1817 /*
1818 * Now forward all shutdown requests between both sides of the buffer
1819 */
1820
Willy Tarreau520d95e2009-09-19 21:04:57 +02001821 /* first, let's check if the request buffer needs to shutdown(write), which may
1822 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001823 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001824 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001825 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001826 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001827 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001828
1829 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001830 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 +01001831 s->req->cons->shutw(s->req->cons);
1832
1833 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001834 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1835 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001836 buffer_shutr_now(s->req);
1837
1838 /* shutdown(read) pending */
1839 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1840 s->req->prod->shutr(s->req->prod);
1841
Willy Tarreau520d95e2009-09-19 21:04:57 +02001842 /* it's possible that an upper layer has requested a connection setup or abort.
1843 * There are 2 situations where we decide to establish a new connection :
1844 * - there are data scheduled for emission in the buffer
1845 * - the BF_AUTO_CONNECT flag is set (active connection)
1846 */
1847 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001848 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001849 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001850 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001851 * switch to the connected state, otherwise we perform a connection
1852 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001853 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001854 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001855 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001856 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001857 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001858 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1859 s->req->wex = TICK_ETERNITY;
1860 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001861 }
Willy Tarreau73201222009-08-16 18:27:24 +02001862 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001863 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001864 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001865 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1866 buffer_shutr_now(s->rep);
1867 }
Willy Tarreau92795622009-03-06 12:51:23 +01001868 }
1869
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001870
1871 /* we may have a pending connection request, or a connection waiting
1872 * for completion.
1873 */
1874 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1875 do {
1876 /* nb: step 1 might switch from QUE to ASS, but we first want
1877 * to give a chance to step 2 to perform a redirect if needed.
1878 */
1879 if (s->si[1].state != SI_ST_REQ)
1880 sess_update_stream_int(s, &s->si[1]);
1881 if (s->si[1].state == SI_ST_REQ)
1882 sess_prepare_conn_req(s, &s->si[1]);
1883
Willy Tarreau827aee92011-03-10 16:55:02 +01001884 srv = target_srv(&s->target);
1885 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886 perform_http_redirect(s, &s->si[1]);
1887 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001888
1889 /* Now we can add the server name to a header (if requested) */
1890 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01001891 if ((s->flags & SN_BE_ASSIGNED) &&
Mark Lamourinec2247f02012-01-04 13:02:01 -05001892 (s->be->mode == PR_MODE_HTTP) &&
1893 (s->be->server_id_hdr_name != NULL)) {
1894
1895 http_send_name_header(&s->txn,
1896 &s->txn.req,
1897 s->req,
1898 s->be,
1899 target_srv(&s->target)->id);
1900 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001901 }
1902
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001903 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001904 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 goto resync_stream_interface;
1906
Willy Tarreau815a9b22010-07-27 17:15:12 +02001907 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001908 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001909 goto resync_request;
1910
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001911 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001912
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001913 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001914 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001915 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1916 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001917 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001918 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001919 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001920 (s->rep->prod->state >= SI_ST_EST) &&
1921 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001922 /* This buffer is freewheeling, there's no analyser nor hijacker
1923 * attached to it. If any data are left in, we'll permit them to
1924 * move.
1925 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001926 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001927 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001928 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001929
1930 /* We'll let data flow between the producer (if still connected)
1931 * to the consumer.
1932 */
1933 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001934 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001935 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001936
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001937 /* check if it is wise to enable kernel splicing to forward response data */
1938 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1939 s->rep->to_forward &&
1940 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001941 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001942 (pipes_used < global.maxpipes) &&
1943 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1944 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1945 (s->rep->flags & BF_STREAMER_FAST)))) {
1946 s->rep->flags |= BF_KERN_SPLICING;
1947 }
1948
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001949 /* reflect what the L7 analysers have seen last */
1950 rpf_last = s->rep->flags;
1951
1952 /*
1953 * Now forward all shutdown requests between both sides of the buffer
1954 */
1955
1956 /*
1957 * FIXME: this is probably where we should produce error responses.
1958 */
1959
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001960 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001961 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1962 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001963 buffer_shutw_now(s->rep);
1964
1965 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001966 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 +01001967 s->rep->cons->shutw(s->rep->cons);
1968
1969 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001970 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1971 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001972 buffer_shutr_now(s->rep);
1973
1974 /* shutdown(read) pending */
1975 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1976 s->rep->prod->shutr(s->rep->prod);
1977
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001978 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001979 goto resync_stream_interface;
1980
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001981 if (s->req->flags != rqf_last)
1982 goto resync_request;
1983
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001984 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001985 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001986
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001987 /* we're interested in getting wakeups again */
1988 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1989 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1990
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001991 /* This is needed only when debugging is enabled, to indicate
1992 * client-side or server-side close. Please note that in the unlikely
1993 * event where both sides would close at once, the sequence is reported
1994 * on the server side first.
1995 */
1996 if (unlikely((global.mode & MODE_DEBUG) &&
1997 (!(global.mode & MODE_QUIET) ||
1998 (global.mode & MODE_VERBOSE)))) {
1999 int len;
2000
2001 if (s->si[1].state == SI_ST_CLO &&
2002 s->si[1].prev_state == SI_ST_EST) {
2003 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2004 s->uniq_id, s->be->id,
2005 (unsigned short)s->si[0].fd,
2006 (unsigned short)s->si[1].fd);
2007 write(1, trash, len);
2008 }
2009
2010 if (s->si[0].state == SI_ST_CLO &&
2011 s->si[0].prev_state == SI_ST_EST) {
2012 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2013 s->uniq_id, s->be->id,
2014 (unsigned short)s->si[0].fd,
2015 (unsigned short)s->si[1].fd);
2016 write(1, trash, len);
2017 }
2018 }
2019
2020 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2021 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2022
2023 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2024 session_process_counters(s);
2025
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002026 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002027 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002028
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002029 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02002030 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002032 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2033 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 +01002034 s->si[0].prev_state = s->si[0].state;
2035 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002036 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2037 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
2039 /* Trick: if a request is being waiting for the server to respond,
2040 * and if we know the server can timeout, we don't want the timeout
2041 * to expire on the client side first, but we're still interested
2042 * in passing data from the client to the server (eg: POST). Thus,
2043 * we can cancel the client's request timeout if the server's
2044 * request timeout is set and the server has not yet sent a response.
2045 */
2046
Willy Tarreau520d95e2009-09-19 21:04:57 +02002047 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002048 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2049 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002051 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002052
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002053 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002054 * Note that this one may wake the task up again.
2055 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002056 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2057 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2058 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2059 s->req->cons->target.ptr.a->fct(s->req->cons);
2060 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2061 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002062 if (task_in_rq(t)) {
2063 /* If we woke up, we don't want to requeue the
2064 * task to the wait queue, but rather requeue
2065 * it into the runqueue ASAP.
2066 */
2067 t->expire = TICK_ETERNITY;
2068 return t;
2069 }
2070 }
2071
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002072 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2073 tick_first(s->rep->rex, s->rep->wex));
2074 if (s->req->analysers)
2075 t->expire = tick_first(t->expire, s->req->analyse_exp);
2076
2077 if (s->si[0].exp)
2078 t->expire = tick_first(t->expire, s->si[0].exp);
2079
2080 if (s->si[1].exp)
2081 t->expire = tick_first(t->expire, s->si[1].exp);
2082
2083#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002084 fprintf(stderr,
2085 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2086 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2087 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2088 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 +01002089#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090
2091#ifdef DEBUG_DEV
2092 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002093 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002094 ABORT_NOW();
2095#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002096 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097 }
2098
2099 s->fe->feconn--;
2100 if (s->flags & SN_BE_ASSIGNED)
2101 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002102 if (!(s->listener->options & LI_O_UNLIMITED))
2103 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002104 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002105 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002106 if (s->listener->state == LI_FULL)
2107 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002108
Willy Tarreau08ceb102011-07-24 22:58:00 +02002109 /* Dequeues all of the listeners waiting for a resource */
2110 if (!LIST_ISEMPTY(&global_listener_queue))
2111 dequeue_all_listeners(&global_listener_queue);
2112
Willy Tarreaub32907b2011-07-25 08:37:44 +02002113 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2114 (!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 +02002115 dequeue_all_listeners(&s->fe->listener_queue);
2116
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002117 if (unlikely((global.mode & MODE_DEBUG) &&
2118 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2119 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002120 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002121 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002122 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002123 write(1, trash, len);
2124 }
2125
2126 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2127 session_process_counters(s);
2128
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002129 if (s->txn.status) {
2130 int n;
2131
2132 n = s->txn.status / 100;
2133 if (n < 1 || n > 5)
2134 n = 0;
2135
2136 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002137 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002138
Willy Tarreau24657792010-02-26 10:30:28 +01002139 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002140 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002141 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002142 }
2143
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002144 /* let's do a final log if we need it */
2145 if (s->logs.logwait &&
2146 !(s->flags & SN_MONITOR) &&
2147 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002148 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002149 }
2150
2151 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002152 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002153 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002154 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002155 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156}
2157
Willy Tarreau7c669d72008-06-20 15:04:11 +02002158/*
2159 * This function adjusts sess->srv_conn and maintains the previous and new
2160 * server's served session counts. Setting newsrv to NULL is enough to release
2161 * current connection slot. This function also notifies any LB algo which might
2162 * expect to be informed about any change in the number of active sessions on a
2163 * server.
2164 */
2165void sess_change_server(struct session *sess, struct server *newsrv)
2166{
2167 if (sess->srv_conn == newsrv)
2168 return;
2169
2170 if (sess->srv_conn) {
2171 sess->srv_conn->served--;
2172 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2173 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002174 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002175 }
2176
2177 if (newsrv) {
2178 newsrv->served++;
2179 if (newsrv->proxy->lbprm.server_take_conn)
2180 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002181 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002182 }
2183}
2184
Willy Tarreau84455332009-03-15 22:34:05 +01002185/* Handle server-side errors for default protocols. It is called whenever a a
2186 * connection setup is aborted or a request is aborted in queue. It sets the
2187 * session termination flags so that the caller does not have to worry about
2188 * them. It's installed as ->srv_error for the server-side stream_interface.
2189 */
2190void default_srv_error(struct session *s, struct stream_interface *si)
2191{
2192 int err_type = si->err_type;
2193 int err = 0, fin = 0;
2194
2195 if (err_type & SI_ET_QUEUE_ABRT) {
2196 err = SN_ERR_CLICL;
2197 fin = SN_FINST_Q;
2198 }
2199 else if (err_type & SI_ET_CONN_ABRT) {
2200 err = SN_ERR_CLICL;
2201 fin = SN_FINST_C;
2202 }
2203 else if (err_type & SI_ET_QUEUE_TO) {
2204 err = SN_ERR_SRVTO;
2205 fin = SN_FINST_Q;
2206 }
2207 else if (err_type & SI_ET_QUEUE_ERR) {
2208 err = SN_ERR_SRVCL;
2209 fin = SN_FINST_Q;
2210 }
2211 else if (err_type & SI_ET_CONN_TO) {
2212 err = SN_ERR_SRVTO;
2213 fin = SN_FINST_C;
2214 }
2215 else if (err_type & SI_ET_CONN_ERR) {
2216 err = SN_ERR_SRVCL;
2217 fin = SN_FINST_C;
2218 }
2219 else /* SI_ET_CONN_OTHER and others */ {
2220 err = SN_ERR_INTERNAL;
2221 fin = SN_FINST_C;
2222 }
2223
2224 if (!(s->flags & SN_ERR_MASK))
2225 s->flags |= err;
2226 if (!(s->flags & SN_FINST_MASK))
2227 s->flags |= fin;
2228}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002229
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002230/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2231void session_shutdown(struct session *session, int why)
2232{
2233 if (session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))
2234 return;
2235
2236 buffer_shutw_now(session->req);
2237 buffer_shutr_now(session->rep);
2238 session->task->nice = 1024;
2239 if (!(session->flags & SN_ERR_MASK))
2240 session->flags |= why;
2241 task_wakeup(session->task, TASK_WOKEN_OTHER);
2242}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002243
Willy Tarreau8b22a712010-06-18 17:46:06 +02002244/************************************************************************/
2245/* All supported ACL keywords must be declared here. */
2246/************************************************************************/
2247
Willy Tarreaua5e37562011-12-16 17:06:15 +01002248/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002249static int
2250acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2251{
2252 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002253 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002254 if (ts != NULL) {
2255 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2256 if (!ptr)
2257 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002258 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002259 }
2260 return 1;
2261}
2262
Willy Tarreaua5e37562011-12-16 17:06:15 +01002263/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002264 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002265 */
2266static int
Willy Tarreau56123282010-08-06 19:06:56 +02002267acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2268 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002269{
Willy Tarreau56123282010-08-06 19:06:56 +02002270 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002271 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002272 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002273}
2274
Willy Tarreaua5e37562011-12-16 17:06:15 +01002275/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002276 * backend counters.
2277 */
2278static int
Willy Tarreau56123282010-08-06 19:06:56 +02002279acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2280 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002281{
Willy Tarreau56123282010-08-06 19:06:56 +02002282 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002283 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002284 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002285}
2286
Willy Tarreaua5e37562011-12-16 17:06:15 +01002287/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002288 * address in the table pointed to by expr.
2289 */
2290static int
2291acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2292 struct acl_expr *expr, struct acl_test *test)
2293{
2294 struct stktable_key *key;
2295
David du Colombier4f92d322011-03-24 11:09:31 +01002296 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002297 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002298 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002299
2300 if (expr->arg_len)
2301 px = find_stktable(expr->arg.str);
2302
2303 if (!px)
2304 return 0; /* table not found */
2305
2306 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2307}
2308
2309/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002310 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002311 */
2312static int
2313acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2314{
2315 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002316 temp_pattern.data.integer = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002317 if (ts != NULL) {
2318 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2319 if (!ptr)
2320 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002321 temp_pattern.data.integer = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002322 }
2323 return 1;
2324}
2325
2326/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002327 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002328 */
2329static int
Willy Tarreau56123282010-08-06 19:06:56 +02002330acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2331 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002332{
Willy Tarreau56123282010-08-06 19:06:56 +02002333 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002334 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002335 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002336}
2337
2338/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002339 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002340 */
2341static int
Willy Tarreau56123282010-08-06 19:06:56 +02002342acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2343 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002344{
Willy Tarreau56123282010-08-06 19:06:56 +02002345 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002346 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002347 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002348}
2349
2350/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002351 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002352 */
2353static int
2354acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2355 struct acl_expr *expr, struct acl_test *test)
2356{
2357 struct stktable_key *key;
2358
David du Colombier4f92d322011-03-24 11:09:31 +01002359 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002360 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002361 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002362
2363 if (expr->arg_len)
2364 px = find_stktable(expr->arg.str);
2365
2366 if (!px)
2367 return 0; /* table not found */
2368
2369 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2370}
2371
Willy Tarreauf73cd112011-08-13 01:45:16 +02002372/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002373 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002374 */
2375static int
2376acl_fetch_clr_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2377{
2378 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002379 temp_pattern.data.integer = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002380 if (ts != NULL) {
2381 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2382 if (!ptr)
2383 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002384 temp_pattern.data.integer = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002385 stktable_data_cast(ptr, gpc0) = 0;
2386 }
2387 return 1;
2388}
2389
2390/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002391 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002392 */
2393static int
2394acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2395 struct acl_expr *expr, struct acl_test *test)
2396{
2397 if (!l4->stkctr1_entry)
2398 return 0;
2399 return acl_fetch_clr_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
2400}
2401
2402/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002403 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002404 */
2405static int
2406acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2407 struct acl_expr *expr, struct acl_test *test)
2408{
2409 if (!l4->stkctr2_entry)
2410 return 0;
2411 return acl_fetch_clr_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
2412}
2413
2414/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002415 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002416 */
2417static int
2418acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2419 struct acl_expr *expr, struct acl_test *test)
2420{
2421 struct stktable_key *key;
2422
2423 key = tcp_src_to_stktable_key(l4);
2424 if (!key)
2425 return 0;
2426
2427 if (expr->arg_len)
2428 px = find_stktable(expr->arg.str);
2429
2430 if (!px)
2431 return 0; /* table not found */
2432
2433 return acl_fetch_clr_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2434}
2435
Willy Tarreaua5e37562011-12-16 17:06:15 +01002436/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002437static int
2438acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2439{
2440 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002441 temp_pattern.data.integer = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002442 if (ts != NULL) {
2443 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2444 if (!ptr)
2445 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002446 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002447 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002448 return 1;
2449}
2450
Willy Tarreaua5e37562011-12-16 17:06:15 +01002451/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002452static int
Willy Tarreau56123282010-08-06 19:06:56 +02002453acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2454 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002455{
Willy Tarreau56123282010-08-06 19:06:56 +02002456 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002457 return 0;
2458
Willy Tarreau56123282010-08-06 19:06:56 +02002459 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002460}
2461
Willy Tarreaua5e37562011-12-16 17:06:15 +01002462/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002463static int
Willy Tarreau56123282010-08-06 19:06:56 +02002464acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2465 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002466{
Willy Tarreau56123282010-08-06 19:06:56 +02002467 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002468 return 0;
2469
Willy Tarreau56123282010-08-06 19:06:56 +02002470 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002471}
2472
Willy Tarreaua5e37562011-12-16 17:06:15 +01002473/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002474 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002475 */
2476static int
2477acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2478 struct acl_expr *expr, struct acl_test *test)
2479{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002480 struct stktable_key *key;
2481
David du Colombier4f92d322011-03-24 11:09:31 +01002482 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002483 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002484 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002485
2486 if (expr->arg_len)
2487 px = find_stktable(expr->arg.str);
2488
2489 if (!px)
2490 return 0; /* table not found */
2491
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002492 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002493}
2494
Willy Tarreaua5e37562011-12-16 17:06:15 +01002495/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002496static int
2497acl_fetch_conn_rate(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 Tarreau91c43d72010-06-20 11:19:22 +02002501 if (ts != NULL) {
2502 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2503 if (!ptr)
2504 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002505 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002506 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2507 }
2508 return 1;
2509}
2510
Willy Tarreaua5e37562011-12-16 17:06:15 +01002511/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002512 * the configured period.
2513 */
2514static int
Willy Tarreau56123282010-08-06 19:06:56 +02002515acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2516 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002517{
Willy Tarreau56123282010-08-06 19:06:56 +02002518 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002519 return 0;
2520
Willy Tarreau56123282010-08-06 19:06:56 +02002521 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002522}
2523
Willy Tarreaua5e37562011-12-16 17:06:15 +01002524/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002525 * the configured period.
2526 */
2527static int
Willy Tarreau56123282010-08-06 19:06:56 +02002528acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2529 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002530{
Willy Tarreau56123282010-08-06 19:06:56 +02002531 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002532 return 0;
2533
Willy Tarreau56123282010-08-06 19:06:56 +02002534 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002535}
2536
Willy Tarreaua5e37562011-12-16 17:06:15 +01002537/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002538 * table pointed to by expr, over the configured period.
2539 */
2540static int
2541acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2542 struct acl_expr *expr, struct acl_test *test)
2543{
2544 struct stktable_key *key;
2545
David du Colombier4f92d322011-03-24 11:09:31 +01002546 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002547 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002548 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002549
2550 if (expr->arg_len)
2551 px = find_stktable(expr->arg.str);
2552
2553 if (!px)
2554 return 0; /* table not found */
2555
2556 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2557}
2558
Willy Tarreaua5e37562011-12-16 17:06:15 +01002559/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002560 * in the table pointed to by expr, after updating it.
2561 */
2562static int
2563acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2564 struct acl_expr *expr, struct acl_test *test)
2565{
2566 struct stksess *ts;
2567 struct stktable_key *key;
2568 void *ptr;
2569
David du Colombier4f92d322011-03-24 11:09:31 +01002570 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002571 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002572 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002573
2574 if (expr->arg_len)
2575 px = find_stktable(expr->arg.str);
2576
2577 if (!px)
2578 return 0; /* table not found */
2579
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002580 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2581 /* entry does not exist and could not be created */
2582 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002583
2584 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2585 if (!ptr)
2586 return 0; /* parameter not stored in this table */
2587
Willy Tarreaua5e37562011-12-16 17:06:15 +01002588 temp_pattern.data.integer = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002589 test->flags = ACL_TEST_F_VOL_TEST;
2590 return 1;
2591}
2592
Willy Tarreaua5e37562011-12-16 17:06:15 +01002593/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002594static int
2595acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2596{
2597 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002598 temp_pattern.data.integer = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002599
2600 if (ts != NULL) {
2601 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2602 if (!ptr)
2603 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002604 temp_pattern.data.integer = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002605 }
2606 return 1;
2607}
2608
Willy Tarreaua5e37562011-12-16 17:06:15 +01002609/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002610static int
Willy Tarreau56123282010-08-06 19:06:56 +02002611acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2612 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002613{
Willy Tarreau56123282010-08-06 19:06:56 +02002614 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002615 return 0;
2616
Willy Tarreau56123282010-08-06 19:06:56 +02002617 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002618}
2619
Willy Tarreaua5e37562011-12-16 17:06:15 +01002620/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002621static int
Willy Tarreau56123282010-08-06 19:06:56 +02002622acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2623 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002624{
Willy Tarreau56123282010-08-06 19:06:56 +02002625 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002626 return 0;
2627
Willy Tarreau56123282010-08-06 19:06:56 +02002628 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002629}
2630
Willy Tarreaua5e37562011-12-16 17:06:15 +01002631/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002632 * address in the table pointed to by expr.
2633 */
2634static int
2635acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2636 struct acl_expr *expr, struct acl_test *test)
2637{
Willy Tarreau38285c12010-06-18 16:35:43 +02002638 struct stktable_key *key;
2639
David du Colombier4f92d322011-03-24 11:09:31 +01002640 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002641 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002642 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002643
2644 if (expr->arg_len)
2645 px = find_stktable(expr->arg.str);
2646
2647 if (!px)
2648 return 0; /* table not found */
2649
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002650 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002651}
2652
Willy Tarreaua5e37562011-12-16 17:06:15 +01002653/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002654static int
2655acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2656{
2657 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002658 temp_pattern.data.integer = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002659 if (ts != NULL) {
2660 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
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, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002664 }
2665 return 1;
2666}
2667
Willy Tarreaua5e37562011-12-16 17:06:15 +01002668/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002669static int
Willy Tarreau56123282010-08-06 19:06:56 +02002670acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2671 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002672{
Willy Tarreau56123282010-08-06 19:06:56 +02002673 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002674 return 0;
2675
Willy Tarreau56123282010-08-06 19:06:56 +02002676 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002677}
2678
Willy Tarreaua5e37562011-12-16 17:06:15 +01002679/* set temp integer to the cumulated number of sessions 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_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2682 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002683{
Willy Tarreau56123282010-08-06 19:06:56 +02002684 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002685 return 0;
2686
Willy Tarreau56123282010-08-06 19:06:56 +02002687 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002688}
2689
Willy Tarreaua5e37562011-12-16 17:06:15 +01002690/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002691 * address in the table pointed to by expr.
2692 */
2693static int
2694acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2695 struct acl_expr *expr, struct acl_test *test)
2696{
2697 struct stktable_key *key;
2698
David du Colombier4f92d322011-03-24 11:09:31 +01002699 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002700 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002701 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002702
2703 if (expr->arg_len)
2704 px = find_stktable(expr->arg.str);
2705
2706 if (!px)
2707 return 0; /* table not found */
2708
2709 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2710}
2711
Willy Tarreaua5e37562011-12-16 17:06:15 +01002712/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002713static int
2714acl_fetch_sess_rate(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 Tarreau91c43d72010-06-20 11:19:22 +02002718 if (ts != NULL) {
2719 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2720 if (!ptr)
2721 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002722 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002723 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2724 }
2725 return 1;
2726}
2727
Willy Tarreaua5e37562011-12-16 17:06:15 +01002728/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002729 * the configured period.
2730 */
2731static int
Willy Tarreau56123282010-08-06 19:06:56 +02002732acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2733 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002734{
Willy Tarreau56123282010-08-06 19:06:56 +02002735 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002736 return 0;
2737
Willy Tarreau56123282010-08-06 19:06:56 +02002738 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002739}
2740
Willy Tarreaua5e37562011-12-16 17:06:15 +01002741/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002742 * the configured period.
2743 */
2744static int
Willy Tarreau56123282010-08-06 19:06:56 +02002745acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2746 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002747{
Willy Tarreau56123282010-08-06 19:06:56 +02002748 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002749 return 0;
2750
Willy Tarreau56123282010-08-06 19:06:56 +02002751 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002752}
2753
Willy Tarreaua5e37562011-12-16 17:06:15 +01002754/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002755 * table pointed to by expr, over the configured period.
2756 */
2757static int
2758acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2759 struct acl_expr *expr, struct acl_test *test)
2760{
2761 struct stktable_key *key;
2762
David du Colombier4f92d322011-03-24 11:09:31 +01002763 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002764 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002765 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002766
2767 if (expr->arg_len)
2768 px = find_stktable(expr->arg.str);
2769
2770 if (!px)
2771 return 0; /* table not found */
2772
2773 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2774}
2775
Willy Tarreaua5e37562011-12-16 17:06:15 +01002776/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002777static int
2778acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2779{
2780 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002781 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002782 if (ts != NULL) {
2783 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2784 if (!ptr)
2785 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002786 temp_pattern.data.integer = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002787 }
2788 return 1;
2789}
2790
Willy Tarreaua5e37562011-12-16 17:06:15 +01002791/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002792static int
Willy Tarreau56123282010-08-06 19:06:56 +02002793acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2794 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002795{
Willy Tarreau56123282010-08-06 19:06:56 +02002796 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002797 return 0;
2798
Willy Tarreau56123282010-08-06 19:06:56 +02002799 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002800}
2801
Willy Tarreaua5e37562011-12-16 17:06:15 +01002802/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002803static int
Willy Tarreau56123282010-08-06 19:06:56 +02002804acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2805 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002806{
Willy Tarreau56123282010-08-06 19:06:56 +02002807 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002808 return 0;
2809
Willy Tarreau56123282010-08-06 19:06:56 +02002810 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002811}
2812
Willy Tarreaua5e37562011-12-16 17:06:15 +01002813/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002814 * address in the table pointed to by expr.
2815 */
2816static int
2817acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002818 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002819{
2820 struct stktable_key *key;
2821
David du Colombier4f92d322011-03-24 11:09:31 +01002822 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002823 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002824 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +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_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2833}
2834
Willy Tarreaua5e37562011-12-16 17:06:15 +01002835/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002836static int
2837acl_fetch_http_req_rate(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_RATE);
2843 if (!ptr)
2844 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002845 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002846 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2847 }
2848 return 1;
2849}
2850
Willy Tarreaua5e37562011-12-16 17:06:15 +01002851/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002852 * the configured period.
2853 */
2854static int
Willy Tarreau56123282010-08-06 19:06:56 +02002855acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2856 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002857{
Willy Tarreau56123282010-08-06 19:06:56 +02002858 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002859 return 0;
2860
Willy Tarreau56123282010-08-06 19:06:56 +02002861 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002862}
2863
Willy Tarreaua5e37562011-12-16 17:06:15 +01002864/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002865 * the configured period.
2866 */
2867static int
Willy Tarreau56123282010-08-06 19:06:56 +02002868acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2869 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002870{
Willy Tarreau56123282010-08-06 19:06:56 +02002871 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002872 return 0;
2873
Willy Tarreau56123282010-08-06 19:06:56 +02002874 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002875}
2876
Willy Tarreaua5e37562011-12-16 17:06:15 +01002877/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02002878 * table pointed to by expr, over the configured period.
2879 */
2880static int
2881acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002882 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002883{
2884 struct stktable_key *key;
2885
David du Colombier4f92d322011-03-24 11:09:31 +01002886 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002887 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002888 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002889
2890 if (expr->arg_len)
2891 px = find_stktable(expr->arg.str);
2892
2893 if (!px)
2894 return 0; /* table not found */
2895
2896 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2897}
2898
Willy Tarreaua5e37562011-12-16 17:06:15 +01002899/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002900static int
2901acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2902{
2903 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01002904 temp_pattern.data.integer = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002905 if (ts != NULL) {
2906 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2907 if (!ptr)
2908 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002909 temp_pattern.data.integer = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002910 }
2911 return 1;
2912}
2913
Willy Tarreaua5e37562011-12-16 17:06:15 +01002914/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002915static int
Willy Tarreau56123282010-08-06 19:06:56 +02002916acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2917 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002918{
Willy Tarreau56123282010-08-06 19:06:56 +02002919 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002920 return 0;
2921
Willy Tarreau56123282010-08-06 19:06:56 +02002922 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002923}
2924
Willy Tarreaua5e37562011-12-16 17:06:15 +01002925/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002926static int
Willy Tarreau56123282010-08-06 19:06:56 +02002927acl_fetch_sc2_http_err_cnt(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_err_cnt(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 cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937 * address in the table pointed to by expr.
2938 */
2939static int
2940acl_fetch_src_http_err_cnt(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_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2956}
2957
Willy Tarreaua5e37562011-12-16 17:06:15 +01002958/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959static int
2960acl_fetch_http_err_rate(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_RATE);
2966 if (!ptr)
2967 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01002968 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002969 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2970 }
2971 return 1;
2972}
2973
Willy Tarreaua5e37562011-12-16 17:06:15 +01002974/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002975 * the configured period.
2976 */
2977static int
Willy Tarreau56123282010-08-06 19:06:56 +02002978acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2979 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002980{
Willy Tarreau56123282010-08-06 19:06:56 +02002981 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002982 return 0;
2983
Willy Tarreau56123282010-08-06 19:06:56 +02002984 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002985}
2986
Willy Tarreaua5e37562011-12-16 17:06:15 +01002987/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002988 * the configured period.
2989 */
2990static int
Willy Tarreau56123282010-08-06 19:06:56 +02002991acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2992 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993{
Willy Tarreau56123282010-08-06 19:06:56 +02002994 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002995 return 0;
2996
Willy Tarreau56123282010-08-06 19:06:56 +02002997 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998}
2999
Willy Tarreaua5e37562011-12-16 17:06:15 +01003000/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001 * table pointed to by expr, over the configured period.
3002 */
3003static int
3004acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3005 struct acl_expr *expr, struct acl_test *test)
3006{
3007 struct stktable_key *key;
3008
David du Colombier4f92d322011-03-24 11:09:31 +01003009 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003011 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003012
3013 if (expr->arg_len)
3014 px = find_stktable(expr->arg.str);
3015
3016 if (!px)
3017 return 0; /* table not found */
3018
3019 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3020}
3021
Willy Tarreaua5e37562011-12-16 17:06:15 +01003022/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003023static int
3024acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
3025{
3026 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003027 temp_pattern.data.integer = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003028
3029 if (ts != NULL) {
3030 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3031 if (!ptr)
3032 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003033 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003034 }
3035 return 1;
3036}
3037
Willy Tarreaua5e37562011-12-16 17:06:15 +01003038/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003039 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003040 */
3041static int
Willy Tarreau56123282010-08-06 19:06:56 +02003042acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3043 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003044{
Willy Tarreau56123282010-08-06 19:06:56 +02003045 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003046 return 0;
3047
Willy Tarreau56123282010-08-06 19:06:56 +02003048 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003049}
3050
Willy Tarreaua5e37562011-12-16 17:06:15 +01003051/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003052 * session's tracked BE counters.
3053 */
3054static int
Willy Tarreau56123282010-08-06 19:06:56 +02003055acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3056 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003057{
Willy Tarreau56123282010-08-06 19:06:56 +02003058 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003059 return 0;
3060
Willy Tarreau56123282010-08-06 19:06:56 +02003061 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003062}
3063
Willy Tarreaua5e37562011-12-16 17:06:15 +01003064/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003065 * address in the table pointed to by expr.
3066 */
3067static int
3068acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
3069 struct acl_expr *expr, struct acl_test *test)
3070{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003071 struct stktable_key *key;
3072
David du Colombier4f92d322011-03-24 11:09:31 +01003073 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003074 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003075 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003076
3077 if (expr->arg_len)
3078 px = find_stktable(expr->arg.str);
3079
3080 if (!px)
3081 return 0; /* table not found */
3082
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003083 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
3084}
3085
Willy Tarreaua5e37562011-12-16 17:06:15 +01003086/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003087 * configured period.
3088 */
3089static int
3090acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3091{
3092 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003093 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003094 if (ts != NULL) {
3095 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3096 if (!ptr)
3097 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003098 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003099 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3100 }
3101 return 1;
3102}
3103
Willy Tarreaua5e37562011-12-16 17:06:15 +01003104/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003105 * counters over the configured period.
3106 */
3107static int
Willy Tarreau56123282010-08-06 19:06:56 +02003108acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3109 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003110{
Willy Tarreau56123282010-08-06 19:06:56 +02003111 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003112 return 0;
3113
Willy Tarreau56123282010-08-06 19:06:56 +02003114 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003115}
3116
Willy Tarreaua5e37562011-12-16 17:06:15 +01003117/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003118 * counters over the configured period.
3119 */
3120static int
Willy Tarreau56123282010-08-06 19:06:56 +02003121acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3122 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003123{
Willy Tarreau56123282010-08-06 19:06:56 +02003124 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003125 return 0;
3126
Willy Tarreau56123282010-08-06 19:06:56 +02003127 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003128}
3129
Willy Tarreaua5e37562011-12-16 17:06:15 +01003130/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003131 * in the table pointed to by expr, over the configured period.
3132 */
3133static int
3134acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003135 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003136{
3137 struct stktable_key *key;
3138
David du Colombier4f92d322011-03-24 11:09:31 +01003139 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003140 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003141 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003142
3143 if (expr->arg_len)
3144 px = find_stktable(expr->arg.str);
3145
3146 if (!px)
3147 return 0; /* table not found */
3148
3149 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3150}
3151
Willy Tarreaua5e37562011-12-16 17:06:15 +01003152/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003153static int
3154acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3155{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003156 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003157 temp_pattern.data.integer = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003158
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003159 if (ts != NULL) {
3160 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003161 if (!ptr)
3162 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003163 temp_pattern.data.integer = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003164 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003165 return 1;
3166}
3167
Willy Tarreaua5e37562011-12-16 17:06:15 +01003168/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003169 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003170 */
3171static int
Willy Tarreau56123282010-08-06 19:06:56 +02003172acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3173 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003174{
Willy Tarreau56123282010-08-06 19:06:56 +02003175 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003176 return 0;
3177
Willy Tarreau56123282010-08-06 19:06:56 +02003178 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003179}
3180
Willy Tarreaua5e37562011-12-16 17:06:15 +01003181/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003182 * tracked BE counters.
3183 */
3184static int
Willy Tarreau56123282010-08-06 19:06:56 +02003185acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3186 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003187{
Willy Tarreau56123282010-08-06 19:06:56 +02003188 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003189 return 0;
3190
Willy Tarreau56123282010-08-06 19:06:56 +02003191 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003192}
3193
Willy Tarreaua5e37562011-12-16 17:06:15 +01003194/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003195 * the table pointed to by expr.
3196 */
3197static int
3198acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3199 struct acl_expr *expr, struct acl_test *test)
3200{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003201 struct stktable_key *key;
3202
David du Colombier4f92d322011-03-24 11:09:31 +01003203 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003204 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003205 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003206
3207 if (expr->arg_len)
3208 px = find_stktable(expr->arg.str);
3209
3210 if (!px)
3211 return 0; /* table not found */
3212
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003213 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003214}
3215
Willy Tarreaua5e37562011-12-16 17:06:15 +01003216/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003217 * configured period.
3218 */
3219static int
3220acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3221{
3222 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003223 temp_pattern.data.integer = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003224 if (ts != NULL) {
3225 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3226 if (!ptr)
3227 return 0; /* parameter not stored */
Willy Tarreaua5e37562011-12-16 17:06:15 +01003228 temp_pattern.data.integer = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003229 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3230 }
3231 return 1;
3232}
3233
Willy Tarreaua5e37562011-12-16 17:06:15 +01003234/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003235 * over the configured period.
3236 */
3237static int
Willy Tarreau56123282010-08-06 19:06:56 +02003238acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3239 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003240{
Willy Tarreau56123282010-08-06 19:06:56 +02003241 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003242 return 0;
3243
Willy Tarreau56123282010-08-06 19:06:56 +02003244 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003245}
3246
Willy Tarreaua5e37562011-12-16 17:06:15 +01003247/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003248 * over the configured period.
3249 */
3250static int
Willy Tarreau56123282010-08-06 19:06:56 +02003251acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3252 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003253{
Willy Tarreau56123282010-08-06 19:06:56 +02003254 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003255 return 0;
3256
Willy Tarreau56123282010-08-06 19:06:56 +02003257 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003258}
3259
Willy Tarreaua5e37562011-12-16 17:06:15 +01003260/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003261 * the table pointed to by expr, over the configured period.
3262 */
3263static int
3264acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003265 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003266{
3267 struct stktable_key *key;
3268
David du Colombier4f92d322011-03-24 11:09:31 +01003269 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003270 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003271 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003272
3273 if (expr->arg_len)
3274 px = find_stktable(expr->arg.str);
3275
3276 if (!px)
3277 return 0; /* table not found */
3278
3279 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3280}
3281
Willy Tarreaua5e37562011-12-16 17:06:15 +01003282/* set temp integer to the number of used entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003283static int
3284acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
3285 struct acl_expr *expr, struct acl_test *test)
3286{
3287 if (expr->arg_len)
3288 px = find_stktable(expr->arg.str);
3289
3290 if (!px)
3291 return 0; /* table not found */
3292
3293 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003294 temp_pattern.data.integer = px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003295 return 1;
3296}
3297
Willy Tarreaua5e37562011-12-16 17:06:15 +01003298/* set temp integer to the number of free entries in the table pointed to by expr. */
Willy Tarreauc735a072011-03-29 00:57:02 +02003299static int
3300acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, int dir,
3301 struct acl_expr *expr, struct acl_test *test)
3302{
3303 if (expr->arg_len)
3304 px = find_stktable(expr->arg.str);
3305
3306 if (!px)
3307 return 0; /* table not found */
3308
3309 test->flags = ACL_TEST_F_VOL_TEST;
Willy Tarreaua5e37562011-12-16 17:06:15 +01003310 temp_pattern.data.integer = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003311 return 1;
3312}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003313
3314/* Note: must not be declared <const> as its list will be overwritten */
3315static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003316 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3317 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3318 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3319 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3320 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3321 { "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 +02003322 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3323 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING },
3324 { "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 +02003325 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3326 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3327 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3328 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3329 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3330 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3331 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3332 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3333 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3334 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3335 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3336 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3337 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3338 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3339 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3340 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3341 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3342 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3343 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3344 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3345 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3346 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3347 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3348 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3349 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3350 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3351 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3352 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3353 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3354 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3355 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3356 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3357 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3358 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3359 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3360 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3361 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3362 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3363 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3364 { "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 +02003365 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING },
3366 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003367 { NULL, NULL, NULL, NULL },
3368}};
3369
3370
Willy Tarreau56123282010-08-06 19:06:56 +02003371/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003372 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3373 * <prm> struct is fed with the table name if any. If unspecified, the caller
3374 * will assume that the current proxy's table is used.
3375 */
3376int parse_track_counters(char **args, int *arg,
3377 int section_type, struct proxy *curpx,
3378 struct track_ctr_prm *prm,
3379 struct proxy *defpx, char *err, int errlen)
3380{
3381 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003382 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003383
Willy Tarreau56123282010-08-06 19:06:56 +02003384 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003385 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003386 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003387 */
3388 while (args[*arg]) {
3389 if (strcmp(args[*arg], "src") == 0) {
3390 prm->type = STKTABLE_TYPE_IP;
3391 pattern_type = 1;
3392 }
3393 else if (strcmp(args[*arg], "table") == 0) {
3394 if (!args[*arg + 1]) {
3395 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003396 "missing table for %s in %s '%s'.",
3397 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003398 return -1;
3399 }
3400 /* we copy the table name for now, it will be resolved later */
3401 prm->table.n = strdup(args[*arg + 1]);
3402 (*arg)++;
3403 }
3404 else {
3405 /* unhandled keywords are handled by the caller */
3406 break;
3407 }
3408 (*arg)++;
3409 }
3410
3411 if (!pattern_type) {
3412 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003413 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3414 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003415 return -1;
3416 }
3417
3418 return 0;
3419}
3420
Willy Tarreau8b22a712010-06-18 17:46:06 +02003421__attribute__((constructor))
3422static void __session_init(void)
3423{
3424 acl_register_keywords(&acl_kws);
3425}
3426
Willy Tarreaubaaee002006-06-26 02:48:02 +02003427/*
3428 * Local variables:
3429 * c-indent-level: 8
3430 * c-basic-offset: 8
3431 * End:
3432 */